1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLD_ELF_INPUT_FILES_H 11 #define LLD_ELF_INPUT_FILES_H 12 13 #include "Config.h" 14 #include "InputSection.h" 15 #include "Error.h" 16 #include "Symbols.h" 17 18 #include "lld/Core/LLVM.h" 19 #include "lld/Core/Reproduce.h" 20 #include "llvm/ADT/DenseSet.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/IR/Comdat.h" 23 #include "llvm/Object/Archive.h" 24 #include "llvm/Object/ELF.h" 25 #include "llvm/Object/IRObjectFile.h" 26 #include "llvm/Support/StringSaver.h" 27 28 #include <map> 29 30 namespace llvm { 31 namespace lto { 32 class InputFile; 33 } 34 } 35 36 namespace lld { 37 namespace elf { 38 39 using llvm::object::Archive; 40 41 class InputFile; 42 class Lazy; 43 class SymbolBody; 44 45 // The root class of input files. 46 class InputFile { 47 public: 48 virtual ~InputFile() = default; 49 50 enum Kind { 51 ObjectKind, 52 SharedKind, 53 LazyObjectKind, 54 ArchiveKind, 55 BitcodeKind, 56 BinaryKind, 57 }; 58 59 Kind kind() const { return FileKind; } 60 61 StringRef getName() const { return MB.getBufferIdentifier(); } 62 MemoryBufferRef MB; 63 64 // Filename of .a which contained this file. If this file was 65 // not in an archive file, it is the empty string. We use this 66 // string for creating error messages. 67 StringRef ArchiveName; 68 69 // If this file is in an archive, the member contains the offset of 70 // the file in the archive. Otherwise, it's just zero. We store this 71 // field so that we can pass it to lib/LTO in order to disambiguate 72 // between objects. 73 uint64_t OffsetInArchive; 74 75 // If this is an architecture-specific file, the following members 76 // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type. 77 ELFKind EKind = ELFNoneKind; 78 uint16_t EMachine = llvm::ELF::EM_NONE; 79 80 static void freePool(); 81 82 protected: 83 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) { 84 Pool.push_back(this); 85 } 86 87 private: 88 const Kind FileKind; 89 90 // All InputFile instances are added to the pool 91 // and freed all at once on exit by freePool(). 92 static std::vector<InputFile *> Pool; 93 }; 94 95 // Returns "(internal)", "foo.a(bar.o)" or "baz.o". 96 std::string getFilename(const InputFile *F); 97 98 template <typename ELFT> class ELFFileBase : public InputFile { 99 public: 100 typedef typename ELFT::Shdr Elf_Shdr; 101 typedef typename ELFT::Sym Elf_Sym; 102 typedef typename ELFT::Word Elf_Word; 103 typedef typename ELFT::SymRange Elf_Sym_Range; 104 105 ELFFileBase(Kind K, MemoryBufferRef M); 106 static bool classof(const InputFile *F) { 107 Kind K = F->kind(); 108 return K == ObjectKind || K == SharedKind; 109 } 110 111 const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; } 112 llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; } 113 114 uint8_t getOSABI() const { 115 return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI]; 116 } 117 118 StringRef getStringTable() const { return StringTable; } 119 120 uint32_t getSectionIndex(const Elf_Sym &Sym) const; 121 122 Elf_Sym_Range getElfSymbols(bool OnlyGlobals); 123 124 protected: 125 llvm::object::ELFFile<ELFT> ELFObj; 126 const Elf_Shdr *Symtab = nullptr; 127 ArrayRef<Elf_Word> SymtabSHNDX; 128 StringRef StringTable; 129 void initStringTable(); 130 }; 131 132 // .o file. 133 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> { 134 typedef ELFFileBase<ELFT> Base; 135 typedef typename ELFT::Sym Elf_Sym; 136 typedef typename ELFT::Shdr Elf_Shdr; 137 typedef typename ELFT::SymRange Elf_Sym_Range; 138 typedef typename ELFT::Word Elf_Word; 139 typedef typename ELFT::uint uintX_t; 140 141 StringRef getShtGroupSignature(const Elf_Shdr &Sec); 142 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec); 143 144 public: 145 static bool classof(const InputFile *F) { 146 return F->kind() == Base::ObjectKind; 147 } 148 149 ArrayRef<SymbolBody *> getSymbols(); 150 ArrayRef<SymbolBody *> getLocalSymbols(); 151 ArrayRef<SymbolBody *> getNonLocalSymbols(); 152 153 explicit ObjectFile(MemoryBufferRef M); 154 void parse(llvm::DenseSet<StringRef> &ComdatGroups); 155 156 ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; } 157 InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const; 158 159 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const { 160 if (SymbolIndex >= SymbolBodies.size()) 161 fatal(getFilename(this) + ": invalid symbol index"); 162 return *SymbolBodies[SymbolIndex]; 163 } 164 165 template <typename RelT> SymbolBody &getRelocTargetSym(const RelT &Rel) const { 166 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL); 167 return getSymbolBody(SymIndex); 168 } 169 170 const Elf_Shdr *getSymbolTable() const { return this->Symtab; }; 171 172 // Get MIPS GP0 value defined by this file. This value represents the gp value 173 // used to create the relocatable object and required to support 174 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations. 175 uint32_t getMipsGp0() const; 176 177 // The number is the offset in the string table. It will be used as the 178 // st_name of the symbol. 179 std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms; 180 181 // SymbolBodies and Thunks for sections in this file are allocated 182 // using this buffer. 183 llvm::BumpPtrAllocator Alloc; 184 185 private: 186 void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups); 187 void initializeSymbols(); 188 void initializeReverseDependencies(); 189 InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec); 190 InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec); 191 192 bool shouldMerge(const Elf_Shdr &Sec); 193 SymbolBody *createSymbolBody(const Elf_Sym *Sym); 194 195 // List of all sections defined by this file. 196 std::vector<InputSectionBase<ELFT> *> Sections; 197 198 // List of all symbols referenced or defined by this file. 199 std::vector<SymbolBody *> SymbolBodies; 200 201 // MIPS .reginfo section defined by this file. 202 std::unique_ptr<MipsReginfoInputSection<ELFT>> MipsReginfo; 203 // MIPS .MIPS.options section defined by this file. 204 std::unique_ptr<MipsOptionsInputSection<ELFT>> MipsOptions; 205 // MIPS .MIPS.abiflags section defined by this file. 206 std::unique_ptr<MipsAbiFlagsInputSection<ELFT>> MipsAbiFlags; 207 208 llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc; 209 llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc; 210 llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> EHAlloc; 211 }; 212 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. 216 // 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 { 221 public: 222 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {} 223 224 static bool classof(const InputFile *F) { 225 return F->kind() == LazyObjectKind; 226 } 227 228 template <class ELFT> void parse(); 229 MemoryBufferRef getBuffer(); 230 231 private: 232 std::vector<StringRef> getSymbols(); 233 template <class ELFT> std::vector<StringRef> getElfSymbols(); 234 std::vector<StringRef> getBitcodeSymbols(); 235 236 llvm::BumpPtrAllocator Alloc; 237 llvm::StringSaver Saver{Alloc}; 238 bool Seen = false; 239 }; 240 241 // An ArchiveFile object represents a .a file. 242 class ArchiveFile : public InputFile { 243 public: 244 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {} 245 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; } 246 template <class ELFT> void parse(); 247 248 // Returns a memory buffer for a given symbol and the offset in the archive 249 // for the member. An empty memory buffer and an offset of zero 250 // is returned if we have already returned the same memory buffer. 251 // (So that we don't instantiate same members more than once.) 252 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym); 253 254 private: 255 std::unique_ptr<Archive> File; 256 llvm::DenseSet<uint64_t> Seen; 257 }; 258 259 class BitcodeFile : public InputFile { 260 public: 261 explicit BitcodeFile(MemoryBufferRef M); 262 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; } 263 template <class ELFT> 264 void parse(llvm::DenseSet<StringRef> &ComdatGroups); 265 ArrayRef<Symbol *> getSymbols() { return Symbols; } 266 std::unique_ptr<llvm::lto::InputFile> Obj; 267 268 private: 269 std::vector<Symbol *> Symbols; 270 llvm::BumpPtrAllocator Alloc; 271 llvm::StringSaver Saver{Alloc}; 272 }; 273 274 // .so file. 275 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> { 276 typedef ELFFileBase<ELFT> Base; 277 typedef typename ELFT::Shdr Elf_Shdr; 278 typedef typename ELFT::Sym Elf_Sym; 279 typedef typename ELFT::Word Elf_Word; 280 typedef typename ELFT::SymRange Elf_Sym_Range; 281 typedef typename ELFT::Versym Elf_Versym; 282 typedef typename ELFT::Verdef Elf_Verdef; 283 284 std::vector<StringRef> Undefs; 285 StringRef SoName; 286 const Elf_Shdr *VersymSec = nullptr; 287 const Elf_Shdr *VerdefSec = nullptr; 288 289 public: 290 StringRef getSoName() const { return SoName; } 291 const Elf_Shdr *getSection(const Elf_Sym &Sym) const; 292 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; } 293 294 static bool classof(const InputFile *F) { 295 return F->kind() == Base::SharedKind; 296 } 297 298 explicit SharedFile(MemoryBufferRef M); 299 300 void parseSoName(); 301 void parseRest(); 302 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym); 303 304 struct NeededVer { 305 // The string table offset of the version name in the output file. 306 size_t StrTab; 307 308 // The version identifier for this version name. 309 uint16_t Index; 310 }; 311 312 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux 313 // data structures in the output file. 314 std::map<const Elf_Verdef *, NeededVer> VerdefMap; 315 316 // Used for --as-needed 317 bool AsNeeded = false; 318 bool IsUsed = false; 319 bool isNeeded() const { return !AsNeeded || IsUsed; } 320 }; 321 322 class BinaryFile : public InputFile { 323 public: 324 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {} 325 326 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; } 327 328 template <class ELFT> InputFile *createELF(); 329 330 private: 331 std::vector<uint8_t> ELFData; 332 }; 333 334 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "", 335 uint64_t OffsetInArchive = 0); 336 InputFile *createSharedFile(MemoryBufferRef MB); 337 338 } // namespace elf 339 } // namespace lld 340 341 #endif 342