1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_INPUT_FILES_H
11 #define LLD_ELF_INPUT_FILES_H
14 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/LLVM.h"
17 #include "lld/Common/Reproduce.h"
18 #include "llvm/ADT/CachedHashString.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/IR/Comdat.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/ELF.h"
24 #include "llvm/Object/IRObjectFile.h"
25 #include "llvm/Support/Threading.h"
41 class InputSectionBase;
44 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
45 std::string toString(const elf::InputFile *F);
49 using llvm::object::Archive;
54 // If -reproduce option is given, all input files are written
55 // to this tar archive.
56 extern llvm::TarWriter *Tar;
58 // Opens a given file.
59 llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
61 // The root class of input files.
73 Kind kind() const { return FileKind; }
77 return K == ObjKind || K == SharedKind;
80 StringRef getName() const { return MB.getBufferIdentifier(); }
83 // Returns sections. It is a runtime error to call this function
84 // on files that don't have the notion of sections.
85 ArrayRef<InputSectionBase *> getSections() const {
86 assert(FileKind == ObjKind || FileKind == BinaryKind);
90 // Returns object file symbols. It is a runtime error to call this
91 // function on files of other types.
92 ArrayRef<Symbol *> getSymbols() {
93 assert(FileKind == ObjKind || FileKind == BitcodeKind ||
94 FileKind == ArchiveKind);
98 // Filename of .a which contained this file. If this file was
99 // not in an archive file, it is the empty string. We use this
100 // string for creating error messages.
101 StringRef ArchiveName;
103 // If this is an architecture-specific file, the following members
104 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
105 ELFKind EKind = ELFNoneKind;
106 uint16_t EMachine = llvm::ELF::EM_NONE;
109 // Cache for toString(). Only toString() should use this member.
110 mutable std::string ToStringCache;
112 std::string getSrcMsg(const Symbol &Sym, InputSectionBase &Sec,
116 InputFile(Kind K, MemoryBufferRef M);
117 std::vector<InputSectionBase *> Sections;
118 std::vector<Symbol *> Symbols;
124 template <typename ELFT> class ELFFileBase : public InputFile {
126 typedef typename ELFT::Shdr Elf_Shdr;
127 typedef typename ELFT::Sym Elf_Sym;
128 typedef typename ELFT::Word Elf_Word;
129 typedef typename ELFT::SymRange Elf_Sym_Range;
131 ELFFileBase(Kind K, MemoryBufferRef M);
132 static bool classof(const InputFile *F) { return F->isElf(); }
134 llvm::object::ELFFile<ELFT> getObj() const {
135 return check(llvm::object::ELFFile<ELFT>::create(MB.getBuffer()));
138 StringRef getStringTable() const { return StringTable; }
140 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
142 Elf_Sym_Range getGlobalELFSyms();
143 Elf_Sym_Range getELFSyms() const { return ELFSyms; }
146 ArrayRef<Elf_Sym> ELFSyms;
147 uint32_t FirstNonLocal = 0;
148 ArrayRef<Elf_Word> SymtabSHNDX;
149 StringRef StringTable;
150 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
154 template <class ELFT> class ObjFile : public ELFFileBase<ELFT> {
155 typedef ELFFileBase<ELFT> Base;
156 typedef typename ELFT::Rel Elf_Rel;
157 typedef typename ELFT::Rela Elf_Rela;
158 typedef typename ELFT::Sym Elf_Sym;
159 typedef typename ELFT::Shdr Elf_Shdr;
160 typedef typename ELFT::Word Elf_Word;
162 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
163 const Elf_Shdr &Sec);
164 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
167 static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; }
169 ArrayRef<Symbol *> getLocalSymbols();
171 ObjFile(MemoryBufferRef M, StringRef ArchiveName);
172 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
174 Symbol &getSymbol(uint32_t SymbolIndex) const {
175 if (SymbolIndex >= this->Symbols.size())
176 fatal(toString(this) + ": invalid symbol index");
177 return *this->Symbols[SymbolIndex];
180 template <typename RelT> Symbol &getRelocTargetSym(const RelT &Rel) const {
181 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
182 return getSymbol(SymIndex);
185 // Returns source line information for a given offset.
186 // If no information is available, returns "".
187 std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
188 llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
189 llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef Name);
191 // MIPS GP0 value defined by this file. This value represents the gp value
192 // used to create the relocatable object and required to support
193 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
194 uint32_t MipsGp0 = 0;
196 // Name of source file obtained from STT_FILE symbol value,
197 // or empty string if there is no such symbol in object file
199 StringRef SourceFile;
203 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
204 void initializeSymbols();
205 void initializeDwarf();
206 InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
207 InputSectionBase *createInputSection(const Elf_Shdr &Sec);
208 StringRef getSectionName(const Elf_Shdr &Sec);
210 bool shouldMerge(const Elf_Shdr &Sec);
211 Symbol *createSymbol(const Elf_Sym *Sym);
213 // .shstrtab contents.
214 StringRef SectionStringTable;
216 // Debugging information to retrieve source file and line for error
217 // reporting. Linker may find reasonable number of errors in a
218 // single object file, so we cache debugging information in order to
219 // parse it only once for each object file we link.
220 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
221 llvm::DenseMap<StringRef, std::pair<unsigned, unsigned>> VariableLoc;
222 llvm::once_flag InitDwarfLine;
225 // LazyObjFile is analogous to ArchiveFile in the sense that
226 // the file contains lazy symbols. The difference is that
227 // LazyObjFile wraps a single file instead of multiple files.
229 // This class is used for --start-lib and --end-lib options which
230 // instruct the linker to link object files between them with the
231 // archive file semantics.
232 class LazyObjFile : public InputFile {
234 LazyObjFile(MemoryBufferRef M, StringRef ArchiveName,
235 uint64_t OffsetInArchive)
236 : InputFile(LazyObjKind, M), OffsetInArchive(OffsetInArchive) {
237 this->ArchiveName = ArchiveName;
240 static bool classof(const InputFile *F) { return F->kind() == LazyObjKind; }
242 template <class ELFT> void parse();
243 MemoryBufferRef getBuffer();
247 std::vector<StringRef> getSymbolNames();
248 template <class ELFT> std::vector<StringRef> getElfSymbols();
249 std::vector<StringRef> getBitcodeSymbols();
252 uint64_t OffsetInArchive;
255 // An ArchiveFile object represents a .a file.
256 class ArchiveFile : public InputFile {
258 explicit ArchiveFile(std::unique_ptr<Archive> &&File);
259 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
260 template <class ELFT> void parse();
262 // Returns a memory buffer for a given symbol and the offset in the archive
263 // for the member. An empty memory buffer and an offset of zero
264 // is returned if we have already returned the same memory buffer.
265 // (So that we don't instantiate same members more than once.)
266 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
269 std::unique_ptr<Archive> File;
270 llvm::DenseSet<uint64_t> Seen;
273 class BitcodeFile : public InputFile {
275 BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
276 uint64_t OffsetInArchive);
277 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
278 template <class ELFT>
279 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
280 std::unique_ptr<llvm::lto::InputFile> Obj;
284 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
285 typedef ELFFileBase<ELFT> Base;
286 typedef typename ELFT::Dyn Elf_Dyn;
287 typedef typename ELFT::Shdr Elf_Shdr;
288 typedef typename ELFT::Sym Elf_Sym;
289 typedef typename ELFT::SymRange Elf_Sym_Range;
290 typedef typename ELFT::Verdef Elf_Verdef;
291 typedef typename ELFT::Versym Elf_Versym;
293 std::vector<StringRef> Undefs;
294 const Elf_Shdr *VersymSec = nullptr;
295 const Elf_Shdr *VerdefSec = nullptr;
298 std::vector<const Elf_Verdef *> Verdefs;
301 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
303 static bool classof(const InputFile *F) {
304 return F->kind() == Base::SharedKind;
307 SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
311 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
314 // The string table offset of the version name in the output file.
317 // The version identifier for this version name.
321 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
322 // data structures in the output file.
323 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
325 // Used for --as-needed
329 class BinaryFile : public InputFile {
331 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
332 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
336 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
337 uint64_t OffsetInArchive = 0);
338 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
340 extern std::vector<BinaryFile *> BinaryFiles;
341 extern std::vector<BitcodeFile *> BitcodeFiles;
342 extern std::vector<InputFile *> ObjectFiles;
343 extern std::vector<InputFile *> SharedFiles;