debuggers.hg
changeset 11294:9091331dfb35
[TOOLS] Use ELFSIZE to pick the ELF structures to use in readnotes.c
We can remove Elf_Ehdr since it is only used for e_ident which is an
unsigned char array.
Signed-off-by: Ian Campbell <ian.campbell@xensource.com>
We can remove Elf_Ehdr since it is only used for e_ident which is an
unsigned char array.
Signed-off-by: Ian Campbell <ian.campbell@xensource.com>
author | Ian Campbell <ian.campbell@xensource.com> |
---|---|
date | Fri Aug 25 10:39:24 2006 +0100 (2006-08-25) |
parents | 23a0a408edb9 |
children | 86d26e6ec89b |
files | tools/xcutils/readnotes.c |
line diff
1.1 --- a/tools/xcutils/readnotes.c Fri Aug 25 10:06:24 2006 +0100 1.2 +++ b/tools/xcutils/readnotes.c Fri Aug 25 10:39:24 2006 +0100 1.3 @@ -17,18 +17,25 @@ 1.4 #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + (((_n_)->n_namesz+3)&~3)) 1.5 #define ELFNOTE_NEXT(_n_) (ELFNOTE_DESC(_n_) + (((_n_)->n_descsz+3)&~3)) 1.6 1.7 -#if defined(__i386__) 1.8 -typedef Elf32_Ehdr Elf_Ehdr; 1.9 +#ifndef ELFSIZE 1.10 +#include <limits.h> 1.11 +#if UINT_MAX == ULONG_MAX 1.12 +#define ELFSIZE 32 1.13 +#else 1.14 +#define ELFSIZE 64 1.15 +#endif 1.16 +#endif 1.17 + 1.18 +#if (ELFSIZE == 32) 1.19 typedef Elf32_Nhdr Elf_Nhdr; 1.20 typedef Elf32_Half Elf_Half; 1.21 typedef Elf32_Word Elf_Word; 1.22 -#elif defined(__x86_64__) 1.23 -typedef Elf64_Ehdr Elf_Ehdr; 1.24 +#elif (ELFSIZE == 64) 1.25 typedef Elf64_Nhdr Elf_Nhdr; 1.26 typedef Elf64_Half Elf_Half; 1.27 typedef Elf64_Word Elf_Word; 1.28 #else 1.29 -#error "Unknown architecture" 1.30 +#error "Unknown ELFSIZE" 1.31 #endif 1.32 1.33 static void print_string_note(const char *prefix, Elf_Nhdr *note) 1.34 @@ -54,18 +61,35 @@ static void print_numeric_note(const cha 1.35 } 1.36 } 1.37 1.38 +static inline int is_elf(void *image) 1.39 +{ 1.40 + /* 1.41 + * Since we are only accessing the e_ident field we can 1.42 + * acccess the bytes directly without needing to figure out 1.43 + * which version of Elf*_Ehdr structure to use. 1.44 + */ 1.45 + const unsigned char *hdr = image; 1.46 + return ( hdr[EI_MAG0] == ELFMAG0 && 1.47 + hdr[EI_MAG1] == ELFMAG1 && 1.48 + hdr[EI_MAG2] == ELFMAG2 && 1.49 + hdr[EI_MAG3] == ELFMAG3 ); 1.50 +} 1.51 + 1.52 static inline unsigned char ehdr_class(void *image) 1.53 { 1.54 - Elf_Ehdr *ehdr = image; 1.55 - switch (ehdr->e_ident[EI_CLASS]) 1.56 + /* 1.57 + * Since we are only accessing the e_ident field we can 1.58 + * acccess the bytes directly without needing to figure out 1.59 + * which version of Elf*_Ehdr structure to use. 1.60 + */ 1.61 + const unsigned char *hdr = image; 1.62 + switch (hdr[EI_CLASS]) 1.63 { 1.64 case ELFCLASS32: 1.65 case ELFCLASS64: 1.66 - return ehdr->e_ident[EI_CLASS]; 1.67 - break; 1.68 + return hdr[EI_CLASS]; 1.69 default: 1.70 - fprintf(stderr, "Unknown ELF class %d\n", 1.71 - ehdr->e_ident[EI_CLASS]); 1.72 + fprintf(stderr, "Unknown ELF class %d\n", hdr[EI_CLASS]); 1.73 exit(1); 1.74 } 1.75 } 1.76 @@ -198,7 +222,6 @@ int main(int argc, char **argv) 1.77 int fd,h; 1.78 void *image; 1.79 struct stat st; 1.80 - Elf_Ehdr *ehdr; 1.81 Elf_Nhdr *note; 1.82 1.83 if (argc != 2) 1.84 @@ -228,11 +251,7 @@ int main(int argc, char **argv) 1.85 return 1; 1.86 } 1.87 1.88 - ehdr = image; 1.89 - if (ehdr->e_ident[EI_MAG0] != ELFMAG0 || 1.90 - ehdr->e_ident[EI_MAG1] != ELFMAG1 || 1.91 - ehdr->e_ident[EI_MAG2] != ELFMAG2 || 1.92 - ehdr->e_ident[EI_MAG3] != ELFMAG3) 1.93 + if ( !is_elf(image) ) 1.94 { 1.95 fprintf(stderr, "File %s is not an ELF image\n", f); 1.96 return 1;