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.

1
  • 1
    You can non-manually add debug with '--gdwarf-2'. There are also various pseudo-ops which can encode information. See: CFI. Commented Aug 2, 2022 at 14:01

1 Answer 1

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>
...
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I did not know dwarfdump. OK, from this it does not look that there is an easy way to manually encode the DWARF info :-(
Yesterday I posted a question (How do I make this, presumed, very basic DWARF example work?), basically asking how to handle dwarf.exp, trying to build a very simple debugging information file for a compiled source (main.c [template also provided by the gdb testsuite]) to be combined with and also implicitly asking how to approach it with gdb, after all (how to combine the binary + generated dwarf file).
Yeah, I learned about it by lurking around on SO. Manual encoding seems a bit too involved, I agree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.