ctf_create.c revision e083a0c2c99cea982dcf8e12ec3452cc575b5663
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/sysmacros.h>
#include <ctf_impl.h>
/*
* This static string is used as the template for initially populating a
* dynamic container's string table. We always store \0 in the first byte,
* and we use the generic string "PARENT" to mark this container's parent
* if one is associated with the container using ctf_import().
*/
static const char _CTF_STRTAB_TEMPLATE[] = "\0PARENT";
/*
* To create an empty CTF container, we just declare a zeroed header and call
* ctf_bufopen() on it. If ctf_bufopen succeeds, we mark the new container r/w
* and initialize the dynamic members. We set dtstrlen to 1 to reserve the
* first byte of the string table for a \0 byte, and we start assigning type
* IDs at 1 because type ID 0 is used as a sentinel.
*/
ctf_create(int *errp)
{
ctf_file_t *fp;
cts.cts_offset = 0;
return (NULL);
}
fp->ctf_dtoldid = 0;
return (fp);
}
static uchar_t *
{
} else
t += sizeof (ctm);
}
return (t);
}
static uchar_t *
{
} else
t += sizeof (ctlm);
}
return (t);
}
static uchar_t *
{
t += sizeof (cte);
}
return (t);
}
static uchar_t *
{
continue; /* skip anonymous members */
s += len;
}
return (s);
}
/*
* If the specified CTF container is writable and has been modified, reload
* this container with the updated type definitions. In order to make this
* code and the rest of libctf as simple as possible, we perform updates by
* taking the dynamic type definitions and creating an in-memory CTF file
* containing the definitions, and then call ctf_bufopen() on it. This not
* only leverages ctf_bufopen(), but also avoids having to bifurcate the rest
* of the library code with different lookup paths for static and dynamic
* type definitions. We are therefore optimizing greatly for lookup over
* update, which we assume will be an uncommon operation. We perform one
* extra trick here for the benefit of callers and to keep our code simple:
* ctf_bufopen() will return a new ctf_file_t, but we want to keep the fp
* constant for the caller, so after ctf_bufopen() returns, we use bcopy to
* swap the interior of the old and new ctf_file_t's, and then free the old.
*/
int
{
void *buf;
int err;
return (0); /* no update required */
/*
* Fill in an initial CTF header. We will leave the label, object,
* and function sections empty and only output a header, type section,
* and string table. The type section begins at a 4-byte aligned
* boundary past the CTF header itself (at relative offset zero).
*/
/*
* Iterate through the dynamic type definition list and compute the
* size of the CTF type section we will need to generate.
*/
size += sizeof (ctf_stype_t);
else
size += sizeof (ctf_type_t);
switch (kind) {
case CTF_K_INTEGER:
case CTF_K_FLOAT:
break;
case CTF_K_ARRAY:
size += sizeof (ctf_array_t);
break;
case CTF_K_FUNCTION:
break;
case CTF_K_STRUCT:
case CTF_K_UNION:
else
break;
case CTF_K_ENUM:
break;
}
}
/*
* Fill in the string table offset and size, compute the size of the
* entire CTF buffer we need, and then allocate a new buffer and
* bcopy the finished header to the start of the buffer.
*/
s += sizeof (_CTF_STRTAB_TEMPLATE);
/*
* We now take a final lap through the dynamic type definition list and
* copy the appropriate type records and strings to the output buffer.
*/
s += len;
} else
len = sizeof (ctf_stype_t);
else
len = sizeof (ctf_type_t);
t += len;
switch (kind) {
case CTF_K_INTEGER:
case CTF_K_FLOAT:
if (kind == CTF_K_INTEGER) {
} else {
}
t += sizeof (encoding);
break;
case CTF_K_ARRAY:
t += sizeof (cta);
break;
case CTF_K_FUNCTION: {
if (vlen & 1)
*argv++ = 0; /* pad to 4-byte boundary */
break;
}
case CTF_K_STRUCT:
case CTF_K_UNION:
else
s = ctf_copy_membnames(dtd, s);
break;
case CTF_K_ENUM:
s = ctf_copy_membnames(dtd, s);
break;
}
}
/*
* Finally, we are ready to ctf_bufopen() the new container. If this
* is successful, we then switch nfp and fp and free the old container.
*/
cts.cts_offset = 0;
}
fp->ctf_dthashlen = 0;
/*
* Initialize the ctf_lookup_by_name top-level dictionary. We keep an
* array of type name prefixes and the corresponding ctf_hash to use.
* NOTE: This code must be kept in sync with the code in ctf_bufopen().
*/
return (0);
}
void
{
}
void
{
if (p != dtd)
q = &p->dtd_hash;
else
break;
}
if (p != NULL)
*q = p->dtd_hash;
case CTF_K_STRUCT:
case CTF_K_UNION:
case CTF_K_ENUM:
}
}
break;
case CTF_K_FUNCTION:
break;
}
}
}
{
return (NULL);
break;
}
return (dtd);
}
/*
* Discard all of the dynamic type definitions that have been added to the
* container since the last call to ctf_update(). We locate such types by
* scanning the list and deleting elements that have type IDs greater than
* ctf_dtoldid, which is set by ctf_update(), above.
*/
int
{
return (0); /* no update required */
continue; /* skip types that have been committed */
}
return (0);
}
static ctf_id_t
{
char *s = NULL;
}
if (s != NULL)
return (type);
}
/*
* When encoding integer sizes, we want to convert a byte count in the range
* 1-8 to the closest power of 2 (e.g. 3->4, 5->8, etc). The clp2() function
* is a clever implementation from "Hacker's Delight" by Henry Warren, Jr.
*/
static size_t
{
x--;
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return (x + 1);
}
static ctf_id_t
{
return (CTF_ERR); /* errno is set for us */
return (type);
}
static ctf_id_t
{
return (CTF_ERR); /* errno is set for us */
return (type);
}
{
}
{
}
{
}
{
return (CTF_ERR); /* errno is set for us */
return (type);
}
int
{
return (0);
}
{
vlen++; /* add trailing zero to indicate varargs (see below) */
if (vlen > CTF_MAX_VLEN)
return (CTF_ERR); /* errno is set for us */
}
return (type);
}
{
return (CTF_ERR); /* errno is set for us */
return (type);
}
{
return (CTF_ERR); /* errno is set for us */
return (type);
}
{
return (CTF_ERR); /* errno is set for us */
return (type);
}
{
ctf_hash_t *hp;
switch (kind) {
case CTF_K_STRUCT:
break;
case CTF_K_UNION:
break;
case CTF_K_ENUM:
break;
default:
}
/*
* If the type is already defined or exists as a forward tag, just
* return the ctf_id_t of the existing definition.
*/
return (CTF_ERR); /* errno is set for us */
return (type);
}
{
return (CTF_ERR); /* errno is set for us */
return (type);
}
{
}
{
}
{
}
int
{
char *s;
if (kind != CTF_K_ENUM)
if (vlen == CTF_MAX_VLEN)
}
}
dmd->dmd_offset = 0;
return (0);
}
int
{
char *s = NULL;
if (vlen == CTF_MAX_VLEN)
}
}
return (CTF_ERR); /* errno is set for us */
}
/*
* Round up the offset of the end of the last member to the
* next byte boundary, convert 'off' to bytes, and then round
* it up again to the next multiple of the alignment required
* by the new member. Finally, convert back to bits and store
* the result in dmd_offset. Technically we could do more
* efficient packing if the new member is a bit-field, but
* we're the "compiler" and ANSI says we can do as we choose.
*/
} else {
dmd->dmd_offset = 0;
}
if (ssize > CTF_MAX_SIZE) {
} else
if (s != NULL)
return (0);
}
static int
{
int bvalue;
}
static int
{
}
/*ARGSUSED*/
static int
{
}
static int
{
char *s = NULL;
}
/*
* For now, dmd_type is copied as the src_fp's type; it is reset to an
* equivalent dst_fp type by a final loop in ctf_add_type(), below.
*/
if (s != NULL)
return (0);
}
/*
* The ctf_add_type routine is used to copy a type from a source CTF container
* to a dynamic destination container. This routine operates recursively by
* following the source type's links and embedded member types. If the
* destination container already contains a named type which has the same
* attributes, then we succeed and return this type but no changes occur.
*/
{
const ctf_type_t *tp;
const char *name;
ctf_hash_t *hp;
switch (kind) {
case CTF_K_STRUCT:
break;
case CTF_K_UNION:
break;
case CTF_K_ENUM:
break;
default:
break;
}
/*
* If the source type has a name and is a root type (visible at the
* top-level scope), lookup the name in the destination container and
* verify that it is of the same kind before we do anything else.
*/
}
/*
* If an identically named dst_type exists, fail with ECTF_CONFLICT
* unless dst_type is a forward declaration and src_type is a struct,
* union, or enum (i.e. the definition of the previous forward decl).
*/
/*
* If the non-empty name was not found in the appropriate hash, search
* the list of pending dynamic definitions that are not yet committed.
* If a matching name and kind are found, assume this is the type that
* we are looking for. This is necessary to permit ctf_add_type() to
* operate recursively on entities such as a struct that contains a
* pointer member that refers to the same struct type.
*/
}
}
/*
* Now perform kind-specific processing. If dst_type is CTF_ERR, then
* we add a new type with the same properties as src_type to dst_fp.
* If dst_type is not CTF_ERR, then we verify that dst_type has the
* same attributes as src_type. We recurse for embedded references.
*/
switch (kind) {
case CTF_K_INTEGER:
case CTF_K_FLOAT:
return (CTF_ERR); /* errno is set for us */
} else if (kind == CTF_K_INTEGER) {
} else
break;
case CTF_K_POINTER:
case CTF_K_VOLATILE:
case CTF_K_CONST:
case CTF_K_RESTRICT:
return (CTF_ERR); /* errno is set for us */
break;
case CTF_K_ARRAY:
return (CTF_ERR); /* errno is set for us */
return (CTF_ERR); /* errno is set for us */
} else
break;
case CTF_K_FUNCTION:
return (CTF_ERR); /* errno is set for us */
break;
case CTF_K_STRUCT:
case CTF_K_UNION: {
int errs = 0;
/*
* Technically to match a struct or union we need to check both
* ways (src members vs. dst, dst members vs. src) but we make
* this more optimal by only checking src vs. dst and comparing
* the total size of the structure (which we must do anyway)
* which covers the possibility of dst members not in src.
* This optimization can be defeated for unions, but is so
* pathological as to render it irrelevant for our purposes.
*/
break;
}
/*
* Unlike the other cases, copying structs and unions is done
* manually so as to avoid repeated lookups in ctf_add_member
* and to ensure the exact same member offsets as in src_type.
*/
return (CTF_ERR); /* errno is set for us */
errs++; /* increment errs and fail at bottom of case */
} else
/*
* Make a final pass through the members changing each dmd_type
* (a src_fp type) to an equivalent type in dst_fp. We pass
* through all members, leaving any that fail set to CTF_ERR.
*/
errs++;
}
if (errs)
return (CTF_ERR); /* errno is set for us */
break;
}
case CTF_K_ENUM:
} else {
return (CTF_ERR); /* errno is set for us */
}
break;
case CTF_K_FORWARD:
}
break;
case CTF_K_TYPEDEF:
return (CTF_ERR); /* errno is set for us */
/*
* If dst_type is not CTF_ERR at this point, we should check if
* ctf_type_reference(dst_fp, dst_type) != src_type and if so
* fail with ECTF_CONFLICT. However, this causes problems with
* _ILP32x then pid_t is int otherwise long. We therefore omit
* this check and assume that if the identically named typedef
* already exists in dst_fp, it is correct or equivalent.
*/
}
break;
default:
}
return (dst_type);
}