0

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: enter image description here

Michael Petch
  • 46,082
  • 8
  • 107
  • 198

1 Answers1

3

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.

Ruslan
  • 18,162
  • 8
  • 67
  • 136
  • It's not a bug per-se, you (and the OP) simply didn't include *any* debug info for that symbol. NASM doesn't magically associate sizes with labels based on `dd` / `dw` / `db` or whatever other instructions / pseudo-instructions you put after them. (MASM even does this for implying operand-size in `mov symbol, 123`, again unlike NASM). – Peter Cordes Nov 26 '17 at 15:00
  • If you `p &k`, you'll see something like `$1 = ( *) 0x600104`. GDB's default interpretation is `int`. – Peter Cordes Nov 26 '17 at 15:01
  • @PeterCordes ah, I see. So you mean it's just a symbol in the symbol table, but not in the DWARF data? Will have to recheck. – Ruslan Nov 26 '17 at 15:08
  • Right, NASM does put everything in the symbol table with `-g`, even `.local` labels. – Peter Cordes Nov 26 '17 at 15:11
  • @PeterCordes thanks, I've edited the answer to reflect this. – Ruslan Nov 26 '17 at 15:42