]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputFiles.h
Merge lld trunk r321414 to contrib/llvm/tools/lld.
[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
171   ObjFile(MemoryBufferRef M, StringRef ArchiveName);
172   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
173
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];
178   }
179
180   template <typename RelT> Symbol &getRelocTargetSym(const RelT &Rel) const {
181     uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
182     return getSymbol(SymIndex);
183   }
184
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);
190
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;
195
196   // Name of source file obtained from STT_FILE symbol value,
197   // or empty string if there is no such symbol in object file
198   // symbol table.
199   StringRef SourceFile;
200
201 private:
202   void
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);
209
210   bool shouldMerge(const Elf_Shdr &Sec);
211   Symbol *createSymbol(const Elf_Sym *Sym);
212
213   // .shstrtab contents.
214   StringRef SectionStringTable;
215
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;
223 };
224
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.
228 //
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 {
233 public:
234   LazyObjFile(MemoryBufferRef M, StringRef ArchiveName,
235               uint64_t OffsetInArchive)
236       : InputFile(LazyObjKind, M), OffsetInArchive(OffsetInArchive) {
237     this->ArchiveName = ArchiveName;
238   }
239
240   static bool classof(const InputFile *F) { return F->kind() == LazyObjKind; }
241
242   template <class ELFT> void parse();
243   MemoryBufferRef getBuffer();
244   InputFile *fetch();
245
246 private:
247   std::vector<StringRef> getSymbolNames();
248   template <class ELFT> std::vector<StringRef> getElfSymbols();
249   std::vector<StringRef> getBitcodeSymbols();
250
251   bool Seen = false;
252   uint64_t OffsetInArchive;
253 };
254
255 // An ArchiveFile object represents a .a file.
256 class ArchiveFile : public InputFile {
257 public:
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();
261
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);
267
268 private:
269   std::unique_ptr<Archive> File;
270   llvm::DenseSet<uint64_t> Seen;
271 };
272
273 class BitcodeFile : public InputFile {
274 public:
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;
281 };
282
283 // .so file.
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;
292
293   std::vector<StringRef> Undefs;
294   const Elf_Shdr *VersymSec = nullptr;
295   const Elf_Shdr *VerdefSec = nullptr;
296
297 public:
298   std::vector<const Elf_Verdef *> Verdefs;
299   std::string SoName;
300
301   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
302
303   static bool classof(const InputFile *F) {
304     return F->kind() == Base::SharedKind;
305   }
306
307   SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
308
309   void parseSoName();
310   void parseRest();
311   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
312
313   struct NeededVer {
314     // The string table offset of the version name in the output file.
315     size_t StrTab;
316
317     // The version identifier for this version name.
318     uint16_t Index;
319   };
320
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;
324
325   // Used for --as-needed
326   bool IsNeeded;
327 };
328
329 class BinaryFile : public InputFile {
330 public:
331   explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
332   static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
333   void parse();
334 };
335
336 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
337                             uint64_t OffsetInArchive = 0);
338 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
339
340 extern std::vector<BinaryFile *> BinaryFiles;
341 extern std::vector<BitcodeFile *> BitcodeFiles;
342 extern std::vector<InputFile *> ObjectFiles;
343 extern std::vector<InputFile *> SharedFiles;
344
345 } // namespace elf
346 } // namespace lld
347
348 #endif