Skip to contentSkip to conversation

Do not link usermode targets with libhw*.a

PATCH In review Kirill A. Shutemov 2 participants 7 messages Sep 11, 2009 – Sep 12, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Do not link usermode targets with libhw*.a

Usermode targets are hardware-independed.

---
 configure |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
configure +2 -2
index 5232172..1ecb1ab 100755
@@ -2207,15 +2207,15 @@ case "$target_arch2" in
       echo "KVM_CFLAGS=$kvm_cflags" >> $config_mak
     fi
 esac
-echo "HWLIB=../libhw$target_phys_bits/libqemuhw$target_phys_bits.a" >> $config_mak
 echo "TARGET_PHYS_ADDR_BITS=$target_phys_bits" >> $config_mak
-echo "subdir-$target: subdir-libhw$target_phys_bits" >> $config_host_mak
 if test "$target_bigendian" = "yes" ; then
   echo "TARGET_WORDS_BIGENDIAN=y" >> $config_mak
 fi
 if test "$target_softmmu" = "yes" ; then
   echo "CONFIG_SOFTMMU=y" >> $config_mak
   echo "LIBS+=$libs_softmmu" >> $config_mak
+  echo "HWLIB=../libhw$target_phys_bits/libqemuhw$target_phys_bits.a" >> $config_mak
+  echo "subdir-$target: subdir-libhw$target_phys_bits" >> $config_host_mak
 fi
 if test "$target_user_only" = "yes" ; then
   echo "CONFIG_USER_ONLY=y" >> $config_mak
Signature · 1 line
1.6.4.2
#1 lore raw

PATCH 2/3 Fix text relocations in linux-user targets

There is a link hack in linux-user which produces an executable that looks like PIE, but always has text relocations since all object files isn't position-independent (compiled without -fpic/-fpie). Dynamic loader has to do more work to load a binary with text relocations.

The best way to keep this functionality is to build a true PIE without text relocations.

---
 configure         |   12 +-----------
 linux-user/main.c |   26 --------------------------
 2 files changed, 1 insertions(+), 37 deletions(-)
configure +1 -11
index 1ecb1ab..b2f52b2 100755
@@ -2357,16 +2357,6 @@ fi
 linker_script="-Wl,-T../config-host.ld -Wl,-T,\$(SRC_PATH)/\$(ARCH).ld"
 if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" ; then
   case "$ARCH" in
-  i386)
-    if test "$gprof" = "yes" -o "$static" = "yes" ; then
-      ldflags="$linker_script $ldflags"
-    else
-      # WARNING: this LDFLAGS is _very_ tricky : qemu is an ELF shared object
-      # that the kernel ELF loader considers as an executable. I think this
-      # is the simplest way to make it self virtualizable!
-      ldflags="-Wl,-shared $ldflags"
-    fi
-    ;;
   sparc)
     # -static is used to avoid g1/g3 usage by the dynamic linker
     ldflags="$linker_script -static $ldflags"
@@ -2374,7 +2364,7 @@ if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" ; then
   ia64)
     ldflags="-Wl,-G0 $linker_script -static $ldflags"
     ;;
-  x86_64|ppc|ppc64|s390|sparc64|alpha|arm|m68k|mips|mips64)
+  i386|x86_64|ppc|ppc64|s390|sparc64|alpha|arm|m68k|mips|mips64)
     ldflags="$linker_script $ldflags"
     ;;
   esac
linux-user/main.c +0 -26
index a628c01..81a1ada 100644
@@ -48,32 +48,6 @@ int have_guest_base;
 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
 
