1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/mman.h> 32*7c478bd9Sstevel@tonic-gate #include <ctf_impl.h> 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* 35*7c478bd9Sstevel@tonic-gate * This static string is used as the template for initially populating a 36*7c478bd9Sstevel@tonic-gate * dynamic container's string table. We always store \0 in the first byte, 37*7c478bd9Sstevel@tonic-gate * and we use the generic string "PARENT" to mark this container's parent 38*7c478bd9Sstevel@tonic-gate * if one is associated with the container using ctf_import(). 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT"; 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate /* 43*7c478bd9Sstevel@tonic-gate * To create an empty CTF container, we just declare a zeroed header and call 44*7c478bd9Sstevel@tonic-gate * ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w 45*7c478bd9Sstevel@tonic-gate * and initialize the dynamic members. We set dtstrlen to 1 to reserve the 46*7c478bd9Sstevel@tonic-gate * first byte of the string table for a \0 byte, and we start assigning type 47*7c478bd9Sstevel@tonic-gate * IDs at 1 because type ID 0 is used as a sentinel. 48*7c478bd9Sstevel@tonic-gate */ 49*7c478bd9Sstevel@tonic-gate ctf_file_t * 50*7c478bd9Sstevel@tonic-gate ctf_create(int *errp) 51*7c478bd9Sstevel@tonic-gate { 52*7c478bd9Sstevel@tonic-gate static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } }; 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate ctf_sect_t cts; 55*7c478bd9Sstevel@tonic-gate ctf_file_t *fp; 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate cts.cts_name = _CTF_SECTION; 58*7c478bd9Sstevel@tonic-gate cts.cts_type = SHT_PROGBITS; 59*7c478bd9Sstevel@tonic-gate cts.cts_flags = 0; 60*7c478bd9Sstevel@tonic-gate cts.cts_data = &hdr; 61*7c478bd9Sstevel@tonic-gate cts.cts_size = sizeof (hdr); 62*7c478bd9Sstevel@tonic-gate cts.cts_entsize = 1; 63*7c478bd9Sstevel@tonic-gate cts.cts_offset = 0; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) != NULL) { 66*7c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_RDWR; 67*7c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE); 68*7c478bd9Sstevel@tonic-gate fp->ctf_dtnextid = 1; 69*7c478bd9Sstevel@tonic-gate fp->ctf_dtoldid = 0; 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate return (fp); 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate static uchar_t * 76*7c478bd9Sstevel@tonic-gate ctf_copy_smembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 77*7c478bd9Sstevel@tonic-gate { 78*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 79*7c478bd9Sstevel@tonic-gate ctf_member_t ctm; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 82*7c478bd9Sstevel@tonic-gate if (dmd->dmd_name) { 83*7c478bd9Sstevel@tonic-gate ctm.ctm_name = soff; 84*7c478bd9Sstevel@tonic-gate soff += strlen(dmd->dmd_name) + 1; 85*7c478bd9Sstevel@tonic-gate } else 86*7c478bd9Sstevel@tonic-gate ctm.ctm_name = 0; 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate ctm.ctm_type = (ushort_t)dmd->dmd_type; 89*7c478bd9Sstevel@tonic-gate ctm.ctm_offset = (ushort_t)dmd->dmd_offset; 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate bcopy(&ctm, t, sizeof (ctm)); 92*7c478bd9Sstevel@tonic-gate t += sizeof (ctm); 93*7c478bd9Sstevel@tonic-gate } 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate return (t); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate static uchar_t * 99*7c478bd9Sstevel@tonic-gate ctf_copy_lmembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 100*7c478bd9Sstevel@tonic-gate { 101*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 102*7c478bd9Sstevel@tonic-gate ctf_lmember_t ctlm; 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 105*7c478bd9Sstevel@tonic-gate if (dmd->dmd_name) { 106*7c478bd9Sstevel@tonic-gate ctlm.ctlm_name = soff; 107*7c478bd9Sstevel@tonic-gate soff += strlen(dmd->dmd_name) + 1; 108*7c478bd9Sstevel@tonic-gate } else 109*7c478bd9Sstevel@tonic-gate ctlm.ctlm_name = 0; 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate ctlm.ctlm_type = (ushort_t)dmd->dmd_type; 112*7c478bd9Sstevel@tonic-gate ctlm.ctlm_pad = 0; 113*7c478bd9Sstevel@tonic-gate ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset); 114*7c478bd9Sstevel@tonic-gate ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset); 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate bcopy(&ctlm, t, sizeof (ctlm)); 117*7c478bd9Sstevel@tonic-gate t += sizeof (ctlm); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate return (t); 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate static uchar_t * 124*7c478bd9Sstevel@tonic-gate ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t) 125*7c478bd9Sstevel@tonic-gate { 126*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 127*7c478bd9Sstevel@tonic-gate ctf_enum_t cte; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 130*7c478bd9Sstevel@tonic-gate cte.cte_name = soff; 131*7c478bd9Sstevel@tonic-gate cte.cte_value = dmd->dmd_value; 132*7c478bd9Sstevel@tonic-gate soff += strlen(dmd->dmd_name) + 1; 133*7c478bd9Sstevel@tonic-gate bcopy(&cte, t, sizeof (cte)); 134*7c478bd9Sstevel@tonic-gate t += sizeof (cte); 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate return (t); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate static uchar_t * 141*7c478bd9Sstevel@tonic-gate ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s) 142*7c478bd9Sstevel@tonic-gate { 143*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 144*7c478bd9Sstevel@tonic-gate size_t len; 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate for (; dmd != NULL; dmd = ctf_list_next(dmd)) { 147*7c478bd9Sstevel@tonic-gate if (dmd->dmd_name == NULL) 148*7c478bd9Sstevel@tonic-gate continue; /* skip anonymous members */ 149*7c478bd9Sstevel@tonic-gate len = strlen(dmd->dmd_name) + 1; 150*7c478bd9Sstevel@tonic-gate bcopy(dmd->dmd_name, s, len); 151*7c478bd9Sstevel@tonic-gate s += len; 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate return (s); 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate /* 158*7c478bd9Sstevel@tonic-gate * If the specified CTF container is writable and has been modified, reload 159*7c478bd9Sstevel@tonic-gate * this container with the updated type definitions. In order to make this 160*7c478bd9Sstevel@tonic-gate * code and the rest of libctf as simple as possible, we perform updates by 161*7c478bd9Sstevel@tonic-gate * taking the dynamic type definitions and creating an in-memory CTF file 162*7c478bd9Sstevel@tonic-gate * containing the definitions, and then call ctf_bufopen() on it. This not 163*7c478bd9Sstevel@tonic-gate * only leverages ctf_bufopen(), but also avoids having to bifurcate the rest 164*7c478bd9Sstevel@tonic-gate * of the library code with different lookup paths for static and dynamic 165*7c478bd9Sstevel@tonic-gate * type definitions. We are therefore optimizing greatly for lookup over 166*7c478bd9Sstevel@tonic-gate * update, which we assume will be an uncommon operation. We perform one 167*7c478bd9Sstevel@tonic-gate * extra trick here for the benefit of callers and to keep our code simple: 168*7c478bd9Sstevel@tonic-gate * ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp 169*7c478bd9Sstevel@tonic-gate * constant for the caller, so after ctf_bufopen() returns, we use bcopy to 170*7c478bd9Sstevel@tonic-gate * swap the interior of the old and new ctf_file_t's, and then free the old. 171*7c478bd9Sstevel@tonic-gate */ 172*7c478bd9Sstevel@tonic-gate int 173*7c478bd9Sstevel@tonic-gate ctf_update(ctf_file_t *fp) 174*7c478bd9Sstevel@tonic-gate { 175*7c478bd9Sstevel@tonic-gate ctf_file_t ofp, *nfp; 176*7c478bd9Sstevel@tonic-gate ctf_header_t hdr; 177*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 178*7c478bd9Sstevel@tonic-gate ctf_sect_t cts; 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate uchar_t *s, *s0, *t; 181*7c478bd9Sstevel@tonic-gate size_t size; 182*7c478bd9Sstevel@tonic-gate void *buf; 183*7c478bd9Sstevel@tonic-gate int err; 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 186*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_DIRTY)) 189*7c478bd9Sstevel@tonic-gate return (0); /* no update required */ 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate /* 192*7c478bd9Sstevel@tonic-gate * Fill in an initial CTF header. We will leave the label, object, 193*7c478bd9Sstevel@tonic-gate * and function sections empty and only output a header, type section, 194*7c478bd9Sstevel@tonic-gate * and string table. The type section begins at a 4-byte aligned 195*7c478bd9Sstevel@tonic-gate * boundary past the CTF header itself (at relative offset zero). 196*7c478bd9Sstevel@tonic-gate */ 197*7c478bd9Sstevel@tonic-gate bzero(&hdr, sizeof (hdr)); 198*7c478bd9Sstevel@tonic-gate hdr.cth_magic = CTF_MAGIC; 199*7c478bd9Sstevel@tonic-gate hdr.cth_version = CTF_VERSION; 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate if (fp->ctf_flags & LCTF_CHILD) 202*7c478bd9Sstevel@tonic-gate hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */ 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate /* 205*7c478bd9Sstevel@tonic-gate * Iterate through the dynamic type definition list and compute the 206*7c478bd9Sstevel@tonic-gate * size of the CTF type section we will need to generate. 207*7c478bd9Sstevel@tonic-gate */ 208*7c478bd9Sstevel@tonic-gate for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs); 209*7c478bd9Sstevel@tonic-gate dtd != NULL; dtd = ctf_list_next(dtd)) { 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 212*7c478bd9Sstevel@tonic-gate uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) 215*7c478bd9Sstevel@tonic-gate size += sizeof (ctf_stype_t); 216*7c478bd9Sstevel@tonic-gate else 217*7c478bd9Sstevel@tonic-gate size += sizeof (ctf_type_t); 218*7c478bd9Sstevel@tonic-gate 219*7c478bd9Sstevel@tonic-gate switch (kind) { 220*7c478bd9Sstevel@tonic-gate case CTF_K_INTEGER: 221*7c478bd9Sstevel@tonic-gate case CTF_K_FLOAT: 222*7c478bd9Sstevel@tonic-gate size += sizeof (uint_t); 223*7c478bd9Sstevel@tonic-gate break; 224*7c478bd9Sstevel@tonic-gate case CTF_K_ARRAY: 225*7c478bd9Sstevel@tonic-gate size += sizeof (ctf_array_t); 226*7c478bd9Sstevel@tonic-gate break; 227*7c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: 228*7c478bd9Sstevel@tonic-gate size += sizeof (ushort_t) * (vlen + (vlen & 1)); 229*7c478bd9Sstevel@tonic-gate break; 230*7c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 231*7c478bd9Sstevel@tonic-gate case CTF_K_UNION: 232*7c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) 233*7c478bd9Sstevel@tonic-gate size += sizeof (ctf_member_t) * vlen; 234*7c478bd9Sstevel@tonic-gate else 235*7c478bd9Sstevel@tonic-gate size += sizeof (ctf_lmember_t) * vlen; 236*7c478bd9Sstevel@tonic-gate break; 237*7c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 238*7c478bd9Sstevel@tonic-gate size += sizeof (ctf_enum_t) * vlen; 239*7c478bd9Sstevel@tonic-gate break; 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate /* 244*7c478bd9Sstevel@tonic-gate * Fill in the string table offset and size, compute the size of the 245*7c478bd9Sstevel@tonic-gate * entire CTF buffer we need, and then allocate a new buffer and 246*7c478bd9Sstevel@tonic-gate * bcopy the finished header to the start of the buffer. 247*7c478bd9Sstevel@tonic-gate */ 248*7c478bd9Sstevel@tonic-gate hdr.cth_stroff = hdr.cth_typeoff + size; 249*7c478bd9Sstevel@tonic-gate hdr.cth_strlen = fp->ctf_dtstrlen; 250*7c478bd9Sstevel@tonic-gate size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen; 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate if ((buf = ctf_data_alloc(size)) == MAP_FAILED) 253*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate bcopy(&hdr, buf, sizeof (ctf_header_t)); 256*7c478bd9Sstevel@tonic-gate t = (uchar_t *)buf + sizeof (ctf_header_t); 257*7c478bd9Sstevel@tonic-gate s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff; 258*7c478bd9Sstevel@tonic-gate 259*7c478bd9Sstevel@tonic-gate bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE)); 260*7c478bd9Sstevel@tonic-gate s += sizeof (_CTF_STRTAB_TEMPLATE); 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate /* 263*7c478bd9Sstevel@tonic-gate * We now take a final lap through the dynamic type definition list and 264*7c478bd9Sstevel@tonic-gate * copy the appropriate type records and strings to the output buffer. 265*7c478bd9Sstevel@tonic-gate */ 266*7c478bd9Sstevel@tonic-gate for (dtd = ctf_list_next(&fp->ctf_dtdefs); 267*7c478bd9Sstevel@tonic-gate dtd != NULL; dtd = ctf_list_next(dtd)) { 268*7c478bd9Sstevel@tonic-gate 269*7c478bd9Sstevel@tonic-gate uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 270*7c478bd9Sstevel@tonic-gate uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate ctf_array_t cta; 273*7c478bd9Sstevel@tonic-gate uint_t encoding; 274*7c478bd9Sstevel@tonic-gate size_t len; 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate if (dtd->dtd_name != NULL) { 277*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_name = (uint_t)(s - s0); 278*7c478bd9Sstevel@tonic-gate len = strlen(dtd->dtd_name) + 1; 279*7c478bd9Sstevel@tonic-gate bcopy(dtd->dtd_name, s, len); 280*7c478bd9Sstevel@tonic-gate s += len; 281*7c478bd9Sstevel@tonic-gate } else 282*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_name = 0; 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT) 285*7c478bd9Sstevel@tonic-gate len = sizeof (ctf_stype_t); 286*7c478bd9Sstevel@tonic-gate else 287*7c478bd9Sstevel@tonic-gate len = sizeof (ctf_type_t); 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate bcopy(&dtd->dtd_data, t, len); 290*7c478bd9Sstevel@tonic-gate t += len; 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate switch (kind) { 293*7c478bd9Sstevel@tonic-gate case CTF_K_INTEGER: 294*7c478bd9Sstevel@tonic-gate case CTF_K_FLOAT: 295*7c478bd9Sstevel@tonic-gate if (kind == CTF_K_INTEGER) { 296*7c478bd9Sstevel@tonic-gate encoding = CTF_INT_DATA( 297*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_format, 298*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_offset, 299*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_bits); 300*7c478bd9Sstevel@tonic-gate } else { 301*7c478bd9Sstevel@tonic-gate encoding = CTF_FP_DATA( 302*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_format, 303*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_offset, 304*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc.cte_bits); 305*7c478bd9Sstevel@tonic-gate } 306*7c478bd9Sstevel@tonic-gate bcopy(&encoding, t, sizeof (encoding)); 307*7c478bd9Sstevel@tonic-gate t += sizeof (encoding); 308*7c478bd9Sstevel@tonic-gate break; 309*7c478bd9Sstevel@tonic-gate 310*7c478bd9Sstevel@tonic-gate case CTF_K_ARRAY: 311*7c478bd9Sstevel@tonic-gate cta.cta_contents = (ushort_t) 312*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr.ctr_contents; 313*7c478bd9Sstevel@tonic-gate cta.cta_index = (ushort_t) 314*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr.ctr_index; 315*7c478bd9Sstevel@tonic-gate cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems; 316*7c478bd9Sstevel@tonic-gate bcopy(&cta, t, sizeof (cta)); 317*7c478bd9Sstevel@tonic-gate t += sizeof (cta); 318*7c478bd9Sstevel@tonic-gate break; 319*7c478bd9Sstevel@tonic-gate 320*7c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: { 321*7c478bd9Sstevel@tonic-gate ushort_t *argv = (ushort_t *)(uintptr_t)t; 322*7c478bd9Sstevel@tonic-gate uint_t argc; 323*7c478bd9Sstevel@tonic-gate 324*7c478bd9Sstevel@tonic-gate for (argc = 0; argc < vlen; argc++) 325*7c478bd9Sstevel@tonic-gate *argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc]; 326*7c478bd9Sstevel@tonic-gate 327*7c478bd9Sstevel@tonic-gate if (vlen & 1) 328*7c478bd9Sstevel@tonic-gate *argv++ = 0; /* pad to 4-byte boundary */ 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate t = (uchar_t *)argv; 331*7c478bd9Sstevel@tonic-gate break; 332*7c478bd9Sstevel@tonic-gate } 333*7c478bd9Sstevel@tonic-gate 334*7c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 335*7c478bd9Sstevel@tonic-gate case CTF_K_UNION: 336*7c478bd9Sstevel@tonic-gate if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH) 337*7c478bd9Sstevel@tonic-gate t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t); 338*7c478bd9Sstevel@tonic-gate else 339*7c478bd9Sstevel@tonic-gate t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t); 340*7c478bd9Sstevel@tonic-gate s = ctf_copy_membnames(dtd, s); 341*7c478bd9Sstevel@tonic-gate break; 342*7c478bd9Sstevel@tonic-gate 343*7c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 344*7c478bd9Sstevel@tonic-gate t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t); 345*7c478bd9Sstevel@tonic-gate s = ctf_copy_membnames(dtd, s); 346*7c478bd9Sstevel@tonic-gate break; 347*7c478bd9Sstevel@tonic-gate } 348*7c478bd9Sstevel@tonic-gate } 349*7c478bd9Sstevel@tonic-gate 350*7c478bd9Sstevel@tonic-gate /* 351*7c478bd9Sstevel@tonic-gate * Finally, we are ready to ctf_bufopen() the new container. If this 352*7c478bd9Sstevel@tonic-gate * is successful, we then switch nfp and fp and free the old container. 353*7c478bd9Sstevel@tonic-gate */ 354*7c478bd9Sstevel@tonic-gate ctf_data_protect(buf, size); 355*7c478bd9Sstevel@tonic-gate cts.cts_name = _CTF_SECTION; 356*7c478bd9Sstevel@tonic-gate cts.cts_type = SHT_PROGBITS; 357*7c478bd9Sstevel@tonic-gate cts.cts_flags = 0; 358*7c478bd9Sstevel@tonic-gate cts.cts_data = buf; 359*7c478bd9Sstevel@tonic-gate cts.cts_size = size; 360*7c478bd9Sstevel@tonic-gate cts.cts_entsize = 1; 361*7c478bd9Sstevel@tonic-gate cts.cts_offset = 0; 362*7c478bd9Sstevel@tonic-gate 363*7c478bd9Sstevel@tonic-gate if ((nfp = ctf_bufopen(&cts, NULL, NULL, &err)) == NULL) { 364*7c478bd9Sstevel@tonic-gate ctf_data_free(buf, size); 365*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, err)); 366*7c478bd9Sstevel@tonic-gate } 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate (void) ctf_setmodel(nfp, ctf_getmodel(fp)); 369*7c478bd9Sstevel@tonic-gate (void) ctf_import(nfp, fp->ctf_parent); 370*7c478bd9Sstevel@tonic-gate 371*7c478bd9Sstevel@tonic-gate nfp->ctf_refcnt = fp->ctf_refcnt; 372*7c478bd9Sstevel@tonic-gate nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY; 373*7c478bd9Sstevel@tonic-gate nfp->ctf_data.cts_data = NULL; /* force ctf_data_free() on close */ 374*7c478bd9Sstevel@tonic-gate nfp->ctf_dtdefs = fp->ctf_dtdefs; 375*7c478bd9Sstevel@tonic-gate nfp->ctf_dtstrlen = fp->ctf_dtstrlen; 376*7c478bd9Sstevel@tonic-gate nfp->ctf_dtnextid = fp->ctf_dtnextid; 377*7c478bd9Sstevel@tonic-gate nfp->ctf_dtoldid = fp->ctf_dtnextid - 1; 378*7c478bd9Sstevel@tonic-gate nfp->ctf_specific = fp->ctf_specific; 379*7c478bd9Sstevel@tonic-gate 380*7c478bd9Sstevel@tonic-gate bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t)); 381*7c478bd9Sstevel@tonic-gate bcopy(fp, &ofp, sizeof (ctf_file_t)); 382*7c478bd9Sstevel@tonic-gate bcopy(nfp, fp, sizeof (ctf_file_t)); 383*7c478bd9Sstevel@tonic-gate bcopy(&ofp, nfp, sizeof (ctf_file_t)); 384*7c478bd9Sstevel@tonic-gate 385*7c478bd9Sstevel@tonic-gate /* 386*7c478bd9Sstevel@tonic-gate * Initialize the ctf_lookup_by_name top-level dictionary. We keep an 387*7c478bd9Sstevel@tonic-gate * array of type name prefixes and the corresponding ctf_hash to use. 388*7c478bd9Sstevel@tonic-gate * NOTE: This code must be kept in sync with the code in ctf_bufopen(). 389*7c478bd9Sstevel@tonic-gate */ 390*7c478bd9Sstevel@tonic-gate fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs; 391*7c478bd9Sstevel@tonic-gate fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions; 392*7c478bd9Sstevel@tonic-gate fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums; 393*7c478bd9Sstevel@tonic-gate fp->ctf_lookups[3].ctl_hash = &fp->ctf_names; 394*7c478bd9Sstevel@tonic-gate 395*7c478bd9Sstevel@tonic-gate nfp->ctf_refcnt = 1; /* force nfp to be freed */ 396*7c478bd9Sstevel@tonic-gate ctf_close(nfp); 397*7c478bd9Sstevel@tonic-gate 398*7c478bd9Sstevel@tonic-gate return (0); 399*7c478bd9Sstevel@tonic-gate } 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate /* 402*7c478bd9Sstevel@tonic-gate * Discard all of the dynamic type definitions that have been added to the 403*7c478bd9Sstevel@tonic-gate * container since the last call to ctf_update(). We locate such types by 404*7c478bd9Sstevel@tonic-gate * scanning the list and deleting elements that have type IDs greater than 405*7c478bd9Sstevel@tonic-gate * ctf_dtoldid, which is set by ctf_update(), above. 406*7c478bd9Sstevel@tonic-gate */ 407*7c478bd9Sstevel@tonic-gate int 408*7c478bd9Sstevel@tonic-gate ctf_discard(ctf_file_t *fp) 409*7c478bd9Sstevel@tonic-gate { 410*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd, *ntd; 411*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd, *nmd; 412*7c478bd9Sstevel@tonic-gate size_t len; 413*7c478bd9Sstevel@tonic-gate 414*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 415*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 416*7c478bd9Sstevel@tonic-gate 417*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_DIRTY)) 418*7c478bd9Sstevel@tonic-gate return (0); /* no update required */ 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate for (dtd = ctf_list_next(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) { 421*7c478bd9Sstevel@tonic-gate if (dtd->dtd_type <= fp->ctf_dtoldid) 422*7c478bd9Sstevel@tonic-gate continue; /* skip types that have been committed */ 423*7c478bd9Sstevel@tonic-gate 424*7c478bd9Sstevel@tonic-gate switch (CTF_INFO_KIND(dtd->dtd_data.ctt_info)) { 425*7c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 426*7c478bd9Sstevel@tonic-gate case CTF_K_UNION: 427*7c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 428*7c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 429*7c478bd9Sstevel@tonic-gate dmd != NULL; dmd = nmd) { 430*7c478bd9Sstevel@tonic-gate if (dmd->dmd_name != NULL) { 431*7c478bd9Sstevel@tonic-gate len = strlen(dmd->dmd_name) + 1; 432*7c478bd9Sstevel@tonic-gate ctf_free(dmd->dmd_name, len); 433*7c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen -= len; 434*7c478bd9Sstevel@tonic-gate } 435*7c478bd9Sstevel@tonic-gate nmd = ctf_list_next(dmd); 436*7c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 437*7c478bd9Sstevel@tonic-gate } 438*7c478bd9Sstevel@tonic-gate break; 439*7c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: 440*7c478bd9Sstevel@tonic-gate ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) * 441*7c478bd9Sstevel@tonic-gate CTF_INFO_VLEN(dtd->dtd_data.ctt_info)); 442*7c478bd9Sstevel@tonic-gate break; 443*7c478bd9Sstevel@tonic-gate } 444*7c478bd9Sstevel@tonic-gate 445*7c478bd9Sstevel@tonic-gate if (dtd->dtd_name) { 446*7c478bd9Sstevel@tonic-gate len = strlen(dtd->dtd_name) + 1; 447*7c478bd9Sstevel@tonic-gate ctf_free(dtd->dtd_name, len); 448*7c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen -= len; 449*7c478bd9Sstevel@tonic-gate } 450*7c478bd9Sstevel@tonic-gate 451*7c478bd9Sstevel@tonic-gate ntd = ctf_list_next(dtd); 452*7c478bd9Sstevel@tonic-gate ctf_list_delete(&fp->ctf_dtdefs, dtd); 453*7c478bd9Sstevel@tonic-gate ctf_free(dtd, sizeof (ctf_dtdef_t)); 454*7c478bd9Sstevel@tonic-gate } 455*7c478bd9Sstevel@tonic-gate 456*7c478bd9Sstevel@tonic-gate fp->ctf_dtnextid = fp->ctf_dtoldid + 1; 457*7c478bd9Sstevel@tonic-gate fp->ctf_flags &= ~LCTF_DIRTY; 458*7c478bd9Sstevel@tonic-gate 459*7c478bd9Sstevel@tonic-gate return (0); 460*7c478bd9Sstevel@tonic-gate } 461*7c478bd9Sstevel@tonic-gate 462*7c478bd9Sstevel@tonic-gate static ctf_id_t 463*7c478bd9Sstevel@tonic-gate ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp) 464*7c478bd9Sstevel@tonic-gate { 465*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 466*7c478bd9Sstevel@tonic-gate ctf_id_t type; 467*7c478bd9Sstevel@tonic-gate char *s = NULL; 468*7c478bd9Sstevel@tonic-gate 469*7c478bd9Sstevel@tonic-gate if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT) 470*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 471*7c478bd9Sstevel@tonic-gate 472*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 473*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 474*7c478bd9Sstevel@tonic-gate 475*7c478bd9Sstevel@tonic-gate if (CTF_INDEX_TO_TYPE(fp->ctf_dtnextid, 1) > CTF_MAX_TYPE) 476*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_FULL)); 477*7c478bd9Sstevel@tonic-gate 478*7c478bd9Sstevel@tonic-gate if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL) 479*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 480*7c478bd9Sstevel@tonic-gate 481*7c478bd9Sstevel@tonic-gate if (name != NULL && (s = ctf_strdup(name)) == NULL) { 482*7c478bd9Sstevel@tonic-gate ctf_free(dtd, sizeof (ctf_dtdef_t)); 483*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 484*7c478bd9Sstevel@tonic-gate } 485*7c478bd9Sstevel@tonic-gate 486*7c478bd9Sstevel@tonic-gate type = fp->ctf_dtnextid++; 487*7c478bd9Sstevel@tonic-gate type = CTF_INDEX_TO_TYPE(type, (fp->ctf_flags & LCTF_CHILD)); 488*7c478bd9Sstevel@tonic-gate 489*7c478bd9Sstevel@tonic-gate bzero(dtd, sizeof (ctf_dtdef_t)); 490*7c478bd9Sstevel@tonic-gate dtd->dtd_name = s; 491*7c478bd9Sstevel@tonic-gate dtd->dtd_type = type; 492*7c478bd9Sstevel@tonic-gate 493*7c478bd9Sstevel@tonic-gate if (s != NULL) 494*7c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen += strlen(s) + 1; 495*7c478bd9Sstevel@tonic-gate 496*7c478bd9Sstevel@tonic-gate ctf_list_append(&fp->ctf_dtdefs, dtd); 497*7c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 498*7c478bd9Sstevel@tonic-gate 499*7c478bd9Sstevel@tonic-gate *rp = dtd; 500*7c478bd9Sstevel@tonic-gate return (type); 501*7c478bd9Sstevel@tonic-gate } 502*7c478bd9Sstevel@tonic-gate 503*7c478bd9Sstevel@tonic-gate /* 504*7c478bd9Sstevel@tonic-gate * When encoding integer sizes, we want to convert a byte count in the range 505*7c478bd9Sstevel@tonic-gate * 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function 506*7c478bd9Sstevel@tonic-gate * is a clever implementation from "Hacker's Delight" by Henry Warren, Jr. 507*7c478bd9Sstevel@tonic-gate */ 508*7c478bd9Sstevel@tonic-gate static size_t 509*7c478bd9Sstevel@tonic-gate clp2(size_t x) 510*7c478bd9Sstevel@tonic-gate { 511*7c478bd9Sstevel@tonic-gate x--; 512*7c478bd9Sstevel@tonic-gate 513*7c478bd9Sstevel@tonic-gate x |= (x >> 1); 514*7c478bd9Sstevel@tonic-gate x |= (x >> 2); 515*7c478bd9Sstevel@tonic-gate x |= (x >> 4); 516*7c478bd9Sstevel@tonic-gate x |= (x >> 8); 517*7c478bd9Sstevel@tonic-gate x |= (x >> 16); 518*7c478bd9Sstevel@tonic-gate 519*7c478bd9Sstevel@tonic-gate return (x + 1); 520*7c478bd9Sstevel@tonic-gate } 521*7c478bd9Sstevel@tonic-gate 522*7c478bd9Sstevel@tonic-gate static ctf_id_t 523*7c478bd9Sstevel@tonic-gate ctf_add_encoded(ctf_file_t *fp, uint_t flag, 524*7c478bd9Sstevel@tonic-gate const char *name, const ctf_encoding_t *ep, uint_t kind) 525*7c478bd9Sstevel@tonic-gate { 526*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 527*7c478bd9Sstevel@tonic-gate ctf_id_t type; 528*7c478bd9Sstevel@tonic-gate 529*7c478bd9Sstevel@tonic-gate if (ep == NULL) 530*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 533*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 534*7c478bd9Sstevel@tonic-gate 535*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 536*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = clp2(P2ROUNDUP(ep->cte_bits, NBBY) / NBBY); 537*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_enc = *ep; 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate return (type); 540*7c478bd9Sstevel@tonic-gate } 541*7c478bd9Sstevel@tonic-gate 542*7c478bd9Sstevel@tonic-gate static ctf_id_t 543*7c478bd9Sstevel@tonic-gate ctf_add_reftype(ctf_file_t *fp, uint_t flag, ctf_id_t ref, uint_t kind) 544*7c478bd9Sstevel@tonic-gate { 545*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 546*7c478bd9Sstevel@tonic-gate ctf_id_t type; 547*7c478bd9Sstevel@tonic-gate 548*7c478bd9Sstevel@tonic-gate if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE) 549*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 550*7c478bd9Sstevel@tonic-gate 551*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) 552*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 553*7c478bd9Sstevel@tonic-gate 554*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, 0); 555*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_type = (ushort_t)ref; 556*7c478bd9Sstevel@tonic-gate 557*7c478bd9Sstevel@tonic-gate return (type); 558*7c478bd9Sstevel@tonic-gate } 559*7c478bd9Sstevel@tonic-gate 560*7c478bd9Sstevel@tonic-gate ctf_id_t 561*7c478bd9Sstevel@tonic-gate ctf_add_integer(ctf_file_t *fp, uint_t flag, 562*7c478bd9Sstevel@tonic-gate const char *name, const ctf_encoding_t *ep) 563*7c478bd9Sstevel@tonic-gate { 564*7c478bd9Sstevel@tonic-gate return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER)); 565*7c478bd9Sstevel@tonic-gate } 566*7c478bd9Sstevel@tonic-gate 567*7c478bd9Sstevel@tonic-gate ctf_id_t 568*7c478bd9Sstevel@tonic-gate ctf_add_float(ctf_file_t *fp, uint_t flag, 569*7c478bd9Sstevel@tonic-gate const char *name, const ctf_encoding_t *ep) 570*7c478bd9Sstevel@tonic-gate { 571*7c478bd9Sstevel@tonic-gate return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT)); 572*7c478bd9Sstevel@tonic-gate } 573*7c478bd9Sstevel@tonic-gate 574*7c478bd9Sstevel@tonic-gate ctf_id_t 575*7c478bd9Sstevel@tonic-gate ctf_add_pointer(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 576*7c478bd9Sstevel@tonic-gate { 577*7c478bd9Sstevel@tonic-gate return (ctf_add_reftype(fp, flag, ref, CTF_K_POINTER)); 578*7c478bd9Sstevel@tonic-gate } 579*7c478bd9Sstevel@tonic-gate 580*7c478bd9Sstevel@tonic-gate ctf_id_t 581*7c478bd9Sstevel@tonic-gate ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp) 582*7c478bd9Sstevel@tonic-gate { 583*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 584*7c478bd9Sstevel@tonic-gate ctf_id_t type; 585*7c478bd9Sstevel@tonic-gate 586*7c478bd9Sstevel@tonic-gate if (arp == NULL) 587*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 588*7c478bd9Sstevel@tonic-gate 589*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) 590*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 591*7c478bd9Sstevel@tonic-gate 592*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ARRAY, flag, 0); 593*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = 0; 594*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr = *arp; 595*7c478bd9Sstevel@tonic-gate 596*7c478bd9Sstevel@tonic-gate return (type); 597*7c478bd9Sstevel@tonic-gate } 598*7c478bd9Sstevel@tonic-gate 599*7c478bd9Sstevel@tonic-gate int 600*7c478bd9Sstevel@tonic-gate ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) 601*7c478bd9Sstevel@tonic-gate { 602*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 603*7c478bd9Sstevel@tonic-gate 604*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 605*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 606*7c478bd9Sstevel@tonic-gate 607*7c478bd9Sstevel@tonic-gate for (dtd = ctf_list_next(&fp->ctf_dtdefs); 608*7c478bd9Sstevel@tonic-gate dtd != NULL; dtd = ctf_list_next(dtd)) { 609*7c478bd9Sstevel@tonic-gate if (dtd->dtd_type == type) 610*7c478bd9Sstevel@tonic-gate break; 611*7c478bd9Sstevel@tonic-gate } 612*7c478bd9Sstevel@tonic-gate 613*7c478bd9Sstevel@tonic-gate if (dtd == NULL || CTF_INFO_KIND(dtd->dtd_data.ctt_info) != CTF_K_ARRAY) 614*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_BADID)); 615*7c478bd9Sstevel@tonic-gate 616*7c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 617*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_arr = *arp; 618*7c478bd9Sstevel@tonic-gate 619*7c478bd9Sstevel@tonic-gate return (0); 620*7c478bd9Sstevel@tonic-gate } 621*7c478bd9Sstevel@tonic-gate 622*7c478bd9Sstevel@tonic-gate ctf_id_t 623*7c478bd9Sstevel@tonic-gate ctf_add_function(ctf_file_t *fp, uint_t flag, 624*7c478bd9Sstevel@tonic-gate const ctf_funcinfo_t *ctc, const ctf_id_t *argv) 625*7c478bd9Sstevel@tonic-gate { 626*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 627*7c478bd9Sstevel@tonic-gate ctf_id_t type; 628*7c478bd9Sstevel@tonic-gate uint_t vlen; 629*7c478bd9Sstevel@tonic-gate ctf_id_t *vdat = NULL; 630*7c478bd9Sstevel@tonic-gate 631*7c478bd9Sstevel@tonic-gate if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 || 632*7c478bd9Sstevel@tonic-gate (ctc->ctc_argc != 0 && argv == NULL)) 633*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 634*7c478bd9Sstevel@tonic-gate 635*7c478bd9Sstevel@tonic-gate vlen = ctc->ctc_argc; 636*7c478bd9Sstevel@tonic-gate if (ctc->ctc_flags & CTF_FUNC_VARARG) 637*7c478bd9Sstevel@tonic-gate vlen++; /* add trailing zero to indicate varargs (see below) */ 638*7c478bd9Sstevel@tonic-gate 639*7c478bd9Sstevel@tonic-gate if (vlen > CTF_MAX_VLEN) 640*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EOVERFLOW)); 641*7c478bd9Sstevel@tonic-gate 642*7c478bd9Sstevel@tonic-gate if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL) 643*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 644*7c478bd9Sstevel@tonic-gate 645*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) { 646*7c478bd9Sstevel@tonic-gate ctf_free(vdat, sizeof (ctf_id_t) * vlen); 647*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 648*7c478bd9Sstevel@tonic-gate } 649*7c478bd9Sstevel@tonic-gate 650*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FUNCTION, flag, vlen); 651*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_type = (ushort_t)ctc->ctc_return; 652*7c478bd9Sstevel@tonic-gate 653*7c478bd9Sstevel@tonic-gate bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc); 654*7c478bd9Sstevel@tonic-gate if (ctc->ctc_flags & CTF_FUNC_VARARG) 655*7c478bd9Sstevel@tonic-gate vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */ 656*7c478bd9Sstevel@tonic-gate dtd->dtd_u.dtu_argv = vdat; 657*7c478bd9Sstevel@tonic-gate 658*7c478bd9Sstevel@tonic-gate return (type); 659*7c478bd9Sstevel@tonic-gate } 660*7c478bd9Sstevel@tonic-gate 661*7c478bd9Sstevel@tonic-gate ctf_id_t 662*7c478bd9Sstevel@tonic-gate ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name) 663*7c478bd9Sstevel@tonic-gate { 664*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 665*7c478bd9Sstevel@tonic-gate ctf_id_t type; 666*7c478bd9Sstevel@tonic-gate 667*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 668*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 669*7c478bd9Sstevel@tonic-gate 670*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_STRUCT, flag, 0); 671*7c478bd9Sstevel@tonic-gate return (type); 672*7c478bd9Sstevel@tonic-gate } 673*7c478bd9Sstevel@tonic-gate 674*7c478bd9Sstevel@tonic-gate ctf_id_t 675*7c478bd9Sstevel@tonic-gate ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name) 676*7c478bd9Sstevel@tonic-gate { 677*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 678*7c478bd9Sstevel@tonic-gate ctf_id_t type; 679*7c478bd9Sstevel@tonic-gate 680*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 681*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 682*7c478bd9Sstevel@tonic-gate 683*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_UNION, flag, 0); 684*7c478bd9Sstevel@tonic-gate return (type); 685*7c478bd9Sstevel@tonic-gate } 686*7c478bd9Sstevel@tonic-gate 687*7c478bd9Sstevel@tonic-gate ctf_id_t 688*7c478bd9Sstevel@tonic-gate ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name) 689*7c478bd9Sstevel@tonic-gate { 690*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 691*7c478bd9Sstevel@tonic-gate ctf_id_t type; 692*7c478bd9Sstevel@tonic-gate 693*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 694*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 695*7c478bd9Sstevel@tonic-gate 696*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_ENUM, flag, 0); 697*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int; 698*7c478bd9Sstevel@tonic-gate 699*7c478bd9Sstevel@tonic-gate return (type); 700*7c478bd9Sstevel@tonic-gate } 701*7c478bd9Sstevel@tonic-gate 702*7c478bd9Sstevel@tonic-gate ctf_id_t 703*7c478bd9Sstevel@tonic-gate ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind) 704*7c478bd9Sstevel@tonic-gate { 705*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 706*7c478bd9Sstevel@tonic-gate ctf_id_t type; 707*7c478bd9Sstevel@tonic-gate 708*7c478bd9Sstevel@tonic-gate if (kind != CTF_K_STRUCT && kind != CTF_K_UNION && kind != CTF_K_ENUM) 709*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTSUE)); 710*7c478bd9Sstevel@tonic-gate 711*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 712*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 713*7c478bd9Sstevel@tonic-gate 714*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_FORWARD, flag, 0); 715*7c478bd9Sstevel@tonic-gate return (type); 716*7c478bd9Sstevel@tonic-gate } 717*7c478bd9Sstevel@tonic-gate 718*7c478bd9Sstevel@tonic-gate ctf_id_t 719*7c478bd9Sstevel@tonic-gate ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref) 720*7c478bd9Sstevel@tonic-gate { 721*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 722*7c478bd9Sstevel@tonic-gate ctf_id_t type; 723*7c478bd9Sstevel@tonic-gate 724*7c478bd9Sstevel@tonic-gate if (ref == CTF_ERR || ref < 0 || ref > CTF_MAX_TYPE) 725*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 726*7c478bd9Sstevel@tonic-gate 727*7c478bd9Sstevel@tonic-gate if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR) 728*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 729*7c478bd9Sstevel@tonic-gate 730*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(CTF_K_TYPEDEF, flag, 0); 731*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_type = (ushort_t)ref; 732*7c478bd9Sstevel@tonic-gate 733*7c478bd9Sstevel@tonic-gate return (type); 734*7c478bd9Sstevel@tonic-gate } 735*7c478bd9Sstevel@tonic-gate 736*7c478bd9Sstevel@tonic-gate ctf_id_t 737*7c478bd9Sstevel@tonic-gate ctf_add_volatile(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 738*7c478bd9Sstevel@tonic-gate { 739*7c478bd9Sstevel@tonic-gate return (ctf_add_reftype(fp, flag, ref, CTF_K_VOLATILE)); 740*7c478bd9Sstevel@tonic-gate } 741*7c478bd9Sstevel@tonic-gate 742*7c478bd9Sstevel@tonic-gate ctf_id_t 743*7c478bd9Sstevel@tonic-gate ctf_add_const(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 744*7c478bd9Sstevel@tonic-gate { 745*7c478bd9Sstevel@tonic-gate return (ctf_add_reftype(fp, flag, ref, CTF_K_CONST)); 746*7c478bd9Sstevel@tonic-gate } 747*7c478bd9Sstevel@tonic-gate 748*7c478bd9Sstevel@tonic-gate ctf_id_t 749*7c478bd9Sstevel@tonic-gate ctf_add_restrict(ctf_file_t *fp, uint_t flag, ctf_id_t ref) 750*7c478bd9Sstevel@tonic-gate { 751*7c478bd9Sstevel@tonic-gate return (ctf_add_reftype(fp, flag, ref, CTF_K_RESTRICT)); 752*7c478bd9Sstevel@tonic-gate } 753*7c478bd9Sstevel@tonic-gate 754*7c478bd9Sstevel@tonic-gate int 755*7c478bd9Sstevel@tonic-gate ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value) 756*7c478bd9Sstevel@tonic-gate { 757*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 758*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 759*7c478bd9Sstevel@tonic-gate 760*7c478bd9Sstevel@tonic-gate uint_t kind, vlen, root; 761*7c478bd9Sstevel@tonic-gate char *s; 762*7c478bd9Sstevel@tonic-gate 763*7c478bd9Sstevel@tonic-gate if (name == NULL) 764*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 765*7c478bd9Sstevel@tonic-gate 766*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 767*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 768*7c478bd9Sstevel@tonic-gate 769*7c478bd9Sstevel@tonic-gate for (dtd = ctf_list_next(&fp->ctf_dtdefs); 770*7c478bd9Sstevel@tonic-gate dtd != NULL; dtd = ctf_list_next(dtd)) { 771*7c478bd9Sstevel@tonic-gate if (dtd->dtd_type == enid) 772*7c478bd9Sstevel@tonic-gate break; 773*7c478bd9Sstevel@tonic-gate } 774*7c478bd9Sstevel@tonic-gate 775*7c478bd9Sstevel@tonic-gate if (dtd == NULL) 776*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_BADID)); 777*7c478bd9Sstevel@tonic-gate 778*7c478bd9Sstevel@tonic-gate kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 779*7c478bd9Sstevel@tonic-gate root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 780*7c478bd9Sstevel@tonic-gate vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 781*7c478bd9Sstevel@tonic-gate 782*7c478bd9Sstevel@tonic-gate if (kind != CTF_K_ENUM) 783*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTENUM)); 784*7c478bd9Sstevel@tonic-gate 785*7c478bd9Sstevel@tonic-gate if (vlen == CTF_MAX_VLEN) 786*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DTFULL)); 787*7c478bd9Sstevel@tonic-gate 788*7c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 789*7c478bd9Sstevel@tonic-gate dmd != NULL; dmd = ctf_list_next(dmd)) { 790*7c478bd9Sstevel@tonic-gate if (strcmp(dmd->dmd_name, name) == 0) 791*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 792*7c478bd9Sstevel@tonic-gate } 793*7c478bd9Sstevel@tonic-gate 794*7c478bd9Sstevel@tonic-gate if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 795*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 796*7c478bd9Sstevel@tonic-gate 797*7c478bd9Sstevel@tonic-gate if ((s = ctf_strdup(name)) == NULL) { 798*7c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 799*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 800*7c478bd9Sstevel@tonic-gate } 801*7c478bd9Sstevel@tonic-gate 802*7c478bd9Sstevel@tonic-gate dmd->dmd_name = s; 803*7c478bd9Sstevel@tonic-gate dmd->dmd_type = CTF_ERR; 804*7c478bd9Sstevel@tonic-gate dmd->dmd_offset = 0; 805*7c478bd9Sstevel@tonic-gate dmd->dmd_value = value; 806*7c478bd9Sstevel@tonic-gate 807*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 808*7c478bd9Sstevel@tonic-gate ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 809*7c478bd9Sstevel@tonic-gate 810*7c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen += strlen(s) + 1; 811*7c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 812*7c478bd9Sstevel@tonic-gate 813*7c478bd9Sstevel@tonic-gate return (0); 814*7c478bd9Sstevel@tonic-gate } 815*7c478bd9Sstevel@tonic-gate 816*7c478bd9Sstevel@tonic-gate int 817*7c478bd9Sstevel@tonic-gate ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type) 818*7c478bd9Sstevel@tonic-gate { 819*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 820*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 821*7c478bd9Sstevel@tonic-gate 822*7c478bd9Sstevel@tonic-gate ssize_t msize, malign, ssize; 823*7c478bd9Sstevel@tonic-gate uint_t kind, vlen, root; 824*7c478bd9Sstevel@tonic-gate char *s = NULL; 825*7c478bd9Sstevel@tonic-gate 826*7c478bd9Sstevel@tonic-gate if (!(fp->ctf_flags & LCTF_RDWR)) 827*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_RDONLY)); 828*7c478bd9Sstevel@tonic-gate 829*7c478bd9Sstevel@tonic-gate for (dtd = ctf_list_next(&fp->ctf_dtdefs); 830*7c478bd9Sstevel@tonic-gate dtd != NULL; dtd = ctf_list_next(dtd)) { 831*7c478bd9Sstevel@tonic-gate if (dtd->dtd_type == souid) 832*7c478bd9Sstevel@tonic-gate break; 833*7c478bd9Sstevel@tonic-gate } 834*7c478bd9Sstevel@tonic-gate 835*7c478bd9Sstevel@tonic-gate if (dtd == NULL) 836*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_BADID)); 837*7c478bd9Sstevel@tonic-gate 838*7c478bd9Sstevel@tonic-gate kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info); 839*7c478bd9Sstevel@tonic-gate root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info); 840*7c478bd9Sstevel@tonic-gate vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info); 841*7c478bd9Sstevel@tonic-gate 842*7c478bd9Sstevel@tonic-gate if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) 843*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTSOU)); 844*7c478bd9Sstevel@tonic-gate 845*7c478bd9Sstevel@tonic-gate if (vlen == CTF_MAX_VLEN) 846*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DTFULL)); 847*7c478bd9Sstevel@tonic-gate 848*7c478bd9Sstevel@tonic-gate if (name != NULL) { 849*7c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 850*7c478bd9Sstevel@tonic-gate dmd != NULL; dmd = ctf_list_next(dmd)) { 851*7c478bd9Sstevel@tonic-gate if (dmd->dmd_name != NULL && 852*7c478bd9Sstevel@tonic-gate strcmp(dmd->dmd_name, name) == 0) 853*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_DUPMEMBER)); 854*7c478bd9Sstevel@tonic-gate } 855*7c478bd9Sstevel@tonic-gate } 856*7c478bd9Sstevel@tonic-gate 857*7c478bd9Sstevel@tonic-gate if ((msize = ctf_type_size(fp, type)) == CTF_ERR || 858*7c478bd9Sstevel@tonic-gate (malign = ctf_type_align(fp, type)) == CTF_ERR) 859*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 860*7c478bd9Sstevel@tonic-gate 861*7c478bd9Sstevel@tonic-gate if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 862*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 863*7c478bd9Sstevel@tonic-gate 864*7c478bd9Sstevel@tonic-gate if (name != NULL && (s = ctf_strdup(name)) == NULL) { 865*7c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 866*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(fp, EAGAIN)); 867*7c478bd9Sstevel@tonic-gate } 868*7c478bd9Sstevel@tonic-gate 869*7c478bd9Sstevel@tonic-gate dmd->dmd_name = s; 870*7c478bd9Sstevel@tonic-gate dmd->dmd_type = type; 871*7c478bd9Sstevel@tonic-gate dmd->dmd_value = -1; 872*7c478bd9Sstevel@tonic-gate 873*7c478bd9Sstevel@tonic-gate if (kind == CTF_K_STRUCT && vlen != 0) { 874*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members); 875*7c478bd9Sstevel@tonic-gate ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type); 876*7c478bd9Sstevel@tonic-gate size_t off = lmd->dmd_offset; 877*7c478bd9Sstevel@tonic-gate 878*7c478bd9Sstevel@tonic-gate ctf_encoding_t linfo; 879*7c478bd9Sstevel@tonic-gate ssize_t lsize; 880*7c478bd9Sstevel@tonic-gate 881*7c478bd9Sstevel@tonic-gate if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR) 882*7c478bd9Sstevel@tonic-gate off += linfo.cte_bits; 883*7c478bd9Sstevel@tonic-gate else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR) 884*7c478bd9Sstevel@tonic-gate off += lsize * NBBY; 885*7c478bd9Sstevel@tonic-gate 886*7c478bd9Sstevel@tonic-gate /* 887*7c478bd9Sstevel@tonic-gate * Round up the offset of the end of the last member to the 888*7c478bd9Sstevel@tonic-gate * next byte boundary, convert 'off' to bytes, and then round 889*7c478bd9Sstevel@tonic-gate * it up again to the next multiple of the alignment required 890*7c478bd9Sstevel@tonic-gate * by the new member. Finally, convert back to bits and store 891*7c478bd9Sstevel@tonic-gate * the result in dmd_offset. Technically we could do more 892*7c478bd9Sstevel@tonic-gate * efficient packing if the new member is a bit-field, but 893*7c478bd9Sstevel@tonic-gate * we're the "compiler" and ANSI says we can do as we choose. 894*7c478bd9Sstevel@tonic-gate */ 895*7c478bd9Sstevel@tonic-gate off = roundup(off, NBBY) / NBBY; 896*7c478bd9Sstevel@tonic-gate off = roundup(off, MAX(malign, 1)); 897*7c478bd9Sstevel@tonic-gate dmd->dmd_offset = off * NBBY; 898*7c478bd9Sstevel@tonic-gate ssize = off + msize; 899*7c478bd9Sstevel@tonic-gate } else { 900*7c478bd9Sstevel@tonic-gate dmd->dmd_offset = 0; 901*7c478bd9Sstevel@tonic-gate ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL); 902*7c478bd9Sstevel@tonic-gate ssize = MAX(ssize, msize); 903*7c478bd9Sstevel@tonic-gate } 904*7c478bd9Sstevel@tonic-gate 905*7c478bd9Sstevel@tonic-gate if (ssize > CTF_MAX_SIZE) { 906*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 907*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize); 908*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize); 909*7c478bd9Sstevel@tonic-gate } else 910*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = (ushort_t)ssize; 911*7c478bd9Sstevel@tonic-gate 912*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1); 913*7c478bd9Sstevel@tonic-gate ctf_list_append(&dtd->dtd_u.dtu_members, dmd); 914*7c478bd9Sstevel@tonic-gate 915*7c478bd9Sstevel@tonic-gate if (s != NULL) 916*7c478bd9Sstevel@tonic-gate fp->ctf_dtstrlen += strlen(s) + 1; 917*7c478bd9Sstevel@tonic-gate 918*7c478bd9Sstevel@tonic-gate fp->ctf_flags |= LCTF_DIRTY; 919*7c478bd9Sstevel@tonic-gate return (0); 920*7c478bd9Sstevel@tonic-gate } 921*7c478bd9Sstevel@tonic-gate 922*7c478bd9Sstevel@tonic-gate static int 923*7c478bd9Sstevel@tonic-gate enumcmp(const char *name, int value, void *arg) 924*7c478bd9Sstevel@tonic-gate { 925*7c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 926*7c478bd9Sstevel@tonic-gate int bvalue; 927*7c478bd9Sstevel@tonic-gate 928*7c478bd9Sstevel@tonic-gate return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type, 929*7c478bd9Sstevel@tonic-gate name, &bvalue) == CTF_ERR || value != bvalue); 930*7c478bd9Sstevel@tonic-gate } 931*7c478bd9Sstevel@tonic-gate 932*7c478bd9Sstevel@tonic-gate static int 933*7c478bd9Sstevel@tonic-gate enumadd(const char *name, int value, void *arg) 934*7c478bd9Sstevel@tonic-gate { 935*7c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 936*7c478bd9Sstevel@tonic-gate 937*7c478bd9Sstevel@tonic-gate return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type, 938*7c478bd9Sstevel@tonic-gate name, value) == CTF_ERR); 939*7c478bd9Sstevel@tonic-gate } 940*7c478bd9Sstevel@tonic-gate 941*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 942*7c478bd9Sstevel@tonic-gate static int 943*7c478bd9Sstevel@tonic-gate membcmp(const char *name, ctf_id_t type, ulong_t offset, void *arg) 944*7c478bd9Sstevel@tonic-gate { 945*7c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 946*7c478bd9Sstevel@tonic-gate ctf_membinfo_t ctm; 947*7c478bd9Sstevel@tonic-gate 948*7c478bd9Sstevel@tonic-gate return (ctf_member_info(ctb->ctb_file, ctb->ctb_type, 949*7c478bd9Sstevel@tonic-gate name, &ctm) == CTF_ERR || ctm.ctm_offset != offset); 950*7c478bd9Sstevel@tonic-gate } 951*7c478bd9Sstevel@tonic-gate 952*7c478bd9Sstevel@tonic-gate static int 953*7c478bd9Sstevel@tonic-gate membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg) 954*7c478bd9Sstevel@tonic-gate { 955*7c478bd9Sstevel@tonic-gate ctf_bundle_t *ctb = arg; 956*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 957*7c478bd9Sstevel@tonic-gate char *s = NULL; 958*7c478bd9Sstevel@tonic-gate 959*7c478bd9Sstevel@tonic-gate if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL) 960*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 961*7c478bd9Sstevel@tonic-gate 962*7c478bd9Sstevel@tonic-gate if (name != NULL && (s = ctf_strdup(name)) == NULL) { 963*7c478bd9Sstevel@tonic-gate ctf_free(dmd, sizeof (ctf_dmdef_t)); 964*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(ctb->ctb_file, EAGAIN)); 965*7c478bd9Sstevel@tonic-gate } 966*7c478bd9Sstevel@tonic-gate 967*7c478bd9Sstevel@tonic-gate /* 968*7c478bd9Sstevel@tonic-gate * For now, dmd_type is copied as the src_fp's type; it is reset to an 969*7c478bd9Sstevel@tonic-gate * equivalent dst_fp type by a final loop in ctf_add_type(), below. 970*7c478bd9Sstevel@tonic-gate */ 971*7c478bd9Sstevel@tonic-gate dmd->dmd_name = s; 972*7c478bd9Sstevel@tonic-gate dmd->dmd_type = type; 973*7c478bd9Sstevel@tonic-gate dmd->dmd_offset = offset; 974*7c478bd9Sstevel@tonic-gate dmd->dmd_value = -1; 975*7c478bd9Sstevel@tonic-gate 976*7c478bd9Sstevel@tonic-gate ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd); 977*7c478bd9Sstevel@tonic-gate 978*7c478bd9Sstevel@tonic-gate if (s != NULL) 979*7c478bd9Sstevel@tonic-gate ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1; 980*7c478bd9Sstevel@tonic-gate 981*7c478bd9Sstevel@tonic-gate ctb->ctb_file->ctf_flags |= LCTF_DIRTY; 982*7c478bd9Sstevel@tonic-gate return (0); 983*7c478bd9Sstevel@tonic-gate } 984*7c478bd9Sstevel@tonic-gate 985*7c478bd9Sstevel@tonic-gate /* 986*7c478bd9Sstevel@tonic-gate * The ctf_add_type routine is used to copy a type from a source CTF container 987*7c478bd9Sstevel@tonic-gate * to a dynamic destination container. This routine operates recursively by 988*7c478bd9Sstevel@tonic-gate * following the source type's links and embedded member types. If the 989*7c478bd9Sstevel@tonic-gate * destination container already contains a named type which has the same 990*7c478bd9Sstevel@tonic-gate * attributes, then we succeed and return this type but no changes occur. 991*7c478bd9Sstevel@tonic-gate */ 992*7c478bd9Sstevel@tonic-gate ctf_id_t 993*7c478bd9Sstevel@tonic-gate ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type) 994*7c478bd9Sstevel@tonic-gate { 995*7c478bd9Sstevel@tonic-gate ctf_id_t dst_type = CTF_ERR; 996*7c478bd9Sstevel@tonic-gate uint_t dst_kind = CTF_K_UNKNOWN; 997*7c478bd9Sstevel@tonic-gate 998*7c478bd9Sstevel@tonic-gate const ctf_type_t *tp; 999*7c478bd9Sstevel@tonic-gate const char *name; 1000*7c478bd9Sstevel@tonic-gate uint_t kind, flag, vlen; 1001*7c478bd9Sstevel@tonic-gate 1002*7c478bd9Sstevel@tonic-gate ctf_bundle_t src, dst; 1003*7c478bd9Sstevel@tonic-gate ctf_encoding_t src_en, dst_en; 1004*7c478bd9Sstevel@tonic-gate ctf_arinfo_t src_ar, dst_ar; 1005*7c478bd9Sstevel@tonic-gate 1006*7c478bd9Sstevel@tonic-gate ctf_dtdef_t *dtd; 1007*7c478bd9Sstevel@tonic-gate ctf_funcinfo_t ctc; 1008*7c478bd9Sstevel@tonic-gate ssize_t size; 1009*7c478bd9Sstevel@tonic-gate 1010*7c478bd9Sstevel@tonic-gate ctf_hash_t *hp; 1011*7c478bd9Sstevel@tonic-gate ctf_helem_t *hep; 1012*7c478bd9Sstevel@tonic-gate 1013*7c478bd9Sstevel@tonic-gate if (!(dst_fp->ctf_flags & LCTF_RDWR)) 1014*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_RDONLY)); 1015*7c478bd9Sstevel@tonic-gate 1016*7c478bd9Sstevel@tonic-gate if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL) 1017*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1018*7c478bd9Sstevel@tonic-gate 1019*7c478bd9Sstevel@tonic-gate name = ctf_strptr(src_fp, tp->ctt_name); 1020*7c478bd9Sstevel@tonic-gate kind = LCTF_INFO_KIND(src_fp, tp->ctt_info); 1021*7c478bd9Sstevel@tonic-gate flag = LCTF_INFO_ROOT(src_fp, tp->ctt_info); 1022*7c478bd9Sstevel@tonic-gate vlen = LCTF_INFO_VLEN(src_fp, tp->ctt_info); 1023*7c478bd9Sstevel@tonic-gate 1024*7c478bd9Sstevel@tonic-gate switch (kind) { 1025*7c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 1026*7c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_structs; 1027*7c478bd9Sstevel@tonic-gate break; 1028*7c478bd9Sstevel@tonic-gate case CTF_K_UNION: 1029*7c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_unions; 1030*7c478bd9Sstevel@tonic-gate break; 1031*7c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 1032*7c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_enums; 1033*7c478bd9Sstevel@tonic-gate break; 1034*7c478bd9Sstevel@tonic-gate default: 1035*7c478bd9Sstevel@tonic-gate hp = &dst_fp->ctf_names; 1036*7c478bd9Sstevel@tonic-gate break; 1037*7c478bd9Sstevel@tonic-gate } 1038*7c478bd9Sstevel@tonic-gate 1039*7c478bd9Sstevel@tonic-gate /* 1040*7c478bd9Sstevel@tonic-gate * If the source type has a name and is a root type (visible at the 1041*7c478bd9Sstevel@tonic-gate * top-level scope), lookup the name in the destination container and 1042*7c478bd9Sstevel@tonic-gate * verify that it is of the same kind before we do anything else. 1043*7c478bd9Sstevel@tonic-gate */ 1044*7c478bd9Sstevel@tonic-gate if ((flag & CTF_ADD_ROOT) && name[0] != '\0' && 1045*7c478bd9Sstevel@tonic-gate (hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) { 1046*7c478bd9Sstevel@tonic-gate dst_type = (ctf_id_t)hep->h_type; 1047*7c478bd9Sstevel@tonic-gate dst_kind = ctf_type_kind(dst_fp, dst_type); 1048*7c478bd9Sstevel@tonic-gate } 1049*7c478bd9Sstevel@tonic-gate 1050*7c478bd9Sstevel@tonic-gate /* 1051*7c478bd9Sstevel@tonic-gate * If an identically named dst_type exists, fail with ECTF_CONFLICT 1052*7c478bd9Sstevel@tonic-gate * unless dst_type is a forward declaration and src_type is a struct, 1053*7c478bd9Sstevel@tonic-gate * union, or enum (i.e. the definition of the previous forward decl). 1054*7c478bd9Sstevel@tonic-gate */ 1055*7c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR && dst_kind != kind && ( 1056*7c478bd9Sstevel@tonic-gate dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM && 1057*7c478bd9Sstevel@tonic-gate kind != CTF_K_STRUCT && kind != CTF_K_UNION))) 1058*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1059*7c478bd9Sstevel@tonic-gate 1060*7c478bd9Sstevel@tonic-gate /* 1061*7c478bd9Sstevel@tonic-gate * If the non-empty name was not found in the appropriate hash, search 1062*7c478bd9Sstevel@tonic-gate * the list of pending dynamic definitions that are not yet committed. 1063*7c478bd9Sstevel@tonic-gate * If a matching name and kind are found, assume this is the type that 1064*7c478bd9Sstevel@tonic-gate * we are looking for. This is necessary to permit ctf_add_type() to 1065*7c478bd9Sstevel@tonic-gate * operate recursively on entities such as a struct that contains a 1066*7c478bd9Sstevel@tonic-gate * pointer member that refers to the same struct type. 1067*7c478bd9Sstevel@tonic-gate */ 1068*7c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR && name[0] != '\0') { 1069*7c478bd9Sstevel@tonic-gate for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL && 1070*7c478bd9Sstevel@tonic-gate dtd->dtd_type > dst_fp->ctf_dtoldid; 1071*7c478bd9Sstevel@tonic-gate dtd = ctf_list_prev(dtd)) { 1072*7c478bd9Sstevel@tonic-gate if (CTF_INFO_KIND(dtd->dtd_data.ctt_info) == kind && 1073*7c478bd9Sstevel@tonic-gate dtd->dtd_name != NULL && 1074*7c478bd9Sstevel@tonic-gate strcmp(dtd->dtd_name, name) == 0) 1075*7c478bd9Sstevel@tonic-gate return (dtd->dtd_type); 1076*7c478bd9Sstevel@tonic-gate } 1077*7c478bd9Sstevel@tonic-gate } 1078*7c478bd9Sstevel@tonic-gate 1079*7c478bd9Sstevel@tonic-gate src.ctb_file = src_fp; 1080*7c478bd9Sstevel@tonic-gate src.ctb_type = src_type; 1081*7c478bd9Sstevel@tonic-gate src.ctb_dtd = NULL; 1082*7c478bd9Sstevel@tonic-gate 1083*7c478bd9Sstevel@tonic-gate dst.ctb_file = dst_fp; 1084*7c478bd9Sstevel@tonic-gate dst.ctb_type = dst_type; 1085*7c478bd9Sstevel@tonic-gate dst.ctb_dtd = NULL; 1086*7c478bd9Sstevel@tonic-gate 1087*7c478bd9Sstevel@tonic-gate /* 1088*7c478bd9Sstevel@tonic-gate * Now perform kind-specific processing. If dst_type is CTF_ERR, then 1089*7c478bd9Sstevel@tonic-gate * we add a new type with the same properties as src_type to dst_fp. 1090*7c478bd9Sstevel@tonic-gate * If dst_type is not CTF_ERR, then we verify that dst_type has the 1091*7c478bd9Sstevel@tonic-gate * same attributes as src_type. We recurse for embedded references. 1092*7c478bd9Sstevel@tonic-gate */ 1093*7c478bd9Sstevel@tonic-gate switch (kind) { 1094*7c478bd9Sstevel@tonic-gate case CTF_K_INTEGER: 1095*7c478bd9Sstevel@tonic-gate case CTF_K_FLOAT: 1096*7c478bd9Sstevel@tonic-gate if (ctf_type_encoding(src_fp, src_type, &src_en) != 0) 1097*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1098*7c478bd9Sstevel@tonic-gate 1099*7c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR) { 1100*7c478bd9Sstevel@tonic-gate if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0) 1101*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1102*7c478bd9Sstevel@tonic-gate 1103*7c478bd9Sstevel@tonic-gate if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t))) 1104*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1105*7c478bd9Sstevel@tonic-gate 1106*7c478bd9Sstevel@tonic-gate } else if (kind == CTF_K_INTEGER) { 1107*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_integer(dst_fp, flag, name, &src_en); 1108*7c478bd9Sstevel@tonic-gate } else 1109*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_float(dst_fp, flag, name, &src_en); 1110*7c478bd9Sstevel@tonic-gate break; 1111*7c478bd9Sstevel@tonic-gate 1112*7c478bd9Sstevel@tonic-gate case CTF_K_POINTER: 1113*7c478bd9Sstevel@tonic-gate case CTF_K_VOLATILE: 1114*7c478bd9Sstevel@tonic-gate case CTF_K_CONST: 1115*7c478bd9Sstevel@tonic-gate case CTF_K_RESTRICT: 1116*7c478bd9Sstevel@tonic-gate src_type = ctf_type_reference(src_fp, src_type); 1117*7c478bd9Sstevel@tonic-gate src_type = ctf_add_type(dst_fp, src_fp, src_type); 1118*7c478bd9Sstevel@tonic-gate 1119*7c478bd9Sstevel@tonic-gate if (src_type == CTF_ERR) 1120*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1121*7c478bd9Sstevel@tonic-gate 1122*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_reftype(dst_fp, flag, src_type, kind); 1123*7c478bd9Sstevel@tonic-gate break; 1124*7c478bd9Sstevel@tonic-gate 1125*7c478bd9Sstevel@tonic-gate case CTF_K_ARRAY: 1126*7c478bd9Sstevel@tonic-gate if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR) 1127*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ctf_errno(src_fp))); 1128*7c478bd9Sstevel@tonic-gate 1129*7c478bd9Sstevel@tonic-gate src_ar.ctr_contents = 1130*7c478bd9Sstevel@tonic-gate ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents); 1131*7c478bd9Sstevel@tonic-gate src_ar.ctr_index = 1132*7c478bd9Sstevel@tonic-gate ctf_add_type(dst_fp, src_fp, src_ar.ctr_index); 1133*7c478bd9Sstevel@tonic-gate src_ar.ctr_nelems = src_ar.ctr_nelems; 1134*7c478bd9Sstevel@tonic-gate 1135*7c478bd9Sstevel@tonic-gate if (src_ar.ctr_contents == CTF_ERR || 1136*7c478bd9Sstevel@tonic-gate src_ar.ctr_index == CTF_ERR) 1137*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1138*7c478bd9Sstevel@tonic-gate 1139*7c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR) { 1140*7c478bd9Sstevel@tonic-gate if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0) 1141*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1142*7c478bd9Sstevel@tonic-gate 1143*7c478bd9Sstevel@tonic-gate if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t))) 1144*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1145*7c478bd9Sstevel@tonic-gate } else 1146*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_array(dst_fp, flag, &src_ar); 1147*7c478bd9Sstevel@tonic-gate break; 1148*7c478bd9Sstevel@tonic-gate 1149*7c478bd9Sstevel@tonic-gate case CTF_K_FUNCTION: 1150*7c478bd9Sstevel@tonic-gate ctc.ctc_return = ctf_add_type(dst_fp, src_fp, tp->ctt_type); 1151*7c478bd9Sstevel@tonic-gate ctc.ctc_argc = 0; 1152*7c478bd9Sstevel@tonic-gate ctc.ctc_flags = 0; 1153*7c478bd9Sstevel@tonic-gate 1154*7c478bd9Sstevel@tonic-gate if (ctc.ctc_return == CTF_ERR) 1155*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1156*7c478bd9Sstevel@tonic-gate 1157*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_function(dst_fp, flag, &ctc, NULL); 1158*7c478bd9Sstevel@tonic-gate break; 1159*7c478bd9Sstevel@tonic-gate 1160*7c478bd9Sstevel@tonic-gate case CTF_K_STRUCT: 1161*7c478bd9Sstevel@tonic-gate case CTF_K_UNION: { 1162*7c478bd9Sstevel@tonic-gate ctf_dmdef_t *dmd; 1163*7c478bd9Sstevel@tonic-gate int errs = 0; 1164*7c478bd9Sstevel@tonic-gate 1165*7c478bd9Sstevel@tonic-gate /* 1166*7c478bd9Sstevel@tonic-gate * Technically to match a struct or union we need to check both 1167*7c478bd9Sstevel@tonic-gate * ways (src members vs. dst, dst members vs. src) but we make 1168*7c478bd9Sstevel@tonic-gate * this more optimal by only checking src vs. dst and comparing 1169*7c478bd9Sstevel@tonic-gate * the total size of the structure (which we must do anyway) 1170*7c478bd9Sstevel@tonic-gate * which covers the possibility of dst members not in src. 1171*7c478bd9Sstevel@tonic-gate * This optimization can be defeated for unions, but is so 1172*7c478bd9Sstevel@tonic-gate * pathological as to render it irrelevant for our purposes. 1173*7c478bd9Sstevel@tonic-gate */ 1174*7c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 1175*7c478bd9Sstevel@tonic-gate if (ctf_type_size(src_fp, src_type) != 1176*7c478bd9Sstevel@tonic-gate ctf_type_size(dst_fp, dst_type)) 1177*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1178*7c478bd9Sstevel@tonic-gate 1179*7c478bd9Sstevel@tonic-gate if (ctf_member_iter(src_fp, src_type, membcmp, &dst)) 1180*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1181*7c478bd9Sstevel@tonic-gate 1182*7c478bd9Sstevel@tonic-gate break; 1183*7c478bd9Sstevel@tonic-gate } 1184*7c478bd9Sstevel@tonic-gate 1185*7c478bd9Sstevel@tonic-gate /* 1186*7c478bd9Sstevel@tonic-gate * Unlike the other cases, copying structs and unions is done 1187*7c478bd9Sstevel@tonic-gate * manually so as to avoid repeated lookups in ctf_add_member 1188*7c478bd9Sstevel@tonic-gate * and to ensure the exact same member offsets as in src_type. 1189*7c478bd9Sstevel@tonic-gate */ 1190*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_generic(dst_fp, flag, name, &dtd); 1191*7c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR) 1192*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1193*7c478bd9Sstevel@tonic-gate 1194*7c478bd9Sstevel@tonic-gate dst.ctb_type = dst_type; 1195*7c478bd9Sstevel@tonic-gate dst.ctb_dtd = dtd; 1196*7c478bd9Sstevel@tonic-gate 1197*7c478bd9Sstevel@tonic-gate if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0) 1198*7c478bd9Sstevel@tonic-gate errs++; /* increment errs and fail at bottom of case */ 1199*7c478bd9Sstevel@tonic-gate 1200*7c478bd9Sstevel@tonic-gate if ((size = ctf_type_size(src_fp, src_type)) > CTF_MAX_SIZE) { 1201*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = CTF_LSIZE_SENT; 1202*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size); 1203*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size); 1204*7c478bd9Sstevel@tonic-gate } else 1205*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_size = (ushort_t)size; 1206*7c478bd9Sstevel@tonic-gate 1207*7c478bd9Sstevel@tonic-gate dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, flag, vlen); 1208*7c478bd9Sstevel@tonic-gate 1209*7c478bd9Sstevel@tonic-gate /* 1210*7c478bd9Sstevel@tonic-gate * Make a final pass through the members changing each dmd_type 1211*7c478bd9Sstevel@tonic-gate * (a src_fp type) to an equivalent type in dst_fp. We pass 1212*7c478bd9Sstevel@tonic-gate * through all members, leaving any that fail set to CTF_ERR. 1213*7c478bd9Sstevel@tonic-gate */ 1214*7c478bd9Sstevel@tonic-gate for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); 1215*7c478bd9Sstevel@tonic-gate dmd != NULL; dmd = ctf_list_next(dmd)) { 1216*7c478bd9Sstevel@tonic-gate if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp, 1217*7c478bd9Sstevel@tonic-gate dmd->dmd_type)) == CTF_ERR) 1218*7c478bd9Sstevel@tonic-gate errs++; 1219*7c478bd9Sstevel@tonic-gate } 1220*7c478bd9Sstevel@tonic-gate 1221*7c478bd9Sstevel@tonic-gate if (errs) 1222*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1223*7c478bd9Sstevel@tonic-gate break; 1224*7c478bd9Sstevel@tonic-gate } 1225*7c478bd9Sstevel@tonic-gate 1226*7c478bd9Sstevel@tonic-gate case CTF_K_ENUM: 1227*7c478bd9Sstevel@tonic-gate if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) { 1228*7c478bd9Sstevel@tonic-gate if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) || 1229*7c478bd9Sstevel@tonic-gate ctf_enum_iter(dst_fp, dst_type, enumcmp, &src)) 1230*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CONFLICT)); 1231*7c478bd9Sstevel@tonic-gate } else { 1232*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_enum(dst_fp, flag, name); 1233*7c478bd9Sstevel@tonic-gate if ((dst.ctb_type = dst_type) == CTF_ERR || 1234*7c478bd9Sstevel@tonic-gate ctf_enum_iter(src_fp, src_type, enumadd, &dst)) 1235*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1236*7c478bd9Sstevel@tonic-gate } 1237*7c478bd9Sstevel@tonic-gate break; 1238*7c478bd9Sstevel@tonic-gate 1239*7c478bd9Sstevel@tonic-gate case CTF_K_FORWARD: 1240*7c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR) { 1241*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_forward(dst_fp, 1242*7c478bd9Sstevel@tonic-gate flag, name, CTF_K_STRUCT); /* assume STRUCT */ 1243*7c478bd9Sstevel@tonic-gate } 1244*7c478bd9Sstevel@tonic-gate break; 1245*7c478bd9Sstevel@tonic-gate 1246*7c478bd9Sstevel@tonic-gate case CTF_K_TYPEDEF: 1247*7c478bd9Sstevel@tonic-gate src_type = ctf_type_reference(src_fp, src_type); 1248*7c478bd9Sstevel@tonic-gate src_type = ctf_add_type(dst_fp, src_fp, src_type); 1249*7c478bd9Sstevel@tonic-gate 1250*7c478bd9Sstevel@tonic-gate if (src_type == CTF_ERR) 1251*7c478bd9Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 1252*7c478bd9Sstevel@tonic-gate 1253*7c478bd9Sstevel@tonic-gate /* 1254*7c478bd9Sstevel@tonic-gate * If dst_type is not CTF_ERR at this point, we should check if 1255*7c478bd9Sstevel@tonic-gate * ctf_type_reference(dst_fp, dst_type) != src_type and if so 1256*7c478bd9Sstevel@tonic-gate * fail with ECTF_CONFLICT. However, this causes problems with 1257*7c478bd9Sstevel@tonic-gate * <sys/types.h> typedefs that vary based on things like if 1258*7c478bd9Sstevel@tonic-gate * _ILP32x then pid_t is int otherwise long. We therefore omit 1259*7c478bd9Sstevel@tonic-gate * this check and assume that if the identically named typedef 1260*7c478bd9Sstevel@tonic-gate * already exists in dst_fp, it is correct or equivalent. 1261*7c478bd9Sstevel@tonic-gate */ 1262*7c478bd9Sstevel@tonic-gate if (dst_type == CTF_ERR) { 1263*7c478bd9Sstevel@tonic-gate dst_type = ctf_add_typedef(dst_fp, flag, 1264*7c478bd9Sstevel@tonic-gate name, src_type); 1265*7c478bd9Sstevel@tonic-gate } 1266*7c478bd9Sstevel@tonic-gate break; 1267*7c478bd9Sstevel@tonic-gate 1268*7c478bd9Sstevel@tonic-gate default: 1269*7c478bd9Sstevel@tonic-gate return (ctf_set_errno(dst_fp, ECTF_CORRUPT)); 1270*7c478bd9Sstevel@tonic-gate } 1271*7c478bd9Sstevel@tonic-gate 1272*7c478bd9Sstevel@tonic-gate return (dst_type); 1273*7c478bd9Sstevel@tonic-gate } 1274