Skip to content

WIP [readobj][AArch64] Parse AArch64 build attributes #124276

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions llvm/include/llvm/Object/ELFObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
case ELF::EM_ARM:
Type = ELF::SHT_ARM_ATTRIBUTES;
break;
case ELF::EM_AARCH64:
Type = ELF::SHT_AARCH64_ATTRIBUTES;
break;
case ELF::EM_RISCV:
Type = ELF::SHT_RISCV_ATTRIBUTES;
break;
Expand Down
32 changes: 32 additions & 0 deletions llvm/include/llvm/Support/AArch64AttributeParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//=== - AArch64AttributeParser.h-AArch64 Attribute Information Printer - ===//
//
// 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
//
//===--------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_AARCH64ATTRIBUTEPARSER_H
#define LLVM_SUPPORT_AARCH64ATTRIBUTEPARSER_H

#include "ELFAttributeParser.h"
#include "llvm/Support/Error.h"

namespace llvm {

class ScopedPrinter;

class AArch64AttributeParser : public ELFAttributeParser {
Error handler(uint64_t Tag, bool &Handled) override {
return Error::success();
}

public:
Error parse(ArrayRef<uint8_t> Section, llvm::endianness Endian) override;

AArch64AttributeParser(ScopedPrinter *Sw) : ELFAttributeParser(Sw) {}
AArch64AttributeParser() {}
};
} // namespace llvm

