aboutsummaryrefslogtreecommitdiff
path: root/lld/MachO
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-12-02 13:17:55 -0500
committerNico Weber <thakis@chromium.org>2020-12-02 18:33:12 -0500
commit542d3b609dbe99a30759942271398890fc7770dc (patch)
treea5da88f871d5890307514ffea5bd33fa73088210 /lld/MachO
parent86f59de13b8058280d468984727bd1e6727f2112 (diff)
downloadllvm-project-542d3b609dbe99a30759942271398890fc7770dc.tar.gz
[mac/lld] Implement -why_load
This is useful for debugging why lld loads .o files it shouldn't load. It's also useful for users of lld -- I've used ld64's version of this a few times. Differential Revision: https://reviews.llvm.org/D92496
Diffstat (limited to 'lld/MachO')
-rw-r--r--lld/MachO/Config.h1
-rw-r--r--lld/MachO/Driver.cpp4
-rw-r--r--lld/MachO/Driver.h3
-rw-r--r--lld/MachO/DriverUtils.cpp8
-rw-r--r--lld/MachO/InputFiles.cpp4
-rw-r--r--lld/MachO/Options.td3
6 files changed, 21 insertions, 2 deletions
diff --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index 82c017063d44..bc22680db58f 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -38,6 +38,7 @@ struct Configuration {
bool staticLink = false;
bool isPic = false;
bool headerPadMaxInstallNames = false;
+ bool printWhyLoad = false;
bool searchDylibsFirst = false;
bool saveTemps = false;
uint32_t headerPad;
diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 426101f4f9e1..9822ecdd9f2a 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -275,6 +275,8 @@ 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());
}
}
} else if (config->forceLoadObjC) {
@@ -291,6 +293,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());
}
}
}
@@ -637,6 +640,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->printWhyLoad = args.hasArg(OPT_why_load);
config->outputType = getOutputType(args);
config->runtimePaths = args::getStrings(args, OPT_rpath);
config->allLoad = args.hasArg(OPT_all_load);
diff --git a/lld/MachO/Driver.h b/lld/MachO/Driver.h
index d5625fd3873e..db7c54c59966 100644
--- a/lld/MachO/Driver.h
+++ b/lld/MachO/Driver.h
@@ -19,6 +19,7 @@ namespace lld {
namespace macho {
class DylibFile;
+class InputFile;
class MachOOptTable : public llvm::opt::OptTable {
public:
@@ -45,6 +46,8 @@ llvm::Optional<DylibFile *> makeDylibFromTAPI(llvm::MemoryBufferRef mbref,
uint32_t getModTime(llvm::StringRef path);
+void printWhyLoad(StringRef reason, const InputFile *);
+
} // namespace macho
} // namespace lld
diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp
index 77f76b522d7e..9c09ff682286 100644
--- a/lld/MachO/DriverUtils.cpp
+++ b/lld/MachO/DriverUtils.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "Driver.h"
+#include "Config.h"
#include "InputFiles.h"
#include "lld/Common/Args.h"
@@ -185,3 +186,10 @@ uint32_t macho::getModTime(StringRef path) {
warn("failed to get modification time of " + path);
return 0;
}
+
+void macho::printWhyLoad(StringRef reason, const InputFile *f) {
+ if (!config->printWhyLoad)
+ return;
+ lld::outs() << reason << " forced load of " << toString(f)
+ << '\n';
+}
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 4fd9873bcbd3..a24d045096c6 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -621,6 +621,10 @@ void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
auto file = make<ObjFile>(mb, modTime, getName());
+ // ld64 doesn't demangle sym here even with -demangle. Match that, so
+ // intentionally no call to toMachOString() here.
+ printWhyLoad(sym.getName(), file);
+
symbols.insert(symbols.end(), file->symbols.begin(), file->symbols.end());
subsections.insert(subsections.end(), file->subsections.begin(),
file->subsections.end());
diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index 399928e8d9ae..5b38fc650e9f 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -426,8 +426,7 @@ def commons : Separate<["-"], "commons">,
def grp_introspect : OptionGroup<"introspect">, HelpText<"INTROSPECTING THE LINKER">;
def why_load : Flag<["-"], "why_load">,
- HelpText<"Log the symbol that compels loading of each object file from a static library">,
- Flags<[HelpHidden]>,
+ HelpText<"Log why each object file is loaded from a static library">,
Group<grp_introspect>;
def whyload : Flag<["-"], "whyload">,
Alias<why_load>,