Rearrange the inclusion of curve448/curve448_lcl.h
[oweals/openssl.git] / crypto / dso / dso_dlfcn.c
index 7abfe662842272873e2b4d0bc5d8df29889a8149..4240f5f5e30c85f51d2ac508cb952f41a43978f9 100644 (file)
@@ -17,6 +17,7 @@
 #endif
 
 #include "dso_locl.h"
+#include "e_os.h"
 
 #ifdef DSO_DLFCN
 
@@ -99,6 +100,7 @@ static int dlfcn_load(DSO *dso)
     /* See applicable comments in dso_dl.c */
     char *filename = DSO_convert_filename(dso, NULL);
     int flags = DLOPEN_FLAG;
+    int saveerrno = get_last_sys_error();
 
     if (filename == NULL) {
         DSOerr(DSO_F_DLFCN_LOAD, DSO_R_NO_FILENAME);
@@ -107,6 +109,10 @@ static int dlfcn_load(DSO *dso)
 # ifdef RTLD_GLOBAL
     if (dso->flags & DSO_FLAG_GLOBAL_SYMBOLS)
         flags |= RTLD_GLOBAL;
+# endif
+# ifdef _AIX
+    if (filename[strlen(filename) - 1] == ')')
+        flags |= RTLD_MEMBER;
 # endif
     ptr = dlopen(filename, flags);
     if (ptr == NULL) {
@@ -114,6 +120,11 @@ static int dlfcn_load(DSO *dso)
         ERR_add_error_data(4, "filename(", filename, "): ", dlerror());
         goto err;
     }
+    /*
+     * Some dlopen() implementations (e.g. solaris) do no preserve errno, even
+     * on a successful call.
+     */
+    set_sys_error(saveerrno);
     if (!sk_void_push(dso->meth_data, (char *)ptr)) {
         DSOerr(DSO_F_DLFCN_LOAD, DSO_R_STACK_ERROR);
         goto err;
@@ -326,12 +337,13 @@ typedef struct Dl_info {
  * address of a function, which is just located in the DATA segment instead of
  * the TEXT segment.
  */
-static int dladdr(void *addr, Dl_info *dl)
+static int dladdr(void *ptr, Dl_info *dl)
 {
+    uintptr_t addr = (uintptr_t)ptr;
     unsigned int found = 0;
     struct ld_info *ldinfos, *next_ldi, *this_ldi;
 
-    if ((ldinfos = (struct ld_info *)OPENSSL_malloc(DLFCN_LDINFO_SIZE)) == NULL) {
+    if ((ldinfos = OPENSSL_malloc(DLFCN_LDINFO_SIZE)) == NULL) {
         errno = ENOMEM;
         dl->dli_fname = NULL;
         return 0;
@@ -352,22 +364,39 @@ static int dladdr(void *addr, Dl_info *dl)
 
     do {
         this_ldi = next_ldi;
-        if (((addr >= this_ldi->ldinfo_textorg)
-             && (addr < (this_ldi->ldinfo_textorg + this_ldi->ldinfo_textsize)))
-            || ((addr >= this_ldi->ldinfo_dataorg)
-                && (addr <
-                    (this_ldi->ldinfo_dataorg + this_ldi->ldinfo_datasize)))) {
+        if (((addr >= (uintptr_t)this_ldi->ldinfo_textorg)
+             && (addr < ((uintptr_t)this_ldi->ldinfo_textorg +
+                         this_ldi->ldinfo_textsize)))
+            || ((addr >= (uintptr_t)this_ldi->ldinfo_dataorg)
+                && (addr < ((uintptr_t)this_ldi->ldinfo_dataorg +
+                            this_ldi->ldinfo_datasize)))) {
+            char *buffer, *member;
+            size_t buffer_sz, member_len;
+
+            buffer_sz = strlen(this_ldi->ldinfo_filename) + 1;
+            member = this_ldi->ldinfo_filename + buffer_sz;
+            if ((member_len = strlen(member)) > 0)
+                buffer_sz += 1 + member_len + 1;
             found = 1;
-            /*
-             * Ignoring the possibility of a member name and just returning
-             * the path name. See docs: sys/ldr.h, loadquery() and
-             * dlopen()/RTLD_MEMBER.
-             */
-            if ((dl->dli_fname =
-                 OPENSSL_strdup(this_ldi->ldinfo_filename)) == NULL)
+            if ((buffer = OPENSSL_malloc(buffer_sz)) != NULL) {
+                OPENSSL_strlcpy(buffer, this_ldi->ldinfo_filename, buffer_sz);
+                if (member_len > 0) {
+                    /*
+                     * Need to respect a possible member name and not just
+                     * returning the path name in this case. See docs:
+                     * sys/ldr.h, loadquery() and dlopen()/RTLD_MEMBER.
+                     */
+                    OPENSSL_strlcat(buffer, "(", buffer_sz);
+                    OPENSSL_strlcat(buffer, member, buffer_sz);
+                    OPENSSL_strlcat(buffer, ")", buffer_sz);
+                }
+                dl->dli_fname = buffer;
+            } else {
                 errno = ENOMEM;
+            }
         } else {
-            next_ldi = (char *)this_ldi + this_ldi->ldinfo_next;
+            next_ldi = (struct ld_info *)((uintptr_t)this_ldi +
+                                          this_ldi->ldinfo_next);
         }
     } while (this_ldi->ldinfo_next && !found);
     OPENSSL_free((void *)ldinfos);
@@ -395,7 +424,7 @@ static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
         len = (int)strlen(dli.dli_fname);
         if (sz <= 0) {
 #  ifdef _AIX
-            OPENSSL_free(dli.dli_fname);
+            OPENSSL_free((void *)dli.dli_fname);
 #  endif
             return len + 1;
         }
@@ -404,7 +433,7 @@ static int dlfcn_pathbyaddr(void *addr, char *path, int sz)
         memcpy(path, dli.dli_fname, len);
         path[len++] = 0;
 #  ifdef _AIX
-        OPENSSL_free(dli.dli_fname);
+        OPENSSL_free((void *)dli.dli_fname);
 #  endif
         return len;
     }