Skip to content

[lld][AArch64][Build Attributes] Add support for AArch64 Build Attributes #144082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 103 additions & 10 deletions lld/ELF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/LTO/LTO.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Support/AArch64AttributeParser.h"
#include "llvm/Support/ARMAttributeParser.h"
#include "llvm/Support/ARMBuildAttributes.h"
#include "llvm/Support/Endian.h"
Expand Down Expand Up @@ -537,6 +538,52 @@ uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {
this);
}

template <class ELFT>
static void
handleAArch64BAAndGnuProperties(ObjFile<ELFT> *file, Ctx &ctx, bool hasGP,
const AArch64BuildAttrSubsections &baInfo,
const GnuPropertiesInfo &gpInfo) {
if (hasGP) {
// Check for data mismatch
if (gpInfo.pauthAbiCoreInfo) {
if (baInfo.Pauth.TagPlatform != gpInfo.pauthAbiCoreInfo->platform ||
baInfo.Pauth.TagSchema != gpInfo.pauthAbiCoreInfo->version)
Err(ctx)
<< file
<< " Pauth Data mismatch: file contains both GNU properties and "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "PAuth" (uppercase A) the canonical spelling? Linker diagnostics are usually concise. Perhaps

GNU properties and build attributes have conflicting AArch64 PAuth data

Copy link
Collaborator

@smithp35 smithp35 Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can answer that part.

The spelling of PAuthABI was derived from the spelling of the Architecture feature FEAT_PAuth https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions/The-Armv8-3-architecture-extension?lang=en#md448-the-armv83-architecture-extension__feat_FEAT_PAuth

The PAuthABI spec uses that spelling https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst

I don't think it hugely matters which form is used in source code, but I agree it should be consistent.

"AArch64 build attributes sections with different Pauth data";
}
if (baInfo.AndFeatures != gpInfo.andFeatures)
Err(ctx) << file
<< " Features Data mismatch: file contains both GNU "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider making this conciser: GNU properties and AArch64 build attributes have conflicting And Features

"properties and AArch64 build attributes sections with "
"different And Features data";
} else {
// Write missing data
// We can only know when Pauth is missing.
// Unlike AArch64 Build Attributes, GNU properties does not give a way to
// distinguish between no-value given to value of '0' given.
if (baInfo.Pauth.TagPlatform || baInfo.Pauth.TagSchema) {
// According to the BuildAttributes specification Build Attributes
// default to a value of 0 when not present. A (TagPlatform, TagSchema) of
// (0, 0) maps to 'no PAuth property present'. A (TagPlatform, TagSchema)
// of (0, 1) maps to an explicit PAuth property of platform = 0, version =
// 0 ('Invalid').
if (baInfo.Pauth.TagPlatform == 0 && baInfo.Pauth.TagSchema == 1) {
file->aarch64PauthAbiCoreInfo = {0, 0};
} else {
file->aarch64PauthAbiCoreInfo = {baInfo.Pauth.TagPlatform,
baInfo.Pauth.TagSchema};
}
}
file->andFeatures = baInfo.AndFeatures;
}
}

template <typename ELFT>
static GnuPropertiesInfo readGnuProperty(Ctx &, const InputSection &,
ObjFile<ELFT> &);

template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
object::ELFFile<ELFT> obj = this->getObj();
// Read a section table. justSymbols is usually false.
Expand All @@ -552,8 +599,31 @@ template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
StringRef shstrtab = CHECK2(obj.getSectionStringTable(objSections), this);
uint64_t size = objSections.size();
sections.resize(size);

// For handling AArch64 Build attributes and GNU properties
AArch64BuildAttrSubsections aarch64BAsubSections;
GnuPropertiesInfo gnuProperty;
bool hasAArch64BuildAttributes = false;
bool hasGNUProperties = false;

