1 //===- Driver.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "Driver.h"
10 #include "Config.h"
11 #include "ICF.h"
12 #include "InputFiles.h"
13 #include "LTO.h"
14 #include "MarkLive.h"
15 #include "ObjC.h"
16 #include "OutputSection.h"
17 #include "OutputSegment.h"
18 #include "SectionPriorities.h"
19 #include "SymbolTable.h"
20 #include "Symbols.h"
21 #include "SyntheticSections.h"
22 #include "Target.h"
23 #include "UnwindInfoSection.h"
24 #include "Writer.h"
25
26 #include "lld/Common/Args.h"
27 #include "lld/Common/CommonLinkerContext.h"
28 #include "lld/Common/Driver.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/StringExtras.h"
36 #include "llvm/ADT/StringRef.h"
37 #include "llvm/BinaryFormat/MachO.h"
38 #include "llvm/BinaryFormat/Magic.h"
39 #include "llvm/Config/llvm-config.h"
40 #include "llvm/LTO/LTO.h"
41 #include "llvm/Object/Archive.h"
42 #include "llvm/Option/ArgList.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/FileSystem.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/Parallel.h"
47 #include "llvm/Support/Path.h"
48 #include "llvm/Support/TarWriter.h"
49 #include "llvm/Support/TargetSelect.h"
50 #include "llvm/Support/TimeProfiler.h"
51 #include "llvm/TargetParser/Host.h"
52 #include "llvm/TextAPI/PackedVersion.h"
53
54 #include <algorithm>
55
56 using namespace llvm;
57 using namespace llvm::MachO;
58 using namespace llvm::object;
59 using namespace llvm::opt;
60 using namespace llvm::sys;
61 using namespace lld;
62 using namespace lld::macho;
63
64 std::unique_ptr<Configuration> macho::config;
65 std::unique_ptr<DependencyTracker> macho::depTracker;
66
getOutputType(const InputArgList & args)67 static HeaderFileType getOutputType(const InputArgList &args) {
68 // TODO: -r, -dylinker, -preload...
69 Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute);
70 if (outputArg == nullptr)
71 return MH_EXECUTE;
72
73 switch (outputArg->getOption().getID()) {
74 case OPT_bundle:
75 return MH_BUNDLE;
76 case OPT_dylib:
77 return MH_DYLIB;
78 case OPT_execute:
79 return MH_EXECUTE;
80 default:
81 llvm_unreachable("internal error");
82 }
83 }
84
85 static DenseMap<CachedHashStringRef, StringRef> resolvedLibraries;
findLibrary(StringRef name)86 static std::optional<StringRef> findLibrary(StringRef name) {
87 CachedHashStringRef key(name);
88 auto entry = resolvedLibraries.find(key);
89 if (entry != resolvedLibraries.end())
90 return entry->second;
91
92 auto doFind = [&] {
93 // Special case for Csu support files required for Mac OS X 10.7 and older
94 // (crt1.o)
95 if (name.ends_with(".o"))
96 return findPathCombination(name, config->librarySearchPaths, {""});
97 if (config->searchDylibsFirst) {
98 if (std::optional<StringRef> path =
99 findPathCombination("lib" + name, config->librarySearchPaths,
100 {".tbd", ".dylib", ".so"}))
101 return path;
102 return findPathCombination("lib" + name, config->librarySearchPaths,
103 {".a"});
104 }
105 return findPathCombination("lib" + name, config->librarySearchPaths,
106 {".tbd", ".dylib", ".so", ".a"});
107 };
108
109 std::optional<StringRef> path = doFind();
110 if (path)
111 resolvedLibraries[key] = *path;
112
113 return path;
114 }
115
116 static DenseMap<CachedHashStringRef, StringRef> resolvedFrameworks;
findFramework(StringRef name)117 static std::optional<StringRef> findFramework(StringRef name) {
118 CachedHashStringRef key(name);
119 auto entry = resolvedFrameworks.find(key);
120 if (entry != resolvedFrameworks.end())
121 return entry->second;
122
123 SmallString<260> symlink;
124 StringRef suffix;
125 std::tie(name, suffix) = name.split(",");
126 for (StringRef dir : config->frameworkSearchPaths) {
127 symlink = dir;
128 path::append(symlink, name + ".framework", name);
129
130 if (!suffix.empty()) {
131 // NOTE: we must resolve the symlink before trying the suffixes, because
132 // there are no symlinks for the suffixed paths.
133 SmallString<260> location;
134 if (!fs::real_path(symlink, location)) {
135 // only append suffix if realpath() succeeds
136 Twine suffixed = location + suffix;
137 if (fs::exists(suffixed))
138 return resolvedFrameworks[key] = saver().save(suffixed.str());
139 }
140 // Suffix lookup failed, fall through to the no-suffix case.
141 }
142
143 if (std::optional<StringRef> path = resolveDylibPath(symlink.str()))
144 return resolvedFrameworks[key] = *path;
145 }
146 return {};
147 }
148
warnIfNotDirectory(StringRef option,StringRef path)149 static bool warnIfNotDirectory(StringRef option, StringRef path) {
150 if (!fs::exists(path)) {
151 warn("directory not found for option -" + option + path);
152 return false;
153 } else if (!fs::is_directory(path)) {
154 warn("option -" + option + path + " references a non-directory path");
155 return false;
156 }
157 return true;
158 }
159
160 static std::vector<StringRef>
getSearchPaths(unsigned optionCode,InputArgList & args,const std::vector<StringRef> & roots,const SmallVector<StringRef,2> & systemPaths)161 getSearchPaths(unsigned optionCode, InputArgList &args,
162 const std::vector<StringRef> &roots,
163 const SmallVector<StringRef, 2> &systemPaths) {
164 std::vector<StringRef> paths;
165 StringRef optionLetter{optionCode == OPT_F ? "F" : "L"};
166 for (StringRef path : args::getStrings(args, optionCode)) {
167 // NOTE: only absolute paths are re-rooted to syslibroot(s)
168 bool found = false;
169 if (path::is_absolute(path, path::Style::posix)) {
170 for (StringRef root : roots) {
171 SmallString<261> buffer(root);
172 path::append(buffer, path);
173 // Do not warn about paths that are computed via the syslib roots
174 if (fs::is_directory(buffer)) {
175 paths.push_back(saver().save(buffer.str()));
176 found = true;
177 }
178 }
179 }
180 if (!found && warnIfNotDirectory(optionLetter, path))
181 paths.push_back(path);
182 }
183
184 // `-Z` suppresses the standard "system" search paths.
185 if (args.hasArg(OPT_Z))
186 return paths;
187
188 for (const StringRef &path : systemPaths) {
189 for (const StringRef &root : roots) {
190 SmallString<261> buffer(root);
191 path::append(buffer, path);
192 if (fs::is_directory(buffer))
193 paths.push_back(saver().save(buffer.str()));
194 }
195 }
196 return paths;
197 }
198
getSystemLibraryRoots(InputArgList & args)199 static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) {
200 std::vector<StringRef> roots;
201 for (const Arg *arg : args.filtered(OPT_syslibroot))
202 roots.push_back(arg->getValue());
203 // NOTE: the final `-syslibroot` being `/` will ignore all roots
204 if (!roots.empty() && roots.back() == "/")
205 roots.clear();
206 // NOTE: roots can never be empty - add an empty root to simplify the library
207 // and framework search path computation.
208 if (roots.empty())
209 roots.emplace_back("");
210 return roots;
211 }
212
213 static std::vector<StringRef>
getLibrarySearchPaths(InputArgList & args,const std::vector<StringRef> & roots)214 getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) {
215 return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"});
216 }
217
218 static std::vector<StringRef>
getFrameworkSearchPaths(InputArgList & args,const std::vector<StringRef> & roots)219 getFrameworkSearchPaths(InputArgList &args,
220 const std::vector<StringRef> &roots) {
221 return getSearchPaths(OPT_F, args, roots,
222 {"/Library/Frameworks", "/System/Library/Frameworks"});
223 }
224
getLTOCachePolicy(InputArgList & args)225 static llvm::CachePruningPolicy getLTOCachePolicy(InputArgList &args) {
226 SmallString<128> ltoPolicy;
227 auto add = [<oPolicy](Twine val) {
228 if (!ltoPolicy.empty())
229 ltoPolicy += ":";
230 val.toVector(ltoPolicy);
231 };
232 for (const Arg *arg :
233 args.filtered(OPT_thinlto_cache_policy_eq, OPT_prune_interval_lto,
234 OPT_prune_after_lto, OPT_max_relative_cache_size_lto)) {
235 switch (arg->getOption().getID()) {
236 case OPT_thinlto_cache_policy_eq:
237 add(arg->getValue());
238 break;
239 case OPT_prune_interval_lto:
240 if (!strcmp("-1", arg->getValue()))
241 add("prune_interval=87600h"); // 10 years
242 else
243 add(Twine("prune_interval=") + arg->getValue() + "s");
244 break;
245 case OPT_prune_after_lto:
246 add(Twine("prune_after=") + arg->getValue() + "s");
247 break;
248 case OPT_max_relative_cache_size_lto:
249 add(Twine("cache_size=") + arg->getValue() + "%");
250 break;
251 }
252 }
253 return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy");
254 }
255
256 // What caused a given library to be loaded. Only relevant for archives.
257 // Note that this does not tell us *how* we should load the library, i.e.
258 // whether we should do it lazily or eagerly (AKA force loading). The "how" is
259 // decided within addFile().
260 enum class LoadType {
261 CommandLine, // Library was passed as a regular CLI argument
262 CommandLineForce, // Library was passed via `-force_load`
263 LCLinkerOption, // Library was passed via LC_LINKER_OPTIONS
264 };
265
266 struct ArchiveFileInfo {
267 ArchiveFile *file;
268 bool isCommandLineLoad;
269 };
270
271 static DenseMap<StringRef, ArchiveFileInfo> loadedArchives;
272
addFile(StringRef path,LoadType loadType,bool isLazy=false,bool isExplicit=true,bool isBundleLoader=false,bool isForceHidden=false)273 static InputFile *addFile(StringRef path, LoadType loadType,
274 bool isLazy = false, bool isExplicit = true,
275 bool isBundleLoader = false,
276 bool isForceHidden = false) {
277 std::optional<MemoryBufferRef> buffer = readFile(path);
278 if (!buffer)
279 return nullptr;
280 MemoryBufferRef mbref = *buffer;
281 InputFile *newFile = nullptr;
282
283 file_magic magic = identify_magic(mbref.getBuffer());
284 switch (magic) {
285 case file_magic::archive: {
286 bool isCommandLineLoad = loadType != LoadType::LCLinkerOption;
287 // Avoid loading archives twice. If the archives are being force-loaded,
288 // loading them twice would create duplicate symbol errors. In the
289 // non-force-loading case, this is just a minor performance optimization.
290 // We don't take a reference to cachedFile here because the
291 // loadArchiveMember() call below may recursively call addFile() and
292 // invalidate this reference.
293 auto entry = loadedArchives.find(path);
294
295 ArchiveFile *file;
296 if (entry == loadedArchives.end()) {
297 // No cached archive, we need to create a new one
298 std::unique_ptr<object::Archive> archive = CHECK(
299 object::Archive::create(mbref), path + ": failed to parse archive");
300
301 if (!archive->isEmpty() && !archive->hasSymbolTable())
302 error(path + ": archive has no index; run ranlib to add one");
303 file = make<ArchiveFile>(std::move(archive), isForceHidden);
304 } else {
305 file = entry->second.file;
306 // Command-line loads take precedence. If file is previously loaded via
307 // command line, or is loaded via LC_LINKER_OPTION and being loaded via
308 // LC_LINKER_OPTION again, using the cached archive is enough.
309 if (entry->second.isCommandLineLoad || !isCommandLineLoad)
310 return file;
311 }
312
313 bool isLCLinkerForceLoad = loadType == LoadType::LCLinkerOption &&
314 config->forceLoadSwift &&
315 path::filename(path).starts_with("libswift");
316 if ((isCommandLineLoad && config->allLoad) ||
317 loadType == LoadType::CommandLineForce || isLCLinkerForceLoad) {
318 if (readFile(path)) {
319 Error e = Error::success();
320 for (const object::Archive::Child &c : file->getArchive().children(e)) {
321 StringRef reason;
322 switch (loadType) {
323 case LoadType::LCLinkerOption:
324 reason = "LC_LINKER_OPTION";
325 break;
326 case LoadType::CommandLineForce:
327 reason = "-force_load";
328 break;
329 case LoadType::CommandLine:
330 reason = "-all_load";
331 break;
332 }
333 if (Error e = file->fetch(c, reason))
334 error(toString(file) + ": " + reason +
335 " failed to load archive member: " + toString(std::move(e)));
336 }
337 if (e)
338 error(toString(file) +
339 ": Archive::children failed: " + toString(std::move(e)));
340 }
341 } else if (isCommandLineLoad && config->forceLoadObjC) {
342 for (const object::Archive::Symbol &sym : file->getArchive().symbols())
343 if (sym.getName().starts_with(objc::klass))
344 file->fetch(sym);
345
346 // TODO: no need to look for ObjC sections for a given archive member if
347 // we already found that it contains an ObjC symbol.
348 if (readFile(path)) {
349 Error e = Error::success();
350 for (const object::Archive::Child &c : file->getArchive().children(e)) {
351 Expected<MemoryBufferRef> mb = c.getMemoryBufferRef();
352 if (!mb || !hasObjCSection(*mb))
353 continue;
354 if (Error e = file->fetch(c, "-ObjC"))
355 error(toString(file) + ": -ObjC failed to load archive member: " +
356 toString(std::move(e)));
357 }
358 if (e)
359 error(toString(file) +
360 ": Archive::children failed: " + toString(std::move(e)));
361 }
362 }
363
364 file->addLazySymbols();
365 loadedArchives[path] = ArchiveFileInfo{file, isCommandLineLoad};
366 newFile = file;
367 break;
368 }
369 case file_magic::macho_object:
370 newFile = make<ObjFile>(mbref, getModTime(path), "", isLazy);
371 break;
372 case file_magic::macho_dynamically_linked_shared_lib:
373 case file_magic::macho_dynamically_linked_shared_lib_stub:
374 case file_magic::tapi_file:
375 if (DylibFile *dylibFile =
376 loadDylib(mbref, nullptr, /*isBundleLoader=*/false, isExplicit))
377 newFile = dylibFile;
378 break;
379 case file_magic::bitcode:
380 newFile = make<BitcodeFile>(mbref, "", 0, isLazy);
381 break;
382 case file_magic::macho_executable:
383 case file_magic::macho_bundle:
384 // We only allow executable and bundle type here if it is used
385 // as a bundle loader.
386 if (!isBundleLoader)
387 error(path + ": unhandled file type");
388 if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader))
389 newFile = dylibFile;
390 break;
391 default:
392 error(path + ": unhandled file type");
393 }
394 if (newFile && !isa<DylibFile>(newFile)) {
395 if ((isa<ObjFile>(newFile) || isa<BitcodeFile>(newFile)) && newFile->lazy &&
396 config->forceLoadObjC) {
397 for (Symbol *sym : newFile->symbols)
398 if (sym && sym->getName().starts_with(objc::klass)) {
399 extract(*newFile, "-ObjC");
400 break;
401 }
402 if (newFile->lazy && hasObjCSection(mbref))
403 extract(*newFile, "-ObjC");
404 }
405
406 // printArchiveMemberLoad() prints both .a and .o names, so no need to
407 // print the .a name here. Similarly skip lazy files.
408 if (config->printEachFile && magic != file_magic::archive && !isLazy)
409 message(toString(newFile));
410 inputFiles.insert(newFile);
411 }
412 return newFile;
413 }
414
415 static std::vector<StringRef> missingAutolinkWarnings;
addLibrary(StringRef name,bool isNeeded,bool isWeak,bool isReexport,bool isHidden,bool isExplicit,LoadType loadType)416 static void addLibrary(StringRef name, bool isNeeded, bool isWeak,
417 bool isReexport, bool isHidden, bool isExplicit,
418 LoadType loadType) {
419 if (std::optional<StringRef> path = findLibrary(name)) {
420 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
421 addFile(*path, loadType, /*isLazy=*/false, isExplicit,
422 /*isBundleLoader=*/false, isHidden))) {
423 if (isNeeded)
424 dylibFile->forceNeeded = true;
425 if (isWeak)
426 dylibFile->forceWeakImport = true;
427 if (isReexport) {
428 config->hasReexports = true;
429 dylibFile->reexport = true;
430 }
431 }
432 return;
433 }
434 if (loadType == LoadType::LCLinkerOption) {
435 missingAutolinkWarnings.push_back(
436 saver().save("auto-linked library not found for -l" + name));
437 return;
438 }
439 error("library not found for -l" + name);
440 }
441
442 static DenseSet<StringRef> loadedObjectFrameworks;
addFramework(StringRef name,bool isNeeded,bool isWeak,bool isReexport,bool isExplicit,LoadType loadType)443 static void addFramework(StringRef name, bool isNeeded, bool isWeak,
444 bool isReexport, bool isExplicit, LoadType loadType) {
445 if (std::optional<StringRef> path = findFramework(name)) {
446 if (loadedObjectFrameworks.contains(*path))
447 return;
448
449 InputFile *file =
450 addFile(*path, loadType, /*isLazy=*/false, isExplicit, false);
451 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(file)) {
452 if (isNeeded)
453 dylibFile->forceNeeded = true;
454 if (isWeak)
455 dylibFile->forceWeakImport = true;
456 if (isReexport) {
457 config->hasReexports = true;
458 dylibFile->reexport = true;
459 }
460 } else if (isa_and_nonnull<ObjFile>(file) ||
461 isa_and_nonnull<BitcodeFile>(file)) {
462 // Cache frameworks containing object or bitcode files to avoid duplicate
463 // symbols. Frameworks containing static archives are cached separately
464 // in addFile() to share caching with libraries, and frameworks
465 // containing dylibs should allow overwriting of attributes such as
466 // forceNeeded by subsequent loads
467 loadedObjectFrameworks.insert(*path);
468 }
469 return;
470 }
471 if (loadType == LoadType::LCLinkerOption) {
472 missingAutolinkWarnings.push_back(
473 saver().save("auto-linked framework not found for -framework " + name));
474 return;
475 }
476 error("framework not found for -framework " + name);
477 }
478
479 // Parses LC_LINKER_OPTION contents, which can add additional command line
480 // flags. This directly parses the flags instead of using the standard argument
481 // parser to improve performance.
parseLCLinkerOption(llvm::SmallVectorImpl<StringRef> & LCLinkerOptions,InputFile * f,unsigned argc,StringRef data)482 void macho::parseLCLinkerOption(
483 llvm::SmallVectorImpl<StringRef> &LCLinkerOptions, InputFile *f,
484 unsigned argc, StringRef data) {
485 if (config->ignoreAutoLink)
486 return;
487
488 SmallVector<StringRef, 4> argv;
489 size_t offset = 0;
490 for (unsigned i = 0; i < argc && offset < data.size(); ++i) {
491 argv.push_back(data.data() + offset);
492 offset += strlen(data.data() + offset) + 1;
493 }
494 if (argv.size() != argc || offset > data.size())
495 fatal(toString(f) + ": invalid LC_LINKER_OPTION");
496
497 unsigned i = 0;
498 StringRef arg = argv[i];
499 if (arg.consume_front("-l")) {
500 if (config->ignoreAutoLinkOptions.contains(arg))
501 return;
502 } else if (arg == "-framework") {
503 StringRef name = argv[++i];
504 if (config->ignoreAutoLinkOptions.contains(name))
505 return;
506 } else {
507 error(arg + " is not allowed in LC_LINKER_OPTION");
508 }
509
510 LCLinkerOptions.append(argv);
511 }
512
resolveLCLinkerOptions()513 void macho::resolveLCLinkerOptions() {
514 while (!unprocessedLCLinkerOptions.empty()) {
515 SmallVector<StringRef> LCLinkerOptions(unprocessedLCLinkerOptions);
516 unprocessedLCLinkerOptions.clear();
517
518 for (unsigned i = 0; i < LCLinkerOptions.size(); ++i) {
519 StringRef arg = LCLinkerOptions[i];
520 if (arg.consume_front("-l")) {
521 assert(!config->ignoreAutoLinkOptions.contains(arg));
522 addLibrary(arg, /*isNeeded=*/false, /*isWeak=*/false,
523 /*isReexport=*/false, /*isHidden=*/false,
524 /*isExplicit=*/false, LoadType::LCLinkerOption);
525 } else if (arg == "-framework") {
526 StringRef name = LCLinkerOptions[++i];
527 assert(!config->ignoreAutoLinkOptions.contains(name));
528 addFramework(name, /*isNeeded=*/false, /*isWeak=*/false,
529 /*isReexport=*/false, /*isExplicit=*/false,
530 LoadType::LCLinkerOption);
531 } else {
532 error(arg + " is not allowed in LC_LINKER_OPTION");
533 }
534 }
535 }
536 }
537
addFileList(StringRef path,bool isLazy)538 static void addFileList(StringRef path, bool isLazy) {
539 std::optional<MemoryBufferRef> buffer = readFile(path);
540 if (!buffer)
541 return;
542 MemoryBufferRef mbref = *buffer;
543 for (StringRef path : args::getLines(mbref))
544 addFile(rerootPath(path), LoadType::CommandLine, isLazy);
545 }
546
547 // We expect sub-library names of the form "libfoo", which will match a dylib
548 // with a path of .*/libfoo.{dylib, tbd}.
549 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
550 // I'm not sure what the use case for that is.
markReexport(StringRef searchName,ArrayRef<StringRef> extensions)551 static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) {
552 for (InputFile *file : inputFiles) {
553 if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
554 StringRef filename = path::filename(dylibFile->getName());
555 if (filename.consume_front(searchName) &&
556 (filename.empty() || llvm::is_contained(extensions, filename))) {
557 dylibFile->reexport = true;
558 return true;
559 }
560 }
561 }
562 return false;
563 }
564
565 // This function is called on startup. We need this for LTO since
566 // LTO calls LLVM functions to compile bitcode files to native code.
567 // Technically this can be delayed until we read bitcode files, but
568 // we don't bother to do lazily because the initialization is fast.
initLLVM()569 static void initLLVM() {
570 InitializeAllTargets();
571 InitializeAllTargetMCs();
572 InitializeAllAsmPrinters();
573 InitializeAllAsmParsers();
574 }
575
compileBitcodeFiles()576 static bool compileBitcodeFiles() {
577 TimeTraceScope timeScope("LTO");
578 auto *lto = make<BitcodeCompiler>();
579 for (InputFile *file : inputFiles)
580 if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file))
581 if (!file->lazy)
582 lto->add(*bitcodeFile);
583
584 std::vector<ObjFile *> compiled = lto->compile();
585 for (ObjFile *file : compiled)
586 inputFiles.insert(file);
587
588 return !compiled.empty();
589 }
590
591 // Replaces common symbols with defined symbols residing in __common sections.
592 // This function must be called after all symbol names are resolved (i.e. after
593 // all InputFiles have been loaded.) As a result, later operations won't see
594 // any CommonSymbols.
replaceCommonSymbols()595 static void replaceCommonSymbols() {
596 TimeTraceScope timeScope("Replace common symbols");
597 ConcatOutputSection *osec = nullptr;
598 for (Symbol *sym : symtab->getSymbols()) {
599 auto *common = dyn_cast<CommonSymbol>(sym);
600 if (common == nullptr)
601 continue;
602
603 // Casting to size_t will truncate large values on 32-bit architectures,
604 // but it's not really worth supporting the linking of 64-bit programs on
605 // 32-bit archs.
606 ArrayRef<uint8_t> data = {nullptr, static_cast<size_t>(common->size)};
607 // FIXME avoid creating one Section per symbol?
608 auto *section =
609 make<Section>(common->getFile(), segment_names::data,
610 section_names::common, S_ZEROFILL, /*addr=*/0);
611 auto *isec = make<ConcatInputSection>(*section, data, common->align);
612 if (!osec)
613 osec = ConcatOutputSection::getOrCreateForInput(isec);
614 isec->parent = osec;
615 inputSections.push_back(isec);
616
617 // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
618 // and pass them on here.
619 replaceSymbol<Defined>(
620 sym, sym->getName(), common->getFile(), isec, /*value=*/0, common->size,
621 /*isWeakDef=*/false, /*isExternal=*/true, common->privateExtern,
622 /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
623 /*noDeadStrip=*/false);
624 }
625 }
626
initializeSectionRenameMap()627 static void initializeSectionRenameMap() {
628 if (config->dataConst) {
629 SmallVector<StringRef> v{section_names::got,
630 section_names::authGot,
631 section_names::authPtr,
632 section_names::nonLazySymbolPtr,
633 section_names::const_,
634 section_names::cfString,
635 section_names::moduleInitFunc,
636 section_names::moduleTermFunc,
637 section_names::objcClassList,
638 section_names::objcNonLazyClassList,
639 section_names::objcCatList,
640 section_names::objcNonLazyCatList,
641 section_names::objcProtoList,
642 section_names::objCImageInfo};
643 for (StringRef s : v)
644 config->sectionRenameMap[{segment_names::data, s}] = {
645 segment_names::dataConst, s};
646 }
647 config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = {
648 segment_names::text, section_names::text};
649 config->sectionRenameMap[{segment_names::import, section_names::pointers}] = {
650 config->dataConst ? segment_names::dataConst : segment_names::data,
651 section_names::nonLazySymbolPtr};
652 }
653
toLowerDash(char x)654 static inline char toLowerDash(char x) {
655 if (x >= 'A' && x <= 'Z')
656 return x - 'A' + 'a';
657 else if (x == ' ')
658 return '-';
659 return x;
660 }
661
lowerDash(StringRef s)662 static std::string lowerDash(StringRef s) {
663 return std::string(map_iterator(s.begin(), toLowerDash),
664 map_iterator(s.end(), toLowerDash));
665 }
666
667 struct PlatformVersion {
668 PlatformType platform = PLATFORM_UNKNOWN;
669 llvm::VersionTuple minimum;
670 llvm::VersionTuple sdk;
671 };
672
parsePlatformVersion(const Arg * arg)673 static PlatformVersion parsePlatformVersion(const Arg *arg) {
674 assert(arg->getOption().getID() == OPT_platform_version);
675 StringRef platformStr = arg->getValue(0);
676 StringRef minVersionStr = arg->getValue(1);
677 StringRef sdkVersionStr = arg->getValue(2);
678
679 PlatformVersion platformVersion;
680
681 // TODO(compnerd) see if we can generate this case list via XMACROS
682 platformVersion.platform =
683 StringSwitch<PlatformType>(lowerDash(platformStr))
684 .Cases("macos", "1", PLATFORM_MACOS)
685 .Cases("ios", "2", PLATFORM_IOS)
686 .Cases("tvos", "3", PLATFORM_TVOS)
687 .Cases("watchos", "4", PLATFORM_WATCHOS)
688 .Cases("bridgeos", "5", PLATFORM_BRIDGEOS)
689 .Cases("mac-catalyst", "6", PLATFORM_MACCATALYST)
690 .Cases("ios-simulator", "7", PLATFORM_IOSSIMULATOR)
691 .Cases("tvos-simulator", "8", PLATFORM_TVOSSIMULATOR)
692 .Cases("watchos-simulator", "9", PLATFORM_WATCHOSSIMULATOR)
693 .Cases("driverkit", "10", PLATFORM_DRIVERKIT)
694 .Default(PLATFORM_UNKNOWN);
695 if (platformVersion.platform == PLATFORM_UNKNOWN)
696 error(Twine("malformed platform: ") + platformStr);
697 // TODO: check validity of version strings, which varies by platform
698 // NOTE: ld64 accepts version strings with 5 components
699 // llvm::VersionTuple accepts no more than 4 components
700 // Has Apple ever published version strings with 5 components?
701 if (platformVersion.minimum.tryParse(minVersionStr))
702 error(Twine("malformed minimum version: ") + minVersionStr);
703 if (platformVersion.sdk.tryParse(sdkVersionStr))
704 error(Twine("malformed sdk version: ") + sdkVersionStr);
705 return platformVersion;
706 }
707
708 // Has the side-effect of setting Config::platformInfo and
709 // potentially Config::secondaryPlatformInfo.
setPlatformVersions(StringRef archName,const ArgList & args)710 static void setPlatformVersions(StringRef archName, const ArgList &args) {
711 std::map<PlatformType, PlatformVersion> platformVersions;
712 const PlatformVersion *lastVersionInfo = nullptr;
713 for (const Arg *arg : args.filtered(OPT_platform_version)) {
714 PlatformVersion version = parsePlatformVersion(arg);
715
716 // For each platform, the last flag wins:
717 // `-platform_version macos 2 3 -platform_version macos 4 5` has the same
718 // effect as just passing `-platform_version macos 4 5`.
719 // FIXME: ld64 warns on multiple flags for one platform. Should we?
720 platformVersions[version.platform] = version;
721 lastVersionInfo = &platformVersions[version.platform];
722 }
723
724 if (platformVersions.empty()) {
725 error("must specify -platform_version");
726 return;
727 }
728 if (platformVersions.size() > 2) {
729 error("must specify -platform_version at most twice");
730 return;
731 }
732 if (platformVersions.size() == 2) {
733 bool isZipperedCatalyst = platformVersions.count(PLATFORM_MACOS) &&
734 platformVersions.count(PLATFORM_MACCATALYST);
735
736 if (!isZipperedCatalyst) {
737 error("lld supports writing zippered outputs only for "
738 "macos and mac-catalyst");
739 } else if (config->outputType != MH_DYLIB &&
740 config->outputType != MH_BUNDLE) {
741 error("writing zippered outputs only valid for -dylib and -bundle");
742 }
743
744 config->platformInfo = {
745 MachO::Target(getArchitectureFromName(archName), PLATFORM_MACOS,
746 platformVersions[PLATFORM_MACOS].minimum),
747 platformVersions[PLATFORM_MACOS].sdk};
748 config->secondaryPlatformInfo = {
749 MachO::Target(getArchitectureFromName(archName), PLATFORM_MACCATALYST,
750 platformVersions[PLATFORM_MACCATALYST].minimum),
751 platformVersions[PLATFORM_MACCATALYST].sdk};
752 return;
753 }
754
755 config->platformInfo = {MachO::Target(getArchitectureFromName(archName),
756 lastVersionInfo->platform,
757 lastVersionInfo->minimum),
758 lastVersionInfo->sdk};
759 }
760
761 // Has the side-effect of setting Config::target.
createTargetInfo(InputArgList & args)762 static TargetInfo *createTargetInfo(InputArgList &args) {
763 StringRef archName = args.getLastArgValue(OPT_arch);
764 if (archName.empty()) {
765 error("must specify -arch");
766 return nullptr;
767 }
768
769 setPlatformVersions(archName, args);
770 auto [cpuType, cpuSubtype] = getCPUTypeFromArchitecture(config->arch());
771 switch (cpuType) {
772 case CPU_TYPE_X86_64:
773 return createX86_64TargetInfo();
774 case CPU_TYPE_ARM64:
775 return createARM64TargetInfo();
776 case CPU_TYPE_ARM64_32:
777 return createARM64_32TargetInfo();
778 default:
779 error("missing or unsupported -arch " + archName);
780 return nullptr;
781 }
782 }
783
784 static UndefinedSymbolTreatment
getUndefinedSymbolTreatment(const ArgList & args)785 getUndefinedSymbolTreatment(const ArgList &args) {
786 StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
787 auto treatment =
788 StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
789 .Cases("error", "", UndefinedSymbolTreatment::error)
790 .Case("warning", UndefinedSymbolTreatment::warning)
791 .Case("suppress", UndefinedSymbolTreatment::suppress)
792 .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
793 .Default(UndefinedSymbolTreatment::unknown);
794 if (treatment == UndefinedSymbolTreatment::unknown) {
795 warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
796 "', defaulting to 'error'");
797 treatment = UndefinedSymbolTreatment::error;
798 } else if (config->namespaceKind == NamespaceKind::twolevel &&
799 (treatment == UndefinedSymbolTreatment::warning ||
800 treatment == UndefinedSymbolTreatment::suppress)) {
801 if (treatment == UndefinedSymbolTreatment::warning)
802 fatal("'-undefined warning' only valid with '-flat_namespace'");
803 else
804 fatal("'-undefined suppress' only valid with '-flat_namespace'");
805 treatment = UndefinedSymbolTreatment::error;
806 }
807 return treatment;
808 }
809
getICFLevel(const ArgList & args)810 static ICFLevel getICFLevel(const ArgList &args) {
811 StringRef icfLevelStr = args.getLastArgValue(OPT_icf_eq);
812 auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr)
813 .Cases("none", "", ICFLevel::none)
814 .Case("safe", ICFLevel::safe)
815 .Case("all", ICFLevel::all)
816 .Default(ICFLevel::unknown);
817 if (icfLevel == ICFLevel::unknown) {
818 warn(Twine("unknown --icf=OPTION `") + icfLevelStr +
819 "', defaulting to `none'");
820 icfLevel = ICFLevel::none;
821 }
822 return icfLevel;
823 }
824
getObjCStubsMode(const ArgList & args)825 static ObjCStubsMode getObjCStubsMode(const ArgList &args) {
826 const Arg *arg = args.getLastArg(OPT_objc_stubs_fast, OPT_objc_stubs_small);
827 if (!arg)
828 return ObjCStubsMode::fast;
829
830 if (arg->getOption().getID() == OPT_objc_stubs_small) {
831 if (is_contained({AK_arm64e, AK_arm64}, config->arch()))
832 return ObjCStubsMode::small;
833 else
834 warn("-objc_stubs_small is not yet implemented, defaulting to "
835 "-objc_stubs_fast");
836 }
837 return ObjCStubsMode::fast;
838 }
839
warnIfDeprecatedOption(const Option & opt)840 static void warnIfDeprecatedOption(const Option &opt) {
841 if (!opt.getGroup().isValid())
842 return;
843 if (opt.getGroup().getID() == OPT_grp_deprecated) {
844 warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
845 warn(opt.getHelpText());
846 }
847 }
848
warnIfUnimplementedOption(const Option & opt)849 static void warnIfUnimplementedOption(const Option &opt) {
850 if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
851 return;
852 switch (opt.getGroup().getID()) {
853 case OPT_grp_deprecated:
854 // warn about deprecated options elsewhere
855 break;
856 case OPT_grp_undocumented:
857 warn("Option `" + opt.getPrefixedName() +
858 "' is undocumented. Should lld implement it?");
859 break;
860 case OPT_grp_obsolete:
861 warn("Option `" + opt.getPrefixedName() +
862 "' is obsolete. Please modernize your usage.");
863 break;
864 case OPT_grp_ignored:
865 warn("Option `" + opt.getPrefixedName() + "' is ignored.");
866 break;
867 case OPT_grp_ignored_silently:
868 break;
869 default:
870 warn("Option `" + opt.getPrefixedName() +
871 "' is not yet implemented. Stay tuned...");
872 break;
873 }
874 }
875
getReproduceOption(InputArgList & args)876 static const char *getReproduceOption(InputArgList &args) {
877 if (const Arg *arg = args.getLastArg(OPT_reproduce))
878 return arg->getValue();
879 return getenv("LLD_REPRODUCE");
880 }
881
882 // Parse options of the form "old;new".
getOldNewOptions(opt::InputArgList & args,unsigned id)883 static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
884 unsigned id) {
885 auto *arg = args.getLastArg(id);
886 if (!arg)
887 return {"", ""};
888
889 StringRef s = arg->getValue();
890 std::pair<StringRef, StringRef> ret = s.split(';');
891 if (ret.second.empty())
892 error(arg->getSpelling() + " expects 'old;new' format, but got " + s);
893 return ret;
894 }
895
896 // Parse options of the form "old;new[;extra]".
897 static std::tuple<StringRef, StringRef, StringRef>
getOldNewOptionsExtra(opt::InputArgList & args,unsigned id)898 getOldNewOptionsExtra(opt::InputArgList &args, unsigned id) {
899 auto [oldDir, second] = getOldNewOptions(args, id);
900 auto [newDir, extraDir] = second.split(';');
901 return {oldDir, newDir, extraDir};
902 }
903
parseClangOption(StringRef opt,const Twine & msg)904 static void parseClangOption(StringRef opt, const Twine &msg) {
905 std::string err;
906 raw_string_ostream os(err);
907
908 const char *argv[] = {"lld", opt.data()};
909 if (cl::ParseCommandLineOptions(2, argv, "", &os))
910 return;
911 os.flush();
912 error(msg + ": " + StringRef(err).trim());
913 }
914
parseDylibVersion(const ArgList & args,unsigned id)915 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
916 const Arg *arg = args.getLastArg(id);
917 if (!arg)
918 return 0;
919
920 if (config->outputType != MH_DYLIB) {
921 error(arg->getAsString(args) + ": only valid with -dylib");
922 return 0;
923 }
924
925 PackedVersion version;
926 if (!version.parse32(arg->getValue())) {
927 error(arg->getAsString(args) + ": malformed version");
928 return 0;
929 }
930
931 return version.rawValue();
932 }
933
parseProtection(StringRef protStr)934 static uint32_t parseProtection(StringRef protStr) {
935 uint32_t prot = 0;
936 for (char c : protStr) {
937 switch (c) {
938 case 'r':
939 prot |= VM_PROT_READ;
940 break;
941 case 'w':
942 prot |= VM_PROT_WRITE;
943 break;
944 case 'x':
945 prot |= VM_PROT_EXECUTE;
946 break;
947 case '-':
948 break;
949 default:
950 error("unknown -segprot letter '" + Twine(c) + "' in " + protStr);
951 return 0;
952 }
953 }
954 return prot;
955 }
956
parseSectAlign(const opt::InputArgList & args)957 static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) {
958 std::vector<SectionAlign> sectAligns;
959 for (const Arg *arg : args.filtered(OPT_sectalign)) {
960 StringRef segName = arg->getValue(0);
961 StringRef sectName = arg->getValue(1);
962 StringRef alignStr = arg->getValue(2);
963 alignStr.consume_front_insensitive("0x");
964 uint32_t align;
965 if (alignStr.getAsInteger(16, align)) {
966 error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) +
967 "' as number");
968 continue;
969 }
970 if (!isPowerOf2_32(align)) {
971 error("-sectalign: '" + StringRef(arg->getValue(2)) +
972 "' (in base 16) not a power of two");
973 continue;
974 }
975 sectAligns.push_back({segName, sectName, align});
976 }
977 return sectAligns;
978 }
979
removeSimulator(PlatformType platform)980 PlatformType macho::removeSimulator(PlatformType platform) {
981 switch (platform) {
982 case PLATFORM_IOSSIMULATOR:
983 return PLATFORM_IOS;
984 case PLATFORM_TVOSSIMULATOR:
985 return PLATFORM_TVOS;
986 case PLATFORM_WATCHOSSIMULATOR:
987 return PLATFORM_WATCHOS;
988 default:
989 return platform;
990 }
991 }
992
supportsNoPie()993 static bool supportsNoPie() {
994 return !(config->arch() == AK_arm64 || config->arch() == AK_arm64e ||
995 config->arch() == AK_arm64_32);
996 }
997
shouldAdhocSignByDefault(Architecture arch,PlatformType platform)998 static bool shouldAdhocSignByDefault(Architecture arch, PlatformType platform) {
999 if (arch != AK_arm64 && arch != AK_arm64e)
1000 return false;
1001
1002 return platform == PLATFORM_MACOS || platform == PLATFORM_IOSSIMULATOR ||
1003 platform == PLATFORM_TVOSSIMULATOR ||
1004 platform == PLATFORM_WATCHOSSIMULATOR;
1005 }
1006
dataConstDefault(const InputArgList & args)1007 static bool dataConstDefault(const InputArgList &args) {
1008 static const std::array<std::pair<PlatformType, VersionTuple>, 5> minVersion =
1009 {{{PLATFORM_MACOS, VersionTuple(10, 15)},
1010 {PLATFORM_IOS, VersionTuple(13, 0)},
1011 {PLATFORM_TVOS, VersionTuple(13, 0)},
1012 {PLATFORM_WATCHOS, VersionTuple(6, 0)},
1013 {PLATFORM_BRIDGEOS, VersionTuple(4, 0)}}};
1014 PlatformType platform = removeSimulator(config->platformInfo.target.Platform);
1015 auto it = llvm::find_if(minVersion,
1016 [&](const auto &p) { return p.first == platform; });
1017 if (it != minVersion.end())
1018 if (config->platformInfo.target.MinDeployment < it->second)
1019 return false;
1020
1021 switch (config->outputType) {
1022 case MH_EXECUTE:
1023 return !(args.hasArg(OPT_no_pie) && supportsNoPie());
1024 case MH_BUNDLE:
1025 // FIXME: return false when -final_name ...
1026 // has prefix "/System/Library/UserEventPlugins/"
1027 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd"
1028 return true;
1029 case MH_DYLIB:
1030 return true;
1031 case MH_OBJECT:
1032 return false;
1033 default:
1034 llvm_unreachable(
1035 "unsupported output type for determining data-const default");
1036 }
1037 return false;
1038 }
1039
shouldEmitChainedFixups(const InputArgList & args)1040 static bool shouldEmitChainedFixups(const InputArgList &args) {
1041 const Arg *arg = args.getLastArg(OPT_fixup_chains, OPT_no_fixup_chains);
1042 if (arg && arg->getOption().matches(OPT_no_fixup_chains))
1043 return false;
1044
1045 bool isRequested = arg != nullptr;
1046
1047 // Version numbers taken from the Xcode 13.3 release notes.
1048 static const std::array<std::pair<PlatformType, VersionTuple>, 4> minVersion =
1049 {{{PLATFORM_MACOS, VersionTuple(11, 0)},
1050 {PLATFORM_IOS, VersionTuple(13, 4)},
1051 {PLATFORM_TVOS, VersionTuple(14, 0)},
1052 {PLATFORM_WATCHOS, VersionTuple(7, 0)}}};
1053 PlatformType platform = removeSimulator(config->platformInfo.target.Platform);
1054 auto it = llvm::find_if(minVersion,
1055 [&](const auto &p) { return p.first == platform; });
1056 if (it != minVersion.end() &&
1057 it->second > config->platformInfo.target.MinDeployment) {
1058 if (!isRequested)
1059 return false;
1060
1061 warn("-fixup_chains requires " + getPlatformName(config->platform()) + " " +
1062 it->second.getAsString() + ", which is newer than target minimum of " +
1063 config->platformInfo.target.MinDeployment.getAsString());
1064 }
1065
1066 if (!is_contained({AK_x86_64, AK_x86_64h, AK_arm64}, config->arch())) {
1067 if (isRequested)
1068 error("-fixup_chains is only supported on x86_64 and arm64 targets");
1069 return false;
1070 }
1071
1072 if (!config->isPic) {
1073 if (isRequested)
1074 error("-fixup_chains is incompatible with -no_pie");
1075 return false;
1076 }
1077
1078 // TODO: Enable by default once stable.
1079 return isRequested;
1080 }
1081
clear()1082 void SymbolPatterns::clear() {
1083 literals.clear();
1084 globs.clear();
1085 }
1086
insert(StringRef symbolName)1087 void SymbolPatterns::insert(StringRef symbolName) {
1088 if (symbolName.find_first_of("*?[]") == StringRef::npos)
1089 literals.insert(CachedHashStringRef(symbolName));
1090 else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName))
1091 globs.emplace_back(*pattern);
1092 else
1093 error("invalid symbol-name pattern: " + symbolName);
1094 }
1095
matchLiteral(StringRef symbolName) const1096 bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
1097 return literals.contains(CachedHashStringRef(symbolName));
1098 }
1099
matchGlob(StringRef symbolName) const1100 bool SymbolPatterns::matchGlob(StringRef symbolName) const {
1101 for (const GlobPattern &glob : globs)
1102 if (glob.match(symbolName))
1103 return true;
1104 return false;
1105 }
1106
match(StringRef symbolName) const1107 bool SymbolPatterns::match(StringRef symbolName) const {
1108 return matchLiteral(symbolName) || matchGlob(symbolName);
1109 }
1110
parseSymbolPatternsFile(const Arg * arg,SymbolPatterns & symbolPatterns)1111 static void parseSymbolPatternsFile(const Arg *arg,
1112 SymbolPatterns &symbolPatterns) {
1113 StringRef path = arg->getValue();
1114 std::optional<MemoryBufferRef> buffer = readFile(path);
1115 if (!buffer) {
1116 error("Could not read symbol file: " + path);
1117 return;
1118 }
1119 MemoryBufferRef mbref = *buffer;
1120 for (StringRef line : args::getLines(mbref)) {
1121 line = line.take_until([](char c) { return c == '#'; }).trim();
1122 if (!line.empty())
1123 symbolPatterns.insert(line);
1124 }
1125 }
1126
handleSymbolPatterns(InputArgList & args,SymbolPatterns & symbolPatterns,unsigned singleOptionCode,unsigned listFileOptionCode)1127 static void handleSymbolPatterns(InputArgList &args,
1128 SymbolPatterns &symbolPatterns,
1129 unsigned singleOptionCode,
1130 unsigned listFileOptionCode) {
1131 for (const Arg *arg : args.filtered(singleOptionCode))
1132 symbolPatterns.insert(arg->getValue());
1133 for (const Arg *arg : args.filtered(listFileOptionCode))
1134 parseSymbolPatternsFile(arg, symbolPatterns);
1135 }
1136
createFiles(const InputArgList & args)1137 static void createFiles(const InputArgList &args) {
1138 TimeTraceScope timeScope("Load input files");
1139 // This loop should be reserved for options whose exact ordering matters.
1140 // Other options should be handled via filtered() and/or getLastArg().
1141 bool isLazy = false;
1142 for (const Arg *arg : args) {
1143 const Option &opt = arg->getOption();
1144 warnIfDeprecatedOption(opt);
1145 warnIfUnimplementedOption(opt);
1146
1147 switch (opt.getID()) {
1148 case OPT_INPUT:
1149 addFile(rerootPath(arg->getValue()), LoadType::CommandLine, isLazy);
1150 break;
1151 case OPT_needed_library:
1152 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1153 addFile(rerootPath(arg->getValue()), LoadType::CommandLine)))
1154 dylibFile->forceNeeded = true;
1155 break;
1156 case OPT_reexport_library:
1157 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1158 addFile(rerootPath(arg->getValue()), LoadType::CommandLine))) {
1159 config->hasReexports = true;
1160 dylibFile->reexport = true;
1161 }
1162 break;
1163 case OPT_weak_library:
1164 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
1165 addFile(rerootPath(arg->getValue()), LoadType::CommandLine)))
1166 dylibFile->forceWeakImport = true;
1167 break;
1168 case OPT_filelist:
1169 addFileList(arg->getValue(), isLazy);
1170 break;
1171 case OPT_force_load:
1172 addFile(rerootPath(arg->getValue()), LoadType::CommandLineForce);
1173 break;
1174 case OPT_load_hidden:
1175 addFile(rerootPath(arg->getValue()), LoadType::CommandLine,
1176 /*isLazy=*/false, /*isExplicit=*/true, /*isBundleLoader=*/false,
1177 /*isForceHidden=*/true);
1178 break;
1179 case OPT_l:
1180 case OPT_needed_l:
1181 case OPT_reexport_l:
1182 case OPT_weak_l:
1183 case OPT_hidden_l:
1184 addLibrary(arg->getValue(), opt.getID() == OPT_needed_l,
1185 opt.getID() == OPT_weak_l, opt.getID() == OPT_reexport_l,
1186 opt.getID() == OPT_hidden_l,
1187 /*isExplicit=*/true, LoadType::CommandLine);
1188 break;
1189 case OPT_framework:
1190 case OPT_needed_framework:
1191 case OPT_reexport_framework:
1192 case OPT_weak_framework:
1193 addFramework(arg->getValue(), opt.getID() == OPT_needed_framework,
1194 opt.getID() == OPT_weak_framework,
1195 opt.getID() == OPT_reexport_framework, /*isExplicit=*/true,
1196 LoadType::CommandLine);
1197 break;
1198 case OPT_start_lib:
1199 if (isLazy)
1200 error("nested --start-lib");
1201 isLazy = true;
1202 break;
1203 case OPT_end_lib:
1204 if (!isLazy)
1205 error("stray --end-lib");
1206 isLazy = false;
1207 break;
1208 default:
1209 break;
1210 }
1211 }
1212 }
1213
gatherInputSections()1214 static void gatherInputSections() {
1215 TimeTraceScope timeScope("Gathering input sections");
1216 int inputOrder = 0;
1217 for (const InputFile *file : inputFiles) {
1218 for (const Section *section : file->sections) {
1219 // Compact unwind entries require special handling elsewhere. (In
1220 // contrast, EH frames are handled like regular ConcatInputSections.)
1221 if (section->name == section_names::compactUnwind)
1222 continue;
1223 ConcatOutputSection *osec = nullptr;
1224 for (const Subsection &subsection : section->subsections) {
1225 if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) {
1226 if (isec->isCoalescedWeak())
1227 continue;
1228 if (config->emitInitOffsets &&
1229 sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS) {
1230 in.initOffsets->addInput(isec);
1231 continue;
1232 }
1233 isec->outSecOff = inputOrder++;
1234 if (!osec)
1235 osec = ConcatOutputSection::getOrCreateForInput(isec);
1236 isec->parent = osec;
1237 inputSections.push_back(isec);
1238 } else if (auto *isec =
1239 dyn_cast<CStringInputSection>(subsection.isec)) {
1240 if (isec->getName() == section_names::objcMethname) {
1241 if (in.objcMethnameSection->inputOrder == UnspecifiedInputOrder)
1242 in.objcMethnameSection->inputOrder = inputOrder++;
1243 in.objcMethnameSection->addInput(isec);
1244 } else {
1245 if (in.cStringSection->inputOrder == UnspecifiedInputOrder)
1246 in.cStringSection->inputOrder = inputOrder++;
1247 in.cStringSection->addInput(isec);
1248 }
1249 } else if (auto *isec =
1250 dyn_cast<WordLiteralInputSection>(subsection.isec)) {
1251 if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder)
1252 in.wordLiteralSection->inputOrder = inputOrder++;
1253 in.wordLiteralSection->addInput(isec);
1254 } else {
1255 llvm_unreachable("unexpected input section kind");
1256 }
1257 }
1258 }
1259 if (!file->objCImageInfo.empty())
1260 in.objCImageInfo->addFile(file);
1261 }
1262 assert(inputOrder <= UnspecifiedInputOrder);
1263 }
1264
foldIdenticalLiterals()1265 static void foldIdenticalLiterals() {
1266 TimeTraceScope timeScope("Fold identical literals");
1267 // We always create a cStringSection, regardless of whether dedupLiterals is
1268 // true. If it isn't, we simply create a non-deduplicating CStringSection.
1269 // Either way, we must unconditionally finalize it here.
1270 in.cStringSection->finalizeContents();
1271 in.objcMethnameSection->finalizeContents();
1272 in.wordLiteralSection->finalizeContents();
1273 }
1274
addSynthenticMethnames()1275 static void addSynthenticMethnames() {
1276 std::string &data = *make<std::string>();
1277 llvm::raw_string_ostream os(data);
1278 const int prefixLength = ObjCStubsSection::symbolPrefix.size();
1279 for (Symbol *sym : symtab->getSymbols())
1280 if (isa<Undefined>(sym))
1281 if (sym->getName().starts_with(ObjCStubsSection::symbolPrefix))
1282 os << sym->getName().drop_front(prefixLength) << '\0';
1283
1284 if (data.empty())
1285 return;
1286
1287 const auto *buf = reinterpret_cast<const uint8_t *>(data.c_str());
1288 Section §ion = *make<Section>(/*file=*/nullptr, segment_names::text,
1289 section_names::objcMethname,
1290 S_CSTRING_LITERALS, /*addr=*/0);
1291
1292 auto *isec =
1293 make<CStringInputSection>(section, ArrayRef<uint8_t>{buf, data.size()},
1294 /*align=*/1, /*dedupLiterals=*/true);
1295 isec->splitIntoPieces();
1296 for (auto &piece : isec->pieces)
1297 piece.live = true;
1298 section.subsections.push_back({0, isec});
1299 in.objcMethnameSection->addInput(isec);
1300 in.objcMethnameSection->isec->markLive(0);
1301 }
1302
referenceStubBinder()1303 static void referenceStubBinder() {
1304 bool needsStubHelper = config->outputType == MH_DYLIB ||
1305 config->outputType == MH_EXECUTE ||
1306 config->outputType == MH_BUNDLE;
1307 if (!needsStubHelper || !symtab->find("dyld_stub_binder"))
1308 return;
1309
1310 // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here
1311 // adds a opportunistic reference to dyld_stub_binder if it happens to exist.
1312 // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This
1313 // isn't needed for correctness, but the presence of that symbol suppresses
1314 // "no symbols" diagnostics from `nm`.
1315 // StubHelperSection::setUp() adds a reference and errors out if
1316 // dyld_stub_binder doesn't exist in case it is actually needed.
1317 symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/false);
1318 }
1319
createAliases()1320 static void createAliases() {
1321 for (const auto &pair : config->aliasedSymbols) {
1322 if (const auto &sym = symtab->find(pair.first)) {
1323 if (const auto &defined = dyn_cast<Defined>(sym)) {
1324 symtab->aliasDefined(defined, pair.second, defined->getFile())
1325 ->noDeadStrip = true;
1326 } else {
1327 error("TODO: support aliasing to symbols of kind " +
1328 Twine(sym->kind()));
1329 }
1330 } else {
1331 warn("undefined base symbol '" + pair.first + "' for alias '" +
1332 pair.second + "'\n");
1333 }
1334 }
1335
1336 for (const InputFile *file : inputFiles) {
1337 if (auto *objFile = dyn_cast<ObjFile>(file)) {
1338 for (const AliasSymbol *alias : objFile->aliases) {
1339 if (const auto &aliased = symtab->find(alias->getAliasedName())) {
1340 if (const auto &defined = dyn_cast<Defined>(aliased)) {
1341 symtab->aliasDefined(defined, alias->getName(), alias->getFile(),
1342 alias->privateExtern);
1343 } else {
1344 // Common, dylib, and undefined symbols are all valid alias
1345 // referents (undefineds can become valid Defined symbols later on
1346 // in the link.)
1347 error("TODO: support aliasing to symbols of kind " +
1348 Twine(aliased->kind()));
1349 }
1350 } else {
1351 // This shouldn't happen since MC generates undefined symbols to
1352 // represent the alias referents. Thus we fatal() instead of just
1353 // warning here.
1354 fatal("unable to find alias referent " + alias->getAliasedName() +
1355 " for " + alias->getName());
1356 }
1357 }
1358 }
1359 }
1360 }
1361
handleExplicitExports()1362 static void handleExplicitExports() {
1363 static constexpr int kMaxWarnings = 3;
1364 if (config->hasExplicitExports) {
1365 std::atomic<uint64_t> warningsCount{0};
1366 parallelForEach(symtab->getSymbols(), [&warningsCount](Symbol *sym) {
1367 if (auto *defined = dyn_cast<Defined>(sym)) {
1368 if (config->exportedSymbols.match(sym->getName())) {
1369 if (defined->privateExtern) {
1370 if (defined->weakDefCanBeHidden) {
1371 // weak_def_can_be_hidden symbols behave similarly to
1372 // private_extern symbols in most cases, except for when
1373 // it is explicitly exported.
1374 // The former can be exported but the latter cannot.
1375 defined->privateExtern = false;
1376 } else {
1377 // Only print the first 3 warnings verbosely, and
1378 // shorten the rest to avoid crowding logs.
1379 if (warningsCount.fetch_add(1, std::memory_order_relaxed) <
1380 kMaxWarnings)
1381 warn("cannot export hidden symbol " + toString(*defined) +
1382 "\n>>> defined in " + toString(defined->getFile()));
1383 }
1384 }
1385 } else {
1386 defined->privateExtern = true;
1387 }
1388 } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
1389 dysym->shouldReexport = config->exportedSymbols.match(sym->getName());
1390 }
1391 });
1392 if (warningsCount > kMaxWarnings)
1393 warn("<... " + Twine(warningsCount - kMaxWarnings) +
1394 " more similar warnings...>");
1395 } else if (!config->unexportedSymbols.empty()) {
1396 parallelForEach(symtab->getSymbols(), [](Symbol *sym) {
1397 if (auto *defined = dyn_cast<Defined>(sym))
1398 if (config->unexportedSymbols.match(defined->getName()))
1399 defined->privateExtern = true;
1400 });
1401 }
1402 }
1403
1404 namespace lld {
1405 namespace macho {
link(ArrayRef<const char * > argsArr,llvm::raw_ostream & stdoutOS,llvm::raw_ostream & stderrOS,bool exitEarly,bool disableOutput)1406 bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
1407 llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
1408 // This driver-specific context will be freed later by lldMain().
1409 auto *ctx = new CommonLinkerContext;
1410
1411 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
1412 ctx->e.cleanupCallback = []() {
1413 resolvedFrameworks.clear();
1414 resolvedLibraries.clear();
1415 cachedReads.clear();
1416 concatOutputSections.clear();
1417 inputFiles.clear();
1418 inputSections.clear();
1419 loadedArchives.clear();
1420 loadedObjectFrameworks.clear();
1421 missingAutolinkWarnings.clear();
1422 syntheticSections.clear();
1423 thunkMap.clear();
1424 unprocessedLCLinkerOptions.clear();
1425
1426 firstTLVDataSection = nullptr;
1427 tar = nullptr;
1428 memset(&in, 0, sizeof(in));
1429
1430 resetLoadedDylibs();
1431 resetOutputSegments();
1432 resetWriter();
1433 InputFile::resetIdCount();
1434 };
1435
1436 ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]);
1437
1438 MachOOptTable parser;
1439 InputArgList args = parser.parse(argsArr.slice(1));
1440
1441 ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now "
1442 "(use --error-limit=0 to see all errors)";
1443 ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
1444 ctx->e.verbose = args.hasArg(OPT_verbose);
1445
1446 if (args.hasArg(OPT_help_hidden)) {
1447 parser.printHelp(argsArr[0], /*showHidden=*/true);
1448 return true;
1449 }
1450 if (args.hasArg(OPT_help)) {
1451 parser.printHelp(argsArr[0], /*showHidden=*/false);
1452 return true;
1453 }
1454 if (args.hasArg(OPT_version)) {
1455 message(getLLDVersion());
1456 return true;
1457 }
1458
1459 config = std::make_unique<Configuration>();
1460 symtab = std::make_unique<SymbolTable>();
1461 config->outputType = getOutputType(args);
1462 target = createTargetInfo(args);
1463 depTracker = std::make_unique<DependencyTracker>(
1464 args.getLastArgValue(OPT_dependency_info));
1465
1466 config->ltoo = args::getInteger(args, OPT_lto_O, 2);
1467 if (config->ltoo > 3)
1468 error("--lto-O: invalid optimization level: " + Twine(config->ltoo));
1469 unsigned ltoCgo =
1470 args::getInteger(args, OPT_lto_CGO, args::getCGOptLevel(config->ltoo));
1471 if (auto level = CodeGenOpt::getLevel(ltoCgo))
1472 config->ltoCgo = *level;
1473 else
1474 error("--lto-CGO: invalid codegen optimization level: " + Twine(ltoCgo));
1475
1476 if (errorCount())
1477 return false;
1478
1479 if (args.hasArg(OPT_pagezero_size)) {
1480 uint64_t pagezeroSize = args::getHex(args, OPT_pagezero_size, 0);
1481
1482 // ld64 does something really weird. It attempts to realign the value to the
1483 // page size, but assumes the page size is 4K. This doesn't work with most
1484 // of Apple's ARM64 devices, which use a page size of 16K. This means that
1485 // it will first 4K align it by rounding down, then round up to 16K. This
1486 // probably only happened because no one using this arg with anything other
1487 // then 0, so no one checked if it did what is what it says it does.
1488
1489 // So we are not copying this weird behavior and doing the it in a logical
1490 // way, by always rounding down to page size.
1491 if (!isAligned(Align(target->getPageSize()), pagezeroSize)) {
1492 pagezeroSize -= pagezeroSize % target->getPageSize();
1493 warn("__PAGEZERO size is not page aligned, rounding down to 0x" +
1494 Twine::utohexstr(pagezeroSize));
1495 }
1496
1497 target->pageZeroSize = pagezeroSize;
1498 }
1499
1500 config->osoPrefix = args.getLastArgValue(OPT_oso_prefix);
1501 if (!config->osoPrefix.empty()) {
1502 // Expand special characters, such as ".", "..", or "~", if present.
1503 // Note: LD64 only expands "." and not other special characters.
1504 // That seems silly to imitate so we will not try to follow it, but rather
1505 // just use real_path() to do it.
1506
1507 // The max path length is 4096, in theory. However that seems quite long
1508 // and seems unlikely that any one would want to strip everything from the
1509 // path. Hence we've picked a reasonably large number here.
1510 SmallString<1024> expanded;
1511 if (!fs::real_path(config->osoPrefix, expanded,
1512 /*expand_tilde=*/true)) {
1513 // Note: LD64 expands "." to be `<current_dir>/`
1514 // (ie., it has a slash suffix) whereas real_path() doesn't.
1515 // So we have to append '/' to be consistent.
1516 StringRef sep = sys::path::get_separator();
1517 // real_path removes trailing slashes as part of the normalization, but
1518 // these are meaningful for our text based stripping
1519 if (config->osoPrefix.equals(".") || config->osoPrefix.ends_with(sep))
1520 expanded += sep;
1521 config->osoPrefix = saver().save(expanded.str());
1522 }
1523 }
1524
1525 bool pie = args.hasFlag(OPT_pie, OPT_no_pie, true);
1526 if (!supportsNoPie() && !pie) {
1527 warn("-no_pie ignored for arm64");
1528 pie = true;
1529 }
1530
1531 config->isPic = config->outputType == MH_DYLIB ||
1532 config->outputType == MH_BUNDLE ||
1533 (config->outputType == MH_EXECUTE && pie);
1534
1535 // Must be set before any InputSections and Symbols are created.
1536 config->deadStrip = args.hasArg(OPT_dead_strip);
1537
1538 config->systemLibraryRoots = getSystemLibraryRoots(args);
1539 if (const char *path = getReproduceOption(args)) {
1540 // Note that --reproduce is a debug option so you can ignore it
1541 // if you are trying to understand the whole picture of the code.
1542 Expected<std::unique_ptr<TarWriter>> errOrWriter =
1543 TarWriter::create(path, path::stem(path));
1544 if (errOrWriter) {
1545 tar = std::move(*errOrWriter);
1546 tar->append("response.txt", createResponseFile(args));
1547 tar->append("version.txt", getLLDVersion() + "\n");
1548 } else {
1549 error("--reproduce: " + toString(errOrWriter.takeError()));
1550 }
1551 }
1552
1553 if (auto *arg = args.getLastArg(OPT_threads_eq)) {
1554 StringRef v(arg->getValue());
1555 unsigned threads = 0;
1556 if (!llvm::to_integer(v, threads, 0) || threads == 0)
1557 error(arg->getSpelling() + ": expected a positive integer, but got '" +
1558 arg->getValue() + "'");
1559 parallel::strategy = hardware_concurrency(threads);
1560 config->thinLTOJobs = v;
1561 }
1562 if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
1563 config->thinLTOJobs = arg->getValue();
1564 if (!get_threadpool_strategy(config->thinLTOJobs))
1565 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
1566
1567 for (const Arg *arg : args.filtered(OPT_u)) {
1568 config->explicitUndefineds.push_back(symtab->addUndefined(
1569 arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
1570 }
1571
1572 for (const Arg *arg : args.filtered(OPT_U))
1573 config->explicitDynamicLookups.insert(arg->getValue());
1574
1575 config->mapFile = args.getLastArgValue(OPT_map);
1576 config->optimize = args::getInteger(args, OPT_O, 1);
1577 config->outputFile = args.getLastArgValue(OPT_o, "a.out");
1578 config->finalOutput =
1579 args.getLastArgValue(OPT_final_output, config->outputFile);
1580 config->astPaths = args.getAllArgValues(OPT_add_ast_path);
1581 config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
1582 config->headerPadMaxInstallNames =
1583 args.hasArg(OPT_headerpad_max_install_names);
1584 config->printDylibSearch =
1585 args.hasArg(OPT_print_dylib_search) || getenv("RC_TRACE_DYLIB_SEARCHING");
1586 config->printEachFile = args.hasArg(OPT_t);
1587 config->printWhyLoad = args.hasArg(OPT_why_load);
1588 config->omitDebugInfo = args.hasArg(OPT_S);
1589 config->errorForArchMismatch = args.hasArg(OPT_arch_errors_fatal);
1590 if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
1591 if (config->outputType != MH_BUNDLE)
1592 error("-bundle_loader can only be used with MachO bundle output");
1593 addFile(arg->getValue(), LoadType::CommandLine, /*isLazy=*/false,
1594 /*isExplicit=*/false, /*isBundleLoader=*/true);
1595 }
1596 for (auto *arg : args.filtered(OPT_dyld_env)) {
1597 StringRef envPair(arg->getValue());
1598 if (!envPair.contains('='))
1599 error("-dyld_env's argument is malformed. Expected "
1600 "-dyld_env <ENV_VAR>=<VALUE>, got `" +
1601 envPair + "`");
1602 config->dyldEnvs.push_back(envPair);
1603 }
1604 if (!config->dyldEnvs.empty() && config->outputType != MH_EXECUTE)
1605 error("-dyld_env can only be used when creating executable output");
1606
1607 if (const Arg *arg = args.getLastArg(OPT_umbrella)) {
1608 if (config->outputType != MH_DYLIB)
1609 warn("-umbrella used, but not creating dylib");
1610 config->umbrella = arg->getValue();
1611 }
1612 config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
1613 config->thinLTOCacheDir = args.getLastArgValue(OPT_cache_path_lto);
1614 config->thinLTOCachePolicy = getLTOCachePolicy(args);
1615 config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
1616 config->thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) ||
1617 args.hasArg(OPT_thinlto_index_only) ||
1618 args.hasArg(OPT_thinlto_index_only_eq);
1619 config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
1620 args.hasArg(OPT_thinlto_index_only_eq);
1621 config->thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
1622 config->thinLTOObjectSuffixReplace =
1623 getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
1624 std::tie(config->thinLTOPrefixReplaceOld, config->thinLTOPrefixReplaceNew,
1625 config->thinLTOPrefixReplaceNativeObject) =
1626 getOldNewOptionsExtra(args, OPT_thinlto_prefix_replace_eq);
1627 if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) {
1628 if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
1629 error("--thinlto-object-suffix-replace is not supported with "
1630 "--thinlto-emit-index-files");
1631 else if (args.hasArg(OPT_thinlto_prefix_replace_eq))
1632 error("--thinlto-prefix-replace is not supported with "
1633 "--thinlto-emit-index-files");
1634 }
1635 if (!config->thinLTOPrefixReplaceNativeObject.empty() &&
1636 config->thinLTOIndexOnlyArg.empty()) {
1637 error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
1638 "--thinlto-index-only=");
1639 }
1640 config->runtimePaths = args::getStrings(args, OPT_rpath);
1641 config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false);
1642 config->archMultiple = args.hasArg(OPT_arch_multiple);
1643 config->applicationExtension = args.hasFlag(
1644 OPT_application_extension, OPT_no_application_extension, false);
1645 config->exportDynamic = args.hasArg(OPT_export_dynamic);
1646 config->forceLoadObjC = args.hasArg(OPT_ObjC);
1647 config->forceLoadSwift = args.hasArg(OPT_force_load_swift_libs);
1648 config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs);
1649 config->demangle = args.hasArg(OPT_demangle);
1650 config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
1651 config->emitFunctionStarts =
1652 args.hasFlag(OPT_function_starts, OPT_no_function_starts, true);
1653 config->emitDataInCodeInfo =
1654 args.hasFlag(OPT_data_in_code_info, OPT_no_data_in_code_info, true);
1655 config->emitChainedFixups = shouldEmitChainedFixups(args);
1656 config->emitInitOffsets =
1657 config->emitChainedFixups || args.hasArg(OPT_init_offsets);
1658 config->icfLevel = getICFLevel(args);
1659 config->dedupStrings =
1660 args.hasFlag(OPT_deduplicate_strings, OPT_no_deduplicate_strings, true);
1661 config->deadStripDuplicates = args.hasArg(OPT_dead_strip_duplicates);
1662 config->warnDylibInstallName = args.hasFlag(
1663 OPT_warn_dylib_install_name, OPT_no_warn_dylib_install_name, false);
1664 config->ignoreOptimizationHints = args.hasArg(OPT_ignore_optimization_hints);
1665 config->callGraphProfileSort = args.hasFlag(
1666 OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
1667 config->printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order_eq);
1668 config->forceExactCpuSubtypeMatch =
1669 getenv("LD_DYLIB_CPU_SUBTYPES_MUST_MATCH");
1670 config->objcStubsMode = getObjCStubsMode(args);
1671 config->ignoreAutoLink = args.hasArg(OPT_ignore_auto_link);
1672 for (const Arg *arg : args.filtered(OPT_ignore_auto_link_option))
1673 config->ignoreAutoLinkOptions.insert(arg->getValue());
1674 config->strictAutoLink = args.hasArg(OPT_strict_auto_link);
1675 config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
1676 config->csProfileGenerate = args.hasArg(OPT_cs_profile_generate);
1677 config->csProfilePath = args.getLastArgValue(OPT_cs_profile_path);
1678 config->pgoWarnMismatch =
1679 args.hasFlag(OPT_pgo_warn_mismatch, OPT_no_pgo_warn_mismatch, true);
1680 config->generateUuid = !args.hasArg(OPT_no_uuid);
1681
1682 for (const Arg *arg : args.filtered(OPT_alias)) {
1683 config->aliasedSymbols.push_back(
1684 std::make_pair(arg->getValue(0), arg->getValue(1)));
1685 }
1686
1687 if (const char *zero = getenv("ZERO_AR_DATE"))
1688 config->zeroModTime = strcmp(zero, "0") != 0;
1689 if (args.getLastArg(OPT_reproducible))
1690 config->zeroModTime = true;
1691
1692 std::array<PlatformType, 3> encryptablePlatforms{
1693 PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS};
1694 config->emitEncryptionInfo =
1695 args.hasFlag(OPT_encryptable, OPT_no_encryption,
1696 is_contained(encryptablePlatforms, config->platform()));
1697
1698 if (const Arg *arg = args.getLastArg(OPT_install_name)) {
1699 if (config->warnDylibInstallName && config->outputType != MH_DYLIB)
1700 warn(
1701 arg->getAsString(args) +
1702 ": ignored, only has effect with -dylib [--warn-dylib-install-name]");
1703 else
1704 config->installName = arg->getValue();
1705 } else if (config->outputType == MH_DYLIB) {
1706 config->installName = config->finalOutput;
1707 }
1708
1709 if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
1710 if (config->outputType != MH_DYLIB)
1711 warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
1712 else
1713 config->markDeadStrippableDylib = true;
1714 }
1715
1716 if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
1717 config->staticLink = (arg->getOption().getID() == OPT_static);
1718
1719 if (const Arg *arg =
1720 args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
1721 config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
1722 ? NamespaceKind::twolevel
1723 : NamespaceKind::flat;
1724
1725 config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
1726
1727 if (config->outputType == MH_EXECUTE)
1728 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
1729 /*file=*/nullptr,
1730 /*isWeakRef=*/false);
1731
1732 config->librarySearchPaths =
1733 getLibrarySearchPaths(args, config->systemLibraryRoots);
1734 config->frameworkSearchPaths =
1735 getFrameworkSearchPaths(args, config->systemLibraryRoots);
1736 if (const Arg *arg =
1737 args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
1738 config->searchDylibsFirst =
1739 arg->getOption().getID() == OPT_search_dylibs_first;
1740
1741 config->dylibCompatibilityVersion =
1742 parseDylibVersion(args, OPT_compatibility_version);
1743 config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1744
1745 config->dataConst =
1746 args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args));
1747 // Populate config->sectionRenameMap with builtin default renames.
1748 // Options -rename_section and -rename_segment are able to override.
1749 initializeSectionRenameMap();
1750 // Reject every special character except '.' and '$'
1751 // TODO(gkm): verify that this is the proper set of invalid chars
1752 StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1753 auto validName = [invalidNameChars](StringRef s) {
1754 if (s.find_first_of(invalidNameChars) != StringRef::npos)
1755 error("invalid name for segment or section: " + s);
1756 return s;
1757 };
1758 for (const Arg *arg : args.filtered(OPT_rename_section)) {
1759 config->sectionRenameMap[{validName(arg->getValue(0)),
1760 validName(arg->getValue(1))}] = {
1761 validName(arg->getValue(2)), validName(arg->getValue(3))};
1762 }
1763 for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1764 config->segmentRenameMap[validName(arg->getValue(0))] =
1765 validName(arg->getValue(1));
1766 }
1767
1768 config->sectionAlignments = parseSectAlign(args);
1769
1770 for (const Arg *arg : args.filtered(OPT_segprot)) {
1771 StringRef segName = arg->getValue(0);
1772 uint32_t maxProt = parseProtection(arg->getValue(1));
1773 uint32_t initProt = parseProtection(arg->getValue(2));
1774 if (maxProt != initProt && config->arch() != AK_i386)
1775 error("invalid argument '" + arg->getAsString(args) +
1776 "': max and init must be the same for non-i386 archs");
1777 if (segName == segment_names::linkEdit)
1778 error("-segprot cannot be used to change __LINKEDIT's protections");
1779 config->segmentProtections.push_back({segName, maxProt, initProt});
1780 }
1781
1782 config->hasExplicitExports =
1783 args.hasArg(OPT_no_exported_symbols) ||
1784 args.hasArgNoClaim(OPT_exported_symbol, OPT_exported_symbols_list);
1785 handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
1786 OPT_exported_symbols_list);
1787 handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
1788 OPT_unexported_symbols_list);
1789 if (config->hasExplicitExports && !config->unexportedSymbols.empty())
1790 error("cannot use both -exported_symbol* and -unexported_symbol* options");
1791
1792 if (args.hasArg(OPT_no_exported_symbols) && !config->exportedSymbols.empty())
1793 error("cannot use both -exported_symbol* and -no_exported_symbols options");
1794
1795 // Imitating LD64's:
1796 // -non_global_symbols_no_strip_list and -non_global_symbols_strip_list can't
1797 // both be present.
1798 // But -x can be used with either of these two, in which case, the last arg
1799 // takes effect.
1800 // (TODO: This is kind of confusing - considering disallowing using them
1801 // together for a more straightforward behaviour)
1802 {
1803 bool includeLocal = false;
1804 bool excludeLocal = false;
1805 for (const Arg *arg :
1806 args.filtered(OPT_x, OPT_non_global_symbols_no_strip_list,
1807 OPT_non_global_symbols_strip_list)) {
1808 switch (arg->getOption().getID()) {
1809 case OPT_x:
1810 config->localSymbolsPresence = SymtabPresence::None;
1811 break;
1812 case OPT_non_global_symbols_no_strip_list:
1813 if (excludeLocal) {
1814 error("cannot use both -non_global_symbols_no_strip_list and "
1815 "-non_global_symbols_strip_list");
1816 } else {
1817 includeLocal = true;
1818 config->localSymbolsPresence = SymtabPresence::SelectivelyIncluded;
1819 parseSymbolPatternsFile(arg, config->localSymbolPatterns);
1820 }
1821 break;
1822 case OPT_non_global_symbols_strip_list:
1823 if (includeLocal) {
1824 error("cannot use both -non_global_symbols_no_strip_list and "
1825 "-non_global_symbols_strip_list");
1826 } else {
1827 excludeLocal = true;
1828 config->localSymbolsPresence = SymtabPresence::SelectivelyExcluded;
1829 parseSymbolPatternsFile(arg, config->localSymbolPatterns);
1830 }
1831 break;
1832 default:
1833 llvm_unreachable("unexpected option");
1834 }
1835 }
1836 }
1837 // Explicitly-exported literal symbols must be defined, but might
1838 // languish in an archive if unreferenced elsewhere or if they are in the
1839 // non-global strip list. Light a fire under those lazy symbols!
1840 for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
1841 symtab->addUndefined(cachedName.val(), /*file=*/nullptr,
1842 /*isWeakRef=*/false);
1843
1844 for (const Arg *arg : args.filtered(OPT_why_live))
1845 config->whyLive.insert(arg->getValue());
1846 if (!config->whyLive.empty() && !config->deadStrip)
1847 warn("-why_live has no effect without -dead_strip, ignoring");
1848
1849 config->saveTemps = args.hasArg(OPT_save_temps);
1850
1851 config->adhocCodesign = args.hasFlag(
1852 OPT_adhoc_codesign, OPT_no_adhoc_codesign,
1853 shouldAdhocSignByDefault(config->arch(), config->platform()));
1854
1855 if (args.hasArg(OPT_v)) {
1856 message(getLLDVersion(), lld::errs());
1857 message(StringRef("Library search paths:") +
1858 (config->librarySearchPaths.empty()
1859 ? ""
1860 : "\n\t" + join(config->librarySearchPaths, "\n\t")),
1861 lld::errs());
1862 message(StringRef("Framework search paths:") +
1863 (config->frameworkSearchPaths.empty()
1864 ? ""
1865 : "\n\t" + join(config->frameworkSearchPaths, "\n\t")),
1866 lld::errs());
1867 }
1868
1869 config->progName = argsArr[0];
1870
1871 config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
1872 config->timeTraceGranularity =
1873 args::getInteger(args, OPT_time_trace_granularity_eq, 500);
1874
1875 // Initialize time trace profiler.
1876 if (config->timeTraceEnabled)
1877 timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
1878
1879 {
1880 TimeTraceScope timeScope("ExecuteLinker");
1881
1882 initLLVM(); // must be run before any call to addFile()
1883 createFiles(args);
1884
1885 // Now that all dylibs have been loaded, search for those that should be
1886 // re-exported.
1887 {
1888 auto reexportHandler = [](const Arg *arg,
1889 const std::vector<StringRef> &extensions) {
1890 config->hasReexports = true;
1891 StringRef searchName = arg->getValue();
1892 if (!markReexport(searchName, extensions))
1893 error(arg->getSpelling() + " " + searchName +
1894 " does not match a supplied dylib");
1895 };
1896 std::vector<StringRef> extensions = {".tbd"};
1897 for (const Arg *arg : args.filtered(OPT_sub_umbrella))
1898 reexportHandler(arg, extensions);
1899
1900 extensions.push_back(".dylib");
1901 for (const Arg *arg : args.filtered(OPT_sub_library))
1902 reexportHandler(arg, extensions);
1903 }
1904
1905 cl::ResetAllOptionOccurrences();
1906
1907 // Parse LTO options.
1908 if (const Arg *arg = args.getLastArg(OPT_mcpu))
1909 parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())),
1910 arg->getSpelling());
1911
1912 for (const Arg *arg : args.filtered(OPT_mllvm)) {
1913 parseClangOption(arg->getValue(), arg->getSpelling());
1914 config->mllvmOpts.emplace_back(arg->getValue());
1915 }
1916
1917 createSyntheticSections();
1918 createSyntheticSymbols();
1919 addSynthenticMethnames();
1920
1921 createAliases();
1922 // If we are in "explicit exports" mode, hide everything that isn't
1923 // explicitly exported. Do this before running LTO so that LTO can better
1924 // optimize.
1925 handleExplicitExports();
1926
1927 bool didCompileBitcodeFiles = compileBitcodeFiles();
1928
1929 resolveLCLinkerOptions();
1930
1931 // If --thinlto-index-only is given, we should create only "index
1932 // files" and not object files. Index file creation is already done
1933 // in compileBitcodeFiles, so we are done if that's the case.
1934 if (config->thinLTOIndexOnly)
1935 return errorCount() == 0;
1936
1937 // LTO may emit a non-hidden (extern) object file symbol even if the
1938 // corresponding bitcode symbol is hidden. In particular, this happens for
1939 // cross-module references to hidden symbols under ThinLTO. Thus, if we
1940 // compiled any bitcode files, we must redo the symbol hiding.
1941 if (didCompileBitcodeFiles)
1942 handleExplicitExports();
1943 replaceCommonSymbols();
1944
1945 StringRef orderFile = args.getLastArgValue(OPT_order_file);
1946 if (!orderFile.empty())
1947 priorityBuilder.parseOrderFile(orderFile);
1948
1949 referenceStubBinder();
1950
1951 // FIXME: should terminate the link early based on errors encountered so
1952 // far?
1953
1954 for (const Arg *arg : args.filtered(OPT_sectcreate)) {
1955 StringRef segName = arg->getValue(0);
1956 StringRef sectName = arg->getValue(1);
1957 StringRef fileName = arg->getValue(2);
1958 std::optional<MemoryBufferRef> buffer = readFile(fileName);
1959 if (buffer)
1960 inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
1961 }
1962
1963 for (const Arg *arg : args.filtered(OPT_add_empty_section)) {
1964 StringRef segName = arg->getValue(0);
1965 StringRef sectName = arg->getValue(1);
1966 inputFiles.insert(make<OpaqueFile>(MemoryBufferRef(), segName, sectName));
1967 }
1968
1969 gatherInputSections();
1970 if (config->callGraphProfileSort)
1971 priorityBuilder.extractCallGraphProfile();
1972
1973 if (config->deadStrip)
1974 markLive();
1975
1976 if (args.hasArg(OPT_check_category_conflicts))
1977 objc::checkCategories();
1978
1979 // ICF assumes that all literals have been folded already, so we must run
1980 // foldIdenticalLiterals before foldIdenticalSections.
1981 foldIdenticalLiterals();
1982 if (config->icfLevel != ICFLevel::none) {
1983 if (config->icfLevel == ICFLevel::safe)
1984 markAddrSigSymbols();
1985 foldIdenticalSections(/*onlyCfStrings=*/false);
1986 } else if (config->dedupStrings) {
1987 foldIdenticalSections(/*onlyCfStrings=*/true);
1988 }
1989
1990 // Write to an output file.
1991 if (target->wordSize == 8)
1992 writeResult<LP64>();
1993 else
1994 writeResult<ILP32>();
1995
1996 depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
1997 }
1998
1999 if (config->timeTraceEnabled) {
2000 checkError(timeTraceProfilerWrite(
2001 args.getLastArgValue(OPT_time_trace_eq).str(), config->outputFile));
2002
2003 timeTraceProfilerCleanup();
2004 }
2005
2006 if (errorCount() != 0 || config->strictAutoLink)
2007 for (const auto &warning : missingAutolinkWarnings)
2008 warn(warning);
2009
2010 return errorCount() == 0;
2011 }
2012 } // namespace macho
2013 } // namespace lld
2014