1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <fcntl.h>
31*0Sstevel@tonic-gate #include <ctype.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <signal.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <stdarg.h>
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <limits.h>
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/stat.h>
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #include <libelf.h>
43*0Sstevel@tonic-gate #include <link.h>
44*0Sstevel@tonic-gate #include <elf.h>
45*0Sstevel@tonic-gate #include <sys/machelf.h>
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate #include <kstat.h>
48*0Sstevel@tonic-gate #include <sys/cpuvar.h>
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate typedef struct syment {
51*0Sstevel@tonic-gate uintptr_t addr;
52*0Sstevel@tonic-gate char *name;
53*0Sstevel@tonic-gate size_t size;
54*0Sstevel@tonic-gate } syment_t;
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate static syment_t *symbol_table;
57*0Sstevel@tonic-gate static int nsyms, maxsyms;
58*0Sstevel@tonic-gate static char maxsymname[64];
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate #ifdef _ELF64
61*0Sstevel@tonic-gate #define elf_getshdr elf64_getshdr
62*0Sstevel@tonic-gate #else
63*0Sstevel@tonic-gate #define elf_getshdr elf32_getshdr
64*0Sstevel@tonic-gate #endif
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate static void
add_symbol(char * name,uintptr_t addr,size_t size)67*0Sstevel@tonic-gate add_symbol(char *name, uintptr_t addr, size_t size)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate syment_t *sep;
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate if (nsyms >= maxsyms) {
72*0Sstevel@tonic-gate maxsyms += 10000;
73*0Sstevel@tonic-gate symbol_table = realloc(symbol_table, maxsyms * sizeof (*sep));
74*0Sstevel@tonic-gate if (symbol_table == NULL) {
75*0Sstevel@tonic-gate (void) fprintf(stderr, "can't allocate symbol table\n");
76*0Sstevel@tonic-gate exit(3);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate sep = &symbol_table[nsyms++];
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate sep->name = name;
82*0Sstevel@tonic-gate sep->addr = addr;
83*0Sstevel@tonic-gate sep->size = size;
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate static void
remove_symbol(uintptr_t addr)87*0Sstevel@tonic-gate remove_symbol(uintptr_t addr)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate int i;
90*0Sstevel@tonic-gate syment_t *sep = symbol_table;
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate for (i = 0; i < nsyms; i++, sep++)
93*0Sstevel@tonic-gate if (sep->addr == addr)
94*0Sstevel@tonic-gate sep->addr = 0;
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate static void
fake_up_certain_popular_kernel_symbols(void)98*0Sstevel@tonic-gate fake_up_certain_popular_kernel_symbols(void)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate kstat_ctl_t *kc;
101*0Sstevel@tonic-gate kstat_t *ksp;
102*0Sstevel@tonic-gate char *name;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate if ((kc = kstat_open()) == NULL)
105*0Sstevel@tonic-gate return;
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
108*0Sstevel@tonic-gate if (strcmp(ksp->ks_module, "cpu_info") == 0) {
109*0Sstevel@tonic-gate if ((name = malloc(20)) == NULL)
110*0Sstevel@tonic-gate break;
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate * For consistency, keep cpu[0] and toss cpu0
113*0Sstevel@tonic-gate * or any other such symbols.
114*0Sstevel@tonic-gate */
115*0Sstevel@tonic-gate if (ksp->ks_instance == 0)
116*0Sstevel@tonic-gate remove_symbol((uintptr_t)ksp->ks_private);
117*0Sstevel@tonic-gate (void) sprintf(name, "cpu[%d]", ksp->ks_instance);
118*0Sstevel@tonic-gate add_symbol(name, (uintptr_t)ksp->ks_private,
119*0Sstevel@tonic-gate sizeof (struct cpu));
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate (void) kstat_close(kc);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate static int
symcmp(const void * p1,const void * p2)126*0Sstevel@tonic-gate symcmp(const void *p1, const void *p2)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate uintptr_t a1 = ((syment_t *)p1)->addr;
129*0Sstevel@tonic-gate uintptr_t a2 = ((syment_t *)p2)->addr;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate if (a1 < a2)
132*0Sstevel@tonic-gate return (-1);
133*0Sstevel@tonic-gate if (a1 > a2)
134*0Sstevel@tonic-gate return (1);
135*0Sstevel@tonic-gate return (0);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate int
symtab_init(void)139*0Sstevel@tonic-gate symtab_init(void)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate Elf *elf;
142*0Sstevel@tonic-gate Elf_Scn *scn = NULL;
143*0Sstevel@tonic-gate Sym *symtab, *symp, *lastsym;
144*0Sstevel@tonic-gate char *strtab;
145*0Sstevel@tonic-gate uint_t cnt;
146*0Sstevel@tonic-gate int fd;
147*0Sstevel@tonic-gate int i;
148*0Sstevel@tonic-gate int strindex = -1;
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
151*0Sstevel@tonic-gate return (-1);
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate (void) elf_version(EV_CURRENT);
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate elf = elf_begin(fd, ELF_C_READ, NULL);
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
158*0Sstevel@tonic-gate Shdr *shdr = elf_getshdr(scn);
159*0Sstevel@tonic-gate if (shdr->sh_type == SHT_SYMTAB) {
160*0Sstevel@tonic-gate symtab = (Sym *)elf_getdata(scn, NULL)->d_buf;
161*0Sstevel@tonic-gate nsyms = shdr->sh_size / shdr->sh_entsize;
162*0Sstevel@tonic-gate strindex = shdr->sh_link;
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
167*0Sstevel@tonic-gate if (cnt == strindex)
168*0Sstevel@tonic-gate strtab = (char *)elf_getdata(scn, NULL)->d_buf;
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate lastsym = symtab + nsyms;
172*0Sstevel@tonic-gate nsyms = 0;
173*0Sstevel@tonic-gate for (symp = symtab; symp < lastsym; symp++)
174*0Sstevel@tonic-gate if ((uint_t)ELF32_ST_TYPE(symp->st_info) <= STT_FUNC &&
175*0Sstevel@tonic-gate symp->st_size != 0)
176*0Sstevel@tonic-gate add_symbol(symp->st_name + strtab,
177*0Sstevel@tonic-gate (uintptr_t)symp->st_value, (size_t)symp->st_size);
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate fake_up_certain_popular_kernel_symbols();
180*0Sstevel@tonic-gate (void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
181*0Sstevel@tonic-gate add_symbol(maxsymname, ULONG_MAX, 1);
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * Destroy all duplicate symbols, then sort it again.
187*0Sstevel@tonic-gate */
188*0Sstevel@tonic-gate for (i = 0; i < nsyms - 1; i++)
189*0Sstevel@tonic-gate if (symbol_table[i].addr == symbol_table[i + 1].addr)
190*0Sstevel@tonic-gate symbol_table[i].addr = 0;
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate while (symbol_table[1].addr == 0) {
195*0Sstevel@tonic-gate symbol_table++;
196*0Sstevel@tonic-gate nsyms--;
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate symbol_table[0].name = "(usermode)";
199*0Sstevel@tonic-gate symbol_table[0].addr = 0;
200*0Sstevel@tonic-gate symbol_table[0].size = 1;
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate return (0);
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate char *
addr_to_sym(uintptr_t addr,uintptr_t * offset,size_t * sizep)206*0Sstevel@tonic-gate addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate int lo = 0;
209*0Sstevel@tonic-gate int hi = nsyms - 1;
210*0Sstevel@tonic-gate int mid;
211*0Sstevel@tonic-gate syment_t *sep;
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate while (hi - lo > 1) {
214*0Sstevel@tonic-gate mid = (lo + hi) / 2;
215*0Sstevel@tonic-gate if (addr >= symbol_table[mid].addr) {
216*0Sstevel@tonic-gate lo = mid;
217*0Sstevel@tonic-gate } else {
218*0Sstevel@tonic-gate hi = mid;
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate sep = &symbol_table[lo];
222*0Sstevel@tonic-gate *offset = addr - sep->addr;
223*0Sstevel@tonic-gate *sizep = sep->size;
224*0Sstevel@tonic-gate return (sep->name);
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gate uintptr_t
sym_to_addr(char * name)228*0Sstevel@tonic-gate sym_to_addr(char *name)
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate int i;
231*0Sstevel@tonic-gate syment_t *sep = symbol_table;
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate for (i = 0; i < nsyms; i++) {
234*0Sstevel@tonic-gate if (strcmp(name, sep->name) == 0)
235*0Sstevel@tonic-gate return (sep->addr);
236*0Sstevel@tonic-gate sep++;
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate return (NULL);
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate size_t
sym_size(char * name)242*0Sstevel@tonic-gate sym_size(char *name)
243*0Sstevel@tonic-gate {
244*0Sstevel@tonic-gate int i;
245*0Sstevel@tonic-gate syment_t *sep = symbol_table;
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate for (i = 0; i < nsyms; i++) {
248*0Sstevel@tonic-gate if (strcmp(name, sep->name) == 0)
249*0Sstevel@tonic-gate return (sep->size);
250*0Sstevel@tonic-gate sep++;
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate return (0);
253*0Sstevel@tonic-gate }
254