6

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
Phonon
  • 12,549
  • 13
  • 64
  • 114
diciu
  • 29,133
  • 4
  • 51
  • 68
  • Have you tried dumping the registers (info registers) or the stack before and, if different, after the "stepi" command? Why do you care if the executable runs ok? – AlastairG Dec 01 '10 at 15:59
  • I can dump the registers before and after the "stepi" but I see nothing relevant. I care because I don't like unexplained behavior. – diciu Dec 02 '10 at 06:45
  • How did you call 'ld' and 'gcc' (and 'as' of course)? Which flags did you use? – Bart Dec 03 '10 at 08:18
  • @Bart - I've listed the commands in the answer. – diciu Dec 03 '10 at 09:48

2 Answers2

2

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).

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • thanks for the answer. Now I see the problem - a call to frame_register_unwind on the binary created by ld returns 0x0. I've no idea why, but it's a good start. – diciu Dec 06 '10 at 07:09
1

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.

Community
  • 1
  • 1
Scott Wisniewski
  • 24,561
  • 8
  • 60
  • 89
  • thanks for the answer, I can indeed link using ld with the extra arguments gathered this way. – diciu Dec 06 '10 at 07:16