xref: /illumos-gate/usr/src/common/ctf/ctf_open.c (revision a676209deb2ce5d0c98f331659de25e2483f8c4c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /*
28  * Copyright (c) 2015, Joyent, Inc.  All rights reserved.
29  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
30  */
31 
32 #include <ctf_impl.h>
33 #include <sys/mman.h>
34 #include <sys/zmod.h>
35 
36 static const ctf_dmodel_t _libctf_models[] = {
37 	{ "ILP32", CTF_MODEL_ILP32, 4, 1, 2, 4, 4 },
38 	{ "LP64", CTF_MODEL_LP64, 8, 1, 2, 4, 8 },
39 	{ NULL, 0, 0, 0, 0, 0, 0 }
40 };
41 
42 const char _CTF_SECTION[] = ".SUNW_ctf";
43 const char _CTF_NULLSTR[] = "";
44 
45 int _libctf_version = CTF_VERSION;	/* library client version */
46 int _libctf_debug = 0;			/* debugging messages enabled */
47 
48 static ushort_t
get_kind_v1(ushort_t info)49 get_kind_v1(ushort_t info)
50 {
51 	return (CTF_INFO_KIND_V1(info));
52 }
53 
54 static ushort_t
get_kind_v2(ushort_t info)55 get_kind_v2(ushort_t info)
56 {
57 	return (CTF_INFO_KIND(info));
58 }
59 
60 static ushort_t
get_root_v1(ushort_t info)61 get_root_v1(ushort_t info)
62 {
63 	return (CTF_INFO_ISROOT_V1(info));
64 }
65 
66 static ushort_t
get_root_v2(ushort_t info)67 get_root_v2(ushort_t info)
68 {
69 	return (CTF_INFO_ISROOT(info));
70 }
71 
72 static ushort_t
get_vlen_v1(ushort_t info)73 get_vlen_v1(ushort_t info)
74 {
75 	return (CTF_INFO_VLEN_V1(info));
76 }
77 
78 static ushort_t
get_vlen_v2(ushort_t info)79 get_vlen_v2(ushort_t info)
80 {
81 	return (CTF_INFO_VLEN(info));
82 }
83 
84 static const ctf_fileops_t ctf_fileops[] = {
85 	{ NULL, NULL },
86 	{ get_kind_v1, get_root_v1, get_vlen_v1 },
87 	{ get_kind_v2, get_root_v2, get_vlen_v2 },
88 };
89 
90 /*
91  * Convert a 32-bit ELF symbol into GElf (Elf64) and return a pointer to it.
92  */
93 static Elf64_Sym *
sym_to_gelf(const Elf32_Sym * src,Elf64_Sym * dst)94 sym_to_gelf(const Elf32_Sym *src, Elf64_Sym *dst)
95 {
96 	dst->st_name = src->st_name;
97 	dst->st_value = src->st_value;
98 	dst->st_size = src->st_size;
99 	dst->st_info = src->st_info;
100 	dst->st_other = src->st_other;
101 	dst->st_shndx = src->st_shndx;
102 
103 	return (dst);
104 }
105 
106 /*
107  * Initialize the symtab translation table by filling each entry with the
108  * offset of the CTF type or function data corresponding to each STT_FUNC or
109  * STT_OBJECT entry in the symbol table.
110  */
111 static int
init_symtab(ctf_file_t * fp,const ctf_header_t * hp,const ctf_sect_t * sp,const ctf_sect_t * strp)112 init_symtab(ctf_file_t *fp, const ctf_header_t *hp,
113     const ctf_sect_t *sp, const ctf_sect_t *strp)
114 {
115 	const uchar_t *symp = sp->cts_data;
116 	uint_t *xp = fp->ctf_sxlate;
117 	uint_t *xend = xp + fp->ctf_nsyms;
118 
119 	uint_t objtoff = hp->cth_objtoff;
120 	uint_t funcoff = hp->cth_funcoff;
121 
122 	ushort_t info, vlen;
123 	Elf64_Sym sym, *gsp;
124 	const char *name;
125 
126 	/*
127 	 * The CTF data object and function type sections are ordered to match
128 	 * the relative order of the respective symbol types in the symtab.
129 	 * If no type information is available for a symbol table entry, a
130 	 * pad is inserted in the CTF section.  As a further optimization,
131 	 * anonymous or undefined symbols are omitted from the CTF data.
132 	 */
133 	for (; xp < xend; xp++, symp += sp->cts_entsize) {
134 		if (sp->cts_entsize == sizeof (Elf32_Sym))
135 			gsp = sym_to_gelf((Elf32_Sym *)(uintptr_t)symp, &sym);
136 		else
137 			gsp = (Elf64_Sym *)(uintptr_t)symp;
138 
139 		if (gsp->st_name < strp->cts_size)
140 			name = (const char *)strp->cts_data + gsp->st_name;
141 		else
142 			name = _CTF_NULLSTR;
143 
144 		if (gsp->st_name == 0 || gsp->st_shndx == SHN_UNDEF ||
145 		    strcmp(name, "_START_") == 0 ||
146 		    strcmp(name, "_END_") == 0) {
147 			*xp = -1u;
148 			continue;
149 		}
150 
151 		switch (ELF64_ST_TYPE(gsp->st_info)) {
152 		case STT_OBJECT:
153 			if (objtoff >= hp->cth_funcoff ||
154 			    (gsp->st_shndx == SHN_ABS && gsp->st_value == 0)) {
155 				*xp = -1u;
156 				break;
157 			}
158 
159 			*xp = objtoff;
160 			objtoff += sizeof (ushort_t);
161 			break;
162 
163 		case STT_FUNC:
164 			if (funcoff >= hp->cth_typeoff) {
165 				*xp = -1u;
166 				break;
167 			}
168 
169 			*xp = funcoff;
170 
171 			info = *(ushort_t *)((uintptr_t)fp->ctf_buf + funcoff);
172 			vlen = LCTF_INFO_VLEN(fp, info);
173 
174 			/*
175 			 * If we encounter a zero pad at the end, just skip it.
176 			 * Otherwise skip over the function and its return type
177 			 * (+2) and the argument list (vlen).
178 			 */
179 			if (LCTF_INFO_KIND(fp, info) == CTF_K_UNKNOWN &&
180 			    vlen == 0)
181 				funcoff += sizeof (ushort_t); /* skip pad */
182 			else
183 				funcoff += sizeof (ushort_t) * (vlen + 2);
184 			break;
185 
186 		default:
187 			*xp = -1u;
188 			break;
189 		}
190 	}
191 
192 	ctf_dprintf("loaded %lu symtab entries\n", fp->ctf_nsyms);
193 	return (0);
194 }
195 
196 /*
197  * Initialize the type ID translation table with the byte offset of each type,
198  * and initialize the hash tables of each named type.
199  */
200 static int
init_types(ctf_file_t * fp,const ctf_header_t * cth)201 init_types(ctf_file_t *fp, const ctf_header_t *cth)
202 {
203 	/* LINTED - pointer alignment */
204 	const ctf_type_t *tbuf = (ctf_type_t *)(fp->ctf_buf + cth->cth_typeoff);
205 	/* LINTED - pointer alignment */
206 	const ctf_type_t *tend = (ctf_type_t *)(fp->ctf_buf + cth->cth_stroff);
207 
208 	ulong_t pop[CTF_K_MAX + 1] = { 0 };
209 	const ctf_type_t *tp;
210 	ctf_hash_t *hp;
211 	ushort_t id, dst;
212 	uint_t *xp;
213 
214 	/*
215 	 * We initially determine whether the container is a child or a parent
216 	 * based on the value of cth_parname.  To support containers that pre-
217 	 * date cth_parname, we also scan the types themselves for references
218 	 * to values in the range reserved for child types in our first pass.
219 	 */
220 	int child = cth->cth_parname != 0;
221 	int nlstructs = 0, nlunions = 0;
222 	int err;
223 
224 	/*
225 	 * We make two passes through the entire type section.  In this first
226 	 * pass, we count the number of each type and the total number of types.
227 	 */
228 	for (tp = tbuf; tp < tend; fp->ctf_typemax++) {
229 		ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info);
230 		ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info);
231 		ssize_t size, increment;
232 
233 		size_t vbytes;
234 		uint_t n;
235 
236 		(void) ctf_get_ctt_size(fp, tp, &size, &increment);
237 
238 		switch (kind) {
239 		case CTF_K_INTEGER:
240 		case CTF_K_FLOAT:
241 			vbytes = sizeof (uint_t);
242 			break;
243 		case CTF_K_ARRAY:
244 			vbytes = sizeof (ctf_array_t);
245 			break;
246 		case CTF_K_FUNCTION:
247 			vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
248 			break;
249 		case CTF_K_STRUCT:
250 		case CTF_K_UNION:
251 			if (fp->ctf_version == CTF_VERSION_1 ||
252 			    size < CTF_LSTRUCT_THRESH) {
253 				ctf_member_t *mp = (ctf_member_t *)
254 				    ((uintptr_t)tp + increment);
255 
256 				vbytes = sizeof (ctf_member_t) * vlen;
257 				for (n = vlen; n != 0; n--, mp++)
258 					child |= CTF_TYPE_ISCHILD(mp->ctm_type);
259 			} else {
260 				ctf_lmember_t *lmp = (ctf_lmember_t *)
261 				    ((uintptr_t)tp + increment);
262 
263 				vbytes = sizeof (ctf_lmember_t) * vlen;
264 				for (n = vlen; n != 0; n--, lmp++)
265 					child |=
266 					    CTF_TYPE_ISCHILD(lmp->ctlm_type);
267 			}
268 			break;
269 		case CTF_K_ENUM:
270 			vbytes = sizeof (ctf_enum_t) * vlen;
271 			break;
272 		case CTF_K_FORWARD:
273 			/*
274 			 * For forward declarations, ctt_type is the CTF_K_*
275 			 * kind for the tag, so bump that population count too.
276 			 * If ctt_type is unknown, treat the tag as a struct.
277 			 */
278 			if (tp->ctt_type == CTF_K_UNKNOWN ||
279 			    tp->ctt_type >= CTF_K_MAX)
280 				pop[CTF_K_STRUCT]++;
281 			else
282 				pop[tp->ctt_type]++;
283 			/*FALLTHRU*/
284 		case CTF_K_UNKNOWN:
285 			vbytes = 0;
286 			break;
287 		case CTF_K_POINTER:
288 		case CTF_K_TYPEDEF:
289 		case CTF_K_VOLATILE:
290 		case CTF_K_CONST:
291 		case CTF_K_RESTRICT:
292 			child |= CTF_TYPE_ISCHILD(tp->ctt_type);
293 			vbytes = 0;
294 			break;
295 		default:
296 			ctf_dprintf("detected invalid CTF kind -- %u\n", kind);
297 			return (ECTF_CORRUPT);
298 		}
299 		tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
300 		pop[kind]++;
301 	}
302 
303 	/*
304 	 * If we detected a reference to a child type ID, then we know this
305 	 * container is a child and may have a parent's types imported later.
306 	 */
307 	if (child) {
308 		ctf_dprintf("CTF container %p is a child\n", (void *)fp);
309 		fp->ctf_flags |= LCTF_CHILD;
310 	} else
311 		ctf_dprintf("CTF container %p is a parent\n", (void *)fp);
312 
313 	/*
314 	 * Now that we've counted up the number of each type, we can allocate
315 	 * the hash tables, type translation table, and pointer table.
316 	 */
317 	if ((err = ctf_hash_create(&fp->ctf_structs, pop[CTF_K_STRUCT])) != 0)
318 		return (err);
319 
320 	if ((err = ctf_hash_create(&fp->ctf_unions, pop[CTF_K_UNION])) != 0)
321 		return (err);
322 
323 	if ((err = ctf_hash_create(&fp->ctf_enums, pop[CTF_K_ENUM])) != 0)
324 		return (err);
325 
326 	if ((err = ctf_hash_create(&fp->ctf_names,
327 	    pop[CTF_K_INTEGER] + pop[CTF_K_FLOAT] + pop[CTF_K_FUNCTION] +
328 	    pop[CTF_K_TYPEDEF] + pop[CTF_K_POINTER] + pop[CTF_K_VOLATILE] +
329 	    pop[CTF_K_CONST] + pop[CTF_K_RESTRICT])) != 0)
330 		return (err);
331 
332 	fp->ctf_txlate = ctf_alloc(sizeof (uint_t) * (fp->ctf_typemax + 1));
333 	fp->ctf_ptrtab = ctf_alloc(sizeof (ushort_t) * (fp->ctf_typemax + 1));
334 
335 	if (fp->ctf_txlate == NULL || fp->ctf_ptrtab == NULL)
336 		return (EAGAIN); /* memory allocation failed */
337 
338 	xp = fp->ctf_txlate;
339 	*xp++ = 0; /* type id 0 is used as a sentinel value */
340 
341 	bzero(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1));
342 	bzero(fp->ctf_ptrtab, sizeof (ushort_t) * (fp->ctf_typemax + 1));
343 
344 	/*
345 	 * In the second pass through the types, we fill in each entry of the
346 	 * type and pointer tables and add names to the appropriate hashes.
347 	 */
348 	for (id = 1, tp = tbuf; tp < tend; xp++, id++) {
349 		ushort_t kind = LCTF_INFO_KIND(fp, tp->ctt_info);
350 		ulong_t vlen = LCTF_INFO_VLEN(fp, tp->ctt_info);
351 		ssize_t size, increment;
352 
353 		const char *name;
354 		size_t vbytes;
355 		ctf_helem_t *hep;
356 		ctf_encoding_t cte;
357 
358 		(void) ctf_get_ctt_size(fp, tp, &size, &increment);
359 		name = ctf_strptr(fp, tp->ctt_name);
360 
361 		switch (kind) {
362 		case CTF_K_INTEGER:
363 		case CTF_K_FLOAT:
364 			/*
365 			 * Only insert a new integer base type definition if
366 			 * this type name has not been defined yet.  We re-use
367 			 * the names with different encodings for bit-fields.
368 			 */
369 			if ((hep = ctf_hash_lookup(&fp->ctf_names, fp,
370 			    name, strlen(name))) == NULL) {
371 				err = ctf_hash_insert(&fp->ctf_names, fp,
372 				    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
373 				if (err != 0 && err != ECTF_STRTAB)
374 					return (err);
375 			} else if (ctf_type_encoding(fp, hep->h_type,
376 			    &cte) == 0 && cte.cte_bits == 0) {
377 				/*
378 				 * Work-around SOS8 stabs bug: replace existing
379 				 * intrinsic w/ same name if it was zero bits.
380 				 */
381 				hep->h_type = CTF_INDEX_TO_TYPE(id, child);
382 			}
383 			vbytes = sizeof (uint_t);
384 			break;
385 
386 		case CTF_K_ARRAY:
387 			vbytes = sizeof (ctf_array_t);
388 			break;
389 
390 		case CTF_K_FUNCTION:
391 			err = ctf_hash_insert(&fp->ctf_names, fp,
392 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
393 			if (err != 0 && err != ECTF_STRTAB)
394 				return (err);
395 			vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
396 			break;
397 
398 		case CTF_K_STRUCT:
399 			err = ctf_hash_define(&fp->ctf_structs, fp,
400 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
401 
402 			if (err != 0 && err != ECTF_STRTAB)
403 				return (err);
404 
405 			if (fp->ctf_version == CTF_VERSION_1 ||
406 			    size < CTF_LSTRUCT_THRESH)
407 				vbytes = sizeof (ctf_member_t) * vlen;
408 			else {
409 				vbytes = sizeof (ctf_lmember_t) * vlen;
410 				nlstructs++;
411 			}
412 			break;
413 
414 		case CTF_K_UNION:
415 			err = ctf_hash_define(&fp->ctf_unions, fp,
416 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
417 
418 			if (err != 0 && err != ECTF_STRTAB)
419 				return (err);
420 
421 			if (fp->ctf_version == CTF_VERSION_1 ||
422 			    size < CTF_LSTRUCT_THRESH)
423 				vbytes = sizeof (ctf_member_t) * vlen;
424 			else {
425 				vbytes = sizeof (ctf_lmember_t) * vlen;
426 				nlunions++;
427 			}
428 			break;
429 
430 		case CTF_K_ENUM:
431 			err = ctf_hash_define(&fp->ctf_enums, fp,
432 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
433 
434 			if (err != 0 && err != ECTF_STRTAB)
435 				return (err);
436 
437 			vbytes = sizeof (ctf_enum_t) * vlen;
438 			break;
439 
440 		case CTF_K_TYPEDEF:
441 			err = ctf_hash_insert(&fp->ctf_names, fp,
442 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
443 			if (err != 0 && err != ECTF_STRTAB)
444 				return (err);
445 			vbytes = 0;
446 			break;
447 
448 		case CTF_K_FORWARD:
449 			/*
450 			 * Only insert forward tags into the given hash if the
451 			 * type or tag name is not already present.
452 			 */
453 			switch (tp->ctt_type) {
454 			case CTF_K_STRUCT:
455 				hp = &fp->ctf_structs;
456 				break;
457 			case CTF_K_UNION:
458 				hp = &fp->ctf_unions;
459 				break;
460 			case CTF_K_ENUM:
461 				hp = &fp->ctf_enums;
462 				break;
463 			default:
464 				hp = &fp->ctf_structs;
465 			}
466 
467 			if (ctf_hash_lookup(hp, fp,
468 			    name, strlen(name)) == NULL) {
469 				err = ctf_hash_insert(hp, fp,
470 				    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
471 				if (err != 0 && err != ECTF_STRTAB)
472 					return (err);
473 			}
474 			vbytes = 0;
475 			break;
476 
477 		case CTF_K_POINTER:
478 			/*
479 			 * If the type referenced by the pointer is in this CTF
480 			 * container, then store the index of the pointer type
481 			 * in fp->ctf_ptrtab[ index of referenced type ].
482 			 */
483 			if (CTF_TYPE_ISCHILD(tp->ctt_type) == child &&
484 			    CTF_TYPE_TO_INDEX(tp->ctt_type) <= fp->ctf_typemax)
485 				fp->ctf_ptrtab[
486 				    CTF_TYPE_TO_INDEX(tp->ctt_type)] = id;
487 			/*FALLTHRU*/
488 
489 		case CTF_K_VOLATILE:
490 		case CTF_K_CONST:
491 		case CTF_K_RESTRICT:
492 			err = ctf_hash_insert(&fp->ctf_names, fp,
493 			    CTF_INDEX_TO_TYPE(id, child), tp->ctt_name);
494 			if (err != 0 && err != ECTF_STRTAB)
495 				return (err);
496 			/*FALLTHRU*/
497 
498 		default:
499 			vbytes = 0;
500 			break;
501 		}
502 
503 		*xp = (uint_t)((uintptr_t)tp - (uintptr_t)fp->ctf_buf);
504 		tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
505 	}
506 
507 	ctf_dprintf("%lu total types processed\n", fp->ctf_typemax);
508 	ctf_dprintf("%u enum names hashed\n", ctf_hash_size(&fp->ctf_enums));
509 	ctf_dprintf("%u struct names hashed (%d long)\n",
510 	    ctf_hash_size(&fp->ctf_structs), nlstructs);
511 	ctf_dprintf("%u union names hashed (%d long)\n",
512 	    ctf_hash_size(&fp->ctf_unions), nlunions);
513 	ctf_dprintf("%u base type names hashed\n",
514 	    ctf_hash_size(&fp->ctf_names));
515 
516 	/*
517 	 * Make an additional pass through the pointer table to find pointers
518 	 * that point to anonymous typedef nodes.  If we find one, modify the
519 	 * pointer table so that the pointer is also known to point to the
520 	 * node that is referenced by the anonymous typedef node.
521 	 */
522 	for (id = 1; id <= fp->ctf_typemax; id++) {
523 		if ((dst = fp->ctf_ptrtab[id]) != 0) {
524 			tp = LCTF_INDEX_TO_TYPEPTR(fp, id);
525 
526 			if (LCTF_INFO_KIND(fp, tp->ctt_info) == CTF_K_TYPEDEF &&
527 			    strcmp(ctf_strptr(fp, tp->ctt_name), "") == 0 &&
528 			    CTF_TYPE_ISCHILD(tp->ctt_type) == child &&
529 			    CTF_TYPE_TO_INDEX(tp->ctt_type) <= fp->ctf_typemax)
530 				fp->ctf_ptrtab[
531 				    CTF_TYPE_TO_INDEX(tp->ctt_type)] = dst;
532 		}
533 	}
534 
535 	return (0);
536 }
537 
538 /*
539  * Decode the specified CTF buffer and optional symbol table and create a new
540  * CTF container representing the symbolic debugging information.  This code
541  * can be used directly by the debugger, or it can be used as the engine for
542  * ctf_fdopen() or ctf_open(), below.
543  */
544 ctf_file_t *
ctf_bufopen(const ctf_sect_t * ctfsect,const ctf_sect_t * symsect,const ctf_sect_t * strsect,int * errp)545 ctf_bufopen(const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
546     const ctf_sect_t *strsect, int *errp)
547 {
548 	const ctf_preamble_t *pp;
549 	ctf_header_t hp;
550 	ctf_file_t *fp;
551 	void *buf, *base;
552 	size_t size, hdrsz;
553 	int err;
554 	uint_t hflags;
555 
556 	if (ctfsect == NULL || ((symsect == NULL) != (strsect == NULL)))
557 		return (ctf_set_open_errno(errp, EINVAL));
558 
559 	if (symsect != NULL && symsect->cts_entsize != sizeof (Elf32_Sym) &&
560 	    symsect->cts_entsize != sizeof (Elf64_Sym))
561 		return (ctf_set_open_errno(errp, ECTF_SYMTAB));
562 
563 	if (symsect != NULL && symsect->cts_data == NULL)
564 		return (ctf_set_open_errno(errp, ECTF_SYMBAD));
565 
566 	if (strsect != NULL && strsect->cts_data == NULL)
567 		return (ctf_set_open_errno(errp, ECTF_STRBAD));
568 
569 	if (ctfsect->cts_size < sizeof (ctf_preamble_t))
570 		return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
571 
572 	pp = (const ctf_preamble_t *)ctfsect->cts_data;
573 
574 	ctf_dprintf("ctf_bufopen: magic=0x%x version=%u\n",
575 	    pp->ctp_magic, pp->ctp_version);
576 
577 	/*
578 	 * Validate each part of the CTF header (either V1 or V2).
579 	 * First, we validate the preamble (common to all versions).  At that
580 	 * point, we know specific header version, and can validate the
581 	 * version-specific parts including section offsets and alignments.
582 	 */
583 	if (pp->ctp_magic != CTF_MAGIC)
584 		return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
585 
586 	if (pp->ctp_version == CTF_VERSION_2) {
587 		if (ctfsect->cts_size < sizeof (ctf_header_t))
588 			return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
589 
590 		bcopy(ctfsect->cts_data, &hp, sizeof (hp));
591 		hdrsz = sizeof (ctf_header_t);
592 
593 	} else if (pp->ctp_version == CTF_VERSION_1) {
594 		const ctf_header_v1_t *h1p =
595 		    (const ctf_header_v1_t *)ctfsect->cts_data;
596 
597 		if (ctfsect->cts_size < sizeof (ctf_header_v1_t))
598 			return (ctf_set_open_errno(errp, ECTF_NOCTFBUF));
599 
600 		bzero(&hp, sizeof (hp));
601 		hp.cth_preamble = h1p->cth_preamble;
602 		hp.cth_objtoff = h1p->cth_objtoff;
603 		hp.cth_funcoff = h1p->cth_funcoff;
604 		hp.cth_typeoff = h1p->cth_typeoff;
605 		hp.cth_stroff = h1p->cth_stroff;
606 		hp.cth_strlen = h1p->cth_strlen;
607 
608 		hdrsz = sizeof (ctf_header_v1_t);
609 	} else
610 		return (ctf_set_open_errno(errp, ECTF_CTFVERS));
611 
612 	size = hp.cth_stroff + hp.cth_strlen;
613 
614 	ctf_dprintf("ctf_bufopen: uncompressed size=%lu\n", (ulong_t)size);
615 
616 	if (hp.cth_lbloff > size || hp.cth_objtoff > size ||
617 	    hp.cth_funcoff > size || hp.cth_typeoff > size ||
618 	    hp.cth_stroff > size)
619 		return (ctf_set_open_errno(errp, ECTF_CORRUPT));
620 
621 	if (hp.cth_lbloff > hp.cth_objtoff ||
622 	    hp.cth_objtoff > hp.cth_funcoff ||
623 	    hp.cth_funcoff > hp.cth_typeoff ||
624 	    hp.cth_typeoff > hp.cth_stroff)
625 		return (ctf_set_open_errno(errp, ECTF_CORRUPT));
626 
627 	if ((hp.cth_lbloff & 3) || (hp.cth_objtoff & 1) ||
628 	    (hp.cth_funcoff & 1) || (hp.cth_typeoff & 3))
629 		return (ctf_set_open_errno(errp, ECTF_CORRUPT));
630 
631 	/*
632 	 * Once everything is determined to be valid, attempt to decompress
633 	 * the CTF data buffer if it is compressed.  Otherwise we just put
634 	 * the data section's buffer pointer into ctf_buf, below.
635 	 */
636 	hflags = hp.cth_flags;
637 	if (hp.cth_flags & CTF_F_COMPRESS) {
638 		size_t srclen, dstlen;
639 		const void *src;
640 		int rc = Z_OK;
641 
642 		if (ctf_zopen(errp) == NULL)
643 			return (NULL); /* errp is set for us */
644 
645 		if ((base = ctf_data_alloc(size + hdrsz)) == MAP_FAILED)
646 			return (ctf_set_open_errno(errp, ECTF_ZALLOC));
647 
648 		bcopy(ctfsect->cts_data, base, hdrsz);
649 		((ctf_preamble_t *)base)->ctp_flags &= ~CTF_F_COMPRESS;
650 		buf = (uchar_t *)base + hdrsz;
651 
652 		src = (uchar_t *)ctfsect->cts_data + hdrsz;
653 		srclen = ctfsect->cts_size - hdrsz;
654 		dstlen = size;
655 
656 		if ((rc = z_uncompress(buf, &dstlen, src, srclen)) != Z_OK) {
657 			ctf_dprintf("zlib inflate err: %s\n", z_strerror(rc));
658 			ctf_data_free(base, size + hdrsz);
659 			return (ctf_set_open_errno(errp, ECTF_DECOMPRESS));
660 		}
661 
662 		if (dstlen != size) {
663 			ctf_dprintf("zlib inflate short -- got %lu of %lu "
664 			    "bytes\n", (ulong_t)dstlen, (ulong_t)size);
665 			ctf_data_free(base, size + hdrsz);
666 			return (ctf_set_open_errno(errp, ECTF_CORRUPT));
667 		}
668 
669 		ctf_data_protect(base, size + hdrsz);
670 
671 	} else {
672 		base = (void *)ctfsect->cts_data;
673 		buf = (uchar_t *)base + hdrsz;
674 	}
675 
676 	/*
677 	 * Once we have uncompressed and validated the CTF data buffer, we can
678 	 * proceed with allocating a ctf_file_t and initializing it.
679 	 */
680 	if ((fp = ctf_alloc(sizeof (ctf_file_t))) == NULL)
681 		return (ctf_set_open_errno(errp, EAGAIN));
682 
683 	bzero(fp, sizeof (ctf_file_t));
684 	fp->ctf_version = hp.cth_version;
685 	fp->ctf_fileops = &ctf_fileops[hp.cth_version];
686 	fp->ctf_hflags = hflags;
687 	bcopy(ctfsect, &fp->ctf_data, sizeof (ctf_sect_t));
688 
689 	if (symsect != NULL) {
690 		bcopy(symsect, &fp->ctf_symtab, sizeof (ctf_sect_t));
691 		bcopy(strsect, &fp->ctf_strtab, sizeof (ctf_sect_t));
692 	}
693 
694 	if (fp->ctf_data.cts_name != NULL)
695 		fp->ctf_data.cts_name = ctf_strdup(fp->ctf_data.cts_name);
696 	if (fp->ctf_symtab.cts_name != NULL)
697 		fp->ctf_symtab.cts_name = ctf_strdup(fp->ctf_symtab.cts_name);
698 	if (fp->ctf_strtab.cts_name != NULL)
699 		fp->ctf_strtab.cts_name = ctf_strdup(fp->ctf_strtab.cts_name);
700 
701 	if (fp->ctf_data.cts_name == NULL)
702 		fp->ctf_data.cts_name = _CTF_NULLSTR;
703 	if (fp->ctf_symtab.cts_name == NULL)
704 		fp->ctf_symtab.cts_name = _CTF_NULLSTR;
705 	if (fp->ctf_strtab.cts_name == NULL)
706 		fp->ctf_strtab.cts_name = _CTF_NULLSTR;
707 
708 	fp->ctf_str[CTF_STRTAB_0].cts_strs = (const char *)buf + hp.cth_stroff;
709 	fp->ctf_str[CTF_STRTAB_0].cts_len = hp.cth_strlen;
710 
711 	if (strsect != NULL) {
712 		fp->ctf_str[CTF_STRTAB_1].cts_strs = strsect->cts_data;
713 		fp->ctf_str[CTF_STRTAB_1].cts_len = strsect->cts_size;
714 	}
715 
716 	fp->ctf_base = base;
717 	fp->ctf_buf = buf;
718 	fp->ctf_size = size + hdrsz;
719 
720 	/*
721 	 * If we have a parent container name and label, store the relocated
722 	 * string pointers in the CTF container for easy access later.
723 	 */
724 	if (hp.cth_parlabel != 0)
725 		fp->ctf_parlabel = ctf_strptr(fp, hp.cth_parlabel);
726 	if (hp.cth_parname != 0)
727 		fp->ctf_parname = ctf_strptr(fp, hp.cth_parname);
728 
729 	ctf_dprintf("ctf_bufopen: parent name %s (label %s)\n",
730 	    fp->ctf_parname ? fp->ctf_parname : "<NULL>",
731 	    fp->ctf_parlabel ? fp->ctf_parlabel : "<NULL>");
732 
733 	/*
734 	 * If we have a symbol table section, allocate and initialize
735 	 * the symtab translation table, pointed to by ctf_sxlate.
736 	 */
737 	if (symsect != NULL) {
738 		fp->ctf_nsyms = symsect->cts_size / symsect->cts_entsize;
739 		fp->ctf_sxlate = ctf_alloc(fp->ctf_nsyms * sizeof (uint_t));
740 
741 		if (fp->ctf_sxlate == NULL) {
742 			(void) ctf_set_open_errno(errp, EAGAIN);
743 			goto bad;
744 		}
745 
746 		if ((err = init_symtab(fp, &hp, symsect, strsect)) != 0) {
747 			(void) ctf_set_open_errno(errp, err);
748 			goto bad;
749 		}
750 	}
751 
752 	if ((err = init_types(fp, &hp)) != 0) {
753 		(void) ctf_set_open_errno(errp, err);
754 		goto bad;
755 	}
756 
757 	/*
758 	 * Initialize the ctf_lookup_by_name top-level dictionary.  We keep an
759 	 * array of type name prefixes and the corresponding ctf_hash to use.
760 	 * NOTE: This code must be kept in sync with the code in ctf_update().
761 	 */
762 	fp->ctf_lookups[0].ctl_prefix = "struct";
763 	fp->ctf_lookups[0].ctl_len = strlen(fp->ctf_lookups[0].ctl_prefix);
764 	fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
765 	fp->ctf_lookups[1].ctl_prefix = "union";
766 	fp->ctf_lookups[1].ctl_len = strlen(fp->ctf_lookups[1].ctl_prefix);
767 	fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
768 	fp->ctf_lookups[2].ctl_prefix = "enum";
769 	fp->ctf_lookups[2].ctl_len = strlen(fp->ctf_lookups[2].ctl_prefix);
770 	fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
771 	fp->ctf_lookups[3].ctl_prefix = _CTF_NULLSTR;
772 	fp->ctf_lookups[3].ctl_len = strlen(fp->ctf_lookups[3].ctl_prefix);
773 	fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
774 	fp->ctf_lookups[4].ctl_prefix = NULL;
775 	fp->ctf_lookups[4].ctl_len = 0;
776 	fp->ctf_lookups[4].ctl_hash = NULL;
777 
778 	if (symsect != NULL) {
779 		if (symsect->cts_entsize == sizeof (Elf64_Sym))
780 			(void) ctf_setmodel(fp, CTF_MODEL_LP64);
781 		else
782 			(void) ctf_setmodel(fp, CTF_MODEL_ILP32);
783 	} else
784 		(void) ctf_setmodel(fp, CTF_MODEL_NATIVE);
785 
786 	fp->ctf_refcnt = 1;
787 	return (fp);
788 
789 bad:
790 	ctf_close(fp);
791 	return (NULL);
792 }
793 
794 /*
795  * Dupliate a ctf_file_t and its underlying section information into a new
796  * container. This works by copying the three ctf_sect_t's of the original
797  * container if they exist and passing those into ctf_bufopen. To copy those, we
798  * mmap anonymous memory with ctf_data_alloc and bcopy the data across. It's not
799  * the cheapest thing, but it's what we've got.
800  */
801 ctf_file_t *
ctf_dup(ctf_file_t * ofp)802 ctf_dup(ctf_file_t *ofp)
803 {
804 	ctf_file_t *fp;
805 	ctf_sect_t ctfsect, symsect, strsect;
806 	ctf_sect_t *ctp, *symp, *strp;
807 	void *cbuf, *symbuf, *strbuf;
808 	int err;
809 
810 	cbuf = symbuf = strbuf = NULL;
811 	/*
812 	 * The ctfsect isn't allowed to not exist, but the symbol and string
813 	 * section might not. We only need to copy the data of the section, not
814 	 * the name, as ctf_bufopen will take care of that.
815 	 */
816 	bcopy(&ofp->ctf_data, &ctfsect, sizeof (ctf_sect_t));
817 	cbuf = ctf_data_alloc(ctfsect.cts_size);
818 	if (cbuf == NULL) {
819 		(void) ctf_set_errno(ofp, ECTF_MMAP);
820 		return (NULL);
821 	}
822 
823 	bcopy(ctfsect.cts_data, cbuf, ctfsect.cts_size);
824 	ctf_data_protect(cbuf, ctfsect.cts_size);
825 	ctfsect.cts_data = cbuf;
826 	ctfsect.cts_offset = 0;
827 	ctp = &ctfsect;
828 
829 	if (ofp->ctf_symtab.cts_data != NULL) {
830 		bcopy(&ofp->ctf_symtab, &symsect, sizeof (ctf_sect_t));
831 		symbuf = ctf_data_alloc(symsect.cts_size);
832 		if (symbuf == NULL) {
833 			(void) ctf_set_errno(ofp, ECTF_MMAP);
834 			goto err;
835 		}
836 		bcopy(symsect.cts_data, symbuf, symsect.cts_size);
837 		ctf_data_protect(symbuf, symsect.cts_size);
838 		symsect.cts_data = symbuf;
839 		symsect.cts_offset = 0;
840 		symp = &symsect;
841 	} else {
842 		symp = NULL;
843 	}
844 
845 	if (ofp->ctf_strtab.cts_data != NULL) {
846 		bcopy(&ofp->ctf_strtab, &strsect, sizeof (ctf_sect_t));
847 		strbuf = ctf_data_alloc(strsect.cts_size);
848 		if (strbuf == NULL) {
849 			(void) ctf_set_errno(ofp, ECTF_MMAP);
850 			goto err;
851 		}
852 		bcopy(strsect.cts_data, strbuf, strsect.cts_size);
853 		ctf_data_protect(strbuf, strsect.cts_size);
854 		strsect.cts_data = strbuf;
855 		strsect.cts_offset = 0;
856 		strp = &strsect;
857 	} else {
858 		strp = NULL;
859 	}
860 
861 	fp = ctf_bufopen(ctp, symp, strp, &err);
862 	if (fp == NULL) {
863 		(void) ctf_set_errno(ofp, err);
864 		goto err;
865 	}
866 
867 	fp->ctf_flags |= LCTF_MMAP;
868 
869 	return (fp);
870 
871 err:
872 	ctf_data_free(cbuf, ctfsect.cts_size);
873 	if (symbuf != NULL)
874 		ctf_data_free(symbuf, symsect.cts_size);
875 	if (strbuf != NULL)
876 		ctf_data_free(strbuf, strsect.cts_size);
877 	return (NULL);
878 }
879 
880 /*
881  * Close the specified CTF container and free associated data structures.  Note
882  * that ctf_close() is a reference counted operation: if the specified file is
883  * the parent of other active containers, its reference count will be greater
884  * than one and it will be freed later when no active children exist.
885  */
886 void
ctf_close(ctf_file_t * fp)887 ctf_close(ctf_file_t *fp)
888 {
889 	ctf_dtdef_t *dtd, *ntd;
890 	ctf_dsdef_t *dsd, *nsd;
891 	ctf_dldef_t *dld, *nld;
892 
893 	if (fp == NULL)
894 		return; /* allow ctf_close(NULL) to simplify caller code */
895 
896 	ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt);
897 
898 	if (fp->ctf_refcnt > 1) {
899 		fp->ctf_refcnt--;
900 		return;
901 	}
902 
903 	if (fp->ctf_parent != NULL)
904 		ctf_close(fp->ctf_parent);
905 
906 	/*
907 	 * Note, to work properly with reference counting on the dynamic
908 	 * section, we must delete the list in reverse.
909 	 */
910 	for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
911 		ntd = ctf_list_prev(dtd);
912 		ctf_dtd_delete(fp, dtd);
913 	}
914 
915 	for (dsd = ctf_list_prev(&fp->ctf_dsdefs); dsd != NULL; dsd = nsd) {
916 		nsd = ctf_list_prev(dsd);
917 		ctf_dsd_delete(fp, dsd);
918 	}
919 
920 	for (dld = ctf_list_prev(&fp->ctf_dldefs); dld != NULL; dld = nld) {
921 		nld = ctf_list_prev(dld);
922 		ctf_dld_delete(fp, dld);
923 	}
924 
925 	ctf_free(fp->ctf_dthash, fp->ctf_dthashlen * sizeof (ctf_dtdef_t *));
926 
927 	if (fp->ctf_flags & LCTF_MMAP) {
928 		/*
929 		 * Writeable containers shouldn't necessairily have the CTF
930 		 * section freed.
931 		 */
932 		if (fp->ctf_data.cts_data != NULL &&
933 		    !(fp->ctf_flags & LCTF_RDWR))
934 			ctf_sect_munmap(&fp->ctf_data);
935 		if (fp->ctf_symtab.cts_data != NULL)
936 			ctf_sect_munmap(&fp->ctf_symtab);
937 		if (fp->ctf_strtab.cts_data != NULL)
938 			ctf_sect_munmap(&fp->ctf_strtab);
939 	}
940 	if (fp->ctf_flags & LCTF_FREE) {
941 		ctf_data_free((void *)fp->ctf_data.cts_data,
942 		    fp->ctf_data.cts_size);
943 	}
944 
945 	if (fp->ctf_data.cts_name != _CTF_NULLSTR &&
946 	    fp->ctf_data.cts_name != NULL) {
947 		ctf_free((char *)fp->ctf_data.cts_name,
948 		    strlen(fp->ctf_data.cts_name) + 1);
949 	}
950 
951 	if (fp->ctf_symtab.cts_name != _CTF_NULLSTR &&
952 	    fp->ctf_symtab.cts_name != NULL) {
953 		ctf_free((char *)fp->ctf_symtab.cts_name,
954 		    strlen(fp->ctf_symtab.cts_name) + 1);
955 	}
956 
957 	if (fp->ctf_strtab.cts_name != _CTF_NULLSTR &&
958 	    fp->ctf_strtab.cts_name != NULL) {
959 		ctf_free((char *)fp->ctf_strtab.cts_name,
960 		    strlen(fp->ctf_strtab.cts_name) + 1);
961 	}
962 
963 	if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL)
964 		ctf_data_free((void *)fp->ctf_base, fp->ctf_size);
965 
966 	if (fp->ctf_sxlate != NULL)
967 		ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms);
968 
969 	if (fp->ctf_txlate != NULL) {
970 		ctf_free(fp->ctf_txlate,
971 		    sizeof (uint_t) * (fp->ctf_typemax + 1));
972 	}
973 
974 	if (fp->ctf_ptrtab != NULL) {
975 		ctf_free(fp->ctf_ptrtab,
976 		    sizeof (ushort_t) * (fp->ctf_typemax + 1));
977 	}
978 
979 	ctf_hash_destroy(&fp->ctf_structs);
980 	ctf_hash_destroy(&fp->ctf_unions);
981 	ctf_hash_destroy(&fp->ctf_enums);
982 	ctf_hash_destroy(&fp->ctf_names);
983 
984 	ctf_free(fp, sizeof (ctf_file_t));
985 }
986 
987 /*
988  * Return the CTF handle for the parent CTF container, if one exists.
989  * Otherwise return NULL to indicate this container has no imported parent.
990  */
991 ctf_file_t *
ctf_parent_file(ctf_file_t * fp)992 ctf_parent_file(ctf_file_t *fp)
993 {
994 	return (fp->ctf_parent);
995 }
996 
997 /*
998  * Return the name of the parent CTF container, if one exists.  Otherwise
999  * return NULL to indicate this container is a root container.
1000  */
1001 const char *
ctf_parent_name(ctf_file_t * fp)1002 ctf_parent_name(ctf_file_t *fp)
1003 {
1004 	return (fp->ctf_parname);
1005 }
1006 
1007 /*
1008  * Return the label of the parent CTF container, if one exists. Otherwise return
1009  * NULL.
1010  */
1011 const char *
ctf_parent_label(ctf_file_t * fp)1012 ctf_parent_label(ctf_file_t *fp)
1013 {
1014 	return (fp->ctf_parlabel);
1015 }
1016 
1017 /*
1018  * Import the types from the specified parent container by storing a pointer
1019  * to it in ctf_parent and incrementing its reference count.  Only one parent
1020  * is allowed: if a parent already exists, it is replaced by the new parent.
1021  */
1022 int
ctf_import(ctf_file_t * fp,ctf_file_t * pfp)1023 ctf_import(ctf_file_t *fp, ctf_file_t *pfp)
1024 {
1025 	if (fp == NULL || fp == pfp || (pfp != NULL && pfp->ctf_refcnt == 0))
1026 		return (ctf_set_errno(fp, EINVAL));
1027 
1028 	if (pfp != NULL && pfp->ctf_dmodel != fp->ctf_dmodel)
1029 		return (ctf_set_errno(fp, ECTF_DMODEL));
1030 
1031 	if (fp->ctf_parent != NULL)
1032 		ctf_close(fp->ctf_parent);
1033 
1034 	if (pfp != NULL) {
1035 		fp->ctf_flags |= LCTF_CHILD;
1036 		pfp->ctf_refcnt++;
1037 	}
1038 
1039 	fp->ctf_parent = pfp;
1040 	return (0);
1041 }
1042 
1043 /*
1044  * Set the data model constant for the CTF container.
1045  */
1046 int
ctf_setmodel(ctf_file_t * fp,int model)1047 ctf_setmodel(ctf_file_t *fp, int model)
1048 {
1049 	const ctf_dmodel_t *dp;
1050 
1051 	for (dp = _libctf_models; dp->ctd_name != NULL; dp++) {
1052 		if (dp->ctd_code == model) {
1053 			fp->ctf_dmodel = dp;
1054 			return (0);
1055 		}
1056 	}
1057 
1058 	return (ctf_set_errno(fp, EINVAL));
1059 }
1060 
1061 /*
1062  * Return the data model constant for the CTF container.
1063  */
1064 int
ctf_getmodel(ctf_file_t * fp)1065 ctf_getmodel(ctf_file_t *fp)
1066 {
1067 	return (fp->ctf_dmodel->ctd_code);
1068 }
1069 
1070 void
ctf_setspecific(ctf_file_t * fp,void * data)1071 ctf_setspecific(ctf_file_t *fp, void *data)
1072 {
1073 	fp->ctf_specific = data;
1074 }
1075 
1076 void *
ctf_getspecific(ctf_file_t * fp)1077 ctf_getspecific(ctf_file_t *fp)
1078 {
1079 	return (fp->ctf_specific);
1080 }
1081 
1082 uint_t
ctf_flags(ctf_file_t * fp)1083 ctf_flags(ctf_file_t *fp)
1084 {
1085 	return (fp->ctf_hflags);
1086 }
1087