Skip to contentSkip to conversation

configure: Simplify alternate .text segment

PATCH Superseded Ed Maste 2 participants 4 messages Jun 19, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ed Maste

author
#0 lore raw

PATCH 1/1 configure: Simplify alternate .text segment

For bsd-user and linux-user emulation modes QEMU is linked at an alternate .text segment address, so that it's out of the way of the guest executable.

Instead of including modified linker scripts for each arch, just set the address with -Ttext-segment if that is supported, or by using sed to edit the default linker script if not.

Note that ldscripts/alpha.ld was already unused prior to this change, as it was handled by the "default placement of the application is fine" case.

---
I'm posting this patch for review, comments, and testing at the moment, and would like to obtain test coverage on as many architectures as possible to gain confidence that it's suitable. To validate the patch, build with bsd-user or linux-user enabled and compare the result of 'readelf -l' on one of the QEMU binaries with and without the patch.

 configure            |  35 +++++---
 ldscripts/alpha.ld   | 127 ---------------------------
 ldscripts/arm.ld     | 153 ---------------------------------
 ldscripts/hppa.ld    | 211 ---------------------------------------------
 ldscripts/i386.ld    | 153 ---------------------------------
 ldscripts/ia64.ld    | 209 ---------------------------------------------
 ldscripts/m68k.ld    | 175 -------------------------------------
 ldscripts/mips.ld    | 222 -----------------------------------------------
 ldscripts/ppc.ld     | 237 ---------------------------------------------------
 ldscripts/ppc64.ld   | 230 -------------------------------------------------
 ldscripts/s390.ld    | 201 -------------------------------------------
 ldscripts/sparc.ld   | 150 --------------------------------
 ldscripts/sparc64.ld | 138 ------------------------------
 ldscripts/x86_64.ld  | 180 --------------------------------------
 14 files changed, 22 insertions(+), 2399 deletions(-)
 delete mode 100644 ldscripts/alpha.ld
 delete mode 100644 ldscripts/arm.ld
 delete mode 100644 ldscripts/hppa.ld
 delete mode 100644 ldscripts/i386.ld
 delete mode 100644 ldscripts/ia64.ld
 delete mode 100644 ldscripts/m68k.ld
 delete mode 100644 ldscripts/mips.ld
 delete mode 100644 ldscripts/ppc.ld
 delete mode 100644 ldscripts/ppc64.ld
 delete mode 100644 ldscripts/s390.ld
 delete mode 100644 ldscripts/sparc.ld
 delete mode 100644 ldscripts/sparc64.ld
 delete mode 100644 ldscripts/x86_64.ld
configure +22 -13
index ad32f87..9d46354 100755
@@ -4072,9 +4072,6 @@ if test "$gcov" = "yes" ; then
   echo "GCOV=$gcov_tool" >> $config_host_mak
 fi
 
-# generate list of library paths for linker script
-$ld --verbose -v 2> /dev/null | grep SEARCH_DIR > config-host.ld
-
 # use included Linux headers
 if test "$linux" = "yes" ; then
   mkdir -p linux-headers
@@ -4437,21 +4434,33 @@ if test "$gprof" = "yes" ; then
   fi
 fi
 
-if test "$ARCH" = "tci"; then
-  linker_script=""
-else
-  linker_script="-Wl,-T../config-host.ld -Wl,-T,\$(SRC_PATH)/ldscripts/\$(ARCH).ld"
-fi
-
 if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" ; then
+  textseg_addr=
   case "$ARCH" in
-  alpha | s390x | aarch64)
-    # The default placement of the application is fine.
+  arm | hppa | i386 | ia64 | m68k | ppc | ppc64 | s390 | sparc | sparc64 | x86_64)
+    default_textseg_addr=0x400000
+    textseg_addr=0x60000000
     ;;
-  *)
-    ldflags="$linker_script $ldflags"
+  mips)
+    default_textseg_addr=0x120000000
+    textseg_addr=0x400000
     ;;
   esac
+  if [ -n "$textseg_addr" ]; then
+    try_ldflags="-Ttext-segment=$textseg_addr"
+    if $ld $try_ldflags /dev/null >/dev/null 2>&1; then
+      ldflags="$ldflags $try_ldflags"
+    else
+      # In case ld does not support -Ttext-segment, edit the default linker
+      # script via sed to set the .text start addr.  This is needed on FreeBSD
+      # at least.
+      $ld --verbose 2>/dev/null | sed \
+        -e '1,/==================================================/d' \
+        -e '/==================================================/,$d' \
+        -e "s/$default_textseg_addr/$textseg_addr/g" > config-host.ld
+      ldflags="$ldflags -Wl,-T../config-host.ld"
+    fi
+  fi
 fi
 
 echo "LDFLAGS+=$ldflags" >> $config_target_mak
ldscripts/alpha.ld deleted +0 -127

127 lines not on this page · raw message

ldscripts/arm.ld deleted +0 -153

153 lines not on this page · raw message

ldscripts/hppa.ld deleted +0 -211

211 lines not on this page · raw message

ldscripts/i386.ld deleted +0 -153

153 lines not on this page · raw message

ldscripts/ia64.ld deleted +0 -209

