1

For example, consider the following Linux freestanding true program with an useless my_longs variable for illustrative purposes:

main.S

.section .rodata
    my_longs: .quad 0x12, 0x34
.text
.global _start
_start:
asm_main_after_prologue:
    mov $60, %rax
    mov $0, %rdi
    syscall

which you can assemble and run with:

as -o main.o main.S
ld -o main.out main.o
gdb main.out

Then start the program with:

starti

Now, to see the value of my_longs, I have to type the annoying (long[2]) part:

p/x (long[2])my_longs

Is there any way to tell GDB the type of my_longs on by adding some extra annotation to my assembly program?

When writing a C program:

main_c.c

long my_longs[] = {0x12, 0x34};

int main(void) {
    return 0;
}

compiled with:

gcc -O0 -ggdb3 -save-temps -o main_c.out main_c.c

I can see the symbols directly with;

p/x my_longs

presumably due to the DWARF debug information GCC added.

When I had a look at the generated assembly, the only references I could find to my_longs were:

.Ltext0:
    .comm   my_longs,16,16
.Ldebug_info0:
    .long   .LASF351
    .quad   my_longs
.LASF351:
    .string "my_longs"

so I'm not sure where the type information is being stored.

I know that this question may come down basically to: how are types encoded in DWARF, and is it practical to manually encode that information. I intend to study DWARF a bit later on if this keeps on bothering me.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 1
    You can non-manually add debug with '--gdwarf-2'. There are also various pseudo-ops which can encode information. See: [CFI](https://sourceware.org/binutils/docs/as/CFI-directives.html). – artless noise Aug 02 '22 at 14:01

1 Answers1

3

I am no DWARF expert either, but poking around with dwarfdump reveals enough to answer the question.

$ dwarfdump ./main_c.out

..  DW_TAG_variable
        DW_AT_name                  my_longs
        ...
        DW_AT_type                  <0x00000031>

And if we look at the type node, we'll see that it is an array (whose elements are of type described with node 0x0048 which corresponds to long int).

$ dwarfdump ./main_c.out  | egrep -A3 '00031'

< 1><0x00000031>    DW_TAG_array_type
                      DW_AT_type                  <0x00000048>
                      DW_AT_sibling               <0x00000041>
...
Vladislav Ivanishin
  • 2,092
  • 16
  • 22