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 
26 // Implementation of LoadedLibraries and friends
27 
28 // Ultimately this just uses loadquery()
29 // See:
30 // http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp
31 //      ?topic=/com.ibm.aix.basetechref/doc/basetrf1/loadquery.htm
32 
33 #ifndef __STDC_FORMAT_MACROS
34 #define __STDC_FORMAT_MACROS
35 #endif
36 // 'allocation.inline.hpp' triggers the inclusion of 'inttypes.h' which defines macros
37 // required by the definitions in 'globalDefinitions.hpp'. But these macros in 'inttypes.h'
38 // are only defined if '__STDC_FORMAT_MACROS' is defined!
39 #include "memory/allocation.inline.hpp"
40 #include "oops/oop.inline.hpp"
41 #include "runtime/threadCritical.hpp"
42 #include "utilities/debug.hpp"
43 #include "utilities/ostream.hpp"
44 #include "loadlib_aix.hpp"
45 #include "porting_aix.hpp"
46 
47 // For loadquery()
48 #include <sys/ldr.h>
49 
50 ///////////////////////////////////////////////////////////////////////////////
51 // Implementation for LoadedLibraryModule
52 
53 // output debug info
print(outputStream * os) const54 void LoadedLibraryModule::print(outputStream* os) const {
55   os->print("%15.15s: text: " INTPTR_FORMAT " - " INTPTR_FORMAT
56                ", data: " INTPTR_FORMAT " - " INTPTR_FORMAT " ",
57       shortname, text_from, text_to, data_from, data_to);
58   os->print(" %s", fullpath);
59   if (strlen(membername) > 0) {
60     os->print("(%s)", membername);
61   }
62   os->cr();
63 }
64 
65 
66 ///////////////////////////////////////////////////////////////////////////////
67 // Implementation for LoadedLibraries
68 
69 // class variables
70 LoadedLibraryModule LoadedLibraries::tab[MAX_MODULES];
71 int LoadedLibraries::num_loaded = 0;
72 
73 // Checks whether the address p points to any of the loaded code segments.
74 // If it does, returns the LoadedLibraryModule entry. If not, returns NULL.
75 // static
find_for_text_address(const unsigned char * p)76 const LoadedLibraryModule* LoadedLibraries::find_for_text_address(const unsigned char* p) {
77 
78   if (num_loaded == 0) {
79     reload();
80   }
81   for (int i = 0; i < num_loaded; i++) {
82     if (tab[i].is_in_text(p)) {
83       return &tab[i];
84     }
85   }
86   return NULL;
87 }
88 
89 // Checks whether the address p points to any of the loaded data segments.
90 // If it does, returns the LoadedLibraryModule entry. If not, returns NULL.
91 // static
find_for_data_address(const unsigned char * p)92 const LoadedLibraryModule* LoadedLibraries::find_for_data_address(const unsigned char* p) {
93   if (num_loaded == 0) {
94     reload();
95   }
96   for (int i = 0; i < num_loaded; i++) {
97     if (tab[i].is_in_data(p)) {
98       return &tab[i];
99     }
100   }
101   return NULL;
102 }
103 
104 // Rebuild the internal table of LoadedLibraryModule objects
105 // static
reload()106 void LoadedLibraries::reload() {
107 
108   ThreadCritical cs;
109 
110   // discard old content
111   num_loaded = 0;
112 
113   // Call loadquery(L_GETINFO..) to get a list of all loaded Dlls from AIX.
114   size_t buf_size = 4096;
115   char* loadquery_buf = AllocateHeap(buf_size, mtInternal);
116 
117   while(loadquery(L_GETINFO, loadquery_buf, buf_size) == -1) {
118     if (errno == ENOMEM) {
119       buf_size *= 2;
120       loadquery_buf = ReallocateHeap(loadquery_buf, buf_size, mtInternal);
121     } else {
122       FreeHeap(loadquery_buf);
123       // Ensure that the uintptr_t pointer is valid
124       assert(errno != EFAULT, "loadquery: Invalid uintptr_t in info buffer.");
125       fprintf(stderr, "loadquery failed (%d %s)", errno, strerror(errno));
126       return;
127     }
128   }
129 
130   // Iterate over the loadquery result. For details see sys/ldr.h on AIX.
131   const struct ld_info* p = (struct ld_info*) loadquery_buf;
132 
133   // Ensure we have all loaded libs.
134   bool all_loaded = false;
135   while(num_loaded < MAX_MODULES) {
136     LoadedLibraryModule& mod = tab[num_loaded];
137     mod.text_from = (const unsigned char*) p->ldinfo_textorg;
138     mod.text_to   = (const unsigned char*) (((char*)p->ldinfo_textorg) + p->ldinfo_textsize);
139     mod.data_from = (const unsigned char*) p->ldinfo_dataorg;
140     mod.data_to   = (const unsigned char*) (((char*)p->ldinfo_dataorg) + p->ldinfo_datasize);
141     sprintf(mod.fullpath, "%.*s", sizeof(mod.fullpath), p->ldinfo_filename);
142     // do we have a member name as well (see ldr.h)?
143     const char* p_mbr_name = p->ldinfo_filename + strlen(p->ldinfo_filename) + 1;
144     if (*p_mbr_name) {
145       sprintf(mod.membername, "%.*s", sizeof(mod.membername), p_mbr_name);
146     } else {
147       mod.membername[0] = '\0';
148     }
149 
150     // fill in the short name
151     const char* p_slash = strrchr(mod.fullpath, '/');
152     if (p_slash) {
153       sprintf(mod.shortname, "%.*s", sizeof(mod.shortname), p_slash + 1);
154     } else {
155       sprintf(mod.shortname, "%.*s", sizeof(mod.shortname), mod.fullpath);
156     }
157     num_loaded ++;
158 
159     // next entry...
160     if (p->ldinfo_next) {
161       p = (struct ld_info*)(((char*)p) + p->ldinfo_next);
162     } else {
163       all_loaded = true;
164       break;
165     }
166   }
167 
168   FreeHeap(loadquery_buf);
169 
170   // Ensure we have all loaded libs
171   assert(all_loaded, "loadquery returned more entries then expected. Please increase MAX_MODULES");
172 
173 } // end LoadedLibraries::reload()
174 
175 
176 // output loaded libraries table
177 //static
print(outputStream * os)178 void LoadedLibraries::print(outputStream* os) {
179 
180   for (int i = 0; i < num_loaded; i++) {
181     tab[i].print(os);
182   }
183 
184 }
185 
186