vmlinux ELF 查找给定结构成员的偏移量
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
结构的大小和布局存在于已编译目标文件的调试信息中(如果使用
-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'tstrip
).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.6.47 Offsetof
GCC 为 C 和 C++ 实现了一个语法扩展来实现 offsetof 宏。
这个扩展就足够了,它
是 offsetof 宏的合适定义。在 C++ 中,类型可能是相关的。在任何一种情况下,成员都可以由单个标识符或成员访问和数组引用的序列组成。
6.47 Offsetof
GCC implements for both C and C++ a syntactic extension to implement the offsetof macro.
This extension is sufficient such that
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.
使用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.