Skip to content

Support PIC and PIE for AArch64, RISC-V and x86-64 #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

xavierleroy
Copy link
Contributor

This PR adds support for generating position-independent executables (PIE), Position-independent code (PIC) and shared libraries for AArch64, RISC-V and x86-64 (Linux and macOS; no Cygwin at this point).

For AArch64 and x86-64, it builds on the existing macOS support, which requires all symbols not defined in the current compilation unit to be accessed via the GOT, not with an absolute address. We just need to adjust the criterion used to choose between "access via the GOT or the PLT" and "access at fixed addresses":

  • for PIC under ELF: all non-static symbols go through the GOT
  • for PIE under ELF and in all cases under macOS: all symbols not defined in the current unit go through the GOT
  • for non-PIC, non-PIE under ELF: all symbols use fixed (at static link-time) addresses.

The RISC-V port was adapted to use the same per-symbol approach. It already had some support for PIC code, but it was less precise (all symbols were using the GOT), turned off, and slightly buggy.

The other CompCert target platforms (ARM, PowerPC, x86-32) lack PC-relative addressing, making it significantly harder to generate PIC code. We will stick with statically-linked code for the time being.

PIE was already the default for macOS. This PR makes it the default for Linux and BSD, since OpenBSD and most Linux distributions are PIE by default. This can be turned off with -fno-pie at code generation time and -no-pie at link time.

I didn't try to quantify the performance degradation caused by PIE. It's probably low, as it only affects accesses to global variables defined in another compilation unit.

With a default installation we can set `stdlib=../lib/compcert`.

This enables relocating a CompCert installation without changing compcert.ini.

This can be useful for binary distributions such as the Coq platform for Windows.
Like we did already for AArch64/macOS.

BSD and macOS use clang by default; gcc is an external package.
`-fpic` is not implemented yet and just ignored (with an "unsupported" warning).
`-shared` is passed to the GNU linker.
This is used on ELF platforms to determine which symbols are relocatable.
The same ELF target platforms that support PIC also use PIE by default.
PIE code generation is a small variation on PIC code generation.
Controllable via `-fpie` / `-fno-pie` (for code generation)
and `-pie` / `-no-pie` (for linking).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant