xref: /netbsd-src/external/cddl/osnet/dist/common/ctf/ctf_create.c (revision 0aab751630a599b3ffb4012ed2857c34b16f979d)
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 #ifdef HAVE_NBTOOL_CONFIG_H
23 #include "nbtool_config.h"
24 #endif
25 
26 /*
27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
32  */
33 
34 #include <sys/sysmacros.h>
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <ctf_impl.h>
38 #include <sys/debug.h>
39 
40 /*
41  * This static string is used as the template for initially populating a
42  * dynamic container's string table.  We always store \0 in the first byte,
43  * and we use the generic string "PARENT" to mark this container's parent
44  * if one is associated with the container using ctf_import().
45  */
46 static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
47 
48 /*
49  * To create an empty CTF container, we just declare a zeroed header and call
50  * ctf_bufopen() on it.  If ctf_bufopen succeeds, we mark the new container r/w
51  * and initialize the dynamic members.  We set dtstrlen to 1 to reserve the
52  * first byte of the string table for a \0 byte, and we start assigning type
53  * IDs at 1 because type ID 0 is used as a sentinel.
54  */
55 ctf_file_t *
ctf_create(int * errp)56 ctf_create(int *errp)
57 {
58 	static const ctf_header_t hdr = { .cth_preamble = {
59 		.ctp_magic = CTF_MAGIC,
60 		.ctp_version = CTF_VERSION,
61 		.ctp_flags = 0
62 	} };
63 
64 	const ulong_t hashlen = 128;
65 	ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));
66 	ctf_sect_t cts;
67 	ctf_file_t *fp;
68 
69 	if (hash == NULL)
70 		return (ctf_set_open_errno(errp, EAGAIN));
71 
72 	cts.cts_name = __UNCONST(_CTF_SECTION);
73 	cts.cts_type = SHT_PROGBITS;
74 	cts.cts_flags = 0;
75 	cts.cts_data = __UNCONST(&hdr);
76 	cts.cts_size = sizeof (hdr);
77 	cts.cts_entsize = 1;
78 	cts.cts_offset = 0;
79 
80 	if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) {
81 		ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));
82 		return (NULL);
83 	}
84 
85 	fp->ctf_flags |= LCTF_RDWR;
86 	fp->ctf_dthashlen = hashlen;
87 	bzero(hash, hashlen * sizeof (ctf_dtdef_t *));
88 	fp->ctf_dthash = hash;
89 	fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);
90 	fp->ctf_dtnextid = 1;
91 	fp->ctf_dtoldid = 0;
92 
93 	return (fp);
94 }
95 
96 static uchar_t *
ctf_copy_smembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)97 ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
98 {
99 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
100 	ctf_member_t ctm;
101 
102 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
103 		if (dmd->dmd_name) {
104 			ctm.ctm_name = soff;
105 			soff += strlen(dmd->dmd_name) + 1;
106 		} else
107 			ctm.ctm_name = 0;
108 
109 		ctm.ctm_type = (ushort_t)dmd->dmd_type;
110 		ctm.ctm_offset = (ushort_t)dmd->dmd_offset;
111 
112 		bcopy(&ctm, t, sizeof (ctm));
113 		t += sizeof (ctm);
114 	}
115 
116 	return (t);
117 }
118 
119 static uchar_t *
ctf_copy_lmembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)120 ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
121 {
122 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
123 	ctf_lmember_t ctlm;
124 
125 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
126 		if (dmd->dmd_name) {
127 			ctlm.ctlm_name = soff;
128 			soff += strlen(dmd->dmd_name) + 1;
129 		} else
130 			ctlm.ctlm_name = 0;
131 
132 		ctlm.ctlm_type = (ushort_t)dmd->dmd_type;
133 		ctlm.ctlm_pad = 0;
134 		ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);
135 		ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);
136 
137 		bcopy(&ctlm, t, sizeof (ctlm));
138 		t += sizeof (ctlm);
139 	}
140 
141 	return (t);
142 }
143 
144 static uchar_t *
ctf_copy_emembers(ctf_dtdef_t * dtd,uint_t soff,uchar_t * t)145 ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)
146 {
147 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
148 	ctf_enum_t cte;
149 
150 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
151 		cte.cte_name = soff;
152 		cte.cte_value = dmd->dmd_value;
153 		soff += strlen(dmd->dmd_name) + 1;
154 		bcopy(&cte, t, sizeof (cte));
155 		t += sizeof (cte);
156 	}
157 
158 	return (t);
159 }
160 
161 static uchar_t *
ctf_copy_membnames(ctf_dtdef_t * dtd,uchar_t * s)162 ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)
163 {
164 	ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
165 	size_t len;
166 
167 	for (; dmd != NULL; dmd = ctf_list_next(dmd)) {
168 		if (dmd->dmd_name == NULL)
169 			continue; /* skip anonymous members */
170 		len = strlen(dmd->dmd_name) + 1;
171 		bcopy(dmd->dmd_name, s, len);
172 		s += len;
173 	}
174 
175 	return (s);
176 }
177 
178 /*
179  * Only types of dyanmic CTF containers contain reference counts. These
180  * containers are marked RD/WR. Because of that we basically make this a no-op
181  * for compatability with non-dynamic CTF sections. This is also a no-op for
182  * types which are not dynamic types. It is the responsibility of the caller to
183  * make sure it is a valid type. We help that caller out on debug builds.
184  *
185  * Note that the reference counts are not maintained for types that are not
186  * within this container. In other words if we have a type in a parent, that
187  * will not have its reference count increased. On the flip side, the parent
188  * will not be allowed to remove dynamic types if it has children.
189  */
190 static void
ctf_ref_inc(ctf_file_t * fp,ctf_id_t tid)191 ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid)
192 {
193 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
194 
195 	if (dtd == NULL)
196 		return;
197 
198 	if (!(fp->ctf_flags & LCTF_RDWR))
199 		return;
200 
201 	dtd->dtd_ref++;
202 }
203 
204 /*
205  * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the
206  * caller should ensure that this is already a valid type.
207  */
208 static void
ctf_ref_dec(ctf_file_t * fp,ctf_id_t tid)209 ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid)
210 {
211 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);
212 
213 	if (dtd == NULL)
214 		return;
215 
216 	if (!(fp->ctf_flags & LCTF_RDWR))
217 		return;
218 
219 	ASSERT(dtd->dtd_ref >= 1);
220 	dtd->dtd_ref--;
221 }
222 
223 /*
224  * If the specified CTF container is writable and has been modified, reload
225  * this container with the updated type definitions.  In order to make this
226  * code and the rest of libctf as simple as possible, we perform updates by
227  * taking the dynamic type definitions and creating an in-memory CTF file
228  * containing the definitions, and then call ctf_bufopen() on it.  This not
229  * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
230  * of the library code with different lookup paths for static and dynamic
231  * type definitions.  We are therefore optimizing greatly for lookup over
232  * update, which we assume will be an uncommon operation.  We perform one
233  * extra trick here for the benefit of callers and to keep our code simple:
234  * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
235  * constant for the caller, so after ctf_bufopen() returns, we use bcopy to
236  * swap the interior of the old and new ctf_file_t's, and then free the old.
237  *
238  * Note that the lists of dynamic types stays around and the resulting container
239  * is still writeable. Furthermore, the reference counts that are on the dtd's
240  * are still valid.
241  */
242 int
ctf_update(ctf_file_t * fp)243 ctf_update(ctf_file_t *fp)
244 {
245 	ctf_file_t ofp, *nfp;
246 	ctf_header_t hdr;
247 	ctf_dtdef_t *dtd;
248 	ctf_sect_t cts;
249 
250 	uchar_t *s, *s0, *t;
251 	size_t size;
252 	void *buf;
253 	int err;
254 
255 	if (!(fp->ctf_flags & LCTF_RDWR))
256 		return (ctf_set_errno(fp, ECTF_RDONLY));
257 
258 	if (!(fp->ctf_flags & LCTF_DIRTY))
259 		return (0); /* no update required */
260 
261 	/*
262 	 * Fill in an initial CTF header.  We will leave the label, object,
263 	 * and function sections empty and only output a header, type section,
264 	 * and string table.  The type section begins at a 4-byte aligned
265 	 * boundary past the CTF header itself (at relative offset zero).
266 	 */
267 	bzero(&hdr, sizeof (hdr));
268 	hdr.cth_magic = CTF_MAGIC;
269 	hdr.cth_version = CTF_VERSION;
270 
271 	if (fp->ctf_flags & LCTF_CHILD)
272 		hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */
273 
274 	/*
275 	 * Iterate through the dynamic type definition list and compute the
276 	 * size of the CTF type section we will need to generate.
277 	 */
278 	for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
279 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
280 
281 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
282 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
283 
284 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
285 			size += sizeof (ctf_stype_t);
286 		else
287 			size += sizeof (ctf_type_t);
288 
289 		switch (kind) {
290 		case CTF_K_INTEGER:
291 		case CTF_K_FLOAT:
292 			size += sizeof (uint_t);
293 			break;
294 		case CTF_K_ARRAY:
295 			size += sizeof (ctf_array_t);
296 			break;
297 		case CTF_K_FUNCTION:
298 			size += sizeof (ushort_t) * (vlen + (vlen & 1));
299 			break;
300 		case CTF_K_STRUCT:
301 		case CTF_K_UNION:
302 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
303 				size += sizeof (ctf_member_t) * vlen;
304 			else
305 				size += sizeof (ctf_lmember_t) * vlen;
306 			break;
307 		case CTF_K_ENUM:
308 			size += sizeof (ctf_enum_t) * vlen;
309 			break;
310 		}
311 	}
312 
313 	/*
314 	 * Fill in the string table offset and size, compute the size of the
315 	 * entire CTF buffer we need, and then allocate a new buffer and
316 	 * bcopy the finished header to the start of the buffer.
317 	 */
318 	hdr.cth_stroff = hdr.cth_typeoff + size;
319 	hdr.cth_strlen = fp->ctf_dtstrlen;
320 	size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
321 
322 	if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
323 		return (ctf_set_errno(fp, EAGAIN));
324 
325 	bcopy(&hdr, buf, sizeof (ctf_header_t));
326 	t = (uchar_t *)buf + sizeof (ctf_header_t);
327 	s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;
328 
329 	bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
330 	s += sizeof (_CTF_STRTAB_TEMPLATE);
331 
332 	/*
333 	 * We now take a final lap through the dynamic type definition list and
334 	 * copy the appropriate type records and strings to the output buffer.
335 	 */
336 	for (dtd = ctf_list_next(&fp->ctf_dtdefs);
337 	    dtd != NULL; dtd = ctf_list_next(dtd)) {
338 
339 		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
340 		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
341 
342 		ctf_array_t cta;
343 		uint_t encoding;
344 		size_t len;
345 
346 		if (dtd->dtd_name != NULL) {
347 			dtd->dtd_data.ctt_name = (uint_t)(s - s0);
348 			len = strlen(dtd->dtd_name) + 1;
349 			bcopy(dtd->dtd_name, s, len);
350 			s += len;
351 		} else
352 			dtd->dtd_data.ctt_name = 0;
353 
354 		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
355 			len = sizeof (ctf_stype_t);
356 		else
357 			len = sizeof (ctf_type_t);
358 
359 		bcopy(&dtd->dtd_data, t, len);
360 		t += len;
361 
362 		switch (kind) {
363 		case CTF_K_INTEGER:
364 		case CTF_K_FLOAT:
365 			if (kind == CTF_K_INTEGER) {
366 				encoding = CTF_INT_DATA(
367 				    dtd->dtd_u.dtu_enc.cte_format,
368 				    dtd->dtd_u.dtu_enc.cte_offset,
369 				    dtd->dtd_u.dtu_enc.cte_bits);
370 			} else {
371 				encoding = CTF_FP_DATA(
372 				    dtd->dtd_u.dtu_enc.cte_format,
373 				    dtd->dtd_u.dtu_enc.cte_offset,
374 				    dtd->dtd_u.dtu_enc.cte_bits);
375 			}
376 			bcopy(&encoding, t, sizeof (encoding));
377 			t += sizeof (encoding);
378 			break;
379 
380 		case CTF_K_ARRAY:
381 			cta.cta_contents = (ushort_t)
382 			    dtd->dtd_u.dtu_arr.ctr_contents;
383 			cta.cta_index = (ushort_t)
384 			    dtd->dtd_u.dtu_arr.ctr_index;
385 			cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
386 			bcopy(&cta, t, sizeof (cta));
387 			t += sizeof (cta);
388 			break;
389 
390 		case CTF_K_FUNCTION: {
391 			ushort_t *argv = (ushort_t *)(uintptr_t)t;
392 			uint_t argc;
393 
394 			for (argc = 0; argc < vlen; argc++)
395 				*argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc];
396 
397 			if (vlen & 1)
398 				*argv++ = 0; /* pad to 4-byte boundary */
399 
400 			t = (uchar_t *)argv;
401 			break;
402 		}
403 
404 		case CTF_K_STRUCT:
405 		case CTF_K_UNION:
406 			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
407 				t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t);
408 			else
409 				t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t);
410 			s = ctf_copy_membnames(dtd, s);
411 			break;
412 
413 		case CTF_K_ENUM:
414 			t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
415 			s = ctf_copy_membnames(dtd, s);
416 			break;
417 		}
418 	}
419 
420 	/*
421 	 * Finally, we are ready to ctf_bufopen() the new container.  If this
422 	 * is successful, we then switch nfp and fp and free the old container.
423 	 */
424 	ctf_data_protect(buf, size);
425 	cts.cts_name = _CTF_SECTION;
426 	cts.cts_type = SHT_PROGBITS;
427 	cts.cts_flags = 0;
428 	cts.cts_data = buf;
429 	cts.cts_size = size;
430 	cts.cts_entsize = 1;
431 	cts.cts_offset = 0;
432 
433 	if ((nfp = ctf_bufopen(&cts, NULL, NULL, &err)) == NULL) {
434 		ctf_data_free(buf, size);
435 		return (ctf_set_errno(fp, err));
436 	}
437 
438 	(void) ctf_setmodel(nfp, ctf_getmodel(fp));
439 	(void) ctf_import(nfp, fp->ctf_parent);
440 
441 	nfp->ctf_refcnt = fp->ctf_refcnt;
442 	nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
443 	nfp->ctf_data.cts_data = NULL; /* force ctf_data_free() on close */
444 	nfp->ctf_dthash = fp->ctf_dthash;
445 	nfp->ctf_dthashlen = fp->ctf_dthashlen;
446 	nfp->ctf_dtdefs = fp->ctf_dtdefs;
447 	nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
448 	nfp->ctf_dtnextid = fp->ctf_dtnextid;
449 	nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
450 	nfp->ctf_specific = fp->ctf_specific;
451 
452 	fp->ctf_dthash = NULL;
453 	fp->ctf_dthashlen = 0;
454 	bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));
455 
456 	bcopy(fp, &ofp, sizeof (ctf_file_t));
457 	bcopy(nfp, fp, sizeof (ctf_file_t));
458 	bcopy(&ofp, nfp, sizeof (ctf_file_t));
459 
460 	/*
461 	 * Initialize the ctf_lookup_by_name top-level dictionary.  We keep an
462 	 * array of type name prefixes and the corresponding ctf_hash to use.
463 	 * NOTE: This code must be kept in sync with the code in ctf_bufopen().
464 	 */
465 	fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
466 	fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
467 	fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
468 	fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;
469 
470 	nfp->ctf_refcnt = 1; /* force nfp to be freed */
471 	ctf_close(nfp);
472 
473 	return (0);
474 }
475 
476 void
ctf_dtd_insert(ctf_file_t * fp,ctf_dtdef_t * dtd)477 ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)
478 {
479 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
480 
481 	dtd->dtd_hash = fp->ctf_dthash[h];
482 	fp->ctf_dthash[h] = dtd;
483 	ctf_list_append(&fp->ctf_dtdefs, dtd);
484 }
485 
486 void
ctf_dtd_delete(ctf_file_t * fp,ctf_dtdef_t * dtd)487 ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
488 {
489 	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
490 	ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
491 	ctf_dmdef_t *dmd, *nmd;
492 	size_t len;
493 	int kind, i;
494 
495 	for (p = *q; p != NULL; p = p->dtd_hash) {
496 		if (p != dtd)
497 			q = &p->dtd_hash;
498 		else
499 			break;
500 	}
501 
502 	if (p != NULL)
503 		*q = p->dtd_hash;
504 
505 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
506 	switch (kind) {
507 	case CTF_K_STRUCT:
508 	case CTF_K_UNION:
509 	case CTF_K_ENUM:
510 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
511 		    dmd != NULL; dmd = nmd) {
512 			if (dmd->dmd_name != NULL) {
513 				len = strlen(dmd->dmd_name) + 1;
514 				ctf_free(dmd->dmd_name, len);
515 				fp->ctf_dtstrlen -= len;
516 			}
517 			if (kind != CTF_K_ENUM)
518 				ctf_ref_dec(fp, dmd->dmd_type);
519 			nmd = ctf_list_next(dmd);
520 			ctf_free(dmd, sizeof (ctf_dmdef_t));
521 		}
522 		break;
523 	case CTF_K_FUNCTION:
524 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
525 		for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++)
526 			if (dtd->dtd_u.dtu_argv[i] != 0)
527 				ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);
528 		ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
529 		    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
530 		break;
531 	case CTF_K_ARRAY:
532 		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
533 		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
534 		break;
535 	case CTF_K_TYPEDEF:
536 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
537 		break;
538 	case CTF_K_POINTER:
539 	case CTF_K_VOLATILE:
540 	case CTF_K_CONST:
541 	case CTF_K_RESTRICT:
542 		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
543 		break;
544 	}
545 
546 	if (dtd->dtd_name) {
547 		len = strlen(dtd->dtd_name) + 1;
548 		ctf_free(dtd->dtd_name, len);
549 		fp->ctf_dtstrlen -= len;
550 	}
551 
552 	ctf_list_delete(&fp->ctf_dtdefs, dtd);
553 	ctf_free(dtd, sizeof (ctf_dtdef_t));
554 }
555 
556 ctf_dtdef_t *
ctf_dtd_lookup(ctf_file_t * fp,ctf_id_t type)557 ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)
558 {
559 	ulong_t h = type & (fp->ctf_dthashlen - 1);
560 	ctf_dtdef_t *dtd;
561 
562 	if (fp->ctf_dthash == NULL)
563 		return (NULL);
564 
565 	for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {
566 		if (dtd->dtd_type == type)
567 			break;
568 	}
569 
570 	return (dtd);
571 }
572 
573 /*
574  * Discard all of the dynamic type definitions that have been added to the
575  * container since the last call to ctf_update().  We locate such types by
576  * scanning the list and deleting elements that have type IDs greater than
577  * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly
578  * with our reference counting schemes, we must delete the dynamic list in
579  * reverse.
580  */
581 int
ctf_discard(ctf_file_t * fp)582 ctf_discard(ctf_file_t *fp)
583 {
584 	ctf_dtdef_t *dtd, *ntd;
585 
586 	if (!(fp->ctf_flags & LCTF_RDWR))
587 		return (ctf_set_errno(fp, ECTF_RDONLY));
588 
589 	if (!(fp->ctf_flags & LCTF_DIRTY))
590 		return (0); /* no update required */
591 
592 	for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
593 		ntd = ctf_list_prev(dtd);
594 		if (CTF_TYPE_TO_INDEX(dtd->dtd_type) <= fp->ctf_dtoldid)
595 			continue; /* skip types that have been committed */
596 
597 		ctf_dtd_delete(fp, dtd);
598 	}
599 
600 	fp->ctf_dtnextid = fp->ctf_dtoldid + 1;
601 	fp->ctf_flags &= ~LCTF_DIRTY;
602 
603 	return (0);
604 }
605 
606 static ctf_id_t
ctf_add_generic(ctf_file_t * fp,uint_t flag,const char * name,ctf_dtdef_t ** rp)607 ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)
608 {
609 	ctf_dtdef_t *dtd;
610 	ctf_id_t type;
611 	char *s = NULL;
612 
613 	if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
614 		return (ctf_set_errno(fp, EINVAL));
615 
616 	if (!(fp->ctf_flags & LCTF_RDWR))
617 		return (ctf_set_errno(fp, ECTF_RDONLY));
618 
619 	if (CTF_TYPE_ISCHILD(fp->ctf_dtnextid) ||
620 	    CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE) {
621 		ctf_dprintf("type id overflow %lu\n", fp->ctf_dtnextid);
622 		return (ctf_set_errno(fp, ECTF_FULL));
623 	}
624 
625 	if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)
626 		return (ctf_set_errno(fp, EAGAIN));
627 
628 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
629 		ctf_free(dtd, sizeof (ctf_dtdef_t));
630 		return (ctf_set_errno(fp, EAGAIN));
631 	}
632 
633 	type = fp->ctf_dtnextid++;
634 	type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD));
635 
636 	bzero(dtd, sizeof (ctf_dtdef_t));
637 	dtd->dtd_name = s;
638 	dtd->dtd_type = type;
639 
640 	if (s != NULL)
641 		fp->ctf_dtstrlen += strlen(s) + 1;
642 
643 	ctf_dtd_insert(fp, dtd);
644 	fp->ctf_flags |= LCTF_DIRTY;
645 
646 	*rp = dtd;
647 	return (type);
648 }
649 
650 /*
651  * When encoding integer sizes, we want to convert a byte count in the range
652  * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc).  The clp2() function
653  * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
654  */
655 static size_t
clp2(size_t x)656 clp2(size_t x)
657 {
658 	x--;
659 
660 	x |= (x >> 1);
661 	x |= (x >> 2);
662 	x |= (x >> 4);
663 	x |= (x >> 8);
664 	x |= (x >> 16);
665 
666 	return (x + 1);
667 }
668 
669 static ctf_id_t
ctf_add_encoded(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep,uint_t kind)670 ctf_add_encoded(ctf_file_t *fp, uint_t flag,
671     const char *name, const ctf_encoding_t *ep, uint_t kind)
672 {
673 	ctf_dtdef_t *dtd;
674 	ctf_id_t type;
675 
676 	if (ep == NULL)
677 		return (ctf_set_errno(fp, EINVAL));
678 
679 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
680 		return (CTF_ERR); /* errno is set for us */
681 
682 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
683 	dtd->dtd_data.ctt_size = clp2(P2ROUNDUP(ep->cte_bits, NBBY) / NBBY);
684 	dtd->dtd_u.dtu_enc = *ep;
685 
686 	return (type);
687 }
688 
689 static ctf_id_t
ctf_add_reftype(ctf_file_t * fp,uint_t flag,ctf_id_t ref,uint_t kind)690 ctf_add_reftype(ctf_file_t *fp, uint_t flag, ctf_id_t ref, uint_t kind)
691 {
692 	ctf_dtdef_t *dtd;
693 	ctf_id_t type;
694 
695 	if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE)
696 		return (ctf_set_errno(fp, EINVAL));
697 
698 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
699 		return (CTF_ERR); /* errno is set for us */
700 
701 	ctf_ref_inc(fp, ref);
702 
703 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0);
704 	dtd->dtd_data.ctt_type = (ushort_t)ref;
705 
706 	return (type);
707 }
708 
709 ctf_id_t
ctf_add_integer(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)710 ctf_add_integer(ctf_file_t *fp, uint_t flag,
711     const char *name, const ctf_encoding_t *ep)
712 {
713 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));
714 }
715 
716 ctf_id_t
ctf_add_float(ctf_file_t * fp,uint_t flag,const char * name,const ctf_encoding_t * ep)717 ctf_add_float(ctf_file_t *fp, uint_t flag,
718     const char *name, const ctf_encoding_t *ep)
719 {
720 	return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));
721 }
722 
723 ctf_id_t
ctf_add_pointer(ctf_file_t * fp,uint_t flag,ctf_id_t ref)724 ctf_add_pointer(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
725 {
726 	return (ctf_add_reftype(fp, flag, ref, CTF_K_POINTER));
727 }
728 
729 ctf_id_t
ctf_add_array(ctf_file_t * fp,uint_t flag,const ctf_arinfo_t * arp)730 ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)
731 {
732 	ctf_dtdef_t *dtd;
733 	ctf_id_t type;
734 	ctf_file_t *fpd;
735 
736 	if (arp == NULL)
737 		return (ctf_set_errno(fp, EINVAL));
738 
739 	fpd = fp;
740 	if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
741 	    ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
742 		return (ctf_set_errno(fp, ECTF_BADID));
743 
744 	fpd = fp;
745 	if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
746 	    ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
747 		return (ctf_set_errno(fp, ECTF_BADID));
748 
749 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)
750 		return (CTF_ERR); /* errno is set for us */
751 
752 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0);
753 	dtd->dtd_data.ctt_size = 0;
754 	dtd->dtd_u.dtu_arr = *arp;
755 	ctf_ref_inc(fp, arp->ctr_contents);
756 	ctf_ref_inc(fp, arp->ctr_index);
757 
758 	return (type);
759 }
760 
761 int
ctf_set_array(ctf_file_t * fp,ctf_id_t type,const ctf_arinfo_t * arp)762 ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)
763 {
764 	ctf_file_t *fpd;
765 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
766 
767 	if (!(fp->ctf_flags & LCTF_RDWR))
768 		return (ctf_set_errno(fp, ECTF_RDONLY));
769 
770 	if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY)
771 		return (ctf_set_errno(fp, ECTF_BADID));
772 
773 	fpd = fp;
774 	if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&
775 	    ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)
776 		return (ctf_set_errno(fp, ECTF_BADID));
777 
778 	fpd = fp;
779 	if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&
780 	    ctf_dtd_lookup(fp, arp->ctr_index) == NULL)
781 		return (ctf_set_errno(fp, ECTF_BADID));
782 
783 	ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
784 	ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
785 	fp->ctf_flags |= LCTF_DIRTY;
786 	dtd->dtd_u.dtu_arr = *arp;
787 	ctf_ref_inc(fp, arp->ctr_contents);
788 	ctf_ref_inc(fp, arp->ctr_index);
789 
790 	return (0);
791 }
792 
793 ctf_id_t
ctf_add_function(ctf_file_t * fp,uint_t flag,const ctf_funcinfo_t * ctc,const ctf_id_t * argv)794 ctf_add_function(ctf_file_t *fp, uint_t flag,
795     const ctf_funcinfo_t *ctc, const ctf_id_t *argv)
796 {
797 	ctf_dtdef_t *dtd;
798 	ctf_id_t type;
799 	uint_t vlen;
800 	int i;
801 	ctf_id_t *vdat = NULL;
802 	ctf_file_t *fpd;
803 
804 	if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||
805 	    (ctc->ctc_argc != 0 && argv == NULL))
806 		return (ctf_set_errno(fp, EINVAL));
807 
808 	vlen = ctc->ctc_argc;
809 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
810 		vlen++; /* add trailing zero to indicate varargs (see below) */
811 
812 	if (vlen > CTF_MAX_VLEN)
813 		return (ctf_set_errno(fp, EOVERFLOW));
814 
815 	fpd = fp;
816 	if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL &&
817 	    ctf_dtd_lookup(fp, ctc->ctc_return) == NULL)
818 		return (ctf_set_errno(fp, ECTF_BADID));
819 
820 	for (i = 0; i < ctc->ctc_argc; i++) {
821 		fpd = fp;
822 		if (ctf_lookup_by_id(&fpd, argv[i]) == NULL &&
823 		    ctf_dtd_lookup(fp, argv[i]) == NULL)
824 			return (ctf_set_errno(fp, ECTF_BADID));
825 	}
826 
827 	if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)
828 		return (ctf_set_errno(fp, EAGAIN));
829 
830 	if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {
831 		ctf_free(vdat, sizeof (ctf_id_t) * vlen);
832 		return (CTF_ERR); /* errno is set for us */
833 	}
834 
835 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen);
836 	dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return;
837 
838 	ctf_ref_inc(fp, ctc->ctc_return);
839 	for (i = 0; i < ctc->ctc_argc; i++)
840 		ctf_ref_inc(fp, argv[i]);
841 
842 	bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);
843 	if (ctc->ctc_flags & CTF_FUNC_VARARG)
844 		vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */
845 	dtd->dtd_u.dtu_argv = vdat;
846 
847 	return (type);
848 }
849 
850 ctf_id_t
ctf_add_struct(ctf_file_t * fp,uint_t flag,const char * name)851 ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)
852 {
853 	ctf_hash_t *hp = &fp->ctf_structs;
854 	ctf_helem_t *hep = NULL;
855 	ctf_dtdef_t *dtd;
856 	ctf_id_t type;
857 
858 	if (name != NULL)
859 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
860 
861 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
862 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
863 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
864 		return (CTF_ERR); /* errno is set for us */
865 
866 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0);
867 	dtd->dtd_data.ctt_size = 0;
868 
869 	return (type);
870 }
871 
872 ctf_id_t
ctf_add_union(ctf_file_t * fp,uint_t flag,const char * name)873 ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)
874 {
875 	ctf_hash_t *hp = &fp->ctf_unions;
876 	ctf_helem_t *hep = NULL;
877 	ctf_dtdef_t *dtd;
878 	ctf_id_t type;
879 
880 	if (name != NULL)
881 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
882 
883 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
884 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
885 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
886 		return (CTF_ERR); /* errno is set for us */
887 
888 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0);
889 	dtd->dtd_data.ctt_size = 0;
890 
891 	return (type);
892 }
893 
894 ctf_id_t
ctf_add_enum(ctf_file_t * fp,uint_t flag,const char * name)895 ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
896 {
897 	ctf_hash_t *hp = &fp->ctf_enums;
898 	ctf_helem_t *hep = NULL;
899 	ctf_dtdef_t *dtd;
900 	ctf_id_t type;
901 
902 	if (name != NULL)
903 		hep = ctf_hash_lookup(hp, fp, name, strlen(name));
904 
905 	if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)
906 		dtd = ctf_dtd_lookup(fp, type = hep->h_type);
907 	else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
908 		return (CTF_ERR); /* errno is set for us */
909 
910 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
911 	dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
912 
913 	return (type);
914 }
915 
916 ctf_id_t
ctf_add_forward(ctf_file_t * fp,uint_t flag,const char * name,uint_t kind)917 ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
918 {
919 	ctf_hash_t *hp;
920 	ctf_helem_t *hep;
921 	ctf_dtdef_t *dtd;
922 	ctf_id_t type;
923 
924 	switch (kind) {
925 	case CTF_K_STRUCT:
926 		hp = &fp->ctf_structs;
927 		break;
928 	case CTF_K_UNION:
929 		hp = &fp->ctf_unions;
930 		break;
931 	case CTF_K_ENUM:
932 		hp = &fp->ctf_enums;
933 		break;
934 	default:
935 		return (ctf_set_errno(fp, ECTF_NOTSUE));
936 	}
937 
938 	/*
939 	 * If the type is already defined or exists as a forward tag, just
940 	 * return the ctf_id_t of the existing definition.
941 	 */
942 	if (name != NULL && (hep = ctf_hash_lookup(hp,
943 	    fp, name, strlen(name))) != NULL)
944 		return (hep->h_type);
945 
946 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
947 		return (CTF_ERR); /* errno is set for us */
948 
949 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
950 	dtd->dtd_data.ctt_type = kind;
951 
952 	return (type);
953 }
954 
955 ctf_id_t
ctf_add_typedef(ctf_file_t * fp,uint_t flag,const char * name,ctf_id_t ref)956 ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
957 {
958 	ctf_dtdef_t *dtd;
959 	ctf_id_t type;
960 	ctf_file_t *fpd;
961 
962 	fpd = fp;
963 	if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
964 	    ctf_dtd_lookup(fp, ref) == NULL))
965 		return (ctf_set_errno(fp, EINVAL));
966 
967 	if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
968 		return (CTF_ERR); /* errno is set for us */
969 
970 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
971 	dtd->dtd_data.ctt_type = (ushort_t)ref;
972 	ctf_ref_inc(fp, ref);
973 
974 	return (type);
975 }
976 
977 ctf_id_t
ctf_add_volatile(ctf_file_t * fp,uint_t flag,ctf_id_t ref)978 ctf_add_volatile(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
979 {
980 	return (ctf_add_reftype(fp, flag, ref, CTF_K_VOLATILE));
981 }
982 
983 ctf_id_t
ctf_add_const(ctf_file_t * fp,uint_t flag,ctf_id_t ref)984 ctf_add_const(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
985 {
986 	return (ctf_add_reftype(fp, flag, ref, CTF_K_CONST));
987 }
988 
989 ctf_id_t
ctf_add_restrict(ctf_file_t * fp,uint_t flag,ctf_id_t ref)990 ctf_add_restrict(ctf_file_t *fp, uint_t flag, ctf_id_t ref)
991 {
992 	return (ctf_add_reftype(fp, flag, ref, CTF_K_RESTRICT));
993 }
994 
995 int
ctf_add_enumerator(ctf_file_t * fp,ctf_id_t enid,const char * name,int value)996 ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
997 {
998 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
999 	ctf_dmdef_t *dmd;
1000 
1001 	uint_t kind, vlen, root;
1002 	char *s;
1003 
1004 	if (name == NULL)
1005 		return (ctf_set_errno(fp, EINVAL));
1006 
1007 	if (!(fp->ctf_flags & LCTF_RDWR))
1008 		return (ctf_set_errno(fp, ECTF_RDONLY));
1009 
1010 	if (dtd == NULL)
1011 		return (ctf_set_errno(fp, ECTF_BADID));
1012 
1013 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1014 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1015 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1016 
1017 	if (kind != CTF_K_ENUM)
1018 		return (ctf_set_errno(fp, ECTF_NOTENUM));
1019 
1020 	if (vlen == CTF_MAX_VLEN)
1021 		return (ctf_set_errno(fp, ECTF_DTFULL));
1022 
1023 	for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1024 	    dmd != NULL; dmd = ctf_list_next(dmd)) {
1025 		if (strcmp(dmd->dmd_name, name) == 0)
1026 			return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1027 	}
1028 
1029 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1030 		return (ctf_set_errno(fp, EAGAIN));
1031 
1032 	if ((s = ctf_strdup(name)) == NULL) {
1033 		ctf_free(dmd, sizeof (ctf_dmdef_t));
1034 		return (ctf_set_errno(fp, EAGAIN));
1035 	}
1036 
1037 	dmd->dmd_name = s;
1038 	dmd->dmd_type = CTF_ERR;
1039 	dmd->dmd_offset = 0;
1040 	dmd->dmd_value = value;
1041 
1042 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1043 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1044 
1045 	fp->ctf_dtstrlen += strlen(s) + 1;
1046 	fp->ctf_flags |= LCTF_DIRTY;
1047 
1048 	return (0);
1049 }
1050 
1051 int
ctf_add_member(ctf_file_t * fp,ctf_id_t souid,const char * name,ctf_id_t type)1052 ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type)
1053 {
1054 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
1055 	ctf_dmdef_t *dmd;
1056 
1057 	ssize_t msize, malign, ssize;
1058 	uint_t kind, vlen, root;
1059 	char *s = NULL;
1060 
1061 	if (!(fp->ctf_flags & LCTF_RDWR))
1062 		return (ctf_set_errno(fp, ECTF_RDONLY));
1063 
1064 	if (dtd == NULL)
1065 		return (ctf_set_errno(fp, ECTF_BADID));
1066 
1067 	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1068 	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1069 	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1070 
1071 	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1072 		return (ctf_set_errno(fp, ECTF_NOTSOU));
1073 
1074 	if (vlen == CTF_MAX_VLEN)
1075 		return (ctf_set_errno(fp, ECTF_DTFULL));
1076 
1077 	if (name != NULL) {
1078 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1079 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
1080 			if (dmd->dmd_name != NULL &&
1081 			    strcmp(dmd->dmd_name, name) == 0)
1082 				return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1083 		}
1084 	}
1085 
1086 	if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
1087 	    (malign = ctf_type_align(fp, type)) == CTF_ERR)
1088 		return (CTF_ERR); /* errno is set for us */
1089 
1090 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1091 		return (ctf_set_errno(fp, EAGAIN));
1092 
1093 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1094 		ctf_free(dmd, sizeof (ctf_dmdef_t));
1095 		return (ctf_set_errno(fp, EAGAIN));
1096 	}
1097 
1098 	dmd->dmd_name = s;
1099 	dmd->dmd_type = type;
1100 	dmd->dmd_value = -1;
1101 
1102 	if (kind == CTF_K_STRUCT && vlen != 0) {
1103 		ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
1104 		ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
1105 		size_t off = lmd->dmd_offset;
1106 
1107 		ctf_encoding_t linfo;
1108 		ssize_t lsize;
1109 
1110 		if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
1111 			off += linfo.cte_bits;
1112 		else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
1113 			off += lsize * NBBY;
1114 
1115 		/*
1116 		 * Round up the offset of the end of the last member to the
1117 		 * next byte boundary, convert 'off' to bytes, and then round
1118 		 * it up again to the next multiple of the alignment required
1119 		 * by the new member.  Finally, convert back to bits and store
1120 		 * the result in dmd_offset.  Technically we could do more
1121 		 * efficient packing if the new member is a bit-field, but
1122 		 * we're the "compiler" and ANSI says we can do as we choose.
1123 		 */
1124 		off = roundup(off, NBBY) / NBBY;
1125 		off = roundup(off, MAX(malign, 1));
1126 		dmd->dmd_offset = off * NBBY;
1127 		ssize = off + msize;
1128 	} else {
1129 		dmd->dmd_offset = 0;
1130 		ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
1131 		ssize = MAX(ssize, msize);
1132 	}
1133 
1134 	if (ssize > CTF_MAX_SIZE) {
1135 		dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1136 		dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
1137 		dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
1138 	} else
1139 		dtd->dtd_data.ctt_size = (ushort_t)ssize;
1140 
1141 	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1142 	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1143 
1144 	if (s != NULL)
1145 		fp->ctf_dtstrlen += strlen(s) + 1;
1146 
1147 	ctf_ref_inc(fp, type);
1148 	fp->ctf_flags |= LCTF_DIRTY;
1149 	return (0);
1150 }
1151 
1152 /*
1153  * This removes a type from the dynamic section. This will fail if the type is
1154  * referenced by another type. Note that the CTF ID is never reused currently by
1155  * CTF. Note that if this container is a parent container then we just outright
1156  * refuse to remove the type. There currently is no notion of searching for the
1157  * ctf_dtdef_t in parent containers. If there is, then this constraint could
1158  * become finer grained.
1159  */
1160 int
ctf_delete_type(ctf_file_t * fp,ctf_id_t type)1161 ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
1162 {
1163 	ctf_file_t *fpd;
1164 	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1165 
1166 	if (!(fp->ctf_flags & LCTF_RDWR))
1167 		return (ctf_set_errno(fp, ECTF_RDONLY));
1168 
1169 	/*
1170 	 * We want to give as useful an errno as possible. That means that we
1171 	 * want to distinguish between a type which does not exist and one for
1172 	 * which the type is not dynamic.
1173 	 */
1174 	fpd = fp;
1175 	if (ctf_lookup_by_id(&fpd, type) == NULL &&
1176 	    ctf_dtd_lookup(fp, type) == NULL)
1177 		return (CTF_ERR); /* errno is set for us */
1178 
1179 	if (dtd == NULL)
1180 		return (ctf_set_errno(fp, ECTF_NOTDYN));
1181 
1182 	if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
1183 		return (ctf_set_errno(fp, ECTF_REFERENCED));
1184 
1185 	ctf_dtd_delete(fp, dtd);
1186 	fp->ctf_flags |= LCTF_DIRTY;
1187 	return (0);
1188 }
1189 
1190 static int
enumcmp(const char * name,int value,void * arg)1191 enumcmp(const char *name, int value, void *arg)
1192 {
1193 	ctf_bundle_t *ctb = arg;
1194 	int bvalue;
1195 
1196 	return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
1197 	    name, &bvalue) == CTF_ERR || value != bvalue);
1198 }
1199 
1200 static int
enumadd(const char * name,int value,void * arg)1201 enumadd(const char *name, int value, void *arg)
1202 {
1203 	ctf_bundle_t *ctb = arg;
1204 
1205 	return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
1206 	    name, value) == CTF_ERR);
1207 }
1208 
1209 /*ARGSUSED*/
1210 static int
membcmp(const char * name,ctf_id_t type,ulong_t offset,void * arg)1211 membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1212 {
1213 	ctf_bundle_t *ctb = arg;
1214 	ctf_membinfo_t ctm;
1215 
1216 	return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
1217 	    name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
1218 }
1219 
1220 static int
membadd(const char * name,ctf_id_t type,ulong_t offset,void * arg)1221 membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1222 {
1223 	ctf_bundle_t *ctb = arg;
1224 	ctf_dmdef_t *dmd;
1225 	char *s = NULL;
1226 
1227 	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1228 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1229 
1230 	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1231 		ctf_free(dmd, sizeof (ctf_dmdef_t));
1232 		return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1233 	}
1234 
1235 	/*
1236 	 * For now, dmd_type is copied as the src_fp's type; it is reset to an
1237 	 * equivalent dst_fp type by a final loop in ctf_add_type(), below.
1238 	 */
1239 	dmd->dmd_name = s;
1240 	dmd->dmd_type = type;
1241 	dmd->dmd_offset = offset;
1242 	dmd->dmd_value = -1;
1243 
1244 	ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1245 
1246 	if (s != NULL)
1247 		ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
1248 
1249 	ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1250 	return (0);
1251 }
1252 
1253 /*
1254  * The ctf_add_type routine is used to copy a type from a source CTF container
1255  * to a dynamic destination container.  This routine operates recursively by
1256  * following the source type's links and embedded member types.  If the
1257  * destination container already contains a named type which has the same
1258  * attributes, then we succeed and return this type but no changes occur.
1259  */
1260 ctf_id_t
ctf_add_type(ctf_file_t * dst_fp,ctf_file_t * src_fp,ctf_id_t src_type)1261 ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
1262 {
1263 	ctf_id_t dst_type = CTF_ERR;
1264 	uint_t dst_kind = CTF_K_UNKNOWN;
1265 
1266 	const ctf_type_t *tp;
1267 	const char *name;
1268 	uint_t kind, flag, vlen;
1269 
1270 	ctf_bundle_t src, dst;
1271 	ctf_encoding_t src_en, dst_en;
1272 	ctf_arinfo_t src_ar, dst_ar;
1273 
1274 	ctf_dtdef_t *dtd;
1275 	ctf_funcinfo_t ctc;
1276 	ssize_t size;
1277 
1278 	ctf_hash_t *hp;
1279 	ctf_helem_t *hep;
1280 
1281 	if (dst_fp == src_fp)
1282 		return (src_type);
1283 
1284 	if (!(dst_fp->ctf_flags & LCTF_RDWR))
1285 		return (ctf_set_errno(dst_fp, ECTF_RDONLY));
1286 
1287 	if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
1288 		return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1289 
1290 	name = ctf_strptr(src_fp, tp->ctt_name);
1291 	kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
1292 	flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
1293 	vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
1294 
1295 	switch (kind) {
1296 	case CTF_K_STRUCT:
1297 		hp = &dst_fp->ctf_structs;
1298 		break;
1299 	case CTF_K_UNION:
1300 		hp = &dst_fp->ctf_unions;
1301 		break;
1302 	case CTF_K_ENUM:
1303 		hp = &dst_fp->ctf_enums;
1304 		break;
1305 	default:
1306 		hp = &dst_fp->ctf_names;
1307 		break;
1308 	}
1309 
1310 	/*
1311 	 * If the source type has a name and is a root type (visible at the
1312 	 * top-level scope), lookup the name in the destination container and
1313 	 * verify that it is of the same kind before we do anything else.
1314 	 */
1315 	if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
1316 	    (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
1317 		dst_type = (ctf_id_t)hep->h_type;
1318 		dst_kind = ctf_type_kind(dst_fp, dst_type);
1319 	}
1320 
1321 	/*
1322 	 * If an identically named dst_type exists, fail with ECTF_CONFLICT
1323 	 * unless dst_type is a forward declaration and src_type is a struct,
1324 	 * union, or enum (i.e. the definition of the previous forward decl).
1325 	 */
1326 	if (dst_type != CTF_ERR && dst_kind != kind) {
1327 		if (dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
1328 		    kind != CTF_K_STRUCT && kind != CTF_K_UNION))
1329 			return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1330 		else
1331 			dst_type = CTF_ERR;
1332 	}
1333 
1334 	/*
1335 	 * If the non-empty name was not found in the appropriate hash, search
1336 	 * the list of pending dynamic definitions that are not yet committed.
1337 	 * If a matching name and kind are found, assume this is the type that
1338 	 * we are looking for.  This is necessary to permit ctf_add_type() to
1339 	 * operate recursively on entities such as a struct that contains a
1340 	 * pointer member that refers to the same struct type.
1341 	 *
1342 	 * In the case of integer and floating point types, we match using the
1343 	 * type encoding as well - else we may incorrectly return a bitfield
1344 	 * type, for instance.
1345 	 */
1346 	if (dst_type == CTF_ERR && name[0] != '\0') {
1347 		for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
1348 		    CTF_TYPE_TO_INDEX(dtd->dtd_type) > dst_fp->ctf_dtoldid;
1349 		    dtd = ctf_list_prev(dtd)) {
1350 			if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != kind ||
1351 			    dtd->dtd_name == NULL ||
1352 			    strcmp(dtd->dtd_name, name) != 0)
1353 				continue;
1354 			if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT) {
1355 				if (ctf_type_encoding(src_fp, src_type,
1356 				    &src_en) != 0)
1357 					continue;
1358 				if (bcmp(&src_en, &dtd->dtd_u.dtu_enc,
1359 				    sizeof (ctf_encoding_t)) != 0)
1360 					continue;
1361 			}
1362 			return (dtd->dtd_type);
1363 		}
1364 	}
1365 
1366 	src.ctb_file = src_fp;
1367 	src.ctb_type = src_type;
1368 	src.ctb_dtd = NULL;
1369 
1370 	dst.ctb_file = dst_fp;
1371 	dst.ctb_type = dst_type;
1372 	dst.ctb_dtd = NULL;
1373 
1374 	/*
1375 	 * Now perform kind-specific processing.  If dst_type is CTF_ERR, then
1376 	 * we add a new type with the same properties as src_type to dst_fp.
1377 	 * If dst_type is not CTF_ERR, then we verify that dst_type has the
1378 	 * same attributes as src_type.  We recurse for embedded references.
1379 	 */
1380 	switch (kind) {
1381 	case CTF_K_INTEGER:
1382 	case CTF_K_FLOAT:
1383 		if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
1384 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1385 
1386 		if (dst_type != CTF_ERR) {
1387 			if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
1388 				return (CTF_ERR); /* errno is set for us */
1389 
1390 			if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
1391 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1392 
1393 		} else if (kind == CTF_K_INTEGER) {
1394 			dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
1395 		} else
1396 			dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
1397 		break;
1398 
1399 	case CTF_K_POINTER:
1400 	case CTF_K_VOLATILE:
1401 	case CTF_K_CONST:
1402 	case CTF_K_RESTRICT:
1403 		src_type = ctf_type_reference(src_fp, src_type);
1404 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
1405 
1406 		if (src_type == CTF_ERR)
1407 			return (CTF_ERR); /* errno is set for us */
1408 
1409 		dst_type = ctf_add_reftype(dst_fp, flag, src_type, kind);
1410 		break;
1411 
1412 	case CTF_K_ARRAY:
1413 		if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
1414 			return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1415 
1416 		src_ar.ctr_contents =
1417 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
1418 		src_ar.ctr_index =
1419 		    ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
1420 		src_ar.ctr_nelems = src_ar.ctr_nelems;
1421 
1422 		if (src_ar.ctr_contents == CTF_ERR ||
1423 		    src_ar.ctr_index == CTF_ERR)
1424 			return (CTF_ERR); /* errno is set for us */
1425 
1426 		if (dst_type != CTF_ERR) {
1427 			if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
1428 				return (CTF_ERR); /* errno is set for us */
1429 
1430 			if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1431 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1432 		} else
1433 			dst_type = ctf_add_array(dst_fp, flag, &src_ar);
1434 		break;
1435 
1436 	case CTF_K_FUNCTION:
1437 		ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
1438 		ctc.ctc_argc = 0;
1439 		ctc.ctc_flags = 0;
1440 
1441 		if (ctc.ctc_return == CTF_ERR)
1442 			return (CTF_ERR); /* errno is set for us */
1443 
1444 		dst_type = ctf_add_function(dst_fp, flag, &ctc, NULL);
1445 		break;
1446 
1447 	case CTF_K_STRUCT:
1448 	case CTF_K_UNION: {
1449 		ctf_dmdef_t *dmd;
1450 		int errs = 0;
1451 
1452 		/*
1453 		 * Technically to match a struct or union we need to check both
1454 		 * ways (src members vs. dst, dst members vs. src) but we make
1455 		 * this more optimal by only checking src vs. dst and comparing
1456 		 * the total size of the structure (which we must do anyway)
1457 		 * which covers the possibility of dst members not in src.
1458 		 * This optimization can be defeated for unions, but is so
1459 		 * pathological as to render it irrelevant for our purposes.
1460 		 */
1461 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1462 			if (ctf_type_size(src_fp, src_type) !=
1463 			    ctf_type_size(dst_fp, dst_type))
1464 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1465 
1466 			if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
1467 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1468 
1469 			break;
1470 		}
1471 
1472 		/*
1473 		 * Unlike the other cases, copying structs and unions is done
1474 		 * manually so as to avoid repeated lookups in ctf_add_member
1475 		 * and to ensure the exact same member offsets as in src_type.
1476 		 */
1477 		dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
1478 		if (dst_type == CTF_ERR)
1479 			return (CTF_ERR); /* errno is set for us */
1480 
1481 		dst.ctb_type = dst_type;
1482 		dst.ctb_dtd = dtd;
1483 
1484 		if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
1485 			errs++; /* increment errs and fail at bottom of case */
1486 
1487 		if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
1488 			dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1489 			dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1490 			dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1491 		} else
1492 			dtd->dtd_data.ctt_size = (ushort_t)size;
1493 
1494 		dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
1495 
1496 		/*
1497 		 * Make a final pass through the members changing each dmd_type
1498 		 * (a src_fp type) to an equivalent type in dst_fp.  We pass
1499 		 * through all members, leaving any that fail set to CTF_ERR.
1500 		 */
1501 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1502 		    dmd != NULL; dmd = ctf_list_next(dmd)) {
1503 			if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
1504 			    dmd->dmd_type)) == CTF_ERR)
1505 				errs++;
1506 		}
1507 
1508 		if (errs)
1509 			return (CTF_ERR); /* errno is set for us */
1510 
1511 		/*
1512 		 * Now that we know that we can't fail, we go through and bump
1513 		 * all the reference counts on the member types.
1514 		 */
1515 		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1516 		    dmd != NULL; dmd = ctf_list_next(dmd))
1517 			ctf_ref_inc(dst_fp, dmd->dmd_type);
1518 		break;
1519 	}
1520 
1521 	case CTF_K_ENUM:
1522 		if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1523 			if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
1524 			    ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
1525 				return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1526 		} else {
1527 			dst_type = ctf_add_enum(dst_fp, flag, name);
1528 			if ((dst.ctb_type = dst_type) == CTF_ERR ||
1529 			    ctf_enum_iter(src_fp, src_type, enumadd, &dst))
1530 				return (CTF_ERR); /* errno is set for us */
1531 		}
1532 		break;
1533 
1534 	case CTF_K_FORWARD:
1535 		if (dst_type == CTF_ERR) {
1536 			dst_type = ctf_add_forward(dst_fp,
1537 			    flag, name, CTF_K_STRUCT); /* assume STRUCT */
1538 		}
1539 		break;
1540 
1541 	case CTF_K_TYPEDEF:
1542 		src_type = ctf_type_reference(src_fp, src_type);
1543 		src_type = ctf_add_type(dst_fp, src_fp, src_type);
1544 
1545 		if (src_type == CTF_ERR)
1546 			return (CTF_ERR); /* errno is set for us */
1547 
1548 		/*
1549 		 * If dst_type is not CTF_ERR at this point, we should check if
1550 		 * ctf_type_reference(dst_fp, dst_type) != src_type and if so
1551 		 * fail with ECTF_CONFLICT.  However, this causes problems with
1552 		 * <sys/types.h> typedefs that vary based on things like if
1553 		 * _ILP32x then pid_t is int otherwise long.  We therefore omit
1554 		 * this check and assume that if the identically named typedef
1555 		 * already exists in dst_fp, it is correct or equivalent.
1556 		 */
1557 		if (dst_type == CTF_ERR) {
1558 			dst_type = ctf_add_typedef(dst_fp, flag,
1559 			    name, src_type);
1560 		}
1561 		break;
1562 
1563 	default:
1564 		return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
1565 	}
1566 
1567 	return (dst_type);
1568 }
1569