Path: blob/master/src/jdk.hotspot.agent/linux/native/libsaproc/symtab.c
41149 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 <sys/procfs.h>26#include <search.h>27#include <stdlib.h>28#include <string.h>29#include "symtab.h"30#include "salibelf.h"313233// ----------------------------------------------------34// functions for symbol lookups35// ----------------------------------------------------3637struct elf_symbol {38char *name;39uintptr_t offset;40uintptr_t size;41};4243typedef struct symtab {44char *strs;45size_t num_symbols;46struct elf_symbol *symbols;47struct hsearch_data *hash_table;48} symtab_t;495051// Directory that contains global debuginfo files. In theory it52// should be possible to change this, but in a Java environment there53// is no obvious place to put a user interface to do it. Maybe this54// could be set with an environment variable.55static const char debug_file_directory[] = "/usr/lib/debug";5657/* The CRC used in gnu_debuglink, retrieved from58http://sourceware.org/gdb/current/onlinedocs/gdb/Separate-Debug-Files.html#Separate-Debug-Files. */59unsigned int gnu_debuglink_crc32 (unsigned int crc,60unsigned char *buf, size_t len)61{62static const unsigned int crc32_table[256] =63{640x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,650x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,660xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,670x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,680x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,690x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,700xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,710xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,720x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,730x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,740xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,750xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,760x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,770x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,780x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,790xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,800x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,810x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,820x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,830xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,840x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,850x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,860xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,870xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,880x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,890x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,900x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,910x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,920xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,930x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,940x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,950x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,960xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,970xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,980x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,990x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,1000xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,1010xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,1020x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,1030x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,1040x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,1050xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,1060x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,1070x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,1080x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,1090xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,1100x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,1110x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,1120xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,1130xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,1140x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,1150x2d02ef8d116};117unsigned char *end;118119crc = ~crc & 0xffffffff;120for (end = buf + len; buf < end; ++buf)121crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);122return ~crc & 0xffffffff;123}124125/* Open a debuginfo file and check its CRC. If it exists and the CRC126matches return its fd. */127static int128open_debug_file (const char *pathname, unsigned int crc)129{130unsigned int file_crc = 0;131unsigned char buffer[8 * 1024];132133int fd = pathmap_open(pathname);134135if (fd < 0)136return -1;137138lseek(fd, 0, SEEK_SET);139140for (;;) {141int len = read(fd, buffer, sizeof buffer);142if (len <= 0)143break;144file_crc = gnu_debuglink_crc32(file_crc, buffer, len);145}146147if (crc == file_crc)148return fd;149else {150close(fd);151return -1;152}153}154155/* Look for a ".gnu_debuglink" section. If one exists, try to open a156suitable debuginfo file. */157static int open_file_from_debug_link(const char *name,158int fd,159ELF_EHDR *ehdr,160struct elf_section *scn_cache)161{162int debug_fd;163struct elf_section *debug_link = find_section_by_name(".gnu_debuglink", fd, ehdr,164scn_cache);165if (debug_link == NULL)166return -1;167char *debug_filename = debug_link->c_data;168int offset = (strlen(debug_filename) + 4) >> 2;169static unsigned int crc;170crc = ((unsigned int*)debug_link->c_data)[offset];171char *debug_pathname = malloc(strlen(debug_filename)172+ strlen(name)173+ strlen(".debug/")174+ strlen(debug_file_directory)175+ 2);176if (debug_pathname == NULL) {177return -1;178}179strcpy(debug_pathname, name);180char *last_slash = strrchr(debug_pathname, '/');181if (last_slash == NULL) {182free(debug_pathname);183return -1;184}185186/* Look in the same directory as the object. */187strcpy(last_slash+1, debug_filename);188debug_fd = open_debug_file(debug_pathname, crc);189if (debug_fd >= 0) {190free(debug_pathname);191return debug_fd;192}193194/* Look in a subdirectory named ".debug". */195strcpy(last_slash+1, ".debug/");196strcat(last_slash, debug_filename);197198debug_fd = open_debug_file(debug_pathname, crc);199if (debug_fd >= 0) {200free(debug_pathname);201return debug_fd;202}203204/* Look in /usr/lib/debug + the full pathname. */205strcpy(debug_pathname, debug_file_directory);206strcat(debug_pathname, name);207last_slash = strrchr(debug_pathname, '/');208strcpy(last_slash+1, debug_filename);209210debug_fd = open_debug_file(debug_pathname, crc);211if (debug_fd >= 0) {212free(debug_pathname);213return debug_fd;214}215216free(debug_pathname);217return -1;218}219220static struct symtab* build_symtab_internal(int fd, const char *filename, bool try_debuginfo);221222/* Look for a ".gnu_debuglink" section. If one exists, try to open a223suitable debuginfo file and read a symbol table from it. */224static struct symtab *build_symtab_from_debug_link(const char *name,225int fd,226ELF_EHDR *ehdr,227struct elf_section *scn_cache)228{229fd = open_file_from_debug_link(name, fd, ehdr, scn_cache);230231if (fd >= 0) {232struct symtab *symtab = build_symtab_internal(fd, NULL, /* try_debuginfo */ false);233close(fd);234return symtab;235}236237return NULL;238}239240// Given a build_id, find the associated debuginfo file241static char *242build_id_to_debug_filename (size_t size, unsigned char *data)243{244char *filename, *s;245246filename = malloc(strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1247+ 2 * size + (sizeof ".debug" - 1) + 1);248if (filename == NULL) {249return NULL;250}251s = filename + sprintf (filename, "%s/.build-id/", debug_file_directory);252if (size > 0)253{254size--;255s += sprintf (s, "%02x", *data++);256}257if (size > 0)258*s++ = '/';259while (size-- > 0)260s += sprintf (s, "%02x", *data++);261strcpy (s, ".debug");262263return filename;264}265266// Read a build ID note. Try to open any associated debuginfo file267// and return its symtab268static struct symtab* build_symtab_from_build_id(Elf64_Nhdr *note)269{270int fd;271struct symtab *symtab = NULL;272273unsigned char *bytes274= (unsigned char*)(note+1) + note->n_namesz;275char *filename276= (build_id_to_debug_filename (note->n_descsz, bytes));277if (filename == NULL) {278return NULL;279}280fd = pathmap_open(filename);281if (fd >= 0) {282symtab = build_symtab_internal(fd, NULL, /* try_debuginfo */ false);283close(fd);284}285free(filename);286287return symtab;288}289290// read symbol table from given fd. If try_debuginfo) is true, also291// try to open an associated debuginfo file292static struct symtab* build_symtab_internal(int fd, const char *filename, bool try_debuginfo) {293ELF_EHDR ehdr;294char *names = NULL;295struct symtab* symtab = NULL;296297// Reading of elf header298struct elf_section *scn_cache = NULL;299#if defined(ppc64) && !defined(ABI_ELFv2)300// Only big endian ppc64 (i.e. ABI_ELFv1) has 'official procedure descriptors' in ELF files301// see: http://refspecs.linuxfoundation.org/LSB_3.1.1/LSB-Core-PPC64/LSB-Core-PPC64/specialsections.html302struct elf_section *opd_sect = NULL;303ELF_SHDR *opd = NULL;304#endif305int cnt = 0;306ELF_SHDR* shbuf = NULL;307ELF_SHDR* cursct = NULL;308ELF_PHDR* phbuf = NULL;309ELF_PHDR* phdr = NULL;310int sym_section = SHT_DYNSYM;311312uintptr_t baseaddr = (uintptr_t)-1;313314lseek(fd, (off_t)0L, SEEK_SET);315if (! read_elf_header(fd, &ehdr)) {316// not an elf317return NULL;318}319320// read ELF header321if ((shbuf = read_section_header_table(fd, &ehdr)) == NULL) {322goto quit;323}324325baseaddr = find_base_address(fd, &ehdr);326327scn_cache = (struct elf_section *)328calloc(ehdr.e_shnum * sizeof(struct elf_section), 1);329if (scn_cache == NULL) {330goto quit;331}332333for (cursct = shbuf, cnt = 0; cnt < ehdr.e_shnum; cnt++) {334scn_cache[cnt].c_shdr = cursct;335if (cursct->sh_type == SHT_SYMTAB || cursct->sh_type == SHT_STRTAB336|| cursct->sh_type == SHT_NOTE || cursct->sh_type == SHT_DYNSYM) {337if ( (scn_cache[cnt].c_data = read_section_data(fd, &ehdr, cursct)) == NULL) {338goto quit;339}340}341if (cursct->sh_type == SHT_SYMTAB) {342// Full symbol table available so use that343sym_section = cursct->sh_type;344}345cursct++;346}347348#if defined(ppc64) && !defined(ABI_ELFv2)349opd_sect = find_section_by_name(".opd", fd, &ehdr, scn_cache);350if (opd_sect != NULL && opd_sect->c_data != NULL && opd_sect->c_shdr != NULL) {351// plausibility check352opd = opd_sect->c_shdr;353}354#endif355356for (cnt = 1; cnt < ehdr.e_shnum; cnt++) {357ELF_SHDR *shdr = scn_cache[cnt].c_shdr;358359if (shdr->sh_type == sym_section) {360ELF_SYM *syms;361int rslt;362size_t size, n, j, htab_sz;363364// FIXME: there could be multiple data buffers associated with the365// same ELF section. Here we can handle only one buffer. See man page366// for elf_getdata on Solaris.367368// guarantee(symtab == NULL, "multiple symtab");369symtab = (struct symtab*)calloc(1, sizeof(struct symtab));370if (symtab == NULL) {371goto quit;372}373// the symbol table374syms = (ELF_SYM *)scn_cache[cnt].c_data;375376// number of symbols377n = shdr->sh_size / shdr->sh_entsize;378379// create hash table, we use hcreate_r, hsearch_r and hdestroy_r to380// manipulate the hash table.381382// NOTES section in the man page of hcreate_r says383// "Hash table implementations are usually more efficient when384// the table contains enough free space to minimize collisions.385// Typically, this means that nel should be at least 25% larger386// than the maximum number of elements that the caller expects387// to store in the table."388htab_sz = n*1.25;389390symtab->hash_table = (struct hsearch_data*) calloc(1, sizeof(struct hsearch_data));391if (symtab->hash_table == NULL) {392goto bad;393}394395rslt = hcreate_r(n, symtab->hash_table);396// guarantee(rslt, "unexpected failure: hcreate_r");397398// shdr->sh_link points to the section that contains the actual strings399// for symbol names. the st_name field in ELF_SYM is just the400// string table index. we make a copy of the string table so the401// strings will not be destroyed by elf_end.402size = scn_cache[shdr->sh_link].c_shdr->sh_size;403symtab->strs = (char *)malloc(size);404if (symtab->strs == NULL) {405goto bad;406}407memcpy(symtab->strs, scn_cache[shdr->sh_link].c_data, size);408409// allocate memory for storing symbol offset and size;410symtab->num_symbols = n;411symtab->symbols = (struct elf_symbol *)calloc(n , sizeof(struct elf_symbol));412if (symtab->symbols == NULL) {413goto bad;414}415416// copy symbols info our symtab and enter them info the hash table417for (j = 0; j < n; j++, syms++) {418ENTRY item, *ret;419uintptr_t sym_value;420char *sym_name = symtab->strs + syms->st_name;421422// skip non-object and non-function symbols423int st_type = ELF_ST_TYPE(syms->st_info);424if ( st_type != STT_FUNC && st_type != STT_OBJECT)425continue;426// skip empty strings and undefined symbols427if (*sym_name == '\0' || syms->st_shndx == SHN_UNDEF) continue;428429symtab->symbols[j].name = sym_name;430symtab->symbols[j].size = syms->st_size;431sym_value = syms->st_value;432433#if defined(ppc64) && !defined(ABI_ELFv2)434// see hotspot/src/share/vm/utilities/elfFuncDescTable.hpp for a detailed description435// of why we have to go this extra way via the '.opd' section on big endian ppc64436if (opd != NULL && *sym_name != '.' &&437(opd->sh_addr <= sym_value && sym_value <= opd->sh_addr + opd->sh_size)) {438sym_value = ((ELF_ADDR*)opd_sect->c_data)[(sym_value - opd->sh_addr) / sizeof(ELF_ADDR*)];439}440#endif441442symtab->symbols[j].offset = sym_value - baseaddr;443item.key = sym_name;444item.data = (void *)&(symtab->symbols[j]);445hsearch_r(item, ENTER, &ret, symtab->hash_table);446}447}448}449450#if defined(ppc64) && !defined(ABI_ELFv2)451// On Linux/PPC64 the debuginfo files contain an empty function descriptor452// section (i.e. '.opd' section) which makes the resolution of symbols453// with the above algorithm impossible (we would need the have both, the454// .opd section from the library and the symbol table from the debuginfo455// file which doesn't match with the current workflow.)456goto quit;457#endif458459// Look for a separate debuginfo file.460if (try_debuginfo) {461// We prefer a debug symtab to an object's own symtab, so look in462// the debuginfo file. We stash a copy of the old symtab in case463// there is no debuginfo.464struct symtab* prev_symtab = symtab;465symtab = NULL;466467#ifdef NT_GNU_BUILD_ID468// First we look for a Build ID469for (cursct = shbuf, cnt = 0;470symtab == NULL && cnt < ehdr.e_shnum;471cnt++) {472if (cursct->sh_type == SHT_NOTE) {473Elf64_Nhdr *note = (Elf64_Nhdr *)scn_cache[cnt].c_data;474if (note->n_type == NT_GNU_BUILD_ID) {475symtab = build_symtab_from_build_id(note);476}477}478cursct++;479}480#endif481482// Then, if that doesn't work, the debug link483if (symtab == NULL) {484symtab = build_symtab_from_debug_link(filename, fd, &ehdr,485scn_cache);486}487488// If we still haven't found a symtab, use the object's own symtab.489if (symtab != NULL) {490if (prev_symtab != NULL)491destroy_symtab(prev_symtab);492} else {493symtab = prev_symtab;494}495}496goto quit;497498bad:499destroy_symtab(symtab);500symtab = NULL;501502quit:503if (shbuf) free(shbuf);504if (phbuf) free(phbuf);505if (scn_cache) {506for (cnt = 0; cnt < ehdr.e_shnum; cnt++) {507if (scn_cache[cnt].c_data != NULL) {508free(scn_cache[cnt].c_data);509}510}511free(scn_cache);512}513return symtab;514}515516struct symtab* build_symtab(int fd, const char *filename) {517return build_symtab_internal(fd, filename, /* try_debuginfo */ true);518}519520521void destroy_symtab(struct symtab* symtab) {522if (!symtab) return;523if (symtab->strs) free(symtab->strs);524if (symtab->symbols) free(symtab->symbols);525if (symtab->hash_table) {526hdestroy_r(symtab->hash_table);527free(symtab->hash_table);528}529free(symtab);530}531532uintptr_t search_symbol(struct symtab* symtab, uintptr_t base,533const char *sym_name, int *sym_size) {534ENTRY item;535ENTRY* ret = NULL;536537// library does not have symbol table538if (!symtab || !symtab->hash_table)539return (uintptr_t)NULL;540541item.key = (char*) strdup(sym_name);542item.data = NULL;543hsearch_r(item, FIND, &ret, symtab->hash_table);544if (ret) {545struct elf_symbol * sym = (struct elf_symbol *)(ret->data);546uintptr_t rslt = (uintptr_t) ((char*)base + sym->offset);547if (sym_size) *sym_size = sym->size;548free(item.key);549return rslt;550}551552quit:553free(item.key);554return (uintptr_t) NULL;555}556557const char* nearest_symbol(struct symtab* symtab, uintptr_t offset,558uintptr_t* poffset) {559int n = 0;560if (!symtab) return NULL;561for (; n < symtab->num_symbols; n++) {562struct elf_symbol* sym = &(symtab->symbols[n]);563if (sym->name != NULL &&564offset >= sym->offset && offset < sym->offset + sym->size) {565if (poffset) *poffset = (offset - sym->offset);566return sym->name;567}568}569return NULL;570}571572573