I am trying to link some files. Here is my command:

gcc -T linker.ld -o Stack\ Berry.bin -ffreestanding -O2 -nostlib kernel.o boot.o -fPIC -lgcc

How ever, I get this error:

/usr/bin/ld: boot.o: relocation R_X86_64_32 against `.multiboot' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output

I have included the -fPIC argument, have I just put it in the wrong place? I haven't been able to find any thing for the syntax for this flag. Thanks!

  • 1
    The -fPIC option must be applied during the compile phase (the command that generates the object files) I think - rather than the link phase, as you are showing – steeldriver Nov 13 '18 at 1:32
  • It doesn't seem to work. I tried it on compiling the GAS code (not a arg), and the command that generates the object file. It does not seem to work. gcc -fPIC -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra – Milo Banks Nov 13 '18 at 1:58
  • (1) My first guess (and it is only a guess) was that it’s a command-line order problem. Have you tried changing the order of the command line? (2) Actually reading the “recompile with -fPIC” error make me doubt my first guess and believe that @steeldriver has the right idea. … (Cont’d) – Scott Nov 13 '18 at 3:07
  • (Cont’d) …  (3) People will say that this question is off-topic here, and belongs on Stack Overflow. I believe that it’s on-topic here, but I agree that it is likely to get answered more quickly and accurately on Stack Overflow. If you don’t get an answer here, and the question doesn’t get migrated, I suggest that you flag it and ask to have it moved. Please don’t just repost the question. – Scott Nov 13 '18 at 3:07
  • Yes, I have tried changing the order. – Milo Banks Nov 13 '18 at 3:49

Your boot.o has a .multiboot elf section, which is incompatible with -fPIC. You can try to compile boot.o with -fPIC but that should fail.

See https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#OS-image-format for the header.

On the other hand, there's little sense in creating a bootable object with -fPIC. Even if there was a "dynamic loader", there can't be anything to dynamically link against during the boot process.

  • I could only find info on the header and the flag value, and it matched up with my boot.s. BTW the boot.o is an object file, non-compilable, as it should be boot.s. – Milo Banks Nov 13 '18 at 2:27
  • What are you trying to achieve by compiling/linking your .bin with -fPIC? – V13 Nov 14 '18 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.