xref: /netbsd-src/sys/kern/kern_ctf.c (revision 17da3d2c5a0f5d29acbaca90d62a43b62f515b37)
1 /*	$NetBSD: kern_ctf.c,v 1.8 2021/04/06 07:57:03 simonb Exp $	*/
2 /*-
3  * Copyright (c) 2008 John Birrell <jb@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/kern/kern_ctf.c,v 1.1.4.1 2009/08/03 08:13:06 kensmith Exp $
28  */
29 
30 #define ELFSIZE ARCH_ELFSIZE
31 #include <sys/proc.h>
32 #include <sys/module.h>
33 #include <sys/exec.h>
34 #include <sys/exec_elf.h>
35 #include <sys/kmem.h>
36 #include <sys/malloc.h>
37 #include <sys/kobj_impl.h>
38 #include <sys/kobj.h>
39 #include <sys/kern_ctf.h>
40 
41 #define _KSYMS_PRIVATE
42 #include <sys/ksyms.h>
43 
44 #include <net/zlib.h>
45 
46 /*
47  * Note this file is included by both link_elf.c and link_elf_obj.c.
48  *
49  * The CTF header structure definition can't be used here because it's
50  * (annoyingly) covered by the CDDL. We will just use a few bytes from
51  * it as an integer array where we 'know' what they mean.
52  */
53 #define CTF_HDR_SIZE		36
54 #define CTF_HDR_STRTAB_U32	7
55 #define CTF_HDR_STRLEN_U32	8
56 
57 static void *
z_alloc(void * nil,u_int items,u_int size)58 z_alloc(void *nil, u_int items, u_int size)
59 {
60 	void *ptr;
61 
62 	ptr = malloc(items * size, M_TEMP, M_NOWAIT);
63 	return ptr;
64 }
65 
66 static void
z_free(void * nil,void * ptr)67 z_free(void *nil, void *ptr)
68 {
69 	free(ptr, M_TEMP);
70 }
71 
72 int
mod_ctf_get(struct module * mod,mod_ctf_t ** mcp)73 mod_ctf_get(struct module *mod, mod_ctf_t **mcp)
74 {
75 	mod_ctf_t *mc;
76 	struct ksyms_symtab *st;
77 	void * ctftab = NULL;
78 	size_t sz;
79 	int error = 0;
80 	int compressed = 0;
81 
82 	void *ctfbuf = NULL;
83 	uint8_t *ctfaddr;
84 	uint16_t ctfmagic;
85 	size_t ctfsize;
86 
87 	/*
88 	 * Return the cached mc if there is one already.
89 	 */
90 
91 	extern specificdata_key_t fbt_module_key;
92 
93 	mc = module_getspecific(mod, fbt_module_key);
94 	if (mc != NULL) {
95 		*mcp = mc;
96 		return (0);
97 	}
98 
99 	/*
100 	 * Allocate and initialize a new mc.
101 	 */
102 
103 	mc = kmem_zalloc(sizeof(mod_ctf_t), KM_SLEEP);
104 	st = ksyms_get_mod(module_name(mod));
105 	if (st != NULL) {
106 		mc->nmap     = st->sd_nmap;
107 		mc->nmapsize = st->sd_nmapsize;
108 	}
109 
110 	if (mod->mod_kobj == NULL) {
111 	    	/* no kobj entry, try building from ksyms list */
112 		if (st == NULL) {
113 			error = ENOENT;
114 			goto out;
115 		}
116 
117 		ctfaddr = st->sd_ctfstart;
118 		ctfsize = st->sd_ctfsize;
119 
120 		mc->symtab = st->sd_symstart;
121 		mc->strtab = st->sd_strstart;
122 		mc->strcnt = 0;		/* XXX TBD */
123 		mc->nsym   = st->sd_symsize / sizeof(Elf_Sym);
124 	} else {
125 		if (kobj_find_section(mod->mod_kobj, ".SUNW_ctf", (void **)&ctfaddr, &ctfsize)) {
126 			error = ENOENT;
127 			goto out;
128 		}
129 
130 		mc->symtab = mod->mod_kobj->ko_symtab;
131 		mc->strtab = mod->mod_kobj->ko_strtab;
132 		mc->strcnt = 0;		/* XXX TBD */
133 		mc->nsym   = mod->mod_kobj->ko_symcnt;
134 	}
135 
136 	if (ctfaddr == NULL) {
137 	    	error = ENOENT;
138 		goto out;
139 	}
140 
141 	/* Check the CTF magic number. */
142 	memcpy(&ctfmagic, ctfaddr, sizeof ctfmagic);
143 	if (ctfmagic != CTF_MAGIC) {
144 	    	error = EINVAL;
145 		goto out;
146 	}
147 
148 	/* Check if version 2. */
149 	if (ctfaddr[2] != 2) {
150 	    	error = EINVAL;
151 		goto out;
152 	}
153 
154 	/* Check if the data is compressed. */
155 	if ((ctfaddr[3] & 0x1) != 0) {
156 		uint32_t *u32 = (uint32_t *) ctfaddr;
157 
158 		/*
159 		 * The last two fields in the CTF header are the offset
160 		 * from the end of the header to the start of the string
161 		 * data and the length of that string data. se this
162 		 * information to determine the decompressed CTF data
163 		 * buffer required.
164 		 */
165 		sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
166 		    CTF_HDR_SIZE;
167 
168 		compressed = 1;
169 	} else {
170 		/*
171 		 * The CTF data is not compressed, so the ELF section
172 		 * size is the same as the buffer size required.
173 		 */
174 		sz = ctfsize;
175 	}
176 
177 	/*
178 	 * Allocate memory to buffer the CTF data in its decompressed
179 	 * form.
180 	 */
181 	if (compressed) {
182 		if ((ctfbuf = malloc(sz, M_TEMP, M_WAITOK)) == NULL) {
183 			error = ENOMEM;
184 			goto out;
185 		}
186 		ctftab = ctfbuf;
187 		mc->ctfalloc = 1;
188 	} else {
189 		ctftab = (void *)ctfaddr;
190 	}
191 
192 	/* Check if decompression is required. */
193 	if (compressed) {
194 		z_stream zs;
195 		int ret;
196 
197 		/*
198 		 * The header isn't compressed, so copy that into the
199 		 * CTF buffer first.
200 		 */
201 		memcpy(ctftab, ctfaddr, CTF_HDR_SIZE);
202 
203 		/* Initialise the zlib structure. */
204 		memset(&zs, 0, sizeof(zs));
205 		zs.zalloc = z_alloc;
206 		zs.zfree = z_free;
207 
208 		if (inflateInit2(&zs, MAX_WBITS) != Z_OK) {
209 			error = EIO;
210 			goto out;
211 		}
212 
213 		zs.avail_in = ctfsize - CTF_HDR_SIZE;
214 		zs.next_in = ctfaddr + CTF_HDR_SIZE;
215 		zs.avail_out = sz - CTF_HDR_SIZE;
216 		zs.next_out = ((uint8_t *) ctftab) + CTF_HDR_SIZE;
217 		inflateReset(&zs);
218 		if ((ret = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
219 			printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
220 			error = EIO;
221 			goto out;
222 		}
223 	}
224 
225 	/* Got the CTF data! */
226 	mc->ctfcnt = ctfsize;
227 	mc->ctftab = ctftab;
228 	ctfbuf = NULL;
229 
230 	module_setspecific(mod, fbt_module_key, mc);
231 	*mcp = mc;
232 	mc = NULL;
233 
234 out:
235 	if (ctfbuf != NULL)
236 		free(ctfbuf, M_TEMP);
237 	if (mc != NULL)
238 		kmem_free(mc, sizeof(*mc));
239 
240 	return (error);
241 }
242