In an LJ post, the --defsym
flag was used for passing the build date into the source code:
#include <stdio.h>
extern char __BUILD_DATE;
void main(void) {
printf("Build date: %u\n", (unsigned long) &__BUILD_DATE);
}
by linking with the following flags:
gcc example.c -Xlinker --defsym -Xlinker __BUILD_DATE=$(date +%Y%m%d)
According to the ld manual,
--defsym symbol=expression
Create a global symbol in the output file, containing the absolute address given by expression.
I am trying to understand the following:
- How is the 9-character string for the build date (
YYYYmmdd
+\0
) stored in memory? - If
--defsym
creates a symbol containing an address, why__BUILD_DATE
is defined as a char and not as a pointer or as an integral type? - Why
__BUILD_DATE
is defined aschar
and notunsigned long
if it is eventually casted tounsigned long
?