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