Fixed support for RTLD_NEXT in dlsym()

The previous implementation of this flag was broken--it behaved identically
to RTLD_DEFAULT.  This adds a proper implementation, which examines the address
of the calling function, and uses it to determine which library to use to begin
the symbol search process.

Change-Id: I2ad2b46363f68932af63a3828a22f9c7987eea67
diff --git a/linker/dlfcn.c b/linker/dlfcn.c
index dcadce5..30f5f4c 100644
--- a/linker/dlfcn.c
+++ b/linker/dlfcn.c
@@ -90,9 +90,15 @@
     }
 
     if(handle == RTLD_DEFAULT) {
-        sym = lookup(symbol, &found);
+        sym = lookup(symbol, &found, NULL);
     } else if(handle == RTLD_NEXT) {
-        sym = lookup(symbol, &found);
+        void *ret_addr = __builtin_return_address(0);
+        soinfo *si = find_containing_library(ret_addr);
+
+        sym = NULL;
+        if(si && si->next) {
+            sym = lookup(symbol, &found, si->next);
+        }
     } else {
         found = (soinfo*)handle;
         sym = lookup_in_library(found, symbol);