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 #include "Target.h" 14 15 #include "lld/Common/LLVM.h" 16 #include "lld/Common/Memory.h" 17 #include "llvm/ADT/DenseSet.h" 18 #include "llvm/ADT/SetVector.h" 19 #include "llvm/BinaryFormat/MachO.h" 20 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 21 #include "llvm/Object/Archive.h" 22 #include "llvm/Support/MemoryBuffer.h" 23 #include "llvm/TextAPI/TextAPIReader.h" 24 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 struct PlatformInfo; 41 class ConcatInputSection; 42 class Symbol; 43 struct Reloc; 44 enum class RefState : uint8_t; 45 46 // If --reproduce option is given, all input files are written 47 // to this tar archive. 48 extern std::unique_ptr<llvm::TarWriter> tar; 49 50 // If .subsections_via_symbols is set, each InputSection will be split along 51 // symbol boundaries. The field offset represents the offset of the subsection 52 // from the start of the original pre-split InputSection. 53 struct SubsectionEntry { 54 uint64_t offset; 55 InputSection *isec; 56 }; 57 using SubsectionMap = std::vector<SubsectionEntry>; 58 59 class InputFile { 60 public: 61 enum Kind { 62 ObjKind, 63 OpaqueKind, 64 DylibKind, 65 ArchiveKind, 66 BitcodeKind, 67 }; 68 69 virtual ~InputFile() = default; 70 Kind kind() const { return fileKind; } 71 StringRef getName() const { return name; } 72 73 MemoryBufferRef mb; 74 75 std::vector<Symbol *> symbols; 76 std::vector<SubsectionMap> subsections; 77 // Provides an easy way to sort InputFiles deterministically. 78 const int id; 79 80 // If not empty, this stores the name of the archive containing this file. 81 // We use this string for creating error messages. 82 std::string archiveName; 83 84 protected: 85 InputFile(Kind kind, MemoryBufferRef mb) 86 : mb(mb), id(idCount++), fileKind(kind), name(mb.getBufferIdentifier()) {} 87 88 InputFile(Kind, const llvm::MachO::InterfaceFile &); 89 90 private: 91 const Kind fileKind; 92 const StringRef name; 93 94 static int idCount; 95 }; 96 97 // .o file 98 class ObjFile final : public InputFile { 99 public: 100 ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName); 101 static bool classof(const InputFile *f) { return f->kind() == ObjKind; } 102 103 llvm::DWARFUnit *compileUnit = nullptr; 104 const uint32_t modTime; 105 std::vector<ConcatInputSection *> debugSections; 106 ArrayRef<llvm::MachO::data_in_code_entry> dataInCodeEntries; 107 108 private: 109 template <class LP> void parse(); 110 template <class Section> void parseSections(ArrayRef<Section>); 111 template <class LP> 112 void parseSymbols(ArrayRef<typename LP::section> sectionHeaders, 113 ArrayRef<typename LP::nlist> nList, const char *strtab, 114 bool subsectionsViaSymbols); 115 template <class NList> 116 Symbol *parseNonSectionSymbol(const NList &sym, StringRef name); 117 template <class Section> 118 void parseRelocations(ArrayRef<Section> sectionHeaders, const Section &, 119 SubsectionMap &); 120 void parseDebugInfo(); 121 void parseDataInCode(); 122 void registerCompactUnwind(); 123 }; 124 125 // command-line -sectcreate file 126 class OpaqueFile final : public InputFile { 127 public: 128 OpaqueFile(MemoryBufferRef mb, StringRef segName, StringRef sectName); 129 static bool classof(const InputFile *f) { return f->kind() == OpaqueKind; } 130 }; 131 132 // .dylib or .tbd file 133 class DylibFile final : public InputFile { 134 public: 135 // Mach-O dylibs can re-export other dylibs as sub-libraries, meaning that the 136 // symbols in those sub-libraries will be available under the umbrella 137 // library's namespace. Those sub-libraries can also have their own 138 // re-exports. When loading a re-exported dylib, `umbrella` should be set to 139 // the root dylib to ensure symbols in the child library are correctly bound 140 // to the root. On the other hand, if a dylib is being directly loaded 141 // (through an -lfoo flag), then `umbrella` should be a nullptr. 142 explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella, 143 bool isBundleLoader = false); 144 explicit DylibFile(const llvm::MachO::InterfaceFile &interface, 145 DylibFile *umbrella = nullptr, 146 bool isBundleLoader = false); 147 148 void parseLoadCommands(MemoryBufferRef mb); 149 void parseReexports(const llvm::MachO::InterfaceFile &interface); 150 151 static bool classof(const InputFile *f) { return f->kind() == DylibKind; } 152 153 StringRef installName; 154 DylibFile *exportingFile = nullptr; 155 DylibFile *umbrella; 156 SmallVector<StringRef, 2> rpaths; 157 uint32_t compatibilityVersion = 0; 158 uint32_t currentVersion = 0; 159 int64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel 160 RefState refState; 161 bool reexport = false; 162 bool forceNeeded = false; 163 bool forceWeakImport = false; 164 bool deadStrippable = false; 165 bool explicitlyLinked = false; 166 167 unsigned numReferencedSymbols = 0; 168 169 bool isReferenced() const { return numReferencedSymbols > 0; } 170 171 // An executable can be used as a bundle loader that will load the output 172 // file being linked, and that contains symbols referenced, but not 173 // implemented in the bundle. When used like this, it is very similar 174 // to a Dylib, so we re-used the same class to represent it. 175 bool isBundleLoader; 176 177 private: 178 bool handleLDSymbol(StringRef originalName); 179 void handleLDPreviousSymbol(StringRef name, StringRef originalName); 180 void handleLDInstallNameSymbol(StringRef name, StringRef originalName); 181 void checkAppExtensionSafety(bool dylibIsAppExtensionSafe) const; 182 }; 183 184 // .a file 185 class ArchiveFile final : public InputFile { 186 public: 187 explicit ArchiveFile(std::unique_ptr<llvm::object::Archive> &&file); 188 void addLazySymbols(); 189 void fetch(const llvm::object::Archive::Symbol &); 190 // LLD normally doesn't use Error for error-handling, but the underlying 191 // Archive library does, so this is the cleanest way to wrap it. 192 Error fetch(const llvm::object::Archive::Child &, StringRef reason); 193 const llvm::object::Archive &getArchive() const { return *file; }; 194 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; } 195 196 private: 197 std::unique_ptr<llvm::object::Archive> file; 198 // Keep track of children fetched from the archive by tracking 199 // which address offsets have been fetched already. 200 llvm::DenseSet<uint64_t> seen; 201 }; 202 203 class BitcodeFile final : public InputFile { 204 public: 205 explicit BitcodeFile(MemoryBufferRef mb, StringRef archiveName, 206 uint64_t offsetInArchive); 207 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; } 208 209 std::unique_ptr<llvm::lto::InputFile> obj; 210 }; 211 212 extern llvm::SetVector<InputFile *> inputFiles; 213 214 llvm::Optional<MemoryBufferRef> readFile(StringRef path); 215 216 namespace detail { 217 218 template <class CommandType, class... Types> 219 std::vector<const CommandType *> 220 findCommands(const void *anyHdr, size_t maxCommands, Types... types) { 221 std::vector<const CommandType *> cmds; 222 std::initializer_list<uint32_t> typesList{types...}; 223 const auto *hdr = reinterpret_cast<const llvm::MachO::mach_header *>(anyHdr); 224 const uint8_t *p = 225 reinterpret_cast<const uint8_t *>(hdr) + target->headerSize; 226 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) { 227 auto *cmd = reinterpret_cast<const CommandType *>(p); 228 if (llvm::is_contained(typesList, cmd->cmd)) { 229 cmds.push_back(cmd); 230 if (cmds.size() == maxCommands) 231 return cmds; 232 } 233 p += cmd->cmdsize; 234 } 235 return cmds; 236 } 237 238 } // namespace detail 239 240 // anyHdr should be a pointer to either mach_header or mach_header_64 241 template <class CommandType = llvm::MachO::load_command, class... Types> 242 const CommandType *findCommand(const void *anyHdr, Types... types) { 243 std::vector<const CommandType *> cmds = 244 detail::findCommands<CommandType>(anyHdr, 1, types...); 245 return cmds.size() ? cmds[0] : nullptr; 246 } 247 248 template <class CommandType = llvm::MachO::load_command, class... Types> 249 std::vector<const CommandType *> findCommands(const void *anyHdr, 250 Types... types) { 251 return detail::findCommands<CommandType>(anyHdr, 0, types...); 252 } 253 254 } // namespace macho 255 256 std::string toString(const macho::InputFile *file); 257 } // namespace lld 258 259 #endif 260