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"
42 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
43 std::string toString(const elf::InputFile *F);
47 using llvm::object::Archive;
52 // The root class of input files.
64 Kind kind() const { return FileKind; }
66 StringRef getName() const { return MB.getBufferIdentifier(); }
69 // Filename of .a which contained this file. If this file was
70 // not in an archive file, it is the empty string. We use this
71 // string for creating error messages.
72 StringRef ArchiveName;
74 // If this file is in an archive, the member contains the offset of
75 // the file in the archive. Otherwise, it's just zero. We store this
76 // field so that we can pass it to lib/LTO in order to disambiguate
78 uint64_t OffsetInArchive;
80 // If this is an architecture-specific file, the following members
81 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
82 ELFKind EKind = ELFNoneKind;
83 uint16_t EMachine = llvm::ELF::EM_NONE;
87 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
93 template <typename ELFT> class ELFFileBase : public InputFile {
95 typedef typename ELFT::Shdr Elf_Shdr;
96 typedef typename ELFT::Sym Elf_Sym;
97 typedef typename ELFT::Word Elf_Word;
98 typedef typename ELFT::SymRange Elf_Sym_Range;
100 ELFFileBase(Kind K, MemoryBufferRef M);
101 static bool classof(const InputFile *F) {
103 return K == ObjectKind || K == SharedKind;
106 llvm::object::ELFFile<ELFT> getObj() const {
107 return llvm::object::ELFFile<ELFT>(MB.getBuffer());
110 StringRef getStringTable() const { return StringTable; }
112 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
114 Elf_Sym_Range getGlobalSymbols();
117 ArrayRef<Elf_Sym> Symbols;
118 uint32_t FirstNonLocal = 0;
119 ArrayRef<Elf_Word> SymtabSHNDX;
120 StringRef StringTable;
121 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
125 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
126 typedef ELFFileBase<ELFT> Base;
127 typedef typename ELFT::Rel Elf_Rel;
128 typedef typename ELFT::Rela Elf_Rela;
129 typedef typename ELFT::Sym Elf_Sym;
130 typedef typename ELFT::Shdr Elf_Shdr;
131 typedef typename ELFT::SymRange Elf_Sym_Range;
132 typedef typename ELFT::Word Elf_Word;
133 typedef typename ELFT::uint uintX_t;
135 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
136 const Elf_Shdr &Sec);
137 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
140 static bool classof(const InputFile *F) {
141 return F->kind() == Base::ObjectKind;
144 ArrayRef<SymbolBody *> getSymbols();
145 ArrayRef<SymbolBody *> getLocalSymbols();
146 ArrayRef<SymbolBody *> getNonLocalSymbols();
148 explicit ObjectFile(MemoryBufferRef M);
149 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
151 ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
152 InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
154 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
155 if (SymbolIndex >= SymbolBodies.size())
156 fatal(toString(this) + ": invalid symbol index");
157 return *SymbolBodies[SymbolIndex];
160 template <typename RelT>
161 SymbolBody &getRelocTargetSym(const RelT &Rel) const {
162 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
163 return getSymbolBody(SymIndex);
166 // Returns source line information for a given offset.
167 // If no information is available, returns "".
168 std::string getLineInfo(InputSectionBase<ELFT> *S, uintX_t Offset);
170 // MIPS GP0 value defined by this file. This value represents the gp value
171 // used to create the relocatable object and required to support
172 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
173 uint32_t MipsGp0 = 0;
175 // The number is the offset in the string table. It will be used as the
176 // st_name of the symbol.
177 std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
179 // Name of source file obtained from STT_FILE symbol value,
180 // or empty string if there is no such symbol in object file
182 StringRef SourceFile;
186 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
187 void initializeSymbols();
188 void initializeDwarfLine();
189 InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
190 InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec,
191 StringRef SectionStringTable);
193 bool shouldMerge(const Elf_Shdr &Sec);
194 SymbolBody *createSymbolBody(const Elf_Sym *Sym);
196 // List of all sections defined by this file.
197 std::vector<InputSectionBase<ELFT> *> Sections;
199 // List of all symbols referenced or defined by this file.
200 std::vector<SymbolBody *> SymbolBodies;
202 // Debugging information to retrieve source file and line for error
203 // reporting. Linker may find reasonable number of errors in a
204 // single object file, so we cache debugging information in order to
205 // parse it only once for each object file we link.
206 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
209 // LazyObjectFile is analogous to ArchiveFile in the sense that
210 // the file contains lazy symbols. The difference is that
211 // LazyObjectFile wraps a single file instead of multiple files.
213 // This class is used for --start-lib and --end-lib options which
214 // instruct the linker to link object files between them with the
215 // archive file semantics.
216 class LazyObjectFile : public InputFile {
218 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
220 static bool classof(const InputFile *F) {
221 return F->kind() == LazyObjectKind;
224 template <class ELFT> void parse();
225 MemoryBufferRef getBuffer();
228 std::vector<StringRef> getSymbols();
229 template <class ELFT> std::vector<StringRef> getElfSymbols();
230 std::vector<StringRef> getBitcodeSymbols();
235 // An ArchiveFile object represents a .a file.
236 class ArchiveFile : public InputFile {
238 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
239 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
240 template <class ELFT> void parse();
242 // Returns a memory buffer for a given symbol and the offset in the archive
243 // for the member. An empty memory buffer and an offset of zero
244 // is returned if we have already returned the same memory buffer.
245 // (So that we don't instantiate same members more than once.)
246 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
249 std::unique_ptr<Archive> File;
250 llvm::DenseSet<uint64_t> Seen;
253 class BitcodeFile : public InputFile {
255 explicit BitcodeFile(MemoryBufferRef M);
256 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
257 template <class ELFT>
258 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
259 ArrayRef<Symbol *> getSymbols() { return Symbols; }
260 std::unique_ptr<llvm::lto::InputFile> Obj;
263 std::vector<Symbol *> Symbols;
267 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
268 typedef ELFFileBase<ELFT> Base;
269 typedef typename ELFT::Dyn Elf_Dyn;
270 typedef typename ELFT::Shdr Elf_Shdr;
271 typedef typename ELFT::Sym Elf_Sym;
272 typedef typename ELFT::SymRange Elf_Sym_Range;
273 typedef typename ELFT::Verdef Elf_Verdef;
274 typedef typename ELFT::Versym Elf_Versym;
275 typedef typename ELFT::Word Elf_Word;
276 typedef typename ELFT::uint uintX_t;
278 std::vector<StringRef> Undefs;
280 const Elf_Shdr *VersymSec = nullptr;
281 const Elf_Shdr *VerdefSec = nullptr;
284 StringRef getSoName() const { return SoName; }
285 const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
286 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
288 static bool classof(const InputFile *F) {
289 return F->kind() == Base::SharedKind;
292 explicit SharedFile(MemoryBufferRef M);
296 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
299 // The string table offset of the version name in the output file.
302 // The version identifier for this version name.
306 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
307 // data structures in the output file.
308 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
310 // Used for --as-needed
311 bool AsNeeded = false;
313 bool isNeeded() const { return !AsNeeded || IsUsed; }
316 class BinaryFile : public InputFile {
318 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
319 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
320 template <class ELFT> void parse();
321 ArrayRef<InputSectionData *> getSections() const { return Sections; }
324 std::vector<InputSectionData *> Sections;
327 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
328 uint64_t OffsetInArchive = 0);
329 InputFile *createSharedFile(MemoryBufferRef MB);