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