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"
40 using llvm::object::Archive;
46 // The root class of input files.
58 Kind kind() const { return FileKind; }
60 StringRef getName() const { return MB.getBufferIdentifier(); }
63 // Filename of .a which contained this file. If this file was
64 // not in an archive file, it is the empty string. We use this
65 // string for creating error messages.
66 StringRef ArchiveName;
68 // If this file is in an archive, the member contains the offset of
69 // the file in the archive. Otherwise, it's just zero. We store this
70 // field so that we can pass it to lib/LTO in order to disambiguate
72 uint64_t OffsetInArchive;
74 // If this is an architecture-specific file, the following members
75 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
76 ELFKind EKind = ELFNoneKind;
77 uint16_t EMachine = llvm::ELF::EM_NONE;
81 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
87 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
88 std::string toString(const InputFile *F);
90 template <typename ELFT> class ELFFileBase : public InputFile {
92 typedef typename ELFT::Shdr Elf_Shdr;
93 typedef typename ELFT::Sym Elf_Sym;
94 typedef typename ELFT::Word Elf_Word;
95 typedef typename ELFT::SymRange Elf_Sym_Range;
97 ELFFileBase(Kind K, MemoryBufferRef M);
98 static bool classof(const InputFile *F) {
100 return K == ObjectKind || K == SharedKind;
103 llvm::object::ELFFile<ELFT> getObj() const {
104 return llvm::object::ELFFile<ELFT>(MB.getBuffer());
107 StringRef getStringTable() const { return StringTable; }
109 uint32_t getSectionIndex(const Elf_Sym &Sym) const;
111 Elf_Sym_Range getGlobalSymbols();
114 ArrayRef<Elf_Sym> Symbols;
115 uint32_t FirstNonLocal = 0;
116 ArrayRef<Elf_Word> SymtabSHNDX;
117 StringRef StringTable;
118 void initSymtab(ArrayRef<Elf_Shdr> Sections, const Elf_Shdr *Symtab);
122 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
123 typedef ELFFileBase<ELFT> Base;
124 typedef typename ELFT::Rel Elf_Rel;
125 typedef typename ELFT::Rela Elf_Rela;
126 typedef typename ELFT::Sym Elf_Sym;
127 typedef typename ELFT::Shdr Elf_Shdr;
128 typedef typename ELFT::SymRange Elf_Sym_Range;
129 typedef typename ELFT::Word Elf_Word;
130 typedef typename ELFT::uint uintX_t;
132 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
133 const Elf_Shdr &Sec);
134 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec);
137 static bool classof(const InputFile *F) {
138 return F->kind() == Base::ObjectKind;
141 ArrayRef<SymbolBody *> getSymbols();
142 ArrayRef<SymbolBody *> getLocalSymbols();
143 ArrayRef<SymbolBody *> getNonLocalSymbols();
145 explicit ObjectFile(MemoryBufferRef M);
146 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
148 ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
149 InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const;
151 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const {
152 if (SymbolIndex >= SymbolBodies.size())
153 fatal(toString(this) + ": invalid symbol index");
154 return *SymbolBodies[SymbolIndex];
157 template <typename RelT>
158 SymbolBody &getRelocTargetSym(const RelT &Rel) const {
159 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
160 return getSymbolBody(SymIndex);
163 // Returns source line information for a given offset.
164 // If no information is available, returns "".
165 std::string getLineInfo(InputSectionBase<ELFT> *S, uintX_t Offset);
167 // MIPS GP0 value defined by this file. This value represents the gp value
168 // used to create the relocatable object and required to support
169 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations.
170 uint32_t MipsGp0 = 0;
172 // The number is the offset in the string table. It will be used as the
173 // st_name of the symbol.
174 std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
176 // Name of source file obtained from STT_FILE symbol value,
177 // or empty string if there is no such symbol in object file
179 StringRef SourceFile;
183 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
184 void initializeSymbols();
185 void initializeDwarfLine();
186 InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
187 InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec,
188 StringRef SectionStringTable);
190 bool shouldMerge(const Elf_Shdr &Sec);
191 SymbolBody *createSymbolBody(const Elf_Sym *Sym);
193 // List of all sections defined by this file.
194 std::vector<InputSectionBase<ELFT> *> Sections;
196 // List of all symbols referenced or defined by this file.
197 std::vector<SymbolBody *> SymbolBodies;
199 // Debugging information to retrieve source file and line for error
200 // reporting. Linker may find reasonable number of errors in a
201 // single object file, so we cache debugging information in order to
202 // parse it only once for each object file we link.
203 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
206 // LazyObjectFile is analogous to ArchiveFile in the sense that
207 // the file contains lazy symbols. The difference is that
208 // LazyObjectFile wraps a single file instead of multiple files.
210 // This class is used for --start-lib and --end-lib options which
211 // instruct the linker to link object files between them with the
212 // archive file semantics.
213 class LazyObjectFile : public InputFile {
215 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {}
217 static bool classof(const InputFile *F) {
218 return F->kind() == LazyObjectKind;
221 template <class ELFT> void parse();
222 MemoryBufferRef getBuffer();
225 std::vector<StringRef> getSymbols();
226 template <class ELFT> std::vector<StringRef> getElfSymbols();
227 std::vector<StringRef> getBitcodeSymbols();
232 // An ArchiveFile object represents a .a file.
233 class ArchiveFile : public InputFile {
235 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
236 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
237 template <class ELFT> void parse();
239 // Returns a memory buffer for a given symbol and the offset in the archive
240 // for the member. An empty memory buffer and an offset of zero
241 // is returned if we have already returned the same memory buffer.
242 // (So that we don't instantiate same members more than once.)
243 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym);
246 std::unique_ptr<Archive> File;
247 llvm::DenseSet<uint64_t> Seen;
250 class BitcodeFile : public InputFile {
252 explicit BitcodeFile(MemoryBufferRef M);
253 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
254 template <class ELFT>
255 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
256 ArrayRef<Symbol *> getSymbols() { return Symbols; }
257 std::unique_ptr<llvm::lto::InputFile> Obj;
260 std::vector<Symbol *> Symbols;
264 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> {
265 typedef ELFFileBase<ELFT> Base;
266 typedef typename ELFT::Dyn Elf_Dyn;
267 typedef typename ELFT::Shdr Elf_Shdr;
268 typedef typename ELFT::Sym Elf_Sym;
269 typedef typename ELFT::SymRange Elf_Sym_Range;
270 typedef typename ELFT::Verdef Elf_Verdef;
271 typedef typename ELFT::Versym Elf_Versym;
272 typedef typename ELFT::Word Elf_Word;
273 typedef typename ELFT::uint uintX_t;
275 std::vector<StringRef> Undefs;
277 const Elf_Shdr *VersymSec = nullptr;
278 const Elf_Shdr *VerdefSec = nullptr;
281 StringRef getSoName() const { return SoName; }
282 const Elf_Shdr *getSection(const Elf_Sym &Sym) const;
283 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; }
285 static bool classof(const InputFile *F) {
286 return F->kind() == Base::SharedKind;
289 explicit SharedFile(MemoryBufferRef M);
293 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym);
296 // The string table offset of the version name in the output file.
299 // The version identifier for this version name.
303 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux
304 // data structures in the output file.
305 std::map<const Elf_Verdef *, NeededVer> VerdefMap;
307 // Used for --as-needed
308 bool AsNeeded = false;
310 bool isNeeded() const { return !AsNeeded || IsUsed; }
313 class BinaryFile : public InputFile {
315 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {}
316 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; }
317 template <class ELFT> void parse();
318 ArrayRef<InputSectionData *> getSections() const { return Sections; }
321 std::vector<InputSectionData *> Sections;
324 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
325 uint64_t OffsetInArchive = 0);
326 InputFile *createSharedFile(MemoryBufferRef MB);