Skip to contentSkip to conversation

PPC: Fix linker scripts on ppc hosts

PATCH In review Alexander Graf 4 participants 19 messages Dec 12, 2011 – Dec 20, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PPC: Fix linker scripts on ppc hosts

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

---
 ppc.ld   |   16 ++++++++++++++--
 ppc64.ld |   16 ++++++++++++++--
 2 files changed, 28 insertions(+), 4 deletions(-)
ppc.ld +14 -2
index 69aa3f2..2a0dcad 100644
@@ -49,8 +49,20 @@ SECTIONS
   .rela.sbss2     : { *(.rela.sbss2 .rela.sbss2.* .rela.gnu.linkonce.sb2.*) }
   .rel.bss        : { *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*) }
   .rela.bss       : { *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) }
-  .rel.plt        : { *(.rel.plt) }
-  .rela.plt       : { *(.rela.plt) }
+  .rel.plt      :
+  {
+    *(.rel.plt)
+    PROVIDE (__rel_iplt_start = .);
+    *(.rel.iplt)
+    PROVIDE (__rel_iplt_end = .);
+  }
+  .rela.plt       :
+  {
+    *(.rela.plt)
+    PROVIDE (__rela_iplt_start = .);
+    *(.rela.iplt)
+    PROVIDE (__rela_iplt_end = .);
+  }
   .init           :
   {
     KEEP (*(.init))
ppc64.ld +14 -2
index 0a7c0dd..e2dafa0 100644
@@ -54,8 +54,20 @@ SECTIONS
       *(.rela.sbss2 .rela.sbss2.* .rela.gnu.linkonce.sb2.*)
       *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
     }
-  .rel.plt        : { *(.rel.plt) }
-  .rela.plt       : { *(.rela.plt) }
+  .rel.plt      :
+  {
+    *(.rel.plt)
+    PROVIDE (__rel_iplt_start = .);
+    *(.rel.iplt)
+    PROVIDE (__rel_iplt_end = .);
+  }
+  .rela.plt       :
+  {
+    *(.rela.plt)
+    PROVIDE (__rela_iplt_start = .);
+    *(.rela.iplt)
+    PROVIDE (__rela_iplt_end = .);
+  }
   .rela.tocbss	  : { *(.rela.tocbss) }
   .init           :
   {
Signature · 1 line
1.6.0.2
#1 lore raw
Quoted 3 lines · Alexander Graf

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

r~

#2 lore raw
Quoted 8 lines · Richard Henderson
Quoted 3 lines · Alexander Graf

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

I tried to play with that as well but couldn't get it to work. If you do, I'd be more than happy to get rid of them :)

Alex

#3 lore raw
In reply to Richard Henderson
Quoted 8 lines
Quoted 3 lines

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

Now that we have the automatic GUEST_BASE stuff you shouldn't need to do either.

Paul

#4 lore raw
Quoted 11 lines · Paul Brook
Quoted 8 lines
Quoted 3 lines

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

Now that we have the automatic GUEST_BASE stuff you shouldn't need to do either.

If it was working, yes :)

Alex

#5 lore raw
Quoted 10 lines · Paul Brook
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

Quoted 4 lines
Quoted 2 lines

Now that we have the automatic GUEST_BASE stuff you shouldn't need to do either.

If it was working, yes :)

What doesn't work? I put a fair amout of effort into making it automatically pick a sensible value. If there's some reason that won't work then you probably want to be using -R.

Paul

#6 lore raw
Quoted 10 lines · Paul Brook
Quoted 10 lines · Paul Brook
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Now that we have the automatic GUEST_BASE stuff you shouldn't need to do either.

If it was working, yes :)

What doesn't work? I put a fair amout of effort into making it automatically pick a sensible value. If there's some reason that won't work then you probably want to be using -R.

IIRC mmap'ing files would break with 32-on-64, but I'd have to check up on the details. I ended up passing MAP_32BIT to all linux-user mmap calla for 32-on-x86_64, but that doesn't work with -R.

But either way, this patch fixes an immediate build failure on ppc hosts for us and should regardless be applied to 1.0-stable. I would also vote for adding it to HEAD now and go for the removal of all linker scripts later, if we're sure we don't have any regressions.