-#if defined(__i386__) && !defined(CONFIG_STATIC)
-/* Force usage of an ELF interpreter even if it is an ELF shared
-   object ! */
-const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
-#endif
-
-/* for recent libc, we add these dummy symbols which are not declared
-   when generating a linked object (bug in ld ?) */
-#if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined(CONFIG_STATIC)
-asm(".globl __preinit_array_start\n"
-    ".globl __preinit_array_end\n"
-    ".globl __init_array_start\n"
-    ".globl __init_array_end\n"
-    ".globl __fini_array_start\n"
-    ".globl __fini_array_end\n"
-    ".section \".rodata\"\n"
-    "__preinit_array_start:\n"
-    "__preinit_array_end:\n"
-    "__init_array_start:\n"
-    "__init_array_end:\n"
-    "__fini_array_start:\n"
-    "__fini_array_end:\n"
-    ".long 0\n"
-    ".previous\n");
-#endif
-
 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
    we allocate a bigger stack. Need a better solution, for example
    by remapping the process stack directly at the right place */
Signature · 1 line
1.6.4.2
#2 lore raw

PATCH 3/3 Add configure option to compile user targets as PIE

Build uset targers as true PIE if user want to keep qemu self-virtualizable.

v5:
- Split into to patches: drop link hack and add PIE support
- do not build PIE by default and drop toolchain check

v4:
- Add test for toolchain if it has proper PIE support

v3:
- One more pice of the hack was removed
- Description updated

v2:
- Add configure options do enable/disable PIE for usermode targets. Disabling can be useful if you build uswing toolchain which has broken PIE support. PIE for usermode targets enabled by default.

---
 Makefile        |   10 +---------
 Makefile.target |   19 +++++++++++++++----
 configure       |   14 ++++++++++++++
 3 files changed, 30 insertions(+), 13 deletions(-)
Makefile +1 -9
index cb2614b..b1de39a 100644
@@ -39,8 +39,6 @@ subdir-%:
 	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $* V="$(V)" TARGET_DIR="$*/" all,)
 
 $(filter %-softmmu,$(SUBDIR_RULES)): libqemu_common.a
-$(filter %-user,$(SUBDIR_RULES)): libqemu_user.a
-
 
 ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
 romsubdir-%:
@@ -74,7 +72,7 @@ block-obj-y +=  $(addprefix block/, $(block-nested-y))
 # CPUs and machines.
 
 obj-y = $(block-obj-y)
-obj-y += readline.o console.o host-utils.o
+obj-y += readline.o console.o
 
 obj-y += irq.o ptimer.o
 obj-y += i2c.o smbus.o smbus_eeprom.o max7310.o max111x.o wm8750.o
@@ -162,12 +160,6 @@ bt-host.o: QEMU_CFLAGS += $(BLUEZ_CFLAGS)
 
 libqemu_common.a: $(obj-y)
 
-#######################################################################
-# user-obj-y is code used by qemu userspace emulation
-user-obj-y = cutils.o cache-utils.o path.o envlist.o host-utils.o
-
-libqemu_user.a: $(user-obj-y)
-
 ######################################################################
 
 qemu-img.o: qemu-img-cmds.h
Makefile.target +15 -4
index 0fe8b6a..6314e0e 100644
@@ -31,7 +31,7 @@ all: $(PROGS)
 
 #########################################################
 # cpu emulator library
-libobj-y = exec.o translate-all.o cpu-exec.o translate.o
+libobj-y = exec.o translate-all.o cpu-exec.o translate.o host-utils.o
 libobj-y += tcg/tcg.o tcg/tcg-runtime.o
 libobj-$(CONFIG_SOFTFLOAT) += fpu/softfloat.o
 libobj-$(CONFIG_NOSOFTFLOAT) += fpu/softfloat-native.o
@@ -80,9 +80,9 @@ ifdef CONFIG_LINUX_USER
 
 VPATH+=:$(SRC_PATH)/linux-user:$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR)
 QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user -I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR)
-
 obj-y = main.o syscall.o strace.o mmap.o signal.o thunk.o \
       elfload.o linuxload.o uaccess.o gdbstub.o gdbstub-xml.o
