[LLD][COFF] Ignore DEBUG_S_XFGHASH_TYPE/VIRTUAL
[llvm-project.git] / lld / MachO / Driver.cpp
blob583917aec73c7278a93aa160a49b4403de928e31
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 //===----------------------------------------------------------------------===//
9 #include "Driver.h"
10 #include "Config.h"
11 #include "ICF.h"
12 #include "InputFiles.h"
13 #include "LTO.h"
14 #include "MarkLive.h"
15 #include "ObjC.h"
16 #include "OutputSection.h"
17 #include "OutputSegment.h"
18 #include "SectionPriorities.h"
19 #include "SymbolTable.h"
20 #include "Symbols.h"
21 #include "SyntheticSections.h"
22 #include "Target.h"
23 #include "UnwindInfoSection.h"
24 #include "Writer.h"
26 #include "lld/Common/Args.h"
27 #include "lld/Common/Driver.h"
28 #include "lld/Common/ErrorHandler.h"
29 #include "lld/Common/LLVM.h"
30 #include "lld/Common/Memory.h"
31 #include "lld/Common/Reproduce.h"
32 #include "lld/Common/Version.h"
33 #include "llvm/ADT/DenseSet.h"
34 #include "llvm/ADT/StringExtras.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/BinaryFormat/MachO.h"
37 #include "llvm/BinaryFormat/Magic.h"
38 #include "llvm/Config/llvm-config.h"
39 #include "llvm/LTO/LTO.h"
40 #include "llvm/Object/Archive.h"
41 #include "llvm/Option/ArgList.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/FileSystem.h"
44 #include "llvm/Support/Host.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/Parallel.h"
47 #include "llvm/Support/Path.h"
48 #include "llvm/Support/TarWriter.h"
49 #include "llvm/Support/TargetSelect.h"
50 #include "llvm/Support/TimeProfiler.h"
51 #include "llvm/TextAPI/PackedVersion.h"
53 #include <algorithm>
55 using namespace llvm;
56 using namespace llvm::MachO;
57 using namespace llvm::object;
58 using namespace llvm::opt;
59 using namespace llvm::sys;
60 using namespace lld;
61 using namespace lld::macho;
63 std::unique_ptr<Configuration> macho::config;
64 std::unique_ptr<DependencyTracker> macho::depTracker;
66 static HeaderFileType getOutputType(const InputArgList &args) {
67 // TODO: -r, -dylinker, -preload...
68 Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute);
69 if (outputArg == nullptr)
70 return MH_EXECUTE;
72 switch (outputArg->getOption().getID()) {
73 case OPT_bundle:
74 return MH_BUNDLE;
75 case OPT_dylib:
76 return MH_DYLIB;
77 case OPT_execute:
78 return MH_EXECUTE;
79 default:
80 llvm_unreachable("internal error");
84 static DenseMap<CachedHashStringRef, StringRef> resolvedLibraries;
85 static Optional<StringRef> findLibrary(StringRef name) {
86 CachedHashStringRef key(name);
87 auto entry = resolvedLibraries.find(key);
88 if (entry != resolvedLibraries.end())
89 return entry->second;
91 auto doFind = [&] {
92 if (config->searchDylibsFirst) {
93 if (Optional<StringRef> path = findPathCombination(
94 "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"}))
95 return path;
96 return findPathCombination("lib" + name, config->librarySearchPaths,
97 {".a"});
99 return findPathCombination("lib" + name, config->librarySearchPaths,
100 {".tbd", ".dylib", ".a"});
103 Optional<StringRef> path = doFind();
104 if (path)
105 resolvedLibraries[key] = *path;
107 return path;
110 static DenseMap<CachedHashStringRef, StringRef> resolvedFrameworks;
111 static Optional<StringRef> findFramework(StringRef name) {
112 CachedHashStringRef key(name);
113 auto entry = resolvedFrameworks.find(key);
114 if (entry != resolvedFrameworks.end())
115 return entry->second;
117 SmallString<260> symlink;
118 StringRef suffix;
119 std::tie(name, suffix) = name.split(",");
120 for (StringRef dir : config->frameworkSearchPaths) {
121 symlink = dir;
122 path::append(symlink, name + ".framework", name);
124 if (!suffix.empty()) {
125 // NOTE: we must resolve the symlink before trying the suffixes, because
126 // there are no symlinks for the suffixed paths.
127 SmallString<260> location;
128 if (!fs::real_path(symlink, location)) {
129 // only append suffix if realpath() succeeds
130 Twine suffixed = location + suffix;
131 if (fs::exists(suffixed))
132 return resolvedFrameworks[key] = saver().save(suffixed.str());
134 // Suffix lookup failed, fall through to the no-suffix case.
137 if (Optional<StringRef> path = resolveDylibPath(symlink.str()))
138 return resolvedFrameworks[key] = *path;
140 return {};
143 static bool warnIfNotDirectory(StringRef option, StringRef path) {
144 if (!fs::exists(path)) {
145 warn("directory not found for option -" + option + path);
146 return false;
147 } else if (!fs::is_directory(path)) {
148 warn("option -" + option + path + " references a non-directory path");
149 return false;
151 return true;
154 static std::vector<StringRef>
155 getSearchPaths(unsigned optionCode, InputArgList &args,
156 const std::vector<StringRef> &roots,
157 const SmallVector<StringRef, 2> &systemPaths) {
158 std::vector<StringRef> paths;
159 StringRef optionLetter{optionCode == OPT_F ? "F" : "L"};
160 for (StringRef path : args::getStrings(args, optionCode)) {
161 // NOTE: only absolute paths are re-rooted to syslibroot(s)
162 bool found = false;
163 if (path::is_absolute(path, path::Style::posix)) {
164 for (StringRef root : roots) {
165 SmallString<261> buffer(root);
166 path::append(buffer, path);
167 // Do not warn about paths that are computed via the syslib roots
168 if (fs::is_directory(buffer)) {
169 paths.push_back(saver().save(buffer.str()));
170 found = true;
174 if (!found && warnIfNotDirectory(optionLetter, path))
175 paths.push_back(path);
178 // `-Z` suppresses the standard "system" search paths.
179 if (args.hasArg(OPT_Z))
180 return paths;
182 for (const StringRef &path : systemPaths) {
183 for (const StringRef &root : roots) {
184 SmallString<261> buffer(root);
185 path::append(buffer, path);
186 if (fs::is_directory(buffer))
187 paths.push_back(saver().save(buffer.str()));
190 return paths;
193 static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) {
194 std::vector<StringRef> roots;
195 for (const Arg *arg : args.filtered(OPT_syslibroot))
196 roots.push_back(arg->getValue());
197 // NOTE: the final `-syslibroot` being `/` will ignore all roots
198 if (!roots.empty() && roots.back() == "/")
199 roots.clear();
200 // NOTE: roots can never be empty - add an empty root to simplify the library
201 // and framework search path computation.
202 if (roots.empty())
203 roots.emplace_back("");
204 return roots;
207 static std::vector<StringRef>
208 getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) {
209 return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"});
212 static std::vector<StringRef>
213 getFrameworkSearchPaths(InputArgList &args,
214 const std::vector<StringRef> &roots) {
215 return getSearchPaths(OPT_F, args, roots,
216 {"/Library/Frameworks", "/System/Library/Frameworks"});
219 static llvm::CachePruningPolicy getLTOCachePolicy(InputArgList &args) {
220 SmallString<128> ltoPolicy;
221 auto add = [&ltoPolicy](Twine val) {
222 if (!ltoPolicy.empty())
223 ltoPolicy += ":";
224 val.toVector(ltoPolicy);
226 for (const Arg *arg :
227 args.filtered(OPT_thinlto_cache_policy, OPT_prune_interval_lto,
228 OPT_prune_after_lto, OPT_max_relative_cache_size_lto)) {
229 switch (arg->getOption().getID()) {
230 case OPT_thinlto_cache_policy:
231 add(arg->getValue());
232 break;
233 case OPT_prune_interval_lto:
234 if (!strcmp("-1", arg->getValue()))
235 add("prune_interval=87600h"); // 10 years
236 else
237 add(Twine("prune_interval=") + arg->getValue() + "s");
238 break;
239 case OPT_prune_after_lto:
240 add(Twine("prune_after=") + arg->getValue() + "s");
241 break;
242 case OPT_max_relative_cache_size_lto:
243 add(Twine("cache_size=") + arg->getValue() + "%");
244 break;
247 return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy");
250 // What caused a given library to be loaded. Only relevant for archives.
251 // Note that this does not tell us *how* we should load the library, i.e.
252 // whether we should do it lazily or eagerly (AKA force loading). The "how" is
253 // decided within addFile().
254 enum class LoadType {
255 CommandLine, // Library was passed as a regular CLI argument
256 CommandLineForce, // Library was passed via `-force_load`
257 LCLinkerOption, // Library was passed via LC_LINKER_OPTIONS
260 struct ArchiveFileInfo {
261 ArchiveFile *file;
262 bool isCommandLineLoad;
265 static DenseMap<StringRef, ArchiveFileInfo> loadedArchives;
267 static InputFile *addFile(StringRef path, LoadType loadType,
268 bool isLazy = false, bool isExplicit = true,
269 bool isBundleLoader = false,
270 bool isForceHidden = false) {
271 Optional<MemoryBufferRef> buffer = readFile(path);
272 if (!buffer)
273 return nullptr;
274 MemoryBufferRef mbref = *buffer;
275 InputFile *newFile = nullptr;
277 file_magic magic = identify_magic(mbref.getBuffer());
278 switch (magic) {
279 case file_magic::archive: {
280 bool isCommandLineLoad = loadType != LoadType::LCLinkerOption;
281 // Avoid loading archives twice. If the archives are being force-loaded,
282 // loading them twice would create duplicate symbol errors. In the
283 // non-force-loading case, this is just a minor performance optimization.
284 // We don't take a reference to cachedFile here because the
285 // loadArchiveMember() call below may recursively call addFile() and
286 // invalidate this reference.
287 auto entry = loadedArchives.find(path);
289 ArchiveFile *file;
290 if (entry == loadedArchives.end()) {
291 // No cached archive, we need to create a new one
292 std::unique_ptr<object::Archive> archive = CHECK(
293 object::Archive::create(mbref), path + ": failed to parse archive");
295 if (!archive->isEmpty() && !archive->hasSymbolTable())
296 error(path + ": archive has no index; run ranlib to add one");
297 file = make<ArchiveFile>(std::move(archive), isForceHidden);
298 } else {
299 file = entry->second.file;
300 // Command-line loads take precedence. If file is previously loaded via
301 // command line, or is loaded via LC_LINKER_OPTION and being loaded via
302 // LC_LINKER_OPTION again, using the cached archive is enough.
303 if (entry->second.isCommandLineLoad || !isCommandLineLoad)
304 return file;
307 bool isLCLinkerForceLoad = loadType == LoadType::LCLinkerOption &&
308 config->forceLoadSwift &&
309 path::filename(path).startswith("libswift");
310 if ((isCommandLineLoad && config->allLoad) ||
311 loadType == LoadType::CommandLineForce || isLCLinkerForceLoad) {
312 if (Optional<MemoryBufferRef> buffer = readFile(path)) {
313 Error e = Error::success();
314 for (const object::Archive::Child &c : file->getArchive().children(e)) {
315 StringRef reason;
316 switch (loadType) {
317 case LoadType::LCLinkerOption:
318 reason = "LC_LINKER_OPTION";
319 break;
320 case LoadType::CommandLineForce:
321 reason = "-force_load";
322 break;
323 case LoadType::CommandLine:
324 reason = "-all_load";
325 break;
327 if (Error e = file->fetch(c, reason))
328 error(toString(file) + ": " + reason +
329 " failed to load archive member: " + toString(std::move(e)));
331 if (e)
332 error(toString(file) +
333 ": Archive::children failed: " + toString(std::move(e)));
335 } else if (isCommandLineLoad && config->forceLoadObjC) {
336 for (const object::Archive::Symbol &sym : file->getArchive().symbols())
337 if (sym.getName().startswith(objc::klass))
338 file->fetch(sym);
340 // TODO: no need to look for ObjC sections for a given archive member if
341 // we already found that it contains an ObjC symbol.
342 if (Optional<MemoryBufferRef> buffer = readFile(path)) {
343 Error e = Error::success();
344 for (const object::Archive::Child &c : file->getArchive().children(e)) {
345 Expected<MemoryBufferRef> mb = c.getMemoryBufferRef();
346 if (!mb || !hasObjCSection(*mb))
347 continue;
348 if (Error e = file->fetch(c, "-ObjC"))
349 error(toString(file) + ": -ObjC failed to load archive member: " +
350 toString(std::move(e)));
352 if (e)
353 error(toString(file) +
354 ": Archive::children failed: " + toString(std::move(e)));
358 file->addLazySymbols();
359 loadedArchives[path] = ArchiveFileInfo{file, isCommandLineLoad};
360 newFile = file;
361 break;
363 case file_magic::macho_object:
364 newFile = make<ObjFile>(mbref, getModTime(path), "", isLazy);
365 break;
366 case file_magic::macho_dynamically_linked_shared_lib:
367 case file_magic::macho_dynamically_linked_shared_lib_stub:
368 case file_magic::tapi_file:
369 if (DylibFile *dylibFile =
370 loadDylib(mbref, nullptr, /*isBundleLoader=*/false, isExplicit))
371 newFile = dylibFile;
372 break;
373 case file_magic::bitcode:
374 newFile = make<BitcodeFile>(mbref, "", 0, isLazy);
375 break;
376 case file_magic::macho_executable:
377 case file_magic::macho_bundle:
378 // We only allow executable and bundle type here if it is used
379 // as a bundle loader.
380 if (!isBundleLoader)
381 error(path + ": unhandled file type");
382 if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader))
383 newFile = dylibFile;
384 break;
385 default:
386 error(path + ": unhandled file type");
388 if (newFile && !isa<DylibFile>(newFile)) {
389 if ((isa<ObjFile>(newFile) || isa<BitcodeFile>(newFile)) && newFile->lazy &&
390 config->forceLoadObjC) {
391 for (Symbol *sym : newFile->symbols)
392 if (sym && sym->getName().startswith(objc::klass)) {
393 extract(*newFile, "-ObjC");
394 break;
396 if (newFile->lazy && hasObjCSection(mbref))
397 extract(*newFile, "-ObjC");
400 // printArchiveMemberLoad() prints both .a and .o names, so no need to
401 // print the .a name here. Similarly skip lazy files.
402 if (config->printEachFile && magic != file_magic::archive && !isLazy)
403 message(toString(newFile));
404 inputFiles.insert(newFile);
406 return newFile;
409 static void addLibrary(StringRef name, bool isNeeded, bool isWeak,
410 bool isReexport, bool isHidden, bool isExplicit,
411 LoadType loadType) {
412 if (Optional<StringRef> path = findLibrary(name)) {
413 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
414 addFile(*path, loadType, /*isLazy=*/false, isExplicit,
415 /*isBundleLoader=*/false, isHidden))) {
416 if (isNeeded)
417 dylibFile->forceNeeded = true;
418 if (isWeak)
419 dylibFile->forceWeakImport = true;
420 if (isReexport) {
421 config->hasReexports = true;
422 dylibFile->reexport = true;
425 return;
427 error("library not found for -l" + name);
430 static DenseSet<StringRef> loadedObjectFrameworks;
431 static void addFramework(StringRef name, bool isNeeded, bool isWeak,
432 bool isReexport, bool isExplicit, LoadType loadType) {
433 if (Optional<StringRef> path = findFramework(name)) {
434 if (loadedObjectFrameworks.contains(*path))
435 return;
437 InputFile *file =
438 addFile(*path, loadType, /*isLazy=*/false, isExplicit, false);
439 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(file)) {
440 if (isNeeded)
441 dylibFile->forceNeeded = true;
442 if (isWeak)
443 dylibFile->forceWeakImport = true;
444 if (isReexport) {
445 config->hasReexports = true;
446 dylibFile->reexport = true;
448 } else if (isa_and_nonnull<ObjFile>(file) ||
449 isa_and_nonnull<BitcodeFile>(file)) {
450 // Cache frameworks containing object or bitcode files to avoid duplicate
451 // symbols. Frameworks containing static archives are cached separately
452 // in addFile() to share caching with libraries, and frameworks
453 // containing dylibs should allow overwriting of attributes such as
454 // forceNeeded by subsequent loads
455 loadedObjectFrameworks.insert(*path);
457 return;
459 error("framework not found for -framework " + name);
462 // Parses LC_LINKER_OPTION contents, which can add additional command line
463 // flags. This directly parses the flags instead of using the standard argument
464 // parser to improve performance.
465 void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) {
466 SmallVector<StringRef, 4> argv;
467 size_t offset = 0;
468 for (unsigned i = 0; i < argc && offset < data.size(); ++i) {
469 argv.push_back(data.data() + offset);
470 offset += strlen(data.data() + offset) + 1;
472 if (argv.size() != argc || offset > data.size())
473 fatal(toString(f) + ": invalid LC_LINKER_OPTION");
475 unsigned i = 0;
476 StringRef arg = argv[i];
477 if (arg.consume_front("-l")) {
478 addLibrary(arg, /*isNeeded=*/false, /*isWeak=*/false,
479 /*isReexport=*/false, /*isHidden=*/false, /*isExplicit=*/false,
480 LoadType::LCLinkerOption);
481 } else if (arg == "-framework") {
482 StringRef name = argv[++i];
483 addFramework(name, /*isNeeded=*/false, /*isWeak=*/false,
484 /*isReexport=*/false, /*isExplicit=*/false,
485 LoadType::LCLinkerOption);
486 } else {
487 error(arg + " is not allowed in LC_LINKER_OPTION");
491 static void addFileList(StringRef path, bool isLazy) {
492 Optional<MemoryBufferRef> buffer = readFile(path);
493 if (!buffer)
494 return;
495 MemoryBufferRef mbref = *buffer;
496 for (StringRef path : args::getLines(mbref))
497 addFile(rerootPath(path), LoadType::CommandLine, isLazy);
500 // We expect sub-library names of the form "libfoo", which will match a dylib
501 // with a path of .*/libfoo.{dylib, tbd}.
502 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
503 // I'm not sure what the use case for that is.
504 static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) {
505 for (InputFile *file : inputFiles) {
506 if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
507 StringRef filename = path::filename(dylibFile->getName());
508 if (filename.consume_front(searchName) &&
509 (filename.empty() || llvm::is_contained(extensions, filename))) {
510 dylibFile->reexport = true;
511 return true;
515 return false;
518 // This function is called on startup. We need this for LTO since
519 // LTO calls LLVM functions to compile bitcode files to native code.
520 // Technically this can be delayed until we read bitcode files, but
521 // we don't bother to do lazily because the initialization is fast.
522 static void initLLVM() {
523 InitializeAllTargets();
524 InitializeAllTargetMCs();
525 InitializeAllAsmPrinters();
526 InitializeAllAsmParsers();
529 static bool compileBitcodeFiles() {
530 TimeTraceScope timeScope("LTO");
531 auto *lto = make<BitcodeCompiler>();
532 for (InputFile *file : inputFiles)
533 if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file))
534 if (!file->lazy)
535 lto->add(*bitcodeFile);
537 std::vector<ObjFile *> compiled = lto->compile();
538 for (ObjFile *file : compiled)
539 inputFiles.insert(file);
541 return !compiled.empty();
544 // Replaces common symbols with defined symbols residing in __common sections.
545 // This function must be called after all symbol names are resolved (i.e. after
546 // all InputFiles have been loaded.) As a result, later operations won't see
547 // any CommonSymbols.
548 static void replaceCommonSymbols() {
549 TimeTraceScope timeScope("Replace common symbols");
550 ConcatOutputSection *osec = nullptr;
551 for (Symbol *sym : symtab->getSymbols()) {
552 auto *common = dyn_cast<CommonSymbol>(sym);
553 if (common == nullptr)
554 continue;
556 // Casting to size_t will truncate large values on 32-bit architectures,
557 // but it's not really worth supporting the linking of 64-bit programs on
558 // 32-bit archs.
559 ArrayRef<uint8_t> data = {nullptr, static_cast<size_t>(common->size)};
560 // FIXME avoid creating one Section per symbol?
561 auto *section =
562 make<Section>(common->getFile(), segment_names::data,
563 section_names::common, S_ZEROFILL, /*addr=*/0);
564 auto *isec = make<ConcatInputSection>(*section, data, common->align);
565 if (!osec)
566 osec = ConcatOutputSection::getOrCreateForInput(isec);
567 isec->parent = osec;
568 inputSections.push_back(isec);
570 // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
571 // and pass them on here.
572 replaceSymbol<Defined>(
573 sym, sym->getName(), common->getFile(), isec, /*value=*/0, /*size=*/0,
574 /*isWeakDef=*/false, /*isExternal=*/true, common->privateExtern,
575 /*includeInSymtab=*/true, /*isThumb=*/false,
576 /*isReferencedDynamically=*/false, /*noDeadStrip=*/false);
580 static void initializeSectionRenameMap() {
581 if (config->dataConst) {
582 SmallVector<StringRef> v{section_names::got,
583 section_names::authGot,
584 section_names::authPtr,
585 section_names::nonLazySymbolPtr,
586 section_names::const_,
587 section_names::cfString,
588 section_names::moduleInitFunc,
589 section_names::moduleTermFunc,
590 section_names::objcClassList,
591 section_names::objcNonLazyClassList,
592 section_names::objcCatList,
593 section_names::objcNonLazyCatList,
594 section_names::objcProtoList,
595 section_names::objCImageInfo};
596 for (StringRef s : v)
597 config->sectionRenameMap[{segment_names::data, s}] = {
598 segment_names::dataConst, s};
600 config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = {
601 segment_names::text, section_names::text};
602 config->sectionRenameMap[{segment_names::import, section_names::pointers}] = {
603 config->dataConst ? segment_names::dataConst : segment_names::data,
604 section_names::nonLazySymbolPtr};
607 static inline char toLowerDash(char x) {
608 if (x >= 'A' && x <= 'Z')
609 return x - 'A' + 'a';
610 else if (x == ' ')
611 return '-';
612 return x;
615 static std::string lowerDash(StringRef s) {
616 return std::string(map_iterator(s.begin(), toLowerDash),
617 map_iterator(s.end(), toLowerDash));
620 struct PlatformVersion {
621 PlatformType platform = PLATFORM_UNKNOWN;
622 llvm::VersionTuple minimum;
623 llvm::VersionTuple sdk;
626 static PlatformVersion parsePlatformVersion(const Arg *arg) {
627 assert(arg->getOption().getID() == OPT_platform_version);
628 StringRef platformStr = arg->getValue(0);
629 StringRef minVersionStr = arg->getValue(1);
630 StringRef sdkVersionStr = arg->getValue(2);
632 PlatformVersion platformVersion;
634 // TODO(compnerd) see if we can generate this case list via XMACROS
635 platformVersion.platform =
636 StringSwitch<PlatformType>(lowerDash(platformStr))
637 .Cases("macos", "1", PLATFORM_MACOS)
638 .Cases("ios", "2", PLATFORM_IOS)
639 .Cases("tvos", "3", PLATFORM_TVOS)
640 .Cases("watchos", "4", PLATFORM_WATCHOS)
641 .Cases("bridgeos", "5", PLATFORM_BRIDGEOS)
642 .Cases("mac-catalyst", "6", PLATFORM_MACCATALYST)
643 .Cases("ios-simulator", "7", PLATFORM_IOSSIMULATOR)
644 .Cases("tvos-simulator", "8", PLATFORM_TVOSSIMULATOR)
645 .Cases("watchos-simulator", "9", PLATFORM_WATCHOSSIMULATOR)
646 .Cases("driverkit", "10", PLATFORM_DRIVERKIT)
647 .Default(PLATFORM_UNKNOWN);
648 if (platformVersion.platform == PLATFORM_UNKNOWN)
649 error(Twine("malformed platform: ") + platformStr);
650 // TODO: check validity of version strings, which varies by platform
651 // NOTE: ld64 accepts version strings with 5 components
652 // llvm::VersionTuple accepts no more than 4 components
653 // Has Apple ever published version strings with 5 components?
654 if (platformVersion.minimum.tryParse(minVersionStr))
655 error(Twine("malformed minimum version: ") + minVersionStr);
656 if (platformVersion.sdk.tryParse(sdkVersionStr))
657 error(Twine("malformed sdk version: ") + sdkVersionStr);
658 return platformVersion;
661 // Has the side-effect of setting Config::platformInfo.
662 static PlatformType parsePlatformVersions(const ArgList &args) {
663 std::map<PlatformType, PlatformVersion> platformVersions;
664 const PlatformVersion *lastVersionInfo = nullptr;
665 for (const Arg *arg : args.filtered(OPT_platform_version)) {
666 PlatformVersion version = parsePlatformVersion(arg);
668 // For each platform, the last flag wins:
669 // `-platform_version macos 2 3 -platform_version macos 4 5` has the same
670 // effect as just passing `-platform_version macos 4 5`.
671 // FIXME: ld64 warns on multiple flags for one platform. Should we?
672 platformVersions[version.platform] = version;
673 lastVersionInfo = &platformVersions[version.platform];
676 if (platformVersions.empty()) {
677 error("must specify -platform_version");
678 return PLATFORM_UNKNOWN;
680 if (platformVersions.size() > 2) {
681 error("must specify -platform_version at most twice");
682 return PLATFORM_UNKNOWN;
684 if (platformVersions.size() == 2) {
685 bool isZipperedCatalyst = platformVersions.count(PLATFORM_MACOS) &&
686 platformVersions.count(PLATFORM_MACCATALYST);
688 if (!isZipperedCatalyst) {
689 error("lld supports writing zippered outputs only for "
690 "macos and mac-catalyst");
691 } else if (config->outputType != MH_DYLIB &&
692 config->outputType != MH_BUNDLE) {
693 error("writing zippered outputs only valid for -dylib and -bundle");
694 } else {
695 config->platformInfo.minimum = platformVersions[PLATFORM_MACOS].minimum;
696 config->platformInfo.sdk = platformVersions[PLATFORM_MACOS].sdk;
697 config->secondaryPlatformInfo = PlatformInfo{};
698 config->secondaryPlatformInfo->minimum =
699 platformVersions[PLATFORM_MACCATALYST].minimum;
700 config->secondaryPlatformInfo->sdk =
701 platformVersions[PLATFORM_MACCATALYST].sdk;
703 return PLATFORM_MACOS;
706 config->platformInfo.minimum = lastVersionInfo->minimum;
707 config->platformInfo.sdk = lastVersionInfo->sdk;
708 return lastVersionInfo->platform;
711 // Has the side-effect of setting Config::target.
712 static TargetInfo *createTargetInfo(InputArgList &args) {
713 StringRef archName = args.getLastArgValue(OPT_arch);
714 if (archName.empty()) {
715 error("must specify -arch");
716 return nullptr;
719 PlatformType platform = parsePlatformVersions(args);
720 config->platformInfo.target =
721 MachO::Target(getArchitectureFromName(archName), platform);
722 if (config->secondaryPlatformInfo) {
723 config->secondaryPlatformInfo->target =
724 MachO::Target(getArchitectureFromName(archName), PLATFORM_MACCATALYST);
727 uint32_t cpuType;
728 uint32_t cpuSubtype;
729 std::tie(cpuType, cpuSubtype) = getCPUTypeFromArchitecture(config->arch());
731 switch (cpuType) {
732 case CPU_TYPE_X86_64:
733 return createX86_64TargetInfo();
734 case CPU_TYPE_ARM64:
735 return createARM64TargetInfo();
736 case CPU_TYPE_ARM64_32:
737 return createARM64_32TargetInfo();
738 case CPU_TYPE_ARM:
739 return createARMTargetInfo(cpuSubtype);
740 default:
741 error("missing or unsupported -arch " + archName);
742 return nullptr;
746 static UndefinedSymbolTreatment
747 getUndefinedSymbolTreatment(const ArgList &args) {
748 StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
749 auto treatment =
750 StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
751 .Cases("error", "", UndefinedSymbolTreatment::error)
752 .Case("warning", UndefinedSymbolTreatment::warning)
753 .Case("suppress", UndefinedSymbolTreatment::suppress)
754 .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
755 .Default(UndefinedSymbolTreatment::unknown);
756 if (treatment == UndefinedSymbolTreatment::unknown) {
757 warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
758 "', defaulting to 'error'");
759 treatment = UndefinedSymbolTreatment::error;
760 } else if (config->namespaceKind == NamespaceKind::twolevel &&
761 (treatment == UndefinedSymbolTreatment::warning ||
762 treatment == UndefinedSymbolTreatment::suppress)) {
763 if (treatment == UndefinedSymbolTreatment::warning)
764 error("'-undefined warning' only valid with '-flat_namespace'");
765 else
766 error("'-undefined suppress' only valid with '-flat_namespace'");
767 treatment = UndefinedSymbolTreatment::error;
769 return treatment;
772 static ICFLevel getICFLevel(const ArgList &args) {
773 StringRef icfLevelStr = args.getLastArgValue(OPT_icf_eq);
774 auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr)
775 .Cases("none", "", ICFLevel::none)
776 .Case("safe", ICFLevel::safe)
777 .Case("all", ICFLevel::all)
778 .Default(ICFLevel::unknown);
779 if (icfLevel == ICFLevel::unknown) {
780 warn(Twine("unknown --icf=OPTION `") + icfLevelStr +
781 "', defaulting to `none'");
782 icfLevel = ICFLevel::none;
784 return icfLevel;
787 static void warnIfDeprecatedOption(const Option &opt) {
788 if (!opt.getGroup().isValid())
789 return;
790 if (opt.getGroup().getID() == OPT_grp_deprecated) {
791 warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
792 warn(opt.getHelpText());
796 static void warnIfUnimplementedOption(const Option &opt) {
797 if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
798 return;
799 switch (opt.getGroup().getID()) {
800 case OPT_grp_deprecated:
801 // warn about deprecated options elsewhere
802 break;
803 case OPT_grp_undocumented:
804 warn("Option `" + opt.getPrefixedName() +
805 "' is undocumented. Should lld implement it?");
806 break;
807 case OPT_grp_obsolete:
808 warn("Option `" + opt.getPrefixedName() +
809 "' is obsolete. Please modernize your usage.");
810 break;
811 case OPT_grp_ignored:
812 warn("Option `" + opt.getPrefixedName() + "' is ignored.");
813 break;
814 case OPT_grp_ignored_silently:
815 break;
816 default:
817 warn("Option `" + opt.getPrefixedName() +
818 "' is not yet implemented. Stay tuned...");
819 break;
823 static const char *getReproduceOption(InputArgList &args) {
824 if (const Arg *arg = args.getLastArg(OPT_reproduce))
825 return arg->getValue();
826 return getenv("LLD_REPRODUCE");
829 static void parseClangOption(StringRef opt, const Twine &msg) {
830 std::string err;
831 raw_string_ostream os(err);
833 const char *argv[] = {"lld", opt.data()};
834 if (cl::ParseCommandLineOptions(2, argv, "", &os))
835 return;
836 os.flush();
837 error(msg + ": " + StringRef(err).trim());
840 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
841 const Arg *arg = args.getLastArg(id);
842 if (!arg)
843 return 0;
845 if (config->outputType != MH_DYLIB) {
846 error(arg->getAsString(args) + ": only valid with -dylib");
847 return 0;
850 PackedVersion version;
851 if (!version.parse32(arg->getValue())) {
852 error(arg->getAsString(args) + ": malformed version");
853 return 0;
856 return version.rawValue();
859 static uint32_t parseProtection(StringRef protStr) {
860 uint32_t prot = 0;
861 for (char c : protStr) {
862 switch (c) {
863 case 'r':
864 prot |= VM_PROT_READ;
865 break;
866 case 'w':
867 prot |= VM_PROT_WRITE;
868 break;
869 case 'x':
870 prot |= VM_PROT_EXECUTE;
871 break;
872 case '-':
873 break;
874 default:
875 error("unknown -segprot letter '" + Twine(c) + "' in " + protStr);
876 return 0;
879 return prot;
882 static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) {
883 std::vector<SectionAlign> sectAligns;
884 for (const Arg *arg : args.filtered(OPT_sectalign)) {
885 StringRef segName = arg->getValue(0);
886 StringRef sectName = arg->getValue(1);
887 StringRef alignStr = arg->getValue(2);
888 if (alignStr.startswith("0x") || alignStr.startswith("0X"))
889 alignStr = alignStr.drop_front(2);
890 uint32_t align;
891 if (alignStr.getAsInteger(16, align)) {
892 error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) +
893 "' as number");
894 continue;
896 if (!isPowerOf2_32(align)) {
897 error("-sectalign: '" + StringRef(arg->getValue(2)) +
898 "' (in base 16) not a power of two");
899 continue;
901 sectAligns.push_back({segName, sectName, align});
903 return sectAligns;
906 PlatformType macho::removeSimulator(PlatformType platform) {
907 switch (platform) {
908 case PLATFORM_IOSSIMULATOR:
909 return PLATFORM_IOS;
910 case PLATFORM_TVOSSIMULATOR:
911 return PLATFORM_TVOS;
912 case PLATFORM_WATCHOSSIMULATOR:
913 return PLATFORM_WATCHOS;
914 default:
915 return platform;
919 static bool dataConstDefault(const InputArgList &args) {
920 static const std::vector<std::pair<PlatformType, VersionTuple>> minVersion = {
921 {PLATFORM_MACOS, VersionTuple(10, 15)},
922 {PLATFORM_IOS, VersionTuple(13, 0)},
923 {PLATFORM_TVOS, VersionTuple(13, 0)},
924 {PLATFORM_WATCHOS, VersionTuple(6, 0)},
925 {PLATFORM_BRIDGEOS, VersionTuple(4, 0)}};
926 PlatformType platform = removeSimulator(config->platformInfo.target.Platform);
927 auto it = llvm::find_if(minVersion,
928 [&](const auto &p) { return p.first == platform; });
929 if (it != minVersion.end())
930 if (config->platformInfo.minimum < it->second)
931 return false;
933 switch (config->outputType) {
934 case MH_EXECUTE:
935 return !args.hasArg(OPT_no_pie);
936 case MH_BUNDLE:
937 // FIXME: return false when -final_name ...
938 // has prefix "/System/Library/UserEventPlugins/"
939 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd"
940 return true;
941 case MH_DYLIB:
942 return true;
943 case MH_OBJECT:
944 return false;
945 default:
946 llvm_unreachable(
947 "unsupported output type for determining data-const default");
949 return false;
952 void SymbolPatterns::clear() {
953 literals.clear();
954 globs.clear();
957 void SymbolPatterns::insert(StringRef symbolName) {
958 if (symbolName.find_first_of("*?[]") == StringRef::npos)
959 literals.insert(CachedHashStringRef(symbolName));
960 else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName))
961 globs.emplace_back(*pattern);
962 else
963 error("invalid symbol-name pattern: " + symbolName);
966 bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
967 return literals.contains(CachedHashStringRef(symbolName));
970 bool SymbolPatterns::matchGlob(StringRef symbolName) const {
971 for (const GlobPattern &glob : globs)
972 if (glob.match(symbolName))
973 return true;
974 return false;
977 bool SymbolPatterns::match(StringRef symbolName) const {
978 return matchLiteral(symbolName) || matchGlob(symbolName);
981 static void parseSymbolPatternsFile(const Arg *arg,
982 SymbolPatterns &symbolPatterns) {
983 StringRef path = arg->getValue();
984 Optional<MemoryBufferRef> buffer = readFile(path);
985 if (!buffer) {
986 error("Could not read symbol file: " + path);
987 return;
989 MemoryBufferRef mbref = *buffer;
990 for (StringRef line : args::getLines(mbref)) {
991 line = line.take_until([](char c) { return c == '#'; }).trim();
992 if (!line.empty())
993 symbolPatterns.insert(line);
997 static void handleSymbolPatterns(InputArgList &args,
998 SymbolPatterns &symbolPatterns,
999 unsigned singleOptionCode,
1000 unsigned listFileOptionCode) {
1001 for (const Arg *arg : args.filtered(singleOptionCode))
1002 symbolPatterns.insert(arg->getValue());
1003 for (const Arg *arg : args.filtered(listFileOptionCode))
1004 parseSymbolPatternsFile(arg, symbolPatterns);
1007 static void createFiles(const InputArgList &args) {
1008 TimeTraceScope timeScope("Load input files");
1009 // This loop should be reserved for options whose exact ordering matters.
1010 // Other options should be handled via filtered() and/or getLastArg().
1011 bool isLazy = false;
1012 for (const Arg *arg : args) {
1013 const Option &opt = arg->getOption();
1014 warnIfDeprecatedOption(opt);
1015 warnIfUnimplementedOption(opt);
1017 switch (opt.getID()) {
1018 case OPT_INPUT:
1019 addFile(rerootPath(arg->getValue()), LoadType::CommandLine, isLazy);
1020 break;
1021 case OPT_needed_library:
1022 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1023 addFile(rerootPath(arg->getValue()), LoadType::CommandLine)))
1024 dylibFile->forceNeeded = true;
1025 break;
1026 case OPT_reexport_library:
1027 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1028 addFile(rerootPath(arg->getValue()), LoadType::CommandLine))) {
1029 config->hasReexports = true;
1030 dylibFile->reexport = true;
1032 break;
1033 case OPT_weak_library:
1034 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1035 addFile(rerootPath(arg->getValue()), LoadType::CommandLine)))
1036 dylibFile->forceWeakImport = true;
1037 break;
1038 case OPT_filelist:
1039 addFileList(arg->getValue(), isLazy);
1040 break;
1041 case OPT_force_load:
1042 addFile(rerootPath(arg->getValue()), LoadType::CommandLineForce);
1043 break;
1044 case OPT_load_hidden:
1045 addFile(rerootPath(arg->getValue()), LoadType::CommandLine,
1046 /*isLazy=*/false, /*isExplicit=*/true, /*isBundleLoader=*/false,
1047 /*isForceHidden=*/true);
1048 break;
1049 case OPT_l:
1050 case OPT_needed_l:
1051 case OPT_reexport_l:
1052 case OPT_weak_l:
1053 case OPT_hidden_l:
1054 addLibrary(arg->getValue(), opt.getID() == OPT_needed_l,
1055 opt.getID() == OPT_weak_l, opt.getID() == OPT_reexport_l,
1056 opt.getID() == OPT_hidden_l,
1057 /*isExplicit=*/true, LoadType::CommandLine);
1058 break;
1059 case OPT_framework:
1060 case OPT_needed_framework:
1061 case OPT_reexport_framework:
1062 case OPT_weak_framework:
1063 addFramework(arg->getValue(), opt.getID() == OPT_needed_framework,
1064 opt.getID() == OPT_weak_framework,
1065 opt.getID() == OPT_reexport_framework, /*isExplicit=*/true,
1066 LoadType::CommandLine);
1067 break;
1068 case OPT_start_lib:
1069 if (isLazy)
1070 error("nested --start-lib");
1071 isLazy = true;
1072 break;
1073 case OPT_end_lib:
1074 if (!isLazy)
1075 error("stray --end-lib");
1076 isLazy = false;
1077 break;
1078 default:
1079 break;
1084 static void gatherInputSections() {
1085 TimeTraceScope timeScope("Gathering input sections");
1086 int inputOrder = 0;
1087 for (const InputFile *file : inputFiles) {
1088 for (const Section *section : file->sections) {
1089 // Compact unwind entries require special handling elsewhere. (In
1090 // contrast, EH frames are handled like regular ConcatInputSections.)
1091 if (section->name == section_names::compactUnwind)
1092 continue;
1093 ConcatOutputSection *osec = nullptr;
1094 for (const Subsection &subsection : section->subsections) {
1095 if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) {
1096 if (isec->isCoalescedWeak())
1097 continue;
1098 isec->outSecOff = inputOrder++;
1099 if (!osec)
1100 osec = ConcatOutputSection::getOrCreateForInput(isec);
1101 isec->parent = osec;
1102 inputSections.push_back(isec);
1103 } else if (auto *isec =
1104 dyn_cast<CStringInputSection>(subsection.isec)) {
1105 if (in.cStringSection->inputOrder == UnspecifiedInputOrder)
1106 in.cStringSection->inputOrder = inputOrder++;
1107 in.cStringSection->addInput(isec);
1108 } else if (auto *isec =
1109 dyn_cast<WordLiteralInputSection>(subsection.isec)) {
1110 if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder)
1111 in.wordLiteralSection->inputOrder = inputOrder++;
1112 in.wordLiteralSection->addInput(isec);
1113 } else {
1114 llvm_unreachable("unexpected input section kind");
1118 if (!file->objCImageInfo.empty())
1119 in.objCImageInfo->addFile(file);
1121 assert(inputOrder <= UnspecifiedInputOrder);
1124 static void foldIdenticalLiterals() {
1125 TimeTraceScope timeScope("Fold identical literals");
1126 // We always create a cStringSection, regardless of whether dedupLiterals is
1127 // true. If it isn't, we simply create a non-deduplicating CStringSection.
1128 // Either way, we must unconditionally finalize it here.
1129 in.cStringSection->finalizeContents();
1130 if (in.wordLiteralSection)
1131 in.wordLiteralSection->finalizeContents();
1134 static void referenceStubBinder() {
1135 bool needsStubHelper = config->outputType == MH_DYLIB ||
1136 config->outputType == MH_EXECUTE ||
1137 config->outputType == MH_BUNDLE;
1138 if (!needsStubHelper || !symtab->find("dyld_stub_binder"))
1139 return;
1141 // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here
1142 // adds a opportunistic reference to dyld_stub_binder if it happens to exist.
1143 // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This
1144 // isn't needed for correctness, but the presence of that symbol suppresses
1145 // "no symbols" diagnostics from `nm`.
1146 // StubHelperSection::setup() adds a reference and errors out if
1147 // dyld_stub_binder doesn't exist in case it is actually needed.
1148 symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/false);
1151 static void createAliases() {
1152 for (const auto &pair : config->aliasedSymbols) {
1153 if (const auto &sym = symtab->find(pair.first)) {
1154 if (const auto &defined = dyn_cast<Defined>(sym)) {
1155 symtab->aliasDefined(defined, pair.second);
1156 continue;
1160 warn("undefined base symbol '" + pair.first + "' for alias '" +
1161 pair.second + "'\n");
1165 static void handleExplicitExports() {
1166 if (config->hasExplicitExports) {
1167 parallelForEach(symtab->getSymbols(), [](Symbol *sym) {
1168 if (auto *defined = dyn_cast<Defined>(sym)) {
1169 StringRef symbolName = defined->getName();
1170 if (config->exportedSymbols.match(symbolName)) {
1171 if (defined->privateExtern) {
1172 if (defined->weakDefCanBeHidden) {
1173 // weak_def_can_be_hidden symbols behave similarly to
1174 // private_extern symbols in most cases, except for when
1175 // it is explicitly exported.
1176 // The former can be exported but the latter cannot.
1177 defined->privateExtern = false;
1178 } else {
1179 warn("cannot export hidden symbol " + toString(*defined) +
1180 "\n>>> defined in " + toString(defined->getFile()));
1183 } else {
1184 defined->privateExtern = true;
1188 } else if (!config->unexportedSymbols.empty()) {
1189 parallelForEach(symtab->getSymbols(), [](Symbol *sym) {
1190 if (auto *defined = dyn_cast<Defined>(sym))
1191 if (config->unexportedSymbols.match(defined->getName()))
1192 defined->privateExtern = true;
1197 bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
1198 llvm::raw_ostream &stderrOS, bool exitEarly,
1199 bool disableOutput) {
1200 // This driver-specific context will be freed later by lldMain().
1201 auto *ctx = new CommonLinkerContext;
1203 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
1204 ctx->e.cleanupCallback = []() {
1205 resolvedFrameworks.clear();
1206 resolvedLibraries.clear();
1207 cachedReads.clear();
1208 concatOutputSections.clear();
1209 inputFiles.clear();
1210 inputSections.clear();
1211 loadedArchives.clear();
1212 loadedObjectFrameworks.clear();
1213 syntheticSections.clear();
1214 thunkMap.clear();
1216 firstTLVDataSection = nullptr;
1217 tar = nullptr;
1218 memset(&in, 0, sizeof(in));
1220 resetLoadedDylibs();
1221 resetOutputSegments();
1222 resetWriter();
1223 InputFile::resetIdCount();
1226 ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]);
1228 MachOOptTable parser;
1229 InputArgList args = parser.parse(argsArr.slice(1));
1231 ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now "
1232 "(use --error-limit=0 to see all errors)";
1233 ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
1234 ctx->e.verbose = args.hasArg(OPT_verbose);
1236 if (args.hasArg(OPT_help_hidden)) {
1237 parser.printHelp(argsArr[0], /*showHidden=*/true);
1238 return true;
1240 if (args.hasArg(OPT_help)) {
1241 parser.printHelp(argsArr[0], /*showHidden=*/false);
1242 return true;
1244 if (args.hasArg(OPT_version)) {
1245 message(getLLDVersion());
1246 return true;
1249 config = std::make_unique<Configuration>();
1250 symtab = std::make_unique<SymbolTable>();
1251 config->outputType = getOutputType(args);
1252 target = createTargetInfo(args);
1253 depTracker = std::make_unique<DependencyTracker>(
1254 args.getLastArgValue(OPT_dependency_info));
1255 if (errorCount())
1256 return false;
1258 if (args.hasArg(OPT_pagezero_size)) {
1259 uint64_t pagezeroSize = args::getHex(args, OPT_pagezero_size, 0);
1261 // ld64 does something really weird. It attempts to realign the value to the
1262 // page size, but assumes the the page size is 4K. This doesn't work with
1263 // most of Apple's ARM64 devices, which use a page size of 16K. This means
1264 // that it will first 4K align it by rounding down, then round up to 16K.
1265 // This probably only happened because no one using this arg with anything
1266 // other then 0, so no one checked if it did what is what it says it does.
1268 // So we are not copying this weird behavior and doing the it in a logical
1269 // way, by always rounding down to page size.
1270 if (!isAligned(Align(target->getPageSize()), pagezeroSize)) {
1271 pagezeroSize -= pagezeroSize % target->getPageSize();
1272 warn("__PAGEZERO size is not page aligned, rounding down to 0x" +
1273 Twine::utohexstr(pagezeroSize));
1276 target->pageZeroSize = pagezeroSize;
1279 config->osoPrefix = args.getLastArgValue(OPT_oso_prefix);
1280 if (!config->osoPrefix.empty()) {
1281 // Expand special characters, such as ".", "..", or "~", if present.
1282 // Note: LD64 only expands "." and not other special characters.
1283 // That seems silly to imitate so we will not try to follow it, but rather
1284 // just use real_path() to do it.
1286 // The max path length is 4096, in theory. However that seems quite long
1287 // and seems unlikely that any one would want to strip everything from the
1288 // path. Hence we've picked a reasonably large number here.
1289 SmallString<1024> expanded;
1290 if (!fs::real_path(config->osoPrefix, expanded,
1291 /*expand_tilde=*/true)) {
1292 // Note: LD64 expands "." to be `<current_dir>/`
1293 // (ie., it has a slash suffix) whereas real_path() doesn't.
1294 // So we have to append '/' to be consistent.
1295 StringRef sep = sys::path::get_separator();
1296 // real_path removes trailing slashes as part of the normalization, but
1297 // these are meaningful for our text based stripping
1298 if (config->osoPrefix.equals(".") || config->osoPrefix.endswith(sep))
1299 expanded += sep;
1300 config->osoPrefix = saver().save(expanded.str());
1304 // Must be set before any InputSections and Symbols are created.
1305 config->deadStrip = args.hasArg(OPT_dead_strip);
1307 config->systemLibraryRoots = getSystemLibraryRoots(args);
1308 if (const char *path = getReproduceOption(args)) {
1309 // Note that --reproduce is a debug option so you can ignore it
1310 // if you are trying to understand the whole picture of the code.
1311 Expected<std::unique_ptr<TarWriter>> errOrWriter =
1312 TarWriter::create(path, path::stem(path));
1313 if (errOrWriter) {
1314 tar = std::move(*errOrWriter);
1315 tar->append("response.txt", createResponseFile(args));
1316 tar->append("version.txt", getLLDVersion() + "\n");
1317 } else {
1318 error("--reproduce: " + toString(errOrWriter.takeError()));
1322 if (auto *arg = args.getLastArg(OPT_threads_eq)) {
1323 StringRef v(arg->getValue());
1324 unsigned threads = 0;
1325 if (!llvm::to_integer(v, threads, 0) || threads == 0)
1326 error(arg->getSpelling() + ": expected a positive integer, but got '" +
1327 arg->getValue() + "'");
1328 parallel::strategy = hardware_concurrency(threads);
1329 config->thinLTOJobs = v;
1331 if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
1332 config->thinLTOJobs = arg->getValue();
1333 if (!get_threadpool_strategy(config->thinLTOJobs))
1334 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
1336 for (const Arg *arg : args.filtered(OPT_u)) {
1337 config->explicitUndefineds.push_back(symtab->addUndefined(
1338 arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
1341 for (const Arg *arg : args.filtered(OPT_U))
1342 config->explicitDynamicLookups.insert(arg->getValue());
1344 config->mapFile = args.getLastArgValue(OPT_map);
1345 config->optimize = args::getInteger(args, OPT_O, 1);
1346 config->outputFile = args.getLastArgValue(OPT_o, "a.out");
1347 config->finalOutput =
1348 args.getLastArgValue(OPT_final_output, config->outputFile);
1349 config->astPaths = args.getAllArgValues(OPT_add_ast_path);
1350 config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
1351 config->headerPadMaxInstallNames =
1352 args.hasArg(OPT_headerpad_max_install_names);
1353 config->printDylibSearch =
1354 args.hasArg(OPT_print_dylib_search) || getenv("RC_TRACE_DYLIB_SEARCHING");
1355 config->printEachFile = args.hasArg(OPT_t);
1356 config->printWhyLoad = args.hasArg(OPT_why_load);
1357 config->omitDebugInfo = args.hasArg(OPT_S);
1358 config->errorForArchMismatch = args.hasArg(OPT_arch_errors_fatal);
1359 if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
1360 if (config->outputType != MH_BUNDLE)
1361 error("-bundle_loader can only be used with MachO bundle output");
1362 addFile(arg->getValue(), LoadType::CommandLine, /*isLazy=*/false,
1363 /*isExplicit=*/false, /*isBundleLoader=*/true);
1365 if (const Arg *arg = args.getLastArg(OPT_umbrella)) {
1366 if (config->outputType != MH_DYLIB)
1367 warn("-umbrella used, but not creating dylib");
1368 config->umbrella = arg->getValue();
1370 config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
1371 config->ltoo = args::getInteger(args, OPT_lto_O, 2);
1372 if (config->ltoo > 3)
1373 error("--lto-O: invalid optimization level: " + Twine(config->ltoo));
1374 config->thinLTOCacheDir = args.getLastArgValue(OPT_cache_path_lto);
1375 config->thinLTOCachePolicy = getLTOCachePolicy(args);
1376 config->runtimePaths = args::getStrings(args, OPT_rpath);
1377 config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false);
1378 config->archMultiple = args.hasArg(OPT_arch_multiple);
1379 config->applicationExtension = args.hasFlag(
1380 OPT_application_extension, OPT_no_application_extension, false);
1381 config->exportDynamic = args.hasArg(OPT_export_dynamic);
1382 config->forceLoadObjC = args.hasArg(OPT_ObjC);
1383 config->forceLoadSwift = args.hasArg(OPT_force_load_swift_libs);
1384 config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs);
1385 config->demangle = args.hasArg(OPT_demangle);
1386 config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
1387 config->emitFunctionStarts =
1388 args.hasFlag(OPT_function_starts, OPT_no_function_starts, true);
1389 config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle);
1390 config->emitDataInCodeInfo =
1391 args.hasFlag(OPT_data_in_code_info, OPT_no_data_in_code_info, true);
1392 config->icfLevel = getICFLevel(args);
1393 config->dedupLiterals =
1394 args.hasFlag(OPT_deduplicate_literals, OPT_icf_eq, false) ||
1395 config->icfLevel != ICFLevel::none;
1396 config->warnDylibInstallName = args.hasFlag(
1397 OPT_warn_dylib_install_name, OPT_no_warn_dylib_install_name, false);
1398 config->ignoreOptimizationHints = args.hasArg(OPT_ignore_optimization_hints);
1399 config->callGraphProfileSort = args.hasFlag(
1400 OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
1401 config->printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order);
1402 config->forceExactCpuSubtypeMatch =
1403 getenv("LD_DYLIB_CPU_SUBTYPES_MUST_MATCH");
1405 for (const Arg *arg : args.filtered(OPT_alias)) {
1406 config->aliasedSymbols.push_back(
1407 std::make_pair(arg->getValue(0), arg->getValue(1)));
1410 // FIXME: Add a commandline flag for this too.
1411 config->zeroModTime = getenv("ZERO_AR_DATE");
1413 std::array<PlatformType, 3> encryptablePlatforms{
1414 PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS};
1415 config->emitEncryptionInfo =
1416 args.hasFlag(OPT_encryptable, OPT_no_encryption,
1417 is_contained(encryptablePlatforms, config->platform()));
1419 #ifndef LLVM_HAVE_LIBXAR
1420 if (config->emitBitcodeBundle)
1421 error("-bitcode_bundle unsupported because LLD wasn't built with libxar");
1422 #endif
1424 if (const Arg *arg = args.getLastArg(OPT_install_name)) {
1425 if (config->warnDylibInstallName && config->outputType != MH_DYLIB)
1426 warn(
1427 arg->getAsString(args) +
1428 ": ignored, only has effect with -dylib [--warn-dylib-install-name]");
1429 else
1430 config->installName = arg->getValue();
1431 } else if (config->outputType == MH_DYLIB) {
1432 config->installName = config->finalOutput;
1435 if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
1436 if (config->outputType != MH_DYLIB)
1437 warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
1438 else
1439 config->markDeadStrippableDylib = true;
1442 if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
1443 config->staticLink = (arg->getOption().getID() == OPT_static);
1445 if (const Arg *arg =
1446 args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
1447 config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
1448 ? NamespaceKind::twolevel
1449 : NamespaceKind::flat;
1451 config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
1453 if (config->outputType == MH_EXECUTE)
1454 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
1455 /*file=*/nullptr,
1456 /*isWeakRef=*/false);
1458 config->librarySearchPaths =
1459 getLibrarySearchPaths(args, config->systemLibraryRoots);
1460 config->frameworkSearchPaths =
1461 getFrameworkSearchPaths(args, config->systemLibraryRoots);
1462 if (const Arg *arg =
1463 args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
1464 config->searchDylibsFirst =
1465 arg->getOption().getID() == OPT_search_dylibs_first;
1467 config->dylibCompatibilityVersion =
1468 parseDylibVersion(args, OPT_compatibility_version);
1469 config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1471 config->dataConst =
1472 args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args));
1473 // Populate config->sectionRenameMap with builtin default renames.
1474 // Options -rename_section and -rename_segment are able to override.
1475 initializeSectionRenameMap();
1476 // Reject every special character except '.' and '$'
1477 // TODO(gkm): verify that this is the proper set of invalid chars
1478 StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1479 auto validName = [invalidNameChars](StringRef s) {
1480 if (s.find_first_of(invalidNameChars) != StringRef::npos)
1481 error("invalid name for segment or section: " + s);
1482 return s;
1484 for (const Arg *arg : args.filtered(OPT_rename_section)) {
1485 config->sectionRenameMap[{validName(arg->getValue(0)),
1486 validName(arg->getValue(1))}] = {
1487 validName(arg->getValue(2)), validName(arg->getValue(3))};
1489 for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1490 config->segmentRenameMap[validName(arg->getValue(0))] =
1491 validName(arg->getValue(1));
1494 config->sectionAlignments = parseSectAlign(args);
1496 for (const Arg *arg : args.filtered(OPT_segprot)) {
1497 StringRef segName = arg->getValue(0);
1498 uint32_t maxProt = parseProtection(arg->getValue(1));
1499 uint32_t initProt = parseProtection(arg->getValue(2));
1500 if (maxProt != initProt && config->arch() != AK_i386)
1501 error("invalid argument '" + arg->getAsString(args) +
1502 "': max and init must be the same for non-i386 archs");
1503 if (segName == segment_names::linkEdit)
1504 error("-segprot cannot be used to change __LINKEDIT's protections");
1505 config->segmentProtections.push_back({segName, maxProt, initProt});
1508 config->hasExplicitExports =
1509 args.hasArg(OPT_no_exported_symbols) ||
1510 args.hasArgNoClaim(OPT_exported_symbol, OPT_exported_symbols_list);
1511 handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
1512 OPT_exported_symbols_list);
1513 handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
1514 OPT_unexported_symbols_list);
1515 if (config->hasExplicitExports && !config->unexportedSymbols.empty())
1516 error("cannot use both -exported_symbol* and -unexported_symbol* options");
1518 if (args.hasArg(OPT_no_exported_symbols) && !config->exportedSymbols.empty())
1519 error("cannot use both -exported_symbol* and -no_exported_symbols options");
1521 // Imitating LD64's:
1522 // -non_global_symbols_no_strip_list and -non_global_symbols_strip_list can't
1523 // both be present.
1524 // But -x can be used with either of these two, in which case, the last arg
1525 // takes effect.
1526 // (TODO: This is kind of confusing - considering disallowing using them
1527 // together for a more straightforward behaviour)
1529 bool includeLocal = false;
1530 bool excludeLocal = false;
1531 for (const Arg *arg :
1532 args.filtered(OPT_x, OPT_non_global_symbols_no_strip_list,
1533 OPT_non_global_symbols_strip_list)) {
1534 switch (arg->getOption().getID()) {
1535 case OPT_x:
1536 config->localSymbolsPresence = SymtabPresence::None;
1537 break;
1538 case OPT_non_global_symbols_no_strip_list:
1539 if (excludeLocal) {
1540 error("cannot use both -non_global_symbols_no_strip_list and "
1541 "-non_global_symbols_strip_list");
1542 } else {
1543 includeLocal = true;
1544 config->localSymbolsPresence = SymtabPresence::SelectivelyIncluded;
1545 parseSymbolPatternsFile(arg, config->localSymbolPatterns);
1547 break;
1548 case OPT_non_global_symbols_strip_list:
1549 if (includeLocal) {
1550 error("cannot use both -non_global_symbols_no_strip_list and "
1551 "-non_global_symbols_strip_list");
1552 } else {
1553 excludeLocal = true;
1554 config->localSymbolsPresence = SymtabPresence::SelectivelyExcluded;
1555 parseSymbolPatternsFile(arg, config->localSymbolPatterns);
1557 break;
1558 default:
1559 llvm_unreachable("unexpected option");
1563 // Explicitly-exported literal symbols must be defined, but might
1564 // languish in an archive if unreferenced elsewhere or if they are in the
1565 // non-global strip list. Light a fire under those lazy symbols!
1566 for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
1567 symtab->addUndefined(cachedName.val(), /*file=*/nullptr,
1568 /*isWeakRef=*/false);
1570 for (const Arg *arg : args.filtered(OPT_why_live))
1571 config->whyLive.insert(arg->getValue());
1572 if (!config->whyLive.empty() && !config->deadStrip)
1573 warn("-why_live has no effect without -dead_strip, ignoring");
1575 config->saveTemps = args.hasArg(OPT_save_temps);
1577 config->adhocCodesign = args.hasFlag(
1578 OPT_adhoc_codesign, OPT_no_adhoc_codesign,
1579 (config->arch() == AK_arm64 || config->arch() == AK_arm64e) &&
1580 config->platform() == PLATFORM_MACOS);
1582 if (args.hasArg(OPT_v)) {
1583 message(getLLDVersion(), lld::errs());
1584 message(StringRef("Library search paths:") +
1585 (config->librarySearchPaths.empty()
1586 ? ""
1587 : "\n\t" + join(config->librarySearchPaths, "\n\t")),
1588 lld::errs());
1589 message(StringRef("Framework search paths:") +
1590 (config->frameworkSearchPaths.empty()
1591 ? ""
1592 : "\n\t" + join(config->frameworkSearchPaths, "\n\t")),
1593 lld::errs());
1596 config->progName = argsArr[0];
1598 config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
1599 config->timeTraceGranularity =
1600 args::getInteger(args, OPT_time_trace_granularity_eq, 500);
1602 // Initialize time trace profiler.
1603 if (config->timeTraceEnabled)
1604 timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
1607 TimeTraceScope timeScope("ExecuteLinker");
1609 initLLVM(); // must be run before any call to addFile()
1610 createFiles(args);
1612 config->isPic = config->outputType == MH_DYLIB ||
1613 config->outputType == MH_BUNDLE ||
1614 (config->outputType == MH_EXECUTE &&
1615 args.hasFlag(OPT_pie, OPT_no_pie, true));
1617 // Now that all dylibs have been loaded, search for those that should be
1618 // re-exported.
1620 auto reexportHandler = [](const Arg *arg,
1621 const std::vector<StringRef> &extensions) {
1622 config->hasReexports = true;
1623 StringRef searchName = arg->getValue();
1624 if (!markReexport(searchName, extensions))
1625 error(arg->getSpelling() + " " + searchName +
1626 " does not match a supplied dylib");
1628 std::vector<StringRef> extensions = {".tbd"};
1629 for (const Arg *arg : args.filtered(OPT_sub_umbrella))
1630 reexportHandler(arg, extensions);
1632 extensions.push_back(".dylib");
1633 for (const Arg *arg : args.filtered(OPT_sub_library))
1634 reexportHandler(arg, extensions);
1637 cl::ResetAllOptionOccurrences();
1639 // Parse LTO options.
1640 if (const Arg *arg = args.getLastArg(OPT_mcpu))
1641 parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())),
1642 arg->getSpelling());
1644 for (const Arg *arg : args.filtered(OPT_mllvm))
1645 parseClangOption(arg->getValue(), arg->getSpelling());
1647 createSyntheticSections();
1648 createSyntheticSymbols();
1650 createAliases();
1651 // If we are in "explicit exports" mode, hide everything that isn't
1652 // explicitly exported. Do this before running LTO so that LTO can better
1653 // optimize.
1654 handleExplicitExports();
1655 // LTO may emit a non-hidden (extern) object file symbol even if the
1656 // corresponding bitcode symbol is hidden. In particular, this happens for
1657 // cross-module references to hidden symbols under ThinLTO. Thus, if we
1658 // compiled any bitcode files, we must redo the symbol hiding.
1659 if (compileBitcodeFiles())
1660 handleExplicitExports();
1661 replaceCommonSymbols();
1663 StringRef orderFile = args.getLastArgValue(OPT_order_file);
1664 if (!orderFile.empty())
1665 priorityBuilder.parseOrderFile(orderFile);
1667 referenceStubBinder();
1669 // FIXME: should terminate the link early based on errors encountered so
1670 // far?
1672 for (const Arg *arg : args.filtered(OPT_sectcreate)) {
1673 StringRef segName = arg->getValue(0);
1674 StringRef sectName = arg->getValue(1);
1675 StringRef fileName = arg->getValue(2);
1676 Optional<MemoryBufferRef> buffer = readFile(fileName);
1677 if (buffer)
1678 inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
1681 for (const Arg *arg : args.filtered(OPT_add_empty_section)) {
1682 StringRef segName = arg->getValue(0);
1683 StringRef sectName = arg->getValue(1);
1684 inputFiles.insert(make<OpaqueFile>(MemoryBufferRef(), segName, sectName));
1687 gatherInputSections();
1688 if (config->callGraphProfileSort)
1689 priorityBuilder.extractCallGraphProfile();
1691 if (config->deadStrip)
1692 markLive();
1694 // ICF assumes that all literals have been folded already, so we must run
1695 // foldIdenticalLiterals before foldIdenticalSections.
1696 foldIdenticalLiterals();
1697 if (config->icfLevel != ICFLevel::none) {
1698 if (config->icfLevel == ICFLevel::safe)
1699 markAddrSigSymbols();
1700 foldIdenticalSections(/*onlyCfStrings=*/false);
1701 } else if (config->dedupLiterals) {
1702 foldIdenticalSections(/*onlyCfStrings=*/true);
1705 // Write to an output file.
1706 if (target->wordSize == 8)
1707 writeResult<LP64>();
1708 else
1709 writeResult<ILP32>();
1711 depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
1714 if (config->timeTraceEnabled) {
1715 checkError(timeTraceProfilerWrite(
1716 args.getLastArgValue(OPT_time_trace_eq).str(), config->outputFile));
1718 timeTraceProfilerCleanup();
1720 return errorCount() == 0;