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