Android is quiet peculiar in that it default to PIC, not PIE. To enable PIE only optimizations on has to explicitly pass -fPIE. I just checked that behaviour with r9d sdk using gcc 4.8 -E -dM.
Currently that is implemented with some custom logic in the driver. The attached patch moves some of that to isPICDefault and isPIEDefault. The changes are
- Declare that android isPIEDefault and isPICDefault
- For running -cc1, give preference to isPICDefault over isPIEDefault, since that is the most restrictive.
The main behaviour is unchanged:
- clang -target arm-linux-android -S test.c -> -mrelocation-model pic -pic-level 1
- clang -target arm-linux-android -S test.c -fPIC -> -mrelocation-model pic -pic-level 2
- clang -target arm-linux-android -S test.c -fPIE -> -mrelocation-model pic -pic-level 2 -pie-level 2
The one change (which is reflected in the tests) is that enabling sanitizers doesn't force PIE. It is quiet surprising that currently enabling the sanitizers moves us from PIC to PIE.