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