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 // The number is the offset in the string table. It will be used as the
184 // st_name of the symbol.
185 std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
187 // Name of source file obtained from STT_FILE symbol value,
188 // or empty string if there is no such symbol in object file
190 StringRef SourceFile;
194 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
195 void initializeSymbols();
196 void initializeDwarfLine();
197 InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
198 InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec,
199 StringRef SectionStringTable);
201 bool shouldMerge(const Elf_Shdr &Sec);
202 SymbolBody *createSymbolBody(const Elf_Sym *Sym);
204 // List of all sections defined by this file.
205 std::vector<InputSectionBase<ELFT> *> Sections;
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 explicit BitcodeFile(MemoryBufferRef M);
264 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
265 template <class ELFT>
266 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
267 ArrayRef<Symbol *> getSymbols() { return Symbols; }
268 std::unique_ptr<llvm::lto::InputFile> Obj;
271 std::vector<Symbol *> Symbols;
275 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
276 typedef ELFFileBase<ELFT> Base;
277 typedef typename ELFT::Dyn Elf_Dyn;
278 typedef typename ELFT::Shdr Elf_Shdr;
279 typedef typename ELFT::Sym Elf_Sym;
280 typedef typename ELFT::SymRange Elf_Sym_Range;
281 typedef typename ELFT::Verdef Elf_Verdef;
282 typedef typename ELFT::Versym Elf_Versym;
283 typedef typename ELFT::Word Elf_Word;
284 typedef typename ELFT::uint uintX_t;
286 std::vector<StringRef> Undefs;
288 const Elf_Shdr *VersymSec = nullptr;
289 const Elf_Shdr *VerdefSec = nullptr;
292 StringRef getSoName() const { return SoName; }
293 const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
294 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
296 static bool classof(const InputFile *F) {
297 return F->kind() == Base::SharedKind;
300 explicit SharedFile(MemoryBufferRef M);
304 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
307 // The string table offset of the version name in the output file.
310 // The version identifier for this version name.
314 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
315 // data structures in the output file.
316 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
318 // Used for --as-needed
319 bool AsNeeded = false;
321 bool isNeeded() const { return !AsNeeded || IsUsed; }
324 class BinaryFile : public InputFile {
326 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
327 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
328 template <class ELFT> void parse();
329 ArrayRef<InputSectionData *> getSections() const { return Sections; }
332 std::vector<InputSectionData *> Sections;
335 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
336 uint64_t OffsetInArchive = 0);
337 InputFile *createSharedFile(MemoryBufferRef MB);