hotspot/agent/src/os/linux/symtab.c
author never
Mon, 04 May 2009 22:06:47 -0700
changeset 2744 57f0579fbe09
parent 1 489c9b5090e2
child 5045 59dd2a304f1d
permissions -rw-r--r--
6837224: libsaproc.so on linux needs version of 6799141 Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include <unistd.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include <sys/procfs.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
#include <search.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include <string.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#include "symtab.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#include "salibelf.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// ----------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// functions for symbol lookups
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// ----------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
struct elf_section {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  ELF_SHDR   *c_shdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  void       *c_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
struct elf_symbol {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  char *name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  uintptr_t offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  uintptr_t size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
typedef struct symtab {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  char *strs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  size_t num_symbols;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  struct elf_symbol *symbols;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  struct hsearch_data *hash_table;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
} symtab_t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// read symbol table from given fd.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
struct symtab* build_symtab(int fd) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  ELF_EHDR ehdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  char *names = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  struct symtab* symtab = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  // Reading of elf header
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  struct elf_section *scn_cache = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  int cnt = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  ELF_SHDR* shbuf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  ELF_SHDR* cursct = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  ELF_PHDR* phbuf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  ELF_PHDR* phdr = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  uintptr_t baseaddr = (uintptr_t)-1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  lseek(fd, (off_t)0L, SEEK_SET);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  if (! read_elf_header(fd, &ehdr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    // not an elf
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  // read ELF header
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  if ((shbuf = read_section_header_table(fd, &ehdr)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    goto quit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  baseaddr = find_base_address(fd, &ehdr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  scn_cache = (struct elf_section *)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
              calloc(ehdr.e_shnum * sizeof(struct elf_section), 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  if (scn_cache == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    goto quit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  for (cursct = shbuf, cnt = 0; cnt < ehdr.e_shnum; cnt++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    scn_cache[cnt].c_shdr = cursct;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    if (cursct->sh_type == SHT_SYMTAB || cursct->sh_type == SHT_STRTAB) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      if ( (scn_cache[cnt].c_data = read_section_data(fd, &ehdr, cursct)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
         goto quit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    cursct++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  for (cnt = 1; cnt < ehdr.e_shnum; cnt++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    ELF_SHDR *shdr = scn_cache[cnt].c_shdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    if (shdr->sh_type == SHT_SYMTAB) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      ELF_SYM  *syms;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
      int j, n, rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      size_t size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
      // FIXME: there could be multiple data buffers associated with the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
      // same ELF section. Here we can handle only one buffer. See man page
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      // for elf_getdata on Solaris.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      // guarantee(symtab == NULL, "multiple symtab");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      symtab = (struct symtab*)calloc(1, sizeof(struct symtab));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      if (symtab == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
         goto quit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
      // the symbol table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      syms = (ELF_SYM *)scn_cache[cnt].c_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      // number of symbols
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      n = shdr->sh_size / shdr->sh_entsize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      // create hash table, we use hcreate_r, hsearch_r and hdestroy_r to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      // manipulate the hash table.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      symtab->hash_table = (struct hsearch_data*) calloc(1, sizeof(struct hsearch_data));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      rslt = hcreate_r(n, symtab->hash_table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      // guarantee(rslt, "unexpected failure: hcreate_r");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      // shdr->sh_link points to the section that contains the actual strings
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      // for symbol names. the st_name field in ELF_SYM is just the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      // string table index. we make a copy of the string table so the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      // strings will not be destroyed by elf_end.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
      size = scn_cache[shdr->sh_link].c_shdr->sh_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      symtab->strs = (char *)malloc(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
      memcpy(symtab->strs, scn_cache[shdr->sh_link].c_data, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      // allocate memory for storing symbol offset and size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      symtab->num_symbols = n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      symtab->symbols = (struct elf_symbol *)calloc(n , sizeof(struct elf_symbol));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      // copy symbols info our symtab and enter them info the hash table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      for (j = 0; j < n; j++, syms++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        ENTRY item, *ret;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
        char *sym_name = symtab->strs + syms->st_name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
        // skip non-object and non-function symbols
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
        int st_type = ELF_ST_TYPE(syms->st_info);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
        if ( st_type != STT_FUNC && st_type != STT_OBJECT)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
           continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
        // skip empty strings and undefined symbols
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        if (*sym_name == '\0' || syms->st_shndx == SHN_UNDEF) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        symtab->symbols[j].name   = sym_name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
        symtab->symbols[j].offset = syms->st_value - baseaddr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
        symtab->symbols[j].size   = syms->st_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
        item.key = sym_name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
        item.data = (void *)&(symtab->symbols[j]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
        hsearch_r(item, ENTER, &ret, symtab->hash_table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
quit:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  if (shbuf) free(shbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  if (phbuf) free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  if (scn_cache) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    for (cnt = 0; cnt < ehdr.e_shnum; cnt++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      if (scn_cache[cnt].c_data != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
        free(scn_cache[cnt].c_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    free(scn_cache);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  return symtab;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
void destroy_symtab(struct symtab* symtab) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  if (!symtab) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  if (symtab->strs) free(symtab->strs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  if (symtab->symbols) free(symtab->symbols);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  if (symtab->hash_table) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
     hdestroy_r(symtab->hash_table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
     free(symtab->hash_table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  free(symtab);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
uintptr_t search_symbol(struct symtab* symtab, uintptr_t base,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
                      const char *sym_name, int *sym_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  ENTRY item;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  ENTRY* ret = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  // library does not have symbol table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  if (!symtab || !symtab->hash_table)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
     return (uintptr_t)NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  item.key = (char*) strdup(sym_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  hsearch_r(item, FIND, &ret, symtab->hash_table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  if (ret) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    struct elf_symbol * sym = (struct elf_symbol *)(ret->data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    uintptr_t rslt = (uintptr_t) ((char*)base + sym->offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    if (sym_size) *sym_size = sym->size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    free(item.key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    return rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
quit:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  free(item.key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  return (uintptr_t) NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
const char* nearest_symbol(struct symtab* symtab, uintptr_t offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
                           uintptr_t* poffset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  int n = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  if (!symtab) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  for (; n < symtab->num_symbols; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
     struct elf_symbol* sym = &(symtab->symbols[n]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
     if (sym->name != NULL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
         offset >= sym->offset && offset < sym->offset + sym->size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
        if (poffset) *poffset = (offset - sym->offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
        return sym->name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
}