Beispiel #1
0
Datei: ctf.c Projekt: DataIX/src
static int
count_types(ctf_header_t *h, caddr_t data)
{
	caddr_t dptr = data + h->cth_typeoff;
	int count = 0;

	dptr = data + h->cth_typeoff;
	while (dptr < data + h->cth_stroff) {
		void *v = (void *) dptr;
		ctf_type_t *ctt = v;
		size_t vlen = CTF_INFO_VLEN(ctt->ctt_info);
		size_t size, increment;

		get_ctt_size(ctt, &size, &increment);

		switch (CTF_INFO_KIND(ctt->ctt_info)) {
		case CTF_K_INTEGER:
		case CTF_K_FLOAT:
			dptr += 4;
			break;
		case CTF_K_POINTER:
		case CTF_K_FORWARD:
		case CTF_K_TYPEDEF:
		case CTF_K_VOLATILE:
		case CTF_K_CONST:
		case CTF_K_RESTRICT:
		case CTF_K_FUNCTION:
			dptr += sizeof (ushort_t) * (vlen + (vlen & 1));
			break;
		case CTF_K_ARRAY:
			dptr += sizeof (ctf_array_t);
			break;
		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (size < CTF_LSTRUCT_THRESH)
				dptr += sizeof (ctf_member_t) * vlen;
			else
				dptr += sizeof (ctf_lmember_t) * vlen;
			break;
		case CTF_K_ENUM:
			dptr += sizeof (ctf_enum_t) * vlen;
			break;
		case CTF_K_UNKNOWN:
			break;
		default:
			parseterminate("Unknown CTF type %d (#%d) at %#x",
			    CTF_INFO_KIND(ctt->ctt_info), count, dptr - data);
		}

		dptr += increment;
		count++;
	}

	debug(3, "CTF read %d types\n", count);

	return (count);
}
Beispiel #2
0
int
ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
{
	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
	ctf_dmdef_t *dmd;

	uint_t kind, vlen, root;
	char *s;

	if (name == NULL)
		return (ctf_set_errno(fp, EINVAL));

	if (!(fp->ctf_flags & LCTF_RDWR))
		return (ctf_set_errno(fp, ECTF_RDONLY));

	if (dtd == NULL)
		return (ctf_set_errno(fp, ECTF_BADID));

	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);

	if (kind != CTF_K_ENUM)
		return (ctf_set_errno(fp, ECTF_NOTENUM));

	if (vlen == CTF_MAX_VLEN)
		return (ctf_set_errno(fp, ECTF_DTFULL));

	for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
	    dmd != NULL; dmd = ctf_list_next(dmd)) {
		if (strcmp(dmd->dmd_name, name) == 0)
			return (ctf_set_errno(fp, ECTF_DUPMEMBER));
	}

	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
		return (ctf_set_errno(fp, EAGAIN));

	if ((s = ctf_strdup(name)) == NULL) {
		ctf_free(dmd, sizeof (ctf_dmdef_t));
		return (ctf_set_errno(fp, EAGAIN));
	}

	dmd->dmd_name = s;
	dmd->dmd_type = CTF_ERR;
	dmd->dmd_offset = 0;
	dmd->dmd_value = value;

	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);

	fp->ctf_dtstrlen += strlen(s) + 1;
	fp->ctf_flags |= LCTF_DIRTY;

	return (0);
}
Beispiel #3
0
void
ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
{
	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
	ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
	ctf_dmdef_t *dmd, *nmd;
	size_t len;

	for (p = *q; p != NULL; p = p->dtd_hash) {
		if (p != dtd)
			q = &p->dtd_hash;
		else
			break;
	}

	if (p != NULL)
		*q = p->dtd_hash;

	switch (CTF_INFO_KIND(dtd->dtd_data.ctt_info)) {
	case CTF_K_STRUCT:
	case CTF_K_UNION:
	case CTF_K_ENUM:
		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
		    dmd != NULL; dmd = nmd) {
			if (dmd->dmd_name != NULL) {
				len = strlen(dmd->dmd_name) + 1;
				ctf_free(dmd->dmd_name, len);
				fp->ctf_dtstrlen -= len;
			}
			nmd = ctf_list_next(dmd);
			ctf_free(dmd, sizeof (ctf_dmdef_t));
		}
		break;
	case CTF_K_FUNCTION:
		ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
		    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
		break;
	}

	if (dtd->dtd_name) {
		len = strlen(dtd->dtd_name) + 1;
		ctf_free(dtd->dtd_name, len);
		fp->ctf_dtstrlen -= len;
	}

	ctf_list_delete(&fp->ctf_dtdefs, dtd);
	ctf_free(dtd, sizeof (ctf_dtdef_t));
}
Beispiel #4
0
static ushort_t
get_vlen_v2(ushort_t info)
{
	return (CTF_INFO_VLEN(info));
}
Beispiel #5
0
static int
fbt_typoff_init(linker_ctf_t *lc)
{
	const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
	const ctf_type_t *tbuf;
	const ctf_type_t *tend;
	const ctf_type_t *tp;
	const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
	int ctf_typemax = 0;
	uint32_t *xp;
	ulong_t pop[CTF_K_MAX + 1] = { 0 };


	/* Sanity check. */
	if (hp->cth_magic != CTF_MAGIC)
		return (EINVAL);

	tbuf = (const ctf_type_t *) (ctfdata + hp->cth_typeoff);
	tend = (const ctf_type_t *) (ctfdata + hp->cth_stroff);

	int child = hp->cth_parname != 0;

	/*
	 * We make two passes through the entire type section.  In this first
	 * pass, we count the number of each type and the total number of types.
	 */
	for (tp = tbuf; tp < tend; ctf_typemax++) {
		ushort_t kind = CTF_INFO_KIND(tp->ctt_info);
		ulong_t vlen = CTF_INFO_VLEN(tp->ctt_info);
		ssize_t size, increment;

		size_t vbytes;
		uint_t n;

		(void) fbt_get_ctt_size(hp->cth_version, tp, &size, &increment);

		switch (kind) {
		case CTF_K_INTEGER:
		case CTF_K_FLOAT:
			vbytes = sizeof (uint_t);
			break;
		case CTF_K_ARRAY:
			vbytes = sizeof (ctf_array_t);
			break;
		case CTF_K_FUNCTION:
			vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
			break;
		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (size < CTF_LSTRUCT_THRESH) {
				ctf_member_t *mp = (ctf_member_t *)
				    ((uintptr_t)tp + increment);

				vbytes = sizeof (ctf_member_t) * vlen;
				for (n = vlen; n != 0; n--, mp++)
					child |= CTF_TYPE_ISCHILD(mp->ctm_type);
			} else {
				ctf_lmember_t *lmp = (ctf_lmember_t *)
				    ((uintptr_t)tp + increment);

				vbytes = sizeof (ctf_lmember_t) * vlen;
				for (n = vlen; n != 0; n--, lmp++)
					child |=
					    CTF_TYPE_ISCHILD(lmp->ctlm_type);
			}
			break;
		case CTF_K_ENUM:
			vbytes = sizeof (ctf_enum_t) * vlen;
			break;
		case CTF_K_FORWARD:
			/*
			 * For forward declarations, ctt_type is the CTF_K_*
			 * kind for the tag, so bump that population count too.
			 * If ctt_type is unknown, treat the tag as a struct.
			 */
			if (tp->ctt_type == CTF_K_UNKNOWN ||
			    tp->ctt_type >= CTF_K_MAX)
				pop[CTF_K_STRUCT]++;
			else
				pop[tp->ctt_type]++;
			/*FALLTHRU*/
		case CTF_K_UNKNOWN:
			vbytes = 0;
			break;
		case CTF_K_POINTER:
		case CTF_K_TYPEDEF:
		case CTF_K_VOLATILE:
		case CTF_K_CONST:
		case CTF_K_RESTRICT:
			child |= CTF_TYPE_ISCHILD(tp->ctt_type);
			vbytes = 0;
			break;
		default:
			printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
			return (EIO);
		}
		tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
		pop[kind]++;
	}

	*lc->typlenp = ctf_typemax;

	if ((xp = malloc(sizeof(uint32_t) * ctf_typemax, M_LINKER, M_ZERO | M_WAITOK)) == NULL)
		return (ENOMEM);

	*lc->typoffp = xp;

	/* type id 0 is used as a sentinel value */
	*xp++ = 0;

	/*
	 * In the second pass, fill in the type offset.
	 */
	for (tp = tbuf; tp < tend; xp++) {
		ushort_t kind = CTF_INFO_KIND(tp->ctt_info);
		ulong_t vlen = CTF_INFO_VLEN(tp->ctt_info);
		ssize_t size, increment;

		size_t vbytes;
		uint_t n;

		(void) fbt_get_ctt_size(hp->cth_version, tp, &size, &increment);

		switch (kind) {
		case CTF_K_INTEGER:
		case CTF_K_FLOAT:
			vbytes = sizeof (uint_t);
			break;
		case CTF_K_ARRAY:
			vbytes = sizeof (ctf_array_t);
			break;
		case CTF_K_FUNCTION:
			vbytes = sizeof (ushort_t) * (vlen + (vlen & 1));
			break;
		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (size < CTF_LSTRUCT_THRESH) {
				ctf_member_t *mp = (ctf_member_t *)
				    ((uintptr_t)tp + increment);

				vbytes = sizeof (ctf_member_t) * vlen;
				for (n = vlen; n != 0; n--, mp++)
					child |= CTF_TYPE_ISCHILD(mp->ctm_type);
			} else {
				ctf_lmember_t *lmp = (ctf_lmember_t *)
				    ((uintptr_t)tp + increment);

				vbytes = sizeof (ctf_lmember_t) * vlen;
				for (n = vlen; n != 0; n--, lmp++)
					child |=
					    CTF_TYPE_ISCHILD(lmp->ctlm_type);
			}
			break;
		case CTF_K_ENUM:
			vbytes = sizeof (ctf_enum_t) * vlen;
			break;
		case CTF_K_FORWARD:
		case CTF_K_UNKNOWN:
			vbytes = 0;
			break;
		case CTF_K_POINTER:
		case CTF_K_TYPEDEF:
		case CTF_K_VOLATILE:
		case CTF_K_CONST:
		case CTF_K_RESTRICT:
			vbytes = 0;
			break;
		default:
			printf("%s(%d): detected invalid CTF kind -- %u\n", __func__, __LINE__, kind);
			return (EIO);
		}
		*xp = (uint32_t)((uintptr_t) tp - (uintptr_t) ctfdata);
		tp = (ctf_type_t *)((uintptr_t)tp + increment + vbytes);
	}

	return (0);
}
Beispiel #6
0
static int
fbt_ctfoff_init(modctl_t *lf, linker_ctf_t *lc)
{
	const Elf_Sym *symp = lc->symtab;;
	const char *name;
	const ctf_header_t *hp = (const ctf_header_t *) lc->ctftab;
	const uint8_t *ctfdata = lc->ctftab + sizeof(ctf_header_t);
	int i;
	uint32_t *ctfoff;
	uint32_t objtoff = hp->cth_objtoff;
	uint32_t funcoff = hp->cth_funcoff;
	ushort_t info;
	ushort_t vlen;

	/* Sanity check. */
	if (hp->cth_magic != CTF_MAGIC) {
		printf("Bad magic value in CTF data of '%s'\n",lf->pathname);
		return (EINVAL);
	}

	if (lc->symtab == NULL) {
		printf("No symbol table in '%s'\n",lf->pathname);
		return (EINVAL);
	}

	if ((ctfoff = malloc(sizeof(uint32_t) * lc->nsym, M_LINKER, M_WAITOK)) == NULL)
		return (ENOMEM);

	*lc->ctfoffp = ctfoff;

	for (i = 0; i < lc->nsym; i++, ctfoff++, symp++) {
		if (symp->st_name == 0 || symp->st_shndx == SHN_UNDEF) {
			*ctfoff = 0xffffffff;
			continue;
		}

		if (symp->st_name < lc->strcnt)
			name = lc->strtab + symp->st_name;
		else
			name = "(?)";

		switch (ELF_ST_TYPE(symp->st_info)) {
		case STT_OBJECT:
			if (objtoff >= hp->cth_funcoff ||
                            (symp->st_shndx == SHN_ABS && symp->st_value == 0)) {
				*ctfoff = 0xffffffff;
                                break;
                        }

                        *ctfoff = objtoff;
                        objtoff += sizeof (ushort_t);
			break;

		case STT_FUNC:
			if (funcoff >= hp->cth_typeoff) {
				*ctfoff = 0xffffffff;
				break;
			}

			*ctfoff = funcoff;

			info = *((const ushort_t *)(ctfdata + funcoff));
			vlen = CTF_INFO_VLEN(info);

			/*
			 * If we encounter a zero pad at the end, just skip it.
			 * Otherwise skip over the function and its return type
			 * (+2) and the argument list (vlen).
			 */
			if (CTF_INFO_KIND(info) == CTF_K_UNKNOWN && vlen == 0)
				funcoff += sizeof (ushort_t); /* skip pad */
			else
				funcoff += sizeof (ushort_t) * (vlen + 2);
			break;

		default:
			*ctfoff = 0xffffffff;
			break;
		}
	}

	return (0);
}
Beispiel #7
0
void
ctf_dtd_delete(ctf_file_t *fp, ctf_dtdef_t *dtd)
{
	ulong_t h = dtd->dtd_type & (fp->ctf_dthashlen - 1);
	ctf_dtdef_t *p, **q = &fp->ctf_dthash[h];
	ctf_dmdef_t *dmd, *nmd;
	size_t len;
	int kind, i;

	for (p = *q; p != NULL; p = p->dtd_hash) {
		if (p != dtd)
			q = &p->dtd_hash;
		else
			break;
	}

	if (p != NULL)
		*q = p->dtd_hash;

	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
	switch (kind) {
	case CTF_K_STRUCT:
	case CTF_K_UNION:
	case CTF_K_ENUM:
		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
		    dmd != NULL; dmd = nmd) {
			if (dmd->dmd_name != NULL) {
				len = strlen(dmd->dmd_name) + 1;
				ctf_free(dmd->dmd_name, len);
				fp->ctf_dtstrlen -= len;
			}
			if (kind != CTF_K_ENUM)
				ctf_ref_dec(fp, dmd->dmd_type);
			nmd = ctf_list_next(dmd);
			ctf_free(dmd, sizeof (ctf_dmdef_t));
		}
		break;
	case CTF_K_FUNCTION:
		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
		for (i = 0; i < CTF_INFO_VLEN(dtd->dtd_data.ctt_info); i++)
			if (dtd->dtd_u.dtu_argv[i] != 0)
				ctf_ref_dec(fp, dtd->dtd_u.dtu_argv[i]);
		ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
		    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
		break;
	case CTF_K_ARRAY:
		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_contents);
		ctf_ref_dec(fp, dtd->dtd_u.dtu_arr.ctr_index);
		break;
	case CTF_K_TYPEDEF:
		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
		break;
	case CTF_K_POINTER:
	case CTF_K_VOLATILE:
	case CTF_K_CONST:
	case CTF_K_RESTRICT:
		ctf_ref_dec(fp, dtd->dtd_data.ctt_type);
		break;
	}

	if (dtd->dtd_name) {
		len = strlen(dtd->dtd_name) + 1;
		ctf_free(dtd->dtd_name, len);
		fp->ctf_dtstrlen -= len;
	}

	ctf_list_delete(&fp->ctf_dtdefs, dtd);
	ctf_free(dtd, sizeof (ctf_dtdef_t));
}
Beispiel #8
0
/*
 * 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.
 *
 * Note that the lists of dynamic types stays around and the resulting container
 * is still writeable. Furthermore, the reference counts that are on the dtd's
 * are still valid.
 */
