WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-changelog

[Xen-changelog] [xen-unstable] [TOOLS] Use ELFSIZE to pick the ELF struc

To: xen-changelog@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-changelog] [xen-unstable] [TOOLS] Use ELFSIZE to pick the ELF structures to use in readnotes.c
From: Xen patchbot-unstable <patchbot-unstable@xxxxxxxxxxxxxxxxxxx>
Date: Fri, 25 Aug 2006 17:00:39 +0000
Delivery-date: Fri, 25 Aug 2006 10:01:21 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-changelog-request@lists.xensource.com?subject=help>
List-id: BK change log <xen-changelog.lists.xensource.com>
List-post: <mailto:xen-changelog@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.xensource.com?subject=unsubscribe>
Reply-to: xen-devel@xxxxxxxxxxxxxxxxxxx
Sender: xen-changelog-bounces@xxxxxxxxxxxxxxxxxxx
# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxxxxx>
# Node ID 9091331dfb353212781622f3c9020492cb049178
# Parent  23a0a408edb9f0675eaec0493c9063c19a14b9cb
[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@xxxxxxxxxxxxx>
---
 tools/xcutils/readnotes.c |   61 ++++++++++++++++++++++++++++++----------------
 1 files changed, 40 insertions(+), 21 deletions(-)

diff -r 23a0a408edb9 -r 9091331dfb35 tools/xcutils/readnotes.c
--- a/tools/xcutils/readnotes.c Fri Aug 25 10:06:24 2006 +0100
+++ b/tools/xcutils/readnotes.c Fri Aug 25 10:39:24 2006 +0100
@@ -17,18 +17,25 @@
 #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + (((_n_)->n_namesz+3)&~3))
 #define ELFNOTE_NEXT(_n_) (ELFNOTE_DESC(_n_) + (((_n_)->n_descsz+3)&~3))
 
-#if defined(__i386__)
-typedef Elf32_Ehdr Elf_Ehdr;
+#ifndef ELFSIZE
+#include <limits.h>
+#if UINT_MAX == ULONG_MAX
+#define ELFSIZE 32
+#else
+#define ELFSIZE 64
+#endif
+#endif
+
+#if (ELFSIZE == 32)
 typedef Elf32_Nhdr Elf_Nhdr;
 typedef Elf32_Half Elf_Half;
 typedef Elf32_Word Elf_Word;
-#elif defined(__x86_64__)
-typedef Elf64_Ehdr Elf_Ehdr;
+#elif (ELFSIZE == 64)
 typedef Elf64_Nhdr Elf_Nhdr;
 typedef Elf64_Half Elf_Half;
 typedef Elf64_Word Elf_Word;
 #else
-#error "Unknown architecture"
+#error "Unknown ELFSIZE"
 #endif
 
 static void print_string_note(const char *prefix, Elf_Nhdr *note)
@@ -54,18 +61,35 @@ static void print_numeric_note(const cha
        }
 }
 
+static inline int is_elf(void *image)
+{
+       /*
+        * Since we are only accessing the e_ident field we can
+        * acccess the bytes directly without needing to figure out
+        * which version of Elf*_Ehdr structure to use.
+        */
+       const unsigned char *hdr = image;
+       return ( hdr[EI_MAG0] == ELFMAG0 &&
+                hdr[EI_MAG1] == ELFMAG1 &&
+                hdr[EI_MAG2] == ELFMAG2 &&
+                hdr[EI_MAG3] == ELFMAG3 );
+}
+
 static inline unsigned char ehdr_class(void *image)
 {
-       Elf_Ehdr *ehdr = image;
-       switch (ehdr->e_ident[EI_CLASS])
-       {
-       case ELFCLASS32:
-       case ELFCLASS64:
-               return ehdr->e_ident[EI_CLASS];
-               break;
-       default:
-               fprintf(stderr, "Unknown ELF class %d\n",
-                       ehdr->e_ident[EI_CLASS]);
+       /*
+        * Since we are only accessing the e_ident field we can
+        * acccess the bytes directly without needing to figure out
+        * which version of Elf*_Ehdr structure to use.
+        */
+       const unsigned char *hdr = image;
+       switch (hdr[EI_CLASS])
+       {
+       case ELFCLASS32:
+       case ELFCLASS64:
+               return hdr[EI_CLASS];
+       default:
+               fprintf(stderr, "Unknown ELF class %d\n", hdr[EI_CLASS]);
                exit(1);
        }
 }
@@ -198,7 +222,6 @@ int main(int argc, char **argv)
        int fd,h;
        void *image;
        struct stat st;
-       Elf_Ehdr *ehdr;
        Elf_Nhdr *note;
 
        if (argc != 2)
@@ -228,11 +251,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       ehdr = image;
-       if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
-           ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
-           ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
-           ehdr->e_ident[EI_MAG3] != ELFMAG3)
+       if ( !is_elf(image) )
        {
                fprintf(stderr, "File %s is not an ELF image\n", f);
                return 1;

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog

<Prev in Thread] Current Thread [Next in Thread>
  • [Xen-changelog] [xen-unstable] [TOOLS] Use ELFSIZE to pick the ELF structures to use in readnotes.c, Xen patchbot-unstable <=