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 "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/IR/Comdat.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/ELF.h"
24 #include "llvm/Object/IRObjectFile.h"
25 #include "llvm/Support/StringSaver.h"
32 using llvm::object::Archive;
38 // The root class of input files.
49 Kind kind() const { return FileKind; }
51 StringRef getName() const { return MB.getBufferIdentifier(); }
54 // Filename of .a which contained this file. If this file was
55 // not in an archive file, it is the empty string. We use this
56 // string for creating error messages.
57 StringRef ArchiveName;
59 // If this is an architecture-specific file, the following members
60 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
61 ELFKind EKind = ELFNoneKind;
62 uint16_t EMachine = llvm::ELF::EM_NONE;
65 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
71 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
72 std::string getFilename(const InputFile *F);
74 template <typename ELFT> class ELFFileBase : public InputFile {
76 typedef typename ELFT::Shdr Elf_Shdr;
77 typedef typename ELFT::Sym Elf_Sym;
78 typedef typename ELFT::Word Elf_Word;
79 typedef typename ELFT::SymRange Elf_Sym_Range;
81 ELFFileBase(Kind K, MemoryBufferRef M);
82 static bool classof(const InputFile *F) {
84 return K == ObjectKind || K == SharedKind;
87 const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; }
88 llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; }
90 uint8_t getOSABI() const {
91 return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
94 StringRef getStringTable() const { return StringTable; }
96 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
98 Elf_Sym_Range getElfSymbols(bool OnlyGlobals);
101 llvm::object::ELFFile<ELFT> ELFObj;
102 const Elf_Shdr *Symtab = nullptr;
103 ArrayRef<Elf_Word> SymtabSHNDX;
104 StringRef StringTable;
105 void initStringTable();
109 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
110 typedef ELFFileBase<ELFT> Base;
111 typedef typename ELFT::Sym Elf_Sym;
112 typedef typename ELFT::Shdr Elf_Shdr;
113 typedef typename ELFT::SymRange Elf_Sym_Range;
114 typedef typename ELFT::Word Elf_Word;
115 typedef typename ELFT::uint uintX_t;
117 StringRef getShtGroupSignature(const Elf_Shdr &Sec);
118 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
121 static bool classof(const InputFile *F) {
122 return F->kind() == Base::ObjectKind;
125 ArrayRef<SymbolBody *> getSymbols();
126 ArrayRef<SymbolBody *> getLocalSymbols();
127 ArrayRef<SymbolBody *> getNonLocalSymbols();
129 explicit ObjectFile(MemoryBufferRef M);
130 void parse(llvm::DenseSet<StringRef> &ComdatGroups);
132 ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
133 InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
135 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
136 return *SymbolBodies[SymbolIndex];
139 template <typename RelT> SymbolBody &getRelocTargetSym(const RelT &Rel) const {
140 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
141 return getSymbolBody(SymIndex);
144 const Elf_Shdr *getSymbolTable() const { return this->Symtab; };
146 // Get MIPS GP0 value defined by this file. This value represents the gp value
147 // used to create the relocatable object and required to support
148 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
149 uint32_t getMipsGp0() const;
151 // The number is the offset in the string table. It will be used as the
152 // st_name of the symbol.
153 std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
155 // SymbolBodies and Thunks for sections in this file are allocated
156 // using this buffer.
157 llvm::BumpPtrAllocator Alloc;
160 void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups);
161 void initializeSymbols();
162 InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
163 InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec);
165 bool shouldMerge(const Elf_Shdr &Sec);
166 SymbolBody *createSymbolBody(const Elf_Sym *Sym);
168 // List of all sections defined by this file.
169 std::vector<InputSectionBase<ELFT> *> Sections;
171 // List of all symbols referenced or defined by this file.
172 std::vector<SymbolBody *> SymbolBodies;
174 // MIPS .reginfo section defined by this file.
175 std::unique_ptr<MipsReginfoInputSection<ELFT>> MipsReginfo;
176 // MIPS .MIPS.options section defined by this file.
177 std::unique_ptr<MipsOptionsInputSection<ELFT>> MipsOptions;
179 llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc;
180 llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
181 llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> EHAlloc;
184 // LazyObjectFile is analogous to ArchiveFile in the sense that
185 // the file contains lazy symbols. The difference is that
186 // LazyObjectFile wraps a single file instead of multiple files.
188 // This class is used for --start-lib and --end-lib options which
189 // instruct the linker to link object files between them with the
190 // archive file semantics.
191 class LazyObjectFile : public InputFile {
193 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
195 static bool classof(const InputFile *F) {
196 return F->kind() == LazyObjectKind;
199 template <class ELFT> void parse();
200 MemoryBufferRef getBuffer();
203 std::vector<StringRef> getSymbols();
204 template <class ELFT> std::vector<StringRef> getElfSymbols();
205 std::vector<StringRef> getBitcodeSymbols();
207 llvm::BumpPtrAllocator Alloc;
208 llvm::StringSaver Saver{Alloc};
212 // An ArchiveFile object represents a .a file.
213 class ArchiveFile : public InputFile {
215 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
216 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
217 template <class ELFT> void parse();
219 // Returns a memory buffer for a given symbol. An empty memory buffer
220 // is returned if we have already returned the same memory buffer.
221 // (So that we don't instantiate same members more than once.)
222 MemoryBufferRef getMember(const Archive::Symbol *Sym);
225 std::unique_ptr<Archive> File;
226 llvm::DenseSet<uint64_t> Seen;
229 class BitcodeFile : public InputFile {
231 explicit BitcodeFile(MemoryBufferRef M);
232 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
233 template <class ELFT>
234 void parse(llvm::DenseSet<StringRef> &ComdatGroups);
235 ArrayRef<Symbol *> getSymbols() { return Symbols; }
236 static bool shouldSkip(uint32_t Flags);
237 std::unique_ptr<llvm::object::IRObjectFile> Obj;
240 std::vector<Symbol *> Symbols;
241 llvm::BumpPtrAllocator Alloc;
242 llvm::StringSaver Saver{Alloc};
243 template <class ELFT>
244 Symbol *createSymbol(const llvm::DenseSet<const llvm::Comdat *> &KeptComdats,
245 const llvm::object::IRObjectFile &Obj,
246 const llvm::object::BasicSymbolRef &Sym);
250 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
251 typedef ELFFileBase<ELFT> Base;
252 typedef typename ELFT::Shdr Elf_Shdr;
253 typedef typename ELFT::Sym Elf_Sym;
254 typedef typename ELFT::Word Elf_Word;
255 typedef typename ELFT::SymRange Elf_Sym_Range;
256 typedef typename ELFT::Versym Elf_Versym;
257 typedef typename ELFT::Verdef Elf_Verdef;
259 std::vector<StringRef> Undefs;
261 const Elf_Shdr *VersymSec = nullptr;
262 const Elf_Shdr *VerdefSec = nullptr;
265 StringRef getSoName() const { return SoName; }
266 const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
267 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
269 static bool classof(const InputFile *F) {
270 return F->kind() == Base::SharedKind;
273 explicit SharedFile(MemoryBufferRef M);
277 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
280 // The string table offset of the version name in the output file.
283 // The version identifier for this version name.
287 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
288 // data structures in the output file.
289 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
291 // Used for --as-needed
292 bool AsNeeded = false;
294 bool isNeeded() const { return !AsNeeded || IsUsed; }
297 std::unique_ptr<InputFile> createObjectFile(MemoryBufferRef MB,
298 StringRef ArchiveName = "");
299 std::unique_ptr<InputFile> createSharedFile(MemoryBufferRef MB);