+obj-y += envlist.o path.o
 
 obj-$(TARGET_HAS_BFLT) += flatload.o
 obj-$(TARGET_HAS_ELFLOAD32) += elfload32.o
@@ -98,7 +98,7 @@ obj-arm-y += arm-semi.o
 
 obj-m68k-y += m68k-sim.o m68k-semi.o
 
-ARLIBS=../libqemu_user.a libqemu.a
+ARLIBS=libqemu.a
 endif #CONFIG_LINUX_USER
 
 #########################################################
@@ -116,6 +116,7 @@ LIBS+=-lmx
 
 obj-y = main.o commpage.o machload.o mmap.o signal.o syscall.o thunk.o \
         gdbstub.o gdbstub-xml.o
+obj-y += envlist.o path.o
 
 obj-i386-y += ioport-user.o
 
@@ -133,13 +134,23 @@ QEMU_CFLAGS+=-I$(SRC_PATH)/bsd-user -I$(SRC_PATH)/bsd-user/$(TARGET_ARCH)
 
 obj-y = main.o bsdload.o elfload.o mmap.o signal.o strace.o syscall.o \
         gdbstub.o gdbstub-xml.o uaccess.o
+obj-y += envlist.o path.o
 
 obj-i386-y += ioport-user.o
 
-ARLIBS=libqemu.a ../libqemu_user.a
+ARLIBS=libqemu.a
 
 endif #CONFIG_BSD_USER
 
+ifdef CONFIG_USER_ONLY
+# hack to compile with -fpie for *-user targets
+obj-y += cutils-user.o cache-utils-user.o
+cutils-user.c cache-utils-user.c:
+	@echo "  LN	$(TARGET_DIR)$@"
+	@ln -s $(SRC_PATH)/$(@:%-user.c=%.c) $@
+endif
+
+
 #########################################################
 # System emulator target
 ifdef CONFIG_SOFTMMU
configure +14 -0
index b2f52b2..6b54c63 100755
@@ -222,6 +222,7 @@ aix="no"
 blobs="yes"
 pkgversion=""
 check_utests="no"
+user_pie="no"
 
 # OS specific
 if check_define __linux__ ; then
@@ -499,6 +500,10 @@ for opt do
   ;;
   --disable-guest-base) guest_base="no"
   ;;
+  --enable-user-pie) user_pie="yes"
+  ;;
+  --disable-user-pie) user_pie="no"
+  ;;
   --enable-uname-release=*) uname_release="$optarg"
   ;;
   --sparc_cpu=*)
@@ -679,6 +684,8 @@ echo "  --disable-bsd-user       disable all BSD usermode emulation targets"
 echo "  --enable-guest-base      enable GUEST_BASE support for usermode"
 echo "                           emulation targets"
 echo "  --disable-guest-base     disable GUEST_BASE support"
+echo "  --enable-user-pie        build usermode emulation targets as PIE"
+echo "  --disable-user-pie       do not build usermode emulation targets as PIE"
 echo "  --fmod-lib               path to FMOD library"
 echo "  --fmod-inc               path to FMOD includes"
 echo "  --oss-lib                path to OSS library"
@@ -1707,6 +1714,7 @@ echo "Documentation     $docs"
 echo "uname -r          $uname_release"
 echo "NPTL support      $nptl"
 echo "GUEST_BASE        $guest_base"
+echo "PIE user targets  $user_pie"
 echo "vde support       $vde"
 echo "IO thread         $io_thread"
 echo "Linux AIO support $linux_aio"
@@ -2336,6 +2344,12 @@ if test "$target_softmmu" = "yes" ; then
   esac
 fi
 
+if test "$target_user_only" = "yes" -a "$static" = "no" -a \
+	"$user_pie" = "yes" ; then
+  cflags="-fpie $cflags"
+  ldflags="-pie $ldflags"
+fi
+
 if test "$target_softmmu" = "yes" -a \( \
         "$TARGET_ARCH" = "microblaze" -o \
         "$TARGET_ARCH" = "cris" \) ; then
