Port Wine preloader to QEMU
This is the only way I know to get qemu 0.14.0 to work reliably with Scratchbox 2 (libsb2.so)
The problem stems from two things:
* The shared object loader is permitted to load LD_PRELOAD objects below the QEMU load address (0x60000000), and does this with libsb2.so
* QEMU's do_brk() (linux-user/syscall.c #738) uses MAP_FIXED, which means after allocating enough memory, libsb2.so is clobbered.
I have tried:
* using plain QEMU 0.14.0 - crashes build perl
* QEMU 0.14.0, appending -R 512M to qemu's command line in scratchbox2/lua_scripts/argvenvp.lua, but
still get crashes when building perl.
* modifying do_brk to not use MAP_FIXED - causes an out of memory failure rather than a crash
The preloader resolves this by allocating a fixed chunk of memory before any dynamic linking occurs. Order of loading is:
1) load staticly linked qemu-arm (now the preloader)
2) reserve memory
3) load ld-linux.so and qemu-arm-bin (originally qemu-arm) into memory
4) jump to ld.so entry point
The preloader is lookups a special symbol in qemu-arm-bin and set it to a pointer with information about the reserved memory, so qemu-arm should be linked with --dynamic-list-data so wine_main_preload_info is exported.
TODO:
* The -R and -B flags are disallowed when using the preloader, as memory is already reserved. It may be good to parse these in the preloader itself and reserve what the user specifies.
* Fix formatting for QEMU, remove Wine specific variable names, etc.
* solicit fla^H^H^H comments from QEMU developers
--- Makefile.target | 12 +- elf.h | 11 + linux-user/main.c | 18 + linux-user/preloader.c | 1161 ++++++++++++++++++++++++++++++++++++++++++++++++ linux-user/preloader.h | 31 ++ 5 files changed, 1232 insertions(+), 1 deletions(-) create mode 100644 linux-user/preloader.c create mode 100644 linux-user/preloader.h
Makefile.target +11 -1
@@ -31,6 +31,10 @@ endif endif PROGS=$(QEMU_PROG) +ifdef CONFIG_LINUX_USER +QEMU_TARGET=$(QEMU_PROG)-bin +PROGS += $(QEMU_TARGET) +endif STPFILES= ifndef CONFIG_HAIKU
@@ -131,6 +135,12 @@ obj-y += $(addprefix ../libuser/, $(user-obj-y)) obj-y += $(addprefix ../libdis-user/, $(libdis-y)) obj-y += $(libobj-y) +PRELOADER_LDFLAGS = -static -nostartfiles -nodefaultlibs -Wl,-Ttext=0x7c000000 +$(QEMU_PROG): preloader.o + $(call quiet-command,$(CC) -o $@ $(PRELOADER_LDFLAGS) $<," LINK $(TARGET_DIR)$@") + +LDFLAGS += -Wl,--dynamic-list-data + endif #CONFIG_LINUX_USER #########################################################
@@ -386,7 +396,7 @@ endif # CONFIG_LINUX_USER obj-y += $(addprefix ../, $(trace-obj-y)) obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o -$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) +$(QEMU_TARGET): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) $(call LINK,$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y))
elf.h +11 -0
@@ -1213,6 +1213,17 @@ struct elf32_fdpic_loadmap {struct elf32_fdpic_loadseg segs[/*nsegs*/ ]; }; +typedef struct { +uint32_t a_type; +union { +uint32_t a_val; + } a_un; +}Elf32_auxv_t ; + +#define ELF32_ST_INFO (bind, type) (((bind) << 4) + ((type) & 0xf)) + +#define STN_UNDEF 0 +#ifdef ELF_CLASS #if ELF_CLASS ==ELFCLASS32
linux-user/main.c +18 -0
@@ -34,6 +34,7 @@#include "tcg.h" #include "qemu-timer.h" #include "envlist.h" +#include "preloader.h" #define DEBUG_LOGFILE "/tmp/qemu.log"
@@ -47,6 +48,9 @@ int have_guest_base;unsigned long reserved_va;#endif +/* this is set by the preloader */ +const struct wine_preload_info *wine_main_preload_info; +static const char *interp_prefix =CONFIG_QEMU_INTERP_PREFIX ;const char *qemu_uname_release =CONFIG_UNAME_RELEASE ;
@@ -2928,11 +2932,19 @@ int main(int argc, char **argv, char **envp) }#if defined(CONFIG_USE_GUEST_BASE ) } else if (!strcmp(r, "B")) { +if (wine_main_preload_info) { +fprintf (stderr,"Preloader used, but -B specified\n" ); +exit (1 ); + } guest_base =strtol (argv[optind++],NULL ,0 ); have_guest_base =1 ; }else if (!strcmp (r,"R" )) {char *p;int shift =0 ; +if (wine_main_preload_info) { +fprintf (stderr,"Preloader used, but -R specified\n" ); +exit (1 ); + } reserved_va =strtoul (argv[optind++], &p,0 );switch (*p) {case 'k' :
@@ -3049,6 +3061,11 @@ int main(int argc, char **argv, char **envp) target_environ =envlist_to_environ (envlist,NULL );envlist_free (envlist); +if (wine_main_preload_info) { + guest_base = (unsigned long )wine_main_preload_info->addr; + reserved_va = wine_main_preload_info->size; + goto reserve_done; + }#if defined(CONFIG_USE_GUEST_BASE )/* * Now that page sizes are configured in cpu_init() we can do
@@ -3084,6 +3101,7 @@ int main(int argc, char **argv, char **envp) qemu_log("Reserved 0x%lx bytes of guest address space\n", reserved_va); }#endif /* CONFIG_USE_GUEST_BASE */ +reserve_done:/* * Read in mmap_min_addr kernel parameter. This value is used
linux-user/preloader.c new file +1161 -0
1,161 lines not on this page · raw message
linux-user/preloader.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* Definitions for Wine main program +* +* Copyright 2004 Mike McCormack for CodeWeavers +* Copyright 2004 Alexandre Julliard +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA +*/ + +#ifndef __WINE_LOADER_MAIN_H +#define __WINE_LOADER_MAIN_H + +struct wine_preload_info +{ +void *addr; +size_t size; +}; + +#endif /* __WINE_LOADER_MAIN_H */
Signature · 1 line
1.7.0.4
Quoted 13 lines · Mike McCormack
This is the only way I know to get qemu 0.14.0 to work reliably with Scratchbox 2 (libsb2.so)
The problem stems from two things:
* The shared object loader is permitted to load
LD_PRELOADobjects below the QEMU load address (0x60000000), and does this with libsb2.so* QEMU's
do_brk()(linux-user/syscall.c#738) usesMAP_FIXED, which means after allocating enough memory, libsb2.so is clobbered.I have tried:
Quoted 8 lines
* using plain QEMU 0.14.0 - crashes build perl
* QEMU 0.14.0, appending -R 512M to qemu's command line in scratchbox2/lua_scripts/argvenvp.lua, but
still get crashes when building perl.* modifying
do_brkto not useMAP_FIXED- causes an out of memory failure rather than a crash
Did you try --enable-user-pie? It may not really help, but I'm curious.
Honestly I'm not keen on this patch. This level of obfuscation on the startup and memory map of the host binary is just a gross hack working around the lack of proper page tables in user mode.
If you really really need to get this working with a 32-bit host binary (rather than doing the sensible thing and using a 64-bit PIE binary), then working to enable CONFIG_SOFTMMU in user mode instead would be the most useful thing you could do. Indeed, this would fix a number of problems we currently have emulating other guests that have a page size different from the host.
r~
Quoted 1 line · Richard Henderson
Did you try --enable-user-pie? It may not really help, but I'm curious.
No. I don't think it will help because placement of the executable probably doesn't account for how large its heap will grow.
You'll still run out of memory as the heap grows and runs into an LD_PRELOAD'ed shared object that's been mapped below 0x60000000, then crash without your do_brk() MAP_FIXED patch, or fail with some error code with it.
Quoted 3 lines
Honestly I'm not keen on this patch. This level of obfuscation on the startup and memory map of the host binary is just a gross hack working around the lack of proper page tables in user mode.
This mechanism has been used in Wine for 6 years, but Wine doesn't have any other way to guarantee the memory layout.
Quoted 6 lines
If you really really need to get this working with a 32-bit host binary (rather than doing the sensible thing and using a 64-bit PIE binary), then working to enable
CONFIG_SOFTMMUin user mode instead would be the most useful thing you could do. Indeed, this would fix a number of problems we currently have emulating other guests that have a page size different from the host.
Yes, having page tables in user mode emulation would help, but would probably make the target executable considerably slower too.
thanks,
Mike
Quoted 2 lines · Mike McCormack
* modifying
do_brkto not useMAP_FIXED- causes an out of memory failure rather than a crash
Have you tried the patch posted by Peter Maydell yesterday:
http://www.mail-archive.com/[email protected]/msg61733.html
Riku
Quoted 7 lines · Riku Voipio
Quoted 2 lines · Mike McCormack
* modifying
do_brkto not useMAP_FIXED- causes an out of memory failure rather than a crashHave you tried the patch posted by Peter Maydell yesterday:
That will presumably get the out-of-memory failure as Mike says :-) I think that's an improvement over crashing, in the short term...
-- PMM