public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Nick Clifton <nickc@redhat.com>
To: Lakshay Garg <lakshaygarg1996+gcc@gmail.com>, binutils@sourceware.org
Subject: Re: Help with linking and symbol resolution
Date: Tue, 31 Jan 2017 14:19:00 -0000	[thread overview]
Message-ID: <9a23f613-bb32-8b08-ce37-ebc5694ae2e2@redhat.com> (raw)
In-Reply-To: <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

      parent reply	other threads:[~2017-01-31 14:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-28 15:29 Lakshay Garg
2017-01-30  4:51 ` Alan Modra
2017-01-31 14:19 ` Nick Clifton [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9a23f613-bb32-8b08-ce37-ebc5694ae2e2@redhat.com \
    --to=nickc@redhat.com \
    --cc=binutils@sourceware.org \
    --cc=lakshaygarg1996+gcc@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).