4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <sys/types.h>
46 #include <sys/machelf.h>
51 #include <sys/ksyms.h>
52 #include <sys/param.h>
53 #include <sys/module.h>
54 #include <sys/linker.h>
56 #include <sys/cpuvar.h>
58 typedef struct syment {
64 static syment_t *symbol_table;
65 static int nsyms, maxsyms;
66 static char maxsymname[64];
70 #define elf_getshdr elf64_getshdr
72 #define elf_getshdr elf32_getshdr
77 add_symbol(char *name, uintptr_t addr, size_t size)
81 if (nsyms >= maxsyms) {
83 symbol_table = realloc(symbol_table, maxsyms * sizeof (*sep));
84 if (symbol_table == NULL) {
85 (void) fprintf(stderr, "can't allocate symbol table\n");
89 sep = &symbol_table[nsyms++];
97 remove_symbol(uintptr_t addr)
100 syment_t *sep = symbol_table;
102 for (i = 0; i < nsyms; i++, sep++)
103 if (sep->addr == addr)
109 fake_up_certain_popular_kernel_symbols(void)
115 if ((kc = kstat_open()) == NULL)
118 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
119 if (strcmp(ksp->ks_module, "cpu_info") == 0) {
120 if ((name = malloc(20)) == NULL)
123 * For consistency, keep cpu[0] and toss cpu0
124 * or any other such symbols.
126 if (ksp->ks_instance == 0)
127 remove_symbol((uintptr_t)ksp->ks_private);
128 (void) sprintf(name, "cpu[%d]", ksp->ks_instance);
129 add_symbol(name, (uintptr_t)ksp->ks_private,
130 sizeof (struct cpu));
133 (void) kstat_close(kc);
137 fake_up_certain_popular_kernel_symbols(void)
143 /* Good for up to 256 CPUs */
144 for(i=0; i < 256; i++) {
145 if ((name = malloc(20)) == NULL)
147 (void) sprintf(name, "cpu[%d]", i);
148 addr = 0x01000000 + (i << 16);
149 add_symbol(name, addr, sizeof (uintptr_t));
155 symcmp(const void *p1, const void *p2)
157 uintptr_t a1 = ((syment_t *)p1)->addr;
158 uintptr_t a2 = ((syment_t *)p2)->addr;
172 Sym *symtab, *symp, *lastsym;
184 if ((fd = open("/dev/ksyms", O_RDONLY)) == -1) {
185 if (errno == ENOENT && modfind("ksyms") == -1) {
187 fd = open("/dev/ksyms", O_RDONLY);
193 if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
198 (void) elf_version(EV_CURRENT);
200 elf = elf_begin(fd, ELF_C_READ, NULL);
203 * XXX - libelf needs to be fixed so it will work with
204 * non 'ordinary' files like /dev/ksyms. The following
205 * is a work around for now.
207 if (elf_version(EV_CURRENT) == EV_NONE) {
211 if (ioctl(fd, KIOCGSIZE, &sz) < 0) {
215 if (ioctl(fd, KIOCGADDR, &ksyms) < 0) {
219 if ((elf = elf_memory(ksyms, sz)) == NULL) {
225 for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
226 Shdr *shdr = elf_getshdr(scn);
227 if (shdr->sh_type == SHT_SYMTAB) {
228 symtab = (Sym *)elf_getdata(scn, NULL)->d_buf;
229 nsyms = shdr->sh_size / shdr->sh_entsize;
230 strindex = shdr->sh_link;
234 for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
236 strtab = (char *)elf_getdata(scn, NULL)->d_buf;
239 lastsym = symtab + nsyms;
241 for (symp = symtab; symp < lastsym; symp++)
242 if ((uint_t)ELF32_ST_TYPE(symp->st_info) <= STT_FUNC &&
244 add_symbol(symp->st_name + strtab,
245 (uintptr_t)symp->st_value, (size_t)symp->st_size);
247 fake_up_certain_popular_kernel_symbols();
248 (void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
249 add_symbol(maxsymname, ULONG_MAX, 1);
251 qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
254 * Destroy all duplicate symbols, then sort it again.
256 for (i = 0; i < nsyms - 1; i++)
257 if (symbol_table[i].addr == symbol_table[i + 1].addr)
258 symbol_table[i].addr = 0;
260 qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
262 while (symbol_table[1].addr == 0) {
266 symbol_table[0].name = "(usermode)";
267 symbol_table[0].addr = 0;
268 symbol_table[0].size = 1;
275 addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
282 while (hi - lo > 1) {
284 if (addr >= symbol_table[mid].addr) {
290 sep = &symbol_table[lo];
291 *offset = addr - sep->addr;
297 sym_to_addr(char *name)
300 syment_t *sep = symbol_table;
302 for (i = 0; i < nsyms; i++) {
303 if (strcmp(name, sep->name) == 0)
314 syment_t *sep = symbol_table;
316 for (i = 0; i < nsyms; i++) {
317 if (strcmp(name, sep->name) == 0)