Skip to content

Commit 70ab9ec

Browse files
committed
Simplify code
1 parent 3a03477 commit 70ab9ec

File tree

3 files changed

+42
-45
lines changed

3 files changed

+42
-45
lines changed

lld/ELF/Config.h

+1-13
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ enum LtoKind : uint8_t {UnifiedThin, UnifiedRegular, Default};
137137
enum class GcsPolicy { Implicit, Never, Always };
138138

139139
// For -z gcs-report= and -zgcs-report-dynamic
140-
enum class GcsReportPolicy { None, Warning, Error, Unknown };
140+
enum class GcsReportPolicy { None, Warning, Error };
141141

142142
struct SymbolVersion {
143143
llvm::StringRef name;
@@ -750,18 +750,6 @@ uint64_t errCount(Ctx &ctx);
750750

751751
ELFSyncStream InternalErr(Ctx &ctx, const uint8_t *buf);
752752

753-
inline StringRef gcsReportPolicytoString(GcsReportPolicy value) {
754-
StringRef ret;
755-
if (value == GcsReportPolicy::Warning)
756-
ret = "warning";
757-
else if (value == GcsReportPolicy::Error)
758-
ret = "error";
759-
else
760-
ret = "none";
761-
762-
return ret;
763-
}
764-
765753
#define CHECK2(E, S) lld::check2((E), [&] { return toStr(ctx, S); })
766754

767755
} // namespace lld::elf

lld/ELF/Driver.cpp

