xref: /netbsd-src/sys/kern/kern_ctf.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: kern_ctf.c,v 1.5 2014/10/18 08:33:29 snj 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/module.h>
32 #include <sys/exec.h>
33 #include <sys/exec_elf.h>
34 #include <sys/kmem.h>
35 #include <sys/malloc.h>
36 #include <sys/kobj_impl.h>
37 #include <sys/kobj.h>
38 #include <sys/kern_ctf.h>
39 
40 #define _KSYMS_PRIVATE
41 #include <sys/ksyms.h>
42 
43 #include <net/zlib.h>
44 
45 /*
46  * Note this file is included by both link_elf.c and link_elf_obj.c.
47  *
48  * The CTF header structure definition can't be used here because it's
49  * (annoyingly) covered by the CDDL. We will just use a few bytes from
50  * it as an integer array where we 'know' what they mean.
51  */
52 #define CTF_HDR_SIZE		36
53 #define CTF_HDR_STRTAB_U32	7
54 #define CTF_HDR_STRLEN_U32	8
55 
56 static void *
57 z_alloc(void *nil, u_int items, u_int size)
58 {
59 	void *ptr;
60 
61 	ptr = malloc(items * size, M_TEMP, M_NOWAIT);
62 	return ptr;
63 }
64 
65 static void
66 z_free(void *nil, void *ptr)
67 {
68 	free(ptr, M_TEMP);
69 }
70 
71 int
72 mod_ctf_get(struct module *mod, mod_ctf_t *mc)
73 {
74 	mod_ctf_t *cmc;
75 	struct ksyms_symtab *st;
76 	void * ctftab = NULL;
77 	size_t sz;
78 	int error = 0;
79 	int compressed = 0;
80 
81 	void *ctfbuf = NULL;
82 	uint8_t *ctfaddr;
83 	size_t ctfsize;
84 
85 	if (mc == NULL) {
86 		return EINVAL;
87 	}
88 
89 	/* Set the defaults for no CTF present. That's not a crime! */
90 	memset(mc, 0, sizeof(*mc));
91 
92 	/* cached mc? */
93 	if (mod->mod_ctf != NULL) {
94 		cmc = mod->mod_ctf;
95 		*mc = *cmc;
96 		return (0);
97 	}
98 
99 	st = ksyms_get_mod(mod->mod_info->mi_name);
100 
101 	if (st != NULL) {
102 		mc->nmap     = st->sd_nmap;
103 		mc->nmapsize = st->sd_nmapsize;
104 	}
105 
106 	if (mod->mod_kobj == NULL) {
107 	    	/* no kobj entry, try building from ksyms list */
108 		if (st == NULL) {
109 			return ENOENT;
110 		}
111 
112 		ctfaddr = st->sd_ctfstart;
113 		ctfsize = st->sd_ctfsize;
114 
115 		mc->symtab = st->sd_symstart;
116 		mc->strtab = st->sd_strstart;
117 		mc->strcnt = 0;		/* XXX TBD */
118 		mc->nsym   = st->sd_symsize / sizeof(Elf_Sym);
119 	} else {
120 		if (kobj_find_section(mod->mod_kobj, ".SUNW_ctf", (void **)&ctfaddr, &ctfsize)) {
121 			return ENOENT;
122 		}
123 
124 		mc->symtab = mod->mod_kobj->ko_symtab;
125 		mc->strtab = mod->mod_kobj->ko_strtab;
126 		mc->strcnt = 0;		/* XXX TBD */
127 		mc->nsym   = mod->mod_kobj->ko_symcnt;
128 	}
129 
130 	if (ctfaddr == NULL) {
131 	    	error = ENOENT;
132 		goto out;
133 	}
134 
135 	/* Check the CTF magic number. (XXX check for big endian!) */
136 	if (ctfaddr[0] != 0xf1 || ctfaddr[1] != 0xcf) {
137 	    	error = EINVAL;
138 		goto out;
139 	}
140 
141 	/* Check if version 2. */
142 	if (ctfaddr[2] != 2) {
143 	    	error = EINVAL;
144 		goto out;
145 	}
146 
147 	/* Check if the data is compressed. */
148 	if ((ctfaddr[3] & 0x1) != 0) {
149 		uint32_t *u32 = (uint32_t *) ctfaddr;
150 
151 		/*
152 		 * The last two fields in the CTF header are the offset
153 		 * from the end of the header to the start of the string
154 		 * data and the length of that string data. se this
155 		 * information to determine the decompressed CTF data
156 		 * buffer required.
157 		 */
158 		sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
159 		    CTF_HDR_SIZE;
160 
161 		compressed = 1;
162 	} else {
163 		/*
164 		 * The CTF data is not compressed, so the ELF section
165 		 * size is the same as the buffer size required.
166 		 */
167 		sz = ctfsize;
168 	}
169 
170 	/*
171 	 * Allocate memory to buffer the CTF data in its decompressed
172 	 * form.
173 	 */
174 	if (compressed) {
175 		if ((ctfbuf = malloc(sz, M_TEMP, M_WAITOK)) == NULL) {
176 			error = ENOMEM;
177 			goto out;
178 		}
179 		ctftab = ctfbuf;
180 		mc->ctfalloc = 1;
181 	} else {
182 		ctftab = (void *)ctfaddr;
183 	}
184 
185 	/* Check if decompression is required. */
186 	if (compressed) {
187 		z_stream zs;
188 		int ret;
189 
190 		/*
191 		 * The header isn't compressed, so copy that into the
192 		 * CTF buffer first.
193 		 */
194 		memcpy(ctftab, ctfaddr, CTF_HDR_SIZE);
195 
196 		/* Initialise the zlib structure. */
197 		memset(&zs, 0, sizeof(zs));
198 		zs.zalloc = z_alloc;
199 		zs.zfree = z_free;
200 
201 		if (inflateInit2(&zs, MAX_WBITS) != Z_OK) {
202 			error = EIO;
203 			goto out;
204 		}
205 
206 		zs.avail_in = ctfsize - CTF_HDR_SIZE;
207 		zs.next_in = ctfaddr + CTF_HDR_SIZE;
208 		zs.avail_out = sz - CTF_HDR_SIZE;
209 		zs.next_out = ((uint8_t *) ctftab) + CTF_HDR_SIZE;
210 		inflateReset(&zs);
211 		if ((ret = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
212 			printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
213 			error = EIO;
214 			goto out;
215 		}
216 	}
217 
218 	/* Got the CTF data! */
219 	mc->ctftab = ctftab;
220 	mc->ctfcnt = ctfsize;
221 
222 	/* cache it */
223 	cmc = kmem_alloc(sizeof(mod_ctf_t), KM_SLEEP);
224 
225 	*cmc = *mc;
226 	mod->mod_ctf = cmc;
227 
228 	/* We'll retain the memory allocated for the CTF data. */
229 	ctfbuf = NULL;
230 
231 out:
232 	if (ctfbuf != NULL)
233 		free(ctfbuf, M_TEMP);
234 
235 	return (error);
236 }
237