Help with linking and symbol resolution

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Thank You

--
Lakshay G.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux