3

If you have an object file, how do you get the initialized value of a global variable in that object file's data segment? For example, say I've done the following:

# I'm interested in the variable foo inside bar.o in libbar.a:
$ ar -x libbar.a bar.o
$ nm --print-size bar.o | grep foo
00000048 00000004 D foo

This tells me that foo is at offset 0x48 in the data segment with size 4, but how do I obtain the actual initialized value it obtains upon load?

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589

1 Answers1

6

Figured it out:

objdump -j.data -s bar.o

This gives a hexdump of the data segment, making it easy to look up values. I've used objdump -d before to disassemble code, but the -s option is new to me.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589