0
votes

I have a number of object files in ELF format, with the usual .text and other common sections, and I was wondering if the gnu ld or gold could be used to link a number of ELF object files into an ELF executable, even if the architecture (an 8-bit micro with a proprietary toolchain) is not known beforehand by the linker. In essence I'm asking if the linking process is, to some extent, platform independent once you have all the required obect files, or if on the contrary I will need to roll my own linker at some point.

1
While still unsure if it will work, it seems a linker script is all I need to do. ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_6.html#SEC6 It is aexplained a bit better here on an old GNU doc scoberlin.de/content/media/http/informatik/gcc_docs/ld_3.html - NeonMan

1 Answers

0
votes

No, it won't work.

A major thing the linker has to do is to handle relocations. Relocations are arch-specific:

int f(){return 42;}
$ gcc -c foo.c -o foo && readelf -r foo

Relocation section '.rela.eh_frame' at offset 0x198 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0

$ gcc -m32 -c foo.c -o foo && readelf -r foo

Relocation section '.rel.text' at offset 0x1d0 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000004  00000b02 R_386_PC32        00000000   __x86.get_pc_thunk.ax
00000009  00000c0a R_386_GOTPC       00000000   _GLOBAL_OFFSET_TABLE_

Relocation section '.rel.eh_frame' at offset 0x1e0 contains 2 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
00000020  00000202 R_386_PC32        00000000   .text
00000040  00000502 R_386_PC32        00000000   .text.__x86.get_pc_thu

$ clang -target arm-linux-gnueabi -c foo.c -o foo && readelf -r foo

Relocation section '.rel.ARM.exidx' at offset 0x104 contains 1 entry:
 Offset     Info    Type            Sym.Value  Sym. Name
00000000  0000032a R_ARM_PREL31      00000000   .text

Moreover the linker script which says how the ELF file should be generated (page size, start address, etc.) is arch-specific:

ld -m elf_x86_64 --verbose
ld -m elf_i386 --verbose
arm-linux-gnueabi-ld --verbose

If your not compiling to a static executable, the linker has to generate PLT entries as well which are native code (and thus arch-specific).

Some architecture have arch-specific segments as well (eg. .ARM.extab, .ARM.exidx).