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"
16 #include "lld/Common/LLVM.h"
17 #include "lld/Common/Reproduce.h"
18 #include "llvm/ADT/CachedHashString.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/STLExtras.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"
41 class InputSectionBase;
44 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
45 std::string toString(const elf::InputFile *F);
49 using llvm::object::Archive;
54 // If -reproduce option is given, all input files are written
55 // to this tar archive.
56 extern llvm::TarWriter *Tar;
58 // Opens a given file.
59 llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
61 // The root class of input files.
73 Kind kind() const { return FileKind; }
77 return K == ObjKind || K == SharedKind;
80 StringRef getName() const { return MB.getBufferIdentifier(); }
83 // Returns sections. It is a runtime error to call this function
84 // on files that don't have the notion of sections.
85 ArrayRef<InputSectionBase *> getSections() const {
86 assert(FileKind == ObjKind || FileKind == BinaryKind);
90 // Returns object file symbols. It is a runtime error to call this
91 // function on files of other types.
92 ArrayRef<Symbol *> getSymbols() {
93 assert(FileKind == ObjKind || FileKind == BitcodeKind ||
94 FileKind == ArchiveKind);
98 // Filename of .a which contained this file. If this file was
99 // not in an archive file, it is the empty string. We use this
100 // string for creating error messages.
101 StringRef ArchiveName;
103 // If this is an architecture-specific file, the following members
104 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
105 ELFKind EKind = ELFNoneKind;
106 uint16_t EMachine = llvm::ELF::EM_NONE;
109 // Cache for toString(). Only toString() should use this member.
110 mutable std::string ToStringCache;
112 std::string getSrcMsg(const Symbol &Sym, InputSectionBase &Sec,
116 InputFile(Kind K, MemoryBufferRef M);
117 std::vector<InputSectionBase *> Sections;
118 std::vector<Symbol *> Symbols;
124 template <typename ELFT> class ELFFileBase : public InputFile {
126 typedef typename ELFT::Shdr Elf_Shdr;
127 typedef typename ELFT::Sym Elf_Sym;
128 typedef typename ELFT::Word Elf_Word;
129 typedef typename ELFT::SymRange Elf_Sym_Range;
131 ELFFileBase(Kind K, MemoryBufferRef M);
132 static bool classof(const InputFile *F) { return F->isElf(); }
134 llvm::object::ELFFile<ELFT> getObj() const {
135 return check(llvm::object::ELFFile<ELFT>::create(MB.getBuffer()));
138 StringRef getStringTable() const { return StringTable; }
140 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
142 Elf_Sym_Range getGlobalELFSyms();
143 Elf_Sym_Range getELFSyms() const { return ELFSyms; }
146 ArrayRef<Elf_Sym> ELFSyms;
147 uint32_t FirstNonLocal = 0;
148 ArrayRef<Elf_Word> SymtabSHNDX;
149 StringRef StringTable;
150 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
154 template <class ELFT> class ObjFile : public ELFFileBase<ELFT> {
155 typedef ELFFileBase<ELFT> Base;
156 typedef typename ELFT::Rel Elf_Rel;
157 typedef typename ELFT::Rela Elf_Rela;
158 typedef typename ELFT::Sym Elf_Sym;
159 typedef typename ELFT::Shdr Elf_Shdr;
160 typedef typename ELFT::Word Elf_Word;
162 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
163 const Elf_Shdr &Sec);
164 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
167 static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; }
169 ArrayRef<Symbol *> getLocalSymbols();
170 ArrayRef<Symbol *> getGlobalSymbols();
172 ObjFile(MemoryBufferRef M, StringRef ArchiveName);
173 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
175 Symbol &getSymbol(uint32_t SymbolIndex) const {
176 if (SymbolIndex >= this->Symbols.size())
177 fatal(toString(this) + ": invalid symbol index");
178 return *this->Symbols[SymbolIndex];
181 template <typename RelT> Symbol &getRelocTargetSym(const RelT &Rel) const {
182 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
183 return getSymbol(SymIndex);
186 // Returns source line information for a given offset.
187 // If no information is available, returns "".
188 std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
189 llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
190 llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef Name);
192 // MIPS GP0 value defined by this file. This value represents the gp value
193 // used to create the relocatable object and required to support
194 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
195 uint32_t MipsGp0 = 0;
197 // Name of source file obtained from STT_FILE symbol value,
198 // or empty string if there is no such symbol in object file
200 StringRef SourceFile;
204 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
205 void initializeSymbols();
206 void initializeDwarf();
207 InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
208 InputSectionBase *createInputSection(const Elf_Shdr &Sec);
209 StringRef getSectionName(const Elf_Shdr &Sec);
211 bool shouldMerge(const Elf_Shdr &Sec);
212 Symbol *createSymbol(const Elf_Sym *Sym);
214 // .shstrtab contents.
215 StringRef SectionStringTable;
217 // Debugging information to retrieve source file and line for error
218 // reporting. Linker may find reasonable number of errors in a
219 // single object file, so we cache debugging information in order to
220 // parse it only once for each object file we link.
221 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
222 llvm::DenseMap<StringRef, std::pair<unsigned, unsigned>> VariableLoc;
223 llvm::once_flag InitDwarfLine;
226 // LazyObjFile is analogous to ArchiveFile in the sense that
227 // the file contains lazy symbols. The difference is that
228 // LazyObjFile wraps a single file instead of multiple files.
230 // This class is used for --start-lib and --end-lib options which
231 // instruct the linker to link object files between them with the
232 // archive file semantics.
233 class LazyObjFile : public InputFile {
235 LazyObjFile(MemoryBufferRef M, StringRef ArchiveName,
236 uint64_t OffsetInArchive)
237 : InputFile(LazyObjKind, M), OffsetInArchive(OffsetInArchive) {
238 this->ArchiveName = ArchiveName;
241 static bool classof(const InputFile *F) { return F->kind() == LazyObjKind; }
243 template <class ELFT> void parse();
244 MemoryBufferRef getBuffer();
248 std::vector<StringRef> getSymbolNames();
249 template <class ELFT> std::vector<StringRef> getElfSymbols();
250 std::vector<StringRef> getBitcodeSymbols();
253 uint64_t OffsetInArchive;
256 // An ArchiveFile object represents a .a file.
257 class ArchiveFile : public InputFile {
259 explicit ArchiveFile(std::unique_ptr<Archive> &&File);
260 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
261 template <class ELFT> void parse();
263 // Returns a memory buffer for a given symbol and the offset in the archive
264 // for the member. An empty memory buffer and an offset of zero
265 // is returned if we have already returned the same memory buffer.
266 // (So that we don't instantiate same members more than once.)
267 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
270 std::unique_ptr<Archive> File;
271 llvm::DenseSet<uint64_t> Seen;
274 class BitcodeFile : public InputFile {
276 BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
277 uint64_t OffsetInArchive);
278 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
279 template <class ELFT>
280 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
281 std::unique_ptr<llvm::lto::InputFile> Obj;
285 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
286 typedef ELFFileBase<ELFT> Base;
287 typedef typename ELFT::Dyn Elf_Dyn;
288 typedef typename ELFT::Shdr Elf_Shdr;
289 typedef typename ELFT::Sym Elf_Sym;
290 typedef typename ELFT::SymRange Elf_Sym_Range;
291 typedef typename ELFT::Verdef Elf_Verdef;
292 typedef typename ELFT::Versym Elf_Versym;
294 std::vector<StringRef> Undefs;
295 const Elf_Shdr *VersymSec = nullptr;
296 const Elf_Shdr *VerdefSec = nullptr;
299 std::vector<const Elf_Verdef *> Verdefs;
302 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
304 static bool classof(const InputFile *F) {
305 return F->kind() == Base::SharedKind;
308 SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
312 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
315 // The string table offset of the version name in the output file.
318 // The version identifier for this version name.
322 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
323 // data structures in the output file.
324 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
326 // Used for --as-needed
330 class BinaryFile : public InputFile {
332 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
333 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
337 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
338 uint64_t OffsetInArchive = 0);
339 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
341 extern std::vector<BinaryFile *> BinaryFiles;
342 extern std::vector<BitcodeFile *> BitcodeFiles;
343 extern std::vector<InputFile *> ObjectFiles;
344 extern std::vector<InputFile *> SharedFiles;