linux - 如何不在 NASM 中发出局部符号,以便 GDB disas 不会停在它们上面?

标签 linux assembly linker nasm elf

我正在尝试在 Linux 上使用 nasm 编写一些汇编程序。一切都很好,但我大量使用本地符号(.loop、.else 等),这在调试时很痛苦,因为这些符号被发送到符号表,例如:

[BITS 32]
global main
section .text
main:
    do stuff
.else:
    do other stuff

将生成如下所示的反汇编:

<main>:
00000000      do stuff
<main.else>:
00000000      do other stuff

这有点烦人,因为 gdb 会认为这些都是独立的函数,所以当我“disas”时,它只会在遇到另一个标签并停止之前反汇编几条指令。

有没有办法在 linux 下使用 nasm 抑制将这些符号发送到 ELF 符号表?

最佳答案

我还没有找到直接用 nasm 来完成它的方法,但是如果你用 ld 链接你的对象,那么你就可以随意使用一个非常方便的开关. 引用自 ld 的 man page :

-x --discard-all Delete all local symbols.

-X --discard-locals Delete all temporary local symbols. (These symbols start with system-specific local label prefixes, typically .L for ELF systems or L for traditional a.out systems.)

所以如果你有,例如,这个:

section .data
    hello:     db 'Hello world!',10
    helen:     equ $-hello           
    hi:        db 'Hi!',10
    hilen:     equ $-hi
section .text
    global _start
_start:
    mov eax,4            
    mov ebx,1            
    mov ecx,hello        
    mov edx,helen                        
    int 80h
.there:
    mov eax,4
    mov ebx,1   
    mov ecx,hi
    mov edx,hilen
    int 80h
.end:
    mov eax,1
    mov ebx,0
    int 80h

然后像这样构建、链接(并运行)它:

$ nasm -g -f elf32 prog.asm && ld -x prog.o -o prog && ./prog
Hello world!
Hi!

然后,当您在 gdb 中加载它时,您会得到:

$ gdb prog
.....
Reading symbols from prog...done.
(gdb) disas _start
Dump of assembler code for function _start:
   0x08048080 <+0>: mov    $0x4,%eax
   0x08048085 <+5>: mov    $0x1,%ebx
   0x0804808a <+10>:    mov    $0x80490b8,%ecx
   0x0804808f <+15>:    mov    $0xd,%edx
   0x08048094 <+20>:    int    $0x80
   0x08048096 <+22>:    mov    $0x4,%eax
   0x0804809b <+27>:    mov    $0x1,%ebx
   0x080480a0 <+32>:    mov    $0x80490c5,%ecx
   0x080480a5 <+37>:    mov    $0x4,%edx
   0x080480aa <+42>:    int    $0x80
   0x080480ac <+44>:    mov    $0x1,%eax
   0x080480b1 <+49>:    mov    $0x0,%ebx
   0x080480b6 <+54>:    int    $0x80
End of assembler dump.
(gdb)

反汇编不再受本地符号的阻碍。

关于linux - 如何不在 NASM 中发出局部符号,以便 GDB disas 不会停在它们上面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33624619/

相关文章:

Python BeautifulSoup & 问题 Mac 与 Linux Ubuntu

linux - Gnome-terminal - 如何自动将选择复制到剪贴板?

assembly - "AND AL,0xFF"的目的是什么?

c++ - 切换 C++ 函数的调用堆栈

linux - Bss 段 --> 无效的操作数组合

linux - 无法链接到 libxml2

linux - Ubuntu Linux : How to count the numbers of certain files in a directory

c++ - 链接到 visual studio 2013 boost

ios - 由于 Google Analytics 的 undefined symbol ,存档失败

linux - 使用 xxd 将整数转换为二进制