1 //===- Driver.cpp ---------------------------------------------------------===// 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 #include "Driver.h" 10 #include "Config.h" 11 #include "InputFiles.h" 12 #include "OutputSection.h" 13 #include "OutputSegment.h" 14 #include "SymbolTable.h" 15 #include "Symbols.h" 16 #include "Target.h" 17 #include "Writer.h" 18 19 #include "lld/Common/Args.h" 20 #include "lld/Common/Driver.h" 21 #include "lld/Common/ErrorHandler.h" 22 #include "lld/Common/LLVM.h" 23 #include "lld/Common/Memory.h" 24 #include "lld/Common/Version.h" 25 #include "llvm/ADT/DenseSet.h" 26 #include "llvm/ADT/StringExtras.h" 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/BinaryFormat/MachO.h" 29 #include "llvm/BinaryFormat/Magic.h" 30 #include "llvm/Config/config.h" 31 #include "llvm/Object/Archive.h" 32 #include "llvm/Option/ArgList.h" 33 #include "llvm/Option/Option.h" 34 #include "llvm/Support/MemoryBuffer.h" 35 #include "llvm/Support/Path.h" 36 37 using namespace llvm; 38 using namespace llvm::MachO; 39 using namespace llvm::sys; 40 using namespace lld; 41 using namespace lld::macho; 42 43 Configuration *lld::macho::config; 44 45 // Create prefix string literals used in Options.td 46 #define PREFIX(NAME, VALUE) const char *NAME[] = VALUE; 47 #include "Options.inc" 48 #undef PREFIX 49 50 // Create table mapping all options defined in Options.td 51 static const opt::OptTable::Info optInfo[] = { 52 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 53 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ 54 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, 55 #include "Options.inc" 56 #undef OPTION 57 }; 58 59 MachOOptTable::MachOOptTable() : OptTable(optInfo) {} 60 61 opt::InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) { 62 // Make InputArgList from string vectors. 63 unsigned missingIndex; 64 unsigned missingCount; 65 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size()); 66 67 opt::InputArgList args = ParseArgs(vec, missingIndex, missingCount); 68 69 if (missingCount) 70 error(Twine(args.getArgString(missingIndex)) + ": missing argument"); 71 72 for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) 73 error("unknown argument: " + arg->getSpelling()); 74 return args; 75 } 76 77 static Optional<std::string> findLibrary(StringRef name) { 78 std::string stub = (llvm::Twine("lib") + name + ".tbd").str(); 79 std::string shared = (llvm::Twine("lib") + name + ".dylib").str(); 80 std::string archive = (llvm::Twine("lib") + name + ".a").str(); 81 llvm::SmallString<260> location; 82 83 for (StringRef dir : config->searchPaths) { 84 for (StringRef library : {stub, shared, archive}) { 85 location = dir; 86 llvm::sys::path::append(location, library); 87 if (fs::exists(location)) 88 return location.str().str(); 89 } 90 } 91 return {}; 92 } 93 94 static TargetInfo *createTargetInfo(opt::InputArgList &args) { 95 StringRef arch = llvm::Triple(LLVM_DEFAULT_TARGET_TRIPLE).getArchName(); 96 config->arch = llvm::MachO::getArchitectureFromName( 97 args.getLastArgValue(OPT_arch, arch)); 98 switch (config->arch) { 99 case llvm::MachO::AK_x86_64: 100 case llvm::MachO::AK_x86_64h: 101 return createX86_64TargetInfo(); 102 default: 103 fatal("missing or unsupported -arch " + args.getLastArgValue(OPT_arch)); 104 } 105 } 106 107 static std::vector<StringRef> getSearchPaths(opt::InputArgList &args) { 108 std::vector<StringRef> ret{args::getStrings(args, OPT_L)}; 109 if (!args.hasArg(OPT_Z)) { 110 ret.push_back("/usr/lib"); 111 ret.push_back("/usr/local/lib"); 112 } 113 return ret; 114 } 115 116 static void addFile(StringRef path) { 117 Optional<MemoryBufferRef> buffer = readFile(path); 118 if (!buffer) 119 return; 120 MemoryBufferRef mbref = *buffer; 121 122 switch (identify_magic(mbref.getBuffer())) { 123 case file_magic::archive: { 124 std::unique_ptr<object::Archive> file = CHECK( 125 object::Archive::create(mbref), path + ": failed to parse archive"); 126 127 if (!file->isEmpty() && !file->hasSymbolTable()) 128 error(path + ": archive has no index; run ranlib to add one"); 129 130 inputFiles.push_back(make<ArchiveFile>(std::move(file))); 131 break; 132 } 133 case file_magic::macho_object: 134 inputFiles.push_back(make<ObjFile>(mbref)); 135 break; 136 case file_magic::macho_dynamically_linked_shared_lib: 137 inputFiles.push_back(make<DylibFile>(mbref)); 138 break; 139 case file_magic::tapi_file: { 140 llvm::Expected<std::unique_ptr<llvm::MachO::InterfaceFile>> result = 141 TextAPIReader::get(mbref); 142 if (!result) 143 return; 144 145 std::unique_ptr<llvm::MachO::InterfaceFile> interface{std::move(*result)}; 146 inputFiles.push_back(make<DylibFile>(std::move(interface))); 147 break; 148 } 149 default: 150 error(path + ": unhandled file type"); 151 } 152 } 153 154 static std::array<StringRef, 6> archNames{"arm", "arm64", "i386", 155 "x86_64", "ppc", "ppc64"}; 156 static bool isArchString(StringRef s) { 157 static DenseSet<StringRef> archNamesSet(archNames.begin(), archNames.end()); 158 return archNamesSet.find(s) != archNamesSet.end(); 159 } 160 161 // An order file has one entry per line, in the following format: 162 // 163 // <arch>:<object file>:<symbol name> 164 // 165 // <arch> and <object file> are optional. If not specified, then that entry 166 // matches any symbol of that name. 167 // 168 // If a symbol is matched by multiple entries, then it takes the lowest-ordered 169 // entry (the one nearest to the front of the list.) 170 // 171 // The file can also have line comments that start with '#'. 172 void parseOrderFile(StringRef path) { 173 Optional<MemoryBufferRef> buffer = readFile(path); 174 if (!buffer) { 175 error("Could not read order file at " + path); 176 return; 177 } 178 179 MemoryBufferRef mbref = *buffer; 180 size_t priority = std::numeric_limits<size_t>::max(); 181 for (StringRef rest : args::getLines(mbref)) { 182 StringRef arch, objectFile, symbol; 183 184 std::array<StringRef, 3> fields; 185 uint8_t fieldCount = 0; 186 while (rest != "" && fieldCount < 3) { 187 std::pair<StringRef, StringRef> p = getToken(rest, ": \t\n\v\f\r"); 188 StringRef tok = p.first; 189 rest = p.second; 190 191 // Check if we have a comment 192 if (tok == "" || tok[0] == '#') 193 break; 194 195 fields[fieldCount++] = tok; 196 } 197 198 switch (fieldCount) { 199 case 3: 200 arch = fields[0]; 201 objectFile = fields[1]; 202 symbol = fields[2]; 203 break; 204 case 2: 205 (isArchString(fields[0]) ? arch : objectFile) = fields[0]; 206 symbol = fields[1]; 207 break; 208 case 1: 209 symbol = fields[0]; 210 break; 211 case 0: 212 break; 213 default: 214 llvm_unreachable("too many fields in order file"); 215 } 216 217 if (!arch.empty()) { 218 if (!isArchString(arch)) { 219 error("invalid arch \"" + arch + "\" in order file: expected one of " + 220 llvm::join(archNames, ", ")); 221 continue; 222 } 223 224 // TODO: Update when we extend support for other archs 225 if (arch != "x86_64") 226 continue; 227 } 228 229 if (!objectFile.empty() && !objectFile.endswith(".o")) { 230 error("invalid object file name \"" + objectFile + 231 "\" in order file: should end with .o"); 232 continue; 233 } 234 235 if (!symbol.empty()) { 236 SymbolPriorityEntry &entry = config->priorities[symbol]; 237 if (!objectFile.empty()) 238 entry.objectFiles.insert(std::make_pair(objectFile, priority)); 239 else 240 entry.anyObjectFile = std::max(entry.anyObjectFile, priority); 241 } 242 243 --priority; 244 } 245 } 246 247 // We expect sub-library names of the form "libfoo", which will match a dylib 248 // with a path of .*/libfoo.dylib. 249 static bool markSubLibrary(StringRef searchName) { 250 for (InputFile *file : inputFiles) { 251 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 252 StringRef filename = path::filename(dylibFile->getName()); 253 if (filename.consume_front(searchName) && filename == ".dylib") { 254 dylibFile->reexport = true; 255 return true; 256 } 257 } 258 } 259 return false; 260 } 261 262 static void handlePlatformVersion(opt::ArgList::iterator &it, 263 const opt::ArgList::iterator &end) { 264 // -platform_version takes 3 args, which LLVM's option library doesn't 265 // support directly. So this explicitly handles that. 266 // FIXME: stash skipped args for later use. 267 for (int i = 0; i < 3; ++i) { 268 ++it; 269 if (it == end || (*it)->getOption().getID() != OPT_INPUT) 270 fatal("usage: -platform_version platform min_version sdk_version"); 271 } 272 } 273 274 bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly, 275 raw_ostream &stdoutOS, raw_ostream &stderrOS) { 276 lld::stdoutOS = &stdoutOS; 277 lld::stderrOS = &stderrOS; 278 279 stderrOS.enable_colors(stderrOS.has_colors()); 280 // TODO: Set up error handler properly, e.g. the errorLimitExceededMsg 281 282 MachOOptTable parser; 283 opt::InputArgList args = parser.parse(argsArr.slice(1)); 284 285 config = make<Configuration>(); 286 symtab = make<SymbolTable>(); 287 target = createTargetInfo(args); 288 289 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main")); 290 config->outputFile = args.getLastArgValue(OPT_o, "a.out"); 291 config->installName = 292 args.getLastArgValue(OPT_install_name, config->outputFile); 293 config->searchPaths = getSearchPaths(args); 294 config->outputType = args.hasArg(OPT_dylib) ? MH_DYLIB : MH_EXECUTE; 295 296 if (args.hasArg(OPT_v)) { 297 message(getLLDVersion()); 298 std::vector<StringRef> &searchPaths = config->searchPaths; 299 message("Library search paths:\n" + 300 llvm::join(searchPaths.begin(), searchPaths.end(), "\n")); 301 freeArena(); 302 return !errorCount(); 303 } 304 305 for (opt::ArgList::iterator it = args.begin(), end = args.end(); it != end; 306 ++it) { 307 const opt::Arg *arg = *it; 308 switch (arg->getOption().getID()) { 309 case OPT_INPUT: 310 addFile(arg->getValue()); 311 break; 312 case OPT_l: { 313 StringRef name = arg->getValue(); 314 if (Optional<std::string> path = findLibrary(name)) { 315 addFile(*path); 316 break; 317 } 318 error("library not found for -l" + name); 319 break; 320 } 321 case OPT_platform_version: { 322 handlePlatformVersion(it, end); // Can advance "it". 323 break; 324 } 325 } 326 } 327 328 // Now that all dylibs have been loaded, search for those that should be 329 // re-exported. 330 for (opt::Arg *arg : args.filtered(OPT_sub_library)) { 331 config->hasReexports = true; 332 StringRef searchName = arg->getValue(); 333 if (!markSubLibrary(searchName)) 334 error("-sub_library " + searchName + " does not match a supplied dylib"); 335 } 336 337 StringRef orderFile = args.getLastArgValue(OPT_order_file); 338 if (!orderFile.empty()) 339 parseOrderFile(orderFile); 340 341 // dyld requires us to load libSystem. Since we may run tests on non-OSX 342 // systems which do not have libSystem, we mock it out here. 343 // TODO: Replace this with a stub tbd file once we have TAPI support. 344 if (StringRef(getenv("LLD_IN_TEST")) == "1" && 345 config->outputType == MH_EXECUTE) { 346 inputFiles.push_back(DylibFile::createLibSystemMock()); 347 } 348 349 if (config->outputType == MH_EXECUTE && !isa<Defined>(config->entry)) { 350 error("undefined symbol: " + config->entry->getName()); 351 return false; 352 } 353 354 createSyntheticSections(); 355 356 // Initialize InputSections. 357 for (InputFile *file : inputFiles) { 358 for (SubsectionMap &map : file->subsections) { 359 for (auto &p : map) { 360 InputSection *isec = p.second; 361 inputSections.push_back(isec); 362 } 363 } 364 } 365 366 // Write to an output file. 367 writeResult(); 368 369 if (canExitEarly) 370 exitLld(errorCount() ? 1 : 0); 371 372 freeArena(); 373 return !errorCount(); 374 } 375