I am trying to compile some GAS code for a project using the GCC gnu compiler. Here is how I am compiling it:
gcc -c boot.s -o boot.o -fPIC
After I compile my kernel.c file with the -fPIC
argument, I try to link it with this command:
gcc -N -T linker.ld -o Slack\ Berry.bin -ffreestanding -nostdlib kernel.o boot.o -lgcc
It comes up with:
/usr/bin/ld: boot.o: relocation R_X86_64_32 against '.multiboot' can not be used when making a PIE object; recompile with -fPIC
This leads me to think that it is not compiling my GAS code with -fPIC
. How can I fix this?
First of all you probly need
-fPIE
rather than-fPIC
.-fPIE
allows compiler to generate more efficient code but can only be used for code that's part of main executable (not shared library).Now both
-fPIC
and-fPIE
are compiler-only flags and are not passed to assembler. You'll need to explicitly use PIC-specific mnemonics in your assembly code instead of position-dependent calls and branches e.g instead ofuse
(normally to get the syntax I need I just run
gcc -fPIE -S -o-
on matching C snippet).