int
ctf_update(ctf_file_t *fp)
{
	ctf_file_t ofp, *nfp;
	ctf_header_t hdr;
	ctf_dtdef_t *dtd;
	ctf_sect_t cts;

	uchar_t *s, *s0, *t;
	size_t size;
	void *buf;
	int err;

	if (!(fp->ctf_flags & LCTF_RDWR))
		return (ctf_set_errno(fp, ECTF_RDONLY));

	if (!(fp->ctf_flags & LCTF_DIRTY))
		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).
	 */
	bzero(&hdr, sizeof (hdr));
	hdr.cth_magic = CTF_MAGIC;
	hdr.cth_version = CTF_VERSION;

	if (fp->ctf_flags & LCTF_CHILD)
		hdr.cth_parname = 1; /* i.e. _CTF_STRTAB_TEMPLATE[1] */

	/*
	 * Iterate through the dynamic type definition list and compute the
	 * size of the CTF type section we will need to generate.
	 */
	for (size = 0, dtd = ctf_list_next(&fp->ctf_dtdefs);
	    dtd != NULL; dtd = ctf_list_next(dtd)) {

		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);

		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
			size += sizeof (ctf_stype_t);
		else
			size += sizeof (ctf_type_t);

		switch (kind) {
		case CTF_K_INTEGER:
		case CTF_K_FLOAT:
			size += sizeof (uint_t);
			break;
		case CTF_K_ARRAY:
			size += sizeof (ctf_array_t);
			break;
		case CTF_K_FUNCTION:
			size += sizeof (ushort_t) * (vlen + (vlen & 1));
			break;
		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
				size += sizeof (ctf_member_t) * vlen;
			else
				size += sizeof (ctf_lmember_t) * vlen;
			break;
		case CTF_K_ENUM:
			size += sizeof (ctf_enum_t) * vlen;
			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.
	 */
	hdr.cth_stroff = hdr.cth_typeoff + size;
	hdr.cth_strlen = fp->ctf_dtstrlen;
	size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;

	if ((buf = ctf_data_alloc(size)) == MAP_FAILED)
		return (ctf_set_errno(fp, EAGAIN));

	bcopy(&hdr, buf, sizeof (ctf_header_t));
	t = (uchar_t *)buf + sizeof (ctf_header_t);
	s = s0 = (uchar_t *)buf + sizeof (ctf_header_t) + hdr.cth_stroff;

	bcopy(_CTF_STRTAB_TEMPLATE, s, sizeof (_CTF_STRTAB_TEMPLATE));
	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.
	 */
	for (dtd = ctf_list_next(&fp->ctf_dtdefs);
	    dtd != NULL; dtd = ctf_list_next(dtd)) {

		uint_t kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
		uint_t vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);

		ctf_array_t cta;
		uint_t encoding;
		size_t len;

		if (dtd->dtd_name != NULL) {
			dtd->dtd_data.ctt_name = (uint_t)(s - s0);
			len = strlen(dtd->dtd_name) + 1;
			bcopy(dtd->dtd_name, s, len);
			s += len;
		} else
			dtd->dtd_data.ctt_name = 0;

		if (dtd->dtd_data.ctt_size != CTF_LSIZE_SENT)
			len = sizeof (ctf_stype_t);
		else
			len = sizeof (ctf_type_t);

		bcopy(&dtd->dtd_data, t, len);
		t += len;

		switch (kind) {
		case CTF_K_INTEGER:
		case CTF_K_FLOAT:
			if (kind == CTF_K_INTEGER) {
				encoding = CTF_INT_DATA(
				    dtd->dtd_u.dtu_enc.cte_format,
				    dtd->dtd_u.dtu_enc.cte_offset,
				    dtd->dtd_u.dtu_enc.cte_bits);
			} else {
				encoding = CTF_FP_DATA(
				    dtd->dtd_u.dtu_enc.cte_format,
				    dtd->dtd_u.dtu_enc.cte_offset,
				    dtd->dtd_u.dtu_enc.cte_bits);
			}
			bcopy(&encoding, t, sizeof (encoding));
			t += sizeof (encoding);
			break;

		case CTF_K_ARRAY:
			cta.cta_contents = (ushort_t)
			    dtd->dtd_u.dtu_arr.ctr_contents;
			cta.cta_index = (ushort_t)
			    dtd->dtd_u.dtu_arr.ctr_index;
			cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems;
			bcopy(&cta, t, sizeof (cta));
			t += sizeof (cta);
			break;

		case CTF_K_FUNCTION: {
			ushort_t *argv = (ushort_t *)(uintptr_t)t;
			uint_t argc;

			for (argc = 0; argc < vlen; argc++)
				*argv++ = (ushort_t)dtd->dtd_u.dtu_argv[argc];

			if (vlen & 1)
				*argv++ = 0; /* pad to 4-byte boundary */

			t = (uchar_t *)argv;
			break;
		}

		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (dtd->dtd_data.ctt_size < CTF_LSTRUCT_THRESH)
				t = ctf_copy_smembers(dtd, (uint_t)(s - s0), t);
			else
				t = ctf_copy_lmembers(dtd, (uint_t)(s - s0), t);
			s = ctf_copy_membnames(dtd, s);
			break;

		case CTF_K_ENUM:
			t = ctf_copy_emembers(dtd, (uint_t)(s - s0), t);
			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.
	 */
	ctf_data_protect(buf, size);
	cts.cts_name = _CTF_SECTION;
	cts.cts_type = SHT_PROGBITS;
	cts.cts_flags = 0;
	cts.cts_data = buf;
	cts.cts_size = size;
	cts.cts_entsize = 1;
	cts.cts_offset = 0;

	if ((nfp = ctf_bufopen(&cts, NULL, NULL, &err)) == NULL) {
		ctf_data_free(buf, size);
		return (ctf_set_errno(fp, err));
	}

	(void) ctf_setmodel(nfp, ctf_getmodel(fp));
	(void) ctf_import(nfp, fp->ctf_parent);

	nfp->ctf_refcnt = fp->ctf_refcnt;
	nfp->ctf_flags |= fp->ctf_flags & ~LCTF_DIRTY;
	nfp->ctf_data.cts_data = NULL; /* force ctf_data_free() on close */
	nfp->ctf_dthash = fp->ctf_dthash;
	nfp->ctf_dthashlen = fp->ctf_dthashlen;
	nfp->ctf_dtdefs = fp->ctf_dtdefs;
	nfp->ctf_dtstrlen = fp->ctf_dtstrlen;
	nfp->ctf_dtnextid = fp->ctf_dtnextid;
	nfp->ctf_dtoldid = fp->ctf_dtnextid - 1;
	nfp->ctf_specific = fp->ctf_specific;

	fp->ctf_dthash = NULL;
	fp->ctf_dthashlen = 0;
	bzero(&fp->ctf_dtdefs, sizeof (ctf_list_t));

	bcopy(fp, &ofp, sizeof (ctf_file_t));
	bcopy(nfp, fp, sizeof (ctf_file_t));
	bcopy(&ofp, nfp, sizeof (ctf_file_t));

	/*
	 * 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().
	 */
	fp->ctf_lookups[0].ctl_hash = &fp->ctf_structs;
	fp->ctf_lookups[1].ctl_hash = &fp->ctf_unions;
	fp->ctf_lookups[2].ctl_hash = &fp->ctf_enums;
	fp->ctf_lookups[3].ctl_hash = &fp->ctf_names;

	nfp->ctf_refcnt = 1; /* force nfp to be freed */
	ctf_close(nfp);

	return (0);
}
Beispiel #9
0
int
ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type)
{
	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
	ctf_dmdef_t *dmd;

	ssize_t msize, malign, ssize;
	uint_t kind, vlen, root;
	char *s = NULL;

	if (!(fp->ctf_flags & LCTF_RDWR))
		return (ctf_set_errno(fp, ECTF_RDONLY));

	if (dtd == NULL)
		return (ctf_set_errno(fp, ECTF_BADID));

	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);

	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
		return (ctf_set_errno(fp, ECTF_NOTSOU));

	if (vlen == CTF_MAX_VLEN)
		return (ctf_set_errno(fp, ECTF_DTFULL));

	if (name != NULL) {
		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
		    dmd != NULL; dmd = ctf_list_next(dmd)) {
			if (dmd->dmd_name != NULL &&
			    strcmp(dmd->dmd_name, name) == 0)
				return (ctf_set_errno(fp, ECTF_DUPMEMBER));
		}
	}

	if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
	    (malign = ctf_type_align(fp, type)) == CTF_ERR)
		return (CTF_ERR); /* errno is set for us */

	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
		return (ctf_set_errno(fp, EAGAIN));

	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
		ctf_free(dmd, sizeof (ctf_dmdef_t));
		return (ctf_set_errno(fp, EAGAIN));
	}

	dmd->dmd_name = s;
	dmd->dmd_type = type;
	dmd->dmd_value = -1;

	if (kind == CTF_K_STRUCT && vlen != 0) {
		ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
		ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
		size_t off = lmd->dmd_offset;

		ctf_encoding_t linfo;
		ssize_t lsize;

		if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
			off += linfo.cte_bits;
		else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
			off += lsize * NBBY;

		/*
		 * 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.
		 */
		off = roundup(off, NBBY) / NBBY;
		off = roundup(off, MAX(malign, 1));
		dmd->dmd_offset = off * NBBY;
		ssize = off + msize;
	} else {
		dmd->dmd_offset = 0;
		ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
		ssize = MAX(ssize, msize);
	}

	if (ssize > CTF_MAX_SIZE) {
		dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
		dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
		dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
	} else
		dtd->dtd_data.ctt_size = (ushort_t)ssize;

	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);

	if (s != NULL)
		fp->ctf_dtstrlen += strlen(s) + 1;

	ctf_ref_inc(fp, type);
	fp->ctf_flags |= LCTF_DIRTY;
	return (0);
}
Beispiel #10
0
Datei: ctf.c Projekt: DataIX/src
static void
resurrect_functions(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
    caddr_t ctfdata, symit_data_t *si)
{
	caddr_t buf = ctfdata + h->cth_funcoff;
	size_t bufsz = h->cth_typeoff - h->cth_funcoff;
	caddr_t dptr = buf;
	iidesc_t *ii;
	ushort_t info;
	ushort_t retid;
	GElf_Sym *sym;
	int i;

	symit_reset(si);
	while (dptr < buf + bufsz) {
		void *v = (void *) dptr;
		info = *((ushort_t *)v);
		dptr += 2;

		if (!(sym = symit_next(si, STT_FUNC)) && info != 0)
			parseterminate("Unexpected end of function symbols");

		if (info == 0) {
			debug(3, "Skipping null function (%s)\n",
			    symit_name(si));
			continue;
		}

		v = (void *) dptr;
		retid = *((ushort_t *)v);
		dptr += 2;

		if (retid >= tdsize)
			parseterminate("Reference to invalid type %d", retid);

		ii = iidesc_new(symit_name(si));
		ii->ii_dtype = tdarr[retid];
		if (GELF_ST_BIND(sym->st_info) == STB_LOCAL) {
			ii->ii_type = II_SFUN;
			ii->ii_owner = xstrdup(symit_curfile(si));
		} else
			ii->ii_type = II_GFUN;
		ii->ii_nargs = CTF_INFO_VLEN(info);
		if (ii->ii_nargs)
			ii->ii_args =
			    xmalloc(sizeof (tdesc_t *) * ii->ii_nargs);

		for (i = 0; i < ii->ii_nargs; i++, dptr += 2) {
			v = (void *) dptr;
			ushort_t id = *((ushort_t *)v);
			if (id >= tdsize)
				parseterminate("Reference to invalid type %d",
				    id);
			ii->ii_args[i] = tdarr[id];
		}

		if (ii->ii_nargs && ii->ii_args[ii->ii_nargs - 1] == NULL) {
			ii->ii_nargs--;
			ii->ii_vargs = 1;
		}

		hash_add(td->td_iihash, ii);

		debug(3, "Resurrected %s function %s (%d, %d args)\n",
		    (ii->ii_type == II_GFUN ? "global" : "static"),
		    ii->ii_name, retid, ii->ii_nargs);
	}
}
Beispiel #11
0
Datei: ctf.c Projekt: DataIX/src
static void
resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
    caddr_t ctfdata, int maxid)
{
	caddr_t buf = ctfdata + h->cth_typeoff;
	size_t bufsz = h->cth_stroff - h->cth_typeoff;
	caddr_t sbuf = ctfdata + h->cth_stroff;
	caddr_t dptr = buf;
	tdesc_t *tdp;
	uint_t data;
	uint_t encoding;
	size_t size, increment;
	int tcnt;
	int iicnt = 0;
	tid_t tid, argid;
	int kind, vlen;
	int i;

	elist_t **epp;
	mlist_t **mpp;
	intr_t *ip;

	ctf_type_t *ctt;
	ctf_array_t *cta;
	ctf_enum_t *cte;

	/*
	 * A maxid of zero indicates a request to resurrect all types, so reset
	 * maxid to the maximum type id.
	 */
	if (maxid == 0)
		maxid = CTF_MAX_TYPE;

	for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
		if (tid > maxid)
			break;

		if (tid >= tdsize)
			parseterminate("Reference to invalid type %d", tid);

		void *v = (void *) dptr;
		ctt = v;

		get_ctt_size(ctt, &size, &increment);
		dptr += increment;

		tdp = tdarr[tid];

		if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
			parseterminate(
			    "Unable to cope with non-zero strtab id");
		if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
			tdp->t_name =
			    xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
		} else
			tdp->t_name = NULL;

		kind = CTF_INFO_KIND(ctt->ctt_info);
		vlen = CTF_INFO_VLEN(ctt->ctt_info);

		switch (kind) {
		case CTF_K_INTEGER:
			tdp->t_type = INTRINSIC;
			tdp->t_size = size;

			v = (void *) dptr;
			data = *((uint_t *)v);
			dptr += sizeof (uint_t);
			encoding = CTF_INT_ENCODING(data);

			ip = xmalloc(sizeof (intr_t));
			ip->intr_type = INTR_INT;
			ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;

			if (encoding & CTF_INT_CHAR)
				ip->intr_iformat = 'c';
			else if (encoding & CTF_INT_BOOL)
				ip->intr_iformat = 'b';
			else if (encoding & CTF_INT_VARARGS)
				ip->intr_iformat = 'v';
			else
				ip->intr_iformat = '\0';

			ip->intr_offset = CTF_INT_OFFSET(data);
			ip->intr_nbits = CTF_INT_BITS(data);
			tdp->t_intr = ip;
			break;

		case CTF_K_FLOAT:
			tdp->t_type = INTRINSIC;
			tdp->t_size = size;

			v = (void *) dptr;
			data = *((uint_t *)v);
			dptr += sizeof (uint_t);

			ip = xcalloc(sizeof (intr_t));
			ip->intr_type = INTR_REAL;
			ip->intr_fformat = CTF_FP_ENCODING(data);
			ip->intr_offset = CTF_FP_OFFSET(data);
			ip->intr_nbits = CTF_FP_BITS(data);
			tdp->t_intr = ip;
			break;

		case CTF_K_POINTER:
			tdp->t_type = POINTER;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_ARRAY:
			tdp->t_type = ARRAY;
			tdp->t_size = size;

			v = (void *) dptr;
			cta = v;
			dptr += sizeof (ctf_array_t);

			tdp->t_ardef = xmalloc(sizeof (ardef_t));
			tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
			tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
			tdp->t_ardef->ad_nelems = cta->cta_nelems;
			break;

		case CTF_K_STRUCT:
		case CTF_K_UNION:
			tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
			tdp->t_size = size;

			if (size < CTF_LSTRUCT_THRESH) {
				for (i = 0, mpp = &tdp->t_members; i < vlen;
				    i++, mpp = &((*mpp)->ml_next)) {
					v = (void *) dptr;
					ctf_member_t *ctm = v;
					dptr += sizeof (ctf_member_t);

					*mpp = xmalloc(sizeof (mlist_t));
					(*mpp)->ml_name = xstrdup(sbuf +
					    ctm->ctm_name);
					(*mpp)->ml_type = tdarr[ctm->ctm_type];
					(*mpp)->ml_offset = ctm->ctm_offset;
					(*mpp)->ml_size = 0;
					if (ctm->ctm_type > ntypes) {
						parseterminate("Invalid member type ctm_type=%d",
						    ctm->ctm_type);
					}
				}
			} else {
				for (i = 0, mpp = &tdp->t_members; i < vlen;
				    i++, mpp = &((*mpp)->ml_next)) {
					v = (void *) dptr;
					ctf_lmember_t *ctlm = v;
					dptr += sizeof (ctf_lmember_t);

					*mpp = xmalloc(sizeof (mlist_t));
					(*mpp)->ml_name = xstrdup(sbuf +
					    ctlm->ctlm_name);
					(*mpp)->ml_type =
					    tdarr[ctlm->ctlm_type];
					(*mpp)->ml_offset =
					    (int)CTF_LMEM_OFFSET(ctlm);
					(*mpp)->ml_size = 0;
					if (ctlm->ctlm_type > ntypes) {
						parseterminate("Invalid lmember type ctlm_type=%d",
						    ctlm->ctlm_type);
					}
				}
			}

			*mpp = NULL;
			break;

		case CTF_K_ENUM:
			tdp->t_type = ENUM;
			tdp->t_size = size;

			for (i = 0, epp = &tdp->t_emem; i < vlen;
			    i++, epp = &((*epp)->el_next)) {
				v = (void *) dptr;
				cte = v;
				dptr += sizeof (ctf_enum_t);

				*epp = xmalloc(sizeof (elist_t));
				(*epp)->el_name = xstrdup(sbuf + cte->cte_name);
				(*epp)->el_number = cte->cte_value;
			}
			*epp = NULL;
			break;

		case CTF_K_FORWARD:
			tdp->t_type = FORWARD;
			list_add(&td->td_fwdlist, tdp);
			break;

		case CTF_K_TYPEDEF:
			tdp->t_type = TYPEDEF;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_VOLATILE:
			tdp->t_type = VOLATILE;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_CONST:
			tdp->t_type = CONST;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_FUNCTION:
			tdp->t_type = FUNCTION;
			tdp->t_fndef = xcalloc(sizeof (fndef_t));
			tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];

			v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
			if (vlen > 0 && *(ushort_t *)v == 0)
				tdp->t_fndef->fn_vargs = 1;

			tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
			tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
			    vlen - tdp->t_fndef->fn_vargs);

			for (i = 0; i < vlen; i++) {
				v = (void *) dptr;
				argid = *(ushort_t *)v;
				dptr += sizeof (ushort_t);

				if (argid != 0)
					tdp->t_fndef->fn_args[i] = tdarr[argid];
			}

			if (vlen & 1)
				dptr += sizeof (ushort_t);
			break;

		case CTF_K_RESTRICT:
			tdp->t_type = RESTRICT;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_UNKNOWN:
			break;

		default:
			warning("Can't parse unknown CTF type %d\n", kind);
		}

		if (CTF_INFO_ISROOT(ctt->ctt_info)) {
			iidesc_t *ii = iidesc_new(tdp->t_name);
			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
			    tdp->t_type == ENUM)
				ii->ii_type = II_SOU;
			else
				ii->ii_type = II_TYPE;
			ii->ii_dtype = tdp;
			hash_add(td->td_iihash, ii);

			iicnt++;
		}

		debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
		    (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
		    tdesc_name(tdp), tdp->t_id);
	}

	debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
}
Beispiel #12
0
/*
 * Close the specified CTF container and free associated data structures.  Note
 * that ctf_close() is a reference counted operation: if the specified file is
 * the parent of other active containers, its reference count will be greater
 * than one and it will be freed later when no active children exist.
 */
