2

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!

Milo Banks
  • 21
  • 2
  • 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 01: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 01: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 03:07
  • (Cont’d) …  (3) People will say that this question is off-topic here, and belongs on [SO]. I believe that it’s on-topic here, but I agree that it is likely to get answered more quickly and accurately on [SO]. 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 03:07
  • Yes, I have tried changing the order. – Milo Banks Nov 13 '18 at 03:49

1 Answers1

0

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.

V13
  • 4,551
  • 1
  • 15
  • 20
  • 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 02:27
  • What are you trying to achieve by compiling/linking your .bin with -fPIC? – V13 Nov 14 '18 at 00:29