xref: /freebsd/sys/kern/kern_ctf.c (revision a970610a3af63b3f4df5b69d91c6b4093a00ed8f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 John Birrell <jb@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/ctf.h>
30 #include <sys/kdb.h>
31 #include <sys/linker.h>
32 
33 #include <ddb/db_ctf.h>
34 
35 /*
36  * Note this file is included by both link_elf.c and link_elf_obj.c.
37  */
38 
39 #ifdef DDB_CTF
40 #include <contrib/zlib/zlib.h>
41 #endif
42 
43 static int
44 link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
45 {
46 #ifdef DDB_CTF
47 	Elf_Ehdr *hdr = NULL;
48 	Elf_Shdr *shdr = NULL;
49 	caddr_t ctftab = NULL;
50 	caddr_t raw = NULL;
51 	caddr_t shstrtab = NULL;
52 	elf_file_t ef = (elf_file_t) lf;
53 	int flags;
54 	int i;
55 	int nbytes;
56 	size_t sz;
57 	struct nameidata nd;
58 	struct thread *td = curthread;
59 	struct ctf_header cth;
60 #endif
61 	int error = 0;
62 
63 	if (lf == NULL || lc == NULL)
64 		return (EINVAL);
65 
66 	/* Set the defaults for no CTF present. That's not a crime! */
67 	bzero(lc, sizeof(*lc));
68 
69 #ifdef DDB_CTF
70 	/*
71 	 * First check if we've tried to load CTF data previously and the
72 	 * CTF ELF section wasn't found. We flag that condition by setting
73 	 * ctfcnt to -1. See below.
74 	 */
75 	if (ef->ctfcnt < 0)
76 		return (EFTYPE);
77 
78 	/* Now check if we've already loaded the CTF data.. */
79 	if (ef->ctfcnt > 0) {
80 		/* We only need to load once. */
81 		lc->ctftab = ef->ctftab;
82 		lc->ctfcnt = ef->ctfcnt;
83 		lc->symtab = ef->ddbsymtab;
84 		lc->strtab = ef->ddbstrtab;
85 		lc->strcnt = ef->ddbstrcnt;
86 		lc->nsym   = ef->ddbsymcnt;
87 		lc->ctfoffp = (uint32_t **) &ef->ctfoff;
88 		lc->typoffp = (uint32_t **) &ef->typoff;
89 		lc->typlenp = &ef->typlen;
90 		return (0);
91 	}
92 
93 	if (panicstr != NULL || kdb_active)
94 		return (ENXIO);
95 
96 	/*
97 	 * We need to try reading the CTF data. Flag no CTF data present
98 	 * by default and if we actually succeed in reading it, we'll
99 	 * update ctfcnt to the number of bytes read.
100 	 */
101 	ef->ctfcnt = -1;
102 
103 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, lf->pathname);
104 	flags = FREAD;
105 	error = vn_open(&nd, &flags, 0, NULL);
106 	if (error)
107 		return (error);
108 	NDFREE_PNBUF(&nd);
109 
110 	/* Allocate memory for the FLF header. */
111 	hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
112 
113 	/* Read the ELF header. */
114 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, hdr, sizeof(*hdr),
115 	    0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL,
116 	    td)) != 0)
117 		goto out;
118 
119 	/* Sanity check. */
120 	if (!IS_ELF(*hdr)) {
121 		error = ENOEXEC;
122 		goto out;
123 	}
124 
125 	nbytes = hdr->e_shnum * hdr->e_shentsize;
126 	if (nbytes == 0 || hdr->e_shoff == 0 ||
127 	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
128 		error = ENOEXEC;
129 		goto out;
130 	}
131 
132 	/* Allocate memory for all the section headers */
133 	shdr = malloc(nbytes, M_LINKER, M_WAITOK);
134 
135 	/* Read all the section headers */
136 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes,
137 	    hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
138 	    NULL, td)) != 0)
139 		goto out;
140 
141 	/*
142 	 * We need to search for the CTF section by name, so if the
143 	 * section names aren't present, then we can't locate the
144 	 * .SUNW_ctf section containing the CTF data.
145 	 */
146 	if (hdr->e_shstrndx == 0 || shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
147 		if (bootverbose) {
148 			printf(
149 			    "%s(%d): module %s e_shstrndx is %d, sh_type is %d\n",
150 			    __func__, __LINE__, lf->pathname, hdr->e_shstrndx,
151 			    shdr[hdr->e_shstrndx].sh_type);
152 		}
153 		error = EFTYPE;
154 		goto out;
155 	}
156 
157 	/* Allocate memory to buffer the section header strings. */
158 	shstrtab = malloc(shdr[hdr->e_shstrndx].sh_size, M_LINKER, M_WAITOK);
159 
160 	/* Read the section header strings. */
161 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, shstrtab,
162 	    shdr[hdr->e_shstrndx].sh_size, shdr[hdr->e_shstrndx].sh_offset,
163 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL, td)) != 0)
164 		goto out;
165 
166 	/* Search for the section containing the CTF data. */
167 	for (i = 0; i < hdr->e_shnum; i++)
168 		if (strcmp(".SUNW_ctf", shstrtab + shdr[i].sh_name) == 0)
169 			break;
170 
171 	/* Check if the CTF section wasn't found. */
172 	if (i >= hdr->e_shnum) {
173 		if (bootverbose) {
174 			printf("%s(%d): module %s has no .SUNW_ctf section\n",
175 			    __func__, __LINE__, lf->pathname);
176 		}
177 		error = EFTYPE;
178 		goto out;
179 	}
180 
181 	/* Read the CTF header. */
182 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, &cth, sizeof(cth),
183 	    shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
184 	    NOCRED, NULL, td)) != 0)
185 		goto out;
186 
187 	/* Check the CTF magic number. */
188 	if (cth.cth_magic != CTF_MAGIC) {
189 		if (bootverbose) {
190 			printf("%s(%d): module %s has invalid format\n",
191 			    __func__, __LINE__, lf->pathname);
192 		}
193 		error = EFTYPE;
194 		goto out;
195 	}
196 
197 	if (cth.cth_version != CTF_VERSION_2 &&
198 	    cth.cth_version != CTF_VERSION_3) {
199 		if (bootverbose) {
200 			printf(
201 			    "%s(%d): module %s CTF format has unsupported version %d\n",
202 			    __func__, __LINE__, lf->pathname, cth.cth_version);
203 		}
204 		error = EFTYPE;
205 		goto out;
206 	}
207 
208 	/* Check if the data is compressed. */
209 	if ((cth.cth_flags & CTF_F_COMPRESS) != 0) {
210 		/*
211 		 * The last two fields in the CTF header are the offset
212 		 * from the end of the header to the start of the string
213 		 * data and the length of that string data. Use this
214 		 * information to determine the decompressed CTF data
215 		 * buffer required.
216 		 */
217 		sz = cth.cth_stroff + cth.cth_strlen + sizeof(cth);
218 
219 		/*
220 		 * Allocate memory for the compressed CTF data, including
221 		 * the header (which isn't compressed).
222 		 */
223 		raw = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
224 	} else {
225 		/*
226 		 * The CTF data is not compressed, so the ELF section
227 		 * size is the same as the buffer size required.
228 		 */
229 		sz = shdr[i].sh_size;
230 	}
231 
232 	/*
233 	 * Allocate memory to buffer the CTF data in its decompressed
234 	 * form.
235 	 */
236 	ctftab = malloc(sz, M_LINKER, M_WAITOK);
237 
238 	/*
239 	 * Read the CTF data into the raw buffer if compressed, or
240 	 * directly into the CTF buffer otherwise.
241 	 */
242 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, raw == NULL ? ctftab : raw,
243 	    shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
244 	    td->td_ucred, NOCRED, NULL, td)) != 0)
245 		goto out;
246 
247 	/* Check if decompression is required. */
248 	if (raw != NULL) {
249 		uLongf destlen;
250 		int ret;
251 
252 		/*
253 		 * The header isn't compressed, so copy that into the
254 		 * CTF buffer first.
255 		 */
256 		bcopy(&cth, ctftab, sizeof(cth));
257 
258 		destlen = sz - sizeof(cth);
259 		ret = uncompress(ctftab + sizeof(cth), &destlen,
260 		    raw + sizeof(cth), shdr[i].sh_size - sizeof(cth));
261 		if (ret != Z_OK) {
262 			if (bootverbose) {
263 				printf("%s(%d): zlib uncompress returned %d\n",
264 				    __func__, __LINE__, ret);
265 			}
266 			error = EIO;
267 			goto out;
268 		}
269 	}
270 
271 	/* Got the CTF data! */
272 	ef->ctftab = ctftab;
273 	ef->ctfcnt = shdr[i].sh_size;
274 
275 	/* We'll retain the memory allocated for the CTF data. */
276 	ctftab = NULL;
277 
278 	/* Let the caller use the CTF data read. */
279 	lc->ctftab = ef->ctftab;
280 	lc->ctfcnt = ef->ctfcnt;
281 	lc->symtab = ef->ddbsymtab;
282 	lc->strtab = ef->ddbstrtab;
283 	lc->strcnt = ef->ddbstrcnt;
284 	lc->nsym   = ef->ddbsymcnt;
285 	lc->ctfoffp = (uint32_t **) &ef->ctfoff;
286 	lc->typoffp = (uint32_t **) &ef->typoff;
287 	lc->typlenp = &ef->typlen;
288 
289 out:
290 	VOP_UNLOCK(nd.ni_vp);
291 	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
292 
293 	if (hdr != NULL)
294 		free(hdr, M_LINKER);
295 	if (shdr != NULL)
296 		free(shdr, M_LINKER);
297 	if (shstrtab != NULL)
298 		free(shstrtab, M_LINKER);
299 	if (ctftab != NULL)
300 		free(ctftab, M_LINKER);
301 	if (raw != NULL)
302 		free(raw, M_LINKER);
303 #else
304 	error = EOPNOTSUPP;
305 #endif
306 
307 	return (error);
308 }
309 
310 static int
311 link_elf_ctf_get_ddb(linker_file_t lf, linker_ctf_t *lc)
312 {
313 	elf_file_t ef = (elf_file_t)lf;
314 
315 	/*
316 	 * Check whether CTF data was loaded or if a
317 	 * previous loading attempt failed (ctfcnt == -1).
318 	 */
319 	if (ef->ctfcnt <= 0) {
320 		return (ENOENT);
321 	}
322 
323 	lc->ctftab = ef->ctftab;
324 	lc->ctfcnt = ef->ctfcnt;
325 	lc->symtab = ef->ddbsymtab;
326 	lc->strtab = ef->ddbstrtab;
327 	lc->strcnt = ef->ddbstrcnt;
328 	lc->nsym = ef->ddbsymcnt;
329 
330 	return (0);
331 }
332 
333 static int
334 link_elf_ctf_lookup_typename(linker_file_t lf, linker_ctf_t *lc,
335     const char *typename)
336 {
337 	if (link_elf_ctf_get_ddb(lf, lc))
338 		return (ENOENT);
339 
340 #ifdef DDB
341 	return (db_ctf_lookup_typename(lc, typename) ? 0 : ENOENT);
342 #else
343 	return (ENOENT);
344 #endif
345 }
346