src/jdk.hotspot.agent/macosx/native/libsaproc/symtab.c
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 58017 9726449d2644
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
58017
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     4
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     8
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    13
 * accompanied this code).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    14
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    18
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    21
 * questions.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    22
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    23
 */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    24
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    25
#include <unistd.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    26
#include <search.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    27
#include <stdlib.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    28
#include <string.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    29
#include <db.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    30
#include <fcntl.h>
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    31
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    32
#include "libproc_impl.h"
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    33
#include "symtab.h"
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    34
#ifndef __APPLE__
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    35
#include "salibelf.h"
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    36
#endif // __APPLE__
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    37
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    38
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    39
// ----------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    40
// functions for symbol lookups
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    41
// ----------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    42
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    43
typedef struct symtab_symbol {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    44
  char *name;                // name like __ZThread_...
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    45
  uintptr_t offset;          // to loaded address
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    46
  uintptr_t size;            // size strlen
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    47
} symtab_symbol;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    48
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    49
typedef struct symtab {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    50
  char *strs;                // all symbols "__symbol1__'\0'__symbol2__...."
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    51
  size_t num_symbols;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    52
  DB* hash_table;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    53
  symtab_symbol* symbols;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    54
} symtab_t;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    55
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    56
#ifdef __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    57
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    58
void build_search_table(symtab_t *symtab) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    59
  int i;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    60
  for (i = 0; i < symtab->num_symbols; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    61
    DBT key, value;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    62
    key.data = symtab->symbols[i].name;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    63
    key.size = strlen(key.data) + 1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    64
    value.data = &(symtab->symbols[i]);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    65
    value.size = sizeof(symtab_symbol);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    66
    (*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    67
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    68
    // check result
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    69
    if (is_debug()) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    70
      DBT rkey, rvalue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    71
      char* tmp = (char *)malloc(strlen(symtab->symbols[i].name) + 1);
58017
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    72
      if (tmp == NULL) {
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    73
        print_debug("error allocating array in build_search_table\n");
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    74
      } else {
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    75
        strcpy(tmp, symtab->symbols[i].name);
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    76
        rkey.data = tmp;
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    77
        rkey.size = strlen(tmp) + 1;
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    78
        (*symtab->hash_table->get)(symtab->hash_table, &rkey, &rvalue, 0);
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    79
        // we may get a copy back so compare contents
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    80
        symtab_symbol *res = (symtab_symbol *)rvalue.data;
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    81
        if (strcmp(res->name, symtab->symbols[i].name)  ||
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    82
          res->offset != symtab->symbols[i].offset    ||
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    83
          res->size != symtab->symbols[i].size) {
58017
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    84
            print_debug("error to get hash_table value!\n");
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    85
        }
9726449d2644 8230466: check malloc/calloc results in jdk.hotspot.agent
mbaesken
parents: 47216
diff changeset
    86
        free(tmp);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    87
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    88
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    89
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    90
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    91
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    92
// read symbol table from given fd.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    93
struct symtab* build_symtab(int fd) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    94
  symtab_t* symtab = NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    95
  int i;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    96
  mach_header_64 header;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    97
  off_t image_start;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    98
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
    99
  if (!get_arch_off(fd, CPU_TYPE_X86_64, &image_start)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   100
    print_debug("failed in get fat header\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   101
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   102
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   103
  lseek(fd, image_start, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   104
  if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   105
    print_debug("reading header failed!\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   106
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   107
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   108
  // header
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   109
  if (header.magic != MH_MAGIC_64) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   110
    print_debug("not a valid .dylib file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   111
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   112
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   113
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   114
  load_command lcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   115
  symtab_command symtabcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   116
  nlist_64 lentry;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   117
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   118
  bool lcsymtab_exist = false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   119
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   120
  long filepos = ltell(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   121
  for (i = 0; i < header.ncmds; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   122
    lseek(fd, filepos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   123
    if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   124
      print_debug("read load_command failed for file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   125
      return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   126
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   127
    filepos += lcmd.cmdsize;  // next command position
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   128
    if (lcmd.cmd == LC_SYMTAB) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   129
      lseek(fd, -sizeof(load_command), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   130
      lcsymtab_exist = true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   131
      break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   132
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   133
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   134
  if (!lcsymtab_exist) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   135
    print_debug("No symtab command found!\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   136
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   137
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   138
  if (read(fd, (void *)&symtabcmd, sizeof(symtab_command)) != sizeof(symtab_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   139
    print_debug("read symtab_command failed for file");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   140
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   141
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   142
  symtab = (symtab_t *)malloc(sizeof(symtab_t));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   143
  if (symtab == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   144
    print_debug("out of memory: allocating symtab\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   145
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   146
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   147
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   148
  // create hash table, we use berkeley db to
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   149
  // manipulate the hash table.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   150
  symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   151
  if (symtab->hash_table == NULL)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   152
    goto quit;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   153
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   154
  symtab->num_symbols = symtabcmd.nsyms;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   155
  symtab->symbols = (symtab_symbol *)malloc(sizeof(symtab_symbol) * symtab->num_symbols);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   156
  symtab->strs    = (char *)malloc(sizeof(char) * symtabcmd.strsize);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   157
  if (symtab->symbols == NULL || symtab->strs == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   158
     print_debug("out of memory: allocating symtab.symbol or symtab.strs\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   159
     goto quit;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   160
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   161
  lseek(fd, image_start + symtabcmd.symoff, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   162
  for (i = 0; i < symtab->num_symbols; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   163
    if (read(fd, (void *)&lentry, sizeof(nlist_64)) != sizeof(nlist_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   164
      print_debug("read nlist_64 failed at %i\n", i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   165
      goto quit;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   166
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   167
    symtab->symbols[i].offset = lentry.n_value;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   168
    symtab->symbols[i].size  = lentry.n_un.n_strx;        // index
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   169
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   170
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   171
  // string table
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   172
  lseek(fd, image_start + symtabcmd.stroff, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   173
  int size = read(fd, (void *)(symtab->strs), symtabcmd.strsize * sizeof(char));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   174
  if (size != symtabcmd.strsize * sizeof(char)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   175
     print_debug("reading string table failed\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   176
     goto quit;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   177
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   178
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   179
  for (i = 0; i < symtab->num_symbols; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   180
    symtab->symbols[i].name = symtab->strs + symtab->symbols[i].size;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   181
    if (i > 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   182
      // fix size
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   183
      symtab->symbols[i - 1].size = symtab->symbols[i].size - symtab->symbols[i - 1].size;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   184
      print_debug("%s size = %d\n", symtab->symbols[i - 1].name, symtab->symbols[i - 1].size);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   185
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   186
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   187
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   188
    if (i == symtab->num_symbols - 1) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   189
      // last index
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   190
      symtab->symbols[i].size =
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   191
            symtabcmd.strsize - symtab->symbols[i].size;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   192
      print_debug("%s size = %d\n", symtab->symbols[i].name, symtab->symbols[i].size);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   193
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   194
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   195
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   196
  // build a hashtable for fast query
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   197
  build_search_table(symtab);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   198
  return symtab;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   199
quit:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   200
  if (symtab) destroy_symtab(symtab);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   201
  return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   202
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   203
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   204
#else // __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   205
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   206
struct elf_section {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   207
  ELF_SHDR   *c_shdr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   208
  void       *c_data;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   209
};
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   210
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   211
// read symbol table from given fd.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   212
struct symtab* build_symtab(int fd) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   213
  ELF_EHDR ehdr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   214
  struct symtab* symtab = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   215
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   216
  // Reading of elf header
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   217
  struct elf_section *scn_cache = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   218
  int cnt = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   219
  ELF_SHDR* shbuf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   220
  ELF_SHDR* cursct = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   221
  ELF_PHDR* phbuf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   222
  int symtab_found = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   223
  int dynsym_found = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   224
  uint32_t symsection = SHT_SYMTAB;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   225
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   226
  uintptr_t baseaddr = (uintptr_t)-1;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   227
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   228
  lseek(fd, (off_t)0L, SEEK_SET);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   229
  if (! read_elf_header(fd, &ehdr)) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   230
    // not an elf
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   231
    return NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   232
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   233
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   234
  // read ELF header
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   235
  if ((shbuf = read_section_header_table(fd, &ehdr)) == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   236
    goto quit;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   237
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   238
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   239
  baseaddr = find_base_address(fd, &ehdr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   240
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   241
  scn_cache = calloc(ehdr.e_shnum, sizeof(*scn_cache));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   242
  if (scn_cache == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   243
    goto quit;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   244
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   245
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   246
  for (cursct = shbuf, cnt = 0; cnt < ehdr.e_shnum; cnt++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   247
    scn_cache[cnt].c_shdr = cursct;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   248
    if (cursct->sh_type == SHT_SYMTAB ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   249
        cursct->sh_type == SHT_STRTAB ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   250
        cursct->sh_type == SHT_DYNSYM) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   251
      if ( (scn_cache[cnt].c_data = read_section_data(fd, &ehdr, cursct)) == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   252
         goto quit;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   253
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   254
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   255
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   256
    if (cursct->sh_type == SHT_SYMTAB)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   257
       symtab_found++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   258
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   259
    if (cursct->sh_type == SHT_DYNSYM)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   260
       dynsym_found++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   261
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   262
    cursct++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   263
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   264
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   265
  if (!symtab_found && dynsym_found)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   266
     symsection = SHT_DYNSYM;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   267
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   268
  for (cnt = 1; cnt < ehdr.e_shnum; cnt++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   269
    ELF_SHDR *shdr = scn_cache[cnt].c_shdr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   270
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   271
    if (shdr->sh_type == symsection) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   272
      ELF_SYM  *syms;
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   273
      int j, n;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   274
      size_t size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   275
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   276
      // FIXME: there could be multiple data buffers associated with the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   277
      // same ELF section. Here we can handle only one buffer. See man page
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   278
      // for elf_getdata on Solaris.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   279
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   280
      // guarantee(symtab == NULL, "multiple symtab");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   281
      symtab = calloc(1, sizeof(*symtab));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   282
      if (symtab == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   283
         goto quit;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   284
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   285
      // the symbol table
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   286
      syms = (ELF_SYM *)scn_cache[cnt].c_data;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   287
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   288
      // number of symbols
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   289
      n = shdr->sh_size / shdr->sh_entsize;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   290
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   291
      // create hash table, we use berkeley db to
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   292
      // manipulate the hash table.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   293
      symtab->hash_table = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   294
      // guarantee(symtab->hash_table, "unexpected failure: dbopen");
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   295
      if (symtab->hash_table == NULL)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   296
        goto bad;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   297
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   298
      // shdr->sh_link points to the section that contains the actual strings
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   299
      // for symbol names. the st_name field in ELF_SYM is just the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   300
      // string table index. we make a copy of the string table so the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   301
      // strings will not be destroyed by elf_end.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   302
      size = scn_cache[shdr->sh_link].c_shdr->sh_size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   303
      symtab->strs = malloc(size);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   304
      if (symtab->strs == NULL)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   305
        goto bad;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   306
      memcpy(symtab->strs, scn_cache[shdr->sh_link].c_data, size);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   307
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   308
      // allocate memory for storing symbol offset and size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   309
      symtab->num_symbols = n;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   310
      symtab->symbols = calloc(n , sizeof(*symtab->symbols));
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   311
      if (symtab->symbols == NULL)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   312
        goto bad;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   313
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   314
      // copy symbols info our symtab and enter them info the hash table
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   315
      for (j = 0; j < n; j++, syms++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   316
        DBT key, value;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   317
        char *sym_name = symtab->strs + syms->st_name;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   318
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   319
        // skip non-object and non-function symbols
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   320
        int st_type = ELF_ST_TYPE(syms->st_info);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   321
        if ( st_type != STT_FUNC && st_type != STT_OBJECT)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   322
           continue;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   323
        // skip empty strings and undefined symbols
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   324
        if (*sym_name == '\0' || syms->st_shndx == SHN_UNDEF) continue;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   325
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   326
        symtab->symbols[j].name   = sym_name;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   327
        symtab->symbols[j].offset = syms->st_value - baseaddr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   328
        symtab->symbols[j].size   = syms->st_size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   329
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   330
        key.data = sym_name;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   331
        key.size = strlen(sym_name) + 1;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   332
        value.data = &(symtab->symbols[j]);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   333
        value.size = sizeof(symtab_symbol);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   334
        (*symtab->hash_table->put)(symtab->hash_table, &key, &value, 0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   335
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   336
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   337
  }
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   338
  goto quit;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   339
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   340
bad:
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   341
  destroy_symtab(symtab);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   342
  symtab = NULL;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   343
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   344
quit:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   345
  if (shbuf) free(shbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   346
  if (phbuf) free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   347
  if (scn_cache) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   348
    for (cnt = 0; cnt < ehdr.e_shnum; cnt++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   349
      if (scn_cache[cnt].c_data != NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   350
        free(scn_cache[cnt].c_data);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   351
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   352
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   353
    free(scn_cache);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   354
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   355
  return symtab;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   356
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   357
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   358
#endif // __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   359
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   360
void destroy_symtab(symtab_t* symtab) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   361
  if (!symtab) return;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   362
  free(symtab->strs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   363
  free(symtab->symbols);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   364
  free(symtab);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   365
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   366
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   367
uintptr_t search_symbol(struct symtab* symtab, uintptr_t base, const char *sym_name, int *sym_size) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   368
  DBT key, value;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   369
  int ret;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   370
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   371
  // library does not have symbol table
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   372
  if (!symtab || !symtab->hash_table) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   373
     return 0;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   374
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   375
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   376
  key.data = (char*)(uintptr_t)sym_name;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   377
  key.size = strlen(sym_name) + 1;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   378
  ret = (*symtab->hash_table->get)(symtab->hash_table, &key, &value, 0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   379
  if (ret == 0) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   380
    symtab_symbol *sym = value.data;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   381
    uintptr_t rslt = (uintptr_t) ((char*)base + sym->offset);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   382
    if (sym_size) *sym_size = sym->size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   383
    return rslt;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   384
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   385
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   386
  return 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   387
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   388
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   389
const char* nearest_symbol(struct symtab* symtab, uintptr_t offset,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   390
                           uintptr_t* poffset) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   391
  int n = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   392
  if (!symtab) return NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   393
  for (; n < symtab->num_symbols; n++) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10739
diff changeset
   394
    symtab_symbol* sym = &(symtab->symbols[n]);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   395
    if (sym->name != NULL &&
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   396
      offset >= sym->offset && offset < sym->offset + sym->size) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   397
      if (poffset) *poffset = (offset - sym->offset);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   398
      return sym->name;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 10565
diff changeset
   399
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   400
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   401
  return NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   402
}