gcc为全局变量生成不必要的调试符号?

在我们的项目中,我们从gcc 5.3移到gcc 7.3,并发现调试符号增加了很多; 经过一番挖掘,我发现gcc 7.3生成不必要的(我想,请纠正我,如果我错了)调试全局变量的符号,因为我们声明了很多全局变量并且很多c文件都包含它们,调试符号大小为最终的ELF筹集到巨大的价值。

是否有一个gcc开关来解决这个问题而不改变全局变量的使用?

编辑1

"-O3" or "-Os", "-feliminate-unused-debug-types" does not help. 

下面是一个显示问题的示例(在ubuntu 18.04上,使用gcc 5.5和gcc 7.3):示例代码:

 ### test.c #include "global.h" int main() { return 0; } ###global.c #include "global.h" int __my_unused = 1; ###global.h extern int __my_unused; 

GCC 5.5,test.c中没有不必要的调试符号“__my_unused”

 root@b-test:/tmp# gcc-5 -g -O2 test.c global.c root@b-test:/tmp# readelf -wi a.out Contents of the .debug_info section: Compilation Unit @ offset 0x0: Length: 0x4a (32-bit) Version: 4 Abbrev Offset: 0x0 Pointer Size: 8 : Abbrev Number: 1 (DW_TAG_compile_unit)  DW_AT_producer : (indirect string, offset: 0x0): GNU C11 5.5.0 20171010 -mtune=generic -march=x86-64 -g -O2 -fstack-protector-strong  DW_AT_language : 12 (ANSI C99)  DW_AT_name : (indirect string, offset: 0x59): test.c  DW_AT_comp_dir : (indirect string, offset: 0x54): /tmp  DW_AT_ranges : 0x0  DW_AT_low_pc : 0x0  DW_AT_stmt_list : 0x0 : Abbrev Number: 2 (DW_TAG_subprogram)  DW_AT_external : 1  DW_AT_name : (indirect string, offset: 0x60): main  DW_AT_decl_file : 1  DW_AT_decl_line : 3  DW_AT_type :   DW_AT_low_pc : 0x530  DW_AT_high_pc : 0x3  DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa)  DW_AT_GNU_all_call_sites: 1 : Abbrev Number: 3 (DW_TAG_base_type)  DW_AT_byte_size : 4  DW_AT_encoding : 5 (signed)  DW_AT_name : int : Abbrev Number: 0 Compilation Unit @ offset 0x4e: Length: 0x36 (32-bit) Version: 4 Abbrev Offset: 0x37 Pointer Size: 8 : Abbrev Number: 1 (DW_TAG_compile_unit)  DW_AT_producer : (indirect string, offset: 0x0): GNU C11 5.5.0 20171010 -mtune=generic -march=x86-64 -g -O2 -fstack-protector-strong  DW_AT_language : 12 (ANSI C99)  DW_AT_name : (indirect string, offset: 0x65): global.c  DW_AT_comp_dir : (indirect string, offset: 0x54): /tmp  DW_AT_stmt_list : 0x39 : Abbrev Number: 2 (DW_TAG_variable)  DW_AT_name : (indirect string, offset: 0x6e): __my_unused  DW_AT_decl_file : 1  DW_AT_decl_line : 2  DW_AT_type :   DW_AT_external : 1  DW_AT_location : 9 byte block: 3 10 10 20 0 0 0 0 0 (DW_OP_addr: 201010) : Abbrev Number: 3 (DW_TAG_base_type)  DW_AT_byte_size : 4  DW_AT_encoding : 5 (signed)  DW_AT_name : int : Abbrev Number: 0 

GCC 7.3,test.c中不必要的调试符号“__my_unused”

 root@b-test:/tmp# gcc-7 -g -O2 test.c global.c root@b-test:/tmp# readelf -wi a.out Contents of the .debug_info section: Compilation Unit @ offset 0x0: Length: 0x55 (32-bit) Version: 4 Abbrev Offset: 0x0 Pointer Size: 8 : Abbrev Number: 1 (DW_TAG_compile_unit)  DW_AT_producer : (indirect string, offset: 0xc): GNU C11 7.3.0 -mtune=generic -march=x86-64 -g -O2 -fstack-protector-strong  DW_AT_language : 12 (ANSI C99)  DW_AT_name : (indirect string, offset: 0x5c): test.c  DW_AT_comp_dir : (indirect string, offset: 0x57): /tmp  DW_AT_ranges : 0x0  DW_AT_low_pc : 0x0  DW_AT_stmt_list : 0x0 : Abbrev Number: 2 (DW_TAG_variable)  DW_AT_name : (indirect string, offset: 0x0): __my_unused  DW_AT_decl_file : 2  DW_AT_decl_line : 1  DW_AT_type :   DW_AT_external : 1  DW_AT_declaration : 1 : Abbrev Number: 3 (DW_TAG_base_type)  DW_AT_byte_size : 4  DW_AT_encoding : 5 (signed)  DW_AT_name : int : Abbrev Number: 4 (DW_TAG_subprogram)  DW_AT_external : 1  DW_AT_name : (indirect string, offset: 0x63): main  DW_AT_decl_file : 1  DW_AT_decl_line : 3  DW_AT_type :   DW_AT_low_pc : 0x4f0  DW_AT_high_pc : 0x3  DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa)  DW_AT_GNU_all_call_sites: 1 : Abbrev Number: 0 Compilation Unit @ offset 0x59: Length: 0x3d (32-bit) Version: 4 Abbrev Offset: 0x48 Pointer Size: 8 : Abbrev Number: 1 (DW_TAG_compile_unit)  DW_AT_producer : (indirect string, offset: 0xc): GNU C11 7.3.0 -mtune=generic -march=x86-64 -g -O2 -fstack-protector-strong  DW_AT_language : 12 (ANSI C99)  DW_AT_name : (indirect string, offset: 0x68): global.c  DW_AT_comp_dir : (indirect string, offset: 0x57): /tmp  DW_AT_stmt_list : 0x45 : Abbrev Number: 2 (DW_TAG_variable)  DW_AT_name : (indirect string, offset: 0x0): __my_unused  DW_AT_decl_file : 1  DW_AT_decl_line : 1  DW_AT_type :   DW_AT_external : 1  DW_AT_declaration : 1 : Abbrev Number: 3 (DW_TAG_base_type)  DW_AT_byte_size : 4  DW_AT_encoding : 5 (signed)  DW_AT_name : int : Abbrev Number: 4 (DW_TAG_variable)  DW_AT_specification:   DW_AT_decl_file : 2  DW_AT_decl_line : 2  DW_AT_location : 9 byte block: 3 10 10 20 0 0 0 0 0 (DW_OP_addr: 201010) : Abbrev Number: 0 

我也尝试gcc 6.4和8.0,同样的问题; 对于gcc 5和clang 5和6,没有这样的问题。