From owner-freebsd-hackers@FreeBSD.ORG Wed Apr 18 10:50:03 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A04B31065677; Wed, 18 Apr 2012 10:50:03 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.zoral.com.ua (mx0.zoral.com.ua [91.193.166.200]) by mx1.freebsd.org (Postfix) with ESMTP id 1888E8FC16; Wed, 18 Apr 2012 10:50:02 +0000 (UTC) Received: from skuns.kiev.zoral.com.ua (localhost [127.0.0.1]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id q3IAntkT095361; Wed, 18 Apr 2012 13:49:55 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.5/8.14.5) with ESMTP id q3IAntBL098721; Wed, 18 Apr 2012 13:49:55 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.5/8.14.5/Submit) id q3IAntkS098720; Wed, 18 Apr 2012 13:49:55 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 18 Apr 2012 13:49:55 +0300 From: Konstantin Belousov To: Andriy Gapon Message-ID: <20120418104955.GV2358@deviant.kiev.zoral.com.ua> References: <4F8E992A.2090705@FreeBSD.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="bvzltMstmN4o5fnw" Content-Disposition: inline In-Reply-To: <4F8E992A.2090705@FreeBSD.org> User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: clamav-milter 0.95.2 at skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.0 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua Cc: freebsd-hackers@freebsd.org Subject: Re: weak symbols vs archive libraries X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Apr 2012 10:50:03 -0000 --bvzltMstmN4o5fnw Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Apr 18, 2012 at 01:36:26PM +0300, Andriy Gapon wrote: >=20 > I just would like to share something that I stumbled upon. > Maybe this is something well known, then forgive me for the noise. >=20 > When ld combines multiple object files it overrides weak symbol definitio= ns with > a strong definition (if any). There are many examples/demonstrations on = the > Internet on how this works, e.g.: > http://winfred-lu.blogspot.com/2009/11/understand-weak-symbols-by-example= s.html >=20 > But when the object files are spread across multiple archives, the there = could > be some surprises. > My understanding is that there are two big rules that linker follows with > respect to archive libraries: > - linker extracts an object file from a library only if according to a sy= mbol > table of the library the object file contains some interesting symbols > - if linker extracts an object file then it processes all symbols in it >=20 > And now the following observation: if linker has already seen a weak defi= nition > for a symbol, then it will not actively seek any other definitions for it= . But > it will take into account the definitions that it stumbles upon. > For example, if an object file in an archive library contains only (stron= g) > definitions for some symbols that already have weak definitions, the link= er will > not extract that object file and will not look into it. And thus the weak > definition will not be overridden. OTOH, if that object file contains a > definition for at least one still undefined symbol, then the file will be > interesting to linker, it will extract the file, process _all_ symbols in= it and > thus will override the weak definitions with the strong ones. >=20 > This is something that was unexpected to me. >=20 > Some references: > http://webpages.charter.net/ppluzhnikov/linker.html > http://glandium.org/blog/?p=3D2388 This is from the ELF standard version 1.2 PDF, page 1-5: When the link editor searches archive libraries, it extracts archive members that contain definitions of undefined global symbols. The member's definition may be either a global or a weak symbol. The link editor does not extract archive members to resolve undefined weak symbols. Unresolved weak symbols have a zero value. --bvzltMstmN4o5fnw Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (FreeBSD) iEYEARECAAYFAk+OnFMACgkQC3+MBN1Mb4gR9gCfWKSt7zwtmutep4JKV15Tv0F0 xEMAoIaOLawLAjA1k4N5Dsbc4fVRwQPg =CCfN -----END PGP SIGNATURE----- --bvzltMstmN4o5fnw--