GNU Argp (part of glibc, the GNU C library), changes its behavior based on the non-zero existence of various global variables. For example, if I define argp_program_bug_address as a non-zero value, it will be used as part of generated --help output.
Detecting the difference between zero and non-zero is straightforward. It's not as obvious how Arpg handles that I might or might not define argp_program_bug_address at all. If I do not define it, presumably a definition setting it to zero must exist in glibc. Indeed, there appears to be a defintion in argp/argp-ba.c (declaration). But if that's the case, if I define it, that would mean both I and the library defined it, resulting in link errors complaining about the multiple definition.
Weak symbols seem like a solution, but examining the glibc source, I cannot find any evidence that argp_program_bug_address or similar variables are defined weakly.
How is this working so that I can define a value that is used without causing a multiple definition error, but it also works if I never define it?
When you link to glibc, you're linking with (e.g.)
/usr/lib64/libc.so.This is a linker script:
argp/argp-ba.chas only a single define of:This definition goes into
/usr/lib64/libc.aand is a common symbol (nmtypeCandreadelftypeCOM).So, if you do not define it, the symbol ends up in the
.bsssection using the definition fromlibc.a:argp-ba.o.If you do define it, the linker uses your definition and the symbol ends up in the
.datasection.