void
ctf_close(ctf_file_t *fp)
{
	ctf_dtdef_t *dtd, *ntd;
	ctf_dmdef_t *dmd, *nmd;

	if (fp == NULL)
		return; /* allow ctf_close(NULL) to simplify caller code */

	ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt);

	if (fp->ctf_refcnt > 1) {
		fp->ctf_refcnt--;
		return;
	}

	for (dtd = ctf_list_next(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) {
		switch (CTF_INFO_KIND(dtd->dtd_data.ctt_info)) {
		case CTF_K_STRUCT:
		case CTF_K_UNION:
		case CTF_K_ENUM:
			for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
			    dmd != NULL; dmd = nmd) {
				if (dmd->dmd_name != NULL) {
					ctf_free(dmd->dmd_name,
					    strlen(dmd->dmd_name) + 1);
				}
				nmd = ctf_list_next(dmd);
				ctf_free(dmd, sizeof (ctf_dmdef_t));
			}
			break;
		case CTF_K_FUNCTION:
			ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) *
			    CTF_INFO_VLEN(dtd->dtd_data.ctt_info));
			break;
		}

		if (dtd->dtd_name != NULL)
			ctf_free(dtd->dtd_name, strlen(dtd->dtd_name) + 1);

		ntd = ctf_list_next(dtd);
		ctf_free(dtd, sizeof (ctf_dtdef_t));
	}

	if (fp->ctf_parent != NULL)
		ctf_close(fp->ctf_parent);

	if (fp->ctf_flags & LCTF_MMAP) {
		if (fp->ctf_data.cts_data != NULL)
			ctf_sect_munmap(&fp->ctf_data);
		if (fp->ctf_symtab.cts_data != NULL)
			ctf_sect_munmap(&fp->ctf_symtab);
		if (fp->ctf_strtab.cts_data != NULL)
			ctf_sect_munmap(&fp->ctf_strtab);
	}

	if (fp->ctf_data.cts_name != _CTF_NULLSTR &&
	    fp->ctf_data.cts_name != NULL) {
		ctf_free((char *)fp->ctf_data.cts_name,
		    strlen(fp->ctf_data.cts_name) + 1);
	}

	if (fp->ctf_symtab.cts_name != _CTF_NULLSTR &&
	    fp->ctf_symtab.cts_name != NULL) {
		ctf_free((char *)fp->ctf_symtab.cts_name,
		    strlen(fp->ctf_symtab.cts_name) + 1);
	}

	if (fp->ctf_strtab.cts_name != _CTF_NULLSTR &&
	    fp->ctf_strtab.cts_name != NULL) {
		ctf_free((char *)fp->ctf_strtab.cts_name,
		    strlen(fp->ctf_strtab.cts_name) + 1);
	}

	if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL)
		ctf_data_free((void *)fp->ctf_base, fp->ctf_size);

	if (fp->ctf_sxlate != NULL)
		ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms);

	if (fp->ctf_txlate != NULL) {
		ctf_free(fp->ctf_txlate,
		    sizeof (uint_t) * (fp->ctf_typemax + 1));
	}

	if (fp->ctf_ptrtab != NULL) {
		ctf_free(fp->ctf_ptrtab,
		    sizeof (ushort_t) * (fp->ctf_typemax + 1));
	}

	ctf_hash_destroy(&fp->ctf_structs);
	ctf_hash_destroy(&fp->ctf_unions);
	ctf_hash_destroy(&fp->ctf_enums);
	ctf_hash_destroy(&fp->ctf_names);

	ctf_free(fp, sizeof (ctf_file_t));
}
Beispiel #13
0
static int
read_types(const ctf_header_t *hp, const ctf_data_t *cd)
{
	/* LINTED - pointer alignment */
	const ctf_type_t *tp = (ctf_type_t *)(cd->cd_ctfdata + hp->cth_typeoff);

	/* LINTED - pointer alignment */
	const ctf_type_t *end = (ctf_type_t *)(cd->cd_ctfdata + hp->cth_stroff);

	ulong_t id;

	if (flags != F_STATS)
		print_line("- Types ");

	if (hp->cth_typeoff & 3)
		WARN("cth_typeoff is not aligned properly\n");
	if (hp->cth_typeoff >= cd->cd_ctflen)
		WARN("file is truncated or cth_typeoff is corrupt\n");
	if (hp->cth_stroff >= cd->cd_ctflen)
		WARN("file is truncated or cth_stroff is corrupt\n");
	if (hp->cth_typeoff > hp->cth_stroff)
		WARN("file is corrupt -- cth_typeoff > cth_stroff\n");

	id = 1;
	if (hp->cth_parlabel || hp->cth_parname)
		id += 1 << CTF_PARENT_SHIFT;

	for (/* */; tp < end; id++) {
		ulong_t i, n = CTF_INFO_VLEN(tp->ctt_info);
		size_t size, increment, vlen = 0;
		int kind = CTF_INFO_KIND(tp->ctt_info);

		union {
			const void *ptr;
			const ctf_array_t *ap;
			const ctf_member_t *mp;
			const ctf_lmember_t *lmp;
			const ctf_enum_t *ep;
			const ushort_t *argp;
		} u;

		if (flags != F_STATS) {
			(void) printf("  %c%lu%c ",
			    "[<"[CTF_INFO_ISROOT(tp->ctt_info)], id,
			    "]>"[CTF_INFO_ISROOT(tp->ctt_info)]);
		}

		if (tp->ctt_size == CTF_LSIZE_SENT) {
			increment = sizeof (ctf_type_t);
			size = (size_t)CTF_TYPE_LSIZE(tp);
		} else {
			increment = sizeof (ctf_stype_t);
			size = tp->ctt_size;
		}
		u.ptr = (caddr_t)tp + increment;

		switch (kind) {
		case CTF_K_INTEGER:
			if (flags != F_STATS) {
				uint_t encoding = *((const uint_t *)u.ptr);

				(void) printf("INTEGER %s encoding=%s offset=%u"
				    " bits=%u", ref_to_str(tp->ctt_name, hp,
				    cd), int_encoding_to_str(
				    CTF_INT_ENCODING(encoding)),
				    CTF_INT_OFFSET(encoding),
				    CTF_INT_BITS(encoding));
			}
			vlen = sizeof (uint_t);
			break;

		case CTF_K_FLOAT:
			if (flags != F_STATS) {
				uint_t encoding = *((const uint_t *)u.ptr);

				(void) printf("FLOAT %s encoding=%s offset=%u "
				    "bits=%u", ref_to_str(tp->ctt_name, hp,
				    cd), fp_encoding_to_str(
				    CTF_FP_ENCODING(encoding)),
				    CTF_FP_OFFSET(encoding),
				    CTF_FP_BITS(encoding));
			}
			vlen = sizeof (uint_t);
			break;

		case CTF_K_POINTER:
			if (flags != F_STATS) {
				(void) printf("POINTER %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_ARRAY:
			if (flags != F_STATS) {
				(void) printf("ARRAY %s content: %u index: %u "
				    "nelems: %u\n", ref_to_str(tp->ctt_name,
				    hp, cd), u.ap->cta_contents,
				    u.ap->cta_index, u.ap->cta_nelems);
			}
			vlen = sizeof (ctf_array_t);
			break;

		case CTF_K_FUNCTION:
			if (flags != F_STATS) {
				(void) printf("FUNCTION %s returns: %u args: (",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);

				if (n != 0) {
					(void) printf("%u", *u.argp++);
					for (i = 1; i < n; i++, u.argp++)
						(void) printf(", %u", *u.argp);
				}

				(void) printf(")");
			}

			vlen = sizeof (ushort_t) * (n + (n & 1));
			break;

		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (kind == CTF_K_STRUCT) {
				stats.s_nsmem += n;
				stats.s_smmax = MAX(stats.s_smmax, n);
				stats.s_nsbytes += size;
				stats.s_sbmax = MAX(stats.s_sbmax, size);

				if (flags != F_STATS)
					(void) printf("STRUCT");
			} else {
				stats.s_numem += n;
				stats.s_ummax = MAX(stats.s_ummax, n);
				stats.s_nubytes += size;
				stats.s_ubmax = MAX(stats.s_ubmax, size);

				if (flags != F_STATS)
					(void) printf("UNION");
			}

			if (flags != F_STATS) {
				(void) printf(" %s (%d bytes)\n",
				    ref_to_str(tp->ctt_name, hp, cd), size);

				if (size >= CTF_LSTRUCT_THRESH) {
					for (i = 0; i < n; i++, u.lmp++) {
						(void) printf(
						    "\t%s type=%u off=%llu\n",
						    ref_to_str(u.lmp->ctlm_name,
						    hp, cd), u.lmp->ctlm_type,
						    CTF_LMEM_OFFSET(u.lmp));
					}
				} else {
					for (i = 0; i < n; i++, u.mp++) {
						(void) printf(
						    "\t%s type=%u off=%u\n",
						    ref_to_str(u.mp->ctm_name,
						    hp, cd), u.mp->ctm_type,
						    u.mp->ctm_offset);
					}
				}
			}

			vlen = n * (size >= CTF_LSTRUCT_THRESH ?
			    sizeof (ctf_lmember_t) : sizeof (ctf_member_t));
			break;

		case CTF_K_ENUM:
			if (flags != F_STATS) {
				(void) printf("ENUM %s\n",
				    ref_to_str(tp->ctt_name, hp, cd));

				for (i = 0; i < n; i++, u.ep++) {
					(void) printf("\t%s = %d\n",
					    ref_to_str(u.ep->cte_name, hp, cd),
					    u.ep->cte_value);
				}
			}

			stats.s_nemem += n;
			stats.s_emmax = MAX(stats.s_emmax, n);

			vlen = sizeof (ctf_enum_t) * n;
			break;

		case CTF_K_FORWARD:
			if (flags != F_STATS) {
				(void) printf("FORWARD %s",
				    ref_to_str(tp->ctt_name, hp, cd));
			}
			break;

		case CTF_K_TYPEDEF:
			if (flags != F_STATS) {
				(void) printf("TYPEDEF %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_VOLATILE:
			if (flags != F_STATS) {
				(void) printf("VOLATILE %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_CONST:
			if (flags != F_STATS) {
				(void) printf("CONST %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_RESTRICT:
			if (flags != F_STATS) {
				(void) printf("RESTRICT %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_UNKNOWN:
			break; /* hole in type id space */

		default:
			(void) printf("unexpected kind %u\n", kind);
			return (E_ERROR);
		}

		if (flags != F_STATS)
			(void) printf("\n");

		stats.s_ntypes++;
		stats.s_types[kind]++;

		tp = (ctf_type_t *)((uintptr_t)tp + increment + vlen);
	}

	return (E_SUCCESS);
}
Beispiel #14
0
static int
read_funcs(const ctf_header_t *hp, const ctf_data_t *cd)
{
	/* LINTED - pointer alignment */
	const ushort_t *fp = (ushort_t *)(cd->cd_ctfdata + hp->cth_funcoff);

	/* LINTED - pointer alignment */
	const ushort_t *end = (ushort_t *)(cd->cd_ctfdata + hp->cth_typeoff);

	ulong_t id;
	int symidx;

	if (flags != F_STATS)
		print_line("- Functions ");

	if (hp->cth_funcoff & 1)
		WARN("cth_funcoff is not aligned properly\n");
	if (hp->cth_funcoff >= cd->cd_ctflen)
		WARN("file is truncated or cth_funcoff is corrupt\n");
	if (hp->cth_typeoff >= cd->cd_ctflen)
		WARN("file is truncated or cth_typeoff is corrupt\n");
	if (hp->cth_funcoff > hp->cth_typeoff)
		WARN("file is corrupt -- cth_funcoff > cth_typeoff\n");

	for (symidx = -1, id = 0; fp < end; id++) {
		ushort_t info = *fp++;
		ushort_t kind = CTF_INFO_KIND(info);
		ushort_t n = CTF_INFO_VLEN(info);
		ushort_t i;
		int nextsym;
		char *name;

		if (cd->cd_symdata == NULL || (nextsym = next_sym(cd, symidx,
		    STT_FUNC, &name)) < 0)
			name = NULL;
		else
			symidx = nextsym;

		if (kind == CTF_K_UNKNOWN && n == 0)
			continue; /* skip padding */

		if (kind != CTF_K_FUNCTION) {
			(void) printf("  [%lu] unexpected kind -- %u\n",
			    id, kind);
			return (E_ERROR);
		}

		if (fp + n > end) {
			(void) printf("  [%lu] vlen %u extends past section "
			    "boundary\n", id, n);
			return (E_ERROR);
		}

		if (flags != F_STATS) {
			(void) printf("  [%lu] FUNC ", id);
			if (name != NULL)
				(void) printf("(%s) ", name);
			(void) printf("returns: %u args: (", *fp++);

			if (n != 0) {
				(void) printf("%u", *fp++);
				for (i = 1; i < n; i++)
					(void) printf(", %u", *fp++);
			}

			(void) printf(")\n");
		} else
			fp += n + 1; /* skip to next function definition */

		stats.s_nfunc++;
		stats.s_nargs += n;
		stats.s_argmax = MAX(stats.s_argmax, n);
	}

	return (E_SUCCESS);
}
Beispiel #15
0
static int
fbt_ctfoff_init(dtrace_modctl_t *mod, mod_ctf_t *mc)
{
	const Elf_Sym *symp = mc->symtab;
	const char *name;
	const ctf_header_t *hp = (const ctf_header_t *) mc->ctftab;
	const uint8_t *ctfdata = mc->ctftab + sizeof(ctf_header_t);
	int i;
	uint32_t *ctfoff;
	uint32_t objtoff = hp->cth_objtoff;
	uint32_t funcoff = hp->cth_funcoff;
	ushort_t info;
	ushort_t vlen;
	int nsyms = (mc->nmap != NULL) ? mc->nmapsize : mc->nsym;

	/* Sanity check. */
	if (hp->cth_magic != CTF_MAGIC) {
		printf("Bad magic value in CTF data of '%s'\n",
			mod->mod_info->mi_name);
		return (EINVAL);
	}

	if (mc->symtab == NULL) {
		printf("No symbol table in '%s'\n",
			mod->mod_info->mi_name);
		return (EINVAL);
	}

	if ((ctfoff = malloc(sizeof(uint32_t) * nsyms, M_FBT, M_WAITOK)) == NULL)
		return (ENOMEM);

	mc->ctfoffp = ctfoff;

	for (i = 0; i < nsyms; i++, ctfoff++, symp++) {
	   	if (mc->nmap != NULL) {
			if (mc->nmap[i] == 0) {
				printf("%s.%d: Error! Got zero nmap!\n",
					__func__, __LINE__);
				continue;
			}

			/* CTF expects the pre-sorted symbol ordering, 
			 * so map it from that to the current sorted
			 * and trimmed symbol table.
			 * ctfoff[new-ind] = oldind symbol info.
			 */

			/* map old index to new symbol table */
			symp = &mc->symtab[mc->nmap[i] - 1];

			/* map old index to new ctfoff index */
			ctfoff = &mc->ctfoffp[mc->nmap[i]-1];
		}

		if (symp->st_name == 0 || symp->st_shndx == SHN_UNDEF) {
			*ctfoff = 0xffffffff;
			continue;
		}

		if (symp->st_name < mc->strcnt)
			name = mc->strtab + symp->st_name;
		else
			name = "(?)";

		switch (ELF_ST_TYPE(symp->st_info)) {
		case STT_OBJECT:
			if (objtoff >= hp->cth_funcoff ||
                            (symp->st_shndx == SHN_ABS && symp->st_value == 0)) {
				*ctfoff = 0xffffffff;
                                break;
                        }

                        *ctfoff = objtoff;
                        objtoff += sizeof (ushort_t);
			break;

		case STT_FUNC:
			if (funcoff >= hp->cth_typeoff) {
				*ctfoff = 0xffffffff;
				break;
			}

			*ctfoff = funcoff;

			info = *((const ushort_t *)(ctfdata + funcoff));
			vlen = CTF_INFO_VLEN(info);

			/*
			 * If we encounter a zero pad at the end, just skip it.
			 * Otherwise skip over the function and its return type
			 * (+2) and the argument list (vlen).
			 */
			if (CTF_INFO_KIND(info) == CTF_K_UNKNOWN && vlen == 0)
				funcoff += sizeof (ushort_t); /* skip pad */
			else
				funcoff += sizeof (ushort_t) * (vlen + 2);
			break;

		default:
			*ctfoff = 0xffffffff;
			break;
		}
	}

	return (0);
}