This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Help with linking and symbol resolution
- From: Nick Clifton <nickc at redhat dot com>
- To: Lakshay Garg <lakshaygarg1996+gcc at gmail dot com>, binutils at sourceware dot org
- Date: Tue, 31 Jan 2017 14:19:40 +0000
- Subject: Re: Help with linking and symbol resolution
- Authentication-results: sourceware.org; auth=none
- References: <CAJq3jywFvezWrBWknbXyD3bZ9wZdNMwv80N71t=FfpV4bquzSA@mail.gmail.com>
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