2

We have built our code using gcc4.1.2, and we have used function "lstat64" that is defined in the "sys/stat.h" system header file and also defined in a third party library that we use.

When we "nm" our executable, we find that:

W  lstat64

My question Is: why gcc marked it as a weak function?

Also, we have ported our code to gcc4.4.4, we found that the new gcc did not marked the function as "weak", it marked it as undefined?

Why this change in behavior?

mnabil
  • 695
  • 1
  • 5
  • 19
  • 1
    Without more details, any answer we give here would be conjecture. We know nothing about the 'third party library' - did you rebuild it with the new compiler? Did you change *anything* else going from 4.1 to 4.4 - i.e. is it being built on a different computer? did you change the architecture from 32bit to 64bit (can be a side effect of compiling gcc without multilib support), did you change compilation flags? – Anya Shenanigans Mar 13 '19 at 12:01

1 Answers1

3

As per the GCC documentation:

weak
The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.

In your case lstat64 was probably marked as weak in GCC 4.1.2 because it would then not conflict with the third party library function. GCC probably wanted these external functions to have precedence.

But in a later version, GCC would have wanted its own version of lstat64 to have precedence.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • The idea is that, gcc4.1.2 marked the "lstat64" of the third party library as weak, however, in the runtime, the application uses the one of glibc. On the other hand, when we ported our code to gcc4.4.4, we found that the compiler marked "lstat64" of the third party library as unresolved, and in the run time, it try to use the one of the third party library not the one of glibc. Thus, I am trying to understand the root cause of this new behavior? – mnabil Mar 13 '19 at 11:25
  • I'm not sure how GCC can mark a third party function as weak. Possibly, the third party library writers marked their function as weak. – P.W Mar 13 '19 at 11:26
  • No, the third party library does not mark it as weak; – mnabil Mar 13 '19 at 11:59
  • `nm` command shows `lstat64` as weak. How would you know if this was marked for the GCC function or the third party function? – P.W Mar 13 '19 at 12:03
  • This is a goo question. The lstat64 of the gcc is in fact a wrapper for __lxstat64 as defined in the "stat.h" file. When I issue "nm" on our application, it gives a reference for the __lxstat64@glibc and a reference for lstat64 – mnabil Mar 13 '19 at 12:06
  • Also, in gcc4.4.4, nm of the application gives a reference to "U lstat64@ThirdParty.so", but in gcc4.1.2, nm the application gives "W lstat64" with no reference to the third party so – mnabil Mar 13 '19 at 12:07