Skip to content

Commit 4286f4d

Browse files
Stylie777MaskRay
andauthored
[AArch64][GCS][LLD] Introduce -zgcs-report-dynamic Command Line Option (#127787)
When GCS was introduced to LLD, the gcs-report option allowed for a user to gain information relating to if their relocatable objects supported the feature. For an executable or shared-library to support GCS, all relocatable objects must declare that they support GCS. The gcs-report checks were only done on relocatable object files, however for a program to enable GCS, the executable and all shared libraries that it loads must enable GCS. gcs-report-dynamic enables checks to be performed on all shared objects loaded by LLD, and in cases where GCS is not supported, a warning or error will be emitted. It should be noted that only shared files directly passed to LLD are checked for GCS support. Files that are noted in the `DT_NEEDED` tags are assumed to have had their GCS support checked when they were created. The behaviour of the -zgcs-dynamic-report option matches that of GNU ld. The behaviour is as follows unless the user explicitly sets the value: * -zgcs-report=warning or -zgcs-report=error implies -zgcs-report-dynamic=warning. This approach avoids inheriting an error level if the user wishes to continue building a module without rebuilding all the shared libraries. The same approach was taken for the GNU ld linker, so behaviour is identical across the toolchains. This implementation matches the error message and command line interface used within the GNU ld Linker. See here: bminor/binutils-gdb@724a834 To support this option being introduced, two other changes are included as part of this PR. The first converts the -zgcs-report option to utilise an Enum, opposed to StringRef values. This enables easier tracking of the value the user defines when inheriting the value for the gas-report-dynamic option. The second is to parse the Dynamic Objects program headers to locate the GNU Attribute flag that shows GCS is supported. This is needed so, when using the gcs-report-dynamic option, LLD can correctly determine if a dynamic object supports GCS. --------- Co-authored-by: Fangrui Song <[email protected]>
1 parent 7722d75 commit 4286f4d

File tree

6 files changed

+127
-38
lines changed

6 files changed

+127
-38
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ struct Config {
232232
ReportPolicy zCetReport = ReportPolicy::None;
233233
ReportPolicy zPauthReport = ReportPolicy::None;
234234
ReportPolicy zGcsReport = ReportPolicy::None;
235+
ReportPolicy zGcsReportDynamic = ReportPolicy::None;
235236
ReportPolicy zExecuteOnlyReport = ReportPolicy::None;
236237
bool ltoBBAddrMap;
237238
llvm::StringRef ltoBasicBlockSections;

lld/ELF/Driver.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656
#include "llvm/Object/IRObjectFile.h"
5757
#include "llvm/Remarks/HotnessThresholdParser.h"
5858
#include "llvm/Support/CommandLine.h"
59-
#include "llvm/Support/SaveAndRestore.h"
6059
#include "llvm/Support/Compression.h"
6160
#include "llvm/Support/FileSystem.h"
6261
#include "llvm/Support/GlobPattern.h"
6362
#include "llvm/Support/LEB128.h"
6463
#include "llvm/Support/Parallel.h"
6564
#include "llvm/Support/Path.h"
65+
#include "llvm/Support/SaveAndRestore.h"
6666
#include "llvm/Support/TarWriter.h"
6767
#include "llvm/Support/TargetSelect.h"
6868
#include "llvm/Support/TimeProfiler.h"
@@ -402,6 +402,8 @@ static void checkOptions(Ctx &ctx) {
402402
ErrAlways(ctx) << "-z pauth-report only supported on AArch64";
403403
if (ctx.arg.zGcsReport != ReportPolicy::None)
404404
ErrAlways(ctx) << "-z gcs-report only supported on AArch64";
405+
if (ctx.arg.zGcsReportDynamic != ReportPolicy::None)
406+
ErrAlways(ctx) << "-z gcs-report-dynamic only supported on AArch64";
405407
if (ctx.arg.zGcs != GcsPolicy::Implicit)
406408
ErrAlways(ctx) << "-z gcs only supported on AArch64";
407409
}
@@ -1625,7 +1627,9 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
16251627
std::make_pair("cet-report", &ctx.arg.zCetReport),
16261628
std::make_pair("execute-only-report", &ctx.arg.zExecuteOnlyReport),
16271629
std::make_pair("gcs-report", &ctx.arg.zGcsReport),
1630+
std::make_pair("gcs-report-dynamic", &ctx.arg.zGcsReportDynamic),
16281631
std::make_pair("pauth-report", &ctx.arg.zPauthReport)};
1632+
bool hasGcsReportDynamic = false;
16291633
for (opt::Arg *arg : args.filtered(OPT_z)) {
16301634
std::pair<StringRef, StringRef> option =
16311635
StringRef(arg->getValue()).split('=');
@@ -1644,9 +1648,16 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
16441648
<< "= value: " << option.second;
16451649
continue;
16461650
}
1651+
hasGcsReportDynamic |= option.first == "gcs-report-dynamic";
16471652
}
16481653
}
16491654

