Anik Samiur Rahman
Anik Samiur Rahman

Reputation: 131

GDB shows error message when trying to print a variable in an Assembly program

While learning assembly language from a book there is a listing showing some basic operations:

segment .data
a   dq  176
b   dq  4097

segment .text
global _start

_start:
    mov rax, [a]    ; Move a into rax.
    add rax, [b]    ; add b o rax.
    xor rax, rax
    ret

After assembling with "$yasm -f elf64 -g dwarf2 -l listing.lst listing.asm" command and linking with "$ld -o listing listing.o" I ran the program in gdb. There whenever I tried to print the value of a variable, gdb showed this error message:

(gdb) p a
'a' has unknown type; cast it to its declared type

Same for the other variable 'b'. However casting 'a' or 'b' for int worked:

(gdb) p (int)a
$11 = 176
(gdb) p (int)b
$12 = 4097

But isn't this supposed to work without casting? Why do I need to cast? What mistake I've made in my source file?

Upvotes: 13

Views: 16608

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 363980

Older GDB used to default to assuming that a symbol was an int when it didn't have debug info describing the size / type.

This generally caused more confusion than the current behaviour, so it was changed.
(e.g. The value displayed in Kdbg is wrong -- NASM)

Upvotes: 4

Employed Russian
Employed Russian

Reputation: 213385

But isn't this supposed to work without casting?

No. GDB tells you that it has no idea what type a and b are.

What mistake I've made in my source file?

You didn't make any mistakes, but you also didn't supply any debugging info that GDB could use.

You may have expected yasm -g dwarf2 ... to do so, but it only creates minimal debug info describing the source, nothing else:

$ readelf -wi listing.o

Contents of the .debug_info section:

  Compilation Unit @ offset 0x0:
   Length:        0x37 (32-bit)
   Version:       2
   Abbrev Offset: 0x0
   Pointer Size:  8
 <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <c>   DW_AT_stmt_list   : 0x0
    <10>   DW_AT_low_pc      : 0x0
    <18>   DW_AT_high_pc     : 0x14
    <20>   DW_AT_name        : listing.asm
    <28>   DW_AT_comp_dir    : /tmp/
    <2e>   DW_AT_producer    : yasm 1.3.0
    <39>   DW_AT_language    : 32769    (MIPS assembler)

Upvotes: 4

Related Questions