CompCert icon indicating copy to clipboard operation
CompCert copied to clipboard

feature request: add support for -fpic

Open didickman opened this issue 11 years ago • 3 comments

with gcc:

$ cat foo.c
#include <stdio.h>
void foo(void)
{
    puts("Hello Shared World");
}
$ gcc -fpic -std=c99 -c foo.c
$ gcc -shared -o libfoo.so foo.o
$

with CompCert this seems to work:

$ ccomp -fall -c foo.c
$ ccomp -Wl,-shared -o libfoo.so foo.c
$

But it would be nice to have support for -fpic.

didickman avatar Dec 28 '14 06:12 didickman

Right, on x86-32 shared libraries can be built from code compiled "normally", without PIC. The downside is more relocation work at dynamic loading time and inability to share the code of the shared library between several processes that use it. On platforms other than x86-32, this may not work at all.

For reference, here is a summary of PIC conventions for x86-32/ELF: http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/ and more info on PIC and its close cousin PIE for OpenBSD: http://www.openbsd.org/papers/nycbsdcon08-pie/mgp00001.html

The most annoying aspect of PIC, from CompCert's back-end viewpoint, is the need to reserve register ebx. Otherwise, there is already a bit of support for not using absolute addressing modes to access global variables (the "Oindirectsymbol" pseudo-instruction), which is already used for MacOS X. This could be reused to provide PIC access to global variables. For function calls, it's a simple matter of calling the "@PLT" stubs.

xavierleroy avatar Dec 29 '14 15:12 xavierleroy

Just noting that this PR is still highly desirable from my end. Many thanks for any consideration!

didickman avatar Oct 01 '21 17:10 didickman