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 "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 "StripSwiftForceLoad.h"
20#include "SymbolTable.h"
21#include "Symbols.h"
22#include "SyntheticSections.h"
23#include "Target.h"
24#include "UnwindInfoSection.h"
25#include "Writer.h"
26
27#include "lld/Common/Args.h"
28#include "lld/Common/CommonLinkerContext.h"
29#include "lld/Common/ErrorHandler.h"
30#include "lld/Common/LLVM.h"
31#include "lld/Common/Memory.h"
32#include "lld/Common/Reproduce.h"
33#include "lld/Common/TargetOptionsCommandFlags.h"
34#include "lld/Common/Version.h"
35#include "llvm/ADT/DenseSet.h"
36#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/StringExtras.h"
38#include "llvm/ADT/StringRef.h"
39#include "llvm/BinaryFormat/MachO.h"
40#include "llvm/BinaryFormat/Magic.h"
41#include "llvm/CGData/CodeGenDataWriter.h"
42#include "llvm/Config/llvm-config.h"
43#include "llvm/LTO/LTO.h"
44#include "llvm/Object/Archive.h"
45#include "llvm/Option/ArgList.h"
46#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
48#include "llvm/Support/FileSystem.h"
49#include "llvm/Support/Parallel.h"
50#include "llvm/Support/Path.h"
51#include "llvm/Support/Process.h"
52#include "llvm/Support/TarWriter.h"
53#include "llvm/Support/TargetSelect.h"
54#include "llvm/Support/Threading.h"
55#include "llvm/Support/TimeProfiler.h"
56#include "llvm/TargetParser/Host.h"
57#include "llvm/TextAPI/Architecture.h"
58#include "llvm/TextAPI/PackedVersion.h"
59
60#if !_WIN32
61#include <sys/mman.h>
62#endif
63
64using namespace llvm;
65using namespace llvm::MachO;
66using namespace llvm::object;
67using namespace llvm::opt;
68using namespace llvm::sys;
69using namespace lld;
70using namespace lld::macho;
71
72std::unique_ptr<Configuration> macho::config;
73std::unique_ptr<DependencyTracker> macho::depTracker;
74
75static HeaderFileType getOutputType(const InputArgList &args) {
76 // TODO: -r, -dylinker, -preload...
77 Arg *outputArg = args.getLastArg(Ids: OPT_bundle, Ids: OPT_dylib, Ids: OPT_execute);
78 if (outputArg == nullptr)
79 return MH_EXECUTE;
80
81 switch (outputArg->getOption().getID()) {
82 case OPT_bundle:
83 return MH_BUNDLE;
84 case OPT_dylib:
85 return MH_DYLIB;
86 case OPT_execute:
87 return MH_EXECUTE;
88 default:
89 llvm_unreachable("internal error");
90 }
91}
92
93static DenseMap<CachedHashStringRef, StringRef> resolvedLibraries;
94static std::optional<StringRef> findLibrary(StringRef name) {
95 CachedHashStringRef key(name);
96 auto entry = resolvedLibraries.find(Val: key);
97 if (entry != resolvedLibraries.end())
98 return entry->second;
99
100 auto doFind = [&] {
101 // Special case for Csu support files required for Mac OS X 10.7 and older
102 // (crt1.o)
103 if (name.ends_with(Suffix: ".o"))
104 return findPathCombination(name, roots: config->librarySearchPaths, extensions: {""});
105 if (config->searchDylibsFirst) {
106 if (std::optional<StringRef> path =
107 findPathCombination(name: "lib" + name, roots: config->librarySearchPaths,
108 extensions: {".tbd", ".dylib", ".so"}))
109 return path;
110 return findPathCombination(name: "lib" + name, roots: config->librarySearchPaths,
111 extensions: {".a"});
112 }
113 return findPathCombination(name: "lib" + name, roots: config->librarySearchPaths,
114 extensions: {".tbd", ".dylib", ".so", ".a"});
115 };
116
117 std::optional<StringRef> path = doFind();
118 if (path)
119 resolvedLibraries[key] = *path;
120
121 return path;
122}
123
124static DenseMap<CachedHashStringRef, StringRef> resolvedFrameworks;
125static std::optional<StringRef> findFramework(StringRef name) {
126 CachedHashStringRef key(name);
127 auto entry = resolvedFrameworks.find(Val: key);
128 if (entry != resolvedFrameworks.end())
129 return entry->second;
130
131 SmallString<260> symlink;
132 StringRef suffix;
133 std::tie(args&: name, args&: suffix) = name.split(Separator: ",");
134 for (StringRef dir : config->frameworkSearchPaths) {
135 symlink = dir;
136 path::append(path&: symlink, a: name + ".framework", b: name);
137
138 if (!suffix.empty()) {
139 // NOTE: we must resolve the symlink before trying the suffixes, because
140 // there are no symlinks for the suffixed paths.
141 SmallString<260> location;
142 if (!fs::real_path(path: symlink, output&: location)) {
143 // only append suffix if realpath() succeeds
144 Twine suffixed = location + suffix;
145 if (fs::exists(Path: suffixed))
146 return resolvedFrameworks[key] = saver().save(S: suffixed.str());
147 }
148 // Suffix lookup failed, fall through to the no-suffix case.
149 }
150
151 if (std::optional<StringRef> path = resolveDylibPath(path: symlink.str()))
152 return resolvedFrameworks[key] = *path;
153 }
154 return {};
155}
156
157static bool warnIfNotDirectory(StringRef option, StringRef path) {
158 if (!fs::exists(Path: path)) {
159 warn(msg: "directory not found for option -" + option + path);
160 return false;
161 } else if (!fs::is_directory(Path: path)) {
162 warn(msg: "option -" + option + path + " references a non-directory path");
163 return false;
164 }
165 return true;
166}
167
168static std::vector<StringRef>
169getSearchPaths(unsigned optionCode, InputArgList &args,
170 const std::vector<StringRef> &roots,
171 const SmallVector<StringRef, 2> &systemPaths) {
172 std::vector<StringRef> paths;
173 StringRef optionLetter{optionCode == OPT_F ? "F" : "L"};
174 for (StringRef path : args::getStrings(args, id: optionCode))
175 for (StringRef searchPath : getRerootedSearchPaths(path, roots))
176 if (searchPath != path || warnIfNotDirectory(option: optionLetter, path: searchPath))
177 paths.push_back(x: searchPath);
178
179 // `-Z` suppresses the standard "system" search paths.
180 if (args.hasArg(Ids: OPT_Z))
181 return paths;
182
183 for (const StringRef &path : systemPaths) {
184 for (const StringRef &root : roots) {
185 SmallString<261> buffer(root);
186 path::append(path&: buffer, a: path);
187 if (fs::is_directory(Path: buffer))
188 paths.push_back(x: saver().save(S: buffer.str()));
189 }
190 }
191 return paths;
192}
193
194static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) {
195 std::vector<StringRef> roots;
196 for (const Arg *arg : args.filtered(Ids: OPT_syslibroot))
197 roots.push_back(x: arg->getValue());
198 // NOTE: the final `-syslibroot` being `/` will ignore all roots
199 if (!roots.empty() && roots.back() == "/")
200 roots.clear();
201 // NOTE: roots can never be empty - add an empty root to simplify the library
202 // and framework search path computation.
203 if (roots.empty())
204 roots.emplace_back(args: "");
205 return roots;
206}
207
208static std::vector<StringRef>
209getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) {
210 return getSearchPaths(optionCode: OPT_L, args, roots, systemPaths: {"/usr/lib", "/usr/local/lib"});
211}
212
213static std::vector<StringRef>
214getFrameworkSearchPaths(InputArgList &args,
215 const std::vector<StringRef> &roots) {
216 return getSearchPaths(optionCode: OPT_F, args, roots,
217 systemPaths: {"/Library/Frameworks", "/System/Library/Frameworks"});
218}
219
220static llvm::CachePruningPolicy getLTOCachePolicy(InputArgList &args) {
221 SmallString<128> ltoPolicy;
222 auto add = [&ltoPolicy](Twine val) {
223 if (!ltoPolicy.empty())
224 ltoPolicy += ":";
225 val.toVector(Out&: ltoPolicy);
226 };
227 for (const Arg *arg :
228 args.filtered(Ids: OPT_thinlto_cache_policy_eq, Ids: OPT_prune_interval_lto,
229 Ids: OPT_prune_after_lto, Ids: OPT_max_relative_cache_size_lto)) {
230 switch (arg->getOption().getID()) {
231 case OPT_thinlto_cache_policy_eq:
232 add(arg->getValue());
233 break;
234 case OPT_prune_interval_lto:
235 if (!strcmp(s1: "-1", s2: arg->getValue()))
236 add("prune_interval=87600h"); // 10 years
237 else
238 add(Twine("prune_interval=") + arg->getValue() + "s");
239 break;
240 case OPT_prune_after_lto:
241 add(Twine("prune_after=") + arg->getValue() + "s");
242 break;
243 case OPT_max_relative_cache_size_lto:
244 add(Twine("cache_size=") + arg->getValue() + "%");
245 break;
246 }
247 }
248 return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy");
249}
250
251// What caused a given library to be loaded. Only relevant for archives.
252// Note that this does not tell us *how* we should load the library, i.e.
253// whether we should do it lazily or eagerly (AKA force loading). The "how" is
254// decided within addFile().
255enum class LoadType {
256 CommandLine, // Library was passed as a regular CLI argument
257 CommandLineForce, // Library was passed via `-force_load`
258 LCLinkerOption, // Library was passed via LC_LINKER_OPTIONS
259};
260
261struct ArchiveFileInfo {
262 ArchiveFile *file;
263 bool isCommandLineLoad;
264};
265
266static DenseMap<StringRef, ArchiveFileInfo> loadedArchives;
267
268static void saveThinArchiveToRepro(ArchiveFile const *file) {
269 assert(tar && file->getArchive().isThin());
270
271 Error e = Error::success();
272 for (const object::Archive::Child &c : file->getArchive().children(Err&: e)) {
273 MemoryBufferRef mb = CHECK(c.getMemoryBufferRef(),
274 toString(file) + ": failed to get buffer");
275 tar->append(Path: relativeToRoot(CHECK(c.getFullName(), file)), Data: mb.getBuffer());
276 }
277 if (e)
278 error(msg: toString(file) +
279 ": Archive::children failed: " + toString(E: std::move(e)));
280}
281
282struct DeferredFile {
283 StringRef path;
284 bool isLazy;
285 MemoryBufferRef buffer;
286 LoadType loadType = LoadType::CommandLine;
287 bool isNeeded = false;
288 bool isWeak = false;
289 bool isReexport = false;
290 bool isHidden = false;
291 bool isExplicit = true;
292};
293using DeferredFiles = std::vector<DeferredFile>;
294
295#if LLVM_ENABLE_THREADS
296class SerialBackgroundWorkQueue {
297 std::deque<std::function<void()>> queue;
298 std::thread *running;
299 std::mutex mutex;
300
301public:
302 std::atomic_bool stopAllWork = false;
303 void queueWork(std::function<void()> work) {
304 mutex.lock();
305 if (running && queue.empty()) {
306 mutex.unlock();
307 running->join();
308 mutex.lock();
309 delete running;
310 running = nullptr;
311 }
312
313 if (work) {
314 queue.emplace_back(args: std::move(work));
315 if (!running)
316 running = new std::thread([&]() {
317 while (!stopAllWork) {
318 mutex.lock();
319 if (queue.empty()) {
320 mutex.unlock();
321 break;
322 }
323 auto work = std::move(queue.front());
324 mutex.unlock();
325 work();
326 mutex.lock();
327 queue.pop_front();
328 mutex.unlock();
329 }
330 });
331 }
332 mutex.unlock();
333 }
334};
335
336static SerialBackgroundWorkQueue pageInQueue;
337
338// Most input files have been mapped but not yet paged in.
339// This code forces the page-ins on multiple threads so
340// the process is not stalled waiting on disk buffer i/o.
341void multiThreadedPageInBackground(DeferredFiles &deferred) {
342 static const size_t pageSize = Process::getPageSizeEstimate();
343 static const size_t largeArchive = 10 * 1024 * 1024;
344#ifndef NDEBUG
345 using namespace std::chrono;
346 static std::atomic_uint64_t totalBytes = 0;
347 std::atomic_int numDeferedFilesAdvised = 0;
348 auto t0 = high_resolution_clock::now();
349#endif
350
351 auto preloadDeferredFile = [&](const DeferredFile &deferredFile) {
352 const StringRef &buff = deferredFile.buffer.getBuffer();
353 if (buff.size() > largeArchive)
354 return;
355
356#ifndef NDEBUG
357 totalBytes += buff.size();
358 numDeferedFilesAdvised += 1;
359#endif
360#if _WIN32
361 // Reference all file's mmap'd pages to load them into memory.
362 for (const char *page = buff.data(), *end = page + buff.size();
363 page < end && !pageInQueue.stopAllWork; page += pageSize) {
364 [[maybe_unused]] volatile char t = *page;
365 (void)t;
366 }
367#else
368#define DEBUG_TYPE "lld-madvise"
369 auto aligned =
370 llvm::alignDown(Value: reinterpret_cast<uintptr_t>(buff.data()), Align: pageSize);
371 if (madvise(addr: (void *)aligned, len: buff.size(), MADV_WILLNEED) < 0)
372 LLVM_DEBUG(llvm::dbgs() << "madvise error: " << strerror(errno) << "\n");
373#undef DEBUG_TYPE
374#endif
375 };
376
377 { // Create scope for waiting for the taskGroup
378 std::atomic_size_t index = 0;
379 llvm::parallel::TaskGroup taskGroup;
380 for (int w = 0; w < config->readWorkers; w++)
381 taskGroup.spawn(f: [&index, &preloadDeferredFile, &deferred]() {
382 while (!pageInQueue.stopAllWork) {
383 size_t localIndex = index.fetch_add(i: 1);
384 if (localIndex >= deferred.size())
385 break;
386 preloadDeferredFile(deferred[localIndex]);
387 }
388 });
389 }
390
391#ifndef NDEBUG
392 auto dt = high_resolution_clock::now() - t0;
393 if (Process::GetEnv("LLD_MULTI_THREAD_PAGE"))
394 llvm::dbgs() << "multiThreadedPageIn " << totalBytes << "/"
395 << numDeferedFilesAdvised << "/" << deferred.size() << "/"
396 << duration_cast<milliseconds>(dt).count() / 1000. << "\n";
397#endif
398}
399
400static void multiThreadedPageIn(const DeferredFiles &deferred) {
401 pageInQueue.queueWork(work: [=]() {
402 DeferredFiles files = deferred;
403 multiThreadedPageInBackground(deferred&: files);
404 });
405}
406#endif
407
408static InputFile *processFile(std::optional<MemoryBufferRef> buffer,
409 DeferredFiles *archiveContents, StringRef path,
410 LoadType loadType, bool isLazy = false,
411 bool isExplicit = true,
412 bool isBundleLoader = false,
413 bool isForceHidden = false) {
414 if (!buffer)
415 return nullptr;
416 MemoryBufferRef mbref = *buffer;
417 InputFile *newFile = nullptr;
418
419 file_magic magic = identify_magic(magic: mbref.getBuffer());
420 switch (magic) {
421 case file_magic::archive: {
422 bool isCommandLineLoad = loadType != LoadType::LCLinkerOption;
423 // Avoid loading archives twice. If the archives are being force-loaded,
424 // loading them twice would create duplicate symbol errors. In the
425 // non-force-loading case, this is just a minor performance optimization.
426 // We don't take a reference to cachedFile here because the
427 // loadArchiveMember() call below may recursively call addFile() and
428 // invalidate this reference.
429 auto entry = loadedArchives.find(Val: path);
430
431 ArchiveFile *file;
432 if (entry == loadedArchives.end()) {
433 // No cached archive, we need to create a new one
434 std::unique_ptr<object::Archive> archive = CHECK(
435 object::Archive::create(mbref), path + ": failed to parse archive");
436
437 file = make<ArchiveFile>(args: std::move(archive), args&: isForceHidden);
438
439 if (tar && file->getArchive().isThin())
440 saveThinArchiveToRepro(file);
441 } else {
442 file = entry->second.file;
443 // Command-line loads take precedence. If file is previously loaded via
444 // command line, or is loaded via LC_LINKER_OPTION and being loaded via
445 // LC_LINKER_OPTION again, using the cached archive is enough.
446 if (entry->second.isCommandLineLoad || !isCommandLineLoad)
447 return file;
448 }
449
450 bool isLCLinkerForceLoad = loadType == LoadType::LCLinkerOption &&
451 config->forceLoadSwift &&
452 path::filename(path).starts_with(Prefix: "libswift");
453 if ((isCommandLineLoad && config->allLoad) ||
454 loadType == LoadType::CommandLineForce || isLCLinkerForceLoad) {
455 if (readFile(path)) {
456 Error e = Error::success();
457 for (const object::Archive::Child &c : file->getArchive().children(Err&: e)) {
458 StringRef reason;
459 switch (loadType) {
460 case LoadType::LCLinkerOption:
461 reason = "LC_LINKER_OPTION";
462 break;
463 case LoadType::CommandLineForce:
464 reason = "-force_load";
465 break;
466 case LoadType::CommandLine:
467 reason = "-all_load";
468 break;
469 }
470 if (Error e = file->fetch(c, reason)) {
471 if (config->warnThinArchiveMissingMembers)
472 warn(msg: toString(file) + ": " + reason +
473 " failed to load archive member: " + toString(E: std::move(e)));
474 else
475 llvm::consumeError(Err: std::move(e));
476 }
477 }
478 if (e)
479 error(msg: toString(file) +
480 ": Archive::children failed: " + toString(E: std::move(e)));
481 }
482 } else if (isCommandLineLoad && config->forceLoadObjC) {
483 if (file->getArchive().hasSymbolTable()) {
484 for (const object::Archive::Symbol &sym : file->getArchive().symbols())
485 if (sym.getName().starts_with(Prefix: objc::symbol_names::klass))
486 file->fetch(sym);
487 }
488
489 // TODO: no need to look for ObjC sections for a given archive member if
490 // we already found that it contains an ObjC symbol.
491 if (readFile(path)) {
492 Error e = Error::success();
493 for (const object::Archive::Child &c : file->getArchive().children(Err&: e)) {
494 Expected<MemoryBufferRef> mb = c.getMemoryBufferRef();
495 if (!mb) {
496 // We used to create broken repro tarballs that only included those
497 // object files from thin archives that ended up being used.
498 if (config->warnThinArchiveMissingMembers)
499 warn(msg: toString(file) + ": -ObjC failed to open archive member: " +
500 toString(E: mb.takeError()));
501 else
502 llvm::consumeError(Err: mb.takeError());
503 continue;
504 }
505
506 if (config->readWorkers && archiveContents)
507 archiveContents->push_back(x: {.path: path, .isLazy: isLazy, .buffer: *mb});
508 if (!hasObjCSection(*mb))
509 continue;
510 if (Error e = file->fetch(c, reason: "-ObjC"))
511 error(msg: toString(file) + ": -ObjC failed to load archive member: " +
512 toString(E: std::move(e)));
513 }
514 if (e)
515 error(msg: toString(file) +
516 ": Archive::children failed: " + toString(E: std::move(e)));
517 }
518 }
519 if (!archiveContents || archiveContents->empty())
520 file->addLazySymbols();
521 loadedArchives[path] = ArchiveFileInfo{.file: file, .isCommandLineLoad: isCommandLineLoad};
522 newFile = file;
523 break;
524 }
525 case file_magic::macho_object:
526 newFile = make<ObjFile>(args&: mbref, args: getModTime(path), args: "", args&: isLazy);
527 break;
528 case file_magic::macho_dynamically_linked_shared_lib:
529 case file_magic::macho_dynamically_linked_shared_lib_stub:
530 case file_magic::tapi_file:
531 if (DylibFile *dylibFile =
532 loadDylib(mbref, umbrella: nullptr, /*isBundleLoader=*/false, explicitlyLinked: isExplicit))
533 newFile = dylibFile;
534 break;
535 case file_magic::bitcode:
536 newFile = make<BitcodeFile>(args&: mbref, args: "", args: 0, args&: isLazy);
537 break;
538 case file_magic::macho_executable:
539 case file_magic::macho_bundle:
540 // We only allow executable and bundle type here if it is used
541 // as a bundle loader.
542 if (!isBundleLoader)
543 error(msg: path + ": unhandled file type");
544 if (DylibFile *dylibFile = loadDylib(mbref, umbrella: nullptr, isBundleLoader))
545 newFile = dylibFile;
546 break;
547 default:
548 error(msg: path + ": unhandled file type");
549 }
550 if (newFile && !isa<DylibFile>(Val: newFile)) {
551 if ((isa<ObjFile>(Val: newFile) || isa<BitcodeFile>(Val: newFile)) && newFile->lazy &&
552 config->forceLoadObjC) {
553 for (Symbol *sym : newFile->symbols)
554 if (sym && sym->getName().starts_with(Prefix: objc::symbol_names::klass)) {
555 extract(file&: *newFile, reason: "-ObjC");
556 break;
557 }
558 if (newFile->lazy && hasObjCSection(mbref))
559 extract(file&: *newFile, reason: "-ObjC");
560 }
561
562 // printArchiveMemberLoad() prints both .a and .o names, so no need to
563 // print the .a name here. Similarly skip lazy files.
564 if (config->printEachFile && magic != file_magic::archive && !isLazy)
565 message(msg: toString(file: newFile));
566 inputFiles.insert(X: newFile);
567 }
568 return newFile;
569}
570
571static InputFile *addFile(StringRef path, LoadType loadType,
572 bool isLazy = false, bool isExplicit = true,
573 bool isBundleLoader = false,
574 bool isForceHidden = false) {
575 return processFile(buffer: readFile(path), archiveContents: nullptr, path, loadType, isLazy,
576 isExplicit, isBundleLoader, isForceHidden);
577}
578
579static DenseSet<StringRef> loadedObjectFrameworks;
580
581static void applyDylibMetadata(InputFile *file, bool isNeeded, bool isWeak,
582 bool isReexport) {
583 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(Val: file)) {
584 dylibFile->forceNeeded |= isNeeded;
585 dylibFile->forceWeakImport |= isWeak;
586 if (isReexport) {
587 config->hasReexports = true;
588 dylibFile->reexport = true;
589 }
590 }
591}
592
593static void checkAndCacheFramework(InputFile *file, StringRef path) {
594 if (isa_and_nonnull<ObjFile>(Val: file) || isa_and_nonnull<BitcodeFile>(Val: file)) {
595 if (path.contains(Other: ".framework"))
596 loadedObjectFrameworks.insert(V: path);
597 }
598}
599
600static void deferFile(StringRef path, bool isLazy, DeferredFiles &deferred,
601 LoadType loadType = LoadType::CommandLine,
602 bool isNeeded = false, bool isWeak = false,
603 bool isReexport = false, bool isHidden = false,
604 bool isExplicit = true) {
605 std::optional<MemoryBufferRef> buffer = readFile(path);
606 if (!buffer)
607 return;
608 if (config->readWorkers)
609 deferred.push_back(x: {.path: path, .isLazy: isLazy, .buffer: *buffer, .loadType: loadType, .isNeeded: isNeeded, .isWeak: isWeak,
610 .isReexport: isReexport, .isHidden: isHidden, .isExplicit: isExplicit});
611 else {
612 if (loadedObjectFrameworks.contains(V: path))
613 return;
614
615 InputFile *file =
616 processFile(buffer, archiveContents: nullptr, path, loadType, isLazy, isExplicit,
617 /*isBundleLoader=*/false, isForceHidden: isHidden);
618 applyDylibMetadata(file, isNeeded, isWeak, isReexport);
619 checkAndCacheFramework(file, path);
620 }
621}
622
623static std::vector<StringRef> missingAutolinkWarnings;
624static void addLibrary(StringRef name, bool isNeeded, bool isWeak,
625 bool isReexport, bool isHidden, bool isExplicit,
626 LoadType loadType, DeferredFiles &deferred) {
627 if (std::optional<StringRef> path = findLibrary(name)) {
628 deferFile(path: *path, /*isLazy=*/false, deferred, loadType, isNeeded, isWeak,
629 isReexport, isHidden, isExplicit);
630 return;
631 }
632 if (loadType == LoadType::LCLinkerOption) {
633 missingAutolinkWarnings.push_back(
634 x: saver().save(S: "auto-linked library not found for -l" + name));
635 return;
636 }
637 error(msg: "library not found for -l" + name);
638}
639
640static void addFramework(StringRef name, bool isNeeded, bool isWeak,
641 bool isReexport, bool isExplicit, LoadType loadType,
642 DeferredFiles &deferred) {
643 if (std::optional<StringRef> path = findFramework(name)) {
644 if (loadedObjectFrameworks.contains(V: *path))
645 return;
646
647 deferFile(path: *path, /*isLazy=*/false, deferred, loadType, isNeeded, isWeak,
648 isReexport, /*isHidden=*/false, isExplicit);
649 return;
650 }
651 if (loadType == LoadType::LCLinkerOption) {
652 missingAutolinkWarnings.push_back(
653 x: saver().save(S: "auto-linked framework not found for -framework " + name));
654 return;
655 }
656 error(msg: "framework not found for -framework " + name);
657}
658
659// Parses LC_LINKER_OPTION contents, which can add additional command line
660// flags. This directly parses the flags instead of using the standard argument
661// parser to improve performance.
662void macho::parseLCLinkerOption(
663 llvm::SmallVectorImpl<StringRef> &LCLinkerOptions, InputFile *f,
664 unsigned argc, StringRef data) {
665 if (config->ignoreAutoLink)
666 return;
667
668 SmallVector<StringRef, 4> argv;
669 size_t offset = 0;
670 for (unsigned i = 0; i < argc && offset < data.size(); ++i) {
671 argv.push_back(Elt: data.data() + offset);
672 offset += strlen(s: data.data() + offset) + 1;
673 }
674 if (argv.size() != argc || offset > data.size())
675 fatal(msg: toString(file: f) + ": invalid LC_LINKER_OPTION");
676
677 unsigned i = 0;
678 StringRef arg = argv[i];
679 if (arg.consume_front(Prefix: "-l")) {
680 if (config->ignoreAutoLinkOptions.contains(key: arg))
681 return;
682 } else if (arg == "-framework") {
683 StringRef name = argv[++i];
684 if (config->ignoreAutoLinkOptions.contains(key: name))
685 return;
686 } else {
687 error(msg: arg + " is not allowed in LC_LINKER_OPTION");
688 }
689
690 LCLinkerOptions.append(RHS: argv);
691}
692
693void macho::resolveLCLinkerOptions() {
694 while (!unprocessedLCLinkerOptions.empty()) {
695 SmallVector<StringRef> LCLinkerOptions(unprocessedLCLinkerOptions);
696 unprocessedLCLinkerOptions.clear();
697
698 DeferredFiles deferred;
699 SmallVector<StringRef> frameworks;
700 SmallVector<StringRef> libraries;
701
702 for (unsigned i = 0; i < LCLinkerOptions.size(); ++i) {
703 StringRef arg = LCLinkerOptions[i];
704 if (arg.consume_front(Prefix: "-l")) {
705 assert(!config->ignoreAutoLinkOptions.contains(arg));
706 libraries.push_back(Elt: arg);
707 } else if (arg == "-framework") {
708 StringRef name = LCLinkerOptions[++i];
709 assert(!config->ignoreAutoLinkOptions.contains(name));
710 frameworks.push_back(Elt: name);
711 } else {
712 error(msg: arg + " is not allowed in LC_LINKER_OPTION");
713 }
714 }
715
716 llvm::sort(C&: frameworks);
717 llvm::sort(C&: libraries);
718
719 frameworks.erase(CS: std::unique(first: frameworks.begin(), last: frameworks.end()),
720 CE: frameworks.end());
721 libraries.erase(CS: std::unique(first: libraries.begin(), last: libraries.end()),
722 CE: libraries.end());
723
724 for (const StringRef framework : frameworks) {
725 addFramework(name: framework, /*isNeeded=*/false, /*isWeak=*/false,
726 /*isReexport=*/false, /*isExplicit=*/false,
727 loadType: LoadType::LCLinkerOption, deferred);
728 }
729
730 for (const StringRef library : libraries) {
731 addLibrary(name: library, /*isNeeded=*/false, /*isWeak=*/false,
732 /*isReexport=*/false, /*isHidden=*/false,
733 /*isExplicit=*/false, loadType: LoadType::LCLinkerOption, deferred);
734 }
735
736 for (auto &file : deferred) {
737 if (loadedObjectFrameworks.contains(V: file.path))
738 continue;
739
740 auto inputFile = processFile(buffer: file.buffer, archiveContents: nullptr, path: file.path,
741 loadType: file.loadType, isLazy: file.isLazy, isExplicit: file.isExplicit,
742 /*isBundleLoader=*/false, isForceHidden: file.isHidden);
743 applyDylibMetadata(file: inputFile, isNeeded: file.isNeeded, isWeak: file.isWeak,
744 isReexport: file.isReexport);
745 checkAndCacheFramework(file: inputFile, path: file.path);
746 }
747 }
748}
749
750static void addFileList(StringRef path, bool isLazy,
751 DeferredFiles &deferredFiles) {
752 std::optional<MemoryBufferRef> buffer = readFile(path);
753 if (!buffer)
754 return;
755 MemoryBufferRef mbref = *buffer;
756 for (StringRef path : args::getLines(mb: mbref))
757 deferFile(path: rerootPath(path), isLazy, deferred&: deferredFiles);
758}
759
760// We expect sub-library names of the form "libfoo", which will match a dylib
761// with a path of .*/libfoo.{dylib, tbd}.
762// XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
763// I'm not sure what the use case for that is.
764static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) {
765 for (InputFile *file : inputFiles) {
766 if (auto *dylibFile = dyn_cast<DylibFile>(Val: file)) {
767 StringRef filename = path::filename(path: dylibFile->getName());
768 if (filename.consume_front(Prefix: searchName) &&
769 (filename.empty() || llvm::is_contained(Range&: extensions, Element: filename))) {
770 dylibFile->reexport = true;
771 return true;
772 }
773 }
774 }
775 return false;
776}
777
778// This function is called on startup. We need this for LTO since
779// LTO calls LLVM functions to compile bitcode files to native code.
780// Technically this can be delayed until we read bitcode files, but
781// we don't bother to do lazily because the initialization is fast.
782static void initLLVM() {
783 InitializeAllTargets();
784 InitializeAllTargetMCs();
785 InitializeAllAsmPrinters();
786 InitializeAllAsmParsers();
787}
788
789static bool compileBitcodeFiles() {
790 TimeTraceScope timeScope("LTO");
791 auto *lto = make<BitcodeCompiler>();
792 for (InputFile *file : inputFiles)
793 if (auto *bitcodeFile = dyn_cast<BitcodeFile>(Val: file))
794 if (!file->lazy)
795 lto->add(f&: *bitcodeFile);
796
797 std::vector<ObjFile *> compiled = lto->compile();
798 inputFiles.insert_range(R&: compiled);
799
800 return !compiled.empty();
801}
802
803// Replaces common symbols with defined symbols residing in __common sections.
804// This function must be called after all symbol names are resolved (i.e. after
805// all InputFiles have been loaded.) As a result, later operations won't see
806// any CommonSymbols.
807static void replaceCommonSymbols() {
808 TimeTraceScope timeScope("Replace common symbols");
809 ConcatOutputSection *osec = nullptr;
810 for (Symbol *sym : symtab->getSymbols()) {
811 auto *common = dyn_cast<CommonSymbol>(Val: sym);
812 if (common == nullptr)
813 continue;
814
815 // Casting to size_t will truncate large values on 32-bit architectures,
816 // but it's not really worth supporting the linking of 64-bit programs on
817 // 32-bit archs.
818 ArrayRef<uint8_t> data = {nullptr, static_cast<size_t>(common->size)};
819 // FIXME avoid creating one Section per symbol?
820 auto *section =
821 make<Section>(args: common->getFile(), args: segment_names::data,
822 args: section_names::common, args: S_ZEROFILL, /*addr=*/args: 0);
823 auto *isec = make<ConcatInputSection>(args&: *section, args&: data, args: common->align);
824 if (!osec)
825 osec = ConcatOutputSection::getOrCreateForInput(isec);
826 isec->parent = osec;
827 addInputSection(inputSection: isec);
828
829 // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
830 // and pass them on here.
831 replaceSymbol<Defined>(
832 s: sym, arg: sym->getName(), arg: common->getFile(), arg&: isec, /*value=*/arg: 0, arg: common->size,
833 /*isWeakDef=*/arg: false, /*isExternal=*/arg: true, arg: common->privateExtern,
834 /*includeInSymtab=*/arg: true, /*isReferencedDynamically=*/arg: false,
835 /*noDeadStrip=*/arg: false);
836 }
837}
838
839static void initializeSectionRenameMap() {
840 if (config->dataConst) {
841 SmallVector<StringRef> v{section_names::authGot,
842 section_names::authPtr,
843 section_names::cfString,
844 section_names::const_,
845 section_names::constCfObj2,
846 section_names::got,
847 section_names::moduleInitFunc,
848 section_names::moduleTermFunc,
849 section_names::nonLazySymbolPtr,
850 section_names::objcArrayData,
851 section_names::objcArrayObj,
852 section_names::objcCatList,
853 section_names::objcCatList2,
854 section_names::objcClassList,
855 section_names::objcDateObj,
856 section_names::objcDictObj,
857 section_names::objcDoubleObj,
858 section_names::objcFloatObj,
859 section_names::objCImageInfo,
860 section_names::objcIntObj,
861 section_names::objcNonLazyCatList,
862 section_names::objcNonLazyClassList,
863 section_names::objcProtoList};
864 for (StringRef s : v)
865 config->sectionRenameMap[{segment_names::data, s}] = {
866 segment_names::dataConst, s};
867 }
868 config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = {
869 segment_names::text, section_names::text};
870 config->sectionRenameMap[{segment_names::import, section_names::pointers}] = {
871 config->dataConst ? segment_names::dataConst : segment_names::data,
872 section_names::nonLazySymbolPtr};
873}
874
875static inline char toLowerDash(char x) {
876 if (x >= 'A' && x <= 'Z')
877 return x - 'A' + 'a';
878 else if (x == ' ')
879 return '-';
880 return x;
881}
882
883static std::string lowerDash(StringRef s) {
884 return std::string(map_iterator(I: s.begin(), F: toLowerDash),
885 map_iterator(I: s.end(), F: toLowerDash));
886}
887
888struct PlatformVersion {
889 PlatformType platform = PLATFORM_UNKNOWN;
890 llvm::VersionTuple minimum;
891 llvm::VersionTuple sdk;
892};
893
894static PlatformVersion parsePlatformVersion(const Arg *arg) {
895 assert(arg->getOption().getID() == OPT_platform_version);
896 StringRef platformStr = arg->getValue(N: 0);
897 StringRef minVersionStr = arg->getValue(N: 1);
898 StringRef sdkVersionStr = arg->getValue(N: 2);
899
900 PlatformVersion platformVersion;
901
902 // TODO(compnerd) see if we can generate this case list via XMACROS
903 platformVersion.platform =
904 StringSwitch<PlatformType>(lowerDash(s: platformStr))
905 .Cases(CaseStrings: {"macos", "1"}, Value: PLATFORM_MACOS)
906 .Cases(CaseStrings: {"ios", "2"}, Value: PLATFORM_IOS)
907 .Cases(CaseStrings: {"tvos", "3"}, Value: PLATFORM_TVOS)
908 .Cases(CaseStrings: {"watchos", "4"}, Value: PLATFORM_WATCHOS)
909 .Cases(CaseStrings: {"bridgeos", "5"}, Value: PLATFORM_BRIDGEOS)
910 .Cases(CaseStrings: {"mac-catalyst", "6"}, Value: PLATFORM_MACCATALYST)
911 .Cases(CaseStrings: {"ios-simulator", "7"}, Value: PLATFORM_IOSSIMULATOR)
912 .Cases(CaseStrings: {"tvos-simulator", "8"}, Value: PLATFORM_TVOSSIMULATOR)
913 .Cases(CaseStrings: {"watchos-simulator", "9"}, Value: PLATFORM_WATCHOSSIMULATOR)
914 .Cases(CaseStrings: {"driverkit", "10"}, Value: PLATFORM_DRIVERKIT)
915 .Cases(CaseStrings: {"xros", "11"}, Value: PLATFORM_XROS)
916 .Cases(CaseStrings: {"xros-simulator", "12"}, Value: PLATFORM_XROS_SIMULATOR)
917 .Default(Value: PLATFORM_UNKNOWN);
918 if (platformVersion.platform == PLATFORM_UNKNOWN)
919 error(msg: Twine("malformed platform: ") + platformStr);
920 // The underlying load command only supports 3 components.
921 if (platformVersion.minimum.tryParse(string: minVersionStr) ||
922 platformVersion.minimum.getBuild())
923 error(msg: Twine("malformed minimum version: ") + minVersionStr);
924 if (platformVersion.sdk.tryParse(string: sdkVersionStr) ||
925 platformVersion.sdk.getBuild())
926 error(msg: Twine("malformed sdk version: ") + sdkVersionStr);
927 return platformVersion;
928}
929
930// Has the side-effect of setting Config::platformInfo and
931// potentially Config::secondaryPlatformInfo.
932static void setPlatformVersions(StringRef archName, const ArgList &args) {
933 std::map<PlatformType, PlatformVersion> platformVersions;
934 const PlatformVersion *lastVersionInfo = nullptr;
935 for (const Arg *arg : args.filtered(Ids: OPT_platform_version)) {
936 PlatformVersion version = parsePlatformVersion(arg);
937
938 // For each platform, the last flag wins:
939 // `-platform_version macos 2 3 -platform_version macos 4 5` has the same
940 // effect as just passing `-platform_version macos 4 5`.
941 // FIXME: ld64 warns on multiple flags for one platform. Should we?
942 platformVersions[version.platform] = version;
943 lastVersionInfo = &platformVersions[version.platform];
944 }
945
946 if (platformVersions.empty()) {
947 error(msg: "must specify -platform_version");
948 return;
949 }
950 if (platformVersions.size() > 2) {
951 error(msg: "must specify -platform_version at most twice");
952 return;
953 }
954 if (platformVersions.size() == 2) {
955 bool isZipperedCatalyst = platformVersions.count(x: PLATFORM_MACOS) &&
956 platformVersions.count(x: PLATFORM_MACCATALYST);
957
958 if (!isZipperedCatalyst) {
959 error(msg: "lld supports writing zippered outputs only for "
960 "macos and mac-catalyst");
961 } else if (config->outputType != MH_DYLIB &&
962 config->outputType != MH_BUNDLE) {
963 error(msg: "writing zippered outputs only valid for -dylib and -bundle");
964 }
965
966 config->platformInfo = {
967 .target: MachO::Target(getArchitectureFromName(Name: archName), PLATFORM_MACOS,
968 platformVersions[PLATFORM_MACOS].minimum),
969 .sdk: platformVersions[PLATFORM_MACOS].sdk};
970 config->secondaryPlatformInfo = {
971 .target: MachO::Target(getArchitectureFromName(Name: archName), PLATFORM_MACCATALYST,
972 platformVersions[PLATFORM_MACCATALYST].minimum),
973 .sdk: platformVersions[PLATFORM_MACCATALYST].sdk};
974 return;
975 }
976
977 config->platformInfo = {.target: MachO::Target(getArchitectureFromName(Name: archName),
978 lastVersionInfo->platform,
979 lastVersionInfo->minimum),
980 .sdk: lastVersionInfo->sdk};
981}
982
983// Has the side-effect of setting Config::target.
984static TargetInfo *createTargetInfo(InputArgList &args) {
985 StringRef archName = args.getLastArgValue(Id: OPT_arch);
986 if (archName.empty()) {
987 error(msg: "must specify -arch");
988 return nullptr;
989 }
990
991 setPlatformVersions(archName, args);
992 auto [cpuType, cpuSubtype] = getCPUTypeFromArchitecture(Arch: config->arch());
993 switch (cpuType) {
994 case CPU_TYPE_X86_64:
995 return createX86_64TargetInfo();
996 case CPU_TYPE_ARM64:
997 if (config->arch() == AK_arm64e)
998 return createARM64eTargetInfo();
999 return createARM64TargetInfo();
1000 case CPU_TYPE_ARM64_32:
1001 return createARM64_32TargetInfo();
1002 default:
1003 error(msg: "missing or unsupported -arch " + archName);
1004 return nullptr;
1005 }
1006}
1007
1008static UndefinedSymbolTreatment
1009getUndefinedSymbolTreatment(const ArgList &args) {
1010 StringRef treatmentStr = args.getLastArgValue(Id: OPT_undefined);
1011 auto treatment =
1012 StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
1013 .Cases(CaseStrings: {"error", ""}, Value: UndefinedSymbolTreatment::error)
1014 .Case(S: "warning", Value: UndefinedSymbolTreatment::warning)
1015 .Case(S: "suppress", Value: UndefinedSymbolTreatment::suppress)
1016 .Case(S: "dynamic_lookup", Value: UndefinedSymbolTreatment::dynamic_lookup)
1017 .Default(Value: UndefinedSymbolTreatment::unknown);
1018 if (treatment == UndefinedSymbolTreatment::unknown) {
1019 warn(msg: Twine("unknown -undefined TREATMENT '") + treatmentStr +
1020 "', defaulting to 'error'");
1021 treatment = UndefinedSymbolTreatment::error;
1022 } else if (config->namespaceKind == NamespaceKind::twolevel &&
1023 (treatment == UndefinedSymbolTreatment::warning ||
1024 treatment == UndefinedSymbolTreatment::suppress)) {
1025 if (treatment == UndefinedSymbolTreatment::warning)
1026 fatal(msg: "'-undefined warning' only valid with '-flat_namespace'");
1027 else
1028 fatal(msg: "'-undefined suppress' only valid with '-flat_namespace'");
1029 treatment = UndefinedSymbolTreatment::error;
1030 }
1031 return treatment;
1032}
1033
1034static ICFLevel getICFLevel(const ArgList &args) {
1035 StringRef icfLevelStr = args.getLastArgValue(Id: OPT_icf_eq);
1036 auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr)
1037 .Cases(CaseStrings: {"none", ""}, Value: ICFLevel::none)
1038 .Case(S: "safe", Value: ICFLevel::safe)
1039 .Case(S: "safe_thunks", Value: ICFLevel::safe_thunks)
1040 .Case(S: "all", Value: ICFLevel::all)
1041 .Default(Value: ICFLevel::unknown);
1042
1043 if ((icfLevel == ICFLevel::safe_thunks) && (config->arch() != AK_arm64)) {
1044 error(msg: "--icf=safe_thunks is only supported on arm64 targets");
1045 }
1046
1047 if (icfLevel == ICFLevel::unknown) {
1048 warn(msg: Twine("unknown --icf=OPTION `") + icfLevelStr +
1049 "', defaulting to `none'");
1050 icfLevel = ICFLevel::none;
1051 }
1052 return icfLevel;
1053}
1054
1055static ObjCStubsMode getObjCStubsMode(const ArgList &args) {
1056 const Arg *arg = args.getLastArg(Ids: OPT_objc_stubs_fast, Ids: OPT_objc_stubs_small);
1057 if (!arg)
1058 return ObjCStubsMode::fast;
1059
1060 if (arg->getOption().getID() == OPT_objc_stubs_small) {
1061 if (is_contained(Set: {AK_arm64e, AK_arm64}, Element: config->arch()))
1062 return ObjCStubsMode::small;
1063 else
1064 warn(msg: "-objc_stubs_small is not yet implemented, defaulting to "
1065 "-objc_stubs_fast");
1066 }
1067 return ObjCStubsMode::fast;
1068}
1069
1070static void warnIfDeprecatedOption(const Option &opt) {
1071 if (!opt.getGroup().isValid())
1072 return;
1073 if (opt.getGroup().getID() == OPT_grp_deprecated) {
1074 warn(msg: "Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
1075 warn(msg: opt.getHelpText());
1076 }
1077}
1078
1079static void warnIfUnimplementedOption(const Option &opt) {
1080 if (!opt.getGroup().isValid() || !opt.hasFlag(Val: DriverFlag::HelpHidden))
1081 return;
1082 switch (opt.getGroup().getID()) {
1083 case OPT_grp_deprecated:
1084 // warn about deprecated options elsewhere
1085 break;
1086 case OPT_grp_undocumented:
1087 warn(msg: "Option `" + opt.getPrefixedName() +
1088 "' is undocumented. Should lld implement it?");
1089 break;
1090 case OPT_grp_obsolete:
1091 warn(msg: "Option `" + opt.getPrefixedName() +
1092 "' is obsolete. Please modernize your usage.");
1093 break;
1094 case OPT_grp_ignored:
1095 warn(msg: "Option `" + opt.getPrefixedName() + "' is ignored.");
1096 break;
1097 case OPT_grp_ignored_silently:
1098 break;
1099 default:
1100 warn(msg: "Option `" + opt.getPrefixedName() +
1101 "' is not yet implemented. Stay tuned...");
1102 break;
1103 }
1104}
1105
1106static const char *getReproduceOption(InputArgList &args) {
1107 if (const Arg *arg = args.getLastArg(Ids: OPT_reproduce))
1108 return arg->getValue();
1109 return getenv(name: "LLD_REPRODUCE");
1110}
1111
1112// Parse options of the form "old;new".
1113static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
1114 unsigned id) {
1115 auto *arg = args.getLastArg(Ids: id);
1116 if (!arg)
1117 return {"", ""};
1118
1119 StringRef s = arg->getValue();
1120 std::pair<StringRef, StringRef> ret = s.split(Separator: ';');
1121 if (ret.second.empty())
1122 error(msg: arg->getSpelling() + " expects 'old;new' format, but got " + s);
1123 return ret;
1124}
1125
1126// Parse options of the form "old;new[;extra]".
1127static std::tuple<StringRef, StringRef, StringRef>
1128getOldNewOptionsExtra(opt::InputArgList &args, unsigned id) {
1129 auto [oldDir, second] = getOldNewOptions(args, id);
1130 auto [newDir, extraDir] = second.split(Separator: ';');
1131 return {oldDir, newDir, extraDir};
1132}
1133
1134static void parseClangOption(StringRef opt, const Twine &msg) {
1135 std::string err;
1136 raw_string_ostream os(err);
1137
1138 const char *argv[] = {"lld", opt.data()};
1139 if (cl::parseCommandLineOptions(Context&: config->libraryOptions, ToolOptions: getToolOptions(),
1140 Args: argv, Overview: "", Errs: &os))
1141 return;
1142 error(msg: msg + ": " + StringRef(err).trim());
1143}
1144
1145static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
1146 const Arg *arg = args.getLastArg(Ids: id);
1147 if (!arg)
1148 return 0;
1149
1150 if (config->outputType != MH_DYLIB) {
1151 error(msg: arg->getAsString(Args: args) + ": only valid with -dylib");
1152 return 0;
1153 }
1154
1155 PackedVersion version;
1156 if (!version.parse32(Str: arg->getValue())) {
1157 error(msg: arg->getAsString(Args: args) + ": malformed version");
1158 return 0;
1159 }
1160
1161 return version.rawValue();
1162}
1163
1164static uint32_t parseProtection(StringRef protStr) {
1165 uint32_t prot = 0;
1166 for (char c : protStr) {
1167 switch (c) {
1168 case 'r':
1169 prot |= VM_PROT_READ;
1170 break;
1171 case 'w':
1172 prot |= VM_PROT_WRITE;
1173 break;
1174 case 'x':
1175 prot |= VM_PROT_EXECUTE;
1176 break;
1177 case '-':
1178 break;
1179 default:
1180 error(msg: "unknown -segprot letter '" + Twine(c) + "' in " + protStr);
1181 return 0;
1182 }
1183 }
1184 return prot;
1185}
1186
1187static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) {
1188 std::vector<SectionAlign> sectAligns;
1189 for (const Arg *arg : args.filtered(Ids: OPT_sectalign)) {
1190 StringRef segName = arg->getValue(N: 0);
1191 StringRef sectName = arg->getValue(N: 1);
1192 StringRef alignStr = arg->getValue(N: 2);
1193 alignStr.consume_front_insensitive(Prefix: "0x");
1194 uint32_t align;
1195 if (alignStr.getAsInteger(Radix: 16, Result&: align)) {
1196 error(msg: "-sectalign: failed to parse '" + StringRef(arg->getValue(N: 2)) +
1197 "' as number");
1198 continue;
1199 }
1200 if (!isPowerOf2_32(Value: align)) {
1201 error(msg: "-sectalign: '" + StringRef(arg->getValue(N: 2)) +
1202 "' (in base 16) not a power of two");
1203 continue;
1204 }
1205 sectAligns.push_back(x: {.segName: segName, .sectName: sectName, .align: align});
1206 }
1207 return sectAligns;
1208}
1209
1210PlatformType macho::removeSimulator(PlatformType platform) {
1211 switch (platform) {
1212 case PLATFORM_IOSSIMULATOR:
1213 return PLATFORM_IOS;
1214 case PLATFORM_TVOSSIMULATOR:
1215 return PLATFORM_TVOS;
1216 case PLATFORM_WATCHOSSIMULATOR:
1217 return PLATFORM_WATCHOS;
1218 case PLATFORM_XROS_SIMULATOR:
1219 return PLATFORM_XROS;
1220 default:
1221 return platform;
1222 }
1223}
1224
1225static bool supportsNoPie() {
1226 return !(config->arch() == AK_arm64 || config->arch() == AK_arm64e ||
1227 config->arch() == AK_arm64_32);
1228}
1229
1230static bool shouldAdhocSignByDefault(Architecture arch, PlatformType platform) {
1231 if (arch != AK_arm64 && arch != AK_arm64e)
1232 return false;
1233
1234 return platform == PLATFORM_MACOS || platform == PLATFORM_IOSSIMULATOR ||
1235 platform == PLATFORM_TVOSSIMULATOR ||
1236 platform == PLATFORM_WATCHOSSIMULATOR ||
1237 platform == PLATFORM_XROS_SIMULATOR;
1238}
1239
1240template <std::size_t N>
1241using MinVersions = std::array<std::pair<PlatformType, VersionTuple>, N>;
1242
1243/// Returns true if the platform is greater than the min version.
1244/// Returns false if the platform does not exist.
1245template <std::size_t N>
1246static bool greaterEqMinVersion(const MinVersions<N> &minVersions,
1247 bool ignoreSimulator) {
1248 PlatformType platform = config->platformInfo.target.Platform;
1249 if (ignoreSimulator)
1250 platform = removeSimulator(platform);
1251 auto it = llvm::find_if(minVersions,
1252 [&](const auto &p) { return p.first == platform; });
1253 if (it != minVersions.end())
1254 if (config->platformInfo.target.MinDeployment >= it->second)
1255 return true;
1256 return false;
1257}
1258
1259static bool dataConstDefault(const InputArgList &args) {
1260 static const MinVersions<6> minVersion = {._M_elems: {
1261 {PLATFORM_MACOS, VersionTuple(10, 15)},
1262 {PLATFORM_IOS, VersionTuple(13, 0)},
1263 {PLATFORM_TVOS, VersionTuple(13, 0)},
1264 {PLATFORM_WATCHOS, VersionTuple(6, 0)},
1265 {PLATFORM_XROS, VersionTuple(1, 0)},
1266 {PLATFORM_BRIDGEOS, VersionTuple(4, 0)},
1267 }};
1268 if (!greaterEqMinVersion(minVersions: minVersion, ignoreSimulator: true))
1269 return false;
1270
1271 switch (config->outputType) {
1272 case MH_EXECUTE:
1273 return !(args.hasArg(Ids: OPT_no_pie) && supportsNoPie());
1274 case MH_BUNDLE:
1275 // FIXME: return false when -final_name ...
1276 // has prefix "/System/Library/UserEventPlugins/"
1277 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd"
1278 return true;
1279 case MH_DYLIB:
1280 return true;
1281 case MH_OBJECT:
1282 return false;
1283 default:
1284 llvm_unreachable(
1285 "unsupported output type for determining data-const default");
1286 }
1287 return false;
1288}
1289
1290static bool shouldEmitChainedFixups(const InputArgList &args) {
1291 const Arg *arg = args.getLastArg(Ids: OPT_fixup_chains, Ids: OPT_no_fixup_chains);
1292 if (arg && arg->getOption().matches(ID: OPT_no_fixup_chains)) {
1293 if (config->arch() != AK_arm64e)
1294 return false;
1295 // Authenticated pointers can only be encoded as chained fixups.
1296 warn(msg: "bind opcodes are no longer supported with arm64e, switching to "
1297 "chained fixups");
1298 config->noFixupChainsIgnored = true;
1299 return true;
1300 }
1301
1302 bool requested = arg && arg->getOption().matches(ID: OPT_fixup_chains);
1303 if (!config->isPic) {
1304 if (requested)
1305 error(msg: "-fixup_chains is incompatible with -no_pie");
1306
1307 return false;
1308 }
1309
1310 if (!is_contained(Set: {AK_x86_64, AK_x86_64h, AK_arm64, AK_arm64e},
1311 Element: config->arch())) {
1312 if (requested)
1313 error(msg: "-fixup_chains is only supported on x86_64 and arm64 targets");
1314
1315 return false;
1316 }
1317
1318 if (args.hasArg(Ids: OPT_preload)) {
1319 if (requested)
1320 error(msg: "-fixup_chains is incompatible with -preload");
1321
1322 return false;
1323 }
1324
1325 if (requested || config->arch() == AK_arm64e)
1326 return true;
1327
1328 static const MinVersions<9> minVersion = {._M_elems: {
1329 {PLATFORM_IOS, VersionTuple(13, 4)},
1330 {PLATFORM_IOSSIMULATOR, VersionTuple(16, 0)},
1331 {PLATFORM_MACOS, VersionTuple(13, 0)},
1332 {PLATFORM_TVOS, VersionTuple(14, 0)},
1333 {PLATFORM_TVOSSIMULATOR, VersionTuple(15, 0)},
1334 {PLATFORM_WATCHOS, VersionTuple(7, 0)},
1335 {PLATFORM_WATCHOSSIMULATOR, VersionTuple(8, 0)},
1336 {PLATFORM_XROS, VersionTuple(1, 0)},
1337 {PLATFORM_XROS_SIMULATOR, VersionTuple(1, 0)},
1338 }};
1339 return greaterEqMinVersion(minVersions: minVersion, ignoreSimulator: false);
1340}
1341
1342static uint16_t getChainedFixupsPointerFormat() {
1343 // FIXME: Use DYLD_CHAINED_PTR_64_OFFSET on newer OS versions.
1344 if (config->arch() != AK_arm64e)
1345 return DYLD_CHAINED_PTR_64;
1346 // Like ld64, keep the original format if -no_fixup_chains was given.
1347 if (config->noFixupChainsIgnored)
1348 return DYLD_CHAINED_PTR_ARM64E;
1349 // The releases from which arm64e uses DYLD_CHAINED_PTR_ARM64E_USERLAND24.
1350 static const MinVersions<7> minVersion = {._M_elems: {
1351 {PLATFORM_MACOS, VersionTuple(12, 0)},
1352 {PLATFORM_IOS, VersionTuple(15, 0)},
1353 {PLATFORM_MACCATALYST, VersionTuple(15, 0)},
1354 {PLATFORM_TVOS, VersionTuple(15, 0)},
1355 {PLATFORM_WATCHOS, VersionTuple(8, 0)},
1356 {PLATFORM_XROS, VersionTuple(1, 0)},
1357 {PLATFORM_DRIVERKIT, VersionTuple(21, 0)},
1358 }};
1359 return greaterEqMinVersion(minVersions: minVersion, ignoreSimulator: true)
1360 ? DYLD_CHAINED_PTR_ARM64E_USERLAND24
1361 : DYLD_CHAINED_PTR_ARM64E;
1362}
1363
1364static bool shouldEmitRelativeMethodLists(const InputArgList &args) {
1365 if (const Arg *arg = args.getLastArg(Ids: OPT_objc_relative_method_lists,
1366 Ids: OPT_no_objc_relative_method_lists))
1367 return arg->getOption().matches(ID: OPT_objc_relative_method_lists);
1368
1369 // If no flag is specified, enable this on newer versions by default.
1370 // The min versions is taken from
1371 // ld64(https://github.com/apple-oss-distributions/ld64/blob/47f477cb721755419018f7530038b272e9d0cdea/src/ld/ld.hpp#L310)
1372 // to mimic to operation of ld64
1373 // [here](https://github.com/apple-oss-distributions/ld64/blob/47f477cb721755419018f7530038b272e9d0cdea/src/ld/Options.cpp#L6085-L6101)
1374 static const MinVersions<6> minVersion = {._M_elems: {
1375 {PLATFORM_MACOS, VersionTuple(10, 16)},
1376 {PLATFORM_IOS, VersionTuple(14, 0)},
1377 {PLATFORM_WATCHOS, VersionTuple(7, 0)},
1378 {PLATFORM_TVOS, VersionTuple(14, 0)},
1379 {PLATFORM_BRIDGEOS, VersionTuple(5, 0)},
1380 {PLATFORM_XROS, VersionTuple(1, 0)},
1381 }};
1382 return greaterEqMinVersion(minVersions: minVersion, ignoreSimulator: true);
1383}
1384
1385void SymbolPatterns::clear() {
1386 literals.clear();
1387 globs.clear();
1388}
1389
1390void SymbolPatterns::insert(StringRef symbolName) {
1391 Expected<GlobPattern> pattern = GlobPattern::create(Pat: symbolName);
1392 if (!pattern) {
1393 error(msg: "invalid symbol-name pattern: " + symbolName + ": " +
1394 toString(E: pattern.takeError()));
1395 return;
1396 }
1397 // A pattern that denotes a single string is kept as a literal: literals are
1398 // matched by hash lookup, and only literals seed the force-load of lazy
1399 // archive members below.
1400 if (std::optional<std::string> literal = pattern->asLiteral()) {
1401 literals.insert(X: CachedHashStringRef(saver().save(S: *literal)));
1402 return;
1403 }
1404 globs.emplace_back(args: std::move(*pattern));
1405}
1406
1407bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
1408 return literals.contains(key: CachedHashStringRef(symbolName));
1409}
1410
1411bool SymbolPatterns::matchGlob(StringRef symbolName) const {
1412 for (const GlobPattern &glob : globs)
1413 if (glob.match(S: symbolName))
1414 return true;
1415 return false;
1416}
1417
1418bool SymbolPatterns::match(StringRef symbolName) const {
1419 return matchLiteral(symbolName) || matchGlob(symbolName);
1420}
1421
1422static void parseSymbolPatternsFile(const Arg *arg,
1423 SymbolPatterns &symbolPatterns) {
1424 StringRef path = arg->getValue();
1425 std::optional<MemoryBufferRef> buffer = readFile(path);
1426 if (!buffer) {
1427 error(msg: "Could not read symbol file: " + path);
1428 return;
1429 }
1430 MemoryBufferRef mbref = *buffer;
1431 for (StringRef line : args::getLines(mb: mbref)) {
1432 line = line.take_until(F: [](char c) { return c == '#'; }).trim();
1433 if (!line.empty())
1434 symbolPatterns.insert(symbolName: line);
1435 }
1436}
1437
1438static void handleSymbolPatterns(InputArgList &args,
1439 SymbolPatterns &symbolPatterns,
1440 unsigned singleOptionCode,
1441 unsigned listFileOptionCode) {
1442 for (const Arg *arg : args.filtered(Ids: singleOptionCode))
1443 symbolPatterns.insert(symbolName: arg->getValue());
1444 for (const Arg *arg : args.filtered(Ids: listFileOptionCode))
1445 parseSymbolPatternsFile(arg, symbolPatterns);
1446}
1447
1448static void createFiles(const InputArgList &args) {
1449 TimeTraceScope timeScope("Load input files");
1450 // This loop should be reserved for options whose exact ordering matters.
1451 // Other options should be handled via filtered() and/or getLastArg().
1452 bool isLazy = false;
1453 // If we've processed an opening --start-lib, without a matching --end-lib
1454 bool inLib = false;
1455 DeferredFiles deferredFiles;
1456
1457 for (const Arg *arg : args) {
1458 const Option &opt = arg->getOption();
1459 warnIfDeprecatedOption(opt);
1460 warnIfUnimplementedOption(opt);
1461
1462 switch (opt.getID()) {
1463 case OPT_INPUT:
1464 deferFile(path: rerootPath(path: arg->getValue()), isLazy, deferred&: deferredFiles);
1465 break;
1466 case OPT_needed_library:
1467 deferFile(path: rerootPath(path: arg->getValue()), /*isLazy=*/false, deferred&: deferredFiles,
1468 loadType: LoadType::CommandLine, /*isNeeded=*/true);
1469 break;
1470 case OPT_reexport_library:
1471 deferFile(path: rerootPath(path: arg->getValue()), /*isLazy=*/false, deferred&: deferredFiles,
1472 loadType: LoadType::CommandLine, /*isNeeded=*/false, /*isWeak=*/false,
1473 /*isReexport=*/true);
1474 break;
1475 case OPT_weak_library:
1476 deferFile(path: rerootPath(path: arg->getValue()), /*isLazy=*/false, deferred&: deferredFiles,
1477 loadType: LoadType::CommandLine, /*isNeeded=*/false, /*isWeak=*/true);
1478 break;
1479 case OPT_filelist:
1480 addFileList(path: arg->getValue(), isLazy, deferredFiles);
1481 break;
1482 case OPT_force_load:
1483 deferFile(path: rerootPath(path: arg->getValue()), /*isLazy=*/false, deferred&: deferredFiles,
1484 loadType: LoadType::CommandLineForce);
1485 break;
1486 case OPT_load_hidden:
1487 deferFile(path: rerootPath(path: arg->getValue()), /*isLazy=*/false, deferred&: deferredFiles,
1488 loadType: LoadType::CommandLine, /*isNeeded=*/false, /*isWeak=*/false,
1489 /*isReexport=*/false, /*isHidden=*/true);
1490 break;
1491 case OPT_l:
1492 case OPT_needed_l:
1493 case OPT_reexport_l:
1494 case OPT_weak_l:
1495 case OPT_hidden_l:
1496 addLibrary(name: arg->getValue(), isNeeded: opt.getID() == OPT_needed_l,
1497 isWeak: opt.getID() == OPT_weak_l, isReexport: opt.getID() == OPT_reexport_l,
1498 isHidden: opt.getID() == OPT_hidden_l,
1499 /*isExplicit=*/true, loadType: LoadType::CommandLine, deferred&: deferredFiles);
1500 break;
1501 case OPT_framework:
1502 case OPT_needed_framework:
1503 case OPT_reexport_framework:
1504 case OPT_weak_framework:
1505 addFramework(name: arg->getValue(), isNeeded: opt.getID() == OPT_needed_framework,
1506 isWeak: opt.getID() == OPT_weak_framework,
1507 isReexport: opt.getID() == OPT_reexport_framework, /*isExplicit=*/true,
1508 loadType: LoadType::CommandLine, deferred&: deferredFiles);
1509 break;
1510 case OPT_start_lib:
1511 if (inLib)
1512 error(msg: "nested --start-lib");
1513 inLib = true;
1514 if (!config->allLoad)
1515 isLazy = true;
1516 break;
1517 case OPT_end_lib:
1518 if (!inLib)
1519 error(msg: "stray --end-lib");
1520 inLib = false;
1521 isLazy = false;
1522 break;
1523 default:
1524 break;
1525 }
1526 }
1527
1528#if LLVM_ENABLE_THREADS
1529 if (config->readWorkers) {
1530 multiThreadedPageIn(deferred: deferredFiles);
1531
1532 DeferredFiles archiveContents;
1533 for (auto &file : deferredFiles) {
1534 if (loadedObjectFrameworks.contains(V: file.path))
1535 continue;
1536
1537 auto inputFile = processFile(buffer: file.buffer, archiveContents: &archiveContents, path: file.path,
1538 loadType: file.loadType, isLazy: file.isLazy, isExplicit: file.isExplicit,
1539 /*isBundleLoader=*/false, isForceHidden: file.isHidden);
1540 applyDylibMetadata(file: inputFile, isNeeded: file.isNeeded, isWeak: file.isWeak,
1541 isReexport: file.isReexport);
1542 checkAndCacheFramework(file: inputFile, path: file.path);
1543
1544 if (ArchiveFile *archive = dyn_cast<ArchiveFile>(Val: inputFile))
1545 archive->addLazySymbols();
1546 }
1547
1548 if (!archiveContents.empty())
1549 multiThreadedPageIn(deferred: archiveContents);
1550
1551 pageInQueue.stopAllWork = true;
1552 }
1553#endif
1554}
1555
1556static void gatherInputSections() {
1557 TimeTraceScope timeScope("Gathering input sections");
1558 for (const InputFile *file : inputFiles) {
1559 for (const Section *section : file->sections) {
1560 // Compact unwind entries require special handling elsewhere. (In
1561 // contrast, EH frames are handled like regular ConcatInputSections.)
1562 if (section->name == section_names::compactUnwind)
1563 continue;
1564 // Addrsig sections contain metadata only needed at link time.
1565 if (section->name == section_names::addrSig)
1566 continue;
1567 for (const Subsection &subsection : section->subsections)
1568 addInputSection(inputSection: subsection.isec);
1569 }
1570 if (!file->objCImageInfo.empty())
1571 in.objCImageInfo->addFile(file);
1572 }
1573}
1574
1575static void codegenDataGenerate() {
1576 TimeTraceScope timeScope("Generating codegen data");
1577
1578 OutlinedHashTreeRecord globalOutlineRecord;
1579 StableFunctionMapRecord globalMergeRecord;
1580 for (ConcatInputSection *isec : inputSections) {
1581 if (isec->getSegName() != segment_names::data)
1582 continue;
1583 if (isec->getName() == section_names::outlinedHashTree) {
1584 // Read outlined hash tree from each section.
1585 OutlinedHashTreeRecord localOutlineRecord;
1586 // Use a pointer to allow modification by the function.
1587 auto *data = isec->data.data();
1588 localOutlineRecord.deserialize(Ptr&: data);
1589
1590 // Merge it to the global hash tree.
1591 globalOutlineRecord.merge(Other: localOutlineRecord);
1592 }
1593 if (isec->getName() == section_names::functionMap) {
1594 // Read stable functions from each section.
1595 StableFunctionMapRecord localMergeRecord;
1596 // Use a pointer to allow modification by the function.
1597 auto *data = isec->data.data();
1598 localMergeRecord.deserialize(Ptr&: data);
1599
1600 // Merge it to the global function map.
1601 globalMergeRecord.merge(Other: localMergeRecord);
1602 }
1603 }
1604
1605 globalMergeRecord.finalize(LibraryOptions: config->libraryOptions);
1606
1607 CodeGenDataWriter Writer;
1608 if (!globalOutlineRecord.empty())
1609 Writer.addRecord(Record&: globalOutlineRecord);
1610 if (!globalMergeRecord.empty())
1611 Writer.addRecord(Record&: globalMergeRecord);
1612
1613 std::error_code EC;
1614 auto fileName = config->codegenDataGeneratePath;
1615 assert(!fileName.empty());
1616 raw_fd_ostream Output(fileName, EC, sys::fs::OF_None);
1617 if (EC)
1618 error(msg: "fail to create " + fileName + ": " + EC.message());
1619
1620 if (auto E = Writer.write(OS&: Output))
1621 error(msg: "fail to write CGData: " + toString(E: std::move(E)));
1622}
1623
1624static void foldIdenticalLiterals() {
1625 TimeTraceScope timeScope("Fold identical literals");
1626 // We always create a cStringSection, regardless of whether dedupLiterals is
1627 // true. If it isn't, we simply create a non-deduplicating CStringSection.
1628 // Either way, we must unconditionally finalize it here.
1629 for (auto *sec : in.cStringSections)
1630 sec->finalizeContents();
1631 in.wordLiteralSection->finalizeContents();
1632}
1633
1634static void prepareObjCStubs() {
1635 std::string &data = *make<std::string>();
1636 llvm::raw_string_ostream os(data);
1637 // Iterate by index: resolving a class symbol may extract an archive member,
1638 // which appends new symbols, including more stubs, to the symbol table.
1639 for (size_t i = 0; i < symtab->getSymbols().size(); ++i) {
1640 Symbol *sym = symtab->getSymbols()[i];
1641 if (!isa<Undefined>(Val: sym))
1642 continue;
1643 if (ObjCStubsSection::isObjCMsgSendStubSymbol(sym)) {
1644 os << ObjCStubsSection::getMethname(sym) << '\0';
1645 continue;
1646 }
1647 if (!ObjCStubsSection::isObjCClassStubSymbol(sym) ||
1648 !target->supportsObjCClassStubs())
1649 continue;
1650 std::optional<ObjCStubsSection::ObjCClassStubNames> names =
1651 ObjCStubsSection::parseObjCClassStubSymbol(sym);
1652 if (!names)
1653 continue;
1654 os << names->selectorName << '\0';
1655
1656 Symbol *classSym = symtab->find(name: names->classSymbolName);
1657 bool needsEarlyUndefinedClassRef =
1658 !classSym || isa<LazyArchive>(Val: classSym) || isa<LazyObject>(Val: classSym);
1659 // Missing symbols need a placeholder for diagnostics, and lazy symbols need
1660 // extraction before markLive(). Do not call addUndefined() for DylibSymbol
1661 // here: live stubs reference them later in ObjCStubsSection::addEntry(),
1662 // and adding them here would mark real dylib symbols referenced before dead
1663 // stripping.
1664 if (needsEarlyUndefinedClassRef)
1665 classSym = symtab->addUndefined(name: names->classSymbolName, /*file=*/nullptr,
1666 /*isWeakRef=*/false);
1667 in.objcStubs->recordClassSymbol(stubSym: sym, classSym);
1668 }
1669
1670 if (data.empty())
1671 return;
1672
1673 const auto *buf = reinterpret_cast<const uint8_t *>(data.c_str());
1674 Section &section = *make<Section>(/*file=*/args: nullptr, args: segment_names::text,
1675 args: section_names::objcMethname,
1676 args: S_CSTRING_LITERALS, /*addr=*/args: 0);
1677
1678 auto *isec =
1679 make<CStringInputSection>(args&: section, args: ArrayRef<uint8_t>{buf, data.size()},
1680 /*align=*/args: 1, /*dedupLiterals=*/args: true);
1681 isec->splitIntoPieces();
1682 for (auto &piece : isec->pieces)
1683 piece.live = true;
1684 section.subsections.push_back(x: {.offset: 0, .isec: isec});
1685 in.objcMethnameSection->addInput(isec);
1686 in.objcMethnameSection->isec->markLive(off: 0);
1687}
1688
1689static void referenceStubBinder() {
1690 bool needsStubHelper = config->outputType == MH_DYLIB ||
1691 config->outputType == MH_EXECUTE ||
1692 config->outputType == MH_BUNDLE;
1693 if (!needsStubHelper || !symtab->find(name: "dyld_stub_binder"))
1694 return;
1695
1696 // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here
1697 // adds a opportunistic reference to dyld_stub_binder if it happens to exist.
1698 // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This
1699 // isn't needed for correctness, but the presence of that symbol suppresses
1700 // "no symbols" diagnostics from `nm`.
1701 // StubHelperSection::setUp() adds a reference and errors out if
1702 // dyld_stub_binder doesn't exist in case it is actually needed.
1703 symtab->addUndefined(name: "dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/isWeakRef: false);
1704}
1705
1706static void createAliases() {
1707 for (const auto &pair : config->aliasedSymbols) {
1708 if (const auto &sym = symtab->find(name: pair.first)) {
1709 if (const auto &defined = dyn_cast<Defined>(Val: sym)) {
1710 symtab->aliasDefined(src: defined, target: pair.second, newFile: defined->getFile())
1711 ->noDeadStrip = true;
1712 } else {
1713 error(msg: "TODO: support aliasing to symbols of kind " +
1714 Twine(sym->kind()));
1715 }
1716 } else {
1717 warn(msg: "undefined base symbol '" + pair.first + "' for alias '" +
1718 pair.second + "'\n");
1719 }
1720 }
1721
1722 for (const InputFile *file : inputFiles) {
1723 if (auto *objFile = dyn_cast<ObjFile>(Val: file)) {
1724 for (const AliasSymbol *alias : objFile->aliases) {
1725 if (const auto &aliased = symtab->find(name: alias->getAliasedName())) {
1726 if (const auto &defined = dyn_cast<Defined>(Val: aliased)) {
1727 symtab->aliasDefined(src: defined, target: alias->getName(), newFile: alias->getFile(),
1728 makePrivateExtern: alias->privateExtern);
1729 } else {
1730 // Common, dylib, and undefined symbols are all valid alias
1731 // referents (undefineds can become valid Defined symbols later on
1732 // in the link.)
1733 error(msg: "TODO: support aliasing to symbols of kind " +
1734 Twine(aliased->kind()));
1735 }
1736 } else {
1737 // This shouldn't happen since MC generates undefined symbols to
1738 // represent the alias referents. Thus we fatal() instead of just
1739 // warning here.
1740 fatal(msg: "unable to find alias referent " + alias->getAliasedName() +
1741 " for " + alias->getName());
1742 }
1743 }
1744 }
1745 }
1746}
1747
1748static void handleExplicitExports() {
1749 static constexpr int kMaxWarnings = 3;
1750 if (config->hasExplicitExports) {
1751 std::atomic<uint64_t> warningsCount{0};
1752 parallelForEach(R: symtab->getSymbols(), Fn: [&warningsCount](Symbol *sym) {
1753 if (auto *defined = dyn_cast<Defined>(Val: sym)) {
1754 if (config->exportedSymbols.match(symbolName: sym->getName())) {
1755 if (defined->privateExtern) {
1756 if (defined->weakDefCanBeHidden) {
1757 // weak_def_can_be_hidden symbols behave similarly to
1758 // private_extern symbols in most cases, except for when
1759 // it is explicitly exported.
1760 // The former can be exported but the latter cannot.
1761 defined->privateExtern = false;
1762 } else {
1763 // Only print the first 3 warnings verbosely, and
1764 // shorten the rest to avoid crowding logs.
1765 if (warningsCount.fetch_add(i: 1, m: std::memory_order_relaxed) <
1766 kMaxWarnings)
1767 warn(msg: "cannot export hidden symbol " + toString(*defined) +
1768 "\n>>> defined in " + toString(file: defined->getFile()));
1769 }
1770 }
1771 } else {
1772 defined->privateExtern = true;
1773 }
1774 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: sym)) {
1775 dysym->shouldReexport = config->exportedSymbols.match(symbolName: sym->getName());
1776 }
1777 });
1778 if (warningsCount > kMaxWarnings)
1779 warn(msg: "<... " + Twine(warningsCount - kMaxWarnings) +
1780 " more similar warnings...>");
1781 } else if (!config->unexportedSymbols.empty()) {
1782 parallelForEach(R: symtab->getSymbols(), Fn: [](Symbol *sym) {
1783 if (auto *defined = dyn_cast<Defined>(Val: sym))
1784 if (config->unexportedSymbols.match(symbolName: defined->getName()))
1785 defined->privateExtern = true;
1786 });
1787 }
1788}
1789
1790static void eraseInitializerSymbols() {
1791 for (ConcatInputSection *isec : in.initOffsets->inputs())
1792 for (Defined *sym : isec->symbols)
1793 sym->used = false;
1794}
1795
1796static SmallVector<StringRef, 0> getRuntimePaths(opt::InputArgList &args) {
1797 SmallVector<StringRef, 0> vals;
1798 DenseSet<StringRef> seen;
1799 for (const Arg *arg : args.filtered(Ids: OPT_rpath)) {
1800 StringRef val = arg->getValue();
1801 if (seen.insert(V: val).second)
1802 vals.push_back(Elt: val);
1803 else if (config->warnDuplicateRpath)
1804 warn(msg: "duplicate -rpath '" + val + "' ignored [--warn-duplicate-rpath]");
1805 }
1806 return vals;
1807}
1808
1809static SmallVector<StringRef, 0> getAllowableClients(opt::InputArgList &args) {
1810 SmallVector<StringRef, 0> vals;
1811 DenseSet<StringRef> seen;
1812 for (const Arg *arg : args.filtered(Ids: OPT_allowable_client)) {
1813 StringRef val = arg->getValue();
1814 if (seen.insert(V: val).second)
1815 vals.push_back(Elt: val);
1816 }
1817 return vals;
1818}
1819
1820static void computeColdness() {
1821 TimeTraceScope timeScope("Compute coldness");
1822 for (InputSection *isec : inputSections) {
1823 if (!isCodeSection(isec))
1824 continue;
1825 isec->isCold =
1826 llvm::any_of(Range&: isec->symbols, P: [](Defined *sym) { return sym->isCold(); });
1827 }
1828}
1829
1830namespace lld {
1831namespace macho {
1832bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
1833 llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
1834 // This driver-specific context will be freed later by lldMain().
1835 auto *ctx = new CommonLinkerContext;
1836
1837 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
1838 ctx->e.cleanupCallback = []() {
1839 resolvedFrameworks.clear();
1840 resolvedLibraries.clear();
1841 cachedReads.clear();
1842 concatOutputSections.clear();
1843 inputFiles.clear();
1844 inputSections.clear();
1845 inputSectionsOrder = 0;
1846 loadedArchives.clear();
1847 loadedObjectFrameworks.clear();
1848 missingAutolinkWarnings.clear();
1849 syntheticSections.clear();
1850 thunkMap.clear();
1851 unprocessedLCLinkerOptions.clear();
1852 ObjCSelRefsHelper::cleanup();
1853
1854 firstTLVDataSection = nullptr;
1855 tar = nullptr;
1856 in = InStruct();
1857
1858 resetLoadedDylibs();
1859 resetOutputSegments();
1860 resetWriter();
1861 InputFile::resetIdCount();
1862
1863 objc::doCleanup();
1864 };
1865
1866 ctx->e.logName = args::getFilenameWithoutExe(path: argsArr[0]);
1867
1868 MachOOptTable parser;
1869 InputArgList args = parser.parse(ctx&: *ctx, argv: argsArr.slice(N: 1));
1870
1871 ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now "
1872 "(use --error-limit=0 to see all errors)";
1873 ctx->e.errorLimit = args::getInteger(args, key: OPT_error_limit_eq, Default: 20);
1874 ctx->e.verbose = args.hasArg(Ids: OPT_verbose);
1875
1876 if (args.hasArg(Ids: OPT_help_hidden)) {
1877 parser.printHelp(ctx&: *ctx, argv0: argsArr[0], /*showHidden=*/true);
1878 return true;
1879 }
1880 if (args.hasArg(Ids: OPT_help)) {
1881 parser.printHelp(ctx&: *ctx, argv0: argsArr[0], /*showHidden=*/false);
1882 return true;
1883 }
1884 if (args.hasArg(Ids: OPT_version)) {
1885 message(msg: getLLDVersion());
1886 return true;
1887 }
1888
1889 config = std::make_unique<Configuration>();
1890 symtab = std::make_unique<SymbolTable>();
1891 config->outputType = getOutputType(args);
1892 target = createTargetInfo(args);
1893 depTracker = std::make_unique<DependencyTracker>(
1894 args: args.getLastArgValue(Id: OPT_dependency_info));
1895
1896 config->ltoo = args::getInteger(args, key: OPT_lto_O, Default: 2);
1897 if (config->ltoo > 3)
1898 error(msg: "--lto-O: invalid optimization level: " + Twine(config->ltoo));
1899 unsigned ltoCgo =
1900 args::getInteger(args, key: OPT_lto_CGO, Default: args::getCGOptLevel(optLevelLTO: config->ltoo));
1901 if (auto level = CodeGenOpt::getLevel(OL: ltoCgo))
1902 config->ltoCgo = *level;
1903 else
1904 error(msg: "--lto-CGO: invalid codegen optimization level: " + Twine(ltoCgo));
1905
1906 if (errorCount())
1907 return false;
1908
1909 if (args.hasArg(Ids: OPT_pagezero_size)) {
1910 uint64_t pagezeroSize = args::getHex(args, key: OPT_pagezero_size, Default: 0);
1911
1912 // ld64 does something really weird. It attempts to realign the value to the
1913 // page size, but assumes the page size is 4K. This doesn't work with most
1914 // of Apple's ARM64 devices, which use a page size of 16K. This means that
1915 // it will first 4K align it by rounding down, then round up to 16K. This
1916 // probably only happened because no one using this arg with anything other
1917 // then 0, so no one checked if it did what is what it says it does.
1918
1919 // So we are not copying this weird behavior and doing the it in a logical
1920 // way, by always rounding down to page size.
1921 if (!isAligned(Lhs: Align(target->getPageSize()), SizeInBytes: pagezeroSize)) {
1922 pagezeroSize -= pagezeroSize % target->getPageSize();
1923 warn(msg: "__PAGEZERO size is not page aligned, rounding down to 0x" +
1924 Twine::utohexstr(Val: pagezeroSize));
1925 }
1926
1927 target->pageZeroSize = pagezeroSize;
1928 }
1929
1930 config->osoPrefix = args.getLastArgValue(Id: OPT_oso_prefix);
1931 if (!config->osoPrefix.empty()) {
1932 // The max path length is 4096, in theory. However that seems quite long
1933 // and seems unlikely that any one would want to strip everything from the
1934 // path. Hence we've picked a reasonably large number here.
1935 SmallString<1024> expanded;
1936 // Expand "." into the current working directory.
1937 if (config->osoPrefix == "." && !fs::current_path(result&: expanded)) {
1938 // Note: LD64 expands "." to be `<current_dir>/
1939 // (ie., it has a slash suffix) whereas current_path() doesn't.
1940 // So we have to append '/' to be consistent because this is
1941 // meaningful for our text based stripping.
1942 expanded += sys::path::get_separator();
1943 } else {
1944 expanded = config->osoPrefix;
1945 }
1946 config->osoPrefix = saver().save(S: expanded.str());
1947 }
1948
1949 bool pie = args.hasFlag(Pos: OPT_pie, Neg: OPT_no_pie, Default: true);
1950 if (!supportsNoPie() && !pie) {
1951 warn(msg: "-no_pie ignored for arm64");
1952 pie = true;
1953 }
1954
1955 config->isPic = config->outputType == MH_DYLIB ||
1956 config->outputType == MH_BUNDLE ||
1957 (config->outputType == MH_EXECUTE && pie);
1958
1959 // Must be set before any InputSections and Symbols are created.
1960 config->deadStrip = args.hasArg(Ids: OPT_dead_strip);
1961 config->interposable = args.hasArg(Ids: OPT_interposable);
1962
1963 config->systemLibraryRoots = getSystemLibraryRoots(args);
1964 if (const char *path = getReproduceOption(args)) {
1965 // Note that --reproduce is a debug option so you can ignore it
1966 // if you are trying to understand the whole picture of the code.
1967 Expected<std::unique_ptr<TarWriter>> errOrWriter =
1968 TarWriter::create(OutputPath: path, BaseDir: path::stem(path));
1969 if (errOrWriter) {
1970 tar = std::move(*errOrWriter);
1971 tar->append(Path: "response.txt", Data: createResponseFile(args));
1972 tar->append(Path: "version.txt", Data: getLLDVersion() + "\n");
1973 } else {
1974 error(msg: "--reproduce: " + toString(E: errOrWriter.takeError()));
1975 }
1976 }
1977
1978 if (auto *arg = args.getLastArg(Ids: OPT_read_workers)) {
1979#if LLVM_ENABLE_THREADS
1980 StringRef v(arg->getValue());
1981 unsigned workers = 0;
1982 if (!llvm::to_integer(S: v, Num&: workers, Base: 0))
1983 error(msg: arg->getSpelling() +
1984 ": expected a non-negative integer, but got '" + arg->getValue() +
1985 "'");
1986 config->readWorkers = workers;
1987#else
1988 warn(arg->getSpelling() +
1989 ": option unavailable because lld was not built with thread support");
1990#endif
1991 }
1992 if (auto *arg = args.getLastArg(Ids: OPT_threads_eq)) {
1993 StringRef v(arg->getValue());
1994 unsigned threads = 0;
1995 if (!llvm::to_integer(S: v, Num&: threads, Base: 0) || threads == 0)
1996 error(msg: arg->getSpelling() + ": expected a positive integer, but got '" +
1997 arg->getValue() + "'");
1998 parallel::strategy = hardware_concurrency(ThreadCount: threads);
1999 config->thinLTOJobs = v;
2000 }
2001 if (auto *arg = args.getLastArg(Ids: OPT_thinlto_jobs_eq))
2002 config->thinLTOJobs = arg->getValue();
2003 if (!get_threadpool_strategy(Num: config->thinLTOJobs))
2004 error(msg: "--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
2005
2006 for (const Arg *arg : args.filtered(Ids: OPT_u)) {
2007 config->explicitUndefineds.push_back(x: symtab->addUndefined(
2008 name: arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
2009 }
2010
2011 for (const Arg *arg : args.filtered(Ids: OPT_U))
2012 config->explicitDynamicLookups.insert(key: arg->getValue());
2013
2014 config->mapFile = args.getLastArgValue(Id: OPT_map);
2015 config->optimize = args::getInteger(args, key: OPT_O, Default: 1);
2016 config->outputFile = args.getLastArgValue(Id: OPT_o, Default: "a.out");
2017 config->finalOutput =
2018 args.getLastArgValue(Id: OPT_final_output, Default: config->outputFile);
2019 config->astPaths = args.getAllArgValues(Id: OPT_add_ast_path);
2020 config->headerPad = args::getHex(args, key: OPT_headerpad, /*Default=*/32);
2021 config->headerPadMaxInstallNames =
2022 args.hasArg(Ids: OPT_headerpad_max_install_names);
2023 config->printDylibSearch =
2024 args.hasArg(Ids: OPT_print_dylib_search) || getenv(name: "RC_TRACE_DYLIB_SEARCHING");
2025 config->printEachFile = args.hasArg(Ids: OPT_t);
2026 config->printWhyLoad = args.hasArg(Ids: OPT_why_load);
2027 config->omitDebugInfo = args.hasArg(Ids: OPT_S);
2028 config->errorForArchMismatch = args.hasArg(Ids: OPT_arch_errors_fatal);
2029 if (const Arg *arg = args.getLastArg(Ids: OPT_bundle_loader)) {
2030 if (config->outputType != MH_BUNDLE)
2031 error(msg: "-bundle_loader can only be used with MachO bundle output");
2032 addFile(path: arg->getValue(), loadType: LoadType::CommandLine, /*isLazy=*/false,
2033 /*isExplicit=*/false, /*isBundleLoader=*/true);
2034 }
2035 for (auto *arg : args.filtered(Ids: OPT_dyld_env)) {
2036 StringRef envPair(arg->getValue());
2037 if (!envPair.contains(C: '='))
2038 error(msg: "-dyld_env's argument is malformed. Expected "
2039 "-dyld_env <ENV_VAR>=<VALUE>, got `" +
2040 envPair + "`");
2041 config->dyldEnvs.push_back(x: envPair);
2042 }
2043 if (!config->dyldEnvs.empty() && config->outputType != MH_EXECUTE)
2044 error(msg: "-dyld_env can only be used when creating executable output");
2045
2046 if (const Arg *arg = args.getLastArg(Ids: OPT_umbrella)) {
2047 if (config->outputType != MH_DYLIB)
2048 warn(msg: "-umbrella used, but not creating dylib");
2049 config->umbrella = arg->getValue();
2050 }
2051 config->ltoObjPath = args.getLastArgValue(Id: OPT_object_path_lto);
2052 config->ltoNewPmPasses = args.getLastArgValue(Id: OPT_lto_newpm_passes);
2053 config->thinLTOCacheDir = args.getLastArgValue(Id: OPT_cache_path_lto);
2054 config->thinLTOCachePolicy = getLTOCachePolicy(args);
2055 config->thinLTOEmitImportsFiles = args.hasArg(Ids: OPT_thinlto_emit_imports_files);
2056 config->thinLTOEmitIndexFiles = args.hasArg(Ids: OPT_thinlto_emit_index_files) ||
2057 args.hasArg(Ids: OPT_thinlto_index_only) ||
2058 args.hasArg(Ids: OPT_thinlto_index_only_eq);
2059 config->thinLTOIndexOnly = args.hasArg(Ids: OPT_thinlto_index_only) ||
2060 args.hasArg(Ids: OPT_thinlto_index_only_eq);
2061 config->thinLTOIndexOnlyArg = args.getLastArgValue(Id: OPT_thinlto_index_only_eq);
2062 config->thinLTOObjectSuffixReplace =
2063 getOldNewOptions(args, id: OPT_thinlto_object_suffix_replace_eq);
2064 std::tie(args&: config->thinLTOPrefixReplaceOld, args&: config->thinLTOPrefixReplaceNew,
2065 args&: config->thinLTOPrefixReplaceNativeObject) =
2066 getOldNewOptionsExtra(args, id: OPT_thinlto_prefix_replace_eq);
2067 if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) {
2068 if (args.hasArg(Ids: OPT_thinlto_object_suffix_replace_eq))
2069 error(msg: "--thinlto-object-suffix-replace is not supported with "
2070 "--thinlto-emit-index-files");
2071 else if (args.hasArg(Ids: OPT_thinlto_prefix_replace_eq))
2072 error(msg: "--thinlto-prefix-replace is not supported with "
2073 "--thinlto-emit-index-files");
2074 }
2075 if (!config->thinLTOPrefixReplaceNativeObject.empty() &&
2076 config->thinLTOIndexOnlyArg.empty()) {
2077 error(msg: "--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
2078 "--thinlto-index-only=");
2079 }
2080 config->warnDuplicateRpath =
2081 args.hasFlag(Pos: OPT_warn_duplicate_rpath, Neg: OPT_no_warn_duplicate_rpath, Default: true);
2082 config->runtimePaths = getRuntimePaths(args);
2083 config->allowableClients = getAllowableClients(args);
2084 config->allLoad = args.hasFlag(Pos: OPT_all_load, Neg: OPT_noall_load, Default: false);
2085 config->archMultiple = args.hasArg(Ids: OPT_arch_multiple);
2086 config->applicationExtension = args.hasFlag(
2087 Pos: OPT_application_extension, Neg: OPT_no_application_extension, Default: false);
2088 config->exportDynamic = args.hasArg(Ids: OPT_export_dynamic);
2089 config->forceLoadObjC = args.hasArg(Ids: OPT_ObjC);
2090 config->forceLoadSwift = args.hasArg(Ids: OPT_force_load_swift_libs);
2091 config->deadStripDylibs = args.hasArg(Ids: OPT_dead_strip_dylibs);
2092 config->demangle = args.hasArg(Ids: OPT_demangle);
2093 config->implicitDylibs = !args.hasArg(Ids: OPT_no_implicit_dylibs);
2094 config->emitFunctionStarts =
2095 args.hasFlag(Pos: OPT_function_starts, Neg: OPT_no_function_starts, Default: true);
2096 config->emitDataInCodeInfo =
2097 args.hasFlag(Pos: OPT_data_in_code_info, Neg: OPT_no_data_in_code_info, Default: true);
2098 config->emitChainedFixups = shouldEmitChainedFixups(args);
2099 config->chainedFixupsPointerFormat = getChainedFixupsPointerFormat();
2100 config->emitInitOffsets =
2101 (config->emitChainedFixups && !config->noFixupChainsIgnored) ||
2102 args.hasArg(Ids: OPT_init_offsets);
2103 config->emitRelativeMethodLists = shouldEmitRelativeMethodLists(args);
2104 config->icfLevel = getICFLevel(args);
2105 config->keepICFStabs = args.hasArg(Ids: OPT_keep_icf_stabs);
2106 config->dedupStrings =
2107 args.hasFlag(Pos: OPT_deduplicate_strings, Neg: OPT_no_deduplicate_strings, Default: true);
2108 config->dedupSymbolStrings = !args.hasArg(Ids: OPT_no_deduplicate_symbol_strings);
2109 config->deadStripDuplicates = args.hasArg(Ids: OPT_dead_strip_duplicates);
2110 config->stripSwiftForceLoad =
2111 args.hasFlag(Pos: OPT_strip_swift_force_load, Neg: OPT_no_strip_swift_force_load,
2112 /*Default=*/false);
2113 config->warnDylibInstallName = args.hasFlag(
2114 Pos: OPT_warn_dylib_install_name, Neg: OPT_no_warn_dylib_install_name, Default: false);
2115 config->ignoreOptimizationHints = args.hasArg(Ids: OPT_ignore_optimization_hints);
2116 config->callGraphProfileSort = args.hasFlag(
2117 Pos: OPT_call_graph_profile_sort, Neg: OPT_no_call_graph_profile_sort, Default: true);
2118 config->printSymbolOrder = args.getLastArgValue(Id: OPT_print_symbol_order_eq);
2119 config->forceExactCpuSubtypeMatch =
2120 getenv(name: "LD_DYLIB_CPU_SUBTYPES_MUST_MATCH");
2121 config->objcStubsMode = getObjCStubsMode(args);
2122 config->ignoreAutoLink = args.hasArg(Ids: OPT_ignore_auto_link);
2123 for (const Arg *arg : args.filtered(Ids: OPT_ignore_auto_link_option))
2124 config->ignoreAutoLinkOptions.insert(key: arg->getValue());
2125 config->strictAutoLink = args.hasArg(Ids: OPT_strict_auto_link);
2126 config->ltoDebugPassManager = args.hasArg(Ids: OPT_lto_debug_pass_manager);
2127 config->emitLLVM = args.hasArg(Ids: OPT_lto_emit_llvm);
2128 config->codegenDataGeneratePath =
2129 args.getLastArgValue(Id: OPT_codegen_data_generate_path);
2130 config->csProfileGenerate = args.hasArg(Ids: OPT_cs_profile_generate);
2131 config->csProfilePath = args.getLastArgValue(Id: OPT_cs_profile_path);
2132 config->pgoWarnMismatch =
2133 args.hasFlag(Pos: OPT_pgo_warn_mismatch, Neg: OPT_no_pgo_warn_mismatch, Default: true);
2134 config->warnThinArchiveMissingMembers =
2135 args.hasFlag(Pos: OPT_warn_thin_archive_missing_members,
2136 Neg: OPT_no_warn_thin_archive_missing_members, Default: true);
2137 config->warnMissingSubsectionsViaSymbols =
2138 args.hasFlag(Pos: OPT_warn_missing_subsections_via_symbols,
2139 Neg: OPT_no_warn_missing_subsections_via_symbols, Default: false);
2140 config->generateUuid = !args.hasArg(Ids: OPT_no_uuid);
2141#ifdef NDEBUG
2142 config->disableVerify = true;
2143#else
2144 config->disableVerify = args.hasArg(OPT_disable_verify);
2145#endif
2146 config->separateCstringLiteralSections =
2147 args.hasFlag(Pos: OPT_separate_cstring_literal_sections,
2148 Neg: OPT_no_separate_cstring_literal_sections, Default: false);
2149 config->tailMergeStrings =
2150 args.hasFlag(Pos: OPT_tail_merge_strings, Neg: OPT_no_tail_merge_strings, Default: false);
2151 if (auto *arg = args.getLastArg(Ids: OPT_slop_scale_eq)) {
2152 StringRef v(arg->getValue());
2153 unsigned slop = 0;
2154 if (!llvm::to_integer(S: v, Num&: slop))
2155 error(msg: arg->getSpelling() +
2156 ": expected a non-negative integer, but got '" + v + "'");
2157 config->slopScale = slop;
2158 }
2159
2160 auto IncompatWithCGSort = [&](StringRef firstArgStr) {
2161 // Throw an error only if --call-graph-profile-sort is explicitly specified
2162 if (config->callGraphProfileSort)
2163 if (const Arg *arg = args.getLastArgNoClaim(Ids: OPT_call_graph_profile_sort))
2164 error(msg: firstArgStr + " is incompatible with " + arg->getSpelling());
2165 };
2166 if (args.hasArg(Ids: OPT_irpgo_profile_sort) ||
2167 args.hasArg(Ids: OPT_irpgo_profile_sort_eq))
2168 warn(msg: "--irpgo-profile-sort is deprecated. Please use "
2169 "--bp-startup-sort=function");
2170 if (const Arg *arg = args.getLastArg(Ids: OPT_irpgo_profile))
2171 config->irpgoProfilePath = arg->getValue();
2172
2173 if (const Arg *arg = args.getLastArg(Ids: OPT_irpgo_profile_sort)) {
2174 config->irpgoProfilePath = arg->getValue();
2175 config->bpStartupFunctionSort = true;
2176 IncompatWithCGSort(arg->getSpelling());
2177 }
2178 config->bpCompressionSortStartupFunctions =
2179 args.hasFlag(Pos: OPT_bp_compression_sort_startup_functions,
2180 Neg: OPT_no_bp_compression_sort_startup_functions, Default: false);
2181 if (const Arg *arg = args.getLastArg(Ids: OPT_bp_startup_sort)) {
2182 StringRef startupSortStr = arg->getValue();
2183 if (startupSortStr == "function") {
2184 config->bpStartupFunctionSort = true;
2185 } else if (startupSortStr != "none") {
2186 error(msg: "unknown value `" + startupSortStr + "` for " + arg->getSpelling());
2187 }
2188 if (startupSortStr != "none")
2189 IncompatWithCGSort(arg->getSpelling());
2190 }
2191 if (!config->bpStartupFunctionSort &&
2192 config->bpCompressionSortStartupFunctions)
2193 error(msg: "--bp-compression-sort-startup-functions must be used with "
2194 "--bp-startup-sort=function");
2195 if (config->irpgoProfilePath.empty() && config->bpStartupFunctionSort)
2196 error(msg: "--bp-startup-sort=function must be used with "
2197 "--irpgo-profile");
2198 auto addCompressionSortSpec = [&](StringRef value) {
2199 SmallVector<StringRef, 3> parts;
2200 value.split(A&: parts, Separator: '=');
2201
2202 StringRef globString = parts[0];
2203 unsigned layoutPriority = 0;
2204 std::optional<unsigned> matchPriority;
2205
2206 if (parts.size() > 1 && !parts[1].empty()) {
2207 if (!to_integer(S: parts[1], Num&: layoutPriority)) {
2208 error(msg: "--bp-compression-sort-section: expected integer "
2209 "for layout_priority, got '" +
2210 parts[1] + "'");
2211 return;
2212 }
2213 }
2214 if (parts.size() > 2 && !parts[2].empty()) {
2215 unsigned mp;
2216 if (!to_integer(S: parts[2], Num&: mp)) {
2217 error(msg: "--bp-compression-sort-section: expected integer "
2218 "for match_priority, got '" +
2219 parts[2] + "'");
2220 return;
2221 }
2222 matchPriority = mp;
2223 }
2224 if (parts.size() > 3) {
2225 error(msg: "--bp-compression-sort-section: too many '=' in '" + value + "'");
2226 return;
2227 }
2228
2229 auto spec = BPCompressionSortSpec::create(globString, layoutPriority,
2230 matchPriority);
2231 if (!spec) {
2232 error(msg: "--bp-compression-sort-section: " + toString(E: spec.takeError()));
2233 return;
2234 }
2235 config->bpCompressionSortSpecs.emplace_back(Args: std::move(*spec));
2236 };
2237
2238 for (const Arg *arg : args.filtered(Ids: OPT_bp_compression_sort_section))
2239 addCompressionSortSpec(arg->getValue());
2240 if (!config->bpCompressionSortSpecs.empty())
2241 IncompatWithCGSort("--bp-compression-sort-section");
2242 if (const Arg *arg = args.getLastArg(Ids: OPT_bp_compression_sort)) {
2243 StringRef compressionSortStr = arg->getValue();
2244 if (compressionSortStr == "function") {
2245 config->bpFunctionOrderForCompression = true;
2246 } else if (compressionSortStr == "data") {
2247 config->bpDataOrderForCompression = true;
2248 } else if (compressionSortStr == "both") {
2249 config->bpFunctionOrderForCompression = true;
2250 config->bpDataOrderForCompression = true;
2251 } else if (compressionSortStr != "none") {
2252 error(msg: "unknown value `" + compressionSortStr + "` for " +
2253 arg->getSpelling());
2254 }
2255 if (compressionSortStr != "none")
2256 IncompatWithCGSort(arg->getSpelling());
2257 }
2258 config->bpVerboseSectionOrderer = args.hasArg(Ids: OPT_verbose_bp_section_orderer);
2259
2260 for (const Arg *arg : args.filtered(Ids: OPT_alias)) {
2261 config->aliasedSymbols.push_back(
2262 x: std::make_pair(x: arg->getValue(N: 0), y: arg->getValue(N: 1)));
2263 }
2264
2265 if (const char *zero = getenv(name: "ZERO_AR_DATE"))
2266 config->zeroModTime = strcmp(s1: zero, s2: "0") != 0;
2267 if (args.getLastArg(Ids: OPT_reproducible))
2268 config->zeroModTime = true;
2269
2270 std::array<PlatformType, 4> encryptablePlatforms{
2271 PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS, PLATFORM_XROS};
2272 config->emitEncryptionInfo =
2273 args.hasFlag(Pos: OPT_encryptable, Neg: OPT_no_encryption,
2274 Default: is_contained(Range&: encryptablePlatforms, Element: config->platform()));
2275
2276 if (const Arg *arg = args.getLastArg(Ids: OPT_install_name)) {
2277 if (config->warnDylibInstallName && config->outputType != MH_DYLIB)
2278 warn(
2279 msg: arg->getAsString(Args: args) +
2280 ": ignored, only has effect with -dylib [--warn-dylib-install-name]");
2281 else
2282 config->installName = arg->getValue();
2283 } else if (config->outputType == MH_DYLIB) {
2284 config->installName = config->finalOutput;
2285 }
2286
2287 auto getClientName = [&]() {
2288 StringRef cn = path::filename(path: config->finalOutput);
2289 cn.consume_front(Prefix: "lib");
2290 auto firstDotOrUnderscore = cn.find_first_of(Chars: "._");
2291 cn = cn.take_front(N: firstDotOrUnderscore);
2292 return cn;
2293 };
2294 config->clientName = args.getLastArgValue(Id: OPT_client_name, Default: getClientName());
2295
2296 if (args.hasArg(Ids: OPT_mark_dead_strippable_dylib)) {
2297 if (config->outputType != MH_DYLIB)
2298 warn(msg: "-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
2299 else
2300 config->markDeadStrippableDylib = true;
2301 }
2302
2303 if (const Arg *arg = args.getLastArg(Ids: OPT_static, Ids: OPT_dynamic))
2304 config->staticLink = (arg->getOption().getID() == OPT_static);
2305
2306 if (const Arg *arg =
2307 args.getLastArg(Ids: OPT_flat_namespace, Ids: OPT_twolevel_namespace))
2308 config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
2309 ? NamespaceKind::twolevel
2310 : NamespaceKind::flat;
2311
2312 config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
2313
2314 if (config->outputType == MH_EXECUTE)
2315 config->entry = symtab->addUndefined(name: args.getLastArgValue(Id: OPT_e, Default: "_main"),
2316 /*file=*/nullptr,
2317 /*isWeakRef=*/false);
2318
2319 config->librarySearchPaths =
2320 getLibrarySearchPaths(args, roots: config->systemLibraryRoots);
2321 config->frameworkSearchPaths =
2322 getFrameworkSearchPaths(args, roots: config->systemLibraryRoots);
2323 if (const Arg *arg =
2324 args.getLastArg(Ids: OPT_search_paths_first, Ids: OPT_search_dylibs_first))
2325 config->searchDylibsFirst =
2326 arg->getOption().getID() == OPT_search_dylibs_first;
2327
2328 config->dylibCompatibilityVersion =
2329 parseDylibVersion(args, id: OPT_compatibility_version);
2330 config->dylibCurrentVersion = parseDylibVersion(args, id: OPT_current_version);
2331
2332 config->dataConst =
2333 args.hasFlag(Pos: OPT_data_const, Neg: OPT_no_data_const, Default: dataConstDefault(args));
2334 // Populate config->sectionRenameMap with builtin default renames.
2335 // Options -rename_section and -rename_segment are able to override.
2336 initializeSectionRenameMap();
2337 // Reject every special character except '.' and '$'
2338 // TODO(gkm): verify that this is the proper set of invalid chars
2339 StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
2340 auto validName = [invalidNameChars](StringRef s) {
2341 if (s.find_first_of(Chars: invalidNameChars) != StringRef::npos)
2342 error(msg: "invalid name for segment or section: " + s);
2343 return s;
2344 };
2345 for (const Arg *arg : args.filtered(Ids: OPT_rename_section)) {
2346 config->sectionRenameMap[{validName(arg->getValue(N: 0)),
2347 validName(arg->getValue(N: 1))}] = {
2348 validName(arg->getValue(N: 2)), validName(arg->getValue(N: 3))};
2349 }
2350 for (const Arg *arg : args.filtered(Ids: OPT_rename_segment)) {
2351 config->segmentRenameMap[validName(arg->getValue(N: 0))] =
2352 validName(arg->getValue(N: 1));
2353 }
2354
2355 config->sectionAlignments = parseSectAlign(args);
2356
2357 for (const Arg *arg : args.filtered(Ids: OPT_segprot)) {
2358 StringRef segName = arg->getValue(N: 0);
2359 uint32_t maxProt = parseProtection(protStr: arg->getValue(N: 1));
2360 uint32_t initProt = parseProtection(protStr: arg->getValue(N: 2));
2361
2362 // FIXME: Check if this works on more platforms.
2363 bool allowsDifferentInitAndMaxProt =
2364 config->platform() == PLATFORM_MACOS ||
2365 config->platform() == PLATFORM_MACCATALYST;
2366 if (allowsDifferentInitAndMaxProt) {
2367 if (initProt > maxProt)
2368 error(msg: "invalid argument '" + arg->getAsString(Args: args) +
2369 "': init must not be more permissive than max");
2370 } else {
2371 if (maxProt != initProt && config->arch() != AK_i386)
2372 error(msg: "invalid argument '" + arg->getAsString(Args: args) +
2373 "': max and init must be the same for non-macOS non-i386 archs");
2374 }
2375
2376 if (segName == segment_names::linkEdit)
2377 error(msg: "-segprot cannot be used to change __LINKEDIT's protections");
2378 config->segmentProtections.push_back(x: {.name: segName, .maxProt: maxProt, .initProt: initProt});
2379 }
2380
2381 config->hasExplicitExports =
2382 args.hasArg(Ids: OPT_no_exported_symbols) ||
2383 args.hasArgNoClaim(Ids: OPT_exported_symbol, Ids: OPT_exported_symbols_list);
2384 handleSymbolPatterns(args, symbolPatterns&: config->exportedSymbols, singleOptionCode: OPT_exported_symbol,
2385 listFileOptionCode: OPT_exported_symbols_list);
2386 handleSymbolPatterns(args, symbolPatterns&: config->unexportedSymbols, singleOptionCode: OPT_unexported_symbol,
2387 listFileOptionCode: OPT_unexported_symbols_list);
2388 if (config->hasExplicitExports && !config->unexportedSymbols.empty())
2389 error(msg: "cannot use both -exported_symbol* and -unexported_symbol* options");
2390
2391 if (args.hasArg(Ids: OPT_no_exported_symbols) && !config->exportedSymbols.empty())
2392 error(msg: "cannot use both -exported_symbol* and -no_exported_symbols options");
2393
2394 // Imitating LD64's:
2395 // -non_global_symbols_no_strip_list and -non_global_symbols_strip_list can't
2396 // both be present.
2397 // But -x can be used with either of these two, in which case, the last arg
2398 // takes effect.
2399 // (TODO: This is kind of confusing - considering disallowing using them
2400 // together for a more straightforward behaviour)
2401 {
2402 bool includeLocal = false;
2403 bool excludeLocal = false;
2404 for (const Arg *arg :
2405 args.filtered(Ids: OPT_x, Ids: OPT_non_global_symbols_no_strip_list,
2406 Ids: OPT_non_global_symbols_strip_list)) {
2407 switch (arg->getOption().getID()) {
2408 case OPT_x:
2409 config->localSymbolsPresence = SymtabPresence::None;
2410 break;
2411 case OPT_non_global_symbols_no_strip_list:
2412 if (excludeLocal) {
2413 error(msg: "cannot use both -non_global_symbols_no_strip_list and "
2414 "-non_global_symbols_strip_list");
2415 } else {
2416 includeLocal = true;
2417 config->localSymbolsPresence = SymtabPresence::SelectivelyIncluded;
2418 parseSymbolPatternsFile(arg, symbolPatterns&: config->localSymbolPatterns);
2419 }
2420 break;
2421 case OPT_non_global_symbols_strip_list:
2422 if (includeLocal) {
2423 error(msg: "cannot use both -non_global_symbols_no_strip_list and "
2424 "-non_global_symbols_strip_list");
2425 } else {
2426 excludeLocal = true;
2427 config->localSymbolsPresence = SymtabPresence::SelectivelyExcluded;
2428 parseSymbolPatternsFile(arg, symbolPatterns&: config->localSymbolPatterns);
2429 }
2430 break;
2431 default:
2432 llvm_unreachable("unexpected option");
2433 }
2434 }
2435 }
2436 // Explicitly-exported literal symbols must be defined, but might
2437 // languish in an archive if unreferenced elsewhere or if they are in the
2438 // non-global strip list. Light a fire under those lazy symbols!
2439 for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
2440 symtab->addUndefined(name: cachedName.val(), /*file=*/nullptr,
2441 /*isWeakRef=*/false);
2442
2443 for (const Arg *arg : args.filtered(Ids: OPT_why_live))
2444 config->whyLive.insert(symbolName: arg->getValue());
2445 if (!config->whyLive.empty() && !config->deadStrip)
2446 warn(msg: "-why_live has no effect without -dead_strip, ignoring");
2447
2448 config->saveTemps = args.hasArg(Ids: OPT_save_temps);
2449
2450 config->adhocCodesign = args.hasFlag(
2451 Pos: OPT_adhoc_codesign, Neg: OPT_no_adhoc_codesign,
2452 Default: shouldAdhocSignByDefault(arch: config->arch(), platform: config->platform()));
2453
2454 if (args.hasArg(Ids: OPT_v)) {
2455 message(msg: getLLDVersion(), s&: ctx->e.errs());
2456 message(msg: StringRef("Library search paths:") +
2457 (config->librarySearchPaths.empty()
2458 ? ""
2459 : "\n\t" + join(R&: config->librarySearchPaths, Separator: "\n\t")),
2460 s&: ctx->e.errs());
2461 message(msg: StringRef("Framework search paths:") +
2462 (config->frameworkSearchPaths.empty()
2463 ? ""
2464 : "\n\t" + join(R&: config->frameworkSearchPaths, Separator: "\n\t")),
2465 s&: ctx->e.errs());
2466 }
2467
2468 config->progName = argsArr[0];
2469
2470 config->timeTraceEnabled = args.hasArg(Ids: OPT_time_trace_eq);
2471 config->timeTraceGranularity =
2472 args::getInteger(args, key: OPT_time_trace_granularity_eq, Default: 500);
2473
2474 // Initialize time trace profiler.
2475 if (config->timeTraceEnabled)
2476 timeTraceProfilerInitialize(TimeTraceGranularity: config->timeTraceGranularity, ProcName: config->progName);
2477
2478 {
2479 TimeTraceScope timeScope("ExecuteLinker");
2480
2481 initLLVM(); // must be run before any call to addFile()
2482 createFiles(args);
2483
2484 // Now that all dylibs have been loaded, search for those that should be
2485 // re-exported.
2486 {
2487 auto reexportHandler = [](const Arg *arg,
2488 const std::vector<StringRef> &extensions) {
2489 config->hasReexports = true;
2490 StringRef searchName = arg->getValue();
2491 if (!markReexport(searchName, extensions))
2492 error(msg: arg->getSpelling() + " " + searchName +
2493 " does not match a supplied dylib");
2494 };
2495 std::vector<StringRef> extensions = {".tbd"};
2496 for (const Arg *arg : args.filtered(Ids: OPT_sub_umbrella))
2497 reexportHandler(arg, extensions);
2498
2499 extensions.push_back(x: ".dylib");
2500 for (const Arg *arg : args.filtered(Ids: OPT_sub_library))
2501 reexportHandler(arg, extensions);
2502 }
2503
2504 // Parse LTO options.
2505 if (const Arg *arg = args.getLastArg(Ids: OPT_mcpu))
2506 parseClangOption(opt: saver().save(S: "-mcpu=" + StringRef(arg->getValue())),
2507 msg: arg->getSpelling());
2508
2509 for (const Arg *arg : args.filtered(Ids: OPT_mllvm)) {
2510 parseClangOption(opt: arg->getValue(), msg: arg->getSpelling());
2511 config->mllvmOpts.emplace_back(Args: arg->getValue());
2512 }
2513
2514 config->passPlugins = args::getStrings(args, id: OPT_load_pass_plugins);
2515
2516 createSyntheticSections();
2517 createSyntheticSymbols();
2518
2519 createAliases();
2520 // If we are in "explicit exports" mode, hide everything that isn't
2521 // explicitly exported. Do this before running LTO so that LTO can better
2522 // optimize.
2523 handleExplicitExports();
2524
2525 bool didCompileBitcodeFiles = compileBitcodeFiles();
2526
2527 resolveLCLinkerOptions();
2528
2529 // If either --thinlto-index-only or --lto-emit-llvm is given, we should
2530 // not create object files. Index file creation is already done in
2531 // compileBitcodeFiles, so we are done if that's the case.
2532 if (config->thinLTOIndexOnly || config->emitLLVM)
2533 return errorCount() == 0;
2534
2535 prepareObjCStubs();
2536
2537 // LTO may emit a non-hidden (extern) object file symbol even if the
2538 // corresponding bitcode symbol is hidden. In particular, this happens for
2539 // cross-module references to hidden symbols under ThinLTO. Thus, if we
2540 // compiled any bitcode files, we must redo the symbol hiding.
2541 if (didCompileBitcodeFiles)
2542 handleExplicitExports();
2543 replaceCommonSymbols();
2544
2545 StringRef orderFile = args.getLastArgValue(Id: OPT_order_file);
2546 if (!orderFile.empty())
2547 priorityBuilder.parseOrderFile(path: orderFile);
2548
2549 referenceStubBinder();
2550
2551 // FIXME: should terminate the link early based on errors encountered so
2552 // far?
2553
2554 for (const Arg *arg : args.filtered(Ids: OPT_sectcreate)) {
2555 StringRef segName = arg->getValue(N: 0);
2556 StringRef sectName = arg->getValue(N: 1);
2557 StringRef fileName = arg->getValue(N: 2);
2558 std::optional<MemoryBufferRef> buffer = readFile(path: fileName);
2559 if (buffer)
2560 inputFiles.insert(X: make<OpaqueFile>(args&: *buffer, args&: segName, args&: sectName));
2561 }
2562
2563 for (const Arg *arg : args.filtered(Ids: OPT_add_empty_section)) {
2564 StringRef segName = arg->getValue(N: 0);
2565 StringRef sectName = arg->getValue(N: 1);
2566 inputFiles.insert(X: make<OpaqueFile>(args: MemoryBufferRef(), args&: segName, args&: sectName));
2567 }
2568
2569 parseDeferredRelocations();
2570
2571 gatherInputSections();
2572
2573 if (!config->codegenDataGeneratePath.empty())
2574 codegenDataGenerate();
2575
2576 if (config->callGraphProfileSort)
2577 priorityBuilder.extractCallGraphProfile();
2578
2579 if (config->deadStrip)
2580 markLive();
2581
2582 // Ensure that no symbols point inside __mod_init_func sections if they are
2583 // removed due to -init_offsets. This must run after dead stripping.
2584 if (config->emitInitOffsets)
2585 eraseInitializerSymbols();
2586
2587 // Categories are not subject to dead-strip. The __objc_catlist section is
2588 // marked as NO_DEAD_STRIP and that propagates into all category data.
2589 if (args.hasArg(Ids: OPT_check_category_conflicts))
2590 objc::checkCategories();
2591
2592 // Category merging uses "->live = false" to erase old category data, so
2593 // it has to run after dead-stripping (markLive).
2594 if (args.hasFlag(Pos: OPT_objc_category_merging, Neg: OPT_no_objc_category_merging,
2595 Default: false)) {
2596 // FIXME: Merge arm64e categories too, as ld64 does by default. Merging
2597 // doesn't preserve the signing schemas in arm64e category data yet.
2598 if (config->arch() == AK_arm64e)
2599 warn(msg: "-objc_category_merging is not supported on arm64e, ignoring");
2600 else
2601 objc::mergeCategories();
2602 }
2603
2604 computeColdness();
2605
2606 // ICF assumes that all literals have been folded already, so we must run
2607 // foldIdenticalLiterals before foldIdenticalSections.
2608 foldIdenticalLiterals();
2609 if (config->icfLevel != ICFLevel::none) {
2610 if (config->icfLevel == ICFLevel::safe ||
2611 config->icfLevel == ICFLevel::safe_thunks)
2612 markAddrSigSymbols();
2613 foldIdenticalSections(/*onlyCfStrings=*/false);
2614 } else if (config->dedupStrings) {
2615 foldIdenticalSections(/*onlyCfStrings=*/true);
2616 }
2617
2618 stripSwiftForceLoadFixups();
2619
2620 // Write to an output file.
2621 if (target->wordSize == 8)
2622 writeResult<LP64>();
2623 else
2624 writeResult<ILP32>();
2625
2626 depTracker->write(version: getLLDVersion(), inputs: inputFiles, output: config->outputFile);
2627 }
2628
2629 if (config->timeTraceEnabled) {
2630 checkError(e: timeTraceProfilerWrite(
2631 PreferredFileName: args.getLastArgValue(Id: OPT_time_trace_eq).str(), FallbackFileName: config->outputFile));
2632
2633 timeTraceProfilerCleanup();
2634 }
2635
2636 if (errorCount() != 0 || config->strictAutoLink)
2637 for (const auto &warning : missingAutolinkWarnings)
2638 warn(msg: warning);
2639
2640 return errorCount() == 0;
2641}
2642} // namespace macho
2643} // namespace lld
2644