]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputFiles.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r308421, and update
[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 "Error.h"
15 #include "InputSection.h"
16 #include "Symbols.h"
17
18 #include "lld/Core/LLVM.h"
19 #include "lld/Core/Reproduce.h"
20 #include "llvm/ADT/CachedHashString.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/IR/Comdat.h"
24 #include "llvm/Object/Archive.h"
25 #include "llvm/Object/ELF.h"
26 #include "llvm/Object/IRObjectFile.h"
27
28 #include <map>
29
30 namespace llvm {
31 class DWARFDebugLine;
32 class TarWriter;
33 struct DILineInfo;
34 namespace lto {
35 class InputFile;
36 }
37 } // namespace llvm
38
39 namespace lld {
40 namespace elf {
41 class InputFile;
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 SymbolBody;
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     ObjectKind,
66     SharedKind,
67     LazyObjectKind,
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 == ObjectKind || FileKind == BinaryKind);
82     return Sections;
83   }
84
85   // Filename of .a which contained this file. If this file was
86   // not in an archive file, it is the empty string. We use this
87   // string for creating error messages.
88   StringRef ArchiveName;
89
90   // If this is an architecture-specific file, the following members
91   // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
92   ELFKind EKind = ELFNoneKind;
93   uint16_t EMachine = llvm::ELF::EM_NONE;
94   uint8_t OSABI = 0;
95
96   // Cache for toString(). Only toString() should use this member.
97   mutable std::string ToStringCache;
98
99 protected:
100   InputFile(Kind K, MemoryBufferRef M);
101   std::vector<InputSectionBase *> Sections;
102
103 private:
104   const Kind FileKind;
105 };
106
107 template <typename ELFT> class ELFFileBase : public InputFile {
108 public:
109   typedef typename ELFT::Shdr Elf_Shdr;
110   typedef typename ELFT::Sym Elf_Sym;
111   typedef typename ELFT::Word Elf_Word;
112   typedef typename ELFT::SymRange Elf_Sym_Range;
113
114   ELFFileBase(Kind K, MemoryBufferRef M);
115   static bool classof(const InputFile *F) {
116     Kind K = F->kind();
117     return K == ObjectKind || K == SharedKind;
118   }
119
120   llvm::object::ELFFile<ELFT> getObj() const {
121     return llvm::object::ELFFile<ELFT>(MB.getBuffer());
122   }
123
124   StringRef getStringTable() const { return StringTable; }
125
126   uint32_t getSectionIndex(const Elf_Sym &Sym) const;
127
128   Elf_Sym_Range getGlobalSymbols();
129
130 protected:
131   ArrayRef<Elf_Sym> Symbols;
132   uint32_t FirstNonLocal = 0;
133   ArrayRef<Elf_Word> SymtabSHNDX;
134   StringRef StringTable;
135   void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
136 };
137
138 // .o file.
139 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
140   typedef ELFFileBase<ELFT> Base;
141   typedef typename ELFT::Rel Elf_Rel;
142   typedef typename ELFT::Rela Elf_Rela;
143   typedef typename ELFT::Sym Elf_Sym;
144   typedef typename ELFT::Shdr Elf_Shdr;
145   typedef typename ELFT::Word Elf_Word;
146
147   StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
148                                  const Elf_Shdr &Sec);
149   ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
150
151 public:
152   static bool classof(const InputFile *F) {
153     return F->kind() == Base::ObjectKind;
154   }
155
156   ArrayRef<SymbolBody *> getSymbols();
157   ArrayRef<SymbolBody *> getLocalSymbols();
158
159   ObjectFile(MemoryBufferRef M, StringRef ArchiveName);
160   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
161
162   InputSectionBase *getSection(const Elf_Sym &Sym) const;
163
164   SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
165     if (SymbolIndex >= SymbolBodies.size())
166       fatal(toString(this) + ": invalid symbol index");
167     return *SymbolBodies[SymbolIndex];
168   }
169
170   template <typename RelT>
171   SymbolBody &getRelocTargetSym(const RelT &Rel) const {
172     uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
173     return getSymbolBody(SymIndex);
174   }
175
176   // Returns source line information for a given offset.
177   // If no information is available, returns "".
178   std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
179   llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
180
181   // MIPS GP0 value defined by this file. This value represents the gp value
182   // used to create the relocatable object and required to support
183   // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
184   uint32_t MipsGp0 = 0;
185
186   // Name of source file obtained from STT_FILE symbol value,
187   // or empty string if there is no such symbol in object file
188   // symbol table.
189   StringRef SourceFile;
190
191 private:
192   void
193   initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
194   void initializeSymbols();
195   void initializeDwarfLine();
196   InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
197   InputSectionBase *createInputSection(const Elf_Shdr &Sec);
198   StringRef getSectionName(const Elf_Shdr &Sec);
199
200   bool shouldMerge(const Elf_Shdr &Sec);
201   SymbolBody *createSymbolBody(const Elf_Sym *Sym);
202
203   // List of all symbols referenced or defined by this file.
204   std::vector<SymbolBody *> SymbolBodies;
205
206   // .shstrtab contents.
207   StringRef SectionStringTable;
208
209   // Debugging information to retrieve source file and line for error
210   // reporting. Linker may find reasonable number of errors in a
211   // single object file, so we cache debugging information in order to
212   // parse it only once for each object file we link.
213   std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
214 };
215
216 // LazyObjectFile is analogous to ArchiveFile in the sense that
217 // the file contains lazy symbols. The difference is that
218 // LazyObjectFile wraps a single file instead of multiple files.
219 //
220 // This class is used for --start-lib and --end-lib options which
221 // instruct the linker to link object files between them with the
222 // archive file semantics.
223 class LazyObjectFile : public InputFile {
224 public:
225   LazyObjectFile(MemoryBufferRef M, StringRef ArchiveName,
226                  uint64_t OffsetInArchive)
227       : InputFile(LazyObjectKind, M), OffsetInArchive(OffsetInArchive) {
228     this->ArchiveName = ArchiveName;
229   }
230
231   static bool classof(const InputFile *F) {
232     return F->kind() == LazyObjectKind;
233   }
234
235   template <class ELFT> void parse();
236   MemoryBufferRef getBuffer();
237   InputFile *fetch();
238
239 private:
240   std::vector<StringRef> getSymbols();
241   template <class ELFT> std::vector<StringRef> getElfSymbols();
242   std::vector<StringRef> getBitcodeSymbols();
243
244   bool Seen = false;
245   uint64_t OffsetInArchive;
246 };
247
248 // An ArchiveFile object represents a .a file.
249 class ArchiveFile : public InputFile {
250 public:
251   explicit ArchiveFile(std::unique_ptr<Archive> &&File);
252   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
253   template <class ELFT> void parse();
254   ArrayRef<Symbol *> getSymbols() { return Symbols; }
255
256   // Returns a memory buffer for a given symbol and the offset in the archive
257   // for the member. An empty memory buffer and an offset of zero
258   // is returned if we have already returned the same memory buffer.
259   // (So that we don't instantiate same members more than once.)
260   std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
261
262 private:
263   std::unique_ptr<Archive> File;
264   llvm::DenseSet<uint64_t> Seen;
265   std::vector<Symbol *> Symbols;
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   ArrayRef<Symbol *> getSymbols() { return Symbols; }
276   std::unique_ptr<llvm::lto::InputFile> Obj;
277
278 private:
279   std::vector<Symbol *> Symbols;
280 };
281
282 // .so file.
283 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
284   typedef ELFFileBase<ELFT> Base;
285   typedef typename ELFT::Dyn Elf_Dyn;
286   typedef typename ELFT::Shdr Elf_Shdr;
287   typedef typename ELFT::Sym Elf_Sym;
288   typedef typename ELFT::SymRange Elf_Sym_Range;
289   typedef typename ELFT::Verdef Elf_Verdef;
290   typedef typename ELFT::Versym Elf_Versym;
291
292   std::vector<StringRef> Undefs;
293   const Elf_Shdr *VersymSec = nullptr;
294   const Elf_Shdr *VerdefSec = nullptr;
295
296 public:
297   std::string SoName;
298
299   const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
300   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
301
302   static bool classof(const InputFile *F) {
303     return F->kind() == Base::SharedKind;
304   }
305
306   SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
307
308   void parseSoName();
309   void parseRest();
310   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
311
312   struct NeededVer {
313     // The string table offset of the version name in the output file.
314     size_t StrTab;
315
316     // The version identifier for this version name.
317     uint16_t Index;
318   };
319
320   // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
321   // data structures in the output file.
322   std::map<const Elf_Verdef *, NeededVer> VerdefMap;
323
324   // Used for --as-needed
325   bool AsNeeded = false;
326   bool IsUsed = false;
327   bool isNeeded() const { return !AsNeeded || IsUsed; }
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   template <class ELFT> 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 } // namespace elf
342 } // namespace lld
343
344 #endif