From owner-freebsd-current@FreeBSD.ORG Wed Oct 8 22:33:49 2008 Return-Path: Delivered-To: current@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E8DDB106569C; Wed, 8 Oct 2008 22:33:49 +0000 (UTC) (envelope-from sobomax@FreeBSD.org) Received: from sippysoft.com (gk1.360sip.com [72.236.70.240]) by mx1.freebsd.org (Postfix) with ESMTP id B101C8FC19; Wed, 8 Oct 2008 22:33:49 +0000 (UTC) (envelope-from sobomax@FreeBSD.org) Received: from [192.168.54.128] (S01060014bf8629c0.vc.shawcable.net [24.87.44.186]) (authenticated bits=0) by sippysoft.com (8.13.8/8.13.8) with ESMTP id m98MXlvk002312 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 8 Oct 2008 15:33:48 -0700 (PDT) (envelope-from sobomax@FreeBSD.org) Message-ID: <48ED3545.6030609@FreeBSD.org> Date: Wed, 08 Oct 2008 15:33:41 -0700 From: Maxim Sobolev Organization: Sippy Software, Inc. User-Agent: Thunderbird 2.0.0.17 (Windows/20080914) MIME-Version: 1.0 To: Alexander Kabaev References: <48ED27EA.9020407@FreeBSD.org> In-Reply-To: <48ED27EA.9020407@FreeBSD.org> Content-Type: text/plain; charset=KOI8-U; format=flowed Content-Transfer-Encoding: 7bit Cc: "current@freebsd.org" Subject: Re: dlsym(RTLD_NEXT) and weak symbols X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Oct 2008 22:33:50 -0000 Maxim Sobolev wrote: > Hi, > > I am not sure if it has even worked correctly, but calling > dlsym("dlopen", RTLD_NEXT) returns reference to the dlopen() function in > the libc, not reference to dlopen() function in the ld-elf.so. The > attempt to call this function then fails, since dlopen() in libc is just > a stub to make static linking happy. > > #pragma weak dlopen > void * > dlopen(const char *name, int mode) > { > _rtld_error(sorry); > return NULL; > } > > IMHO this is incorrect and is probably part of the bigger problem. The > dlsym(3) should return first non-weak symbol instead. The following patch fixes the issue for me: --- rtld.c 2008-08-18 13:24:19.000000000 -0700 +++ rtld.c 2008-10-08 15:28:47.000000000 -0700 @@ -1871,10 +1871,25 @@ if (handle == RTLD_NEXT) obj = obj->next; for (; obj != NULL; obj = obj->next) { - if ((def = symlook_obj(name, hash, obj, true)) != NULL) { + if ((def = symlook_obj(name, hash, obj, true)) != NULL && + ELF_ST_BIND(def->st_info) != STB_WEAK) { defobj = obj; break; } + def = NULL; + } + /* + * Search the dynamic linker itself, and possibly resolve the + * symbol from there. Only the values listed in the "exports" + * array can be resolved from the dynamic linker. + */ + if (def == NULL) { + def = symlook_obj(name, hash, &obj_rtld, true); + if (def != NULL && is_exported(def)) { + defobj = &obj_rtld; + } else { + def = NULL; + } } } else { assert(handle == RTLD_DEFAULT); -Maxim