Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Details on gdb memory access complaint

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
like image 978
diciu Avatar asked Nov 25 '10 12:11

diciu


People also ask

How do you examine your memory?

You can use the command x (for "examine") to examine memory in any of several formats, independently of your program's data types. Use the x command to examine memory.

What is X S in GDB?

x/d ADDRESS will print the value as an integer; x/i ADDRESS as an instruction; x/s ADDRESS as a string. x/8xw ADDRESS will print 8 four-byte words in hexadecimal format.

How do you step out in GDB?

Bookmark this question. Show activity on this post. Those who use Visual Studio will be familiar with the Shift + F11 hotkey, which steps out of a function, meaning it continues execution of the current function until it returns to its caller, at which point it stops.


1 Answers

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

like image 144
Martin v. Löwis Avatar answered Sep 22 '22 22:09

Martin v. Löwis