1 //===- Driver.cpp ---------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
12 #include "InputFiles.h"
16 #include "OutputSection.h"
17 #include "OutputSegment.h"
18 #include "SymbolTable.h"
20 #include "SyntheticSections.h"
22 #include "UnwindInfoSection.h"
25 #include "lld/Common/Args.h"
26 #include "lld/Common/Driver.h"
27 #include "lld/Common/ErrorHandler.h"
28 #include "lld/Common/LLVM.h"
29 #include "lld/Common/Memory.h"
30 #include "lld/Common/Reproduce.h"
31 #include "lld/Common/Version.h"
32 #include "llvm/ADT/DenseSet.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/ADT/StringRef.h"
35 #include "llvm/BinaryFormat/MachO.h"
36 #include "llvm/BinaryFormat/Magic.h"
37 #include "llvm/Config/llvm-config.h"
38 #include "llvm/LTO/LTO.h"
39 #include "llvm/Object/Archive.h"
40 #include "llvm/Option/ArgList.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/Host.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/Parallel.h"
46 #include "llvm/Support/Path.h"
47 #include "llvm/Support/TarWriter.h"
48 #include "llvm/Support/TargetSelect.h"
49 #include "llvm/Support/TimeProfiler.h"
50 #include "llvm/TextAPI/PackedVersion.h"
55 using namespace llvm::MachO
;
56 using namespace llvm::object
;
57 using namespace llvm::opt
;
58 using namespace llvm::sys
;
60 using namespace lld::macho
;
62 Configuration
*macho::config
;
63 DependencyTracker
*macho::depTracker
;
65 static HeaderFileType
getOutputType(const InputArgList
&args
) {
66 // TODO: -r, -dylinker, -preload...
67 Arg
*outputArg
= args
.getLastArg(OPT_bundle
, OPT_dylib
, OPT_execute
);
68 if (outputArg
== nullptr)
71 switch (outputArg
->getOption().getID()) {
79 llvm_unreachable("internal error");
83 static Optional
<StringRef
> findLibrary(StringRef name
) {
84 if (config
->searchDylibsFirst
) {
85 if (Optional
<StringRef
> path
= findPathCombination(
86 "lib" + name
, config
->librarySearchPaths
, {".tbd", ".dylib"}))
88 return findPathCombination("lib" + name
, config
->librarySearchPaths
,
91 return findPathCombination("lib" + name
, config
->librarySearchPaths
,
92 {".tbd", ".dylib", ".a"});
95 static Optional
<std::string
> findFramework(StringRef name
) {
96 SmallString
<260> symlink
;
98 std::tie(name
, suffix
) = name
.split(",");
99 for (StringRef dir
: config
->frameworkSearchPaths
) {
101 path::append(symlink
, name
+ ".framework", name
);
103 if (!suffix
.empty()) {
104 // NOTE: we must resolve the symlink before trying the suffixes, because
105 // there are no symlinks for the suffixed paths.
106 SmallString
<260> location
;
107 if (!fs::real_path(symlink
, location
)) {
108 // only append suffix if realpath() succeeds
109 Twine suffixed
= location
+ suffix
;
110 if (fs::exists(suffixed
))
111 return suffixed
.str();
113 // Suffix lookup failed, fall through to the no-suffix case.
116 if (Optional
<std::string
> path
= resolveDylibPath(symlink
))
122 static bool warnIfNotDirectory(StringRef option
, StringRef path
) {
123 if (!fs::exists(path
)) {
124 warn("directory not found for option -" + option
+ path
);
126 } else if (!fs::is_directory(path
)) {
127 warn("option -" + option
+ path
+ " references a non-directory path");
133 static std::vector
<StringRef
>
134 getSearchPaths(unsigned optionCode
, InputArgList
&args
,
135 const std::vector
<StringRef
> &roots
,
136 const SmallVector
<StringRef
, 2> &systemPaths
) {
137 std::vector
<StringRef
> paths
;
138 StringRef optionLetter
{optionCode
== OPT_F
? "F" : "L"};
139 for (StringRef path
: args::getStrings(args
, optionCode
)) {
140 // NOTE: only absolute paths are re-rooted to syslibroot(s)
142 if (path::is_absolute(path
, path::Style::posix
)) {
143 for (StringRef root
: roots
) {
144 SmallString
<261> buffer(root
);
145 path::append(buffer
, path
);
146 // Do not warn about paths that are computed via the syslib roots
147 if (fs::is_directory(buffer
)) {
148 paths
.push_back(saver
.save(buffer
.str()));
153 if (!found
&& warnIfNotDirectory(optionLetter
, path
))
154 paths
.push_back(path
);
157 // `-Z` suppresses the standard "system" search paths.
158 if (args
.hasArg(OPT_Z
))
161 for (const StringRef
&path
: systemPaths
) {
162 for (const StringRef
&root
: roots
) {
163 SmallString
<261> buffer(root
);
164 path::append(buffer
, path
);
165 if (fs::is_directory(buffer
))
166 paths
.push_back(saver
.save(buffer
.str()));
172 static std::vector
<StringRef
> getSystemLibraryRoots(InputArgList
&args
) {
173 std::vector
<StringRef
> roots
;
174 for (const Arg
*arg
: args
.filtered(OPT_syslibroot
))
175 roots
.push_back(arg
->getValue());
176 // NOTE: the final `-syslibroot` being `/` will ignore all roots
177 if (roots
.size() && roots
.back() == "/")
179 // NOTE: roots can never be empty - add an empty root to simplify the library
180 // and framework search path computation.
182 roots
.emplace_back("");
186 static std::vector
<StringRef
>
187 getLibrarySearchPaths(InputArgList
&args
, const std::vector
<StringRef
> &roots
) {
188 return getSearchPaths(OPT_L
, args
, roots
, {"/usr/lib", "/usr/local/lib"});
191 static std::vector
<StringRef
>
192 getFrameworkSearchPaths(InputArgList
&args
,
193 const std::vector
<StringRef
> &roots
) {
194 return getSearchPaths(OPT_F
, args
, roots
,
195 {"/Library/Frameworks", "/System/Library/Frameworks"});
198 static llvm::CachePruningPolicy
getLTOCachePolicy(InputArgList
&args
) {
199 SmallString
<128> ltoPolicy
;
200 auto add
= [<oPolicy
](Twine val
) {
201 if (!ltoPolicy
.empty())
203 val
.toVector(ltoPolicy
);
205 for (const Arg
*arg
:
206 args
.filtered(OPT_thinlto_cache_policy
, OPT_prune_interval_lto
,
207 OPT_prune_after_lto
, OPT_max_relative_cache_size_lto
)) {
208 switch (arg
->getOption().getID()) {
209 case OPT_thinlto_cache_policy
: add(arg
->getValue()); break;
210 case OPT_prune_interval_lto
:
211 if (!strcmp("-1", arg
->getValue()))
212 add("prune_interval=87600h"); // 10 years
214 add(Twine("prune_interval=") + arg
->getValue() + "s");
216 case OPT_prune_after_lto
:
217 add(Twine("prune_after=") + arg
->getValue() + "s");
219 case OPT_max_relative_cache_size_lto
:
220 add(Twine("cache_size=") + arg
->getValue() + "%");
224 return CHECK(parseCachePruningPolicy(ltoPolicy
), "invalid LTO cache policy");
227 static DenseMap
<StringRef
, ArchiveFile
*> loadedArchives
;
229 static InputFile
*addFile(StringRef path
, bool forceLoadArchive
,
230 bool isExplicit
= true, bool isBundleLoader
= false) {
231 Optional
<MemoryBufferRef
> buffer
= readFile(path
);
234 MemoryBufferRef mbref
= *buffer
;
235 InputFile
*newFile
= nullptr;
237 file_magic magic
= identify_magic(mbref
.getBuffer());
239 case file_magic::archive
: {
240 // Avoid loading archives twice. If the archives are being force-loaded,
241 // loading them twice would create duplicate symbol errors. In the
242 // non-force-loading case, this is just a minor performance optimization.
243 // We don't take a reference to cachedFile here because the
244 // loadArchiveMember() call below may recursively call addFile() and
245 // invalidate this reference.
246 if (ArchiveFile
*cachedFile
= loadedArchives
[path
])
249 std::unique_ptr
<object::Archive
> archive
= CHECK(
250 object::Archive::create(mbref
), path
+ ": failed to parse archive");
252 if (!archive
->isEmpty() && !archive
->hasSymbolTable())
253 error(path
+ ": archive has no index; run ranlib to add one");
255 auto *file
= make
<ArchiveFile
>(std::move(archive
));
256 if (config
->allLoad
|| forceLoadArchive
) {
257 if (Optional
<MemoryBufferRef
> buffer
= readFile(path
)) {
258 Error e
= Error::success();
259 for (const object::Archive::Child
&c
: file
->getArchive().children(e
)) {
260 StringRef reason
= forceLoadArchive
? "-force_load" : "-all_load";
261 if (Error e
= file
->fetch(c
, reason
))
262 error(toString(file
) + ": " + reason
+
263 " failed to load archive member: " + toString(std::move(e
)));
266 error(toString(file
) +
267 ": Archive::children failed: " + toString(std::move(e
)));
269 } else if (config
->forceLoadObjC
) {
270 for (const object::Archive::Symbol
&sym
: file
->getArchive().symbols())
271 if (sym
.getName().startswith(objc::klass
))
274 // TODO: no need to look for ObjC sections for a given archive member if
275 // we already found that it contains an ObjC symbol.
276 if (Optional
<MemoryBufferRef
> buffer
= readFile(path
)) {
277 Error e
= Error::success();
278 for (const object::Archive::Child
&c
: file
->getArchive().children(e
)) {
279 Expected
<MemoryBufferRef
> mb
= c
.getMemoryBufferRef();
280 if (!mb
|| !hasObjCSection(*mb
))
282 if (Error e
= file
->fetch(c
, "-ObjC"))
283 error(toString(file
) + ": -ObjC failed to load archive member: " +
284 toString(std::move(e
)));
287 error(toString(file
) +
288 ": Archive::children failed: " + toString(std::move(e
)));
292 file
->addLazySymbols();
293 newFile
= loadedArchives
[path
] = file
;
296 case file_magic::macho_object
:
297 newFile
= make
<ObjFile
>(mbref
, getModTime(path
), "");
299 case file_magic::macho_dynamically_linked_shared_lib
:
300 case file_magic::macho_dynamically_linked_shared_lib_stub
:
301 case file_magic::tapi_file
:
302 if (DylibFile
*dylibFile
= loadDylib(mbref
)) {
304 dylibFile
->explicitlyLinked
= true;
308 case file_magic::bitcode
:
309 newFile
= make
<BitcodeFile
>(mbref
, "", 0);
311 case file_magic::macho_executable
:
312 case file_magic::macho_bundle
:
313 // We only allow executable and bundle type here if it is used
314 // as a bundle loader.
316 error(path
+ ": unhandled file type");
317 if (DylibFile
*dylibFile
= loadDylib(mbref
, nullptr, isBundleLoader
))
321 error(path
+ ": unhandled file type");
323 if (newFile
&& !isa
<DylibFile
>(newFile
)) {
324 // printArchiveMemberLoad() prints both .a and .o names, so no need to
325 // print the .a name here.
326 if (config
->printEachFile
&& magic
!= file_magic::archive
)
327 message(toString(newFile
));
328 inputFiles
.insert(newFile
);
333 static void addLibrary(StringRef name
, bool isNeeded
, bool isWeak
,
334 bool isReexport
, bool isExplicit
, bool forceLoad
) {
335 if (Optional
<StringRef
> path
= findLibrary(name
)) {
336 if (auto *dylibFile
= dyn_cast_or_null
<DylibFile
>(
337 addFile(*path
, forceLoad
, isExplicit
))) {
339 dylibFile
->forceNeeded
= true;
341 dylibFile
->forceWeakImport
= true;
343 config
->hasReexports
= true;
344 dylibFile
->reexport
= true;
349 error("library not found for -l" + name
);
352 static void addFramework(StringRef name
, bool isNeeded
, bool isWeak
,
353 bool isReexport
, bool isExplicit
) {
354 if (Optional
<std::string
> path
= findFramework(name
)) {
355 if (auto *dylibFile
= dyn_cast_or_null
<DylibFile
>(
356 addFile(*path
, /*forceLoadArchive=*/false, isExplicit
))) {
358 dylibFile
->forceNeeded
= true;
360 dylibFile
->forceWeakImport
= true;
362 config
->hasReexports
= true;
363 dylibFile
->reexport
= true;
368 error("framework not found for -framework " + name
);
371 // Parses LC_LINKER_OPTION contents, which can add additional command line
373 void macho::parseLCLinkerOption(InputFile
*f
, unsigned argc
, StringRef data
) {
374 SmallVector
<const char *, 4> argv
;
376 for (unsigned i
= 0; i
< argc
&& offset
< data
.size(); ++i
) {
377 argv
.push_back(data
.data() + offset
);
378 offset
+= strlen(data
.data() + offset
) + 1;
380 if (argv
.size() != argc
|| offset
> data
.size())
381 fatal(toString(f
) + ": invalid LC_LINKER_OPTION");
384 unsigned missingIndex
, missingCount
;
385 InputArgList args
= table
.ParseArgs(argv
, missingIndex
, missingCount
);
387 fatal(Twine(args
.getArgString(missingIndex
)) + ": missing argument");
388 for (const Arg
*arg
: args
.filtered(OPT_UNKNOWN
))
389 error("unknown argument: " + arg
->getAsString(args
));
391 for (const Arg
*arg
: args
) {
392 switch (arg
->getOption().getID()) {
394 StringRef name
= arg
->getValue();
396 config
->forceLoadSwift
? name
.startswith("swift") : false;
397 addLibrary(name
, /*isNeeded=*/false, /*isWeak=*/false,
398 /*isReexport=*/false, /*isExplicit=*/false, forceLoad
);
402 addFramework(arg
->getValue(), /*isNeeded=*/false, /*isWeak=*/false,
403 /*isReexport=*/false, /*isExplicit=*/false);
406 error(arg
->getSpelling() + " is not allowed in LC_LINKER_OPTION");
411 static void addFileList(StringRef path
) {
412 Optional
<MemoryBufferRef
> buffer
= readFile(path
);
415 MemoryBufferRef mbref
= *buffer
;
416 for (StringRef path
: args::getLines(mbref
))
417 addFile(rerootPath(path
), /*forceLoadArchive=*/false);
420 // An order file has one entry per line, in the following format:
422 // <cpu>:<object file>:<symbol name>
424 // <cpu> and <object file> are optional. If not specified, then that entry
425 // matches any symbol of that name. Parsing this format is not quite
426 // straightforward because the symbol name itself can contain colons, so when
427 // encountering a colon, we consider the preceding characters to decide if it
428 // can be a valid CPU type or file path.
430 // If a symbol is matched by multiple entries, then it takes the lowest-ordered
431 // entry (the one nearest to the front of the list.)
433 // The file can also have line comments that start with '#'.
434 static void parseOrderFile(StringRef path
) {
435 Optional
<MemoryBufferRef
> buffer
= readFile(path
);
437 error("Could not read order file at " + path
);
441 MemoryBufferRef mbref
= *buffer
;
442 size_t priority
= std::numeric_limits
<size_t>::max();
443 for (StringRef line
: args::getLines(mbref
)) {
444 StringRef objectFile
, symbol
;
445 line
= line
.take_until([](char c
) { return c
== '#'; }); // ignore comments
448 CPUType cpuType
= StringSwitch
<CPUType
>(line
)
449 .StartsWith("i386:", CPU_TYPE_I386
)
450 .StartsWith("x86_64:", CPU_TYPE_X86_64
)
451 .StartsWith("arm:", CPU_TYPE_ARM
)
452 .StartsWith("arm64:", CPU_TYPE_ARM64
)
453 .StartsWith("ppc:", CPU_TYPE_POWERPC
)
454 .StartsWith("ppc64:", CPU_TYPE_POWERPC64
)
455 .Default(CPU_TYPE_ANY
);
457 if (cpuType
!= CPU_TYPE_ANY
&& cpuType
!= target
->cpuType
)
460 // Drop the CPU type as well as the colon
461 if (cpuType
!= CPU_TYPE_ANY
)
462 line
= line
.drop_until([](char c
) { return c
== ':'; }).drop_front();
464 constexpr std::array
<StringRef
, 2> fileEnds
= {".o:", ".o):"};
465 for (StringRef fileEnd
: fileEnds
) {
466 size_t pos
= line
.find(fileEnd
);
467 if (pos
!= StringRef::npos
) {
468 // Split the string around the colon
469 objectFile
= line
.take_front(pos
+ fileEnd
.size() - 1);
470 line
= line
.drop_front(pos
+ fileEnd
.size());
474 symbol
= line
.trim();
476 if (!symbol
.empty()) {
477 SymbolPriorityEntry
&entry
= config
->priorities
[symbol
];
478 if (!objectFile
.empty())
479 entry
.objectFiles
.insert(std::make_pair(objectFile
, priority
));
481 entry
.anyObjectFile
= std::max(entry
.anyObjectFile
, priority
);
488 // We expect sub-library names of the form "libfoo", which will match a dylib
489 // with a path of .*/libfoo.{dylib, tbd}.
490 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
491 // I'm not sure what the use case for that is.
492 static bool markReexport(StringRef searchName
, ArrayRef
<StringRef
> extensions
) {
493 for (InputFile
*file
: inputFiles
) {
494 if (auto *dylibFile
= dyn_cast
<DylibFile
>(file
)) {
495 StringRef filename
= path::filename(dylibFile
->getName());
496 if (filename
.consume_front(searchName
) &&
498 find(extensions
, filename
) != extensions
.end())) {
499 dylibFile
->reexport
= true;
507 // This function is called on startup. We need this for LTO since
508 // LTO calls LLVM functions to compile bitcode files to native code.
509 // Technically this can be delayed until we read bitcode files, but
510 // we don't bother to do lazily because the initialization is fast.
511 static void initLLVM() {
512 InitializeAllTargets();
513 InitializeAllTargetMCs();
514 InitializeAllAsmPrinters();
515 InitializeAllAsmParsers();
518 static void compileBitcodeFiles() {
519 // FIXME: Remove this once LTO.cpp honors config->exportDynamic.
520 if (config
->exportDynamic
)
521 for (InputFile
*file
: inputFiles
)
522 if (isa
<BitcodeFile
>(file
)) {
523 warn("the effect of -export_dynamic on LTO is not yet implemented");
527 TimeTraceScope
timeScope("LTO");
528 auto *lto
= make
<BitcodeCompiler
>();
529 for (InputFile
*file
: inputFiles
)
530 if (auto *bitcodeFile
= dyn_cast
<BitcodeFile
>(file
))
531 lto
->add(*bitcodeFile
);
533 for (ObjFile
*file
: lto
->compile())
534 inputFiles
.insert(file
);
537 // Replaces common symbols with defined symbols residing in __common sections.
538 // This function must be called after all symbol names are resolved (i.e. after
539 // all InputFiles have been loaded.) As a result, later operations won't see
540 // any CommonSymbols.
541 static void replaceCommonSymbols() {
542 TimeTraceScope
timeScope("Replace common symbols");
543 ConcatOutputSection
*osec
= nullptr;
544 for (Symbol
*sym
: symtab
->getSymbols()) {
545 auto *common
= dyn_cast
<CommonSymbol
>(sym
);
546 if (common
== nullptr)
549 // Casting to size_t will truncate large values on 32-bit architectures,
550 // but it's not really worth supporting the linking of 64-bit programs on
552 ArrayRef
<uint8_t> data
= {nullptr, static_cast<size_t>(common
->size
)};
553 auto *isec
= make
<ConcatInputSection
>(
554 segment_names::data
, section_names::common
, common
->getFile(), data
,
555 common
->align
, S_ZEROFILL
);
557 osec
= ConcatOutputSection::getOrCreateForInput(isec
);
559 inputSections
.push_back(isec
);
561 // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
562 // and pass them on here.
563 replaceSymbol
<Defined
>(sym
, sym
->getName(), isec
->getFile(), isec
,
567 /*isExternal=*/true, common
->privateExtern
,
569 /*isReferencedDynamically=*/false,
570 /*noDeadStrip=*/false);
574 static void initializeSectionRenameMap() {
575 if (config
->dataConst
) {
576 SmallVector
<StringRef
> v
{section_names::got
,
577 section_names::authGot
,
578 section_names::authPtr
,
579 section_names::nonLazySymbolPtr
,
580 section_names::const_
,
581 section_names::cfString
,
582 section_names::moduleInitFunc
,
583 section_names::moduleTermFunc
,
584 section_names::objcClassList
,
585 section_names::objcNonLazyClassList
,
586 section_names::objcCatList
,
587 section_names::objcNonLazyCatList
,
588 section_names::objcProtoList
,
589 section_names::objcImageInfo
};
590 for (StringRef s
: v
)
591 config
->sectionRenameMap
[{segment_names::data
, s
}] = {
592 segment_names::dataConst
, s
};
594 config
->sectionRenameMap
[{segment_names::text
, section_names::staticInit
}] = {
595 segment_names::text
, section_names::text
};
596 config
->sectionRenameMap
[{segment_names::import
, section_names::pointers
}] = {
597 config
->dataConst
? segment_names::dataConst
: segment_names::data
,
598 section_names::nonLazySymbolPtr
};
601 static inline char toLowerDash(char x
) {
602 if (x
>= 'A' && x
<= 'Z')
603 return x
- 'A' + 'a';
609 static std::string
lowerDash(StringRef s
) {
610 return std::string(map_iterator(s
.begin(), toLowerDash
),
611 map_iterator(s
.end(), toLowerDash
));
614 // Has the side-effect of setting Config::platformInfo.
615 static PlatformKind
parsePlatformVersion(const ArgList
&args
) {
616 const Arg
*arg
= args
.getLastArg(OPT_platform_version
);
618 error("must specify -platform_version");
619 return PlatformKind::unknown
;
622 StringRef platformStr
= arg
->getValue(0);
623 StringRef minVersionStr
= arg
->getValue(1);
624 StringRef sdkVersionStr
= arg
->getValue(2);
626 // TODO(compnerd) see if we can generate this case list via XMACROS
627 PlatformKind platform
=
628 StringSwitch
<PlatformKind
>(lowerDash(platformStr
))
629 .Cases("macos", "1", PlatformKind::macOS
)
630 .Cases("ios", "2", PlatformKind::iOS
)
631 .Cases("tvos", "3", PlatformKind::tvOS
)
632 .Cases("watchos", "4", PlatformKind::watchOS
)
633 .Cases("bridgeos", "5", PlatformKind::bridgeOS
)
634 .Cases("mac-catalyst", "6", PlatformKind::macCatalyst
)
635 .Cases("ios-simulator", "7", PlatformKind::iOSSimulator
)
636 .Cases("tvos-simulator", "8", PlatformKind::tvOSSimulator
)
637 .Cases("watchos-simulator", "9", PlatformKind::watchOSSimulator
)
638 .Cases("driverkit", "10", PlatformKind::driverKit
)
639 .Default(PlatformKind::unknown
);
640 if (platform
== PlatformKind::unknown
)
641 error(Twine("malformed platform: ") + platformStr
);
642 // TODO: check validity of version strings, which varies by platform
643 // NOTE: ld64 accepts version strings with 5 components
644 // llvm::VersionTuple accepts no more than 4 components
645 // Has Apple ever published version strings with 5 components?
646 if (config
->platformInfo
.minimum
.tryParse(minVersionStr
))
647 error(Twine("malformed minimum version: ") + minVersionStr
);
648 if (config
->platformInfo
.sdk
.tryParse(sdkVersionStr
))
649 error(Twine("malformed sdk version: ") + sdkVersionStr
);
653 // Has the side-effect of setting Config::target.
654 static TargetInfo
*createTargetInfo(InputArgList
&args
) {
655 StringRef archName
= args
.getLastArgValue(OPT_arch
);
656 if (archName
.empty())
657 fatal("must specify -arch");
658 PlatformKind platform
= parsePlatformVersion(args
);
660 config
->platformInfo
.target
=
661 MachO::Target(getArchitectureFromName(archName
), platform
);
665 std::tie(cpuType
, cpuSubtype
) = getCPUTypeFromArchitecture(config
->arch());
668 case CPU_TYPE_X86_64
:
669 return createX86_64TargetInfo();
671 return createARM64TargetInfo();
672 case CPU_TYPE_ARM64_32
:
673 return createARM64_32TargetInfo();
675 return createARMTargetInfo(cpuSubtype
);
677 fatal("missing or unsupported -arch " + archName
);
681 static UndefinedSymbolTreatment
682 getUndefinedSymbolTreatment(const ArgList
&args
) {
683 StringRef treatmentStr
= args
.getLastArgValue(OPT_undefined
);
685 StringSwitch
<UndefinedSymbolTreatment
>(treatmentStr
)
686 .Cases("error", "", UndefinedSymbolTreatment::error
)
687 .Case("warning", UndefinedSymbolTreatment::warning
)
688 .Case("suppress", UndefinedSymbolTreatment::suppress
)
689 .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup
)
690 .Default(UndefinedSymbolTreatment::unknown
);
691 if (treatment
== UndefinedSymbolTreatment::unknown
) {
692 warn(Twine("unknown -undefined TREATMENT '") + treatmentStr
+
693 "', defaulting to 'error'");
694 treatment
= UndefinedSymbolTreatment::error
;
695 } else if (config
->namespaceKind
== NamespaceKind::twolevel
&&
696 (treatment
== UndefinedSymbolTreatment::warning
||
697 treatment
== UndefinedSymbolTreatment::suppress
)) {
698 if (treatment
== UndefinedSymbolTreatment::warning
)
699 error("'-undefined warning' only valid with '-flat_namespace'");
701 error("'-undefined suppress' only valid with '-flat_namespace'");
702 treatment
= UndefinedSymbolTreatment::error
;
707 static ICFLevel
getICFLevel(const ArgList
&args
) {
708 bool noDeduplicate
= args
.hasArg(OPT_no_deduplicate
);
709 StringRef icfLevelStr
= args
.getLastArgValue(OPT_icf_eq
);
710 auto icfLevel
= StringSwitch
<ICFLevel
>(icfLevelStr
)
711 .Cases("none", "", ICFLevel::none
)
712 .Case("safe", ICFLevel::safe
)
713 .Case("all", ICFLevel::all
)
714 .Default(ICFLevel::unknown
);
715 if (icfLevel
== ICFLevel::unknown
) {
716 warn(Twine("unknown --icf=OPTION `") + icfLevelStr
+
717 "', defaulting to `none'");
718 icfLevel
= ICFLevel::none
;
719 } else if (icfLevel
!= ICFLevel::none
&& noDeduplicate
) {
720 warn(Twine("`--icf=" + icfLevelStr
+
721 "' conflicts with -no_deduplicate, setting to `none'"));
722 icfLevel
= ICFLevel::none
;
723 } else if (icfLevel
== ICFLevel::safe
) {
724 warn(Twine("`--icf=safe' is not yet implemented, reverting to `none'"));
725 icfLevel
= ICFLevel::none
;
730 static void warnIfDeprecatedOption(const Option
&opt
) {
731 if (!opt
.getGroup().isValid())
733 if (opt
.getGroup().getID() == OPT_grp_deprecated
) {
734 warn("Option `" + opt
.getPrefixedName() + "' is deprecated in ld64:");
735 warn(opt
.getHelpText());
739 static void warnIfUnimplementedOption(const Option
&opt
) {
740 if (!opt
.getGroup().isValid() || !opt
.hasFlag(DriverFlag::HelpHidden
))
742 switch (opt
.getGroup().getID()) {
743 case OPT_grp_deprecated
:
744 // warn about deprecated options elsewhere
746 case OPT_grp_undocumented
:
747 warn("Option `" + opt
.getPrefixedName() +
748 "' is undocumented. Should lld implement it?");
750 case OPT_grp_obsolete
:
751 warn("Option `" + opt
.getPrefixedName() +
752 "' is obsolete. Please modernize your usage.");
754 case OPT_grp_ignored
:
755 warn("Option `" + opt
.getPrefixedName() + "' is ignored.");
758 warn("Option `" + opt
.getPrefixedName() +
759 "' is not yet implemented. Stay tuned...");
764 static const char *getReproduceOption(InputArgList
&args
) {
765 if (const Arg
*arg
= args
.getLastArg(OPT_reproduce
))
766 return arg
->getValue();
767 return getenv("LLD_REPRODUCE");
770 static void parseClangOption(StringRef opt
, const Twine
&msg
) {
772 raw_string_ostream
os(err
);
774 const char *argv
[] = {"lld", opt
.data()};
775 if (cl::ParseCommandLineOptions(2, argv
, "", &os
))
778 error(msg
+ ": " + StringRef(err
).trim());
781 static uint32_t parseDylibVersion(const ArgList
&args
, unsigned id
) {
782 const Arg
*arg
= args
.getLastArg(id
);
786 if (config
->outputType
!= MH_DYLIB
) {
787 error(arg
->getAsString(args
) + ": only valid with -dylib");
791 PackedVersion version
;
792 if (!version
.parse32(arg
->getValue())) {
793 error(arg
->getAsString(args
) + ": malformed version");
797 return version
.rawValue();
800 static uint32_t parseProtection(StringRef protStr
) {
802 for (char c
: protStr
) {
805 prot
|= VM_PROT_READ
;
808 prot
|= VM_PROT_WRITE
;
811 prot
|= VM_PROT_EXECUTE
;
816 error("unknown -segprot letter '" + Twine(c
) + "' in " + protStr
);
823 static std::vector
<SectionAlign
> parseSectAlign(const opt::InputArgList
&args
) {
824 std::vector
<SectionAlign
> sectAligns
;
825 for (const Arg
*arg
: args
.filtered(OPT_sectalign
)) {
826 StringRef segName
= arg
->getValue(0);
827 StringRef sectName
= arg
->getValue(1);
828 StringRef alignStr
= arg
->getValue(2);
829 if (alignStr
.startswith("0x") || alignStr
.startswith("0X"))
830 alignStr
= alignStr
.drop_front(2);
832 if (alignStr
.getAsInteger(16, align
)) {
833 error("-sectalign: failed to parse '" + StringRef(arg
->getValue(2)) +
837 if (!isPowerOf2_32(align
)) {
838 error("-sectalign: '" + StringRef(arg
->getValue(2)) +
839 "' (in base 16) not a power of two");
842 sectAligns
.push_back({segName
, sectName
, align
});
847 PlatformKind
macho::removeSimulator(PlatformKind platform
) {
849 case PlatformKind::iOSSimulator
:
850 return PlatformKind::iOS
;
851 case PlatformKind::tvOSSimulator
:
852 return PlatformKind::tvOS
;
853 case PlatformKind::watchOSSimulator
:
854 return PlatformKind::watchOS
;
860 static bool dataConstDefault(const InputArgList
&args
) {
861 static const std::vector
<std::pair
<PlatformKind
, VersionTuple
>> minVersion
= {
862 {PlatformKind::macOS
, VersionTuple(10, 15)},
863 {PlatformKind::iOS
, VersionTuple(13, 0)},
864 {PlatformKind::tvOS
, VersionTuple(13, 0)},
865 {PlatformKind::watchOS
, VersionTuple(6, 0)},
866 {PlatformKind::bridgeOS
, VersionTuple(4, 0)}};
867 PlatformKind platform
= removeSimulator(config
->platformInfo
.target
.Platform
);
868 auto it
= llvm::find_if(minVersion
,
869 [&](const auto &p
) { return p
.first
== platform
; });
870 if (it
!= minVersion
.end())
871 if (config
->platformInfo
.minimum
< it
->second
)
874 switch (config
->outputType
) {
876 return !args
.hasArg(OPT_no_pie
);
878 // FIXME: return false when -final_name ...
879 // has prefix "/System/Library/UserEventPlugins/"
880 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd"
888 "unsupported output type for determining data-const default");
893 void SymbolPatterns::clear() {
898 void SymbolPatterns::insert(StringRef symbolName
) {
899 if (symbolName
.find_first_of("*?[]") == StringRef::npos
)
900 literals
.insert(CachedHashStringRef(symbolName
));
901 else if (Expected
<GlobPattern
> pattern
= GlobPattern::create(symbolName
))
902 globs
.emplace_back(*pattern
);
904 error("invalid symbol-name pattern: " + symbolName
);
907 bool SymbolPatterns::matchLiteral(StringRef symbolName
) const {
908 return literals
.contains(CachedHashStringRef(symbolName
));
911 bool SymbolPatterns::matchGlob(StringRef symbolName
) const {
912 for (const GlobPattern
&glob
: globs
)
913 if (glob
.match(symbolName
))
918 bool SymbolPatterns::match(StringRef symbolName
) const {
919 return matchLiteral(symbolName
) || matchGlob(symbolName
);
922 static void handleSymbolPatterns(InputArgList
&args
,
923 SymbolPatterns
&symbolPatterns
,
924 unsigned singleOptionCode
,
925 unsigned listFileOptionCode
) {
926 for (const Arg
*arg
: args
.filtered(singleOptionCode
))
927 symbolPatterns
.insert(arg
->getValue());
928 for (const Arg
*arg
: args
.filtered(listFileOptionCode
)) {
929 StringRef path
= arg
->getValue();
930 Optional
<MemoryBufferRef
> buffer
= readFile(path
);
932 error("Could not read symbol file: " + path
);
935 MemoryBufferRef mbref
= *buffer
;
936 for (StringRef line
: args::getLines(mbref
)) {
937 line
= line
.take_until([](char c
) { return c
== '#'; }).trim();
939 symbolPatterns
.insert(line
);
944 void createFiles(const InputArgList
&args
) {
945 TimeTraceScope
timeScope("Load input files");
946 // This loop should be reserved for options whose exact ordering matters.
947 // Other options should be handled via filtered() and/or getLastArg().
948 for (const Arg
*arg
: args
) {
949 const Option
&opt
= arg
->getOption();
950 warnIfDeprecatedOption(opt
);
951 warnIfUnimplementedOption(opt
);
953 switch (opt
.getID()) {
955 addFile(rerootPath(arg
->getValue()), /*forceLoadArchive=*/false);
957 case OPT_needed_library
:
958 if (auto *dylibFile
= dyn_cast_or_null
<DylibFile
>(
959 addFile(rerootPath(arg
->getValue()), false)))
960 dylibFile
->forceNeeded
= true;
962 case OPT_reexport_library
:
963 if (auto *dylibFile
= dyn_cast_or_null
<DylibFile
>(addFile(
964 rerootPath(arg
->getValue()), /*forceLoadArchive=*/false))) {
965 config
->hasReexports
= true;
966 dylibFile
->reexport
= true;
969 case OPT_weak_library
:
970 if (auto *dylibFile
= dyn_cast_or_null
<DylibFile
>(
971 addFile(rerootPath(arg
->getValue()), /*forceLoadArchive=*/false)))
972 dylibFile
->forceWeakImport
= true;
975 addFileList(arg
->getValue());
978 addFile(rerootPath(arg
->getValue()), /*forceLoadArchive=*/true);
984 addLibrary(arg
->getValue(), opt
.getID() == OPT_needed_l
,
985 opt
.getID() == OPT_weak_l
, opt
.getID() == OPT_reexport_l
,
986 /*isExplicit=*/true, /*forceLoad=*/false);
989 case OPT_needed_framework
:
990 case OPT_reexport_framework
:
991 case OPT_weak_framework
:
992 addFramework(arg
->getValue(), opt
.getID() == OPT_needed_framework
,
993 opt
.getID() == OPT_weak_framework
,
994 opt
.getID() == OPT_reexport_framework
, /*isExplicit=*/true);
1002 static void gatherInputSections() {
1003 TimeTraceScope
timeScope("Gathering input sections");
1005 for (const InputFile
*file
: inputFiles
) {
1006 for (const SubsectionMap
&map
: file
->subsections
) {
1007 ConcatOutputSection
*osec
= nullptr;
1008 for (const SubsectionEntry
&entry
: map
) {
1009 if (auto *isec
= dyn_cast
<ConcatInputSection
>(entry
.isec
)) {
1010 if (isec
->isCoalescedWeak())
1012 if (isec
->getSegName() == segment_names::ld
) {
1013 assert(isec
->getName() == section_names::compactUnwind
);
1014 in
.unwindInfo
->addInput(isec
);
1017 isec
->outSecOff
= inputOrder
++;
1019 osec
= ConcatOutputSection::getOrCreateForInput(isec
);
1020 isec
->parent
= osec
;
1021 inputSections
.push_back(isec
);
1022 } else if (auto *isec
= dyn_cast
<CStringInputSection
>(entry
.isec
)) {
1023 if (in
.cStringSection
->inputOrder
== UnspecifiedInputOrder
)
1024 in
.cStringSection
->inputOrder
= inputOrder
++;
1025 in
.cStringSection
->addInput(isec
);
1026 } else if (auto *isec
= dyn_cast
<WordLiteralInputSection
>(entry
.isec
)) {
1027 if (in
.wordLiteralSection
->inputOrder
== UnspecifiedInputOrder
)
1028 in
.wordLiteralSection
->inputOrder
= inputOrder
++;
1029 in
.wordLiteralSection
->addInput(isec
);
1031 llvm_unreachable("unexpected input section kind");
1036 assert(inputOrder
<= UnspecifiedInputOrder
);
1039 static void foldIdenticalLiterals() {
1040 // We always create a cStringSection, regardless of whether dedupLiterals is
1041 // true. If it isn't, we simply create a non-deduplicating CStringSection.
1042 // Either way, we must unconditionally finalize it here.
1043 in
.cStringSection
->finalizeContents();
1044 if (in
.wordLiteralSection
)
1045 in
.wordLiteralSection
->finalizeContents();
1048 static void referenceStubBinder() {
1049 bool needsStubHelper
= config
->outputType
== MH_DYLIB
||
1050 config
->outputType
== MH_EXECUTE
||
1051 config
->outputType
== MH_BUNDLE
;
1052 if (!needsStubHelper
|| !symtab
->find("dyld_stub_binder"))
1055 // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here
1056 // adds a opportunistic reference to dyld_stub_binder if it happens to exist.
1057 // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This
1058 // isn't needed for correctness, but the presence of that symbol suppresses
1059 // "no symbols" diagnostics from `nm`.
1060 // StubHelperSection::setup() adds a reference and errors out if
1061 // dyld_stub_binder doesn't exist in case it is actually needed.
1062 symtab
->addUndefined("dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/false);
1065 bool macho::link(ArrayRef
<const char *> argsArr
, bool canExitEarly
,
1066 raw_ostream
&stdoutOS
, raw_ostream
&stderrOS
) {
1067 lld::stdoutOS
= &stdoutOS
;
1068 lld::stderrOS
= &stderrOS
;
1070 errorHandler().cleanupCallback
= []() { freeArena(); };
1072 errorHandler().logName
= args::getFilenameWithoutExe(argsArr
[0]);
1073 stderrOS
.enable_colors(stderrOS
.has_colors());
1075 MachOOptTable parser
;
1076 InputArgList args
= parser
.parse(argsArr
.slice(1));
1078 errorHandler().errorLimitExceededMsg
=
1079 "too many errors emitted, stopping now "
1080 "(use --error-limit=0 to see all errors)";
1081 errorHandler().errorLimit
= args::getInteger(args
, OPT_error_limit_eq
, 20);
1082 errorHandler().verbose
= args
.hasArg(OPT_verbose
);
1084 if (args
.hasArg(OPT_help_hidden
)) {
1085 parser
.printHelp(argsArr
[0], /*showHidden=*/true);
1088 if (args
.hasArg(OPT_help
)) {
1089 parser
.printHelp(argsArr
[0], /*showHidden=*/false);
1092 if (args
.hasArg(OPT_version
)) {
1093 message(getLLDVersion());
1097 config
= make
<Configuration
>();
1098 symtab
= make
<SymbolTable
>();
1099 target
= createTargetInfo(args
);
1101 make
<DependencyTracker
>(args
.getLastArgValue(OPT_dependency_info
));
1103 // Must be set before any InputSections and Symbols are created.
1104 config
->deadStrip
= args
.hasArg(OPT_dead_strip
);
1106 config
->systemLibraryRoots
= getSystemLibraryRoots(args
);
1107 if (const char *path
= getReproduceOption(args
)) {
1108 // Note that --reproduce is a debug option so you can ignore it
1109 // if you are trying to understand the whole picture of the code.
1110 Expected
<std::unique_ptr
<TarWriter
>> errOrWriter
=
1111 TarWriter::create(path
, path::stem(path
));
1113 tar
= std::move(*errOrWriter
);
1114 tar
->append("response.txt", createResponseFile(args
));
1115 tar
->append("version.txt", getLLDVersion() + "\n");
1117 error("--reproduce: " + toString(errOrWriter
.takeError()));
1121 if (auto *arg
= args
.getLastArg(OPT_threads_eq
)) {
1122 StringRef
v(arg
->getValue());
1123 unsigned threads
= 0;
1124 if (!llvm::to_integer(v
, threads
, 0) || threads
== 0)
1125 error(arg
->getSpelling() + ": expected a positive integer, but got '" +
1126 arg
->getValue() + "'");
1127 parallel::strategy
= hardware_concurrency(threads
);
1128 config
->thinLTOJobs
= v
;
1130 if (auto *arg
= args
.getLastArg(OPT_thinlto_jobs_eq
))
1131 config
->thinLTOJobs
= arg
->getValue();
1132 if (!get_threadpool_strategy(config
->thinLTOJobs
))
1133 error("--thinlto-jobs: invalid job count: " + config
->thinLTOJobs
);
1135 for (const Arg
*arg
: args
.filtered(OPT_u
)) {
1136 config
->explicitUndefineds
.push_back(symtab
->addUndefined(
1137 arg
->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
1140 for (const Arg
*arg
: args
.filtered(OPT_U
))
1141 config
->explicitDynamicLookups
.insert(arg
->getValue());
1143 config
->mapFile
= args
.getLastArgValue(OPT_map
);
1144 config
->optimize
= args::getInteger(args
, OPT_O
, 1);
1145 config
->outputFile
= args
.getLastArgValue(OPT_o
, "a.out");
1146 config
->finalOutput
=
1147 args
.getLastArgValue(OPT_final_output
, config
->outputFile
);
1148 config
->astPaths
= args
.getAllArgValues(OPT_add_ast_path
);
1149 config
->headerPad
= args::getHex(args
, OPT_headerpad
, /*Default=*/32);
1150 config
->headerPadMaxInstallNames
=
1151 args
.hasArg(OPT_headerpad_max_install_names
);
1152 config
->printDylibSearch
=
1153 args
.hasArg(OPT_print_dylib_search
) || getenv("RC_TRACE_DYLIB_SEARCHING");
1154 config
->printEachFile
= args
.hasArg(OPT_t
);
1155 config
->printWhyLoad
= args
.hasArg(OPT_why_load
);
1156 config
->outputType
= getOutputType(args
);
1157 if (const Arg
*arg
= args
.getLastArg(OPT_bundle_loader
)) {
1158 if (config
->outputType
!= MH_BUNDLE
)
1159 error("-bundle_loader can only be used with MachO bundle output");
1160 addFile(arg
->getValue(), /*forceLoadArchive=*/false, /*isExplicit=*/false,
1161 /*isBundleLoader=*/true);
1163 if (const Arg
*arg
= args
.getLastArg(OPT_umbrella
)) {
1164 if (config
->outputType
!= MH_DYLIB
)
1165 warn("-umbrella used, but not creating dylib");
1166 config
->umbrella
= arg
->getValue();
1168 config
->ltoObjPath
= args
.getLastArgValue(OPT_object_path_lto
);
1169 config
->ltoNewPassManager
=
1170 args
.hasFlag(OPT_no_lto_legacy_pass_manager
, OPT_lto_legacy_pass_manager
,
1171 LLVM_ENABLE_NEW_PASS_MANAGER
);
1172 config
->ltoo
= args::getInteger(args
, OPT_lto_O
, 2);
1173 if (config
->ltoo
> 3)
1174 error("--lto-O: invalid optimization level: " + Twine(config
->ltoo
));
1175 config
->thinLTOCacheDir
= args
.getLastArgValue(OPT_cache_path_lto
);
1176 config
->thinLTOCachePolicy
= getLTOCachePolicy(args
);
1177 config
->runtimePaths
= args::getStrings(args
, OPT_rpath
);
1178 config
->allLoad
= args
.hasArg(OPT_all_load
);
1179 config
->archMultiple
= args
.hasArg(OPT_arch_multiple
);
1180 config
->applicationExtension
= args
.hasFlag(
1181 OPT_application_extension
, OPT_no_application_extension
, false);
1182 config
->exportDynamic
= args
.hasArg(OPT_export_dynamic
);
1183 config
->forceLoadObjC
= args
.hasArg(OPT_ObjC
);
1184 config
->forceLoadSwift
= args
.hasArg(OPT_force_load_swift_libs
);
1185 config
->deadStripDylibs
= args
.hasArg(OPT_dead_strip_dylibs
);
1186 config
->demangle
= args
.hasArg(OPT_demangle
);
1187 config
->implicitDylibs
= !args
.hasArg(OPT_no_implicit_dylibs
);
1188 config
->emitFunctionStarts
=
1189 args
.hasFlag(OPT_function_starts
, OPT_no_function_starts
, true);
1190 config
->emitBitcodeBundle
= args
.hasArg(OPT_bitcode_bundle
);
1191 config
->emitDataInCodeInfo
=
1192 args
.hasFlag(OPT_data_in_code_info
, OPT_no_data_in_code_info
, true);
1193 config
->icfLevel
= getICFLevel(args
);
1194 config
->dedupLiterals
= args
.hasArg(OPT_deduplicate_literals
) ||
1195 config
->icfLevel
!= ICFLevel::none
;
1197 // FIXME: Add a commandline flag for this too.
1198 config
->zeroModTime
= getenv("ZERO_AR_DATE");
1200 std::array
<PlatformKind
, 3> encryptablePlatforms
{
1201 PlatformKind::iOS
, PlatformKind::watchOS
, PlatformKind::tvOS
};
1202 config
->emitEncryptionInfo
=
1203 args
.hasFlag(OPT_encryptable
, OPT_no_encryption
,
1204 is_contained(encryptablePlatforms
, config
->platform()));
1206 #ifndef LLVM_HAVE_LIBXAR
1207 if (config
->emitBitcodeBundle
)
1208 error("-bitcode_bundle unsupported because LLD wasn't built with libxar");
1211 if (const Arg
*arg
= args
.getLastArg(OPT_install_name
)) {
1212 if (config
->outputType
!= MH_DYLIB
)
1213 warn(arg
->getAsString(args
) + ": ignored, only has effect with -dylib");
1215 config
->installName
= arg
->getValue();
1216 } else if (config
->outputType
== MH_DYLIB
) {
1217 config
->installName
= config
->finalOutput
;
1220 if (args
.hasArg(OPT_mark_dead_strippable_dylib
)) {
1221 if (config
->outputType
!= MH_DYLIB
)
1222 warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
1224 config
->markDeadStrippableDylib
= true;
1227 if (const Arg
*arg
= args
.getLastArg(OPT_static
, OPT_dynamic
))
1228 config
->staticLink
= (arg
->getOption().getID() == OPT_static
);
1230 if (const Arg
*arg
=
1231 args
.getLastArg(OPT_flat_namespace
, OPT_twolevel_namespace
))
1232 config
->namespaceKind
= arg
->getOption().getID() == OPT_twolevel_namespace
1233 ? NamespaceKind::twolevel
1234 : NamespaceKind::flat
;
1236 config
->undefinedSymbolTreatment
= getUndefinedSymbolTreatment(args
);
1238 if (config
->outputType
== MH_EXECUTE
)
1239 config
->entry
= symtab
->addUndefined(args
.getLastArgValue(OPT_e
, "_main"),
1241 /*isWeakRef=*/false);
1243 config
->librarySearchPaths
=
1244 getLibrarySearchPaths(args
, config
->systemLibraryRoots
);
1245 config
->frameworkSearchPaths
=
1246 getFrameworkSearchPaths(args
, config
->systemLibraryRoots
);
1247 if (const Arg
*arg
=
1248 args
.getLastArg(OPT_search_paths_first
, OPT_search_dylibs_first
))
1249 config
->searchDylibsFirst
=
1250 arg
->getOption().getID() == OPT_search_dylibs_first
;
1252 config
->dylibCompatibilityVersion
=
1253 parseDylibVersion(args
, OPT_compatibility_version
);
1254 config
->dylibCurrentVersion
= parseDylibVersion(args
, OPT_current_version
);
1257 args
.hasFlag(OPT_data_const
, OPT_no_data_const
, dataConstDefault(args
));
1258 // Populate config->sectionRenameMap with builtin default renames.
1259 // Options -rename_section and -rename_segment are able to override.
1260 initializeSectionRenameMap();
1261 // Reject every special character except '.' and '$'
1262 // TODO(gkm): verify that this is the proper set of invalid chars
1263 StringRef
invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1264 auto validName
= [invalidNameChars
](StringRef s
) {
1265 if (s
.find_first_of(invalidNameChars
) != StringRef::npos
)
1266 error("invalid name for segment or section: " + s
);
1269 for (const Arg
*arg
: args
.filtered(OPT_rename_section
)) {
1270 config
->sectionRenameMap
[{validName(arg
->getValue(0)),
1271 validName(arg
->getValue(1))}] = {
1272 validName(arg
->getValue(2)), validName(arg
->getValue(3))};
1274 for (const Arg
*arg
: args
.filtered(OPT_rename_segment
)) {
1275 config
->segmentRenameMap
[validName(arg
->getValue(0))] =
1276 validName(arg
->getValue(1));
1279 config
->sectionAlignments
= parseSectAlign(args
);
1281 for (const Arg
*arg
: args
.filtered(OPT_segprot
)) {
1282 StringRef segName
= arg
->getValue(0);
1283 uint32_t maxProt
= parseProtection(arg
->getValue(1));
1284 uint32_t initProt
= parseProtection(arg
->getValue(2));
1285 if (maxProt
!= initProt
&& config
->arch() != AK_i386
)
1286 error("invalid argument '" + arg
->getAsString(args
) +
1287 "': max and init must be the same for non-i386 archs");
1288 if (segName
== segment_names::linkEdit
)
1289 error("-segprot cannot be used to change __LINKEDIT's protections");
1290 config
->segmentProtections
.push_back({segName
, maxProt
, initProt
});
1293 handleSymbolPatterns(args
, config
->exportedSymbols
, OPT_exported_symbol
,
1294 OPT_exported_symbols_list
);
1295 handleSymbolPatterns(args
, config
->unexportedSymbols
, OPT_unexported_symbol
,
1296 OPT_unexported_symbols_list
);
1297 if (!config
->exportedSymbols
.empty() && !config
->unexportedSymbols
.empty()) {
1298 error("cannot use both -exported_symbol* and -unexported_symbol* options\n"
1299 ">>> ignoring unexports");
1300 config
->unexportedSymbols
.clear();
1302 // Explicitly-exported literal symbols must be defined, but might
1303 // languish in an archive if unreferenced elsewhere. Light a fire
1304 // under those lazy symbols!
1305 for (const CachedHashStringRef
&cachedName
: config
->exportedSymbols
.literals
)
1306 symtab
->addUndefined(cachedName
.val(), /*file=*/nullptr,
1307 /*isWeakRef=*/false);
1309 config
->saveTemps
= args
.hasArg(OPT_save_temps
);
1311 config
->adhocCodesign
= args
.hasFlag(
1312 OPT_adhoc_codesign
, OPT_no_adhoc_codesign
,
1313 (config
->arch() == AK_arm64
|| config
->arch() == AK_arm64e
) &&
1314 config
->platform() == PlatformKind::macOS
);
1316 if (args
.hasArg(OPT_v
)) {
1317 message(getLLDVersion());
1318 message(StringRef("Library search paths:") +
1319 (config
->librarySearchPaths
.empty()
1321 : "\n\t" + join(config
->librarySearchPaths
, "\n\t")));
1322 message(StringRef("Framework search paths:") +
1323 (config
->frameworkSearchPaths
.empty()
1325 : "\n\t" + join(config
->frameworkSearchPaths
, "\n\t")));
1328 config
->progName
= argsArr
[0];
1330 config
->timeTraceEnabled
= args
.hasArg(
1331 OPT_time_trace
, OPT_time_trace_granularity_eq
, OPT_time_trace_file_eq
);
1332 config
->timeTraceGranularity
=
1333 args::getInteger(args
, OPT_time_trace_granularity_eq
, 500);
1335 // Initialize time trace profiler.
1336 if (config
->timeTraceEnabled
)
1337 timeTraceProfilerInitialize(config
->timeTraceGranularity
, config
->progName
);
1340 TimeTraceScope
timeScope("ExecuteLinker");
1342 initLLVM(); // must be run before any call to addFile()
1345 config
->isPic
= config
->outputType
== MH_DYLIB
||
1346 config
->outputType
== MH_BUNDLE
||
1347 (config
->outputType
== MH_EXECUTE
&&
1348 args
.hasFlag(OPT_pie
, OPT_no_pie
, true));
1350 // Now that all dylibs have been loaded, search for those that should be
1353 auto reexportHandler
= [](const Arg
*arg
,
1354 const std::vector
<StringRef
> &extensions
) {
1355 config
->hasReexports
= true;
1356 StringRef searchName
= arg
->getValue();
1357 if (!markReexport(searchName
, extensions
))
1358 error(arg
->getSpelling() + " " + searchName
+
1359 " does not match a supplied dylib");
1361 std::vector
<StringRef
> extensions
= {".tbd"};
1362 for (const Arg
*arg
: args
.filtered(OPT_sub_umbrella
))
1363 reexportHandler(arg
, extensions
);
1365 extensions
.push_back(".dylib");
1366 for (const Arg
*arg
: args
.filtered(OPT_sub_library
))
1367 reexportHandler(arg
, extensions
);
1370 // Parse LTO options.
1371 if (const Arg
*arg
= args
.getLastArg(OPT_mcpu
))
1372 parseClangOption(saver
.save("-mcpu=" + StringRef(arg
->getValue())),
1373 arg
->getSpelling());
1375 for (const Arg
*arg
: args
.filtered(OPT_mllvm
))
1376 parseClangOption(arg
->getValue(), arg
->getSpelling());
1378 compileBitcodeFiles();
1379 replaceCommonSymbols();
1381 StringRef orderFile
= args
.getLastArgValue(OPT_order_file
);
1382 if (!orderFile
.empty())
1383 parseOrderFile(orderFile
);
1385 referenceStubBinder();
1387 // FIXME: should terminate the link early based on errors encountered so
1390 createSyntheticSections();
1391 createSyntheticSymbols();
1393 if (!config
->exportedSymbols
.empty()) {
1394 for (Symbol
*sym
: symtab
->getSymbols()) {
1395 if (auto *defined
= dyn_cast
<Defined
>(sym
)) {
1396 StringRef symbolName
= defined
->getName();
1397 if (config
->exportedSymbols
.match(symbolName
)) {
1398 if (defined
->privateExtern
) {
1399 warn("cannot export hidden symbol " + symbolName
+
1400 "\n>>> defined in " + toString(defined
->getFile()));
1403 defined
->privateExtern
= true;
1407 } else if (!config
->unexportedSymbols
.empty()) {
1408 for (Symbol
*sym
: symtab
->getSymbols())
1409 if (auto *defined
= dyn_cast
<Defined
>(sym
))
1410 if (config
->unexportedSymbols
.match(defined
->getName()))
1411 defined
->privateExtern
= true;
1414 for (const Arg
*arg
: args
.filtered(OPT_sectcreate
)) {
1415 StringRef segName
= arg
->getValue(0);
1416 StringRef sectName
= arg
->getValue(1);
1417 StringRef fileName
= arg
->getValue(2);
1418 Optional
<MemoryBufferRef
> buffer
= readFile(fileName
);
1420 inputFiles
.insert(make
<OpaqueFile
>(*buffer
, segName
, sectName
));
1423 gatherInputSections();
1425 if (config
->deadStrip
)
1428 // ICF assumes that all literals have been folded already, so we must run
1429 // foldIdenticalLiterals before foldIdenticalSections.
1430 foldIdenticalLiterals();
1431 if (config
->icfLevel
!= ICFLevel::none
)
1432 foldIdenticalSections();
1434 // Write to an output file.
1435 if (target
->wordSize
== 8)
1436 writeResult
<LP64
>();
1438 writeResult
<ILP32
>();
1440 depTracker
->write(getLLDVersion(), inputFiles
, config
->outputFile
);
1443 if (config
->timeTraceEnabled
) {
1444 if (auto E
= timeTraceProfilerWrite(
1445 args
.getLastArgValue(OPT_time_trace_file_eq
).str(),
1446 config
->outputFile
)) {
1447 handleAllErrors(std::move(E
),
1448 [&](const StringError
&SE
) { error(SE
.getMessage()); });
1451 timeTraceProfilerCleanup();
1455 exitLld(errorCount() ? 1 : 0);
1457 return !errorCount();