HexHive / retrowrite

RetroWrite -- Retrofitting compiler passes through binary rewriting
Other
655 stars 78 forks source link

Errors on LD #5

Closed yaghmr closed 2 years ago

yaghmr commented 4 years ago

I'm trying this command

$ gcc ls-basan-instrumented.s -lasan -o ls-basan-instrumented

and I get these errors:

/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':(.text+0x20): undefined reference to `main' /tmp/ccEO4Rw4.o: In function `_obstack_newchunk': (.text+0x276): undefined reference to `.L153e0' (.text+0x4a3): undefined reference to `.L15400' /tmp/ccEO4Rw4.o: In function `_obstack_free': (.text+0x5fb): undefined reference to `.L15400' /tmp/ccEO4Rw4.o: In function `_obstack_begin': (.text+0x94): undefined reference to `.L15420' /tmp/ccEO4Rw4.o: In function `_obstack_begin_1': (.text+0x159): undefined reference to `.L15420' /tmp/ccEO4Rw4.o:(.data+0x1f0): undefined reference to `.LCc2b0' /tmp/ccEO4Rw4.o:(.data+0x260): undefined reference to `.LC153a0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x0): undefined reference to `.LC6530' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x8): undefined reference to `.LC6c70' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x10): undefined reference to `.LC6540' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x18): undefined reference to `.LC6d30' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x20): undefined reference to `.LC6070' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x28): undefined reference to `.LC6cd0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x30): undefined reference to `.LC6080' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x38): undefined reference to `.LC6d90' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x40): undefined reference to `.LCb5f0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x48): undefined reference to `.LCb9a0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x50): undefined reference to `.LCb580' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x58): undefined reference to `.LCb8f0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x60): undefined reference to `.LCb870' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x68): undefined reference to `.LCba50' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x70): undefined reference to `.LCb7f0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x78): undefined reference to `.LC7000' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x80): undefined reference to `.LC64c0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x88): undefined reference to `.LC6a90' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x90): undefined reference to `.LC64f0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x98): undefined reference to `.LC6b80' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xa0): undefined reference to `.LC5ff0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xa8): undefined reference to `.LC6b00' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xb0): undefined reference to `.LC6030' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xb8): undefined reference to `.LC6bf0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xc0): undefined reference to `.LC6560' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xc8): undefined reference to `.LC6fa0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xd0): undefined reference to `.LC6550' /tmp/ccEO4Rw4.o:(.data.rel.ro+0xd8): undefined reference to `.LC6f40' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x100): undefined reference to `.LCb080' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x108): undefined reference to `.LCb370' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x110): undefined reference to `.LCafb0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x118): undefined reference to `.LCb480' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x120): undefined reference to `.LCb100' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x128): undefined reference to `.LC6a10' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x130): undefined reference to `.LCb180' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x138): undefined reference to `.LCb770' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x140): undefined reference to `.LCaf20' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x148): undefined reference to `.LCb3f0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x150): undefined reference to `.LCb030' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x158): undefined reference to `.LCb260' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x160): undefined reference to `.LCb210' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x168): undefined reference to `.LCb660' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x170): undefined reference to `.LCb1c0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x178): undefined reference to `.LC9880' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x180): undefined reference to `.LCaff0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x188): undefined reference to `.LCb500' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x190): undefined reference to `.LCaf70' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x198): undefined reference to `.LCb2f0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x1a0): undefined reference to `.LCb140' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x1a8): undefined reference to `.LCb6f0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x1b0): undefined reference to `.LCb0c0' /tmp/ccEO4Rw4.o:(.data.rel.ro+0x1b8): undefined reference to `.LC9800' collect2: error: ld returned 1 exit status

my GCC version is:

gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

yaghmr commented 4 years ago

Additional information: /bin/ls is stripped

$ file /bin/ls /bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=9567f9a28e66f4d7ec4baf31cfbf68d0410f0ae6, stripped

vanhauser-thc commented 4 years ago

I have the same issue - but only on a few files targets, e.g. libxml2-2.9.2 (others work, all working and non working compiled freshly with the same gcc):

