Skip to content

Commit 665f913

Browse files
committed
[ELF] Reimplement unknown -z options using the isClaimed bit
Maintaining the long list of known -z options (https://reviews.llvm.org/D48621) turns out to be cumbersome. Go the D48433 route instead. max-page-size/common-page-size are claimed when `target` is available. Inspired by: https://reviews.llvm.org/D48433
1 parent 2e30e31 commit 665f913

File tree

3 files changed

+8
-59
lines changed

3 files changed

+8
-59
lines changed

lld/ELF/Driver.cpp

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -553,65 +553,14 @@ static uint8_t getZStartStopVisibility(opt::InputArgList &args) {
553553
return ret;
554554
}
555555

556-
constexpr const char *knownZFlags[] = {
557-
"combreloc",
558-
"copyreloc",
559-
"defs",
560-
"execstack",
561-
"force-bti",
562-
"force-ibt",
563-
"global",
564-
"hazardplt",
565-
"ifunc-noplt",
566-
"initfirst",
567-
"interpose",
568-
"keep-text-section-prefix",
569-
"lazy",
570-
"muldefs",
571-
"nocombreloc",
572-
"nocopyreloc",
573-
"nodefaultlib",
574-
"nodelete",
575-
"nodlopen",
576-
"noexecstack",
577-
"nognustack",
578-
"nokeep-text-section-prefix",
579-
"nopack-relative-relocs",
580-
"norelro",
581-
"noseparate-code",
582-
"nostart-stop-gc",
583-
"notext",
584-
"now",
585-
"origin",
586-
"pac-plt",
587-
"pack-relative-relocs",
588-
"rel",
589-
"rela",
590-
"relro",
591-
"retpolineplt",
592-
"rodynamic",
593-
"separate-code",
594-
"separate-loadable-segments",
595-
"shstk",
596-
"start-stop-gc",
597-
"text",
598-
"undefs",
599-
"wxneeded",
600-
};
601-
602-
static bool isKnownZFlag(StringRef s) {
603-
return llvm::is_contained(knownZFlags, s) ||
604-
s.starts_with("common-page-size=") || s.starts_with("bti-report=") ||
605-
s.starts_with("cet-report=") ||
606-
s.starts_with("dead-reloc-in-nonalloc=") ||
607-
s.starts_with("max-page-size=") || s.starts_with("stack-size=") ||
608-
s.starts_with("start-stop-visibility=");
609-
}
610-
611556
// Report a warning for an unknown -z option.
612557
static void checkZOptions(opt::InputArgList &args) {
558+
// This function is called before getTarget(), when certain options are not
559+
// initialized yet. Claim them here.
560+
args::getZOptionValue(args, OPT_z, "max-page-size", 0);
561+
args::getZOptionValue(args, OPT_z, "common-page-size", 0);
613562
for (auto *arg : args.filtered(OPT_z))
614-
if (!isKnownZFlag(arg->getValue()))
563+
if (!arg->isClaimed())
615564
warn("unknown -z value: " + StringRef(arg->getValue()));
616565
}
617566

@@ -629,7 +578,6 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
629578
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) &&
630579
!args.hasArg(OPT_no_warnings);
631580
errorHandler().suppressWarnings = args.hasArg(OPT_no_warnings);
632-
checkZOptions(args);
633581

634582
// Handle -help
635583
if (args.hasArg(OPT_help)) {
@@ -672,6 +620,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
672620
}
673621

674622
readConfigs(args);
623+
checkZOptions(args);
675624

676625
// The behavior of -v or --version is a bit strange, but this is
677626
// needed for compatibility with GNU linkers.

lld/test/ELF/common-page.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _start:
1111
# of 4k. If the last loadable segment is executable then lld aligns the next
1212
# section using the common page size.
1313

14-
# RUN: ld.lld -z max-page-size=0x10000 -z common-page-size=0x1000 %t -o %t2
14+
# RUN: ld.lld -z max-page-size=0x10000 -z common-page-size=0x1000 %t -o %t2 2>&1 | count 0
1515
# RUN: llvm-readobj --sections -l %t2 | FileCheck --check-prefix=CHECK-MAX %s
1616

1717
# CHECK-MAX: Sections [

lld/test/ELF/driver.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
# ERR10-FATAL: error: unknown -z value: foo
6666

6767
# RUN: not ld.lld %t -z max-page-size 2>&1 | FileCheck -check-prefix=ERR11 %s
68-
# ERR11: unknown -z value: max-page-size
68+
# ERR11: error: invalid max-page-size:
6969

7070
## Attempt to use -r and --export-dynamic together
7171
# RUN: not ld.lld -r -export-dynamic %t -o /dev/null 2>&1 | FileCheck -check-prefix=ERR12 %s

0 commit comments

Comments
 (0)