Signature · 1 line
1.6.4.2
#3 lore raw

Re: [Qemu-devel] [PATCH 3/3] Add configure option to compile user targets as PIE

Quoted 2 lines · Kirill A. Shutemov

Build uset targers as true PIE if user want to keep qemu self-virtualizable.

With PIE enabled, I get the following error:
LINK ppc-linux-user/qemu-ppc
/usr/bin/ld: main.o: relocation R_X86_64_TPOFF32 against `thread_env' can not be used when making a shared object; recompile with -fPIC main.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

Maybe -fPIC should be used as well?

#4 lore raw

Re: [Qemu-devel] [PATCH 3/3] Add configure option to compile user targets as PIE

Quoted 5 lines · Blue Swirl
Quoted 2 lines · Kirill A. Shutemov

Build uset targers as true PIE if user want to keep qemu self-virtualizable.

Quoted 9 lines

With PIE enabled, I get the following error:
 LINK  ppc-linux-user/qemu-ppc
/usr/bin/ld: main.o: relocation R_X86_64_TPOFF32 against `thread_env' can not be used when making a shared object; recompile with -fPIC main.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

Maybe -fPIC should be used as well?

Your binutils(fedora, I guess) is buggy. Please apply this patch or do not enable PIE:
http://git.altlinux.org/people/kas/packages/binutils.git?p=binutils.git;a=blob;f=binutils-2.19.51.0.14-alt-tls-pie.patch

#5 lore raw

Re: [Qemu-devel] [PATCH 3/3] Add configure option to compile user targets as PIE

Quoted 13 lines · Kirill A. Shutemov
Quoted 12 lines · Blue Swirl
Quoted 2 lines · Kirill A. Shutemov

Build uset targers as true PIE if user want to keep qemu self-virtualizable.

With PIE enabled, I get the following error:
 LINK  ppc-linux-user/qemu-ppc
/usr/bin/ld: main.o: relocation R_X86_64_TPOFF32 against `thread_env' can not be used when making a shared object; recompile with -fPIC main.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

Quoted 7 lines
Quoted 2 lines

Maybe -fPIC should be used as well?

Your binutils(fedora, I guess) is buggy. Please apply this patch or do not enable PIE:
http://git.altlinux.org/people/kas/packages/binutils.git?p=binutils.git;a=blob;f=binutils-2.19.51.0.14-alt-tls-pie.patch

Looks like a very recent fix (< 2 months). Can we expect that the builders already have the fixed version? Maybe the PIE toolchain check should be restored?

#6 lore raw

Re: [Qemu-devel] [PATCH 3/3] Add configure option to compile user targets as PIE

Quoted 18 lines · Blue Swirl
Quoted 16 lines · Kirill A. Shutemov
Quoted 14 lines · Blue Swirl
Quoted 2 lines · Kirill A. Shutemov

Build uset targers as true PIE if user want to keep qemu self-virtualizable.

With PIE enabled, I get the following error:
 LINK  ppc-linux-user/qemu-ppc
/usr/bin/ld: main.o: relocation R_X86_64_TPOFF32 against `thread_env' can not be used when making a shared object; recompile with -fPIC main.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

Maybe -fPIC should be used as well?

Quoted 7 lines
Quoted 4 lines

Your binutils(fedora, I guess) is buggy. Please apply this patch or do not enable PIE:
http://git.altlinux.org/people/kas/packages/binutils.git?p=binutils.git;a=blob;f=binutils-2.19.51.0.14-alt-tls-pie.patch

Looks like a very recent fix (< 2 months). Can we expect that the builders already have the fixed version?

This is a regression, so I guess most of distributions have sane binutils, except distributions which use fedora-based binutils(like ALT Linux).

Quoted 1 line

Maybe the PIE toolchain check should be restored?

Binutils(and other part of toolchain) can be broken in many ways. Provide test for every possible bug is overkill for qemu.