首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法看到使用GDB的程序集源代码

无法看到使用GDB的程序集源代码
EN

Stack Overflow用户
提问于 2022-10-06 22:16:50
回答 1查看 93关注 0票数 1

我开始编写linux x86_64程序集代码,因此我试着像一个普通C代码一样进行调试,例如,当我运行gdb -tui ./a.out时,我得到了我的源代码,然后我可以切换寄存器来查看发生了什么,并一步步地去做等等。

但是,当我在程序集二进制文件中使用它时,情况并非如此,我只是得到了[ No Source Available ]。但是,我可以看到寄存器和生成的程序集代码(假设我做了layout next)。问题是,由于我在代码的中间使用C函数(主要是printf,以便按照我的教授的指示简化IO过程);我希望看到我的原始汇编代码。目前我已经安装了:

  • gcc :(GCC) 12.2.0
  • ld :GNU ld (GNU Binutils) 2.39.0
  • NASM版本2.15.05

为了编译和运行我运行的程序:

代码语言:javascript
运行
复制
nasm -f elf64 -o hello.o hello.asm
gcc hello.o -o hello -no-pie 
gdb ./hello

这些都是我老师告诉我们要跑的命令。我在网上读到,我可以将-g标志传递给gcc来调试我的代码;它编译得很好,但我也看不到我的源代码。我也尝试过将-g传递给nasm (同样的问题,编译,但没有源代码)

有我没有经过的旗帜/设置吗?

EN

回答 1

Stack Overflow用户

发布于 2022-10-06 22:43:26

只需告诉NASM用-g编译源代码以添加调试符号:

代码语言:javascript
运行
复制
nasm -g -f elf64 -o hello.o hello.asm
gcc -m64 hello.o -o hello -no-pie
gdb hello

一旦GDB打开,就可以用list显示源代码。

代码语言:javascript
运行
复制
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

下面是一个示例源代码

代码语言:javascript
运行
复制
; 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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73980539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档