]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputFiles.h
Merge lld trunk r321017 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   StringRef getName() const { return MB.getBufferIdentifier(); }
76   MemoryBufferRef MB;
77
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);
82     return Sections;
83   }
84
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);
90     return Symbols;
91   }
92
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;
97
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;
102   uint8_t OSABI = 0;
103
104   // Cache for toString(). Only toString() should use this member.
105   mutable std::string ToStringCache;
106
107 protected:
108   InputFile(Kind K, MemoryBufferRef M);
109   std::vector<InputSectionBase *> Sections;
110   std::vector<Symbol *> Symbols;
111
112 private:
113   const Kind FileKind;
114 };
115
116 template <typename ELFT> class ELFFileBase : public InputFile {
117 public:
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;
122
123   ELFFileBase(Kind K, MemoryBufferRef M);
124   static bool classof(const InputFile *F) {
125     Kind K = F->kind();
126     return K == ObjKind || K == SharedKind;
127   }
128
129   llvm::object::ELFFile<ELFT> getObj() const {
130     return check(llvm::object::ELFFile<ELFT>::create(MB.getBuffer()));
131   }
132
133   StringRef getStringTable() const { return StringTable; }
134
135   uint32_t getSectionIndex(const Elf_Sym &Sym) const;
136
137   Elf_Sym_Range getGlobalELFSyms();
138   Elf_Sym_Range getELFSyms() const { return ELFSyms; }
139
140 protected:
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);
146 };
147
148 // .o file.
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;
156
157   StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
158                                  const Elf_Shdr &Sec);
159   ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
160
161 public:
162   static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; }
163
164   ArrayRef<Symbol *> getLocalSymbols();
165
166   ObjFile(MemoryBufferRef M, StringRef ArchiveName);
167   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
168
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];
173   }
174
175   template <typename RelT> Symbol &getRelocTargetSym(const RelT &Rel) const {
176     uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
177     return getSymbol(SymIndex);
178   }
179
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);
185
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;
190
191   // Name of source file obtained from STT_FILE symbol value,
192   // or empty string if there is no such symbol in object file
193   // symbol table.
194   StringRef SourceFile;
195
196 private:
197   void
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);
204
205   bool shouldMerge(const Elf_Shdr &Sec);
206   Symbol *createSymbol(const Elf_Sym *Sym);
207
208   // .shstrtab contents.
209   StringRef SectionStringTable;
210
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;
218 };
219
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.
223 //
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 {
228 public:
229   LazyObjFile(MemoryBufferRef M, StringRef ArchiveName,
230               uint64_t OffsetInArchive)
231       : InputFile(LazyObjKind, M), OffsetInArchive(OffsetInArchive) {
232     this->ArchiveName = ArchiveName;
233   }
234
235   static bool classof(const InputFile *F) { return F->kind() == LazyObjKind; }
236
237   template <class ELFT> void parse();
238   MemoryBufferRef getBuffer();
239   InputFile *fetch();
240
241 private:
242   std::vector<StringRef> getSymbolNames();
243   template <class ELFT> std::vector<StringRef> getElfSymbols();
244   std::vector<StringRef> getBitcodeSymbols();
245
246   bool Seen = false;
247   uint64_t OffsetInArchive;
248 };
249
250 // An ArchiveFile object represents a .a file.
251 class ArchiveFile : public InputFile {
252 public:
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();
256
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);
262
263 private:
264   std::unique_ptr<Archive> File;
265   llvm::DenseSet<uint64_t> Seen;
266 };
267
268 class BitcodeFile : public InputFile {
269 public:
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;
276 };
277
278 // .so file.
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;
287
288   std::vector<StringRef> Undefs;
289   const Elf_Shdr *VersymSec = nullptr;
290   const Elf_Shdr *VerdefSec = nullptr;
291
292 public:
293   std::vector<const Elf_Verdef *> Verdefs;
294   std::string SoName;
295
296   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
297
298   static bool classof(const InputFile *F) {
299     return F->kind() == Base::SharedKind;
300   }
301
302   SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
303
304   void parseSoName();
305   void parseRest();
306   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
307
308   struct NeededVer {
309     // The string table offset of the version name in the output file.
310     size_t StrTab;
311
312     // The version identifier for this version name.
313     uint16_t Index;
314   };
315
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;
319
320   // Used for --as-needed
321   bool IsNeeded;
322 };
323
324 class BinaryFile : public InputFile {
325 public:
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();
329 };
330
331 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
332                             uint64_t OffsetInArchive = 0);
333 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
334
335 extern std::vector<BinaryFile *> BinaryFiles;
336 extern std::vector<BitcodeFile *> BitcodeFiles;
337 extern std::vector<InputFile *> ObjectFiles;
338 extern std::vector<InputFile *> SharedFiles;
339
340 } // namespace elf
341 } // namespace lld
342
343 #endif