Why does arm-linux-androideabi-gcc enforce -fpic?

2019-02-25 07:29发布

问题:

I was surprised to see that gcc enforces code to be position independent, even if such flag wasn't provided explicitly in the command line.

I suspect it might have to do with certain expectations from Android's dynamic loader (e.g. expectations on relocation types and freedom to put code wherever it wants) but I am not certain.

Can anybody explain why that really is?

$ arm-linux-androideabi-gcc --version | grep GCC
arm-linux-androideabi-gcc (GCC) 4.4.3

$ arm-linux-androideabi-gcc -v -S main.c |& grep fpic
 /home1/local64/android-toolchain/bin/../libexec/gcc/arm-linux-androideabi/4.4.3/cc1 -quiet -v -iprefix /home1/local64/android-toolchain/bin/../lib/gcc/arm-linux-androideabi/4.4.3/ -isysroot /home1/local64/android-toolchain/bin/../sysroot main.c -mbionic -fpic -quiet -dumpbase main.c -march=armv5te -mfloat-abi=soft -mfpu=vfp -auxbase main -version -o main.s

回答1:

Starting with Android 4.1, Google is forcing full ASLR to overcome common security exploits, see this article for more details.

Position Independent Code (PIC) is required for this to work but also PIE (Position Independent Executable) too.