2020-04-03 02:54:05 +08:00
|
|
|
//===- Driver.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Driver.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "InputFiles.h"
|
2020-10-27 10:18:29 +08:00
|
|
|
#include "LTO.h"
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
#include "MarkLive.h"
|
2020-08-19 05:37:04 +08:00
|
|
|
#include "ObjC.h"
|
2020-05-02 07:29:06 +08:00
|
|
|
#include "OutputSection.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "OutputSegment.h"
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
2020-07-31 05:28:41 +08:00
|
|
|
#include "SyntheticSections.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "Target.h"
|
|
|
|
#include "Writer.h"
|
|
|
|
|
|
|
|
#include "lld/Common/Args.h"
|
|
|
|
#include "lld/Common/Driver.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
#include "lld/Common/LLVM.h"
|
|
|
|
#include "lld/Common/Memory.h"
|
2020-12-02 12:31:57 +08:00
|
|
|
#include "lld/Common/Reproduce.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "lld/Common/Version.h"
|
2020-05-06 07:37:34 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2020-04-22 04:37:57 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2021-05-19 23:07:39 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2020-10-27 10:18:29 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2020-05-15 03:43:51 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
2020-12-09 20:06:50 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-07-27 03:46:46 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2020-06-18 10:59:27 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2021-03-26 02:39:45 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2020-04-24 11:16:49 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-11-29 11:38:27 +08:00
|
|
|
#include "llvm/Support/TarWriter.h"
|
2020-10-27 10:18:29 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2021-03-11 22:04:27 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2021-04-06 00:59:50 +08:00
|
|
|
#include "llvm/TextAPI/PackedVersion.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2020-08-11 09:47:16 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::MachO;
|
2020-08-13 10:50:27 +08:00
|
|
|
using namespace llvm::object;
|
2020-06-16 03:36:32 +08:00
|
|
|
using namespace llvm::opt;
|
2020-08-13 10:50:27 +08:00
|
|
|
using namespace llvm::sys;
|
2020-04-03 02:54:05 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::macho;
|
|
|
|
|
2021-03-23 10:05:46 +08:00
|
|
|
Configuration *macho::config;
|
|
|
|
DependencyTracker *macho::depTracker;
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
static HeaderFileType getOutputType(const InputArgList &args) {
|
2020-09-01 14:23:37 +08:00
|
|
|
// TODO: -r, -dylinker, -preload...
|
2021-03-10 12:40:08 +08:00
|
|
|
Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute);
|
2020-09-01 14:23:37 +08:00
|
|
|
if (outputArg == nullptr)
|
|
|
|
return MH_EXECUTE;
|
|
|
|
|
|
|
|
switch (outputArg->getOption().getID()) {
|
|
|
|
case OPT_bundle:
|
|
|
|
return MH_BUNDLE;
|
|
|
|
case OPT_dylib:
|
|
|
|
return MH_DYLIB;
|
|
|
|
case OPT_execute:
|
|
|
|
return MH_EXECUTE;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("internal error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 10:19:09 +08:00
|
|
|
static Optional<StringRef> findLibrary(StringRef name) {
|
2020-09-20 23:37:20 +08:00
|
|
|
if (config->searchDylibsFirst) {
|
2021-06-19 10:19:09 +08:00
|
|
|
if (Optional<StringRef> path = findPathCombination(
|
2021-04-16 09:14:30 +08:00
|
|
|
"lib" + name, config->librarySearchPaths, {".tbd", ".dylib"}))
|
2020-09-20 23:37:20 +08:00
|
|
|
return path;
|
2021-04-16 09:14:30 +08:00
|
|
|
return findPathCombination("lib" + name, config->librarySearchPaths,
|
|
|
|
{".a"});
|
2020-06-04 03:20:35 +08:00
|
|
|
}
|
2021-04-16 09:14:30 +08:00
|
|
|
return findPathCombination("lib" + name, config->librarySearchPaths,
|
|
|
|
{".tbd", ".dylib", ".a"});
|
|
|
|
}
|
|
|
|
|
2020-07-27 03:46:46 +08:00
|
|
|
static Optional<std::string> findFramework(StringRef name) {
|
2021-01-10 00:58:19 +08:00
|
|
|
SmallString<260> symlink;
|
2020-07-27 03:46:46 +08:00
|
|
|
StringRef suffix;
|
|
|
|
std::tie(name, suffix) = name.split(",");
|
|
|
|
for (StringRef dir : config->frameworkSearchPaths) {
|
|
|
|
symlink = dir;
|
|
|
|
path::append(symlink, name + ".framework", name);
|
2020-08-08 02:04:54 +08:00
|
|
|
|
2020-07-27 03:46:46 +08:00
|
|
|
if (!suffix.empty()) {
|
2020-08-08 02:04:54 +08:00
|
|
|
// NOTE: we must resolve the symlink before trying the suffixes, because
|
|
|
|
// there are no symlinks for the suffixed paths.
|
2021-01-10 00:58:19 +08:00
|
|
|
SmallString<260> location;
|
2020-08-08 02:04:54 +08:00
|
|
|
if (!fs::real_path(symlink, location)) {
|
|
|
|
// only append suffix if realpath() succeeds
|
|
|
|
Twine suffixed = location + suffix;
|
|
|
|
if (fs::exists(suffixed))
|
|
|
|
return suffixed.str();
|
|
|
|
}
|
2020-07-27 03:46:46 +08:00
|
|
|
// Suffix lookup failed, fall through to the no-suffix case.
|
|
|
|
}
|
2020-08-08 02:04:54 +08:00
|
|
|
|
2020-08-14 04:48:47 +08:00
|
|
|
if (Optional<std::string> path = resolveDylibPath(symlink))
|
2020-08-08 02:04:54 +08:00
|
|
|
return path;
|
2020-07-27 03:46:46 +08:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-08-14 04:03:00 +08:00
|
|
|
static bool warnIfNotDirectory(StringRef option, StringRef path) {
|
2020-06-18 10:59:27 +08:00
|
|
|
if (!fs::exists(path)) {
|
|
|
|
warn("directory not found for option -" + option + path);
|
|
|
|
return false;
|
|
|
|
} else if (!fs::is_directory(path)) {
|
|
|
|
warn("option -" + option + path + " references a non-directory path");
|
|
|
|
return false;
|
2020-04-22 04:37:57 +08:00
|
|
|
}
|
2020-06-18 10:59:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-19 11:51:38 +08:00
|
|
|
static std::vector<StringRef>
|
2021-03-10 12:40:08 +08:00
|
|
|
getSearchPaths(unsigned optionCode, InputArgList &args,
|
2020-09-19 11:51:38 +08:00
|
|
|
const std::vector<StringRef> &roots,
|
|
|
|
const SmallVector<StringRef, 2> &systemPaths) {
|
|
|
|
std::vector<StringRef> paths;
|
2020-08-14 04:03:00 +08:00
|
|
|
StringRef optionLetter{optionCode == OPT_F ? "F" : "L"};
|
|
|
|
for (StringRef path : args::getStrings(args, optionCode)) {
|
2020-06-20 12:13:03 +08:00
|
|
|
// NOTE: only absolute paths are re-rooted to syslibroot(s)
|
2020-08-14 04:03:00 +08:00
|
|
|
bool found = false;
|
|
|
|
if (path::is_absolute(path, path::Style::posix)) {
|
2020-06-20 12:13:03 +08:00
|
|
|
for (StringRef root : roots) {
|
|
|
|
SmallString<261> buffer(root);
|
2020-08-14 04:03:00 +08:00
|
|
|
path::append(buffer, path);
|
2020-06-20 12:13:03 +08:00
|
|
|
// Do not warn about paths that are computed via the syslib roots
|
2020-08-14 04:03:00 +08:00
|
|
|
if (fs::is_directory(buffer)) {
|
2020-06-20 12:13:03 +08:00
|
|
|
paths.push_back(saver.save(buffer.str()));
|
2020-08-14 04:03:00 +08:00
|
|
|
found = true;
|
|
|
|
}
|
2020-06-20 12:13:03 +08:00
|
|
|
}
|
2020-06-18 10:59:27 +08:00
|
|
|
}
|
2020-08-14 04:03:00 +08:00
|
|
|
if (!found && warnIfNotDirectory(optionLetter, path))
|
|
|
|
paths.push_back(path);
|
2020-06-18 10:59:27 +08:00
|
|
|
}
|
2020-06-20 12:13:03 +08:00
|
|
|
|
|
|
|
// `-Z` suppresses the standard "system" search paths.
|
|
|
|
if (args.hasArg(OPT_Z))
|
2020-09-19 11:51:38 +08:00
|
|
|
return paths;
|
2020-06-20 12:13:03 +08:00
|
|
|
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const StringRef &path : systemPaths) {
|
|
|
|
for (const StringRef &root : roots) {
|
2020-06-20 12:13:03 +08:00
|
|
|
SmallString<261> buffer(root);
|
2020-08-14 04:03:00 +08:00
|
|
|
path::append(buffer, path);
|
2020-11-21 05:05:33 +08:00
|
|
|
if (fs::is_directory(buffer))
|
2020-06-20 12:13:03 +08:00
|
|
|
paths.push_back(saver.save(buffer.str()));
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 11:51:38 +08:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) {
|
2020-09-19 11:51:38 +08:00
|
|
|
std::vector<StringRef> roots;
|
|
|
|
for (const Arg *arg : args.filtered(OPT_syslibroot))
|
|
|
|
roots.push_back(arg->getValue());
|
|
|
|
// NOTE: the final `-syslibroot` being `/` will ignore all roots
|
|
|
|
if (roots.size() && roots.back() == "/")
|
|
|
|
roots.clear();
|
|
|
|
// NOTE: roots can never be empty - add an empty root to simplify the library
|
|
|
|
// and framework search path computation.
|
|
|
|
if (roots.empty())
|
|
|
|
roots.emplace_back("");
|
|
|
|
return roots;
|
2020-06-18 10:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-09-19 11:51:38 +08:00
|
|
|
static std::vector<StringRef>
|
2021-03-10 12:40:08 +08:00
|
|
|
getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) {
|
2020-09-19 11:51:38 +08:00
|
|
|
return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"});
|
2020-06-18 10:59:27 +08:00
|
|
|
}
|
|
|
|
|
2020-09-19 11:51:38 +08:00
|
|
|
static std::vector<StringRef>
|
2021-03-10 12:40:08 +08:00
|
|
|
getFrameworkSearchPaths(InputArgList &args,
|
2020-09-19 11:51:38 +08:00
|
|
|
const std::vector<StringRef> &roots) {
|
|
|
|
return getSearchPaths(OPT_F, args, roots,
|
|
|
|
{"/Library/Frameworks", "/System/Library/Frameworks"});
|
2020-04-22 04:37:57 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 06:45:11 +08:00
|
|
|
namespace {
|
|
|
|
struct ArchiveMember {
|
|
|
|
MemoryBufferRef mbref;
|
|
|
|
uint32_t modTime;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2020-08-19 05:37:04 +08:00
|
|
|
// Returns slices of MB by parsing MB as an archive file.
|
|
|
|
// Each slice consists of a member file in the archive.
|
2020-12-02 06:45:11 +08:00
|
|
|
static std::vector<ArchiveMember> getArchiveMembers(MemoryBufferRef mb) {
|
2020-08-19 05:37:04 +08:00
|
|
|
std::unique_ptr<Archive> file =
|
|
|
|
CHECK(Archive::create(mb),
|
|
|
|
mb.getBufferIdentifier() + ": failed to parse archive");
|
2020-11-20 23:14:57 +08:00
|
|
|
Archive *archive = file.get();
|
|
|
|
make<std::unique_ptr<Archive>>(std::move(file)); // take ownership
|
2020-08-19 05:37:04 +08:00
|
|
|
|
2020-12-02 06:45:11 +08:00
|
|
|
std::vector<ArchiveMember> v;
|
2020-08-19 05:37:04 +08:00
|
|
|
Error err = Error::success();
|
2020-12-02 12:31:57 +08:00
|
|
|
|
2021-05-06 02:38:36 +08:00
|
|
|
// Thin archives refer to .o files, so --reproduce needs the .o files too.
|
2020-12-02 12:31:57 +08:00
|
|
|
bool addToTar = archive->isThin() && tar;
|
|
|
|
|
2020-11-20 23:14:57 +08:00
|
|
|
for (const Archive::Child &c : archive->children(err)) {
|
2020-08-19 05:37:04 +08:00
|
|
|
MemoryBufferRef mbref =
|
|
|
|
CHECK(c.getMemoryBufferRef(),
|
|
|
|
mb.getBufferIdentifier() +
|
|
|
|
": could not get the buffer for a child of the archive");
|
2020-12-02 12:31:57 +08:00
|
|
|
if (addToTar)
|
|
|
|
tar->append(relativeToRoot(check(c.getFullName())), mbref.getBuffer());
|
2020-12-02 06:45:11 +08:00
|
|
|
uint32_t modTime = toTimeT(
|
|
|
|
CHECK(c.getLastModified(), mb.getBufferIdentifier() +
|
|
|
|
": could not get the modification "
|
|
|
|
"time for a child of the archive"));
|
|
|
|
v.push_back({mbref, modTime});
|
2020-08-19 05:37:04 +08:00
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
fatal(mb.getBufferIdentifier() +
|
|
|
|
": Archive::children failed: " + toString(std::move(err)));
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-06-19 10:30:57 +08:00
|
|
|
static DenseMap<StringRef, ArchiveFile *> loadedArchives;
|
|
|
|
|
2021-02-23 02:03:02 +08:00
|
|
|
static InputFile *addFile(StringRef path, bool forceLoadArchive,
|
2021-06-02 20:54:36 +08:00
|
|
|
bool isExplicit = true,
|
2021-02-23 02:03:02 +08:00
|
|
|
bool isBundleLoader = false) {
|
2021-03-02 17:20:22 +08:00
|
|
|
Optional<MemoryBufferRef> buffer = readFile(path);
|
2020-04-03 02:54:05 +08:00
|
|
|
if (!buffer)
|
2020-09-19 02:38:15 +08:00
|
|
|
return nullptr;
|
2020-04-03 02:54:05 +08:00
|
|
|
MemoryBufferRef mbref = *buffer;
|
2020-09-19 02:38:15 +08:00
|
|
|
InputFile *newFile = nullptr;
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-03-10 12:15:29 +08:00
|
|
|
file_magic magic = identify_magic(mbref.getBuffer());
|
2020-12-03 07:57:30 +08:00
|
|
|
switch (magic) {
|
2020-05-15 03:43:51 +08:00
|
|
|
case file_magic::archive: {
|
2021-06-19 10:30:57 +08:00
|
|
|
// Avoid loading archives twice. If the archives are being force-loaded,
|
|
|
|
// loading them twice would create duplicate symbol errors. In the
|
|
|
|
// non-force-loading case, this is just a minor performance optimization.
|
|
|
|
// We don't take a reference to cachedFile here because the
|
|
|
|
// loadArchiveMember() call below may recursively call addFile() and
|
|
|
|
// invalidate this reference.
|
|
|
|
ArchiveFile *cachedFile = loadedArchives[path];
|
|
|
|
if (cachedFile)
|
|
|
|
return cachedFile;
|
|
|
|
|
2020-05-15 03:43:51 +08:00
|
|
|
std::unique_ptr<object::Archive> file = CHECK(
|
|
|
|
object::Archive::create(mbref), path + ": failed to parse archive");
|
|
|
|
|
|
|
|
if (!file->isEmpty() && !file->hasSymbolTable())
|
|
|
|
error(path + ": archive has no index; run ranlib to add one");
|
|
|
|
|
2020-11-20 23:14:57 +08:00
|
|
|
if (config->allLoad || forceLoadArchive) {
|
2021-03-02 17:20:22 +08:00
|
|
|
if (Optional<MemoryBufferRef> buffer = readFile(path)) {
|
2020-11-20 23:14:57 +08:00
|
|
|
for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
|
2021-02-04 02:31:42 +08:00
|
|
|
if (Optional<InputFile *> file = loadArchiveMember(
|
|
|
|
member.mbref, member.modTime, path, /*objCOnly=*/false)) {
|
|
|
|
inputFiles.insert(*file);
|
|
|
|
printArchiveMemberLoad(
|
|
|
|
(forceLoadArchive ? "-force_load" : "-all_load"),
|
|
|
|
inputFiles.back());
|
|
|
|
}
|
2020-11-20 23:14:57 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-26 11:00:42 +08:00
|
|
|
} else if (config->forceLoadObjC) {
|
2020-08-19 05:37:04 +08:00
|
|
|
for (const object::Archive::Symbol &sym : file->symbols())
|
|
|
|
if (sym.getName().startswith(objc::klass))
|
2021-02-04 02:31:40 +08:00
|
|
|
symtab->addUndefined(sym.getName(), /*file=*/nullptr,
|
|
|
|
/*isWeakRef=*/false);
|
2020-08-19 05:37:04 +08:00
|
|
|
|
|
|
|
// TODO: no need to look for ObjC sections for a given archive member if
|
|
|
|
// we already found that it contains an ObjC symbol. We should also
|
|
|
|
// consider creating a LazyObjFile class in order to avoid double-loading
|
|
|
|
// these files here and below (as part of the ArchiveFile).
|
2021-03-02 17:20:22 +08:00
|
|
|
if (Optional<MemoryBufferRef> buffer = readFile(path)) {
|
2020-12-02 08:00:48 +08:00
|
|
|
for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
|
2021-02-04 02:31:42 +08:00
|
|
|
if (Optional<InputFile *> file = loadArchiveMember(
|
|
|
|
member.mbref, member.modTime, path, /*objCOnly=*/true)) {
|
|
|
|
inputFiles.insert(*file);
|
2020-12-03 07:57:30 +08:00
|
|
|
printArchiveMemberLoad("-ObjC", inputFiles.back());
|
2020-12-02 08:00:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 05:37:04 +08:00
|
|
|
}
|
|
|
|
|
2021-06-19 10:30:57 +08:00
|
|
|
newFile = loadedArchives[path] = make<ArchiveFile>(std::move(file));
|
2020-05-15 03:43:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-04-03 02:54:05 +08:00
|
|
|
case file_magic::macho_object:
|
2020-12-02 08:00:48 +08:00
|
|
|
newFile = make<ObjFile>(mbref, getModTime(path), "");
|
2020-04-03 02:54:05 +08:00
|
|
|
break;
|
2020-04-22 04:37:57 +08:00
|
|
|
case file_magic::macho_dynamically_linked_shared_lib:
|
2020-10-08 05:50:42 +08:00
|
|
|
case file_magic::macho_dynamically_linked_shared_lib_stub:
|
2021-01-19 23:44:42 +08:00
|
|
|
case file_magic::tapi_file:
|
2021-06-02 04:09:41 +08:00
|
|
|
if (DylibFile * dylibFile = loadDylib(mbref)) {
|
2021-06-02 20:54:36 +08:00
|
|
|
if (isExplicit)
|
|
|
|
dylibFile->explicitlyLinked = true;
|
2021-06-02 04:09:41 +08:00
|
|
|
newFile = dylibFile;
|
2021-06-01 10:12:35 +08:00
|
|
|
}
|
2020-06-06 02:18:33 +08:00
|
|
|
break;
|
2020-10-27 10:18:29 +08:00
|
|
|
case file_magic::bitcode:
|
|
|
|
newFile = make<BitcodeFile>(mbref);
|
|
|
|
break;
|
2021-02-23 02:03:02 +08:00
|
|
|
case file_magic::macho_executable:
|
|
|
|
case file_magic::macho_bundle:
|
|
|
|
// We only allow executable and bundle type here if it is used
|
|
|
|
// as a bundle loader.
|
|
|
|
if (!isBundleLoader)
|
|
|
|
error(path + ": unhandled file type");
|
2021-06-02 04:09:41 +08:00
|
|
|
if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader))
|
|
|
|
newFile = dylibFile;
|
2021-02-23 02:03:02 +08:00
|
|
|
break;
|
2020-04-03 02:54:05 +08:00
|
|
|
default:
|
|
|
|
error(path + ": unhandled file type");
|
|
|
|
}
|
2021-06-07 23:00:34 +08:00
|
|
|
if (newFile && !isa<DylibFile>(newFile)) {
|
2020-12-03 07:57:30 +08:00
|
|
|
// printArchiveMemberLoad() prints both .a and .o names, so no need to
|
|
|
|
// print the .a name here.
|
2021-06-07 23:00:34 +08:00
|
|
|
if (config->printEachFile && magic != file_magic::archive)
|
2020-12-18 05:19:06 +08:00
|
|
|
message(toString(newFile));
|
2020-12-15 06:59:22 +08:00
|
|
|
inputFiles.insert(newFile);
|
2020-12-03 07:57:30 +08:00
|
|
|
}
|
2020-09-19 02:38:15 +08:00
|
|
|
return newFile;
|
2020-04-03 02:54:05 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 23:06:42 +08:00
|
|
|
static void addLibrary(StringRef name, bool isNeeded, bool isWeak,
|
2021-06-08 11:48:16 +08:00
|
|
|
bool isReexport, bool isExplicit, bool forceLoad) {
|
2021-06-19 10:19:09 +08:00
|
|
|
if (Optional<StringRef> path = findLibrary(name)) {
|
2021-06-02 20:54:36 +08:00
|
|
|
if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
|
2021-06-08 11:48:16 +08:00
|
|
|
addFile(*path, forceLoad, isExplicit))) {
|
2021-06-02 23:06:42 +08:00
|
|
|
if (isNeeded)
|
|
|
|
dylibFile->forceNeeded = true;
|
2021-06-01 10:12:35 +08:00
|
|
|
if (isWeak)
|
|
|
|
dylibFile->forceWeakImport = true;
|
2021-06-02 08:17:04 +08:00
|
|
|
if (isReexport) {
|
|
|
|
config->hasReexports = true;
|
|
|
|
dylibFile->reexport = true;
|
|
|
|
}
|
2021-06-01 10:12:35 +08:00
|
|
|
}
|
2020-12-04 05:40:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
error("library not found for -l" + name);
|
|
|
|
}
|
|
|
|
|
2021-06-02 23:06:42 +08:00
|
|
|
static void addFramework(StringRef name, bool isNeeded, bool isWeak,
|
|
|
|
bool isReexport, bool isExplicit) {
|
2020-12-04 05:40:04 +08:00
|
|
|
if (Optional<std::string> path = findFramework(name)) {
|
2021-06-02 20:54:36 +08:00
|
|
|
if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
|
|
|
|
addFile(*path, /*forceLoadArchive=*/false, isExplicit))) {
|
2021-06-02 23:06:42 +08:00
|
|
|
if (isNeeded)
|
|
|
|
dylibFile->forceNeeded = true;
|
2021-06-01 10:12:35 +08:00
|
|
|
if (isWeak)
|
|
|
|
dylibFile->forceWeakImport = true;
|
2021-06-02 08:17:04 +08:00
|
|
|
if (isReexport) {
|
|
|
|
config->hasReexports = true;
|
|
|
|
dylibFile->reexport = true;
|
|
|
|
}
|
2021-06-01 10:12:35 +08:00
|
|
|
}
|
2020-12-04 05:40:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
error("framework not found for -framework " + name);
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:05:15 +08:00
|
|
|
// Parses LC_LINKER_OPTION contents, which can add additional command line
|
|
|
|
// flags.
|
|
|
|
void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) {
|
2020-12-04 05:40:04 +08:00
|
|
|
SmallVector<const char *, 4> argv;
|
|
|
|
size_t offset = 0;
|
|
|
|
for (unsigned i = 0; i < argc && offset < data.size(); ++i) {
|
|
|
|
argv.push_back(data.data() + offset);
|
|
|
|
offset += strlen(data.data() + offset) + 1;
|
|
|
|
}
|
|
|
|
if (argv.size() != argc || offset > data.size())
|
|
|
|
fatal(toString(f) + ": invalid LC_LINKER_OPTION");
|
|
|
|
|
|
|
|
MachOOptTable table;
|
|
|
|
unsigned missingIndex, missingCount;
|
2021-03-10 12:40:08 +08:00
|
|
|
InputArgList args = table.ParseArgs(argv, missingIndex, missingCount);
|
2020-12-04 05:40:04 +08:00
|
|
|
if (missingCount)
|
|
|
|
fatal(Twine(args.getArgString(missingIndex)) + ": missing argument");
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_UNKNOWN))
|
2020-12-04 05:40:04 +08:00
|
|
|
error("unknown argument: " + arg->getAsString(args));
|
|
|
|
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args) {
|
2020-12-04 05:40:04 +08:00
|
|
|
switch (arg->getOption().getID()) {
|
2021-06-08 11:48:16 +08:00
|
|
|
case OPT_l: {
|
|
|
|
StringRef name = arg->getValue();
|
|
|
|
bool forceLoad =
|
|
|
|
config->forceLoadSwift ? name.startswith("swift") : false;
|
|
|
|
addLibrary(name, /*isNeeded=*/false, /*isWeak=*/false,
|
|
|
|
/*isReexport=*/false, /*isExplicit=*/false, forceLoad);
|
2020-12-04 05:40:04 +08:00
|
|
|
break;
|
2021-06-08 11:48:16 +08:00
|
|
|
}
|
2020-12-04 05:40:04 +08:00
|
|
|
case OPT_framework:
|
2021-06-02 23:06:42 +08:00
|
|
|
addFramework(arg->getValue(), /*isNeeded=*/false, /*isWeak=*/false,
|
|
|
|
/*isReexport=*/false, /*isExplicit=*/false);
|
2020-12-04 05:40:04 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(arg->getSpelling() + " is not allowed in LC_LINKER_OPTION");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 00:56:50 +08:00
|
|
|
static void addFileList(StringRef path) {
|
2021-03-02 17:20:22 +08:00
|
|
|
Optional<MemoryBufferRef> buffer = readFile(path);
|
2020-07-29 00:56:50 +08:00
|
|
|
if (!buffer)
|
|
|
|
return;
|
|
|
|
MemoryBufferRef mbref = *buffer;
|
|
|
|
for (StringRef path : args::getLines(mbref))
|
2021-06-02 20:54:36 +08:00
|
|
|
addFile(rerootPath(path), /*forceLoadArchive=*/false);
|
2020-07-29 00:56:50 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 07:37:34 +08:00
|
|
|
// An order file has one entry per line, in the following format:
|
|
|
|
//
|
2020-12-19 01:47:15 +08:00
|
|
|
// <cpu>:<object file>:<symbol name>
|
2020-05-06 07:37:34 +08:00
|
|
|
//
|
2020-12-19 01:47:15 +08:00
|
|
|
// <cpu> and <object file> are optional. If not specified, then that entry
|
|
|
|
// matches any symbol of that name. Parsing this format is not quite
|
|
|
|
// straightforward because the symbol name itself can contain colons, so when
|
|
|
|
// encountering a colon, we consider the preceding characters to decide if it
|
|
|
|
// can be a valid CPU type or file path.
|
2020-05-06 07:37:34 +08:00
|
|
|
//
|
|
|
|
// If a symbol is matched by multiple entries, then it takes the lowest-ordered
|
|
|
|
// entry (the one nearest to the front of the list.)
|
|
|
|
//
|
|
|
|
// The file can also have line comments that start with '#'.
|
2020-07-17 19:49:11 +08:00
|
|
|
static void parseOrderFile(StringRef path) {
|
2021-03-02 17:20:22 +08:00
|
|
|
Optional<MemoryBufferRef> buffer = readFile(path);
|
2020-05-06 07:37:34 +08:00
|
|
|
if (!buffer) {
|
|
|
|
error("Could not read order file at " + path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryBufferRef mbref = *buffer;
|
|
|
|
size_t priority = std::numeric_limits<size_t>::max();
|
2020-12-19 01:47:15 +08:00
|
|
|
for (StringRef line : args::getLines(mbref)) {
|
|
|
|
StringRef objectFile, symbol;
|
|
|
|
line = line.take_until([](char c) { return c == '#'; }); // ignore comments
|
|
|
|
line = line.ltrim();
|
|
|
|
|
|
|
|
CPUType cpuType = StringSwitch<CPUType>(line)
|
|
|
|
.StartsWith("i386:", CPU_TYPE_I386)
|
|
|
|
.StartsWith("x86_64:", CPU_TYPE_X86_64)
|
|
|
|
.StartsWith("arm:", CPU_TYPE_ARM)
|
|
|
|
.StartsWith("arm64:", CPU_TYPE_ARM64)
|
|
|
|
.StartsWith("ppc:", CPU_TYPE_POWERPC)
|
|
|
|
.StartsWith("ppc64:", CPU_TYPE_POWERPC64)
|
|
|
|
.Default(CPU_TYPE_ANY);
|
2021-05-11 03:45:20 +08:00
|
|
|
|
|
|
|
if (cpuType != CPU_TYPE_ANY && cpuType != target->cpuType)
|
|
|
|
continue;
|
|
|
|
|
2020-12-19 01:47:15 +08:00
|
|
|
// Drop the CPU type as well as the colon
|
|
|
|
if (cpuType != CPU_TYPE_ANY)
|
|
|
|
line = line.drop_until([](char c) { return c == ':'; }).drop_front();
|
2020-05-06 07:37:34 +08:00
|
|
|
|
2020-12-19 01:47:15 +08:00
|
|
|
constexpr std::array<StringRef, 2> fileEnds = {".o:", ".o):"};
|
|
|
|
for (StringRef fileEnd : fileEnds) {
|
|
|
|
size_t pos = line.find(fileEnd);
|
|
|
|
if (pos != StringRef::npos) {
|
|
|
|
// Split the string around the colon
|
|
|
|
objectFile = line.take_front(pos + fileEnd.size() - 1);
|
|
|
|
line = line.drop_front(pos + fileEnd.size());
|
|
|
|
break;
|
2020-05-06 07:37:34 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 01:47:15 +08:00
|
|
|
symbol = line.trim();
|
2020-05-06 07:37:34 +08:00
|
|
|
|
|
|
|
if (!symbol.empty()) {
|
|
|
|
SymbolPriorityEntry &entry = config->priorities[symbol];
|
|
|
|
if (!objectFile.empty())
|
|
|
|
entry.objectFiles.insert(std::make_pair(objectFile, priority));
|
|
|
|
else
|
|
|
|
entry.anyObjectFile = std::max(entry.anyObjectFile, priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
--priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:16:49 +08:00
|
|
|
// We expect sub-library names of the form "libfoo", which will match a dylib
|
2020-08-19 06:46:21 +08:00
|
|
|
// with a path of .*/libfoo.{dylib, tbd}.
|
|
|
|
// XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
|
|
|
|
// I'm not sure what the use case for that is.
|
2020-12-15 11:55:28 +08:00
|
|
|
static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) {
|
2020-04-24 11:16:49 +08:00
|
|
|
for (InputFile *file : inputFiles) {
|
|
|
|
if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
|
|
|
|
StringRef filename = path::filename(dylibFile->getName());
|
2020-08-19 06:46:21 +08:00
|
|
|
if (filename.consume_front(searchName) &&
|
2020-12-15 11:55:28 +08:00
|
|
|
(filename.empty() ||
|
|
|
|
find(extensions, filename) != extensions.end())) {
|
2020-04-24 11:16:49 +08:00
|
|
|
dylibFile->reexport = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-27 10:18:29 +08:00
|
|
|
// This function is called on startup. We need this for LTO since
|
|
|
|
// LTO calls LLVM functions to compile bitcode files to native code.
|
|
|
|
// Technically this can be delayed until we read bitcode files, but
|
|
|
|
// we don't bother to do lazily because the initialization is fast.
|
|
|
|
static void initLLVM() {
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compileBitcodeFiles() {
|
2021-03-26 02:39:44 +08:00
|
|
|
TimeTraceScope timeScope("LTO");
|
2021-03-10 12:15:29 +08:00
|
|
|
auto *lto = make<BitcodeCompiler>();
|
2020-10-27 10:18:29 +08:00
|
|
|
for (InputFile *file : inputFiles)
|
|
|
|
if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file))
|
|
|
|
lto->add(*bitcodeFile);
|
|
|
|
|
|
|
|
for (ObjFile *file : lto->compile())
|
2020-12-15 06:59:22 +08:00
|
|
|
inputFiles.insert(file);
|
2020-10-27 10:18:29 +08:00
|
|
|
}
|
|
|
|
|
2020-09-25 05:44:14 +08:00
|
|
|
// Replaces common symbols with defined symbols residing in __common sections.
|
|
|
|
// This function must be called after all symbol names are resolved (i.e. after
|
|
|
|
// all InputFiles have been loaded.) As a result, later operations won't see
|
|
|
|
// any CommonSymbols.
|
|
|
|
static void replaceCommonSymbols() {
|
2021-03-26 02:39:44 +08:00
|
|
|
TimeTraceScope timeScope("Replace common symbols");
|
2021-03-30 08:19:29 +08:00
|
|
|
for (Symbol *sym : symtab->getSymbols()) {
|
2020-09-25 05:44:14 +08:00
|
|
|
auto *common = dyn_cast<CommonSymbol>(sym);
|
|
|
|
if (common == nullptr)
|
|
|
|
continue;
|
|
|
|
|
2021-06-12 07:49:53 +08:00
|
|
|
auto *isec =
|
|
|
|
make<ConcatInputSection>(segment_names::data, section_names::common);
|
2021-02-04 02:31:40 +08:00
|
|
|
isec->file = common->getFile();
|
2020-09-25 05:44:14 +08:00
|
|
|
isec->align = common->align;
|
|
|
|
// Casting to size_t will truncate large values on 32-bit architectures,
|
|
|
|
// but it's not really worth supporting the linking of 64-bit programs on
|
|
|
|
// 32-bit archs.
|
|
|
|
isec->data = {nullptr, static_cast<size_t>(common->size)};
|
|
|
|
isec->flags = S_ZEROFILL;
|
|
|
|
inputSections.push_back(isec);
|
|
|
|
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
// FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip
|
|
|
|
// and pass them on here.
|
2021-02-04 02:31:40 +08:00
|
|
|
replaceSymbol<Defined>(sym, sym->getName(), isec->file, isec, /*value=*/0,
|
2021-04-02 08:48:09 +08:00
|
|
|
/*size=*/0,
|
2020-09-25 05:44:14 +08:00
|
|
|
/*isWeakDef=*/false,
|
2021-05-01 04:17:26 +08:00
|
|
|
/*isExternal=*/true, common->privateExtern,
|
2021-05-17 21:15:39 +08:00
|
|
|
/*isThumb=*/false,
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
/*isReferencedDynamically=*/false,
|
|
|
|
/*noDeadStrip=*/false);
|
2020-09-25 05:44:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 07:00:24 +08:00
|
|
|
static void initializeSectionRenameMap() {
|
|
|
|
if (config->dataConst) {
|
|
|
|
SmallVector<StringRef> v{section_names::got,
|
|
|
|
section_names::authGot,
|
|
|
|
section_names::authPtr,
|
|
|
|
section_names::nonLazySymbolPtr,
|
|
|
|
section_names::const_,
|
|
|
|
section_names::cfString,
|
|
|
|
section_names::moduleInitFunc,
|
|
|
|
section_names::moduleTermFunc,
|
|
|
|
section_names::objcClassList,
|
|
|
|
section_names::objcNonLazyClassList,
|
|
|
|
section_names::objcCatList,
|
|
|
|
section_names::objcNonLazyCatList,
|
|
|
|
section_names::objcProtoList,
|
|
|
|
section_names::objcImageInfo};
|
|
|
|
for (StringRef s : v)
|
|
|
|
config->sectionRenameMap[{segment_names::data, s}] = {
|
|
|
|
segment_names::dataConst, s};
|
|
|
|
}
|
|
|
|
config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = {
|
|
|
|
segment_names::text, section_names::text};
|
|
|
|
config->sectionRenameMap[{segment_names::import, section_names::pointers}] = {
|
|
|
|
config->dataConst ? segment_names::dataConst : segment_names::data,
|
|
|
|
section_names::nonLazySymbolPtr};
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:47:16 +08:00
|
|
|
static inline char toLowerDash(char x) {
|
|
|
|
if (x >= 'A' && x <= 'Z')
|
|
|
|
return x - 'A' + 'a';
|
|
|
|
else if (x == ' ')
|
|
|
|
return '-';
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string lowerDash(StringRef s) {
|
|
|
|
return std::string(map_iterator(s.begin(), toLowerDash),
|
|
|
|
map_iterator(s.end(), toLowerDash));
|
|
|
|
}
|
|
|
|
|
2021-03-05 03:36:47 +08:00
|
|
|
// Has the side-effect of setting Config::platformInfo.
|
2021-03-10 12:40:08 +08:00
|
|
|
static PlatformKind parsePlatformVersion(const ArgList &args) {
|
|
|
|
const Arg *arg = args.getLastArg(OPT_platform_version);
|
2021-03-04 04:52:10 +08:00
|
|
|
if (!arg) {
|
|
|
|
error("must specify -platform_version");
|
2021-03-05 03:36:47 +08:00
|
|
|
return PlatformKind::unknown;
|
2021-03-04 04:52:10 +08:00
|
|
|
}
|
2021-03-04 04:52:06 +08:00
|
|
|
|
2020-08-11 09:47:16 +08:00
|
|
|
StringRef platformStr = arg->getValue(0);
|
|
|
|
StringRef minVersionStr = arg->getValue(1);
|
|
|
|
StringRef sdkVersionStr = arg->getValue(2);
|
|
|
|
|
|
|
|
// TODO(compnerd) see if we can generate this case list via XMACROS
|
2021-03-05 03:36:47 +08:00
|
|
|
PlatformKind platform =
|
2020-12-24 00:24:02 +08:00
|
|
|
StringSwitch<PlatformKind>(lowerDash(platformStr))
|
|
|
|
.Cases("macos", "1", PlatformKind::macOS)
|
|
|
|
.Cases("ios", "2", PlatformKind::iOS)
|
|
|
|
.Cases("tvos", "3", PlatformKind::tvOS)
|
|
|
|
.Cases("watchos", "4", PlatformKind::watchOS)
|
|
|
|
.Cases("bridgeos", "5", PlatformKind::bridgeOS)
|
|
|
|
.Cases("mac-catalyst", "6", PlatformKind::macCatalyst)
|
|
|
|
.Cases("ios-simulator", "7", PlatformKind::iOSSimulator)
|
|
|
|
.Cases("tvos-simulator", "8", PlatformKind::tvOSSimulator)
|
|
|
|
.Cases("watchos-simulator", "9", PlatformKind::watchOSSimulator)
|
|
|
|
.Cases("driverkit", "10", PlatformKind::driverKit)
|
|
|
|
.Default(PlatformKind::unknown);
|
2021-03-05 03:36:47 +08:00
|
|
|
if (platform == PlatformKind::unknown)
|
2020-08-11 09:47:16 +08:00
|
|
|
error(Twine("malformed platform: ") + platformStr);
|
|
|
|
// TODO: check validity of version strings, which varies by platform
|
|
|
|
// NOTE: ld64 accepts version strings with 5 components
|
|
|
|
// llvm::VersionTuple accepts no more than 4 components
|
|
|
|
// Has Apple ever published version strings with 5 components?
|
2021-03-05 03:36:47 +08:00
|
|
|
if (config->platformInfo.minimum.tryParse(minVersionStr))
|
2020-08-11 09:47:16 +08:00
|
|
|
error(Twine("malformed minimum version: ") + minVersionStr);
|
2021-03-05 03:36:47 +08:00
|
|
|
if (config->platformInfo.sdk.tryParse(sdkVersionStr))
|
2020-08-11 09:47:16 +08:00
|
|
|
error(Twine("malformed sdk version: ") + sdkVersionStr);
|
2021-03-04 04:52:06 +08:00
|
|
|
return platform;
|
2020-06-16 03:36:32 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 03:36:47 +08:00
|
|
|
// Has the side-effect of setting Config::target.
|
2021-03-10 12:40:08 +08:00
|
|
|
static TargetInfo *createTargetInfo(InputArgList &args) {
|
2021-03-05 03:36:47 +08:00
|
|
|
StringRef archName = args.getLastArgValue(OPT_arch);
|
|
|
|
if (archName.empty())
|
|
|
|
fatal("must specify -arch");
|
|
|
|
PlatformKind platform = parsePlatformVersion(args);
|
|
|
|
|
2021-04-21 20:41:14 +08:00
|
|
|
config->platformInfo.target =
|
|
|
|
MachO::Target(getArchitectureFromName(archName), platform);
|
2021-03-05 03:36:47 +08:00
|
|
|
|
2021-05-01 04:17:25 +08:00
|
|
|
uint32_t cpuType;
|
|
|
|
uint32_t cpuSubtype;
|
|
|
|
std::tie(cpuType, cpuSubtype) = getCPUTypeFromArchitecture(config->arch());
|
|
|
|
|
|
|
|
switch (cpuType) {
|
2021-03-12 02:28:08 +08:00
|
|
|
case CPU_TYPE_X86_64:
|
2021-03-05 03:36:47 +08:00
|
|
|
return createX86_64TargetInfo();
|
2021-03-12 02:28:08 +08:00
|
|
|
case CPU_TYPE_ARM64:
|
2021-03-05 03:36:47 +08:00
|
|
|
return createARM64TargetInfo();
|
2021-04-16 09:14:32 +08:00
|
|
|
case CPU_TYPE_ARM64_32:
|
|
|
|
return createARM64_32TargetInfo();
|
2021-05-01 04:17:25 +08:00
|
|
|
case CPU_TYPE_ARM:
|
|
|
|
return createARMTargetInfo(cpuSubtype);
|
2021-03-05 03:36:47 +08:00
|
|
|
default:
|
|
|
|
fatal("missing or unsupported -arch " + archName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 04:52:06 +08:00
|
|
|
static UndefinedSymbolTreatment
|
2021-03-10 12:40:08 +08:00
|
|
|
getUndefinedSymbolTreatment(const ArgList &args) {
|
2021-03-04 04:52:06 +08:00
|
|
|
StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
|
2021-02-18 21:48:07 +08:00
|
|
|
auto treatment =
|
2021-01-10 00:58:19 +08:00
|
|
|
StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
|
2021-03-04 04:52:06 +08:00
|
|
|
.Cases("error", "", UndefinedSymbolTreatment::error)
|
2020-12-14 11:31:33 +08:00
|
|
|
.Case("warning", UndefinedSymbolTreatment::warning)
|
|
|
|
.Case("suppress", UndefinedSymbolTreatment::suppress)
|
|
|
|
.Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
|
|
|
|
.Default(UndefinedSymbolTreatment::unknown);
|
2021-02-18 21:48:07 +08:00
|
|
|
if (treatment == UndefinedSymbolTreatment::unknown) {
|
2020-12-14 11:31:33 +08:00
|
|
|
warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
|
|
|
|
"', defaulting to 'error'");
|
2021-02-18 21:48:07 +08:00
|
|
|
treatment = UndefinedSymbolTreatment::error;
|
|
|
|
} else if (config->namespaceKind == NamespaceKind::twolevel &&
|
|
|
|
(treatment == UndefinedSymbolTreatment::warning ||
|
|
|
|
treatment == UndefinedSymbolTreatment::suppress)) {
|
|
|
|
if (treatment == UndefinedSymbolTreatment::warning)
|
|
|
|
error("'-undefined warning' only valid with '-flat_namespace'");
|
|
|
|
else
|
|
|
|
error("'-undefined suppress' only valid with '-flat_namespace'");
|
|
|
|
treatment = UndefinedSymbolTreatment::error;
|
2020-12-14 11:31:33 +08:00
|
|
|
}
|
2021-03-04 04:52:06 +08:00
|
|
|
return treatment;
|
2020-12-14 11:31:33 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 00:58:17 +08:00
|
|
|
static ICFLevel getICFLevel(const ArgList &args) {
|
|
|
|
bool noDeduplicate = args.hasArg(OPT_no_deduplicate);
|
2021-06-19 00:38:50 +08:00
|
|
|
StringRef icfLevelStr = args.getLastArgValue(OPT_icf_eq);
|
2021-05-20 00:58:17 +08:00
|
|
|
auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr)
|
|
|
|
.Cases("none", "", ICFLevel::none)
|
|
|
|
.Case("safe", ICFLevel::safe)
|
|
|
|
.Case("all", ICFLevel::all)
|
|
|
|
.Default(ICFLevel::unknown);
|
|
|
|
if (icfLevel == ICFLevel::unknown) {
|
2021-06-19 00:38:50 +08:00
|
|
|
warn(Twine("unknown --icf=OPTION `") + icfLevelStr +
|
2021-05-20 00:58:17 +08:00
|
|
|
"', defaulting to `none'");
|
|
|
|
icfLevel = ICFLevel::none;
|
|
|
|
} else if (icfLevel != ICFLevel::none && noDeduplicate) {
|
2021-06-19 00:38:50 +08:00
|
|
|
warn(Twine("`--icf=" + icfLevelStr +
|
2021-05-20 00:58:17 +08:00
|
|
|
"' conflicts with -no_deduplicate, setting to `none'"));
|
|
|
|
icfLevel = ICFLevel::none;
|
|
|
|
} else if (icfLevel == ICFLevel::safe) {
|
2021-06-19 00:38:50 +08:00
|
|
|
warn(Twine("`--icf=safe' is not yet implemented, reverting to `none'"));
|
2021-05-20 00:58:17 +08:00
|
|
|
icfLevel = ICFLevel::none;
|
|
|
|
}
|
|
|
|
return icfLevel;
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
static void warnIfDeprecatedOption(const Option &opt) {
|
2020-06-16 03:36:32 +08:00
|
|
|
if (!opt.getGroup().isValid())
|
|
|
|
return;
|
|
|
|
if (opt.getGroup().getID() == OPT_grp_deprecated) {
|
|
|
|
warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
|
|
|
|
warn(opt.getHelpText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
static void warnIfUnimplementedOption(const Option &opt) {
|
2020-09-23 23:53:37 +08:00
|
|
|
if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
|
2020-06-16 03:36:32 +08:00
|
|
|
return;
|
|
|
|
switch (opt.getGroup().getID()) {
|
|
|
|
case OPT_grp_deprecated:
|
|
|
|
// warn about deprecated options elsewhere
|
|
|
|
break;
|
|
|
|
case OPT_grp_undocumented:
|
|
|
|
warn("Option `" + opt.getPrefixedName() +
|
|
|
|
"' is undocumented. Should lld implement it?");
|
|
|
|
break;
|
|
|
|
case OPT_grp_obsolete:
|
|
|
|
warn("Option `" + opt.getPrefixedName() +
|
|
|
|
"' is obsolete. Please modernize your usage.");
|
|
|
|
break;
|
|
|
|
case OPT_grp_ignored:
|
|
|
|
warn("Option `" + opt.getPrefixedName() + "' is ignored.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warn("Option `" + opt.getPrefixedName() +
|
|
|
|
"' is not yet implemented. Stay tuned...");
|
|
|
|
break;
|
2020-05-13 02:02:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
static const char *getReproduceOption(InputArgList &args) {
|
2021-03-10 12:15:29 +08:00
|
|
|
if (const Arg *arg = args.getLastArg(OPT_reproduce))
|
2020-11-29 11:38:27 +08:00
|
|
|
return arg->getValue();
|
|
|
|
return getenv("LLD_REPRODUCE");
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:08:56 +08:00
|
|
|
static void parseClangOption(StringRef opt, const Twine &msg) {
|
|
|
|
std::string err;
|
|
|
|
raw_string_ostream os(err);
|
|
|
|
|
|
|
|
const char *argv[] = {"lld", opt.data()};
|
|
|
|
if (cl::ParseCommandLineOptions(2, argv, "", &os))
|
|
|
|
return;
|
|
|
|
os.flush();
|
|
|
|
error(msg + ": " + StringRef(err).trim());
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
|
|
|
|
const Arg *arg = args.getLastArg(id);
|
2020-12-15 07:24:50 +08:00
|
|
|
if (!arg)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (config->outputType != MH_DYLIB) {
|
|
|
|
error(arg->getAsString(args) + ": only valid with -dylib");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:37:37 +08:00
|
|
|
PackedVersion version;
|
|
|
|
if (!version.parse32(arg->getValue())) {
|
2020-12-15 07:24:50 +08:00
|
|
|
error(arg->getAsString(args) + ": malformed version");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:37:37 +08:00
|
|
|
return version.rawValue();
|
2020-12-15 07:24:50 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 02:08:12 +08:00
|
|
|
static uint32_t parseProtection(StringRef protStr) {
|
|
|
|
uint32_t prot = 0;
|
|
|
|
for (char c : protStr) {
|
|
|
|
switch (c) {
|
|
|
|
case 'r':
|
|
|
|
prot |= VM_PROT_READ;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
prot |= VM_PROT_WRITE;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
prot |= VM_PROT_EXECUTE;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("unknown -segprot letter '" + Twine(c) + "' in " + protStr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prot;
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:43:48 +08:00
|
|
|
static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) {
|
|
|
|
std::vector<SectionAlign> sectAligns;
|
|
|
|
for (const Arg *arg : args.filtered(OPT_sectalign)) {
|
|
|
|
StringRef segName = arg->getValue(0);
|
|
|
|
StringRef sectName = arg->getValue(1);
|
|
|
|
StringRef alignStr = arg->getValue(2);
|
|
|
|
if (alignStr.startswith("0x") || alignStr.startswith("0X"))
|
|
|
|
alignStr = alignStr.drop_front(2);
|
|
|
|
uint32_t align;
|
|
|
|
if (alignStr.getAsInteger(16, align)) {
|
|
|
|
error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) +
|
|
|
|
"' as number");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isPowerOf2_32(align)) {
|
|
|
|
error("-sectalign: '" + StringRef(arg->getValue(2)) +
|
|
|
|
"' (in base 16) not a power of two");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sectAligns.push_back({segName, sectName, align});
|
|
|
|
}
|
|
|
|
return sectAligns;
|
|
|
|
}
|
|
|
|
|
2021-04-26 07:00:24 +08:00
|
|
|
static bool dataConstDefault(const InputArgList &args) {
|
|
|
|
switch (config->outputType) {
|
|
|
|
case MH_EXECUTE:
|
|
|
|
return !args.hasArg(OPT_no_pie);
|
|
|
|
case MH_BUNDLE:
|
|
|
|
// FIXME: return false when -final_name ...
|
|
|
|
// has prefix "/System/Library/UserEventPlugins/"
|
|
|
|
// or matches "/usr/libexec/locationd" "/usr/libexec/terminusd"
|
|
|
|
return true;
|
|
|
|
case MH_DYLIB:
|
|
|
|
return true;
|
|
|
|
case MH_OBJECT:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
llvm_unreachable(
|
|
|
|
"unsupported output type for determining data-const default");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-04 04:15:09 +08:00
|
|
|
void SymbolPatterns::clear() {
|
|
|
|
literals.clear();
|
|
|
|
globs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolPatterns::insert(StringRef symbolName) {
|
|
|
|
if (symbolName.find_first_of("*?[]") == StringRef::npos)
|
|
|
|
literals.insert(CachedHashStringRef(symbolName));
|
|
|
|
else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName))
|
|
|
|
globs.emplace_back(*pattern);
|
|
|
|
else
|
|
|
|
error("invalid symbol-name pattern: " + symbolName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
|
|
|
|
return literals.contains(CachedHashStringRef(symbolName));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolPatterns::matchGlob(StringRef symbolName) const {
|
|
|
|
for (const llvm::GlobPattern &glob : globs)
|
|
|
|
if (glob.match(symbolName))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymbolPatterns::match(StringRef symbolName) const {
|
|
|
|
return matchLiteral(symbolName) || matchGlob(symbolName);
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
static void handleSymbolPatterns(InputArgList &args,
|
2021-03-04 04:15:09 +08:00
|
|
|
SymbolPatterns &symbolPatterns,
|
|
|
|
unsigned singleOptionCode,
|
|
|
|
unsigned listFileOptionCode) {
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args.filtered(singleOptionCode))
|
2021-03-04 04:15:09 +08:00
|
|
|
symbolPatterns.insert(arg->getValue());
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args.filtered(listFileOptionCode)) {
|
2021-03-04 04:15:09 +08:00
|
|
|
StringRef path = arg->getValue();
|
|
|
|
Optional<MemoryBufferRef> buffer = readFile(path);
|
|
|
|
if (!buffer) {
|
|
|
|
error("Could not read symbol file: " + path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MemoryBufferRef mbref = *buffer;
|
|
|
|
for (StringRef line : args::getLines(mbref)) {
|
|
|
|
line = line.take_until([](char c) { return c == '#'; }).trim();
|
|
|
|
if (!line.empty())
|
|
|
|
symbolPatterns.insert(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 02:39:44 +08:00
|
|
|
void createFiles(const InputArgList &args) {
|
|
|
|
TimeTraceScope timeScope("Load input files");
|
|
|
|
// This loop should be reserved for options whose exact ordering matters.
|
|
|
|
// Other options should be handled via filtered() and/or getLastArg().
|
|
|
|
for (const Arg *arg : args) {
|
|
|
|
const Option &opt = arg->getOption();
|
|
|
|
warnIfDeprecatedOption(opt);
|
|
|
|
warnIfUnimplementedOption(opt);
|
|
|
|
|
|
|
|
switch (opt.getID()) {
|
|
|
|
case OPT_INPUT:
|
2021-06-02 20:54:36 +08:00
|
|
|
addFile(rerootPath(arg->getValue()), /*forceLoadArchive=*/false);
|
2021-03-26 02:39:44 +08:00
|
|
|
break;
|
2021-06-02 23:06:42 +08:00
|
|
|
case OPT_needed_library:
|
|
|
|
if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
|
|
|
|
addFile(rerootPath(arg->getValue()), false)))
|
|
|
|
dylibFile->forceNeeded = true;
|
|
|
|
break;
|
2021-06-02 08:17:04 +08:00
|
|
|
case OPT_reexport_library:
|
2021-06-02 20:54:36 +08:00
|
|
|
if (auto *dylibFile = dyn_cast_or_null<DylibFile>(addFile(
|
|
|
|
rerootPath(arg->getValue()), /*forceLoadArchive=*/false))) {
|
2021-06-02 08:17:04 +08:00
|
|
|
config->hasReexports = true;
|
|
|
|
dylibFile->reexport = true;
|
|
|
|
}
|
|
|
|
break;
|
2021-03-26 02:39:44 +08:00
|
|
|
case OPT_weak_library:
|
2021-04-16 09:14:30 +08:00
|
|
|
if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
|
2021-06-02 20:54:36 +08:00
|
|
|
addFile(rerootPath(arg->getValue()), /*forceLoadArchive=*/false)))
|
2021-03-26 02:39:44 +08:00
|
|
|
dylibFile->forceWeakImport = true;
|
|
|
|
break;
|
|
|
|
case OPT_filelist:
|
|
|
|
addFileList(arg->getValue());
|
|
|
|
break;
|
|
|
|
case OPT_force_load:
|
2021-06-02 20:54:36 +08:00
|
|
|
addFile(rerootPath(arg->getValue()), /*forceLoadArchive=*/true);
|
2021-03-26 02:39:44 +08:00
|
|
|
break;
|
|
|
|
case OPT_l:
|
2021-06-02 23:06:42 +08:00
|
|
|
case OPT_needed_l:
|
2021-06-02 08:17:04 +08:00
|
|
|
case OPT_reexport_l:
|
2021-03-26 02:39:44 +08:00
|
|
|
case OPT_weak_l:
|
2021-06-02 23:06:42 +08:00
|
|
|
addLibrary(arg->getValue(), opt.getID() == OPT_needed_l,
|
|
|
|
opt.getID() == OPT_weak_l, opt.getID() == OPT_reexport_l,
|
2021-06-08 11:48:16 +08:00
|
|
|
/*isExplicit=*/true, /*forceLoad=*/false);
|
2021-03-26 02:39:44 +08:00
|
|
|
break;
|
|
|
|
case OPT_framework:
|
2021-06-02 23:06:42 +08:00
|
|
|
case OPT_needed_framework:
|
2021-06-02 08:17:04 +08:00
|
|
|
case OPT_reexport_framework:
|
2021-03-26 02:39:44 +08:00
|
|
|
case OPT_weak_framework:
|
2021-06-02 23:06:42 +08:00
|
|
|
addFramework(arg->getValue(), opt.getID() == OPT_needed_framework,
|
|
|
|
opt.getID() == OPT_weak_framework,
|
2021-06-02 08:17:04 +08:00
|
|
|
opt.getID() == OPT_reexport_framework, /*isExplicit=*/true);
|
2021-03-26 02:39:44 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 00:58:19 +08:00
|
|
|
bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
|
2020-04-03 02:54:05 +08:00
|
|
|
raw_ostream &stdoutOS, raw_ostream &stderrOS) {
|
|
|
|
lld::stdoutOS = &stdoutOS;
|
|
|
|
lld::stderrOS = &stderrOS;
|
|
|
|
|
2021-03-02 03:45:17 +08:00
|
|
|
errorHandler().cleanupCallback = []() { freeArena(); };
|
|
|
|
|
|
|
|
errorHandler().logName = args::getFilenameWithoutExe(argsArr[0]);
|
2020-04-30 06:42:32 +08:00
|
|
|
stderrOS.enable_colors(stderrOS.has_colors());
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
MachOOptTable parser;
|
2021-03-10 12:40:08 +08:00
|
|
|
InputArgList args = parser.parse(argsArr.slice(1));
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-04-26 08:28:49 +08:00
|
|
|
errorHandler().errorLimitExceededMsg =
|
|
|
|
"too many errors emitted, stopping now "
|
|
|
|
"(use --error-limit=0 to see all errors)";
|
|
|
|
errorHandler().errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
|
2021-03-30 08:33:48 +08:00
|
|
|
errorHandler().verbose = args.hasArg(OPT_verbose);
|
2021-04-26 08:28:49 +08:00
|
|
|
|
2020-06-16 03:36:32 +08:00
|
|
|
if (args.hasArg(OPT_help_hidden)) {
|
|
|
|
parser.printHelp(argsArr[0], /*showHidden=*/true);
|
|
|
|
return true;
|
2020-12-23 04:51:20 +08:00
|
|
|
}
|
|
|
|
if (args.hasArg(OPT_help)) {
|
2020-06-16 03:36:32 +08:00
|
|
|
parser.printHelp(argsArr[0], /*showHidden=*/false);
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-23 04:51:20 +08:00
|
|
|
if (args.hasArg(OPT_version)) {
|
|
|
|
message(getLLDVersion());
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-16 03:36:32 +08:00
|
|
|
|
2021-05-06 02:38:36 +08:00
|
|
|
config = make<Configuration>();
|
|
|
|
symtab = make<SymbolTable>();
|
|
|
|
target = createTargetInfo(args);
|
|
|
|
depTracker =
|
|
|
|
make<DependencyTracker>(args.getLastArgValue(OPT_dependency_info));
|
|
|
|
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
// Must be set before any InputSections and Symbols are created.
|
|
|
|
config->deadStrip = args.hasArg(OPT_dead_strip);
|
|
|
|
|
2021-05-06 02:38:36 +08:00
|
|
|
config->systemLibraryRoots = getSystemLibraryRoots(args);
|
2020-11-29 11:38:27 +08:00
|
|
|
if (const char *path = getReproduceOption(args)) {
|
|
|
|
// Note that --reproduce is a debug option so you can ignore it
|
|
|
|
// if you are trying to understand the whole picture of the code.
|
|
|
|
Expected<std::unique_ptr<TarWriter>> errOrWriter =
|
|
|
|
TarWriter::create(path, path::stem(path));
|
|
|
|
if (errOrWriter) {
|
|
|
|
tar = std::move(*errOrWriter);
|
|
|
|
tar->append("response.txt", createResponseFile(args));
|
|
|
|
tar->append("version.txt", getLLDVersion() + "\n");
|
|
|
|
} else {
|
|
|
|
error("--reproduce: " + toString(errOrWriter.takeError()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 02:39:45 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_threads_eq)) {
|
|
|
|
StringRef v(arg->getValue());
|
|
|
|
unsigned threads = 0;
|
|
|
|
if (!llvm::to_integer(v, threads, 0) || threads == 0)
|
|
|
|
error(arg->getSpelling() + ": expected a positive integer, but got '" +
|
|
|
|
arg->getValue() + "'");
|
|
|
|
parallel::strategy = hardware_concurrency(threads);
|
2021-04-09 00:14:47 +08:00
|
|
|
config->thinLTOJobs = v;
|
2021-03-26 02:39:45 +08:00
|
|
|
}
|
2021-04-09 00:14:47 +08:00
|
|
|
if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
|
|
|
|
config->thinLTOJobs = arg->getValue();
|
|
|
|
if (!get_threadpool_strategy(config->thinLTOJobs))
|
|
|
|
error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
|
2021-03-26 02:39:45 +08:00
|
|
|
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_u)) {
|
2021-02-09 21:18:23 +08:00
|
|
|
config->explicitUndefineds.push_back(symtab->addUndefined(
|
|
|
|
arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
|
|
|
|
}
|
2021-02-26 08:56:31 +08:00
|
|
|
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_U))
|
2021-02-26 08:56:31 +08:00
|
|
|
symtab->addDynamicLookup(arg->getValue());
|
|
|
|
|
2021-03-18 22:38:30 +08:00
|
|
|
config->mapFile = args.getLastArgValue(OPT_map);
|
2020-04-03 02:54:05 +08:00
|
|
|
config->outputFile = args.getLastArgValue(OPT_o, "a.out");
|
2021-04-09 02:12:20 +08:00
|
|
|
config->astPaths = args.getAllArgValues(OPT_add_ast_path);
|
2020-07-31 05:38:58 +08:00
|
|
|
config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
|
2020-09-22 02:04:13 +08:00
|
|
|
config->headerPadMaxInstallNames =
|
|
|
|
args.hasArg(OPT_headerpad_max_install_names);
|
2021-06-10 03:16:45 +08:00
|
|
|
config->printDylibSearch =
|
|
|
|
args.hasArg(OPT_print_dylib_search) || getenv("RC_TRACE_DYLIB_SEARCHING");
|
2020-12-03 07:57:30 +08:00
|
|
|
config->printEachFile = args.hasArg(OPT_t);
|
2020-12-03 07:59:00 +08:00
|
|
|
config->printWhyLoad = args.hasArg(OPT_why_load);
|
2020-09-01 14:23:37 +08:00
|
|
|
config->outputType = getOutputType(args);
|
2021-03-10 12:40:08 +08:00
|
|
|
if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
|
2021-02-23 02:03:02 +08:00
|
|
|
if (config->outputType != MH_BUNDLE)
|
|
|
|
error("-bundle_loader can only be used with MachO bundle output");
|
2021-06-02 20:54:36 +08:00
|
|
|
addFile(arg->getValue(), /*forceLoadArchive=*/false, /*isExplicit=*/false,
|
|
|
|
/*isBundleLoader=*/true);
|
2021-02-23 02:03:02 +08:00
|
|
|
}
|
2020-12-03 12:34:17 +08:00
|
|
|
config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
|
2021-01-13 03:41:56 +08:00
|
|
|
config->ltoNewPassManager =
|
|
|
|
args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager,
|
|
|
|
LLVM_ENABLE_NEW_PASS_MANAGER);
|
2020-08-13 10:50:28 +08:00
|
|
|
config->runtimePaths = args::getStrings(args, OPT_rpath);
|
2020-08-26 11:00:42 +08:00
|
|
|
config->allLoad = args.hasArg(OPT_all_load);
|
2020-09-19 11:51:38 +08:00
|
|
|
config->forceLoadObjC = args.hasArg(OPT_ObjC);
|
2021-06-08 11:48:16 +08:00
|
|
|
config->forceLoadSwift = args.hasArg(OPT_force_load_swift_libs);
|
2021-06-01 10:12:35 +08:00
|
|
|
config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs);
|
clang+lld: Improve clang+ld.darwinnew.lld interaction, pass -demangle
This patch:
- adds an ld64.lld.darwinnew symlink for lld, to go with f2710d4b576,
so that `clang -fuse-ld=lld.darwinnew` can be used to test new
Mach-O lld while it's in bring-up. (The expectation is that we'll
remove this again once new Mach-O lld is the defauld and only Mach-O
lld.)
- lets the clang driver know if the linker is lld (currently
only triggered if `-fuse-ld=lld` or `-fuse-ld=lld.darwinnew` is
passed). Currently only used for the next point, but could be used
to implement other features that need close coordination between
compiler and linker, e.g. having a diag for calling `clang++` instead
of `clang` when link errors are caused by a missing C++ stdlib.
- lets the clang driver pass `-demangle` to Mach-O lld (both old and
new), in addition to ld64
- implements -demangle for new Mach-O lld
- changes demangleItanium() to accept _Z, __Z, ___Z, ____Z prefixes
(and updates one test added in D68014). Mach-O has an extra
underscore for symbols, and the three (or, on Mach-O, four)
underscores are used for block names.
Differential Revision: https://reviews.llvm.org/D91884
2020-11-21 02:57:44 +08:00
|
|
|
config->demangle = args.hasArg(OPT_demangle);
|
2020-12-10 07:08:05 +08:00
|
|
|
config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
|
2021-06-18 23:47:49 +08:00
|
|
|
config->emitFunctionStarts =
|
|
|
|
args.hasFlag(OPT_function_starts, OPT_no_function_starts, true);
|
2021-04-17 04:46:45 +08:00
|
|
|
config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle);
|
2021-06-18 23:47:49 +08:00
|
|
|
config->emitDataInCodeInfo =
|
|
|
|
args.hasFlag(OPT_data_in_code_info, OPT_no_data_in_code_info, true);
|
[lld-macho] Implement cstring deduplication
Our implementation draws heavily from LLD-ELF's, which in turn delegates
its string deduplication to llvm-mc's StringTableBuilder. The messiness of
this diff is largely due to the fact that we've previously assumed that
all InputSections get concatenated together to form the output. This is
no longer true with CStringInputSections, which split their contents into
StringPieces. StringPieces are much more lightweight than InputSections,
which is important as we create a lot of them. They may also overlap in
the output, which makes it possible for strings to be tail-merged. In
fact, the initial version of this diff implemented tail merging, but
I've dropped it for reasons I'll explain later.
**Alignment Issues**
Mergeable cstring literals are found under the `__TEXT,__cstring`
section. In contrast to ELF, which puts strings that need different
alignments into different sections, clang's Mach-O backend puts them all
in one section. Strings that need to be aligned have the `.p2align`
directive emitted before them, which simply translates into zero padding
in the object file.
I *think* ld64 extracts the desired per-string alignment from this data
by preserving each string's offset from the last section-aligned
address. I'm not entirely certain since it doesn't seem consistent about
doing this; but perhaps this can be chalked up to cases where ld64 has
to deduplicate strings with different offset/alignment combos -- it
seems to pick one of their alignments to preserve. This doesn't seem
correct in general; we can in fact can induce ld64 to produce a crashing
binary just by linking in an additional object file that only contains
cstrings and no code. See PR50563 for details.
Moreover, this scheme seems rather inefficient: since unaligned and
aligned strings are all put in the same section, which has a single
alignment value, it doesn't seem possible to tell whether a given string
doesn't have any alignment requirements. Preserving offset+alignments
for strings that don't need it is wasteful.
In practice, the crashes seen so far seem to stem from x86_64 SIMD
operations on cstrings. X86_64 requires SIMD accesses to be
16-byte-aligned. So for now, I'm thinking of just aligning all strings
to 16 bytes on x86_64. This is indeed wasteful, but implementation-wise
it's simpler than preserving per-string alignment+offsets. It also
avoids the aforementioned crash after deduplication of
differently-aligned strings. Finally, the overhead is not huge: using
16-byte alignment (vs no alignment) is only a 0.5% size overhead when
linking chromium_framework.
With these alignment requirements, it doesn't make sense to attempt tail
merging -- most strings will not be eligible since their overlaps aren't
likely to start at a 16-byte boundary. Tail-merging (with alignment) for
chromium_framework only improves size by 0.3%.
It's worth noting that LLD-ELF only does tail merging at `-O2`. By
default (at `-O1`), it just deduplicates w/o tail merging. @thakis has
also mentioned that they saw it regress compressed size in some cases
and therefore turned it off. `ld64` does not seem to do tail merging at
all.
**Performance Numbers**
CString deduplication reduces chromium_framework from 250MB to 242MB, or
about a 3.2% reduction.
Numbers for linking chromium_framework on my 3.2 GHz 16-Core Intel Xeon W:
N Min Max Median Avg Stddev
x 20 3.91 4.03 3.935 3.95 0.034641016
+ 20 3.99 4.14 4.015 4.0365 0.0492336
Difference at 95.0% confidence
0.0865 +/- 0.027245
2.18987% +/- 0.689746%
(Student's t, pooled s = 0.0425673)
As expected, cstring merging incurs some non-trivial overhead.
When passing `--no-literal-merge`, it seems that performance is the
same, i.e. the refactoring in this diff didn't cost us.
N Min Max Median Avg Stddev
x 20 3.91 4.03 3.935 3.95 0.034641016
+ 20 3.89 4.02 3.935 3.9435 0.043197831
No difference proven at 95.0% confidence
Reviewed By: #lld-macho, gkm
Differential Revision: https://reviews.llvm.org/D102964
2021-06-08 11:47:12 +08:00
|
|
|
config->dedupLiterals = args.hasArg(OPT_deduplicate_literals);
|
2021-04-17 04:46:45 +08:00
|
|
|
|
2021-06-01 18:55:36 +08:00
|
|
|
// FIXME: Add a commandline flag for this too.
|
|
|
|
config->zeroModTime = getenv("ZERO_AR_DATE");
|
|
|
|
|
2021-04-22 01:35:12 +08:00
|
|
|
std::array<PlatformKind, 3> encryptablePlatforms{
|
|
|
|
PlatformKind::iOS, PlatformKind::watchOS, PlatformKind::tvOS};
|
2021-04-22 03:43:38 +08:00
|
|
|
config->emitEncryptionInfo =
|
|
|
|
args.hasFlag(OPT_encryptable, OPT_no_encryption,
|
|
|
|
is_contained(encryptablePlatforms, config->platform()));
|
2021-04-22 01:35:12 +08:00
|
|
|
|
2021-05-19 23:07:39 +08:00
|
|
|
#ifndef LLVM_HAVE_LIBXAR
|
2021-04-17 04:46:45 +08:00
|
|
|
if (config->emitBitcodeBundle)
|
|
|
|
error("-bitcode_bundle unsupported because LLD wasn't built with libxar");
|
|
|
|
#endif
|
2020-04-22 04:37:57 +08:00
|
|
|
|
2021-03-09 23:02:24 +08:00
|
|
|
if (const Arg *arg = args.getLastArg(OPT_install_name)) {
|
|
|
|
if (config->outputType != MH_DYLIB)
|
|
|
|
warn(arg->getAsString(args) + ": ignored, only has effect with -dylib");
|
|
|
|
else
|
|
|
|
config->installName = arg->getValue();
|
|
|
|
} else if (config->outputType == MH_DYLIB) {
|
|
|
|
config->installName = config->outputFile;
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:17:01 +08:00
|
|
|
if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
|
|
|
|
if (config->outputType != MH_DYLIB)
|
|
|
|
warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
|
|
|
|
else
|
|
|
|
config->markDeadStrippableDylib = true;
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
|
2020-09-22 04:21:45 +08:00
|
|
|
config->staticLink = (arg->getOption().getID() == OPT_static);
|
|
|
|
|
2021-03-10 12:40:08 +08:00
|
|
|
if (const Arg *arg =
|
2021-03-04 04:52:06 +08:00
|
|
|
args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
|
2021-02-18 21:48:07 +08:00
|
|
|
config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
|
|
|
|
? NamespaceKind::twolevel
|
|
|
|
: NamespaceKind::flat;
|
2021-03-04 04:52:06 +08:00
|
|
|
|
|
|
|
config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
|
2021-02-18 21:48:07 +08:00
|
|
|
|
2021-05-20 00:58:17 +08:00
|
|
|
config->icfLevel = getICFLevel(args);
|
|
|
|
|
2021-05-10 08:05:45 +08:00
|
|
|
if (config->outputType == MH_EXECUTE)
|
|
|
|
config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
|
|
|
|
/*file=*/nullptr,
|
|
|
|
/*isWeakRef=*/false);
|
|
|
|
|
2020-09-19 11:51:38 +08:00
|
|
|
config->librarySearchPaths =
|
|
|
|
getLibrarySearchPaths(args, config->systemLibraryRoots);
|
|
|
|
config->frameworkSearchPaths =
|
|
|
|
getFrameworkSearchPaths(args, config->systemLibraryRoots);
|
2021-03-10 12:40:08 +08:00
|
|
|
if (const Arg *arg =
|
2020-09-20 23:37:20 +08:00
|
|
|
args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
|
|
|
|
config->searchDylibsFirst =
|
2021-01-10 10:17:59 +08:00
|
|
|
arg->getOption().getID() == OPT_search_dylibs_first;
|
2020-06-20 12:13:03 +08:00
|
|
|
|
2020-12-15 07:24:50 +08:00
|
|
|
config->dylibCompatibilityVersion =
|
|
|
|
parseDylibVersion(args, OPT_compatibility_version);
|
|
|
|
config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
|
|
|
|
|
2021-04-26 07:00:24 +08:00
|
|
|
config->dataConst =
|
|
|
|
args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args));
|
|
|
|
// Populate config->sectionRenameMap with builtin default renames.
|
|
|
|
// Options -rename_section and -rename_segment are able to override.
|
|
|
|
initializeSectionRenameMap();
|
2021-02-27 07:36:49 +08:00
|
|
|
// Reject every special character except '.' and '$'
|
|
|
|
// TODO(gkm): verify that this is the proper set of invalid chars
|
|
|
|
StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
|
|
|
|
auto validName = [invalidNameChars](StringRef s) {
|
|
|
|
if (s.find_first_of(invalidNameChars) != StringRef::npos)
|
|
|
|
error("invalid name for segment or section: " + s);
|
|
|
|
return s;
|
|
|
|
};
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_rename_section)) {
|
2021-02-27 07:36:49 +08:00
|
|
|
config->sectionRenameMap[{validName(arg->getValue(0)),
|
|
|
|
validName(arg->getValue(1))}] = {
|
|
|
|
validName(arg->getValue(2)), validName(arg->getValue(3))};
|
|
|
|
}
|
2021-03-10 12:15:29 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_rename_segment)) {
|
2021-02-27 07:36:49 +08:00
|
|
|
config->segmentRenameMap[validName(arg->getValue(0))] =
|
|
|
|
validName(arg->getValue(1));
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:43:48 +08:00
|
|
|
config->sectionAlignments = parseSectAlign(args);
|
|
|
|
|
2021-03-30 02:08:12 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_segprot)) {
|
|
|
|
StringRef segName = arg->getValue(0);
|
|
|
|
uint32_t maxProt = parseProtection(arg->getValue(1));
|
|
|
|
uint32_t initProt = parseProtection(arg->getValue(2));
|
2021-04-22 03:43:38 +08:00
|
|
|
if (maxProt != initProt && config->arch() != AK_i386)
|
2021-03-30 02:08:12 +08:00
|
|
|
error("invalid argument '" + arg->getAsString(args) +
|
|
|
|
"': max and init must be the same for non-i386 archs");
|
|
|
|
if (segName == segment_names::linkEdit)
|
|
|
|
error("-segprot cannot be used to change __LINKEDIT's protections");
|
|
|
|
config->segmentProtections.push_back({segName, maxProt, initProt});
|
|
|
|
}
|
|
|
|
|
2021-03-04 04:15:09 +08:00
|
|
|
handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
|
|
|
|
OPT_exported_symbols_list);
|
|
|
|
handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
|
|
|
|
OPT_unexported_symbols_list);
|
|
|
|
if (!config->exportedSymbols.empty() && !config->unexportedSymbols.empty()) {
|
|
|
|
error("cannot use both -exported_symbol* and -unexported_symbol* options\n"
|
|
|
|
">>> ignoring unexports");
|
|
|
|
config->unexportedSymbols.clear();
|
|
|
|
}
|
2021-05-08 09:05:47 +08:00
|
|
|
// Explicitly-exported literal symbols must be defined, but might
|
|
|
|
// languish in an archive if unreferenced elsewhere. Light a fire
|
|
|
|
// under those lazy symbols!
|
|
|
|
for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
|
|
|
|
symtab->addUndefined(cachedName.val(), /*file=*/nullptr,
|
|
|
|
/*isWeakRef=*/false);
|
2021-03-04 04:15:09 +08:00
|
|
|
|
2020-10-27 10:18:29 +08:00
|
|
|
config->saveTemps = args.hasArg(OPT_save_temps);
|
|
|
|
|
2021-03-06 01:12:56 +08:00
|
|
|
config->adhocCodesign = args.hasFlag(
|
|
|
|
OPT_adhoc_codesign, OPT_no_adhoc_codesign,
|
2021-04-22 03:43:38 +08:00
|
|
|
(config->arch() == AK_arm64 || config->arch() == AK_arm64e) &&
|
|
|
|
config->platform() == PlatformKind::macOS);
|
2021-03-05 22:07:58 +08:00
|
|
|
|
2020-04-22 04:37:57 +08:00
|
|
|
if (args.hasArg(OPT_v)) {
|
|
|
|
message(getLLDVersion());
|
2020-06-18 10:59:27 +08:00
|
|
|
message(StringRef("Library search paths:") +
|
2021-04-07 13:32:59 +08:00
|
|
|
(config->librarySearchPaths.empty()
|
|
|
|
? ""
|
|
|
|
: "\n\t" + join(config->librarySearchPaths, "\n\t")));
|
2020-06-18 10:59:27 +08:00
|
|
|
message(StringRef("Framework search paths:") +
|
2021-04-07 13:32:59 +08:00
|
|
|
(config->frameworkSearchPaths.empty()
|
|
|
|
? ""
|
|
|
|
: "\n\t" + join(config->frameworkSearchPaths, "\n\t")));
|
2020-04-22 04:37:57 +08:00
|
|
|
}
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
config->progName = argsArr[0];
|
2020-12-09 13:51:32 +08:00
|
|
|
|
2021-04-09 02:11:45 +08:00
|
|
|
config->timeTraceEnabled = args.hasArg(
|
|
|
|
OPT_time_trace, OPT_time_trace_granularity_eq, OPT_time_trace_file_eq);
|
2021-03-25 09:05:26 +08:00
|
|
|
config->timeTraceGranularity =
|
|
|
|
args::getInteger(args, OPT_time_trace_granularity_eq, 500);
|
2021-02-23 02:03:02 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
// Initialize time trace profiler.
|
|
|
|
if (config->timeTraceEnabled)
|
|
|
|
timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
|
|
|
|
|
|
|
|
{
|
2021-03-25 09:05:26 +08:00
|
|
|
TimeTraceScope timeScope("ExecuteLinker");
|
2021-03-11 22:04:27 +08:00
|
|
|
|
|
|
|
initLLVM(); // must be run before any call to addFile()
|
2021-03-26 02:39:44 +08:00
|
|
|
createFiles(args);
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
config->isPic = config->outputType == MH_DYLIB ||
|
2021-04-30 03:09:01 +08:00
|
|
|
config->outputType == MH_BUNDLE ||
|
|
|
|
(config->outputType == MH_EXECUTE &&
|
|
|
|
args.hasFlag(OPT_pie, OPT_no_pie, true));
|
2021-03-11 22:04:27 +08:00
|
|
|
|
|
|
|
// Now that all dylibs have been loaded, search for those that should be
|
|
|
|
// re-exported.
|
2021-04-07 13:32:59 +08:00
|
|
|
{
|
|
|
|
auto reexportHandler = [](const Arg *arg,
|
|
|
|
const std::vector<StringRef> &extensions) {
|
|
|
|
config->hasReexports = true;
|
|
|
|
StringRef searchName = arg->getValue();
|
|
|
|
if (!markReexport(searchName, extensions))
|
|
|
|
error(arg->getSpelling() + " " + searchName +
|
|
|
|
" does not match a supplied dylib");
|
|
|
|
};
|
|
|
|
std::vector<StringRef> extensions = {".tbd"};
|
|
|
|
for (const Arg *arg : args.filtered(OPT_sub_umbrella))
|
|
|
|
reexportHandler(arg, extensions);
|
|
|
|
|
|
|
|
extensions.push_back(".dylib");
|
|
|
|
for (const Arg *arg : args.filtered(OPT_sub_library))
|
|
|
|
reexportHandler(arg, extensions);
|
2021-03-11 22:04:27 +08:00
|
|
|
}
|
2020-04-24 11:16:49 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
// Parse LTO options.
|
|
|
|
if (const Arg *arg = args.getLastArg(OPT_mcpu))
|
|
|
|
parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
|
|
|
|
arg->getSpelling());
|
2020-12-08 21:08:56 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_mllvm))
|
|
|
|
parseClangOption(arg->getValue(), arg->getSpelling());
|
2020-12-08 21:08:56 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
compileBitcodeFiles();
|
|
|
|
replaceCommonSymbols();
|
2020-09-25 05:44:14 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
StringRef orderFile = args.getLastArgValue(OPT_order_file);
|
|
|
|
if (!orderFile.empty())
|
|
|
|
parseOrderFile(orderFile);
|
2020-05-06 07:37:34 +08:00
|
|
|
|
2021-05-11 03:45:18 +08:00
|
|
|
if (config->entry)
|
|
|
|
if (auto *undefined = dyn_cast<Undefined>(config->entry))
|
|
|
|
treatUndefinedSymbol(*undefined, "the entry point");
|
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
// FIXME: This prints symbols that are undefined both in input files and
|
|
|
|
// via -u flag twice.
|
2021-05-11 03:45:18 +08:00
|
|
|
for (const Symbol *sym : config->explicitUndefineds) {
|
|
|
|
if (const auto *undefined = dyn_cast<Undefined>(sym))
|
|
|
|
treatUndefinedSymbol(*undefined, "-u");
|
2021-03-11 22:04:27 +08:00
|
|
|
}
|
2021-03-11 08:45:18 +08:00
|
|
|
// Literal exported-symbol names must be defined, but glob
|
|
|
|
// patterns need not match.
|
|
|
|
for (const CachedHashStringRef &cachedName :
|
|
|
|
config->exportedSymbols.literals) {
|
|
|
|
if (const Symbol *sym = symtab->find(cachedName))
|
2021-05-11 03:45:18 +08:00
|
|
|
if (const auto *undefined = dyn_cast<Undefined>(sym))
|
|
|
|
treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)");
|
2021-03-11 08:45:18 +08:00
|
|
|
}
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-05-11 03:45:18 +08:00
|
|
|
// FIXME: should terminate the link early based on errors encountered so
|
|
|
|
// far?
|
|
|
|
|
2021-05-04 06:31:23 +08:00
|
|
|
createSyntheticSections();
|
2021-03-19 06:49:45 +08:00
|
|
|
createSyntheticSymbols();
|
2020-04-22 04:37:57 +08:00
|
|
|
|
2021-05-18 08:53:55 +08:00
|
|
|
if (!config->exportedSymbols.empty()) {
|
|
|
|
for (Symbol *sym : symtab->getSymbols()) {
|
|
|
|
if (auto *defined = dyn_cast<Defined>(sym)) {
|
|
|
|
StringRef symbolName = defined->getName();
|
|
|
|
if (config->exportedSymbols.match(symbolName)) {
|
|
|
|
if (defined->privateExtern) {
|
|
|
|
error("cannot export hidden symbol " + symbolName +
|
|
|
|
"\n>>> defined in " + toString(defined->getFile()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defined->privateExtern = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!config->unexportedSymbols.empty()) {
|
|
|
|
for (Symbol *sym : symtab->getSymbols())
|
|
|
|
if (auto *defined = dyn_cast<Defined>(sym))
|
|
|
|
if (config->unexportedSymbols.match(defined->getName()))
|
|
|
|
defined->privateExtern = true;
|
|
|
|
}
|
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
for (const Arg *arg : args.filtered(OPT_sectcreate)) {
|
|
|
|
StringRef segName = arg->getValue(0);
|
|
|
|
StringRef sectName = arg->getValue(1);
|
|
|
|
StringRef fileName = arg->getValue(2);
|
|
|
|
Optional<MemoryBufferRef> buffer = readFile(fileName);
|
|
|
|
if (buffer)
|
|
|
|
inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
|
|
|
|
}
|
2020-08-11 09:47:13 +08:00
|
|
|
|
2021-03-26 02:39:44 +08:00
|
|
|
{
|
|
|
|
TimeTraceScope timeScope("Gathering input sections");
|
|
|
|
// Gather all InputSections into one vector.
|
|
|
|
for (const InputFile *file : inputFiles) {
|
2021-06-12 07:49:52 +08:00
|
|
|
for (const SubsectionMap &map : file->subsections) {
|
|
|
|
for (const SubsectionEntry &entry : map) {
|
|
|
|
if (auto concatIsec = dyn_cast<ConcatInputSection>(entry.isec))
|
|
|
|
if (concatIsec->isCoalescedWeak())
|
|
|
|
continue;
|
|
|
|
inputSections.push_back(entry.isec);
|
|
|
|
}
|
|
|
|
}
|
[lld-macho][re-land] Support .subsections_via_symbols
Summary:
This diff restores and builds upon @pcc and @ruiu's initial work on
subsections.
The .subsections_via_symbols directive indicates we can split each
section along symbol boundaries, unless those symbols have been marked
with `.alt_entry`.
We exercise this functionality in our tests by using order files that
rearrange those symbols.
Depends on D79668.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Reviewed By: smeenai
Subscribers: thakis, llvm-commits, pcc, ruiu
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D79926
2020-05-19 23:46:07 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 22:04:27 +08:00
|
|
|
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
if (config->deadStrip)
|
|
|
|
markLive();
|
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
// Write to an output file.
|
2021-04-03 06:46:18 +08:00
|
|
|
if (target->wordSize == 8)
|
|
|
|
writeResult<LP64>();
|
|
|
|
else
|
|
|
|
writeResult<ILP32>();
|
2021-03-23 10:05:46 +08:00
|
|
|
|
|
|
|
depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
|
[lld-macho][re-land] Support .subsections_via_symbols
Summary:
This diff restores and builds upon @pcc and @ruiu's initial work on
subsections.
The .subsections_via_symbols directive indicates we can split each
section along symbol boundaries, unless those symbols have been marked
with `.alt_entry`.
We exercise this functionality in our tests by using order files that
rearrange those symbols.
Depends on D79668.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Reviewed By: smeenai
Subscribers: thakis, llvm-commits, pcc, ruiu
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D79926
2020-05-19 23:46:07 +08:00
|
|
|
}
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-03-11 22:04:27 +08:00
|
|
|
if (config->timeTraceEnabled) {
|
|
|
|
if (auto E = timeTraceProfilerWrite(
|
|
|
|
args.getLastArgValue(OPT_time_trace_file_eq).str(),
|
|
|
|
config->outputFile)) {
|
|
|
|
handleAllErrors(std::move(E),
|
|
|
|
[&](const StringError &SE) { error(SE.getMessage()); });
|
|
|
|
}
|
|
|
|
|
|
|
|
timeTraceProfilerCleanup();
|
|
|
|
}
|
2020-04-03 02:54:05 +08:00
|
|
|
|
|
|
|
if (canExitEarly)
|
|
|
|
exitLld(errorCount() ? 1 : 0);
|
|
|
|
|
|
|
|
return !errorCount();
|
|
|
|
}
|