2
* Copyright (c) 2012, 2024 SAP SE. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
24
// needs to be defined first, so that the implicit loaded xcoff.h header defines
25
// the right structures to analyze the loader header of 64 Bit executable files
26
// this is needed for rtv_linkedin_libpath() to get the linked (burned) in library
27
// search path of an XCOFF executable
31
#include "asm/assembler.hpp"
32
#include "compiler/disassembler.hpp"
33
#include "loadlib_aix.hpp"
34
#include "memory/allocation.hpp"
35
#include "memory/allocation.inline.hpp"
36
#include "misc_aix.hpp"
37
#include "porting_aix.hpp"
38
#include "runtime/javaThread.hpp"
39
#include "runtime/os.hpp"
40
#include "utilities/align.hpp"
41
#include "utilities/debug.hpp"
47
//////////////////////////////////
48
// Provide implementation for dladdr based on LoadedLibraries pool and
49
// traceback table scan
51
// Search traceback table in stack,
52
// return procedure name from trace back table.
53
#define MAX_FUNC_SEARCH_LEN 0x10000
55
#define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))
57
// Typedefs for stackslots, stack pointers, pointers to op codes.
58
typedef unsigned long stackslot_t;
59
typedef stackslot_t* stackptr_t;
60
typedef unsigned int* codeptr_t;
62
// Unfortunately, the interface of dladdr makes the implementer
63
// responsible for maintaining memory for function name/library
64
// name. I guess this is because most OS's keep those values as part
65
// of the mapped executable image ready to use. On AIX, this doesn't
66
// work, so I have to keep the returned strings. For now, I do this in
67
// a primitive string map. Should this turn out to be a performance
68
// problem, a better hashmap has to be used.
70
struct node : public CHeapObj<mtInternal> {
79
fixed_strings() : first(0) {}
90
char* intern(const char* s) {
91
for (node* n = first; n; n = n->next) {
92
if (strcmp(n->v, s) == 0) {
97
p->v = os::strdup_check_oom(s);
104
static fixed_strings dladdr_fixed_strings;
106
bool AixSymbols::get_function_name (
107
address pc0, // [in] program counter
108
char* p_name, size_t namelen, // [out] optional: function name ("" if not available)
109
int* p_displacement, // [out] optional: displacement (-1 if not available)
110
const struct tbtable** p_tb, // [out] optional: ptr to traceback table to get further
111
// information (null if not available)
112
bool demangle // [in] whether to demangle the name
114
struct tbtable* tb = 0;
115
unsigned int searchcount = 0;
117
// initialize output parameters
118
if (p_name && namelen > 0) {
121
if (p_displacement) {
122
*p_displacement = -1;
128
codeptr_t pc = (codeptr_t)pc0;
130
// weed out obvious bogus states
131
if (pc < (codeptr_t)0x1000) {
132
trcVerbose("invalid program counter");
136
// We see random but frequent crashes in this function since some months mainly on shutdown
137
// (-XX:+DumpInfoAtExit). It appears the page we are reading is randomly disappearing while
139
// As the pc cannot be trusted to be anything sensible lets make all reads via SafeFetch. Also
140
// bail if this is not a text address right now.
141
if (!LoadedLibraries::find_for_text_address(pc, nullptr)) {
142
trcVerbose("not a text address");
146
// .. (Note that is_readable_pointer returns true if safefetch stubs are not there yet;
147
// in that case I try reading the traceback table unsafe - I rather risk secondary crashes in
148
// error files than not having a callstack.)
149
#define CHECK_POINTER_READABLE(p) \
150
if (!os::is_readable_pointer(p)) { \
151
trcVerbose("pc not readable"); \
155
codeptr_t pc2 = (codeptr_t) pc;
157
// Make sure the pointer is word aligned.
158
pc2 = (codeptr_t) align_up((char*)pc2, 4);
159
CHECK_POINTER_READABLE(pc2)
161
// Find start of traceback table.
162
// (starts after code, is marked by word-aligned (32bit) zeros)
163
while ((*pc2 != 0) && (searchcount++ < MAX_FUNC_SEARCH_LEN)) {
164
CHECK_POINTER_READABLE(pc2)
168
trcVerbose("no traceback table found");
172
// Set up addressability to the traceback table
174
tb = (struct tbtable*) (pc2 + 1);
176
// Is this really a traceback table? No way to be sure but
177
// some indicators we can check.
178
if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {
179
// Language specifiers, go from 0 (C) to 14 (Objective C).
180
// According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm.
181
trcVerbose("no traceback table found");
185
// Existence of fields in the tbtable extension are contingent upon
186
// specific fields in the base table. Check for their existence so
187
// that we can address the function name if it exists.
188
pc2 = (codeptr_t) tb +
189
sizeof(struct tbtable_short)/sizeof(int);
190
if (tb->tb.fixedparms != 0 || tb->tb.floatparms != 0)
193
CHECK_POINTER_READABLE(pc2)
195
if (tb->tb.has_tboff == TRUE) {
197
// I want to know the displacement
198
const unsigned int tb_offset = *pc2;
199
codeptr_t start_of_procedure =
200
(codeptr_t)(((char*)tb) - 4 - tb_offset); // (-4 to omit leading 0000)
202
// Weed out the cases where we did find the wrong traceback table.
203
if (pc < start_of_procedure) {
204
trcVerbose("no traceback table found");
208
// return the displacement
209
if (p_displacement) {
210
(*p_displacement) = (int) PTRDIFF_BYTES(pc, start_of_procedure);
215
// return -1 for displacement
216
if (p_displacement) {
217
(*p_displacement) = -1;
221
if (tb->tb.int_hndl == TRUE)
224
if (tb->tb.has_ctl == TRUE)
225
pc2 += (*pc2) + 1; // don't care
227
CHECK_POINTER_READABLE(pc2)
230
// return function name if it exists.
232
if (p_name && namelen > 0) {
233
if (tb->tb.name_present) {
234
// Copy name from text because it may not be zero terminated.
235
const short l = MIN2<short>(*((short*)pc2), namelen - 1);
237
int i = 0; char* const p = (char*)pc2 + sizeof(short);
238
while (i < l && os::is_readable_pointer(p + i)) {
244
// If it is a C++ name, try and demangle it using the __cxa_demangle interface(see demangle.h).
247
char *demangled_name = abi::__cxa_demangle(p_name, nullptr, nullptr, &status);
248
if ((demangled_name != nullptr) && (status == 0)) {
249
strncpy(p_name, demangled_name, namelen-1);
250
p_name[namelen-1] = '\0';
252
if (demangled_name != nullptr) {
253
ALLOW_C_FUNCTION(::free, ::free(demangled_name));
257
strncpy(p_name, "<nameless function>", namelen-1);
258
p_name[namelen-1] = '\0';
262
// Return traceback table, if user wants it.
271
bool AixSymbols::get_module_name(address pc,
272
char* p_name, size_t namelen) {
274
if (p_name && namelen > 0) {
277
if (LoadedLibraries::find_for_text_address(pc, &lm)) {
278
strncpy(p_name, lm.shortname, namelen);
279
p_name[namelen - 1] = '\0';
287
bool AixSymbols::get_module_name_and_base(address pc,
288
char* p_name, size_t namelen,
291
if (p_base && p_name && namelen > 0) {
294
if (LoadedLibraries::find_for_text_address(pc, &lm)) {
295
strncpy(p_name, lm.shortname, namelen);
296
p_name[namelen - 1] = '\0';
297
*p_base = (address) lm.text;
305
// Special implementation of dladdr for Aix based on LoadedLibraries
306
// Note: dladdr returns non-zero for ok, 0 for error!
307
// Note: dladdr is not posix, but a non-standard GNU extension. So this tries to
308
// fulfill the contract of dladdr on Linux (see http://linux.die.net/man/3/dladdr)
309
// Note: addr may be both an AIX function descriptor or a real code pointer
310
// to the entry of a function.
312
int dladdr(void* addr, Dl_info* info) {
322
const char* const ZEROSTRING = "";
324
// Always return a string, even if a "" one. Linux dladdr manpage
325
// does not say anything about returning null
326
info->dli_fname = ZEROSTRING;
327
info->dli_sname = ZEROSTRING;
328
info->dli_saddr = nullptr;
330
address p = (address) addr;
334
enum { noclue, code, data } type = noclue;
336
trcVerbose("dladdr(%p)...", p);
338
// Note: input address may be a function. I accept both a pointer to
339
// the entry of a function and a pointer to the function descriptor.
341
found = LoadedLibraries::find_for_text_address(p, &lm);
347
// Not a pointer into any text segment. Is it a function descriptor?
348
const FunctionDescriptor* const pfd = (const FunctionDescriptor*) p;
351
found = LoadedLibraries::find_for_text_address(p, &lm);
359
// Neither direct code pointer nor function descriptor. A data ptr?
361
found = LoadedLibraries::find_for_data_address(p, &lm);
367
// If we did find the shared library this address belongs to (either
368
// code or data segment) resolve library path and, if possible, the
372
// No need to intern the libpath, that one is already interned one layer below.
373
info->dli_fname = lm.path;
377
// For code symbols resolve function name and displacement. Use
378
// displacement to calc start of function.
379
char funcname[256] = "";
380
int displacement = 0;
382
if (AixSymbols::get_function_name(p, funcname, sizeof(funcname),
383
&displacement, nullptr, true)) {
384
if (funcname[0] != '\0') {
385
const char* const interned = dladdr_fixed_strings.intern(funcname);
386
info->dli_sname = interned;
387
trcVerbose("... function name: %s ...", interned);
390
// From the displacement calculate the start of the function.
391
if (displacement != -1) {
392
info->dli_saddr = p - displacement;
398
// No traceback table found. Just assume the pointer is it.
403
} else if (type == data) {
409
ShouldNotReachHere();
412
rc = 1; // success: return 1 [sic]
418
assert(info->dli_fname, "");
419
assert(info->dli_sname, "");
420
assert(info->dli_saddr, "");
423
return rc; // error: return 0 [sic]
427
/////////////////////////////////////////////////////////////////////////////
428
// Native callstack dumping
430
// Print the traceback table for one stack frame.
431
static void print_tbtable (outputStream* st, const struct tbtable* p_tb) {
433
if (p_tb == nullptr) {
438
switch(p_tb->tb.lang) {
439
case TB_C: st->print("C"); break;
440
case TB_FORTRAN: st->print("FORTRAN"); break;
441
case TB_PASCAL: st->print("PASCAL"); break;
442
case TB_ADA: st->print("ADA"); break;
443
case TB_PL1: st->print("PL1"); break;
444
case TB_BASIC: st->print("BASIC"); break;
445
case TB_LISP: st->print("LISP"); break;
446
case TB_COBOL: st->print("COBOL"); break;
447
case TB_MODULA2: st->print("MODULA2"); break;
448
case TB_CPLUSPLUS: st->print("C++"); break;
449
case TB_RPG: st->print("RPG"); break;
450
case TB_PL8: st->print("PL8"); break;
451
case TB_ASM: st->print("ASM"); break;
452
case TB_HPJ: st->print("HPJ"); break;
453
default: st->print("unknown");
457
if (p_tb->tb.globallink) {
458
st->print("globallink ");
460
if (p_tb->tb.is_eprol) {
463
if (p_tb->tb.int_proc) {
464
st->print("int_proc ");
466
if (p_tb->tb.tocless) {
467
st->print("tocless ");
469
if (p_tb->tb.fp_present) {
470
st->print("fp_present ");
472
if (p_tb->tb.int_hndl) {
473
st->print("interrupt_handler ");
475
if (p_tb->tb.uses_alloca) {
476
st->print("uses_alloca ");
478
if (p_tb->tb.saves_cr) {
479
st->print("saves_cr ");
481
if (p_tb->tb.saves_lr) {
482
st->print("saves_lr ");
484
if (p_tb->tb.stores_bc) {
485
st->print("stores_bc ");
487
if (p_tb->tb.fixup) {
490
if (p_tb->tb.fpr_saved > 0) {
491
st->print("fpr_saved:%d ", p_tb->tb.fpr_saved);
493
if (p_tb->tb.gpr_saved > 0) {
494
st->print("gpr_saved:%d ", p_tb->tb.gpr_saved);
496
if (p_tb->tb.fixedparms > 0) {
497
st->print("fixedparms:%d ", p_tb->tb.fixedparms);
499
if (p_tb->tb.floatparms > 0) {
500
st->print("floatparms:%d ", p_tb->tb.floatparms);
502
if (p_tb->tb.parmsonstk > 0) {
503
st->print("parmsonstk:%d", p_tb->tb.parmsonstk);
507
// Print information for pc (module, function, displacement, traceback table)
509
static void print_info_for_pc (outputStream* st, codeptr_t pc, char* buf,
510
size_t buf_size, bool demangle) {
511
const struct tbtable* tb = nullptr;
512
int displacement = -1;
514
if (!os::is_readable_pointer(pc)) {
515
st->print("(invalid)");
519
if (AixSymbols::get_module_name((address)pc, buf, buf_size)) {
520
st->print("%s", buf);
522
st->print("(unknown module)");
525
if (AixSymbols::get_function_name((address)pc, buf, buf_size,
526
&displacement, &tb, demangle)) {
527
st->print("%s", buf);
529
st->print("(unknown function)");
531
if (displacement == -1) {
534
st->print("+0x%x", displacement);
539
print_tbtable(st, tb);
544
static void print_stackframe(outputStream* st, stackptr_t sp, char* buf,
545
size_t buf_size, bool demangle) {
557
// retrieve lrsave. That is the only info I need to get the function/displacement
559
codeptr_t lrsave = (codeptr_t) *(sp2);
560
st->print (PTR_FORMAT " - " PTR_FORMAT " ", p2i(sp2), p2i(lrsave));
562
if (lrsave != nullptr) {
563
print_info_for_pc(st, lrsave, buf, buf_size, demangle);
568
// Function to check a given stack pointer against given stack limits.
569
static bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) {
570
if (((uintptr_t)sp) & 0x7) {
573
if (sp > stack_base) {
576
if (sp < (stackptr_t) ((address)stack_base - stack_size)) {
582
// Returns true if function is a valid codepointer.
583
static bool is_valid_codepointer(codeptr_t p) {
587
if (((uintptr_t)p) & 0x3) {
590
return LoadedLibraries::find_for_text_address(p, nullptr);
593
// Function tries to guess if the given combination of stack pointer, stack base
594
// and stack size is a valid stack frame.
595
static bool is_valid_frame (stackptr_t p, stackptr_t stack_base, size_t stack_size) {
597
if (!is_valid_stackpointer(p, stack_base, stack_size)) {
601
// First check - the occurrence of a valid backchain pointer up the stack, followed by a
602
// valid codeptr, counts as a good candidate.
603
stackptr_t sp2 = (stackptr_t) *p;
604
if (is_valid_stackpointer(sp2, stack_base, stack_size) && // found a valid stack pointer in the stack...
605
((sp2 - p) > 6) && // ... pointing upwards and not into my frame...
606
is_valid_codepointer((codeptr_t)(*(sp2 + 2)))) // ... followed by a code pointer after two slots...
614
// Try to relocate a stack back chain in a given stack.
615
// Used in callstack dumping, when the backchain is broken by an overwriter
616
static stackptr_t try_find_backchain (stackptr_t last_known_good_frame,
617
stackptr_t stack_base, size_t stack_size)
619
if (!is_valid_stackpointer(last_known_good_frame, stack_base, stack_size)) {
623
stackptr_t sp = last_known_good_frame;
625
sp += 6; // Omit next fixed frame slots.
626
while (sp < stack_base) {
627
if (is_valid_frame(sp, stack_base, stack_size)) {
636
static void decode_instructions_at_pc(const char* header,
637
codeptr_t pc, int num_before,
638
int num_after, outputStream* st) {
639
// TODO: PPC port Disassembler::decode(pc, 16, 16, st);
643
void AixNativeCallstack::print_callstack_for_context(outputStream* st, const ucontext_t* context,
644
bool demangle, char* buf, size_t buf_size) {
646
#define MAX_CALLSTACK_DEPTH 50
649
unsigned long* sp_last;
652
// To print the first frame, use the current value of iar:
653
// current entry indicated by iar (the current pc)
654
codeptr_t cur_iar = 0;
655
stackptr_t cur_sp = 0;
656
codeptr_t cur_rtoc = 0;
657
codeptr_t cur_lr = 0;
659
const ucontext_t* uc = (const ucontext_t*) context;
661
// fallback: use the current context
662
ucontext_t local_context;
664
st->print_cr("No context given, using current context.");
665
if (getcontext(&local_context) == 0) {
668
st->print_cr("No context given and getcontext failed. ");
673
cur_iar = (codeptr_t)uc->uc_mcontext.jmp_context.iar;
674
cur_sp = (stackptr_t)uc->uc_mcontext.jmp_context.gpr[1];
675
cur_rtoc = (codeptr_t)uc->uc_mcontext.jmp_context.gpr[2];
676
cur_lr = (codeptr_t)uc->uc_mcontext.jmp_context.lr;
679
// n -------------- <-- stack_base, stack_to
683
// | | | stack grows downward
687
// |------------| <-- cur_sp, current stack ptr
697
// 0 -------------- <-- stack_from
700
// Retrieve current stack base, size from the current thread. If there is none,
701
// retrieve it from the OS.
702
stackptr_t stack_base = nullptr;
703
size_t stack_size = 0;
705
AixMisc::stackbounds_t stackbounds;
706
if (!AixMisc::query_stack_bounds_for_current_thread(&stackbounds)) {
707
st->print_cr("Cannot retrieve stack bounds.");
710
stack_base = (stackptr_t)stackbounds.base;
711
stack_size = stackbounds.size;
714
st->print_cr("Native frame:");
715
st->print("iar: " PTR_FORMAT " ", p2i(cur_iar));
716
print_info_for_pc(st, cur_iar, buf, buf_size, demangle);
719
if (cur_iar && os::is_readable_pointer(cur_iar)) {
720
decode_instructions_at_pc(
721
"Decoded instructions at iar:",
722
cur_iar, 32, 16, st);
725
// Print out lr too, which may be interesting if we did jump to some bogus location;
726
// in those cases the new frame is not built up yet and the caller location is only
727
// preserved via lr register.
728
st->print("lr: " PTR_FORMAT " ", p2i(cur_lr));
729
print_info_for_pc(st, cur_lr, buf, buf_size, demangle);
732
if (cur_lr && os::is_readable_pointer(cur_lr)) {
733
decode_instructions_at_pc(
734
"Decoded instructions at lr:",
738
// Check and print sp.
739
st->print("sp: " PTR_FORMAT " ", p2i(cur_sp));
740
if (!is_valid_stackpointer(cur_sp, stack_base, stack_size)) {
741
st->print("(invalid) ");
744
st->print("(base - 0x%lX) ", PTRDIFF_BYTES(stack_base, cur_sp));
748
// Check and print rtoc.
749
st->print("rtoc: " PTR_FORMAT " ", p2i(cur_rtoc));
750
if (cur_rtoc == nullptr || cur_rtoc == (codeptr_t)-1 ||
751
!os::is_readable_pointer(cur_rtoc)) {
752
st->print("(invalid)");
753
} else if (((uintptr_t)cur_rtoc) & 0x7) {
754
st->print("(unaligned)");
758
st->print_cr("|---stackaddr----| |----lrsave------|: <function name>");
763
// (if no context was given, use the current stack)
764
sp = (unsigned long*)(*(unsigned long*)cur_sp); // Stack pointer
769
while (frame < MAX_CALLSTACK_DEPTH) {
774
// The backchain pointer was null. This normally means the end of the chain. But the
775
// stack might be corrupted, and it may be worth looking for the stack chain.
776
if (is_valid_stackpointer(sp_last, stack_base, stack_size) && (stack_base - 0x10) > sp_last) {
777
// If we are not within <guess> 0x10 stackslots of the stack base, we assume that this
778
// is indeed not the end of the chain but that the stack was corrupted. So lets try to
779
// find the end of the chain.
780
st->print_cr("*** back chain pointer is null - end of stack or broken backchain ? ***");
783
st->print_cr("*** end of backchain ***");
784
goto end_walk_callstack;
786
} else if (!is_valid_stackpointer(sp, stack_base, stack_size)) {
787
st->print_cr("*** stack pointer invalid - backchain corrupted (" PTR_FORMAT ") ***", p2i(sp));
789
} else if (sp < sp_last) {
790
st->print_cr("invalid stack pointer: " PTR_FORMAT " (not monotone raising)", p2i(sp));
794
// If backchain is broken, try to recover, by manually scanning the stack for a pattern
795
// which looks like a valid stack.
797
st->print_cr("trying to recover and find backchain...");
798
sp = try_find_backchain(sp_last, stack_base, stack_size);
800
st->print_cr("found something which looks like a backchain at " PTR_FORMAT ", after 0x%lx bytes... ",
801
p2i(sp), PTRDIFF_BYTES(sp, sp_last));
803
st->print_cr("did not find a backchain, giving up.");
804
goto end_walk_callstack;
809
print_stackframe(st, sp, buf, buf_size, demangle);
813
// Next stack frame and link area.
815
sp = (unsigned long*)(*sp);
818
// Prevent endless loops in case of invalid callstacks.
819
if (frame == MAX_CALLSTACK_DEPTH) {
820
st->print_cr("...(stopping after %d frames.", MAX_CALLSTACK_DEPTH);
825
st->print_cr("-----------------------");
834
bool AixMisc::query_stack_bounds_for_current_thread(stackbounds_t* out) {
836
// Information about this api can be found (a) in the pthread.h header and
837
// (b) in http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/pthread_getthrds_np.htm
839
// The use of this API to find out the current stack is kind of undefined.
840
// But after a lot of tries and asking IBM about it, I concluded that it is safe
841
// enough for cases where I let the pthread library create its stacks. For cases
842
// where I create an own stack and pass this to pthread_create, it seems not to
843
// work (the returned stack size in that case is 0).
845
pthread_t tid = pthread_self();
846
struct __pthrdsinfo pinfo;
847
char dummy[1]; // Just needed to satisfy pthread_getthrds_np.
848
int dummy_size = sizeof(dummy);
850
memset(&pinfo, 0, sizeof(pinfo));
852
const int rc = pthread_getthrds_np(&tid, PTHRDSINFO_QUERY_ALL, &pinfo,
853
sizeof(pinfo), dummy, &dummy_size);
856
fprintf(stderr, "pthread_getthrds_np failed (%d)\n", rc);
861
// The following may happen when invoking pthread_getthrds_np on a pthread
862
// running on a user provided stack (when handing down a stack to pthread
863
// create, see pthread_attr_setstackaddr).
864
// Not sure what to do then.
865
if (pinfo.__pi_stackend == nullptr || pinfo.__pi_stackaddr == nullptr) {
866
fprintf(stderr, "pthread_getthrds_np - invalid values\n");
871
// Note: we get three values from pthread_getthrds_np:
872
// __pi_stackaddr, __pi_stacksize, __pi_stackend
874
// high addr --------------------- base, high
876
// | pthread internal data, like ~2K
878
// | --------------------- __pi_stackend (usually not page aligned, (xxxxF890))
885
// | --------------------- (__pi_stackend - __pi_stacksize)
887
// | padding to align the following AIX guard pages, if enabled.
889
// V --------------------- __pi_stackaddr low, base - size
891
// low addr AIX guard pages, if enabled (AIXTHREAD_GUARDPAGES > 0)
894
out->base = (address)pinfo.__pi_stackend;
895
address low = (address)pinfo.__pi_stackaddr;
896
out->size = out->base - low;
901
// variables needed to emulate linux behavior in os::dll_load() if library is loaded twice
902
static pthread_mutex_t g_handletable_mutex = PTHREAD_MUTEX_INITIALIZER;
905
TableLocker() { pthread_mutex_lock(&g_handletable_mutex); }
906
~TableLocker() { pthread_mutex_unlock(&g_handletable_mutex); }
908
struct handletableentry{
915
constexpr unsigned init_num_handles = 128;
916
static unsigned max_handletable = 0;
917
static unsigned g_handletable_used = 0;
918
// We start with an empty array. At first use we will dynamically allocate memory for 128 entries.
919
// If this table is full we dynamically reallocate a memory reagion of double size, and so on.
920
static struct handletableentry* p_handletable = nullptr;
922
// get the library search path burned in to the executable file during linking
923
// If the libpath cannot be retrieved return an empty path
924
static const char* rtv_linkedin_libpath() {
925
constexpr int bufsize = 4096;
926
static char buffer[bufsize];
927
static const char* libpath = 0;
929
// we only try to retrieve the libpath once. After that try we
930
// let libpath point to buffer, which then contains a valid libpath
931
// or an empty string
932
if (libpath != nullptr) {
936
// retrieve the path to the currently running executable binary
938
snprintf(buffer, 100, "/proc/%ld/object/a.out", (long)getpid());
940
struct xcoffhdr the_xcoff;
941
struct scnhdr the_scn;
942
struct ldhdr the_ldr;
943
constexpr size_t xcoffsz = FILHSZ + _AOUTHSZ_EXEC;
944
STATIC_ASSERT(sizeof(the_xcoff) == xcoffsz);
945
STATIC_ASSERT(sizeof(the_scn) == SCNHSZ);
946
STATIC_ASSERT(sizeof(the_ldr) == LDHDRSZ);
947
// read the generic XCOFF header and analyze the substructures
948
// to find the burned in libpath. In any case of error perform the assert
949
if (nullptr == (f = fopen(buffer, "r")) ||
950
xcoffsz != fread(&the_xcoff, 1, xcoffsz, f) ||
951
the_xcoff.filehdr.f_magic != U64_TOCMAGIC ||
952
0 != fseek(f, (FILHSZ + the_xcoff.filehdr.f_opthdr + (the_xcoff.aouthdr.o_snloader -1)*SCNHSZ), SEEK_SET) ||
953
SCNHSZ != fread(&the_scn, 1, SCNHSZ, f) ||
954
0 != strcmp(the_scn.s_name, ".loader") ||
955
0 != fseek(f, the_scn.s_scnptr, SEEK_SET) ||
956
LDHDRSZ != fread(&the_ldr, 1, LDHDRSZ, f) ||
957
0 != fseek(f, the_scn.s_scnptr + the_ldr.l_impoff, SEEK_SET) ||
958
0 == fread(buffer, 1, bufsize, f)) {
960
assert(false, "could not retrieve burned in library path from executables loader section");
971
// Simulate the library search algorithm of dlopen() (in os::dll_load)
972
static bool search_file_in_LIBPATH(const char* path, struct stat64x* stat) {
976
char* path2 = os::strdup(path);
977
// if exist, strip off trailing (shr_64.o) or similar
979
if (path2[strlen(path2) - 1] == ')' && (substr = strrchr(path2, '('))) {
984
// If FilePath contains a slash character, FilePath is used directly,
985
// and no directories are searched.
986
// But if FilePath does not start with / or . we have to prepend it with ./
987
if (strchr(path2, '/')) {
988
stringStream combined;
989
if (*path2 == '/' || *path2 == '.') {
990
combined.print("%s", path2);
992
combined.print("./%s", path2);
994
ret = (0 == stat64x(combined.base(), stat));
999
const char* env = getenv("LIBPATH");
1000
if (env == nullptr) {
1001
// no LIBPATH, try with LD_LIBRARY_PATH
1002
env = getenv("LD_LIBRARY_PATH");
1005
stringStream Libpath;
1006
if (env == nullptr) {
1007
// no LIBPATH or LD_LIBRARY_PATH given -> try only with burned in libpath
1008
Libpath.print("%s", rtv_linkedin_libpath());
1009
} else if (*env == 0) {
1010
// LIBPATH or LD_LIBRARY_PATH given but empty -> try first with burned
1011
// in libpath and with current working directory second
1012
Libpath.print("%s:.", rtv_linkedin_libpath());
1014
// LIBPATH or LD_LIBRARY_PATH given with content -> try first with
1015
// LIBPATH or LD_LIBRARY_PATH and second with burned in libpath.
1016
// No check against current working directory
1017
Libpath.print("%s:%s", env, rtv_linkedin_libpath());
1020
char* libpath = os::strdup(Libpath.base());
1022
char *saveptr, *token;
1023
for (token = strtok_r(libpath, ":", &saveptr); token != nullptr; token = strtok_r(nullptr, ":", &saveptr)) {
1024
stringStream combined;
1025
combined.print("%s/%s", token, path2);
1026
if ((ret = (0 == stat64x(combined.base(), stat))))
1035
// specific AIX versions for ::dlopen() and ::dlclose(), which handles the struct g_handletable
1036
// This way we mimic dl handle equality for a library
1037
// opened a second time, as it is implemented on other platforms.
1038
void* Aix_dlopen(const char* filename, int Flags, int *eno, const char** error_report) {
1039
assert(error_report != nullptr, "error_report is nullptr");
1041
struct stat64x libstat;
1043
if (false == search_file_in_LIBPATH(filename, &libstat)) {
1044
// file with filename does not exist
1046
result = ::dlopen(filename, Flags);
1047
assert(result == nullptr, "dll_load: Could not stat() file %s, but dlopen() worked; Have to improve stat()", filename);
1049
*error_report = "Could not load module .\nSystem error: No such file or directory";
1054
// extract member string if exist duplicate it and store pointer of it
1055
// if member does not exist store nullptr
1056
char* member = nullptr;
1058
if (filename[strlen(filename) - 1] == ')' && (substr = strrchr(filename, '('))) {
1059
member = os::strdup(substr);
1064
// check if library belonging to filename is already loaded.
1065
// If yes use stored handle from previous ::dlopen() and increase refcount
1066
for (i = 0; i < g_handletable_used; i++) {
1067
if ((p_handletable + i)->handle &&
1068
(p_handletable + i)->inode == libstat.st_ino &&
1069
(p_handletable + i)->devid == libstat.st_dev &&
1070
(((p_handletable + i)->member == nullptr && member == nullptr) ||
1071
((p_handletable + i)->member != nullptr && member != nullptr &&
1072
strcmp((p_handletable + i)->member, member) == 0))) {
1073
(p_handletable + i)->refcount++;
1074
result = (p_handletable + i)->handle;
1078
if (i == g_handletable_used) {
1079
// library not yet loaded. Check if there is space left in array
1080
// to store new ::dlopen() handle
1081
if (g_handletable_used == max_handletable) {
1082
// No place in array anymore; increase array.
1083
unsigned new_max = MAX2(max_handletable * 2, init_num_handles);
1084
struct handletableentry* new_tab = (struct handletableentry*)::realloc(p_handletable, new_max * sizeof(struct handletableentry));
1085
assert(new_tab != nullptr, "no more memory for handletable");
1086
if (new_tab == nullptr) {
1087
*error_report = "dlopen: no more memory for handletable";
1090
max_handletable = new_max;
1091
p_handletable = new_tab;
1093
// Library not yet loaded; load it, then store its handle in handle table
1095
result = ::dlopen(filename, Flags);
1096
if (result != nullptr) {
1097
g_handletable_used++;
1098
(p_handletable + i)->handle = result;
1099
(p_handletable + i)->inode = libstat.st_ino;
1100
(p_handletable + i)->devid = libstat.st_dev;
1101
(p_handletable + i)->member = member;
1102
(p_handletable + i)->refcount = 1;
1105
// error analysis when dlopen fails
1107
*error_report = ::dlerror();
1108
if (*error_report == nullptr) {
1109
*error_report = "dlerror returned no error description";
1117
bool os::pd_dll_unload(void* libhandle, char* ebuf, int ebuflen) {
1121
if (ebuf && ebuflen > 0) {
1123
ebuf[ebuflen - 1] = '\0';
1128
// try to find handle in array, which means library was loaded by os::dll_load() call
1129
for (i = 0; i < g_handletable_used; i++) {
1130
if ((p_handletable + i)->handle == libhandle) {
1131
// handle found, decrease refcount
1132
assert((p_handletable + i)->refcount > 0, "Sanity");
1133
(p_handletable + i)->refcount--;
1134
if ((p_handletable + i)->refcount > 0) {
1135
// if refcount is still >0 then we have to keep library and just return true
1138
// refcount == 0, so we have to ::dlclose() the lib
1139
// and delete the entry from the array.
1144
// If we reach this point either the libhandle was found with refcount == 0, or the libhandle
1145
// was not found in the array at all. In both cases we have to ::dlclose the lib and perform
1146
// the error handling. In the first case we then also have to delete the entry from the array
1147
// while in the second case we simply have to nag.
1148
res = (0 == ::dlclose(libhandle));
1150
// error analysis when dlclose fails
1151
const char* error_report = ::dlerror();
1152
if (error_report == nullptr) {
1153
error_report = "dlerror returned no error description";
1155
if (ebuf != nullptr && ebuflen > 0) {
1156
snprintf(ebuf, ebuflen - 1, "%s", error_report);
1158
assert(false, "os::pd_dll_unload() ::dlclose() failed");
1161
if (i < g_handletable_used) {
1163
// First case: libhandle was found (with refcount == 0) and ::dlclose successful,
1164
// so delete entry from array (do not forget to free member-string space if member exists)
1165
if ((p_handletable + i)->member) {
1166
os::free((p_handletable + i)->member);
1167
(p_handletable + i)->member = nullptr;
1169
g_handletable_used--;
1170
// If the entry was the last one of the array, the previous g_handletable_used--
1171
// is sufficient to remove the entry from the array, otherwise we move the last
1172
// entry of the array to the place of the entry we want to remove and overwrite it
1173
if (i < g_handletable_used) {
1174
*(p_handletable + i) = *(p_handletable + g_handletable_used);
1175
(p_handletable + g_handletable_used)->handle = nullptr;
1180
// Second case: libhandle was not found (library was not loaded by os::dll_load())
1182
assert(false, "os::pd_dll_unload() library was not loaded by os::dll_load()");
1186
// Update the dll cache
1187
LoadedLibraries::reload();
1190
} // end: os::pd_dll_unload()