0

I have defined an array in asm as:

arr: .word 1,3,9,27

And now that I'm in gdb, the current way I'm using to find and print that array is looking for the instruction that looks like it might include the array address, such as:

0x00000000004000dc  ? movzwq 0x400078(,%rdi,2),%r12

And then copy-pasting in that address (knowing the element size) and doing:

>>> x/4hd  0x400078
0x400078 <arr>: 1   3   9   27

This is an awfully tedious way to find and print labels and often involves me pressing si ten or twenty times down to find it. Is there a better way to find and print labels and their content?

Additionally, is there a way to see all labels defined in a program/function, such as:

0x400078 arr
0x450090 my_other_var

Update: my code is:

# file.s
myarray: .word 1,3,9,27
myvar:  .long 927

.globl _start
_start:
    mov $7, %ebx
    mov $1, %eax
    int $0x80

Compiled as:

$ as file.s -o file.o && ld file.o -o file
$ gdb file --ex 'b _start' --ex 'r' --ex 'set confirm off'

With gdb version:

>>> show version
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software...
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • 1
    Does `x/4hd &arr` not work? Unless you strip the executable, symbols should still be there if you didn't use `.Larr:` to make it a "local" symbol. – Peter Cordes Aug 25 '20 at 01:25
  • @PeterCordes I see, yea that works, doing both `&arr` and just `arr` by itself gives the same result which is great. Is there a way to print all `labels` ? Doing something like `>>> info variables` returns nothing for me. – samuelbrody1249 Aug 25 '20 at 01:32
  • `info variables` works for me, showing all "Non-debugging symbols", just like `nm a.out` would. In a static executable there are only your symbols plus `__bss_start` and a couple other linker-added ones so it's pretty easy to find stuff. Note that labels are an asm source concept; a label defines a *symbol*, but there are other ways of defining symbols like `.set`. So inside GDB you'll only see stuff about "symbols" not "labels". – Peter Cordes Aug 25 '20 at 01:52
  • @PeterCordes thanks -- doing `$ nm file.o` works for me but curious why `gdb` doesn't show it. – samuelbrody1249 Aug 25 '20 at 02:15
  • It does show it for me. Make a [mcve] including asm source and build commands, and GDB version. Also, `x/4hd arr` *doesn't* work because it tries to use the value, not address. I get `'arr' has unknown type; cast it to its declared type`. If you don't, maybe you have an older GDB version that works differently, and maybe its `info var` doesn't show non-debug symbols? – Peter Cordes Aug 25 '20 at 02:19
  • @PeterCordes updated with code and version. – samuelbrody1249 Aug 25 '20 at 02:28
  • 2
    Probably GDB assumes that symbols in the `.text` section aren't variables. (I can reproduce your results with that source). I put mine in `.rodata` for the earlier test, since I thought that was more likely to get GDB to treat it as a "variable", not a function. – Peter Cordes Aug 25 '20 at 02:49
  • @PeterCordes oh, got it. Yes, that fixed it. I put it in a `.data` section and now it prints out when I do that. Is it suggested to put variables in the `.rodata` section or `.data` section or it really doesn't matter? – samuelbrody1249 Aug 25 '20 at 02:57
  • 1
    Yes, of course you should put read-only constants in `.rodata`, that's why it has that name. And read/write data in `.data`, or in `.bss` if zero-initialized is fine. [Why do Compilers put data inside .text(code) section of the PE and ELF files and how does the CPU distinguish between data and code?](https://stackoverflow.com/q/55607052) - they don't, that would be a disaster for read/write vars, and a waste of cache footprint for read-only. – Peter Cordes Aug 25 '20 at 02:59

0 Answers0