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