Path: blob/master/src/jdk.hotspot.agent/macosx/native/libsaproc/symtab.c
41152 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include <unistd.h>25#include <search.h>26#include <stddef.h>27#include <stdlib.h>28#include <string.h>29#include <db.h>30#include <fcntl.h>3132#include "libproc_impl.h"33#include "symtab.h"34#ifndef __APPLE__35#include "salibelf.h"36#endif // __APPLE__373839// ----------------------------------------------------40// functions for symbol lookups41// ----------------------------------------------------4243typedef struct symtab_symbol {44char *name; // name like __ZThread_...45uintptr_t offset; // to loaded address46uintptr_t size; // size strlen47} symtab_symbol;4849typedef struct symtab {50char *strs; // all symbols "__symbol1__'\0'__symbol2__...."51size_t num_symbols;52DB* hash_table;53symtab_symbol* symbols;54} symtab_t;5556#ifdef __APPLE__5758void build_search_table(symtab_t *symtab) {59int i;60print_debug("build_search_table\n");61for (i = 0; i < symtab->num_symbols; i++) {62DBT key, value;63key.data = symtab->symbols[i].name;64key.size = strlen(key.data) + 1;65value.data = &(symtab->symbols[i]);66value.size = sizeof(symtab_symbol);67//print_debug("build_search_table: %d 0x%x %s\n", i, symtab->symbols[i].offset, symtab->symbols[i].name);68(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);6970// check result71if (is_debug()) {72DBT rkey, rvalue;73char* tmp = (char *)malloc(strlen(symtab->symbols[i].name) + 1);74if (tmp == NULL) {75print_debug("error allocating array in build_search_table\n");76} else {77strcpy(tmp, symtab->symbols[i].name);78rkey.data = tmp;79rkey.size = strlen(tmp) + 1;80(*symtab->hash_table->get)(symtab->hash_table, &rkey, &rvalue, 0);81// we may get a copy back so compare contents82symtab_symbol *res = (symtab_symbol *)rvalue.data;83if (strcmp(res->name, symtab->symbols[i].name) ||84res->offset != symtab->symbols[i].offset ||85res->size != symtab->symbols[i].size) {86print_debug("error to get hash_table value!\n");87}88free(tmp);89}90}91}92}9394// read symbol table from given fd.95struct symtab* build_symtab(int fd, size_t *p_max_offset) {96symtab_t* symtab = NULL;97int i, j;98mach_header_64 header;99off_t image_start;100size_t max_offset = 0;101102print_debug("build_symtab\n");103if (!get_arch_off(fd, CPU_TYPE_X86_64, &image_start)) {104print_debug("failed in get fat header\n");105return NULL;106}107lseek(fd, image_start, SEEK_SET);108if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {109print_debug("reading header failed!\n");110return NULL;111}112// header113if (header.magic != MH_MAGIC_64) {114print_debug("not a valid .dylib file\n");115return NULL;116}117118load_command lcmd;119symtab_command symtabcmd;120nlist_64 lentry;121122bool lcsymtab_exist = false;123124long filepos = ltell(fd);125for (i = 0; i < header.ncmds; i++) {126lseek(fd, filepos, SEEK_SET);127if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {128print_debug("read load_command failed for file\n");129return NULL;130}131filepos += lcmd.cmdsize; // next command position132if (lcmd.cmd == LC_SYMTAB) {133lseek(fd, -sizeof(load_command), SEEK_CUR);134lcsymtab_exist = true;135break;136}137}138if (!lcsymtab_exist) {139print_debug("No symtab command found!\n");140return NULL;141}142if (read(fd, (void *)&symtabcmd, sizeof(symtab_command)) != sizeof(symtab_command)) {143print_debug("read symtab_command failed for file");144return NULL;145}146symtab = (symtab_t *)malloc(sizeof(symtab_t));147if (symtab == NULL) {148print_debug("out of memory: allocating symtab\n");149return NULL;150}151152// create hash table, we use berkeley db to153// manipulate the hash table.154symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL);155if (symtab->hash_table == NULL)156goto quit;157158// allocate the symtab159symtab->num_symbols = symtabcmd.nsyms;160symtab->symbols = (symtab_symbol *)malloc(sizeof(symtab_symbol) * symtab->num_symbols);161symtab->strs = (char *)malloc(sizeof(char) * symtabcmd.strsize);162if (symtab->symbols == NULL || symtab->strs == NULL) {163print_debug("out of memory: allocating symtab.symbol or symtab.strs\n");164goto quit;165}166167// read in the string table168lseek(fd, image_start + symtabcmd.stroff, SEEK_SET);169int size = read(fd, (void *)(symtab->strs), symtabcmd.strsize * sizeof(char));170if (size != symtabcmd.strsize * sizeof(char)) {171print_debug("reading string table failed\n");172goto quit;173}174175// read in each nlist_64 from the symbol table and use to fill in symtab->symbols176lseek(fd, image_start + symtabcmd.symoff, SEEK_SET);177i = 0;178for (j = 0; j < symtab->num_symbols; j++) {179if (read(fd, (void *)&lentry, sizeof(nlist_64)) != sizeof(nlist_64)) {180print_debug("read nlist_64 failed at %j\n", j);181goto quit;182}183184uintptr_t offset = lentry.n_value; // offset of the symbol code/data in the file185uintptr_t stridx = lentry.n_un.n_strx; // offset of symbol string in the symtabcmd.symoff section186187if (stridx == 0 || offset == 0) {188continue; // Skip this entry. It's not a reference to code or data189}190if (lentry.n_type == N_OSO) {191// This is an object file name/path. These entries have something other than192// an offset in lentry.n_value, so we need to ignore them.193continue;194}195symtab->symbols[i].offset = offset;196symtab->symbols[i].name = symtab->strs + stridx;197symtab->symbols[i].size = strlen(symtab->symbols[i].name);198199if (symtab->symbols[i].size == 0) {200continue; // Skip this entry. It points to an empty string.201}202203// Track the maximum offset we've seen. This is used to determine the address range204// that the library covers.205if (offset > max_offset) {206max_offset = (offset + 4096) & ~0xfff; // Round up to next page boundary207}208print_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",209j, i, lentry.n_type, lentry.n_sect, lentry.n_desc, stridx, offset, symtab->symbols[i].name);210i++;211}212213// Update symtab->num_symbols to be the actual number of symbols we added. Since the symbols214// array was allocated larger, reallocate it to the proper size.215print_debug("build_symtab: included %d of %d entries.\n", i, symtab->num_symbols);216symtab->num_symbols = i;217symtab->symbols = (symtab_symbol *)realloc(symtab->symbols, sizeof(symtab_symbol) * symtab->num_symbols);218if (symtab->symbols == NULL) {219print_debug("out of memory: reallocating symtab.symbol\n");220goto quit;221}222223// build a hashtable for fast query224build_search_table(symtab);225*p_max_offset = max_offset;226return symtab;227quit:228if (symtab) destroy_symtab(symtab);229return NULL;230}231232#else // __APPLE__233234struct elf_section {235ELF_SHDR *c_shdr;236void *c_data;237};238239// read symbol table from given fd.240struct symtab* build_symtab(int fd) {241ELF_EHDR ehdr;242struct symtab* symtab = NULL;243244// Reading of elf header245struct elf_section *scn_cache = NULL;246int cnt = 0;247ELF_SHDR* shbuf = NULL;248ELF_SHDR* cursct = NULL;249ELF_PHDR* phbuf = NULL;250int symtab_found = 0;251int dynsym_found = 0;252uint32_t symsection = SHT_SYMTAB;253254uintptr_t baseaddr = (uintptr_t)-1;255256lseek(fd, (off_t)0L, SEEK_SET);257if (! read_elf_header(fd, &ehdr)) {258// not an elf259return NULL;260}261262// read ELF header263if ((shbuf = read_section_header_table(fd, &ehdr)) == NULL) {264goto quit;265}266267baseaddr = find_base_address(fd, &ehdr);268269scn_cache = calloc(ehdr.e_shnum, sizeof(*scn_cache));270if (scn_cache == NULL) {271goto quit;272}273274for (cursct = shbuf, cnt = 0; cnt < ehdr.e_shnum; cnt++) {275scn_cache[cnt].c_shdr = cursct;276if (cursct->sh_type == SHT_SYMTAB ||277cursct->sh_type == SHT_STRTAB ||278cursct->sh_type == SHT_DYNSYM) {279if ( (scn_cache[cnt].c_data = read_section_data(fd, &ehdr, cursct)) == NULL) {280goto quit;281}282}283284if (cursct->sh_type == SHT_SYMTAB)285symtab_found++;286287if (cursct->sh_type == SHT_DYNSYM)288dynsym_found++;289290cursct++;291}292293if (!symtab_found && dynsym_found)294symsection = SHT_DYNSYM;295296for (cnt = 1; cnt < ehdr.e_shnum; cnt++) {297ELF_SHDR *shdr = scn_cache[cnt].c_shdr;298299if (shdr->sh_type == symsection) {300ELF_SYM *syms;301int j, n;302size_t size;303304// FIXME: there could be multiple data buffers associated with the305// same ELF section. Here we can handle only one buffer. See man page306// for elf_getdata on Solaris.307308// guarantee(symtab == NULL, "multiple symtab");309symtab = calloc(1, sizeof(*symtab));310if (symtab == NULL) {311goto quit;312}313// the symbol table314syms = (ELF_SYM *)scn_cache[cnt].c_data;315316// number of symbols317n = shdr->sh_size / shdr->sh_entsize;318319// create hash table, we use berkeley db to320// manipulate the hash table.321symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL);322// guarantee(symtab->hash_table, "unexpected failure: dbopen");323if (symtab->hash_table == NULL)324goto bad;325326// shdr->sh_link points to the section that contains the actual strings327// for symbol names. the st_name field in ELF_SYM is just the328// string table index. we make a copy of the string table so the329// strings will not be destroyed by elf_end.330size = scn_cache[shdr->sh_link].c_shdr->sh_size;331symtab->strs = malloc(size);332if (symtab->strs == NULL)333goto bad;334memcpy(symtab->strs, scn_cache[shdr->sh_link].c_data, size);335336// allocate memory for storing symbol offset and size;337symtab->num_symbols = n;338symtab->symbols = calloc(n , sizeof(*symtab->symbols));339if (symtab->symbols == NULL)340goto bad;341342// copy symbols info our symtab and enter them info the hash table343for (j = 0; j < n; j++, syms++) {344DBT key, value;345char *sym_name = symtab->strs + syms->st_name;346347// skip non-object and non-function symbols348int st_type = ELF_ST_TYPE(syms->st_info);349if ( st_type != STT_FUNC && st_type != STT_OBJECT)350continue;351// skip empty strings and undefined symbols352if (*sym_name == '\0' || syms->st_shndx == SHN_UNDEF) continue;353354symtab->symbols[j].name = sym_name;355symtab->symbols[j].offset = syms->st_value - baseaddr;356symtab->symbols[j].size = syms->st_size;357358key.data = sym_name;359key.size = strlen(sym_name) + 1;360value.data = &(symtab->symbols[j]);361value.size = sizeof(symtab_symbol);362(*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);363}364}365}366goto quit;367368bad:369destroy_symtab(symtab);370symtab = NULL;371372quit:373if (shbuf) free(shbuf);374if (phbuf) free(phbuf);375if (scn_cache) {376for (cnt = 0; cnt < ehdr.e_shnum; cnt++) {377if (scn_cache[cnt].c_data != NULL) {378free(scn_cache[cnt].c_data);379}380}381free(scn_cache);382}383return symtab;384}385386#endif // __APPLE__387388void destroy_symtab(symtab_t* symtab) {389if (!symtab) return;390free(symtab->strs);391free(symtab->symbols);392free(symtab);393}394395uintptr_t search_symbol(struct symtab* symtab, uintptr_t base, const char *sym_name, int *sym_size) {396DBT key, value;397int ret;398399// library does not have symbol table400if (!symtab || !symtab->hash_table) {401return 0;402}403404key.data = (char*)(uintptr_t)sym_name;405key.size = strlen(sym_name) + 1;406ret = (*symtab->hash_table->get)(symtab->hash_table, &key, &value, 0);407if (ret == 0) {408symtab_symbol *sym = value.data;409uintptr_t rslt = (uintptr_t) ((char*)base + sym->offset);410if (sym_size) *sym_size = sym->size;411return rslt;412}413414return 0;415}416417const char* nearest_symbol(struct symtab* symtab, uintptr_t offset,418uintptr_t* poffset) {419int n = 0;420char* result = NULL;421ptrdiff_t lowest_offset_from_sym = -1;422if (!symtab) return NULL;423// Search the symbol table for the symbol that is closest to the specified offset, but is not under.424//425// Note we can't just use the first symbol that is >= the offset because the symbols may not be426// sorted by offset.427//428// Note this is a rather slow search that is O(n/2), and libjvm has as many as 250k symbols.429// Probably would be good to sort the array and do a binary search, or use a hash table like430// we do for name -> address lookups. However, this functionality is not used often and431// generally just involves one lookup, such as with the clhsdb "findpc" command.432for (; n < symtab->num_symbols; n++) {433symtab_symbol* sym = &(symtab->symbols[n]);434if (sym->size != 0 && offset >= sym->offset) {435ptrdiff_t offset_from_sym = offset - sym->offset;436if (offset_from_sym >= 0) { // ignore symbols that come after "offset"437if (lowest_offset_from_sym == -1 || offset_from_sym < lowest_offset_from_sym) {438lowest_offset_from_sym = offset_from_sym;439result = sym->name;440//print_debug("nearest_symbol: found %d %s 0x%x 0x%x 0x%x\n",441// n, sym->name, offset, sym->offset, lowest_offset_from_sym);442}443}444}445}446print_debug("nearest_symbol: found symbol %d file_offset=0x%lx sym_offset=0x%lx %s\n",447n, offset, lowest_offset_from_sym, result);448// Save the offset from the symbol if requested.449if (result != NULL && poffset) {450*poffset = lowest_offset_from_sym;451}452return result;453}454455456