1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_INPUT_FILES_H
11 #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/CachedHashString.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
21 #include "llvm/IR/Comdat.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/ELF.h"
24 #include "llvm/Object/IRObjectFile.h"
25 #include "llvm/Support/Threading.h"
39 class InputSectionBase;
42 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
43 std::string toString(const elf::InputFile *F);
47 using llvm::object::Archive;
51 // If -reproduce option is given, all input files are written
52 // to this tar archive.
53 extern llvm::TarWriter *Tar;
55 // Opens a given file.
56 llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
58 // The root class of input files.
70 Kind kind() const { return FileKind; }
74 return K == ObjKind || K == SharedKind;
77 StringRef getName() const { return MB.getBufferIdentifier(); }
80 // Returns sections. It is a runtime error to call this function
81 // on files that don't have the notion of sections.
82 ArrayRef<InputSectionBase *> getSections() const {
83 assert(FileKind == ObjKind || FileKind == BinaryKind);
87 // Returns object file symbols. It is a runtime error to call this
88 // function on files of other types.
89 ArrayRef<Symbol *> getSymbols() {
90 assert(FileKind == BinaryKind || FileKind == ObjKind ||
91 FileKind == BitcodeKind);
95 // Filename of .a which contained this file. If this file was
96 // not in an archive file, it is the empty string. We use this
97 // string for creating error messages.
98 std::string ArchiveName;
100 // If this is an architecture-specific file, the following members
101 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
102 ELFKind EKind = ELFNoneKind;
103 uint16_t EMachine = llvm::ELF::EM_NONE;
106 // Cache for toString(). Only toString() should use this member.
107 mutable std::string ToStringCache;
109 std::string getSrcMsg(const Symbol &Sym, InputSectionBase &Sec,
112 // True if this is an argument for --just-symbols. Usually false.
113 bool JustSymbols = false;
115 // GroupId is used for --warn-backrefs which is an optional error
116 // checking feature. All files within the same --{start,end}-group or
117 // --{start,end}-lib get the same group ID. Otherwise, each file gets a new
118 // group ID. For more info, see checkDependency() in SymbolTable.cpp.
120 static bool IsInGroup;
121 static uint32_t NextGroupId;
123 // Index of MIPS GOT built for this file.
124 llvm::Optional<size_t> MipsGotIndex;
127 InputFile(Kind K, MemoryBufferRef M);
128 std::vector<InputSectionBase *> Sections;
129 std::vector<Symbol *> Symbols;
135 template <typename ELFT> class ELFFileBase : public InputFile {
137 typedef typename ELFT::Shdr Elf_Shdr;
138 typedef typename ELFT::Sym Elf_Sym;
139 typedef typename ELFT::Word Elf_Word;
140 typedef typename ELFT::SymRange Elf_Sym_Range;
142 ELFFileBase(Kind K, MemoryBufferRef M);
143 static bool classof(const InputFile *F) { return F->isElf(); }
145 llvm::object::ELFFile<ELFT> getObj() const {
146 return check(llvm::object::ELFFile<ELFT>::create(MB.getBuffer()));
149 StringRef getStringTable() const { return StringTable; }
151 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
153 Elf_Sym_Range getGlobalELFSyms();
154 Elf_Sym_Range getELFSyms() const { return ELFSyms; }
157 ArrayRef<Elf_Sym> ELFSyms;
158 uint32_t FirstGlobal = 0;
159 ArrayRef<Elf_Word> SymtabSHNDX;
160 StringRef StringTable;
161 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
165 template <class ELFT> class ObjFile : public ELFFileBase<ELFT> {
166 typedef ELFFileBase<ELFT> Base;
167 typedef typename ELFT::Rel Elf_Rel;
168 typedef typename ELFT::Rela Elf_Rela;
169 typedef typename ELFT::Sym Elf_Sym;
170 typedef typename ELFT::Shdr Elf_Shdr;
171 typedef typename ELFT::Word Elf_Word;
173 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
174 const Elf_Shdr &Sec);
175 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
178 static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; }
180 ArrayRef<Symbol *> getLocalSymbols();
181 ArrayRef<Symbol *> getGlobalSymbols();
183 ObjFile(MemoryBufferRef M, StringRef ArchiveName);
184 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
186 Symbol &getSymbol(uint32_t SymbolIndex) const {
187 if (SymbolIndex >= this->Symbols.size())
188 fatal(toString(this) + ": invalid symbol index");
189 return *this->Symbols[SymbolIndex];
192 template <typename RelT> Symbol &getRelocTargetSym(const RelT &Rel) const {
193 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
194 return getSymbol(SymIndex);
197 llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
198 llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef Name);
200 // MIPS GP0 value defined by this file. This value represents the gp value
201 // used to create the relocatable object and required to support
202 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
203 uint32_t MipsGp0 = 0;
205 // Name of source file obtained from STT_FILE symbol value,
206 // or empty string if there is no such symbol in object file
208 StringRef SourceFile;
210 // True if the file defines functions compiled with
211 // -fsplit-stack. Usually false.
212 bool SplitStack = false;
214 // True if the file defines functions compiled with -fsplit-stack,
215 // but had one or more functions with the no_split_stack attribute.
216 bool SomeNoSplitStack = false;
218 // Pointer to this input file's .llvm_addrsig section, if it has one.
219 const Elf_Shdr *AddrsigSec = nullptr;
223 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
224 void initializeSymbols();
225 void initializeJustSymbols();
226 void initializeDwarf();
227 InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
228 InputSectionBase *createInputSection(const Elf_Shdr &Sec);
229 StringRef getSectionName(const Elf_Shdr &Sec);
231 bool shouldMerge(const Elf_Shdr &Sec);
232 Symbol *createSymbol(const Elf_Sym *Sym);
234 // .shstrtab contents.
235 StringRef SectionStringTable;
237 // Debugging information to retrieve source file and line for error
238 // reporting. Linker may find reasonable number of errors in a
239 // single object file, so we cache debugging information in order to
240 // parse it only once for each object file we link.
241 std::unique_ptr<llvm::DWARFContext> Dwarf;
242 std::vector<const llvm::DWARFDebugLine::LineTable *> LineTables;
244 const llvm::DWARFDebugLine::LineTable *LT;
248 llvm::DenseMap<StringRef, VarLoc> VariableLoc;
249 llvm::once_flag InitDwarfLine;
252 // LazyObjFile is analogous to ArchiveFile in the sense that
253 // the file contains lazy symbols. The difference is that
254 // LazyObjFile wraps a single file instead of multiple files.
256 // This class is used for --start-lib and --end-lib options which
257 // instruct the linker to link object files between them with the
258 // archive file semantics.
259 class LazyObjFile : public InputFile {
261 LazyObjFile(MemoryBufferRef M, StringRef ArchiveName,
262 uint64_t OffsetInArchive)
263 : InputFile(LazyObjKind, M), OffsetInArchive(OffsetInArchive) {
264 this->ArchiveName = ArchiveName;
267 static bool classof(const InputFile *F) { return F->kind() == LazyObjKind; }
269 template <class ELFT> void parse();
270 MemoryBufferRef getBuffer();
272 bool AddedToLink = false;
275 template <class ELFT> void addElfSymbols();
277 uint64_t OffsetInArchive;
280 // An ArchiveFile object represents a .a file.
281 class ArchiveFile : public InputFile {
283 explicit ArchiveFile(std::unique_ptr<Archive> &&File);
284 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
285 template <class ELFT> void parse();
287 // Pulls out an object file that contains a definition for Sym and
288 // returns it. If the same file was instantiated before, this
289 // function returns a nullptr (so we don't instantiate the same file
291 InputFile *fetch(const Archive::Symbol &Sym);
294 std::unique_ptr<Archive> File;
295 llvm::DenseSet<uint64_t> Seen;
298 class BitcodeFile : public InputFile {
300 BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
301 uint64_t OffsetInArchive);
302 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
303 template <class ELFT>
304 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
305 std::unique_ptr<llvm::lto::InputFile> Obj;
309 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
310 typedef ELFFileBase<ELFT> Base;
311 typedef typename ELFT::Dyn Elf_Dyn;
312 typedef typename ELFT::Shdr Elf_Shdr;
313 typedef typename ELFT::Sym Elf_Sym;
314 typedef typename ELFT::SymRange Elf_Sym_Range;
315 typedef typename ELFT::Verdef Elf_Verdef;
316 typedef typename ELFT::Versym Elf_Versym;
318 const Elf_Shdr *VersymSec = nullptr;
319 const Elf_Shdr *VerdefSec = nullptr;
322 std::vector<const Elf_Verdef *> Verdefs;
325 static bool classof(const InputFile *F) {
326 return F->kind() == Base::SharedKind;
329 SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
333 uint32_t getAlignment(ArrayRef<Elf_Shdr> Sections, const Elf_Sym &Sym);
334 std::vector<const Elf_Verdef *> parseVerdefs();
335 std::vector<uint32_t> parseVersyms();
338 // The string table offset of the version name in the output file.
341 // The version identifier for this version name.
345 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
346 // data structures in the output file.
347 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
349 // Used for --as-needed
353 class BinaryFile : public InputFile {
355 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
356 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
360 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
361 uint64_t OffsetInArchive = 0);
362 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
364 inline bool isBitcode(MemoryBufferRef MB) {
365 return identify_magic(MB.getBuffer()) == llvm::file_magic::bitcode;
368 std::string replaceThinLTOSuffix(StringRef Path);
370 extern std::vector<BinaryFile *> BinaryFiles;
371 extern std::vector<BitcodeFile *> BitcodeFiles;
372 extern std::vector<LazyObjFile *> LazyObjFiles;
373 extern std::vector<InputFile *> ObjectFiles;
374 extern std::vector<InputFile *> SharedFiles;