-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[BOLT] Add GNUPropertyRewriter and warn on AArch64 BTI note #161206
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,147 @@ | ||
| //===- bolt/Rewrite/GNUPropertyRewriter.cpp -------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Read the .gnu.property.note section. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "bolt/Rewrite/MetadataRewriter.h" | ||
| #include "bolt/Rewrite/MetadataRewriters.h" | ||
| #include "llvm/Support/Errc.h" | ||
|
|
||
| using namespace llvm; | ||
| using namespace bolt; | ||
|
|
||
| namespace { | ||
|
|
||
| class GNUPropertyRewriter final : public MetadataRewriter { | ||
|
|
||
| Expected<uint32_t> decodeGNUPropertyNote(StringRef Desc); | ||
|
|
||
| public: | ||
| GNUPropertyRewriter(StringRef Name, BinaryContext &BC) | ||
| : MetadataRewriter(Name, BC) {} | ||
|
|
||
| Error sectionInitializer() override; | ||
| }; | ||
|
|
||
| Error GNUPropertyRewriter::sectionInitializer() { | ||
|
|
||
| ErrorOr<BinarySection &> Sec = | ||
| BC.getUniqueSectionByName(".note.gnu.property"); | ||
| if (!Sec) | ||
| return Error::success(); | ||
|
|
||
| // Accumulate feature bits | ||
| uint32_t FeaturesAcc = 0; | ||
|
|
||
| StringRef Buf = Sec->getContents(); | ||
| DataExtractor DE(Buf, BC.AsmInfo->isLittleEndian(), | ||
| BC.AsmInfo->getCodePointerSize()); | ||
| DataExtractor::Cursor Cursor(0); | ||
| while (Cursor && !DE.eof(Cursor)) { | ||
| const uint32_t NameSz = DE.getU32(Cursor); | ||
| const uint32_t DescSz = DE.getU32(Cursor); | ||
| const uint32_t Type = DE.getU32(Cursor); | ||
|
|
||
| StringRef Name = | ||
| NameSz ? Buf.slice(Cursor.tell(), Cursor.tell() + NameSz) : "<empty>"; | ||
| Cursor.seek(alignTo(Cursor.tell() + NameSz, 4)); | ||
|
|
||
| const uint64_t DescOffset = Cursor.tell(); | ||
| StringRef Desc = | ||
| DescSz ? Buf.slice(DescOffset, DescOffset + DescSz) : "<empty>"; | ||
| Cursor.seek(alignTo(DescOffset + DescSz, 4)); | ||
| if (!Cursor) | ||
| return createStringError( | ||
| errc::executable_format_error, | ||
| "out of bounds while reading .gnu.property.note section: %s", | ||
| toString(Cursor.takeError()).c_str()); | ||
|
|
||
| if (Type == ELF::NT_GNU_PROPERTY_TYPE_0 && Name.starts_with("GNU") && | ||
| DescSz) { | ||
| auto Features = decodeGNUPropertyNote(Desc); | ||
| if (!Features) | ||
| return Features.takeError(); | ||
| FeaturesAcc |= *Features; | ||
| } | ||
| } | ||
|
|
||
| if (BC.isAArch64()) { | ||
| BC.setUsesBTI(FeaturesAcc & llvm::ELF::GNU_PROPERTY_AARCH64_FEATURE_1_BTI); | ||
| if (BC.usesBTI()) | ||
| BC.outs() << "BOLT-WARNING: binary is using BTI. Optimized binary may be " | ||
| "corrupted\n"; | ||
| } | ||
|
|
||
| return Error::success(); | ||
| } | ||
|
|
||
| /// Desc contains an array of property descriptors. Each member has the | ||
| /// following structure: | ||
| /// typedef struct { | ||
| /// Elf_Word pr_type; | ||
| /// Elf_Word pr_datasz; | ||
| /// unsigned char pr_data[PR_DATASZ]; | ||
| /// unsigned char pr_padding[PR_PADDING]; | ||
| /// } Elf_Prop; | ||
| /// | ||
| /// As there is no guarantee that the features are encoded in which element of | ||
| /// the array, we have to read all, and OR together the result. | ||
| Expected<uint32_t> GNUPropertyRewriter::decodeGNUPropertyNote(StringRef Desc) { | ||
| DataExtractor DE(Desc, BC.AsmInfo->isLittleEndian(), | ||
| BC.AsmInfo->getCodePointerSize()); | ||
| DataExtractor::Cursor Cursor(0); | ||
| const uint32_t Align = DE.getAddressSize(); | ||
|
|
||
| std::optional<uint32_t> Features = 0; | ||
| while (Cursor && !DE.eof(Cursor)) { | ||
| const uint32_t PrType = DE.getU32(Cursor); | ||
| const uint32_t PrDataSz = DE.getU32(Cursor); | ||
|
|
||
| const uint64_t PrDataStart = Cursor.tell(); | ||
| const uint64_t PrDataEnd = PrDataStart + PrDataSz; | ||
| Cursor.seek(PrDataEnd); | ||
| if (!Cursor) | ||
| return createStringError( | ||
| errc::executable_format_error, | ||
| "out of bounds while reading .gnu.property.note section: %s", | ||
| toString(Cursor.takeError()).c_str()); | ||
|
|
||
| if (PrType == llvm::ELF::GNU_PROPERTY_AARCH64_FEATURE_1_AND) { | ||
| if (PrDataSz != 4) { | ||
| return createStringError( | ||
| errc::executable_format_error, | ||
| "Property descriptor size has to be 4 bytes on AArch64\n"); | ||
| } | ||
| DataExtractor::Cursor Tmp(PrDataStart); | ||
| // PrDataSz = 4 -> PrData is uint32_t | ||
| const uint32_t FeaturesItem = DE.getU32(Tmp); | ||
| if (!Tmp) | ||
| return createStringError( | ||
| errc::executable_format_error, | ||
| "out of bounds while reading .gnu.property.note section: %s", | ||
bgergely0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| toString(Tmp.takeError()).c_str()); | ||
| Features = Features ? (*Features | FeaturesItem) : FeaturesItem; | ||
| } | ||
|
|
||
| Cursor.seek(alignTo(PrDataEnd, Align)); | ||
| if (!Cursor) | ||
| return createStringError( | ||
| errc::executable_format_error, | ||
| "out of bounds while reading .gnu.property.note section: %s", | ||
| toString(Cursor.takeError()).c_str()); | ||
| } | ||
| return Features.value_or(0u); | ||
| } | ||
| } // namespace | ||
|
|
||
| std::unique_ptr<MetadataRewriter> | ||
| llvm::bolt::createGNUPropertyRewriter(BinaryContext &BC) { | ||
| return std::make_unique<GNUPropertyRewriter>("gnu-property-rewriter", BC); | ||
| } | ||
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,50 @@ | ||
| --- !ELF | ||
| FileHeader: | ||
| Class: ELFCLASS64 | ||
| Data: ELFDATA2LSB | ||
| Type: ET_EXEC | ||
| Machine: EM_AARCH64 | ||
| Entry: 0x400510 | ||
| ProgramHeaders: | ||
| - Type: PT_NOTE | ||
| Flags: [ PF_R ] | ||
| FirstSec: .note.gnu.property | ||
| LastSec: .note.gnu.property | ||
| VAddr: 0x400338 | ||
| Align: 0x8 | ||
| - Type: PT_LOAD | ||
| Flags: [ PF_R ] | ||
| VAddr: 0x0 | ||
| Align: 0x10000 | ||
| FileSize: 0xf8 | ||
| MemSize: 0xf8 | ||
| Offset: 0x0 | ||
| Sections: | ||
| - Name: .text | ||
| Type: SHT_PROGBITS | ||
| Flags: [ SHF_ALLOC, SHF_EXECINSTR ] | ||
| Address: 0x2a0000 | ||
| AddressAlign: 0x4 | ||
| Content: 400580d2c0035fd6 | ||
| - Name: .note.gnu.property | ||
| Type: SHT_NOTE | ||
| Flags: [ SHF_ALLOC ] | ||
| Address: 0x400338 | ||
| AddressAlign: 0x8 | ||
| Notes: | ||
| - Name: GNU | ||
| Desc: 000000C0040000000300000000000000 | ||
| Type: NT_GNU_PROPERTY_TYPE_0 | ||
| - Type: SectionHeaderTable | ||
| Sections: | ||
| - Name: .note.gnu.property | ||
| - Name: .symtab | ||
| - Name: .strtab | ||
| - Name: .shstrtab | ||
| - Name: .text | ||
| Symbols: | ||
| - Name: .note.gnu.property | ||
| Type: STT_SECTION | ||
| Section: .note.gnu.property | ||
| Value: 0x400338 | ||
| ... |
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,50 @@ | ||
| --- !ELF | ||
| FileHeader: | ||
| Class: ELFCLASS64 | ||
| Data: ELFDATA2LSB | ||
| Type: ET_EXEC | ||
| Machine: EM_AARCH64 | ||
| Entry: 0x400510 | ||
| ProgramHeaders: | ||
| - Type: PT_NOTE | ||
| Flags: [ PF_R ] | ||
| FirstSec: .note.gnu.property | ||
| LastSec: .note.gnu.property | ||
| VAddr: 0x400338 | ||
| Align: 0x8 | ||
| - Type: PT_LOAD | ||
| Flags: [ PF_R ] | ||
| VAddr: 0x0 | ||
| Align: 0x10000 | ||
| FileSize: 0xf8 | ||
| MemSize: 0xf8 | ||
| Offset: 0x0 | ||
| Sections: | ||
| - Name: .text | ||
| Type: SHT_PROGBITS | ||
| Flags: [ SHF_ALLOC, SHF_EXECINSTR ] | ||
| Address: 0x2a0000 | ||
| AddressAlign: 0x4 | ||
| Content: 400580d2c0035fd6 | ||
| - Name: .note.gnu.property | ||
| Type: SHT_NOTE | ||
| Flags: [ SHF_ALLOC ] | ||
| Address: 0x400338 | ||
| AddressAlign: 0x8 | ||
| Notes: | ||
| - Name: GNU | ||
| Desc: 000000C0040000000200000000000000 | ||
| Type: NT_GNU_PROPERTY_TYPE_0 | ||
| - Type: SectionHeaderTable | ||
| Sections: | ||
| - Name: .note.gnu.property | ||
| - Name: .symtab | ||
| - Name: .strtab | ||
| - Name: .shstrtab | ||
| - Name: .text | ||
| Symbols: | ||
| - Name: .note.gnu.property | ||
| Type: STT_SECTION | ||
| Section: .note.gnu.property | ||
| Value: 0x400338 | ||
| ... |
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,10 @@ | ||
| // This test checks that the GNUPropertyRewriter can decode the BTI feature flag. | ||
| // It decodes an executable with BTI, and checks for the warning. | ||
|
|
||
| RUN: yaml2obj %p/Inputs/property-note-bti.yaml &> %t.exe | ||
|
|
||
| RUN: llvm-readelf -n %t.exe | FileCheck %s | ||
| CHECK: BTI | ||
|
|
||
| RUN: llvm-bolt %t.exe -o %t.exe.bolt | FileCheck %s -check-prefix=CHECK-BOLT | ||
| CHECK-BOLT: BOLT-WARNING: binary is using BTI. | ||
bgergely0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,10 @@ | ||
| // This test checks that the GNUPropertyRewriter can decode the BTI feature flag. | ||
| // It decodes an executable without BTI, and checks for the warning. | ||
|
|
||
| RUN: yaml2obj %p/Inputs/property-note-nobti.yaml &> %t.exe | ||
|
|
||
| RUN: llvm-readelf -n %t.exe | FileCheck %s | ||
| CHECK-NOT: BTI | ||
|
|
||
| RUN: llvm-bolt %t.exe -o %t.exe.bolt | FileCheck %s -check-prefix=CHECK-BOLT | ||
| CHECK-BOLT-NOT: BOLT-WARNING: binary is using BTI. |
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.
Uh oh!
There was an error while loading. Please reload this page.