Skip to content

Commit 90c11ad

Browse files
authored
[ELF] Introduce ReportPolicy to handle -z *-report options. NFC
Use an enum to replace string comparison. Pull Request: #130715
1 parent 3464766 commit 90c11ad

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

lld/ELF/Config.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ enum LtoKind : uint8_t {UnifiedThin, UnifiedRegular, Default};
136136
// For -z gcs=
137137
enum class GcsPolicy { Implicit, Never, Always };
138138

139+
// For some options that resemble -z bti-report={none,warning,error}
140+
enum class ReportPolicy { None, Warning, Error };
141+
139142
struct SymbolVersion {
140143
llvm::StringRef name;
141144
bool isExternCpp;
@@ -225,11 +228,11 @@ struct Config {
225228
llvm::StringRef whyExtract;
226229
llvm::StringRef cmseInputLib;
227230
llvm::StringRef cmseOutputLib;
228-
StringRef zBtiReport = "none";
229-
StringRef zCetReport = "none";
230-
StringRef zPauthReport = "none";
231-
StringRef zGcsReport = "none";
232-
StringRef zExecuteOnlyReport = "none";
231+
ReportPolicy zBtiReport = ReportPolicy::None;
232+
ReportPolicy zCetReport = ReportPolicy::None;
233+
ReportPolicy zPauthReport = ReportPolicy::None;
234+
ReportPolicy zGcsReport = ReportPolicy::None;
235+
ReportPolicy zExecuteOnlyReport = ReportPolicy::None;
233236
bool ltoBBAddrMap;
234237
llvm::StringRef ltoBasicBlockSections;
235238
std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
@@ -748,6 +751,14 @@ ELFSyncStream InternalErr(Ctx &ctx, const uint8_t *buf);
748751

749752
#define CHECK2(E, S) lld::check2((E), [&] { return toStr(ctx, S); })
750753

754+
inline DiagLevel toDiagLevel(ReportPolicy policy) {
755+
if (policy == ReportPolicy::Error)
756+
return DiagLevel::Err;
757+
else if (policy == ReportPolicy::Warning)
758+
return DiagLevel::Warn;
759+
return DiagLevel::None;
760+
}
761+
751762
} // namespace lld::elf
752763

753764
#endif

lld/ELF/Driver.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -396,18 +396,18 @@ static void checkOptions(Ctx &ctx) {
396396
ErrAlways(ctx) << "-z pac-plt only supported on AArch64";
397397
if (ctx.arg.zForceBti)
398398
ErrAlways(ctx) << "-z force-bti only supported on AArch64";
399-
if (ctx.arg.zBtiReport != "none")
399+
if (ctx.arg.zBtiReport != ReportPolicy::None)
400400
ErrAlways(ctx) << "-z bti-report only supported on AArch64";
401-
if (ctx.arg.zPauthReport != "none")
401+
if (ctx.arg.zPauthReport != ReportPolicy::None)
402402
ErrAlways(ctx) << "-z pauth-report only supported on AArch64";
403-
if (ctx.arg.zGcsReport != "none")
403+
if (ctx.arg.zGcsReport != ReportPolicy::None)
404404
ErrAlways(ctx) << "-z gcs-report only supported on AArch64";
405405
if (ctx.arg.zGcs != GcsPolicy::Implicit)
406406
ErrAlways(ctx) << "-z gcs only supported on AArch64";
407407
}
408408

409409
if (ctx.arg.emachine != EM_AARCH64 && ctx.arg.emachine != EM_ARM &&
410-
ctx.arg.zExecuteOnlyReport != "none")
410+
ctx.arg.zExecuteOnlyReport != ReportPolicy::None)
411411
ErrAlways(ctx)
412412
<< "-z execute-only-report only supported on AArch64 and ARM";
413413

@@ -423,7 +423,7 @@ static void checkOptions(Ctx &ctx) {
423423
ErrAlways(ctx) << "--relax-gp is only supported on RISC-V targets";
424424

425425
if (ctx.arg.emachine != EM_386 && ctx.arg.emachine != EM_X86_64 &&
426-
ctx.arg.zCetReport != "none")
426+
ctx.arg.zCetReport != ReportPolicy::None)
427427
ErrAlways(ctx) << "-z cet-report only supported on X86 and X86_64";
428428

429429
if (ctx.arg.pie && ctx.arg.shared)
@@ -1272,11 +1272,6 @@ static void parseClangOption(Ctx &ctx, StringRef opt, const Twine &msg) {
12721272
ErrAlways(ctx) << msg << ": " << StringRef(err).trim();
12731273
}
12741274

