Skip to contentSkip to conversation

libelf: new elf parser for xen

PATCH In review Gerd Hoffmann 3 participants 13 messages Jan 23, 2007 – Jan 25, 2007

lore
Share & tools
lore
#0 lore raw

cover libelf: new elf parser for xen

Hi,

These patches add a new elf parser to xen. It can be used by both xen kernel (dom0 builder) and the tools (domU builder). The first patch adds the code to the tree, the following patches put it into use. Detailed descriptions come with the individual patches. The elf/linux domain builder rewrite comming ReallySoonNow[tm] will use the new elf parser as well.

cheers,
Gerd

#1 lore raw

PATCH 1/5 add libelf: an ELF binary parser library.

This patch adds a library with a small collection of helper functions to parse and load elf binaries. The library handles endianess and elfsize at runtime.

The patch also shuffles around the include files a bit. Now there is *one* include file holding all the elf structures (xen/include/public/elfstructs.h) which is included by everyone who needs them.

It's dead code with this patch only, putting the code into use happens in followup patches.

---
 tools/libxc/xc_elf.h                |  525 -----------------------------------
 xen/arch/x86/boot/mkelf32.c         |    2 
 xen/common/Makefile                 |    2 
 xen/common/libelf/Makefile          |    4 
 xen/common/libelf/README            |    1 
 xen/common/libelf/libelf-dominfo.c  |  420 ++++++++++++++++++++++++++++
 xen/common/libelf/libelf-loader.c   |  156 ++++++++++
 xen/common/libelf/libelf-private.h  |   51 +++
 xen/common/libelf/libelf-relocate.c |  345 +++++++++++++++++++++++
 xen/common/libelf/libelf-tools.c    |  225 +++++++++++++++
 xen/include/public/elfstructs.h     |  527 ++++++++++++++++++++++++++++++++++++
 xen/include/public/libelf.h         |  238 ++++++++++++++++
 xen/include/xen/elf.h               |  490 ---------------------------------
 13 files changed, 1972 insertions(+), 1014 deletions(-)
xen/common/Makefile +2 -0
Index: build-32-unstable-13377/xen/common/Makefile
===================================================================
@@ -37,6 +37,8 @@ obj-$(CONFIG_XENCOMM) += xencomm.o
 
 subdir-$(CONFIG_COMPAT) += compat
 
+subdir-y += libelf
+
 # Object file contains changeset and compiler information.
 version.o: $(BASEDIR)/include/xen/compile.h
 
xen/common/libelf/Makefile new file +4 -0
Index: build-32-unstable-13377/xen/common/libelf/Makefile
===================================================================
@@ -0,0 +1,4 @@
+obj-y += libelf-tools.o
+obj-y += libelf-loader.o
+obj-y += libelf-dominfo.o
+#obj-y += libelf-relocate.o
xen/common/libelf/README new file +1 -0
Index: build-32-unstable-13377/xen/common/libelf/README
===================================================================
@@ -0,0 +1 @@
+Take care, this code is used by both xen and tools ...
xen/common/libelf/libelf-dominfo.c new file +420 -0

420 lines not on this page · raw message

xen/common/libelf/libelf-loader.c new file +156 -0

156 lines not on this page · raw message

xen/common/libelf/libelf-private.h new file +51 -0

51 lines not on this page · raw message

xen/common/libelf/libelf-relocate.c new file +345 -0

345 lines not on this page · raw message

xen/common/libelf/libelf-tools.c new file +225 -0

225 lines not on this page · raw message

xen/include/public/elfstructs.h new file +527 -0

527 lines not on this page · raw message

xen/include/public/libelf.h new file +238 -0

238 lines not on this page · raw message

tools/libxc/xc_elf.h +1 -524

525 lines not on this page · raw message

xen/include/xen/elf.h +1 -489

496 lines not on this page · raw message

xen/arch/x86/boot/mkelf32.c +1 -1
Index: build-32-unstable-13377/xen/arch/x86/boot/mkelf32.c
===================================================================
@@ -25,7 +25,7 @@
 #define s16 int16_t
 #define s32 int32_t
 #define s64 int64_t
-#include "../../../include/xen/elf.h"
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#2 lore raw

Re: [patch 1/5] add libelf: an ELF binary parser library.

On Tuesday 23 January 2007 15:53, Gerd Hoffmann wrote:

[...]

Quoted 14 lines
---
 tools/libxc/xc_elf.h                |  525
----------------------------------- xen/arch/x86/boot/mkelf32.c         |  
 2
 xen/common/Makefile                 |    2
 xen/common/libelf/Makefile          |    4
 xen/common/libelf/README            |    1
 xen/common/libelf/libelf-dominfo.c  |  420 ++++++++++++++++++++++++++++
 xen/common/libelf/libelf-loader.c   |  156 ++++++++++
 xen/common/libelf/libelf-private.h  |   51 +++
 xen/common/libelf/libelf-relocate.c |  345 +++++++++++++++++++++++
 xen/common/libelf/libelf-tools.c    |  225 +++++++++++++++
 xen/include/public/elfstructs.h     |  527

[...]

How about putting libelf into xen/lib/libelf ?
When we also move the libc files from xen/common/
to xen/lib/libc, then we have this structure:

xen/lib/libelf
xen/lib/libc

This is a better structured IMO.

Christoph Egger

#3 lore raw

Re: [patch 1/5] add libelf: an ELF binary parser library.

In reply to Gerd Hoffmann

Hi Gerd,

I have to say I'm not a big fan of sticking a 32bit and 64bit version of each structure in a union and then checking at all levels of the function call stack which one to reference (via elf_xval() for example).

