From owner-freebsd-hackers@FreeBSD.ORG Wed Apr 18 10:36:31 2012 Return-Path: Delivered-To: freebsd-hackers@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 2B7721065811 for ; Wed, 18 Apr 2012 10:36:31 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from citadel.icyb.net.ua (citadel.icyb.net.ua [212.40.38.140]) by mx1.freebsd.org (Postfix) with ESMTP id 14D838FC1B for ; Wed, 18 Apr 2012 10:36:29 +0000 (UTC) Received: from porto.starpoint.kiev.ua (porto-e.starpoint.kiev.ua [212.40.38.100]) by citadel.icyb.net.ua (8.8.8p3/ICyb-2.3exp) with ESMTP id NAA10235 for ; Wed, 18 Apr 2012 13:36:28 +0300 (EEST) (envelope-from avg@FreeBSD.org) Received: from localhost ([127.0.0.1]) by porto.starpoint.kiev.ua with esmtp (Exim 4.34 (FreeBSD)) id 1SKSFQ-000M6M-8Z for freebsd-hackers@FreeBSD.org; Wed, 18 Apr 2012 13:36:28 +0300 Message-ID: <4F8E992A.2090705@FreeBSD.org> Date: Wed, 18 Apr 2012 13:36:26 +0300 From: Andriy Gapon User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:10.0.3) Gecko/20120317 Thunderbird/10.0.3 MIME-Version: 1.0 To: freebsd-hackers@FreeBSD.org X-Enigmail-Version: 1.4 Content-Type: text/plain; charset=X-VIET-VPS Content-Transfer-Encoding: 7bit Cc: Subject: 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:36:31 -0000 I just would like to share something that I stumbled upon. Maybe this is something well known, then forgive me for the noise. When ld combines multiple object files it overrides weak symbol definitions 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-examples.html 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 symbol table of the library the object file contains some interesting symbols - if linker extracts an object file then it processes all symbols in it And now the following observation: if linker has already seen a weak definition 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 (strong) definitions for some symbols that already have weak definitions, the linker 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. This is something that was unexpected to me. Some references: http://webpages.charter.net/ppluzhnikov/linker.html http://glandium.org/blog/?p=2388 -- Andriy Gapon