#endif // LLVM_SUPPORT_AARCH64ATTRIBUTEPARSER_H
4 changes: 2 additions & 2 deletions llvm/include/llvm/Support/AArch64BuildAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace llvm {

namespace AArch64BuildAttrs {
namespace AArch64BuildAttributes {

/// AArch64 build attributes vendors IDs (a.k.a subsection name)
enum VendorID : unsigned {
Expand Down Expand Up @@ -69,7 +69,7 @@ enum FeatureAndBitsFlag : unsigned {
Feature_PAC_Flag = 1 << 1,
Feature_GCS_Flag = 1 << 2
};
} // namespace AArch64BuildAttrs
} // namespace AArch64BuildAttributes
} // namespace llvm

#endif // LLVM_SUPPORT_AARCH64BUILDATTRIBUTES_H
4 changes: 3 additions & 1 deletion llvm/include/llvm/Support/ELFAttributeParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ class ELFAttributeParser {

ELFAttributeParser(TagNameMap tagNameMap, StringRef vendor)
: vendor(vendor), sw(nullptr), tagToStringMap(tagNameMap) {}
ELFAttributeParser(ScopedPrinter *sw) : sw(sw) {}
ELFAttributeParser() : sw(nullptr) {}

Error parse(ArrayRef<uint8_t> section, llvm::endianness endian);
virtual Error parse(ArrayRef<uint8_t> section, llvm::endianness endian);

std::optional<unsigned> getAttributeValue(unsigned tag) const {
auto I = attributes.find(tag);
Expand Down
152 changes: 152 additions & 0 deletions llvm/lib/Support/AArch64AttributeParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//===-AArch64AttributeParser.cpp-AArch64 Attribute Information Printer-===//
//
// 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/AArch64AttributeParser.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/AArch64BuildAttributes.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>

using namespace llvm;

Error AArch64AttributeParser::parse(ArrayRef<uint8_t> Section,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is completely replacing the parse function in the base class, which I think makes the design more complicated because there is lots of code now in the base class which is only sometimes used.

I think it would be better to split out parsing of the two sub-classes to parse the ARM-style (also used by other architectures) and AArch64-style (currently AArch64 only, but not guaranteed to remain AArch64-specific). This would leave the base class as an abstract class without an implementation of the parse function. The architecture-specific classes can then derive from one of the new classes, depending on which format it uses.

The new AArch64AttributeParser implementation also only implements some of the behaviour of ELFAttributeParser, for example it does not populate the attributes map, so the getAttributeValue and getAttributeString functions will fail. This should either implement all of the functionality provided by the base class, or the parts not needed for the AArch64-style attributes should be moved to the ARM-style class.

llvm::endianness Endian) {

unsigned SectionNumber = 0;
de = DataExtractor(Section, Endian == llvm::endianness::little, 0);

// Early returns have specific errors. Consume the Error in cursor.
struct ClearCursorError {
DataExtractor::Cursor &Cursor;
~ClearCursorError() { consumeError(Cursor.takeError()); }
} Clear{cursor};

/*
AArch64 build attributes layout:
<format-version: ‘A’> --> There is only one version, 'A' (0x41)
[ <uint32: subsection-length> <NTBS: vendor-name> <bytes: vendor-data> ]
--> subsection-length: the offset from the start of this subsection to the
start of the next one.
--> vendor-name: NUL-terminated byte string.
--> vendor-data expands to:
[ <uint8: optional> <uint8: parameter type> <attribute>*]
--> optional: 0- required, 1- optional
--> type: 0- ULEB128, 1- NTBS
--> attribute: <tag, value>* pair. Tag is ULEB128, value is <parameter
type> type.
*/

// Get format-version
uint8_t FormatVersion = de.getU8(cursor);
if (ELFAttrs::Format_Version != FormatVersion)
return createStringError(errc::invalid_argument,
"unrecognized format-version: 0x" +
utohexstr(FormatVersion));

while (!de.eof(cursor)) {
uint32_t BASubsectionLength = de.getU32(cursor);
// Minimal valid BA subsection header size is at least 8: length(4) name(at
// least a single char + null) optionality(1) and type(1)
if (BASubsectionLength < 8)
return createStringError(
errc::invalid_argument,
"invalid AArch64 build attribute subsection size at offset: " +
utohexstr(cursor.tell() - 4));

StringRef VendorName = de.getCStrRef(cursor);
// The layout of a private subsection (--> vendor name does not starts with
// 'aeabi') is unknown, skip)
if (!VendorName.starts_with("aeabi")) {
sw->startLine()
<< "** Skipping private AArch64 build attributes subsection: "
<< VendorName << "\n";
// Offset in Section
uint64_t OffsetInSection = cursor.tell();
// Size: 4 bytes, Vendor Name: VendorName.size() + 1 (null termination)
uint32_t BytesForLengthName = 4 + (VendorName.size() + 1);
cursor.seek(OffsetInSection + BASubsectionLength - BytesForLengthName);
continue;
}
// All public subsections names must be known
if (VendorName.starts_with("aeabi")) {
if (!("aeabi_feature_and_bits" == VendorName ||
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should be able to print any "aeabi*" subsection, because the format is known, even if the tag names aren't.

"aeabi_pauthabi" == VendorName)) {
return createStringError(
errc::invalid_argument,
"unknown public AArch64 build attribute subsection name at "
"offset: " +
utohexstr(cursor.tell() - (VendorName.size() + 1)));
}
}

uint8_t IsOptional = de.getU8(cursor);
StringRef IsOptionalStr = IsOptional ? "optional" : "required";
uint8_t Type = de.getU8(cursor);
StringRef TypeStr = Type ? "ntbs" : "uleb128";

if (sw) {
sw->startLine() << "Section " << ++SectionNumber << " {\n";
sw->indent();
sw->printNumber("SectionLength", BASubsectionLength);
sw->startLine() << "VendorName" << ": " << VendorName
<< " Optionality: " << IsOptionalStr
<< " Type: " << TypeStr << "\n";
sw->startLine() << "Attributes {\n";
sw->indent();
}

// Offset in Section
uint64_t OffsetInSection = cursor.tell();
// Size: 4 bytes, Vendor Name: VendorName.size() + 1 (null termination),
// optionality: 1, size: 1
uint32_t BytesAllButAttributes = 4 + (VendorName.size() + 1) + 1 + 1;
while (cursor.tell() <
(OffsetInSection + BASubsectionLength - BytesAllButAttributes)) {

uint64_t Tag = de.getULEB128(cursor);
std::string Str = utostr(Tag);
StringRef TagStr(Str);
if ("aeabi_feature_and_bits" == VendorName) {
StringRef TagAsString =
AArch64BuildAttributes::getFeatureAndBitsTagsStr(Tag);
if ("" != TagAsString)
TagStr = TagAsString;
}
if ("aeabi_pauthabi" == VendorName) {
StringRef TagAsString = AArch64BuildAttributes::getPauthABITagsStr(Tag);
if ("" != TagAsString)
TagStr = TagAsString;
}

if (Type) { // type==1 --> ntbs
StringRef Value = de.getCStrRef(cursor);
if (sw)
sw->printString(TagStr, Value);
} else { // type==0 --> uleb128
uint64_t Value = de.getULEB128(cursor);
if (sw)
sw->printNumber(TagStr, Value);
}
}
if (sw) {
// Close 'Attributes'
sw->unindent();
sw->startLine() << "}\n";
// Close 'Section'
sw->unindent();
sw->startLine() << "}\n";
}
}

return cursor.takeError();
}
26 changes: 13 additions & 13 deletions llvm/lib/Support/AArch64BuildAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include "llvm/ADT/StringSwitch.h"

using namespace llvm;
using namespace llvm::AArch64BuildAttrs;
using namespace llvm::AArch64BuildAttributes;

StringRef AArch64BuildAttrs::getVendorName(unsigned Vendor) {
StringRef AArch64BuildAttributes::getVendorName(unsigned Vendor) {
switch (Vendor) {
case AEABI_FEATURE_AND_BITS:
return "aeabi_feature_and_bits";
Expand All @@ -25,14 +25,14 @@ StringRef AArch64BuildAttrs::getVendorName(unsigned Vendor) {
return "";
}
}
VendorID AArch64BuildAttrs::getVendorID(StringRef Vendor) {
VendorID AArch64BuildAttributes::getVendorID(StringRef Vendor) {
return StringSwitch<VendorID>(Vendor)
.Case("aeabi_feature_and_bits", AEABI_FEATURE_AND_BITS)
.Case("aeabi_pauthabi", AEABI_PAUTHABI)
.Default(VENDOR_UNKNOWN);
}

StringRef AArch64BuildAttrs::getOptionalStr(unsigned Optional) {
StringRef AArch64BuildAttributes::getOptionalStr(unsigned Optional) {
switch (Optional) {
case REQUIRED:
return "required";
Expand All @@ -43,18 +43,18 @@ StringRef AArch64BuildAttrs::getOptionalStr(unsigned Optional) {
return "";
}
}
SubsectionOptional AArch64BuildAttrs::getOptionalID(StringRef Optional) {
SubsectionOptional AArch64BuildAttributes::getOptionalID(StringRef Optional) {
return StringSwitch<SubsectionOptional>(Optional)
.Case("required", REQUIRED)
.Case("optional", OPTIONAL)
.Default(OPTIONAL_NOT_FOUND);
}
StringRef AArch64BuildAttrs::getSubsectionOptionalUnknownError() {
StringRef AArch64BuildAttributes::getSubsectionOptionalUnknownError() {
return "unknown AArch64 build attributes optionality, expected "
"required|optional";
}

StringRef AArch64BuildAttrs::getTypeStr(unsigned Type) {
StringRef AArch64BuildAttributes::getTypeStr(unsigned Type) {
switch (Type) {
case ULEB128:
return "uleb128";
Expand All @@ -65,17 +65,17 @@ StringRef AArch64BuildAttrs::getTypeStr(unsigned Type) {
return "";
}
}
SubsectionType AArch64BuildAttrs::getTypeID(StringRef Type) {
SubsectionType AArch64BuildAttributes::getTypeID(StringRef Type) {
return StringSwitch<SubsectionType>(Type)
.Cases("uleb128", "ULEB128", ULEB128)
.Cases("ntbs", "NTBS", NTBS)
.Default(TYPE_NOT_FOUND);
}
StringRef AArch64BuildAttrs::getSubsectionTypeUnknownError() {
StringRef AArch64BuildAttributes::getSubsectionTypeUnknownError() {
return "unknown AArch64 build attributes type, expected uleb128|ntbs";
}

StringRef AArch64BuildAttrs::getPauthABITagsStr(unsigned PauthABITag) {
StringRef AArch64BuildAttributes::getPauthABITagsStr(unsigned PauthABITag) {
switch (PauthABITag) {
case TAG_PAUTH_PLATFORM:
return "Tag_PAuth_Platform";
Expand All @@ -87,15 +87,15 @@ StringRef AArch64BuildAttrs::getPauthABITagsStr(unsigned PauthABITag) {
}
}

PauthABITags AArch64BuildAttrs::getPauthABITagsID(StringRef PauthABITag) {
PauthABITags AArch64BuildAttributes::getPauthABITagsID(StringRef PauthABITag) {
return StringSwitch<PauthABITags>(PauthABITag)
.Case("Tag_PAuth_Platform", TAG_PAUTH_PLATFORM)
.Case("Tag_PAuth_Schema", TAG_PAUTH_SCHEMA)
.Default(PAUTHABI_TAG_NOT_FOUND);
}

StringRef
AArch64BuildAttrs::getFeatureAndBitsTagsStr(unsigned FeatureAndBitsTag) {
AArch64BuildAttributes::getFeatureAndBitsTagsStr(unsigned FeatureAndBitsTag) {
switch (FeatureAndBitsTag) {
case TAG_FEATURE_BTI:
return "Tag_Feature_BTI";
Expand All @@ -110,7 +110,7 @@ AArch64BuildAttrs::getFeatureAndBitsTagsStr(unsigned FeatureAndBitsTag) {
}

FeatureAndBitsTags
AArch64BuildAttrs::getFeatureAndBitsTagsID(StringRef FeatureAndBitsTag) {
AArch64BuildAttributes::getFeatureAndBitsTagsID(StringRef FeatureAndBitsTag) {
return StringSwitch<FeatureAndBitsTags>(FeatureAndBitsTag)
.Case("Tag_Feature_BTI", TAG_FEATURE_BTI)
.Case("Tag_Feature_PAC", TAG_FEATURE_PAC)
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ add_llvm_component_library(LLVMSupport
ARMBuildAttributes.cpp
AArch64BuildAttributes.cpp
ARMAttributeParser.cpp
AArch64AttributeParser.cpp
ARMWinEH.cpp
Allocator.cpp
AutoConvert.cpp
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Support/ELFAttributeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ Error ELFAttributeParser::parseSubsection(uint32_t length) {
}

// Handle a subsection with an unrecognized vendor-name by skipping
// over it to the next subsection. ADDENDA32 in the Arm ABI defines
// that vendor attribute sections must not affect compatibility, so
// this should always be safe.
// over it to the next subsection. vendor attribute sections must not
// affect compatibility, so this should always be safe.
if (vendorName.lower() != vendor) {
cursor.seek(end);
return Error::success();
Expand Down
Loading