1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_INPUT_FILES_H
10 #define LLD_ELF_INPUT_FILES_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"
38 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
39 std::string
toStr(Ctx
&, const InputFile
*f
);
40 const ELFSyncStream
&operator<<(const ELFSyncStream
&, const InputFile
*);
42 // Opens a given file.
43 std::optional
<MemoryBufferRef
> readFile(Ctx
&, StringRef path
);
45 // Add symbols in File to the symbol table.
46 void parseFile(Ctx
&, InputFile
*file
);
47 void parseFiles(Ctx
&, const SmallVector
<std::unique_ptr
<InputFile
>, 0> &);
49 // The root class of input files.
55 std::unique_ptr
<Symbol
*[]> symbols
;
56 uint32_t numSymbols
= 0;
57 SmallVector
<InputSectionBase
*, 0> sections
;
68 InputFile(Ctx
&, Kind k
, MemoryBufferRef m
);
70 Kind
kind() const { return fileKind
; }
74 return k
== ObjKind
|| k
== SharedKind
;
76 bool isInternal() const { return kind() == InternalKind
; }
78 StringRef
getName() const { return mb
.getBufferIdentifier(); }
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
);
87 void cacheDecodedCrel(size_t i
, InputSectionBase
*s
) { sections
[i
] = s
; }
89 // Returns object file symbols. It is a runtime error to call this
90 // function on files of other types.
91 ArrayRef
<Symbol
*> getSymbols() const {
92 assert(fileKind
== BinaryKind
|| fileKind
== ObjKind
||
93 fileKind
== BitcodeKind
);
94 return {symbols
.get(), numSymbols
};
97 MutableArrayRef
<Symbol
*> getMutableSymbols() {
98 assert(fileKind
== BinaryKind
|| fileKind
== ObjKind
||
99 fileKind
== BitcodeKind
);
100 return {symbols
.get(), numSymbols
};
103 Symbol
&getSymbol(uint32_t symbolIndex
) const {
104 assert(fileKind
== ObjKind
);
105 if (symbolIndex
>= numSymbols
)
106 Fatal(ctx
) << this << ": invalid symbol index";
107 return *this->symbols
[symbolIndex
];
110 template <typename RelT
> Symbol
&getRelocTargetSym(const RelT
&rel
) const {
111 uint32_t symIndex
= rel
.getSymbol(ctx
.arg
.isMips64EL
);
112 return getSymbol(symIndex
);
115 // Get filename to use for linker script processing.
116 StringRef
getNameForScript() const;
118 // Check if a non-common symbol should be extracted to override a common
120 bool shouldExtractForCommon(StringRef name
) const;
122 // .got2 in the current file. This is used by PPC32 -fPIC/-fPIE to compute
123 // offsets in PLT call stubs.
124 InputSection
*ppc32Got2
= nullptr;
126 // Index of MIPS GOT built for this file.
127 uint32_t mipsGotIndex
= -1;
129 // groupId is used for --warn-backrefs which is an optional error
130 // checking feature. All files within the same --{start,end}-group or
131 // --{start,end}-lib get the same group ID. Otherwise, each file gets a new
132 // group ID. For more info, see checkDependency() in SymbolTable.cpp.
135 // If this is an architecture-specific file, the following members
136 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
137 uint16_t emachine
= llvm::ELF::EM_NONE
;
139 ELFKind ekind
= ELFNoneKind
;
141 uint8_t abiVersion
= 0;
143 // True if this is a relocatable object file/bitcode file in an ar archive
144 // or between --start-lib and --end-lib.
147 // True if this is an argument for --just-symbols. Usually false.
148 bool justSymbols
= false;
150 std::string
getSrcMsg(const Symbol
&sym
, const InputSectionBase
&sec
,
153 // On PPC64 we need to keep track of which files contain small code model
154 // relocations that access the .toc section. To minimize the chance of a
155 // relocation overflow, files that do contain said relocations should have
156 // their .toc sections sorted closer to the .got section than files that do
157 // not contain any small code model relocations. Thats because the toc-pointer
158 // is defined to point at .got + 0x8000 and the instructions used with small
159 // code model relocations support immediates in the range [-0x8000, 0x7FFC],
160 // making the addressable range relative to the toc pointer
161 // [.got, .got + 0xFFFC].
162 bool ppc64SmallCodeModelTocRelocs
= false;
164 // True if the file has TLSGD/TLSLD GOT relocations without R_PPC64_TLSGD or
165 // R_PPC64_TLSLD. Disable TLS relaxation to avoid bad code generation.
166 bool ppc64DisableTLSRelax
= false;
169 // If not empty, this stores the name of the archive containing this file.
170 // We use this string for creating error messages.
171 SmallString
<0> archiveName
;
172 // Cache for toStr(Ctx &, const InputFile *). Only toStr should use this
174 mutable SmallString
<0> toStringCache
;
177 // Cache for getNameForScript().
178 mutable SmallString
<0> nameForScriptCache
;
181 class ELFFileBase
: public InputFile
{
183 ELFFileBase(Ctx
&ctx
, Kind k
, ELFKind ekind
, MemoryBufferRef m
);
184 static bool classof(const InputFile
*f
) { return f
->isElf(); }
187 template <typename ELFT
> llvm::object::ELFFile
<ELFT
> getObj() const {
188 return check(llvm::object::ELFFile
<ELFT
>::create(mb
.getBuffer()));
191 StringRef
getStringTable() const { return stringTable
; }
193 ArrayRef
<Symbol
*> getLocalSymbols() {
196 return llvm::ArrayRef(symbols
.get() + 1, firstGlobal
- 1);
198 ArrayRef
<Symbol
*> getGlobalSymbols() {
199 return llvm::ArrayRef(symbols
.get() + firstGlobal
,
200 numSymbols
- firstGlobal
);
202 MutableArrayRef
<Symbol
*> getMutableGlobalSymbols() {
203 return llvm::MutableArrayRef(symbols
.get() + firstGlobal
,
204 numSymbols
- firstGlobal
);
207 template <typename ELFT
> typename
ELFT::ShdrRange
getELFShdrs() const {
208 return typename
ELFT::ShdrRange(
209 reinterpret_cast<const typename
ELFT::Shdr
*>(elfShdrs
), numELFShdrs
);
211 template <typename ELFT
> typename
ELFT::SymRange
getELFSyms() const {
212 return typename
ELFT::SymRange(
213 reinterpret_cast<const typename
ELFT::Sym
*>(elfSyms
), numELFSyms
);
215 template <typename ELFT
> typename
ELFT::SymRange
getGlobalELFSyms() const {
216 return getELFSyms
<ELFT
>().slice(firstGlobal
);
220 // Initializes this class's member variables.
221 template <typename ELFT
> void init(InputFile::Kind k
);
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;
231 uint32_t andFeatures
= 0;
232 bool hasCommonSyms
= false;
233 ArrayRef
<uint8_t> aarch64PauthAbiCoreInfo
;
237 template <class ELFT
> class ObjFile
: public ELFFileBase
{
238 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
241 static bool classof(const InputFile
*f
) { return f
->kind() == ObjKind
; }
243 llvm::object::ELFFile
<ELFT
> getObj() const {
244 return this->ELFFileBase::getObj
<ELFT
>();
247 ObjFile(Ctx
&ctx
, ELFKind ekind
, MemoryBufferRef m
, StringRef archiveName
)
248 : ELFFileBase(ctx
, ObjKind
, ekind
, m
) {
249 this->archiveName
= archiveName
;
252 void parse(bool ignoreComdats
= false);
255 StringRef
getShtGroupSignature(ArrayRef
<Elf_Shdr
> sections
,
256 const Elf_Shdr
&sec
);
258 uint32_t getSectionIndex(const Elf_Sym
&sym
) const;
260 std::optional
<llvm::DILineInfo
> getDILineInfo(const InputSectionBase
*,
262 std::optional
<std::pair
<std::string
, unsigned>>
263 getVariableLoc(StringRef name
);
265 // Name of source file obtained from STT_FILE symbol value,
266 // or empty string if there is no such symbol in object file
268 StringRef sourceFile
;
270 // Pointer to this input file's .llvm_addrsig section, if it has one.
271 const Elf_Shdr
*addrsigSec
= nullptr;
273 // SHT_LLVM_CALL_GRAPH_PROFILE section index.
274 uint32_t cgProfileSectionIndex
= 0;
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;
281 // True if the file defines functions compiled with
282 // -fsplit-stack. Usually false.
283 bool splitStack
= false;
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;
289 // Get cached DWARF information.
290 DWARFCache
*getDwarf();
292 void initSectionsAndLocalSyms(bool ignoreComdats
);
294 void importCmseSymbols();
297 void initializeSections(bool ignoreComdats
,
298 const llvm::object::ELFFile
<ELFT
> &obj
);
299 void initializeSymbols(const llvm::object::ELFFile
<ELFT
> &obj
);
300 void initializeJustSymbols();
302 InputSectionBase
*getRelocTarget(uint32_t idx
, uint32_t info
);
303 InputSectionBase
*createInputSection(uint32_t idx
, const Elf_Shdr
&sec
,
306 bool shouldMerge(const Elf_Shdr
&sec
, StringRef name
);
308 // Each ELF symbol contains a section index which the symbol belongs to.
309 // However, because the number of bits dedicated for that is limited, a
310 // symbol can directly point to a section only when the section index is
311 // equal to or smaller than 65280.
313 // If an object file contains more than 65280 sections, the file must
314 // contain .symtab_shndx section. The section contains an array of
315 // 32-bit integers whose size is the same as the number of symbols.
316 // Nth symbol's section index is in the Nth entry of .symtab_shndx.
318 // The following variable contains the contents of .symtab_shndx.
319 // If the section does not exist (which is common), the array is empty.
320 ArrayRef
<Elf_Word
> shndxTable
;
322 // Debugging information to retrieve source file and line for error
323 // reporting. Linker may find reasonable number of errors in a
324 // single object file, so we cache debugging information in order to
325 // parse it only once for each object file we link.
326 std::unique_ptr
<DWARFCache
> dwarf
;
327 llvm::once_flag initDwarf
;
330 class BitcodeFile
: public InputFile
{
332 BitcodeFile(Ctx
&, MemoryBufferRef m
, StringRef archiveName
,
333 uint64_t offsetInArchive
, bool lazy
);
334 static bool classof(const InputFile
*f
) { return f
->kind() == BitcodeKind
; }
338 std::unique_ptr
<llvm::lto::InputFile
> obj
;
339 std::vector
<bool> keptComdats
;
343 class SharedFile
: public ELFFileBase
{
345 SharedFile(Ctx
&, MemoryBufferRef m
, StringRef defaultSoName
);
347 // This is actually a vector of Elf_Verdef pointers.
348 SmallVector
<const void *, 0> verdefs
;
350 // If the output file needs Elf_Verneed data structures for this file, this is
351 // a vector of Elf_Vernaux version identifiers that map onto the entries in
352 // Verdefs, otherwise it is empty.
353 SmallVector
<uint32_t, 0> vernauxs
;
355 SmallVector
<StringRef
, 0> dtNeeded
;
358 static bool classof(const InputFile
*f
) { return f
->kind() == SharedKind
; }
360 template <typename ELFT
> void parse();
362 // Used for --as-needed
365 // Non-weak undefined symbols which are not yet resolved when the SO is
366 // parsed. Only filled for `--no-allow-shlib-undefined`.
367 SmallVector
<Symbol
*, 0> requiredSymbols
;
370 template <typename ELFT
>
371 std::vector
<uint32_t> parseVerneed(const llvm::object::ELFFile
<ELFT
> &obj
,
372 const typename
ELFT::Shdr
*sec
);
375 class BinaryFile
: public InputFile
{
377 explicit BinaryFile(Ctx
&ctx
, MemoryBufferRef m
)
378 : InputFile(ctx
, BinaryKind
, m
) {}
379 static bool classof(const InputFile
*f
) { return f
->kind() == BinaryKind
; }
383 InputFile
*createInternalFile(Ctx
&, StringRef name
);
384 std::unique_ptr
<ELFFileBase
> createObjFile(Ctx
&, MemoryBufferRef mb
,
385 StringRef archiveName
= "",
388 std::string
replaceThinLTOSuffix(Ctx
&, StringRef path
);