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