Skip to content

Commit cd5d5ce

Browse files
committed
[ELF] Refactor the way we handle -plugin-opt= (GCC collect2 or clang LTO related options)
GCC collect2 passes several options to the linker even if LTO is not used (note, lld does not support GCC LTO). The lto-wrapper may be a relative path (especially during development, when gcc is in a build directory), e.g. -plugin-opt=relative/path/to/lto-wrapper We need to ignore such options, which are currently interpreted by cl::ParseCommandLineOptions() and will fail with `error: --plugin-opt: ld.lld: Unknown command line argument 'relative/path/to/lto-wrapper'` because the path is apparently not an option registered by an `llvm::cl::opt`. See lto-plugin-ignore.s for how we interpret various -plugin-opt= options now. Reviewed By: grimar, tejohnson Differential Revision: https://reviews.llvm.org/D78158
1 parent 7a6aaf9 commit cd5d5ce

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

lld/ELF/Driver.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,16 @@ static void readConfigs(opt::InputArgList &args) {
10351035
parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
10361036
arg->getSpelling());
10371037

1038-
for (auto *arg : args.filtered(OPT_plugin_opt))
1039-
parseClangOption(arg->getValue(), arg->getSpelling());
1038+
for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq_minus))
1039+
parseClangOption(std::string("-") + arg->getValue(), arg->getSpelling());
1040+
1041+
// GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or
1042+
// relative path. Just ignore. If not ended with "lto-wrapper", consider it an
1043+
// unsupported LLVMgold.so option and error.
1044+
for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq))
1045+
if (!StringRef(arg->getValue()).endswith("lto-wrapper"))
1046+
error(arg->getSpelling() + ": unknown plugin option '" + arg->getValue() +
1047+
"'");
10401048

10411049
// Parse -mllvm options.
10421050
for (auto *arg : args.filtered(OPT_mllvm))

lld/ELF/Options.td

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ def opt_remarks_with_hotness: Flag<["--"], "opt-remarks-with-hotness">,
504504
HelpText<"Include hotness information in the optimization remarks file">;
505505
def opt_remarks_format: Separate<["--"], "opt-remarks-format">,
506506
HelpText<"The format used for serializing remarks (default: YAML)">;
507-
defm plugin_opt: Eq<"plugin-opt", "specifies LTO options for compatibility with GNU linkers">;
508507
def save_temps: F<"save-temps">;
509508
def lto_basicblock_sections: J<"lto-basicblock-sections=">,
510509
HelpText<"Enable basic block sections for LTO">;
@@ -571,10 +570,17 @@ def: J<"plugin-opt=thinlto-prefix-replace=">,
571570
// --version output.
572571
defm plugin: Eq<"plugin", "Ignored for compatibility with GNU linkers">;
573572

574-
def plugin_opt_fresolution_eq: J<"plugin-opt=-fresolution=">;
575-
def plugin_opt_pass_through_eq: J<"plugin-opt=-pass-through=">;
576-
def plugin_opt_thinlto: J<"plugin-opt=thinlto">;
577-
def plugin_opt_slash: J<"plugin-opt=/">;
573+
def plugin_opt_eq_minus: J<"plugin-opt=-">,
574+
HelpText<"Specify an LLVM option for compatibility with LLVMgold.so">;
575+
def: J<"plugin-opt=thinlto">;
576+
577+
// Ignore GCC collect2 LTO plugin related options. Note that we don't support
578+
// GCC LTO, but GCC collect2 passes these options even in non-LTO mode.
579+
def: J<"plugin-opt=-fresolution=">;
580+
def: J<"plugin-opt=-pass-through=">;
581+
// This may be either an unhandled LLVMgold.so feature or GCC passed
582+
// -plugin-opt=path/to/{liblto_plugin.so,lto-wrapper}
583+
def plugin_opt_eq : J<"plugin-opt=">;
578584

579585
// Options listed below are silently ignored for now for compatibility.
580586
def: F<"detect-odr-violations">;

lld/test/ELF/lto-plugin-ignore.s

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
# REQUIRES: x86
2+
## Test we ignore some LTO related options from clang/GCC collect2.
23

3-
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
4-
# RUN: ld.lld %t -plugin-opt=/foo/bar -plugin-opt=-fresolution=zed \
5-
# RUN: -plugin-opt=-pass-through=-lgcc -plugin-opt=-function-sections \
6-
# RUN: -plugin-opt=-data-sections -plugin-opt=thinlto -o /dev/null
4+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
75

8-
# RUN: not ld.lld %t -plugin-opt=-abc -plugin-opt=-xyz 2>&1 | FileCheck %s
9-
# CHECK: ld.lld: error: --plugin-opt: ld.lld{{.*}}: Unknown command line argument '-abc'
10-
# CHECK: ld.lld: error: --plugin-opt: ld.lld{{.*}}: Unknown command line argument '-xyz'
6+
## GCC collect2 passes several LTO related options to the linker even if -flto is not used.
7+
## We need to ignore them. Note that the lto-wrapper path can be relative.
8+
# RUN: ld.lld %t.o -o /dev/null \
9+
# RUN: -plugin path/to/liblto_plugin.so \
10+
# RUN: -plugin-opt=/path/to/lto-wrapper \
11+
# RUN: -plugin-opt=relative/path/to/lto-wrapper \
12+
# RUN: -plugin-opt=-fresolution=zed \
13+
# RUN: -plugin-opt=-pass-through=-lgcc \
14+
# RUN: -plugin-opt=-pass-through=-lgcc_eh \
15+
# RUN: -plugin-opt=-pass-through=-lc
16+
17+
## Clang LTO passes several options to the linker, which are intended to be consumed by
18+
## LLVMgold.so. We need to ignore them.
19+
# RUN: ld.lld %t.o -o /dev/null -plugin /path/to/LLVMgold.so -plugin-opt=thinlto
20+
21+
## Other -plugin-opt=- prefixed options are passed through to cl::ParseCommandLineOptions.
22+
# RUN: not ld.lld %t.o -o /dev/null -plugin-opt=-abc -plugin-opt=-xyz 2>&1 | FileCheck %s
23+
# CHECK: ld.lld: error: -plugin-opt=-: ld.lld{{.*}}: Unknown command line argument '-abc'
24+
# CHECK: ld.lld: error: -plugin-opt=-: ld.lld{{.*}}: Unknown command line argument '-xyz'
25+
26+
## Error if the option is an unhandled LLVMgold.so feature.
27+
# RUN: not ld.lld %t.o -o /dev/null -plugin-opt=LLVMgold-feature 2>&1 | FileCheck --check-prefix=GOLD %s
28+
# GOLD: ld.lld: error: -plugin-opt=: unknown plugin option 'LLVMgold-feature'

0 commit comments

Comments
 (0)