c - 来自对象符号表的结构成员详细信息

标签 c linux

试图在 linux 中使用 objdump 命令来显示可执行文件中的符号表信息。 我在下面尝试了一个简单的程序。

#include<stdio.h>
int global = 0;
typedef struct global_struct{
    int a;
    int c;
}global_struct;

global_struct gs;
int main()
{
    printf("%d\n",global);
    printf("%d\n",gs.a);
    return 0;
}

在 gcc 编译器中使用 -g 选项编译 objdump 的输出看起来像

00000000004005b0 l     F .text  0000000000000000              __do_global_ctors_aux
0000000000000000 l    df *ABS*  0000000000000000              symboltable.c
0000000000600870 l     O .got.plt       0000000000000000              _GLOBAL_OFFSET_TABLE_
00000000006006ac l       .ctors 0000000000000000              __init_array_end
00000000006006ac l       .ctors 0000000000000000              __init_array_start
00000000006006d8 l     O .dynamic       0000000000000000              _DYNAMIC
0000000000600898  w      .data  0000000000000000              data_start
00000000006008b4 g     O .bss   0000000000000008              gs
0000000000000000       F *UND*  0000000000000000              printf@@GLIBC_2.2.5
0000000000400510 g     F .text  0000000000000002              __libc_csu_fini
00000000004003e0 g     F .text  0000000000000000              _start
0000000000000000  w      *UND*  0000000000000000              __gmon_start__
0000000000000000  w      *UND*  0000000000000000              _Jv_RegisterClasses
00000000004005e8 g     F .fini  0000000000000000              _fini
0000000000000000       F *UND*  0000000000000000              __libc_start_main@@GLIBC_2.2
00000000006008b0 g     O .bss   0000000000000004              global
00000000004005f8 g     O .rodata        0000000000000004              _IO_stdin_used
0000000000600898 g       .data  0000000000000000              __data_start
0000000000400600 g     O .rodata        0000000000000000              .hidden __dso_handle
00000000006006c8 g     O .dtors 0000000000000000              .hidden __DTOR_END__
0000000000400520 g     F .text  0000000000000089              __libc_csu_init
000000000060089c g       *ABS*  0000000000000000              __bss_start
00000000006008c0 g       *ABS*  0000000000000000              _end

我的要求是gs是C结构,我想知道gs{a,b}的数据成员。我如何从目标文件中知道结构成员的详细信息。感谢您的支持

最佳答案

目标文件中没有详细说明结构成员的详细信息。目标文件将只分配足够的内存来保存结构和一个偏移量,告诉链接器在哪里找到它。

编译器在编译时知道基结构指针的偏移量以找到成员,并在每次使用时将它们硬编码到程序文本中。

关于c - 来自对象符号表的结构成员详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666692/

相关文章:

没有声明和中断的情况

linux - 使用 daystart 和 mmin 查找命令

linux - 确定 Linux 上进程停滞的原因

linux - TCL Expect 正在向标准输出注入(inject)额外的空字符

linux - 如何编写 bash 脚本将多个文件合并为一个文件

c - 以下代码的输出

c - 尝试将 node.js unix 套接字客户端连接到 C 程序时出现 EPROTOTYPE 错误

c - 如何使用 gcc 4.8.4 [-Wunused-parameter] 摆脱 C 中的未使用参数警告

linux - 安装 python 2.7.3 后 yum 坏了

c - 限制结构内的关键字和指针