如何从目标文件中获取全局变量的初始化值?

发布于 2024-08-23 06:43:15 字数 303 浏览 8 评论 0原文

如果您有一个目标文件,如何获取该目标文件数据段中全局变量的初始化值?例如,假设我执行了以下操作:

# 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

这告诉我 foo 位于大小为 4 的数据段中的偏移量 0x48 处,但是如何获取它在加载时获得的实际初始化值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2024-08-30 06:43:15

弄清楚了:

objdump -j.data -s bar.o

这给出了数据段的十六进制转储,使得查找值变得容易。我之前使用过 objdump -d 来反汇编代码,但是 -s 选项对我来说是新的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文