This is the mail archive of the 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]

Re: Help with linking and symbol resolution

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:

  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.


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