Skip to content
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

[RISCV] add test for GNU note property and CFI support #589

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions elftools/elf/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ def describe_note_gnu_properties(properties, machine):
prop_desc = ' <corrupt length: 0x%x>' % sz
else:
prop_desc = describe_note_gnu_property_bitmap_and(_DESCR_NOTE_GNU_PROPERTY_AARCH64_FEATURE_1_AND, 'aarch64 feature', d)
elif t == 'GNU_PROPERTY_AARCH64_FEATURE_1_AND' and machine == 'EM_RISCV':
# RISC-V shares the same bit-mask with AArch64
if sz != 4:
prop_desc = ' <corrupt length: 0x%x>' % sz
else:
prop_desc = describe_note_gnu_property_bitmap_and(_DESCR_NOTE_GNU_PROPERTY_RISCV_FEATURE_1_AND, 'RISC-V AND feature', d)
elif _DESCR_NOTE_GNU_PROPERTY_TYPE_LOPROC <= t <= _DESCR_NOTE_GNU_PROPERTY_TYPE_HIPROC:
prop_desc = '<processor-specific type 0x%x data: %s >' % (t, bytes2hex(d, sep=' '))
elif _DESCR_NOTE_GNU_PROPERTY_TYPE_LOUSER <= t <= _DESCR_NOTE_GNU_PROPERTY_TYPE_HIUSER:
Expand Down Expand Up @@ -675,6 +681,11 @@ def describe_note_gnu_properties(properties, machine):
(2, 'pac'),
)

_DESCR_NOTE_GNU_PROPERTY_RISCV_FEATURE_1_AND = (
(1, 'ZICFILP'),
(2, 'ZICFISS'),
)

def _reverse_dict(d, low_priority=()):
"""
This is a tiny helper function to "reverse" the keys/values of a dictionary
Expand Down
5 changes: 4 additions & 1 deletion elftools/elf/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ def classify_pr_data(ctx):
if ctx.pr_type.startswith('GNU_PROPERTY_X86_'):
return ('GNU_PROPERTY_X86_*', 4, 0)
elif ctx.pr_type.startswith('GNU_PROPERTY_AARCH64_'):
return ('GNU_PROPERTY_AARCH64_*', 4, 0)
return ('GNU_PROPERTY_AARCH64_*', 4, 0)
elif ctx.pr_type.startswith('GNU_PROPERTY_RISCV_'):
return ('GNU_PROPERTY_RISCV_*', 4, 0)
return (ctx.pr_type, ctx.pr_datasz, self.elfclass)

self.Elf_Prop = Struct('Elf_Prop',
Expand All @@ -418,6 +420,7 @@ def classify_pr_data(ctx):
('GNU_PROPERTY_STACK_SIZE', 8, 64): self.Elf_word64('pr_data'),
('GNU_PROPERTY_X86_*', 4, 0): self.Elf_word('pr_data'),
('GNU_PROPERTY_AARCH64_*', 4, 0): self.Elf_word('pr_data'),
('GNU_PROPERTY_RISCV_*', 4, 0): self.Elf_word('pr_data'),
},
default=Field('pr_data', lambda ctx: ctx.pr_datasz)
),
Expand Down
28 changes: 28 additions & 0 deletions test/testfiles_for_readelf/riscv-cfi-zicfilp-zicfiss.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Compile with
# riscv64-unknown-linux-gnu-gcc riscv-cfi-zicfilp-zicfiss.S -c -o riscv-cfi-zicfilp-zicfiss.elf
#define GNU_PROPERTY_RISCV_FEATURE_1_AND 0xc0000000
#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP (1 << 0)
#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS (1 << 1)

.text
.globl _start
.type _start,@function
_start:
ret

.section ".note.gnu.property", "a", @note
.p2align 3
.long 1f - 0f /* name length */
.long 5f - 2f /* data length */
.long 5 /* note type */
0: .asciz "GNU" /* vendor name */
1:
.p2align 3
2: .long GNU_PROPERTY_RISCV_FEATURE_1_AND /* pr_type. */
.long 4f - 3f /* pr_datasz. */
3:
.long (GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP | GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS)
4:
.p2align 3
5:
end:
Binary file not shown.
Loading