Skip to content

Commit

Permalink
ARM: Implement search for public (aeabi) attributes subsection
Browse files Browse the repository at this point in the history
get_cpu_arm() scans through the ".ARM.attributes" section of the
ELF file to find the "aeabi" subsection. This subsection contains
general build attributes that records data about the compatibility
of the ELF file.
  • Loading branch information
valdaarhun committed Feb 7, 2024
1 parent c025dce commit 374bb57
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion librz/bin/format/elf/elf_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,55 @@ static char *get_cpu_mips(ELFOBJ *bin) {
return strdup(" Unknown mips ISA");
}

static char *read_arm_attributes_section(char *ptr, ut32 bytes_to_read, bool isbig) {
/* TODO */
}

/**
* Processor-specific section types explained in:
* 1. https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst
* 2. https://github.com/ARM-software/abi-aa/blob/main/addenda32/addenda32.rst
*/
static char *get_cpu_arm(ELFOBJ *bin) {
RzBinElfSection *section = Elf_(rz_bin_elf_get_section_with_name)(bin, ".ARM.attributes");
if (!section) {
return strdup(" Unknown arm ISA");
}

ut64 offset = section->offset;
ut64 size = section->size;
if (size < 1) {
return strdup(" Unknown arm ISA");
}

char *result = malloc(size + 1);
if (!result) {
return strdup(" Unknown arm ISA");
}

if (rz_buf_read_at(bin->b, offset, (ut8 *)result, size) < 1) {
free(result);
return strdup(" Unknown arm ISA");
}

char *subsection_ptr = result + 1; // Point to the first subsection (first byte is the format-version)
bool isbig = Elf_(rz_bin_elf_is_big_endian)(bin);

while (subsection_ptr - result < size) {
char *ptr = subsection_ptr;
ut32 subsection_size = rz_read_ble32(ptr, isbig);
ptr += 4;

if (!strcmp(ptr, "aeabi")) {
ptr += strlen("aeabi") + 1; // +1 for the null byte
return read_arm_attributes_section(ptr, subsection_size - 10, isbig);
}
subsection_ptr += subsection_size;
}

return strdup(" Unknown arm ISA");
}

static bool is_elf_class64(ELFOBJ *bin) {
return bin->ehdr.e_ident[EI_CLASS] == ELFCLASS64;
}
Expand Down Expand Up @@ -1459,7 +1508,7 @@ RZ_OWN char *Elf_(rz_bin_elf_get_arch)(RZ_NONNULL ELFOBJ *bin) {
* \param elf type
* \return allocated string
*
* Only work on mips right now. Use the elf header to deduce the cpu
* Only work on mips and arm right now. Use the elf header to deduce the cpu
*/
RZ_OWN char *Elf_(rz_bin_elf_get_cpu)(RZ_NONNULL ELFOBJ *bin) {
rz_return_val_if_fail(bin, NULL);
Expand All @@ -1470,6 +1519,8 @@ RZ_OWN char *Elf_(rz_bin_elf_get_cpu)(RZ_NONNULL ELFOBJ *bin) {

if (bin->ehdr.e_machine == EM_MIPS) {
return get_cpu_mips(bin);
} else if (bin->ehdr.e_machine == EM_ARM) {
return get_cpu_arm(bin);
}

return NULL;
Expand Down

0 comments on commit 374bb57

Please sign in to comment.