【发布时间】:2015-03-09 02:31:48
【问题描述】:
假设我有一个简单的 C 程序,我用 gcc -o hello hello.c 编译它:
#include<stdio.h>
main()
{
printf("hello");
}
现在我想用strings 实用程序显示“字符串”:
$ strings hello
/lib64/ld-linux-x86-64.so.2
__gmon_start__
libc.so.6
printf
__libc_start_main
GLIBC_2.2.5
fffff.
l$ L
t$(L
|$0H
hello
;*3$"
正如预期的那样,我可以在二进制文件中看到字符串“hello”。
但是,当我修改 C 程序并将“hello”作为常量时:
#include<stdio.h>
char s[6] = {'h','e','l','l','o','\0' } ;
main()
{
printf("%s\n", s);
}
我在二进制文件中看不到字符串“hello”了。
有人能解释一下原因吗?
【问题讨论】:
-
检查你的可执行文件的sections。
man strings建议“它只打印来自目标文件的初始化和加载部分的字符串”。 -
需要更多详细信息。它在 64 位 Linux 上使用 gcc 4.9.2 为我工作 (tm)。您还可以通过查看汇编程序输出 (
gcc -S) 而不是编译后的二进制文件来更好地了解原因。 -
试试
strings -a hello -
OT:至少是
int main(void)。 -
"但是,当我修改我的 C 程序并将 "hello" 作为常量时:" - 仅供参考,
char s[6] = ....不是常量。出于兴趣,将sinside 移至main()并重复您的测试。如果您按要求添加装配输出,这将是您的问题的弹药,这将表示段名称。