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