Alex

#7 lore raw
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

Now that we have the automatic GUEST_BASE stuff you shouldn't need to do either.

If it was working, yes :)

Quoted 7 lines
Quoted 3 lines

What doesn't work? I put a fair amout of effort into making it automatically pick a sensible value. If there's some reason that won't work then you probably want to be using -R.

IIRC mmap'ing files would break with 32-on-64, but I'd have to check up on the details. I ended up passing MAP_32BIT to all linux-user mmap calla for 32-on-x86_64, but that doesn't work with -R.

Hmm, I thought we'd fixed that. It's the reason h2g_valid exists.

Either way it should definitely work with -R. I specifically added that to avoid problems with the host mmap picking inconvenient addresse.

MAP_32BIT is an unconsionable hack, and doesn't exist on other 64-bit hosts.

Paul

#8 lore raw
Quoted 22 lines · Paul Brook
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

Now that we have the automatic GUEST_BASE stuff you shouldn't need to do either.

If it was working, yes :)

Quoted 7 lines
Quoted 3 lines

What doesn't work? I put a fair amout of effort into making it automatically pick a sensible value. If there's some reason that won't work then you probably want to be using -R.

IIRC mmap'ing files would break with 32-on-64, but I'd have to check up on the details. I ended up passing MAP_32BIT to all linux-user mmap calla for 32-on-x86_64, but that doesn't work with -R.

Quoted 6 lines

Hmm, I thought we'd fixed that. It's the reason h2g_valid exists.

Either way it should definitely work with -R. I specifically added that to avoid problems with the host mmap picking inconvenient addresse.

MAP_32BIT is an unconsionable hack, and doesn't exist on other 64-bit hosts.

Yes, I agree. I just sat down once more and checked if I could get it working with -R and everything seems fine so far.

Is there any particular reason we're not reserving all of the possible 32 bit address space for 32-on-64 guests? The way it's today with the default being completely broken and -R fixing everything if you give it a large enough value is ... suboptimal.

I put this patch onto my tree to make sure we always actually use the -R feature:

Alex

commit ac233b323ad7e498c665e8c74df7e44de4a542c0

Author: Alexander Graf <[email protected]>
Date:   Wed Dec 14 00:33:28 2011 +0100

linux-user: reserve 4GB of vmem for 32-on-64

When running 32-on-64 bit guests, we should always reserve as much virtual memory as we possibly can for the guest process, so it can never overlap with QEMU address space.

Fortunately we already have the infrastructure for that. All that's missing is some sane default value to also make use of it!

linux-user/main.c +11 -0
index 788ff98..3ffee40 100644
@@ -48,8 +48,19 @@ unsigned long mmap_min_addr;
 #if defined(CONFIG_USE_GUEST_BASE)
 unsigned long guest_base;
 int have_guest_base;
+#if (TARGET_LONG_BITS == 32) && (HOST_LONG_BITS == 64)
+/*
+ * When running 32-on-64 we should make sure we can fit all of the possible
+ * guest address space into a contiguous chunk of virtual host memory.
+ *
+ * This way we will never overlap with our own libraries or binaries or stack
+ * or anything else that QEMU maps.
+ */
+unsigned long reserved_va = 0xf7000000;
+#else
 unsigned long reserved_va;
 #endif
+#endif
 
 static void usage(void);
 extern int use_stopflag;
#9 lore raw
Quoted 12 lines
Quoted 11 lines
Quoted 3 lines

IIRC mmap'ing files would break with 32-on-64, but I'd have to check up on the details. I ended up passing MAP_32BIT to all linux-user mmap calla for 32-on-x86_64, but that doesn't work with -R.

Hmm, I thought we'd fixed that. It's the reason h2g_valid exists.

Either way it should definitely work with -R. I specifically added that to avoid problems with the host mmap picking inconvenient addresse.

MAP_32BIT is an unconsionable hack, and doesn't exist on other 64-bit hosts.

Quoted 7 lines

Yes, I agree. I just sat down once more and checked if I could get it working with -R and everything seems fine so far.

Is there any particular reason we're not reserving all of the possible 32 bit address space for 32-on-64 guests? The way it's today with the default being completely broken and -R fixing everything if you give it a large enough value is ... suboptimal.