1275-
// Checks the parameter of the bti-report and cet-report options.
1276-
static bool isValidReportString(StringRef arg) {
1277-
return arg == "none" || arg == "warning" || arg == "error";
1278-
}
1279-
12801275
// Process a remap pattern 'from-glob=to-file'.
12811276
static bool remapInputs(Ctx &ctx, StringRef line, const Twine &location) {
12821277
SmallVector<StringRef, 0> fields;
@@ -1638,12 +1633,17 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
16381633
if (option.first != reportArg.first)
16391634
continue;
16401635
arg->claim();
1641-
if (!isValidReportString(option.second)) {
1636+
if (option.second == "none")
1637+
*reportArg.second = ReportPolicy::None;
1638+
else if (option.second == "warning")
1639+
*reportArg.second = ReportPolicy::Warning;
1640+
else if (option.second == "error")
1641+
*reportArg.second = ReportPolicy::Error;
1642+
else {
16421643
ErrAlways(ctx) << "unknown -z " << reportArg.first
16431644
<< "= value: " << option.second;
16441645
continue;
16451646
}
1646-
*reportArg.second = option.second;
16471647
}
16481648
}
16491649

@@ -2820,17 +2820,13 @@ static void readSecurityNotes(Ctx &ctx) {
28202820
bool hasValidPauthAbiCoreInfo = llvm::any_of(
28212821
ctx.aarch64PauthAbiCoreInfo, [](uint8_t c) { return c != 0; });
28222822

2823-
auto report = [&](StringRef config) -> ELFSyncStream {
2824-
if (config == "error")
2825-
return {ctx, DiagLevel::Err};
2826-
else if (config == "warning")
2827-
return {ctx, DiagLevel::Warn};
2828-
return {ctx, DiagLevel::None};
2823+
auto report = [&](ReportPolicy policy) -> ELFSyncStream {
2824+
return {ctx, toDiagLevel(policy)};
28292825
};
2830-
auto reportUnless = [&](StringRef config, bool cond) -> ELFSyncStream {
2826+
auto reportUnless = [&](ReportPolicy policy, bool cond) -> ELFSyncStream {
28312827
if (cond)
28322828
return {ctx, DiagLevel::None};
2833-
return report(config);
2829+
return {ctx, toDiagLevel(policy)};
28342830
};
28352831
for (ELFFileBase *f : ctx.objectFiles) {
28362832
uint32_t features = f->andFeatures;
@@ -2860,13 +2856,13 @@ static void readSecurityNotes(Ctx &ctx) {
28602856

28612857
if (ctx.arg.zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
28622858
features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
2863-
if (ctx.arg.zBtiReport == "none")
2859+
if (ctx.arg.zBtiReport == ReportPolicy::None)
28642860
Warn(ctx) << f
28652861
<< ": -z force-bti: file does not have "
28662862
"GNU_PROPERTY_AARCH64_FEATURE_1_BTI property";
28672863
} else if (ctx.arg.zForceIbt &&
28682864
!(features & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
2869-
if (ctx.arg.zCetReport == "none")
2865+
if (ctx.arg.zCetReport == ReportPolicy::None)
28702866
Warn(ctx) << f
28712867
<< ": -z force-ibt: file does not have "
28722868
"GNU_PROPERTY_X86_FEATURE_1_IBT property";

lld/ELF/Writer.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,17 +2181,13 @@ template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {
21812181
// Check which input sections of RX output sections don't have the
21822182
// SHF_AARCH64_PURECODE or SHF_ARM_PURECODE flag set.
21832183
template <class ELFT> void Writer<ELFT>::checkExecuteOnlyReport() {
2184-
if (ctx.arg.zExecuteOnlyReport == "none")
2184+
if (ctx.arg.zExecuteOnlyReport == ReportPolicy::None)
21852185
return;
21862186

21872187
auto reportUnless = [&](bool cond) -> ELFSyncStream {
21882188
if (cond)
21892189
return {ctx, DiagLevel::None};
2190-
if (ctx.arg.zExecuteOnlyReport == "error")
2191-
return {ctx, DiagLevel::Err};
2192-
if (ctx.arg.zExecuteOnlyReport == "warning")
2193-
return {ctx, DiagLevel::Warn};
2194-
return {ctx, DiagLevel::None};
2190+
return {ctx, toDiagLevel(ctx.arg.zExecuteOnlyReport)};
21952191
};
21962192

21972193
uint64_t purecodeFlag =

0 commit comments

Comments
 (0)