[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 07/13] linux-user: Fix loading of BSS segments
From: |
Laurent Vivier |
Subject: |
[PULL 07/13] linux-user: Fix loading of BSS segments |
Date: |
Mon, 15 Feb 2021 13:45:13 +0100 |
From: Giuseppe Musacchio <thatlemon@gmail.com>
Some ELF binaries encode the .bss section as an extension of the data
ones by setting the segment p_memsz > p_filesz. Some other binaries take
a different route and encode it as a stand-alone PT_LOAD segment with
p_filesz = 0 and p_memsz > 0.
Both the encodings are actually correct per ELF specification but the
ELF loader had some troubles in handling the former: with the old logic
it was very likely to get Qemu to crash in zero_bss when trying to
access unmapped memory.
zero_bss isn't meant to allocate whole zero-filled segments but to
"complete" a previously mapped segment with the needed zero bits.
The fix is pretty simple, if the segment is completely zero-filled we
simply allocate one or more pages (according to p_memsz) and avoid
calling zero_bss altogether.
Signed-off-by: Giuseppe Musacchio <thatlemon@gmail.com>
Message-Id: <c9106487-dc4d-120a-bd48-665b3c617287@gmail.com>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
linux-user/elfload.c | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index a64050713f28..f5bd4076fcf5 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2805,14 +2805,16 @@ static void load_elf_image(const char *image_name, int
image_fd,
vaddr = load_bias + eppnt->p_vaddr;
vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
- vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
+
+ vaddr_ef = vaddr + eppnt->p_filesz;
+ vaddr_em = vaddr + eppnt->p_memsz;
/*
- * Some segments may be completely empty without any backing file
- * segment, in that case just let zero_bss allocate an empty buffer
- * for it.
+ * Some segments may be completely empty, with a non-zero p_memsz
+ * but no backing file segment.
*/
if (eppnt->p_filesz != 0) {
+ vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
MAP_PRIVATE | MAP_FIXED,
image_fd, eppnt->p_offset - vaddr_po);
@@ -2820,14 +2822,22 @@ static void load_elf_image(const char *image_name, int
image_fd,
if (error == -1) {
goto exit_mmap;
}
- }
- vaddr_ef = vaddr + eppnt->p_filesz;
- vaddr_em = vaddr + eppnt->p_memsz;
+ /*
+ * If the load segment requests extra zeros (e.g. bss), map it.
+ */
+ if (eppnt->p_filesz < eppnt->p_memsz) {
+ zero_bss(vaddr_ef, vaddr_em, elf_prot);
+ }
+ } else if (eppnt->p_memsz != 0) {
+ vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_memsz + vaddr_po);
+ error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
+ MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
+ -1, 0);
- /* If the load segment requests extra zeros (e.g. bss), map it. */
- if (vaddr_ef < vaddr_em) {
- zero_bss(vaddr_ef, vaddr_em, elf_prot);
+ if (error == -1) {
+ goto exit_mmap;
+ }
}
/* Find the full program boundaries. */
--
2.29.2
- [PULL 00/13] Linux user for 6.0 patches, Laurent Vivier, 2021/02/15
- [PULL 05/13] linux-user: fix O_NONBLOCK usage for hppa target, Laurent Vivier, 2021/02/15
- [PULL 06/13] linux-user: fix O_NONBLOCK in signalfd4() and eventfd2() syscalls, Laurent Vivier, 2021/02/15
- [PULL 09/13] linux-user/syscall: Fix do_ioctl_ifconf() for 64 bit targets., Laurent Vivier, 2021/02/15
- [PULL 07/13] linux-user: Fix loading of BSS segments,
Laurent Vivier <=
- [PULL 04/13] linux-user: Add missing TARGET___O_TMPFILE for hppa and alpha, Laurent Vivier, 2021/02/15
- [PULL 12/13] docs/user: Remove outdated 'Quick Start' section, Laurent Vivier, 2021/02/15
- [PULL 13/13] linux-user/mips: Support the n32 ABI for the R5900, Laurent Vivier, 2021/02/15
- [PULL 10/13] linux-user: add TARGET_SO_{DOMAIN,PROTOCOL}, Laurent Vivier, 2021/02/15
- [PULL 08/13] linux-user/mmap: Avoid asserts for out of range mremap calls, Laurent Vivier, 2021/02/15
- [PULL 02/13] linux-user/mips64: Support o32 ABI syscalls, Laurent Vivier, 2021/02/15
- [PULL 01/13] linux-user/mips64: Restore setup_frame() for o32 ABI, Laurent Vivier, 2021/02/15
- [PULL 11/13] linux-user: target: signal: Support TARGET_SS_AUTODISARM, Laurent Vivier, 2021/02/15
- [PULL 03/13] linux-user/signal: Decode waitid si_code, Laurent Vivier, 2021/02/15
- Re: [PULL 00/13] Linux user for 6.0 patches, Philippe Mathieu-Daudé, 2021/02/15