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