[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: -fPIE and stuff



On Mon, Feb 13, 2012 at 10:29:23PM +0000, Uoti Urpala wrote:
> Kurt Roeckx <kurt <at> roeckx.be> writes:
> > So my understanding is that you want to build libraries with -fPIE
> > instead of -fPIC, and that that creates a different ABI?
> 
> What affects the ABI is compiling the library in a way that does not support
> copy relocations. This can be done with visibility attributes or linker flags
> like -Bsymbolic. Then the main program needs to be compiled with -fPIE or it
> will see different symbol addresses (for the library the symbol will refer to
> the "original" one, for the main program it will refer to a copy at a different
> address).

It was always my understanding that protected wasn't useful,
because it's even more expensive.

> Here's an example of a library that requires -fPIE for the main program:
> $$$$$$$$$$$$$$ cat lib.c
> #include <stdio.h>
> 
> __attribute__((visibility("protected"))) int end_of_list_sentinel;
> void f(int **list)
> {
>     for (int i = 0; list[i] != &end_of_list_sentinel; i++)
>         printf("%d\n", *list[i]);
> }
> $$$$$$$$$$$$$$ cat main.c
> #include <stdio.h>
> 
> extern int end_of_list_sentinel;
> void f(int **list);
> 
> int main(void)
> {
>     f((int *[]){&(int){42}, &end_of_list_sentinel});
>     return 0;
> }
> $$$$$$$$$$$$$$ gcc --std=gnu99 -Wall -shared -fPIC -o lib.o lib.c
> $$$$$$$$$$$$$$ gcc -Wall -fPIE main.c lib.o
> $$$$$$$$$$$$$$ LD_LIBRARY_PATH=. ./a.out
> 42
> $$$$$$$$$$$$$$ gcc -Wall main.c lib.o
> $$$$$$$$$$$$$$ LD_LIBRARY_PATH=. ./a.out
> 42
> 0
> 1
> Segmentation fault

As far as I understand things, this is supposed to work, and might
be a bug in the toolchain or dynamic linker.  Which might also
mean that they're trying to make use of a bug in the toolchain.

The protected visibility should act just like default outside the
DSO.  The only difference should be that the DSO itself should
only look for the symbol in it's own DSO, and so can be optimized
based on that.


Kurt


Reply to: