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 (c) 2015, 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 ctf_id_t
1250 ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)
1251 {
1252         ctf_hash_t *hp = &fp->ctf_enums;
1253         ctf_helem_t *hep = NULL;
1254         ctf_dtdef_t *dtd = NULL;
1255         ctf_id_t type = CTF_ERR;
1256 
1257         if (name != NULL)
1258                 hep = ctf_hash_lookup(hp, fp, name, strlen(name));
1259 
1260         if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD) {
1261                 type = hep->h_type;
1262                 dtd = ctf_dtd_lookup(fp, type);
1263                 if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_FORWARD)
1264                         dtd = NULL;
1265         }
1266 
1267         if (dtd == NULL) {
1268                 type = ctf_add_generic(fp, flag, name, &dtd);
1269                 if (type == CTF_ERR)
1270                         return (CTF_ERR); /* errno is set for us */
1271         }
1272 
1273         VERIFY(type != CTF_ERR);
1274         dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0);
1275         dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;
1276 
1277         /*
1278          * Always dirty in case we modified a forward.
1279          */
1280         fp->ctf_flags |= LCTF_DIRTY;
1281 
1282         return (type);
1283 }
1284 
1285 ctf_id_t
1286 ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)
1287 {
1288         ctf_hash_t *hp;
1289         ctf_helem_t *hep;
1290         ctf_dtdef_t *dtd;
1291         ctf_id_t type;
1292 
1293         switch (kind) {
1294         case CTF_K_STRUCT:
1295                 hp = &fp->ctf_structs;
1296                 break;
1297         case CTF_K_UNION:
1298                 hp = &fp->ctf_unions;
1299                 break;
1300         case CTF_K_ENUM:
1301                 hp = &fp->ctf_enums;
1302                 break;
1303         default:
1304                 return (ctf_set_errno(fp, ECTF_NOTSUE));
1305         }
1306 
1307         /*
1308          * If the type is already defined or exists as a forward tag, just
1309          * return the ctf_id_t of the existing definition.
1310          */
1311         if (name != NULL && (hep = ctf_hash_lookup(hp,
1312             fp, name, strlen(name))) != NULL)
1313                 return (hep->h_type);
1314 
1315         if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1316                 return (CTF_ERR); /* errno is set for us */
1317 
1318         dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0);
1319         dtd->dtd_data.ctt_type = kind;
1320 
1321         return (type);
1322 }
1323 
1324 ctf_id_t
1325 ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1326 {
1327         ctf_dtdef_t *dtd;
1328         ctf_id_t type;
1329         ctf_file_t *fpd;
1330 
1331         fpd = fp;
1332         if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&
1333             ctf_dtd_lookup(fp, ref) == NULL))
1334                 return (ctf_set_errno(fp, EINVAL));
1335 
1336         if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)
1337                 return (CTF_ERR); /* errno is set for us */
1338 
1339         dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0);
1340         dtd->dtd_data.ctt_type = (ushort_t)ref;
1341         ctf_ref_inc(fp, ref);
1342 
1343         return (type);
1344 }
1345 
1346 ctf_id_t
1347 ctf_add_volatile(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1348 {
1349         return (ctf_add_reftype(fp, flag, name, ref, CTF_K_VOLATILE));
1350 }
1351 
1352 ctf_id_t
1353 ctf_add_const(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1354 {
1355         return (ctf_add_reftype(fp, flag, name, ref, CTF_K_CONST));
1356 }
1357 
1358 ctf_id_t
1359 ctf_add_restrict(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)
1360 {
1361         return (ctf_add_reftype(fp, flag, name, ref, CTF_K_RESTRICT));
1362 }
1363 
1364 int
1365 ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
1366 {
1367         ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
1368         ctf_dmdef_t *dmd;
1369 
1370         uint_t kind, vlen, root;
1371         char *s;
1372 
1373         if (name == NULL)
1374                 return (ctf_set_errno(fp, EINVAL));
1375 
1376         if (!(fp->ctf_flags & LCTF_RDWR))
1377                 return (ctf_set_errno(fp, ECTF_RDONLY));
1378 
1379         if (dtd == NULL)
1380                 return (ctf_set_errno(fp, ECTF_BADID));
1381 
1382         kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1383         root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1384         vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1385 
1386         if (kind != CTF_K_ENUM)
1387                 return (ctf_set_errno(fp, ECTF_NOTENUM));
1388 
1389         if (vlen == CTF_MAX_VLEN)
1390                 return (ctf_set_errno(fp, ECTF_DTFULL));
1391 
1392         for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1393             dmd != NULL; dmd = ctf_list_next(dmd)) {
1394                 if (strcmp(dmd->dmd_name, name) == 0) {
1395                         ctf_dprintf("encountered duplicate member %s\n", name);
1396                         return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1397                 }
1398         }
1399 
1400         if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1401                 return (ctf_set_errno(fp, EAGAIN));
1402 
1403         if ((s = ctf_strdup(name)) == NULL) {
1404                 ctf_free(dmd, sizeof (ctf_dmdef_t));
1405                 return (ctf_set_errno(fp, EAGAIN));
1406         }
1407 
1408         dmd->dmd_name = s;
1409         dmd->dmd_type = CTF_ERR;
1410         dmd->dmd_offset = 0;
1411         dmd->dmd_value = value;
1412 
1413         dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1414         ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1415 
1416         fp->ctf_dtstrlen += strlen(s) + 1;
1417         fp->ctf_flags |= LCTF_DIRTY;
1418 
1419         return (0);
1420 }
1421 
1422 int
1423 ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type,
1424     ulong_t offset)
1425 {
1426         ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
1427         ctf_dmdef_t *dmd;
1428 
1429         ulong_t mbitsz;
1430         ssize_t msize, malign, ssize;
1431         uint_t kind, vlen, root;
1432         int mkind;
1433         char *s = NULL;
1434 
1435         if (!(fp->ctf_flags & LCTF_RDWR))
1436                 return (ctf_set_errno(fp, ECTF_RDONLY));
1437 
1438         if (dtd == NULL)
1439                 return (ctf_set_errno(fp, ECTF_BADID));
1440 
1441         kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
1442         root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
1443         vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
1444 
1445         if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
1446                 return (ctf_set_errno(fp, ECTF_NOTSOU));
1447 
1448         if (vlen == CTF_MAX_VLEN)
1449                 return (ctf_set_errno(fp, ECTF_DTFULL));
1450 
1451         /*
1452          * Structures may have members which are anonymous. If they have two of
1453          * these, then the duplicate member detection would find it due to the
1454          * string of "", so we skip it.
1455          */
1456         if (name != NULL && *name != '\0') {
1457                 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1458                     dmd != NULL; dmd = ctf_list_next(dmd)) {
1459                         if (dmd->dmd_name != NULL &&
1460                             strcmp(dmd->dmd_name, name) == 0) {
1461                                 return (ctf_set_errno(fp, ECTF_DUPMEMBER));
1462                         }
1463                 }
1464         }
1465 
1466         if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
1467             (malign = ctf_type_align(fp, type)) == CTF_ERR ||
1468             (mkind = ctf_type_kind(fp, type)) == CTF_ERR)
1469                 return (CTF_ERR); /* errno is set for us */
1470 
1471         /*
1472          * ctf_type_size returns sizes in bytes. However, for bitfields, that
1473          * means that it may misrepresent and actually rounds it up to a power
1474          * of two and store that in bytes. So instead we have to get the
1475          * Integers encoding and rely on that.
1476          */
1477         if (mkind == CTF_K_INTEGER) {
1478                 ctf_encoding_t e;
1479 
1480                 if (ctf_type_encoding(fp, type, &e) == CTF_ERR)
1481                         return (CTF_ERR); /* errno is set for us */
1482                 mbitsz = e.cte_bits;
1483         } else if (mkind == CTF_K_FORWARD) {
1484                 /*
1485                  * This is a rather rare case. In general one cannot add a
1486                  * forward to a structure. However, the CTF tools traditionally
1487                  * tried to add a forward to the struct cpu as the last member.
1488                  * Therefore, if we find one here, we're going to verify the
1489                  * size and make sure it's zero. It's certainly odd, but that's
1490                  * life.
1491                  *
1492                  * Further, if it's not an absolute position being specified,
1493                  * then we refuse to add it.
1494                  */
1495                 if (offset == ULONG_MAX)
1496                         return (ctf_set_errno(fp, EINVAL));
1497                 VERIFY(msize == 0);
1498                 mbitsz = msize;
1499         } else {
1500                 mbitsz = msize * 8;
1501         }
1502 
1503         if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1504                 return (ctf_set_errno(fp, EAGAIN));
1505 
1506         if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1507                 ctf_free(dmd, sizeof (ctf_dmdef_t));
1508                 return (ctf_set_errno(fp, EAGAIN));
1509         }
1510 
1511         dmd->dmd_name = s;
1512         dmd->dmd_type = type;
1513         dmd->dmd_value = -1;
1514 
1515         if (kind == CTF_K_STRUCT && vlen != 0) {
1516                 ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
1517                 ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
1518                 size_t off;
1519 
1520                 if (offset == ULONG_MAX) {
1521                         ctf_encoding_t linfo;
1522                         ssize_t lsize;
1523 
1524                         off = lmd->dmd_offset;
1525                         if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
1526                                 off += linfo.cte_bits;
1527                         else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
1528                                 off += lsize * NBBY;
1529 
1530                         /*
1531                          * Round up the offset of the end of the last member to
1532                          * the next byte boundary, convert 'off' to bytes, and
1533                          * then round it up again to the next multiple of the
1534                          * alignment required by the new member.  Finally,
1535                          * convert back to bits and store the result in
1536                          * dmd_offset.  Technically we could do more efficient
1537                          * packing if the new member is a bit-field, but we're
1538                          * the "compiler" and ANSI says we can do as we choose.
1539                          */
1540                         off = roundup(off, NBBY) / NBBY;
1541                         off = roundup(off, MAX(malign, 1));
1542                         dmd->dmd_offset = off * NBBY;
1543                         ssize = off + msize;
1544                 } else {
1545                         dmd->dmd_offset = offset;
1546                         ssize = (offset + mbitsz) / NBBY;
1547                 }
1548         } else {
1549                 dmd->dmd_offset = 0;
1550                 ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
1551                 ssize = MAX(ssize, msize);
1552         }
1553 
1554         if (ssize > CTF_MAX_SIZE) {
1555                 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1556                 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
1557                 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
1558         } else
1559                 dtd->dtd_data.ctt_size = (ushort_t)ssize;
1560 
1561         dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
1562         ctf_list_append(&dtd->dtd_u.dtu_members, dmd);
1563 
1564         if (s != NULL)
1565                 fp->ctf_dtstrlen += strlen(s) + 1;
1566 
1567         ctf_ref_inc(fp, type);
1568         fp->ctf_flags |= LCTF_DIRTY;
1569         return (0);
1570 }
1571 
1572 /*
1573  * This removes a type from the dynamic section. This will fail if the type is
1574  * referenced by another type. Note that the CTF ID is never reused currently by
1575  * CTF. Note that if this container is a parent container then we just outright
1576  * refuse to remove the type. There currently is no notion of searching for the
1577  * ctf_dtdef_t in parent containers. If there is, then this constraint could
1578  * become finer grained.
1579  */
1580 int
1581 ctf_delete_type(ctf_file_t *fp, ctf_id_t type)
1582 {
1583         ctf_file_t *fpd;
1584         ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);
1585 
1586         if (!(fp->ctf_flags & LCTF_RDWR))
1587                 return (ctf_set_errno(fp, ECTF_RDONLY));
1588 
1589         /*
1590          * We want to give as useful an errno as possible. That means that we
1591          * want to distinguish between a type which does not exist and one for
1592          * which the type is not dynamic.
1593          */
1594         fpd = fp;
1595         if (ctf_lookup_by_id(&fpd, type) == NULL &&
1596             ctf_dtd_lookup(fp, type) == NULL)
1597                 return (CTF_ERR); /* errno is set for us */
1598 
1599         if (dtd == NULL)
1600                 return (ctf_set_errno(fp, ECTF_NOTDYN));
1601 
1602         if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)
1603                 return (ctf_set_errno(fp, ECTF_REFERENCED));
1604 
1605         ctf_dtd_delete(fp, dtd);
1606         fp->ctf_flags |= LCTF_DIRTY;
1607         return (0);
1608 }
1609 
1610 static int
1611 enumcmp(const char *name, int value, void *arg)
1612 {
1613         ctf_bundle_t *ctb = arg;
1614         int bvalue;
1615 
1616         return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,
1617             name, &bvalue) == CTF_ERR || value != bvalue);
1618 }
1619 
1620 static int
1621 enumadd(const char *name, int value, void *arg)
1622 {
1623         ctf_bundle_t *ctb = arg;
1624 
1625         return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,
1626             name, value) == CTF_ERR);
1627 }
1628 
1629 /*ARGSUSED*/
1630 static int
1631 membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1632 {
1633         ctf_bundle_t *ctb = arg;
1634         ctf_membinfo_t ctm;
1635 
1636         return (ctf_member_info(ctb->ctb_file, ctb->ctb_type,
1637             name, &ctm) == CTF_ERR || ctm.ctm_offset != offset);
1638 }
1639 
1640 static int
1641 membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)
1642 {
1643         ctf_bundle_t *ctb = arg;
1644         ctf_dmdef_t *dmd;
1645         char *s = NULL;
1646 
1647         if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
1648                 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1649 
1650         if (name != NULL && (s = ctf_strdup(name)) == NULL) {
1651                 ctf_free(dmd, sizeof (ctf_dmdef_t));
1652                 return (ctf_set_errno(ctb->ctb_file, EAGAIN));
1653         }
1654 
1655         /*
1656          * For now, dmd_type is copied as the src_fp's type; it is reset to an
1657          * equivalent dst_fp type by a final loop in ctf_add_type(), below.
1658          */
1659         dmd->dmd_name = s;
1660         dmd->dmd_type = type;
1661         dmd->dmd_offset = offset;
1662         dmd->dmd_value = -1;
1663 
1664         ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);
1665 
1666         if (s != NULL)
1667                 ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;
1668 
1669         ctb->ctb_file->ctf_flags |= LCTF_DIRTY;
1670         return (0);
1671 }
1672 
1673 /*
1674  * The ctf_add_type routine is used to copy a type from a source CTF container
1675  * to a dynamic destination container.  This routine operates recursively by
1676  * following the source type's links and embedded member types.  If the
1677  * destination container already contains a named type which has the same
1678  * attributes, then we succeed and return this type but no changes occur.
1679  */
1680 ctf_id_t
1681 ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)
1682 {
1683         ctf_id_t dst_type = CTF_ERR;
1684         uint_t dst_kind = CTF_K_UNKNOWN;
1685 
1686         const ctf_type_t *tp;
1687         const char *name;
1688         uint_t kind, flag, vlen;
1689 
1690         ctf_bundle_t src, dst;
1691         ctf_encoding_t src_en, dst_en;
1692         ctf_arinfo_t src_ar, dst_ar;
1693 
1694         ctf_dtdef_t *dtd;
1695         ctf_funcinfo_t ctc;
1696         ssize_t size;
1697 
1698         ctf_hash_t *hp;
1699         ctf_helem_t *hep;
1700 
1701         if (dst_fp == src_fp)
1702                 return (src_type);
1703 
1704         if (!(dst_fp->ctf_flags & LCTF_RDWR))
1705                 return (ctf_set_errno(dst_fp, ECTF_RDONLY));
1706 
1707         if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)
1708                 return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1709 
1710         name = ctf_strptr(src_fp, tp->ctt_name);
1711         kind = LCTF_INFO_KIND(src_fp, tp->ctt_info);
1712         flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info);
1713         vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info);
1714 
1715         switch (kind) {
1716         case CTF_K_STRUCT:
1717                 hp = &dst_fp->ctf_structs;
1718                 break;
1719         case CTF_K_UNION:
1720                 hp = &dst_fp->ctf_unions;
1721                 break;
1722         case CTF_K_ENUM:
1723                 hp = &dst_fp->ctf_enums;
1724                 break;
1725         default:
1726                 hp = &dst_fp->ctf_names;
1727                 break;
1728         }
1729 
1730         /*
1731          * If the source type has a name and is a root type (visible at the
1732          * top-level scope), lookup the name in the destination container and
1733          * verify that it is of the same kind before we do anything else.
1734          */
1735         if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&
1736             (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {
1737                 dst_type = (ctf_id_t)hep->h_type;
1738                 dst_kind = ctf_type_kind(dst_fp, dst_type);
1739         }
1740 
1741         /*
1742          * If an identically named dst_type exists, fail with ECTF_CONFLICT
1743          * unless dst_type is a forward declaration and src_type is a struct,
1744          * union, or enum (i.e. the definition of the previous forward decl).
1745          */
1746         if (dst_type != CTF_ERR && dst_kind != kind && (
1747             dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&
1748             kind != CTF_K_STRUCT && kind != CTF_K_UNION)))
1749                 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1750 
1751         /*
1752          * If the non-empty name was not found in the appropriate hash, search
1753          * the list of pending dynamic definitions that are not yet committed.
1754          * If a matching name and kind are found, assume this is the type that
1755          * we are looking for.  This is necessary to permit ctf_add_type() to
1756          * operate recursively on entities such as a struct that contains a
1757          * pointer member that refers to the same struct type.
1758          */
1759         if (dst_type == CTF_ERR && name[0] != '\0') {
1760                 for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&
1761                     dtd->dtd_type > dst_fp->ctf_dtoldid;
1762                     dtd = ctf_list_prev(dtd)) {
1763                         if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind &&
1764                             dtd->dtd_name != NULL &&
1765                             strcmp(dtd->dtd_name, name) == 0)
1766                                 return (dtd->dtd_type);
1767                 }
1768         }
1769 
1770         src.ctb_file = src_fp;
1771         src.ctb_type = src_type;
1772         src.ctb_dtd = NULL;
1773 
1774         dst.ctb_file = dst_fp;
1775         dst.ctb_type = dst_type;
1776         dst.ctb_dtd = NULL;
1777 
1778         /*
1779          * Now perform kind-specific processing.  If dst_type is CTF_ERR, then
1780          * we add a new type with the same properties as src_type to dst_fp.
1781          * If dst_type is not CTF_ERR, then we verify that dst_type has the
1782          * same attributes as src_type.  We recurse for embedded references.
1783          */
1784         switch (kind) {
1785         case CTF_K_INTEGER:
1786         case CTF_K_FLOAT:
1787                 if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)
1788                         return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1789 
1790                 if (dst_type != CTF_ERR) {
1791                         if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)
1792                                 return (CTF_ERR); /* errno is set for us */
1793 
1794                         if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))
1795                                 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1796 
1797                 } else if (kind == CTF_K_INTEGER) {
1798                         dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);
1799                 } else
1800                         dst_type = ctf_add_float(dst_fp, flag, name, &src_en);
1801                 break;
1802 
1803         case CTF_K_POINTER:
1804         case CTF_K_VOLATILE:
1805         case CTF_K_CONST:
1806         case CTF_K_RESTRICT:
1807                 src_type = ctf_type_reference(src_fp, src_type);
1808                 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1809 
1810                 if (src_type == CTF_ERR)
1811                         return (CTF_ERR); /* errno is set for us */
1812 
1813                 dst_type = ctf_add_reftype(dst_fp, flag, NULL, src_type, kind);
1814                 break;
1815 
1816         case CTF_K_ARRAY:
1817                 if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)
1818                         return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));
1819 
1820                 src_ar.ctr_contents =
1821                     ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);
1822                 src_ar.ctr_index =
1823                     ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);
1824                 src_ar.ctr_nelems = src_ar.ctr_nelems;
1825 
1826                 if (src_ar.ctr_contents == CTF_ERR ||
1827                     src_ar.ctr_index == CTF_ERR)
1828                         return (CTF_ERR); /* errno is set for us */
1829 
1830                 if (dst_type != CTF_ERR) {
1831                         if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)
1832                                 return (CTF_ERR); /* errno is set for us */
1833 
1834                         if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))
1835                                 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1836                 } else
1837                         dst_type = ctf_add_array(dst_fp, flag, &src_ar);
1838                 break;
1839 
1840         case CTF_K_FUNCTION:
1841                 ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type);
1842                 ctc.ctc_argc = 0;
1843                 ctc.ctc_flags = 0;
1844 
1845                 if (ctc.ctc_return == CTF_ERR)
1846                         return (CTF_ERR); /* errno is set for us */
1847 
1848                 dst_type = ctf_add_funcptr(dst_fp, flag, &ctc, NULL);
1849                 break;
1850 
1851         case CTF_K_STRUCT:
1852         case CTF_K_UNION: {
1853                 ctf_dmdef_t *dmd;
1854                 int errs = 0;
1855 
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 && dst_kind != CTF_K_FORWARD) {
1866                         if (ctf_type_size(src_fp, src_type) !=
1867                             ctf_type_size(dst_fp, dst_type))
1868                                 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1869 
1870                         if (ctf_member_iter(src_fp, src_type, membcmp, &dst))
1871                                 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1872 
1873                         break;
1874                 }
1875 
1876                 /*
1877                  * Unlike the other cases, copying structs and unions is done
1878                  * manually so as to avoid repeated lookups in ctf_add_member
1879                  * and to ensure the exact same member offsets as in src_type.
1880                  */
1881                 dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);
1882                 if (dst_type == CTF_ERR)
1883                         return (CTF_ERR); /* errno is set for us */
1884 
1885                 dst.ctb_type = dst_type;
1886                 dst.ctb_dtd = dtd;
1887 
1888                 if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)
1889                         errs++; /* increment errs and fail at bottom of case */
1890 
1891                 if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) {
1892                         dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
1893                         dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);
1894                         dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);
1895                 } else
1896                         dtd->dtd_data.ctt_size = (ushort_t)size;
1897 
1898                 dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen);
1899 
1900                 /*
1901                  * Make a final pass through the members changing each dmd_type
1902                  * (a src_fp type) to an equivalent type in dst_fp.  We pass
1903                  * through all members, leaving any that fail set to CTF_ERR.
1904                  */
1905                 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1906                     dmd != NULL; dmd = ctf_list_next(dmd)) {
1907                         if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,
1908                             dmd->dmd_type)) == CTF_ERR)
1909                                 errs++;
1910                 }
1911 
1912                 if (errs)
1913                         return (CTF_ERR); /* errno is set for us */
1914 
1915                 /*
1916                  * Now that we know that we can't fail, we go through and bump
1917                  * all the reference counts on the member types.
1918                  */
1919                 for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
1920                     dmd != NULL; dmd = ctf_list_next(dmd))
1921                         ctf_ref_inc(dst_fp, dmd->dmd_type);
1922                 break;
1923         }
1924 
1925         case CTF_K_ENUM:
1926                 if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {
1927                         if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||
1928                             ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))
1929                                 return (ctf_set_errno(dst_fp, ECTF_CONFLICT));
1930                 } else {
1931                         dst_type = ctf_add_enum(dst_fp, flag, name);
1932                         if ((dst.ctb_type = dst_type) == CTF_ERR ||
1933                             ctf_enum_iter(src_fp, src_type, enumadd, &dst))
1934                                 return (CTF_ERR); /* errno is set for us */
1935                 }
1936                 break;
1937 
1938         case CTF_K_FORWARD:
1939                 if (dst_type == CTF_ERR) {
1940                         dst_type = ctf_add_forward(dst_fp,
1941                             flag, name, CTF_K_STRUCT); /* assume STRUCT */
1942                 }
1943                 break;
1944 
1945         case CTF_K_TYPEDEF:
1946                 src_type = ctf_type_reference(src_fp, src_type);
1947                 src_type = ctf_add_type(dst_fp, src_fp, src_type);
1948 
1949                 if (src_type == CTF_ERR)
1950                         return (CTF_ERR); /* errno is set for us */
1951 
1952                 /*
1953                  * If dst_type is not CTF_ERR at this point, we should check if
1954                  * ctf_type_reference(dst_fp, dst_type) != src_type and if so
1955                  * fail with ECTF_CONFLICT.  However, this causes problems with
1956                  * <sys/types.h> typedefs that vary based on things like if
1957                  * _ILP32x then pid_t is int otherwise long.  We therefore omit
1958                  * this check and assume that if the identically named typedef
1959                  * already exists in dst_fp, it is correct or equivalent.
1960                  */
1961                 if (dst_type == CTF_ERR) {
1962                         dst_type = ctf_add_typedef(dst_fp, flag,
1963                             name, src_type);
1964                 }
1965                 break;
1966 
1967         default:
1968                 return (ctf_set_errno(dst_fp, ECTF_CORRUPT));
1969         }
1970 
1971         return (dst_type);
1972 }
1973 
1974 int
1975 ctf_add_function(ctf_file_t *fp, ulong_t idx, const ctf_funcinfo_t *fip,
1976     const ctf_id_t *argc)
1977 {
1978         int i;
1979         ctf_dsdef_t *dsd;
1980         ctf_file_t *afp;
1981         uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
1982 
1983         if (!(fp->ctf_flags & LCTF_RDWR))
1984                 return (ctf_set_errno(fp, ECTF_RDONLY));
1985 
1986         if (ctf_dsd_lookup(fp, idx) != NULL)
1987                 return (ctf_set_errno(fp, ECTF_CONFLICT));
1988 
1989         if (symbase == (uintptr_t)NULL)
1990                 return (ctf_set_errno(fp, ECTF_STRTAB));
1991 
1992         if (idx > fp->ctf_nsyms)
1993                 return (ctf_set_errno(fp, ECTF_NOTDATA));
1994 
1995         if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
1996                 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
1997                 if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
1998                         return (ctf_set_errno(fp, ECTF_NOTFUNC));
1999         } else {
2000                 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2001                 if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
2002                         return (ctf_set_errno(fp, ECTF_NOTFUNC));
2003         }
2004 
2005         afp = fp;
2006         if (ctf_lookup_by_id(&afp, fip->ctc_return) == NULL)
2007                 return (CTF_ERR); /* errno is set for us */
2008 
2009         for (i = 0; i < fip->ctc_argc; i++) {
2010                 afp = fp;
2011                 if (ctf_lookup_by_id(&afp, argc[i]) == NULL)
2012                         return (CTF_ERR); /* errno is set for us */
2013         }
2014 
2015         dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2016         if (dsd == NULL)
2017                 return (ctf_set_errno(fp, ENOMEM));
2018         dsd->dsd_nargs = fip->ctc_argc;
2019         if (fip->ctc_flags & CTF_FUNC_VARARG)
2020                 dsd->dsd_nargs++;
2021         if (dsd->dsd_nargs != 0) {
2022                 dsd->dsd_argc = ctf_alloc(sizeof (ctf_id_t) * dsd->dsd_nargs);
2023                 if (dsd->dsd_argc == NULL) {
2024                         ctf_free(dsd, sizeof (ctf_dsdef_t));
2025                         return (ctf_set_errno(fp, ENOMEM));
2026                 }
2027                 bcopy(argc, dsd->dsd_argc, sizeof (ctf_id_t) * fip->ctc_argc);
2028                 if (fip->ctc_flags & CTF_FUNC_VARARG)
2029                         dsd->dsd_argc[fip->ctc_argc] = 0;
2030         }
2031         dsd->dsd_symidx = idx;
2032         dsd->dsd_tid = fip->ctc_return;
2033 
2034         ctf_dsd_insert(fp, dsd);
2035         fp->ctf_flags |= LCTF_DIRTY;
2036 
2037         return (0);
2038 }
2039 
2040 int
2041 ctf_add_object(ctf_file_t *fp, ulong_t idx, ctf_id_t type)
2042 {
2043         ctf_dsdef_t *dsd;
2044         ctf_file_t *afp;
2045         uintptr_t symbase = (uintptr_t)fp->ctf_symtab.cts_data;
2046 
2047         if (!(fp->ctf_flags & LCTF_RDWR))
2048                 return (ctf_set_errno(fp, ECTF_RDONLY));
2049 
2050         if (!(fp->ctf_flags & LCTF_RDWR))
2051                 return (ctf_set_errno(fp, ECTF_RDONLY));
2052 
2053         if (ctf_dsd_lookup(fp, idx) != NULL)
2054                 return (ctf_set_errno(fp, ECTF_CONFLICT));
2055 
2056         if (symbase == (uintptr_t)NULL)
2057                 return (ctf_set_errno(fp, ECTF_STRTAB));
2058 
2059         if (idx > fp->ctf_nsyms)
2060                 return (ctf_set_errno(fp, ECTF_NOTDATA));
2061 
2062         if (fp->ctf_symtab.cts_entsize == sizeof (Elf32_Sym)) {
2063                 const Elf32_Sym *symp = (Elf32_Sym *)symbase + idx;
2064                 if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
2065                         return (ctf_set_errno(fp, ECTF_NOTDATA));
2066         } else {
2067                 const Elf64_Sym *symp = (Elf64_Sym *)symbase + idx;
2068                 if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
2069                         return (ctf_set_errno(fp, ECTF_NOTDATA));
2070         }
2071 
2072         afp = fp;
2073         if (ctf_lookup_by_id(&afp, type) == NULL)
2074                 return (CTF_ERR); /* errno is set for us */
2075 
2076         dsd = ctf_alloc(sizeof (ctf_dsdef_t));
2077         if (dsd == NULL)
2078                 return (ctf_set_errno(fp, ENOMEM));
2079         dsd->dsd_symidx = idx;
2080         dsd->dsd_tid = type;
2081         dsd->dsd_argc = NULL;
2082 
2083         ctf_dsd_insert(fp, dsd);
2084         fp->ctf_flags |= LCTF_DIRTY;
2085 
2086         return (0);
2087 }
2088 
2089 void
2090 ctf_dataptr(ctf_file_t *fp, const void **addrp, size_t *sizep)
2091 {
2092         if (addrp != NULL)
2093                 *addrp = fp->ctf_base;
2094         if (sizep != NULL)
2095                 *sizep = fp->ctf_size;
2096 }
2097 
2098 int
2099 ctf_add_label(ctf_file_t *fp, const char *name, ctf_id_t type, uint_t position)
2100 {
2101         ctf_file_t *fpd;
2102         ctf_dldef_t *dld;
2103 
2104         if (name == NULL)
2105                 return (ctf_set_errno(fp, EINVAL));
2106 
2107         if (!(fp->ctf_flags & LCTF_RDWR))
2108                 return (ctf_set_errno(fp, ECTF_RDONLY));
2109 
2110         fpd = fp;
2111         if (type != 0 && ctf_lookup_by_id(&fpd, type) == NULL)
2112                 return (CTF_ERR); /* errno is set for us */
2113 
2114         if (type != 0 && (fp->ctf_flags & LCTF_CHILD) &&
2115             CTF_TYPE_ISPARENT(type))
2116                 return (ctf_set_errno(fp, ECTF_NOPARENT));
2117 
2118         if (ctf_dld_lookup(fp, name) != NULL)
2119                 return (ctf_set_errno(fp, ECTF_LABELEXISTS));
2120 
2121         if ((dld = ctf_alloc(sizeof (ctf_dldef_t))) == NULL)
2122                 return (ctf_set_errno(fp, EAGAIN));
2123 
2124         if ((dld->dld_name = ctf_strdup(name)) == NULL) {
2125                 ctf_free(dld, sizeof (ctf_dldef_t));
2126                 return (ctf_set_errno(fp, EAGAIN));
2127         }
2128 
2129         dld->dld_type = type;
2130         fp->ctf_dtstrlen += strlen(name) + 1;
2131         ctf_dld_insert(fp, dld, position);
2132         fp->ctf_flags |= LCTF_DIRTY;
2133 
2134         return (0);
2135 }
2136 
2137 /*
2138  * Update the size of a structure or union. Note that we don't allow this to
2139  * shrink the size of a struct or union, only to increase it. This is useful for
2140  * cases when you have a structure whose actual size is larger than the sum of
2141  * its members due to padding for natural alignment.
2142  */
2143 int
2144 ctf_set_size(ctf_file_t *fp, ctf_id_t id, const ulong_t newsz)
2145 {
2146         ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2147         uint_t kind;
2148         size_t oldsz;
2149 
2150         if (!(fp->ctf_flags & LCTF_RDWR))
2151                 return (ctf_set_errno(fp, ECTF_RDONLY));
2152 
2153         if (dtd == NULL)
2154                 return (ctf_set_errno(fp, ECTF_BADID));
2155 
2156         kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2157 
2158         if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2159                 return (ctf_set_errno(fp, ECTF_NOTSOU));
2160 
2161         if ((oldsz = dtd->dtd_data.ctt_size) == CTF_LSIZE_SENT)
2162                 oldsz = CTF_TYPE_LSIZE(&dtd->dtd_data);
2163 
2164         if (newsz < oldsz)
2165                 return (ctf_set_errno(fp, EINVAL));
2166 
2167         if (newsz > CTF_MAX_SIZE) {
2168                 dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
2169                 dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(newsz);
2170                 dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(newsz);
2171         } else {
2172                 dtd->dtd_data.ctt_size = (ushort_t)newsz;
2173         }
2174 
2175         fp->ctf_flags |= LCTF_DIRTY;
2176         return (0);
2177 }
2178 
2179 int
2180 ctf_set_root(ctf_file_t *fp, ctf_id_t id, const boolean_t vis)
2181 {
2182         ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, id);
2183         uint_t kind, vlen;
2184 
2185         if (!(fp->ctf_flags & LCTF_RDWR))
2186                 return (ctf_set_errno(fp, ECTF_RDONLY));
2187 
2188         if (dtd == NULL)
2189                 return (ctf_set_errno(fp, ECTF_BADID));
2190 
2191         kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
2192         vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);
2193 
2194         dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, vis, vlen);
2195         return (0);
2196 }