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

elf: add read and write support for GNU properties #541

Merged
merged 5 commits into from
May 5, 2023
Merged
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
89 changes: 83 additions & 6 deletions crates/examples/src/readobj/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ fn print_segment_notes<Elf: FileHeader>(
p: &mut Printer<'_>,
endian: Elf::Endian,
data: &[u8],
_elf: &Elf,
elf: &Elf,
segment: &Elf::ProgramHeader,
) {
if let Some(Some(notes)) = segment.notes(endian, data).print_err(p) {
print_notes(p, endian, notes);
print_notes(p, endian, elf, notes);
}
}

Expand Down Expand Up @@ -497,11 +497,11 @@ fn print_section_notes<Elf: FileHeader>(
p: &mut Printer<'_>,
endian: Elf::Endian,
data: &[u8],
_elf: &Elf,
elf: &Elf,
section: &Elf::SectionHeader,
) {
if let Some(Some(notes)) = section.notes(endian, data).print_err(p) {
print_notes(p, endian, notes);
print_notes(p, endian, elf, notes);
}
}

Expand Down Expand Up @@ -551,6 +551,7 @@ fn print_section_group<Elf: FileHeader>(
fn print_notes<Elf: FileHeader>(
p: &mut Printer<'_>,
endian: Elf::Endian,
elf: &Elf,
mut notes: NoteIterator<Elf>,
) {
while let Some(Some(note)) = notes.next().print_err(p) {
Expand All @@ -568,8 +569,57 @@ fn print_notes<Elf: FileHeader>(
&[]
};
p.field_enum("Type", note.n_type(endian), flags);
// TODO: interpret desc
p.field_bytes("Desc", note.desc());
if let Some(mut properties) = note.gnu_properties(endian) {
while let Some(Some(property)) = properties.next().print_err(p) {
p.group("Property", |p| {
let pr_type = property.pr_type();
let proc = match elf.e_machine(endian) {
EM_386 | EM_X86_64 => FLAGS_GNU_PROPERTY_X86,
EM_AARCH64 => FLAGS_GNU_PROPERTY_AARCH64,
_ => &[],
};
p.field_enums("Type", pr_type, &[FLAGS_GNU_PROPERTY, proc]);
match pr_type {
GNU_PROPERTY_1_NEEDED => {
if let Some(val) = property.data_u32(endian).print_err(p) {
p.field_hex("Value", val);
p.flags(val, 0, FLAGS_GNU_PROPERTY_1_NEEDED);
}
}
_ => {}
}
match elf.e_machine(endian) {
EM_386 | EM_X86_64 => match pr_type {
GNU_PROPERTY_X86_ISA_1_USED | GNU_PROPERTY_X86_ISA_1_NEEDED => {
if let Some(val) = property.data_u32(endian).print_err(p) {
p.field_hex("Value", val);
p.flags(val, 0, FLAGS_GNU_PROPERTY_X86_ISA_1);
}
}
GNU_PROPERTY_X86_FEATURE_1_AND => {
if let Some(val) = property.data_u32(endian).print_err(p) {
p.field_hex("Value", val);
p.flags(val, 0, FLAGS_GNU_PROPERTY_X86_FEATURE_1);
}
}
_ => {}
},
EM_AARCH64 => match pr_type {
GNU_PROPERTY_AARCH64_FEATURE_1_AND => {
if let Some(val) = property.data_u32(endian).print_err(p) {
p.field_hex("Value", val);
p.flags(val, 0, FLAGS_GNU_PROPERTY_AARCH64_FEATURE_1);
}
}
_ => {}
},
_ => {}
}
});
}
} else {
p.field_bytes("Desc", note.desc());
}
});
}
}
Expand Down Expand Up @@ -3185,6 +3235,33 @@ static FLAGS_NT_GNU: &[Flag<u32>] = &flags!(
NT_GNU_GOLD_VERSION,
NT_GNU_PROPERTY_TYPE_0,
);
static FLAGS_GNU_PROPERTY: &[Flag<u32>] = &flags!(
GNU_PROPERTY_STACK_SIZE,
GNU_PROPERTY_NO_COPY_ON_PROTECTED,
GNU_PROPERTY_1_NEEDED,
);
static FLAGS_GNU_PROPERTY_1_NEEDED: &[Flag<u32>] =
&flags!(GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS);
static FLAGS_GNU_PROPERTY_AARCH64: &[Flag<u32>] = &flags!(GNU_PROPERTY_AARCH64_FEATURE_1_AND);
static FLAGS_GNU_PROPERTY_AARCH64_FEATURE_1: &[Flag<u32>] = &flags!(
GNU_PROPERTY_AARCH64_FEATURE_1_BTI,
GNU_PROPERTY_AARCH64_FEATURE_1_PAC,
);
static FLAGS_GNU_PROPERTY_X86: &[Flag<u32>] = &flags!(
GNU_PROPERTY_X86_ISA_1_USED,
GNU_PROPERTY_X86_ISA_1_NEEDED,
GNU_PROPERTY_X86_FEATURE_1_AND,
);
static FLAGS_GNU_PROPERTY_X86_ISA_1: &[Flag<u32>] = &flags!(
GNU_PROPERTY_X86_ISA_1_BASELINE,
GNU_PROPERTY_X86_ISA_1_V2,
GNU_PROPERTY_X86_ISA_1_V3,
GNU_PROPERTY_X86_ISA_1_V4,
);
static FLAGS_GNU_PROPERTY_X86_FEATURE_1: &[Flag<u32>] = &flags!(
GNU_PROPERTY_X86_FEATURE_1_IBT,
GNU_PROPERTY_X86_FEATURE_1_SHSTK,
);
static FLAGS_GRP: &[Flag<u32>] = &flags!(GRP_COMDAT);
static FLAGS_DT: &[Flag<u32>] = &flags!(
DT_NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
SectionHeader {
Index: 8
Name: ".note.gnu.property" (0x52)
Type: SHT_NOTE (0x7)
Flags: 0x2
SHF_ALLOC (0x2)
Address: 0x0
Offset: 0xA8
Size: 0x20
Link: 0
Info: 0
AddressAlign: 0x8
EntrySize: 0x0
Note {
Name: "GNU" (0x4)
Type: NT_GNU_PROPERTY_TYPE_0 (0x5)
Property {
Type: GNU_PROPERTY_AARCH64_FEATURE_1_AND (0xC0000000)
Value: 0x3
GNU_PROPERTY_AARCH64_FEATURE_1_BTI (0x1)
GNU_PROPERTY_AARCH64_FEATURE_1_PAC (0x2)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
SectionHeader {
Index: 8
Name: ".note.gnu.property" (0x52)
Type: SHT_NOTE (0x7)
Flags: 0x2
SHF_ALLOC (0x2)
Address: 0x0
Offset: 0xA0
Size: 0x20
Link: 0
Info: 0
AddressAlign: 0x8
EntrySize: 0x0
Note {
Name: "GNU" (0x4)
Type: NT_GNU_PROPERTY_TYPE_0 (0x5)
Property {
Type: GNU_PROPERTY_X86_FEATURE_1_AND (0xC0000002)
Value: 0x3
GNU_PROPERTY_X86_FEATURE_1_IBT (0x1)
GNU_PROPERTY_X86_FEATURE_1_SHSTK (0x2)
}
}
}
Loading