# gcc xmllint.s -o xmllint.bin -lz -lm -lpthread -llzma
/bin/ld: /tmp/ccMboylz.o:(.data+0x9d8): undefined reference to `.LC15520'
/bin/ld: /tmp/ccMboylz.o:(.data+0x9e0): undefined reference to `.LC151b0'
/bin/ld: /tmp/ccMboylz.o:(.data+0x9e8): undefined reference to `.LC151b0'
/bin/ld: /tmp/ccMboylz.o:(.data+0x9f0): undefined reference to `.LC15270'
collect2: error: ld returned 1 exit status

This is with gcc 7 and gcc 9.

Note that it is gcc/C code and not stripped:

# file xmllint
xmllint: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=398b42c27ccf32335def98f5ae8110f2eb2e7a6b, for GNU/Linux 3.2.0, with debug_info, not stripped
sushant94 commented 4 years ago

Thanks for the report! Your file output on xmllint seems good to me. Could you please link me/attach a copy of xmllint to help debug? (or instructions to build the same would be helpful).

vanhauser-thc commented 4 years ago

It is libxml2-2.9.2 and compiled with "./configure --disable-shared"

I have the binary attached as a gz archive:

xmllint.gz

vanhauser-thc commented 4 years ago

@sushant94 can I assist you further in fixing this?

sushant94 commented 4 years ago

@vanhauser-thc I've isolated the issue. We seem to not handle globals initialized to imported functions correctly. Working on a fix.

tosanjay commented 3 years ago

I too encountered a similar issue. I am providing the output below (including the output from the intrumentation step, if it may be of more help):

(retro) sanjay@sanjay-lap:~/tools/retrowrite$ ./retrowrite --asan ~/TeachingCodeExamples/mallocEx ~/TeachingCodeExamples/mallocEx-san.s
[*] Relocations for a section that's not loaded: .rela.dyn
[*] Relocations for a section that's not loaded: .rela.plt
[x] Could not replace value in .init_array
[x] Couldn't find valid section 200d90
[x] Couldn't find valid section 200fd8
[x] Couldn't find valid section 200fe0
[x] Couldn't find valid section 200fe8
[x] Couldn't find valid section 200ff0
[x] Couldn't find valid section 200ff8
[*] Analyzing free registers
[*] Instrumented: 39 locations
Number of free registers: [1, 2, 2, 2, 1, 2, 1, 3, 4, 3, 4, 3, 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 4, 3, 2, 3, 1, 2, 1, 0, 1, 0, 1, 0, 3, 2, 1, 2, 2]
{"rax": 26, "rflags": 39, "rdi": 22, "rdx": 14, "rsi": 9}
rflags live: 0, rflags + 0 regs: 0, rflags + rax: 0, rflags + >= 1 reg: 0
(retro) sanjay@sanjay-lap:~/tools/retrowrite$ gcc ~/TeachingCodeExamples/mallocEx-san.s -lasan -o ~/TeachingCodeExamples/malloc-instru 
/tmp/ccPfQZnK.o: In function `asan.module_ctor':
(.text+0x7a2): undefined reference to `__asan_init_v4'
collect2: error: ld returned 1 exit status
(retro) sanjay@sanjay-lap:~/tools/retrowrite$ 
diagprov commented 3 years ago

@tosanjay Your error comes from the fact the version of asan was changed. I believe this is fixed in the latest development version, but in the mean time you can fix your example by running the following:

sed -i 's/asan_init_v4/asan_init/g' ~/TeachingCodeExamples/mallocEx-san.s

between the retrowrite command (rewrite step) and reassembling with gcc. You should then be able to link successfully.

tosanjay commented 3 years ago

@tosanjay Your error comes from the fact the version of asan was changed. I believe this is fixed in the latest development version, but in the mean time you can fix your example by running the following:

sed -i 's/asan_init_v4/asan_init/g' ~/TeachingCodeExamples/mallocEx-san.s

between the retrowrite command (rewrite step) and reassembling with gcc. You should then be able to link successfully.

Thanks @diagprov It worked!