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 // For SharedKind inputs, the string to use in DT_NEEDED when the library
98 std::string DefaultSoName;
100 // Cache for toString(). Only toString() should use this member.
101 mutable std::string ToStringCache;
104 InputFile(Kind K, MemoryBufferRef M);
105 std::vector<InputSectionBase *> Sections;
111 template <typename ELFT> class ELFFileBase : public InputFile {
113 typedef typename ELFT::Shdr Elf_Shdr;
114 typedef typename ELFT::Sym Elf_Sym;
115 typedef typename ELFT::Word Elf_Word;
116 typedef typename ELFT::SymRange Elf_Sym_Range;
118 ELFFileBase(Kind K, MemoryBufferRef M);
119 static bool classof(const InputFile *F) {
121 return K == ObjectKind || K == SharedKind;
124 llvm::object::ELFFile<ELFT> getObj() const {
125 return llvm::object::ELFFile<ELFT>(MB.getBuffer());
128 StringRef getStringTable() const { return StringTable; }
130 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
132 Elf_Sym_Range getGlobalSymbols();
135 ArrayRef<Elf_Sym> Symbols;
136 uint32_t FirstNonLocal = 0;
137 ArrayRef<Elf_Word> SymtabSHNDX;
138 StringRef StringTable;
139 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
143 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
144 typedef ELFFileBase<ELFT> Base;
145 typedef typename ELFT::Rel Elf_Rel;
146 typedef typename ELFT::Rela Elf_Rela;
147 typedef typename ELFT::Sym Elf_Sym;
148 typedef typename ELFT::Shdr Elf_Shdr;
149 typedef typename ELFT::Word Elf_Word;
151 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
152 const Elf_Shdr &Sec);
153 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
156 static bool classof(const InputFile *F) {
157 return F->kind() == Base::ObjectKind;
160 ArrayRef<SymbolBody *> getSymbols();
161 ArrayRef<SymbolBody *> getLocalSymbols();
163 explicit ObjectFile(MemoryBufferRef M);
164 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
166 InputSectionBase *getSection(const Elf_Sym &Sym) const;
168 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
169 if (SymbolIndex >= SymbolBodies.size())
170 fatal(toString(this) + ": invalid symbol index");
171 return *SymbolBodies[SymbolIndex];
174 template <typename RelT>
175 SymbolBody &getRelocTargetSym(const RelT &Rel) const {
176 uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
177 return getSymbolBody(SymIndex);
180 // Returns source line information for a given offset.
181 // If no information is available, returns "".
182 std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
183 llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
185 // MIPS GP0 value defined by this file. This value represents the gp value
186 // used to create the relocatable object and required to support
187 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
188 uint32_t MipsGp0 = 0;
190 // Name of source file obtained from STT_FILE symbol value,
191 // or empty string if there is no such symbol in object file
193 StringRef SourceFile;
197 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
198 void initializeSymbols();
199 void initializeDwarfLine();
200 InputSectionBase *getRelocTarget(const Elf_Shdr &Sec);
201 InputSectionBase *createInputSection(const Elf_Shdr &Sec,
202 StringRef SectionStringTable);
204 bool shouldMerge(const Elf_Shdr &Sec);
205 SymbolBody *createSymbolBody(const Elf_Sym *Sym);
207 // List of all symbols referenced or defined by this file.
208 std::vector<SymbolBody *> SymbolBodies;
210 // Debugging information to retrieve source file and line for error
211 // reporting. Linker may find reasonable number of errors in a
212 // single object file, so we cache debugging information in order to
213 // parse it only once for each object file we link.
214 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
217 // LazyObjectFile is analogous to ArchiveFile in the sense that
218 // the file contains lazy symbols. The difference is that
219 // LazyObjectFile wraps a single file instead of multiple files.
221 // This class is used for --start-lib and --end-lib options which
222 // instruct the linker to link object files between them with the
223 // archive file semantics.
224 class LazyObjectFile : public InputFile {
226 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
228 static bool classof(const InputFile *F) {
229 return F->kind() == LazyObjectKind;
232 template <class ELFT> void parse();
233 MemoryBufferRef getBuffer();
236 std::vector<StringRef> getSymbols();
237 template <class ELFT> std::vector<StringRef> getElfSymbols();
238 std::vector<StringRef> getBitcodeSymbols();
243 // An ArchiveFile object represents a .a file.
244 class ArchiveFile : public InputFile {
246 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
247 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
248 template <class ELFT> void parse();
250 // Returns a memory buffer for a given symbol and the offset in the archive
251 // for the member. An empty memory buffer and an offset of zero
252 // is returned if we have already returned the same memory buffer.
253 // (So that we don't instantiate same members more than once.)
254 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
257 std::unique_ptr<Archive> File;
258 llvm::DenseSet<uint64_t> Seen;
261 class BitcodeFile : public InputFile {
263 BitcodeFile(MemoryBufferRef M, StringRef ArchiveName,
264 uint64_t OffsetInArchive);
265 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
266 template <class ELFT>
267 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
268 ArrayRef<Symbol *> getSymbols() { return Symbols; }
269 std::unique_ptr<llvm::lto::InputFile> Obj;
272 std::vector<Symbol *> Symbols;
276 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
277 typedef ELFFileBase<ELFT> Base;
278 typedef typename ELFT::Dyn Elf_Dyn;
279 typedef typename ELFT::Shdr Elf_Shdr;
280 typedef typename ELFT::Sym Elf_Sym;
281 typedef typename ELFT::SymRange Elf_Sym_Range;
282 typedef typename ELFT::Verdef Elf_Verdef;
283 typedef typename ELFT::Versym Elf_Versym;
285 std::vector<StringRef> Undefs;
287 const Elf_Shdr *VersymSec = nullptr;
288 const Elf_Shdr *VerdefSec = nullptr;
291 StringRef getSoName() const;
292 const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
293 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
295 static bool classof(const InputFile *F) {
296 return F->kind() == Base::SharedKind;
299 explicit SharedFile(MemoryBufferRef M);
303 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
306 // The string table offset of the version name in the output file.
309 // The version identifier for this version name.
313 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
314 // data structures in the output file.
315 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
317 // Used for --as-needed
318 bool AsNeeded = false;
320 bool isNeeded() const { return !AsNeeded || IsUsed; }
323 class BinaryFile : public InputFile {
325 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
326 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
327 template <class ELFT> void parse();
330 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
331 uint64_t OffsetInArchive = 0);
332 InputFile *createSharedFile(MemoryBufferRef MB);