Variable name defined in .bss section cannot be found in gdb

I try simple assembly code:

.section .data
output:
    .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
.section .bss
    .lcomm buffer, 12
.section .text
.code32
.globl _start
_start:
    movl $0, %eax
    cpuid
    movl $output, %edi

In the .bss section, I defined a variable called "buffer"

When I try to get its address / value in gdb, it just prints:

(gdb) p $ buffer
$ 1 = void

Using objdump, I found that the name is not in the ELF file, so how to save this information when running as and ld? Thank!

+4
source share
3 answers

Using objdump, I found that the name is not in the ELF file

Works for me on Arch Linux with GNU binutils 2.28.0-3. Maybe you shared your binary after the link?

$ gcc -Wall -m32 -nostdlib gas-symbols.S
$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, BuildID[sha1]=d5fdff41cc52e9de3b4cdae34cf4129de2b4a69f, not stripped

$ nm a.out 
080490ee B __bss_start
080490f0 b buffer           ### local symbol in the bss
080490ee D _edata
080490fc B _end
080490c4 d output
080480b8 T _start

-g, . , , -static -nostdlib. , . Q & A asm 32 64- gcc as ld. NASM ld.

( , .code32 . , , , .code32, , , (, push %ebx), 32- 64- .)

as ld ( gcc , gcc -v, , ), .

$ as gas-symbols.S -o gas-symbols.o  --32 && 
  ld -o a.out gas-symbols.o  -m elf_i386
$ nm a.out 
...
080490b0 b buffer        ## Still there
...

GDB, Jester, , . GDB , . ( . , , gcc -S static char foo[100]; ( .)

, GDB , :

$ gdb ./a.out
(gdb) b _start
(gdb) r
Starting program: /home/peter/src/SO/a.out

Breakpoint 1, _start () at gas-symbols.S:10
(gdb) p buffer
$1 = 0
(gdb) p &buffer
$2 = (<data variable, no debug info> *) 0x80490f0 <buffer>
(gdb) ptype buffer
type = <data variable, no debug info>

, x:

(gdb) p (char[12])buffer
$4 = '\000' <repeats 11 times>
(gdb) p /x (char[12])buffer
$5 = {0x0 <repeats 12 times>}
(gdb) x /4w &buffer             # eXamine the memory as 4 "words" (32-bit).  
0x80490f0 <buffer>:     0x00000000      0x00000000      0x00000000      0x00000000
(gdb) help x   # read this to learn about options for dumping memory

asm ~/.gdbinit:

set disassembly-flavor intel
layout reg
set print static-members off

AT & T, , , Intel. layout asm/layout reg . . wiki. - .

+4

p &buffer p $buffer. $ - , gdb, ( ). - x/12c &buffer p (char[12])buffer

PS: , , .

+3

.lcomm . , , ld.

If you need a character that maps to ld, you must make it .global(or .globl, depending on your assembler).

The idea of โ€‹โ€‹common characters is to allow you to have the same character defined in several compilation units. They go after the link.

+2
source

Source: https://habr.com/ru/post/1680480/


All Articles