[mac/lld] Implement -t

Goes well with `-why_load` to get an idea of load order.

Differential Revision: https://reviews.llvm.org/D92583
This commit is contained in:
Nico Weber
2020-12-02 18:57:30 -05:00
parent 4fff2a7e89
commit 7cb0a373d1
7 changed files with 70 additions and 13 deletions

View File

@@ -38,6 +38,7 @@ struct Configuration {
bool staticLink = false;
bool isPic = false;
bool headerPadMaxInstallNames = false;
bool printEachFile = false;
bool printWhyLoad = false;
bool searchDylibsFirst = false;
bool saveTemps = false;

View File

@@ -262,7 +262,8 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive) {
MemoryBufferRef mbref = *buffer;
InputFile *newFile = nullptr;
switch (identify_magic(mbref.getBuffer())) {
auto magic = identify_magic(mbref.getBuffer());
switch (magic) {
case file_magic::archive: {
std::unique_ptr<object::Archive> file = CHECK(
object::Archive::create(mbref), path + ": failed to parse archive");
@@ -275,8 +276,9 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive) {
for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
inputFiles.push_back(
make<ObjFile>(member.mbref, member.modTime, path));
printWhyLoad((forceLoadArchive ? "-force_load" : "-all_load"),
inputFiles.back());
printArchiveMemberLoad(
(forceLoadArchive ? "-force_load" : "-all_load"),
inputFiles.back());
}
}
} else if (config->forceLoadObjC) {
@@ -293,7 +295,7 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive) {
if (hasObjCSection(member.mbref)) {
inputFiles.push_back(
make<ObjFile>(member.mbref, member.modTime, path));
printWhyLoad("-ObjC", inputFiles.back());
printArchiveMemberLoad("-ObjC", inputFiles.back());
}
}
}
@@ -320,8 +322,13 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive) {
default:
error(path + ": unhandled file type");
}
if (newFile)
if (newFile) {
// printArchiveMemberLoad() prints both .a and .o names, so no need to
// print the .a name here.
if (config->printEachFile && magic != file_magic::archive)
lld::outs() << toString(newFile) << '\n';
inputFiles.push_back(newFile);
}
return newFile;
}
@@ -640,6 +647,7 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
config->headerPadMaxInstallNames =
args.hasArg(OPT_headerpad_max_install_names);
config->printEachFile = args.hasArg(OPT_t);
config->printWhyLoad = args.hasArg(OPT_why_load);
config->outputType = getOutputType(args);
config->runtimePaths = args::getStrings(args, OPT_rpath);

View File

@@ -46,7 +46,7 @@ llvm::Optional<DylibFile *> makeDylibFromTAPI(llvm::MemoryBufferRef mbref,
uint32_t getModTime(llvm::StringRef path);
void printWhyLoad(StringRef reason, const InputFile *);
void printArchiveMemberLoad(StringRef reason, const InputFile *);
} // namespace macho
} // namespace lld

View File

@@ -187,9 +187,9 @@ uint32_t macho::getModTime(StringRef path) {
return 0;
}
void macho::printWhyLoad(StringRef reason, const InputFile *f) {
if (!config->printWhyLoad)
return;
lld::outs() << reason << " forced load of " << toString(f)
<< '\n';
void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
if (config->printEachFile)
lld::outs() << toString(f) << '\n';
if (config->printWhyLoad)
lld::outs() << reason << " forced load of " << toString(f) << '\n';
}

View File

@@ -628,7 +628,7 @@ void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
// ld64 doesn't demangle sym here even with -demangle. Match that, so
// intentionally no call to toMachOString() here.
printWhyLoad(sym_copy.getName(), file);
printArchiveMemberLoad(sym_copy.getName(), file);
symbols.insert(symbols.end(), file->symbols.begin(), file->symbols.end());
subsections.insert(subsections.end(), file->subsections.begin(),

View File

@@ -444,7 +444,6 @@ def print_statistics : Flag<["-"], "print_statistics">,
Group<grp_introspect>;
def t : Flag<["-"], "t">,
HelpText<"Log every file the linker loads: object, archive, and dylib">,
Flags<[HelpHidden]>,
Group<grp_introspect>;
def whatsloaded : Flag<["-"], "whatsloaded">,
HelpText<"Logs only the object files the linker loads">,

49
lld/test/MachO/t.s Normal file
View File

@@ -0,0 +1,49 @@
# REQUIRES: x86
# RUN: rm -rf %t
# RUN: split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/foo.o %t/foo.s
# RUN: %lld -dylib -o %t/libfoo.dylib %t/foo.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/bar.o %t/bar.s
# RUN: llvm-ar csr %t/bar.a %t/bar.o
# RUN: llvm-as %t/baz.ll -o %t/baz.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/main.o %t/main.s
# RUN: %lld %t/main.o %t/baz.o %t/bar.a %t/libfoo.dylib -lSystem -o /dev/null -t | FileCheck -DPATH='%:t' %s
# CHECK-DAG: bar.a(bar.o)
# CHECK-DAG: [[PATH]]/main.o
# CHECK-DAG: [[PATH]]/baz.o
# CHECK-DAG: [[PATH]]/libfoo.dylib
# CHECK-DAG: {{.*}}/usr/lib/libSystem.tbd
#--- foo.s
.globl __Z3foo
__Z3foo:
ret
#--- bar.s
.globl _bar
_bar:
callq __Z3foo
ret
#--- baz.ll
target triple = "x86_64-apple-macosx10.15.0"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
define void @baz() {
ret void
}
#--- main.s
.globl _main
_main:
callq _bar
callq __Z3foo
callq _baz
ret