1 /* CTF dict creation. 2 Copyright (C) 2019-2024 Free Software Foundation, Inc. 3 4 This file is part of libctf. 5 6 libctf is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 This program is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 14 See the GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; see the file COPYING. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 #include <ctf-impl.h> 21 #include <sys/param.h> 22 #include <string.h> 23 #include <unistd.h> 24 25 #ifndef EOVERFLOW 26 #define EOVERFLOW ERANGE 27 #endif 28 29 #ifndef roundup 30 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 31 #endif 32 33 /* The initial size of a dynamic type's vlen in members. Arbitrary: the bigger 34 this is, the less allocation needs to be done for small structure 35 initialization, and the more memory is wasted for small structures during CTF 36 construction. No effect on generated CTF or ctf_open()ed CTF. */ 37 #define INITIAL_VLEN 16 38 39 /* Make sure the ptrtab has enough space for at least one more type. 40 41 We start with 4KiB of ptrtab, enough for a thousand types, then grow it 25% 42 at a time. */ 43 44 static int 45 ctf_grow_ptrtab (ctf_dict_t *fp) 46 { 47 size_t new_ptrtab_len = fp->ctf_ptrtab_len; 48 49 /* We allocate one more ptrtab entry than we need, for the initial zero, 50 plus one because the caller will probably allocate a new type. 51 52 Equally, if the ptrtab is small -- perhaps due to ctf_open of a small 53 dict -- boost it by quite a lot at first, so we don't need to keep 54 realloc()ing. */ 55 56 if (fp->ctf_ptrtab == NULL || fp->ctf_ptrtab_len < 1024) 57 new_ptrtab_len = 1024; 58 else if ((fp->ctf_typemax + 2) > fp->ctf_ptrtab_len) 59 new_ptrtab_len = fp->ctf_ptrtab_len * 1.25; 60 61 if (new_ptrtab_len != fp->ctf_ptrtab_len) 62 { 63 uint32_t *new_ptrtab; 64 65 if ((new_ptrtab = realloc (fp->ctf_ptrtab, 66 new_ptrtab_len * sizeof (uint32_t))) == NULL) 67 return (ctf_set_errno (fp, ENOMEM)); 68 69 fp->ctf_ptrtab = new_ptrtab; 70 memset (fp->ctf_ptrtab + fp->ctf_ptrtab_len, 0, 71 (new_ptrtab_len - fp->ctf_ptrtab_len) * sizeof (uint32_t)); 72 fp->ctf_ptrtab_len = new_ptrtab_len; 73 } 74 return 0; 75 } 76 77 /* Make sure a vlen has enough space: expand it otherwise. Unlike the ptrtab, 78 which grows quite slowly, the vlen grows in big jumps because it is quite 79 expensive to expand: the caller has to scan the old vlen for string refs 80 first and remove them, then re-add them afterwards. The initial size is 81 more or less arbitrary. */ 82 static int 83 ctf_grow_vlen (ctf_dict_t *fp, ctf_dtdef_t *dtd, size_t vlen) 84 { 85 unsigned char *old = dtd->dtd_vlen; 86 87 if (dtd->dtd_vlen_alloc > vlen) 88 return 0; 89 90 if ((dtd->dtd_vlen = realloc (dtd->dtd_vlen, 91 dtd->dtd_vlen_alloc * 2)) == NULL) 92 { 93 dtd->dtd_vlen = old; 94 return (ctf_set_errno (fp, ENOMEM)); 95 } 96 memset (dtd->dtd_vlen + dtd->dtd_vlen_alloc, 0, dtd->dtd_vlen_alloc); 97 dtd->dtd_vlen_alloc *= 2; 98 return 0; 99 } 100 101 /* To create an empty CTF dict, we just declare a zeroed header and call 102 ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new dict r/w and 103 initialize the dynamic members. We start assigning type IDs at 1 because 104 type ID 0 is used as a sentinel and a not-found indicator. */ 105 106 ctf_dict_t * 107 ctf_create (int *errp) 108 { 109 static const ctf_header_t hdr = { .cth_preamble = { CTF_MAGIC, CTF_VERSION, 0 } }; 110 111 ctf_dynhash_t *structs = NULL, *unions = NULL, *enums = NULL, *names = NULL; 112 ctf_sect_t cts; 113 ctf_dict_t *fp; 114 115 libctf_init_debug(); 116 117 structs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, 118 NULL, NULL); 119 unions = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, 120 NULL, NULL); 121 enums = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, 122 NULL, NULL); 123 names = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, 124 NULL, NULL); 125 if (!structs || !unions || !enums || !names) 126 { 127 ctf_set_open_errno (errp, EAGAIN); 128 goto err; 129 } 130 131 cts.cts_name = _CTF_SECTION; 132 cts.cts_data = &hdr; 133 cts.cts_size = sizeof (hdr); 134 cts.cts_entsize = 1; 135 136 if ((fp = ctf_bufopen (&cts, NULL, NULL, errp)) == NULL) 137 goto err; 138 139 /* These hashes will have been initialized with a starting size of zero, 140 which is surely wrong. Use ones with slightly larger sizes. */ 141 ctf_dynhash_destroy (fp->ctf_structs); 142 ctf_dynhash_destroy (fp->ctf_unions); 143 ctf_dynhash_destroy (fp->ctf_enums); 144 ctf_dynhash_destroy (fp->ctf_names); 145 fp->ctf_structs = structs; 146 fp->ctf_unions = unions; 147 fp->ctf_enums = enums; 148 fp->ctf_names = names; 149 fp->ctf_dtoldid = 0; 150 fp->ctf_snapshot_lu = 0; 151 152 /* Make sure the ptrtab starts out at a reasonable size. */ 153 154 ctf_set_ctl_hashes (fp); 155 if (ctf_grow_ptrtab (fp) < 0) 156 { 157 ctf_set_open_errno (errp, ctf_errno (fp)); 158 ctf_dict_close (fp); 159 return NULL; 160 } 161 162 return fp; 163 164 err: 165 ctf_dynhash_destroy (structs); 166 ctf_dynhash_destroy (unions); 167 ctf_dynhash_destroy (enums); 168 ctf_dynhash_destroy (names); 169 return NULL; 170 } 171 172 /* Compatibility: just update the threshold for ctf_discard. */ 173 int 174 ctf_update (ctf_dict_t *fp) 175 { 176 fp->ctf_dtoldid = fp->ctf_typemax; 177 return 0; 178 } 179 180 ctf_dynhash_t * 181 ctf_name_table (ctf_dict_t *fp, int kind) 182 { 183 switch (kind) 184 { 185 case CTF_K_STRUCT: 186 return fp->ctf_structs; 187 case CTF_K_UNION: 188 return fp->ctf_unions; 189 case CTF_K_ENUM: 190 return fp->ctf_enums; 191 default: 192 return fp->ctf_names; 193 } 194 } 195 196 int 197 ctf_dtd_insert (ctf_dict_t *fp, ctf_dtdef_t *dtd, int flag, int kind) 198 { 199 const char *name; 200 if (ctf_dynhash_insert (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type, 201 dtd) < 0) 202 return ctf_set_errno (fp, ENOMEM); 203 204 if (flag == CTF_ADD_ROOT && dtd->dtd_data.ctt_name 205 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL) 206 { 207 if (ctf_dynhash_insert (ctf_name_table (fp, kind), 208 (char *) name, (void *) (uintptr_t) 209 dtd->dtd_type) < 0) 210 { 211 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) 212 dtd->dtd_type); 213 return ctf_set_errno (fp, ENOMEM); 214 } 215 } 216 ctf_list_append (&fp->ctf_dtdefs, dtd); 217 return 0; 218 } 219 220 void 221 ctf_dtd_delete (ctf_dict_t *fp, ctf_dtdef_t *dtd) 222 { 223 int kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); 224 size_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); 225 int name_kind = kind; 226 const char *name; 227 228 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type); 229 230 switch (kind) 231 { 232 case CTF_K_STRUCT: 233 case CTF_K_UNION: 234 { 235 ctf_lmember_t *memb = (ctf_lmember_t *) dtd->dtd_vlen; 236 size_t i; 237 238 for (i = 0; i < vlen; i++) 239 ctf_str_remove_ref (fp, ctf_strraw (fp, memb[i].ctlm_name), 240 &memb[i].ctlm_name); 241 } 242 break; 243 case CTF_K_ENUM: 244 { 245 ctf_enum_t *en = (ctf_enum_t *) dtd->dtd_vlen; 246 size_t i; 247 248 for (i = 0; i < vlen; i++) 249 ctf_str_remove_ref (fp, ctf_strraw (fp, en[i].cte_name), 250 &en[i].cte_name); 251 } 252 break; 253 case CTF_K_FORWARD: 254 name_kind = dtd->dtd_data.ctt_type; 255 break; 256 } 257 free (dtd->dtd_vlen); 258 dtd->dtd_vlen_alloc = 0; 259 260 if (dtd->dtd_data.ctt_name 261 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL 262 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info)) 263 { 264 ctf_dynhash_remove (ctf_name_table (fp, name_kind), name); 265 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name); 266 } 267 268 ctf_list_delete (&fp->ctf_dtdefs, dtd); 269 free (dtd); 270 } 271 272 ctf_dtdef_t * 273 ctf_dtd_lookup (const ctf_dict_t *fp, ctf_id_t type) 274 { 275 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, type)) 276 fp = fp->ctf_parent; 277 278 return (ctf_dtdef_t *) 279 ctf_dynhash_lookup (fp->ctf_dthash, (void *) (uintptr_t) type); 280 } 281 282 ctf_dtdef_t * 283 ctf_dynamic_type (const ctf_dict_t *fp, ctf_id_t id) 284 { 285 ctf_id_t idx; 286 287 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id)) 288 fp = fp->ctf_parent; 289 290 idx = LCTF_TYPE_TO_INDEX(fp, id); 291 292 if ((unsigned long) idx <= fp->ctf_typemax) 293 return ctf_dtd_lookup (fp, id); 294 return NULL; 295 } 296 297 static int 298 ctf_static_type (const ctf_dict_t *fp, ctf_id_t id) 299 { 300 ctf_id_t idx; 301 302 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, id)) 303 fp = fp->ctf_parent; 304 305 idx = LCTF_TYPE_TO_INDEX(fp, id); 306 307 return ((unsigned long) idx <= fp->ctf_stypes); 308 } 309 310 int 311 ctf_dvd_insert (ctf_dict_t *fp, ctf_dvdef_t *dvd) 312 { 313 if (ctf_dynhash_insert (fp->ctf_dvhash, dvd->dvd_name, dvd) < 0) 314 return ctf_set_errno (fp, ENOMEM); 315 ctf_list_append (&fp->ctf_dvdefs, dvd); 316 return 0; 317 } 318 319 void 320 ctf_dvd_delete (ctf_dict_t *fp, ctf_dvdef_t *dvd) 321 { 322 ctf_dynhash_remove (fp->ctf_dvhash, dvd->dvd_name); 323 free (dvd->dvd_name); 324 325 ctf_list_delete (&fp->ctf_dvdefs, dvd); 326 free (dvd); 327 } 328 329 ctf_dvdef_t * 330 ctf_dvd_lookup (const ctf_dict_t *fp, const char *name) 331 { 332 return (ctf_dvdef_t *) ctf_dynhash_lookup (fp->ctf_dvhash, name); 333 } 334 335 /* Discard all of the dynamic type definitions and variable definitions that 336 have been added to the dict since the last call to ctf_update(). We locate 337 such types by scanning the dtd list and deleting elements that have type IDs 338 greater than ctf_dtoldid, which is set by ctf_update(), above, and by 339 scanning the variable list and deleting elements that have update IDs equal 340 to the current value of the last-update snapshot count (indicating that they 341 were added after the most recent call to ctf_update()). */ 342 int 343 ctf_discard (ctf_dict_t *fp) 344 { 345 ctf_snapshot_id_t last_update = 346 { fp->ctf_dtoldid, 347 fp->ctf_snapshot_lu + 1 }; 348 349 return (ctf_rollback (fp, last_update)); 350 } 351 352 ctf_snapshot_id_t 353 ctf_snapshot (ctf_dict_t *fp) 354 { 355 ctf_snapshot_id_t snapid; 356 snapid.dtd_id = fp->ctf_typemax; 357 snapid.snapshot_id = fp->ctf_snapshots++; 358 return snapid; 359 } 360 361 /* Like ctf_discard(), only discards everything after a particular ID. */ 362 int 363 ctf_rollback (ctf_dict_t *fp, ctf_snapshot_id_t id) 364 { 365 ctf_dtdef_t *dtd, *ntd; 366 ctf_dvdef_t *dvd, *nvd; 367 368 if (id.snapshot_id < fp->ctf_stypes) 369 return (ctf_set_errno (fp, ECTF_RDONLY)); 370 371 if (fp->ctf_snapshot_lu >= id.snapshot_id) 372 return (ctf_set_errno (fp, ECTF_OVERROLLBACK)); 373 374 for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) 375 { 376 int kind; 377 const char *name; 378 379 ntd = ctf_list_next (dtd); 380 381 if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id) 382 continue; 383 384 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); 385 if (kind == CTF_K_FORWARD) 386 kind = dtd->dtd_data.ctt_type; 387 388 if (dtd->dtd_data.ctt_name 389 && (name = ctf_strraw (fp, dtd->dtd_data.ctt_name)) != NULL 390 && LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info)) 391 { 392 ctf_dynhash_remove (ctf_name_table (fp, kind), name); 393 ctf_str_remove_ref (fp, name, &dtd->dtd_data.ctt_name); 394 } 395 396 ctf_dynhash_remove (fp->ctf_dthash, (void *) (uintptr_t) dtd->dtd_type); 397 ctf_dtd_delete (fp, dtd); 398 } 399 400 for (dvd = ctf_list_next (&fp->ctf_dvdefs); dvd != NULL; dvd = nvd) 401 { 402 nvd = ctf_list_next (dvd); 403 404 if (dvd->dvd_snapshots <= id.snapshot_id) 405 continue; 406 407 ctf_dvd_delete (fp, dvd); 408 } 409 410 fp->ctf_typemax = id.dtd_id; 411 fp->ctf_snapshots = id.snapshot_id; 412 413 return 0; 414 } 415 416 /* Note: vlen is the amount of space *allocated* for the vlen. It may well not 417 be the amount of space used (yet): the space used is declared in per-kind 418 fashion in the dtd_data's info word. */ 419 static ctf_id_t 420 ctf_add_generic (ctf_dict_t *fp, uint32_t flag, const char *name, int kind, 421 size_t vlen, ctf_dtdef_t **rp) 422 { 423 ctf_dtdef_t *dtd; 424 ctf_id_t type; 425 426 if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT) 427 return (ctf_set_typed_errno (fp, EINVAL)); 428 429 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE) 430 return (ctf_set_typed_errno (fp, ECTF_FULL)); 431 432 if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1)) 433 return (ctf_set_typed_errno (fp, ECTF_FULL)); 434 435 /* Prohibit addition of a root-visible type that is already present 436 in the non-dynamic portion. */ 437 438 if (flag == CTF_ADD_ROOT && name != NULL && name[0] != '\0') 439 { 440 ctf_id_t existing; 441 442 if (((existing = ctf_dynhash_lookup_type (ctf_name_table (fp, kind), 443 name)) > 0) 444 && ctf_static_type (fp, existing)) 445 return (ctf_set_typed_errno (fp, ECTF_RDONLY)); 446 } 447 448 /* Make sure ptrtab always grows to be big enough for all types. */ 449 if (ctf_grow_ptrtab (fp) < 0) 450 return CTF_ERR; /* errno is set for us. */ 451 452 if ((dtd = calloc (1, sizeof (ctf_dtdef_t))) == NULL) 453 return (ctf_set_typed_errno (fp, EAGAIN)); 454 455 dtd->dtd_vlen_alloc = vlen; 456 if (vlen > 0) 457 { 458 if ((dtd->dtd_vlen = calloc (1, vlen)) == NULL) 459 goto oom; 460 } 461 else 462 dtd->dtd_vlen = NULL; 463 464 type = ++fp->ctf_typemax; 465 type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD)); 466 467 dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name); 468 dtd->dtd_type = type; 469 470 if (dtd->dtd_data.ctt_name == 0 && name != NULL && name[0] != '\0') 471 goto oom; 472 473 if (ctf_dtd_insert (fp, dtd, flag, kind) < 0) 474 goto err; /* errno is set for us. */ 475 476 *rp = dtd; 477 return type; 478 479 oom: 480 ctf_set_errno (fp, EAGAIN); 481 err: 482 free (dtd->dtd_vlen); 483 free (dtd); 484 return CTF_ERR; 485 } 486 487 /* When encoding integer sizes, we want to convert a byte count in the range 488 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function 489 is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. */ 490 static size_t 491 clp2 (size_t x) 492 { 493 x--; 494 495 x |= (x >> 1); 496 x |= (x >> 2); 497 x |= (x >> 4); 498 x |= (x >> 8); 499 x |= (x >> 16); 500 501 return (x + 1); 502 } 503 504 ctf_id_t 505 ctf_add_encoded (ctf_dict_t *fp, uint32_t flag, 506 const char *name, const ctf_encoding_t *ep, uint32_t kind) 507 { 508 ctf_dtdef_t *dtd; 509 ctf_id_t type; 510 uint32_t encoding; 511 512 if (ep == NULL) 513 return (ctf_set_typed_errno (fp, EINVAL)); 514 515 if (name == NULL || name[0] == '\0') 516 return (ctf_set_typed_errno (fp, ECTF_NONAME)); 517 518 if (!ctf_assert (fp, kind == CTF_K_INTEGER || kind == CTF_K_FLOAT)) 519 return CTF_ERR; /* errno is set for us. */ 520 521 if ((type = ctf_add_generic (fp, flag, name, kind, sizeof (uint32_t), 522 &dtd)) == CTF_ERR) 523 return CTF_ERR; /* errno is set for us. */ 524 525 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0); 526 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT) 527 / CHAR_BIT); 528 switch (kind) 529 { 530 case CTF_K_INTEGER: 531 encoding = CTF_INT_DATA (ep->cte_format, ep->cte_offset, ep->cte_bits); 532 break; 533 case CTF_K_FLOAT: 534 encoding = CTF_FP_DATA (ep->cte_format, ep->cte_offset, ep->cte_bits); 535 break; 536 default: 537 /* ctf_assert is opaque with -fno-inline. This dead code avoids 538 a warning about "encoding" being used uninitialized. */ 539 return CTF_ERR; 540 } 541 memcpy (dtd->dtd_vlen, &encoding, sizeof (encoding)); 542 543 return type; 544 } 545 546 ctf_id_t 547 ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, uint32_t kind) 548 { 549 ctf_dtdef_t *dtd; 550 ctf_id_t type; 551 ctf_dict_t *tmp = fp; 552 int child = fp->ctf_flags & LCTF_CHILD; 553 554 if (ref == CTF_ERR || ref > CTF_MAX_TYPE) 555 return (ctf_set_typed_errno (fp, EINVAL)); 556 557 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL) 558 return CTF_ERR; /* errno is set for us. */ 559 560 if ((type = ctf_add_generic (fp, flag, NULL, kind, 0, &dtd)) == CTF_ERR) 561 return CTF_ERR; /* errno is set for us. */ 562 563 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, flag, 0); 564 dtd->dtd_data.ctt_type = (uint32_t) ref; 565 566 if (kind != CTF_K_POINTER) 567 return type; 568 569 /* If we are adding a pointer, update the ptrtab, pointing at this type from 570 the type it points to. Note that ctf_typemax is at this point one higher 571 than we want to check against, because it's just been incremented for the 572 addition of this type. The pptrtab is lazily-updated as needed, so is not 573 touched here. */ 574 575 uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type); 576 uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref); 577 578 if (LCTF_TYPE_ISCHILD (fp, ref) == child 579 && ref_idx < fp->ctf_typemax) 580 fp->ctf_ptrtab[ref_idx] = type_idx; 581 582 return type; 583 } 584 585 ctf_id_t 586 ctf_add_slice (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, 587 const ctf_encoding_t *ep) 588 { 589 ctf_dtdef_t *dtd; 590 ctf_slice_t slice; 591 ctf_id_t resolved_ref = ref; 592 ctf_id_t type; 593 int kind; 594 const ctf_type_t *tp; 595 ctf_dict_t *tmp = fp; 596 597 if (ep == NULL) 598 return (ctf_set_typed_errno (fp, EINVAL)); 599 600 if ((ep->cte_bits > 255) || (ep->cte_offset > 255)) 601 return (ctf_set_typed_errno (fp, ECTF_SLICEOVERFLOW)); 602 603 if (ref == CTF_ERR || ref > CTF_MAX_TYPE) 604 return (ctf_set_typed_errno (fp, EINVAL)); 605 606 if (ref != 0 && ((tp = ctf_lookup_by_id (&tmp, ref)) == NULL)) 607 return CTF_ERR; /* errno is set for us. */ 608 609 /* Make sure we ultimately point to an integral type. We also allow slices to 610 point to the unimplemented type, for now, because the compiler can emit 611 such slices, though they're not very much use. */ 612 613 resolved_ref = ctf_type_resolve_unsliced (fp, ref); 614 kind = ctf_type_kind_unsliced (fp, resolved_ref); 615 616 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && 617 (kind != CTF_K_ENUM) 618 && (ref != 0)) 619 return (ctf_set_typed_errno (fp, ECTF_NOTINTFP)); 620 621 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_SLICE, 622 sizeof (ctf_slice_t), &dtd)) == CTF_ERR) 623 return CTF_ERR; /* errno is set for us. */ 624 625 memset (&slice, 0, sizeof (ctf_slice_t)); 626 627 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_SLICE, flag, 0); 628 dtd->dtd_data.ctt_size = clp2 (P2ROUNDUP (ep->cte_bits, CHAR_BIT) 629 / CHAR_BIT); 630 slice.cts_type = (uint32_t) ref; 631 slice.cts_bits = ep->cte_bits; 632 slice.cts_offset = ep->cte_offset; 633 memcpy (dtd->dtd_vlen, &slice, sizeof (ctf_slice_t)); 634 635 return type; 636 } 637 638 ctf_id_t 639 ctf_add_integer (ctf_dict_t *fp, uint32_t flag, 640 const char *name, const ctf_encoding_t *ep) 641 { 642 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_INTEGER)); 643 } 644 645 ctf_id_t 646 ctf_add_float (ctf_dict_t *fp, uint32_t flag, 647 const char *name, const ctf_encoding_t *ep) 648 { 649 return (ctf_add_encoded (fp, flag, name, ep, CTF_K_FLOAT)); 650 } 651 652 ctf_id_t 653 ctf_add_pointer (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) 654 { 655 return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER)); 656 } 657 658 ctf_id_t 659 ctf_add_array (ctf_dict_t *fp, uint32_t flag, const ctf_arinfo_t *arp) 660 { 661 ctf_dtdef_t *dtd; 662 ctf_array_t cta; 663 ctf_id_t type; 664 ctf_dict_t *tmp = fp; 665 666 if (arp == NULL) 667 return (ctf_set_typed_errno (fp, EINVAL)); 668 669 if (arp->ctr_contents != 0 670 && ctf_lookup_by_id (&tmp, arp->ctr_contents) == NULL) 671 return CTF_ERR; /* errno is set for us. */ 672 673 tmp = fp; 674 if (ctf_lookup_by_id (&tmp, arp->ctr_index) == NULL) 675 return CTF_ERR; /* errno is set for us. */ 676 677 if (ctf_type_kind (fp, arp->ctr_index) == CTF_K_FORWARD) 678 { 679 ctf_err_warn (fp, 1, ECTF_INCOMPLETE, 680 _("ctf_add_array: index type %lx is incomplete"), 681 arp->ctr_contents); 682 return (ctf_set_typed_errno (fp, ECTF_INCOMPLETE)); 683 } 684 685 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY, 686 sizeof (ctf_array_t), &dtd)) == CTF_ERR) 687 return CTF_ERR; /* errno is set for us. */ 688 689 memset (&cta, 0, sizeof (ctf_array_t)); 690 691 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0); 692 dtd->dtd_data.ctt_size = 0; 693 cta.cta_contents = (uint32_t) arp->ctr_contents; 694 cta.cta_index = (uint32_t) arp->ctr_index; 695 cta.cta_nelems = arp->ctr_nelems; 696 memcpy (dtd->dtd_vlen, &cta, sizeof (ctf_array_t)); 697 698 return type; 699 } 700 701 int 702 ctf_set_array (ctf_dict_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) 703 { 704 ctf_dict_t *ofp = fp; 705 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type); 706 ctf_array_t *vlen; 707 708 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, type)) 709 fp = fp->ctf_parent; 710 711 /* You can only call ctf_set_array on a type you have added, not a 712 type that was read in via ctf_open(). */ 713 if (type < fp->ctf_stypes) 714 return (ctf_set_errno (ofp, ECTF_RDONLY)); 715 716 if (dtd == NULL 717 || LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY) 718 return (ctf_set_errno (ofp, ECTF_BADID)); 719 720 vlen = (ctf_array_t *) dtd->dtd_vlen; 721 vlen->cta_contents = (uint32_t) arp->ctr_contents; 722 vlen->cta_index = (uint32_t) arp->ctr_index; 723 vlen->cta_nelems = arp->ctr_nelems; 724 725 return 0; 726 } 727 728 ctf_id_t 729 ctf_add_function (ctf_dict_t *fp, uint32_t flag, 730 const ctf_funcinfo_t *ctc, const ctf_id_t *argv) 731 { 732 ctf_dtdef_t *dtd; 733 ctf_id_t type; 734 uint32_t vlen; 735 uint32_t *vdat; 736 ctf_dict_t *tmp = fp; 737 size_t initial_vlen; 738 size_t i; 739 740 if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 741 || (ctc->ctc_argc != 0 && argv == NULL)) 742 return (ctf_set_typed_errno (fp, EINVAL)); 743 744 vlen = ctc->ctc_argc; 745 if (ctc->ctc_flags & CTF_FUNC_VARARG) 746 vlen++; /* Add trailing zero to indicate varargs (see below). */ 747 748 if (ctc->ctc_return != 0 749 && ctf_lookup_by_id (&tmp, ctc->ctc_return) == NULL) 750 return CTF_ERR; /* errno is set for us. */ 751 752 if (vlen > CTF_MAX_VLEN) 753 return (ctf_set_typed_errno (fp, EOVERFLOW)); 754 755 /* One word extra allocated for padding for 4-byte alignment if need be. 756 Not reflected in vlen: we don't want to copy anything into it, and 757 it's in addition to (e.g.) the trailing 0 indicating varargs. */ 758 759 initial_vlen = (sizeof (uint32_t) * (vlen + (vlen & 1))); 760 if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_FUNCTION, 761 initial_vlen, &dtd)) == CTF_ERR) 762 return CTF_ERR; /* errno is set for us. */ 763 764 vdat = (uint32_t *) dtd->dtd_vlen; 765 766 for (i = 0; i < ctc->ctc_argc; i++) 767 { 768 tmp = fp; 769 if (argv[i] != 0 && ctf_lookup_by_id (&tmp, argv[i]) == NULL) 770 return CTF_ERR; /* errno is set for us. */ 771 vdat[i] = (uint32_t) argv[i]; 772 } 773 774 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen); 775 dtd->dtd_data.ctt_type = (uint32_t) ctc->ctc_return; 776 777 if (ctc->ctc_flags & CTF_FUNC_VARARG) 778 vdat[vlen - 1] = 0; /* Add trailing zero to indicate varargs. */ 779 780 return type; 781 } 782 783 ctf_id_t 784 ctf_add_struct_sized (ctf_dict_t *fp, uint32_t flag, const char *name, 785 size_t size) 786 { 787 ctf_dtdef_t *dtd; 788 ctf_id_t type = 0; 789 size_t initial_vlen = sizeof (ctf_lmember_t) * INITIAL_VLEN; 790 791 /* Promote root-visible forwards to structs. */ 792 if (name != NULL) 793 type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name); 794 795 /* Prohibit promotion if this type was ctf_open()ed. */ 796 if (type > 0 && type < fp->ctf_stypes) 797 return (ctf_set_errno (fp, ECTF_RDONLY)); 798 799 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD) 800 dtd = ctf_dtd_lookup (fp, type); 801 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_STRUCT, 802 initial_vlen, &dtd)) == CTF_ERR) 803 return CTF_ERR; /* errno is set for us. */ 804 805 /* Forwards won't have any vlen yet. */ 806 if (dtd->dtd_vlen_alloc == 0) 807 { 808 if ((dtd->dtd_vlen = calloc (1, initial_vlen)) == NULL) 809 return (ctf_set_typed_errno (fp, ENOMEM)); 810 dtd->dtd_vlen_alloc = initial_vlen; 811 } 812 813 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_STRUCT, flag, 0); 814 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 815 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size); 816 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size); 817 818 return type; 819 } 820 821 ctf_id_t 822 ctf_add_struct (ctf_dict_t *fp, uint32_t flag, const char *name) 823 { 824 return (ctf_add_struct_sized (fp, flag, name, 0)); 825 } 826 827 ctf_id_t 828 ctf_add_union_sized (ctf_dict_t *fp, uint32_t flag, const char *name, 829 size_t size) 830 { 831 ctf_dtdef_t *dtd; 832 ctf_id_t type = 0; 833 size_t initial_vlen = sizeof (ctf_lmember_t) * INITIAL_VLEN; 834 835 /* Promote root-visible forwards to unions. */ 836 if (name != NULL) 837 type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name); 838 839 /* Prohibit promotion if this type was ctf_open()ed. */ 840 if (type > 0 && type < fp->ctf_stypes) 841 return (ctf_set_errno (fp, ECTF_RDONLY)); 842 843 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD) 844 dtd = ctf_dtd_lookup (fp, type); 845 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNION, 846 initial_vlen, &dtd)) == CTF_ERR) 847 return CTF_ERR; /* errno is set for us. */ 848 849 /* Forwards won't have any vlen yet. */ 850 if (dtd->dtd_vlen_alloc == 0) 851 { 852 if ((dtd->dtd_vlen = calloc (1, initial_vlen)) == NULL) 853 return (ctf_set_typed_errno (fp, ENOMEM)); 854 dtd->dtd_vlen_alloc = initial_vlen; 855 } 856 857 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNION, flag, 0); 858 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 859 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (size); 860 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (size); 861 862 return type; 863 } 864 865 ctf_id_t 866 ctf_add_union (ctf_dict_t *fp, uint32_t flag, const char *name) 867 { 868 return (ctf_add_union_sized (fp, flag, name, 0)); 869 } 870 871 ctf_id_t 872 ctf_add_enum (ctf_dict_t *fp, uint32_t flag, const char *name) 873 { 874 ctf_dtdef_t *dtd; 875 ctf_id_t type = 0; 876 size_t initial_vlen = sizeof (ctf_enum_t) * INITIAL_VLEN; 877 878 /* Promote root-visible forwards to enums. */ 879 if (name != NULL) 880 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name); 881 882 /* Prohibit promotion if this type was ctf_open()ed. */ 883 if (type > 0 && type < fp->ctf_stypes) 884 return (ctf_set_errno (fp, ECTF_RDONLY)); 885 886 if (type != 0 && ctf_type_kind (fp, type) == CTF_K_FORWARD) 887 dtd = ctf_dtd_lookup (fp, type); 888 else if ((type = ctf_add_generic (fp, flag, name, CTF_K_ENUM, 889 initial_vlen, &dtd)) == CTF_ERR) 890 return CTF_ERR; /* errno is set for us. */ 891 892 /* Forwards won't have any vlen yet. */ 893 if (dtd->dtd_vlen_alloc == 0) 894 { 895 if ((dtd->dtd_vlen = calloc (1, initial_vlen)) == NULL) 896 return (ctf_set_typed_errno (fp, ENOMEM)); 897 dtd->dtd_vlen_alloc = initial_vlen; 898 } 899 900 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ENUM, flag, 0); 901 dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int; 902 903 return type; 904 } 905 906 ctf_id_t 907 ctf_add_enum_encoded (ctf_dict_t *fp, uint32_t flag, const char *name, 908 const ctf_encoding_t *ep) 909 { 910 ctf_id_t type = 0; 911 912 /* First, create the enum if need be, using most of the same machinery as 913 ctf_add_enum(), to ensure that we do not allow things past that are not 914 enums or forwards to them. (This includes other slices: you cannot slice a 915 slice, which would be a useless thing to do anyway.) */ 916 917 if (name != NULL) 918 type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name); 919 920 if (type != 0) 921 { 922 if ((ctf_type_kind (fp, type) != CTF_K_FORWARD) && 923 (ctf_type_kind_unsliced (fp, type) != CTF_K_ENUM)) 924 return (ctf_set_typed_errno (fp, ECTF_NOTINTFP)); 925 } 926 else if ((type = ctf_add_enum (fp, flag, name)) == CTF_ERR) 927 return CTF_ERR; /* errno is set for us. */ 928 929 /* Now attach a suitable slice to it. */ 930 931 return ctf_add_slice (fp, flag, type, ep); 932 } 933 934 ctf_id_t 935 ctf_add_forward (ctf_dict_t *fp, uint32_t flag, const char *name, 936 uint32_t kind) 937 { 938 ctf_dtdef_t *dtd; 939 ctf_id_t type = 0; 940 941 if (!ctf_forwardable_kind (kind)) 942 return (ctf_set_typed_errno (fp, ECTF_NOTSUE)); 943 944 if (name == NULL || name[0] == '\0') 945 return (ctf_set_typed_errno (fp, ECTF_NONAME)); 946 947 /* If the type is already defined or exists as a forward tag, just return 948 the ctf_id_t of the existing definition. Since this changes nothing, 949 it's safe to do even on the read-only portion of the dict. */ 950 951 type = ctf_lookup_by_rawname (fp, kind, name); 952 953 if (type) 954 return type; 955 956 if ((type = ctf_add_generic (fp, flag, name, kind, 0, &dtd)) == CTF_ERR) 957 return CTF_ERR; /* errno is set for us. */ 958 959 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_FORWARD, flag, 0); 960 dtd->dtd_data.ctt_type = kind; 961 962 return type; 963 } 964 965 ctf_id_t 966 ctf_add_unknown (ctf_dict_t *fp, uint32_t flag, const char *name) 967 { 968 ctf_dtdef_t *dtd; 969 ctf_id_t type = 0; 970 971 /* If a type is already defined with this name, error (if not CTF_K_UNKNOWN) 972 or just return it. */ 973 974 if (name != NULL && name[0] != '\0' && flag == CTF_ADD_ROOT 975 && (type = ctf_lookup_by_rawname (fp, CTF_K_UNKNOWN, name))) 976 { 977 if (ctf_type_kind (fp, type) == CTF_K_UNKNOWN) 978 return type; 979 else 980 { 981 ctf_err_warn (fp, 1, ECTF_CONFLICT, 982 _("ctf_add_unknown: cannot add unknown type " 983 "named %s: type of this name already defined"), 984 name ? name : _("(unnamed type)")); 985 return (ctf_set_typed_errno (fp, ECTF_CONFLICT)); 986 } 987 } 988 989 if ((type = ctf_add_generic (fp, flag, name, CTF_K_UNKNOWN, 0, &dtd)) == CTF_ERR) 990 return CTF_ERR; /* errno is set for us. */ 991 992 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_UNKNOWN, flag, 0); 993 dtd->dtd_data.ctt_type = 0; 994 995 return type; 996 } 997 998 ctf_id_t 999 ctf_add_typedef (ctf_dict_t *fp, uint32_t flag, const char *name, 1000 ctf_id_t ref) 1001 { 1002 ctf_dtdef_t *dtd; 1003 ctf_id_t type; 1004 ctf_dict_t *tmp = fp; 1005 1006 if (ref == CTF_ERR || ref > CTF_MAX_TYPE) 1007 return (ctf_set_typed_errno (fp, EINVAL)); 1008 1009 if (name == NULL || name[0] == '\0') 1010 return (ctf_set_typed_errno (fp, ECTF_NONAME)); 1011 1012 if (ref != 0 && ctf_lookup_by_id (&tmp, ref) == NULL) 1013 return CTF_ERR; /* errno is set for us. */ 1014 1015 if ((type = ctf_add_generic (fp, flag, name, CTF_K_TYPEDEF, 0, 1016 &dtd)) == CTF_ERR) 1017 return CTF_ERR; /* errno is set for us. */ 1018 1019 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_TYPEDEF, flag, 0); 1020 dtd->dtd_data.ctt_type = (uint32_t) ref; 1021 1022 return type; 1023 } 1024 1025 ctf_id_t 1026 ctf_add_volatile (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) 1027 { 1028 return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE)); 1029 } 1030 1031 ctf_id_t 1032 ctf_add_const (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) 1033 { 1034 return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST)); 1035 } 1036 1037 ctf_id_t 1038 ctf_add_restrict (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) 1039 { 1040 return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT)); 1041 } 1042 1043 int 1044 ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name, 1045 int value) 1046 { 1047 ctf_dict_t *ofp = fp; 1048 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid); 1049 unsigned char *old_vlen; 1050 ctf_enum_t *en; 1051 size_t i; 1052 1053 uint32_t kind, vlen, root; 1054 1055 if (name == NULL) 1056 return (ctf_set_errno (fp, EINVAL)); 1057 1058 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, enid)) 1059 fp = fp->ctf_parent; 1060 1061 if (enid < fp->ctf_stypes) 1062 return (ctf_set_errno (ofp, ECTF_RDONLY)); 1063 1064 if (dtd == NULL) 1065 return (ctf_set_errno (ofp, ECTF_BADID)); 1066 1067 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); 1068 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info); 1069 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); 1070 1071 if (kind != CTF_K_ENUM) 1072 return (ctf_set_errno (ofp, ECTF_NOTENUM)); 1073 1074 if (vlen == CTF_MAX_VLEN) 1075 return (ctf_set_errno (ofp, ECTF_DTFULL)); 1076 1077 old_vlen = dtd->dtd_vlen; 1078 if (ctf_grow_vlen (fp, dtd, sizeof (ctf_enum_t) * (vlen + 1)) < 0) 1079 return -1; /* errno is set for us. */ 1080 en = (ctf_enum_t *) dtd->dtd_vlen; 1081 1082 /* Remove refs in the old vlen region and reapply them. */ 1083 1084 ctf_str_move_refs (fp, old_vlen, sizeof (ctf_enum_t) * vlen, dtd->dtd_vlen); 1085 1086 for (i = 0; i < vlen; i++) 1087 if (strcmp (ctf_strptr (fp, en[i].cte_name), name) == 0) 1088 return (ctf_set_errno (ofp, ECTF_DUPLICATE)); 1089 1090 en[i].cte_name = ctf_str_add_movable_ref (fp, name, &en[i].cte_name); 1091 en[i].cte_value = value; 1092 1093 if (en[i].cte_name == 0 && name != NULL && name[0] != '\0') 1094 return (ctf_set_errno (ofp, ctf_errno (fp))); 1095 1096 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1); 1097 1098 return 0; 1099 } 1100 1101 int 1102 ctf_add_member_offset (ctf_dict_t *fp, ctf_id_t souid, const char *name, 1103 ctf_id_t type, unsigned long bit_offset) 1104 { 1105 ctf_dict_t *ofp = fp; 1106 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, souid); 1107 1108 ssize_t msize, malign, ssize; 1109 uint32_t kind, vlen, root; 1110 size_t i; 1111 int is_incomplete = 0; 1112 unsigned char *old_vlen; 1113 ctf_lmember_t *memb; 1114 1115 if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, souid)) 1116 { 1117 /* Adding a child type to a parent, even via the child, is prohibited. 1118 Otherwise, climb to the parent and do all work there. */ 1119 1120 if (LCTF_TYPE_ISCHILD (fp, type)) 1121 return (ctf_set_errno (ofp, ECTF_BADID)); 1122 1123 fp = fp->ctf_parent; 1124 } 1125 1126 if (souid < fp->ctf_stypes) 1127 return (ctf_set_errno (ofp, ECTF_RDONLY)); 1128 1129 if (dtd == NULL) 1130 return (ctf_set_errno (ofp, ECTF_BADID)); 1131 1132 if (name != NULL && name[0] == '\0') 1133 name = NULL; 1134 1135 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); 1136 root = LCTF_INFO_ISROOT (fp, dtd->dtd_data.ctt_info); 1137 vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); 1138 1139 if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) 1140 return (ctf_set_errno (ofp, ECTF_NOTSOU)); 1141 1142 if (vlen == CTF_MAX_VLEN) 1143 return (ctf_set_errno (ofp, ECTF_DTFULL)); 1144 1145 old_vlen = dtd->dtd_vlen; 1146 if (ctf_grow_vlen (fp, dtd, sizeof (ctf_lmember_t) * (vlen + 1)) < 0) 1147 return (ctf_set_errno (ofp, ctf_errno (fp))); 1148 memb = (ctf_lmember_t *) dtd->dtd_vlen; 1149 1150 /* Remove pending refs in the old vlen region and reapply them. */ 1151 1152 ctf_str_move_refs (fp, old_vlen, sizeof (ctf_lmember_t) * vlen, dtd->dtd_vlen); 1153 1154 if (name != NULL) 1155 { 1156 for (i = 0; i < vlen; i++) 1157 if (strcmp (ctf_strptr (fp, memb[i].ctlm_name), name) == 0) 1158 return (ctf_set_errno (ofp, ECTF_DUPLICATE)); 1159 } 1160 1161 if ((msize = ctf_type_size (fp, type)) < 0 || 1162 (malign = ctf_type_align (fp, type)) < 0) 1163 { 1164 /* The unimplemented type, and any type that resolves to it, has no size 1165 and no alignment: it can correspond to any number of compiler-inserted 1166 types. We allow incomplete types through since they are routinely 1167 added to the ends of structures, and can even be added elsewhere in 1168 structures by the deduplicator. They are assumed to be zero-size with 1169 no alignment: this is often wrong, but problems can be avoided in this 1170 case by explicitly specifying the size of the structure via the _sized 1171 functions. The deduplicator always does this. */ 1172 1173 msize = 0; 1174 malign = 0; 1175 if (ctf_errno (fp) == ECTF_NONREPRESENTABLE) 1176 ctf_set_errno (fp, 0); 1177 else if (ctf_errno (fp) == ECTF_INCOMPLETE) 1178 is_incomplete = 1; 1179 else 1180 return -1; /* errno is set for us. */ 1181 } 1182 1183 memb[vlen].ctlm_name = ctf_str_add_movable_ref (fp, name, &memb[vlen].ctlm_name); 1184 memb[vlen].ctlm_type = type; 1185 if (memb[vlen].ctlm_name == 0 && name != NULL && name[0] != '\0') 1186 return -1; /* errno is set for us. */ 1187 1188 if (kind == CTF_K_STRUCT && vlen != 0) 1189 { 1190 if (bit_offset == (unsigned long) - 1) 1191 { 1192 /* Natural alignment. */ 1193 1194 ctf_id_t ltype = ctf_type_resolve (fp, memb[vlen - 1].ctlm_type); 1195 size_t off = CTF_LMEM_OFFSET(&memb[vlen - 1]); 1196 1197 ctf_encoding_t linfo; 1198 ssize_t lsize; 1199 1200 /* Propagate any error from ctf_type_resolve. If the last member was 1201 of unimplemented type, this may be -ECTF_NONREPRESENTABLE: we 1202 cannot insert right after such a member without explicit offset 1203 specification, because its alignment and size is not known. */ 1204 if (ltype == CTF_ERR) 1205 return -1; /* errno is set for us. */ 1206 1207 if (is_incomplete) 1208 { 1209 ctf_err_warn (ofp, 1, ECTF_INCOMPLETE, 1210 _("ctf_add_member_offset: cannot add member %s of " 1211 "incomplete type %lx to struct %lx without " 1212 "specifying explicit offset\n"), 1213 name ? name : _("(unnamed member)"), type, souid); 1214 return (ctf_set_errno (ofp, ECTF_INCOMPLETE)); 1215 } 1216 1217 if (ctf_type_encoding (fp, ltype, &linfo) == 0) 1218 off += linfo.cte_bits; 1219 else if ((lsize = ctf_type_size (fp, ltype)) > 0) 1220 off += lsize * CHAR_BIT; 1221 else if (lsize == -1 && ctf_errno (fp) == ECTF_INCOMPLETE) 1222 { 1223 const char *lname = ctf_strraw (fp, memb[vlen - 1].ctlm_name); 1224 1225 ctf_err_warn (ofp, 1, ECTF_INCOMPLETE, 1226 _("ctf_add_member_offset: cannot add member %s of " 1227 "type %lx to struct %lx without specifying " 1228 "explicit offset after member %s of type %lx, " 1229 "which is an incomplete type\n"), 1230 name ? name : _("(unnamed member)"), type, souid, 1231 lname ? lname : _("(unnamed member)"), ltype); 1232 return (ctf_set_errno (ofp, ECTF_INCOMPLETE)); 1233 } 1234 1235 /* Round up the offset of the end of the last member to 1236 the next byte boundary, convert 'off' to bytes, and 1237 then round it up again to the next multiple of the 1238 alignment required by the new member. Finally, 1239 convert back to bits and store the result in 1240 dmd_offset. Technically we could do more efficient 1241 packing if the new member is a bit-field, but we're 1242 the "compiler" and ANSI says we can do as we choose. */ 1243 1244 off = roundup (off, CHAR_BIT) / CHAR_BIT; 1245 off = roundup (off, MAX (malign, 1)); 1246 memb[vlen].ctlm_offsethi = CTF_OFFSET_TO_LMEMHI (off * CHAR_BIT); 1247 memb[vlen].ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO (off * CHAR_BIT); 1248 ssize = off + msize; 1249 } 1250 else 1251 { 1252 /* Specified offset in bits. */ 1253 1254 memb[vlen].ctlm_offsethi = CTF_OFFSET_TO_LMEMHI (bit_offset); 1255 memb[vlen].ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO (bit_offset); 1256 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL); 1257 ssize = MAX (ssize, ((signed) bit_offset / CHAR_BIT) + msize); 1258 } 1259 } 1260 else 1261 { 1262 memb[vlen].ctlm_offsethi = 0; 1263 memb[vlen].ctlm_offsetlo = 0; 1264 ssize = ctf_get_ctt_size (fp, &dtd->dtd_data, NULL, NULL); 1265 ssize = MAX (ssize, msize); 1266 } 1267 1268 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 1269 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI (ssize); 1270 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO (ssize); 1271 dtd->dtd_data.ctt_info = CTF_TYPE_INFO (kind, root, vlen + 1); 1272 1273 return 0; 1274 } 1275 1276 int 1277 ctf_add_member_encoded (ctf_dict_t *fp, ctf_id_t souid, const char *name, 1278 ctf_id_t type, unsigned long bit_offset, 1279 const ctf_encoding_t encoding) 1280 { 1281 ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type); 1282 int kind; 1283 int otype = type; 1284 1285 if (dtd == NULL) 1286 return (ctf_set_errno (fp, ECTF_BADID)); 1287 1288 kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); 1289 1290 if ((kind != CTF_K_INTEGER) && (kind != CTF_K_FLOAT) && (kind != CTF_K_ENUM)) 1291 return (ctf_set_errno (fp, ECTF_NOTINTFP)); 1292 1293 if ((type = ctf_add_slice (fp, CTF_ADD_NONROOT, otype, &encoding)) == CTF_ERR) 1294 return -1; /* errno is set for us. */ 1295 1296 return ctf_add_member_offset (fp, souid, name, type, bit_offset); 1297 } 1298 1299 int 1300 ctf_add_member (ctf_dict_t *fp, ctf_id_t souid, const char *name, 1301 ctf_id_t type) 1302 { 1303 return ctf_add_member_offset (fp, souid, name, type, (unsigned long) - 1); 1304 } 1305 1306 /* Add a variable regardless of whether or not it is already present. 1307 1308 Internal use only. */ 1309 int 1310 ctf_add_variable_forced (ctf_dict_t *fp, const char *name, ctf_id_t ref) 1311 { 1312 ctf_dvdef_t *dvd; 1313 ctf_dict_t *tmp = fp; 1314 1315 if (ctf_lookup_by_id (&tmp, ref) == NULL) 1316 return -1; /* errno is set for us. */ 1317 1318 /* Make sure this type is representable. */ 1319 if ((ctf_type_resolve (fp, ref) == CTF_ERR) 1320 && (ctf_errno (fp) == ECTF_NONREPRESENTABLE)) 1321 return -1; 1322 1323 if ((dvd = malloc (sizeof (ctf_dvdef_t))) == NULL) 1324 return (ctf_set_errno (fp, EAGAIN)); 1325 1326 if (name != NULL && (dvd->dvd_name = strdup (name)) == NULL) 1327 { 1328 free (dvd); 1329 return (ctf_set_errno (fp, EAGAIN)); 1330 } 1331 dvd->dvd_type = ref; 1332 dvd->dvd_snapshots = fp->ctf_snapshots; 1333 1334 if (ctf_dvd_insert (fp, dvd) < 0) 1335 { 1336 free (dvd->dvd_name); 1337 free (dvd); 1338 return -1; /* errno is set for us. */ 1339 } 1340 1341 return 0; 1342 } 1343 1344 int 1345 ctf_add_variable (ctf_dict_t *fp, const char *name, ctf_id_t ref) 1346 { 1347 if (ctf_lookup_variable_here (fp, name) != CTF_ERR) 1348 return (ctf_set_errno (fp, ECTF_DUPLICATE)); 1349 1350 if (ctf_errno (fp) != ECTF_NOTYPEDAT) 1351 return -1; /* errno is set for us. */ 1352 1353 return ctf_add_variable_forced (fp, name, ref); 1354 } 1355 1356 /* Add a function or object symbol regardless of whether or not it is already 1357 present (already existing symbols are silently overwritten). 1358 1359 Internal use only. */ 1360 int 1361 ctf_add_funcobjt_sym_forced (ctf_dict_t *fp, int is_function, const char *name, ctf_id_t id) 1362 { 1363 ctf_dict_t *tmp = fp; 1364 char *dupname; 1365 ctf_dynhash_t *h = is_function ? fp->ctf_funchash : fp->ctf_objthash; 1366 1367 if (ctf_lookup_by_id (&tmp, id) == NULL) 1368 return -1; /* errno is set for us. */ 1369 1370 if (is_function && ctf_type_kind (fp, id) != CTF_K_FUNCTION) 1371 return (ctf_set_errno (fp, ECTF_NOTFUNC)); 1372 1373 if ((dupname = strdup (name)) == NULL) 1374 return (ctf_set_errno (fp, ENOMEM)); 1375 1376 if (ctf_dynhash_insert (h, dupname, (void *) (uintptr_t) id) < 0) 1377 { 1378 free (dupname); 1379 return (ctf_set_errno (fp, ENOMEM)); 1380 } 1381 return 0; 1382 } 1383 1384 int 1385 ctf_add_funcobjt_sym (ctf_dict_t *fp, int is_function, const char *name, ctf_id_t id) 1386 { 1387 if (ctf_lookup_by_sym_or_name (fp, 0, name, 0, is_function) != CTF_ERR) 1388 return (ctf_set_errno (fp, ECTF_DUPLICATE)); 1389 1390 return ctf_add_funcobjt_sym_forced (fp, is_function, name, id); 1391 } 1392 1393 int 1394 ctf_add_objt_sym (ctf_dict_t *fp, const char *name, ctf_id_t id) 1395 { 1396 return (ctf_add_funcobjt_sym (fp, 0, name, id)); 1397 } 1398 1399 int 1400 ctf_add_func_sym (ctf_dict_t *fp, const char *name, ctf_id_t id) 1401 { 1402 return (ctf_add_funcobjt_sym (fp, 1, name, id)); 1403 } 1404 1405 typedef struct ctf_bundle 1406 { 1407 ctf_dict_t *ctb_dict; /* CTF dict handle. */ 1408 ctf_id_t ctb_type; /* CTF type identifier. */ 1409 ctf_dtdef_t *ctb_dtd; /* CTF dynamic type definition (if any). */ 1410 } ctf_bundle_t; 1411 1412 static int 1413 enumcmp (const char *name, int value, void *arg) 1414 { 1415 ctf_bundle_t *ctb = arg; 1416 int bvalue; 1417 1418 if (ctf_enum_value (ctb->ctb_dict, ctb->ctb_type, name, &bvalue) < 0) 1419 { 1420 ctf_err_warn (ctb->ctb_dict, 0, 0, 1421 _("conflict due to enum %s iteration error"), name); 1422 return 1; 1423 } 1424 if (value != bvalue) 1425 { 1426 ctf_err_warn (ctb->ctb_dict, 1, ECTF_CONFLICT, 1427 _("conflict due to enum value change: %i versus %i"), 1428 value, bvalue); 1429 return 1; 1430 } 1431 return 0; 1432 } 1433 1434 static int 1435 enumadd (const char *name, int value, void *arg) 1436 { 1437 ctf_bundle_t *ctb = arg; 1438 1439 return (ctf_add_enumerator (ctb->ctb_dict, ctb->ctb_type, 1440 name, value) < 0); 1441 } 1442 1443 static int 1444 membcmp (const char *name, ctf_id_t type _libctf_unused_, unsigned long offset, 1445 void *arg) 1446 { 1447 ctf_bundle_t *ctb = arg; 1448 ctf_membinfo_t ctm; 1449 1450 /* Don't check nameless members (e.g. anonymous structs/unions) against each 1451 other. */ 1452 if (name[0] == 0) 1453 return 0; 1454 1455 if (ctf_member_info (ctb->ctb_dict, ctb->ctb_type, name, &ctm) < 0) 1456 { 1457 ctf_err_warn (ctb->ctb_dict, 0, 0, 1458 _("conflict due to struct member %s iteration error"), 1459 name); 1460 return 1; 1461 } 1462 if (ctm.ctm_offset != offset) 1463 { 1464 ctf_err_warn (ctb->ctb_dict, 1, ECTF_CONFLICT, 1465 _("conflict due to struct member %s offset change: " 1466 "%lx versus %lx"), 1467 name, ctm.ctm_offset, offset); 1468 return 1; 1469 } 1470 return 0; 1471 } 1472 1473 /* Record the correspondence between a source and ctf_add_type()-added 1474 destination type: both types are translated into parent type IDs if need be, 1475 so they relate to the actual dictionary they are in. Outside controlled 1476 circumstances (like linking) it is probably not useful to do more than 1477 compare these pointers, since there is nothing stopping the user closing the 1478 source dict whenever they want to. 1479 1480 Our OOM handling here is just to not do anything, because this is called deep 1481 enough in the call stack that doing anything useful is painfully difficult: 1482 the worst consequence if we do OOM is a bit of type duplication anyway. */ 1483 1484 static void 1485 ctf_add_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, 1486 ctf_dict_t *dst_fp, ctf_id_t dst_type) 1487 { 1488 if (LCTF_TYPE_ISPARENT (src_fp, src_type) && src_fp->ctf_parent) 1489 src_fp = src_fp->ctf_parent; 1490 1491 src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type); 1492 1493 if (LCTF_TYPE_ISPARENT (dst_fp, dst_type) && dst_fp->ctf_parent) 1494 dst_fp = dst_fp->ctf_parent; 1495 1496 dst_type = LCTF_TYPE_TO_INDEX(dst_fp, dst_type); 1497 1498 if (dst_fp->ctf_link_type_mapping == NULL) 1499 { 1500 ctf_hash_fun f = ctf_hash_type_key; 1501 ctf_hash_eq_fun e = ctf_hash_eq_type_key; 1502 1503 if ((dst_fp->ctf_link_type_mapping = ctf_dynhash_create (f, e, free, 1504 NULL)) == NULL) 1505 return; 1506 } 1507 1508 ctf_link_type_key_t *key; 1509 key = calloc (1, sizeof (struct ctf_link_type_key)); 1510 if (!key) 1511 return; 1512 1513 key->cltk_fp = src_fp; 1514 key->cltk_idx = src_type; 1515 1516 /* No OOM checking needed, because if this doesn't work the worst we'll do is 1517 add a few more duplicate types (which will probably run out of memory 1518 anyway). */ 1519 ctf_dynhash_insert (dst_fp->ctf_link_type_mapping, key, 1520 (void *) (uintptr_t) dst_type); 1521 } 1522 1523 /* Look up a type mapping: return 0 if none. The DST_FP is modified to point to 1524 the parent if need be. The ID returned is from the dst_fp's perspective. */ 1525 static ctf_id_t 1526 ctf_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, ctf_dict_t **dst_fp) 1527 { 1528 ctf_link_type_key_t key; 1529 ctf_dict_t *target_fp = *dst_fp; 1530 ctf_id_t dst_type = 0; 1531 1532 if (LCTF_TYPE_ISPARENT (src_fp, src_type) && src_fp->ctf_parent) 1533 src_fp = src_fp->ctf_parent; 1534 1535 src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type); 1536 key.cltk_fp = src_fp; 1537 key.cltk_idx = src_type; 1538 1539 if (target_fp->ctf_link_type_mapping) 1540 dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping, 1541 &key); 1542 1543 if (dst_type != 0) 1544 { 1545 dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type, 1546 target_fp->ctf_parent != NULL); 1547 *dst_fp = target_fp; 1548 return dst_type; 1549 } 1550 1551 if (target_fp->ctf_parent) 1552 target_fp = target_fp->ctf_parent; 1553 else 1554 return 0; 1555 1556 if (target_fp->ctf_link_type_mapping) 1557 dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping, 1558 &key); 1559 1560 if (dst_type) 1561 dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type, 1562 target_fp->ctf_parent != NULL); 1563 1564 *dst_fp = target_fp; 1565 return dst_type; 1566 } 1567 1568 /* The ctf_add_type routine is used to copy a type from a source CTF dictionary 1569 to a dynamic destination dictionary. This routine operates recursively by 1570 following the source type's links and embedded member types. If the 1571 destination dict already contains a named type which has the same attributes, 1572 then we succeed and return this type but no changes occur. */ 1573 static ctf_id_t 1574 ctf_add_type_internal (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type, 1575 ctf_dict_t *proc_tracking_fp) 1576 { 1577 ctf_id_t dst_type = CTF_ERR; 1578 uint32_t dst_kind = CTF_K_UNKNOWN; 1579 ctf_dict_t *tmp_fp = dst_fp; 1580 ctf_id_t tmp; 1581 1582 const char *name; 1583 uint32_t kind, forward_kind, flag, vlen; 1584 1585 const ctf_type_t *src_tp, *dst_tp; 1586 ctf_bundle_t src, dst; 1587 ctf_encoding_t src_en, dst_en; 1588 ctf_arinfo_t src_ar, dst_ar; 1589 1590 ctf_funcinfo_t ctc; 1591 1592 ctf_id_t orig_src_type = src_type; 1593 1594 if ((src_tp = ctf_lookup_by_id (&src_fp, src_type)) == NULL) 1595 return (ctf_set_typed_errno (dst_fp, ctf_errno (src_fp))); 1596 1597 if ((ctf_type_resolve (src_fp, src_type) == CTF_ERR) 1598 && (ctf_errno (src_fp) == ECTF_NONREPRESENTABLE)) 1599 return (ctf_set_typed_errno (dst_fp, ECTF_NONREPRESENTABLE)); 1600 1601 name = ctf_strptr (src_fp, src_tp->ctt_name); 1602 kind = LCTF_INFO_KIND (src_fp, src_tp->ctt_info); 1603 flag = LCTF_INFO_ISROOT (src_fp, src_tp->ctt_info); 1604 vlen = LCTF_INFO_VLEN (src_fp, src_tp->ctt_info); 1605 1606 /* If this is a type we are currently in the middle of adding, hand it 1607 straight back. (This lets us handle self-referential structures without 1608 considering forwards and empty structures the same as their completed 1609 forms.) */ 1610 1611 tmp = ctf_type_mapping (src_fp, src_type, &tmp_fp); 1612 1613 if (tmp != 0) 1614 { 1615 if (ctf_dynhash_lookup (proc_tracking_fp->ctf_add_processing, 1616 (void *) (uintptr_t) src_type)) 1617 return tmp; 1618 1619 /* If this type has already been added from this dictionary, and is the 1620 same kind and (if a struct or union) has the same number of members, 1621 hand it straight back. */ 1622 1623 if (ctf_type_kind_unsliced (tmp_fp, tmp) == (int) kind) 1624 { 1625 if (kind == CTF_K_STRUCT || kind == CTF_K_UNION 1626 || kind == CTF_K_ENUM) 1627 { 1628 if ((dst_tp = ctf_lookup_by_id (&tmp_fp, dst_type)) != NULL) 1629 if (vlen == LCTF_INFO_VLEN (tmp_fp, dst_tp->ctt_info)) 1630 return tmp; 1631 } 1632 else 1633 return tmp; 1634 } 1635 } 1636 1637 forward_kind = kind; 1638 if (kind == CTF_K_FORWARD) 1639 forward_kind = src_tp->ctt_type; 1640 1641 /* If the source type has a name and is a root type (visible at the top-level 1642 scope), lookup the name in the destination dictionary and verify that it is 1643 of the same kind before we do anything else. */ 1644 1645 if ((flag & CTF_ADD_ROOT) && name[0] != '\0' 1646 && (tmp = ctf_lookup_by_rawname (dst_fp, forward_kind, name)) != 0) 1647 { 1648 dst_type = tmp; 1649 dst_kind = ctf_type_kind_unsliced (dst_fp, dst_type); 1650 } 1651 1652 /* If an identically named dst_type exists, fail with ECTF_CONFLICT 1653 unless dst_type is a forward declaration and src_type is a struct, 1654 union, or enum (i.e. the definition of the previous forward decl). 1655 1656 We also allow addition in the opposite order (addition of a forward when a 1657 struct, union, or enum already exists), which is a NOP and returns the 1658 already-present struct, union, or enum. */ 1659 1660 if (dst_type != CTF_ERR && dst_kind != kind) 1661 { 1662 if (kind == CTF_K_FORWARD 1663 && (dst_kind == CTF_K_ENUM || dst_kind == CTF_K_STRUCT 1664 || dst_kind == CTF_K_UNION)) 1665 { 1666 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); 1667 return dst_type; 1668 } 1669 1670 if (dst_kind != CTF_K_FORWARD 1671 || (kind != CTF_K_ENUM && kind != CTF_K_STRUCT 1672 && kind != CTF_K_UNION)) 1673 { 1674 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, 1675 _("ctf_add_type: conflict for type %s: " 1676 "kinds differ, new: %i; old (ID %lx): %i"), 1677 name, kind, dst_type, dst_kind); 1678 return (ctf_set_typed_errno (dst_fp, ECTF_CONFLICT)); 1679 } 1680 } 1681 1682 /* We take special action for an integer, float, or slice since it is 1683 described not only by its name but also its encoding. For integers, 1684 bit-fields exploit this degeneracy. */ 1685 1686 if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT || kind == CTF_K_SLICE) 1687 { 1688 if (ctf_type_encoding (src_fp, src_type, &src_en) != 0) 1689 return (ctf_set_typed_errno (dst_fp, ctf_errno (src_fp))); 1690 1691 if (dst_type != CTF_ERR) 1692 { 1693 ctf_dict_t *fp = dst_fp; 1694 1695 if ((dst_tp = ctf_lookup_by_id (&fp, dst_type)) == NULL) 1696 return CTF_ERR; 1697 1698 if (ctf_type_encoding (dst_fp, dst_type, &dst_en) != 0) 1699 return CTF_ERR; /* errno set for us. */ 1700 1701 if (LCTF_INFO_ISROOT (fp, dst_tp->ctt_info) & CTF_ADD_ROOT) 1702 { 1703 /* The type that we found in the hash is also root-visible. If 1704 the two types match then use the existing one; otherwise, 1705 declare a conflict. Note: slices are not certain to match 1706 even if there is no conflict: we must check the contained type 1707 too. */ 1708 1709 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0) 1710 { 1711 if (kind != CTF_K_SLICE) 1712 { 1713 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); 1714 return dst_type; 1715 } 1716 } 1717 else 1718 { 1719 return (ctf_set_typed_errno (dst_fp, ECTF_CONFLICT)); 1720 } 1721 } 1722 else 1723 { 1724 /* We found a non-root-visible type in the hash. If its encoding 1725 is the same, we can reuse it, unless it is a slice. */ 1726 1727 if (memcmp (&src_en, &dst_en, sizeof (ctf_encoding_t)) == 0) 1728 { 1729 if (kind != CTF_K_SLICE) 1730 { 1731 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); 1732 return dst_type; 1733 } 1734 } 1735 } 1736 } 1737 } 1738 1739 src.ctb_dict = src_fp; 1740 src.ctb_type = src_type; 1741 src.ctb_dtd = NULL; 1742 1743 dst.ctb_dict = dst_fp; 1744 dst.ctb_type = dst_type; 1745 dst.ctb_dtd = NULL; 1746 1747 /* Now perform kind-specific processing. If dst_type is CTF_ERR, then we add 1748 a new type with the same properties as src_type to dst_fp. If dst_type is 1749 not CTF_ERR, then we verify that dst_type has the same attributes as 1750 src_type. We recurse for embedded references. Before we start, we note 1751 that we are processing this type, to prevent infinite recursion: we do not 1752 re-process any type that appears in this list. The list is emptied 1753 wholesale at the end of processing everything in this recursive stack. */ 1754 1755 if (ctf_dynhash_insert (proc_tracking_fp->ctf_add_processing, 1756 (void *) (uintptr_t) src_type, (void *) 1) < 0) 1757 return ctf_set_typed_errno (dst_fp, ENOMEM); 1758 1759 switch (kind) 1760 { 1761 case CTF_K_INTEGER: 1762 /* If we found a match we will have either returned it or declared a 1763 conflict. */ 1764 dst_type = ctf_add_integer (dst_fp, flag, name, &src_en); 1765 break; 1766 1767 case CTF_K_FLOAT: 1768 /* If we found a match we will have either returned it or declared a 1769 conflict. */ 1770 dst_type = ctf_add_float (dst_fp, flag, name, &src_en); 1771 break; 1772 1773 case CTF_K_SLICE: 1774 /* We have checked for conflicting encodings: now try to add the 1775 contained type. */ 1776 src_type = ctf_type_reference (src_fp, src_type); 1777 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type, 1778 proc_tracking_fp); 1779 1780 if (src_type == CTF_ERR) 1781 return CTF_ERR; /* errno is set for us. */ 1782 1783 dst_type = ctf_add_slice (dst_fp, flag, src_type, &src_en); 1784 break; 1785 1786 case CTF_K_POINTER: 1787 case CTF_K_VOLATILE: 1788 case CTF_K_CONST: 1789 case CTF_K_RESTRICT: 1790 src_type = ctf_type_reference (src_fp, src_type); 1791 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type, 1792 proc_tracking_fp); 1793 1794 if (src_type == CTF_ERR) 1795 return CTF_ERR; /* errno is set for us. */ 1796 1797 dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind); 1798 break; 1799 1800 case CTF_K_ARRAY: 1801 if (ctf_array_info (src_fp, src_type, &src_ar) != 0) 1802 return (ctf_set_typed_errno (dst_fp, ctf_errno (src_fp))); 1803 1804 src_ar.ctr_contents = 1805 ctf_add_type_internal (dst_fp, src_fp, src_ar.ctr_contents, 1806 proc_tracking_fp); 1807 src_ar.ctr_index = ctf_add_type_internal (dst_fp, src_fp, 1808 src_ar.ctr_index, 1809 proc_tracking_fp); 1810 src_ar.ctr_nelems = src_ar.ctr_nelems; 1811 1812 if (src_ar.ctr_contents == CTF_ERR || src_ar.ctr_index == CTF_ERR) 1813 return CTF_ERR; /* errno is set for us. */ 1814 1815 if (dst_type != CTF_ERR) 1816 { 1817 if (ctf_array_info (dst_fp, dst_type, &dst_ar) != 0) 1818 return CTF_ERR; /* errno is set for us. */ 1819 1820 if (memcmp (&src_ar, &dst_ar, sizeof (ctf_arinfo_t))) 1821 { 1822 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, 1823 _("conflict for type %s against ID %lx: array info " 1824 "differs, old %lx/%lx/%x; new: %lx/%lx/%x"), 1825 name, dst_type, src_ar.ctr_contents, 1826 src_ar.ctr_index, src_ar.ctr_nelems, 1827 dst_ar.ctr_contents, dst_ar.ctr_index, 1828 dst_ar.ctr_nelems); 1829 return (ctf_set_typed_errno (dst_fp, ECTF_CONFLICT)); 1830 } 1831 } 1832 else 1833 dst_type = ctf_add_array (dst_fp, flag, &src_ar); 1834 break; 1835 1836 case CTF_K_FUNCTION: 1837 ctc.ctc_return = ctf_add_type_internal (dst_fp, src_fp, 1838 src_tp->ctt_type, 1839 proc_tracking_fp); 1840 ctc.ctc_argc = 0; 1841 ctc.ctc_flags = 0; 1842 1843 if (ctc.ctc_return == CTF_ERR) 1844 return CTF_ERR; /* errno is set for us. */ 1845 1846 dst_type = ctf_add_function (dst_fp, flag, &ctc, NULL); 1847 break; 1848 1849 case CTF_K_STRUCT: 1850 case CTF_K_UNION: 1851 { 1852 ctf_next_t *i = NULL; 1853 ssize_t offset; 1854 const char *membname; 1855 ctf_id_t src_membtype; 1856 1857 /* Technically to match a struct or union we need to check both 1858 ways (src members vs. dst, dst members vs. src) but we make 1859 this more optimal by only checking src vs. dst and comparing 1860 the total size of the structure (which we must do anyway) 1861 which covers the possibility of dst members not in src. 1862 This optimization can be defeated for unions, but is so 1863 pathological as to render it irrelevant for our purposes. */ 1864 1865 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD 1866 && dst_kind != CTF_K_FORWARD) 1867 { 1868 if (ctf_type_size (src_fp, src_type) != 1869 ctf_type_size (dst_fp, dst_type)) 1870 { 1871 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, 1872 _("conflict for type %s against ID %lx: union " 1873 "size differs, old %li, new %li"), name, 1874 dst_type, (long) ctf_type_size (src_fp, src_type), 1875 (long) ctf_type_size (dst_fp, dst_type)); 1876 return (ctf_set_typed_errno (dst_fp, ECTF_CONFLICT)); 1877 } 1878 1879 if (ctf_member_iter (src_fp, src_type, membcmp, &dst)) 1880 { 1881 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, 1882 _("conflict for type %s against ID %lx: members " 1883 "differ, see above"), name, dst_type); 1884 return (ctf_set_typed_errno (dst_fp, ECTF_CONFLICT)); 1885 } 1886 1887 break; 1888 } 1889 1890 dst_type = ctf_add_struct_sized (dst_fp, flag, name, 1891 ctf_type_size (src_fp, src_type)); 1892 if (dst_type == CTF_ERR) 1893 return CTF_ERR; /* errno is set for us. */ 1894 1895 /* Pre-emptively add this struct to the type mapping so that 1896 structures that refer to themselves work. */ 1897 ctf_add_type_mapping (src_fp, src_type, dst_fp, dst_type); 1898 1899 while ((offset = ctf_member_next (src_fp, src_type, &i, &membname, 1900 &src_membtype, 0)) >= 0) 1901 { 1902 ctf_dict_t *dst = dst_fp; 1903 ctf_id_t dst_membtype = ctf_type_mapping (src_fp, src_membtype, &dst); 1904 1905 if (dst_membtype == 0) 1906 { 1907 dst_membtype = ctf_add_type_internal (dst_fp, src_fp, 1908 src_membtype, 1909 proc_tracking_fp); 1910 if (dst_membtype == CTF_ERR) 1911 { 1912 if (ctf_errno (dst_fp) != ECTF_NONREPRESENTABLE) 1913 { 1914 ctf_next_destroy (i); 1915 break; 1916 } 1917 } 1918 } 1919 1920 if (ctf_add_member_offset (dst_fp, dst_type, membname, 1921 dst_membtype, offset) < 0) 1922 { 1923 ctf_next_destroy (i); 1924 break; 1925 } 1926 } 1927 if (ctf_errno (src_fp) != ECTF_NEXT_END) 1928 return CTF_ERR; /* errno is set for us. */ 1929 break; 1930 } 1931 1932 case CTF_K_ENUM: 1933 if (dst_type != CTF_ERR && kind != CTF_K_FORWARD 1934 && dst_kind != CTF_K_FORWARD) 1935 { 1936 if (ctf_enum_iter (src_fp, src_type, enumcmp, &dst) 1937 || ctf_enum_iter (dst_fp, dst_type, enumcmp, &src)) 1938 { 1939 ctf_err_warn (dst_fp, 1, ECTF_CONFLICT, 1940 _("conflict for enum %s against ID %lx: members " 1941 "differ, see above"), name, dst_type); 1942 return (ctf_set_typed_errno (dst_fp, ECTF_CONFLICT)); 1943 } 1944 } 1945 else 1946 { 1947 dst_type = ctf_add_enum (dst_fp, flag, name); 1948 if ((dst.ctb_type = dst_type) == CTF_ERR 1949 || ctf_enum_iter (src_fp, src_type, enumadd, &dst)) 1950 return CTF_ERR; /* errno is set for us */ 1951 } 1952 break; 1953 1954 case CTF_K_FORWARD: 1955 if (dst_type == CTF_ERR) 1956 dst_type = ctf_add_forward (dst_fp, flag, name, forward_kind); 1957 break; 1958 1959 case CTF_K_TYPEDEF: 1960 src_type = ctf_type_reference (src_fp, src_type); 1961 src_type = ctf_add_type_internal (dst_fp, src_fp, src_type, 1962 proc_tracking_fp); 1963 1964 if (src_type == CTF_ERR) 1965 return CTF_ERR; /* errno is set for us. */ 1966 1967 /* If dst_type is not CTF_ERR at this point, we should check if 1968 ctf_type_reference(dst_fp, dst_type) != src_type and if so fail with 1969 ECTF_CONFLICT. However, this causes problems with bitness typedefs 1970 that vary based on things like if 32-bit then pid_t is int otherwise 1971 long. We therefore omit this check and assume that if the identically 1972 named typedef already exists in dst_fp, it is correct or 1973 equivalent. */ 1974 1975 if (dst_type == CTF_ERR) 1976 dst_type = ctf_add_typedef (dst_fp, flag, name, src_type); 1977 1978 break; 1979 1980 default: 1981 return (ctf_set_typed_errno (dst_fp, ECTF_CORRUPT)); 1982 } 1983 1984 if (dst_type != CTF_ERR) 1985 ctf_add_type_mapping (src_fp, orig_src_type, dst_fp, dst_type); 1986 return dst_type; 1987 } 1988 1989 ctf_id_t 1990 ctf_add_type (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type) 1991 { 1992 ctf_id_t id; 1993 1994 if (!src_fp->ctf_add_processing) 1995 src_fp->ctf_add_processing = ctf_dynhash_create (ctf_hash_integer, 1996 ctf_hash_eq_integer, 1997 NULL, NULL); 1998 1999 /* We store the hash on the source, because it contains only source type IDs: 2000 but callers will invariably expect errors to appear on the dest. */ 2001 if (!src_fp->ctf_add_processing) 2002 return (ctf_set_typed_errno (dst_fp, ENOMEM)); 2003 2004 id = ctf_add_type_internal (dst_fp, src_fp, src_type, src_fp); 2005 ctf_dynhash_empty (src_fp->ctf_add_processing); 2006 2007 return id; 2008 } 2009