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