summaryrefslogtreecommitdiff
path: root/lld/ELF/InputFiles.cpp
diff options
context:
space:
mode:
authorJack Styles <jack.styles@arm.com>2025-03-16 01:15:05 +0000
committerGitHub <noreply@github.com>2025-03-15 18:15:05 -0700
commit4286f4dccee9140eef3ef7d059619eaab73fc392 (patch)
treeb8d25ea32886b6c8a54231fd92fd6625ff5f72e1 /lld/ELF/InputFiles.cpp
parent7722d7519ca2679e105e2db68c889b3727938c8d (diff)
[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: https://github.com/bminor/binutils-gdb/commit/724a8341f6491283cf90038260c83a454b7268ef 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 <i@maskray.me>
Diffstat (limited to 'lld/ELF/InputFiles.cpp')
-rw-r--r--lld/ELF/InputFiles.cpp111
1 files changed, 75 insertions, 36 deletions
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index d43de8ce6dfe..5f6d2b6b647e 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -918,6 +918,56 @@ void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
handleSectionGroup<ELFT>(this->sections, entries);
}
+template <typename ELFT>
+static void parseGnuPropertyNote(Ctx &ctx, ELFFileBase &f,
+ uint32_t featureAndType,
+ ArrayRef<uint8_t> &desc, const uint8_t *base,
+ ArrayRef<uint8_t> *data = nullptr) {
+ auto err = [&](const uint8_t *place) -> ELFSyncStream {
+ auto diag = Err(ctx);
+ diag << &f << ":(" << ".note.gnu.property+0x"
+ << Twine::utohexstr(place - base) << "): ";
+ return diag;
+ };
+
+ while (!desc.empty()) {
+ const uint8_t *place = desc.data();
+ if (desc.size() < 8)
+ return void(err(place) << "program property is too short");
+ uint32_t type = read32<ELFT::Endianness>(desc.data());
+ uint32_t size = read32<ELFT::Endianness>(desc.data() + 4);
+ desc = desc.slice(8);
+ if (desc.size() < size)
+ return void(err(place) << "program property is too short");
+
+ if (type == featureAndType) {
+ // We found a FEATURE_1_AND field. There may be more than one of these
+ // in a .note.gnu.property section, for a relocatable object we
+ // accumulate the bits set.
+ if (size < 4)
+ return void(err(place) << "FEATURE_1_AND entry is too short");
+ f.andFeatures |= read32<ELFT::Endianness>(desc.data());
+ } else if (ctx.arg.emachine == EM_AARCH64 &&
+ type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
+ ArrayRef<uint8_t> contents = data ? *data : desc;
+ if (!f.aarch64PauthAbiCoreInfo.empty()) {
+ return void(
+ err(contents.data())
+ << "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
+ "not supported");
+ } else if (size != 16) {
+ return void(err(contents.data())
+ << "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "
+ "is invalid: expected 16 bytes, but got "
+ << size);
+ }
+ f.aarch64PauthAbiCoreInfo = desc;
+ }
+
+ // Padding is present in the note descriptor, if necessary.
+ desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
+ }
+}
// Read the following info from the .note.gnu.property section and write it to
// the corresponding fields in `ObjFile`:
// - Feature flags (32 bits) representing x86 or AArch64 features for
@@ -955,42 +1005,8 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
// Read a body of a NOTE record, which consists of type-length-value fields.
ArrayRef<uint8_t> desc = note.getDesc(sec.addralign);
- while (!desc.empty()) {
- const uint8_t *place = desc.data();
- if (desc.size() < 8)
- return void(err(place) << "program property is too short");
- uint32_t type = read32<ELFT::Endianness>(desc.data());
- uint32_t size = read32<ELFT::Endianness>(desc.data() + 4);
- desc = desc.slice(8);
- if (desc.size() < size)
- return void(err(place) << "program property is too short");
-
- if (type == featureAndType) {
- // We found a FEATURE_1_AND field. There may be more than one of these
- // in a .note.gnu.property section, for a relocatable object we
- // accumulate the bits set.
- if (size < 4)
- return void(err(place) << "FEATURE_1_AND entry is too short");
- f.andFeatures |= read32<ELFT::Endianness>(desc.data());
- } else if (ctx.arg.emachine == EM_AARCH64 &&
- type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
- if (!f.aarch64PauthAbiCoreInfo.empty()) {
- return void(
- err(data.data())
- << "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
- "not supported");
- } else if (size != 16) {
- return void(err(data.data())
- << "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "
- "is invalid: expected 16 bytes, but got "
- << size);
- }
- f.aarch64PauthAbiCoreInfo = desc;
- }
-
- // Padding is present in the note descriptor, if necessary.
- desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
- }
+ const uint8_t *base = sec.content().data();
+ parseGnuPropertyNote<ELFT>(ctx, f, featureAndType, desc, base, &data);
// Go to next NOTE record to look for more FEATURE_1_AND descriptions.
data = data.slice(nhdr->getSize(sec.addralign));
@@ -1418,6 +1434,28 @@ std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,
return verneeds;
}
+// Parse PT_GNU_PROPERTY segments in DSO. The process is similar to
+// readGnuProperty, but we don't have the InputSection information.
+template <typename ELFT>
+void SharedFile::parseGnuAndFeatures(const ELFFile<ELFT> &obj) {
+ if (ctx.arg.emachine != EM_AARCH64)
+ return;
+ const uint8_t *base = obj.base();
+ auto phdrs = CHECK2(obj.program_headers(), this);
+ for (auto phdr : phdrs) {
+ if (phdr.p_type != PT_GNU_PROPERTY)
+ continue;
+ typename ELFT::Note note(
+ *reinterpret_cast<const typename ELFT::Nhdr *>(base + phdr.p_offset));
+ if (note.getType() != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU")
+ continue;
+
+ ArrayRef<uint8_t> desc = note.getDesc(phdr.p_align);
+ parseGnuPropertyNote<ELFT>(ctx, *this, GNU_PROPERTY_AARCH64_FEATURE_1_AND,
+ desc, base);
+ }
+}
+
// We do not usually care about alignments of data in shared object
// files because the loader takes care of it. However, if we promote a
// DSO symbol to point to .bss due to copy relocation, we need to keep
@@ -1528,6 +1566,7 @@ template <class ELFT> void SharedFile::parse() {
verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec);
std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec);
+ parseGnuAndFeatures<ELFT>(obj);
// Parse ".gnu.version" section which is a parallel array for the symbol
// table. If a given file doesn't have a ".gnu.version" section, we use