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/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" 27 28 #include <map> 29 30 namespace llvm { 31 class DWARFDebugLine; 32 namespace lto { 33 class InputFile; 34 } 35 } 36 37 namespace lld { 38 namespace elf { 39 40 using llvm::object::Archive; 41 42 class InputFile; 43 class Lazy; 44 class SymbolBody; 45 46 // The root class of input files. 47 class InputFile { 48 public: 49 enum Kind { 50 ObjectKind, 51 SharedKind, 52 LazyObjectKind, 53 ArchiveKind, 54 BitcodeKind, 55 BinaryKind, 56 }; 57 58 Kind kind() const { return FileKind; } 59 60 StringRef getName() const { return MB.getBufferIdentifier(); } 61 MemoryBufferRef MB; 62 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; 67 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 71 // between objects. 72 uint64_t OffsetInArchive; 73 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; 78 uint8_t OSABI = 0; 79 80 protected: 81 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} 82 83 private: 84 const Kind FileKind; 85 }; 86 87 // Returns "(internal)", "foo.a(bar.o)" or "baz.o". 88 std::string toString(const InputFile *F); 89 90 template <typename ELFT> class ELFFileBase : public InputFile { 91 public: 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; 96 97 ELFFileBase(Kind K, MemoryBufferRef M); 98 static bool classof(const InputFile *F) { 99 Kind K = F->kind(); 100 return K == ObjectKind || K == SharedKind; 101 } 102 103 llvm::object::ELFFile<ELFT> getObj() const { 104 return llvm::object::ELFFile<ELFT>(MB.getBuffer()); 105 } 106 107 StringRef getStringTable() const { return StringTable; } 108 109 uint32_t getSectionIndex(const Elf_Sym &Sym) const; 110 111 Elf_Sym_Range getGlobalSymbols(); 112 113 protected: 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); 119 }; 120 121 // .o file. 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; 131 132 StringRef getShtGroupSignature(ArrayRef<Elf_Shdr> Sections, 133 const Elf_Shdr &Sec); 134 ArrayRef<Elf_Word> getShtGroupEntries(const Elf_Shdr &Sec); 135 136 public: 137 static bool classof(const InputFile *F) { 138 return F->kind() == Base::ObjectKind; 139 } 140 141 ArrayRef<SymbolBody *> getSymbols(); 142 ArrayRef<SymbolBody *> getLocalSymbols(); 143 ArrayRef<SymbolBody *> getNonLocalSymbols(); 144 145 explicit ObjectFile(MemoryBufferRef M); 146 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups); 147 148 ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; } 149 InputSectionBase<ELFT> *getSection(const Elf_Sym &Sym) const; 150 151 SymbolBody &getSymbolBody(uint32_t SymbolIndex) const { 152 if (SymbolIndex >= SymbolBodies.size()) 153 fatal(toString(this) + ": invalid symbol index"); 154 return *SymbolBodies[SymbolIndex]; 155 } 156 157 template <typename RelT> 158 SymbolBody &getRelocTargetSym(const RelT &Rel) const { 159 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL); 160 return getSymbolBody(SymIndex); 161 } 162 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); 166 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; 171 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; 175 176 // Name of source file obtained from STT_FILE symbol value, 177 // or empty string if there is no such symbol in object file 178 // symbol table. 179 StringRef SourceFile; 180 181 private: 182 void 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); 189 190 bool shouldMerge(const Elf_Shdr &Sec); 191 SymbolBody *createSymbolBody(const Elf_Sym *Sym); 192 193 // List of all sections defined by this file. 194 std::vector<InputSectionBase<ELFT> *> Sections; 195 196 // List of all symbols referenced or defined by this file. 197 std::vector<SymbolBody *> SymbolBodies; 198 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; 204 }; 205 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. 209 // 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 { 214 public: 215 explicit LazyObjectFile(MemoryBufferRef M) : InputFile(LazyObjectKind, M) {} 216 217 static bool classof(const InputFile *F) { 218 return F->kind() == LazyObjectKind; 219 } 220 221 template <class ELFT> void parse(); 222 MemoryBufferRef getBuffer(); 223 224 private: 225 std::vector<StringRef> getSymbols(); 226 template <class ELFT> std::vector<StringRef> getElfSymbols(); 227 std::vector<StringRef> getBitcodeSymbols(); 228 229 bool Seen = false; 230 }; 231 232 // An ArchiveFile object represents a .a file. 233 class ArchiveFile : public InputFile { 234 public: 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(); 238 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); 244 245 private: 246 std::unique_ptr<Archive> File; 247 llvm::DenseSet<uint64_t> Seen; 248 }; 249 250 class BitcodeFile : public InputFile { 251 public: 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; 258 259 private: 260 std::vector<Symbol *> Symbols; 261 }; 262 263 // .so file. 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; 274 275 std::vector<StringRef> Undefs; 276 StringRef SoName; 277 const Elf_Shdr *VersymSec = nullptr; 278 const Elf_Shdr *VerdefSec = nullptr; 279 280 public: 281 StringRef getSoName() const { return SoName; } 282 const Elf_Shdr *getSection(const Elf_Sym &Sym) const; 283 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; } 284 285 static bool classof(const InputFile *F) { 286 return F->kind() == Base::SharedKind; 287 } 288 289 explicit SharedFile(MemoryBufferRef M); 290 291 void parseSoName(); 292 void parseRest(); 293 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym); 294 295 struct NeededVer { 296 // The string table offset of the version name in the output file. 297 size_t StrTab; 298 299 // The version identifier for this version name. 300 uint16_t Index; 301 }; 302 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; 306 307 // Used for --as-needed 308 bool AsNeeded = false; 309 bool IsUsed = false; 310 bool isNeeded() const { return !AsNeeded || IsUsed; } 311 }; 312 313 class BinaryFile : public InputFile { 314 public: 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; } 319 320 private: 321 std::vector<InputSectionData *> Sections; 322 }; 323 324 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "", 325 uint64_t OffsetInArchive = 0); 326 InputFile *createSharedFile(MemoryBufferRef MB); 327 328 } // namespace elf 329 } // namespace lld 330 331 #endif 332