-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
512784f
[AArch64][Build Attributes] Improve testing (delete previous tests fi…
sivan-shani 506aad3
[AArch64][Build Attributes] Standardize parsing error messages
sivan-shani 1fb501d
[AArch64][Build Attributes] Print Tags as Numbers with Comments
sivan-shani 083bd48
[AArch64][Build Attributes] Fix Parsing of Some Tags and Values
sivan-shani a126df1
[AArch64][Build Attributes] Remove Assertion for Duplicate Values
sivan-shani e453ae0
[AArch64][Build Attributes] Add test files
sivan-shani b57cce1
[readobj][AArch64] Support Parsing AArch64 Build Attributes
sivan-shani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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 || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofELFAttributeParser
, for example it does not populate theattributes
map, so thegetAttributeValue
andgetAttributeString
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.