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; }
75 StringRef getName() const { return MB.getBufferIdentifier(); }
78 // Returns sections. It is a runtime error to call this function
79 // on files that don't have the notion of sections.
80 ArrayRef<InputSectionBase *> getSections() const {
81 assert(FileKind == ObjKind || FileKind == BinaryKind);
85 // Returns object file symbols. It is a runtime error to call this
86 // function on files of other types.
87 ArrayRef<Symbol *> getSymbols() {
88 assert(FileKind == ObjKind || FileKind == BitcodeKind ||
89 FileKind == ArchiveKind);
93 // Filename of .a which contained this file. If this file was
94 // not in an archive file, it is the empty string. We use this
95 // string for creating error messages.
96 StringRef ArchiveName;
98 // If this is an architecture-specific file, the following members
99 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
100 ELFKind EKind = ELFNoneKind;
101 uint16_t EMachine = llvm::ELF::EM_NONE;
104 // Cache for toString(). Only toString() should use this member.
105 mutable std::string ToStringCache;
108 InputFile(Kind K, MemoryBufferRef M);
109 std::vector<InputSectionBase *> Sections;
110 std::vector<Symbol *> Symbols;
116 template <typename ELFT> class ELFFileBase : public InputFile {
118 typedef typename ELFT::Shdr Elf_Shdr;
119 typedef typename ELFT::Sym Elf_Sym;
120 typedef typename ELFT::Word Elf_Word;
121 typedef typename ELFT::SymRange Elf_Sym_Range;
123 ELFFileBase(Kind K, MemoryBufferRef M);
124 static bool classof(const InputFile *F) {
126 return K == ObjKind || K == SharedKind;
129 llvm::object::ELFFile<ELFT> getObj() const {
130 return check(llvm::object::ELFFile<ELFT>::create(MB.getBuffer()));
133 StringRef getStringTable() const { return StringTable; }
135 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
137 Elf_Sym_Range getGlobalELFSyms();
138 Elf_Sym_Range getELFSyms() const { return ELFSyms; }
141 ArrayRef<Elf_Sym> ELFSyms;
142 uint32_t FirstNonLocal = 0;
143 ArrayRef<Elf_Word> SymtabSHNDX;
144 StringRef StringTable;
145 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
149 template <class ELFT> class ObjFile : public ELFFileBase<ELFT> {
150 typedef ELFFileBase<ELFT> Base;
151 typedef typename ELFT::Rel Elf_Rel;
152 typedef typename ELFT::Rela Elf_Rela;
153 typedef typename ELFT::Sym Elf_Sym;
154 typedef typename ELFT::Shdr Elf_Shdr;
155 typedef typename ELFT::Word Elf_Word;
157 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
158 const Elf_Shdr &Sec);
159 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
162 static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; }
164 ArrayRef<Symbol *> getLocalSymbols();
166 ObjFile(MemoryBufferRef M, StringRef ArchiveName);
167 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
169 Symbol &getSymbol(uint32_t SymbolIndex) const {
170 if (SymbolIndex >= this->Symbols.size())
171 fatal(toString(this) + ": invalid symbol index");
172 return *this->Symbols[SymbolIndex];
175 template <typename RelT> Symbol &getRelocTargetSym(const RelT &Rel) const {
176 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
177 return getSymbol(SymIndex);
180 // Returns source line information for a given offset.
181 // If no information is available, returns "".
182 std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
183 llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
184 llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef Name);
186 // MIPS GP0 value defined by this file. This value represents the gp value
187 // used to create the relocatable object and required to support
188 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
189 uint32_t MipsGp0 = 0;
191 // Name of source file obtained from STT_FILE symbol value,
192 // or empty string if there is no such symbol in object file
194 StringRef SourceFile;
198 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
199 void initializeSymbols();
200 void initializeDwarf();
201 InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
202 InputSectionBase *createInputSection(const Elf_Shdr &Sec);
203 StringRef getSectionName(const Elf_Shdr &Sec);
205 bool shouldMerge(const Elf_Shdr &Sec);
206 Symbol *createSymbol(const Elf_Sym *Sym);
208 // .shstrtab contents.
209 StringRef SectionStringTable;
211 // Debugging information to retrieve source file and line for error
212 // reporting. Linker may find reasonable number of errors in a
213 // single object file, so we cache debugging information in order to
214 // parse it only once for each object file we link.
215 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
216 llvm::DenseMap<StringRef, std::pair<unsigned, unsigned>> VariableLoc;
217 llvm::once_flag InitDwarfLine;
220 // LazyObjFile is analogous to ArchiveFile in the sense that
221 // the file contains lazy symbols. The difference is that
222 // LazyObjFile wraps a single file instead of multiple files.
224 // This class is used for --start-lib and --end-lib options which
225 // instruct the linker to link object files between them with the
226 // archive file semantics.
227 class LazyObjFile : public InputFile {
229 LazyObjFile(MemoryBufferRef M, StringRef ArchiveName,
230 uint64_t OffsetInArchive)
231 : InputFile(LazyObjKind, M), OffsetInArchive(OffsetInArchive) {
232 this->ArchiveName = ArchiveName;
235 static bool classof(const InputFile *F) { return F->kind() == LazyObjKind; }
237 template <class ELFT> void parse();
238 MemoryBufferRef getBuffer();
242 std::vector<StringRef> getSymbolNames();
243 template <class ELFT> std::vector<StringRef> getElfSymbols();
244 std::vector<StringRef> getBitcodeSymbols();
247 uint64_t OffsetInArchive;
250 // An ArchiveFile object represents a .a file.
251 class ArchiveFile : public InputFile {
253 explicit ArchiveFile(std::unique_ptr<Archive> &&File);
254 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
255 template <class ELFT> void parse();
257 // Returns a memory buffer for a given symbol and the offset in the archive
258 // for the member. An empty memory buffer and an offset of zero
259 // is returned if we have already returned the same memory buffer.
260 // (So that we don't instantiate same members more than once.)
261 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
264 std::unique_ptr<Archive> File;
265 llvm::DenseSet<uint64_t> Seen;
268 class BitcodeFile : public InputFile {
270 BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
271 uint64_t OffsetInArchive);
272 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
273 template <class ELFT>
274 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
275 std::unique_ptr<llvm::lto::InputFile> Obj;
279 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
280 typedef ELFFileBase<ELFT> Base;
281 typedef typename ELFT::Dyn Elf_Dyn;
282 typedef typename ELFT::Shdr Elf_Shdr;
283 typedef typename ELFT::Sym Elf_Sym;
284 typedef typename ELFT::SymRange Elf_Sym_Range;
285 typedef typename ELFT::Verdef Elf_Verdef;
286 typedef typename ELFT::Versym Elf_Versym;
288 std::vector<StringRef> Undefs;
289 const Elf_Shdr *VersymSec = nullptr;
290 const Elf_Shdr *VerdefSec = nullptr;
293 std::vector<const Elf_Verdef *> Verdefs;
296 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
298 static bool classof(const InputFile *F) {
299 return F->kind() == Base::SharedKind;
302 SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
306 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
309 // The string table offset of the version name in the output file.
312 // The version identifier for this version name.
316 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
317 // data structures in the output file.
318 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
320 // Used for --as-needed
324 class BinaryFile : public InputFile {
326 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
327 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
328 template <class ELFT> void parse();
331 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
332 uint64_t OffsetInArchive = 0);
333 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
335 extern std::vector<BinaryFile *> BinaryFiles;
336 extern std::vector<BitcodeFile *> BitcodeFiles;
337 extern std::vector<InputFile *> ObjectFiles;
338 extern std::vector<InputFile *> SharedFiles;