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 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
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 <sys/sysmacros.h>
30*0Sstevel@tonic-gate #include <sys/modctl.h>
31*0Sstevel@tonic-gate #include <sys/debug.h>
32*0Sstevel@tonic-gate #include <sys/mman.h>
33*0Sstevel@tonic-gate #include <sys/modctl.h>
34*0Sstevel@tonic-gate #include <sys/kobj.h>
35*0Sstevel@tonic-gate #include <ctf_impl.h>
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate int ctf_leave_compressed = 0;
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate static struct modlmisc modlmisc = {
40*0Sstevel@tonic-gate &mod_miscops, "Compact C Type Format routines"
41*0Sstevel@tonic-gate };
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
44*0Sstevel@tonic-gate MODREV_1, &modlmisc, NULL
45*0Sstevel@tonic-gate };
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate int
_init(void)48*0Sstevel@tonic-gate _init(void)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate return (mod_install(&modlinkage));
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate int
_info(struct modinfo * mip)54*0Sstevel@tonic-gate _info(struct modinfo *mip)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate return (mod_info(&modlinkage, mip));
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate int
_fini(void)60*0Sstevel@tonic-gate _fini(void)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate return (mod_remove(&modlinkage));
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate /*ARGSUSED*/
66*0Sstevel@tonic-gate void *
ctf_zopen(int * errp)67*0Sstevel@tonic-gate ctf_zopen(int *errp)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate return ((void *)1); /* zmod is always loaded because we depend on it */
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate /*ARGSUSED*/
73*0Sstevel@tonic-gate const void *
ctf_sect_mmap(ctf_sect_t * sp,int fd)74*0Sstevel@tonic-gate ctf_sect_mmap(ctf_sect_t *sp, int fd)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate return (MAP_FAILED); /* we don't support this in the kernel */
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate /*ARGSUSED*/
80*0Sstevel@tonic-gate void
ctf_sect_munmap(const ctf_sect_t * sp)81*0Sstevel@tonic-gate ctf_sect_munmap(const ctf_sect_t *sp)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate /* we don't support this in the kernel */
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate /*ARGSUSED*/
87*0Sstevel@tonic-gate ctf_file_t *
ctf_fdopen(int fd,int * errp)88*0Sstevel@tonic-gate ctf_fdopen(int fd, int *errp)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate return (ctf_set_open_errno(errp, ENOTSUP));
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate /*ARGSUSED*/
94*0Sstevel@tonic-gate ctf_file_t *
ctf_open(const char * filename,int * errp)95*0Sstevel@tonic-gate ctf_open(const char *filename, int *errp)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate return (ctf_set_open_errno(errp, ENOTSUP));
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate /*ARGSUSED*/
101*0Sstevel@tonic-gate int
ctf_write(ctf_file_t * fp,int fd)102*0Sstevel@tonic-gate ctf_write(ctf_file_t *fp, int fd)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate return (ctf_set_errno(fp, ENOTSUP));
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate int
ctf_version(int version)108*0Sstevel@tonic-gate ctf_version(int version)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate ASSERT(version > 0 && version <= CTF_VERSION);
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate if (version > 0)
113*0Sstevel@tonic-gate _libctf_version = MIN(CTF_VERSION, version);
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate return (_libctf_version);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate /*ARGSUSED*/
119*0Sstevel@tonic-gate ctf_file_t *
ctf_modopen(struct module * mp,int * error)120*0Sstevel@tonic-gate ctf_modopen(struct module *mp, int *error)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate ctf_sect_t ctfsect, symsect, strsect;
123*0Sstevel@tonic-gate ctf_file_t *fp = NULL;
124*0Sstevel@tonic-gate int err;
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate if (error == NULL)
127*0Sstevel@tonic-gate error = &err;
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate ctfsect.cts_name = ".SUNW_ctf";
130*0Sstevel@tonic-gate ctfsect.cts_type = SHT_PROGBITS;
131*0Sstevel@tonic-gate ctfsect.cts_flags = SHF_ALLOC;
132*0Sstevel@tonic-gate ctfsect.cts_data = mp->ctfdata;
133*0Sstevel@tonic-gate ctfsect.cts_size = mp->ctfsize;
134*0Sstevel@tonic-gate ctfsect.cts_entsize = 1;
135*0Sstevel@tonic-gate ctfsect.cts_offset = 0;
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate symsect.cts_name = ".symtab";
138*0Sstevel@tonic-gate symsect.cts_type = SHT_SYMTAB;
139*0Sstevel@tonic-gate symsect.cts_flags = 0;
140*0Sstevel@tonic-gate symsect.cts_data = mp->symtbl;
141*0Sstevel@tonic-gate symsect.cts_size = mp->symhdr->sh_size;
142*0Sstevel@tonic-gate #ifdef _LP64
143*0Sstevel@tonic-gate symsect.cts_entsize = sizeof (Elf64_Sym);
144*0Sstevel@tonic-gate #else
145*0Sstevel@tonic-gate symsect.cts_entsize = sizeof (Elf32_Sym);
146*0Sstevel@tonic-gate #endif
147*0Sstevel@tonic-gate symsect.cts_offset = 0;
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate strsect.cts_name = ".strtab";
150*0Sstevel@tonic-gate strsect.cts_type = SHT_STRTAB;
151*0Sstevel@tonic-gate strsect.cts_flags = 0;
152*0Sstevel@tonic-gate strsect.cts_data = mp->strings;
153*0Sstevel@tonic-gate strsect.cts_size = mp->strhdr->sh_size;
154*0Sstevel@tonic-gate strsect.cts_entsize = 1;
155*0Sstevel@tonic-gate strsect.cts_offset = 0;
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&mod_lock));
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate if ((fp = ctf_bufopen(&ctfsect, &symsect, &strsect, error)) == NULL)
160*0Sstevel@tonic-gate return (NULL);
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate if (!ctf_leave_compressed && (caddr_t)fp->ctf_base != mp->ctfdata) {
163*0Sstevel@tonic-gate /*
164*0Sstevel@tonic-gate * We must have just uncompressed the CTF data. To avoid
165*0Sstevel@tonic-gate * others having to pay the (substantial) cost of decompressing
166*0Sstevel@tonic-gate * the data, we're going to substitute the uncompressed version
167*0Sstevel@tonic-gate * for the compressed version. Note that this implies that the
168*0Sstevel@tonic-gate * first CTF consumer will induce memory impact on the system
169*0Sstevel@tonic-gate * (but in the name of performance of future CTF consumers).
170*0Sstevel@tonic-gate */
171*0Sstevel@tonic-gate kobj_set_ctf(mp, (caddr_t)fp->ctf_base, fp->ctf_size);
172*0Sstevel@tonic-gate fp->ctf_data.cts_data = fp->ctf_base;
173*0Sstevel@tonic-gate fp->ctf_data.cts_size = fp->ctf_size;
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate return (fp);
177*0Sstevel@tonic-gate }
178