All of lore.kernel.org
 help / color / mirror / Atom feed
From: Max Filippov <jcmvbkbc@gmail.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH] linux-user: elfload: allocate and initialize memsz-filesz gap
Date: Sun, 12 Jul 2009 15:02:38 +0400	[thread overview]
Message-ID: <200907121502.39025.jcmvbkbc@gmail.com> (raw)

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

             reply	other threads:[~2009-07-12 11:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-12 11:02 Max Filippov [this message]
2009-08-13  9:55 ` [Qemu-devel] [PATCH] linux-user: elfload: allocate and initialize memsz-filesz gap Riku Voipio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200907121502.39025.jcmvbkbc@gmail.com \
    --to=jcmvbkbc@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.