209 lines not on this page · raw message

ldscripts/m68k.ld deleted +0 -175

175 lines not on this page · raw message

ldscripts/mips.ld deleted +0 -222

222 lines not on this page · raw message

ldscripts/ppc.ld deleted +0 -237

237 lines not on this page · raw message

ldscripts/ppc64.ld deleted +0 -230

230 lines not on this page · raw message

ldscripts/s390.ld deleted +0 -201

201 lines not on this page · raw message

ldscripts/sparc.ld deleted +0 -150

150 lines not on this page · raw message

ldscripts/sparc64.ld deleted +0 -138

138 lines not on this page · raw message

ldscripts/x86_64.ld deleted +0 -180

180 lines not on this page · raw message

Signature · 1 line
1.7.11.5
#1 lore raw
Quoted 4 lines · Ed Maste

if test "$target_linux_user" = "yes" -o "$target_bsd_user" = "yes" ; then

configure +1 -1
+  textseg_addr=
   case "$ARCH" in
-  alpha | s390x | aarch64)

configure if test "$gprof" = "yes" ; then · line 4446

Quoted 10 lines
configure +6 -3
-    # The default placement of the application is fine.
+  arm | hppa | i386 | ia64 | m68k | ppc | ppc64 | s390 | sparc | sparc64 | x86_64)
+    default_textseg_addr=0x400000
+    textseg_addr=0x60000000
     ;;
-  *)
-    ldflags="$linker_script $ldflags"
+  mips)
+    default_textseg_addr=0x120000000
+    textseg_addr=0x400000

Correct defaults, from current binutils sources:

    arm:	0x8000
    hppa:	0x10000
    i386:	0x08048000
    ia64:	0x4000000000000000
    m68k:	0x80000000
    mips:	0x0400000
    ppc:	0x10000000 (linux),  0x01800000 (other)
    ppc64:	0x10000000
    s390:	0x00400000
    s390x:	0x80000000
    sparc:	0x100000
    sparc64:	0x100000000 (solaris), 0x100000 (other)
    x86_64:	0x400000

I can't imagine that the defaults have changed significantly from previous binutils releases.

So... what you've got is correct for x86_64 only, which means that the sed won't actually be effective.

Given the large variance in the true default, I wonder if we shouldn't ignore it entirely. I wonder if we can't edit more like this:

s/[.] = .* [+] SIZEOF_HEADERS/. = $textseg_addr + SIZEOF_HEADERS/

I note that the current mips.ld file, from whence you took your "new" address, is the same as the real default. I.e. no movement. I suspect that we can actually move the binary to 0x60000000 with the rest of the targets. Failing that, we should move mips to the "don't change" category.

I also suggest that ia64 be moved to the "don't change" category. It's default text address is 0x4000000000000000, which is as far out of the way of the guest address space as we can get.

r~

author
#2 lore raw
Quoted 4 lines · Richard Henderson

Given the large variance in the true default, I wonder if we shouldn't ignore it entirely. I wonder if we can't edit more like this:

s/[.] = .* [+] SIZEOF_HEADERS/. = $textseg_addr + SIZEOF_HEADERS/

Sounds good, although we need to pick up __executable_start too I think; from my ld --verbose:
# PROVIDE (__executable_start = 0x400000); . = 0x400000 + SIZEOF_HEADERS;

How about this?

      $ld --verbose | sed \
        -e '1,/==================================================/d' \
        -e '/==================================================/,$d' \
        -e "s/[.] = .* [+] SIZEOF_HEADERS/. = $textseg_addr + SIZEOF_HEADERS/" \
        -e "s/__executable_start = [0-9x]*/__executable_start =

$textseg_addr/" > config-host.ld

Quoted 8 lines

I note that the current mips.ld file, from whence you took your "new" address, is the same as the real default. I.e. no movement. I suspect that we can actually move the binary to 0x60000000 with the rest of the targets. Failing that, we should move mips to the "don't change" category.

I also suggest that ia64 be moved to the "don't change" category. It's default text address is 0x4000000000000000, which is as far out of the way of the guest address space as we can get.

Both sound reasonable, although I don't have an easy way to test either platform at the moment. We can always make those changes after, or I am happy to respin the patch.

#3 lore raw
Quoted 4 lines · Ed Maste

Sounds good, although we need to pick up __executable_start too I think; from my ld --verbose:
# PROVIDE (__executable_start = 0x400000); . = 0x400000 + SIZEOF_HEADERS;

Quoted 8 lines

How about this?

      $ld --verbose | sed \
        -e '1,/==================================================/d' \
        -e '/==================================================/,$d' \
        -e "s/[.] = .* [+] SIZEOF_HEADERS/. = $textseg_addr + SIZEOF_HEADERS/" \
        -e "s/__executable_start = [0-9x]*/__executable_start =

$textseg_addr/" > config-host.ld

Looks good. Perhaps even use the [0-9a-fx]* pattern for the SIZEOF_HEADERS line too.

I was initially looking at my ld --verbose and trying to think what to do with the SEGMENT_START("text-segment", 0x08048000) pattern, but that's clearly dumb, since that implies that the -Ttext-segment option exists and works. :-P

r~