for (size_t i = 0; i != size; ++i) {
const Elf_Shdr &sec = objSections[i];
// Object files that use processor features such as Intel Control-Flow
// Enforcement (CET) or AArch64 Branch Target Identification BTI, use a
// .note.gnu.property section containing a bitfield of feature bits like the
// GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag.
if (check(obj.getSectionName(sec, shstrtab)) == ".note.gnu.property") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check the section type instead of using the string name (unneeded overhead for other targets)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi I'll be taking this PR over from Sivan.

I don't see much precedent for doing this in this file (looking for getSectionType() uses). Could you please clarify? Thx!

gnuProperty = readGnuProperty(
ctx,
InputSection(*this, sec, check(obj.getSectionName(sec, shstrtab))),
*this);
hasGNUProperties = true;
// Since we merge bitmaps from multiple object files to create a new
// .note.gnu.property containing a single AND'ed bitmap, we discard an
// input file's .note.gnu.property section.
sections[i] = &InputSection::discarded;
}

if (LLVM_LIKELY(sec.sh_type == SHT_PROGBITS))
continue;
if (LLVM_LIKELY(sec.sh_type == SHT_GROUP)) {
Expand Down Expand Up @@ -637,13 +707,27 @@ template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
}
break;
case EM_AARCH64:
// FIXME: BuildAttributes have been implemented in llvm, but not yet in
// lld. Remove the section so that it does not accumulate in the output
// file. When support is implemented we expect not to output a build
// attributes section in files of type ET_EXEC or ET_SHARED, but ld -r
// ouptut will need a single merged attributes section.
if (sec.sh_type == SHT_AARCH64_ATTRIBUTES)
// At this stage AArch64 Build Attributes does not replace GNU Properties.
// When both exists, their values must match.
// When both exists and contain different attributes, they complement each
// other. Currently attributes are represented in the linked object file
// as GNU properties, which are already supported by the Linux kernel and
// the dynamic loader. In the future, when relocatable linking (`-r` flag)
// is performed, a single merged AArch64 Build Attributes section will be
// emitted.
if (sec.sh_type == SHT_AARCH64_ATTRIBUTES) {
ArrayRef<uint8_t> contents = check(obj.getSectionContents(sec));
AArch64AttributeParser attributes;
StringRef name = check(obj.getSectionName(sec, shstrtab));
InputSection isec(*this, sec, name);
if (Error e = attributes.parse(contents, ELFT::Endianness)) {
Warn(ctx) << &isec << ": " << std::move(e);
} else {
aarch64BAsubSections = extractBuildAttributesSubsections(attributes);
hasAArch64BuildAttributes = true;
}
sections[i] = &InputSection::discarded;
}
// Producing a static binary with MTE globals is not currently supported,
// remove all SHT_AARCH64_MEMTAG_GLOBALS_STATIC sections as they're unused
// medatada, and we don't want them to end up in the output file for
Expand All @@ -655,6 +739,14 @@ template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
}
}

if (hasAArch64BuildAttributes) {
// Handle AArch64 Build Attributes and GNU properties:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the comment outside the if and then delete the braces (single-line simple statement doesn't need braces)

// - Err on mismatched values.
// - Store missing values as GNU properties.
handleAArch64BAAndGnuProperties<ELFT>(this, ctx, hasGNUProperties,
aarch64BAsubSections, gnuProperty);
}

