Skip to content

Commit 16cbd32

Browse files
committed
8250750: JDK-8247515 fix for OSX pc_to_symbol() lookup fails with some symbols
Backport-of: d12fdfa
1 parent 6f9d287 commit 16cbd32

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

src/jdk.hotspot.agent/macosx/native/libsaproc/libproc_impl.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ void Prelease(struct ps_prochandle* ph) {
208208
free(ph);
209209
}
210210

211-
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base, size_t memsz) {
212-
return add_lib_info_fd(ph, libname, -1, base, memsz);
211+
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {
212+
return add_lib_info_fd(ph, libname, -1, base);
213213
}
214214

215-
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base, size_t memsz) {
216-
lib_info* newlib;
215+
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
216+
lib_info* newlib;
217217
print_debug("add_lib_info_fd %s\n", libname);
218218

219219
if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
@@ -229,7 +229,6 @@ lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
229229
strcpy(newlib->name, libname);
230230

231231
newlib->base = base;
232-
newlib->memsz = memsz;
233232

234233
if (fd == -1) {
235234
if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {
@@ -259,11 +258,11 @@ lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
259258
}
260259
#endif // __APPLE__
261260

262-
newlib->symtab = build_symtab(newlib->fd);
261+
newlib->symtab = build_symtab(newlib->fd, &newlib->memsz);
263262
if (newlib->symtab == NULL) {
264263
print_debug("symbol table build failed for %s\n", newlib->name);
265264
} else {
266-
print_debug("built symbol table for 0x%lx %s\n", newlib, newlib->name);
265+
print_debug("built symbol table for 0x%lx memsz=0x%lx %s\n", newlib, newlib->memsz, newlib->name);
267266
}
268267

269268
// even if symbol table building fails, we add the lib_info.

src/jdk.hotspot.agent/macosx/native/libsaproc/libproc_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <mach-o/loader.h>
4848
#include <mach-o/nlist.h>
4949
#include <mach-o/fat.h>
50+
#include <mach-o/stab.h>
5051

5152
#ifndef register_t
5253
#define register_t uint64_t
@@ -225,11 +226,10 @@ typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lw
225226
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);
226227

227228
// adds a new shared object to lib list, returns NULL on failure
228-
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base, size_t memsz);
229+
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);
229230

230231
// adds a new shared object to lib list, supply open lib file descriptor as well
231-
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
232-
uintptr_t base, size_t memsz);
232+
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base);
233233

234234
sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
235235
// a test for ELF signature without using libelf

src/jdk.hotspot.agent/macosx/native/libsaproc/ps_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ static bool read_shared_lib_info(struct ps_prochandle* ph) {
965965
} else {
966966
break; // Ignore non-relative paths, which are system libs. See JDK-8249779.
967967
}
968-
add_lib_info(ph, name, iter->vaddr, iter->memsz);
968+
add_lib_info(ph, name, iter->vaddr);
969969
break;
970970
}
971971
}

src/jdk.hotspot.agent/macosx/native/libsaproc/symtab.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ void build_search_table(symtab_t *symtab) {
9393
}
9494

9595
// read symbol table from given fd.
96-
struct symtab* build_symtab(int fd) {
96+
struct symtab* build_symtab(int fd, size_t *p_max_offset) {
9797
symtab_t* symtab = NULL;
9898
int i, j;
9999
mach_header_64 header;
100100
off_t image_start;
101+
size_t max_offset = 0;
101102

102103
print_debug("build_symtab\n");
103104
if (!get_arch_off(fd, CPU_TYPE_X86_64, &image_start)) {
@@ -187,6 +188,11 @@ struct symtab* build_symtab(int fd) {
187188
if (stridx == 0 || offset == 0) {
188189
continue; // Skip this entry. It's not a reference to code or data
189190
}
191+
if (lentry.n_type == N_OSO) {
192+
// This is an object file name/path. These entries have something other than
193+
// an offset in lentry.n_value, so we need to ignore them.
194+
continue;
195+
}
190196
symtab->symbols[i].offset = offset;
191197
symtab->symbols[i].name = symtab->strs + stridx;
192198
symtab->symbols[i].size = strlen(symtab->symbols[i].name);
@@ -195,6 +201,11 @@ struct symtab* build_symtab(int fd) {
195201
continue; // Skip this entry. It points to an empty string.
196202
}
197203

204+
// Track the maximum offset we've seen. This is used to determine the address range
205+
// that the library covers.
206+
if (offset > max_offset) {
207+
max_offset = (offset + 4096) & ~0xfff; // Round up to next page boundary
208+
}
198209
print_debug("symbol read: %d %d n_type=0x%x n_sect=0x%x n_desc=0x%x n_strx=0x%lx offset=0x%lx %s\n",
199210
j, i, lentry.n_type, lentry.n_sect, lentry.n_desc, stridx, offset, symtab->symbols[i].name);
200211
i++;
@@ -212,6 +223,7 @@ struct symtab* build_symtab(int fd) {
212223

213224
// build a hashtable for fast query
214225
build_search_table(symtab);
226+
*p_max_offset = max_offset;
215227
return symtab;
216228
quit:
217229
if (symtab) destroy_symtab(symtab);

src/jdk.hotspot.agent/macosx/native/libsaproc/symtab.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,7 +32,7 @@
3232
struct symtab;
3333

3434
// build symbol table for a given ELF or MachO file escriptor
35-
struct symtab* build_symtab(int fd);
35+
struct symtab* build_symtab(int fd, size_t *p_max_offset);
3636

3737
// destroy the symbol table
3838
void destroy_symtab(struct symtab* symtab);

0 commit comments

Comments
 (0)