xref: /llvm-project-15.0.7/lld/ELF/InputFiles.h (revision fdcca8cd)
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(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   void traceUndefined(StringRef Name);
147 
148   // Get MIPS GP0 value defined by this file. This value represents the gp value
149   // used to create the relocatable object and required to support
150   // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
151   uint32_t getMipsGp0() const;
152 
153   // The number is the offset in the string table. It will be used as the
154   // st_name of the symbol.
155   std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
156 
157 private:
158   void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups);
159   void initializeSymbols();
160   InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
161   InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec);
162 
163   SymbolBody *createSymbolBody(const Elf_Sym *Sym);
164 
165   // List of all sections defined by this file.
166   std::vector<InputSectionBase<ELFT> *> Sections;
167 
168   // List of all symbols referenced or defined by this file.
169   std::vector<SymbolBody *> SymbolBodies;
170 
171   // MIPS .reginfo section defined by this file.
172   std::unique_ptr<MipsReginfoInputSection<ELFT>> MipsReginfo;
173   // MIPS .MIPS.options section defined by this file.
174   std::unique_ptr<MipsOptionsInputSection<ELFT>> MipsOptions;
175 
176   llvm::BumpPtrAllocator Alloc;
177   llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc;
178   llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
179   llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> EHAlloc;
180 };
181 
182 // LazyObjectFile is analogous to ArchiveFile in the sense that
183 // the file contains lazy symbols. The difference is that
184 // LazyObjectFile wraps a single file instead of multiple files.
185 //
186 // This class is used for --start-lib and --end-lib options which
187 // instruct the linker to link object files between them with the
188 // archive file semantics.
189 class LazyObjectFile : public InputFile {
190 public:
191   explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
192 
193   static bool classof(const InputFile *F) {
194     return F->kind() == LazyObjectKind;
195   }
196 
197   template <class ELFT> void parse();
198   MemoryBufferRef getBuffer();
199 
200 private:
201   std::vector<StringRef> getSymbols();
202   template <class ELFT> std::vector<StringRef> getElfSymbols();
203   std::vector<StringRef> getBitcodeSymbols();
204 
205   llvm::BumpPtrAllocator Alloc;
206   llvm::StringSaver Saver{Alloc};
207   bool Seen = false;
208 };
209 
210 // An ArchiveFile object represents a .a file.
211 class ArchiveFile : public InputFile {
212 public:
213   explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
214   static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
215   template <class ELFT> void parse();
216 
217   // Returns a memory buffer for a given symbol. An empty memory buffer
218   // is returned if we have already returned the same memory buffer.
219   // (So that we don't instantiate same members more than once.)
220   MemoryBufferRef getMember(const Archive::Symbol *Sym);
221 
222 private:
223   std::unique_ptr<Archive> File;
224   llvm::DenseSet<uint64_t> Seen;
225 };
226 
227 class BitcodeFile : public InputFile {
228 public:
229   explicit BitcodeFile(MemoryBufferRef M);
230   static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
231   template <class ELFT>
232   void parse(llvm::DenseSet<StringRef> &ComdatGroups);
233   ArrayRef<Symbol *> getSymbols() { return Symbols; }
234   static bool shouldSkip(uint32_t Flags);
235   std::unique_ptr<llvm::object::IRObjectFile> Obj;
236 
237 private:
238   std::vector<Symbol *> Symbols;
239   llvm::BumpPtrAllocator Alloc;
240   llvm::StringSaver Saver{Alloc};
241   template <class ELFT>
242   Symbol *createSymbol(const llvm::DenseSet<const llvm::Comdat *> &KeptComdats,
243                        const llvm::object::IRObjectFile &Obj,
244                        const llvm::object::BasicSymbolRef &Sym);
245 };
246 
247 // .so file.
248 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
249   typedef ELFFileBase<ELFT> Base;
250   typedef typename ELFT::Shdr Elf_Shdr;
251   typedef typename ELFT::Sym Elf_Sym;
252   typedef typename ELFT::Word Elf_Word;
253   typedef typename ELFT::SymRange Elf_Sym_Range;
254   typedef typename ELFT::Versym Elf_Versym;
255   typedef typename ELFT::Verdef Elf_Verdef;
256 
257   std::vector<StringRef> Undefs;
258   StringRef SoName;
259   const Elf_Shdr *VersymSec = nullptr;
260   const Elf_Shdr *VerdefSec = nullptr;
261 
262 public:
263   StringRef getSoName() const { return SoName; }
264   const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
265   llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
266 
267   static bool classof(const InputFile *F) {
268     return F->kind() == Base::SharedKind;
269   }
270 
271   explicit SharedFile(MemoryBufferRef M);
272 
273   void parseSoName();
274   void parseRest();
275   std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
276 
277   struct NeededVer {
278     // The string table offset of the version name in the output file.
279     size_t StrTab;
280 
281     // The version identifier for this version name.
282     uint16_t Index;
283   };
284 
285   // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
286   // data structures in the output file.
287   std::map<const Elf_Verdef *, NeededVer> VerdefMap;
288 
289   // Used for --as-needed
290   bool AsNeeded = false;
291   bool IsUsed = false;
292   bool isNeeded() const { return !AsNeeded || IsUsed; }
293 };
294 
295 std::unique_ptr<InputFile> createObjectFile(MemoryBufferRef MB,
296                                             StringRef ArchiveName = "");
297 std::unique_ptr<InputFile> createSharedFile(MemoryBufferRef MB);
298 
299 } // namespace elf
300 } // namespace lld
301 
302 #endif
303