Re: dlsym(RTLD_NEXT) and weak symbols

From: Alexander Kabaev <kabaev_at_gmail.com>
Date: Wed, 8 Oct 2008 19:29:05 -0400
On Wed, 08 Oct 2008 15:33:41 -0700
Maxim Sobolev <sobomax_at_FreeBSD.org> wrote:
> 
> The following patch fixes the issue for me:
>
<SKIP>

I do not think your patch is completely correct. How about this one
instead: 

=== rtld.c
==================================================================
--- rtld.c	(revision 183657)
+++ rtld.c	(local)
_at__at_ -1925,7 +1925,7 _at__at_
 {
     DoneList donelist;
     const Obj_Entry *obj, *defobj;
-    const Elf_Sym *def;
+    const Elf_Sym *def, *symp;
     unsigned long hash;
     int lockstate;
 
_at__at_ -1951,11 +1951,28 _at__at_
 	    if (handle == RTLD_NEXT)
 		obj = obj->next;
 	    for (; obj != NULL; obj = obj->next) {
-		if ((def = symlook_obj(name, hash, obj, ve, flags)) != NULL) {
-		    defobj = obj;
-		    break;
+	    	if ((symp = symlook_obj(name, hash, obj, ve, flags)) != NULL) {
+		    if (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK) {
+			def = symp;
+			defobj = obj;
+			if (ELF_ST_BIND(def->st_info) != STB_WEAK)
+			    break;
+		    }
 		}
 	    }
+	    /*
+	     * Search the dynamic linker itself, and possibly resolve the
+	     * symbol from there.  This is how the application links to
+	     * dynamic linker services such as dlopen.  Only the values listed
+	     * in the "exports" array can be resolved from the dynamic linker.
+	     */
+	    if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
+		symp = symlook_obj(name, hash, &obj_rtld, ve, flags);
+		if (symp != NULL && is_exported(symp)) {
+		    def = symp;
+		    defobj = &obj_rtld;
+		}
+	    }
 	} else {
 	    assert(handle == RTLD_DEFAULT);
 	    def = symlook_default(name, hash, obj, &defobj, ve, flags);

Received on Wed Oct 08 2008 - 21:55:56 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:36 UTC