]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputFiles.h
Merge ^/head r311460 through r311545.
[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 namespace lto {
33 class InputFile;
34 }
35 }
36
37 namespace lld {
38 namespace elf {
39 class InputFile;
40 }
41
42 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
43 std::string toString(const elf::InputFile *F);
44
45 namespace elf {
46
47 using llvm::object::Archive;
48
49 class Lazy;
50 class SymbolBody;
51
52 // The root class of input files.
53 class InputFile {
54 public:
55   enum Kind {
56     ObjectKind,
57     SharedKind,
58     LazyObjectKind,
59     ArchiveKind,
60     BitcodeKind,
61     BinaryKind,
62   };
63
64   Kind kind() const { return FileKind; }
65
66   StringRef getName() const { return MB.getBufferIdentifier(); }
67   MemoryBufferRef MB;
68
69   // Filename of .a which contained this file. If this file was
70   // not in an archive file, it is the empty string. We use this
71   // string for creating error messages.
72   StringRef ArchiveName;
73
74   // If this file is in an archive, the member contains the offset of
75   // the file in the archive. Otherwise, it's just zero. We store this
76   // field so that we can pass it to lib/LTO in order to disambiguate
77   // between objects.
78   uint64_t OffsetInArchive;
79
80   // If this is an architecture-specific file, the following members
81   // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
82   ELFKind EKind = ELFNoneKind;
83   uint16_t EMachine = llvm::ELF::EM_NONE;
84   uint8_t OSABI = 0;
85
86 protected:
87   InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
88
89 private:
90   const Kind FileKind;
91 };
92
93 template <typename ELFT> class ELFFileBase : public InputFile {
94 public:
95   typedef typename ELFT::Shdr Elf_Shdr;
96   typedef typename ELFT::Sym Elf_Sym;
97   typedef typename ELFT::Word Elf_Word;
98   typedef typename ELFT::SymRange Elf_Sym_Range;
99
100   ELFFileBase(Kind K, MemoryBufferRef M);
101   static bool classof(const InputFile *F) {
102     Kind K = F->kind();
103     return K == ObjectKind || K == SharedKind;
104   }
105
106   llvm::object::ELFFile<ELFT> getObj() const {
107     return llvm::object::ELFFile<ELFT>(MB.getBuffer());
108   }
109
110   StringRef getStringTable() const { return StringTable; }
111
112   uint32_t getSectionIndex(const Elf_Sym &Sym) const;
113
114   Elf_Sym_Range getGlobalSymbols();
115
116 protected:
117   ArrayRef<Elf_Sym> Symbols;
118   uint32_t FirstNonLocal = 0;
119   ArrayRef<Elf_Word> SymtabSHNDX;
120   StringRef StringTable;
121   void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
122 };
123
124 // .o file.
125 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
126   typedef ELFFileBase<ELFT> Base;
127   typedef typename ELFT::Rel Elf_Rel;
128   typedef typename ELFT::Rela Elf_Rela;
129   typedef typename ELFT::Sym Elf_Sym;
130   typedef typename ELFT::Shdr Elf_Shdr;
131   typedef typename ELFT::SymRange Elf_Sym_Range;
132   typedef typename ELFT::Word Elf_Word;
133   typedef typename ELFT::uint uintX_t;
134
135   StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
136                                  const Elf_Shdr &Sec);
137   ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
138
139 public:
140   static bool classof(const InputFile *F) {
141     return F->kind() == Base::ObjectKind;
142   }
143
144   ArrayRef<SymbolBody *> getSymbols();
145   ArrayRef<SymbolBody *> getLocalSymbols();
146   ArrayRef<SymbolBody *> getNonLocalSymbols();
147
148   explicit ObjectFile(MemoryBufferRef M);
149   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
150
151   ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
152   InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
153
154   SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
155     if (SymbolIndex >= SymbolBodies.size())
156       fatal(toString(this) + ": invalid symbol index");
157     return *SymbolBodies[SymbolIndex];
158   }
159
160   template <typename RelT>
161   SymbolBody &getRelocTargetSym(const RelT &Rel) const {
162     uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
163     return getSymbolBody(SymIndex);
164   }
165
166   // Returns source line information for a given offset.
167   // If no information is available, returns "".
168   std::string getLineInfo(InputSectionBase<ELFT> *S, uintX_t Offset);
169
170   // MIPS GP0 value defined by this file. This value represents the gp value
171   // used to create the relocatable object and required to support
172   // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
173   uint32_t MipsGp0 = 0;
174
175   // The number is the offset in the string table. It will be used as the
176   // st_name of the symbol.
177   std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
178
179   // Name of source file obtained from STT_FILE symbol value,
180   // or empty string if there is no such symbol in object file
181   // symbol table.
182   StringRef SourceFile;
183
184 private:
185   void
186   initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
187   void initializeSymbols();
188   void initializeDwarfLine();
189   InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
190   InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec,
191                                              StringRef SectionStringTable);
192
193   bool shouldMerge(const Elf_Shdr &Sec);
194   SymbolBody *createSymbolBody(const Elf_Sym *Sym);
195
196   // List of all sections defined by this file.
197   std::vector<InputSectionBase<ELFT> *> Sections;
198
199   // List of all symbols referenced or defined by this file.
200   std::vector<SymbolBody *> SymbolBodies;
201
202   // Debugging information to retrieve source file and line for error
203   // reporting. Linker may find reasonable number of errors in a
204   // single object file, so we cache debugging information in order to
205   // parse it only once for each object file we link.
206   std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
207 };
208
209 // LazyObjectFile is analogous to ArchiveFile in the sense that
210 // the file contains lazy symbols. The difference is that
211 // LazyObjectFile wraps a single file instead of multiple files.
212 //
213 // This class is used for --start-lib and --end-lib options which
214 // instruct the linker to link object files between them with the
215 // archive file semantics.
216 class LazyObjectFile : public InputFile {
217 public:
218   explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
219
220   static bool classof(const InputFile *F) {
221     return F->kind() == LazyObjectKind;
222   }
223
224   template <class ELFT> void parse();
225   MemoryBufferRef getBuffer();
226
227 private:
228   std::vector<StringRef> getSymbols();
229   template <class ELFT> std::vector<StringRef> getElfSymbols();
230   std::vector<StringRef> getBitcodeSymbols();
231
232   bool Seen = false;
233 };
234
235 // An ArchiveFile object represents a .a file.
236 class ArchiveFile : public InputFile {
237 public:
238   explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
239   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
240   template <class ELFT> void parse();
241
242   // Returns a memory buffer for a given symbol and the offset in the archive
243   // for the member. An empty memory buffer and an offset of zero
244   // is returned if we have already returned the same memory buffer.
245   // (So that we don't instantiate same members more than once.)
246   std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
247
248 private:
249   std::unique_ptr<Archive> File;
250   llvm::DenseSet<uint64_t> Seen;
251 };
252
253 class BitcodeFile : public InputFile {
254 public:
255   explicit BitcodeFile(MemoryBufferRef M);
256   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
257   template <class ELFT>
258   void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
259   ArrayRef<Symbol *> getSymbols() { return Symbols; }
260   std::unique_ptr<llvm::lto::InputFile> Obj;
261
262 private:
263   std::vector<Symbol *> Symbols;
264 };
265
266 // .so file.
267 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
268   typedef ELFFileBase<ELFT> Base;
269   typedef typename ELFT::Dyn Elf_Dyn;
270   typedef typename ELFT::Shdr Elf_Shdr;
271   typedef typename ELFT::Sym Elf_Sym;
272   typedef typename ELFT::SymRange Elf_Sym_Range;
273   typedef typename ELFT::Verdef Elf_Verdef;
274   typedef typename ELFT::Versym Elf_Versym;
275   typedef typename ELFT::Word Elf_Word;
276   typedef typename ELFT::uint uintX_t;
277
278   std::vector<StringRef> Undefs;
279   StringRef SoName;
280   const Elf_Shdr *VersymSec = nullptr;
281   const Elf_Shdr *VerdefSec = nullptr;
282
283 public:
284   StringRef getSoName() const { return SoName; }
285   const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
286   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
287
288   static bool classof(const InputFile *F) {
289     return F->kind() == Base::SharedKind;
290   }
291
292   explicit SharedFile(MemoryBufferRef M);
293
294   void parseSoName();
295   void parseRest();
296   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
297
298   struct NeededVer {
299     // The string table offset of the version name in the output file.
300     size_t StrTab;
301
302     // The version identifier for this version name.
303     uint16_t Index;
304   };
305
306   // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
307   // data structures in the output file.
308   std::map<const Elf_Verdef *, NeededVer> VerdefMap;
309
310   // Used for --as-needed
311   bool AsNeeded = false;
312   bool IsUsed = false;
313   bool isNeeded() const { return !AsNeeded || IsUsed; }
314 };
315
316 class BinaryFile : public InputFile {
317 public:
318   explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
319   static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
320   template <class ELFT> void parse();
321   ArrayRef<InputSectionData *> getSections() const { return Sections; }
322
323 private:
324   std::vector<InputSectionData *> Sections;
325 };
326
327 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
328                             uint64_t OffsetInArchive = 0);
329 InputFile *createSharedFile(MemoryBufferRef MB);
330
331 } // namespace elf
332 } // namespace lld
333
334 #endif