编译时没有使用"-g"选项,为什么file命令报告可执行文件为“未剥离, 带有debug_info”?

5

我的操作系统是Arch Linux,而test.c程序非常简单:

# cat test.c
#include <stdio.h>

int main(void) {
        printf("Hello world!\n");
}

编译时不要使用-g选项,并使用file命令检查可执行文件信息:

# gcc test.c
# file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=c7a538046222b5209d2daafbfc246de341a652d9, not stripped, with debug_info

file 命令输出 "not stripped, with debug_info",但我在编译过程中并没有使用 -g 选项。请使用 gdb 调试 a.out 文件:

# gdb a.out
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...(no debugging symbols found)...done.
(gdb)

我可以看到 gdb 也会提示 "Reading symbols from a.out...(no debugging symbols found)...done."。
为什么在编译时没有使用 "-g" 选项,file 命令会报告可执行文件的 "not stripped, with debug_info"?

1
仍然有最少量的信息可用于调试(全局函数和变量名称)。您可以使用strip命令将它们删除。 - n. m.
1个回答

7
为什么在编译时未使用“-g”选项,文件命令报告可执行文件为“未剥离,带有debug_info”?
您的程序链接了libc_nonshared.a的部分内容(例如c运行时启动ctr0.o),其中可能包含一些调试信息。 您可以通过以下方式查看可用的调试信息并猜测其来自何处:
readelf -wl ./a.out

这表明 objdump -x ./a.out | grep debug_infofile 更好地查找可执行文件是否具有调试信息。 - David Faure

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接