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 "InputSection.h"
18 #include "lld/Core/LLVM.h"
19 #include "lld/Core/Reproduce.h"
20 #include "llvm/ADT/CachedHashString.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/IR/Comdat.h"
24 #include "llvm/Object/Archive.h"
25 #include "llvm/Object/ELF.h"
26 #include "llvm/Object/IRObjectFile.h"
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; }
75 StringRef getName() const { return MB.getBufferIdentifier(); }
78 // Returns sections. It is a runtime error to call this function
79 // on files that don't have the notion of sections.
80 ArrayRef<InputSectionBase *> getSections() const {
81 assert(FileKind == ObjectKind || FileKind == BinaryKind);
85 // Filename of .a which contained this file. If this file was
86 // not in an archive file, it is the empty string. We use this
87 // string for creating error messages.
88 StringRef ArchiveName;
90 // If this is an architecture-specific file, the following members
91 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
92 ELFKind EKind = ELFNoneKind;
93 uint16_t EMachine = llvm::ELF::EM_NONE;
96 // Cache for toString(). Only toString() should use this member.
97 mutable std::string ToStringCache;
100 InputFile(Kind K, MemoryBufferRef M);
101 std::vector<InputSectionBase *> Sections;
107 template <typename ELFT> class ELFFileBase : public InputFile {
109 typedef typename ELFT::Shdr Elf_Shdr;
110 typedef typename ELFT::Sym Elf_Sym;
111 typedef typename ELFT::Word Elf_Word;
112 typedef typename ELFT::SymRange Elf_Sym_Range;
114 ELFFileBase(Kind K, MemoryBufferRef M);
115 static bool classof(const InputFile *F) {
117 return K == ObjectKind || K == SharedKind;
120 llvm::object::ELFFile<ELFT> getObj() const {
121 return llvm::object::ELFFile<ELFT>(MB.getBuffer());
124 StringRef getStringTable() const { return StringTable; }
126 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
128 Elf_Sym_Range getGlobalSymbols();
131 ArrayRef<Elf_Sym> Symbols;
132 uint32_t FirstNonLocal = 0;
133 ArrayRef<Elf_Word> SymtabSHNDX;
134 StringRef StringTable;
135 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
139 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
140 typedef ELFFileBase<ELFT> Base;
141 typedef typename ELFT::Rel Elf_Rel;
142 typedef typename ELFT::Rela Elf_Rela;
143 typedef typename ELFT::Sym Elf_Sym;
144 typedef typename ELFT::Shdr Elf_Shdr;
145 typedef typename ELFT::Word Elf_Word;
147 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
148 const Elf_Shdr &Sec);
149 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
152 static bool classof(const InputFile *F) {
153 return F->kind() == Base::ObjectKind;
156 ArrayRef<SymbolBody *> getSymbols();
157 ArrayRef<SymbolBody *> getLocalSymbols();
159 ObjectFile(MemoryBufferRef M, StringRef ArchiveName);
160 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
162 InputSectionBase *getSection(const Elf_Sym &Sym) const;
164 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
165 if (SymbolIndex >= SymbolBodies.size())
166 fatal(toString(this) + ": invalid symbol index");
167 return *SymbolBodies[SymbolIndex];
170 template <typename RelT>
171 SymbolBody &getRelocTargetSym(const RelT &Rel) const {
172 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
173 return getSymbolBody(SymIndex);
176 // Returns source line information for a given offset.
177 // If no information is available, returns "".
178 std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
179 llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
181 // MIPS GP0 value defined by this file. This value represents the gp value
182 // used to create the relocatable object and required to support
183 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
184 uint32_t MipsGp0 = 0;
186 // Name of source file obtained from STT_FILE symbol value,
187 // or empty string if there is no such symbol in object file
189 StringRef SourceFile;
193 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
194 void initializeSymbols();
195 void initializeDwarfLine();
196 InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
197 InputSectionBase *createInputSection(const Elf_Shdr &Sec,
198 StringRef SectionStringTable);
200 bool shouldMerge(const Elf_Shdr &Sec);
201 SymbolBody *createSymbolBody(const Elf_Sym *Sym);
203 // List of all symbols referenced or defined by this file.
204 std::vector<SymbolBody *> SymbolBodies;
206 // Debugging information to retrieve source file and line for error
207 // reporting. Linker may find reasonable number of errors in a
208 // single object file, so we cache debugging information in order to
209 // parse it only once for each object file we link.
210 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
213 // LazyObjectFile is analogous to ArchiveFile in the sense that
214 // the file contains lazy symbols. The difference is that
215 // LazyObjectFile wraps a single file instead of multiple files.
217 // This class is used for --start-lib and --end-lib options which
218 // instruct the linker to link object files between them with the
219 // archive file semantics.
220 class LazyObjectFile : public InputFile {
222 LazyObjectFile(MemoryBufferRef M, StringRef ArchiveName,
223 uint64_t OffsetInArchive)
224 : InputFile(LazyObjectKind, M), OffsetInArchive(OffsetInArchive) {
225 this->ArchiveName = ArchiveName;
228 static bool classof(const InputFile *F) {
229 return F->kind() == LazyObjectKind;
232 template <class ELFT> void parse();
233 MemoryBufferRef getBuffer();
237 std::vector<StringRef> getSymbols();
238 template <class ELFT> std::vector<StringRef> getElfSymbols();
239 std::vector<StringRef> getBitcodeSymbols();
242 uint64_t OffsetInArchive;
245 // An ArchiveFile object represents a .a file.
246 class ArchiveFile : public InputFile {
248 explicit ArchiveFile(std::unique_ptr<Archive> &&File);
249 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
250 template <class ELFT> void parse();
252 // Returns a memory buffer for a given symbol and the offset in the archive
253 // for the member. An empty memory buffer and an offset of zero
254 // is returned if we have already returned the same memory buffer.
255 // (So that we don't instantiate same members more than once.)
256 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
259 std::unique_ptr<Archive> File;
260 llvm::DenseSet<uint64_t> Seen;
263 class BitcodeFile : public InputFile {
265 BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
266 uint64_t OffsetInArchive);
267 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
268 template <class ELFT>
269 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
270 ArrayRef<Symbol *> getSymbols() { return Symbols; }
271 std::unique_ptr<llvm::lto::InputFile> Obj;
274 std::vector<Symbol *> Symbols;
278 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
279 typedef ELFFileBase<ELFT> Base;
280 typedef typename ELFT::Dyn Elf_Dyn;
281 typedef typename ELFT::Shdr Elf_Shdr;
282 typedef typename ELFT::Sym Elf_Sym;
283 typedef typename ELFT::SymRange Elf_Sym_Range;
284 typedef typename ELFT::Verdef Elf_Verdef;
285 typedef typename ELFT::Versym Elf_Versym;
287 std::vector<StringRef> Undefs;
288 const Elf_Shdr *VersymSec = nullptr;
289 const Elf_Shdr *VerdefSec = nullptr;
294 const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
295 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
297 static bool classof(const InputFile *F) {
298 return F->kind() == Base::SharedKind;
301 SharedFile(MemoryBufferRef M, StringRef DefaultSoName);
305 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
308 // The string table offset of the version name in the output file.
311 // The version identifier for this version name.
315 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
316 // data structures in the output file.
317 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
319 // Used for --as-needed
320 bool AsNeeded = false;
322 bool isNeeded() const { return !AsNeeded || IsUsed; }
325 class BinaryFile : public InputFile {
327 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
328 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
329 template <class ELFT> void parse();
332 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
333 uint64_t OffsetInArchive = 0);
334 InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);