-R doesn't actually fix everything :-)

In particular if you use ulimit -v then there's a good chance it will break completely. Having said that, it's not hard to check ulimit -v first.

I still think the current config should work. The automagical guest_base isn't quite as reliable as -R (the guest and host spaces probably overlap) but it should be comparable to 32-on-32.

If we do default to -R 4G for 32-on-64, then there's a good argument for enabling it by default for all combinations. For 32-bit hosts you can either try to guess the size of the host user address space, or just start by trying 3G and if that doesn't work progressively fall back to 2G, 1G, then current behavior.

For 64-on-64 maybe pick a large value, remebering that the reservation probably isn't completely free.

Paul

#10 lore raw
In reply to Alexander Graf
Quoted 1 line · Alexander Graf

+unsigned long reserved_va = 0xf7000000;

So why 0xf7000000 rather than the whole of the 32 bit space?

(In particular for ARM targets we want to be able to mmap at 0xFFFFxxxx...)

-- PMM

#11 lore raw
Quoted 7 lines · Peter Maydell
Quoted 1 line · Alexander Graf

+unsigned long reserved_va = 0xf7000000;

So why 0xf7000000 rather than the whole of the 32 bit space?

(In particular for ARM targets we want to be able to mmap at 0xFFFFxxxx...)

Because that didn't work for me. We still need the commpage above our reserved memory range, so this was the highest value that was safe for me.

Alex

#12 lore raw
Quoted 4 lines · Alexander Graf
Quoted 3 lines · Peter Maydell
Quoted 1 line · Alexander Graf

+unsigned long reserved_va = 0xf7000000;

Quoted 8 lines
Quoted 4 lines

So why 0xf7000000 rather than the whole of the 32 bit space?

(In particular for ARM targets we want to be able to mmap at 0xFFFFxxxx...)

Because that didn't work for me. We still need the commpage above our reserved memory range, so this was the highest value that was safe for me.

Hmm, that suggests we didn't get the interaction of the commpage with the reserved range right -- the commpage is part of the guest memory space so it should be OK for it to be inside the reserved range.

-- PMM

#13 lore raw
Quoted 10 lines · Peter Maydell
Quoted 9 lines · Alexander Graf
Quoted 7 lines · Peter Maydell
Quoted 1 line · Alexander Graf

+unsigned long reserved_va = 0xf7000000;

So why 0xf7000000 rather than the whole of the 32 bit space?

(In particular for ARM targets we want to be able to mmap at 0xFFFFxxxx...)

Quoted 8 lines
Quoted 3 lines

Because that didn't work for me. We still need the commpage above our reserved memory range, so this was the highest value that was safe for me.

Hmm, that suggests we didn't get the interaction of the commpage with the reserved range right -- the commpage is part of the guest memory space so it should be OK for it to be inside the reserved range.

That is very likely. Right now we take the reserved range and then put the commpage above it, not guaranteeing that nothing is there. I don't see what other way to do it if we want to be able to reserve less than all 32 bits of virtual address space (which might not be available!)

Alex

#14 lore raw
In reply to Paul Brook
Quoted 5 lines · Paul Brook
Quoted 3 lines

IIRC mmap'ing files would break with 32-on-64, but I'd have to check up on the details. I ended up passing MAP_32BIT to all linux-user mmap calla for 32-on-x86_64, but that doesn't work with -R.

Hmm, I thought we'd fixed that.  It's the reason h2g_valid exists

A lot of the problem is that linux-user/mmap.c isn't very clever. What happens, IIRC, is something like this:
* we pick a guest base, and happily start to hand out memory from there
* at some point, we hit a host shared library or whatever, so the kernel can't use our hinted preferred address, and picks one itself. On 64 bit kernels it seems to usually like to skip way ahead into the >4GB bit of the virtual address space, even if there's still plenty of space below 4GB
* mmap_find_vma() wrongly assumes this means there's no more memory to be had below 4GB, and starts again with a hint address at the bottom of memory
* that address is typically already used (by host lib or by a previous guest mmap). The kernel hands us back the same useless >4GB address.
* mmap_find_vma() says "ooh, same as last time" and decides this means we're out of memory.

