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