From 27d501fdbf0c6932e6170e8dece4d178d912bf94 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 10 Jan 2009 21:02:48 +0000 Subject: simple optimizations and style fixes in dynamic loading text data bss dec hex filename - 16709 240 92 17041 4291 lib/ld-uClibc.so + 16634 236 92 16962 4242 lib/ld-uClibc.so - 4602 344 4 4950 1356 lib/libdl-0.9.30-svn.so + 4571 328 4 4903 1327 lib/libdl-0.9.30-svn.so - 4602 344 4 4950 1356 lib/libdl.so + 4571 328 4 4903 1327 lib/libdl.so --- utils/ldd.c | 20 ++++++++------------ utils/readelf.c | 33 +++++++++++++++------------------ 2 files changed, 23 insertions(+), 30 deletions(-) (limited to 'utils') diff --git a/utils/ldd.c b/utils/ldd.c index 17bab2079..289d163b2 100644 --- a/utils/ldd.c +++ b/utils/ldd.c @@ -238,31 +238,27 @@ static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic) int check_elf_header(ElfW(Ehdr) *const ehdr) { - if (!ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 || - ehdr->e_ident[EI_CLASS] != ELFCLASSM || - ehdr->e_ident[EI_VERSION] != EV_CURRENT) - { + if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32 + || ehdr->e_ident[EI_CLASS] != ELFCLASSM + || ehdr->e_ident[EI_VERSION] != EV_CURRENT + ) { return 1; } /* Check if the target endianness matches the host's endianness */ byteswap = 0; #if __BYTE_ORDER == __LITTLE_ENDIAN - if (ehdr->e_ident[5] == ELFDATA2MSB) { - /* Ick -- we will have to byte-swap everything */ + if (ehdr->e_ident[5] == ELFDATA2MSB) byteswap = 1; - } #elif __BYTE_ORDER == __BIG_ENDIAN - if (ehdr->e_ident[5] == ELFDATA2LSB) { - /* Ick -- we will have to byte-swap everything */ + if (ehdr->e_ident[5] == ELFDATA2LSB) byteswap = 1; - } #else #error Unknown host byte order! #endif - /* Be vary lazy, and only byteswap the stuff we use */ - if (byteswap == 1) { + /* Be very lazy, and only byteswap the stuff we use */ + if (byteswap) { ehdr->e_type = bswap_16(ehdr->e_type); ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff); ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff); diff --git a/utils/readelf.c b/utils/readelf.c index 2af4b5ca9..60f14a6bf 100644 --- a/utils/readelf.c +++ b/utils/readelf.c @@ -93,36 +93,33 @@ static void * elf_find_dynamic( int64_t const key, ElfW(Dyn) *dynp, static int check_elf_header(ElfW(Ehdr) *const ehdr) { - if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 || - (ehdr->e_ident[EI_CLASS] != ELFCLASS32 && - ehdr->e_ident[EI_CLASS] != ELFCLASS64) || - ehdr->e_ident[EI_VERSION] != EV_CURRENT) - { + if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32 + || (ehdr->e_ident[EI_CLASS] != ELFCLASS32 + && ehdr->e_ident[EI_CLASS] != ELFCLASS64) + || ehdr->e_ident[EI_VERSION] != EV_CURRENT + ) { return 1; } /* Check if the target endianness matches the host's endianness */ byteswap = 0; #if __BYTE_ORDER == __LITTLE_ENDIAN - if (ehdr->e_ident[5] == ELFDATA2MSB) { - /* Ick -- we will have to byte-swap everything */ + if (ehdr->e_ident[5] == ELFDATA2MSB) byteswap = 1; - } #elif __BYTE_ORDER == __BIG_ENDIAN - if (ehdr->e_ident[5] == ELFDATA2LSB) { + if (ehdr->e_ident[5] == ELFDATA2LSB) byteswap = 1; - } #else #error Unknown host byte order! #endif - /* Be vary lazy, and only byteswap the stuff we use */ - if (byteswap==1) { - ehdr->e_type=bswap_16(ehdr->e_type); - ehdr->e_machine=bswap_16(ehdr->e_machine); - ehdr->e_phoff=byteswap_to_host(ehdr->e_phoff); - ehdr->e_shoff=byteswap_to_host(ehdr->e_shoff); - ehdr->e_phnum=bswap_16(ehdr->e_phnum); - ehdr->e_shnum=bswap_16(ehdr->e_shnum); + /* Be very lazy, and only byteswap the stuff we use */ + if (byteswap) { + ehdr->e_type = bswap_16(ehdr->e_type); + ehdr->e_machine = bswap_16(ehdr->e_machine); + ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff); + ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff); + ehdr->e_phnum = bswap_16(ehdr->e_phnum); + ehdr->e_shnum = bswap_16(ehdr->e_shnum); } return 0; } -- cgit v1.2.3