Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
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
2 changes: 2 additions & 0 deletions llvm/include/llvm/BinaryFormat/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,8 @@ enum : unsigned {
SHT_ARM_ATTRIBUTES = 0x70000003U,
SHT_ARM_DEBUGOVERLAY = 0x70000004U,
SHT_ARM_OVERLAYSECTION = 0x70000005U,
// Support for AArch64 build attributes
SHT_AARCH64_ATTRIBUTES = 0x70000003U,
// Special aarch64-specific section for MTE support, as described in:
// https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#section-types
SHT_AARCH64_AUTH_RELR = 0x70000004U,
Expand Down
25 changes: 23 additions & 2 deletions llvm/include/llvm/MC/MCELFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class MCELFStreamer : public MCObjectStreamer {
// This structure holds all attributes, accounting for their string /
// numeric value, so we can later emit them in declaration order, keeping
// all in the same vector.
enum {
enum Types {
HiddenAttribute = 0,
NumericAttribute,
TextAttribute,
Expand All @@ -105,6 +105,17 @@ class MCELFStreamer : public MCObjectStreamer {
unsigned Tag;
unsigned IntValue;
std::string StringValue;
AttributeItem(Types Ty, unsigned Tg, unsigned IV, std::string SV)
: Type(Ty), Tag(Tg), IntValue(IV), StringValue(SV) {}
};

/// ELF object attributes subsection support
struct AttributeSubSection {
bool IsActive;
StringRef VendorName;
unsigned IsOptional;
unsigned ParameterType;
SmallVector<AttributeItem, 64> Content;
};

// Attributes that are added and managed entirely by target.
Expand All @@ -119,13 +130,23 @@ class MCELFStreamer : public MCObjectStreamer {
unsigned Type, MCSection *&AttributeSection) {
createAttributesSection(Vendor, Section, Type, AttributeSection, Contents);
}
void
emitAttributesSection(MCSection *&AttributeSection, const Twine &Section,
unsigned Type,
SmallVector<AttributeSubSection, 64> &SubSectionVec) {
createAttributesWithSubsection(AttributeSection, Section, Type,
SubSectionVec);
}

private:
AttributeItem *getAttributeItem(unsigned Attribute);
size_t calculateContentSize(SmallVector<AttributeItem, 64> &AttrsVec);
size_t calculateContentSize(SmallVector<AttributeItem, 64> &AttrsVec) const;
void createAttributesSection(StringRef Vendor, const Twine &Section,
unsigned Type, MCSection *&AttributeSection,
SmallVector<AttributeItem, 64> &AttrsVec);
void createAttributesWithSubsection(
MCSection *&AttributeSection, const Twine &Section, unsigned Type,
SmallVector<AttributeSubSection, 64> &SubSectionVec);

// GNU attributes that will get emitted at the end of the asm file.
SmallVector<AttributeItem, 64> GNUAttributes;
Expand Down
88 changes: 88 additions & 0 deletions llvm/include/llvm/Support/AArch64BuildAttributes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//===-- AArch64BuildAttributes.h - AARch64 Build Attributes -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains enumerations and support routines for AArch64 build
// attributes as defined in Build Attributes for the AArch64 document.
//
// Build Attributes for the Arm® 64-bit Architecture (AArch64) 2024Q1
//
// https://github.com/ARM-software/abi-aa/blob/ada00cc8e04f421cce657e8d9f3a439c69437cf4/buildattr64/buildattr64.rst
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this URL points to an un-committed PR, so I don't know if it will remain live after the PR is merged. It would be better to get the spec committed before landing this, but if that's not possible then I think it would be better to link to the PR here (ARM-software/abi-aa#230) because that URL will last longer.

//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_AARCH64BUILDATTRIBUTES_H
#define LLVM_SUPPORT_AARCH64BUILDATTRIBUTES_H

#include "llvm/ADT/StringRef.h"

namespace llvm {

namespace AArch64BuildAttributes {
// AArch64 build attributes
StringRef getSubsectionTag();
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are not going to change, so I think the indirection makes the code harder to read.

StringRef getAttrTag();

/// AArch64 build attributes vendors IDs (a.k.a subsection name)
enum VendorID : unsigned {
AEABI_FEATURE_AND_BITS = 0,
AEABI_PAUTHABI = 1,
VENDOR_UNKNOWN = 404 // Treated as a private subsection name
};
static const StringRef VendorName[] = {"aeabi_feature_and_bits",
"aeabi_pauthabi"};
StringRef getVendorName(unsigned const Vendor);
VendorID getVendorID(StringRef const Vendor);

enum SubsectionOptional : unsigned {
REQUIRED = 0,
OPTIONAL = 1,
OPTIONAL_NOT_FOUND = 404
};
static const StringRef OptionalStr[] = {"required", "optional"};
StringRef getOptionalStr(unsigned Optional);
SubsectionOptional getOptionalID(StringRef Optional);
StringRef getSubsectionOptionalUnknownError();

enum SubsectionType : unsigned { ULEB128 = 0, NTBS = 1, TYPE_NOT_FOUND = 404 };
static const StringRef TypeStr[] = {"uleb128", "ntbs"};
StringRef getTypeStr(unsigned Type);
SubsectionType getTypeID(StringRef Type);
StringRef getSubsectionTypeUnknownError();

enum PauthABITags : unsigned {
TAG_PAUTH_PLATFORM = 1,
TAG_PAUTH_SCHEMA = 2,
PAUTHABI_TAG_NOT_FOUND = 404
};
static const StringRef PauthABITagsStr[] = {"Tag_PAuth_Platform",
"Tag_PAuth_Schema"};
StringRef getPauthABITagsStr(unsigned PauthABITag);
PauthABITags getPauthABITagsID(StringRef PauthABITag);
StringRef getPauthabiTagError();

enum FeatureAndBitsTags : unsigned {
TAG_FEATURE_BTI = 0,
TAG_FEATURE_PAC = 1,
TAG_FEATURE_GCS = 2,
FEATURE_AND_BITS_TAG_NOT_FOUND = 404
};
static const StringRef FeatureAndBitsTagsStr[] = {
"Tag_Feature_BTI", "Tag_Feature_PAC", "Tag_Feature_GCS"};
StringRef getFeatureAndBitsTagsStr(unsigned FeatureAndBitsTag);
FeatureAndBitsTags getFeatureAndBitsTagsID(StringRef FeatureAndBitsTag);
StringRef getFeatureAndBitsTagError();

enum FeatureAndBitsFlag : unsigned {
Feature_BTI_Flag = 1 << 0,
Feature_PAC_Flag = 1 << 1,
Feature_GCS_Flag = 1 << 2
};
} // namespace AArch64BuildAttributes
} // namespace llvm

#endif // LLVM_SUPPORT_AARCH64BUILDATTRIBUTES_H
65 changes: 63 additions & 2 deletions llvm/lib/MC/MCELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,8 @@ MCELFStreamer::getAttributeItem(unsigned Attribute) {
return nullptr;
}

size_t
MCELFStreamer::calculateContentSize(SmallVector<AttributeItem, 64> &AttrsVec) {
size_t MCELFStreamer::calculateContentSize(
SmallVector<AttributeItem, 64> &AttrsVec) const {
size_t Result = 0;
for (const AttributeItem &Item : AttrsVec) {
switch (Item.Type) {
Expand Down Expand Up @@ -783,6 +783,67 @@ void MCELFStreamer::createAttributesSection(
AttrsVec.clear();
}

void MCELFStreamer::createAttributesWithSubsection(
MCSection *&AttributeSection, const Twine &Section, unsigned Type,
SmallVector<AttributeSubSection, 64> &SubSectionVec) {
// <format-version: 'A'>
// [ <uint32: subsection-length> NTBS: vendor-name
// <bytes: vendor-data>
// ]*
// vendor-data expends to:
// <uint8: optional> <uint8: parameter type> <attribute>*
if (0 == SubSectionVec.size()) {
return;
}

// Switch section to AttributeSection or get/create the section.
if (AttributeSection) {
switchSection(AttributeSection);
} else {
AttributeSection = getContext().getELFSection(Section, Type, 0);
switchSection(AttributeSection);

// Format version
emitInt8(0x41);
}

for (AttributeSubSection &SubSection : SubSectionVec) {
// subsection-length + vendor-name + '\0'
const size_t VendorHeaderSize = 4 + SubSection.VendorName.size() + 1;
// optional + parameter-type
const size_t VendorParameters = 1 + 1;
const size_t ContentsSize = calculateContentSize(SubSection.Content);

emitInt32(VendorHeaderSize + VendorParameters + ContentsSize);
emitBytes(SubSection.VendorName);
emitInt8(0); // '\0'
emitInt8(SubSection.IsOptional);
emitInt8(SubSection.ParameterType);

for (AttributeItem &Item : SubSection.Content) {
emitULEB128IntValue(Item.Tag);
switch (Item.Type) {
default:
assert(0 && "Invalid attribute type");
break;
case AttributeItem::NumericAttribute:
emitULEB128IntValue(Item.IntValue);
break;
case AttributeItem::TextAttribute:
emitBytes(Item.StringValue);
emitInt8(0); // '\0'
break;
case AttributeItem::NumericAndTextAttributes:
emitULEB128IntValue(Item.IntValue);
emitBytes(Item.StringValue);
emitInt8(0); // '\0'
break;
}
}
}
SubSectionVec.clear();
}

MCStreamer *llvm::createELFStreamer(MCContext &Context,
std::unique_ptr<MCAsmBackend> &&MAB,
std::unique_ptr<MCObjectWriter> &&OW,
Expand Down
140 changes: 140 additions & 0 deletions llvm/lib/Support/AArch64BuildAttributes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//===-- AArch64BuildAttributes.cpp - AArch64 Build Attributes -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/AArch64BuildAttributes.h"

namespace llvm {
namespace AArch64BuildAttributes {
Copy link
Member

Choose a reason for hiding this comment

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

In ARM we have ARMBuildAttrs. Should this be shortened to AArch64BuildAttrs?

Copy link
Contributor Author

@sivan-shani sivan-shani Jan 24, 2025

Choose a reason for hiding this comment

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

That indeed will be more consistent, on the other hand, Attributes is more descriptive than Attrs, so perhaps changing ARMBuildAttrs to ARMBuildAttributes instead will give both the benefit of consistency and of clarity?

Other files use Attributes as well, and not Attrs (for example: ELFAttributes.cpp, ELFAttributeParser.cpp, HexagonAttributes.cpp, HexagonAttributeParser.cpp, CSKYAttributes.cpp, CSKYAttributeParser.cpp, MSP430Attributes.cpp, MSP430AttributeParser.cpp)
While 'attrs' does not seems to be used elsewhere.

Correction: the files names indeed does not use 'Attrs' but the names inside them does.
Not sure why I was fixated on the files names.
Will change AArch64 accordingly to match.

// AArch64 build attributes
StringRef getSubsectionTag() { return "aeabi_subsection"; }
StringRef getAttrTag() { return "aeabi_attribute"; }

StringRef getVendorName(unsigned Vendor) {
switch (Vendor) {
case AEABI_FEATURE_AND_BITS:
Copy link
Collaborator

Choose a reason for hiding this comment

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

These switch statements will need updating every time a new enum value is added, so I don't think the VendorName array is adding much, and you could just return string literals directly here.

return VendorName[AEABI_FEATURE_AND_BITS];
case AEABI_PAUTHABI:
return VendorName[AEABI_PAUTHABI];
case VENDOR_UNKNOWN:
return "";
default:
assert(0 && "Vendor name error");
return "";
}
}
VendorID getVendorID(StringRef Vendor) {
if (Vendor == VendorName[AEABI_FEATURE_AND_BITS]) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For the string->enum functions, you can use llvm::StringSwitch: https://llvm.org/doxygen/classllvm_1_1StringSwitch.html

return AEABI_FEATURE_AND_BITS;
}
if (Vendor == VendorName[AEABI_PAUTHABI]) {
return AEABI_PAUTHABI;
}
return VENDOR_UNKNOWN;
}

StringRef getOptionalStr(unsigned Optional) {
switch (Optional) {
case REQUIRED:
return OptionalStr[REQUIRED];
case OPTIONAL:
return OptionalStr[OPTIONAL];
case OPTIONAL_NOT_FOUND:
[[fallthrough]];
default:
return "";
}
}
SubsectionOptional getOptionalID(StringRef Optional) {
if (Optional == OptionalStr[REQUIRED])
return REQUIRED;
if (Optional == OptionalStr[OPTIONAL])
return OPTIONAL;
return OPTIONAL_NOT_FOUND;
}
StringRef getSubsectionOptionalUnknownError() {
return "unknown AArch64 build attributes optionality, expecting "
"required|optional";
}

StringRef getTypeStr(unsigned Type) {
switch (Type) {
case ULEB128:
return TypeStr[ULEB128];
case NTBS:
return TypeStr[NTBS];
case TYPE_NOT_FOUND:
[[fallthrough]];
default:
return "";
}
}
SubsectionType getTypeID(StringRef Type) {
if (Type == TypeStr[ULEB128] || Type == (TypeStr[ULEB128].upper()))
return ULEB128;
if (Type == TypeStr[NTBS] || Type == (TypeStr[NTBS].upper()))
return NTBS;
return TYPE_NOT_FOUND;
}
StringRef getSubsectionTypeUnknownError() {
return "unknown AArch64 build attributes type, expecting uleb128|ntbs";
}

StringRef getPauthABITagsStr(unsigned PauthABITag) {
switch (PauthABITag) {
case TAG_PAUTH_PLATFORM:
return PauthABITagsStr[TAG_PAUTH_PLATFORM - 1]; // Tag_PAuth_Platform = 1 in
// accordance with the spec
case TAG_PAUTH_SCHEMA:
return PauthABITagsStr[TAG_PAUTH_SCHEMA - 1]; // Tag_PAuth_Schema = 2 in
// accordance with the spec
case PAUTHABI_TAG_NOT_FOUND:
[[fallthrough]];
default:
return "";
}
}
PauthABITags getPauthABITagsID(StringRef PauthABITag) {
if (PauthABITag == PauthABITagsStr[TAG_PAUTH_PLATFORM - 1])
return TAG_PAUTH_PLATFORM;
if (PauthABITag == PauthABITagsStr[TAG_PAUTH_SCHEMA - 1])
return TAG_PAUTH_SCHEMA;
return PAUTHABI_TAG_NOT_FOUND;
}
StringRef getPauthabiTagError() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused.

return "unknown tag for the AArch64 Pauthabi subsection";
}

StringRef getFeatureAndBitsTagsStr(unsigned FeatureAndBitsTag) {
switch (FeatureAndBitsTag) {
case TAG_FEATURE_BTI:
return FeatureAndBitsTagsStr[TAG_FEATURE_BTI];
case TAG_FEATURE_PAC:
return FeatureAndBitsTagsStr[TAG_FEATURE_PAC];
case TAG_FEATURE_GCS:
return FeatureAndBitsTagsStr[TAG_FEATURE_GCS];
case FEATURE_AND_BITS_TAG_NOT_FOUND:
[[fallthrough]];
default:

return "";
}
}
FeatureAndBitsTags getFeatureAndBitsTagsID(StringRef FeatureAndBitsTag) {
if (FeatureAndBitsTag == FeatureAndBitsTagsStr[TAG_FEATURE_BTI])
return TAG_FEATURE_BTI;
if (FeatureAndBitsTag == FeatureAndBitsTagsStr[TAG_FEATURE_PAC])
return TAG_FEATURE_PAC;
if (FeatureAndBitsTag == FeatureAndBitsTagsStr[TAG_FEATURE_GCS])
return TAG_FEATURE_GCS;
return FEATURE_AND_BITS_TAG_NOT_FOUND;
}
StringRef getFeatureAndBitsTagError() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unused.

return "unknown tag for the AArch64 Feature And Bits subsection";
}
} // namespace AArch64BuildAttributes
} // namespace llvm
1 change: 1 addition & 0 deletions llvm/lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ endif()
add_subdirectory(BLAKE3)

add_llvm_component_library(LLVMSupport
AArch64BuildAttributes.cpp
ABIBreak.cpp
AMDGPUMetadata.cpp
APFixedPoint.cpp
Expand Down
Loading
Loading