1 /*
   2  * Copyright (c) 2016 SAP SE. 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 #include <stdio.h>
  24 #include <sys/ldr.h>
  25 
  26 #include "java_md_aix.h"
  27 
  28 static unsigned char dladdr_buffer[0x4000];
  29 
  30 static int fill_dll_info(void) {
  31     return loadquery(L_GETINFO, dladdr_buffer, sizeof(dladdr_buffer));
  32 }
  33 
  34 static int dladdr_dont_reload(void *addr, Dl_info *info) {
  35     const struct ld_info *p = (struct ld_info *)dladdr_buffer;
  36     memset((void *)info, 0, sizeof(Dl_info));
  37     for (;;) {
  38         if (addr >= p->ldinfo_textorg &&
  39             addr < (((char*)p->ldinfo_textorg) + p->ldinfo_textsize))
  40         {
  41             info->dli_fname = p->ldinfo_filename;
  42             return 1;
  43         }
  44         if (!p->ldinfo_next) {
  45             break;
  46         }
  47         p = (struct ld_info *)(((char *)p) + p->ldinfo_next);
  48     }
  49     return 0;
  50 }
  51 
  52 int dladdr(void *addr, Dl_info *info) {
  53     static int loaded = 0;
  54     int rc = 0;
  55     void *addr0;
  56     if (!addr) {
  57         return rc;
  58     }
  59     if (!loaded) {
  60         if (fill_dll_info() == -1)
  61             return rc;
  62         loaded = 1;
  63     }
  64 
  65     // first try with addr on cached data
  66     rc = dladdr_dont_reload(addr, info);
  67 
  68     // addr could be an AIX function descriptor, so try dereferenced version
  69     if (rc == 0) {
  70         addr0 = *((void **)addr);
  71         rc = dladdr_dont_reload(addr0, info);
  72     }
  73 
  74     // if we had no success until now, maybe loadquery info is outdated.
  75     // refresh and retry
  76     if (rc == 0) {
  77         if (fill_dll_info() == -1)
  78             return rc;
  79         rc = dladdr_dont_reload(addr, info);
  80         if (rc == 0) {
  81             rc = dladdr_dont_reload(addr0, info);
  82         }
  83     }
  84     return rc;
  85 }