17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate * All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <stdarg.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <libelf.h>
417c478bd9Sstevel@tonic-gate #include <link.h>
427c478bd9Sstevel@tonic-gate #include <elf.h>
437c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include <kstat.h>
467c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate typedef struct syment {
497c478bd9Sstevel@tonic-gate uintptr_t addr;
507c478bd9Sstevel@tonic-gate char *name;
517c478bd9Sstevel@tonic-gate size_t size;
527c478bd9Sstevel@tonic-gate } syment_t;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static syment_t *symbol_table;
557c478bd9Sstevel@tonic-gate static int nsyms, maxsyms;
567c478bd9Sstevel@tonic-gate static char maxsymname[64];
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #ifdef _ELF64
597c478bd9Sstevel@tonic-gate #define elf_getshdr elf64_getshdr
607c478bd9Sstevel@tonic-gate #else
617c478bd9Sstevel@tonic-gate #define elf_getshdr elf32_getshdr
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate static void
add_symbol(char * name,uintptr_t addr,size_t size)657c478bd9Sstevel@tonic-gate add_symbol(char *name, uintptr_t addr, size_t size)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate syment_t *sep;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate if (nsyms >= maxsyms) {
707c478bd9Sstevel@tonic-gate maxsyms += 10000;
717c478bd9Sstevel@tonic-gate symbol_table = realloc(symbol_table, maxsyms * sizeof (*sep));
727c478bd9Sstevel@tonic-gate if (symbol_table == NULL) {
737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "can't allocate symbol table\n");
747c478bd9Sstevel@tonic-gate exit(3);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate sep = &symbol_table[nsyms++];
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate sep->name = name;
807c478bd9Sstevel@tonic-gate sep->addr = addr;
817c478bd9Sstevel@tonic-gate sep->size = size;
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate static void
remove_symbol(uintptr_t addr)857c478bd9Sstevel@tonic-gate remove_symbol(uintptr_t addr)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate int i;
887c478bd9Sstevel@tonic-gate syment_t *sep = symbol_table;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate for (i = 0; i < nsyms; i++, sep++)
917c478bd9Sstevel@tonic-gate if (sep->addr == addr)
927c478bd9Sstevel@tonic-gate sep->addr = 0;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate static void
fake_up_certain_popular_kernel_symbols(void)967c478bd9Sstevel@tonic-gate fake_up_certain_popular_kernel_symbols(void)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate kstat_ctl_t *kc;
997c478bd9Sstevel@tonic-gate kstat_t *ksp;
1007c478bd9Sstevel@tonic-gate char *name;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate if ((kc = kstat_open()) == NULL)
1037c478bd9Sstevel@tonic-gate return;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
1067c478bd9Sstevel@tonic-gate if (strcmp(ksp->ks_module, "cpu_info") == 0) {
1077c478bd9Sstevel@tonic-gate if ((name = malloc(20)) == NULL)
1087c478bd9Sstevel@tonic-gate break;
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * For consistency, keep cpu[0] and toss cpu0
1117c478bd9Sstevel@tonic-gate * or any other such symbols.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate if (ksp->ks_instance == 0)
1147c478bd9Sstevel@tonic-gate remove_symbol((uintptr_t)ksp->ks_private);
1157c478bd9Sstevel@tonic-gate (void) sprintf(name, "cpu[%d]", ksp->ks_instance);
1167c478bd9Sstevel@tonic-gate add_symbol(name, (uintptr_t)ksp->ks_private,
1177c478bd9Sstevel@tonic-gate sizeof (struct cpu));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate (void) kstat_close(kc);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate static int
symcmp(const void * p1,const void * p2)1247c478bd9Sstevel@tonic-gate symcmp(const void *p1, const void *p2)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate uintptr_t a1 = ((syment_t *)p1)->addr;
1277c478bd9Sstevel@tonic-gate uintptr_t a2 = ((syment_t *)p2)->addr;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate if (a1 < a2)
1307c478bd9Sstevel@tonic-gate return (-1);
1317c478bd9Sstevel@tonic-gate if (a1 > a2)
1327c478bd9Sstevel@tonic-gate return (1);
1337c478bd9Sstevel@tonic-gate return (0);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate int
symtab_init(void)1377c478bd9Sstevel@tonic-gate symtab_init(void)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate Elf *elf;
1407c478bd9Sstevel@tonic-gate Elf_Scn *scn = NULL;
1417c478bd9Sstevel@tonic-gate Sym *symtab, *symp, *lastsym;
1427c478bd9Sstevel@tonic-gate char *strtab;
1437c478bd9Sstevel@tonic-gate uint_t cnt;
1447c478bd9Sstevel@tonic-gate int fd;
1457c478bd9Sstevel@tonic-gate int i;
1467c478bd9Sstevel@tonic-gate int strindex = -1;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
1497c478bd9Sstevel@tonic-gate return (-1);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate (void) elf_version(EV_CURRENT);
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate elf = elf_begin(fd, ELF_C_READ, NULL);
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
1567c478bd9Sstevel@tonic-gate Shdr *shdr = elf_getshdr(scn);
1577c478bd9Sstevel@tonic-gate if (shdr->sh_type == SHT_SYMTAB) {
1587c478bd9Sstevel@tonic-gate symtab = (Sym *)elf_getdata(scn, NULL)->d_buf;
1597c478bd9Sstevel@tonic-gate nsyms = shdr->sh_size / shdr->sh_entsize;
1607c478bd9Sstevel@tonic-gate strindex = shdr->sh_link;
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
1657c478bd9Sstevel@tonic-gate if (cnt == strindex)
1667c478bd9Sstevel@tonic-gate strtab = (char *)elf_getdata(scn, NULL)->d_buf;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate lastsym = symtab + nsyms;
1707c478bd9Sstevel@tonic-gate nsyms = 0;
1717c478bd9Sstevel@tonic-gate for (symp = symtab; symp < lastsym; symp++)
1727c478bd9Sstevel@tonic-gate if ((uint_t)ELF32_ST_TYPE(symp->st_info) <= STT_FUNC &&
1737c478bd9Sstevel@tonic-gate symp->st_size != 0)
1747c478bd9Sstevel@tonic-gate add_symbol(symp->st_name + strtab,
1757c478bd9Sstevel@tonic-gate (uintptr_t)symp->st_value, (size_t)symp->st_size);
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate fake_up_certain_popular_kernel_symbols();
1787c478bd9Sstevel@tonic-gate (void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
1797c478bd9Sstevel@tonic-gate add_symbol(maxsymname, ULONG_MAX, 1);
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate * Destroy all duplicate symbols, then sort it again.
1857c478bd9Sstevel@tonic-gate */
1867c478bd9Sstevel@tonic-gate for (i = 0; i < nsyms - 1; i++)
1877c478bd9Sstevel@tonic-gate if (symbol_table[i].addr == symbol_table[i + 1].addr)
1887c478bd9Sstevel@tonic-gate symbol_table[i].addr = 0;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate while (symbol_table[1].addr == 0) {
1937c478bd9Sstevel@tonic-gate symbol_table++;
1947c478bd9Sstevel@tonic-gate nsyms--;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate symbol_table[0].name = "(usermode)";
1977c478bd9Sstevel@tonic-gate symbol_table[0].addr = 0;
1987c478bd9Sstevel@tonic-gate symbol_table[0].size = 1;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate return (0);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate char *
addr_to_sym(uintptr_t addr,uintptr_t * offset,size_t * sizep)2047c478bd9Sstevel@tonic-gate addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate int lo = 0;
2077c478bd9Sstevel@tonic-gate int hi = nsyms - 1;
2087c478bd9Sstevel@tonic-gate int mid;
2097c478bd9Sstevel@tonic-gate syment_t *sep;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate while (hi - lo > 1) {
2127c478bd9Sstevel@tonic-gate mid = (lo + hi) / 2;
2137c478bd9Sstevel@tonic-gate if (addr >= symbol_table[mid].addr) {
2147c478bd9Sstevel@tonic-gate lo = mid;
2157c478bd9Sstevel@tonic-gate } else {
2167c478bd9Sstevel@tonic-gate hi = mid;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate sep = &symbol_table[lo];
2207c478bd9Sstevel@tonic-gate *offset = addr - sep->addr;
2217c478bd9Sstevel@tonic-gate *sizep = sep->size;
2227c478bd9Sstevel@tonic-gate return (sep->name);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate uintptr_t
sym_to_addr(char * name)2267c478bd9Sstevel@tonic-gate sym_to_addr(char *name)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate int i;
2297c478bd9Sstevel@tonic-gate syment_t *sep = symbol_table;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate for (i = 0; i < nsyms; i++) {
2327c478bd9Sstevel@tonic-gate if (strcmp(name, sep->name) == 0)
2337c478bd9Sstevel@tonic-gate return (sep->addr);
2347c478bd9Sstevel@tonic-gate sep++;
2357c478bd9Sstevel@tonic-gate }
236*621caadeSToomas Soome return ((uintptr_t)NULL);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate size_t
sym_size(char * name)2407c478bd9Sstevel@tonic-gate sym_size(char *name)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate int i;
2437c478bd9Sstevel@tonic-gate syment_t *sep = symbol_table;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate for (i = 0; i < nsyms; i++) {
2467c478bd9Sstevel@tonic-gate if (strcmp(name, sep->name) == 0)
2477c478bd9Sstevel@tonic-gate return (sep->size);
2487c478bd9Sstevel@tonic-gate sep++;
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate return (0);
2517c478bd9Sstevel@tonic-gate }
252