For learning purposes I've created (using a hex editor and some macros) the following ELF file:
hello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
xxd hello.o:
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
00000010: 0100 3e00 0100 0000 0000 0000 0000 0000 ..>.............
00000020: 0000 0000 0000 0000 4000 0000 0000 0000 ........@.......
00000030: 0000 0000 4000 3800 0000 4000 0400 0300 [email protected]...@.....
00000040: 0c00 0000 0300 0000 2000 0000 0000 0000 ........ .......
00000050: 0000 0000 0000 0000 4001 0000 0000 0000 ........@.......
00000060: 0600 0000 0000 0000 0000 0000 0000 0000 ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000080: 0600 0000 0100 0000 2200 0000 0000 0000 ........".......
00000090: 0000 0000 0000 0000 4601 0000 0000 0000 ........F.......
000000a0: 0e00 0000 0000 0000 0000 0000 0000 0000 ................
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000000c0: 1400 0000 0200 0000 2000 0000 0000 0000 ........ .......
000000d0: 0000 0000 0000 0000 5401 0000 0000 0000 ........T.......
000000e0: 1800 0000 0000 0000 0300 0000 0000 0000 ................
000000f0: 0000 0000 0000 0000 1800 0000 0000 0000 ................
00000100: 1c00 0000 0300 0000 2000 0000 0000 0000 ........ .......
00000110: 0000 0000 0000 0000 6c01 0000 0000 0000 ........l.......
00000120: 2600 0000 0000 0000 0000 0000 0000 0000 &...............
00000130: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000140: 6865 6c6c 6f00 4865 6c6c 6f2c 2077 6f72 hello.Hello, wor
00000150: 6c64 2100 0000 0000 1100 0100 0000 0000 ld!.............
00000160: 0000 0000 0e00 0000 0000 0000 6865 6c6c ............hell
00000170: 6f00 2e64 6174 6100 2e73 7472 7461 6200 o..data..strtab.
00000180: 2e73 796d 7461 6200 2e73 6873 7472 7461 .symtab..shstrta
00000190: 6200 b.
I wish to link it together with a C program (main.c):
#include <stdio.h>
extern char hello[];
int main() {
printf("%s\n", hello);
return 0;
}
However, running gcc main.o hello.o
shows an error:
/usr/bin/ld: main.o: in function `main':
main.c:(.text+0x7): undefined reference to `hello'
collect2: error: ld returned 1 exit status
readelf -s hello.o:
Symbol table '.symtab' contains 1 entry:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 14 OBJECT GLOBAL DEFAULT 1 hello
(readelf output is similar to object files created by GCC)
Why doesn't GCC accept my created object file?