1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 /* 28 * Copyright 2020 Joyent, Inc. 29 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association. 30 * Copyright 2025 Oxide Computer Company 31 */ 32 33 #include <sys/sysmacros.h> 34 #include <sys/param.h> 35 #include <sys/mman.h> 36 #include <ctf_impl.h> 37 #include <sys/debug.h> 38 39 /* 40 * SSIZE_MAX is not available in the kernel, so we define it here rather than 41 * accidentally inject into headers where it's not wanted. 42 */ 43 #ifndef SSIZE_MAX 44 #define SSIZE_MAX (LONG_MAX) 45 #endif 46 47 /* 48 * This static string is used as the template for initially populating a 49 * dynamic container's string table. We always store \0 in the first byte, 50 * and we use the generic string "PARENT" to mark this container's parent 51 * if one is associated with the container using ctf_import(). 52 */ 53 static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT"; 54 55 /* 56 * To create an empty CTF container, we just declare a zeroed header and call 57 * ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w 58 * and initialize the dynamic members. We set dtstrlen to 1 to reserve the 59 * first byte of the string table for a \0 byte, and we start assigning type 60 * IDs at 1 because type ID 0 is used as a sentinel. 61 */ 62 ctf_file_t * 63 ctf_create(int *errp) 64 { 65 static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } }; 66 67 const ulong_t hashlen = 128; 68 ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *)); 69 ctf_sect_t cts; 70 ctf_file_t *fp; 71 72 if (hash == NULL) 73 return (ctf_set_open_errno(errp, EAGAIN)); 74 75 cts.cts_name = _CTF_SECTION; 76 cts.cts_type = SHT_PROGBITS; 77 cts.cts_flags = 0; 78 cts.cts_data = &hdr; 79 cts.cts_size = sizeof (hdr); 80 cts.cts_entsize = 1; 81 cts.cts_offset = 0; 82 83 if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) { 84 ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *)); 85 return (NULL); 86 } 87 88 fp->ctf_flags |= LCTF_RDWR; 89 fp->ctf_dthashlen = hashlen; 90 bzero(hash, hashlen * sizeof (ctf_dtdef_t *)); 91 fp->ctf_dthash = hash; 92 fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE); 93 fp->ctf_dtnextid = 1; 94 fp->ctf_dtoldid = 0; 95 96 return (fp); 97 } 98 99 ctf_file_t * 100 ctf_fdcreate(int fd, int *errp) 101 { 102 ctf_file_t *fp; 103 static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } }; 104 105 const ulong_t hashlen = 128; 106 ctf_dtdef_t **hash; 107 ctf_sect_t cts; 108 109 if (fd == -1) 110 return (ctf_create(errp)); 111 112 hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *)); 113 114 if (hash == NULL) 115 return (ctf_set_open_errno(errp, EAGAIN)); 116 117 cts.cts_name = _CTF_SECTION; 118 cts.cts_type = SHT_PROGBITS; 119 cts.cts_flags = 0; 120 cts.cts_data = &hdr; 121 cts.cts_size = sizeof (hdr); 122 cts.cts_entsize = 1; 123 cts.cts_offset = 0; 124 125 if ((fp = ctf_fdcreate_int(fd, errp, &cts)) == NULL) { 126 ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *)); 127 return (NULL); 128 } 129 130 fp->ctf_flags |= LCTF_RDWR; 131 fp->ctf_dthashlen = hashlen; 132 bzero(hash, hashlen * sizeof (ctf_dtdef_t *)); 133 fp->ctf_dthash = hash; 134 fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE); 135 fp->ctf_dtnextid = 1; 136 fp->ctf_dtoldid = 0; 137 138 return (fp); 139 } 140 141 static uchar_t * 142 ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 143 { 144 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 145 ctf_member_t ctm; 146 147 for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 148 if (dmd->dmd_name) { 149 ctm.ctm_name = soff; 150 soff += strlen(dmd->dmd_name) + 1; 151 } else 152 ctm.ctm_name = 0; 153 154 ctm.ctm_type = (ushort_t)dmd->dmd_type; 155 ctm.ctm_offset = (ushort_t)dmd->dmd_offset; 156 157 bcopy(&ctm, t, sizeof (ctm)); 158 t += sizeof (ctm); 159 } 160 161 return (t); 162 } 163 164 static uchar_t * 165 ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 166 { 167 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 168 ctf_lmember_t ctlm; 169 170 for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 171 if (dmd->dmd_name) { 172 ctlm.ctlm_name = soff; 173 soff += strlen(dmd->dmd_name) + 1; 174 } else 175 ctlm.ctlm_name = 0; 176 177 ctlm.ctlm_type = (ushort_t)dmd->dmd_type; 178 ctlm.ctlm_pad = 0; 179 ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset); 180 ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset); 181 182 bcopy(&ctlm, t, sizeof (ctlm)); 183 t += sizeof (ctlm); 184 } 185 186 return (t); 187 } 188 189 static uchar_t * 190 ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 191 { 192 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 193 ctf_enum_t cte; 194 195 for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 196 cte.cte_name = soff; 197 cte.cte_value = dmd->dmd_value; 198 soff += strlen(dmd->dmd_name) + 1; 199 bcopy(&cte, t, sizeof (cte)); 200 t += sizeof (cte); 201 } 202 203 return (t); 204 } 205 206 static uchar_t * 207 ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s) 208 { 209 ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 210 size_t len; 211 212 for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 213 if (dmd->dmd_name == NULL) 214 continue; /* skip anonymous members */ 215 len = strlen(dmd->dmd_name) + 1; 216 bcopy(dmd->dmd_name, s, len); 217 s += len; 218 } 219 220 return (s); 221 } 222 223 /* 224 * Only types of dyanmic CTF containers contain reference counts. These 225 * containers are marked RD/WR. Because of that we basically make this a no-op 226 * for compatability with non-dynamic CTF sections. This is also a no-op for 227 * types which are not dynamic types. It is the responsibility of the caller to 228 * make sure it is a valid type. We help that caller out on debug builds. 229 * 230 * Note that the reference counts are not maintained for types that are not 231 * within this container. In other words if we have a type in a parent, that 232 * will not have its reference count increased. On the flip side, the parent 233 * will not be allowed to remove dynamic types if it has children. 234 */ 235 static void 236 ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid) 237 { 238 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid); 239 240 if (dtd == NULL) 241 return; 242 243 if (!(fp->ctf_flags & LCTF_RDWR)) 244 return; 245 246 dtd->dtd_ref++; 247 } 248 249 /* 250 * Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the 251 * caller should ensure that this is already a valid type. 252 */ 253 static void 254 ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid) 255 { 256 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid); 257 258 if (dtd == NULL) 259 return; 260 261 if (!(fp->ctf_flags & LCTF_RDWR)) 262 return; 263 264 ASSERT(dtd->dtd_ref >= 1); 265 dtd->dtd_ref--; 266 } 267 268 /* 269 * If the specified CTF container is writable and has been modified, reload 270 * this container with the updated type definitions. In order to make this 271 * code and the rest of libctf as simple as possible, we perform updates by 272 * taking the dynamic type definitions and creating an in-memory CTF file 273 * containing the definitions, and then call ctf_bufopen() on it. This not 274 * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest 275 * of the library code with different lookup paths for static and dynamic 276 * type definitions. We are therefore optimizing greatly for lookup over 277 * update, which we assume will be an uncommon operation. We perform one 278 * extra trick here for the benefit of callers and to keep our code simple: 279 * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp 280 * constant for the caller, so after ctf_bufopen() returns, we use bcopy to 281 * swap the interior of the old and new ctf_file_t's, and then free the old. 282 * 283 * Note that the lists of dynamic types stays around and the resulting container 284 * is still writeable. Furthermore, the reference counts that are on the dtd's 285 * are still valid. 286 */ 287 int 288 ctf_update(ctf_file_t *fp) 289 { 290 ctf_file_t ofp, *nfp; 291 ctf_header_t hdr, *bhdr; 292 ctf_dtdef_t *dtd; 293 ctf_dsdef_t *dsd; 294 ctf_dldef_t *dld; 295 ctf_sect_t cts, *symp, *strp; 296 297 uchar_t *s, *s0, *t; 298 ctf_lblent_t *label; 299 uint16_t *obj, *func; 300 size_t size, objsize, funcsize, labelsize, plen; 301 void *buf; 302 int err; 303 ulong_t i; 304 const char *plabel; 305 const char *sname; 306 307 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data; 308 uintptr_t strbase = (uintptr_t)fp->ctf_strtab.cts_data; 309 310 if (!(fp->ctf_flags & LCTF_RDWR)) 311 return (ctf_set_errno(fp, ECTF_RDONLY)); 312 313 if (!(fp->ctf_flags & LCTF_DIRTY)) 314 return (0); /* no update required */ 315 316 /* 317 * Fill in an initial CTF header. We will leave the label, object, 318 * and function sections empty and only output a header, type section, 319 * and string table. The type section begins at a 4-byte aligned 320 * boundary past the CTF header itself (at relative offset zero). 321 */ 322 bzero(&hdr, sizeof (hdr)); 323 hdr.cth_magic = CTF_MAGIC; 324 hdr.cth_version = CTF_VERSION; 325 326 if (fp->ctf_flags & LCTF_CHILD) { 327 if (fp->ctf_parname == NULL) { 328 plen = 0; 329 hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */ 330 plabel = NULL; 331 } else { 332 plen = strlen(fp->ctf_parname) + 1; 333 plabel = ctf_label_topmost(fp->ctf_parent); 334 } 335 } else { 336 plabel = NULL; 337 plen = 0; 338 } 339 340 /* 341 * Iterate over the labels that we have. 342 */ 343 for (labelsize = 0, dld = ctf_list_next(&fp->ctf_dldefs); 344 dld != NULL; dld = ctf_list_next(dld)) 345 labelsize += sizeof (ctf_lblent_t); 346 347 /* 348 * Iterate through the dynamic type definition list and compute the 349 * size of the CTF type section we will need to generate. 350 */ 351 for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs); 352 dtd != NULL; dtd = ctf_list_next(dtd)) { 353 354 uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 355 uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 356 357 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) 358 size += sizeof (ctf_stype_t); 359 else 360 size += sizeof (ctf_type_t); 361 362 switch (kind) { 363 case CTF_K_INTEGER: 364 case CTF_K_FLOAT: 365 size += sizeof (uint_t); 366 break; 367 case CTF_K_ARRAY: 368 size += sizeof (ctf_array_t); 369 break; 370 case CTF_K_FUNCTION: 371 size += sizeof (ushort_t) * (vlen + (vlen & 1)); 372 break; 373 case CTF_K_STRUCT: 374 case CTF_K_UNION: 375 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) 376 size += sizeof (ctf_member_t) * vlen; 377 else 378 size += sizeof (ctf_lmember_t) * vlen; 379 break; 380 case CTF_K_ENUM: 381 size += sizeof (ctf_enum_t) * vlen; 382 break; 383 } 384 } 385 386 /* 387 * An entry for each object must exist in the data section. However, if 388 * the symbol is SHN_UNDEF, then it is skipped. For objects, the storage 389 * is just the size of the 2-byte id. For functions it's always 2 bytes, 390 * plus 2 bytes per argument and the return type. 391 */ 392 dsd = ctf_list_next(&fp->ctf_dsdefs); 393 for (objsize = 0, funcsize = 0, i = 0; i < fp->ctf_nsyms; i++) { 394 int type; 395 396 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 397 const Elf32_Sym *symp = (Elf32_Sym *)symbase + i; 398 399 type = ELF32_ST_TYPE(symp->st_info); 400 if (ctf_sym_valid(strbase, type, symp->st_shndx, 401 symp->st_value, symp->st_name) == B_FALSE) 402 continue; 403 } else { 404 const Elf64_Sym *symp = (Elf64_Sym *)symbase + i; 405 406 type = ELF64_ST_TYPE(symp->st_info); 407 if (ctf_sym_valid(strbase, type, symp->st_shndx, 408 symp->st_value, symp->st_name) == B_FALSE) 409 continue; 410 } 411 412 while (dsd != NULL && i > dsd->dsd_symidx) 413 dsd = ctf_list_next(dsd); 414 if (type == STT_OBJECT) { 415 objsize += sizeof (uint16_t); 416 } else { 417 /* Every function has a uint16_t info no matter what */ 418 if (dsd == NULL || i < dsd->dsd_symidx) { 419 funcsize += sizeof (uint16_t); 420 } else { 421 funcsize += sizeof (uint16_t) * 422 (dsd->dsd_nargs + 2); 423 } 424 } 425 } 426 427 /* 428 * The objtoff and funcoffset must be 2-byte aligned. We're guaranteed 429 * that this is always true for the objtoff because labels are always 8 430 * bytes large. Similarly, because objects are always two bytes of data, 431 * this will always be true for funcoff. 432 */ 433 hdr.cth_objtoff = hdr.cth_lbloff + labelsize; 434 hdr.cth_funcoff = hdr.cth_objtoff + objsize; 435 436 /* 437 * The type offset must be 4 byte aligned. 438 */ 439 hdr.cth_typeoff = hdr.cth_funcoff + funcsize; 440 if (hdr.cth_typeoff & 3) 441 hdr.cth_typeoff += 4 - (hdr.cth_typeoff & 3); 442 ASSERT((hdr.cth_typeoff & 3) == 0); 443 444 /* 445 * Fill in the string table offset and size, compute the size of the 446 * entire CTF buffer we need, and then allocate a new buffer and 447 * bcopy the finished header to the start of the buffer. 448 */ 449 hdr.cth_stroff = hdr.cth_typeoff + size; 450 hdr.cth_strlen = fp->ctf_dtstrlen + plen; 451 size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen; 452 ctf_dprintf("lbloff: %u\nobjtoff: %u\nfuncoff: %u\n" 453 "typeoff: %u\nstroff: %u\nstrlen: %u\n", 454 hdr.cth_lbloff, hdr.cth_objtoff, hdr.cth_funcoff, 455 hdr.cth_typeoff, hdr.cth_stroff, hdr.cth_strlen); 456 457 if ((buf = ctf_data_alloc(size)) == MAP_FAILED) 458 return (ctf_set_errno(fp, EAGAIN)); 459 460 bcopy(&hdr, buf, sizeof (ctf_header_t)); 461 bhdr = buf; 462 label = (ctf_lblent_t *)((uintptr_t)buf + sizeof (ctf_header_t)); 463 t = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_typeoff; 464 s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff; 465 obj = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) + 466 hdr.cth_objtoff); 467 func = (uint16_t *)((uintptr_t)buf + sizeof (ctf_header_t) + 468 hdr.cth_funcoff); 469 470 bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE)); 471 s += sizeof (_CTF_STRTAB_TEMPLATE); 472 473 /* 474 * We have an actual parent name and we're a child container, therefore 475 * we should make sure to note our parent's name here. 476 */ 477 if (plen != 0) { 478 VERIFY(s + plen - s0 <= hdr.cth_strlen); 479 bcopy(fp->ctf_parname, s, plen); 480 bhdr->cth_parname = s - s0; 481 s += plen; 482 } 483 484 /* 485 * First pass over the labels and copy them out. 486 */ 487 for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL; 488 dld = ctf_list_next(dld), label++) { 489 size_t len = strlen(dld->dld_name) + 1; 490 491 VERIFY(s + len - s0 <= hdr.cth_strlen); 492 bcopy(dld->dld_name, s, len); 493 label->ctl_typeidx = dld->dld_type; 494 label->ctl_label = s - s0; 495 s += len; 496 497 if (plabel != NULL && strcmp(plabel, dld->dld_name) == 0) 498 bhdr->cth_parlabel = label->ctl_label; 499 } 500 501 /* 502 * We now take a final lap through the dynamic type definition list and 503 * copy the appropriate type records and strings to the output buffer. 504 */ 505 for (dtd = ctf_list_next(&fp->ctf_dtdefs); 506 dtd != NULL; dtd = ctf_list_next(dtd)) { 507 508 uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 509 uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 510 511 ctf_array_t cta; 512 uint_t encoding; 513 size_t len; 514 515 if (dtd->dtd_name != NULL) { 516 dtd->dtd_data.ctt_name = (uint_t)(s - s0); 517 len = strlen(dtd->dtd_name) + 1; 518 VERIFY(s + len - s0 <= hdr.cth_strlen); 519 bcopy(dtd->dtd_name, s, len); 520 s += len; 521 } else 522 dtd->dtd_data.ctt_name = 0; 523 524 if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) 525 len = sizeof (ctf_stype_t); 526 else 527 len = sizeof (ctf_type_t); 528 529 bcopy(&dtd->dtd_data, t, len); 530 t += len; 531 532 switch (kind) { 533 case CTF_K_INTEGER: 534 case CTF_K_FLOAT: 535 if (kind == CTF_K_INTEGER) { 536 encoding = CTF_INT_DATA( 537 dtd->dtd_u.dtu_enc.cte_format, 538 dtd->dtd_u.dtu_enc.cte_offset, 539 dtd->dtd_u.dtu_enc.cte_bits); 540 } else { 541 encoding = CTF_FP_DATA( 542 dtd->dtd_u.dtu_enc.cte_format, 543 dtd->dtd_u.dtu_enc.cte_offset, 544 dtd->dtd_u.dtu_enc.cte_bits); 545 } 546 bcopy(&encoding, t, sizeof (encoding)); 547 t += sizeof (encoding); 548 break; 549 550 case CTF_K_ARRAY: 551 cta.cta_contents = (ushort_t) 552 dtd->dtd_u.dtu_arr.ctr_contents; 553 cta.cta_index = (ushort_t) 554 dtd->dtd_u.dtu_arr.ctr_index; 555 cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems; 556 bcopy(&cta, t, sizeof (cta)); 557 t += sizeof (cta); 558 break; 559 560 case CTF_K_FUNCTION: { 561 ushort_t *argv = (ushort_t *)(uintptr_t)t; 562 uint_t argc; 563 564 for (argc = 0; argc < vlen; argc++) 565 *argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc]; 566 567 if (vlen & 1) 568 *argv++ = 0; /* pad to 4-byte boundary */ 569 570 t = (uchar_t *)argv; 571 break; 572 } 573 574 case CTF_K_STRUCT: 575 case CTF_K_UNION: 576 if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) 577 t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t); 578 else 579 t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t); 580 s = ctf_copy_membnames(dtd, s); 581 break; 582 583 case CTF_K_ENUM: 584 t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t); 585 s = ctf_copy_membnames(dtd, s); 586 break; 587 } 588 } 589 590 /* 591 * Now we fill in our dynamic data and function sections. We use the 592 * same criteria as above, but also consult the dsd list. 593 */ 594 dsd = ctf_list_next(&fp->ctf_dsdefs); 595 for (i = 0; i < fp->ctf_nsyms; i++) { 596 int type; 597 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 598 const Elf32_Sym *symp = (Elf32_Sym *)symbase + i; 599 type = ELF32_ST_TYPE(symp->st_info); 600 601 if (ctf_sym_valid(strbase, type, symp->st_shndx, 602 symp->st_value, symp->st_name) == B_FALSE) 603 continue; 604 } else { 605 const Elf64_Sym *symp = (Elf64_Sym *)symbase + i; 606 type = ELF64_ST_TYPE(symp->st_info); 607 if (ctf_sym_valid(strbase, type, symp->st_shndx, 608 symp->st_value, symp->st_name) == B_FALSE) 609 continue; 610 } 611 612 while (dsd != NULL && i > dsd->dsd_symidx) { 613 dsd = ctf_list_next(dsd); 614 } 615 if (type == STT_OBJECT) { 616 if (dsd == NULL || i < dsd->dsd_symidx) { 617 *obj = 0; 618 } else { 619 *obj = dsd->dsd_tid; 620 } 621 obj++; 622 VERIFY((uintptr_t)obj <= (uintptr_t)func); 623 } else { 624 if (dsd == NULL || i < dsd->dsd_symidx) { 625 ushort_t data = CTF_TYPE_INFO(CTF_K_UNKNOWN, 626 0, 0); 627 *func = data; 628 func++; 629 } else { 630 int j; 631 ushort_t data = CTF_TYPE_INFO(CTF_K_FUNCTION, 0, 632 dsd->dsd_nargs); 633 634 *func = data; 635 func++; 636 *func = dsd->dsd_tid; 637 func++; 638 for (j = 0; j < dsd->dsd_nargs; j++) 639 func[j] = dsd->dsd_argc[j]; 640 func += dsd->dsd_nargs; 641 } 642 } 643 } 644 645 /* 646 * Finally, we are ready to ctf_bufopen() the new container. If this 647 * is successful, we then switch nfp and fp and free the old container. 648 */ 649 ctf_data_protect(buf, size); 650 cts.cts_name = _CTF_SECTION; 651 cts.cts_type = SHT_PROGBITS; 652 cts.cts_flags = 0; 653 cts.cts_data = buf; 654 cts.cts_size = size; 655 cts.cts_entsize = 1; 656 cts.cts_offset = 0; 657 658 if (fp->ctf_nsyms == 0) { 659 symp = NULL; 660 strp = NULL; 661 } else { 662 symp = &fp->ctf_symtab; 663 strp = &fp->ctf_strtab; 664 } 665 666 if ((nfp = ctf_bufopen(&cts, symp, strp, &err)) == NULL) { 667 ctf_data_free(buf, size); 668 return (ctf_set_errno(fp, err)); 669 } 670 671 (void) ctf_setmodel(nfp, ctf_getmodel(fp)); 672 (void) ctf_import(nfp, fp->ctf_parent); 673 674 nfp->ctf_refcnt = fp->ctf_refcnt; 675 nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY; 676 nfp->ctf_flags |= LCTF_FREE; 677 nfp->ctf_dthash = fp->ctf_dthash; 678 nfp->ctf_dthashlen = fp->ctf_dthashlen; 679 nfp->ctf_dtdefs = fp->ctf_dtdefs; 680 nfp->ctf_dsdefs = fp->ctf_dsdefs; 681 nfp->ctf_dldefs = fp->ctf_dldefs; 682 nfp->ctf_dtstrlen = fp->ctf_dtstrlen; 683 nfp->ctf_dtnextid = fp->ctf_dtnextid; 684 nfp->ctf_dtoldid = fp->ctf_dtnextid - 1; 685 nfp->ctf_specific = fp->ctf_specific; 686 687 fp->ctf_dthash = NULL; 688 fp->ctf_dthashlen = 0; 689 bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t)); 690 bzero(&fp->ctf_dsdefs, sizeof (ctf_list_t)); 691 bzero(&fp->ctf_dldefs, sizeof (ctf_list_t)); 692 693 /* 694 * Because the various containers share the data sections, we don't want 695 * to have ctf_close free it all. However, the name of the section is in 696 * fact unique to the ctf_sect_t. Thus we save the names of the symbol 697 * and string sections around the bzero() and restore them afterwards, 698 * ensuring that we don't result in a memory leak. 699 */ 700 sname = fp->ctf_symtab.cts_name; 701 bzero(&fp->ctf_symtab, sizeof (ctf_sect_t)); 702 fp->ctf_symtab.cts_name = sname; 703 704 sname = fp->ctf_strtab.cts_name; 705 bzero(&fp->ctf_strtab, sizeof (ctf_sect_t)); 706 fp->ctf_strtab.cts_name = sname; 707 708 bcopy(fp, &ofp, sizeof (ctf_file_t)); 709 bcopy(nfp, fp, sizeof (ctf_file_t)); 710 bcopy(&ofp, nfp, sizeof (ctf_file_t)); 711 712 /* 713 * Initialize the ctf_lookup_by_name top-level dictionary. We keep an 714 * array of type name prefixes and the corresponding ctf_hash to use. 715 * NOTE: This code must be kept in sync with the code in ctf_bufopen(). 716 */ 717 fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs; 718 fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions; 719 fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums; 720 fp->ctf_lookups[3].ctl_hash = &fp->ctf_names; 721 722 nfp->ctf_refcnt = 1; /* force nfp to be freed */ 723 ctf_close(nfp); 724 725 return (0); 726 } 727 728 void 729 ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd) 730 { 731 ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1); 732 733 dtd->dtd_hash = fp->ctf_dthash[h]; 734 fp->ctf_dthash[h] = dtd; 735 ctf_list_append(&fp->ctf_dtdefs, dtd); 736 } 737 738 void 739 ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd) 740 { 741 ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1); 742 ctf_dtdef_t *p, **q = &fp->ctf_dthash[h]; 743 ctf_dmdef_t *dmd, *nmd; 744 size_t len; 745 int kind, i; 746 747 for (p = *q; p != NULL; p = p->dtd_hash) { 748 if (p != dtd) 749 q = &p->dtd_hash; 750 else 751 break; 752 } 753 754 if (p != NULL) 755 *q = p->dtd_hash; 756 757 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 758 switch (kind) { 759 case CTF_K_STRUCT: 760 case CTF_K_UNION: 761 case CTF_K_ENUM: 762 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 763 dmd != NULL; dmd = nmd) { 764 if (dmd->dmd_name != NULL) { 765 len = strlen(dmd->dmd_name) + 1; 766 ctf_free(dmd->dmd_name, len); 767 fp->ctf_dtstrlen -= len; 768 } 769 if (kind != CTF_K_ENUM) 770 ctf_ref_dec(fp, dmd->dmd_type); 771 nmd = ctf_list_next(dmd); 772 ctf_free(dmd, sizeof (ctf_dmdef_t)); 773 } 774 break; 775 case CTF_K_FUNCTION: 776 ctf_ref_dec(fp, dtd->dtd_data.ctt_type); 777 for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++) 778 if (dtd->dtd_u.dtu_argv[i] != 0) 779 ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]); 780 ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) * 781 CTF_INFO_VLEN(dtd->dtd_data.ctt_info)); 782 break; 783 case CTF_K_ARRAY: 784 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents); 785 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index); 786 break; 787 case CTF_K_TYPEDEF: 788 ctf_ref_dec(fp, dtd->dtd_data.ctt_type); 789 break; 790 case CTF_K_POINTER: 791 case CTF_K_VOLATILE: 792 case CTF_K_CONST: 793 case CTF_K_RESTRICT: 794 ctf_ref_dec(fp, dtd->dtd_data.ctt_type); 795 break; 796 } 797 798 if (dtd->dtd_name) { 799 len = strlen(dtd->dtd_name) + 1; 800 ctf_free(dtd->dtd_name, len); 801 fp->ctf_dtstrlen -= len; 802 } 803 804 ctf_list_delete(&fp->ctf_dtdefs, dtd); 805 ctf_free(dtd, sizeof (ctf_dtdef_t)); 806 } 807 808 ctf_dtdef_t * 809 ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type) 810 { 811 ulong_t h = type & (fp->ctf_dthashlen - 1); 812 ctf_dtdef_t *dtd; 813 814 if (fp->ctf_dthash == NULL) 815 return (NULL); 816 817 for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) { 818 if (dtd->dtd_type == type) 819 break; 820 } 821 822 return (dtd); 823 } 824 825 ctf_dsdef_t * 826 ctf_dsd_lookup(ctf_file_t *fp, ulong_t idx) 827 { 828 ctf_dsdef_t *dsd; 829 830 for (dsd = ctf_list_next(&fp->ctf_dsdefs); dsd != NULL; 831 dsd = ctf_list_next(dsd)) { 832 if (dsd->dsd_symidx == idx) 833 return (dsd); 834 } 835 836 return (NULL); 837 } 838 839 /* 840 * We order the ctf_dsdef_t by symbol index to make things better for updates. 841 */ 842 void 843 ctf_dsd_insert(ctf_file_t *fp, ctf_dsdef_t *dsd) 844 { 845 ctf_dsdef_t *i; 846 847 for (i = ctf_list_next(&fp->ctf_dsdefs); i != NULL; 848 i = ctf_list_next(i)) { 849 if (i->dsd_symidx > dsd->dsd_symidx) 850 break; 851 } 852 853 if (i == NULL) { 854 ctf_list_append(&fp->ctf_dsdefs, dsd); 855 return; 856 } 857 858 ctf_list_insert_before(&fp->ctf_dsdefs, i, dsd); 859 } 860 861 /* ARGSUSED */ 862 void 863 ctf_dsd_delete(ctf_file_t *fp, ctf_dsdef_t *dsd) 864 { 865 if (dsd->dsd_nargs > 0) 866 ctf_free(dsd->dsd_argc, 867 sizeof (ctf_id_t) * dsd->dsd_nargs); 868 ctf_list_delete(&fp->ctf_dsdefs, dsd); 869 ctf_free(dsd, sizeof (ctf_dsdef_t)); 870 } 871 872 ctf_dldef_t * 873 ctf_dld_lookup(ctf_file_t *fp, const char *name) 874 { 875 ctf_dldef_t *dld; 876 877 for (dld = ctf_list_next(&fp->ctf_dldefs); dld != NULL; 878 dld = ctf_list_next(dld)) { 879 if (strcmp(name, dld->dld_name) == 0) 880 return (dld); 881 } 882 883 return (NULL); 884 } 885 886 void 887 ctf_dld_insert(ctf_file_t *fp, ctf_dldef_t *dld, uint_t pos) 888 { 889 ctf_dldef_t *l; 890 891 if (pos == 0) { 892 ctf_list_prepend(&fp->ctf_dldefs, dld); 893 return; 894 } 895 896 for (l = ctf_list_next(&fp->ctf_dldefs); pos != 0 && dld != NULL; 897 l = ctf_list_next(l), pos--) 898 ; 899 900 if (l == NULL) 901 ctf_list_append(&fp->ctf_dldefs, dld); 902 else 903 ctf_list_insert_before(&fp->ctf_dsdefs, l, dld); 904 } 905 906 void 907 ctf_dld_delete(ctf_file_t *fp, ctf_dldef_t *dld) 908 { 909 ctf_list_delete(&fp->ctf_dldefs, dld); 910 911 if (dld->dld_name != NULL) { 912 size_t len = strlen(dld->dld_name) + 1; 913 ctf_free(dld->dld_name, len); 914 fp->ctf_dtstrlen -= len; 915 } 916 917 ctf_free(dld, sizeof (ctf_dldef_t)); 918 } 919 920 /* 921 * Discard all of the dynamic type definitions that have been added to the 922 * container since the last call to ctf_update(). We locate such types by 923 * scanning the list and deleting elements that have type IDs greater than 924 * ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly 925 * with our reference counting schemes, we must delete the dynamic list in 926 * reverse. 927 */ 928 int 929 ctf_discard(ctf_file_t *fp) 930 { 931 ctf_dtdef_t *dtd, *ntd; 932 933 if (!(fp->ctf_flags & LCTF_RDWR)) 934 return (ctf_set_errno(fp, ECTF_RDONLY)); 935 936 if (!(fp->ctf_flags & LCTF_DIRTY)) 937 return (0); /* no update required */ 938 939 for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) { 940 ntd = ctf_list_prev(dtd); 941 if (dtd->dtd_type <= fp->ctf_dtoldid) 942 continue; /* skip types that have been committed */ 943 944 ctf_dtd_delete(fp, dtd); 945 } 946 947 fp->ctf_dtnextid = fp->ctf_dtoldid + 1; 948 fp->ctf_flags &= ~LCTF_DIRTY; 949 950 return (0); 951 } 952 953 static ctf_id_t 954 ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp) 955 { 956 ctf_dtdef_t *dtd; 957 ctf_id_t type; 958 char *s = NULL; 959 960 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT) 961 return (ctf_set_errno(fp, EINVAL)); 962 963 if (!(fp->ctf_flags & LCTF_RDWR)) 964 return (ctf_set_errno(fp, ECTF_RDONLY)); 965 966 if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE) 967 return (ctf_set_errno(fp, ECTF_FULL)); 968 969 if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL) 970 return (ctf_set_errno(fp, EAGAIN)); 971 972 /* 973 * Treat an empty string as a missing name that is anonymous. 974 */ 975 if (name != NULL && *name != '\0' && (s = ctf_strdup(name)) == NULL) { 976 ctf_free(dtd, sizeof (ctf_dtdef_t)); 977 return (ctf_set_errno(fp, EAGAIN)); 978 } 979 980 type = fp->ctf_dtnextid++; 981 type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD)); 982 983 bzero(dtd, sizeof (ctf_dtdef_t)); 984 dtd->dtd_name = s; 985 dtd->dtd_type = type; 986 987 if (s != NULL) 988 fp->ctf_dtstrlen += strlen(s) + 1; 989 990 ctf_dtd_insert(fp, dtd); 991 fp->ctf_flags |= LCTF_DIRTY; 992 993 *rp = dtd; 994 return (type); 995 } 996 997 ctf_id_t 998 ctf_add_encoded(ctf_file_t *fp, uint_t flag, 999 const char *name, const ctf_encoding_t *ep, uint_t kind) 1000 { 1001 ctf_dtdef_t *dtd; 1002 ctf_id_t type; 1003 1004 if (ep == NULL) 1005 return (ctf_set_errno(fp, EINVAL)); 1006 1007 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 1008 return (CTF_ERR); /* errno is set for us */ 1009 1010 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 1011 1012 /* 1013 * If the type's size is not an even number of bytes, then we should 1014 * round up the type size to the nearest byte. 1015 */ 1016 dtd->dtd_data.ctt_size = ep->cte_bits / NBBY; 1017 if ((ep->cte_bits % NBBY) != 0) 1018 dtd->dtd_data.ctt_size++; 1019 dtd->dtd_u.dtu_enc = *ep; 1020 1021 return (type); 1022 } 1023 1024 ctf_id_t 1025 ctf_add_reftype(ctf_file_t *fp, uint_t flag, 1026 const char *name, ctf_id_t ref, uint_t kind) 1027 { 1028 ctf_dtdef_t *dtd; 1029 ctf_id_t type; 1030 1031 if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE) 1032 return (ctf_set_errno(fp, EINVAL)); 1033 1034 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 1035 return (CTF_ERR); /* errno is set for us */ 1036 1037 ctf_ref_inc(fp, ref); 1038 1039 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 1040 dtd->dtd_data.ctt_type = (ushort_t)ref; 1041 1042 return (type); 1043 } 1044 1045 ctf_id_t 1046 ctf_add_integer(ctf_file_t *fp, uint_t flag, 1047 const char *name, const ctf_encoding_t *ep) 1048 { 1049 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER)); 1050 } 1051 1052 ctf_id_t 1053 ctf_add_float(ctf_file_t *fp, uint_t flag, 1054 const char *name, const ctf_encoding_t *ep) 1055 { 1056 return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT)); 1057 } 1058 1059 ctf_id_t 1060 ctf_add_pointer(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 1061 { 1062 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_POINTER)); 1063 } 1064 1065 ctf_id_t 1066 ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp) 1067 { 1068 ctf_dtdef_t *dtd; 1069 ctf_id_t type; 1070 ctf_file_t *fpd; 1071 1072 if (arp == NULL) 1073 return (ctf_set_errno(fp, EINVAL)); 1074 1075 fpd = fp; 1076 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL && 1077 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) { 1078 ctf_dprintf("bad contents for array: %ld\n", 1079 arp->ctr_contents); 1080 return (ctf_set_errno(fp, ECTF_BADID)); 1081 } 1082 1083 fpd = fp; 1084 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL && 1085 ctf_dtd_lookup(fp, arp->ctr_index) == NULL) { 1086 ctf_dprintf("bad index for array: %ld\n", arp->ctr_index); 1087 return (ctf_set_errno(fp, ECTF_BADID)); 1088 } 1089 1090 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) 1091 return (CTF_ERR); /* errno is set for us */ 1092 1093 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0); 1094 dtd->dtd_data.ctt_size = 0; 1095 dtd->dtd_u.dtu_arr = *arp; 1096 ctf_ref_inc(fp, arp->ctr_contents); 1097 ctf_ref_inc(fp, arp->ctr_index); 1098 1099 return (type); 1100 } 1101 1102 int 1103 ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) 1104 { 1105 ctf_file_t *fpd; 1106 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type); 1107 1108 if (!(fp->ctf_flags & LCTF_RDWR)) 1109 return (ctf_set_errno(fp, ECTF_RDONLY)); 1110 1111 if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY) 1112 return (ctf_set_errno(fp, ECTF_BADID)); 1113 1114 fpd = fp; 1115 if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL && 1116 ctf_dtd_lookup(fp, arp->ctr_contents) == NULL) 1117 return (ctf_set_errno(fp, ECTF_BADID)); 1118 1119 fpd = fp; 1120 if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL && 1121 ctf_dtd_lookup(fp, arp->ctr_index) == NULL) 1122 return (ctf_set_errno(fp, ECTF_BADID)); 1123 1124 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents); 1125 ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index); 1126 fp->ctf_flags |= LCTF_DIRTY; 1127 dtd->dtd_u.dtu_arr = *arp; 1128 ctf_ref_inc(fp, arp->ctr_contents); 1129 ctf_ref_inc(fp, arp->ctr_index); 1130 1131 return (0); 1132 } 1133 1134 ctf_id_t 1135 ctf_add_funcptr(ctf_file_t *fp, uint_t flag, 1136 const ctf_funcinfo_t *ctc, const ctf_id_t *argv) 1137 { 1138 ctf_dtdef_t *dtd; 1139 ctf_id_t type; 1140 uint_t vlen; 1141 int i; 1142 ctf_id_t *vdat = NULL; 1143 ctf_file_t *fpd; 1144 1145 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 || 1146 (ctc->ctc_argc != 0 && argv == NULL)) 1147 return (ctf_set_errno(fp, EINVAL)); 1148 1149 vlen = ctc->ctc_argc; 1150 if (ctc->ctc_flags & CTF_FUNC_VARARG) 1151 vlen++; /* add trailing zero to indicate varargs (see below) */ 1152 1153 if (vlen > CTF_MAX_VLEN) 1154 return (ctf_set_errno(fp, EOVERFLOW)); 1155 1156 fpd = fp; 1157 if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL && 1158 ctf_dtd_lookup(fp, ctc->ctc_return) == NULL) 1159 return (ctf_set_errno(fp, ECTF_BADID)); 1160 1161 for (i = 0; i < ctc->ctc_argc; i++) { 1162 fpd = fp; 1163 if (ctf_lookup_by_id(&fpd, argv[i]) == NULL && 1164 ctf_dtd_lookup(fp, argv[i]) == NULL) 1165 return (ctf_set_errno(fp, ECTF_BADID)); 1166 } 1167 1168 if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL) 1169 return (ctf_set_errno(fp, EAGAIN)); 1170 1171 if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) { 1172 ctf_free(vdat, sizeof (ctf_id_t) * vlen); 1173 return (CTF_ERR); /* errno is set for us */ 1174 } 1175 1176 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen); 1177 dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return; 1178 1179 ctf_ref_inc(fp, ctc->ctc_return); 1180 for (i = 0; i < ctc->ctc_argc; i++) 1181 ctf_ref_inc(fp, argv[i]); 1182 1183 bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc); 1184 if (ctc->ctc_flags & CTF_FUNC_VARARG) 1185 vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */ 1186 dtd->dtd_u.dtu_argv = vdat; 1187 1188 return (type); 1189 } 1190 1191 ctf_id_t 1192 ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name) 1193 { 1194 ctf_hash_t *hp = &fp->ctf_structs; 1195 ctf_helem_t *hep = NULL; 1196 ctf_dtdef_t *dtd = NULL; 1197 ctf_id_t type = CTF_ERR; 1198 1199 if (name != NULL) 1200 hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 1201 1202 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) { 1203 type = hep->h_type; 1204 dtd = ctf_dtd_lookup(fp, type); 1205 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD) 1206 dtd = NULL; 1207 } 1208 1209 if (dtd == NULL) { 1210 type = ctf_add_generic(fp, flag, name, &dtd); 1211 if (type == CTF_ERR) 1212 return (CTF_ERR); /* errno is set for us */ 1213 } 1214 1215 VERIFY(type != CTF_ERR); 1216 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0); 1217 dtd->dtd_data.ctt_size = 0; 1218 1219 /* 1220 * Always dirty in case we modified a forward. 1221 */ 1222 fp->ctf_flags |= LCTF_DIRTY; 1223 1224 return (type); 1225 } 1226 1227 ctf_id_t 1228 ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name) 1229 { 1230 ctf_hash_t *hp = &fp->ctf_unions; 1231 ctf_helem_t *hep = NULL; 1232 ctf_dtdef_t *dtd = NULL; 1233 ctf_id_t type = CTF_ERR; 1234 1235 if (name != NULL) 1236 hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 1237 1238 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) { 1239 type = hep->h_type; 1240 dtd = ctf_dtd_lookup(fp, type); 1241 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD) 1242 dtd = NULL; 1243 } 1244 1245 if (dtd == NULL) { 1246 type = ctf_add_generic(fp, flag, name, &dtd); 1247 if (type == CTF_ERR) 1248 return (CTF_ERR); /* errno is set for us */ 1249 } 1250 1251 VERIFY(type != CTF_ERR); 1252 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0); 1253 dtd->dtd_data.ctt_size = 0; 1254 1255 /* 1256 * Always dirty in case we modified a forward. 1257 */ 1258 fp->ctf_flags |= LCTF_DIRTY; 1259 1260 return (type); 1261 } 1262 1263 /* 1264 * If size is 0, we use the standard integer size. This is almost always the 1265 * case, except for packed enums. 1266 */ 1267 ctf_id_t 1268 ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name, size_t size) 1269 { 1270 ctf_hash_t *hp = &fp->ctf_enums; 1271 ctf_helem_t *hep = NULL; 1272 ctf_dtdef_t *dtd = NULL; 1273 ctf_id_t type = CTF_ERR; 1274 1275 /* Check we could return something valid in ctf_type_size. */ 1276 if (size > SSIZE_MAX) 1277 return (ctf_set_errno(fp, EINVAL)); 1278 1279 if (name != NULL) 1280 hep = ctf_hash_lookup(hp, fp, name, strlen(name)); 1281 1282 if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) { 1283 type = hep->h_type; 1284 dtd = ctf_dtd_lookup(fp, type); 1285 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD) 1286 dtd = NULL; 1287 } 1288 1289 if (dtd == NULL) { 1290 type = ctf_add_generic(fp, flag, name, &dtd); 1291 if (type == CTF_ERR) 1292 return (CTF_ERR); /* errno is set for us */ 1293 } 1294 1295 VERIFY(type != CTF_ERR); 1296 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0); 1297 1298 ctf_set_ctt_size(&dtd->dtd_data, size == 0 ? 1299 fp->ctf_dmodel->ctd_int : size); 1300 1301 /* 1302 * Always dirty in case we modified a forward. 1303 */ 1304 fp->ctf_flags |= LCTF_DIRTY; 1305 1306 return (type); 1307 } 1308 1309 ctf_id_t 1310 ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind) 1311 { 1312 ctf_hash_t *hp; 1313 ctf_helem_t *hep; 1314 ctf_dtdef_t *dtd; 1315 ctf_id_t type; 1316 1317 switch (kind) { 1318 case CTF_K_STRUCT: 1319 hp = &fp->ctf_structs; 1320 break; 1321 case CTF_K_UNION: 1322 hp = &fp->ctf_unions; 1323 break; 1324 case CTF_K_ENUM: 1325 hp = &fp->ctf_enums; 1326 break; 1327 default: 1328 return (ctf_set_errno(fp, ECTF_NOTSUE)); 1329 } 1330 1331 /* 1332 * If the type is already defined or exists as a forward tag, just 1333 * return the ctf_id_t of the existing definition. 1334 */ 1335 if (name != NULL && (hep = ctf_hash_lookup(hp, 1336 fp, name, strlen(name))) != NULL) 1337 return (hep->h_type); 1338 1339 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 1340 return (CTF_ERR); /* errno is set for us */ 1341 1342 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0); 1343 dtd->dtd_data.ctt_type = kind; 1344 1345 return (type); 1346 } 1347 1348 ctf_id_t 1349 ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 1350 { 1351 ctf_dtdef_t *dtd; 1352 ctf_id_t type; 1353 ctf_file_t *fpd; 1354 1355 fpd = fp; 1356 if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL && 1357 ctf_dtd_lookup(fp, ref) == NULL)) 1358 return (ctf_set_errno(fp, EINVAL)); 1359 1360 if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 1361 return (CTF_ERR); /* errno is set for us */ 1362 1363 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0); 1364 dtd->dtd_data.ctt_type = (ushort_t)ref; 1365 ctf_ref_inc(fp, ref); 1366 1367 return (type); 1368 } 1369 1370 ctf_id_t 1371 ctf_add_volatile(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 1372 { 1373 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_VOLATILE)); 1374 } 1375 1376 ctf_id_t 1377 ctf_add_const(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 1378 { 1379 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_CONST)); 1380 } 1381 1382 ctf_id_t 1383 ctf_add_restrict(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 1384 { 1385 return (ctf_add_reftype(fp, flag, name, ref, CTF_K_RESTRICT)); 1386 } 1387 1388 int 1389 ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value) 1390 { 1391 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid); 1392 ctf_dmdef_t *dmd; 1393 1394 uint_t kind, vlen, root; 1395 char *s; 1396 1397 if (name == NULL) 1398 return (ctf_set_errno(fp, EINVAL)); 1399 1400 if (!(fp->ctf_flags & LCTF_RDWR)) 1401 return (ctf_set_errno(fp, ECTF_RDONLY)); 1402 1403 if (dtd == NULL) 1404 return (ctf_set_errno(fp, ECTF_BADID)); 1405 1406 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 1407 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 1408 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 1409 1410 if (kind != CTF_K_ENUM) 1411 return (ctf_set_errno(fp, ECTF_NOTENUM)); 1412 1413 if (vlen == CTF_MAX_VLEN) 1414 return (ctf_set_errno(fp, ECTF_DTFULL)); 1415 1416 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1417 dmd != NULL; dmd = ctf_list_next(dmd)) { 1418 if (strcmp(dmd->dmd_name, name) == 0) { 1419 ctf_dprintf("encountered duplicate member %s\n", name); 1420 return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 1421 } 1422 } 1423 1424 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 1425 return (ctf_set_errno(fp, EAGAIN)); 1426 1427 if ((s = ctf_strdup(name)) == NULL) { 1428 ctf_free(dmd, sizeof (ctf_dmdef_t)); 1429 return (ctf_set_errno(fp, EAGAIN)); 1430 } 1431 1432 dmd->dmd_name = s; 1433 dmd->dmd_type = CTF_ERR; 1434 dmd->dmd_offset = 0; 1435 dmd->dmd_value = value; 1436 1437 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 1438 ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 1439 1440 fp->ctf_dtstrlen += strlen(s) + 1; 1441 fp->ctf_flags |= LCTF_DIRTY; 1442 1443 return (0); 1444 } 1445 1446 int 1447 ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type, 1448 ulong_t offset) 1449 { 1450 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid); 1451 ctf_dmdef_t *dmd; 1452 1453 ulong_t mbitsz; 1454 ssize_t msize, malign, ssize; 1455 uint_t kind, vlen, root; 1456 int mkind; 1457 char *s = NULL; 1458 1459 if (!(fp->ctf_flags & LCTF_RDWR)) 1460 return (ctf_set_errno(fp, ECTF_RDONLY)); 1461 1462 if (dtd == NULL) 1463 return (ctf_set_errno(fp, ECTF_BADID)); 1464 1465 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 1466 root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 1467 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 1468 1469 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) 1470 return (ctf_set_errno(fp, ECTF_NOTSOU)); 1471 1472 if (vlen == CTF_MAX_VLEN) 1473 return (ctf_set_errno(fp, ECTF_DTFULL)); 1474 1475 /* 1476 * Structures may have members which are anonymous. If they have two of 1477 * these, then the duplicate member detection would find it due to the 1478 * string of "", so we skip it. 1479 */ 1480 if (name != NULL && *name != '\0') { 1481 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1482 dmd != NULL; dmd = ctf_list_next(dmd)) { 1483 if (dmd->dmd_name != NULL && 1484 strcmp(dmd->dmd_name, name) == 0) { 1485 return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 1486 } 1487 } 1488 } 1489 1490 if ((msize = ctf_type_size(fp, type)) == CTF_ERR || 1491 (malign = ctf_type_align(fp, type)) == CTF_ERR || 1492 (mkind = ctf_type_kind(fp, type)) == CTF_ERR) 1493 return (CTF_ERR); /* errno is set for us */ 1494 1495 /* 1496 * ctf_type_size returns sizes in bytes. However, for bitfields, that 1497 * means that it may misrepresent and actually rounds it up to a power 1498 * of two and store that in bytes. So instead we have to get the 1499 * Integers encoding and rely on that. 1500 */ 1501 if (mkind == CTF_K_INTEGER) { 1502 ctf_encoding_t e; 1503 1504 if (ctf_type_encoding(fp, type, &e) == CTF_ERR) 1505 return (CTF_ERR); /* errno is set for us */ 1506 mbitsz = e.cte_bits; 1507 } else if (mkind == CTF_K_FORWARD) { 1508 /* 1509 * This is a rather rare case. In general one cannot add a 1510 * forward to a structure. However, the CTF tools traditionally 1511 * tried to add a forward to the struct cpu as the last member. 1512 * Therefore, if we find one here, we're going to verify the 1513 * size and make sure it's zero. It's certainly odd, but that's 1514 * life. 1515 * 1516 * Further, if it's not an absolute position being specified, 1517 * then we refuse to add it. 1518 */ 1519 if (offset == ULONG_MAX) 1520 return (ctf_set_errno(fp, EINVAL)); 1521 VERIFY(msize == 0); 1522 mbitsz = msize; 1523 } else { 1524 mbitsz = msize * 8; 1525 } 1526 1527 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 1528 return (ctf_set_errno(fp, EAGAIN)); 1529 1530 if (name != NULL && *name != '\0' && (s = ctf_strdup(name)) == NULL) { 1531 ctf_free(dmd, sizeof (ctf_dmdef_t)); 1532 return (ctf_set_errno(fp, EAGAIN)); 1533 } 1534 1535 dmd->dmd_name = s; 1536 dmd->dmd_type = type; 1537 dmd->dmd_value = -1; 1538 1539 if (kind == CTF_K_STRUCT && vlen != 0) { 1540 ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members); 1541 ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type); 1542 size_t off; 1543 1544 if (offset == ULONG_MAX) { 1545 ctf_encoding_t linfo; 1546 ssize_t lsize; 1547 1548 off = lmd->dmd_offset; 1549 if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR) 1550 off += linfo.cte_bits; 1551 else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR) 1552 off += lsize * NBBY; 1553 1554 /* 1555 * Round up the offset of the end of the last member to 1556 * the next byte boundary, convert 'off' to bytes, and 1557 * then round it up again to the next multiple of the 1558 * alignment required by the new member. Finally, 1559 * convert back to bits and store the result in 1560 * dmd_offset. Technically we could do more efficient 1561 * packing if the new member is a bit-field, but we're 1562 * the "compiler" and ANSI says we can do as we choose. 1563 */ 1564 off = roundup(off, NBBY) / NBBY; 1565 off = roundup(off, MAX(malign, 1)); 1566 dmd->dmd_offset = off * NBBY; 1567 ssize = off + msize; 1568 } else { 1569 dmd->dmd_offset = offset; 1570 ssize = (offset + mbitsz) / NBBY; 1571 } 1572 } else { 1573 dmd->dmd_offset = 0; 1574 ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL); 1575 ssize = MAX(ssize, msize); 1576 } 1577 1578 ctf_set_ctt_size(&dtd->dtd_data, ssize); 1579 1580 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 1581 ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 1582 1583 if (s != NULL) 1584 fp->ctf_dtstrlen += strlen(s) + 1; 1585 1586 ctf_ref_inc(fp, type); 1587 fp->ctf_flags |= LCTF_DIRTY; 1588 return (0); 1589 } 1590 1591 /* 1592 * This removes a type from the dynamic section. This will fail if the type is 1593 * referenced by another type. Note that the CTF ID is never reused currently by 1594 * CTF. Note that if this container is a parent container then we just outright 1595 * refuse to remove the type. There currently is no notion of searching for the 1596 * ctf_dtdef_t in parent containers. If there is, then this constraint could 1597 * become finer grained. 1598 */ 1599 int 1600 ctf_delete_type(ctf_file_t *fp, ctf_id_t type) 1601 { 1602 ctf_file_t *fpd; 1603 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type); 1604 1605 if (!(fp->ctf_flags & LCTF_RDWR)) 1606 return (ctf_set_errno(fp, ECTF_RDONLY)); 1607 1608 /* 1609 * We want to give as useful an errno as possible. That means that we 1610 * want to distinguish between a type which does not exist and one for 1611 * which the type is not dynamic. 1612 */ 1613 fpd = fp; 1614 if (ctf_lookup_by_id(&fpd, type) == NULL && 1615 ctf_dtd_lookup(fp, type) == NULL) 1616 return (CTF_ERR); /* errno is set for us */ 1617 1618 if (dtd == NULL) 1619 return (ctf_set_errno(fp, ECTF_NOTDYN)); 1620 1621 if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1) 1622 return (ctf_set_errno(fp, ECTF_REFERENCED)); 1623 1624 ctf_dtd_delete(fp, dtd); 1625 fp->ctf_flags |= LCTF_DIRTY; 1626 return (0); 1627 } 1628 1629 static int 1630 enumcmp(const char *name, int value, void *arg) 1631 { 1632 ctf_bundle_t *ctb = arg; 1633 int bvalue; 1634 1635 return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type, 1636 name, &bvalue) == CTF_ERR || value != bvalue); 1637 } 1638 1639 static int 1640 enumadd(const char *name, int value, void *arg) 1641 { 1642 ctf_bundle_t *ctb = arg; 1643 1644 return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type, 1645 name, value) == CTF_ERR); 1646 } 1647 1648 /*ARGSUSED*/ 1649 static int 1650 membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg) 1651 { 1652 ctf_bundle_t *ctb = arg; 1653 ctf_membinfo_t ctm; 1654 1655 return (ctf_member_info(ctb->ctb_file, ctb->ctb_type, 1656 name, &ctm) == CTF_ERR || ctm.ctm_offset != offset); 1657 } 1658 1659 static int 1660 membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg) 1661 { 1662 ctf_bundle_t *ctb = arg; 1663 ctf_dmdef_t *dmd; 1664 char *s = NULL; 1665 1666 if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 1667 return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 1668 1669 /* 1670 * Treat an empty string as a missing name that is anonymous. 1671 */ 1672 if (name != NULL && *name != '\0' && (s = ctf_strdup(name)) == NULL) { 1673 ctf_free(dmd, sizeof (ctf_dmdef_t)); 1674 return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 1675 } 1676 1677 /* 1678 * For now, dmd_type is copied as the src_fp's type; it is reset to an 1679 * equivalent dst_fp type by a final loop in ctf_add_type(), below. 1680 */ 1681 dmd->dmd_name = s; 1682 dmd->dmd_type = type; 1683 dmd->dmd_offset = offset; 1684 dmd->dmd_value = -1; 1685 1686 ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd); 1687 1688 if (s != NULL) 1689 ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1; 1690 1691 ctb->ctb_file->ctf_flags |= LCTF_DIRTY; 1692 return (0); 1693 } 1694 1695 /* 1696 * The ctf_add_type routine is used to copy a type from a source CTF container 1697 * to a dynamic destination container. This routine operates recursively by 1698 * following the source type's links and embedded member types. If the 1699 * destination container already contains a named type which has the same 1700 * attributes, then we succeed and return this type but no changes occur. 1701 */ 1702 ctf_id_t 1703 ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type) 1704 { 1705 ctf_id_t dst_type = CTF_ERR; 1706 uint_t dst_kind = CTF_K_UNKNOWN; 1707 1708 const ctf_type_t *tp; 1709 const char *name; 1710 uint_t kind, flag, vlen; 1711 1712 ctf_bundle_t src, dst; 1713 ctf_encoding_t src_en, dst_en; 1714 ctf_arinfo_t src_ar, dst_ar; 1715 1716 ctf_dtdef_t *dtd; 1717 ctf_funcinfo_t ctc; 1718 1719 ctf_hash_t *hp; 1720 ctf_helem_t *hep; 1721 1722 if (dst_fp == src_fp) 1723 return (src_type); 1724 1725 if (!(dst_fp->ctf_flags & LCTF_RDWR)) 1726 return (ctf_set_errno(dst_fp, ECTF_RDONLY)); 1727 1728 if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL) 1729 return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1730 1731 name = ctf_strptr(src_fp, tp->ctt_name); 1732 kind = LCTF_INFO_KIND(src_fp, tp->ctt_info); 1733 flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info); 1734 vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info); 1735 1736 switch (kind) { 1737 case CTF_K_STRUCT: 1738 hp = &dst_fp->ctf_structs; 1739 break; 1740 case CTF_K_UNION: 1741 hp = &dst_fp->ctf_unions; 1742 break; 1743 case CTF_K_ENUM: 1744 hp = &dst_fp->ctf_enums; 1745 break; 1746 default: 1747 hp = &dst_fp->ctf_names; 1748 break; 1749 } 1750 1751 /* 1752 * If the source type has a name and is a root type (visible at the 1753 * top-level scope), lookup the name in the destination container and 1754 * verify that it is of the same kind before we do anything else. 1755 */ 1756 if ((flag & CTF_ADD_ROOT) && name[0] != '\0' && 1757 (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) { 1758 dst_type = (ctf_id_t)hep->h_type; 1759 dst_kind = ctf_type_kind(dst_fp, dst_type); 1760 } 1761 1762 /* 1763 * If an identically named dst_type exists, fail with ECTF_CONFLICT 1764 * unless dst_type is a forward declaration and src_type is a struct, 1765 * union, or enum (i.e. the definition of the previous forward decl). 1766 */ 1767 if (dst_type != CTF_ERR && dst_kind != kind && ( 1768 dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM && 1769 kind != CTF_K_STRUCT && kind != CTF_K_UNION))) 1770 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1771 1772 /* 1773 * If the non-empty name was not found in the appropriate hash, search 1774 * the list of pending dynamic definitions that are not yet committed. 1775 * If a matching name and kind are found, assume this is the type that 1776 * we are looking for. This is necessary to permit ctf_add_type() to 1777 * operate recursively on entities such as a struct that contains a 1778 * pointer member that refers to the same struct type. 1779 */ 1780 if (dst_type == CTF_ERR && name[0] != '\0') { 1781 for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL && 1782 dtd->dtd_type > dst_fp->ctf_dtoldid; 1783 dtd = ctf_list_prev(dtd)) { 1784 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind && 1785 dtd->dtd_name != NULL && 1786 strcmp(dtd->dtd_name, name) == 0) 1787 return (dtd->dtd_type); 1788 } 1789 } 1790 1791 src.ctb_file = src_fp; 1792 src.ctb_type = src_type; 1793 src.ctb_dtd = NULL; 1794 1795 dst.ctb_file = dst_fp; 1796 dst.ctb_type = dst_type; 1797 dst.ctb_dtd = NULL; 1798 1799 /* 1800 * Now perform kind-specific processing. If dst_type is CTF_ERR, then 1801 * we add a new type with the same properties as src_type to dst_fp. 1802 * If dst_type is not CTF_ERR, then we verify that dst_type has the 1803 * same attributes as src_type. We recurse for embedded references. 1804 */ 1805 switch (kind) { 1806 case CTF_K_INTEGER: 1807 case CTF_K_FLOAT: 1808 if (ctf_type_encoding(src_fp, src_type, &src_en) != 0) 1809 return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1810 1811 if (dst_type != CTF_ERR) { 1812 if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0) 1813 return (CTF_ERR); /* errno is set for us */ 1814 1815 if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t))) 1816 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1817 1818 } else if (kind == CTF_K_INTEGER) { 1819 dst_type = ctf_add_integer(dst_fp, flag, name, &src_en); 1820 } else 1821 dst_type = ctf_add_float(dst_fp, flag, name, &src_en); 1822 break; 1823 1824 case CTF_K_POINTER: 1825 case CTF_K_VOLATILE: 1826 case CTF_K_CONST: 1827 case CTF_K_RESTRICT: 1828 src_type = ctf_type_reference(src_fp, src_type); 1829 src_type = ctf_add_type(dst_fp, src_fp, src_type); 1830 1831 if (src_type == CTF_ERR) 1832 return (CTF_ERR); /* errno is set for us */ 1833 1834 dst_type = ctf_add_reftype(dst_fp, flag, NULL, src_type, kind); 1835 break; 1836 1837 case CTF_K_ARRAY: 1838 if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR) 1839 return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1840 1841 src_ar.ctr_contents = 1842 ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents); 1843 src_ar.ctr_index = 1844 ctf_add_type(dst_fp, src_fp, src_ar.ctr_index); 1845 src_ar.ctr_nelems = src_ar.ctr_nelems; 1846 1847 if (src_ar.ctr_contents == CTF_ERR || 1848 src_ar.ctr_index == CTF_ERR) 1849 return (CTF_ERR); /* errno is set for us */ 1850 1851 if (dst_type != CTF_ERR) { 1852 if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0) 1853 return (CTF_ERR); /* errno is set for us */ 1854 1855 if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t))) 1856 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1857 } else 1858 dst_type = ctf_add_array(dst_fp, flag, &src_ar); 1859 break; 1860 1861 case CTF_K_FUNCTION: 1862 ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type); 1863 ctc.ctc_argc = 0; 1864 ctc.ctc_flags = 0; 1865 1866 if (ctc.ctc_return == CTF_ERR) 1867 return (CTF_ERR); /* errno is set for us */ 1868 1869 dst_type = ctf_add_funcptr(dst_fp, flag, &ctc, NULL); 1870 break; 1871 1872 case CTF_K_STRUCT: 1873 case CTF_K_UNION: { 1874 ctf_dmdef_t *dmd; 1875 int errs = 0; 1876 1877 /* 1878 * Technically to match a struct or union we need to check both 1879 * ways (src members vs. dst, dst members vs. src) but we make 1880 * this more optimal by only checking src vs. dst and comparing 1881 * the total size of the structure (which we must do anyway) 1882 * which covers the possibility of dst members not in src. 1883 * This optimization can be defeated for unions, but is so 1884 * pathological as to render it irrelevant for our purposes. 1885 */ 1886 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 1887 if (ctf_type_size(src_fp, src_type) != 1888 ctf_type_size(dst_fp, dst_type)) 1889 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1890 1891 if (ctf_member_iter(src_fp, src_type, membcmp, &dst)) 1892 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1893 1894 break; 1895 } 1896 1897 /* 1898 * Unlike the other cases, copying structs and unions is done 1899 * manually so as to avoid repeated lookups in ctf_add_member 1900 * and to ensure the exact same member offsets as in src_type. 1901 */ 1902 dst_type = ctf_add_generic(dst_fp, flag, name, &dtd); 1903 if (dst_type == CTF_ERR) 1904 return (CTF_ERR); /* errno is set for us */ 1905 1906 dst.ctb_type = dst_type; 1907 dst.ctb_dtd = dtd; 1908 1909 if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0) 1910 errs++; /* increment errs and fail at bottom of case */ 1911 1912 ctf_set_ctt_size(&dtd->dtd_data, 1913 ctf_type_size(src_fp, src_type)); 1914 1915 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen); 1916 1917 /* 1918 * Make a final pass through the members changing each dmd_type 1919 * (a src_fp type) to an equivalent type in dst_fp. We pass 1920 * through all members, leaving any that fail set to CTF_ERR. 1921 */ 1922 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1923 dmd != NULL; dmd = ctf_list_next(dmd)) { 1924 if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp, 1925 dmd->dmd_type)) == CTF_ERR) 1926 errs++; 1927 } 1928 1929 if (errs) 1930 return (CTF_ERR); /* errno is set for us */ 1931 1932 /* 1933 * Now that we know that we can't fail, we go through and bump 1934 * all the reference counts on the member types. 1935 */ 1936 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1937 dmd != NULL; dmd = ctf_list_next(dmd)) 1938 ctf_ref_inc(dst_fp, dmd->dmd_type); 1939 break; 1940 } 1941 1942 case CTF_K_ENUM: 1943 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 1944 if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) || 1945 ctf_enum_iter(dst_fp, dst_type, enumcmp, &src)) 1946 return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1947 } else { 1948 ssize_t size = ctf_type_size(src_fp, src_type); 1949 1950 if (size == CTF_ERR) 1951 return (CTF_ERR); /* errno is set for us */ 1952 1953 dst_type = ctf_add_enum(dst_fp, flag, name, size); 1954 if ((dst.ctb_type = dst_type) == CTF_ERR || 1955 ctf_enum_iter(src_fp, src_type, enumadd, &dst)) 1956 return (CTF_ERR); /* errno is set for us */ 1957 } 1958 break; 1959 1960 case CTF_K_FORWARD: 1961 if (dst_type == CTF_ERR) { 1962 dst_type = ctf_add_forward(dst_fp, 1963 flag, name, CTF_K_STRUCT); /* assume STRUCT */ 1964 } 1965 break; 1966 1967 case CTF_K_TYPEDEF: 1968 src_type = ctf_type_reference(src_fp, src_type); 1969 src_type = ctf_add_type(dst_fp, src_fp, src_type); 1970 1971 if (src_type == CTF_ERR) 1972 return (CTF_ERR); /* errno is set for us */ 1973 1974 /* 1975 * If dst_type is not CTF_ERR at this point, we should check if 1976 * ctf_type_reference(dst_fp, dst_type) != src_type and if so 1977 * fail with ECTF_CONFLICT. However, this causes problems with 1978 * <sys/types.h> typedefs that vary based on things like if 1979 * _ILP32x then pid_t is int otherwise long. We therefore omit 1980 * this check and assume that if the identically named typedef 1981 * already exists in dst_fp, it is correct or equivalent. 1982 */ 1983 if (dst_type == CTF_ERR) { 1984 dst_type = ctf_add_typedef(dst_fp, flag, 1985 name, src_type); 1986 } 1987 break; 1988 1989 default: 1990 return (ctf_set_errno(dst_fp, ECTF_CORRUPT)); 1991 } 1992 1993 return (dst_type); 1994 } 1995 1996 int 1997 ctf_add_function(ctf_file_t *fp, ulong_t idx, const ctf_funcinfo_t *fip, 1998 const ctf_id_t *argc) 1999 { 2000 int i; 2001 ctf_dsdef_t *dsd; 2002 ctf_file_t *afp; 2003 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data; 2004 2005 if (!(fp->ctf_flags & LCTF_RDWR)) 2006 return (ctf_set_errno(fp, ECTF_RDONLY)); 2007 2008 if (ctf_dsd_lookup(fp, idx) != NULL) 2009 return (ctf_set_errno(fp, ECTF_CONFLICT)); 2010 2011 if (symbase == (uintptr_t)NULL) 2012 return (ctf_set_errno(fp, ECTF_STRTAB)); 2013 2014 if (idx > fp->ctf_nsyms) 2015 return (ctf_set_errno(fp, ECTF_NOTDATA)); 2016 2017 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 2018 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx; 2019 if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC) 2020 return (ctf_set_errno(fp, ECTF_NOTFUNC)); 2021 } else { 2022 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx; 2023 if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC) 2024 return (ctf_set_errno(fp, ECTF_NOTFUNC)); 2025 } 2026 2027 afp = fp; 2028 if (ctf_lookup_by_id(&afp, fip->ctc_return) == NULL) 2029 return (CTF_ERR); /* errno is set for us */ 2030 2031 for (i = 0; i < fip->ctc_argc; i++) { 2032 afp = fp; 2033 if (ctf_lookup_by_id(&afp, argc[i]) == NULL) 2034 return (CTF_ERR); /* errno is set for us */ 2035 } 2036 2037 dsd = ctf_alloc(sizeof (ctf_dsdef_t)); 2038 if (dsd == NULL) 2039 return (ctf_set_errno(fp, ENOMEM)); 2040 dsd->dsd_nargs = fip->ctc_argc; 2041 if (fip->ctc_flags & CTF_FUNC_VARARG) 2042 dsd->dsd_nargs++; 2043 if (dsd->dsd_nargs != 0) { 2044 dsd->dsd_argc = ctf_alloc(sizeof (ctf_id_t) * dsd->dsd_nargs); 2045 if (dsd->dsd_argc == NULL) { 2046 ctf_free(dsd, sizeof (ctf_dsdef_t)); 2047 return (ctf_set_errno(fp, ENOMEM)); 2048 } 2049 bcopy(argc, dsd->dsd_argc, sizeof (ctf_id_t) * fip->ctc_argc); 2050 if (fip->ctc_flags & CTF_FUNC_VARARG) 2051 dsd->dsd_argc[fip->ctc_argc] = 0; 2052 } 2053 dsd->dsd_symidx = idx; 2054 dsd->dsd_tid = fip->ctc_return; 2055 2056 ctf_dsd_insert(fp, dsd); 2057 fp->ctf_flags |= LCTF_DIRTY; 2058 2059 return (0); 2060 } 2061 2062 int 2063 ctf_add_object(ctf_file_t *fp, ulong_t idx, ctf_id_t type) 2064 { 2065 ctf_dsdef_t *dsd; 2066 ctf_file_t *afp; 2067 uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data; 2068 2069 if (!(fp->ctf_flags & LCTF_RDWR)) 2070 return (ctf_set_errno(fp, ECTF_RDONLY)); 2071 2072 if (!(fp->ctf_flags & LCTF_RDWR)) 2073 return (ctf_set_errno(fp, ECTF_RDONLY)); 2074 2075 if (ctf_dsd_lookup(fp, idx) != NULL) 2076 return (ctf_set_errno(fp, ECTF_CONFLICT)); 2077 2078 if (symbase == (uintptr_t)NULL) 2079 return (ctf_set_errno(fp, ECTF_STRTAB)); 2080 2081 if (idx > fp->ctf_nsyms) 2082 return (ctf_set_errno(fp, ECTF_NOTDATA)); 2083 2084 if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) { 2085 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx; 2086 if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT) 2087 return (ctf_set_errno(fp, ECTF_NOTDATA)); 2088 } else { 2089 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx; 2090 if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT) 2091 return (ctf_set_errno(fp, ECTF_NOTDATA)); 2092 } 2093 2094 afp = fp; 2095 if (ctf_lookup_by_id(&afp, type) == NULL) 2096 return (CTF_ERR); /* errno is set for us */ 2097 2098 dsd = ctf_alloc(sizeof (ctf_dsdef_t)); 2099 if (dsd == NULL) 2100 return (ctf_set_errno(fp, ENOMEM)); 2101 dsd->dsd_symidx = idx; 2102 dsd->dsd_tid = type; 2103 dsd->dsd_argc = NULL; 2104 2105 ctf_dsd_insert(fp, dsd); 2106 fp->ctf_flags |= LCTF_DIRTY; 2107 2108 return (0); 2109 } 2110 2111 void 2112 ctf_dataptr(ctf_file_t *fp, const void **addrp, size_t *sizep) 2113 { 2114 if (addrp != NULL) 2115 *addrp = fp->ctf_base; 2116 if (sizep != NULL) 2117 *sizep = fp->ctf_size; 2118 } 2119 2120 int 2121 ctf_add_label(ctf_file_t *fp, const char *name, ctf_id_t type, uint_t position) 2122 { 2123 ctf_file_t *fpd; 2124 ctf_dldef_t *dld; 2125 2126 if (name == NULL) 2127 return (ctf_set_errno(fp, EINVAL)); 2128 2129 if (!(fp->ctf_flags & LCTF_RDWR)) 2130 return (ctf_set_errno(fp, ECTF_RDONLY)); 2131 2132 fpd = fp; 2133 if (type != 0 && ctf_lookup_by_id(&fpd, type) == NULL) 2134 return (CTF_ERR); /* errno is set for us */ 2135 2136 if (type != 0 && (fp->ctf_flags & LCTF_CHILD) && 2137 CTF_TYPE_ISPARENT(type)) 2138 return (ctf_set_errno(fp, ECTF_NOPARENT)); 2139 2140 if (ctf_dld_lookup(fp, name) != NULL) 2141 return (ctf_set_errno(fp, ECTF_LABELEXISTS)); 2142 2143 if ((dld = ctf_alloc(sizeof (ctf_dldef_t))) == NULL) 2144 return (ctf_set_errno(fp, EAGAIN)); 2145 2146 if ((dld->dld_name = ctf_strdup(name)) == NULL) { 2147 ctf_free(dld, sizeof (ctf_dldef_t)); 2148 return (ctf_set_errno(fp, EAGAIN)); 2149 } 2150 2151 ctf_dprintf("adding label %s, %ld\n", name, type); 2152 dld->dld_type = type; 2153 fp->ctf_dtstrlen += strlen(name) + 1; 2154 ctf_dld_insert(fp, dld, position); 2155 fp->ctf_flags |= LCTF_DIRTY; 2156 2157 return (0); 2158 } 2159 2160 /* 2161 * Update the size of a structure or union. Note that we don't allow this to 2162 * shrink the size of a struct or union, only to increase it. This is useful for 2163 * cases when you have a structure whose actual size is larger than the sum of 2164 * its members due to padding for natural alignment. 2165 */ 2166 int 2167 ctf_set_size(ctf_file_t *fp, ctf_id_t id, const ulong_t newsz) 2168 { 2169 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id); 2170 uint_t kind; 2171 size_t oldsz; 2172 2173 if (!(fp->ctf_flags & LCTF_RDWR)) 2174 return (ctf_set_errno(fp, ECTF_RDONLY)); 2175 2176 if (dtd == NULL) 2177 return (ctf_set_errno(fp, ECTF_BADID)); 2178 2179 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 2180 2181 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) 2182 return (ctf_set_errno(fp, ECTF_NOTSOU)); 2183 2184 if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT) 2185 oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data); 2186 2187 if (newsz < oldsz) 2188 return (ctf_set_errno(fp, EINVAL)); 2189 2190 ctf_set_ctt_size(&dtd->dtd_data, newsz); 2191 2192 fp->ctf_flags |= LCTF_DIRTY; 2193 return (0); 2194 } 2195 2196 int 2197 ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis) 2198 { 2199 ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id); 2200 uint_t kind, vlen; 2201 2202 if (!(fp->ctf_flags & LCTF_RDWR)) 2203 return (ctf_set_errno(fp, ECTF_RDONLY)); 2204 2205 if (dtd == NULL) 2206 return (ctf_set_errno(fp, ECTF_BADID)); 2207 2208 kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 2209 vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 2210 2211 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, vis, vlen); 2212 return (0); 2213 } 2214