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 *> getSections() const { return Sections; } 160 InputSectionBase *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 *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 // Name of source file obtained from STT_FILE symbol value, 184 // or empty string if there is no such symbol in object file 185 // symbol table. 186 StringRef SourceFile; 187 188 private: 189 void 190 initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups); 191 void initializeSymbols(); 192 void initializeDwarfLine(); 193 InputSectionBase *getRelocTarget(const Elf_Shdr &Sec); 194 InputSectionBase *createInputSection(const Elf_Shdr &Sec, 195 StringRef SectionStringTable); 196 197 bool shouldMerge(const Elf_Shdr &Sec); 198 SymbolBody *createSymbolBody(const Elf_Sym *Sym); 199 200 // List of all sections defined by this file. 201 std::vector<InputSectionBase *> Sections; 202 203 // List of all symbols referenced or defined by this file. 204 std::vector<SymbolBody *> SymbolBodies; 205 206 // Debugging information to retrieve source file and line for error 207 // reporting. Linker may find reasonable number of errors in a 208 // single object file, so we cache debugging information in order to 209 // parse it only once for each object file we link. 210 std::unique_ptr<llvm::DWARFDebugLine> DwarfLine; 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 bool Seen = false; 237 }; 238 239 // An ArchiveFile object represents a .a file. 240 class ArchiveFile : public InputFile { 241 public: 242 explicit ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {} 243 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; } 244 template <class ELFT> void parse(); 245 246 // Returns a memory buffer for a given symbol and the offset in the archive 247 // for the member. An empty memory buffer and an offset of zero 248 // is returned if we have already returned the same memory buffer. 249 // (So that we don't instantiate same members more than once.) 250 std::pair<MemoryBufferRef, uint64_t> getMember(const Archive::Symbol *Sym); 251 252 private: 253 std::unique_ptr<Archive> File; 254 llvm::DenseSet<uint64_t> Seen; 255 }; 256 257 class BitcodeFile : public InputFile { 258 public: 259 explicit BitcodeFile(MemoryBufferRef M); 260 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; } 261 template <class ELFT> 262 void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups); 263 ArrayRef<Symbol *> getSymbols() { return Symbols; } 264 std::unique_ptr<llvm::lto::InputFile> Obj; 265 266 private: 267 std::vector<Symbol *> Symbols; 268 }; 269 270 // .so file. 271 template <class ELFT> class SharedFile : public ELFFileBase<ELFT> { 272 typedef ELFFileBase<ELFT> Base; 273 typedef typename ELFT::Dyn Elf_Dyn; 274 typedef typename ELFT::Shdr Elf_Shdr; 275 typedef typename ELFT::Sym Elf_Sym; 276 typedef typename ELFT::SymRange Elf_Sym_Range; 277 typedef typename ELFT::Verdef Elf_Verdef; 278 typedef typename ELFT::Versym Elf_Versym; 279 typedef typename ELFT::Word Elf_Word; 280 typedef typename ELFT::uint uintX_t; 281 282 std::vector<StringRef> Undefs; 283 StringRef SoName; 284 const Elf_Shdr *VersymSec = nullptr; 285 const Elf_Shdr *VerdefSec = nullptr; 286 287 public: 288 StringRef getSoName() const { return SoName; } 289 const Elf_Shdr *getSection(const Elf_Sym &Sym) const; 290 llvm::ArrayRef<StringRef> getUndefinedSymbols() { return Undefs; } 291 292 static bool classof(const InputFile *F) { 293 return F->kind() == Base::SharedKind; 294 } 295 296 explicit SharedFile(MemoryBufferRef M); 297 298 void parseSoName(); 299 void parseRest(); 300 std::vector<const Elf_Verdef *> parseVerdefs(const Elf_Versym *&Versym); 301 302 struct NeededVer { 303 // The string table offset of the version name in the output file. 304 size_t StrTab; 305 306 // The version identifier for this version name. 307 uint16_t Index; 308 }; 309 310 // Mapping from Elf_Verdef data structures to information about Elf_Vernaux 311 // data structures in the output file. 312 std::map<const Elf_Verdef *, NeededVer> VerdefMap; 313 314 // Used for --as-needed 315 bool AsNeeded = false; 316 bool IsUsed = false; 317 bool isNeeded() const { return !AsNeeded || IsUsed; } 318 }; 319 320 class BinaryFile : public InputFile { 321 public: 322 explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {} 323 static bool classof(const InputFile *F) { return F->kind() == BinaryKind; } 324 template <class ELFT> void parse(); 325 ArrayRef<InputSectionBase *> getSections() const { return Sections; } 326 327 private: 328 std::vector<InputSectionBase *> Sections; 329 }; 330 331 InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "", 332 uint64_t OffsetInArchive = 0); 333 InputFile *createSharedFile(MemoryBufferRef MB); 334 335 } // namespace elf 336 } // namespace lld 337 338 #endif 339