From aaf3a8ded47121c8ec8136f97a7a2c39112b3e59 Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Tue, 5 Dec 2023 14:57:45 -0500 Subject: [LLD][COFF] Add -build-id flag to generate .buildid section. (#71433) [RFC](https://discourse.llvm.org/t/rfc-add-build-id-flag-to-lld-link/74661) Before, lld-link only generate the debug directory containing guid when generating PDB with the hash of PDB content. With this change, lld-link can generate the debug directory when only `/build-id` is given: 1. If generating PDB, `/build-id` is ignored. Same behaviour as before. 2. Not generating PDB, using hash of the binary. - Not under MinGW, the debug directory is still in `.rdata` section. - Under MinGW, place the debug directory into new `.buildid` section. --- lld/MinGW/Driver.cpp | 15 +++++++++++++++ lld/MinGW/Options.td | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'lld/MinGW') diff --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp index 19bf2d1..d22b617 100644 --- a/lld/MinGW/Driver.cpp +++ b/lld/MinGW/Driver.cpp @@ -302,6 +302,21 @@ bool link(ArrayRef argsArr, llvm::raw_ostream &stdoutOS, } else if (!args.hasArg(OPT_strip_all)) { add("-debug:dwarf"); } + if (auto *a = args.getLastArg(OPT_build_id)) { + StringRef v = a->getValue(); + if (v == "none") + add("-build-id:no"); + else { + if (!v.empty()) + warn("unsupported build id hashing: " + v + ", using default hashing."); + add("-build-id"); + } + } else { + if (args.hasArg(OPT_strip_debug) || args.hasArg(OPT_strip_all)) + add("-build-id:no"); + else + add("-build-id"); + } if (args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false)) add("-WX"); diff --git a/lld/MinGW/Options.td b/lld/MinGW/Options.td index fa4c4ec..d4a49cd 100644 --- a/lld/MinGW/Options.td +++ b/lld/MinGW/Options.td @@ -196,6 +196,9 @@ defm guard_longjmp : B<"guard-longjmp", "Do not enable Control Flow Guard long jump hardening">; defm error_limit: EqLong<"error-limit", "Maximum number of errors to emit before stopping (0 = no limit)">; +def build_id: J<"build-id=">, HelpText<"Generate build ID note (pass none to disable)">, + MetaVarName<"">; +def : F<"build-id">, Alias, HelpText<"Alias for --build-id=">; // Alias def alias_Bdynamic_call_shared: Flag<["-"], "call_shared">, Alias; @@ -213,7 +216,6 @@ def alias_undefined_u: JoinedOrSeparate<["-"], "u">, Alias; // Ignored options def: Joined<["-"], "O">; def: F<"as-needed">; -def: F<"build-id">; def: F<"disable-auto-image-base">; def: F<"enable-auto-image-base">; def: F<"end-group">; -- cgit v1.1