+32-23
Original file line numberDiff line numberDiff line change
@@ -578,31 +578,32 @@ static GcsPolicy getZGcs(Ctx &ctx, opt::InputArgList &args) {
578578

579579
static void getZGcsReport(Ctx &ctx, opt::InputArgList &args) {
580580
bool reportDynamicDefined = false;
581-
582581
for (auto *arg : args.filtered(OPT_z)) {
583582
std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
584-
if ((kv.first == "gcs-report") || kv.first == "gcs-report-dynamic") {
585-
arg->claim();
586-
GcsReportPolicy value = StringSwitch<GcsReportPolicy>(kv.second)
587-
.Case("none", GcsReportPolicy::None)
588-
.Case("warning", GcsReportPolicy::Warning)
589-
.Case("error", GcsReportPolicy::Error)
590-
.Default(GcsReportPolicy::Unknown);
591-
if (value == GcsReportPolicy::Unknown)
592-
ErrAlways(ctx) << "unknown -z " << kv.first << "= value: " << kv.second;
593-
594-
if (kv.first == "gcs-report")
595-
ctx.arg.zGcsReport = value;
596-
else if (kv.first == "gcs-report-dynamic") {
597-
ctx.arg.zGcsReportDynamic = value;
598-
reportDynamicDefined = true;
599-
}
583+
if (kv.first != "gcs-report" && kv.first != "gcs-report-dynamic")
584+
continue;
585+
arg->claim();
586+
GcsReportPolicy value;
587+
if (kv.second == "none")
588+
value = GcsReportPolicy::None;
589+
else if (kv.second == "warning")
590+
value = GcsReportPolicy::Warning;
591+
else if (kv.second == "error")
592+
value = GcsReportPolicy::Error;
593+
else {
594+
ErrAlways(ctx) << "unknown -z " << kv.first << "= value: " << kv.second;
595+
continue;
596+
}
597+
if (kv.first == "gcs-report") {
598+
ctx.arg.zGcsReport = value;
599+
} else if (kv.first == "gcs-report-dynamic") {
600+
ctx.arg.zGcsReportDynamic = value;
601+
reportDynamicDefined = true;
600602
}
601603
}
602604

603-
// The behaviour of -zgnu-report-dynamic matches that of GNU ld. When
604-
// -zgcs-report is set to `warning` or `error`, -zgcs-report-dynamic will
605-
// inherit this value if there is no user set value. This detects shared
605+
// When -zgcs-report is set to `warning` or `error`, -zgcs-report-dynamic will
606+
// inherit this value if unspecified, matching GNU ld. This detects shared
606607
// libraries without the GCS property but does not the shared-libraries to be
607608
// rebuilt for successful linking
608609
if (!reportDynamicDefined && ctx.arg.zGcsReport != GcsReportPolicy::None &&
@@ -1589,9 +1590,6 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
15891590
ctx.arg.zForceBti = hasZOption(args, "force-bti");
15901591
ctx.arg.zForceIbt = hasZOption(args, "force-ibt");
15911592
ctx.arg.zGcs = getZGcs(ctx, args);
1592-
// getZGcsReport assings the values for `ctx.arg.zGcsReport` and
1593-
// `ctx.arg.zGcsReportDynamic within the function. By doing this, it saves
1594-
// calling the function twice, as both values can be parsed at once.
15951593
getZGcsReport(ctx, args);
15961594
ctx.arg.zGlobal = hasZOption(args, "global");
15971595
ctx.arg.zGnustack = getZGnuStack(args);
@@ -2827,6 +2825,17 @@ static void redirectSymbols(Ctx &ctx, ArrayRef<WrappedSymbol> wrapped) {
28272825
ctx.symtab->wrap(w.sym, w.real, w.wrap);
28282826
}
28292827

2828+
static StringRef gcsReportPolicytoString(GcsReportPolicy value) {
2829+
StringRef ret;
2830+
if (value == GcsReportPolicy::Warning)
2831+
ret = "warning";
2832+
else if (value == GcsReportPolicy::Error)
2833+
ret = "error";
2834+
else
2835+
ret = "none";
2836+
return ret;
2837+
}
2838+
28302839
// To enable CET (x86's hardware-assisted control flow enforcement), each
28312840
// source file must be compiled with -fcf-protection. Object files compiled
28322841
// with the flag contain feature flags indicating that they are compatible

lld/ELF/InputFiles.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -919,13 +919,13 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
919919
}
920920

921921
template <typename ELFT>
922-
static void parseGnuPropertyNote(Ctx &ctx, uint32_t &featureAndType,
923-
ArrayRef<uint8_t> &desc, ELFFileBase *f,
924-
const uint8_t *base,
922+
static void parseGnuPropertyNote(Ctx &ctx, ELFFileBase &f,
923+
uint32_t featureAndType,
924+
ArrayRef<uint8_t> &desc, const uint8_t *base,
925925
ArrayRef<uint8_t> *data = nullptr) {
926926
auto err = [&](const uint8_t *place) -> ELFSyncStream {
927927
auto diag = Err(ctx);
928-
diag << f->getName() << ":(" << ".note.gnu.property+0x"
928+
diag << &f << ":(" << ".note.gnu.property+0x"
929929
<< Twine::utohexstr(place - base) << "): ";
930930
return diag;
931931
};
@@ -946,15 +946,15 @@ static void parseGnuPropertyNote(Ctx &ctx, uint32_t &featureAndType,
946946
// accumulate the bits set.
947947
if (size < 4)
948948
return void(err(place) << "FEATURE_1_AND entry is too short");
949-
f->andFeatures |= read32<ELFT::Endianness>(desc.data());
949+
f.andFeatures |= read32<ELFT::Endianness>(desc.data());
950950
} else if (ctx.arg.emachine == EM_AARCH64 &&
951951
type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
952952
// If the file being parsed is a SharedFile, we cannot pass in
953953
// the data variable as there is no InputSection to collect the
954954
// data from. As such, these are ignored. They are needed either
955955
// when loading a shared library oject.
956956
ArrayRef<uint8_t> contents = data ? *data : desc;
957-
if (!f->aarch64PauthAbiCoreInfo.empty()) {
957+
if (!f.aarch64PauthAbiCoreInfo.empty()) {
958958
return void(
959959
err(contents.data())
960960
<< "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
@@ -965,7 +965,7 @@ static void parseGnuPropertyNote(Ctx &ctx, uint32_t &featureAndType,
965965
"is invalid: expected 16 bytes, but got "
966966
<< size);
967967
}
968-
f->aarch64PauthAbiCoreInfo = desc;
968+
f.aarch64PauthAbiCoreInfo = desc;
969969
}
970970

971971
// Padding is present in the note descriptor, if necessary.
@@ -1010,7 +1010,7 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
10101010
// Read a body of a NOTE record, which consists of type-length-value fields.
10111011
ArrayRef<uint8_t> desc = note.getDesc(sec.addralign);
10121012
const uint8_t *base = sec.content().data();
1013-
parseGnuPropertyNote<ELFT>(ctx, featureAndType, desc, &f, base, &data);
1013+
parseGnuPropertyNote<ELFT>(ctx, f, featureAndType, desc, base, &data);
10141014

10151015
// Go to next NOTE record to look for more FEATURE_1_AND descriptions.
10161016
data = data.slice(nhdr->getSize(sec.addralign));
@@ -1465,7 +1465,7 @@ void SharedFile::parseGnuAndFeatures(const uint8_t *base,
14651465

14661466
// Read a body of a NOTE record, which consists of type-length-value fields.
14671467
ArrayRef<uint8_t> desc = note.getDesc(headers[i].p_align);
1468-
parseGnuPropertyNote<ELFT>(ctx, featureAndType, desc, this, base);
1468+
parseGnuPropertyNote<ELFT>(ctx, *this, featureAndType, desc, base);
14691469
}
14701470
}
14711471

0 commit comments

Comments
 (0)