]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputFiles.h
Upgrade Unbound to 1.8.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / InputFiles.h
1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_ELF_INPUT_FILES_H
11 #define LLD_ELF_INPUT_FILES_H
12
13 #include "Config.h"
14 #include "lld/Common/ErrorHandler.h"
15
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"
26
27 #include <map>
28
29 namespace llvm {
30 class DWARFDebugLine;
31 class TarWriter;
32 struct DILineInfo;
33 namespace lto {
34 class InputFile;
35 }
36 } // namespace llvm
37
38 namespace lld {
39 namespace elf {
40 class InputFile;
41 class InputSectionBase;
42 }
43
44 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
45 std::string toString(const elf::InputFile *F);
46
47 namespace elf {
48
49 using llvm::object::Archive;
50
51 class Lazy;
52 class Symbol;
53
54 // If -reproduce option is given, all input files are written
55 // to this tar archive.
56 extern llvm::TarWriter *Tar;
57
58 // Opens a given file.
59 llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
60
61 // The root class of input files.
62 class InputFile {
63 public:
64   enum Kind {
65     ObjKind,
66     SharedKind,
67     LazyObjKind,
68     ArchiveKind,
69     BitcodeKind,
70     BinaryKind,
71   };
72
73   Kind kind() const { return FileKind; }
74
75   bool isElf() const {
76     Kind K = kind();
77     return K == ObjKind || K == SharedKind;
78   }
79
80   StringRef getName() const { return MB.getBufferIdentifier(); }
81   MemoryBufferRef MB;
82
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);
87     return Sections;
88   }
89
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);
95     return Symbols;
96   }
97
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;
102
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;
107   uint8_t OSABI = 0;
108
109   // Cache for toString(). Only toString() should use this member.
110   mutable std::string ToStringCache;
111
112   std::string getSrcMsg(const Symbol &Sym, InputSectionBase &Sec,
113                         uint64_t Offset);
114
115 protected:
116   InputFile(Kind K, MemoryBufferRef M);
117   std::vector<InputSectionBase *> Sections;
118   std::vector<Symbol *> Symbols;
119
120 private:
121   const Kind FileKind;
122 };
123
124 template <typename ELFT> class ELFFileBase : public InputFile {
125 public:
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;
130
131   ELFFileBase(Kind K, MemoryBufferRef M);
132   static bool classof(const InputFile *F) { return F->isElf(); }
133
134   llvm::object::ELFFile<ELFT> getObj() const {
135     return check(llvm::object::ELFFile<ELFT>::create(MB.getBuffer()));
136   }
137
138   StringRef getStringTable() const { return StringTable; }
139
140   uint32_t getSectionIndex(const Elf_Sym &Sym) const;
141
142   Elf_Sym_Range getGlobalELFSyms();
143   Elf_Sym_Range getELFSyms() const { return ELFSyms; }
144
145 protected:
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);
151 };
152
153 // .o file.
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;
161
162   StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
163                                  const Elf_Shdr &Sec);
164   ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
165
166 public:
167   static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; }
168
169   ArrayRef<Symbol *> getLocalSymbols();
170   ArrayRef<Symbol *> getGlobalSymbols();
171
172   ObjFile(MemoryBufferRef M, StringRef ArchiveName);
173   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
174
175   Symbol &getSymbol(uint32_t SymbolIndex) const {
176     if (SymbolIndex >= this->Symbols.size())
177       fatal(toString(this) + ": invalid symbol index");
178     return *this->Symbols[SymbolIndex];
179   }
180
181   template <typename RelT> Symbol &getRelocTargetSym(const RelT &Rel) const {
182     uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
183     return getSymbol(SymIndex);
184   }
185
186   // Returns source line information for a given offset.
187   // If no information is available, returns "".
188   std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
189   llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
190   llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef Name);
191
192   // MIPS GP0 value defined by this file. This value represents the gp value
193   // used to create the relocatable object and required to support
194   // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
195   uint32_t MipsGp0 = 0;
196
197   // Name of source file obtained from STT_FILE symbol value,
198   // or empty string if there is no such symbol in object file
199   // symbol table.
200   StringRef SourceFile;
201
202 private:
203   void
204   initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
205   void initializeSymbols();
206   void initializeDwarf();
207   InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
208   InputSectionBase *createInputSection(const Elf_Shdr &Sec);
209   StringRef getSectionName(const Elf_Shdr &Sec);
210
211   bool shouldMerge(const Elf_Shdr &Sec);
212   Symbol *createSymbol(const Elf_Sym *Sym);
213
214   // .shstrtab contents.
215   StringRef SectionStringTable;
216
217   // Debugging information to retrieve source file and line for error
218   // reporting. Linker may find reasonable number of errors in a
219   // single object file, so we cache debugging information in order to
220   // parse it only once for each object file we link.
221   std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
222   llvm::DenseMap<StringRef, std::pair<unsigned, unsigned>> VariableLoc;
223   llvm::once_flag InitDwarfLine;
224 };
225
226 // LazyObjFile is analogous to ArchiveFile in the sense that
227 // the file contains lazy symbols. The difference is that
228 // LazyObjFile wraps a single file instead of multiple files.
229 //
230 // This class is used for --start-lib and --end-lib options which
231 // instruct the linker to link object files between them with the
232 // archive file semantics.
233 class LazyObjFile : public InputFile {
234 public:
235   LazyObjFile(MemoryBufferRef M, StringRef ArchiveName,
236               uint64_t OffsetInArchive)
237       : InputFile(LazyObjKind, M), OffsetInArchive(OffsetInArchive) {
238     this->ArchiveName = ArchiveName;
239   }
240
241   static bool classof(const InputFile *F) { return F->kind() == LazyObjKind; }
242
243   template <class ELFT> void parse();
244   MemoryBufferRef getBuffer();
245   InputFile *fetch();
246
247 private:
248   std::vector<StringRef> getSymbolNames();
249   template <class ELFT> std::vector<StringRef> getElfSymbols();
250   std::vector<StringRef> getBitcodeSymbols();
251
252   bool Seen = false;
253   uint64_t OffsetInArchive;
254 };
255
256 // An ArchiveFile object represents a .a file.
257 class ArchiveFile : public InputFile {
258 public:
259   explicit ArchiveFile(std::unique_ptr<Archive> &&File);
260   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
261   template <class ELFT> void parse();
262
263   // Returns a memory buffer for a given symbol and the offset in the archive
264   // for the member. An empty memory buffer and an offset of zero
265   // is returned if we have already returned the same memory buffer.
266   // (So that we don't instantiate same members more than once.)
267   std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
268
269 private:
270   std::unique_ptr<Archive> File;
271   llvm::DenseSet<uint64_t> Seen;
272 };
273
274 class BitcodeFile : public InputFile {
275 public:
276   BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
277               uint64_t OffsetInArchive);
278   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
279   template <class ELFT>
280   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
281   std::unique_ptr<llvm::lto::InputFile> Obj;
282 };
283
284 // .so file.
285 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
286   typedef ELFFileBase<ELFT> Base;
287   typedef typename ELFT::Dyn Elf_Dyn;
288   typedef typename ELFT::Shdr Elf_Shdr;
289   typedef typename ELFT::Sym Elf_Sym;
290   typedef typename ELFT::SymRange Elf_Sym_Range;
291   typedef typename ELFT::Verdef Elf_Verdef;
292   typedef typename ELFT::Versym Elf_Versym;
293
294   std::vector<StringRef> Undefs;
295   const Elf_Shdr *VersymSec = nullptr;
296   const Elf_Shdr *VerdefSec = nullptr;
297
298 public:
299   std::vector<const Elf_Verdef *> Verdefs;
300   std::string SoName;
301
302   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
303
304   static bool classof(const InputFile *F) {
305     return F->kind() == Base::SharedKind;
306   }
307
308   SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
309
310   void parseSoName();
311   void parseRest();
312   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
313
314   struct NeededVer {
315     // The string table offset of the version name in the output file.
316     size_t StrTab;
317
318     // The version identifier for this version name.
319     uint16_t Index;
320   };
321
322   // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
323   // data structures in the output file.
324   std::map<const Elf_Verdef *, NeededVer> VerdefMap;
325
326   // Used for --as-needed
327   bool IsNeeded;
328 };
329
330 class BinaryFile : public InputFile {
331 public:
332   explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
333   static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
334   void parse();
335 };
336
337 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
338                             uint64_t OffsetInArchive = 0);
339 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
340
341 extern std::vector<BinaryFile *> BinaryFiles;
342 extern std::vector<BitcodeFile *> BitcodeFiles;
343 extern std::vector<InputFile *> ObjectFiles;
344 extern std::vector<InputFile *> SharedFiles;
345
346 } // namespace elf
347 } // namespace lld
348
349 #endif