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