The effect is that on a 32-on-64 config we will fail mmap() unnecessarily and in a lot of cases which work fine on 32-on-32.

The cheesy solution is to use MAP_32BIT, which I agree is a nasty hack. The proper solution would be to rewrite mmap.c to be smarter (perhaps by looking at /proc/self/maps and reserving a lot of space with PROT_NONE mappings at startup and then managing it itself), but so far nobody's done that, and MAP_32BIT is a much smaller change that improves matters in the 99% situation (ie "host is x86-64").

-- PMM

#15 lore raw
Quoted 4 lines

The proper solution would be to rewrite mmap.c to be smarter (perhaps by looking at /proc/self/maps and reserving a lot of space with PROT_NONE mappings at startup and then managing it itself), but so far nobody's done that

Yes they have. That's what -R does.

We used to try and parse /proc/self/maps. This caused more problems than it solved. It doesn't cover things like mmap_min_addr, and you have to re-parse it before every allocation in case the host libc allocated something new in between.

Paul

#16 lore raw
Quoted 6 lines · Paul Brook
Quoted 4 lines

The proper solution would be to rewrite mmap.c to be smarter (perhaps by looking at /proc/self/maps and reserving a lot of space with PROT_NONE mappings at startup and then managing it itself), but so far nobody's done that

Yes they have. That's what -R does.

-R doesn't happen by default, it requires you to specify how much you want, and it insists that the space all be in one chunk.

Quoted 4 lines

We used to try and parse /proc/self/maps.  This caused more problems than it solved.  It doesn't cover things like mmap_min_addr, and you have to re-parse it before every allocation in case the host libc allocated something new in between.

If you've used a PROT_NONE mapping to claim the space at startup, host libc doesn't override that mapping, does it?

-- PMM

#17 lore raw
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

The proper solution would be to rewrite mmap.c to be smarter (perhaps by looking at /proc/self/maps and reserving a lot of space with PROT_NONE mappings at startup and then managing it itself), but so far nobody's done that

Yes they have. That's what -R does.

-R doesn't happen by default, it requires you to specify how much you want, and it insists that the space all be in one chunk.

I've covered the defaults elsewhere in this thread.

If your 64-bit host can't find a contiguous 4G block of address space then you've much more serious issues. System policies preventing applications allocating that much address space are a different problem, and splitting the into chunks would not help.

Quoted 7 lines
Quoted 4 lines

We used to try and parse /proc/self/maps. This caused more problems than it solved. It doesn't cover things like mmap_min_addr, and you have to re-parse it before every allocation in case the host libc allocated something new in between.

If you've used a PROT_NONE mapping to claim the space at startup, host libc doesn't override that mapping, does it?

Ah, I see what you mean.

If you're solving problems other than 32-on-64 then there's some argument for allowing discontiguous blocks. I'm not convinced there's much point parsing /proc/self/maps though. Just keep calling mmap for sensible sized blocks until you either have enough address space or it fails. In the latter case maybe free some back immediately to give the host libc room to work.

The hard bit is coming up with heuristics for "sensibe size block" and "have enough". /proc/self/maps only tells you which areas of the host VM are currently mapped. What we really want to know is which areas are available to be mapped. On 32-bit hosts this may be this may be less than half of the gaps in /proc/self/maps. On 64-bit hosts it's many orders of magnitude smaller. For example x86-64 only has 47-bits of usable virtual address space.

Nested qemu is also going to make a complete mess of /proc/self/maps, though I admit you're probably going to trip over other bugs first :-)

Paul

#18 lore raw
In reply to Paul Brook
Quoted 11 lines · Paul Brook
Quoted 8 lines
Quoted 3 lines

When compiling qemu statically with multilib on PPC, we hit the same issue that commit 845f2c2812d9ed24b36c02a3d06ee83aeafe8b49 is fixing. Do the same here.

How many of these ld files can we get rid of if we use -Ttext-segment instead? Generally all we're really caring about is moving the program base around so that it doesn't conflict with the address space we want to use for the client.

Now that we have the automatic GUEST_BASE stuff you shouldn't need to do either.

...which reminds me, are we ever going to add guest_base support to the SPARC TCG targets? configure says that's the only one that doesn't currently support it...

-- PMM