]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputFiles.h
Merge compiler-rt trunk r300890, and update build glue.
[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 "InputSection.h"
15 #include "Error.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 }
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   // For SharedKind inputs, the string to use in DT_NEEDED when the library
97   // has no soname.
98   std::string DefaultSoName;
99
100   // Cache for toString(). Only toString() should use this member.
101   mutable std::string ToStringCache;
102
103 protected:
104   InputFile(Kind K, MemoryBufferRef M);
105   std::vector<InputSectionBase *> Sections;
106
107 private:
108   const Kind FileKind;
109 };
110
111 template <typename ELFT> class ELFFileBase : public InputFile {
112 public:
113   typedef typename ELFT::Shdr Elf_Shdr;
114   typedef typename ELFT::Sym Elf_Sym;
115   typedef typename ELFT::Word Elf_Word;
116   typedef typename ELFT::SymRange Elf_Sym_Range;
117
118   ELFFileBase(Kind K, MemoryBufferRef M);
119   static bool classof(const InputFile *F) {
120     Kind K = F->kind();
121     return K == ObjectKind || K == SharedKind;
122   }
123
124   llvm::object::ELFFile<ELFT> getObj() const {
125     return llvm::object::ELFFile<ELFT>(MB.getBuffer());
126   }
127
128   StringRef getStringTable() const { return StringTable; }
129
130   uint32_t getSectionIndex(const Elf_Sym &Sym) const;
131
132   Elf_Sym_Range getGlobalSymbols();
133
134 protected:
135   ArrayRef<Elf_Sym> Symbols;
136   uint32_t FirstNonLocal = 0;
137   ArrayRef<Elf_Word> SymtabSHNDX;
138   StringRef StringTable;
139   void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
140 };
141
142 // .o file.
143 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
144   typedef ELFFileBase<ELFT> Base;
145   typedef typename ELFT::Rel Elf_Rel;
146   typedef typename ELFT::Rela Elf_Rela;
147   typedef typename ELFT::Sym Elf_Sym;
148   typedef typename ELFT::Shdr Elf_Shdr;
149   typedef typename ELFT::Word Elf_Word;
150
151   StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
152                                  const Elf_Shdr &Sec);
153   ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
154
155 public:
156   static bool classof(const InputFile *F) {
157     return F->kind() == Base::ObjectKind;
158   }
159
160   ArrayRef<SymbolBody *> getSymbols();
161   ArrayRef<SymbolBody *> getLocalSymbols();
162
163   explicit ObjectFile(MemoryBufferRef M);
164   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
165
166   InputSectionBase *getSection(const Elf_Sym &Sym) const;
167
168   SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
169     if (SymbolIndex >= SymbolBodies.size())
170       fatal(toString(this) + ": invalid symbol index");
171     return *SymbolBodies[SymbolIndex];
172   }
173
174   template <typename RelT>
175   SymbolBody &getRelocTargetSym(const RelT &Rel) const {
176     uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
177     return getSymbolBody(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
185   // MIPS GP0 value defined by this file. This value represents the gp value
186   // used to create the relocatable object and required to support
187   // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
188   uint32_t MipsGp0 = 0;
189
190   // Name of source file obtained from STT_FILE symbol value,
191   // or empty string if there is no such symbol in object file
192   // symbol table.
193   StringRef SourceFile;
194
195 private:
196   void
197   initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
198   void initializeSymbols();
199   void initializeDwarfLine();
200   InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
201   InputSectionBase *createInputSection(const Elf_Shdr &Sec,
202                                        StringRef SectionStringTable);
203
204   bool shouldMerge(const Elf_Shdr &Sec);
205   SymbolBody *createSymbolBody(const Elf_Sym *Sym);
206
207   // List of all symbols referenced or defined by this file.
208   std::vector<SymbolBody *> SymbolBodies;
209
210   // Debugging information to retrieve source file and line for error
211   // reporting. Linker may find reasonable number of errors in a
212   // single object file, so we cache debugging information in order to
213   // parse it only once for each object file we link.
214   std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
215 };
216
217 // LazyObjectFile is analogous to ArchiveFile in the sense that
218 // the file contains lazy symbols. The difference is that
219 // LazyObjectFile wraps a single file instead of multiple files.
220 //
221 // This class is used for --start-lib and --end-lib options which
222 // instruct the linker to link object files between them with the
223 // archive file semantics.
224 class LazyObjectFile : public InputFile {
225 public:
226   explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
227
228   static bool classof(const InputFile *F) {
229     return F->kind() == LazyObjectKind;
230   }
231
232   template <class ELFT> void parse();
233   MemoryBufferRef getBuffer();
234
235 private:
236   std::vector<StringRef> getSymbols();
237   template <class ELFT> std::vector<StringRef> getElfSymbols();
238   std::vector<StringRef> getBitcodeSymbols();
239
240   bool Seen = false;
241 };
242
243 // An ArchiveFile object represents a .a file.
244 class ArchiveFile : public InputFile {
245 public:
246   explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
247   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
248   template <class ELFT> void parse();
249
250   // Returns a memory buffer for a given symbol and the offset in the archive
251   // for the member. An empty memory buffer and an offset of zero
252   // is returned if we have already returned the same memory buffer.
253   // (So that we don't instantiate same members more than once.)
254   std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
255
256 private:
257   std::unique_ptr<Archive> File;
258   llvm::DenseSet<uint64_t> Seen;
259 };
260
261 class BitcodeFile : public InputFile {
262 public:
263   BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
264               uint64_t OffsetInArchive);
265   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
266   template <class ELFT>
267   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
268   ArrayRef<Symbol *> getSymbols() { return Symbols; }
269   std::unique_ptr<llvm::lto::InputFile> Obj;
270
271 private:
272   std::vector<Symbol *> Symbols;
273 };
274
275 // .so file.
276 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
277   typedef ELFFileBase<ELFT> Base;
278   typedef typename ELFT::Dyn Elf_Dyn;
279   typedef typename ELFT::Shdr Elf_Shdr;
280   typedef typename ELFT::Sym Elf_Sym;
281   typedef typename ELFT::SymRange Elf_Sym_Range;
282   typedef typename ELFT::Verdef Elf_Verdef;
283   typedef typename ELFT::Versym Elf_Versym;
284
285   std::vector<StringRef> Undefs;
286   StringRef SoName;
287   const Elf_Shdr *VersymSec = nullptr;
288   const Elf_Shdr *VerdefSec = nullptr;
289
290 public:
291   StringRef getSoName() const;
292   const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
293   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
294
295   static bool classof(const InputFile *F) {
296     return F->kind() == Base::SharedKind;
297   }
298
299   explicit SharedFile(MemoryBufferRef M);
300
301   void parseSoName();
302   void parseRest();
303   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
304
305   struct NeededVer {
306     // The string table offset of the version name in the output file.
307     size_t StrTab;
308
309     // The version identifier for this version name.
310     uint16_t Index;
311   };
312
313   // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
314   // data structures in the output file.
315   std::map<const Elf_Verdef *, NeededVer> VerdefMap;
316
317   // Used for --as-needed
318   bool AsNeeded = false;
319   bool IsUsed = false;
320   bool isNeeded() const { return !AsNeeded || IsUsed; }
321 };
322
323 class BinaryFile : public InputFile {
324 public:
325   explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
326   static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
327   template <class ELFT> void parse();
328 };
329
330 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
331                             uint64_t OffsetInArchive = 0);
332 InputFile *createSharedFile(MemoryBufferRef MB);
333
334 } // namespace elf
335 } // namespace lld
336
337 #endif