annotate agent/src/os/linux/symtab.c @ 0:a61af66fc99e jdk7-b24

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