Skip to contentSkip to conversation

linux-user: elfload: allocate and initialize memsz-filesz gap

PATCH In review Max Filippov 2 participants 2 messages Jul 12, 2009 – Aug 13, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: elfload: allocate and initialize memsz-filesz gap

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?

---

linux-user/elfload.c +15 -1
diff -burN qemu-snapshot-2009-07-11_r7249-orig/linux-user/elfload.c qemu-snapshot-2009-07-11_r7249/linux-user/elfload.c
@@ -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

#1 lore raw
Quoted 8 lines · Max Filippov

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.

Quoted 40 lines

---
diff -burN qemu-snapshot-2009-07-11_r7249-orig/linux-user/elfload.c qemu-snapshot-2009-07-11_r7249/linux-user/elfload.c

linux-user/elfload.c +15 -1
@@ -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