Bug 54226 - Executables compiled with -pie do not work on NetBSD/sparc or sparc
Summary: Executables compiled with -pie do not work on NetBSD/sparc or sparc
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.5.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2012-08-11 07:17 UTC by Martin Husemann
Modified: 2012-08-11 10:39 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2012-08-11 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
Description Martin Husemann 2012-08-11 07:17:41 UTC
Due to a missing -fPIC when compiling libgcc crtstuff, the binaries are not actually position independent.

On NetBSD, a "hello world" compiled with gcc -fpie -pie hello.c links to a binary like this:

a.out:     file format elf64-sparc

Program Header:
    PHDR off    0x0000000000000040 vaddr 0x0000000000000040 paddr 0x0000000000000040 align 2**3
         filesz 0x0000000000000150 memsz 0x0000000000000150 flags r-x
  INTERP off    0x0000000000000190 vaddr 0x0000000000000190 paddr 0x0000000000000190 align 2**0
         filesz 0x0000000000000017 memsz 0x0000000000000017 flags r--
    LOAD off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**20
         filesz 0x0000000000000b34 memsz 0x0000000000000b34 flags r-x

but of course the first section is not mapped at 0.

Fix is simple: set TARGET_LIBGCC2_CFLAGS in libgcc/config.host (via a new additional ${tmake_file}) to -fPIC.

I can provide a simple patch doing that, however, I fail to see why this would be a NetBSD speciality, i.e. why it works without that flags on other systems - or maybe it just does not work there as well and we need a broader fix (same solution, different match in config.host).
Comment 1 Andreas Schwab 2012-08-11 08:25:43 UTC
gcc 4.5 is no longer maintained, please try out 4.6 at least.

*-*-netbsd* (in 4.5/4.6) already uses t-netbsd and t-libgcc-pic (in gcc/config) which causes both libgcc and crtstuff to be compiled with -fPIC.  In 4.7 and later this is set by t-crtstuff-pic and t-libgcc-pic in libgcc/config.
Comment 2 Martin Husemann 2012-08-11 10:39:35 UTC
I see - that is fine, and sorry I did not check newer branches before.