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

Re: -fPIE and stuff



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).

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



Reply to: