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"
34 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
35 std::string
toString(const elf::InputFile
*f
);
42 // If --reproduce is specified, all input files are written to this tar archive.
43 extern std::unique_ptr
<llvm::TarWriter
> tar
;
45 // Opens a given file.
46 llvm::Optional
<MemoryBufferRef
> readFile(StringRef path
);
48 // Add symbols in File to the symbol table.
49 void parseFile(InputFile
*file
);
51 // The root class of input files.
54 SmallVector
<Symbol
*, 0> symbols
;
55 SmallVector
<InputSectionBase
*, 0> sections
;
66 Kind
kind() const { return fileKind
; }
70 return k
== ObjKind
|| k
== SharedKind
;
73 StringRef
getName() const { return mb
.getBufferIdentifier(); }
76 // Returns sections. It is a runtime error to call this function
77 // on files that don't have the notion of sections.
78 ArrayRef
<InputSectionBase
*> getSections() const {
79 assert(fileKind
== ObjKind
|| fileKind
== BinaryKind
);
83 // Returns object file symbols. It is a runtime error to call this
84 // function on files of other types.
85 ArrayRef
<Symbol
*> getSymbols() const {
86 assert(fileKind
== BinaryKind
|| fileKind
== ObjKind
||
87 fileKind
== BitcodeKind
);
91 // Get filename to use for linker script processing.
92 StringRef
getNameForScript() const;
94 // Check if a non-common symbol should be extracted to override a common
96 bool shouldExtractForCommon(StringRef name
);
98 // .got2 in the current file. This is used by PPC32 -fPIC/-fPIE to compute
99 // offsets in PLT call stubs.
100 InputSection
*ppc32Got2
= nullptr;
102 // Index of MIPS GOT built for this file.
103 uint32_t mipsGotIndex
= -1;
105 // groupId is used for --warn-backrefs which is an optional error
106 // checking feature. All files within the same --{start,end}-group or
107 // --{start,end}-lib get the same group ID. Otherwise, each file gets a new
108 // group ID. For more info, see checkDependency() in SymbolTable.cpp.
110 static bool isInGroup
;
111 static uint32_t nextGroupId
;
113 // If this is an architecture-specific file, the following members
114 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
115 uint16_t emachine
= llvm::ELF::EM_NONE
;
117 ELFKind ekind
= ELFNoneKind
;
119 uint8_t abiVersion
= 0;
121 // True if this is a relocatable object file/bitcode file between --start-lib
125 // True if this is an argument for --just-symbols. Usually false.
126 bool justSymbols
= false;
128 std::string
getSrcMsg(const Symbol
&sym
, InputSectionBase
&sec
,
131 // On PPC64 we need to keep track of which files contain small code model
132 // relocations that access the .toc section. To minimize the chance of a
133 // relocation overflow, files that do contain said relocations should have
134 // their .toc sections sorted closer to the .got section than files that do
135 // not contain any small code model relocations. Thats because the toc-pointer
136 // is defined to point at .got + 0x8000 and the instructions used with small
137 // code model relocations support immediates in the range [-0x8000, 0x7FFC],
138 // making the addressable range relative to the toc pointer
139 // [.got, .got + 0xFFFC].
140 bool ppc64SmallCodeModelTocRelocs
= false;
142 // True if the file has TLSGD/TLSLD GOT relocations without R_PPC64_TLSGD or
143 // R_PPC64_TLSLD. Disable TLS relaxation to avoid bad code generation.
144 bool ppc64DisableTLSRelax
= false;
147 InputFile(Kind k
, MemoryBufferRef m
);
150 // If not empty, this stores the name of the archive containing this file.
151 // We use this string for creating error messages.
152 SmallString
<0> archiveName
;
153 // Cache for toString(). Only toString() should use this member.
154 mutable SmallString
<0> toStringCache
;
157 // Cache for getNameForScript().
158 mutable SmallString
<0> nameForScriptCache
;
161 class ELFFileBase
: public InputFile
{
163 ELFFileBase(Kind k
, MemoryBufferRef m
);
164 static bool classof(const InputFile
*f
) { return f
->isElf(); }
166 template <typename ELFT
> llvm::object::ELFFile
<ELFT
> getObj() const {
167 return check(llvm::object::ELFFile
<ELFT
>::create(mb
.getBuffer()));
170 StringRef
getStringTable() const { return stringTable
; }
172 ArrayRef
<Symbol
*> getLocalSymbols() {
175 return llvm::makeArrayRef(symbols
).slice(1, firstGlobal
- 1);
177 ArrayRef
<Symbol
*> getGlobalSymbols() {
178 return llvm::makeArrayRef(symbols
).slice(firstGlobal
);
180 MutableArrayRef
<Symbol
*> getMutableGlobalSymbols() {
181 return llvm::makeMutableArrayRef(symbols
.data(), symbols
.size())
185 template <typename ELFT
> typename
ELFT::ShdrRange
getELFShdrs() const {
186 return typename
ELFT::ShdrRange(
187 reinterpret_cast<const typename
ELFT::Shdr
*>(elfShdrs
), numELFShdrs
);
189 template <typename ELFT
> typename
ELFT::SymRange
getELFSyms() const {
190 return typename
ELFT::SymRange(
191 reinterpret_cast<const typename
ELFT::Sym
*>(elfSyms
), numELFSyms
);
193 template <typename ELFT
> typename
ELFT::SymRange
getGlobalELFSyms() const {
194 return getELFSyms
<ELFT
>().slice(firstGlobal
);
198 // Initializes this class's member variables.
199 template <typename ELFT
> void init();
201 StringRef stringTable
;
202 const void *elfShdrs
= nullptr;
203 const void *elfSyms
= nullptr;
204 uint32_t numELFShdrs
= 0;
205 uint32_t numELFSyms
= 0;
206 uint32_t firstGlobal
= 0;
209 uint32_t andFeatures
= 0;
210 bool hasCommonSyms
= false;
214 template <class ELFT
> class ObjFile
: public ELFFileBase
{
215 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
218 static bool classof(const InputFile
*f
) { return f
->kind() == ObjKind
; }
220 llvm::object::ELFFile
<ELFT
> getObj() const {
221 return this->ELFFileBase::getObj
<ELFT
>();
224 ObjFile(MemoryBufferRef m
, StringRef archiveName
) : ELFFileBase(ObjKind
, m
) {
225 this->archiveName
= archiveName
;
228 void parse(bool ignoreComdats
= false);
231 StringRef
getShtGroupSignature(ArrayRef
<Elf_Shdr
> sections
,
232 const Elf_Shdr
&sec
);
234 Symbol
&getSymbol(uint32_t symbolIndex
) const {
235 if (symbolIndex
>= this->symbols
.size())
236 fatal(toString(this) + ": invalid symbol index");
237 return *this->symbols
[symbolIndex
];
240 uint32_t getSectionIndex(const Elf_Sym
&sym
) const;
242 template <typename RelT
> Symbol
&getRelocTargetSym(const RelT
&rel
) const {
243 uint32_t symIndex
= rel
.getSymbol(config
->isMips64EL
);
244 return getSymbol(symIndex
);
247 llvm::Optional
<llvm::DILineInfo
> getDILineInfo(InputSectionBase
*, uint64_t);
248 llvm::Optional
<std::pair
<std::string
, unsigned>> getVariableLoc(StringRef name
);
250 // Name of source file obtained from STT_FILE symbol value,
251 // or empty string if there is no such symbol in object file
253 StringRef sourceFile
;
255 // Pointer to this input file's .llvm_addrsig section, if it has one.
256 const Elf_Shdr
*addrsigSec
= nullptr;
258 // SHT_LLVM_CALL_GRAPH_PROFILE section index.
259 uint32_t cgProfileSectionIndex
= 0;
261 // MIPS GP0 value defined by this file. This value represents the gp value
262 // used to create the relocatable object and required to support
263 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
264 uint32_t mipsGp0
= 0;
266 // True if the file defines functions compiled with
267 // -fsplit-stack. Usually false.
268 bool splitStack
= false;
270 // True if the file defines functions compiled with -fsplit-stack,
271 // but had one or more functions with the no_split_stack attribute.
272 bool someNoSplitStack
= false;
274 // Get cached DWARF information.
275 DWARFCache
*getDwarf();
277 void initSectionsAndLocalSyms(bool ignoreComdats
);
281 void initializeSections(bool ignoreComdats
,
282 const llvm::object::ELFFile
<ELFT
> &obj
);
283 void initializeSymbols(const llvm::object::ELFFile
<ELFT
> &obj
);
284 void initializeJustSymbols();
286 InputSectionBase
*getRelocTarget(uint32_t idx
, const Elf_Shdr
&sec
,
288 InputSectionBase
*createInputSection(uint32_t idx
, const Elf_Shdr
&sec
,
291 bool shouldMerge(const Elf_Shdr
&sec
, StringRef name
);
293 // Each ELF symbol contains a section index which the symbol belongs to.
294 // However, because the number of bits dedicated for that is limited, a
295 // symbol can directly point to a section only when the section index is
296 // equal to or smaller than 65280.
298 // If an object file contains more than 65280 sections, the file must
299 // contain .symtab_shndx section. The section contains an array of
300 // 32-bit integers whose size is the same as the number of symbols.
301 // Nth symbol's section index is in the Nth entry of .symtab_shndx.
303 // The following variable contains the contents of .symtab_shndx.
304 // If the section does not exist (which is common), the array is empty.
305 ArrayRef
<Elf_Word
> shndxTable
;
307 // Debugging information to retrieve source file and line for error
308 // reporting. Linker may find reasonable number of errors in a
309 // single object file, so we cache debugging information in order to
310 // parse it only once for each object file we link.
311 std::unique_ptr
<DWARFCache
> dwarf
;
312 llvm::once_flag initDwarf
;
315 class BitcodeFile
: public InputFile
{
317 BitcodeFile(MemoryBufferRef m
, StringRef archiveName
,
318 uint64_t offsetInArchive
, bool lazy
);
319 static bool classof(const InputFile
*f
) { return f
->kind() == BitcodeKind
; }
320 template <class ELFT
> void parse();
323 std::unique_ptr
<llvm::lto::InputFile
> obj
;
324 std::vector
<bool> keptComdats
;
328 class SharedFile
: public ELFFileBase
{
330 SharedFile(MemoryBufferRef m
, StringRef defaultSoName
)
331 : ELFFileBase(SharedKind
, m
), soName(defaultSoName
),
332 isNeeded(!config
->asNeeded
) {}
334 // This is actually a vector of Elf_Verdef pointers.
335 SmallVector
<const void *, 0> verdefs
;
337 // If the output file needs Elf_Verneed data structures for this file, this is
338 // a vector of Elf_Vernaux version identifiers that map onto the entries in
339 // Verdefs, otherwise it is empty.
340 SmallVector
<uint32_t, 0> vernauxs
;
342 static unsigned vernauxNum
;
344 SmallVector
<StringRef
, 0> dtNeeded
;
347 static bool classof(const InputFile
*f
) { return f
->kind() == SharedKind
; }
349 template <typename ELFT
> void parse();
351 // Used for --as-needed
354 // Non-weak undefined symbols which are not yet resolved when the SO is
355 // parsed. Only filled for `--no-allow-shlib-undefined`.
356 SmallVector
<Symbol
*, 0> requiredSymbols
;
359 template <typename ELFT
>
360 std::vector
<uint32_t> parseVerneed(const llvm::object::ELFFile
<ELFT
> &obj
,
361 const typename
ELFT::Shdr
*sec
);
364 class BinaryFile
: public InputFile
{
366 explicit BinaryFile(MemoryBufferRef m
) : InputFile(BinaryKind
, m
) {}
367 static bool classof(const InputFile
*f
) { return f
->kind() == BinaryKind
; }
371 ELFFileBase
*createObjFile(MemoryBufferRef mb
, StringRef archiveName
= "",
374 std::string
replaceThinLTOSuffix(StringRef path
);