// Read a symbol table.
initializeSymbols(obj);
}
Expand Down Expand Up @@ -974,8 +1066,8 @@ static void parseGnuPropertyNote(Ctx &ctx, ELFFileBase &f,
// hardware-assisted call flow control;
// - AArch64 PAuth ABI core info (16 bytes).
template <class ELFT>
static void readGnuProperty(Ctx &ctx, const InputSection &sec,
ObjFile<ELFT> &f) {
static GnuPropertiesInfo readGnuProperty(Ctx &ctx, const InputSection &sec,
ObjFile<ELFT> &f) {
using Elf_Nhdr = typename ELFT::Nhdr;
using Elf_Note = typename ELFT::Note;

Expand All @@ -992,7 +1084,7 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
featureAndType = GNU_PROPERTY_RISCV_FEATURE_1_AND;
break;
default:
return;
return GnuPropertiesInfo{};
}

ArrayRef<uint8_t> data = sec.content();
Expand All @@ -1007,7 +1099,7 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
if (data.size() < sizeof(Elf_Nhdr) ||
data.size() < nhdr->getSize(sec.addralign))
return void(err(data.data()) << "data is too short");
return (err(data.data()) << "data is too short", GnuPropertiesInfo{});

Elf_Note note(*nhdr);
if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {
Expand All @@ -1023,6 +1115,7 @@ static void readGnuProperty(Ctx &ctx, const InputSection &sec,
// Go to next NOTE record to look for more FEATURE_1_AND descriptions.
data = data.slice(nhdr->getSize(sec.addralign));
}
return GnuPropertiesInfo{f.andFeatures, f.aarch64PauthAbiCoreInfo};
}

template <class ELFT>
Expand Down
5 changes: 5 additions & 0 deletions lld/ELF/InputFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ class ELFFileBase : public InputFile {
std::optional<AArch64PauthAbiCoreInfo> aarch64PauthAbiCoreInfo;
};

struct GnuPropertiesInfo {
uint32_t andFeatures = 0;
std::optional<AArch64PauthAbiCoreInfo> pauthAbiCoreInfo;
};

// .o file.
template <class ELFT> class ObjFile : public ELFFileBase {
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Expand Down
10 changes: 10 additions & 0 deletions lld/test/ELF/Inputs/aarch64-func3-pac-replace.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Declare file properties exclusively with aarch64 build attributes.

.aeabi_subsection aeabi_feature_and_bits, optional, uleb128
.aeabi_attribute Tag_Feature_PAC, 1

.text
.globl func3
.type func3,@function
func3:
ret
13 changes: 13 additions & 0 deletions lld/test/ELF/Inputs/aarch64-pac1-replace.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file replace gnu properties with aarch64 build attributes.

.aeabi_subsection aeabi_feature_and_bits, optional, uleb128
.aeabi_attribute Tag_Feature_PAC, 1

.text
.globl func2
.type func2,@function
func2:
.globl func3
.type func3, @function
bl func3
ret
50 changes: 50 additions & 0 deletions lld/test/ELF/aarch64-build-attributes-be.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// REQUIRES: aarch64
// RUN: llvm-mc -triple=aarch64_be %s -filetype=obj -o %t.o
// RUN: ld.lld %t.o --shared -o %t.so
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE

/// The Build attributes section appearing in the output of
/// llvm-mc should not appear in the output of lld, because
/// AArch64 build attributes are being transformed into .gnu.properties.

/// Test mc -> big endian, lld -> little endian
// RUN: llvm-mc -triple=aarch64_be %s -filetype=obj -o %t.o
// RUN: ld.lld %t.o --shared -o %t.so
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE
// RUN: ld.lld %t.o -o %t
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE
// RUN: ld.lld -r %t.o -o %t2.o
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE

/// Test mc -> little endian, lld -> big endian
// RUN: llvm-mc -triple=aarch64 %s -filetype=obj -o %t.o
// RUN: ld.lld --EB %t.o --shared -o %t.so
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE
// RUN: ld.lld --EB %t.o -o %t
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE
// RUN: ld.lld --EB -r %t.o -o %t2.o
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE

/// Test mc -> big endian, lld -> big endian
// RUN: llvm-mc -triple=aarch64_be %s -filetype=obj -o %t.o
// RUN: ld.lld --EB %t.o --shared -o %t.so
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE
// RUN: ld.lld --EB %t.o -o %t
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE
// RUN: ld.lld --EB -r %t.o -o %t2.o
// RUN: llvm-readelf -n %t.so | FileCheck %s --check-prefix=NOTE

// NOTE: Displaying notes found in: .note.gnu.property
// NOTE-NEXT: Owner Data size Description
// NOTE-NEXT: GNU 0x00000028 NT_GNU_PROPERTY_TYPE_0 (property note)
// NOTE-NEXT: Properties: aarch64 feature: BTI, PAC, GCS
// NOTE-NEXT: AArch64 PAuth ABI core info: platform 0x89abcdef (unknown), version 0x89abcdef


.aeabi_subsection aeabi_pauthabi, required, uleb128
.aeabi_attribute Tag_PAuth_Platform, 81985529216486895
.aeabi_attribute Tag_PAuth_Schema, 81985529216486895
.aeabi_subsection aeabi_feature_and_bits, optional, uleb128
.aeabi_attribute Tag_Feature_BTI, 1
.aeabi_attribute Tag_Feature_PAC, 1
.aeabi_attribute Tag_Feature_GCS, 1
35 changes: 35 additions & 0 deletions lld/test/ELF/aarch64-build-attributes-err.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// REQUIRES: aarch64

// RUN: llvm-mc -triple=aarch64 %s -filetype=obj -o %t.o
// RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR

// ERR: Pauth Data mismatch: file contains both GNU properties and AArch64 build attributes sections with different Pauth data
// ERR-NEXT: Features Data mismatch: file contains both GNU properties and AArch64 build attributes sections with different And Features data

.aeabi_subsection aeabi_pauthabi, required, uleb128
.aeabi_attribute Tag_PAuth_Platform, 5
.aeabi_attribute Tag_PAuth_Schema, 5
.aeabi_subsection aeabi_feature_and_bits, optional, uleb128
.aeabi_attribute Tag_Feature_BTI, 1
.aeabi_attribute Tag_Feature_PAC, 1
.aeabi_attribute Tag_Feature_GCS, 1

.section ".note.gnu.property", "a"
.long 4
.long 0x10
.long 0x5
.asciz "GNU"
.long 0xc0000000 // GNU_PROPERTY_AARCH64_FEATURE_1_AND
.long 4
.long 2 // GNU_PROPERTY_AARCH64_FEATURE_1_PAC
.long 0

.section ".note.gnu.property", "a"
.long 4
.long 24
.long 5
.asciz "GNU"
.long 0xc0000001
.long 16
.quad 305419896 // platform
.quad 2271560481 // version
18 changes: 18 additions & 0 deletions lld/test/ELF/aarch64-build-attributes-invalid.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// REQUIRES: aarch64

// RUN: llvm-mc -triple=aarch64 -filetype=obj %s -o %t.o
// RUN: ld.lld -r %t.o -o %t.invalid.o
// RUN: llvm-readelf -n %t.invalid.o | FileCheck %s

/// According to the BuildAttributes specification Build Attributes
/// A (TagPlatform, TagSchema)of (0, 1) maps to an explicit PAuth property
/// of platform = 0, version = 0 ('Invalid').

// CHECK: Displaying notes found in: .note.gnu.property
// CHECK-NEXT: Owner Data size Description
// CHECK-NEXT: GNU 0x00000018 NT_GNU_PROPERTY_TYPE_0 (property note)
// CHECK-NEXT: Properties: AArch64 PAuth ABI core info: platform 0x0 (invalid), version 0x0

.aeabi_subsection aeabi_pauthabi, required, uleb128
.aeabi_attribute Tag_PAuth_Platform, 0
.aeabi_attribute Tag_PAuth_Schema, 1
16 changes: 16 additions & 0 deletions lld/test/ELF/aarch64-build-attributes-malformed.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# RUN: llvm-mc -triple=aarch64 -filetype=obj %s -o %t.o
# RUN: ld.lld %t.o /dev/null 2>&1 | FileCheck %s

# CHECK: (.ARM.attributes): unexpected end of data at offset 0x3f while reading [0x3d, 0x41)

.section .ARM.attributes,"",%0x70000003
.byte 0x41 // Tag 'A' (format version)
.long 0x00000019 // Subsection length
.asciz "aeabi_pauthabi" // Subsection name
.byte 0x00, 0x00 // Optionality and Type
.byte 0x01, 0x01, 0x02, 0x01 // PAuth_Platform and PAuth_Schema
.long 0x00000023 // Subsection length
.asciz "aeabi_feature_and_bits" // Subsection name
.byte 0x01, 0x00 // Optionality and Type
.byte 0x00, 0x01, 0x01, 0x01, 0x02, 0x01 // BTI, PAC, GCS
.byte 0x00, 0x00 // This is the malformation, data is too long.
Loading
Loading