This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Help with linking and symbol resolution


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]