Wouldn't it be cleaner to just have 2 different version of each elf data structure and function? elf32_xxx and elf64_xxx . And go down one path or the other at the start of your function call stack? I'm not usually a fan of multiple compilation, but due to the layout of 32 and 64bit elf structures, this is one case where it makes sense to me.

Also, do we actually have a need to look at elf files with a different ELFDATA (byte ordering) ?

Quoted 32 lines · Gerd Hoffmann

This patch adds a library with a small collection of helper functions to parse and load elf binaries. The library handles endianess and elfsize at runtime.

The patch also shuffles around the include files a bit. Now there is *one* include file holding all the elf structures (xen/include/public/elfstructs.h) which is included by everyone who needs them.

It's dead code with this patch only, putting the code into use happens in followup patches.

---
 tools/libxc/xc_elf.h                |  525 
-----------------------------------
 xen/arch/x86/boot/mkelf32.c         |    2
 xen/common/Makefile                 |    2
 xen/common/libelf/Makefile          |    4
 xen/common/libelf/README            |    1
 xen/common/libelf/libelf-dominfo.c  |  420 
++++++++++++++++++++++++++++
 xen/common/libelf/libelf-loader.c   |  156 ++++++++++
 xen/common/libelf/libelf-private.h  |   51 +++
 xen/common/libelf/libelf-relocate.c |  345 +++++++++++++++++++++++
 xen/common/libelf/libelf-tools.c    |  225 +++++++++++++++
 xen/include/public/elfstructs.h     |  527 
++++++++++++++++++++++++++++++++++++
 xen/include/public/libelf.h         |  238 ++++++++++++++++
 xen/include/xen/elf.h               |  490 
---------------------------------
 13 files changed, 1972 insertions(+), 1014 deletions(-)
#4 lore raw

Re: [patch 1/5] add libelf: an ELF binary parser library.

Quoted 9 lines · Emmanuel Ackaouy

Hi Gerd,

I have to say I'm not a big fan of sticking a 32bit and 64bit version of each structure in a union and then checking at all levels of the function call stack which one to reference (via elf_xval() for example).

Wouldn't it be cleaner to just have 2 different version of each elf data structure and function? elf32_xxx and elf64_xxx . And go down one path or the other at the start of your function call stack?

No.

Quoted 4 lines

I'm not
usually a fan of multiple compilation, but due to the layout of 32 and 64bit elf structures, this is one case where it makes sense to me.

It doesn't. I did it both ways. Not compiling twice is better.

It is much more flexible and makes reusing the code much easier. You can simply add your own functions without hassle.

Look at the libelf-use-readnotes.diff patch and the print_numeric_note() function it adds to readnotes.c as an simple example. How do you do that when compiling twice? Your options are:

(1) push that into the library which is compiled twice anyway.
(2) split it out into a separate source which you can compile twice.
(3) write two versions of the function, one for 32 and one for 64 bit.

None of these options looks good to me.

Quoted 2 lines

Also, do we actually have a need to look at elf files with a different ELFDATA (byte ordering) ?

Yes. I think it is very handy if readnotes works on ppc kernels too. Also some IA64 guys seem to work on mixed-endian support (there where some messages on the list some weeks ago), for this it is required too.

cheers,
Gerd

Signature · 1 line
Gerd Hoffmann <[email protected]>
#5 lore raw

PATCH 2/5 libelf: use for x86 dom0 builder.

In reply to Gerd Hoffmann

This patch switches the x86 dom0 builder over to libelf.

---
 xen/arch/ia64/xen/domain.c  |   79 +++++------
 xen/arch/x86/domain_build.c |  303 +++++++++++++++-----------------------------
 xen/common/Makefile         |    4 
 3 files changed, 148 insertions(+), 238 deletions(-)
xen/common/Makefile +2 -2
Index: build-ia64-unstable-13282/xen/common/Makefile
===================================================================
@@ -2,8 +2,8 @@ obj-y += acm_ops.o
 obj-y += bitmap.o
 obj-y += domctl.o
 obj-y += domain.o
-obj-y += elf.o
-obj-$(CONFIG_COMPAT) += elf32.o
+#obj-y += elf.o
+#obj-$(CONFIG_COMPAT) += elf32.o
 obj-y += event_channel.o
 obj-y += grant_table.o
 obj-y += kernel.o
xen/arch/x86/domain_build.c +107 -196

515 lines not on this page · raw message

xen/arch/ia64/xen/domain.c +39 -40

133 lines not on this page · raw message

#6 lore raw

Re: [patch 2/5] libelf: use for x86 dom0 builder.

See comments below:

Quoted 15 lines · Gerd Hoffmann

This patch switches the x86 dom0 builder over to libelf.

---
 xen/arch/ia64/xen/domain.c  |   79 +++++------
 xen/arch/x86/domain_build.c |  303 
+++++++++++++++-----------------------------
 xen/common/Makefile         |    4
 3 files changed, 148 insertions(+), 238 deletions(-)

Index: build-ia64-unstable-13282/xen/common/Makefile
===================================================================

xen/common/Makefile obj-y += acm_ops.o · line 6

Quoted 7 lines
xen/common/Makefile +2 -2
@@ -2,8 +2,8 @@ obj-y += acm_ops.o
 obj-y += bitmap.o
 obj-y += domctl.o
 obj-y += domain.o
-obj-y += elf.o
-obj-$(CONFIG_COMPAT) += elf32.o
+#obj-y += elf.o
+#obj-$(CONFIG_COMPAT) += elf32.o

Can we just zap those lines?

Quoted 10 lines
xen/common/Makefile +0 -0
 obj-y += event_channel.o
 obj-y += grant_table.o
 obj-y += kernel.o

