1 /*
   2  * Copyright 2012, 2013 SAP AG. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include <stdio.h>
  26 #include <sys/ldr.h>
  27 #include <errno.h>
  28 
  29 #include "porting_aix.h"
  30 
  31 static unsigned char dladdr_buffer[0x4000];
  32 
  33 static void fill_dll_info(void) {
  34   int rc = loadquery(L_GETINFO,dladdr_buffer, sizeof(dladdr_buffer));
  35   if (rc == -1) {
  36     fprintf(stderr, "loadquery failed (%d %s)", errno, strerror(errno));
  37     fflush(stderr);
  38   }
  39 }
  40 
  41 static int dladdr_dont_reload(void* addr, Dl_info* info) {
  42   const struct ld_info* p = (struct ld_info*) dladdr_buffer;
  43   info->dli_fbase = 0; info->dli_fname = 0;
  44   info->dli_sname = 0; info->dli_saddr = 0;
  45   for (;;) {
  46     if (addr >= p->ldinfo_textorg &&
  47         addr < (((char*)p->ldinfo_textorg) + p->ldinfo_textsize)) {
  48       info->dli_fname = p->ldinfo_filename;
  49       info->dli_fbase = p->ldinfo_textorg;
  50       return 1; /* [sic] */
  51     }
  52     if (!p->ldinfo_next) {
  53       break;
  54     }
  55     p = (struct ld_info*)(((char*)p) + p->ldinfo_next);
  56   }
  57   return 0; /* [sic] */
  58 }
  59 
  60 #ifdef __cplusplus
  61 extern "C"
  62 #endif
  63 int dladdr(void *addr, Dl_info *info) {
  64   static int loaded = 0;
  65   if (!loaded) {
  66     fill_dll_info();
  67     loaded = 1;
  68   }
  69   if (!addr) {
  70     return 0;  /* [sic] */
  71   }
  72   /* Address could be AIX function descriptor? */
  73   void* const addr0 = *( (void**) addr );
  74   int rc = dladdr_dont_reload(addr, info);
  75   if (rc == 0) {
  76     rc = dladdr_dont_reload(addr0, info);
  77     if (rc == 0) { /* [sic] */
  78       fill_dll_info(); /* refill, maybe loadquery info is outdated */
  79       rc = dladdr_dont_reload(addr, info);
  80       if (rc == 0) {
  81         rc = dladdr_dont_reload(addr0, info);
  82       }
  83     }
  84   }
  85   return rc;
  86 }