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