检查可执行文件中的变量

发布于 2024-08-29 07:56:35 字数 224 浏览 2 评论 0原文

有没有办法通过查看可执行文件来知道变量是否已定义。

假设我

int i;

在主函数中声明。通过编译和链接,我得到了一个可执行文件 my_program.exe。

现在,通过查看 my_program.exe 内部,我可以判断它是否有 int eger 变量 i 吗?

Is there a way to know whether a variable is defined, by looking at the executable.

Lets say I declare

int i;

in the main function. By compiling and linking I get an executable my_program.exe.

Now, by looking inside my_program.exe, can I say if it has an int eger variable i ?

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

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

发布评论

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

评论(4

送你一个梦 2024-09-05 07:56:35

除非您在启用调试的情况下进行编译,否则不会。

Not unless you compile with debugging enabled.

居里长安 2024-09-05 07:56:35

正如其他人所说,调试信息会显示它。更具体地说,对于 ELF 文件:

readelf -w binary-name

将具有如下条目:

<2><58>: Abbrev Number: 4 (DW_TAG_variable)
 <59>     DW_AT_name        : i 
 <5b>     DW_AT_decl_file   : 1 
 <5c>     DW_AT_decl_line   : 2 
 <5d>     DW_AT_type        : <73>  
 <61>     DW_AT_location    : 2 byte block: 91 6c   (DW_OP_fbreg: -20)

没有调试信息,本地人不会保留其名称。如果该变量是全局变量,则会有一个符号指向它:

objdump -t binary-name

0804a010 g     O .data  00000004              i

类型信息丢失,但您可以看到大小为 4

As others said, debugging information will show it. More specifically, for ELF files:

readelf -w binary-name

will have an entry like:

<2><58>: Abbrev Number: 4 (DW_TAG_variable)
 <59>     DW_AT_name        : i 
 <5b>     DW_AT_decl_file   : 1 
 <5c>     DW_AT_decl_line   : 2 
 <5d>     DW_AT_type        : <73>  
 <61>     DW_AT_location    : 2 byte block: 91 6c   (DW_OP_fbreg: -20)

Without debugging information, locals don't retain their names. If the variable is a global, there will be a symbol that points to it:

objdump -t binary-name

0804a010 g     O .data  00000004              i

Type information is lost there, but you can see the size is 4

傾城如夢未必闌珊 2024-09-05 07:56:35

如果使用调试符号(例如 gcc -g)进行编译,则可以使用调试器查看几乎所有内容。

If you compile with debugging symbols (for example, gcc -g) you can then use your debugger to see pretty much everything.

豆芽 2024-09-05 07:56:35

局部变量在优化过程中可能会被编译器消除,因此即使使用调试符号也很难找到变量的初始值。但这是特定于平台的。

Local variables could be eliminated by the compiler during optimization process, so the initial value of variables will be hard to find out even with debugging symbols. That is platform specific though.

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