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"
43 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
44 std::string toString(const elf::InputFile *F);
48 using llvm::object::Archive;
53 // If -reproduce option is given, all input files are written
54 // to this tar archive.
55 extern llvm::TarWriter *Tar;
57 // Opens a given file.
58 llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
60 // The root class of input files.
72 Kind kind() const { return FileKind; }
74 StringRef getName() const { return MB.getBufferIdentifier(); }
77 // Filename of .a which contained this file. If this file was
78 // not in an archive file, it is the empty string. We use this
79 // string for creating error messages.
80 StringRef ArchiveName;
82 // If this file is in an archive, the member contains the offset of
83 // the file in the archive. Otherwise, it's just zero. We store this
84 // field so that we can pass it to lib/LTO in order to disambiguate
86 uint64_t OffsetInArchive;
88 // If this is an architecture-specific file, the following members
89 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
90 ELFKind EKind = ELFNoneKind;
91 uint16_t EMachine = llvm::ELF::EM_NONE;
95 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
101 template <typename ELFT> class ELFFileBase : public InputFile {
103 typedef typename ELFT::Shdr Elf_Shdr;
104 typedef typename ELFT::Sym Elf_Sym;
105 typedef typename ELFT::Word Elf_Word;
106 typedef typename ELFT::SymRange Elf_Sym_Range;
108 ELFFileBase(Kind K, MemoryBufferRef M);
109 static bool classof(const InputFile *F) {
111 return K == ObjectKind || K == SharedKind;
114 llvm::object::ELFFile<ELFT> getObj() const {
115 return llvm::object::ELFFile<ELFT>(MB.getBuffer());
118 StringRef getStringTable() const { return StringTable; }
120 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
122 Elf_Sym_Range getGlobalSymbols();
125 ArrayRef<Elf_Sym> Symbols;
126 uint32_t FirstNonLocal = 0;
127 ArrayRef<Elf_Word> SymtabSHNDX;
128 StringRef StringTable;
129 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
133 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
134 typedef ELFFileBase<ELFT> Base;
135 typedef typename ELFT::Rel Elf_Rel;
136 typedef typename ELFT::Rela Elf_Rela;
137 typedef typename ELFT::Sym Elf_Sym;
138 typedef typename ELFT::Shdr Elf_Shdr;
139 typedef typename ELFT::SymRange Elf_Sym_Range;
140 typedef typename ELFT::Word Elf_Word;
141 typedef typename ELFT::uint uintX_t;
143 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
144 const Elf_Shdr &Sec);
145 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
148 static bool classof(const InputFile *F) {
149 return F->kind() == Base::ObjectKind;
152 ArrayRef<SymbolBody *> getSymbols();
153 ArrayRef<SymbolBody *> getLocalSymbols();
154 ArrayRef<SymbolBody *> getNonLocalSymbols();
156 explicit ObjectFile(MemoryBufferRef M);
157 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
159 ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
160 InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
162 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
163 if (SymbolIndex >= SymbolBodies.size())
164 fatal(toString(this) + ": invalid symbol index");
165 return *SymbolBodies[SymbolIndex];
168 template <typename RelT>
169 SymbolBody &getRelocTargetSym(const RelT &Rel) const {
170 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
171 return getSymbolBody(SymIndex);
174 // Returns source line information for a given offset.
175 // If no information is available, returns "".
176 std::string getLineInfo(InputSectionBase<ELFT> *S, uintX_t Offset);
178 // MIPS GP0 value defined by this file. This value represents the gp value
179 // used to create the relocatable object and required to support
180 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
181 uint32_t MipsGp0 = 0;
183 // Name of source file obtained from STT_FILE symbol value,
184 // or empty string if there is no such symbol in object file
186 StringRef SourceFile;
190 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
191 void initializeSymbols();
192 void initializeDwarfLine();
193 InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
194 InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec,
195 StringRef SectionStringTable);
197 bool shouldMerge(const Elf_Shdr &Sec);
198 SymbolBody *createSymbolBody(const Elf_Sym *Sym);
200 // List of all sections defined by this file.
201 std::vector<InputSectionBase<ELFT> *> Sections;
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 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
224 static bool classof(const InputFile *F) {
225 return F->kind() == LazyObjectKind;
228 template <class ELFT> void parse();
229 MemoryBufferRef getBuffer();
232 std::vector<StringRef> getSymbols();
233 template <class ELFT> std::vector<StringRef> getElfSymbols();
234 std::vector<StringRef> getBitcodeSymbols();
239 // An ArchiveFile object represents a .a file.
240 class ArchiveFile : public InputFile {
242 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
243 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
244 template <class ELFT> void parse();
246 // Returns a memory buffer for a given symbol and the offset in the archive
247 // for the member. An empty memory buffer and an offset of zero
248 // is returned if we have already returned the same memory buffer.
249 // (So that we don't instantiate same members more than once.)
250 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
253 std::unique_ptr<Archive> File;
254 llvm::DenseSet<uint64_t> Seen;
257 class BitcodeFile : public InputFile {
259 explicit BitcodeFile(MemoryBufferRef M);
260 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
261 template <class ELFT>
262 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
263 ArrayRef<Symbol *> getSymbols() { return Symbols; }
264 std::unique_ptr<llvm::lto::InputFile> Obj;
267 std::vector<Symbol *> Symbols;
271 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
272 typedef ELFFileBase<ELFT> Base;
273 typedef typename ELFT::Dyn Elf_Dyn;
274 typedef typename ELFT::Shdr Elf_Shdr;
275 typedef typename ELFT::Sym Elf_Sym;
276 typedef typename ELFT::SymRange Elf_Sym_Range;
277 typedef typename ELFT::Verdef Elf_Verdef;
278 typedef typename ELFT::Versym Elf_Versym;
279 typedef typename ELFT::Word Elf_Word;
280 typedef typename ELFT::uint uintX_t;
282 std::vector<StringRef> Undefs;
284 const Elf_Shdr *VersymSec = nullptr;
285 const Elf_Shdr *VerdefSec = nullptr;
288 StringRef getSoName() const { return SoName; }
289 const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
290 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
292 static bool classof(const InputFile *F) {
293 return F->kind() == Base::SharedKind;
296 explicit SharedFile(MemoryBufferRef M);
300 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
303 // The string table offset of the version name in the output file.
306 // The version identifier for this version name.
310 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
311 // data structures in the output file.
312 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
314 // Used for --as-needed
315 bool AsNeeded = false;
317 bool isNeeded() const { return !AsNeeded || IsUsed; }
320 class BinaryFile : public InputFile {
322 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
323 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
324 template <class ELFT> void parse();
325 ArrayRef<InputSectionData *> getSections() const { return Sections; }
328 std::vector<InputSectionData *> Sections;
331 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
332 uint64_t OffsetInArchive = 0);
333 InputFile *createSharedFile(MemoryBufferRef MB);