1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLD_MACHO_INPUT_FILES_H 10 #define LLD_MACHO_INPUT_FILES_H 11 12 #include "MachOStructs.h" 13 14 #include "lld/Common/LLVM.h" 15 #include "lld/Common/Memory.h" 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/ADT/SetVector.h" 18 #include "llvm/BinaryFormat/MachO.h" 19 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 20 #include "llvm/Object/Archive.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include "llvm/TextAPI/MachO/TextAPIReader.h" 23 24 #include <map> 25 #include <vector> 26 27 namespace llvm { 28 namespace lto { 29 class InputFile; 30 } // namespace lto 31 namespace MachO { 32 class InterfaceFile; 33 } // namespace MachO 34 class TarWriter; 35 } // namespace llvm 36 37 namespace lld { 38 namespace macho { 39 40 class InputSection; 41 class Symbol; 42 struct Reloc; 43 enum class RefState : uint8_t; 44 45 // If --reproduce option is given, all input files are written 46 // to this tar archive. 47 extern std::unique_ptr<llvm::TarWriter> tar; 48 49 // If .subsections_via_symbols is set, each InputSection will be split along 50 // symbol boundaries. The keys of a SubsectionMap represent the offsets of 51 // each subsection from the start of the original pre-split InputSection. 52 using SubsectionMap = std::map<uint32_t, InputSection *>; 53 54 class InputFile { 55 public: 56 enum Kind { 57 ObjKind, 58 OpaqueKind, 59 DylibKind, 60 ArchiveKind, 61 BitcodeKind, 62 }; 63 64 virtual ~InputFile() = default; 65 Kind kind() const { return fileKind; } 66 StringRef getName() const { return name; } 67 68 MemoryBufferRef mb; 69 70 std::vector<Symbol *> symbols; 71 std::vector<SubsectionMap> subsections; 72 // Provides an easy way to sort InputFiles deterministically. 73 const int id; 74 75 // If not empty, this stores the name of the archive containing this file. 76 // We use this string for creating error messages. 77 std::string archiveName; 78 79 protected: 80 InputFile(Kind kind, MemoryBufferRef mb) 81 : mb(mb), id(idCount++), fileKind(kind), name(mb.getBufferIdentifier()) {} 82 83 InputFile(Kind, const llvm::MachO::InterfaceFile &); 84 85 private: 86 const Kind fileKind; 87 const StringRef name; 88 89 static int idCount; 90 }; 91 92 // .o file 93 class ObjFile : public InputFile { 94 public: 95 ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName); 96 static bool classof(const InputFile *f) { return f->kind() == ObjKind; } 97 98 llvm::DWARFUnit *compileUnit = nullptr; 99 const uint32_t modTime; 100 ArrayRef<llvm::MachO::section_64> sectionHeaders; 101 std::vector<InputSection *> debugSections; 102 103 private: 104 void parseSections(ArrayRef<llvm::MachO::section_64>); 105 void parseSymbols(ArrayRef<lld::structs::nlist_64> nList, const char *strtab, 106 bool subsectionsViaSymbols); 107 Symbol *parseNonSectionSymbol(const structs::nlist_64 &sym, StringRef name); 108 void parseRelocations(const llvm::MachO::section_64 &, SubsectionMap &); 109 void parseDebugInfo(); 110 }; 111 112 // command-line -sectcreate file 113 class OpaqueFile : public InputFile { 114 public: 115 OpaqueFile(MemoryBufferRef mb, StringRef segName, StringRef sectName); 116 static bool classof(const InputFile *f) { return f->kind() == OpaqueKind; } 117 }; 118 119 // .dylib file 120 class DylibFile : public InputFile { 121 public: 122 // Mach-O dylibs can re-export other dylibs as sub-libraries, meaning that the 123 // symbols in those sub-libraries will be available under the umbrella 124 // library's namespace. Those sub-libraries can also have their own 125 // re-exports. When loading a re-exported dylib, `umbrella` should be set to 126 // the root dylib to ensure symbols in the child library are correctly bound 127 // to the root. On the other hand, if a dylib is being directly loaded 128 // (through an -lfoo flag), then `umbrella` should be a nullptr. 129 explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella = nullptr, 130 bool isBundleLoader = false); 131 132 explicit DylibFile(const llvm::MachO::InterfaceFile &interface, 133 DylibFile *umbrella = nullptr, 134 bool isBundleLoader = false); 135 136 static bool classof(const InputFile *f) { return f->kind() == DylibKind; } 137 138 StringRef dylibName; 139 uint32_t compatibilityVersion = 0; 140 uint32_t currentVersion = 0; 141 int64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel 142 RefState refState; 143 bool reexport = false; 144 bool forceWeakImport = false; 145 146 // An executable can be used as a bundle loader that will load the output 147 // file being linked, and that contains symbols referenced, but not 148 // implemented in the bundle. When used like this, it is very similar 149 // to a Dylib, so we re-used the same class to represent it. 150 bool isBundleLoader; 151 }; 152 153 // .a file 154 class ArchiveFile : public InputFile { 155 public: 156 explicit ArchiveFile(std::unique_ptr<llvm::object::Archive> &&file); 157 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; } 158 void fetch(const llvm::object::Archive::Symbol &sym); 159 160 private: 161 std::unique_ptr<llvm::object::Archive> file; 162 // Keep track of children fetched from the archive by tracking 163 // which address offsets have been fetched already. 164 llvm::DenseSet<uint64_t> seen; 165 }; 166 167 class BitcodeFile : public InputFile { 168 public: 169 explicit BitcodeFile(MemoryBufferRef mb); 170 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; } 171 172 std::unique_ptr<llvm::lto::InputFile> obj; 173 }; 174 175 extern llvm::SetVector<InputFile *> inputFiles; 176 177 llvm::Optional<MemoryBufferRef> readFile(StringRef path); 178 179 template <class CommandType = llvm::MachO::load_command> 180 const CommandType *findCommand(const llvm::MachO::mach_header_64 *hdr, 181 uint32_t type) { 182 const uint8_t *p = reinterpret_cast<const uint8_t *>(hdr) + 183 sizeof(llvm::MachO::mach_header_64); 184 185 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) { 186 auto *cmd = reinterpret_cast<const CommandType *>(p); 187 if (cmd->cmd == type) 188 return cmd; 189 p += cmd->cmdsize; 190 } 191 return nullptr; 192 } 193 194 } // namespace macho 195 196 std::string toString(const macho::InputFile *file); 197 } // namespace lld 198 199 #endif 200