vmlinux ELF 查找给定结构成员的偏移量

发布于 2024-08-04 03:43:30 字数 213 浏览 2 评论 0原文

在Linux内核中,我需要找到所使用的结构体的成员变量的偏移量。例如,对于类型为task_struct的init_task,我想要它的pid和任务的偏移量。

我只有 vmlinux 可用。我可以参考开源内核代码,但它可能与我的构建有所不同。

是否可以在没有源的情况下获得偏移量?

编辑:vmlinux 适用于 ARM,我可能并不总是能够在目标设备上运行 C 代码。

In the Linux kernel, I need to find the offsets of member variables of a struct that is used. For example, for the init_task which is of type task_struct, I would like the offsets of its pid and tasks.

I only have the vmlinux present for this. I can refer to the open source kernel code, but it may differ from the build I have.

Is it possible to get the offsets without the source ?

EDIT: The vmlinux is for ARM, and I may not always be able to run C code on the target device.

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

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

发布评论

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

评论(3

书间行客 2024-08-11 03:43:30

结构的大小和布局存在于已编译目标文件的调试信息中(如果使用 -g 进行编译且不strip)。

pahole(又名“poke”) -a-hole”,打包为 dev-util/dwarves Gentoo 中的 )读取对象的 DWARF 调试信息以输出有关结构中“漏洞”的信息——这对您来说可能是一个很好的起点。

The size and layout of structures is present in the debugging information of the compiled object files (if you compile with -g and don't strip).

pahole (aka "poke-a-hole", packaged as dev-util/dwarves in Gentoo) reads an object's DWARF debugging information to output information about "holes" in structures -- that may be a good starting point for you.

为你鎻心 2024-08-11 03:43:30

6.47 Offsetof

GCC 为 C 和 C++ 实现了一个语法扩展来实现 offsetof 宏。

 primary:
         "__builtin_offsetof" "(" typename "," offsetof_member_designator ")"

 offsetof_member_designator:
           identifier
         | offsetof_member_designator "." identifier
         | offsetof_member_designator "[" expr "]"

这个扩展就足够了,它

 #define offsetof(type, member)  __builtin_offsetof (type, member)

是 offsetof 宏的合适定义。在 C++ 中,类型可能是相关的。在任何一种情况下,成员都可以由单个标识符或成员访问和数组引用的序列组成。

6.47 Offsetof

GCC implements for both C and C++ a syntactic extension to implement the offsetof macro.

 primary:
         "__builtin_offsetof" "(" typename "," offsetof_member_designator ")"

 offsetof_member_designator:
           identifier
         | offsetof_member_designator "." identifier
         | offsetof_member_designator "[" expr "]"

This extension is sufficient such that

 #define offsetof(type, member)  __builtin_offsetof (type, member)

is a suitable definition of the offsetof macro. In C++, type may be dependent. In either case, member may consist of a single identifier, or a sequence of member accesses and array references.

孤独患者 2024-08-11 03:43:30

使用arm-eabi-gdb找到了另一个解决方案 - 我可以执行 print &init_task 和 print &init_task.pid ,区别在于偏移量。

Found another solution with arm-eabi-gdb - I can do print &init_task and print &init_task.pid and the difference is the offset.

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