1253883Ssjg/*
2253883Ssjg * CDDL HEADER START
3253883Ssjg *
4253883Ssjg * The contents of this file are subject to the terms of the
5253883Ssjg * Common Development and Distribution License, Version 1.0 only
6253883Ssjg * (the "License").  You may not use this file except in compliance
7253883Ssjg * with the License.
8253883Ssjg *
9253883Ssjg * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10253883Ssjg * or http://www.opensolaris.org/os/licensing.
11253883Ssjg * See the License for the specific language governing permissions
12253883Ssjg * and limitations under the License.
13253883Ssjg *
14253883Ssjg * When distributing Covered Code, include this CDDL HEADER in each
15253883Ssjg * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16250837Ssjg * If applicable, add the following below this CDDL HEADER, with the
17250837Ssjg * fields enclosed by brackets "[]" replaced with your own identifying
18250837Ssjg * information: Portions Copyright [yyyy] [name of copyright owner]
19250837Ssjg *
20250837Ssjg * CDDL HEADER END
21250837Ssjg */
22250837Ssjg/*
23250837Ssjg * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24250837Ssjg * Use is subject to license terms.
25250837Ssjg */
26250837Ssjg
27250837Ssjg#pragma ident	"%Z%%M%	%I%	%E% SMI"
28250837Ssjg
29250837Ssjg#include <sys/sysmacros.h>
30250837Ssjg#include <sys/modctl.h>
31250837Ssjg#include <sys/debug.h>
32250837Ssjg#include <sys/mman.h>
33250837Ssjg#include <sys/modctl.h>
34250837Ssjg#include <sys/kobj.h>
35250837Ssjg#include <ctf_impl.h>
36249033Ssjg
37249033Ssjgint ctf_leave_compressed = 0;
38249033Ssjg
39249033Ssjgstatic struct modlmisc modlmisc = {
40249033Ssjg	&mod_miscops, "Compact C Type Format routines"
41249033Ssjg};
42249033Ssjg
43249033Ssjgstatic struct modlinkage modlinkage = {
44249033Ssjg	MODREV_1, &modlmisc, NULL
45249033Ssjg};
46249033Ssjg
47249033Ssjgint
48249033Ssjg_init(void)
49249033Ssjg{
50249033Ssjg	return (mod_install(&modlinkage));
51249033Ssjg}
52249033Ssjg
53249033Ssjgint
54249033Ssjg_info(struct modinfo *mip)
55249033Ssjg{
56249033Ssjg	return (mod_info(&modlinkage, mip));
57249033Ssjg}
58249033Ssjg
59249033Ssjgint
60249033Ssjg_fini(void)
61249033Ssjg{
62249033Ssjg	return (mod_remove(&modlinkage));
63249033Ssjg}
64249033Ssjg
65249033Ssjg/*ARGSUSED*/
66249033Ssjgvoid *
67249033Ssjgctf_zopen(int *errp)
68249033Ssjg{
69249033Ssjg	return ((void *)1); /* zmod is always loaded because we depend on it */
70249033Ssjg}
71249033Ssjg
72249033Ssjg/*ARGSUSED*/
73249033Ssjgconst void *
74249033Ssjgctf_sect_mmap(ctf_sect_t *sp, int fd)
75249033Ssjg{
76249033Ssjg	return (MAP_FAILED); /* we don't support this in the kernel */
77249033Ssjg}
78249033Ssjg
79249033Ssjg/*ARGSUSED*/
80249033Ssjgvoid
81249033Ssjgctf_sect_munmap(const ctf_sect_t *sp)
82249033Ssjg{
83249033Ssjg	/* we don't support this in the kernel */
84249033Ssjg}
85249033Ssjg
86249033Ssjg/*ARGSUSED*/
87249033Ssjgctf_file_t *
88249033Ssjgctf_fdopen(int fd, int *errp)
89249033Ssjg{
90249033Ssjg	return (ctf_set_open_errno(errp, ENOTSUP));
91249033Ssjg}
92249033Ssjg
93249033Ssjg/*ARGSUSED*/
94249033Ssjgctf_file_t *
95249033Ssjgctf_open(const char *filename, int *errp)
96249033Ssjg{
97249033Ssjg	return (ctf_set_open_errno(errp, ENOTSUP));
98249033Ssjg}
99249033Ssjg
100249033Ssjg/*ARGSUSED*/
101249033Ssjgint
102249033Ssjgctf_write(ctf_file_t *fp, int fd)
103249033Ssjg{
104246149Ssjg	return (ctf_set_errno(fp, ENOTSUP));
105246149Ssjg}
106246149Ssjg
107246149Ssjgint
108246149Ssjgctf_version(int version)
109246149Ssjg{
110246149Ssjg	ASSERT(version > 0 && version <= CTF_VERSION);
111246149Ssjg
112246149Ssjg	if (version > 0)
113246149Ssjg		_libctf_version = MIN(CTF_VERSION, version);
114246149Ssjg
115246149Ssjg	return (_libctf_version);
116246149Ssjg}
117246149Ssjg
118246149Ssjg/*ARGSUSED*/
119246149Ssjgctf_file_t *
120246149Ssjgctf_modopen(struct module *mp, int *error)
121246149Ssjg{
122246149Ssjg	ctf_sect_t ctfsect, symsect, strsect;
123246149Ssjg	ctf_file_t *fp = NULL;
124246149Ssjg	int err;
125246149Ssjg
126246149Ssjg	if (error == NULL)
127246149Ssjg		error = &err;
128246149Ssjg
129246149Ssjg	ctfsect.cts_name = ".SUNW_ctf";
130246149Ssjg	ctfsect.cts_type = SHT_PROGBITS;
131246149Ssjg	ctfsect.cts_flags = SHF_ALLOC;
132246149Ssjg	ctfsect.cts_data = mp->ctfdata;
133246149Ssjg	ctfsect.cts_size = mp->ctfsize;
134246149Ssjg	ctfsect.cts_entsize = 1;
135246149Ssjg	ctfsect.cts_offset = 0;
136246149Ssjg
137246149Ssjg	symsect.cts_name = ".symtab";
138246149Ssjg	symsect.cts_type = SHT_SYMTAB;
139246149Ssjg	symsect.cts_flags = 0;
140246149Ssjg	symsect.cts_data = mp->symtbl;
141246149Ssjg	symsect.cts_size = mp->symhdr->sh_size;
142246149Ssjg#ifdef _LP64
143246149Ssjg	symsect.cts_entsize = sizeof (Elf64_Sym);
144246149Ssjg#else
145246149Ssjg	symsect.cts_entsize = sizeof (Elf32_Sym);
146246149Ssjg#endif
147246149Ssjg	symsect.cts_offset = 0;
148246149Ssjg
149246149Ssjg	strsect.cts_name = ".strtab";
150246149Ssjg	strsect.cts_type = SHT_STRTAB;
151246149Ssjg	strsect.cts_flags = 0;
152246149Ssjg	strsect.cts_data = mp->strings;
153246149Ssjg	strsect.cts_size = mp->strhdr->sh_size;
154246149Ssjg	strsect.cts_entsize = 1;
155246149Ssjg	strsect.cts_offset = 0;
156246149Ssjg
157246149Ssjg	ASSERT(MUTEX_HELD(&mod_lock));
158246149Ssjg
159246149Ssjg	if ((fp = ctf_bufopen(&ctfsect, &symsect, &strsect, error)) == NULL)
160246149Ssjg		return (NULL);
161246149Ssjg
162246149Ssjg	if (!ctf_leave_compressed && (caddr_t)fp->ctf_base != mp->ctfdata) {
163246149Ssjg		/*
164246149Ssjg		 * We must have just uncompressed the CTF data.  To avoid
165246149Ssjg		 * others having to pay the (substantial) cost of decompressing
166246149Ssjg		 * the data, we're going to substitute the uncompressed version
167246149Ssjg		 * for the compressed version.  Note that this implies that the
168246149Ssjg		 * first CTF consumer will induce memory impact on the system
169246149Ssjg		 * (but in the name of performance of future CTF consumers).
170246149Ssjg		 */
171246149Ssjg		kobj_set_ctf(mp, (caddr_t)fp->ctf_base, fp->ctf_size);
172246149Ssjg		fp->ctf_data.cts_data = fp->ctf_base;
173246149Ssjg		fp->ctf_data.cts_size = fp->ctf_size;
174246149Ssjg	}
175246149Ssjg
176246149Ssjg	return (fp);
177246149Ssjg}
178246149Ssjg