* Help with linking and symbol resolution
@ 2017-01-28 15:29 Lakshay Garg
2017-01-30 4:51 ` Alan Modra
2017-01-31 14:19 ` Nick Clifton
0 siblings, 2 replies; 3+ messages in thread
From: Lakshay Garg @ 2017-01-28 15:29 UTC (permalink / raw)
To: binutils
Hello everyone
I am taking a compilers course and have stumbled on a problem which I
am unable to resolve. Here is the code I am dealing with:
/* f1.c */
#include <stdio.h>
double c;
int main() {
c = 1;
printf("sizeof(c) = %lu\n", sizeof(c));
return 0;
}
/* f2.c */
int c = 0;
I compile the program using gcc f1.c f2.c. From what I have learnt, I
believe that the output of the file should be sizeof(c) = 4 since the
declaration of c in f1.c is weak and would be resolved to the symbol c
in file f2.c which is strong. I also checked the symbol table using
the readelf utility which also shows that the size of c is 4. Here is
a snippet of the output from readelf -s ./a.out:
Num: Value Size Type Bind Vis Ndx Name
--------------------------------------------------------------
61: 0000000000400430 42 FUNC GLOBAL DEFAULT 14 _start
62: 000000000060103c 4 OBJECT GLOBAL DEFAULT 26 c
63: 0000000000601038 0 NOTYPE GLOBAL DEFAULT 26 __bss_start
--------------------------------------------------------------
But the output from the program is sizeof(c) = 8. Could someone please
explain this behavior? I am using Ubuntu 16.04, gcc version 5.4.0
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
Thank You
--
Lakshay G.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Help with linking and symbol resolution
2017-01-28 15:29 Help with linking and symbol resolution Lakshay Garg
@ 2017-01-30 4:51 ` Alan Modra
2017-01-31 14:19 ` Nick Clifton
1 sibling, 0 replies; 3+ messages in thread
From: Alan Modra @ 2017-01-30 4:51 UTC (permalink / raw)
To: Lakshay Garg; +Cc: binutils
On Sat, Jan 28, 2017 at 08:59:35PM +0530, Lakshay Garg wrote:
> From what I have learnt, I
> believe that the output of the file should be sizeof(c) = 4 since the
> declaration of c in f1.c is weak and would be resolved to the symbol c
> in file f2.c which is strong.
No. (Also don't ignore linker warnings).
Read the C standard regarding sizeof. What is the type of "c" in f1.c?
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Help with linking and symbol resolution
2017-01-28 15:29 Help with linking and symbol resolution Lakshay Garg
2017-01-30 4:51 ` Alan Modra
@ 2017-01-31 14:19 ` Nick Clifton
1 sibling, 0 replies; 3+ messages in thread
From: Nick Clifton @ 2017-01-31 14:19 UTC (permalink / raw)
To: Lakshay Garg, binutils
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-01-31 14:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-28 15:29 Help with linking and symbol resolution Lakshay Garg
2017-01-30 4:51 ` Alan Modra
2017-01-31 14:19 ` Nick Clifton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).