Index: build-ia64-unstable-13282/xen/arch/x86/domain_build.c
===================================================================

xen/arch/x86/domain_build.c +0 -0
@@ -13,7 +13,6 @@
 #include <xen/delay.h>
 

[snip]

Quoted 65 lines

-
int construct_dom0(struct domain *d,
unsigned long _image_start, unsigned long
image_len,
unsigned long _initrd_start, unsigned long
initrd_len,
char *cmdline)
{

xen/arch/x86/domain_build.c +1 -3
-    int i, rc, dom0_pae, xen_pae, order;
+    int i, rc, compatible, compat32, order, machine;
     struct cpu_user_regs *regs;
     unsigned long pfn, mfn;
     unsigned long nr_pages;
@@ -263,9 +205,7 @@ int construct_dom0(struct domain *d,
     struct page_info *page = NULL;
     start_info_t *si;
     struct vcpu *v = d->vcpu[0];
-    const char *p;
     unsigned long long value;
-    int value_defined;
 #if defined(__i386__)
     char *image_start  = (char *)_image_start;  /* use lowmem 

mappings */
char *initrd_start = (char *)_initrd_start; /* use lowmem
mappings */

xen/arch/x86/domain_build.c +0 -0
@@ -287,7 +227,10 @@ int construct_dom0(struct domain *d,
      * *_start address are page-aligned, except v_start (and v_end) 

which are
* superpage-aligned.
*/

xen/arch/x86/domain_build.c +9 -6
-    struct domain_setup_info dsi;
+    struct elf_binary elf;
+    struct elf_dom_parms parms;
+    unsigned long vkern_start;
+    unsigned long vkern_end;
     unsigned long vinitrd_start;
     unsigned long vinitrd_end;
     unsigned long vphysmap_start;
@@ -298,6 +241,7 @@ int construct_dom0(struct domain *d,
     unsigned long vstack_end;
     unsigned long vpt_start;
     unsigned long vpt_end;
+    unsigned long v_start;
     unsigned long v_end;
 
     /* Machine address of next candidate page-table page. */
@@ -312,21 +256,71 @@ int construct_dom0(struct domain *d,
     BUG_ON(d->vcpu[0] == NULL);
     BUG_ON(test_bit(_VCPUF_initialised, &v->vcpu_flags));
 
-    memset(&dsi, 0, sizeof(struct domain_setup_info));
-    dsi.image_addr = (unsigned long)image_start;
-    dsi.image_len  = image_len;
-
     printk("*** LOADING DOMAIN 0 ***\n");
 
     d->max_pages = ~0U;
 
     nr_pages = compute_dom0_nr_pages();
 
-    rc = parseelfimage(&dsi);
+    if (0 != (rc = elf_init(&elf, image_start, image_len)))
+        return rc;
+#ifdef VERBOSE
+    elf_set_verbose(&elf);

xen/arch/x86/domain_build.c int construct_dom0(struct domain *d, · line 278

Quoted 10 lines
xen/arch/x86/domain_build.c +10 -0
+#endif
+    elf_parse_binary(&elf);
+    if (0 != (elf_xen_parse(&elf, &parms)))
+        return rc;
+
+    /* compatibility check */
+    compatible = 0;
+    compat32   = 0;
+    machine = elf_uval(&elf, elf.ehdr, e_machine);
+    switch (CONFIG_PAGING_LEVELS) {

Can we make this a compile time check instead of run-time? Also, it would seem easier to do all the checks first and then do a printk specifying which kernel we found and, if it's not compatible with the hypervisor, why not.

Quoted 63 lines
xen/arch/x86/domain_build.c +44 -9
+    case 2: /* x86_32 */
+        if (parms.pae == PAEKERN_bimodal)
+            parms.pae = PAEKERN_no;
+        printk(" Xen  kernel: 32-bit, lsb\n");
+        if (elf_32bit(&elf) && !parms.pae && machine == EM_386)
+            compatible = 1;
+        break;
+    case 3: /* x86_32p */
+        if (parms.pae == PAEKERN_bimodal)
+            parms.pae = PAEKERN_extended_cr3;
+        printk(" Xen  kernel: 32-bit, PAE, lsb\n");
+        if (elf_32bit(&elf) && parms.pae && machine == EM_386)
+            compatible = 1;
+        break;
+    case 4: /* x86_64 */
+#ifndef CONFIG_COMPAT
+        printk(" Xen  kernel: 64-bit, lsb\n");
+#else
+        printk(" Xen  kernel: 64-bit, lsb, compat32\n");
+        if (elf_32bit(&elf) && parms.pae == PAEKERN_bimodal)
+            parms.pae = PAEKERN_extended_cr3;
+        if (elf_32bit(&elf) && parms.pae && machine == EM_386)
+        {
+            compat32 = 1;
+            compatible = 1;
+        }
+#endif
+        if (elf_64bit(&elf) && machine == EM_X86_64)
+            compatible = 1;
+        break;
+    }
+    printk(" Dom0 kernel: %s%s, %s, paddr 0x%" PRIx64 " -> 0x%" 
 PRIx64 "\n",
+           elf_64bit(&elf) ? "64-bit" : "32-bit",
+           parms.pae       ? ", PAE"  : "",
+           elf_msb(&elf)   ? "msb"    : "lsb",
+           elf.pstart, elf.pend);
+
+    if ( !compatible )
+    {
+        printk("Mismatch between Xen and DOM0 kernel\n");
+        return -EINVAL;
+    }
+
 #ifdef CONFIG_COMPAT
-    if ( rc == -ENOSYS
-         && !compat_disabled
-         && (rc = parseelf32image(&dsi)) == 0 )
+    if (compat32)
     {
         l1_pgentry_t gdt_l1e;
@@ -348,42 +342,10 @@ int construct_dom0(struct domain *d,
         local_flush_tlb_one(GDT_LDT_VIRT_START + 
 FIRST_RESERVED_GDT_BYTE);
     }
 #endif
-    if ( rc != 0)
-    {
-        if ( rc == -ENOSYS )
-            printk("DOM0 image is not a Xen-compatible Elf image.\n");
-       return rc;
-    }

[snip]

#7 lore raw

Re: [patch 2/5] libelf: use for x86 dom0 builder.

Hi,

Quoted 6 lines

xen/common/Makefile obj-y += acm_ops.o · line 6

Quoted 4 lines
xen/common/Makefile +2 -2
-obj-y += elf.o
-obj-$(CONFIG_COMPAT) += elf32.o
+#obj-y += elf.o
+#obj-$(CONFIG_COMPAT) += elf32.o

Can we just zap those lines?

Once ia64 is tested and ppc dom0 builder is ported over to libelf we can.

Quoted 7 lines

xen/arch/x86/domain_build.c int construct_dom0(struct domain *d, · line 278

Quoted 5 lines
xen/arch/x86/domain_build.c +5 -0
+    /* compatibility check */
+    compatible = 0;
+    compat32   = 0;
+    machine = elf_uval(&elf, elf.ehdr, e_machine);
+    switch (CONFIG_PAGING_LEVELS) {

Can we make this a compile time check instead of run-time?

CONFIG_PAGING_LEVELS is a constant, thus it actually is compile-time, the gcc optimizer should throw away the unused code paths. I prefer this way over cluttering the source with #ifdefs.

Quoted 4 lines

Also, it
would seem easier to do all the checks first and then do a printk specifying which kernel we found and, if it's not compatible with the hypervisor, why not.

I intentionally print kernel and xen type unconditionally. I think it is useful to have that information in the log, even if the combination is not incompatible.

cheers,
Gerd

Signature · 1 line
Gerd Hoffmann <[email protected]>
#8 lore raw

Re: [patch 2/5] libelf: use for x86 dom0 builder.

Quoted 10 lines · Gerd Hoffmann

Hi,

Quoted 6 lines

xen/common/Makefile obj-y += acm_ops.o · line 6

Quoted 4 lines
xen/common/Makefile +2 -2
-obj-y += elf.o
-obj-$(CONFIG_COMPAT) += elf32.o
+#obj-y += elf.o
+#obj-$(CONFIG_COMPAT) += elf32.o

Can we just zap those lines?

Once ia64 is tested and ppc dom0 builder is ported over to libelf we can.

Can you explain what ia64 or ppc have to do with lines that are commented out?

Quoted 11 lines
Quoted 7 lines

xen/arch/x86/domain_build.c int construct_dom0(struct domain *d, · line 278

Quoted 5 lines
xen/arch/x86/domain_build.c +5 -0
+    /* compatibility check */
+    compatible = 0;
+    compat32   = 0;
+    machine = elf_uval(&elf, elf.ehdr, e_machine);
+    switch (CONFIG_PAGING_LEVELS) {

Can we make this a compile time check instead of run-time?

CONFIG_PAGING_LEVELS is a constant, thus it actually is compile-time, the gcc optimizer should throw away the unused code paths. I prefer this way over cluttering the source with #ifdefs.

I'm not sure I follow. Are you suggesting we replace ifdefs with run time checks because the compiler will deal with it?

Quoted 8 lines
Quoted 4 lines

Also, it
would seem easier to do all the checks first and then do a printk specifying which kernel we found and, if it's not compatible with the hypervisor, why not.

I intentionally print kernel and xen type unconditionally. I think it is useful to have that information in the log, even if the combination is not incompatible.

I didn't suggest printing nothing when the combination is compatible.

#9 lore raw

Re: [patch 2/5] libelf: use for x86 dom0 builder.

Quoted 9 lines · Emmanuel Ackaouy
Quoted 6 lines
Quoted 4 lines

xen/common/Makefile obj-y += acm_ops.o · line 6

Quoted 2 lines
xen/common/Makefile +2 -0
+#obj-y += elf.o
+#obj-$(CONFIG_COMPAT) += elf32.o

Can we just zap those lines?

Once ia64 is tested and ppc dom0 builder is ported over to libelf we can.

Can you explain what ia64 or ppc have to do with lines that are commented out?

ia64 should work in theory, cross-compiling worked last time I tried, didn't boot it though due to lack of hardware.

ppc(64) needs adaptions, simliar to the changes in xen/arch/x86/domain_build.c and xen/arch/ia64/xen/domain.c

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
xen/arch/x86/domain_build.c +5 -0
+    /* compatibility check */
+    compatible = 0;
+    compat32   = 0;
+    machine = elf_uval(&elf, elf.ehdr, e_machine);
+    switch (CONFIG_PAGING_LEVELS) {
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Can we make this a compile time check instead of run-time?

CONFIG_PAGING_LEVELS is a constant, thus it actually is compile-time, the gcc optimizer should throw away the unused code paths. I prefer this way over cluttering the source with #ifdefs.

I'm not sure I follow. Are you suggesting we replace ifdefs with run time checks because the compiler will deal with it?

Yes.

CONFIG_COMPAT is partly handled that way too: with compat=n the IS_COMPAT() macro is defined to 0, and gcc will optimize out the conditional code which can run with compat=y only.

Quoted 10 lines
Quoted 8 lines
Quoted 4 lines

Also, it
would seem easier to do all the checks first and then do a printk specifying which kernel we found and, if it's not compatible with the hypervisor, why not.

I intentionally print kernel and xen type unconditionally. I think it is useful to have that information in the log, even if the combination is not incompatible.

I didn't suggest printing nothing when the combination is compatible.

I don't see the point in rearranging the code then.

cheers,
Gerd

Signature · 1 line
Gerd Hoffmann <[email protected]>
#10 lore raw

PATCH 3/5 libelf: add to libxc

In reply to Gerd Hoffmann

This patch makes libelf available to the tools, by symlinking the source files and compiling them into libxc.

---
 tools/libxc/Makefile |   15 +++++++++++++++
 1 file changed, 15 insertions(+)
tools/libxc/Makefile +15 -0
Index: build-32-unstable-13553/tools/libxc/Makefile
===================================================================
@@ -30,6 +30,21 @@ GUEST_SRCS-$(CONFIG_IA64) += xc_linux_bu
 GUEST_SRCS-$(CONFIG_MIGRATE) += xc_linux_restore.c xc_linux_save.c
 GUEST_SRCS-$(CONFIG_HVM) += xc_hvm_build.c xc_hvm_restore.c xc_hvm_save.c
 
+# symlink libelf from xen/common/libelf/
+LIBELF_SRCS := libelf-tools.c libelf-loader.c
+LIBELF_SRCS += libelf-dominfo.c libelf-relocate.c
+
+libelf-tools.o: libelf-tools.c libelf-private.h
+libelf-loader.o: libelf-loader.c libelf-private.h
+libelf-dominfo.o: libelf-dominfo.c libelf-private.h
+libelf-relocate.o: libelf-relocate.c libelf-private.h
+
+$(LIBELF_SRCS) libelf-private.h:
+	ln -s ../../xen/common/libelf/$@ $@
+
+# add libelf bits to libxc
+GUEST_SRCS-y += $(LIBELF_SRCS)
+
 -include $(XEN_TARGET_ARCH)/Makefile
 
 CFLAGS   += -Werror -Wmissing-prototypes
#11 lore raw

PATCH 4/5 libelf: use for hvm builder.

In reply to Gerd Hoffmann

This patch switches over the hvm domain builder to libelf (for loading hvmloader).

---
 tools/libxc/xc_hvm_build.c |  220 +++++++++++++--------------------------------
 1 file changed, 65 insertions(+), 155 deletions(-)
tools/libxc/xc_hvm_build.c +65 -155
Index: build-32-unstable-12802/tools/libxc/xc_hvm_build.c
===================================================================
@@ -2,29 +2,22 @@
  * xc_hvm_build.c
  */
 
-#define ELFSIZE 32
 #include <stddef.h>
 #include <inttypes.h>
-#include "xg_private.h"
-#include "xc_private.h"
-#include "xc_elf.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <zlib.h>
+
+#include "xg_private.h"
+#include "xc_private.h"
+
 #include <xen/hvm/hvm_info_table.h>
 #include <xen/hvm/params.h>
 #include <xen/hvm/e820.h>
 
-#define SCRATCH_PFN 0xFFFFF
+#include <xen/libelf.h>
 
-#define HVM_LOADER_ENTR_ADDR  0x00100000
-static int
-parseelfimage(
-    char *elfbase, unsigned long elfsize, struct domain_setup_info *dsi);
-static int
-loadelfimage(
-    char *elfbase, int xch, uint32_t dom, unsigned long *parray,
-    struct domain_setup_info *dsi);
+#define SCRATCH_PFN 0xFFFFF
 
 int xc_set_hvm_param(
     int handle, domid_t dom, int param, unsigned long value)
@@ -144,6 +137,48 @@ static void build_e820map(void *e820_pag
     *(((unsigned char *)e820_page) + E820_MAP_NR_OFFSET) = nr_map;
 }
 
+static int
+loadelfimage(struct elf_binary *elf, int xch, uint32_t dom, unsigned long *parray)
+{
+    privcmd_mmap_entry_t *entries = NULL;
+    int pages = (elf->pend - elf->pstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
+    int i, rc = -1;
+
+    /* map hvmloader address space */
+    entries = malloc(pages * sizeof(privcmd_mmap_entry_t));
+    if (NULL == entries)
+        goto err;
+    elf->dest = mmap(NULL, pages << PAGE_SHIFT, PROT_READ | PROT_WRITE,
+                     MAP_SHARED, xch, 0);
+    if (MAP_FAILED == elf->dest)
+        goto err;
+
+    for (i = 0; i < pages; i++)
+    {
+        entries[i].va = (uintptr_t)elf->dest + (i << PAGE_SHIFT);
+        entries[i].mfn = parray[(elf->pstart >> PAGE_SHIFT) + i];
+        entries[i].npages = 1;
+    }
+    rc = xc_map_foreign_ranges(xch, dom, entries, pages);
+    if (rc < 0)
+        goto err;
+
+    /* load hvmloader */
+    elf_load_binary(elf);
+    rc = 0;
+
+ err:
+    /* cleanup */
+    if (elf->dest) {
+        munmap(elf->dest, pages << PAGE_SHIFT);
+        elf->dest = NULL;
+    }
+    if (entries)
+        free(entries);
+
+    return rc;
+}
+
 static int setup_guest(int xc_handle,
                        uint32_t dom, int memsize,
                        char *image, unsigned long image_size,
@@ -155,35 +190,35 @@ static int setup_guest(int xc_handle,
     struct xen_add_to_physmap xatp;
     struct shared_info *shared_info;
     void *e820_page;
-    struct domain_setup_info dsi;
-    uint64_t v_end;
+    struct elf_binary elf;
+    uint64_t v_start, v_end;
     int rc;
 
-    memset(&dsi, 0, sizeof(struct domain_setup_info));
-
-    if ( (parseelfimage(image, image_size, &dsi)) != 0 )
+    if (0 != elf_init(&elf, image, image_size))
         goto error_out;
+    elf_parse_binary(&elf);
+    v_start = 0;
+    v_end = (unsigned long long)memsize << 20;
 
-    if ( (dsi.v_kernstart & (PAGE_SIZE - 1)) != 0 )
+    if ( (elf.pstart & (PAGE_SIZE - 1)) != 0 )
     {
         PERROR("Guest OS must load to a page boundary.\n");
         goto error_out;
     }
 
-    v_end = (unsigned long long)memsize << 20;
-
     IPRINTF("VIRTUAL MEMORY ARRANGEMENT:\n"
-           "  Loaded HVM loader:    %016"PRIx64"->%016"PRIx64"\n"
-           "  TOTAL:                %016"PRIx64"->%016"PRIx64"\n",
-           dsi.v_kernstart, dsi.v_kernend,
-           dsi.v_start, v_end);
-    IPRINTF("  ENTRY ADDRESS:        %016"PRIx64"\n", dsi.v_kernentry);
+            "  Loaded HVM loader:    %016"PRIx64"->%016"PRIx64"\n"
+            "  TOTAL:                %016"PRIx64"->%016"PRIx64"\n"
+            "  ENTRY ADDRESS:        %016"PRIx64"\n",
+            elf.pstart, elf.pend,
+            v_start, v_end,
+            elf_uval(&elf, elf.ehdr, e_entry));
 
-    if ( (v_end - dsi.v_start) > ((unsigned long long)nr_pages << PAGE_SHIFT) )
+    if ( (v_end - v_start) > ((unsigned long long)nr_pages << PAGE_SHIFT) )
     {
         PERROR("Initial guest OS requires too much space: "
                "(%lluMB is greater than %lluMB limit)\n",
-               (unsigned long long)(v_end - dsi.v_start) >> 20,
+               (unsigned long long)(v_end - v_start) >> 20,
                ((unsigned long long)nr_pages << PAGE_SHIFT) >> 20);
         goto error_out;
     }
@@ -212,7 +247,7 @@ static int setup_guest(int xc_handle,
         goto error_out;
     }
 
-    loadelfimage(image, xc_handle, dom, page_array, &dsi);
+    loadelfimage(&elf, xc_handle, dom, page_array);
 
     if ( (e820_page = xc_map_foreign_range(
               xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE,
@@ -256,7 +291,7 @@ static int setup_guest(int xc_handle,
 
     free(page_array);
 
-    ctxt->user_regs.eip = dsi.v_kernentry;
+    ctxt->user_regs.eip = elf_uval(&elf, elf.ehdr, e_entry);
 
     return 0;
 
@@ -315,131 +350,6 @@ static inline int is_loadable_phdr(Elf32
             ((phdr->p_flags & (PF_W|PF_X)) != 0));
 }
 
-static int parseelfimage(char *elfbase,
-                         unsigned long elfsize,
-                         struct domain_setup_info *dsi)
-{
-    Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
-    Elf32_Phdr *phdr;
-    Elf32_Shdr *shdr;
-    unsigned long kernstart = ~0UL, kernend=0UL;
-    char *shstrtab;
-    int h;
-
-    if ( !IS_ELF(*ehdr) )
-    {
-        xc_set_error(XC_INVALID_KERNEL,
-                     "Kernel image does not have an ELF header.");
-        return -EINVAL;
-    }
-
-    if ( (ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize)) > elfsize )
-    {
-        xc_set_error(XC_INVALID_KERNEL,
-                     "ELF program headers extend beyond end of image.");
-        return -EINVAL;
-    }
-
-    if ( (ehdr->e_shoff + (ehdr->e_shnum * ehdr->e_shentsize)) > elfsize )
-    {
-        xc_set_error(XC_INVALID_KERNEL,
-                     "ELF section headers extend beyond end of image.");
-        return -EINVAL;
-    }
-
-    /* Find the section-header strings table. */
-    if ( ehdr->e_shstrndx == SHN_UNDEF )
-    {
-        xc_set_error(XC_INVALID_KERNEL,
-                     "ELF image has no section-header strings table (shstrtab).");
-        return -EINVAL;
-    }
-    shdr = (Elf32_Shdr *)(elfbase + ehdr->e_shoff +
-                          (ehdr->e_shstrndx*ehdr->e_shentsize));
-    shstrtab = elfbase + shdr->sh_offset;
-
-    for ( h = 0; h < ehdr->e_phnum; h++ )
-    {
-        phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
-        if ( !is_loadable_phdr(phdr) )
-            continue;
-        if ( phdr->p_paddr < kernstart )
-            kernstart = phdr->p_paddr;
-        if ( (phdr->p_paddr + phdr->p_memsz) > kernend )
-            kernend = phdr->p_paddr + phdr->p_memsz;
-    }
-
-    if ( (kernstart > kernend) ||
-         (ehdr->e_entry < kernstart) ||
-         (ehdr->e_entry > kernend) )
-    {
-        xc_set_error(XC_INVALID_KERNEL,
-                     "Malformed ELF image.");
-        return -EINVAL;
-    }
-
-    dsi->v_start = 0x00000000;
-
-    dsi->v_kernstart = kernstart;
-    dsi->v_kernend   = kernend;
-    dsi->v_kernentry = HVM_LOADER_ENTR_ADDR;
-
-    dsi->v_end       = dsi->v_kernend;
-
-    return 0;
-}
-
-static int
-loadelfimage(
-    char *elfbase, int xch, uint32_t dom, unsigned long *parray,
-    struct domain_setup_info *dsi)
-{
-    Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
-    Elf32_Phdr *phdr;
-    int h;
-
-    char         *va;
-    unsigned long pa, done, chunksz;
-
-    for ( h = 0; h < ehdr->e_phnum; h++ )
-    {
-        phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
-        if ( !is_loadable_phdr(phdr) )
-            continue;
-
-        for ( done = 0; done < phdr->p_filesz; done += chunksz )
-        {
-            pa = (phdr->p_paddr + done) - dsi->v_start;
-            if ((va = xc_map_foreign_range(
-                xch, dom, PAGE_SIZE, PROT_WRITE,
-                parray[pa >> PAGE_SHIFT])) == 0)
-                return -1;
-            chunksz = phdr->p_filesz - done;
-            if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
-                chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
-            memcpy(va + (pa & (PAGE_SIZE-1)),
-                   elfbase + phdr->p_offset + done, chunksz);
-            munmap(va, PAGE_SIZE);
-        }
-
-        for ( ; done < phdr->p_memsz; done += chunksz )
-        {
-            pa = (phdr->p_paddr + done) - dsi->v_start;
-            if ((va = xc_map_foreign_range(
-                xch, dom, PAGE_SIZE, PROT_WRITE,
-                parray[pa >> PAGE_SHIFT])) == 0)
-                return -1;
-            chunksz = phdr->p_memsz - done;
-            if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
-                chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
-            memset(va + (pa & (PAGE_SIZE-1)), 0, chunksz);
-            munmap(va, PAGE_SIZE);
-        }
-    }
-
-    return 0;
-}
-
 /* xc_hvm_build
  *
  * Create a domain for a virtualized Linux, using files/filenames
#12 lore raw

PATCH 5/5 libelf: use for readnotes utility.

In reply to Gerd Hoffmann

This patch makes the readnotes utility use libelf.

---
 tools/xcutils/readnotes.c |  277 +++++++---------------------------------------
 1 file changed, 45 insertions(+), 232 deletions(-)
tools/xcutils/readnotes.c +45 -232
Index: build-32-unstable-13534/tools/xcutils/readnotes.c
===================================================================
@@ -1,4 +1,3 @@
-#include <elf.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
@@ -11,219 +10,35 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#include <xen/elfnote.h>
-
-#define ELFNOTE_NAME(_n_) ((char*)(_n_) + sizeof(*(_n_)))
-#define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + (((_n_)->n_namesz+3)&~3))
-#define ELFNOTE_NEXT(_n_) (ELFNOTE_DESC(_n_) + (((_n_)->n_descsz+3)&~3))
-
-#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 (ELFSIZE == 64)
-typedef Elf64_Nhdr Elf_Nhdr;
-typedef Elf64_Half Elf_Half;
-typedef Elf64_Word Elf_Word;
-#else
-#error "Unknown ELFSIZE"
-#endif
-
-static void print_string_note(const char *prefix, Elf_Nhdr *note)
-{
-	printf("%s: %s\n", prefix, (const char *)ELFNOTE_DESC(note));
-}
-
-static void print_numeric_note(const char *prefix,Elf_Nhdr *note)
-{
-	switch (note->n_descsz)
-	{
-	case 4:
-		printf("%s: %#010" PRIx32 " (4 bytes)\n",
-		       prefix, *(uint32_t *)ELFNOTE_DESC(note));
-		break;
-	case 8:
-		printf("%s: %#018" PRIx64 " (8 bytes)\n",
-		       prefix, *(uint64_t *)ELFNOTE_DESC(note));
-		break;
-	default:
-		printf("%s: unknown data size %#lx\n", prefix,
-		       (unsigned long)note->n_descsz);
-		break;
-	}
-}
-
-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)
-{
-	/*
-	 * 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);
-	}
-}
+#include <xg_private.h>
 
-static inline Elf_Half ehdr_shnum(void *image)
-{
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-		return ((Elf32_Ehdr *)image)->e_shnum;
-	case ELFCLASS64:
-		return ((Elf64_Ehdr *)image)->e_shnum;
-	default:
-		exit(1);
-	}
-}
+#include <xen/libelf.h>
 
-static inline Elf_Word shdr_type(void *image, int shnum)
+static void print_string_note(const char *prefix, struct elf_binary *elf,
+			      const elf_note *note)
 {
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return shdr->sh_type;
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return shdr->sh_type;
-	}
-	default:
-		exit(1);
-	}
+	printf("%s: %s\n", prefix, (char*)elf_note_desc(elf, note));
 }
 
-static inline const char *shdr_name(void *image, int shnum)
-{
-	const char *shstrtab;
-
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr;
-		/* Find the section-header strings table. */
-		if ( ehdr->e_shstrndx == SHN_UNDEF )
-			return NULL;
-		shdr = (Elf32_Shdr *)(image + ehdr->e_shoff +
-				      (ehdr->e_shstrndx*ehdr->e_shentsize));
-		shstrtab = image + shdr->sh_offset;
-
-		shdr= (Elf32_Shdr*)(image + ehdr->e_shoff +
-				    (shnum*ehdr->e_shentsize));
-		return &shstrtab[shdr->sh_name];
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr;
-		/* Find the section-header strings table. */
-		if ( ehdr->e_shstrndx == SHN_UNDEF )
-			return NULL;
-		shdr = (Elf64_Shdr *)(image + ehdr->e_shoff +
-				      (ehdr->e_shstrndx*ehdr->e_shentsize));
-		shstrtab = image + shdr->sh_offset;
-
-		shdr= (Elf64_Shdr*)(image + ehdr->e_shoff +
-				    (shnum*ehdr->e_shentsize));
-		return &shstrtab[shdr->sh_name];
-	}
-	default:
-		exit(1);
-	}
-}
-static inline void *shdr_start(void *image, int shnum)
+static void print_numeric_note(const char *prefix, struct elf_binary *elf,
+			       const elf_note *note)
 {
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset;
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset;
-	}
-	default:
-		exit(1);
-	}
-}
+	uint64_t value = elf_note_numeric(elf, note);
+	int descsz = elf_uval(elf, note, descsz);
 
-static inline void *shdr_end(void *image, int shnum)
-{
-	switch (ehdr_class(image))
-	{
-	case ELFCLASS32:
-	{
-		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
-		Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset + shdr->sh_size;
-	}
-	case ELFCLASS64:
-	{
-		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
-		Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff +
-						 (shnum*ehdr->e_shentsize));
-		return image + shdr->sh_offset + shdr->sh_size;
-	}
-	default:
-		exit(1);
-	}
+	printf("%s: %#*" PRIx64 " (%d bytes)\n",
+	       prefix, 2+2*descsz, value, descsz);
 }
 
 int main(int argc, char **argv)
 {
 	const char *f;
-	int fd,h;
+	int fd,h,size,count;
 	void *image;
 	struct stat st;
-	Elf_Nhdr *note;
+	struct elf_binary elf;
+	const elf_shdr *shdr;
+	const elf_note *note, *end;
 
 	if (argc != 2)
 	{
@@ -251,76 +66,74 @@ int main(int argc, char **argv)
 		fprintf(stderr, "Unable to map %s: %s\n", f, strerror(errno));
 		return 1;
 	}
+	size = st.st_size;
 
-	if ( !is_elf(image) )
+	if (0 != elf_init(&elf, image, size))
 	{
 		fprintf(stderr, "File %s is not an ELF image\n", f);
 		return 1;
 	}
+	elf_set_logfile(&elf, stderr, 0);
 
-	for ( h=0; h < ehdr_shnum(image); h++)
+	count = elf_shdr_count(&elf);
+	for ( h=0; h < count; h++)
 	{
-		if (shdr_type(image,h) != SHT_NOTE)
+		shdr = elf_shdr_by_index(&elf, h);
+		if (elf_uval(&elf, shdr, sh_type) != SHT_NOTE)
 			continue;
-		for (note = (Elf_Nhdr*)shdr_start(image,h);
-		     note < (Elf_Nhdr*)shdr_end(image,h);
-		     note = (Elf_Nhdr*)(ELFNOTE_NEXT(note)))
+		end = elf_section_end(&elf, shdr);
+		for (note = elf_section_start(&elf, shdr);
+		     note < end;
+		     note = elf_note_next(&elf, note))
 		{
-			switch(note->n_type)
+			if (0 != strcmp(elf_note_name(&elf, note), "Xen"))
+				continue;
+			switch(elf_uval(&elf, note, type))
 			{
 			case XEN_ELFNOTE_INFO:
-				print_string_note("INFO", note);
+				print_string_note("INFO", &elf , note);
 				break;
 			case XEN_ELFNOTE_ENTRY:
-				print_numeric_note("ENTRY", note);
+				print_numeric_note("ENTRY", &elf , note);
 				break;
 			case XEN_ELFNOTE_HYPERCALL_PAGE:
-				print_numeric_note("HYPERCALL_PAGE", note);
+				print_numeric_note("HYPERCALL_PAGE", &elf , note);
 				break;
 			case XEN_ELFNOTE_VIRT_BASE:
-				print_numeric_note("VIRT_BASE", note);
+				print_numeric_note("VIRT_BASE", &elf , note);
 				break;
 			case XEN_ELFNOTE_PADDR_OFFSET:
-				print_numeric_note("PADDR_OFFSET", note);
+				print_numeric_note("PADDR_OFFSET", &elf , note);
 				break;
 			case XEN_ELFNOTE_XEN_VERSION:
-				print_string_note("XEN_VERSION", note);
+				print_string_note("XEN_VERSION", &elf , note);
 				break;
 			case XEN_ELFNOTE_GUEST_OS:
-				print_string_note("GUEST_OS", note);
+				print_string_note("GUEST_OS", &elf , note);
 				break;
 			case XEN_ELFNOTE_GUEST_VERSION:
-				print_string_note("GUEST_VERSION", note);
+				print_string_note("GUEST_VERSION", &elf , note);
 				break;
 			case XEN_ELFNOTE_LOADER:
-				print_string_note("LOADER", note);
+				print_string_note("LOADER", &elf , note);
 				break;
 			case XEN_ELFNOTE_PAE_MODE:
-				print_string_note("PAE_MODE", note);
+				print_string_note("PAE_MODE", &elf , note);
 				break;
 			case XEN_ELFNOTE_FEATURES:
-				print_string_note("FEATURES", note);
+				print_string_note("FEATURES", &elf , note);
 				break;
 			default:
-				printf("unknown note type %#lx\n",
-				       (unsigned long)note->n_type);
+				printf("unknown note type %#x\n",
+				       (int)elf_uval(&elf, note, type));
 				break;
 			}
 		}
 	}
 
-	for ( h=0; h < ehdr_shnum(image); h++)
-	{
-		const char *name = shdr_name(image,h);
-
-		if ( name == NULL )
-			continue;
-		if ( strcmp(name, "__xen_guest") != 0 )
-			continue;
-
-		printf("__xen_guest: %s\n", (const char *)shdr_start(image, h));
-		break;
-	}
+	shdr = elf_shdr_by_name(&elf, "__xen_guest");
+	if (shdr)
+		printf("__xen_guest: %s\n", (char*)elf_section_start(&elf, shdr));
 
 	return 0;
 }