How to not emit local symbols in NASM so that GDB disas won't stop at them?

Rob*_*son 3 linux assembly linker nasm elf

I'm trying to write some assembly programs using nasm on linux. Everything is good, but I make heavy use of local symbols (.loop, .else, etc.), which is a pain when debugging, because these symbols are emitted to the symbol table, e.g.:

[BITS 32]
global main
section .text
main:
    do stuff
.else:
    do other stuff
Run Code Online (Sandbox Code Playgroud)

will produce a disassembly that looks like:

<main>:
00000000      do stuff
<main.else>:
00000000      do other stuff
Run Code Online (Sandbox Code Playgroud)

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

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

sok*_*kin 5

我还没有找到直接使用 的方法nasm,但是如果您将对象与 链接ld,那么您可以使用一个非常方便的开关。引用 ld 的手册页

-x --discard-all 删除所有本地符号。

-X --discard-locals 删除所有临时局部符号。(这些符号以系统特定的本地标签前缀开头,通常是 .L 代表 ELF 系统或 L 代表传统的 a.out 系统。)

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

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
Run Code Online (Sandbox Code Playgroud)

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

$ nasm -g -f elf32 prog.asm && ld -x prog.o -o prog && ./prog
Hello world!
Hi!
Run Code Online (Sandbox Code Playgroud)

然后,当你加载它时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)
Run Code Online (Sandbox Code Playgroud)

本地符号不再妨碍拆卸。