给定一个由 C++ 构建的 ELF 二进制文件;使用gcc,我如何确定其中某种类型的大小?

发布于 2024-08-29 10:33:09 字数 292 浏览 4 评论 0原文

有什么方法可以使用 binutils 工具来实现这一点吗?例如:

// x.cc
typedef long long MyInt;
int main(int argc, char* argv[]) {
  // blah blah blah
}

那么:

g++ -g x.cc -o a.out

如何分析a.out来得到sizeof(MyInt)? GDB 可以做到这一点,但我不想使用 GDB,因为它对于大型二进制文件来说真的很慢。

Is there some way using binutils tools to get this? For example:

// x.cc
typedef long long MyInt;
int main(int argc, char* argv[]) {
  // blah blah blah
}

Then:

g++ -g x.cc -o a.out

How can I analyze a.out to get sizeof(MyInt)? GDB can do it, but I don't want to use GDB because it's really slow for large binaries.

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

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

发布评论

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

评论(3

左耳近心 2024-09-05 10:33:10

您将需要执行 GDB 正在执行的操作并亲自阅读 DWARF 调试信息。有一些工具可以帮助解决这个问题,例如 readelf 和 dwarfdump。请阅读 http://dwarfstd.org/ 上的常见问题解答和其他好东西,看看是否能满足您的需求。

这个答案假设您正在普通 Linux 平台上运行。还有其他适用于其他平台的工具可能也适合您,但我不是询问这些的人。

You will need to do what GDB is doing and read the DWARF debug info for yourself. There are a couple of tools that can help with this like readelf and dwarfdump. Go read the FAQ and other goodies at http://dwarfstd.org/ and see if that will do what you need.

This answer is assuming that you are running on a vanilla Linux platform. There are other tools for other platforms that may also work for you, but I am not the guy to ask about those.

紫﹏色ふ单纯 2024-09-05 10:33:09

readelf 能够列出 dwarf 调试信息:

readelf -wi

查找 DW_TAG_variable 元素,该元素的 DW_AT_name 等于 MyInt,并且是表示此编译单元的 DW_TAG_compile_unit 的直接子元素。

使用 DW_AT_type 属性查找该变量的 DW_TAG_base_type。
DW_AT_type 属性是距存储此类型信息的 CU 开头的偏移量。这些偏移量列在输出的左侧(在<>s中)。
查看这个DIE的DW_AT_byte_size属性。这是类型的大小(以字节为单位)。

readelf is capable of listing dwarf debug information:

readelf -wi

Find the DW_TAG_variable element that har DW_AT_name equal to MyInt and is immediate child of the DW_TAG_compile_unit representing this compile unit.

Use the DW_AT_type attribute to look up the DW_TAG_base_type of this variable.
THe DW_AT_type attribute is the offset from the start of this CU where information about this type is stored. These offsets are listed on the left-hand side in the output (in <>s).
Look at the DW_AT_byte_size attribut of this DIE. This is the size of the type in bytes.

樱桃奶球 2024-09-05 10:33:09

我想说你最好还是坚持使用 gdb 。

bintuls 主要处理可执行格式 (ELF) - 调试内容以 dwarf 格式保存。除了运行 objdump -g -x yourbinary 之外,我还没有看到很多处理调试符号的实用程序。

总而言之,解析 elf 和 dwarf 来提取类型是非常可怕且不简单的——尽管调试器已经完成了这些工作。

I'd say you're better off sticking with gdb for this.

bintuls deals mostly with the executable format (ELF) - the debugging stuff stuff is kept in the dwarf format. Other than running objdump -g -x yourbinary I've not seen many utilities dealing with the debugging symbols.

Altogether, parsing through elf and dwarf to pull out the types is pretty scary and non-trivial - though exactly what a debugger already has done.

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