xref: /freebsd-src/sys/cddl/contrib/opensolaris/uts/common/ctf/ctf_mod.c (revision cd844e7a7df72952bb1135fea0a4c6ee372a7027)
1*cd844e7aSJohn Birrell /*
2*cd844e7aSJohn Birrell  * CDDL HEADER START
3*cd844e7aSJohn Birrell  *
4*cd844e7aSJohn Birrell  * The contents of this file are subject to the terms of the
5*cd844e7aSJohn Birrell  * Common Development and Distribution License, Version 1.0 only
6*cd844e7aSJohn Birrell  * (the "License").  You may not use this file except in compliance
7*cd844e7aSJohn Birrell  * with the License.
8*cd844e7aSJohn Birrell  *
9*cd844e7aSJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*cd844e7aSJohn Birrell  * or http://www.opensolaris.org/os/licensing.
11*cd844e7aSJohn Birrell  * See the License for the specific language governing permissions
12*cd844e7aSJohn Birrell  * and limitations under the License.
13*cd844e7aSJohn Birrell  *
14*cd844e7aSJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
15*cd844e7aSJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*cd844e7aSJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
17*cd844e7aSJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
18*cd844e7aSJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
19*cd844e7aSJohn Birrell  *
20*cd844e7aSJohn Birrell  * CDDL HEADER END
21*cd844e7aSJohn Birrell  */
22*cd844e7aSJohn Birrell /*
23*cd844e7aSJohn Birrell  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*cd844e7aSJohn Birrell  * Use is subject to license terms.
25*cd844e7aSJohn Birrell  */
26*cd844e7aSJohn Birrell 
27*cd844e7aSJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*cd844e7aSJohn Birrell 
29*cd844e7aSJohn Birrell #include <sys/sysmacros.h>
30*cd844e7aSJohn Birrell #include <sys/modctl.h>
31*cd844e7aSJohn Birrell #include <sys/debug.h>
32*cd844e7aSJohn Birrell #include <sys/mman.h>
33*cd844e7aSJohn Birrell #include <sys/modctl.h>
34*cd844e7aSJohn Birrell #include <sys/kobj.h>
35*cd844e7aSJohn Birrell #include <ctf_impl.h>
36*cd844e7aSJohn Birrell 
37*cd844e7aSJohn Birrell int ctf_leave_compressed = 0;
38*cd844e7aSJohn Birrell 
39*cd844e7aSJohn Birrell static struct modlmisc modlmisc = {
40*cd844e7aSJohn Birrell 	&mod_miscops, "Compact C Type Format routines"
41*cd844e7aSJohn Birrell };
42*cd844e7aSJohn Birrell 
43*cd844e7aSJohn Birrell static struct modlinkage modlinkage = {
44*cd844e7aSJohn Birrell 	MODREV_1, &modlmisc, NULL
45*cd844e7aSJohn Birrell };
46*cd844e7aSJohn Birrell 
47*cd844e7aSJohn Birrell int
_init(void)48*cd844e7aSJohn Birrell _init(void)
49*cd844e7aSJohn Birrell {
50*cd844e7aSJohn Birrell 	return (mod_install(&modlinkage));
51*cd844e7aSJohn Birrell }
52*cd844e7aSJohn Birrell 
53*cd844e7aSJohn Birrell int
_info(struct modinfo * mip)54*cd844e7aSJohn Birrell _info(struct modinfo *mip)
55*cd844e7aSJohn Birrell {
56*cd844e7aSJohn Birrell 	return (mod_info(&modlinkage, mip));
57*cd844e7aSJohn Birrell }
58*cd844e7aSJohn Birrell 
59*cd844e7aSJohn Birrell int
_fini(void)60*cd844e7aSJohn Birrell _fini(void)
61*cd844e7aSJohn Birrell {
62*cd844e7aSJohn Birrell 	return (mod_remove(&modlinkage));
63*cd844e7aSJohn Birrell }
64*cd844e7aSJohn Birrell 
65*cd844e7aSJohn Birrell /*ARGSUSED*/
66*cd844e7aSJohn Birrell void *
ctf_zopen(int * errp)67*cd844e7aSJohn Birrell ctf_zopen(int *errp)
68*cd844e7aSJohn Birrell {
69*cd844e7aSJohn Birrell 	return ((void *)1); /* zmod is always loaded because we depend on it */
70*cd844e7aSJohn Birrell }
71*cd844e7aSJohn Birrell 
72*cd844e7aSJohn Birrell /*ARGSUSED*/
73*cd844e7aSJohn Birrell const void *
ctf_sect_mmap(ctf_sect_t * sp,int fd)74*cd844e7aSJohn Birrell ctf_sect_mmap(ctf_sect_t *sp, int fd)
75*cd844e7aSJohn Birrell {
76*cd844e7aSJohn Birrell 	return (MAP_FAILED); /* we don't support this in the kernel */
77*cd844e7aSJohn Birrell }
78*cd844e7aSJohn Birrell 
79*cd844e7aSJohn Birrell /*ARGSUSED*/
80*cd844e7aSJohn Birrell void
ctf_sect_munmap(const ctf_sect_t * sp)81*cd844e7aSJohn Birrell ctf_sect_munmap(const ctf_sect_t *sp)
82*cd844e7aSJohn Birrell {
83*cd844e7aSJohn Birrell 	/* we don't support this in the kernel */
84*cd844e7aSJohn Birrell }
85*cd844e7aSJohn Birrell 
86*cd844e7aSJohn Birrell /*ARGSUSED*/
87*cd844e7aSJohn Birrell ctf_file_t *
ctf_fdopen(int fd,int * errp)88*cd844e7aSJohn Birrell ctf_fdopen(int fd, int *errp)
89*cd844e7aSJohn Birrell {
90*cd844e7aSJohn Birrell 	return (ctf_set_open_errno(errp, ENOTSUP));
91*cd844e7aSJohn Birrell }
92*cd844e7aSJohn Birrell 
93*cd844e7aSJohn Birrell /*ARGSUSED*/
94*cd844e7aSJohn Birrell ctf_file_t *
ctf_open(const char * filename,int * errp)95*cd844e7aSJohn Birrell ctf_open(const char *filename, int *errp)
96*cd844e7aSJohn Birrell {
97*cd844e7aSJohn Birrell 	return (ctf_set_open_errno(errp, ENOTSUP));
98*cd844e7aSJohn Birrell }
99*cd844e7aSJohn Birrell 
100*cd844e7aSJohn Birrell /*ARGSUSED*/
101*cd844e7aSJohn Birrell int
ctf_write(ctf_file_t * fp,int fd)102*cd844e7aSJohn Birrell ctf_write(ctf_file_t *fp, int fd)
103*cd844e7aSJohn Birrell {
104*cd844e7aSJohn Birrell 	return (ctf_set_errno(fp, ENOTSUP));
105*cd844e7aSJohn Birrell }
106*cd844e7aSJohn Birrell 
107*cd844e7aSJohn Birrell int
ctf_version(int version)108*cd844e7aSJohn Birrell ctf_version(int version)
109*cd844e7aSJohn Birrell {
110*cd844e7aSJohn Birrell 	ASSERT(version > 0 && version <= CTF_VERSION);
111*cd844e7aSJohn Birrell 
112*cd844e7aSJohn Birrell 	if (version > 0)
113*cd844e7aSJohn Birrell 		_libctf_version = MIN(CTF_VERSION, version);
114*cd844e7aSJohn Birrell 
115*cd844e7aSJohn Birrell 	return (_libctf_version);
116*cd844e7aSJohn Birrell }
117*cd844e7aSJohn Birrell 
118*cd844e7aSJohn Birrell /*ARGSUSED*/
119*cd844e7aSJohn Birrell ctf_file_t *
ctf_modopen(struct module * mp,int * error)120*cd844e7aSJohn Birrell ctf_modopen(struct module *mp, int *error)
121*cd844e7aSJohn Birrell {
122*cd844e7aSJohn Birrell 	ctf_sect_t ctfsect, symsect, strsect;
123*cd844e7aSJohn Birrell 	ctf_file_t *fp = NULL;
124*cd844e7aSJohn Birrell 	int err;
125*cd844e7aSJohn Birrell 
126*cd844e7aSJohn Birrell 	if (error == NULL)
127*cd844e7aSJohn Birrell 		error = &err;
128*cd844e7aSJohn Birrell 
129*cd844e7aSJohn Birrell 	ctfsect.cts_name = ".SUNW_ctf";
130*cd844e7aSJohn Birrell 	ctfsect.cts_type = SHT_PROGBITS;
131*cd844e7aSJohn Birrell 	ctfsect.cts_flags = SHF_ALLOC;
132*cd844e7aSJohn Birrell 	ctfsect.cts_data = mp->ctfdata;
133*cd844e7aSJohn Birrell 	ctfsect.cts_size = mp->ctfsize;
134*cd844e7aSJohn Birrell 	ctfsect.cts_entsize = 1;
135*cd844e7aSJohn Birrell 	ctfsect.cts_offset = 0;
136*cd844e7aSJohn Birrell 
137*cd844e7aSJohn Birrell 	symsect.cts_name = ".symtab";
138*cd844e7aSJohn Birrell 	symsect.cts_type = SHT_SYMTAB;
139*cd844e7aSJohn Birrell 	symsect.cts_flags = 0;
140*cd844e7aSJohn Birrell 	symsect.cts_data = mp->symtbl;
141*cd844e7aSJohn Birrell 	symsect.cts_size = mp->symhdr->sh_size;
142*cd844e7aSJohn Birrell #ifdef _LP64
143*cd844e7aSJohn Birrell 	symsect.cts_entsize = sizeof (Elf64_Sym);
144*cd844e7aSJohn Birrell #else
145*cd844e7aSJohn Birrell 	symsect.cts_entsize = sizeof (Elf32_Sym);
146*cd844e7aSJohn Birrell #endif
147*cd844e7aSJohn Birrell 	symsect.cts_offset = 0;
148*cd844e7aSJohn Birrell 
149*cd844e7aSJohn Birrell 	strsect.cts_name = ".strtab";
150*cd844e7aSJohn Birrell 	strsect.cts_type = SHT_STRTAB;
151*cd844e7aSJohn Birrell 	strsect.cts_flags = 0;
152*cd844e7aSJohn Birrell 	strsect.cts_data = mp->strings;
153*cd844e7aSJohn Birrell 	strsect.cts_size = mp->strhdr->sh_size;
154*cd844e7aSJohn Birrell 	strsect.cts_entsize = 1;
155*cd844e7aSJohn Birrell 	strsect.cts_offset = 0;
156*cd844e7aSJohn Birrell 
157*cd844e7aSJohn Birrell 	ASSERT(MUTEX_HELD(&mod_lock));
158*cd844e7aSJohn Birrell 
159*cd844e7aSJohn Birrell 	if ((fp = ctf_bufopen(&ctfsect, &symsect, &strsect, error)) == NULL)
160*cd844e7aSJohn Birrell 		return (NULL);
161*cd844e7aSJohn Birrell 
162*cd844e7aSJohn Birrell 	if (!ctf_leave_compressed && (caddr_t)fp->ctf_base != mp->ctfdata) {
163*cd844e7aSJohn Birrell 		/*
164*cd844e7aSJohn Birrell 		 * We must have just uncompressed the CTF data.  To avoid
165*cd844e7aSJohn Birrell 		 * others having to pay the (substantial) cost of decompressing
166*cd844e7aSJohn Birrell 		 * the data, we're going to substitute the uncompressed version
167*cd844e7aSJohn Birrell 		 * for the compressed version.  Note that this implies that the
168*cd844e7aSJohn Birrell 		 * first CTF consumer will induce memory impact on the system
169*cd844e7aSJohn Birrell 		 * (but in the name of performance of future CTF consumers).
170*cd844e7aSJohn Birrell 		 */
171*cd844e7aSJohn Birrell 		kobj_set_ctf(mp, (caddr_t)fp->ctf_base, fp->ctf_size);
172*cd844e7aSJohn Birrell 		fp->ctf_data.cts_data = fp->ctf_base;
173*cd844e7aSJohn Birrell 		fp->ctf_data.cts_size = fp->ctf_size;
174*cd844e7aSJohn Birrell 	}
175*cd844e7aSJohn Birrell 
176*cd844e7aSJohn Birrell 	return (fp);
177*cd844e7aSJohn Birrell }
178