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 "SyntheticSections.h" 17 #include "Target.h" 18 #include "Writer.h" 19 20 #include "lld/Common/Args.h" 21 #include "lld/Common/Driver.h" 22 #include "lld/Common/ErrorHandler.h" 23 #include "lld/Common/LLVM.h" 24 #include "lld/Common/Memory.h" 25 #include "lld/Common/Version.h" 26 #include "llvm/ADT/DenseSet.h" 27 #include "llvm/ADT/StringExtras.h" 28 #include "llvm/ADT/StringRef.h" 29 #include "llvm/BinaryFormat/MachO.h" 30 #include "llvm/BinaryFormat/Magic.h" 31 #include "llvm/Object/Archive.h" 32 #include "llvm/Option/ArgList.h" 33 #include "llvm/Option/Option.h" 34 #include "llvm/Support/FileSystem.h" 35 #include "llvm/Support/Host.h" 36 #include "llvm/Support/MemoryBuffer.h" 37 #include "llvm/Support/Path.h" 38 39 #include <algorithm> 40 41 using namespace llvm; 42 using namespace llvm::MachO; 43 using namespace llvm::sys; 44 using namespace llvm::opt; 45 using namespace lld; 46 using namespace lld::macho; 47 48 Configuration *lld::macho::config; 49 50 // Create prefix string literals used in Options.td 51 #define PREFIX(NAME, VALUE) const char *NAME[] = VALUE; 52 #include "Options.inc" 53 #undef PREFIX 54 55 // Create table mapping all options defined in Options.td 56 static const opt::OptTable::Info optInfo[] = { 57 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 58 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ 59 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, 60 #include "Options.inc" 61 #undef OPTION 62 }; 63 64 MachOOptTable::MachOOptTable() : OptTable(optInfo) {} 65 66 opt::InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) { 67 // Make InputArgList from string vectors. 68 unsigned missingIndex; 69 unsigned missingCount; 70 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size()); 71 72 opt::InputArgList args = ParseArgs(vec, missingIndex, missingCount); 73 74 if (missingCount) 75 error(Twine(args.getArgString(missingIndex)) + ": missing argument"); 76 77 for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) 78 error("unknown argument: " + arg->getSpelling()); 79 return args; 80 } 81 82 void MachOOptTable::printHelp(const char *argv0, bool showHidden) const { 83 PrintHelp(lld::outs(), (std::string(argv0) + " [options] file...").c_str(), 84 "LLVM Linker", showHidden); 85 lld::outs() << "\n"; 86 } 87 88 static Optional<std::string> findWithExtension(StringRef base, 89 ArrayRef<StringRef> extensions) { 90 for (StringRef ext : extensions) { 91 Twine location = base + ext; 92 if (fs::exists(location)) 93 return location.str(); 94 } 95 return {}; 96 } 97 98 static Optional<std::string> findLibrary(StringRef name) { 99 llvm::SmallString<261> location; 100 for (StringRef dir : config->librarySearchPaths) { 101 location = dir; 102 path::append(location, Twine("lib") + name); 103 if (Optional<std::string> path = 104 findWithExtension(location, {".tbd", ".dylib", ".a"})) 105 return path; 106 } 107 return {}; 108 } 109 110 static Optional<std::string> findFramework(StringRef name) { 111 llvm::SmallString<260> symlink; 112 StringRef suffix; 113 std::tie(name, suffix) = name.split(","); 114 for (StringRef dir : config->frameworkSearchPaths) { 115 symlink = dir; 116 path::append(symlink, name + ".framework", name); 117 118 if (!suffix.empty()) { 119 // NOTE: we must resolve the symlink before trying the suffixes, because 120 // there are no symlinks for the suffixed paths. 121 llvm::SmallString<260> location; 122 if (!fs::real_path(symlink, location)) { 123 // only append suffix if realpath() succeeds 124 Twine suffixed = location + suffix; 125 if (fs::exists(suffixed)) 126 return suffixed.str(); 127 } 128 // Suffix lookup failed, fall through to the no-suffix case. 129 } 130 131 if (Optional<std::string> path = findWithExtension(symlink, {".tbd", ""})) 132 return path; 133 } 134 return {}; 135 } 136 137 static TargetInfo *createTargetInfo(opt::InputArgList &args) { 138 StringRef arch = args.getLastArgValue(OPT_arch, "x86_64"); 139 config->arch = llvm::MachO::getArchitectureFromName( 140 args.getLastArgValue(OPT_arch, arch)); 141 switch (config->arch) { 142 case llvm::MachO::AK_x86_64: 143 case llvm::MachO::AK_x86_64h: 144 return createX86_64TargetInfo(); 145 default: 146 fatal("missing or unsupported -arch " + arch); 147 } 148 } 149 150 static bool isDirectory(StringRef option, StringRef path) { 151 if (!fs::exists(path)) { 152 warn("directory not found for option -" + option + path); 153 return false; 154 } else if (!fs::is_directory(path)) { 155 warn("option -" + option + path + " references a non-directory path"); 156 return false; 157 } 158 return true; 159 } 160 161 static void getSearchPaths(std::vector<StringRef> &paths, unsigned optionCode, 162 opt::InputArgList &args, 163 const std::vector<StringRef> &roots, 164 const SmallVector<StringRef, 2> &systemPaths) { 165 StringRef optionLetter{(optionCode == OPT_F ? "F" : "L")}; 166 for (auto const &path : args::getStrings(args, optionCode)) { 167 // NOTE: only absolute paths are re-rooted to syslibroot(s) 168 if (llvm::sys::path::is_absolute(path, llvm::sys::path::Style::posix)) { 169 for (StringRef root : roots) { 170 SmallString<261> buffer(root); 171 llvm::sys::path::append(buffer, path); 172 // Do not warn about paths that are computed via the syslib roots 173 if (llvm::sys::fs::is_directory(buffer)) 174 paths.push_back(saver.save(buffer.str())); 175 } 176 } else { 177 if (isDirectory(optionLetter, path)) 178 paths.push_back(path); 179 } 180 } 181 182 // `-Z` suppresses the standard "system" search paths. 183 if (args.hasArg(OPT_Z)) 184 return; 185 186 for (auto const &path : systemPaths) { 187 for (auto root : roots) { 188 SmallString<261> buffer(root); 189 llvm::sys::path::append(buffer, path); 190 if (isDirectory(optionLetter, buffer)) 191 paths.push_back(saver.save(buffer.str())); 192 } 193 } 194 } 195 196 static void getLibrarySearchPaths(opt::InputArgList &args, 197 const std::vector<StringRef> &roots, 198 std::vector<StringRef> &paths) { 199 getSearchPaths(paths, OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"}); 200 } 201 202 static void getFrameworkSearchPaths(opt::InputArgList &args, 203 const std::vector<StringRef> &roots, 204 std::vector<StringRef> &paths) { 205 getSearchPaths(paths, OPT_F, args, roots, 206 {"/Library/Frameworks", "/System/Library/Frameworks"}); 207 } 208 209 static void addFile(StringRef path) { 210 Optional<MemoryBufferRef> buffer = readFile(path); 211 if (!buffer) 212 return; 213 MemoryBufferRef mbref = *buffer; 214 215 switch (identify_magic(mbref.getBuffer())) { 216 case file_magic::archive: { 217 std::unique_ptr<object::Archive> file = CHECK( 218 object::Archive::create(mbref), path + ": failed to parse archive"); 219 220 if (!file->isEmpty() && !file->hasSymbolTable()) 221 error(path + ": archive has no index; run ranlib to add one"); 222 223 inputFiles.push_back(make<ArchiveFile>(std::move(file))); 224 break; 225 } 226 case file_magic::macho_object: 227 inputFiles.push_back(make<ObjFile>(mbref)); 228 break; 229 case file_magic::macho_dynamically_linked_shared_lib: 230 inputFiles.push_back(make<DylibFile>(mbref)); 231 break; 232 case file_magic::tapi_file: { 233 llvm::Expected<std::unique_ptr<llvm::MachO::InterfaceFile>> result = 234 TextAPIReader::get(mbref); 235 if (!result) 236 return; 237 238 inputFiles.push_back(make<DylibFile>(std::move(*result))); 239 break; 240 } 241 default: 242 error(path + ": unhandled file type"); 243 } 244 } 245 246 static void addFileList(StringRef path) { 247 Optional<MemoryBufferRef> buffer = readFile(path); 248 if (!buffer) 249 return; 250 MemoryBufferRef mbref = *buffer; 251 for (StringRef path : args::getLines(mbref)) 252 addFile(path); 253 } 254 255 static std::array<StringRef, 6> archNames{"arm", "arm64", "i386", 256 "x86_64", "ppc", "ppc64"}; 257 static bool isArchString(StringRef s) { 258 static DenseSet<StringRef> archNamesSet(archNames.begin(), archNames.end()); 259 return archNamesSet.find(s) != archNamesSet.end(); 260 } 261 262 // An order file has one entry per line, in the following format: 263 // 264 // <arch>:<object file>:<symbol name> 265 // 266 // <arch> and <object file> are optional. If not specified, then that entry 267 // matches any symbol of that name. 268 // 269 // If a symbol is matched by multiple entries, then it takes the lowest-ordered 270 // entry (the one nearest to the front of the list.) 271 // 272 // The file can also have line comments that start with '#'. 273 static void parseOrderFile(StringRef path) { 274 Optional<MemoryBufferRef> buffer = readFile(path); 275 if (!buffer) { 276 error("Could not read order file at " + path); 277 return; 278 } 279 280 MemoryBufferRef mbref = *buffer; 281 size_t priority = std::numeric_limits<size_t>::max(); 282 for (StringRef rest : args::getLines(mbref)) { 283 StringRef arch, objectFile, symbol; 284 285 std::array<StringRef, 3> fields; 286 uint8_t fieldCount = 0; 287 while (rest != "" && fieldCount < 3) { 288 std::pair<StringRef, StringRef> p = getToken(rest, ": \t\n\v\f\r"); 289 StringRef tok = p.first; 290 rest = p.second; 291 292 // Check if we have a comment 293 if (tok == "" || tok[0] == '#') 294 break; 295 296 fields[fieldCount++] = tok; 297 } 298 299 switch (fieldCount) { 300 case 3: 301 arch = fields[0]; 302 objectFile = fields[1]; 303 symbol = fields[2]; 304 break; 305 case 2: 306 (isArchString(fields[0]) ? arch : objectFile) = fields[0]; 307 symbol = fields[1]; 308 break; 309 case 1: 310 symbol = fields[0]; 311 break; 312 case 0: 313 break; 314 default: 315 llvm_unreachable("too many fields in order file"); 316 } 317 318 if (!arch.empty()) { 319 if (!isArchString(arch)) { 320 error("invalid arch \"" + arch + "\" in order file: expected one of " + 321 llvm::join(archNames, ", ")); 322 continue; 323 } 324 325 // TODO: Update when we extend support for other archs 326 if (arch != "x86_64") 327 continue; 328 } 329 330 if (!objectFile.empty() && !objectFile.endswith(".o")) { 331 error("invalid object file name \"" + objectFile + 332 "\" in order file: should end with .o"); 333 continue; 334 } 335 336 if (!symbol.empty()) { 337 SymbolPriorityEntry &entry = config->priorities[symbol]; 338 if (!objectFile.empty()) 339 entry.objectFiles.insert(std::make_pair(objectFile, priority)); 340 else 341 entry.anyObjectFile = std::max(entry.anyObjectFile, priority); 342 } 343 344 --priority; 345 } 346 } 347 348 // We expect sub-library names of the form "libfoo", which will match a dylib 349 // with a path of .*/libfoo.dylib. 350 static bool markSubLibrary(StringRef searchName) { 351 for (InputFile *file : inputFiles) { 352 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 353 StringRef filename = path::filename(dylibFile->getName()); 354 if (filename.consume_front(searchName) && filename == ".dylib") { 355 dylibFile->reexport = true; 356 return true; 357 } 358 } 359 } 360 return false; 361 } 362 363 static inline char toLowerDash(char x) { 364 if (x >= 'A' && x <= 'Z') 365 return x - 'A' + 'a'; 366 else if (x == ' ') 367 return '-'; 368 return x; 369 } 370 371 static std::string lowerDash(StringRef s) { 372 return std::string(map_iterator(s.begin(), toLowerDash), 373 map_iterator(s.end(), toLowerDash)); 374 } 375 376 static void handlePlatformVersion(const opt::Arg *arg) { 377 StringRef platformStr = arg->getValue(0); 378 StringRef minVersionStr = arg->getValue(1); 379 StringRef sdkVersionStr = arg->getValue(2); 380 381 // TODO(compnerd) see if we can generate this case list via XMACROS 382 config->platform.kind = 383 llvm::StringSwitch<llvm::MachO::PlatformKind>(lowerDash(platformStr)) 384 .Cases("macos", "1", llvm::MachO::PlatformKind::macOS) 385 .Cases("ios", "2", llvm::MachO::PlatformKind::iOS) 386 .Cases("tvos", "3", llvm::MachO::PlatformKind::tvOS) 387 .Cases("watchos", "4", llvm::MachO::PlatformKind::watchOS) 388 .Cases("bridgeos", "5", llvm::MachO::PlatformKind::bridgeOS) 389 .Cases("mac-catalyst", "6", llvm::MachO::PlatformKind::macCatalyst) 390 .Cases("ios-simulator", "7", llvm::MachO::PlatformKind::iOSSimulator) 391 .Cases("tvos-simulator", "8", 392 llvm::MachO::PlatformKind::tvOSSimulator) 393 .Cases("watchos-simulator", "9", 394 llvm::MachO::PlatformKind::watchOSSimulator) 395 .Default(llvm::MachO::PlatformKind::unknown); 396 if (config->platform.kind == llvm::MachO::PlatformKind::unknown) 397 error(Twine("malformed platform: ") + platformStr); 398 // TODO: check validity of version strings, which varies by platform 399 // NOTE: ld64 accepts version strings with 5 components 400 // llvm::VersionTuple accepts no more than 4 components 401 // Has Apple ever published version strings with 5 components? 402 if (config->platform.minimum.tryParse(minVersionStr)) 403 error(Twine("malformed minimum version: ") + minVersionStr); 404 if (config->platform.sdk.tryParse(sdkVersionStr)) 405 error(Twine("malformed sdk version: ") + sdkVersionStr); 406 } 407 408 static void warnIfDeprecatedOption(const opt::Option &opt) { 409 if (!opt.getGroup().isValid()) 410 return; 411 if (opt.getGroup().getID() == OPT_grp_deprecated) { 412 warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:"); 413 warn(opt.getHelpText()); 414 } 415 } 416 417 static void warnIfUnimplementedOption(const opt::Option &opt) { 418 if (!opt.getGroup().isValid()) 419 return; 420 switch (opt.getGroup().getID()) { 421 case OPT_grp_deprecated: 422 // warn about deprecated options elsewhere 423 break; 424 case OPT_grp_undocumented: 425 warn("Option `" + opt.getPrefixedName() + 426 "' is undocumented. Should lld implement it?"); 427 break; 428 case OPT_grp_obsolete: 429 warn("Option `" + opt.getPrefixedName() + 430 "' is obsolete. Please modernize your usage."); 431 break; 432 case OPT_grp_ignored: 433 warn("Option `" + opt.getPrefixedName() + "' is ignored."); 434 break; 435 default: 436 warn("Option `" + opt.getPrefixedName() + 437 "' is not yet implemented. Stay tuned..."); 438 break; 439 } 440 } 441 442 bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly, 443 raw_ostream &stdoutOS, raw_ostream &stderrOS) { 444 lld::stdoutOS = &stdoutOS; 445 lld::stderrOS = &stderrOS; 446 447 stderrOS.enable_colors(stderrOS.has_colors()); 448 // TODO: Set up error handler properly, e.g. the errorLimitExceededMsg 449 450 MachOOptTable parser; 451 opt::InputArgList args = parser.parse(argsArr.slice(1)); 452 453 if (args.hasArg(OPT_help_hidden)) { 454 parser.printHelp(argsArr[0], /*showHidden=*/true); 455 return true; 456 } else if (args.hasArg(OPT_help)) { 457 parser.printHelp(argsArr[0], /*showHidden=*/false); 458 return true; 459 } 460 461 config = make<Configuration>(); 462 symtab = make<SymbolTable>(); 463 target = createTargetInfo(args); 464 465 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main")); 466 config->outputFile = args.getLastArgValue(OPT_o, "a.out"); 467 config->installName = 468 args.getLastArgValue(OPT_install_name, config->outputFile); 469 config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32); 470 config->outputType = args.hasArg(OPT_dylib) ? MH_DYLIB : MH_EXECUTE; 471 472 std::vector<StringRef> roots; 473 for (const Arg *arg : args.filtered(OPT_syslibroot)) 474 roots.push_back(arg->getValue()); 475 // NOTE: the final `-syslibroot` being `/` will ignore all roots 476 if (roots.size() && roots.back() == "/") 477 roots.clear(); 478 // NOTE: roots can never be empty - add an empty root to simplify the library 479 // and framework search path computation. 480 if (roots.empty()) 481 roots.emplace_back(""); 482 483 getLibrarySearchPaths(args, roots, config->librarySearchPaths); 484 getFrameworkSearchPaths(args, roots, config->frameworkSearchPaths); 485 486 if (args.hasArg(OPT_v)) { 487 message(getLLDVersion()); 488 message(StringRef("Library search paths:") + 489 (config->librarySearchPaths.size() 490 ? "\n\t" + llvm::join(config->librarySearchPaths, "\n\t") 491 : "")); 492 message(StringRef("Framework search paths:") + 493 (config->frameworkSearchPaths.size() 494 ? "\n\t" + llvm::join(config->frameworkSearchPaths, "\n\t") 495 : "")); 496 freeArena(); 497 return !errorCount(); 498 } 499 500 for (const auto &arg : args) { 501 const auto &opt = arg->getOption(); 502 warnIfDeprecatedOption(opt); 503 switch (arg->getOption().getID()) { 504 case OPT_INPUT: 505 addFile(arg->getValue()); 506 break; 507 case OPT_filelist: 508 addFileList(arg->getValue()); 509 break; 510 case OPT_l: { 511 StringRef name = arg->getValue(); 512 if (Optional<std::string> path = findLibrary(name)) { 513 addFile(*path); 514 break; 515 } 516 error("library not found for -l" + name); 517 break; 518 } 519 case OPT_framework: { 520 StringRef name = arg->getValue(); 521 if (Optional<std::string> path = findFramework(name)) { 522 addFile(*path); 523 break; 524 } 525 error("framework not found for -framework " + name); 526 break; 527 } 528 case OPT_platform_version: 529 handlePlatformVersion(arg); 530 break; 531 case OPT_o: 532 case OPT_dylib: 533 case OPT_e: 534 case OPT_F: 535 case OPT_L: 536 case OPT_headerpad: 537 case OPT_install_name: 538 case OPT_Z: 539 case OPT_arch: 540 case OPT_syslibroot: 541 case OPT_sectcreate: 542 // handled elsewhere 543 break; 544 default: 545 warnIfUnimplementedOption(opt); 546 break; 547 } 548 } 549 550 // Now that all dylibs have been loaded, search for those that should be 551 // re-exported. 552 for (opt::Arg *arg : args.filtered(OPT_sub_library)) { 553 config->hasReexports = true; 554 StringRef searchName = arg->getValue(); 555 if (!markSubLibrary(searchName)) 556 error("-sub_library " + searchName + " does not match a supplied dylib"); 557 } 558 559 StringRef orderFile = args.getLastArgValue(OPT_order_file); 560 if (!orderFile.empty()) 561 parseOrderFile(orderFile); 562 563 if (config->outputType == MH_EXECUTE && !isa<Defined>(config->entry)) { 564 error("undefined symbol: " + config->entry->getName()); 565 return false; 566 } 567 568 createSyntheticSections(); 569 symtab->addDSOHandle(in.header); 570 571 for (opt::Arg *arg : args.filtered(OPT_sectcreate)) { 572 StringRef segName = arg->getValue(0); 573 StringRef sectName = arg->getValue(1); 574 StringRef fileName = arg->getValue(2); 575 Optional<MemoryBufferRef> buffer = readFile(fileName); 576 if (buffer) 577 inputFiles.push_back(make<OpaqueFile>(*buffer, segName, sectName)); 578 } 579 580 // Initialize InputSections. 581 for (InputFile *file : inputFiles) { 582 for (SubsectionMap &map : file->subsections) { 583 for (auto &p : map) { 584 InputSection *isec = p.second; 585 inputSections.push_back(isec); 586 } 587 } 588 } 589 590 // Write to an output file. 591 writeResult(); 592 593 if (canExitEarly) 594 exitLld(errorCount() ? 1 : 0); 595 596 freeArena(); 597 return !errorCount(); 598 } 599