Help with linking and symbol resolution
Nick Clifton
nickc@redhat.com
Tue Jan 31 14:19:00 GMT 2017
Hi Lakshay,
Alan has already given you the correct answer to this quandary.
I thought however that I should also point out that there is an
error in the slides you referenced:
> If the question is unclear, please see slides 22,23,24 from here:
> https://www.cs.cmu.edu/afs/cs/academic/class/15213-f10/www/lectures/11-linking.pdf
Slide 22 is wrong. The definition of foo in p2.c creates a
*common* symbol, not a *weak* one. Common symbols can be
combined with strong symbols of the same name, provided that
their size and alignment are the same.
Weak symbols are created using an attribute, at least when
using gcc. To use your example:
/* f1.c */
#include <stdio.h>
double c __attribute__((weak));
int main() {
c = 1;
printf("sizeof(c) = %lu\n", sizeof(c));
return 0;
}
/* f2.c */
int c = 0;
This should compile and link without any warning messages.
You will still get the size being reported as 8 however, because
of the way the sizeof operator works.
Note - the assignment "c = 1" at the start of main() is actually
quite dangerous since it will be storing an 8-byte value into a
4-byte sized area of memory. See the "puzzles" on slide 24.
Cheers
Nick
More information about the Binutils
mailing list