From c40dce1d70fa96c41f82a0311be679201a5968e2 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Mon, 18 Aug 2025 18:56:11 +0100 Subject: [PATCH] Support 64-bit ELF objects --- ranlib.c | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 143 insertions(+), 12 deletions(-) diff --git a/ranlib.c b/ranlib.c index ad6c180..3c5227d 100644 --- a/ranlib.c +++ b/ranlib.c @@ -1,6 +1,7 @@ /****************************************************************************** * @file ranlib.c *****************************************************************************/ +#include #include #include #include @@ -68,7 +69,7 @@ struct coff_symbol { }; -struct elf_exec { +struct elf32_exec { unsigned char e_ident[16]; unsigned char e_type[2]; @@ -87,7 +88,26 @@ struct elf_exec { }; -struct elf_shdr { +struct elf64_exec { + + unsigned char e_ident[16]; + unsigned char e_type[2]; + unsigned char e_machine[2]; + unsigned char e_version[4]; + unsigned char e_entry[8]; + unsigned char e_phoff[8]; + unsigned char e_shoff[8]; + unsigned char e_flags[4]; + unsigned char e_ehsize[2]; + unsigned char e_phentsize[2]; + unsigned char e_phnum[2]; + unsigned char e_shentsize[2]; + unsigned char e_shnum[2]; + unsigned char e_shstrndx[2]; + +}; + +struct elf32_shdr { unsigned char sh_name[4]; unsigned char sh_type[4]; @@ -102,7 +122,22 @@ struct elf_shdr { }; -struct elf_sym { +struct elf64_shdr { + + unsigned char sh_name[4]; + unsigned char sh_type[4]; + unsigned char sh_flags[8]; + unsigned char sh_addr[8]; + unsigned char sh_offset[8]; + unsigned char sh_size[8]; + unsigned char sh_link[4]; + unsigned char sh_info[4]; + unsigned char sh_addralign[8]; + unsigned char sh_entsize[8]; + +}; + +struct elf32_sym { unsigned char st_name[4]; unsigned char st_value[4]; @@ -113,6 +148,17 @@ struct elf_sym { }; +struct elf64_sym { + + unsigned char st_name[4]; + unsigned char st_info[1]; + unsigned char st_other[1]; + unsigned char st_shndx[2]; + unsigned char st_value[8]; + unsigned char st_size[8]; + +}; + struct strtab { const char *name; @@ -265,9 +311,13 @@ static void coff_get_symbols (void *object, long offset) { #define GET_ELF_UINT32(arr) (endianess ? (((uint32_t) arr[0] << 24) | (((uint32_t) arr[1]) << 16) | (((uint32_t) arr[2]) << 8) | (((uint32_t) arr[3]))) \ : ((uint32_t) arr[0] | (((uint32_t) arr[1]) << 8) | (((uint32_t) arr[2]) << 16) | (((uint32_t) arr[3]) << 24))) -static void elf_get_symbols (void *object, long offset, int endianess) { +#define GET_ELF_UINT64(arr) \ + (endianess ? (((uint_fast64_t) arr[0]) << 56) | (((uint_fast64_t) arr[1]) << 48) | (((uint_fast64_t) arr[2]) << 40) | (((uint_fast64_t) arr[3]) >> 32) | (((uint_fast64_t) arr[4]) << 24) | (((uint_fast64_t) arr[5]) << 16) | (((uint_fast64_t) arr[6]) << 8) | (((uint_fast64_t) arr[7])) \ + : ((uint_fast64_t) arr[0]) | (((uint_fast64_t) arr[1]) << 8) | (((uint_fast64_t) arr[2]) << 16) | (((uint_fast64_t) arr[3]) << 24) | (((uint_fast64_t) arr[4]) << 32) | (((uint_fast64_t) arr[5]) << 40) | (((uint_fast64_t) arr[6]) << 48) | (((uint_fast64_t) arr[7]) << 56)) + +static void elf32_get_symbols (void *object, long offset, int endianess) { - struct elf_exec *hdr = (struct elf_exec *) object; + struct elf32_exec *hdr = (struct elf32_exec *) object; unsigned long e_shnum = GET_ELF_UINT16 (hdr->e_shnum); unsigned long e_shoff = GET_ELF_UINT32 (hdr->e_shoff); @@ -276,9 +326,9 @@ static void elf_get_symbols (void *object, long offset, int endianess) { unsigned long sh_link, sh_offset, sh_entsize, sh_size; unsigned long sym_strtab_size, i, j, st_name; - struct elf_shdr strtabhdr; - struct elf_shdr shdr; - struct elf_sym elf_symbol; + struct elf32_shdr strtabhdr; + struct elf32_shdr shdr; + struct elf32_sym elf_symbol; struct strtab *strtab; char *sym_strtab; @@ -343,6 +393,84 @@ static void elf_get_symbols (void *object, long offset, int endianess) { } +static void elf64_get_symbols (void *object, long offset, int endianess) { + + struct elf64_exec *hdr = (struct elf64_exec *) object; + + unsigned long e_shnum = GET_ELF_UINT16 (hdr->e_shnum); + unsigned long e_shoff = GET_ELF_UINT64 (hdr->e_shoff); + unsigned long e_shentsize = GET_ELF_UINT16 (hdr->e_shentsize); + + unsigned long sh_link, sh_offset, sh_entsize, sh_size; + unsigned long sym_strtab_size, i, j, st_name; + + struct elf64_shdr strtabhdr; + struct elf64_shdr shdr; + struct elf64_sym elf_symbol; + + struct strtab *strtab; + char *sym_strtab; + + for (i = 1; i < e_shnum; i++) { + + memcpy (&shdr, (char *) object + e_shoff + i * e_shentsize, sizeof (shdr)); + + if (GET_ELF_UINT32 (shdr.sh_type) != 2) { + continue; + } + + sh_link = GET_ELF_UINT32 (shdr.sh_link); + sh_offset = GET_ELF_UINT64 (shdr.sh_offset); + + if (sh_link == 0 || sh_link >= e_shnum) { + continue; + } + + memcpy (&strtabhdr, (char *) object + e_shoff + sh_link * e_shentsize, sizeof (strtabhdr)); + + if (GET_ELF_UINT32 (strtabhdr.sh_type) != 3) { + continue; + } + + sym_strtab_size = GET_ELF_UINT64 (strtabhdr.sh_size); + sym_strtab = (char *) object + GET_ELF_UINT64 (strtabhdr.sh_offset); + + if ((sh_entsize = GET_ELF_UINT64 (shdr.sh_entsize)) < sizeof (elf_symbol)) { + continue; + } + + sh_size = GET_ELF_UINT64 (shdr.sh_size); + + for (j = 1; j < sh_size / sh_entsize; j++) { + + memcpy (&elf_symbol, (char *) object + sh_offset + j * sh_entsize, sizeof (elf_symbol)); + + if ((st_name = GET_ELF_UINT32 (elf_symbol.st_name)) >= sym_strtab_size) { + continue; + } + + if (GET_ELF_UINT16 (elf_symbol.st_shndx) == 0 || (elf_symbol.st_info[0] >> 4) != 1) { + continue; + } + + if (sym_strtab[st_name] != '\0') { + + strtab = xmalloc (sizeof (*strtab)); + strtab->offset = offset; + + strtab->name = xstrdup (sym_strtab + st_name); + strtab->length = strlen (strtab->name); + + add_strtab (&gstrtab, strtab); + + } + + } + + } + +} + unsigned long array_to_be_integer (unsigned char *arr, int size) { unsigned long value = 0; @@ -693,11 +821,13 @@ void ranlib (void) { } if (object[0] == 0x7f && memcmp (object + 1, "ELF", 3) == 0) { - - if (object[5] == 2) { - elf_get_symbols (object, offset + 8, 1); + + int endianess = (object[5] == 2); + + if (object[4] == 2) { + elf64_get_symbols (object, offset + 8, endianess); } else { - elf_get_symbols (object, offset + 8, 0); + elf32_get_symbols (object, offset + 8, endianess); } free (object); @@ -981,3 +1111,4 @@ void ranlib (void) { fclose (tfp); } + -- 2.34.1