gcc-nm behavior with -flto
Xi Ruoyao
xry111@mengyan1223.wang
Wed Feb 5 02:43:00 GMT 2020
On 2020-02-04 15:58 -0500, Arvind Sankar wrote:
> Hi, I get some odd symbol types when using gcc-nm on an lto file.
>
> Given the source file
> void text(void) {}
> int data = 1;
> int bss = 0;
> int common;
>
> gcc -c test.c && gcc-nm test.o:
> 0000000000000000 B bss
> 0000000000000004 C common
> 0000000000000000 D data
> 0000000000000000 T text
>
> gcc -flto -c test.c && gcc-nm test.o:
> 00000000 T bss
> 00000000 C common
> 00000000 T data
> 00000000 T text
>
> gcc -flto -fno-common -c test.c && gcc-nm test.o:
> 00000000 T bss
> 00000000 T common
> 00000000 T data
> 00000000 T text
>
> i.e. with LTO, .bss and .data symbols are shown as text symbols instead
> by gcc-nm. This causes issues with configure scripts that use libtool
> when the CFLAGS contain both -flto and -fno-common. There's a section in
> the configure script that tries to figure out how to parse nm's output.
> It compiles a source file that contains a common symbol and a function,
> then parses nm output to create a second .c file with those
> declarations, and tries to check that everything is working by linking
> them together. In the -flto -fno-common case, the second file ends up
> with both symbols turning into functions which then gives a link-time
> error.
I can't reproduce this.
$ cat test1.c
void text(void) {}
int data = 1;
int bss = 0;
int common;
$ cat test2.c
#include <assert.h>
extern void text(void);
extern int data;
extern int bss;
extern int common;
int main()
{
assert(bss == 0 && data == 1);
return 0;
}
$ cc test1.c test2.c -flto -fno-common
$ ./a.out
$
Did you forgot the "extern" in test2.c for "int common;"? "int common;" is a
tentative definition, equivalent to "int common = 0;", in ANSI C. "-fcommon" is
a workaround enabled by default to resolve same tentative definitions in
multiple files. With "-fno-common" there is no such a workaround so the linker
will complain.
--
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University
More information about the Binutils
mailing list