From owner-freebsd-hackers@freebsd.org Sat Dec 26 11:59:12 2015 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 3BA06A52AAE for ; Sat, 26 Dec 2015 11:59:12 +0000 (UTC) (envelope-from radovanovic@gmail.com) Received: from mail-wm0-x232.google.com (mail-wm0-x232.google.com [IPv6:2a00:1450:400c:c09::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id EBC4B14AD for ; Sat, 26 Dec 2015 11:59:11 +0000 (UTC) (envelope-from radovanovic@gmail.com) Received: by mail-wm0-x232.google.com with SMTP id l126so221877046wml.0 for ; Sat, 26 Dec 2015 03:59:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=p/Eoc9eZd4Lix5tcSRkyx/w+jT+3xg5kL2Ch1yMyjUg=; b=J6ywhA6iDGWBV8GCIq6dfXckRTElckAGUY+smoq5LThoFdUlnAenEpCuTreM3BTMDh zi/Omh2cWMbP2QVOzXiO/U/999z2J4CsSHaI3N1KW+Z3lXVvHrUqMQuj0RlYLDUvEyjc lEC1MASd97XKNHlGuT4+d/8fQRDaRBh8yRLaAyY87+SdsZWZ10bSuptYiVWU6l2bKqM+ XB8kjuC6Eq+ZdcaBKDsJQbPikyLsyaHxtnnrxGg5obeQDs2hCsvWGA3YJ4iKo0zYqHeL XA06nMKZAnX9yc7xsXGifke0bY8n3/zqQOcpmIymAwo3OWkxH/X5zVh+EGy12PRPD0Dr AGHw== X-Received: by 10.28.94.1 with SMTP id s1mr47987507wmb.60.1451131150253; Sat, 26 Dec 2015 03:59:10 -0800 (PST) Received: from zmaj.softwarehood.com (178-221-115-29.dynamic.isp.telekom.rs. [178.221.115.29]) by smtp.googlemail.com with ESMTPSA id l184sm40411260wmf.6.2015.12.26.03.59.09 for (version=TLSv1/SSLv3 cipher=OTHER); Sat, 26 Dec 2015 03:59:09 -0800 (PST) Message-ID: <567E810C.3040809@gmail.com> Date: Sat, 26 Dec 2015 12:59:08 +0100 From: Ivan Radovanovic User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:17.0) Gecko/20130812 Thunderbird/17.0.8 MIME-Version: 1.0 To: freebsd-hackers@freebsd.org Subject: Unexpected behavior of dynamic linker Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 Dec 2015 11:59:12 -0000 Hello, While investigating possibility of building some module system on FreeBSD I stumbled upon unexpected (at least for me) behavior of dynamic linker. I have several (C++) modules organized like this: * main program (executable) * main library (shared object, loaded with RTLD_GLOBAL since it provides symbols for main program and other modules) * module 1 (shared object, loaded with RTLD_LOCAL) I put all private initializations for library and module in (static) global object constructors, but I gave to both classes same (original) name "PrivateClass" (so there is in fact PrivateClass in mainlib.so, and unrelated PrivateClass in module.so). What is happening is that when main program loads main library (via dlopen(3)) its _init calls correctly mainlib::PrivateClass constructor which performs initialization, but when main program later loads module (again via dlopen(3)) its _init doesn't call module::PrivateClass constructor, but rather mainlib::PrivateClass constructor. I understand this is happening because symbols with same name exist in both shared objects and dynamic linker is replacing reference to module::PrivateClass with reference to mainlib::PrivateClass, but I would expect symbol to be looked up outside of module only if it doesn't exist within it (ie, the inner-most definition to be used) - in this case PrivateClass exists within module? Further I am not sure what would be correct solution for this in my case - C++ has static modifier for objects and functions, but not for classes to make them unavailable from other modules and solution with same random namespace name doesn't sound elegant enough :-) Any thoughts regarding this (I can also supply source files to test this behavior)? Kind regards, Ivan