Question

I have an object file compiled using as (from assembler code).

If I link it using ld, when I try to stepi (or nexti) gdb complains about memory access at address 0x0. If I link it using gcc, all is fine.

I am guessing the problem is caused by ld, which produces fewer sections when compared to the linking result of gcc.

Is there a way to configure gdb to be more verbose so I can maybe figure out what's wrong with the executable?

(gdb) b main
Breakpoint 1 at 0x100000f8e
(gdb) r
Breakpoint 1, 0x0000000100000f8e in main ()
(gdb) x/10i $pc
0x100000f8e <main>: fbld   0x6c(%rip)        # 0x100001000 <data1>
0x100000f94 <main+6>: fimul  0x7a(%rip)        # 0x100001014 <data2>
0x100000f9a <main+12>: fbstp  0x60(%rip)        # 0x100001000 <data1>
0x100000fa0 <main+18>: mov0x0    $0x2000001,%rax
0x100000fa7 <main+25>: mov    $,%rdi
0x100000fae <main+32>: syscall 
(gdb) si
Cannot access memory at address 0x0
0x0000000100000f94 in main ()

PS: The executable itself runs as expected in both versions.

Later edit: commands i've used to compile:

as -arch x86_64 src.s -o src.o
ld -e _main -arch x86_64 src.o -o src
gcc -o src src.o
Was it helpful?

Solution

gdb has a "show debug" command, giving various internal debug settings. E.g. "set debug target 1" will turn on tracing for gdb's interaction with the target process. You might want to experiment with every flag they have (there aren't that many).

OTHER TIPS

GCC doesn't actually do the linking, it just calls ld on your behalf. The options it's providing must be different from the ones you are using.

Per this thread:

How to get GCC linker command?

You should be able to see the ld invocation's command line by running gcc -v. That should tell you how to modify your ld command line so things work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top