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