我开始编写linux x86_64程序集代码,因此我试着像一个普通C代码一样进行调试,例如,当我运行gdb -tui ./a.out
时,我得到了我的源代码,然后我可以切换寄存器来查看发生了什么,并一步步地去做等等。
但是,当我在程序集二进制文件中使用它时,情况并非如此,我只是得到了[ No Source Available ]
。但是,我可以看到寄存器和生成的程序集代码(假设我做了layout next
)。问题是,由于我在代码的中间使用C函数(主要是printf
,以便按照我的教授的指示简化IO过程);我希望看到我的原始汇编代码。目前我已经安装了:
为了编译和运行我运行的程序:
nasm -f elf64 -o hello.o hello.asm
gcc hello.o -o hello -no-pie
gdb ./hello
这些都是我老师告诉我们要跑的命令。我在网上读到,我可以将-g标志传递给gcc来调试我的代码;它编译得很好,但我也看不到我的源代码。我也尝试过将-g传递给nasm (同样的问题,编译,但没有源代码)
有我没有经过的旗帜/设置吗?
发布于 2022-10-06 22:43:26
只需告诉NASM用-g
编译源代码以添加调试符号:
nasm -g -f elf64 -o hello.o hello.asm
gcc -m64 hello.o -o hello -no-pie
gdb hello
一旦GDB打开,就可以用list
显示源代码。
Reading symbols from hello...
(gdb) list
1 extern printf ; the C function, to be called
2
3 section .data ; Data section, initialized variables
4 msg: db "Hello world", 0 ; C string needs 0
5 fmt: db "%s", 10, 0 ; The printf format, "\n",'0'
6
7 section .text ; Code section.
8
9 global main ; the standard gcc entry point
10 main: ; the program label for the entry point
下面是一个示例源代码
; Declare needed C functions
extern printf ; the C function, to be called
section .data ; Data section, initialized variables
msg: db "Hello world", 0 ; C string needs 0
fmt: db "%s", 10, 0 ; The printf format, "\n",'0'
section .text ; Code section.
global main ; the standard gcc entry point
main: ; the program label for the entry point
push rbp ; set up stack frame, must be aligned
mov rdi,fmt
mov rsi,msg
mov rax,0 ; or can be xor rax,rax
call printf ; Call C function
pop rbp ; restore stack
mov rax,0 ; normal, no error, return value
ret ; return
https://stackoverflow.com/questions/73980539
复制相似问题