The value displayed in Kdbg is wrong — NASM

2019-08-25 00:46发布

问题:

How can I test to see if the value of k is correct?

section .data
    k dw 5
    m dw 110
    rez dw 0 
section .bss
    tabela resq 3 
section .text
global _start
extern uslov
_start:
    mov qword [tabela], k
    mov qword [tabela + 8], m
    mov qword [tabela + 16], rez

    mov rbx, tabela
    call uslov
mov rax, 60
mov rdi, 0
syscall

When I try to inspect the values of k,m,rez in kdbg the values of m and rez are just fine but the value of k is totally different, now at first i thought it was random, but it seems as tough it reads the value of rez as an 8 byte number instead of a 2 byte number and also reads in 6 more bytes taking in all the set 1's from m and rez which is wrong, so how can I display it correctly ?

Screenshot:

回答1:

I can reproduce this with your source (removing undefined references to uslov) when I compile using this command line:

nasm -f elf64 test.asm -o test.o
ld test.o -o test

Then, in GDB I can indeed see that k appears to have sizeof(k)==4:

gdb ./test -ex 'tb _start' -ex r -ex 'p sizeof(k)'
Reading symbols from ./test...done.
Starting program: /tmp/test

Temporary breakpoint 1, 0x00000000004000b0 in _start ()
$1 = 4

This is because the only information the final binary has about k is that it's a symbol in data area. See:

(gdb) ptype k
type = <data variable, no debug info>

The debugger (KDbg uses GDB under the hood) can't know its size, so it just guesses the default size to be sizeof(int). Even if you enable debug info in NASM via -F dwarf -g options, it still doesn't appear to put any actual debug info.

So, your only way to get the variables displayed with the right size is to manually specify it, like (short)k instead of k.