xref: /freebsd/sys/kern/kern_ctf.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
29  */
30 
31 /*
32  * Note this file is included by both link_elf.c and link_elf_obj.c.
33  *
34  * The CTF header structure definition can't be used here because it's
35  * (annoyingly) covered by the CDDL. We will just use a few bytes from
36  * it as an integer array where we 'know' what they mean.
37  */
38 #define CTF_HDR_SIZE		36
39 #define CTF_HDR_STRTAB_U32	7
40 #define CTF_HDR_STRLEN_U32	8
41 
42 #ifdef DDB_CTF
43 static void *
44 z_alloc(void *nil, u_int items, u_int size)
45 {
46 	void *ptr;
47 
48 	ptr = malloc(items * size, M_TEMP, M_NOWAIT);
49 	return ptr;
50 }
51 
52 static void
53 z_free(void *nil, void *ptr)
54 {
55 	free(ptr, M_TEMP);
56 }
57 
58 #endif
59 
60 static int
61 link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
62 {
63 #ifdef DDB_CTF
64 	Elf_Ehdr *hdr = NULL;
65 	Elf_Shdr *shdr = NULL;
66 	caddr_t ctftab = NULL;
67 	caddr_t raw = NULL;
68 	caddr_t shstrtab = NULL;
69 	elf_file_t ef = (elf_file_t) lf;
70 	int flags;
71 	int i;
72 	int nbytes;
73 	size_t sz;
74 	struct nameidata nd;
75 	struct thread *td = curthread;
76 	uint8_t ctf_hdr[CTF_HDR_SIZE];
77 #endif
78 	int error = 0;
79 
80 	if (lf == NULL || lc == NULL)
81 		return (EINVAL);
82 
83 	/* Set the defaults for no CTF present. That's not a crime! */
84 	bzero(lc, sizeof(*lc));
85 
86 #ifdef DDB_CTF
87 	/*
88 	 * First check if we've tried to load CTF data previously and the
89 	 * CTF ELF section wasn't found. We flag that condition by setting
90 	 * ctfcnt to -1. See below.
91 	 */
92 	if (ef->ctfcnt < 0)
93 		return (EFTYPE);
94 
95 	/* Now check if we've already loaded the CTF data.. */
96 	if (ef->ctfcnt > 0) {
97 		/* We only need to load once. */
98 		lc->ctftab = ef->ctftab;
99 		lc->ctfcnt = ef->ctfcnt;
100 		lc->symtab = ef->ddbsymtab;
101 		lc->strtab = ef->ddbstrtab;
102 		lc->strcnt = ef->ddbstrcnt;
103 		lc->nsym   = ef->ddbsymcnt;
104 		lc->ctfoffp = (uint32_t **) &ef->ctfoff;
105 		lc->typoffp = (uint32_t **) &ef->typoff;
106 		lc->typlenp = &ef->typlen;
107 		return (0);
108 	}
109 
110 	/*
111 	 * We need to try reading the CTF data. Flag no CTF data present
112 	 * by default and if we actually succeed in reading it, we'll
113 	 * update ctfcnt to the number of bytes read.
114 	 */
115 	ef->ctfcnt = -1;
116 
117 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, lf->pathname, td);
118 	flags = FREAD;
119 	error = vn_open(&nd, &flags, 0, NULL);
120 	if (error)
121 		return (error);
122 	NDFREE(&nd, NDF_ONLY_PNBUF);
123 
124 	/* Allocate memory for the FLF header. */
125 	hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
126 
127 	/* Read the ELF header. */
128 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, hdr, sizeof(*hdr),
129 	    0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL,
130 	    td)) != 0)
131 		goto out;
132 
133 	/* Sanity check. */
134 	if (!IS_ELF(*hdr)) {
135 		error = ENOEXEC;
136 		goto out;
137 	}
138 
139 	nbytes = hdr->e_shnum * hdr->e_shentsize;
140 	if (nbytes == 0 || hdr->e_shoff == 0 ||
141 	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
142 		error = ENOEXEC;
143 		goto out;
144 	}
145 
146 	/* Allocate memory for all the section headers */
147 	shdr = malloc(nbytes, M_LINKER, M_WAITOK);
148 
149 	/* Read all the section headers */
150 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes,
151 	    hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
152 	    NULL, td)) != 0)
153 		goto out;
154 
155 	/*
156 	 * We need to search for the CTF section by name, so if the
157 	 * section names aren't present, then we can't locate the
158 	 * .SUNW_ctf section containing the CTF data.
159 	 */
160 	if (hdr->e_shstrndx == 0 || shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
161 		printf("%s(%d): module %s e_shstrndx is %d, sh_type is %d\n",
162 		    __func__, __LINE__, lf->pathname, hdr->e_shstrndx,
163 		    shdr[hdr->e_shstrndx].sh_type);
164 		error = EFTYPE;
165 		goto out;
166 	}
167 
168 	/* Allocate memory to buffer the section header strings. */
169 	shstrtab = malloc(shdr[hdr->e_shstrndx].sh_size, M_LINKER, M_WAITOK);
170 
171 	/* Read the section header strings. */
172 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, shstrtab,
173 	    shdr[hdr->e_shstrndx].sh_size, shdr[hdr->e_shstrndx].sh_offset,
174 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL, td)) != 0)
175 		goto out;
176 
177 	/* Search for the section containing the CTF data. */
178 	for (i = 0; i < hdr->e_shnum; i++)
179 		if (strcmp(".SUNW_ctf", shstrtab + shdr[i].sh_name) == 0)
180 			break;
181 
182 	/* Check if the CTF section wasn't found. */
183 	if (i >= hdr->e_shnum) {
184 		printf("%s(%d): module %s has no .SUNW_ctf section\n",
185 		    __func__, __LINE__, lf->pathname);
186 		error = EFTYPE;
187 		goto out;
188 	}
189 
190 	/* Read the CTF header. */
191 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, ctf_hdr, sizeof(ctf_hdr),
192 	    shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
193 	    NOCRED, NULL, td)) != 0)
194 		goto out;
195 
196 	/* Check the CTF magic number. (XXX check for big endian!) */
197 	if (ctf_hdr[0] != 0xf1 || ctf_hdr[1] != 0xcf) {
198 		printf("%s(%d): module %s has invalid format\n",
199 		    __func__, __LINE__, lf->pathname);
200 		error = EFTYPE;
201 		goto out;
202 	}
203 
204 	/* Check if version 2. */
205 	if (ctf_hdr[2] != 2) {
206 		printf("%s(%d): module %s CTF format version is %d "
207 		    "(2 expected)\n",
208 		    __func__, __LINE__, lf->pathname, ctf_hdr[2]);
209 		error = EFTYPE;
210 		goto out;
211 	}
212 
213 	/* Check if the data is compressed. */
214 	if ((ctf_hdr[3] & 0x1) != 0) {
215 		uint32_t *u32 = (uint32_t *) ctf_hdr;
216 
217 		/*
218 		 * The last two fields in the CTF header are the offset
219 		 * from the end of the header to the start of the string
220 		 * data and the length of that string data. se this
221 		 * information to determine the decompressed CTF data
222 		 * buffer required.
223 		 */
224 		sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
225 		    sizeof(ctf_hdr);
226 
227 		/*
228 		 * Allocate memory for the compressed CTF data, including
229 		 * the header (which isn't compressed).
230 		 */
231 		raw = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
232 	} else {
233 		/*
234 		 * The CTF data is not compressed, so the ELF section
235 		 * size is the same as the buffer size required.
236 		 */
237 		sz = shdr[i].sh_size;
238 	}
239 
240 	/*
241 	 * Allocate memory to buffer the CTF data in its decompressed
242 	 * form.
243 	 */
244 	ctftab = malloc(sz, M_LINKER, M_WAITOK);
245 
246 	/*
247 	 * Read the CTF data into the raw buffer if compressed, or
248 	 * directly into the CTF buffer otherwise.
249 	 */
250 	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, raw == NULL ? ctftab : raw,
251 	    shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
252 	    td->td_ucred, NOCRED, NULL, td)) != 0)
253 		goto out;
254 
255 	/* Check if decompression is required. */
256 	if (raw != NULL) {
257 		z_stream zs;
258 		int ret;
259 
260 		/*
261 		 * The header isn't compressed, so copy that into the
262 		 * CTF buffer first.
263 		 */
264 		bcopy(ctf_hdr, ctftab, sizeof(ctf_hdr));
265 
266 		/* Initialise the zlib structure. */
267 		bzero(&zs, sizeof(zs));
268 		zs.zalloc = z_alloc;
269 		zs.zfree = z_free;
270 
271 		if (inflateInit(&zs) != Z_OK) {
272 			error = EIO;
273 			goto out;
274 		}
275 
276 		zs.avail_in = shdr[i].sh_size - sizeof(ctf_hdr);
277 		zs.next_in = ((uint8_t *) raw) + sizeof(ctf_hdr);
278 		zs.avail_out = sz - sizeof(ctf_hdr);
279 		zs.next_out = ((uint8_t *) ctftab) + sizeof(ctf_hdr);
280 		ret = inflate(&zs, Z_FINISH);
281 		inflateEnd(&zs);
282 		if (ret != Z_STREAM_END) {
283 			printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
284 			error = EIO;
285 			goto out;
286 		}
287 	}
288 
289 	/* Got the CTF data! */
290 	ef->ctftab = ctftab;
291 	ef->ctfcnt = shdr[i].sh_size;
292 
293 	/* We'll retain the memory allocated for the CTF data. */
294 	ctftab = NULL;
295 
296 	/* Let the caller use the CTF data read. */
297 	lc->ctftab = ef->ctftab;
298 	lc->ctfcnt = ef->ctfcnt;
299 	lc->symtab = ef->ddbsymtab;
300 	lc->strtab = ef->ddbstrtab;
301 	lc->strcnt = ef->ddbstrcnt;
302 	lc->nsym   = ef->ddbsymcnt;
303 	lc->ctfoffp = (uint32_t **) &ef->ctfoff;
304 	lc->typoffp = (uint32_t **) &ef->typoff;
305 	lc->typlenp = &ef->typlen;
306 
307 out:
308 	VOP_UNLOCK(nd.ni_vp, 0);
309 	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
310 
311 	if (hdr != NULL)
312 		free(hdr, M_LINKER);
313 	if (shdr != NULL)
314 		free(shdr, M_LINKER);
315 	if (shstrtab != NULL)
316 		free(shstrtab, M_LINKER);
317 	if (ctftab != NULL)
318 		free(ctftab, M_LINKER);
319 	if (raw != NULL)
320 		free(raw, M_LINKER);
321 #else
322 	error = EOPNOTSUPP;
323 #endif
324 
325 	return (error);
326 }
327