1655+
// When -zgcs-report-dynamic is unspecified, it inherits -zgcs-report
1656+
// but is capped at warning to avoid needing to rebuild the shared library
1657+
// with GCS enabled.
1658+
if (!hasGcsReportDynamic && ctx.arg.zGcsReport != ReportPolicy::None)
1659+
ctx.arg.zGcsReportDynamic = ReportPolicy::Warning;
1660+
16501661
for (opt::Arg *arg : args.filtered(OPT_compress_sections)) {
16511662
SmallVector<StringRef, 0> fields;
16521663
StringRef(arg->getValue()).split(fields, '=');
@@ -2907,6 +2918,22 @@ static void readSecurityNotes(Ctx &ctx) {
29072918
ctx.arg.andFeatures |= GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
29082919
else if (ctx.arg.zGcs == GcsPolicy::Never)
29092920
ctx.arg.andFeatures &= ~GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
2921+
2922+
// If we are utilising GCS at any stage, the sharedFiles should be checked to
2923+
// ensure they also support this feature. The gcs-report-dynamic option is
2924+
// used to indicate if the user wants information relating to this, and will
2925+
// be set depending on the user's input, or warning if gcs-report is set to
2926+
// either `warning` or `error`.
2927+
if (ctx.arg.andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
2928+
for (SharedFile *f : ctx.sharedFiles)
2929+
reportUnless(ctx.arg.zGcsReportDynamic,
2930+
f->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_GCS)
2931+
<< f
2932+
<< ": GCS is required by -z gcs, but this shared library lacks the "
2933+
"necessary property note. The "
2934+
<< "dynamic loader might not enable GCS or refuse to load the "
2935+
"program unless all shared library "
2936+
<< "dependencies have the GCS marking.";
29102937
}
29112938

29122939
static void initSectionsAndLocalSyms(ELFFileBase *file, bool ignoreComdats) {

lld/ELF/InputFiles.cpp

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,56 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
918918
handleSectionGroup<ELFT>(this->sections, entries);
919919
}
920920

921+
template <typename ELFT>
922+
static void parseGnuPropertyNote(Ctx &ctx, ELFFileBase &f,
923+
uint32_t featureAndType,
924+
ArrayRef<uint8_t> &desc, const uint8_t *base,
925+
ArrayRef<uint8_t> *data = nullptr) {
926+
auto err = [&](const uint8_t *place) -> ELFSyncStream {
927+
auto diag = Err(ctx);
928+
diag << &f << ":(" << ".note.gnu.property+0x"
929+
<< Twine::utohexstr(place - base) << "): ";
930+
return diag;
931+
};
932+
933+
while (!desc.empty()) {
934+
const uint8_t *place = desc.data();
935+
if (desc.size() < 8)
936+
return void(err(place) << "program property is too short");
937+
uint32_t type = read32<ELFT::Endianness>(desc.data());
938+
uint32_t size = read32<ELFT::Endianness>(desc.data() + 4);
939+
desc = desc.slice(8);
940+
if (desc.size() < size)
941+
return void(err(place) << "program property is too short");
942+
943+
if (type == featureAndType) {
944+
// We found a FEATURE_1_AND field. There may be more than one of these
945+
// in a .note.gnu.property section, for a relocatable object we
946+
// accumulate the bits set.
947+
if (size < 4)
948+
return void(err(place) << "FEATURE_1_AND entry is too short");
949+
f.andFeatures |= read32<ELFT::Endianness>(desc.data());
950+
} else if (ctx.arg.emachine == EM_AARCH64 &&
951+
type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
952+
ArrayRef<uint8_t> contents = data ? *data : desc;
953+
if (!f.aarch64PauthAbiCoreInfo.empty()) {
954+
return void(
955+
err(contents.data())
956+
<< "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
957+
"not supported");
958+
} else if (size != 16) {
959+
return void(err(contents.data())
960+
<< "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "
961+
"is invalid: expected 16 bytes, but got "
962+
<< size);
963+
}
964+
f.aarch64PauthAbiCoreInfo = desc;
965+
}
966+
967+
// Padding is present in the note descriptor, if necessary.
968+
desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
969+
}
970+
}
921971
// Read the following info from the .note.gnu.property section and write it to
922972
// the corresponding fields in `ObjFile`:
923973
// - Feature flags (32 bits) representing x86 or AArch64 features for
@@ -955,42 +1005,8 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
9551005

9561006
// Read a body of a NOTE record, which consists of type-length-value fields.
9571007
ArrayRef<uint8_t> desc = note.getDesc(sec.addralign);
958-
while (!desc.empty()) {
959-
const uint8_t *place = desc.data();
960-
if (desc.size() < 8)
961-
return void(err(place) << "program property is too short");
962-
uint32_t type = read32<ELFT::Endianness>(desc.data());
963-
uint32_t size = read32<ELFT::Endianness>(desc.data() + 4);
964-
desc = desc.slice(8);
965-
if (desc.size() < size)
966-
return void(err(place) << "program property is too short");
967-
968-
if (type == featureAndType) {
969-
// We found a FEATURE_1_AND field. There may be more than one of these
970-
// in a .note.gnu.property section, for a relocatable object we
971-
// accumulate the bits set.
972-
if (size < 4)
973-
return void(err(place) << "FEATURE_1_AND entry is too short");
974-
f.andFeatures |= read32<ELFT::Endianness>(desc.data());
975-
} else if (ctx.arg.emachine == EM_AARCH64 &&
976-
type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
977-
if (!f.aarch64PauthAbiCoreInfo.empty()) {
978-
return void(
979-
err(data.data())
980-
<< "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
981-
"not supported");
982-
} else if (size != 16) {
983-
return void(err(data.data())
984-
<< "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "
985-
"is invalid: expected 16 bytes, but got "
986-
<< size);
987-
}
988-
f.aarch64PauthAbiCoreInfo = desc;
989-
}
990-
991-
// Padding is present in the note descriptor, if necessary.
992-
desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
993-
}
1008+
const uint8_t *base = sec.content().data();
1009+
parseGnuPropertyNote<ELFT>(ctx, f, featureAndType, desc, base, &data);
9941010

9951011
// Go to next NOTE record to look for more FEATURE_1_AND descriptions.
9961012
data = data.slice(nhdr->getSize(sec.addralign));
@@ -1418,6 +1434,28 @@ std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,
14181434
return verneeds;
14191435
}
14201436

1437+
// Parse PT_GNU_PROPERTY segments in DSO. The process is similar to
1438+
// readGnuProperty, but we don't have the InputSection information.
1439+
template <typename ELFT>
1440+
void SharedFile::parseGnuAndFeatures(const ELFFile<ELFT> &obj) {
1441+
if (ctx.arg.emachine != EM_AARCH64)
1442+
return;
1443+
const uint8_t *base = obj.base();
1444+
auto phdrs = CHECK2(obj.program_headers(), this);
1445+
for (auto phdr : phdrs) {
1446+
if (phdr.p_type != PT_GNU_PROPERTY)
1447+
continue;
1448+
typename ELFT::Note note(
1449+
*reinterpret_cast<const typename ELFT::Nhdr *>(base + phdr.p_offset));
1450+
if (note.getType() != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU")
1451+
continue;
1452+
1453+
ArrayRef<uint8_t> desc = note.getDesc(phdr.p_align);
1454+
parseGnuPropertyNote<ELFT>(ctx, *this, GNU_PROPERTY_AARCH64_FEATURE_1_AND,
1455+
desc, base);
1456+
}
1457+
}
1458+
14211459
// We do not usually care about alignments of data in shared object
14221460
// files because the loader takes care of it. However, if we promote a
14231461
// DSO symbol to point to .bss due to copy relocation, we need to keep
@@ -1528,6 +1566,7 @@ template <class ELFT> void SharedFile::parse() {
15281566

15291567
verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec);
15301568
std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec);
1569+
parseGnuAndFeatures<ELFT>(obj);
15311570

15321571
// Parse ".gnu.version" section which is a parallel array for the symbol
15331572
// table. If a given file doesn't have a ".gnu.version" section, we use

lld/ELF/InputFiles.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ class SharedFile : public ELFFileBase {
364364
template <typename ELFT>
365365
std::vector<uint32_t> parseVerneed(const llvm::object::ELFFile<ELFT> &obj,
366366
const typename ELFT::Shdr *sec);
367+
template <typename ELFT>
368+
void parseGnuAndFeatures(const llvm::object::ELFFile<ELFT> &obj);
367369
};
368370

369371
class BinaryFile : public InputFile {

lld/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Non-comprehensive list of changes in this release
2525

2626
ELF Improvements
2727
----------------
28+
* For AArch64, added support for ``-zgcs-report-dynamic``, enabling checks for
29+
GNU GCS Attribute Flags in Dynamic Objects when GCS is enabled. Inherits value
30+
from ``-zgcs-report`` (capped at ``warning`` level) unless user-defined,
31+
ensuring compatibility with GNU ld linker.
2832

2933
Breaking changes
3034
----------------

lld/test/ELF/aarch64-feature-gcs.s

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,26 @@
4949
# REPORT-WARN: warning: func2.o: -z gcs-report: file does not have GNU_PROPERTY_AARCH64_FEATURE_1_GCS property
5050
# REPORT-ERROR: error: func3.o: -z gcs-report: file does not have GNU_PROPERTY_AARCH64_FEATURE_1_GCS property
5151

52+
## gcs-report-dynamic should report any dynamic objects that does not have the gcs property. This also ensures the inhertance from gcs-report is working correctly.
53+
54+
# RUN: ld.lld func1-gcs.o func3-gcs.o no-gcs.so force-gcs.so -z gcs-report=warning -z gcs=always 2>&1 | FileCheck --check-prefix=REPORT-WARN-DYNAMIC %s
55+
# RUN: ld.lld func1-gcs.o func3-gcs.o no-gcs.so force-gcs.so -z gcs-report=error -z gcs=always 2>&1 | FileCheck --check-prefix=REPORT-WARN-DYNAMIC %s
56+
# RUN: ld.lld func1-gcs.o func3-gcs.o no-gcs.so force-gcs.so -z gcs-report-dynamic=warning -z gcs=always 2>&1 | FileCheck --check-prefix=REPORT-WARN-DYNAMIC %s
57+
# RUN: not ld.lld func1-gcs.o func3-gcs.o no-gcs.so force-gcs.so -z gcs-report-dynamic=error -z gcs=always 2>&1 | FileCheck --check-prefix=REPORT-ERROR-DYNAMIC %s
58+
# RUN: ld.lld func1-gcs.o func3-gcs.o force-gcs.so -z gcs-report-dynamic=warning -z gcs=always 2>&1 | count 0
59+
# RUN: ld.lld func1-gcs.o func3-gcs.o force-gcs.so -z gcs-report-dynamic=error -z gcs=always 2>&1 | count 0
60+
61+
# REPORT-WARN-DYNAMIC: warning: no-gcs.so: GCS is required by -z gcs, but this shared library lacks the necessary property note. The dynamic loader might not enable GCS or refuse to load the program unless all shared library dependencies have the GCS marking.
62+
# REPORT-WARN-DYNAMIC-NOT: warning:
63+
# REPORT-ERROR-DYNAMIC: error: no-gcs.so: GCS is required by -z gcs, but this shared library lacks the necessary property note. The dynamic loader might not enable GCS or refuse to load the program unless all shared library dependencies have the GCS marking.
64+
# REPORT-ERROR-DYNAMIC-NOT: error:
65+
5266
## An invalid gcs option should give an error
53-
# RUN: not ld.lld func1-gcs.o func2-gcs.o func3-gcs.o -z gcs=nonsense 2>&1 | FileCheck --check-prefix=INVALID %s
67+
# RUN: not ld.lld func1-gcs.o func2-gcs.o func3-gcs.o -z gcs=nonsense -z gcs-report=nonsense -z gcs-report-dynamic=nonsense 2>&1 | FileCheck --check-prefix=INVALID %s
5468

5569
# INVALID: error: unknown -z gcs= value: nonsense
70+
# INVALID: error: unknown -z gcs-report= value: nonsense
71+
# INVALID: error: unknown -z gcs-report-dynamic= value: nonsense
5672

5773
#--- func1-gcs.s
5874
.section ".note.gnu.property", "a"

0 commit comments

Comments
 (0)