debuggers.hg
changeset 13659:4aa6044ab967
libelf: use for readnotes utility.
This patch makes the readnotes utility use libelf.
Signed-off-by: Gerd Hoffmann <kraxel@suse.de>
---
tools/xcutils/readnotes.c | 277 +++++++---------------------------------------
1 file changed, 45 insertions(+), 232 deletions(-)
This patch makes the readnotes utility use libelf.
Signed-off-by: Gerd Hoffmann <kraxel@suse.de>
---
tools/xcutils/readnotes.c | 277 +++++++---------------------------------------
1 file changed, 45 insertions(+), 232 deletions(-)
author | Emmanuel Ackaouy <ack@xensource.com> |
---|---|
date | Thu Jan 25 22:16:52 2007 +0000 (2007-01-25) |
parents | a63d1af01b3d |
children | 583441e296a1 |
files | tools/xcutils/readnotes.c |
line diff
1.1 --- a/tools/xcutils/readnotes.c Thu Jan 25 22:16:52 2007 +0000 1.2 +++ b/tools/xcutils/readnotes.c Thu Jan 25 22:16:52 2007 +0000 1.3 @@ -1,4 +1,3 @@ 1.4 -#include <elf.h> 1.5 #include <errno.h> 1.6 #include <fcntl.h> 1.7 #include <inttypes.h> 1.8 @@ -11,219 +10,35 @@ 1.9 #include <sys/stat.h> 1.10 #include <sys/mman.h> 1.11 1.12 -#include <xen/elfnote.h> 1.13 - 1.14 -#define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_))) 1.15 -#define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + (((_n_)->n_namesz+3)&~3)) 1.16 -#define ELFNOTE_NEXT(_n_) (ELFNOTE_DESC(_n_) + (((_n_)->n_descsz+3)&~3)) 1.17 - 1.18 -#ifndef ELFSIZE 1.19 -#include <limits.h> 1.20 -#if UINT_MAX == ULONG_MAX 1.21 -#define ELFSIZE 32 1.22 -#else 1.23 -#define ELFSIZE 64 1.24 -#endif 1.25 -#endif 1.26 +#include <xg_private.h> 1.27 1.28 -#if (ELFSIZE == 32) 1.29 -typedef Elf32_Nhdr Elf_Nhdr; 1.30 -typedef Elf32_Half Elf_Half; 1.31 -typedef Elf32_Word Elf_Word; 1.32 -#elif (ELFSIZE == 64) 1.33 -typedef Elf64_Nhdr Elf_Nhdr; 1.34 -typedef Elf64_Half Elf_Half; 1.35 -typedef Elf64_Word Elf_Word; 1.36 -#else 1.37 -#error "Unknown ELFSIZE" 1.38 -#endif 1.39 +#include <xen/libelf.h> 1.40 1.41 -static void print_string_note(const char *prefix, Elf_Nhdr *note) 1.42 -{ 1.43 - printf("%s: %s\n", prefix, (const char *)ELFNOTE_DESC(note)); 1.44 -} 1.45 - 1.46 -static void print_numeric_note(const char *prefix,Elf_Nhdr *note) 1.47 +static void print_string_note(const char *prefix, struct elf_binary *elf, 1.48 + const elf_note *note) 1.49 { 1.50 - switch (note->n_descsz) 1.51 - { 1.52 - case 4: 1.53 - printf("%s: %#010" PRIx32 " (4 bytes)\n", 1.54 - prefix, *(uint32_t *)ELFNOTE_DESC(note)); 1.55 - break; 1.56 - case 8: 1.57 - printf("%s: %#018" PRIx64 " (8 bytes)\n", 1.58 - prefix, *(uint64_t *)ELFNOTE_DESC(note)); 1.59 - break; 1.60 - default: 1.61 - printf("%s: unknown data size %#lx\n", prefix, 1.62 - (unsigned long)note->n_descsz); 1.63 - break; 1.64 - } 1.65 -} 1.66 - 1.67 -static inline int is_elf(void *image) 1.68 -{ 1.69 - /* 1.70 - * Since we are only accessing the e_ident field we can 1.71 - * acccess the bytes directly without needing to figure out 1.72 - * which version of Elf*_Ehdr structure to use. 1.73 - */ 1.74 - const unsigned char *hdr = image; 1.75 - return ( hdr[EI_MAG0] == ELFMAG0 && 1.76 - hdr[EI_MAG1] == ELFMAG1 && 1.77 - hdr[EI_MAG2] == ELFMAG2 && 1.78 - hdr[EI_MAG3] == ELFMAG3 ); 1.79 -} 1.80 - 1.81 -static inline unsigned char ehdr_class(void *image) 1.82 -{ 1.83 - /* 1.84 - * Since we are only accessing the e_ident field we can 1.85 - * acccess the bytes directly without needing to figure out 1.86 - * which version of Elf*_Ehdr structure to use. 1.87 - */ 1.88 - const unsigned char *hdr = image; 1.89 - switch (hdr[EI_CLASS]) 1.90 - { 1.91 - case ELFCLASS32: 1.92 - case ELFCLASS64: 1.93 - return hdr[EI_CLASS]; 1.94 - default: 1.95 - fprintf(stderr, "Unknown ELF class %d\n", hdr[EI_CLASS]); 1.96 - exit(1); 1.97 - } 1.98 -} 1.99 - 1.100 -static inline Elf_Half ehdr_shnum(void *image) 1.101 -{ 1.102 - switch (ehdr_class(image)) 1.103 - { 1.104 - case ELFCLASS32: 1.105 - return ((Elf32_Ehdr *)image)->e_shnum; 1.106 - case ELFCLASS64: 1.107 - return ((Elf64_Ehdr *)image)->e_shnum; 1.108 - default: 1.109 - exit(1); 1.110 - } 1.111 + printf("%s: %s\n", prefix, (char*)elf_note_desc(elf, note)); 1.112 } 1.113 1.114 -static inline Elf_Word shdr_type(void *image, int shnum) 1.115 +static void print_numeric_note(const char *prefix, struct elf_binary *elf, 1.116 + const elf_note *note) 1.117 { 1.118 - switch (ehdr_class(image)) 1.119 - { 1.120 - case ELFCLASS32: 1.121 - { 1.122 - Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image; 1.123 - Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff + 1.124 - (shnum*ehdr->e_shentsize)); 1.125 - return shdr->sh_type; 1.126 - } 1.127 - case ELFCLASS64: 1.128 - { 1.129 - Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image; 1.130 - Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff + 1.131 - (shnum*ehdr->e_shentsize)); 1.132 - return shdr->sh_type; 1.133 - } 1.134 - default: 1.135 - exit(1); 1.136 - } 1.137 -} 1.138 - 1.139 -static inline const char *shdr_name(void *image, int shnum) 1.140 -{ 1.141 - const char *shstrtab; 1.142 - 1.143 - switch (ehdr_class(image)) 1.144 - { 1.145 - case ELFCLASS32: 1.146 - { 1.147 - Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image; 1.148 - Elf32_Shdr *shdr; 1.149 - /* Find the section-header strings table. */ 1.150 - if ( ehdr->e_shstrndx == SHN_UNDEF ) 1.151 - return NULL; 1.152 - shdr = (Elf32_Shdr *)(image + ehdr->e_shoff + 1.153 - (ehdr->e_shstrndx*ehdr->e_shentsize)); 1.154 - shstrtab = image + shdr->sh_offset; 1.155 + uint64_t value = elf_note_numeric(elf, note); 1.156 + int descsz = elf_uval(elf, note, descsz); 1.157 1.158 - shdr= (Elf32_Shdr*)(image + ehdr->e_shoff + 1.159 - (shnum*ehdr->e_shentsize)); 1.160 - return &shstrtab[shdr->sh_name]; 1.161 - } 1.162 - case ELFCLASS64: 1.163 - { 1.164 - Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image; 1.165 - Elf64_Shdr *shdr; 1.166 - /* Find the section-header strings table. */ 1.167 - if ( ehdr->e_shstrndx == SHN_UNDEF ) 1.168 - return NULL; 1.169 - shdr = (Elf64_Shdr *)(image + ehdr->e_shoff + 1.170 - (ehdr->e_shstrndx*ehdr->e_shentsize)); 1.171 - shstrtab = image + shdr->sh_offset; 1.172 - 1.173 - shdr= (Elf64_Shdr*)(image + ehdr->e_shoff + 1.174 - (shnum*ehdr->e_shentsize)); 1.175 - return &shstrtab[shdr->sh_name]; 1.176 - } 1.177 - default: 1.178 - exit(1); 1.179 - } 1.180 -} 1.181 -static inline void *shdr_start(void *image, int shnum) 1.182 -{ 1.183 - switch (ehdr_class(image)) 1.184 - { 1.185 - case ELFCLASS32: 1.186 - { 1.187 - Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image; 1.188 - Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff + 1.189 - (shnum*ehdr->e_shentsize)); 1.190 - return image + shdr->sh_offset; 1.191 - } 1.192 - case ELFCLASS64: 1.193 - { 1.194 - Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image; 1.195 - Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff + 1.196 - (shnum*ehdr->e_shentsize)); 1.197 - return image + shdr->sh_offset; 1.198 - } 1.199 - default: 1.200 - exit(1); 1.201 - } 1.202 -} 1.203 - 1.204 -static inline void *shdr_end(void *image, int shnum) 1.205 -{ 1.206 - switch (ehdr_class(image)) 1.207 - { 1.208 - case ELFCLASS32: 1.209 - { 1.210 - Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image; 1.211 - Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff + 1.212 - (shnum*ehdr->e_shentsize)); 1.213 - return image + shdr->sh_offset + shdr->sh_size; 1.214 - } 1.215 - case ELFCLASS64: 1.216 - { 1.217 - Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image; 1.218 - Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff + 1.219 - (shnum*ehdr->e_shentsize)); 1.220 - return image + shdr->sh_offset + shdr->sh_size; 1.221 - } 1.222 - default: 1.223 - exit(1); 1.224 - } 1.225 + printf("%s: %#*" PRIx64 " (%d bytes)\n", 1.226 + prefix, 2+2*descsz, value, descsz); 1.227 } 1.228 1.229 int main(int argc, char **argv) 1.230 { 1.231 const char *f; 1.232 - int fd,h; 1.233 + int fd,h,size,count; 1.234 void *image; 1.235 struct stat st; 1.236 - Elf_Nhdr *note; 1.237 + struct elf_binary elf; 1.238 + const elf_shdr *shdr; 1.239 + const elf_note *note, *end; 1.240 1.241 if (argc != 2) 1.242 { 1.243 @@ -251,76 +66,74 @@ int main(int argc, char **argv) 1.244 fprintf(stderr, "Unable to map %s: %s\n", f, strerror(errno)); 1.245 return 1; 1.246 } 1.247 + size = st.st_size; 1.248 1.249 - if ( !is_elf(image) ) 1.250 + if (0 != elf_init(&elf, image, size)) 1.251 { 1.252 fprintf(stderr, "File %s is not an ELF image\n", f); 1.253 return 1; 1.254 } 1.255 + elf_set_logfile(&elf, stderr, 0); 1.256 1.257 - for ( h=0; h < ehdr_shnum(image); h++) 1.258 + count = elf_shdr_count(&elf); 1.259 + for ( h=0; h < count; h++) 1.260 { 1.261 - if (shdr_type(image,h) != SHT_NOTE) 1.262 + shdr = elf_shdr_by_index(&elf, h); 1.263 + if (elf_uval(&elf, shdr, sh_type) != SHT_NOTE) 1.264 continue; 1.265 - for (note = (Elf_Nhdr*)shdr_start(image,h); 1.266 - note < (Elf_Nhdr*)shdr_end(image,h); 1.267 - note = (Elf_Nhdr*)(ELFNOTE_NEXT(note))) 1.268 + end = elf_section_end(&elf, shdr); 1.269 + for (note = elf_section_start(&elf, shdr); 1.270 + note < end; 1.271 + note = elf_note_next(&elf, note)) 1.272 { 1.273 - switch(note->n_type) 1.274 + if (0 != strcmp(elf_note_name(&elf, note), "Xen")) 1.275 + continue; 1.276 + switch(elf_uval(&elf, note, type)) 1.277 { 1.278 case XEN_ELFNOTE_INFO: 1.279 - print_string_note("INFO", note); 1.280 + print_string_note("INFO", &elf , note); 1.281 break; 1.282 case XEN_ELFNOTE_ENTRY: 1.283 - print_numeric_note("ENTRY", note); 1.284 + print_numeric_note("ENTRY", &elf , note); 1.285 break; 1.286 case XEN_ELFNOTE_HYPERCALL_PAGE: 1.287 - print_numeric_note("HYPERCALL_PAGE", note); 1.288 + print_numeric_note("HYPERCALL_PAGE", &elf , note); 1.289 break; 1.290 case XEN_ELFNOTE_VIRT_BASE: 1.291 - print_numeric_note("VIRT_BASE", note); 1.292 + print_numeric_note("VIRT_BASE", &elf , note); 1.293 break; 1.294 case XEN_ELFNOTE_PADDR_OFFSET: 1.295 - print_numeric_note("PADDR_OFFSET", note); 1.296 + print_numeric_note("PADDR_OFFSET", &elf , note); 1.297 break; 1.298 case XEN_ELFNOTE_XEN_VERSION: 1.299 - print_string_note("XEN_VERSION", note); 1.300 + print_string_note("XEN_VERSION", &elf , note); 1.301 break; 1.302 case XEN_ELFNOTE_GUEST_OS: 1.303 - print_string_note("GUEST_OS", note); 1.304 + print_string_note("GUEST_OS", &elf , note); 1.305 break; 1.306 case XEN_ELFNOTE_GUEST_VERSION: 1.307 - print_string_note("GUEST_VERSION", note); 1.308 + print_string_note("GUEST_VERSION", &elf , note); 1.309 break; 1.310 case XEN_ELFNOTE_LOADER: 1.311 - print_string_note("LOADER", note); 1.312 + print_string_note("LOADER", &elf , note); 1.313 break; 1.314 case XEN_ELFNOTE_PAE_MODE: 1.315 - print_string_note("PAE_MODE", note); 1.316 + print_string_note("PAE_MODE", &elf , note); 1.317 break; 1.318 case XEN_ELFNOTE_FEATURES: 1.319 - print_string_note("FEATURES", note); 1.320 + print_string_note("FEATURES", &elf , note); 1.321 break; 1.322 default: 1.323 - printf("unknown note type %#lx\n", 1.324 - (unsigned long)note->n_type); 1.325 + printf("unknown note type %#x\n", 1.326 + (int)elf_uval(&elf, note, type)); 1.327 break; 1.328 } 1.329 } 1.330 } 1.331 1.332 - for ( h=0; h < ehdr_shnum(image); h++) 1.333 - { 1.334 - const char *name = shdr_name(image,h); 1.335 - 1.336 - if ( name == NULL ) 1.337 - continue; 1.338 - if ( strcmp(name, "__xen_guest") != 0 ) 1.339 - continue; 1.340 - 1.341 - printf("__xen_guest: %s\n", (const char *)shdr_start(image, h)); 1.342 - break; 1.343 - } 1.344 + shdr = elf_shdr_by_name(&elf, "__xen_guest"); 1.345 + if (shdr) 1.346 + printf("__xen_guest: %s\n", (char*)elf_section_start(&elf, shdr)); 1.347 1.348 return 0; 1.349 }