All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] linux-user: elfload: allocate and initialize memsz-filesz gap
@ 2009-07-12 11:02 Max Filippov
  2009-08-13  9:55 ` Riku Voipio
  0 siblings, 1 reply; 2+ messages in thread
From: Max Filippov @ 2009-07-12 11:02 UTC (permalink / raw)
  To: qemu-devel

Hello.

I'm having ppc ELF binaries that I'm running on x86 in linux-user emulation.
Some of these binaries work fine, but others segfault.
The latter binaries have segments with p_filesz < p_memsz and sections like .bss in this gap.
Segfaults usually happen in attempt to access address within this gap. 
According to the linux-user/elfload.c only first p_filesz bytes of such segments are mmaped and mprotected.

This patch mmaps p_memsz bytes and then zero out last p_memsz - p_filesz bytes of such segments.
Is there a better way to do it?

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
---
diff -burN qemu-snapshot-2009-07-11_r7249-orig/linux-user/elfload.c qemu-snapshot-2009-07-11_r7249/linux-user/elfload.c
--- qemu-snapshot-2009-07-11_r7249-orig/linux-user/elfload.c	2009-07-11 06:12:23.000000000 +0400
+++ qemu-snapshot-2009-07-11_r7249/linux-user/elfload.c	2009-07-12 04:47:47.000000000 +0400
@@ -1380,7 +1380,7 @@
         }
 
         error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
-                            (elf_ppnt->p_filesz +
+                            (elf_ppnt->p_memsz +
                              TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
                             elf_prot,
                             (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
@@ -1392,6 +1392,20 @@
             exit(-1);
         }
 
+        if(elf_ppnt->p_memsz > elf_ppnt->p_filesz) {
+            abi_ulong pg = TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr) +
+                (elf_ppnt->p_filesz +
+                 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr));
+            abi_ulong sz = elf_ppnt->p_memsz - elf_ppnt->p_filesz;
+
+            void *p = lock_user(PAGE_READ | PAGE_WRITE, pg, sz, 0);
+
+            if (p) {
+                memset(p, 0, sz);
+                unlock_user(p, pg, sz);
+            }
+        }
+
 #ifdef LOW_ELF_STACK
         if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
             elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);


Thanks.
-- Max

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: elfload: allocate and initialize memsz-filesz gap
  2009-07-12 11:02 [Qemu-devel] [PATCH] linux-user: elfload: allocate and initialize memsz-filesz gap Max Filippov
@ 2009-08-13  9:55 ` Riku Voipio
  0 siblings, 0 replies; 2+ messages in thread
From: Riku Voipio @ 2009-08-13  9:55 UTC (permalink / raw)
  To: Max Filippov; +Cc: qemu-devel

On Sun, Jul 12, 2009 at 03:02:38PM +0400, Max Filippov wrote:
> I'm having ppc ELF binaries that I'm running on x86 in linux-user emulation.
> Some of these binaries work fine, but others segfault.
> The latter binaries have segments with p_filesz < p_memsz and sections like .bss in this gap.
> Segfaults usually happen in attempt to access address within this gap. 
> According to the linux-user/elfload.c only first p_filesz bytes of such segments are mmaped and mprotected.

> This patch mmaps p_memsz bytes and then zero out last p_memsz - p_filesz bytes of such segments.
> Is there a better way to do it?

After applying this patch, any arm-linux binaries on x86 fail run.

> Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
> ---
> diff -burN qemu-snapshot-2009-07-11_r7249-orig/linux-user/elfload.c qemu-snapshot-2009-07-11_r7249/linux-user/elfload.c
> --- qemu-snapshot-2009-07-11_r7249-orig/linux-user/elfload.c	2009-07-11 06:12:23.000000000 +0400
> +++ qemu-snapshot-2009-07-11_r7249/linux-user/elfload.c	2009-07-12 04:47:47.000000000 +0400
> @@ -1380,7 +1380,7 @@
>          }
>  
>          error = target_mmap(TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr),
> -                            (elf_ppnt->p_filesz +
> +                            (elf_ppnt->p_memsz +
>                               TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
>                              elf_prot,
>                              (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
> @@ -1392,6 +1392,20 @@
>              exit(-1);
>          }
>  
> +        if(elf_ppnt->p_memsz > elf_ppnt->p_filesz) {
> +            abi_ulong pg = TARGET_ELF_PAGESTART(load_bias + elf_ppnt->p_vaddr) +
> +                (elf_ppnt->p_filesz +
> +                 TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr));
> +            abi_ulong sz = elf_ppnt->p_memsz - elf_ppnt->p_filesz;
> +
> +            void *p = lock_user(PAGE_READ | PAGE_WRITE, pg, sz, 0);
> +
> +            if (p) {
> +                memset(p, 0, sz);
> +                unlock_user(p, pg, sz);
> +            }
> +        }
> +
>  #ifdef LOW_ELF_STACK
>          if (TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr) < elf_stack)
>              elf_stack = TARGET_ELF_PAGESTART(elf_ppnt->p_vaddr);
> 
> 
> Thanks.
> -- Max
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-08-13  9:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-12 11:02 [Qemu-devel] [PATCH] linux-user: elfload: allocate and initialize memsz-filesz gap Max Filippov
2009-08-13  9:55 ` Riku Voipio

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.