[csw-maintainers] ld: fatal: relocations remain against allocatable but non-writable sections

Rafael Ostertag raos at opencsw.org
Sat Jun 16 23:55:15 CEST 2012


Hi Maciej

On Sat, Jun 16, 2012 at 09:39:08PM +0100, Maciej (Matchek) Blizi??ski wrote:
> 2012/6/16 Rafael Ostertag <raos at opencsw.org>:
> > On Sat, Jun 16, 2012 at 09:02:55PM +0100, Maciej (Matchek) Blizi??ski wrote:
> >> When building gcc-4.7.1, I ran against this error:
> >>
> >> ld: fatal: relocations remain against allocatable but non-writable sections
> >> collect2: error: ld returned 1 exit status
> >>
> >> It happens when linking the libgnat shared object. It's reproducible,
> >> try a gcc4 build from the repository and you'll see it.
> >
> > SPARC or x86? If x86, 32bit or 64bit?
> >
> > My first bet would be a missing -pic
> 
> Just checked, sparc 32-bit (sparcv8+).

Ok, it's still building and I'm tuning out. However, I can show how you
reproduce the error, which helps to understand what's wrong:

1. Create a C source file with this content:

static int c = 1;

int f(int a) {
        return a+c;
}

and name it liba.c

2. Create a shared object out of it like this

gcc -shared -o liba.so liba.c

3. You will immediately receive

Text relocation remains                         referenced
    against symbol                  offset      in file
c                                   0x4         /var/tmp//ccqHaWcX.o
ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status

So, what's wrong? If you do

gcc -fpic -shared -o liba.so liba.c

everything works. 

With the first call to gcc without -fpic, you told the compiler and linker to
create a shareable object, but it is not position independent, i.e. during load
time the text section (where the code is) will be adjusted. Using -fpic, the
text section won't be touched, but code is inserted to get the address of the
symbol 'c' during runtime (in depth discussion about this can be found in [1]
and [2]).

Maybe this shed some light on the issue at hand, and you can fix it. Else I'll
be glad to help tomorrow.

cheers
rafi

[1] http://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries/ 
[2] http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/

> _______________________________________________
> maintainers mailing list
> maintainers at lists.opencsw.org
> https://lists.opencsw.org/mailman/listinfo/maintainers
> .:: This mailing list's archive is public. ::.
> 


More information about the maintainers mailing list