为什么 nm 不像目标文件那样显示调试符号?
我的目标文件中的所有符号地址都是 0,这也是正常的吗?
口粮:
main.c
:
#include "foo.h"
int main()
{
foo();
return 0;
}
foo.c
:
void foo()
{
}
foo.h
:
#ifndef FOO_H
# define FOO_H
void foo();
#endif
- 制作和符号
libfoo.a
:
$ gcc -c -g -o foo.o foo.c
$ nm -a foo.o
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_line_str
0000000000000000 N .debug_str
0000000000000000 T foo
0000000000000000 a foo.c
0000000000000000 t .text
$ ar rc libfoo.a foo.o
$ nm -a libfoo.a
foo.o:
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_line_str
0000000000000000 N .debug_str
0000000000000000 T foo
0000000000000000 a foo.c
0000000000000000 t .text
$
- 编译成可执行文件:
$ gcc -g main.c -L . -lfoo
$ nm -a a.out
0000000000000000 a
0000000000004010 B __bss_start
w __cxa_finalize@GLIBC_2.2.5
0000000000004000 D __data_start
0000000000004000 W data_start
0000000000004008 D __dso_handle
0000000000003e20 d _DYNAMIC
0000000000004010 D _edata
0000000000004018 B _end
0000000000001138 T _fini
000000000000112e T foo
0000000000000000 a foo.c
0000000000003fe8 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000002004 r __GNU_EH_FRAME_HDR
0000000000001000 T _init
0000000000002000 R _IO_stdin_used
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
U __libc_start_main@GLIBC_2.34
0000000000001119 T main
0000000000000000 a main.c
0000000000001020 T _start
0000000000004010 D __TMC_END__
$
file
输出:
$ file a.out
a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=48cebaa6f43e93bb1805faa5b71d41f1d1e71501, for GNU/Linux 4.4.0, with debug_info, not stripped
$
readelf -S
输出(附录):
$ readelf -S a.out
There are 34 section headers, starting at offset 0x37c0:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .interp PROGBITS 0000000000000318 00000318
000000000000001c 0000000000000000 A 0 0 1
[ 2] .note.gnu.pr[...] NOTE 0000000000000338 00000338
0000000000000040 0000000000000000 A 0 0 8
[ 3] .note.gnu.bu[...] NOTE 0000000000000378 00000378
0000000000000024 0000000000000000 A 0 0 4
[ 4] .note.ABI-tag NOTE 000000000000039c 0000039c
0000000000000020 0000000000000000 A 0 0 4
[ 5] .gnu.hash GNU_HASH 00000000000003c0 000003c0
000000000000001c 0000000000000000 A 6 0 8
[ 6] .dynsym DYNSYM 00000000000003e0 000003e0
0000000000000090 0000000000000018 A 7 1 8
[ 7] .dynstr STRTAB 0000000000000470 00000470
0000000000000088 0000000000000000 A 0 0 1
[ 8] .gnu.version VERSYM 00000000000004f8 000004f8
000000000000000c 0000000000000002 A 6 0 2
[ 9] .gnu.version_r VERNEED 0000000000000508 00000508
0000000000000030 0000000000000000 A 7 1 8
[10] .rela.dyn RELA 0000000000000538 00000538
00000000000000c0 0000000000000018 A 6 0 8
[11] .init PROGBITS 0000000000001000 00001000
000000000000001b 0000000000000000 AX 0 0 4
[12] .text PROGBITS 0000000000001020 00001020
0000000000000115 0000000000000000 AX 0 0 16
[13] .fini PROGBITS 0000000000001138 00001138
000000000000000d 0000000000000000 AX 0 0 4
[14] .rodata PROGBITS 0000000000002000 00002000
0000000000000004 0000000000000004 AM 0 0 4
[15] .eh_frame_hdr PROGBITS 0000000000002004 00002004
0000000000000024 0000000000000000 A 0 0 4
[16] .eh_frame PROGBITS 0000000000002028 00002028
0000000000000074 0000000000000000 A 0 0 8
[17] .init_array INIT_ARRAY 0000000000003e10 00002e10
0000000000000008 0000000000000008 WA 0 0 8
[18] .fini_array FINI_ARRAY 0000000000003e18 00002e18
0000000000000008 0000000000000008 WA 0 0 8
[19] .dynamic DYNAMIC 0000000000003e20 00002e20
00000000000001a0 0000000000000010 WA 7 0 8
[20] .got PROGBITS 0000000000003fc0 00002fc0
0000000000000028 0000000000000008 WA 0 0 8
[21] .got.plt PROGBITS 0000000000003fe8 00002fe8
0000000000000018 0000000000000008 WA 0 0 8
[22] .data PROGBITS 0000000000004000 00003000
0000000000000010 0000000000000000 WA 0 0 8
[23] .bss NOBITS 0000000000004010 00003010
0000000000000008 0000000000000000 WA 0 0 1
[24] .comment PROGBITS 0000000000000000 00003010
0000000000000036 0000000000000001 MS 0 0 1
[25] .debug_aranges PROGBITS 0000000000000000 00003046
0000000000000060 0000000000000000 0 0 1
[26] .debug_info PROGBITS 0000000000000000 000030a6
00000000000000ab 0000000000000000 0 0 1
[27] .debug_abbrev PROGBITS 0000000000000000 00003151
000000000000007b 0000000000000000 0 0 1
[28] .debug_line PROGBITS 0000000000000000 000031cc
00000000000000a1 0000000000000000 0 0 1
[29] .debug_str PROGBITS 0000000000000000 0000326d
000000000000003d 0000000000000001 MS 0 0 1
[30] .debug_line_str PROGBITS 0000000000000000 000032aa
000000000000003e 0000000000000001 MS 0 0 1
[31] .symtab SYMTAB 0000000000000000 000032e8
0000000000000258 0000000000000018 32 7 8
[32] .strtab STRTAB 0000000000000000 00003540
000000000000011f 0000000000000000 0 0 1
[33] .shstrtab STRTAB 0000000000000000 0000365f
000000000000015c 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
D (mbind), l (large), p (processor specific)