[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] libelf-loader: introduce elf_load_image



# HG changeset patch
# User Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>
# Date 1327311732 0
# Node ID 137c16a83e4084b717a8a5685371800aeb313233
# Parent  d6cdbc4fe0787adccc6194d9352fa950dd42e810
libelf-loader: introduce elf_load_image

Implement a new function, called elf_load_image, to perform the
actually copy of the elf image and clearing the padding.  The function
is implemented as memcpy and memset when the library is built as part
of the tools, but it is implemented as raw_copy_to_guest and
raw_clear_guest when built as part of Xen, so that it can be safely
called with an HVM style dom0.

Signed-off-by: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>
Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
Signed-off-by: Tim Deegan <Tim.Deegan@xxxxxxxxxx>
Committed-by: Keir Fraser <keir@xxxxxxx>
---


diff -r d6cdbc4fe078 -r 137c16a83e40 tools/libxc/xc_dom_elfloader.c
--- a/tools/libxc/xc_dom_elfloader.c    Mon Jan 23 09:41:27 2012 +0000
+++ b/tools/libxc/xc_dom_elfloader.c    Mon Jan 23 09:42:12 2012 +0000
@@ -310,9 +310,15 @@
 static int xc_dom_load_elf_kernel(struct xc_dom_image *dom)
 {
     struct elf_binary *elf = dom->private_loader;
+    int rc;
 
     elf->dest = xc_dom_seg_to_ptr(dom, &dom->kernel_seg);
-    elf_load_binary(elf);
+    rc = elf_load_binary(elf);
+    if ( rc < 0 )
+    {
+        DOMPRINTF("%s: failed to load elf binary", __FUNCTION__);
+        return rc;
+    }
     if ( dom->parms.bsd_symtab )
         xc_dom_load_elf_symtab(dom, elf, 1);
     return 0;
diff -r d6cdbc4fe078 -r 137c16a83e40 tools/libxc/xc_hvm_build.c
--- a/tools/libxc/xc_hvm_build.c        Mon Jan 23 09:41:27 2012 +0000
+++ b/tools/libxc/xc_hvm_build.c        Mon Jan 23 09:42:12 2012 +0000
@@ -109,8 +109,9 @@
     elf->dest += elf->pstart & (PAGE_SIZE - 1);
 
     /* Load the initial elf image. */
-    elf_load_binary(elf);
-    rc = 0;
+    rc = elf_load_binary(elf);
+    if ( rc < 0 )
+        PERROR("Failed to load elf binary\n");
 
     munmap(elf->dest, pages << PAGE_SHIFT);
     elf->dest = NULL;
diff -r d6cdbc4fe078 -r 137c16a83e40 xen/arch/x86/domain_build.c
--- a/xen/arch/x86/domain_build.c       Mon Jan 23 09:41:27 2012 +0000
+++ b/xen/arch/x86/domain_build.c       Mon Jan 23 09:42:12 2012 +0000
@@ -903,7 +903,12 @@
 
     /* Copy the OS image and free temporary buffer. */
     elf.dest = (void*)vkern_start;
-    elf_load_binary(&elf);
+    rc = elf_load_binary(&elf);
+    if ( rc < 0 )
+    {
+        printk("Failed to load the kernel binary\n");
+        return rc;
+    }
     bootstrap_map(NULL);
 
     if ( UNSET_ADDR != parms.virt_hypercall )
diff -r d6cdbc4fe078 -r 137c16a83e40 xen/common/libelf/libelf-loader.c
--- a/xen/common/libelf/libelf-loader.c Mon Jan 23 09:41:27 2012 +0000
+++ b/xen/common/libelf/libelf-loader.c Mon Jan 23 09:42:12 2012 +0000
@@ -107,11 +107,34 @@
     elf->log_caller_data = log_caller_data;
     elf->verbose = verbose;
 }
+
+static int elf_load_image(void *dst, const void *src, uint64_t filesz, 
uint64_t memsz)
+{
+    memcpy(dst, src, filesz);
+    memset(dst + filesz, 0, memsz - filesz);
+    return 0;
+}
 #else
+#include <asm/guest_access.h>
+
 void elf_set_verbose(struct elf_binary *elf)
 {
     elf->verbose = 1;
 }
+
+static int elf_load_image(void *dst, const void *src, uint64_t filesz, 
uint64_t memsz)
+{
+    int rc;
+    if ( filesz > ULONG_MAX || memsz > ULONG_MAX )
+        return -1;
+    rc = raw_copy_to_guest(dst, src, filesz);
+    if ( rc != 0 )
+        return -1;
+    rc = raw_clear_guest(dst + filesz, memsz - filesz);
+    if ( rc != 0 )
+        return -1;
+    return 0;
+}
 #endif
 
 /* Calculate the required additional kernel space for the elf image */
@@ -237,7 +260,7 @@
             __FUNCTION__, elf->pstart, elf->pend);
 }
 
-void elf_load_binary(struct elf_binary *elf)
+int elf_load_binary(struct elf_binary *elf)
 {
     const elf_phdr *phdr;
     uint64_t i, count, paddr, offset, filesz, memsz;
@@ -256,11 +279,12 @@
         dest = elf_get_ptr(elf, paddr);
         elf_msg(elf, "%s: phdr %" PRIu64 " at 0x%p -> 0x%p\n",
                 __func__, i, dest, dest + filesz);
-        memcpy(dest, elf->image + offset, filesz);
-        memset(dest + filesz, 0, memsz - filesz);
+        if ( elf_load_image(dest, elf->image + offset, filesz, memsz) != 0 )
+            return -1;
     }
 
     elf_load_bsdsyms(elf);
+    return 0;
 }
 
 void *elf_get_ptr(struct elf_binary *elf, unsigned long addr)
diff -r d6cdbc4fe078 -r 137c16a83e40 xen/include/xen/libelf.h
--- a/xen/include/xen/libelf.h  Mon Jan 23 09:41:27 2012 +0000
+++ b/xen/include/xen/libelf.h  Mon Jan 23 09:42:12 2012 +0000
@@ -198,7 +198,7 @@
 #endif
 
 void elf_parse_binary(struct elf_binary *elf);
-void elf_load_binary(struct elf_binary *elf);
+int elf_load_binary(struct elf_binary *elf);
 
 void *elf_get_ptr(struct elf_binary *elf, unsigned long addr);
 uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol);

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


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.