]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputFiles.h
Update tcpdump to 4.9.0.
[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 "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/StringSaver.h"
26
27 #include <map>
28
29 namespace lld {
30 namespace elf {
31
32 using llvm::object::Archive;
33
34 class InputFile;
35 class Lazy;
36 class SymbolBody;
37
38 // The root class of input files.
39 class InputFile {
40 public:
41   enum Kind {
42     ObjectKind,
43     SharedKind,
44     LazyObjectKind,
45     ArchiveKind,
46     BitcodeKind,
47   };
48
49   Kind kind() const { return FileKind; }
50
51   StringRef getName() const { return MB.getBufferIdentifier(); }
52   MemoryBufferRef MB;
53
54   // Filename of .a which contained this file. If this file was
55   // not in an archive file, it is the empty string. We use this
56   // string for creating error messages.
57   StringRef ArchiveName;
58
59   // If this is an architecture-specific file, the following members
60   // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
61   ELFKind EKind = ELFNoneKind;
62   uint16_t EMachine = llvm::ELF::EM_NONE;
63
64 protected:
65   InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
66
67 private:
68   const Kind FileKind;
69 };
70
71 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
72 std::string getFilename(const InputFile *F);
73
74 template <typename ELFT> class ELFFileBase : public InputFile {
75 public:
76   typedef typename ELFT::Shdr Elf_Shdr;
77   typedef typename ELFT::Sym Elf_Sym;
78   typedef typename ELFT::Word Elf_Word;
79   typedef typename ELFT::SymRange Elf_Sym_Range;
80
81   ELFFileBase(Kind K, MemoryBufferRef M);
82   static bool classof(const InputFile *F) {
83     Kind K = F->kind();
84     return K == ObjectKind || K == SharedKind;
85   }
86
87   const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; }
88   llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; }
89
90   uint8_t getOSABI() const {
91     return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
92   }
93
94   StringRef getStringTable() const { return StringTable; }
95
96   uint32_t getSectionIndex(const Elf_Sym &Sym) const;
97
98   Elf_Sym_Range getElfSymbols(bool OnlyGlobals);
99
100 protected:
101   llvm::object::ELFFile<ELFT> ELFObj;
102   const Elf_Shdr *Symtab = nullptr;
103   ArrayRef<Elf_Word> SymtabSHNDX;
104   StringRef StringTable;
105   void initStringTable();
106 };
107
108 // .o file.
109 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
110   typedef ELFFileBase<ELFT> Base;
111   typedef typename ELFT::Sym Elf_Sym;
112   typedef typename ELFT::Shdr Elf_Shdr;
113   typedef typename ELFT::SymRange Elf_Sym_Range;
114   typedef typename ELFT::Word Elf_Word;
115   typedef typename ELFT::uint uintX_t;
116
117   StringRef getShtGroupSignature(const Elf_Shdr &Sec);
118   ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
119
120 public:
121   static bool classof(const InputFile *F) {
122     return F->kind() == Base::ObjectKind;
123   }
124
125   ArrayRef<SymbolBody *> getSymbols();
126   ArrayRef<SymbolBody *> getLocalSymbols();
127   ArrayRef<SymbolBody *> getNonLocalSymbols();
128
129   explicit ObjectFile(MemoryBufferRef M);
130   void parse(llvm::DenseSet<StringRef> &ComdatGroups);
131
132   ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
133   InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
134
135   SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
136     return *SymbolBodies[SymbolIndex];
137   }
138
139   template <typename RelT> SymbolBody &getRelocTargetSym(const RelT &Rel) const {
140     uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
141     return getSymbolBody(SymIndex);
142   }
143
144   const Elf_Shdr *getSymbolTable() const { return this->Symtab; };
145
146   // Get MIPS GP0 value defined by this file. This value represents the gp value
147   // used to create the relocatable object and required to support
148   // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
149   uint32_t getMipsGp0() const;
150
151   // The number is the offset in the string table. It will be used as the
152   // st_name of the symbol.
153   std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
154
155   // SymbolBodies and Thunks for sections in this file are allocated
156   // using this buffer.
157   llvm::BumpPtrAllocator Alloc;
158
159 private:
160   void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups);
161   void initializeSymbols();
162   InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
163   InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec);
164
165   bool shouldMerge(const Elf_Shdr &Sec);
166   SymbolBody *createSymbolBody(const Elf_Sym *Sym);
167
168   // List of all sections defined by this file.
169   std::vector<InputSectionBase<ELFT> *> Sections;
170
171   // List of all symbols referenced or defined by this file.
172   std::vector<SymbolBody *> SymbolBodies;
173
174   // MIPS .reginfo section defined by this file.
175   std::unique_ptr<MipsReginfoInputSection<ELFT>> MipsReginfo;
176   // MIPS .MIPS.options section defined by this file.
177   std::unique_ptr<MipsOptionsInputSection<ELFT>> MipsOptions;
178
179   llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc;
180   llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
181   llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> EHAlloc;
182 };
183
184 // LazyObjectFile is analogous to ArchiveFile in the sense that
185 // the file contains lazy symbols. The difference is that
186 // LazyObjectFile wraps a single file instead of multiple files.
187 //
188 // This class is used for --start-lib and --end-lib options which
189 // instruct the linker to link object files between them with the
190 // archive file semantics.
191 class LazyObjectFile : public InputFile {
192 public:
193   explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
194
195   static bool classof(const InputFile *F) {
196     return F->kind() == LazyObjectKind;
197   }
198
199   template <class ELFT> void parse();
200   MemoryBufferRef getBuffer();
201
202 private:
203   std::vector<StringRef> getSymbols();
204   template <class ELFT> std::vector<StringRef> getElfSymbols();
205   std::vector<StringRef> getBitcodeSymbols();
206
207   llvm::BumpPtrAllocator Alloc;
208   llvm::StringSaver Saver{Alloc};
209   bool Seen = false;
210 };
211
212 // An ArchiveFile object represents a .a file.
213 class ArchiveFile : public InputFile {
214 public:
215   explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
216   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
217   template <class ELFT> void parse();
218
219   // Returns a memory buffer for a given symbol. An empty memory buffer
220   // is returned if we have already returned the same memory buffer.
221   // (So that we don't instantiate same members more than once.)
222   MemoryBufferRef getMember(const Archive::Symbol *Sym);
223
224 private:
225   std::unique_ptr<Archive> File;
226   llvm::DenseSet<uint64_t> Seen;
227 };
228
229 class BitcodeFile : public InputFile {
230 public:
231   explicit BitcodeFile(MemoryBufferRef M);
232   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
233   template <class ELFT>
234   void parse(llvm::DenseSet<StringRef> &ComdatGroups);
235   ArrayRef<Symbol *> getSymbols() { return Symbols; }
236   static bool shouldSkip(uint32_t Flags);
237   std::unique_ptr<llvm::object::IRObjectFile> Obj;
238
239 private:
240   std::vector<Symbol *> Symbols;
241   llvm::BumpPtrAllocator Alloc;
242   llvm::StringSaver Saver{Alloc};
243   template <class ELFT>
244   Symbol *createSymbol(const llvm::DenseSet<const llvm::Comdat *> &KeptComdats,
245                        const llvm::object::IRObjectFile &Obj,
246                        const llvm::object::BasicSymbolRef &Sym);
247 };
248
249 // .so file.
250 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
251   typedef ELFFileBase<ELFT> Base;
252   typedef typename ELFT::Shdr Elf_Shdr;
253   typedef typename ELFT::Sym Elf_Sym;
254   typedef typename ELFT::Word Elf_Word;
255   typedef typename ELFT::SymRange Elf_Sym_Range;
256   typedef typename ELFT::Versym Elf_Versym;
257   typedef typename ELFT::Verdef Elf_Verdef;
258
259   std::vector<StringRef> Undefs;
260   StringRef SoName;
261   const Elf_Shdr *VersymSec = nullptr;
262   const Elf_Shdr *VerdefSec = nullptr;
263
264 public:
265   StringRef getSoName() const { return SoName; }
266   const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
267   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
268
269   static bool classof(const InputFile *F) {
270     return F->kind() == Base::SharedKind;
271   }
272
273   explicit SharedFile(MemoryBufferRef M);
274
275   void parseSoName();
276   void parseRest();
277   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
278
279   struct NeededVer {
280     // The string table offset of the version name in the output file.
281     size_t StrTab;
282
283     // The version identifier for this version name.
284     uint16_t Index;
285   };
286
287   // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
288   // data structures in the output file.
289   std::map<const Elf_Verdef *, NeededVer> VerdefMap;
290
291   // Used for --as-needed
292   bool AsNeeded = false;
293   bool IsUsed = false;
294   bool isNeeded() const { return !AsNeeded || IsUsed; }
295 };
296
297 std::unique_ptr<InputFile> createObjectFile(MemoryBufferRef MB,
298                                             StringRef ArchiveName = "");
299 std::unique_ptr<InputFile> createSharedFile(MemoryBufferRef MB);
300
301 } // namespace elf
302 } // namespace lld
303
304 #endif