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 * 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 * 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 * 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 * 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 * 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 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 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 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 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 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 * 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 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 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_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE) 620 return (ctf_set_errno(fp, ECTF_FULL)); 621 622 if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL) 623 return (ctf_set_errno(fp, EAGAIN)); 624 625 if (name != NULL && (s = ctf_strdup(name)) == NULL) { 626 ctf_free(dtd, sizeof (ctf_dtdef_t)); 627 return (ctf_set_errno(fp, EAGAIN)); 628 } 629 630 type = fp->ctf_dtnextid++; 631 type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD)); 632 633 bzero(dtd, sizeof (ctf_dtdef_t)); 634 dtd->dtd_name = s; 635 dtd->dtd_type = type; 636 637 if (s != NULL) 638 fp->ctf_dtstrlen += strlen(s) + 1; 639 640 ctf_dtd_insert(fp, dtd); 641 fp->ctf_flags |= LCTF_DIRTY; 642 643 *rp = dtd; 644 return (type); 645 } 646 647 /* 648 * When encoding integer sizes, we want to convert a byte count in the range 649 * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function 650 * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. 651 */ 652 static size_t 653 clp2(size_t x) 654 { 655 x--; 656 657 x |= (x >> 1); 658 x |= (x >> 2); 659 x |= (x >> 4); 660 x |= (x >> 8); 661 x |= (x >> 16); 662 663 return (x + 1); 664 } 665 666 static ctf_id_t 667 ctf_add_encoded(ctf_file_t *fp, uint_t flag, 668 const char *name, const ctf_encoding_t *ep, uint_t kind) 669 { 670 ctf_dtdef_t *dtd; 671 ctf_id_t type; 672 673 if (ep == NULL) 674 return (ctf_set_errno(fp, EINVAL)); 675 676 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 677 return (CTF_ERR); /* errno is set for us */ 678 679 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 680 dtd->dtd_data.ctt_size = clp2(P2ROUNDUP(ep->cte_bits, NBBY) / NBBY); 681 dtd->dtd_u.dtu_enc = *ep; 682 683 return (type); 684 } 685 686 static ctf_id_t 687 ctf_add_reftype(ctf_file_t *fp, uint_t flag, ctf_id_t ref, uint_t kind) 688 { 689 ctf_dtdef_t *dtd; 690 ctf_id_t type; 691 692 if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE) 693 return (ctf_set_errno(fp, EINVAL)); 694 695 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) 696 return (CTF_ERR); /* errno is set for us */ 697 698 ctf_ref_inc(fp, ref); 699 700 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 701 dtd->dtd_data.ctt_type = (ushort_t)ref; 702 703 return (type); 704 } 705 706 ctf_id_t 707 ctf_add_integer(ctf_file_t *fp, uint_t flag, 708 const char *name, const ctf_encoding_t *ep) 709 { 710 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER)); 711 } 712 713 ctf_id_t 714 ctf_add_float(ctf_file_t *fp, uint_t flag, 715 const char *name, const ctf_encoding_t *ep) 716 { 717 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT)); 718 } 719 720 ctf_id_t 721 ctf_add_pointer(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 722 { 723 return (ctf_add_reftype(fp, flag, ref, CTF_K_POINTER)); 724 } 725 726 ctf_id_t 727 ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp) 728 { 729 ctf_dtdef_t *dtd; 730 ctf_id_t type; 731 ctf_file_t *fpd; 732 733 if (arp == NULL) 734 return (ctf_set_errno(fp, EINVAL)); 735 736 fpd = fp; 737 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL && 738 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) 739 return (ctf_set_errno(fp, ECTF_BADID)); 740 741 fpd = fp; 742 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL && 743 ctf_dtd_lookup(fp, arp->ctr_index) == NULL) 744 return (ctf_set_errno(fp, ECTF_BADID)); 745 746 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) 747 return (CTF_ERR); /* errno is set for us */ 748 749 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0); 750 dtd->dtd_data.ctt_size = 0; 751 dtd->dtd_u.dtu_arr = *arp; 752 ctf_ref_inc(fp, arp->ctr_contents); 753 ctf_ref_inc(fp, arp->ctr_index); 754 755 return (type); 756 } 757 758 int 759 ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) 760 { 761 ctf_file_t *fpd; 762 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type); 763 764 if (!(fp->ctf_flags & LCTF_RDWR)) 765 return (ctf_set_errno(fp, ECTF_RDONLY)); 766 767 if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY) 768 return (ctf_set_errno(fp, ECTF_BADID)); 769 770 fpd = fp; 771 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL && 772 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) 773 return (ctf_set_errno(fp, ECTF_BADID)); 774 775 fpd = fp; 776 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL && 777 ctf_dtd_lookup(fp, arp->ctr_index) == NULL) 778 return (ctf_set_errno(fp, ECTF_BADID)); 779 780 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents); 781 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index); 782 fp->ctf_flags |= LCTF_DIRTY; 783 dtd->dtd_u.dtu_arr = *arp; 784 ctf_ref_inc(fp, arp->ctr_contents); 785 ctf_ref_inc(fp, arp->ctr_index); 786 787 return (0); 788 } 789 790 ctf_id_t 791 ctf_add_function(ctf_file_t *fp, uint_t flag, 792 const ctf_funcinfo_t *ctc, const ctf_id_t *argv) 793 { 794 ctf_dtdef_t *dtd; 795 ctf_id_t type; 796 uint_t vlen; 797 int i; 798 ctf_id_t *vdat = NULL; 799 ctf_file_t *fpd; 800 801 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 || 802 (ctc->ctc_argc != 0 && argv == NULL)) 803 return (ctf_set_errno(fp, EINVAL)); 804 805 vlen = ctc->ctc_argc; 806 if (ctc->ctc_flags & CTF_FUNC_VARARG) 807 vlen++; /* add trailing zero to indicate varargs (see below) */ 808 809 if (vlen > CTF_MAX_VLEN) 810 return (ctf_set_errno(fp, EOVERFLOW)); 811 812 fpd = fp; 813 if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL && 814 ctf_dtd_lookup(fp, ctc->ctc_return) == NULL) 815 return (ctf_set_errno(fp, ECTF_BADID)); 816 817 for (i = 0; i < ctc->ctc_argc; i++) { 818 fpd = fp; 819 if (ctf_lookup_by_id(&fpd, argv[i]) == NULL && 820 ctf_dtd_lookup(fp, argv[i]) == NULL) 821 return (ctf_set_errno(fp, ECTF_BADID)); 822 } 823 824 if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL) 825 return (ctf_set_errno(fp, EAGAIN)); 826 827 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) { 828 ctf_free(vdat, sizeof (ctf_id_t) * vlen); 829 return (CTF_ERR); /* errno is set for us */ 830 } 831 832 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen); 833 dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return; 834 835 ctf_ref_inc(fp, ctc->ctc_return); 836 for (i = 0; i < ctc->ctc_argc; i++) 837 ctf_ref_inc(fp, argv[i]); 838 839 bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc); 840 if (ctc->ctc_flags & CTF_FUNC_VARARG) 841 vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */ 842 dtd->dtd_u.dtu_argv = vdat; 843 844 return (type); 845 } 846 847 ctf_id_t 848 ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name) 849 { 850 ctf_hash_t *hp = &fp->ctf_structs; 851 ctf_helem_t *hep = NULL; 852 ctf_dtdef_t *dtd; 853 ctf_id_t type; 854 855 if (name != NULL) 856 hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 857 858 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) 859 dtd = ctf_dtd_lookup(fp, type = hep->h_type); 860 else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 861 return (CTF_ERR); /* errno is set for us */ 862 863 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0); 864 dtd->dtd_data.ctt_size = 0; 865 866 return (type); 867 } 868 869 ctf_id_t 870 ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name) 871 { 872 ctf_hash_t *hp = &fp->ctf_unions; 873 ctf_helem_t *hep = NULL; 874 ctf_dtdef_t *dtd; 875 ctf_id_t type; 876 877 if (name != NULL) 878 hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 879 880 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) 881 dtd = ctf_dtd_lookup(fp, type = hep->h_type); 882 else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 883 return (CTF_ERR); /* errno is set for us */ 884 885 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0); 886 dtd->dtd_data.ctt_size = 0; 887 888 return (type); 889 } 890 891 ctf_id_t 892 ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name) 893 { 894 ctf_hash_t *hp = &fp->ctf_enums; 895 ctf_helem_t *hep = NULL; 896 ctf_dtdef_t *dtd; 897 ctf_id_t type; 898 899 if (name != NULL) 900 hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 901 902 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) 903 dtd = ctf_dtd_lookup(fp, type = hep->h_type); 904 else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 905 return (CTF_ERR); /* errno is set for us */ 906 907 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0); 908 dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int; 909 910 return (type); 911 } 912 913 ctf_id_t 914 ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind) 915 { 916 ctf_hash_t *hp; 917 ctf_helem_t *hep; 918 ctf_dtdef_t *dtd; 919 ctf_id_t type; 920 921 switch (kind) { 922 case CTF_K_STRUCT: 923 hp = &fp->ctf_structs; 924 break; 925 case CTF_K_UNION: 926 hp = &fp->ctf_unions; 927 break; 928 case CTF_K_ENUM: 929 hp = &fp->ctf_enums; 930 break; 931 default: 932 return (ctf_set_errno(fp, ECTF_NOTSUE)); 933 } 934 935 /* 936 * If the type is already defined or exists as a forward tag, just 937 * return the ctf_id_t of the existing definition. 938 */ 939 if (name != NULL && (hep = ctf_hash_lookup(hp, 940 fp, name, strlen(name))) != NULL) 941 return (hep->h_type); 942 943 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 944 return (CTF_ERR); /* errno is set for us */ 945 946 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0); 947 dtd->dtd_data.ctt_type = kind; 948 949 return (type); 950 } 951 952 ctf_id_t 953 ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 954 { 955 ctf_dtdef_t *dtd; 956 ctf_id_t type; 957 ctf_file_t *fpd; 958 959 fpd = fp; 960 if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL && 961 ctf_dtd_lookup(fp, ref) == NULL)) 962 return (ctf_set_errno(fp, EINVAL)); 963 964 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 965 return (CTF_ERR); /* errno is set for us */ 966 967 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0); 968 dtd->dtd_data.ctt_type = (ushort_t)ref; 969 ctf_ref_inc(fp, ref); 970 971 return (type); 972 } 973 974 ctf_id_t 975 ctf_add_volatile(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 976 { 977 return (ctf_add_reftype(fp, flag, ref, CTF_K_VOLATILE)); 978 } 979 980 ctf_id_t 981 ctf_add_const(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 982 { 983 return (ctf_add_reftype(fp, flag, ref, CTF_K_CONST)); 984 } 985 986 ctf_id_t 987 ctf_add_restrict(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 988 { 989 return (ctf_add_reftype(fp, flag, ref, CTF_K_RESTRICT)); 990 } 991 992 int 993 ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value) 994 { 995 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid); 996 ctf_dmdef_t *dmd; 997 998 uint_t kind, vlen, root; 999 char *s; 1000 1001 if (name == NULL) 1002 return (ctf_set_errno(fp, EINVAL)); 1003 1004 if (!(fp->ctf_flags & LCTF_RDWR)) 1005 return (ctf_set_errno(fp, ECTF_RDONLY)); 1006 1007 if (dtd == NULL) 1008 return (ctf_set_errno(fp, ECTF_BADID)); 1009 1010 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 1011 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 1012 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 1013 1014 if (kind != CTF_K_ENUM) 1015 return (ctf_set_errno(fp, ECTF_NOTENUM)); 1016 1017 if (vlen == CTF_MAX_VLEN) 1018 return (ctf_set_errno(fp, ECTF_DTFULL)); 1019 1020 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1021 dmd != NULL; dmd = ctf_list_next(dmd)) { 1022 if (strcmp(dmd->dmd_name, name) == 0) 1023 return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 1024 } 1025 1026 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 1027 return (ctf_set_errno(fp, EAGAIN)); 1028 1029 if ((s = ctf_strdup(name)) == NULL) { 1030 ctf_free(dmd, sizeof (ctf_dmdef_t)); 1031 return (ctf_set_errno(fp, EAGAIN)); 1032 } 1033 1034 dmd->dmd_name = s; 1035 dmd->dmd_type = CTF_ERR; 1036 dmd->dmd_offset = 0; 1037 dmd->dmd_value = value; 1038 1039 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 1040 ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 1041 1042 fp->ctf_dtstrlen += strlen(s) + 1; 1043 fp->ctf_flags |= LCTF_DIRTY; 1044 1045 return (0); 1046 } 1047 1048 int 1049 ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type) 1050 { 1051 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid); 1052 ctf_dmdef_t *dmd; 1053 1054 ssize_t msize, malign, ssize; 1055 uint_t kind, vlen, root; 1056 char *s = NULL; 1057 1058 if (!(fp->ctf_flags & LCTF_RDWR)) 1059 return (ctf_set_errno(fp, ECTF_RDONLY)); 1060 1061 if (dtd == NULL) 1062 return (ctf_set_errno(fp, ECTF_BADID)); 1063 1064 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 1065 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 1066 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 1067 1068 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) 1069 return (ctf_set_errno(fp, ECTF_NOTSOU)); 1070 1071 if (vlen == CTF_MAX_VLEN) 1072 return (ctf_set_errno(fp, ECTF_DTFULL)); 1073 1074 if (name != NULL) { 1075 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1076 dmd != NULL; dmd = ctf_list_next(dmd)) { 1077 if (dmd->dmd_name != NULL && 1078 strcmp(dmd->dmd_name, name) == 0) 1079 return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 1080 } 1081 } 1082 1083 if ((msize = ctf_type_size(fp, type)) == CTF_ERR || 1084 (malign = ctf_type_align(fp, type)) == CTF_ERR) 1085 return (CTF_ERR); /* errno is set for us */ 1086 1087 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 1088 return (ctf_set_errno(fp, EAGAIN)); 1089 1090 if (name != NULL && (s = ctf_strdup(name)) == NULL) { 1091 ctf_free(dmd, sizeof (ctf_dmdef_t)); 1092 return (ctf_set_errno(fp, EAGAIN)); 1093 } 1094 1095 dmd->dmd_name = s; 1096 dmd->dmd_type = type; 1097 dmd->dmd_value = -1; 1098 1099 if (kind == CTF_K_STRUCT && vlen != 0) { 1100 ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members); 1101 ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type); 1102 size_t off = lmd->dmd_offset; 1103 1104 ctf_encoding_t linfo; 1105 ssize_t lsize; 1106 1107 if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR) 1108 off += linfo.cte_bits; 1109 else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR) 1110 off += lsize * NBBY; 1111 1112 /* 1113 * Round up the offset of the end of the last member to the 1114 * next byte boundary, convert 'off' to bytes, and then round 1115 * it up again to the next multiple of the alignment required 1116 * by the new member. Finally, convert back to bits and store 1117 * the result in dmd_offset. Technically we could do more 1118 * efficient packing if the new member is a bit-field, but 1119 * we're the "compiler" and ANSI says we can do as we choose. 1120 */ 1121 off = roundup(off, NBBY) / NBBY; 1122 off = roundup(off, MAX(malign, 1)); 1123 dmd->dmd_offset = off * NBBY; 1124 ssize = off + msize; 1125 } else { 1126 dmd->dmd_offset = 0; 1127 ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL); 1128 ssize = MAX(ssize, msize); 1129 } 1130 1131 if (ssize > CTF_MAX_SIZE) { 1132 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 1133 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize); 1134 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize); 1135 } else 1136 dtd->dtd_data.ctt_size = (ushort_t)ssize; 1137 1138 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 1139 ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 1140 1141 if (s != NULL) 1142 fp->ctf_dtstrlen += strlen(s) + 1; 1143 1144 ctf_ref_inc(fp, type); 1145 fp->ctf_flags |= LCTF_DIRTY; 1146 return (0); 1147 } 1148 1149 /* 1150 * This removes a type from the dynamic section. This will fail if the type is 1151 * referenced by another type. Note that the CTF ID is never reused currently by 1152 * CTF. Note that if this container is a parent container then we just outright 1153 * refuse to remove the type. There currently is no notion of searching for the 1154 * ctf_dtdef_t in parent containers. If there is, then this constraint could 1155 * become finer grained. 1156 */ 1157 int 1158 ctf_delete_type(ctf_file_t *fp, ctf_id_t type) 1159 { 1160 ctf_file_t *fpd; 1161 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type); 1162 1163 if (!(fp->ctf_flags & LCTF_RDWR)) 1164 return (ctf_set_errno(fp, ECTF_RDONLY)); 1165 1166 /* 1167 * We want to give as useful an errno as possible. That means that we 1168 * want to distinguish between a type which does not exist and one for 1169 * which the type is not dynamic. 1170 */ 1171 fpd = fp; 1172 if (ctf_lookup_by_id(&fpd, type) == NULL && 1173 ctf_dtd_lookup(fp, type) == NULL) 1174 return (CTF_ERR); /* errno is set for us */ 1175 1176 if (dtd == NULL) 1177 return (ctf_set_errno(fp, ECTF_NOTDYN)); 1178 1179 if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1) 1180 return (ctf_set_errno(fp, ECTF_REFERENCED)); 1181 1182 ctf_dtd_delete(fp, dtd); 1183 fp->ctf_flags |= LCTF_DIRTY; 1184 return (0); 1185 } 1186 1187 static int 1188 enumcmp(const char *name, int value, void *arg) 1189 { 1190 ctf_bundle_t *ctb = arg; 1191 int bvalue; 1192 1193 return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type, 1194 name, &bvalue) == CTF_ERR || value != bvalue); 1195 } 1196 1197 static int 1198 enumadd(const char *name, int value, void *arg) 1199 { 1200 ctf_bundle_t *ctb = arg; 1201 1202 return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type, 1203 name, value) == CTF_ERR); 1204 } 1205 1206 /*ARGSUSED*/ 1207 static int 1208 membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg) 1209 { 1210 ctf_bundle_t *ctb = arg; 1211 ctf_membinfo_t ctm; 1212 1213 return (ctf_member_info(ctb->ctb_file, ctb->ctb_type, 1214 name, &ctm) == CTF_ERR || ctm.ctm_offset != offset); 1215 } 1216 1217 static int 1218 membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg) 1219 { 1220 ctf_bundle_t *ctb = arg; 1221 ctf_dmdef_t *dmd; 1222 char *s = NULL; 1223 1224 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 1225 return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 1226 1227 if (name != NULL && (s = ctf_strdup(name)) == NULL) { 1228 ctf_free(dmd, sizeof (ctf_dmdef_t)); 1229 return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 1230 } 1231 1232 /* 1233 * For now, dmd_type is copied as the src_fp's type; it is reset to an 1234 * equivalent dst_fp type by a final loop in ctf_add_type(), below. 1235 */ 1236 dmd->dmd_name = s; 1237 dmd->dmd_type = type; 1238 dmd->dmd_offset = offset; 1239 dmd->dmd_value = -1; 1240 1241 ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd); 1242 1243 if (s != NULL) 1244 ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1; 1245 1246 ctb->ctb_file->ctf_flags |= LCTF_DIRTY; 1247 return (0); 1248 } 1249 1250 /* 1251 * The ctf_add_type routine is used to copy a type from a source CTF container 1252 * to a dynamic destination container. This routine operates recursively by 1253 * following the source type's links and embedded member types. If the 1254 * destination container already contains a named type which has the same 1255 * attributes, then we succeed and return this type but no changes occur. 1256 */ 1257 ctf_id_t 1258 ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type) 1259 { 1260 ctf_id_t dst_type = CTF_ERR; 1261 uint_t dst_kind = CTF_K_UNKNOWN; 1262 1263 const ctf_type_t *tp; 1264 const char *name; 1265 uint_t kind, flag, vlen; 1266 1267 ctf_bundle_t src, dst; 1268 ctf_encoding_t src_en, dst_en; 1269 ctf_arinfo_t src_ar, dst_ar; 1270 1271 ctf_dtdef_t *dtd; 1272 ctf_funcinfo_t ctc; 1273 ssize_t size; 1274 1275 ctf_hash_t *hp; 1276 ctf_helem_t *hep; 1277 1278 if (dst_fp == src_fp) 1279 return (src_type); 1280 1281 if (!(dst_fp->ctf_flags & LCTF_RDWR)) 1282 return (ctf_set_errno(dst_fp, ECTF_RDONLY)); 1283 1284 if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL) 1285 return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1286 1287 name = ctf_strptr(src_fp, tp->ctt_name); 1288 kind = LCTF_INFO_KIND(src_fp, tp->ctt_info); 1289 flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info); 1290 vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info); 1291 1292 switch (kind) { 1293 case CTF_K_STRUCT: 1294 hp = &dst_fp->ctf_structs; 1295 break; 1296 case CTF_K_UNION: 1297 hp = &dst_fp->ctf_unions; 1298 break; 1299 case CTF_K_ENUM: 1300 hp = &dst_fp->ctf_enums; 1301 break; 1302 default: 1303 hp = &dst_fp->ctf_names; 1304 break; 1305 } 1306 1307 /* 1308 * If the source type has a name and is a root type (visible at the 1309 * top-level scope), lookup the name in the destination container and 1310 * verify that it is of the same kind before we do anything else. 1311 */ 1312 if ((flag & CTF_ADD_ROOT) && name[0] != '\0' && 1313 (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) { 1314 dst_type = (ctf_id_t)hep->h_type; 1315 dst_kind = ctf_type_kind(dst_fp, dst_type); 1316 } 1317 1318 /* 1319 * If an identically named dst_type exists, fail with ECTF_CONFLICT 1320 * unless dst_type is a forward declaration and src_type is a struct, 1321 * union, or enum (i.e. the definition of the previous forward decl). 1322 */ 1323 if (dst_type != CTF_ERR && dst_kind != kind) { 1324 if (dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM && 1325 kind != CTF_K_STRUCT && kind != CTF_K_UNION)) 1326 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1327 else 1328 dst_type = CTF_ERR; 1329 } 1330 1331 /* 1332 * If the non-empty name was not found in the appropriate hash, search 1333 * the list of pending dynamic definitions that are not yet committed. 1334 * If a matching name and kind are found, assume this is the type that 1335 * we are looking for. This is necessary to permit ctf_add_type() to 1336 * operate recursively on entities such as a struct that contains a 1337 * pointer member that refers to the same struct type. 1338 * 1339 * In the case of integer and floating point types, we match using the 1340 * type encoding as well - else we may incorrectly return a bitfield 1341 * type, for instance. 1342 */ 1343 if (dst_type == CTF_ERR && name[0] != '\0') { 1344 for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL && 1345 CTF_TYPE_TO_INDEX(dtd->dtd_type) > dst_fp->ctf_dtoldid; 1346 dtd = ctf_list_prev(dtd)) { 1347 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != kind || 1348 dtd->dtd_name == NULL || 1349 strcmp(dtd->dtd_name, name) != 0) 1350 continue; 1351 if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT) { 1352 if (ctf_type_encoding(src_fp, src_type, 1353 &src_en) != 0) 1354 continue; 1355 if (bcmp(&src_en, &dtd->dtd_u.dtu_enc, 1356 sizeof (ctf_encoding_t)) != 0) 1357 continue; 1358 } 1359 return (dtd->dtd_type); 1360 } 1361 } 1362 1363 src.ctb_file = src_fp; 1364 src.ctb_type = src_type; 1365 src.ctb_dtd = NULL; 1366 1367 dst.ctb_file = dst_fp; 1368 dst.ctb_type = dst_type; 1369 dst.ctb_dtd = NULL; 1370 1371 /* 1372 * Now perform kind-specific processing. If dst_type is CTF_ERR, then 1373 * we add a new type with the same properties as src_type to dst_fp. 1374 * If dst_type is not CTF_ERR, then we verify that dst_type has the 1375 * same attributes as src_type. We recurse for embedded references. 1376 */ 1377 switch (kind) { 1378 case CTF_K_INTEGER: 1379 case CTF_K_FLOAT: 1380 if (ctf_type_encoding(src_fp, src_type, &src_en) != 0) 1381 return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1382 1383 if (dst_type != CTF_ERR) { 1384 if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0) 1385 return (CTF_ERR); /* errno is set for us */ 1386 1387 if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t))) 1388 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1389 1390 } else if (kind == CTF_K_INTEGER) { 1391 dst_type = ctf_add_integer(dst_fp, flag, name, &src_en); 1392 } else 1393 dst_type = ctf_add_float(dst_fp, flag, name, &src_en); 1394 break; 1395 1396 case CTF_K_POINTER: 1397 case CTF_K_VOLATILE: 1398 case CTF_K_CONST: 1399 case CTF_K_RESTRICT: 1400 src_type = ctf_type_reference(src_fp, src_type); 1401 src_type = ctf_add_type(dst_fp, src_fp, src_type); 1402 1403 if (src_type == CTF_ERR) 1404 return (CTF_ERR); /* errno is set for us */ 1405 1406 dst_type = ctf_add_reftype(dst_fp, flag, src_type, kind); 1407 break; 1408 1409 case CTF_K_ARRAY: 1410 if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR) 1411 return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1412 1413 src_ar.ctr_contents = 1414 ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents); 1415 src_ar.ctr_index = 1416 ctf_add_type(dst_fp, src_fp, src_ar.ctr_index); 1417 src_ar.ctr_nelems = src_ar.ctr_nelems; 1418 1419 if (src_ar.ctr_contents == CTF_ERR || 1420 src_ar.ctr_index == CTF_ERR) 1421 return (CTF_ERR); /* errno is set for us */ 1422 1423 if (dst_type != CTF_ERR) { 1424 if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0) 1425 return (CTF_ERR); /* errno is set for us */ 1426 1427 if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t))) 1428 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1429 } else 1430 dst_type = ctf_add_array(dst_fp, flag, &src_ar); 1431 break; 1432 1433 case CTF_K_FUNCTION: 1434 ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type); 1435 ctc.ctc_argc = 0; 1436 ctc.ctc_flags = 0; 1437 1438 if (ctc.ctc_return == CTF_ERR) 1439 return (CTF_ERR); /* errno is set for us */ 1440 1441 dst_type = ctf_add_function(dst_fp, flag, &ctc, NULL); 1442 break; 1443 1444 case CTF_K_STRUCT: 1445 case CTF_K_UNION: { 1446 ctf_dmdef_t *dmd; 1447 int errs = 0; 1448 1449 /* 1450 * Technically to match a struct or union we need to check both 1451 * ways (src members vs. dst, dst members vs. src) but we make 1452 * this more optimal by only checking src vs. dst and comparing 1453 * the total size of the structure (which we must do anyway) 1454 * which covers the possibility of dst members not in src. 1455 * This optimization can be defeated for unions, but is so 1456 * pathological as to render it irrelevant for our purposes. 1457 */ 1458 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 1459 if (ctf_type_size(src_fp, src_type) != 1460 ctf_type_size(dst_fp, dst_type)) 1461 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1462 1463 if (ctf_member_iter(src_fp, src_type, membcmp, &dst)) 1464 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1465 1466 break; 1467 } 1468 1469 /* 1470 * Unlike the other cases, copying structs and unions is done 1471 * manually so as to avoid repeated lookups in ctf_add_member 1472 * and to ensure the exact same member offsets as in src_type. 1473 */ 1474 dst_type = ctf_add_generic(dst_fp, flag, name, &dtd); 1475 if (dst_type == CTF_ERR) 1476 return (CTF_ERR); /* errno is set for us */ 1477 1478 dst.ctb_type = dst_type; 1479 dst.ctb_dtd = dtd; 1480 1481 if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0) 1482 errs++; /* increment errs and fail at bottom of case */ 1483 1484 if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) { 1485 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 1486 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size); 1487 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size); 1488 } else 1489 dtd->dtd_data.ctt_size = (ushort_t)size; 1490 1491 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen); 1492 1493 /* 1494 * Make a final pass through the members changing each dmd_type 1495 * (a src_fp type) to an equivalent type in dst_fp. We pass 1496 * through all members, leaving any that fail set to CTF_ERR. 1497 */ 1498 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1499 dmd != NULL; dmd = ctf_list_next(dmd)) { 1500 if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp, 1501 dmd->dmd_type)) == CTF_ERR) 1502 errs++; 1503 } 1504 1505 if (errs) 1506 return (CTF_ERR); /* errno is set for us */ 1507 1508 /* 1509 * Now that we know that we can't fail, we go through and bump 1510 * all the reference counts on the member types. 1511 */ 1512 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1513 dmd != NULL; dmd = ctf_list_next(dmd)) 1514 ctf_ref_inc(dst_fp, dmd->dmd_type); 1515 break; 1516 } 1517 1518 case CTF_K_ENUM: 1519 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 1520 if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) || 1521 ctf_enum_iter(dst_fp, dst_type, enumcmp, &src)) 1522 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1523 } else { 1524 dst_type = ctf_add_enum(dst_fp, flag, name); 1525 if ((dst.ctb_type = dst_type) == CTF_ERR || 1526 ctf_enum_iter(src_fp, src_type, enumadd, &dst)) 1527 return (CTF_ERR); /* errno is set for us */ 1528 } 1529 break; 1530 1531 case CTF_K_FORWARD: 1532 if (dst_type == CTF_ERR) { 1533 dst_type = ctf_add_forward(dst_fp, 1534 flag, name, CTF_K_STRUCT); /* assume STRUCT */ 1535 } 1536 break; 1537 1538 case CTF_K_TYPEDEF: 1539 src_type = ctf_type_reference(src_fp, src_type); 1540 src_type = ctf_add_type(dst_fp, src_fp, src_type); 1541 1542 if (src_type == CTF_ERR) 1543 return (CTF_ERR); /* errno is set for us */ 1544 1545 /* 1546 * If dst_type is not CTF_ERR at this point, we should check if 1547 * ctf_type_reference(dst_fp, dst_type) != src_type and if so 1548 * fail with ECTF_CONFLICT. However, this causes problems with 1549 * <sys/types.h> typedefs that vary based on things like if 1550 * _ILP32x then pid_t is int otherwise long. We therefore omit 1551 * this check and assume that if the identically named typedef 1552 * already exists in dst_fp, it is correct or equivalent. 1553 */ 1554 if (dst_type == CTF_ERR) { 1555 dst_type = ctf_add_typedef(dst_fp, flag, 1556 name, src_type); 1557 } 1558 break; 1559 1560 default: 1561 return (ctf_set_errno(dst_fp, ECTF_CORRUPT)); 1562 } 1563 1564 return (dst_type); 1565 } 1566