xref: /openbsd/gnu/llvm/lld/ELF/InputFiles.h (revision 05edf1c1)
1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLD_ELF_INPUT_FILES_H
10 #define LLD_ELF_INPUT_FILES_H
11 
12 #include "Config.h"
13 #include "Symbols.h"
14 #include "lld/Common/ErrorHandler.h"
15 #include "lld/Common/LLVM.h"
16 #include "lld/Common/Reproduce.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/BinaryFormat/Magic.h"
19 #include "llvm/Object/ELF.h"
20 #include "llvm/Support/MemoryBufferRef.h"
21 #include "llvm/Support/Threading.h"
22 
23 namespace llvm {
24 struct DILineInfo;
25 class TarWriter;
26 namespace lto {
27 class InputFile;
28 }
29 } // namespace llvm
30 
31 namespace lld {
32 class DWARFCache;
33 
34 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
35 std::string toString(const elf::InputFile *f);
36 
37 void parseGNUWarning(StringRef name, ArrayRef<char> data, size_t size);
38 
39 namespace elf {
40 
41 class InputSection;
42 class Symbol;
43 
44 // If --reproduce is specified, all input files are written to this tar archive.
45 extern std::unique_ptr<llvm::TarWriter> tar;
46 
47 // Opens a given file.
48 std::optional<MemoryBufferRef> readFile(StringRef path);
49 
50 // Add symbols in File to the symbol table.
51 void parseFile(InputFile *file);
52 
53 // The root class of input files.
54 class InputFile {
55 protected:
56   std::unique_ptr<Symbol *[]> symbols;
57   uint32_t numSymbols = 0;
58   SmallVector<InputSectionBase *, 0> sections;
59 
60 public:
61   enum Kind : uint8_t {
62     ObjKind,
63     SharedKind,
64     ArchiveKind,
65     BitcodeKind,
66     BinaryKind,
67   };
68 
kind()69   Kind kind() const { return fileKind; }
70 
isElf()71   bool isElf() const {
72     Kind k = kind();
73     return k == ObjKind || k == SharedKind;
74   }
75 
getName()76   StringRef getName() const { return mb.getBufferIdentifier(); }
77   MemoryBufferRef mb;
78 
79   // Returns sections. It is a runtime error to call this function
80   // on files that don't have the notion of sections.
getSections()81   ArrayRef<InputSectionBase *> getSections() const {
82     assert(fileKind == ObjKind || fileKind == BinaryKind);
83     return sections;
84   }
85 
86   // Returns object file symbols. It is a runtime error to call this
87   // function on files of other types.
getSymbols()88   ArrayRef<Symbol *> getSymbols() const {
89     assert(fileKind == BinaryKind || fileKind == ObjKind ||
90            fileKind == BitcodeKind);
91     return {symbols.get(), numSymbols};
92   }
93 
94   // Get filename to use for linker script processing.
95   StringRef getNameForScript() const;
96 
97   // Check if a non-common symbol should be extracted to override a common
98   // definition.
99   bool shouldExtractForCommon(StringRef name);
100 
101   // .got2 in the current file. This is used by PPC32 -fPIC/-fPIE to compute
102   // offsets in PLT call stubs.
103   InputSection *ppc32Got2 = nullptr;
104 
105   // Index of MIPS GOT built for this file.
106   uint32_t mipsGotIndex = -1;
107 
108   // groupId is used for --warn-backrefs which is an optional error
109   // checking feature. All files within the same --{start,end}-group or
110   // --{start,end}-lib get the same group ID. Otherwise, each file gets a new
111   // group ID. For more info, see checkDependency() in SymbolTable.cpp.
112   uint32_t groupId;
113   static bool isInGroup;
114   static uint32_t nextGroupId;
115 
116   // If this is an architecture-specific file, the following members
117   // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
118   uint16_t emachine = llvm::ELF::EM_NONE;
119   const Kind fileKind;
120   ELFKind ekind = ELFNoneKind;
121   uint8_t osabi = 0;
122   uint8_t abiVersion = 0;
123 
124   // True if this is a relocatable object file/bitcode file between --start-lib
125   // and --end-lib.
126   bool lazy = false;
127 
128   // True if this is an argument for --just-symbols. Usually false.
129   bool justSymbols = false;
130 
131   std::string getSrcMsg(const Symbol &sym, InputSectionBase &sec,
132                         uint64_t offset);
133 
134   // On PPC64 we need to keep track of which files contain small code model
135   // relocations that access the .toc section. To minimize the chance of a
136   // relocation overflow, files that do contain said relocations should have
137   // their .toc sections sorted closer to the .got section than files that do
138   // not contain any small code model relocations. Thats because the toc-pointer
139   // is defined to point at .got + 0x8000 and the instructions used with small
140   // code model relocations support immediates in the range [-0x8000, 0x7FFC],
141   // making the addressable range relative to the toc pointer
142   // [.got, .got + 0xFFFC].
143   bool ppc64SmallCodeModelTocRelocs = false;
144 
145   // True if the file has TLSGD/TLSLD GOT relocations without R_PPC64_TLSGD or
146   // R_PPC64_TLSLD. Disable TLS relaxation to avoid bad code generation.
147   bool ppc64DisableTLSRelax = false;
148 
149 protected:
150   InputFile(Kind k, MemoryBufferRef m);
151 
152 public:
153   // If not empty, this stores the name of the archive containing this file.
154   // We use this string for creating error messages.
155   SmallString<0> archiveName;
156   // Cache for toString(). Only toString() should use this member.
157   mutable SmallString<0> toStringCache;
158 
159 private:
160   // Cache for getNameForScript().
161   mutable SmallString<0> nameForScriptCache;
162 };
163 
164 class ELFFileBase : public InputFile {
165 public:
166   ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef m);
classof(const InputFile * f)167   static bool classof(const InputFile *f) { return f->isElf(); }
168 
169   void init();
getObj()170   template <typename ELFT> llvm::object::ELFFile<ELFT> getObj() const {
171     return check(llvm::object::ELFFile<ELFT>::create(mb.getBuffer()));
172   }
173 
getStringTable()174   StringRef getStringTable() const { return stringTable; }
175 
getLocalSymbols()176   ArrayRef<Symbol *> getLocalSymbols() {
177     if (numSymbols == 0)
178       return {};
179     return llvm::ArrayRef(symbols.get() + 1, firstGlobal - 1);
180   }
getGlobalSymbols()181   ArrayRef<Symbol *> getGlobalSymbols() {
182     return llvm::ArrayRef(symbols.get() + firstGlobal,
183                           numSymbols - firstGlobal);
184   }
getMutableGlobalSymbols()185   MutableArrayRef<Symbol *> getMutableGlobalSymbols() {
186     return llvm::MutableArrayRef(symbols.get() + firstGlobal,
187                                      numSymbols - firstGlobal);
188   }
189 
getELFShdrs()190   template <typename ELFT> typename ELFT::ShdrRange getELFShdrs() const {
191     return typename ELFT::ShdrRange(
192         reinterpret_cast<const typename ELFT::Shdr *>(elfShdrs), numELFShdrs);
193   }
getELFSyms()194   template <typename ELFT> typename ELFT::SymRange getELFSyms() const {
195     return typename ELFT::SymRange(
196         reinterpret_cast<const typename ELFT::Sym *>(elfSyms), numELFSyms);
197   }
getGlobalELFSyms()198   template <typename ELFT> typename ELFT::SymRange getGlobalELFSyms() const {
199     return getELFSyms<ELFT>().slice(firstGlobal);
200   }
201 
202 protected:
203   // Initializes this class's member variables.
204   template <typename ELFT> void init(InputFile::Kind k);
205 
206   StringRef stringTable;
207   const void *elfShdrs = nullptr;
208   const void *elfSyms = nullptr;
209   uint32_t numELFShdrs = 0;
210   uint32_t numELFSyms = 0;
211   uint32_t firstGlobal = 0;
212 
213 public:
214   uint32_t andFeatures = 0;
215   bool hasCommonSyms = false;
216 };
217 
218 // .o file.
219 template <class ELFT> class ObjFile : public ELFFileBase {
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)220   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
221 
222 public:
223   static bool classof(const InputFile *f) { return f->kind() == ObjKind; }
224 
getObj()225   llvm::object::ELFFile<ELFT> getObj() const {
226     return this->ELFFileBase::getObj<ELFT>();
227   }
228 
ObjFile(ELFKind ekind,MemoryBufferRef m,StringRef archiveName)229   ObjFile(ELFKind ekind, MemoryBufferRef m, StringRef archiveName)
230       : ELFFileBase(ObjKind, ekind, m) {
231     this->archiveName = archiveName;
232   }
233 
234   void parse(bool ignoreComdats = false);
235   void parseLazy();
236 
237   StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
238                                  const Elf_Shdr &sec);
239 
getSymbol(uint32_t symbolIndex)240   Symbol &getSymbol(uint32_t symbolIndex) const {
241     if (symbolIndex >= numSymbols)
242       fatal(toString(this) + ": invalid symbol index");
243     return *this->symbols[symbolIndex];
244   }
245 
246   uint32_t getSectionIndex(const Elf_Sym &sym) const;
247 
getRelocTargetSym(const RelT & rel)248   template <typename RelT> Symbol &getRelocTargetSym(const RelT &rel) const {
249     uint32_t symIndex = rel.getSymbol(config->isMips64EL);
250     return getSymbol(symIndex);
251   }
252 
253   std::optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
254   std::optional<std::pair<std::string, unsigned>>
255   getVariableLoc(StringRef name);
256 
257   // Name of source file obtained from STT_FILE symbol value,
258   // or empty string if there is no such symbol in object file
259   // symbol table.
260   StringRef sourceFile;
261 
262   // Pointer to this input file's .llvm_addrsig section, if it has one.
263   const Elf_Shdr *addrsigSec = nullptr;
264 
265   // SHT_LLVM_CALL_GRAPH_PROFILE section index.
266   uint32_t cgProfileSectionIndex = 0;
267 
268   // MIPS GP0 value defined by this file. This value represents the gp value
269   // used to create the relocatable object and required to support
270   // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
271   uint32_t mipsGp0 = 0;
272 
273   // True if the file defines functions compiled with
274   // -fsplit-stack. Usually false.
275   bool splitStack = false;
276 
277   // True if the file defines functions compiled with -fsplit-stack,
278   // but had one or more functions with the no_split_stack attribute.
279   bool someNoSplitStack = false;
280 
281   // Get cached DWARF information.
282   DWARFCache *getDwarf();
283 
284   void initSectionsAndLocalSyms(bool ignoreComdats);
285   void postParse();
286 
287 private:
288   void initializeSections(bool ignoreComdats,
289                           const llvm::object::ELFFile<ELFT> &obj);
290   void initializeSymbols(const llvm::object::ELFFile<ELFT> &obj);
291   void initializeJustSymbols();
292 
293   InputSectionBase *getRelocTarget(uint32_t idx, const Elf_Shdr &sec,
294                                    uint32_t info);
295   InputSectionBase *createInputSection(uint32_t idx, const Elf_Shdr &sec,
296                                        StringRef name);
297 
298   bool shouldMerge(const Elf_Shdr &sec, StringRef name);
299 
300   // Each ELF symbol contains a section index which the symbol belongs to.
301   // However, because the number of bits dedicated for that is limited, a
302   // symbol can directly point to a section only when the section index is
303   // equal to or smaller than 65280.
304   //
305   // If an object file contains more than 65280 sections, the file must
306   // contain .symtab_shndx section. The section contains an array of
307   // 32-bit integers whose size is the same as the number of symbols.
308   // Nth symbol's section index is in the Nth entry of .symtab_shndx.
309   //
310   // The following variable contains the contents of .symtab_shndx.
311   // If the section does not exist (which is common), the array is empty.
312   ArrayRef<Elf_Word> shndxTable;
313 
314   // Debugging information to retrieve source file and line for error
315   // reporting. Linker may find reasonable number of errors in a
316   // single object file, so we cache debugging information in order to
317   // parse it only once for each object file we link.
318   std::unique_ptr<DWARFCache> dwarf;
319   llvm::once_flag initDwarf;
320 };
321 
322 class BitcodeFile : public InputFile {
323 public:
324   BitcodeFile(MemoryBufferRef m, StringRef archiveName,
325               uint64_t offsetInArchive, bool lazy);
classof(const InputFile * f)326   static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
327   void parse();
328   void parseLazy();
329   void postParse();
330   std::unique_ptr<llvm::lto::InputFile> obj;
331   std::vector<bool> keptComdats;
332 };
333 
334 // .so file.
335 class SharedFile : public ELFFileBase {
336 public:
337   SharedFile(MemoryBufferRef m, StringRef defaultSoName);
338 
339   // This is actually a vector of Elf_Verdef pointers.
340   SmallVector<const void *, 0> verdefs;
341 
342   // If the output file needs Elf_Verneed data structures for this file, this is
343   // a vector of Elf_Vernaux version identifiers that map onto the entries in
344   // Verdefs, otherwise it is empty.
345   SmallVector<uint32_t, 0> vernauxs;
346 
347   static unsigned vernauxNum;
348 
349   SmallVector<StringRef, 0> dtNeeded;
350   StringRef soName;
351 
classof(const InputFile * f)352   static bool classof(const InputFile *f) { return f->kind() == SharedKind; }
353 
354   template <typename ELFT> void parse();
355 
356   // Used for --as-needed
357   bool isNeeded;
358 
359   // Non-weak undefined symbols which are not yet resolved when the SO is
360   // parsed. Only filled for `--no-allow-shlib-undefined`.
361   SmallVector<Symbol *, 0> requiredSymbols;
362 
363 private:
364   template <typename ELFT>
365   std::vector<uint32_t> parseVerneed(const llvm::object::ELFFile<ELFT> &obj,
366                                      const typename ELFT::Shdr *sec);
367 };
368 
369 class BinaryFile : public InputFile {
370 public:
BinaryFile(MemoryBufferRef m)371   explicit BinaryFile(MemoryBufferRef m) : InputFile(BinaryKind, m) {}
classof(const InputFile * f)372   static bool classof(const InputFile *f) { return f->kind() == BinaryKind; }
373   void parse();
374 };
375 
376 ELFFileBase *createObjFile(MemoryBufferRef mb, StringRef archiveName = "",
377                            bool lazy = false);
378 
379 std::string replaceThinLTOSuffix(StringRef path);
380 
381 } // namespace elf
382 } // namespace lld
383 
384 #endif
385