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 "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" 26 27 namespace lld { 28 namespace elf { 29 30 using llvm::object::Archive; 31 32 class InputFile; 33 class Lazy; 34 class SymbolBody; 35 36 // The root class of input files. 37 class InputFile { 38 public: 39 enum Kind { 40 ObjectKind, 41 SharedKind, 42 LazyObjectKind, 43 ArchiveKind, 44 BitcodeKind, 45 }; 46 47 Kind kind() const { return FileKind; } 48 49 StringRef getName() const { return MB.getBufferIdentifier(); } 50 MemoryBufferRef MB; 51 52 // Filename of .a which contained this file. If this file was 53 // not in an archive file, it is the empty string. We use this 54 // string for creating error messages. 55 StringRef ArchiveName; 56 57 protected: 58 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} 59 60 private: 61 const Kind FileKind; 62 }; 63 64 template <typename ELFT> class ELFFileBase : public InputFile { 65 public: 66 typedef typename ELFT::Shdr Elf_Shdr; 67 typedef typename ELFT::Sym Elf_Sym; 68 typedef typename ELFT::Word Elf_Word; 69 typedef typename ELFT::SymRange Elf_Sym_Range; 70 71 ELFFileBase(Kind K, MemoryBufferRef M); 72 static bool classof(const InputFile *F) { 73 Kind K = F->kind(); 74 return K == ObjectKind || K == SharedKind; 75 } 76 77 static ELFKind getELFKind(); 78 const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; } 79 llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; } 80 81 uint16_t getEMachine() const { return getObj().getHeader()->e_machine; } 82 uint8_t getOSABI() const { 83 return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI]; 84 } 85 86 StringRef getStringTable() const { return StringTable; } 87 88 uint32_t getSectionIndex(const Elf_Sym &Sym) const; 89 90 protected: 91 llvm::object::ELFFile<ELFT> ELFObj; 92 const Elf_Shdr *Symtab = nullptr; 93 ArrayRef<Elf_Word> SymtabSHNDX; 94 StringRef StringTable; 95 void initStringTable(); 96 Elf_Sym_Range getElfSymbols(bool OnlyGlobals); 97 }; 98 99 // .o file. 100 template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> { 101 typedef ELFFileBase<ELFT> Base; 102 typedef typename ELFT::Sym Elf_Sym; 103 typedef typename ELFT::Shdr Elf_Shdr; 104 typedef typename ELFT::SymRange Elf_Sym_Range; 105 typedef typename ELFT::Word Elf_Word; 106 typedef typename ELFT::uint uintX_t; 107 108 StringRef getShtGroupSignature(const Elf_Shdr &Sec); 109 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec); 110 111 public: 112 static bool classof(const InputFile *F) { 113 return F->kind() == Base::ObjectKind; 114 } 115 116 ArrayRef<SymbolBody *> getSymbols(); 117 ArrayRef<SymbolBody *> getLocalSymbols(); 118 ArrayRef<SymbolBody *> getNonLocalSymbols(); 119 120 explicit ObjectFile(MemoryBufferRef M); 121 void parse(llvm::DenseSet<StringRef> &ComdatGroups); 122 123 ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; } 124 InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const; 125 126 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const { 127 return *SymbolBodies[SymbolIndex]; 128 } 129 130 const Elf_Shdr *getSymbolTable() const { return this->Symtab; }; 131 132 // Get MIPS GP0 value defined by this file. This value represents the gp value 133 // used to create the relocatable object and required to support 134 // R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations. 135 uint32_t getMipsGp0() const; 136 137 // The number is the offset in the string table. It will be used as the 138 // st_name of the symbol. 139 std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms; 140 141 private: 142 void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups); 143 void initializeSymbols(); 144 InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec); 145 InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec); 146 147 SymbolBody *createSymbolBody(const Elf_Sym *Sym); 148 149 // List of all sections defined by this file. 150 std::vector<InputSectionBase<ELFT> *> Sections; 151 152 // List of all symbols referenced or defined by this file. 153 std::vector<SymbolBody *> SymbolBodies; 154 155 // MIPS .reginfo section defined by this file. 156 std::unique_ptr<MipsReginfoInputSection<ELFT>> MipsReginfo; 157 158 llvm::BumpPtrAllocator Alloc; 159 llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc; 160 llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc; 161 llvm::SpecificBumpPtrAllocator<EHInputSection<ELFT>> EHAlloc; 162 }; 163 164 // LazyObjectFile is analogous to ArchiveFile in the sense that 165 // the file contains lazy symbols. The difference is that 166 // LazyObjectFile wraps a single file instead of multiple files. 167 // 168 // This class is used for --start-lib and --end-lib options which 169 // instruct the linker to link object files between them with the 170 // archive file semantics. 171 class LazyObjectFile : public InputFile { 172 public: 173 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {} 174 175 static bool classof(const InputFile *F) { 176 return F->kind() == LazyObjectKind; 177 } 178 179 void parse(); 180 181 llvm::MutableArrayRef<LazyObject> getLazySymbols() { return LazySymbols; } 182 183 private: 184 std::vector<StringRef> getSymbols(); 185 template <class ELFT> std::vector<StringRef> getElfSymbols(); 186 std::vector<StringRef> getBitcodeSymbols(); 187 188 llvm::BumpPtrAllocator Alloc; 189 llvm::StringSaver Saver{Alloc}; 190 std::vector<LazyObject> LazySymbols; 191 }; 192 193 // An ArchiveFile object represents a .a file. 194 class ArchiveFile : public InputFile { 195 public: 196 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {} 197 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; } 198 void parse(); 199 200 // Returns a memory buffer for a given symbol. An empty memory buffer 201 // is returned if we have already returned the same memory buffer. 202 // (So that we don't instantiate same members more than once.) 203 MemoryBufferRef getMember(const Archive::Symbol *Sym); 204 205 llvm::MutableArrayRef<LazyArchive> getLazySymbols() { return LazySymbols; } 206 207 private: 208 std::unique_ptr<Archive> File; 209 std::vector<LazyArchive> LazySymbols; 210 llvm::DenseSet<uint64_t> Seen; 211 }; 212 213 class BitcodeFile : public InputFile { 214 public: 215 explicit BitcodeFile(MemoryBufferRef M); 216 static bool classof(const InputFile *F); 217 void parse(llvm::DenseSet<StringRef> &ComdatGroups); 218 ArrayRef<SymbolBody *> getSymbols() { return SymbolBodies; } 219 static bool shouldSkip(const llvm::object::BasicSymbolRef &Sym); 220 221 private: 222 std::vector<SymbolBody *> SymbolBodies; 223 llvm::BumpPtrAllocator Alloc; 224 llvm::StringSaver Saver{Alloc}; 225 SymbolBody * 226 createSymbolBody(const llvm::DenseSet<const llvm::Comdat *> &KeptComdats, 227 const llvm::object::IRObjectFile &Obj, 228 const llvm::object::BasicSymbolRef &Sym); 229 }; 230 231 // .so file. 232 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> { 233 typedef ELFFileBase<ELFT> Base; 234 typedef typename ELFT::Shdr Elf_Shdr; 235 typedef typename ELFT::Sym Elf_Sym; 236 typedef typename ELFT::Word Elf_Word; 237 typedef typename ELFT::SymRange Elf_Sym_Range; 238 239 std::vector<SharedSymbol<ELFT>> SymbolBodies; 240 std::vector<StringRef> Undefs; 241 StringRef SoName; 242 243 public: 244 StringRef getSoName() const { return SoName; } 245 llvm::MutableArrayRef<SharedSymbol<ELFT>> getSharedSymbols() { 246 return SymbolBodies; 247 } 248 const Elf_Shdr *getSection(const Elf_Sym &Sym) const; 249 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; } 250 251 static bool classof(const InputFile *F) { 252 return F->kind() == Base::SharedKind; 253 } 254 255 explicit SharedFile(MemoryBufferRef M); 256 257 void parseSoName(); 258 void parseRest(); 259 260 // Used for --as-needed 261 bool AsNeeded = false; 262 bool IsUsed = false; 263 bool isNeeded() const { return !AsNeeded || IsUsed; } 264 }; 265 266 std::unique_ptr<InputFile> createObjectFile(MemoryBufferRef MB, 267 StringRef ArchiveName = ""); 268 std::unique_ptr<InputFile> createSharedFile(MemoryBufferRef MB); 269 270 } // namespace elf 271 } // namespace lld 272 273 #endif 274