Re: symbol duplication problem with -O0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



이종필 <beleief@xxxxxxxxx> writes:

> I have a question about optimization option.
> When i write/build a test code, system call symbol is different
> according to optimization option.

Yes.  When optimizing, in some cases gcc will replace a call to a known
library function with a call to a different known library function.


> int main(void) {
>     struct stat buf;
>     if ( stat("test.txt", &buf) < 0 ) {
>       printf("error\n");
>     } else {
>       printf("stat success\n");
>     }
>     return 0;
> }
>
> 1. with -O0
>
> $ gcc -O0 -c main.c
> $ nm main.o
> 0000000000000000 T main
>                  U puts
>                  U stat
> $
>
>
> 2. with -O2
> $ gcc -O2 -c main.c
> $ nm main.o
>                  U __xstat
> 0000000000000000 T main
>                  U puts
>
> I don't know why symbol results are different.

When gcc is optimizing, and sees a call to printf with a string with no
% characters ending in a newline, it changes the call to use puts
instead.

> Which gcc option in -O1/-O2 make __xstat symbol?

This one is not gcc, actually; it is glibc.  When glibc is compiled with
optimization, it turns stat into an inline function which calls __xstat.
See <sys/stat.h>.


> I think using -O0 can make a problem.
>
> ex)
> main.c : stat() system call usage
> test.c : stat variable declaration(of course, it's not a good declaration).
> gcc -O0 main.c test.c
>
> In this case, stat in main.c will refer stat symbol in test.c.
> It's a problem.
> Which option prevents this problem when -O0 is used?

This code is broken, because you are trying to call a variable as a
function.  Nothing prevents that problem at -O0.  You will get a crash
at runtime.

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux