Path: blob/main/cddl/contrib/opensolaris/common/ctf/ctf_create.c
39507 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2006 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/26/*27* Copyright (c) 2013, Joyent, Inc. All rights reserved.28*/2930#include <sys/sysmacros.h>31#include <sys/param.h>32#include <sys/mman.h>33#include <ctf_impl.h>34#include <sys/debug.h>3536/*37* This static string is used as the template for initially populating a38* dynamic container's string table. We always store \0 in the first byte,39* and we use the generic string "PARENT" to mark this container's parent40* if one is associated with the container using ctf_import().41*/42static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";4344/*45* To create an empty CTF container, we just declare a zeroed header and call46* ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w47* and initialize the dynamic members. We set dtstrlen to 1 to reserve the48* first byte of the string table for a \0 byte, and we start assigning type49* IDs at 1 because type ID 0 is used as a sentinel.50*/51ctf_file_t *52ctf_create(int *errp)53{54static const ctf_header_t hdr = { { CTF_MAGIC, CTF_VERSION, 0 } };5556const ulong_t hashlen = 128;57ctf_dtdef_t **hash = ctf_alloc(hashlen * sizeof (ctf_dtdef_t *));58ctf_sect_t cts;59ctf_file_t *fp;6061if (hash == NULL)62return (ctf_set_open_errno(errp, EAGAIN));6364cts.cts_name = _CTF_SECTION;65cts.cts_type = SHT_PROGBITS;66cts.cts_flags = 0;67cts.cts_data = (void *)&hdr;68cts.cts_size = sizeof (hdr);69cts.cts_entsize = 1;70cts.cts_offset = 0;7172if ((fp = ctf_bufopen(&cts, NULL, NULL, errp)) == NULL) {73ctf_free(hash, hashlen * sizeof (ctf_dtdef_t *));74return (NULL);75}7677fp->ctf_flags |= LCTF_RDWR;78fp->ctf_dthashlen = hashlen;79bzero(hash, hashlen * sizeof (ctf_dtdef_t *));80fp->ctf_dthash = hash;81fp->ctf_dtstrlen = sizeof (_CTF_STRTAB_TEMPLATE);82fp->ctf_dtnextid = 1;83fp->ctf_dtoldid = 0;8485return (fp);86}8788static uchar_t *89ctf_copy_smembers(const ctf_file_t *fp, ctf_dtdef_t *dtd, uint_t soff,90uchar_t *t)91{92ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);93size_t sz;94uint_t name;9596for (; dmd != NULL; dmd = ctf_list_next(dmd)) {97if (dmd->dmd_name) {98name = soff;99soff += strlen(dmd->dmd_name) + 1;100} else101name = 0;102103if (fp->ctf_version == CTF_VERSION_2) {104struct ctf_member_v2 ctm;105106ctm.ctm_name = name;107ctm.ctm_type = (ushort_t)dmd->dmd_type;108ctm.ctm_offset = (ushort_t)dmd->dmd_offset;109110sz = sizeof (ctm);111bcopy(&ctm, t, sz);112t += sz;113} else {114struct ctf_member_v3 ctm;115116ctm.ctm_name = name;117ctm.ctm_type = dmd->dmd_type;118ctm.ctm_offset = dmd->dmd_offset;119120sz = sizeof (ctm);121bcopy(&ctm, t, sz);122t += sz;123}124}125126return (t);127}128129static uchar_t *130ctf_copy_lmembers(const ctf_file_t *fp, ctf_dtdef_t *dtd, uint_t soff,131uchar_t *t)132{133ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);134size_t sz;135uint_t name;136137for (; dmd != NULL; dmd = ctf_list_next(dmd)) {138if (dmd->dmd_name) {139name = soff;140soff += strlen(dmd->dmd_name) + 1;141} else142name = 0;143144if (fp->ctf_version == CTF_VERSION_2) {145struct ctf_lmember_v2 ctlm;146147ctlm.ctlm_name = name;148ctlm.ctlm_type = (ushort_t)dmd->dmd_type;149ctlm.ctlm_pad = 0;150ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);151ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);152153sz = sizeof (ctlm);154bcopy(&ctlm, t, sz);155t += sz;156} else {157struct ctf_lmember_v3 ctlm;158159ctlm.ctlm_name = name;160ctlm.ctlm_type = dmd->dmd_type;161ctlm.ctlm_offsethi = CTF_OFFSET_TO_LMEMHI(dmd->dmd_offset);162ctlm.ctlm_offsetlo = CTF_OFFSET_TO_LMEMLO(dmd->dmd_offset);163164sz = sizeof (ctlm);165bcopy(&ctlm, t, sz);166t += sz;167}168}169170return (t);171}172173static uchar_t *174ctf_copy_emembers(ctf_dtdef_t *dtd, uint_t soff, uchar_t *t)175{176ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);177ctf_enum_t cte;178179for (; dmd != NULL; dmd = ctf_list_next(dmd)) {180cte.cte_name = soff;181cte.cte_value = dmd->dmd_value;182soff += strlen(dmd->dmd_name) + 1;183bcopy(&cte, t, sizeof (cte));184t += sizeof (cte);185}186187return (t);188}189190static uchar_t *191ctf_copy_membnames(ctf_dtdef_t *dtd, uchar_t *s)192{193ctf_dmdef_t *dmd = ctf_list_next(&dtd->dtd_u.dtu_members);194size_t len;195196for (; dmd != NULL; dmd = ctf_list_next(dmd)) {197if (dmd->dmd_name == NULL)198continue; /* skip anonymous members */199len = strlen(dmd->dmd_name) + 1;200bcopy(dmd->dmd_name, s, len);201s += len;202}203204return (s);205}206207/*208* Only types of dyanmic CTF containers contain reference counts. These209* containers are marked RD/WR. Because of that we basically make this a no-op210* for compatability with non-dynamic CTF sections. This is also a no-op for211* types which are not dynamic types. It is the responsibility of the caller to212* make sure it is a valid type. We help that caller out on debug builds.213*214* Note that the reference counts are not maintained for types that are not215* within this container. In other words if we have a type in a parent, that216* will not have its reference count increased. On the flip side, the parent217* will not be allowed to remove dynamic types if it has children.218*/219static void220ctf_ref_inc(ctf_file_t *fp, ctf_id_t tid)221{222ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);223224if (dtd == NULL)225return;226227if (!(fp->ctf_flags & LCTF_RDWR))228return;229230dtd->dtd_ref++;231}232233/*234* Just as with ctf_ref_inc, this is a no-op on non-writeable containers and the235* caller should ensure that this is already a valid type.236*/237static void238ctf_ref_dec(ctf_file_t *fp, ctf_id_t tid)239{240ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, tid);241242if (dtd == NULL)243return;244245if (!(fp->ctf_flags & LCTF_RDWR))246return;247248ASSERT(dtd->dtd_ref >= 1);249dtd->dtd_ref--;250}251252/*253* If the specified CTF container is writable and has been modified, reload254* this container with the updated type definitions. In order to make this255* code and the rest of libctf as simple as possible, we perform updates by256* taking the dynamic type definitions and creating an in-memory CTF file257* containing the definitions, and then call ctf_bufopen() on it. This not258* only leverages ctf_bufopen(), but also avoids having to bifurcate the rest259* of the library code with different lookup paths for static and dynamic260* type definitions. We are therefore optimizing greatly for lookup over261* update, which we assume will be an uncommon operation. We perform one262* extra trick here for the benefit of callers and to keep our code simple:263* ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp264* constant for the caller, so after ctf_bufopen() returns, we use bcopy to265* swap the interior of the old and new ctf_file_t's, and then free the old.266*267* Note that the lists of dynamic types stays around and the resulting container268* is still writeable. Furthermore, the reference counts that are on the dtd's269* are still valid.270*/271int272ctf_update(ctf_file_t *fp)273{274ctf_file_t ofp, *nfp;275ctf_header_t hdr;276ctf_dtdef_t *dtd;277ctf_sect_t cts;278279uchar_t *s, *s0, *t;280size_t size;281void *buf;282int err;283284if (!(fp->ctf_flags & LCTF_RDWR))285return (ctf_set_errno(fp, ECTF_RDONLY));286287if (!(fp->ctf_flags & LCTF_DIRTY))288return (0); /* no update required */289290/*291* Fill in an initial CTF header. We will leave the label, object,292* and function sections empty and only output a header, type section,293* and string table. The type section begins at a 4-byte aligned294* boundary past the CTF header itself (at relative offset zero).295*/296bzero(&hdr, sizeof (hdr));297hdr.cth_magic = CTF_MAGIC;298hdr.cth_version = fp->ctf_version;299300if (fp->ctf_flags & LCTF_CHILD)301hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */302303/*304* Iterate through the dynamic type definition list and compute the305* size of the CTF type section we will need to generate.306*/307for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);308dtd != NULL; dtd = ctf_list_next(dtd)) {309310uint_t kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);311uint_t vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);312313if (fp->ctf_version == CTF_VERSION_2) {314if (dtd->dtd_data.ctt_size != CTF_V2_LSIZE_SENT)315size += sizeof (struct ctf_stype_v2);316else317size += sizeof (struct ctf_type_v2);318} else {319if (dtd->dtd_data.ctt_size != LCTF_LSIZE_SENT(fp))320size += sizeof (struct ctf_stype_v3);321else322size += sizeof (struct ctf_type_v3);323}324325switch (kind) {326case CTF_K_INTEGER:327case CTF_K_FLOAT:328size += sizeof (uint_t);329break;330case CTF_K_ARRAY:331size += fp->ctf_version == CTF_VERSION_2 ?332sizeof (struct ctf_array_v2) :333sizeof (struct ctf_array_v3);334break;335case CTF_K_FUNCTION:336size += roundup2(fp->ctf_idwidth * vlen, 4);337break;338case CTF_K_STRUCT:339case CTF_K_UNION:340if (fp->ctf_version == CTF_VERSION_2) {341if (dtd->dtd_data.ctt_size <342LCTF_LSTRUCT_THRESH(fp))343size += sizeof (struct ctf_member_v2) *344vlen;345else346size += sizeof (struct ctf_lmember_v2) *347vlen;348} else {349if (dtd->dtd_data.ctt_size <350LCTF_LSTRUCT_THRESH(fp))351size += sizeof (struct ctf_member_v3) *352vlen;353else354size += sizeof (struct ctf_lmember_v3) *355vlen;356}357break;358case CTF_K_ENUM:359size += sizeof (ctf_enum_t) * vlen;360break;361}362}363364/*365* Fill in the string table offset and size, compute the size of the366* entire CTF buffer we need, and then allocate a new buffer and367* bcopy the finished header to the start of the buffer.368*/369hdr.cth_stroff = hdr.cth_typeoff + size;370hdr.cth_strlen = fp->ctf_dtstrlen;371size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;372373if ((buf = ctf_data_alloc(size)) == MAP_FAILED)374return (ctf_set_errno(fp, EAGAIN));375376bcopy(&hdr, buf, sizeof (ctf_header_t));377t = (uchar_t *)buf + sizeof (ctf_header_t);378s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;379380bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));381s += sizeof (_CTF_STRTAB_TEMPLATE);382383/*384* We now take a final lap through the dynamic type definition list and385* copy the appropriate type records and strings to the output buffer.386*/387for (dtd = ctf_list_next(&fp->ctf_dtdefs);388dtd != NULL; dtd = ctf_list_next(dtd)) {389void *tp;390uint_t kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);391uint_t vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);392struct ctf_type_v2 ctt;393394uint_t encoding;395size_t len;396397if (dtd->dtd_name != NULL) {398dtd->dtd_data.ctt_name = (uint_t)(s - s0);399len = strlen(dtd->dtd_name) + 1;400bcopy(dtd->dtd_name, s, len);401s += len;402} else403dtd->dtd_data.ctt_name = 0;404405if (fp->ctf_version == CTF_VERSION_2) {406ctt.ctt_name = dtd->dtd_data.ctt_name;407ctt.ctt_info = (ushort_t)dtd->dtd_data.ctt_info;408ctt.ctt_size = (ushort_t)dtd->dtd_data.ctt_size;409if (dtd->dtd_data.ctt_size != CTF_V2_LSIZE_SENT)410len = sizeof (struct ctf_stype_v2);411else {412len = sizeof (struct ctf_type_v2);413ctt.ctt_lsizehi = dtd->dtd_data.ctt_lsizehi;414ctt.ctt_lsizelo = dtd->dtd_data.ctt_lsizelo;415}416tp = &ctt;417} else {418if (dtd->dtd_data.ctt_size != LCTF_LSIZE_SENT(fp))419len = sizeof (struct ctf_stype_v3);420else421len = sizeof (struct ctf_type_v3);422tp = &dtd->dtd_data;423}424425bcopy(tp, t, len);426t += len;427428switch (kind) {429case CTF_K_INTEGER:430case CTF_K_FLOAT:431if (kind == CTF_K_INTEGER) {432encoding = CTF_INT_DATA(433dtd->dtd_u.dtu_enc.cte_format,434dtd->dtd_u.dtu_enc.cte_offset,435dtd->dtd_u.dtu_enc.cte_bits);436} else {437encoding = CTF_FP_DATA(438dtd->dtd_u.dtu_enc.cte_format,439dtd->dtd_u.dtu_enc.cte_offset,440dtd->dtd_u.dtu_enc.cte_bits);441}442bcopy(&encoding, t, sizeof (encoding));443t += sizeof (encoding);444break;445446case CTF_K_ARRAY:447if (fp->ctf_version == CTF_VERSION_2) {448struct ctf_array_v2 cta;449450cta.cta_contents =451(uint16_t)dtd->dtd_u.dtu_arr.ctr_contents;452cta.cta_index =453(uint16_t)dtd->dtd_u.dtu_arr.ctr_index;454cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;455456bcopy(&cta, t, sizeof (cta));457t += sizeof (cta);458} else {459struct ctf_array_v3 cta;460461cta.cta_contents =462dtd->dtd_u.dtu_arr.ctr_contents;463cta.cta_index = dtd->dtd_u.dtu_arr.ctr_index;464cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;465466bcopy(&cta, t, sizeof (cta));467t += sizeof (cta);468}469break;470471case CTF_K_FUNCTION: {472char *argv = (char *)(uintptr_t)t;473uint_t argc;474475if (fp->ctf_version == CTF_VERSION_2) {476ushort_t arg;477478for (argc = 0; argc < vlen;479argc++, argv += sizeof(arg)) {480arg =481(ushort_t)dtd->dtd_u.dtu_argv[argc];482memcpy(argv, &arg, sizeof(arg));483}484} else {485uint_t arg;486487for (argc = 0; argc < vlen;488argc++, argv += sizeof(arg)) {489arg = (uint_t)dtd->dtd_u.dtu_argv[argc];490memcpy(argv, &arg, sizeof(arg));491}492}493494t = (uchar_t *)argv;495break;496}497498case CTF_K_STRUCT:499case CTF_K_UNION:500if (dtd->dtd_data.ctt_size < LCTF_LSTRUCT_THRESH(fp))501t = ctf_copy_smembers(fp, dtd, (uint_t)(s - s0),502t);503else504t = ctf_copy_lmembers(fp, dtd, (uint_t)(s - s0),505t);506s = ctf_copy_membnames(dtd, s);507break;508509case CTF_K_ENUM:510t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);511s = ctf_copy_membnames(dtd, s);512break;513}514}515516/*517* Finally, we are ready to ctf_bufopen() the new container. If this518* is successful, we then switch nfp and fp and free the old container.519*/520ctf_data_protect(buf, size);521cts.cts_name = _CTF_SECTION;522cts.cts_type = SHT_PROGBITS;523cts.cts_flags = 0;524cts.cts_data = buf;525cts.cts_size = size;526cts.cts_entsize = 1;527cts.cts_offset = 0;528529if ((nfp = ctf_bufopen(&cts, NULL, NULL, &err)) == NULL) {530ctf_data_free(buf, size);531return (ctf_set_errno(fp, err));532}533534(void) ctf_setmodel(nfp, ctf_getmodel(fp));535(void) ctf_import(nfp, fp->ctf_parent);536537nfp->ctf_refcnt = fp->ctf_refcnt;538nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;539nfp->ctf_data.cts_data = NULL; /* force ctf_data_free() on close */540nfp->ctf_dthash = fp->ctf_dthash;541nfp->ctf_dthashlen = fp->ctf_dthashlen;542nfp->ctf_dtdefs = fp->ctf_dtdefs;543nfp->ctf_dtstrlen = fp->ctf_dtstrlen;544nfp->ctf_dtnextid = fp->ctf_dtnextid;545nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;546nfp->ctf_specific = fp->ctf_specific;547548fp->ctf_dthash = NULL;549fp->ctf_dthashlen = 0;550bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));551552bcopy(fp, &ofp, sizeof (ctf_file_t));553bcopy(nfp, fp, sizeof (ctf_file_t));554bcopy(&ofp, nfp, sizeof (ctf_file_t));555556/*557* Initialize the ctf_lookup_by_name top-level dictionary. We keep an558* array of type name prefixes and the corresponding ctf_hash to use.559* NOTE: This code must be kept in sync with the code in ctf_bufopen().560*/561fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;562fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;563fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;564fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;565566nfp->ctf_refcnt = 1; /* force nfp to be freed */567ctf_close(nfp);568569return (0);570}571572void573ctf_dtd_insert(ctf_file_t *fp, ctf_dtdef_t *dtd)574{575ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);576577dtd->dtd_hash = fp->ctf_dthash[h];578fp->ctf_dthash[h] = dtd;579ctf_list_append(&fp->ctf_dtdefs, dtd);580}581582void583ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)584{585ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);586ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];587ctf_dmdef_t *dmd, *nmd;588size_t len;589int kind, i;590591for (p = *q; p != NULL; p = p->dtd_hash) {592if (p != dtd)593q = &p->dtd_hash;594else595break;596}597598if (p != NULL)599*q = p->dtd_hash;600601kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);602switch (kind) {603case CTF_K_STRUCT:604case CTF_K_UNION:605case CTF_K_ENUM:606for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);607dmd != NULL; dmd = nmd) {608if (dmd->dmd_name != NULL) {609len = strlen(dmd->dmd_name) + 1;610ctf_free(dmd->dmd_name, len);611fp->ctf_dtstrlen -= len;612}613if (kind != CTF_K_ENUM)614ctf_ref_dec(fp, dmd->dmd_type);615nmd = ctf_list_next(dmd);616ctf_free(dmd, sizeof (ctf_dmdef_t));617}618break;619case CTF_K_FUNCTION:620ctf_ref_dec(fp, dtd->dtd_data.ctt_type);621for (i = 0; i < LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info); i++)622if (dtd->dtd_u.dtu_argv[i] != 0)623ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);624ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *625LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info));626break;627case CTF_K_ARRAY:628ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);629ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);630break;631case CTF_K_TYPEDEF:632ctf_ref_dec(fp, dtd->dtd_data.ctt_type);633break;634case CTF_K_POINTER:635case CTF_K_VOLATILE:636case CTF_K_CONST:637case CTF_K_RESTRICT:638ctf_ref_dec(fp, dtd->dtd_data.ctt_type);639break;640}641642if (dtd->dtd_name) {643len = strlen(dtd->dtd_name) + 1;644ctf_free(dtd->dtd_name, len);645fp->ctf_dtstrlen -= len;646}647648ctf_list_delete(&fp->ctf_dtdefs, dtd);649ctf_free(dtd, sizeof (ctf_dtdef_t));650}651652ctf_dtdef_t *653ctf_dtd_lookup(ctf_file_t *fp, ctf_id_t type)654{655ulong_t h = type & (fp->ctf_dthashlen - 1);656ctf_dtdef_t *dtd;657658if (fp->ctf_dthash == NULL)659return (NULL);660661for (dtd = fp->ctf_dthash[h]; dtd != NULL; dtd = dtd->dtd_hash) {662if (dtd->dtd_type == type)663break;664}665666return (dtd);667}668669/*670* Discard all of the dynamic type definitions that have been added to the671* container since the last call to ctf_update(). We locate such types by672* scanning the list and deleting elements that have type IDs greater than673* ctf_dtoldid, which is set by ctf_update(), above. Note that to work properly674* with our reference counting schemes, we must delete the dynamic list in675* reverse.676*/677int678ctf_discard(ctf_file_t *fp)679{680ctf_dtdef_t *dtd, *ntd;681682if (!(fp->ctf_flags & LCTF_RDWR))683return (ctf_set_errno(fp, ECTF_RDONLY));684685if (!(fp->ctf_flags & LCTF_DIRTY))686return (0); /* no update required */687688for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {689ntd = ctf_list_prev(dtd);690if (LCTF_TYPE_TO_INDEX(fp, dtd->dtd_type) <= fp->ctf_dtoldid)691continue; /* skip types that have been committed */692693ctf_dtd_delete(fp, dtd);694}695696fp->ctf_dtnextid = fp->ctf_dtoldid + 1;697fp->ctf_flags &= ~LCTF_DIRTY;698699return (0);700}701702static ctf_id_t703ctf_add_generic(ctf_file_t *fp, uint_t flag, const char *name, ctf_dtdef_t **rp)704{705ctf_dtdef_t *dtd;706ctf_id_t type;707char *s = NULL;708709if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)710return (ctf_set_errno(fp, EINVAL));711712if (!(fp->ctf_flags & LCTF_RDWR))713return (ctf_set_errno(fp, ECTF_RDONLY));714715if (LCTF_INDEX_TO_TYPE(fp, fp->ctf_dtnextid, 1) > LCTF_MAX_TYPE(fp))716return (ctf_set_errno(fp, ECTF_FULL));717718if ((dtd = ctf_alloc(sizeof (ctf_dtdef_t))) == NULL)719return (ctf_set_errno(fp, EAGAIN));720721if (name != NULL && *name != '\0' && (s = ctf_strdup(name)) == NULL) {722ctf_free(dtd, sizeof (ctf_dtdef_t));723return (ctf_set_errno(fp, EAGAIN));724}725726type = fp->ctf_dtnextid++;727type = LCTF_INDEX_TO_TYPE(fp, type, (fp->ctf_flags & LCTF_CHILD));728729bzero(dtd, sizeof (ctf_dtdef_t));730dtd->dtd_name = s;731dtd->dtd_type = type;732733if (s != NULL)734fp->ctf_dtstrlen += strlen(s) + 1;735736ctf_dtd_insert(fp, dtd);737fp->ctf_flags |= LCTF_DIRTY;738739*rp = dtd;740return (type);741}742743/*744* When encoding integer sizes, we want to convert a byte count in the range745* 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function746* is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.747*/748static size_t749clp2(size_t x)750{751x--;752753x |= (x >> 1);754x |= (x >> 2);755x |= (x >> 4);756x |= (x >> 8);757x |= (x >> 16);758759return (x + 1);760}761762static ctf_id_t763ctf_add_encoded(ctf_file_t *fp, uint_t flag,764const char *name, const ctf_encoding_t *ep, uint_t kind)765{766ctf_dtdef_t *dtd;767ctf_id_t type;768769if (ep == NULL)770return (ctf_set_errno(fp, EINVAL));771772if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)773return (CTF_ERR); /* errno is set for us */774775dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, flag, 0);776dtd->dtd_data.ctt_size = clp2(P2ROUNDUP(ep->cte_bits, NBBY) / NBBY);777dtd->dtd_u.dtu_enc = *ep;778779return (type);780}781782static ctf_id_t783ctf_add_reftype(ctf_file_t *fp, uint_t flag, ctf_id_t ref, uint_t kind)784{785ctf_dtdef_t *dtd;786ctf_id_t type;787788if (ref == CTF_ERR || ref > LCTF_MAX_TYPE(fp))789return (ctf_set_errno(fp, EINVAL));790791if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)792return (CTF_ERR); /* errno is set for us */793794ctf_ref_inc(fp, ref);795796dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, flag, 0);797dtd->dtd_data.ctt_type = (uint_t)ref;798799return (type);800}801802ctf_id_t803ctf_add_integer(ctf_file_t *fp, uint_t flag,804const char *name, const ctf_encoding_t *ep)805{806return (ctf_add_encoded(fp, flag, name, ep, CTF_K_INTEGER));807}808809ctf_id_t810ctf_add_float(ctf_file_t *fp, uint_t flag,811const char *name, const ctf_encoding_t *ep)812{813return (ctf_add_encoded(fp, flag, name, ep, CTF_K_FLOAT));814}815816ctf_id_t817ctf_add_pointer(ctf_file_t *fp, uint_t flag, ctf_id_t ref)818{819return (ctf_add_reftype(fp, flag, ref, CTF_K_POINTER));820}821822ctf_id_t823ctf_add_array(ctf_file_t *fp, uint_t flag, const ctf_arinfo_t *arp)824{825ctf_dtdef_t *dtd;826ctf_id_t type;827ctf_file_t *fpd;828829if (arp == NULL)830return (ctf_set_errno(fp, EINVAL));831832fpd = fp;833if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&834ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)835return (ctf_set_errno(fp, ECTF_BADID));836837fpd = fp;838if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&839ctf_dtd_lookup(fp, arp->ctr_index) == NULL)840return (ctf_set_errno(fp, ECTF_BADID));841842if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR)843return (CTF_ERR); /* errno is set for us */844845dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_ARRAY, flag, 0);846dtd->dtd_data.ctt_size = 0;847dtd->dtd_u.dtu_arr = *arp;848ctf_ref_inc(fp, arp->ctr_contents);849ctf_ref_inc(fp, arp->ctr_index);850851return (type);852}853854int855ctf_set_array(ctf_file_t *fp, ctf_id_t type, const ctf_arinfo_t *arp)856{857ctf_file_t *fpd;858ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);859860if (!(fp->ctf_flags & LCTF_RDWR))861return (ctf_set_errno(fp, ECTF_RDONLY));862863if (dtd == NULL ||864LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY)865return (ctf_set_errno(fp, ECTF_BADID));866867fpd = fp;868if (ctf_lookup_by_id(&fpd, arp->ctr_contents) == NULL &&869ctf_dtd_lookup(fp, arp->ctr_contents) == NULL)870return (ctf_set_errno(fp, ECTF_BADID));871872fpd = fp;873if (ctf_lookup_by_id(&fpd, arp->ctr_index) == NULL &&874ctf_dtd_lookup(fp, arp->ctr_index) == NULL)875return (ctf_set_errno(fp, ECTF_BADID));876877ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);878ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);879fp->ctf_flags |= LCTF_DIRTY;880dtd->dtd_u.dtu_arr = *arp;881ctf_ref_inc(fp, arp->ctr_contents);882ctf_ref_inc(fp, arp->ctr_index);883884return (0);885}886887ctf_id_t888ctf_add_function(ctf_file_t *fp, uint_t flag,889const ctf_funcinfo_t *ctc, const ctf_id_t *argv)890{891ctf_dtdef_t *dtd;892ctf_id_t type;893uint_t vlen;894int i;895ctf_id_t *vdat = NULL;896ctf_file_t *fpd;897898if (ctc == NULL || (ctc->ctc_flags & ~CTF_FUNC_VARARG) != 0 ||899(ctc->ctc_argc != 0 && argv == NULL))900return (ctf_set_errno(fp, EINVAL));901902vlen = ctc->ctc_argc;903if (ctc->ctc_flags & CTF_FUNC_VARARG)904vlen++; /* add trailing zero to indicate varargs (see below) */905906if (vlen > LCTF_MAX_VLEN(fp))907return (ctf_set_errno(fp, EOVERFLOW));908909fpd = fp;910if (ctf_lookup_by_id(&fpd, ctc->ctc_return) == NULL &&911ctf_dtd_lookup(fp, ctc->ctc_return) == NULL)912return (ctf_set_errno(fp, ECTF_BADID));913914for (i = 0; i < ctc->ctc_argc; i++) {915fpd = fp;916if (ctf_lookup_by_id(&fpd, argv[i]) == NULL &&917ctf_dtd_lookup(fp, argv[i]) == NULL)918return (ctf_set_errno(fp, ECTF_BADID));919}920921if (vlen != 0 && (vdat = ctf_alloc(sizeof (ctf_id_t) * vlen)) == NULL)922return (ctf_set_errno(fp, EAGAIN));923924if ((type = ctf_add_generic(fp, flag, NULL, &dtd)) == CTF_ERR) {925ctf_free(vdat, sizeof (ctf_id_t) * vlen);926return (CTF_ERR); /* errno is set for us */927}928929dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_FUNCTION, flag, vlen);930dtd->dtd_data.ctt_type = ctc->ctc_return;931932ctf_ref_inc(fp, ctc->ctc_return);933for (i = 0; i < ctc->ctc_argc; i++)934ctf_ref_inc(fp, argv[i]);935936bcopy(argv, vdat, sizeof (ctf_id_t) * ctc->ctc_argc);937if (ctc->ctc_flags & CTF_FUNC_VARARG)938vdat[vlen - 1] = 0; /* add trailing zero to indicate varargs */939dtd->dtd_u.dtu_argv = vdat;940941return (type);942}943944ctf_id_t945ctf_add_struct(ctf_file_t *fp, uint_t flag, const char *name)946{947ctf_hash_t *hp = &fp->ctf_structs;948ctf_helem_t *hep = NULL;949ctf_dtdef_t *dtd;950ctf_id_t type;951952if (name != NULL)953hep = ctf_hash_lookup(hp, fp, name, strlen(name));954955if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)956dtd = ctf_dtd_lookup(fp, type = hep->h_type);957else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)958return (CTF_ERR); /* errno is set for us */959960dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_STRUCT, flag, 0);961dtd->dtd_data.ctt_size = 0;962963return (type);964}965966ctf_id_t967ctf_add_union(ctf_file_t *fp, uint_t flag, const char *name)968{969ctf_hash_t *hp = &fp->ctf_unions;970ctf_helem_t *hep = NULL;971ctf_dtdef_t *dtd;972ctf_id_t type;973974if (name != NULL)975hep = ctf_hash_lookup(hp, fp, name, strlen(name));976977if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)978dtd = ctf_dtd_lookup(fp, type = hep->h_type);979else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)980return (CTF_ERR); /* errno is set for us */981982dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_UNION, flag, 0);983dtd->dtd_data.ctt_size = 0;984985return (type);986}987988ctf_id_t989ctf_add_enum(ctf_file_t *fp, uint_t flag, const char *name)990{991ctf_hash_t *hp = &fp->ctf_enums;992ctf_helem_t *hep = NULL;993ctf_dtdef_t *dtd;994ctf_id_t type;995996if (name != NULL)997hep = ctf_hash_lookup(hp, fp, name, strlen(name));998999if (hep != NULL && ctf_type_kind(fp, hep->h_type) == CTF_K_FORWARD)1000dtd = ctf_dtd_lookup(fp, type = hep->h_type);1001else if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)1002return (CTF_ERR); /* errno is set for us */10031004dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_ENUM, flag, 0);1005dtd->dtd_data.ctt_size = fp->ctf_dmodel->ctd_int;10061007return (type);1008}10091010ctf_id_t1011ctf_add_forward(ctf_file_t *fp, uint_t flag, const char *name, uint_t kind)1012{1013ctf_hash_t *hp;1014ctf_helem_t *hep;1015ctf_dtdef_t *dtd;1016ctf_id_t type;10171018switch (kind) {1019case CTF_K_STRUCT:1020hp = &fp->ctf_structs;1021break;1022case CTF_K_UNION:1023hp = &fp->ctf_unions;1024break;1025case CTF_K_ENUM:1026hp = &fp->ctf_enums;1027break;1028default:1029return (ctf_set_errno(fp, ECTF_NOTSUE));1030}10311032/*1033* If the type is already defined or exists as a forward tag, just1034* return the ctf_id_t of the existing definition.1035*/1036if (name != NULL && (hep = ctf_hash_lookup(hp,1037fp, name, strlen(name))) != NULL)1038return (hep->h_type);10391040if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)1041return (CTF_ERR); /* errno is set for us */10421043dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_FORWARD, flag, 0);1044dtd->dtd_data.ctt_type = kind;10451046return (type);1047}10481049ctf_id_t1050ctf_add_typedef(ctf_file_t *fp, uint_t flag, const char *name, ctf_id_t ref)1051{1052ctf_dtdef_t *dtd;1053ctf_id_t type;1054ctf_file_t *fpd;10551056fpd = fp;1057if (ref == CTF_ERR || (ctf_lookup_by_id(&fpd, ref) == NULL &&1058ctf_dtd_lookup(fp, ref) == NULL))1059return (ctf_set_errno(fp, EINVAL));10601061if ((type = ctf_add_generic(fp, flag, name, &dtd)) == CTF_ERR)1062return (CTF_ERR); /* errno is set for us */10631064dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, CTF_K_TYPEDEF, flag, 0);1065dtd->dtd_data.ctt_type = ref;1066ctf_ref_inc(fp, ref);10671068return (type);1069}10701071ctf_id_t1072ctf_add_volatile(ctf_file_t *fp, uint_t flag, ctf_id_t ref)1073{1074return (ctf_add_reftype(fp, flag, ref, CTF_K_VOLATILE));1075}10761077ctf_id_t1078ctf_add_const(ctf_file_t *fp, uint_t flag, ctf_id_t ref)1079{1080return (ctf_add_reftype(fp, flag, ref, CTF_K_CONST));1081}10821083ctf_id_t1084ctf_add_restrict(ctf_file_t *fp, uint_t flag, ctf_id_t ref)1085{1086return (ctf_add_reftype(fp, flag, ref, CTF_K_RESTRICT));1087}10881089int1090ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)1091{1092ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);1093ctf_dmdef_t *dmd;10941095uint_t kind, vlen, root;1096char *s;10971098if (name == NULL)1099return (ctf_set_errno(fp, EINVAL));11001101if (!(fp->ctf_flags & LCTF_RDWR))1102return (ctf_set_errno(fp, ECTF_RDONLY));11031104if (dtd == NULL)1105return (ctf_set_errno(fp, ECTF_BADID));11061107kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);1108root = LCTF_INFO_ROOT(fp, dtd->dtd_data.ctt_info);1109vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);11101111if (kind != CTF_K_ENUM)1112return (ctf_set_errno(fp, ECTF_NOTENUM));11131114if (vlen > LCTF_MAX_VLEN(fp))1115return (ctf_set_errno(fp, ECTF_DTFULL));11161117for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);1118dmd != NULL; dmd = ctf_list_next(dmd)) {1119if (strcmp(dmd->dmd_name, name) == 0)1120return (ctf_set_errno(fp, ECTF_DUPMEMBER));1121}11221123if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)1124return (ctf_set_errno(fp, EAGAIN));11251126if ((s = ctf_strdup(name)) == NULL) {1127ctf_free(dmd, sizeof (ctf_dmdef_t));1128return (ctf_set_errno(fp, EAGAIN));1129}11301131dmd->dmd_name = s;1132dmd->dmd_type = CTF_ERR;1133dmd->dmd_offset = 0;1134dmd->dmd_value = value;11351136dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, root, vlen + 1);1137ctf_list_append(&dtd->dtd_u.dtu_members, dmd);11381139fp->ctf_dtstrlen += strlen(s) + 1;1140fp->ctf_flags |= LCTF_DIRTY;11411142return (0);1143}11441145int1146ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type)1147{1148ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);1149ctf_dmdef_t *dmd;11501151ssize_t msize, malign, ssize;1152uint_t kind, vlen, root;1153char *s = NULL;11541155if (!(fp->ctf_flags & LCTF_RDWR))1156return (ctf_set_errno(fp, ECTF_RDONLY));11571158if (dtd == NULL)1159return (ctf_set_errno(fp, ECTF_BADID));11601161kind = LCTF_INFO_KIND(fp, dtd->dtd_data.ctt_info);1162root = LCTF_INFO_ROOT(fp, dtd->dtd_data.ctt_info);1163vlen = LCTF_INFO_VLEN(fp, dtd->dtd_data.ctt_info);11641165if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)1166return (ctf_set_errno(fp, ECTF_NOTSOU));11671168if (vlen > LCTF_MAX_VLEN(fp))1169return (ctf_set_errno(fp, ECTF_DTFULL));11701171if (name != NULL) {1172for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);1173dmd != NULL; dmd = ctf_list_next(dmd)) {1174if (dmd->dmd_name != NULL &&1175strcmp(dmd->dmd_name, name) == 0)1176return (ctf_set_errno(fp, ECTF_DUPMEMBER));1177}1178}11791180if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||1181(malign = ctf_type_align(fp, type)) == CTF_ERR)1182return (CTF_ERR); /* errno is set for us */11831184if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)1185return (ctf_set_errno(fp, EAGAIN));11861187if (name != NULL && (s = ctf_strdup(name)) == NULL) {1188ctf_free(dmd, sizeof (ctf_dmdef_t));1189return (ctf_set_errno(fp, EAGAIN));1190}11911192dmd->dmd_name = s;1193dmd->dmd_type = type;1194dmd->dmd_value = -1;11951196if (kind == CTF_K_STRUCT && vlen != 0) {1197ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);1198ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);1199size_t off = lmd->dmd_offset;12001201ctf_encoding_t linfo;1202ssize_t lsize;12031204if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)1205off += linfo.cte_bits;1206else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)1207off += lsize * NBBY;12081209/*1210* Round up the offset of the end of the last member to the1211* next byte boundary, convert 'off' to bytes, and then round1212* it up again to the next multiple of the alignment required1213* by the new member. Finally, convert back to bits and store1214* the result in dmd_offset. Technically we could do more1215* efficient packing if the new member is a bit-field, but1216* we're the "compiler" and ANSI says we can do as we choose.1217*/1218off = roundup(off, NBBY) / NBBY;1219off = roundup(off, MAX(malign, 1));1220dmd->dmd_offset = off * NBBY;1221ssize = off + msize;1222} else {1223dmd->dmd_offset = 0;1224ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);1225ssize = MAX(ssize, msize);1226}12271228if (ssize > LCTF_MAX_SIZE(fp)) {1229dtd->dtd_data.ctt_size = LCTF_LSIZE_SENT(fp);1230dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);1231dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);1232} else1233dtd->dtd_data.ctt_size = ssize;12341235dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(fp, kind, root, vlen + 1);1236ctf_list_append(&dtd->dtd_u.dtu_members, dmd);12371238if (s != NULL)1239fp->ctf_dtstrlen += strlen(s) + 1;12401241ctf_ref_inc(fp, type);1242fp->ctf_flags |= LCTF_DIRTY;1243return (0);1244}12451246/*1247* This removes a type from the dynamic section. This will fail if the type is1248* referenced by another type. Note that the CTF ID is never reused currently by1249* CTF. Note that if this container is a parent container then we just outright1250* refuse to remove the type. There currently is no notion of searching for the1251* ctf_dtdef_t in parent containers. If there is, then this constraint could1252* become finer grained.1253*/1254int1255ctf_delete_type(ctf_file_t *fp, ctf_id_t type)1256{1257ctf_file_t *fpd;1258ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, type);12591260if (!(fp->ctf_flags & LCTF_RDWR))1261return (ctf_set_errno(fp, ECTF_RDONLY));12621263/*1264* We want to give as useful an errno as possible. That means that we1265* want to distinguish between a type which does not exist and one for1266* which the type is not dynamic.1267*/1268fpd = fp;1269if (ctf_lookup_by_id(&fpd, type) == NULL &&1270ctf_dtd_lookup(fp, type) == NULL)1271return (CTF_ERR); /* errno is set for us */12721273if (dtd == NULL)1274return (ctf_set_errno(fp, ECTF_NOTDYN));12751276if (dtd->dtd_ref != 0 || fp->ctf_refcnt > 1)1277return (ctf_set_errno(fp, ECTF_REFERENCED));12781279ctf_dtd_delete(fp, dtd);1280fp->ctf_flags |= LCTF_DIRTY;1281return (0);1282}12831284static int1285enumcmp(const char *name, int value, void *arg)1286{1287ctf_bundle_t *ctb = arg;1288int bvalue;12891290return (ctf_enum_value(ctb->ctb_file, ctb->ctb_type,1291name, &bvalue) == CTF_ERR || value != bvalue);1292}12931294static int1295enumadd(const char *name, int value, void *arg)1296{1297ctf_bundle_t *ctb = arg;12981299return (ctf_add_enumerator(ctb->ctb_file, ctb->ctb_type,1300name, value) == CTF_ERR);1301}13021303static int1304membadd(const char *name, ctf_id_t type, ulong_t offset, void *arg)1305{1306ctf_bundle_t *ctb = arg;1307ctf_dmdef_t *dmd;1308char *s = NULL;13091310if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)1311return (ctf_set_errno(ctb->ctb_file, EAGAIN));13121313if (name != NULL && *name != '\0' && (s = ctf_strdup(name)) == NULL) {1314ctf_free(dmd, sizeof (ctf_dmdef_t));1315return (ctf_set_errno(ctb->ctb_file, EAGAIN));1316}13171318/*1319* For now, dmd_type is copied as the src_fp's type; it is reset to an1320* equivalent dst_fp type by a final loop in ctf_add_type(), below.1321*/1322dmd->dmd_name = s;1323dmd->dmd_type = type;1324dmd->dmd_offset = offset;1325dmd->dmd_value = -1;13261327ctf_list_append(&ctb->ctb_dtd->dtd_u.dtu_members, dmd);13281329if (s != NULL)1330ctb->ctb_file->ctf_dtstrlen += strlen(s) + 1;13311332ctb->ctb_file->ctf_flags |= LCTF_DIRTY;1333return (0);1334}13351336static long1337soucmp(ctf_file_t *src_fp, ctf_id_t src_type, ctf_file_t *dst_fp,1338ctf_id_t dst_type)1339{1340const void *src_tp, *dst_tp;1341const char *src_name, *dst_name;1342ssize_t src_sz, dst_sz, src_inc, dst_inc;1343uint_t dst_kind, dst_vlen, src_kind, src_vlen, n;13441345if ((src_type = ctf_type_resolve(src_fp, src_type)) == CTF_ERR)1346return (CTF_ERR);1347if ((dst_type = ctf_type_resolve(dst_fp, dst_type)) == CTF_ERR)1348return (CTF_ERR);13491350if ((src_tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)1351return (CTF_ERR);1352if ((dst_tp = ctf_lookup_by_id(&dst_fp, dst_type)) == NULL)1353return (CTF_ERR);13541355ctf_get_ctt_info(src_fp, src_tp, &src_kind, &src_vlen, NULL);1356ctf_get_ctt_info(dst_fp, dst_tp, &dst_kind, &dst_vlen, NULL);13571358if (src_kind != dst_kind)1359return (ctf_set_errno(dst_fp, ECTF_CONFLICT));1360if (src_kind != CTF_K_STRUCT && src_kind != CTF_K_UNION)1361return (ctf_set_errno(dst_fp, ECTF_CONFLICT));1362if (src_vlen != dst_vlen)1363return (ctf_set_errno(dst_fp, ECTF_CONFLICT));13641365(void) ctf_get_ctt_size(src_fp, src_tp, &src_sz, &src_inc);1366(void) ctf_get_ctt_size(dst_fp, dst_tp, &dst_sz, &dst_inc);1367if (src_sz != dst_sz)1368return (ctf_set_errno(dst_fp, ECTF_CONFLICT));13691370const char *src_mp, *dst_mp;1371ulong_t src_offset, dst_offset;13721373src_mp = (const char *)src_tp + src_inc;1374dst_mp = (const char *)dst_tp + dst_inc;1375for (n = src_vlen; n != 0;1376n--, src_mp += src_inc, dst_mp += dst_inc) {1377ctf_get_ctm_info(src_fp, src_mp, src_sz, &src_inc, NULL,1378&src_offset, &src_name);1379ctf_get_ctm_info(dst_fp, dst_mp, dst_sz, &dst_inc, NULL,1380&dst_offset, &dst_name);13811382if (src_offset != dst_offset)1383return (ctf_set_errno(dst_fp, ECTF_CONFLICT));1384if (strcmp(src_name, dst_name) != 0)1385return (ctf_set_errno(dst_fp, ECTF_CONFLICT));1386}13871388return (0);1389}13901391/*1392* The ctf_add_type routine is used to copy a type from a source CTF container1393* to a dynamic destination container. This routine operates recursively by1394* following the source type's links and embedded member types. If the1395* destination container already contains a named type which has the same1396* attributes, then we succeed and return this type but no changes occur.1397*/1398ctf_id_t1399ctf_add_type(ctf_file_t *dst_fp, ctf_file_t *src_fp, ctf_id_t src_type)1400{1401ctf_id_t dst_type = CTF_ERR;1402uint_t dst_kind = CTF_K_UNKNOWN;14031404const void *tp;1405const char *name;1406uint_t type, kind, flag, vlen;14071408ctf_bundle_t src, dst;1409ctf_encoding_t src_en, main_en, dst_en;1410ctf_arinfo_t src_ar, dst_ar;14111412ctf_dtdef_t *dtd;1413ctf_funcinfo_t ctc;1414ssize_t size;14151416ctf_hash_t *hp;1417ctf_helem_t *hep;14181419if (dst_fp == src_fp)1420return (src_type);14211422if (!(dst_fp->ctf_flags & LCTF_RDWR))1423return (ctf_set_errno(dst_fp, ECTF_RDONLY));14241425if ((tp = ctf_lookup_by_id(&src_fp, src_type)) == NULL)1426return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));14271428name = ctf_type_rname(src_fp, tp);14291430ctf_get_ctt_info(src_fp, tp, &kind, &vlen, &flag);14311432switch (kind) {1433case CTF_K_STRUCT:1434hp = &dst_fp->ctf_structs;1435break;1436case CTF_K_UNION:1437hp = &dst_fp->ctf_unions;1438break;1439case CTF_K_ENUM:1440hp = &dst_fp->ctf_enums;1441break;1442default:1443hp = &dst_fp->ctf_names;1444break;1445}14461447/*1448* If the source type has a name and is a root type (visible at the1449* top-level scope), lookup the name in the destination container and1450* verify that it is of the same kind before we do anything else.1451*/1452if ((flag & CTF_ADD_ROOT) && name[0] != '\0' &&1453(hep = ctf_hash_lookup(hp, dst_fp, name, strlen(name))) != NULL) {1454dst_type = (ctf_id_t)hep->h_type;1455dst_kind = ctf_type_kind(dst_fp, dst_type);1456}14571458/*1459* If an identically named dst_type exists, fail with ECTF_CONFLICT1460* unless dst_type is a forward declaration and src_type is a struct,1461* union, or enum (i.e. the definition of the previous forward decl).1462*/1463if (dst_type != CTF_ERR && dst_kind != kind) {1464if (dst_kind != CTF_K_FORWARD || (kind != CTF_K_ENUM &&1465kind != CTF_K_STRUCT && kind != CTF_K_UNION))1466return (ctf_set_errno(dst_fp, ECTF_CONFLICT));1467else1468dst_type = CTF_ERR;1469}14701471/*1472* If the non-empty name was not found in the appropriate hash, search1473* the list of pending dynamic definitions that are not yet committed.1474* If a matching name and kind are found, assume this is the type that1475* we are looking for. This is necessary to permit ctf_add_type() to1476* operate recursively on entities such as a struct that contains a1477* pointer member that refers to the same struct type.1478*1479* In the case of integer and floating point types, we match using the1480* type encoding as well - else we may incorrectly return a bitfield1481* type, for instance.1482*/1483if (dst_type == CTF_ERR && name[0] != '\0') {1484for (dtd = ctf_list_prev(&dst_fp->ctf_dtdefs); dtd != NULL &&1485LCTF_TYPE_TO_INDEX(dst_fp, dtd->dtd_type) >1486dst_fp->ctf_dtoldid; dtd = ctf_list_prev(dtd)) {1487if (LCTF_INFO_KIND(dst_fp, dtd->dtd_data.ctt_info) !=1488kind || dtd->dtd_name == NULL ||1489strcmp(dtd->dtd_name, name) != 0)1490continue;1491if (kind == CTF_K_INTEGER || kind == CTF_K_FLOAT) {1492if (ctf_type_encoding(src_fp, src_type,1493&src_en) != 0)1494continue;1495if (bcmp(&src_en, &dtd->dtd_u.dtu_enc,1496sizeof (ctf_encoding_t)) != 0)1497continue;1498}1499return (dtd->dtd_type);1500}1501}15021503src.ctb_file = src_fp;1504src.ctb_type = src_type;1505src.ctb_dtd = NULL;15061507dst.ctb_file = dst_fp;1508dst.ctb_type = dst_type;1509dst.ctb_dtd = NULL;15101511/*1512* Now perform kind-specific processing. If dst_type is CTF_ERR, then1513* we add a new type with the same properties as src_type to dst_fp.1514* If dst_type is not CTF_ERR, then we verify that dst_type has the1515* same attributes as src_type. We recurse for embedded references.1516*/1517switch (kind) {1518case CTF_K_INTEGER:1519case CTF_K_FLOAT:1520if (ctf_type_encoding(src_fp, src_type, &src_en) != 0)1521return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));15221523/*1524* This could be a bitfield, and the CTF library assumes1525* intrinsics will appear before bitfields. Therefore,1526* try to copy over the intrinsic prior to copying the1527* bitfield.1528*/1529if (dst_type == CTF_ERR && name[0] != '\0' &&1530(hep = ctf_hash_lookup(&src_fp->ctf_names, src_fp, name,1531strlen(name))) != NULL &&1532src_type != (ctf_id_t)hep->h_type) {1533if (ctf_type_encoding(src_fp, (ctf_id_t)hep->h_type,1534&main_en) != 0) {1535return (ctf_set_errno(dst_fp,1536ctf_errno(src_fp)));1537}1538if (bcmp(&src_en, &main_en, sizeof (ctf_encoding_t)) &&1539ctf_add_type(dst_fp, src_fp,1540(ctf_id_t)hep->h_type) == CTF_ERR)1541return (CTF_ERR); /* errno is set for us */1542}15431544if (dst_type != CTF_ERR) {1545if (ctf_type_encoding(dst_fp, dst_type, &dst_en) != 0)1546return (CTF_ERR); /* errno is set for us */15471548if (bcmp(&src_en, &dst_en, sizeof (ctf_encoding_t)))1549return (ctf_set_errno(dst_fp, ECTF_CONFLICT));15501551} else if (kind == CTF_K_INTEGER) {1552dst_type = ctf_add_integer(dst_fp, flag, name, &src_en);1553} else1554dst_type = ctf_add_float(dst_fp, flag, name, &src_en);1555break;15561557case CTF_K_POINTER:1558case CTF_K_VOLATILE:1559case CTF_K_CONST:1560case CTF_K_RESTRICT:1561src_type = ctf_type_reference(src_fp, src_type);1562src_type = ctf_add_type(dst_fp, src_fp, src_type);15631564if (src_type == CTF_ERR)1565return (CTF_ERR); /* errno is set for us */15661567dst_type = ctf_add_reftype(dst_fp, flag, src_type, kind);1568break;15691570case CTF_K_ARRAY:1571if (ctf_array_info(src_fp, src_type, &src_ar) == CTF_ERR)1572return (ctf_set_errno(dst_fp, ctf_errno(src_fp)));15731574src_ar.ctr_contents =1575ctf_add_type(dst_fp, src_fp, src_ar.ctr_contents);1576src_ar.ctr_index =1577ctf_add_type(dst_fp, src_fp, src_ar.ctr_index);1578src_ar.ctr_nelems = src_ar.ctr_nelems;15791580if (src_ar.ctr_contents == CTF_ERR ||1581src_ar.ctr_index == CTF_ERR)1582return (CTF_ERR); /* errno is set for us */15831584if (dst_type != CTF_ERR) {1585if (ctf_array_info(dst_fp, dst_type, &dst_ar) != 0)1586return (CTF_ERR); /* errno is set for us */15871588if (bcmp(&src_ar, &dst_ar, sizeof (ctf_arinfo_t)))1589return (ctf_set_errno(dst_fp, ECTF_CONFLICT));1590} else1591dst_type = ctf_add_array(dst_fp, flag, &src_ar);1592break;15931594case CTF_K_FUNCTION:1595ctf_get_ctt_index(src_fp, tp, NULL, &type, NULL);1596ctc.ctc_return = ctf_add_type(dst_fp, src_fp, type);1597ctc.ctc_argc = 0;1598ctc.ctc_flags = 0;15991600if (ctc.ctc_return == CTF_ERR)1601return (CTF_ERR); /* errno is set for us */16021603dst_type = ctf_add_function(dst_fp, flag, &ctc, NULL);1604break;16051606case CTF_K_STRUCT:1607case CTF_K_UNION: {1608ctf_dmdef_t *dmd;1609int errs = 0;16101611if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {1612/*1613* Compare the sizes and fields of the two types.1614* The field comparisons only check the names and1615* offsets, so this is not perfect but is good enough1616* for scenarios that we care about.1617*/1618if (soucmp(src_fp, src_type, dst_fp, dst_type) != 0)1619return (CTF_ERR); /* errno is set for us */1620break;1621}16221623/*1624* Unlike the other cases, copying structs and unions is done1625* manually so as to avoid repeated lookups in ctf_add_member1626* and to ensure the exact same member offsets as in src_type.1627*/1628dst_type = ctf_add_generic(dst_fp, flag, name, &dtd);1629if (dst_type == CTF_ERR)1630return (CTF_ERR); /* errno is set for us */16311632dst.ctb_type = dst_type;1633dst.ctb_dtd = dtd;16341635if (ctf_member_iter(src_fp, src_type, membadd, &dst) != 0)1636errs++; /* increment errs and fail at bottom of case */16371638if ((size = ctf_type_size(src_fp, src_type)) >1639LCTF_MAX_SIZE(src_fp)) {1640dtd->dtd_data.ctt_size = LCTF_LSIZE_SENT(dst_fp);1641dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(size);1642dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(size);1643} else1644dtd->dtd_data.ctt_size = size;16451646dtd->dtd_data.ctt_info = LCTF_TYPE_INFO(dst_fp, kind, flag,1647vlen);16481649/*1650* Make a final pass through the members changing each dmd_type1651* (a src_fp type) to an equivalent type in dst_fp. We pass1652* through all members, leaving any that fail set to CTF_ERR.1653*/1654for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);1655dmd != NULL; dmd = ctf_list_next(dmd)) {1656if ((dmd->dmd_type = ctf_add_type(dst_fp, src_fp,1657dmd->dmd_type)) == CTF_ERR)1658errs++;1659}16601661if (errs)1662return (CTF_ERR); /* errno is set for us */16631664/*1665* Now that we know that we can't fail, we go through and bump1666* all the reference counts on the member types.1667*/1668for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);1669dmd != NULL; dmd = ctf_list_next(dmd))1670ctf_ref_inc(dst_fp, dmd->dmd_type);1671break;1672}16731674case CTF_K_ENUM:1675if (dst_type != CTF_ERR && dst_kind != CTF_K_FORWARD) {1676if (ctf_enum_iter(src_fp, src_type, enumcmp, &dst) ||1677ctf_enum_iter(dst_fp, dst_type, enumcmp, &src))1678return (ctf_set_errno(dst_fp, ECTF_CONFLICT));1679} else {1680dst_type = ctf_add_enum(dst_fp, flag, name);1681if ((dst.ctb_type = dst_type) == CTF_ERR ||1682ctf_enum_iter(src_fp, src_type, enumadd, &dst))1683return (CTF_ERR); /* errno is set for us */1684}1685break;16861687case CTF_K_FORWARD:1688if (dst_type == CTF_ERR) {1689dst_type = ctf_add_forward(dst_fp,1690flag, name, CTF_K_STRUCT); /* assume STRUCT */1691}1692break;16931694case CTF_K_TYPEDEF:1695src_type = ctf_type_reference(src_fp, src_type);1696src_type = ctf_add_type(dst_fp, src_fp, src_type);16971698if (src_type == CTF_ERR)1699return (CTF_ERR); /* errno is set for us */17001701/*1702* If dst_type is not CTF_ERR at this point, we should check if1703* ctf_type_reference(dst_fp, dst_type) != src_type and if so1704* fail with ECTF_CONFLICT. However, this causes problems with1705* <sys/types.h> typedefs that vary based on things like if1706* _ILP32x then pid_t is int otherwise long. We therefore omit1707* this check and assume that if the identically named typedef1708* already exists in dst_fp, it is correct or equivalent.1709*/1710if (dst_type == CTF_ERR) {1711dst_type = ctf_add_typedef(dst_fp, flag,1712name, src_type);1713}1714break;17151716default:1717return (ctf_set_errno(dst_fp, ECTF_CORRUPT));1718}17191720return (dst_type);1721}172217231724