10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*1222Smws
230Sstevel@tonic-gate /*
24*1222Smws * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <ctf_impl.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * Compare the given input string and length against a table of known C storage
35*1222Smws * qualifier keywords. We just ignore these in ctf_lookup_by_name, below. To
36*1222Smws * do this quickly, we use a pre-computed Perfect Hash Function similar to the
37*1222Smws * technique originally described in the classic paper:
38*1222Smws *
39*1222Smws * R.J. Cichelli, "Minimal Perfect Hash Functions Made Simple",
40*1222Smws * Communications of the ACM, Volume 23, Issue 1, January 1980, pp. 17-19.
41*1222Smws *
42*1222Smws * For an input string S of length N, we use hash H = S[N - 1] + N - 105, which
43*1222Smws * for the current set of qualifiers yields a unique H in the range [0 .. 20].
44*1222Smws * The hash can be modified when the keyword set changes as necessary. We also
45*1222Smws * store the length of each keyword and check it prior to the final strcmp().
460Sstevel@tonic-gate */
470Sstevel@tonic-gate static int
isqualifier(const char * s,size_t len)480Sstevel@tonic-gate isqualifier(const char *s, size_t len)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate static const struct qual {
510Sstevel@tonic-gate const char *q_name;
520Sstevel@tonic-gate size_t q_len;
53*1222Smws } qhash[] = {
54*1222Smws { "static", 6 }, { "", 0 }, { "", 0 }, { "", 0 },
55*1222Smws { "volatile", 8 }, { "", 0 }, { "", 0 }, { "", 0 }, { "", 0 },
56*1222Smws { "", 0 }, { "auto", 4 }, { "extern", 6 }, { "", 0 }, { "", 0 },
57*1222Smws { "", 0 }, { "", 0 }, { "const", 5 }, { "register", 8 },
58*1222Smws { "", 0 }, { "restrict", 8 }, { "_Restrict", 9 }
590Sstevel@tonic-gate };
600Sstevel@tonic-gate
61*1222Smws int h = s[len - 1] + (int)len - 105;
62*1222Smws const struct qual *qp = &qhash[h];
630Sstevel@tonic-gate
64*1222Smws return (h >= 0 && h < sizeof (qhash) / sizeof (qhash[0]) &&
65*1222Smws len == qp->q_len && strncmp(qp->q_name, s, qp->q_len) == 0);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate * Attempt to convert the given C type name into the corresponding CTF type ID.
700Sstevel@tonic-gate * It is not possible to do complete and proper conversion of type names
710Sstevel@tonic-gate * without implementing a more full-fledged parser, which is necessary to
720Sstevel@tonic-gate * handle things like types that are function pointers to functions that
730Sstevel@tonic-gate * have arguments that are function pointers, and fun stuff like that.
740Sstevel@tonic-gate * Instead, this function implements a very simple conversion algorithm that
750Sstevel@tonic-gate * finds the things that we actually care about: structs, unions, enums,
760Sstevel@tonic-gate * integers, floats, typedefs, and pointers to any of these named types.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate ctf_id_t
ctf_lookup_by_name(ctf_file_t * fp,const char * name)790Sstevel@tonic-gate ctf_lookup_by_name(ctf_file_t *fp, const char *name)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate static const char delimiters[] = " \t\n\r\v\f*";
820Sstevel@tonic-gate
830Sstevel@tonic-gate const ctf_lookup_t *lp;
840Sstevel@tonic-gate const ctf_helem_t *hp;
850Sstevel@tonic-gate const char *p, *q, *end;
860Sstevel@tonic-gate ctf_id_t type = 0;
870Sstevel@tonic-gate ctf_id_t ntype, ptype;
880Sstevel@tonic-gate
890Sstevel@tonic-gate if (name == NULL)
900Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL));
910Sstevel@tonic-gate
920Sstevel@tonic-gate for (p = name, end = name + strlen(name); *p != '\0'; p = q) {
930Sstevel@tonic-gate while (isspace(*p))
940Sstevel@tonic-gate p++; /* skip leading ws */
950Sstevel@tonic-gate
960Sstevel@tonic-gate if (p == end)
970Sstevel@tonic-gate break;
980Sstevel@tonic-gate
990Sstevel@tonic-gate if ((q = strpbrk(p + 1, delimiters)) == NULL)
1000Sstevel@tonic-gate q = end; /* compare until end */
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (*p == '*') {
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * Find a pointer to type by looking in fp->ctf_ptrtab.
1050Sstevel@tonic-gate * If we can't find a pointer to the given type, see if
1060Sstevel@tonic-gate * we can compute a pointer to the type resulting from
1070Sstevel@tonic-gate * resolving the type down to its base type and use
1080Sstevel@tonic-gate * that instead. This helps with cases where the CTF
1090Sstevel@tonic-gate * data includes "struct foo *" but not "foo_t *" and
1100Sstevel@tonic-gate * the user tries to access "foo_t *" in the debugger.
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate ntype = fp->ctf_ptrtab[CTF_TYPE_TO_INDEX(type)];
1130Sstevel@tonic-gate if (ntype == 0) {
1140Sstevel@tonic-gate ntype = ctf_type_resolve(fp, type);
1150Sstevel@tonic-gate if (ntype == CTF_ERR || (ntype = fp->ctf_ptrtab[
1160Sstevel@tonic-gate CTF_TYPE_TO_INDEX(ntype)]) == 0) {
1170Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_NOTYPE);
1180Sstevel@tonic-gate goto err;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate type = CTF_INDEX_TO_TYPE(ntype,
1230Sstevel@tonic-gate (fp->ctf_flags & LCTF_CHILD));
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate q = p + 1;
1260Sstevel@tonic-gate continue;
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate if (isqualifier(p, (size_t)(q - p)))
1300Sstevel@tonic-gate continue; /* skip qualifier keyword */
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++) {
1330Sstevel@tonic-gate if (lp->ctl_prefix[0] == '\0' ||
1340Sstevel@tonic-gate strncmp(p, lp->ctl_prefix, (size_t)(q - p)) == 0) {
1350Sstevel@tonic-gate for (p += lp->ctl_len; isspace(*p); p++)
1360Sstevel@tonic-gate continue; /* skip prefix and next ws */
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate if ((q = strchr(p, '*')) == NULL)
1390Sstevel@tonic-gate q = end; /* compare until end */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate while (isspace(q[-1]))
1420Sstevel@tonic-gate q--; /* exclude trailing ws */
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if ((hp = ctf_hash_lookup(lp->ctl_hash, fp, p,
1450Sstevel@tonic-gate (size_t)(q - p))) == NULL) {
1460Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_NOTYPE);
1470Sstevel@tonic-gate goto err;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate type = hp->h_type;
1510Sstevel@tonic-gate break;
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (lp->ctl_prefix == NULL) {
1560Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_NOTYPE);
1570Sstevel@tonic-gate goto err;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate if (*p != '\0' || type == 0)
1620Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_SYNTAX));
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate return (type);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate err:
1670Sstevel@tonic-gate if (fp->ctf_parent != NULL &&
1680Sstevel@tonic-gate (ptype = ctf_lookup_by_name(fp->ctf_parent, name)) != CTF_ERR)
1690Sstevel@tonic-gate return (ptype);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate return (CTF_ERR);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate * Given a symbol table index, return the type of the data object described
1760Sstevel@tonic-gate * by the corresponding entry in the symbol table.
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate ctf_id_t
ctf_lookup_by_symbol(ctf_file_t * fp,ulong_t symidx)1790Sstevel@tonic-gate ctf_lookup_by_symbol(ctf_file_t *fp, ulong_t symidx)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate const ctf_sect_t *sp = &fp->ctf_symtab;
1820Sstevel@tonic-gate ctf_id_t type;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate if (sp->cts_data == NULL)
1850Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOSYMTAB));
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate if (symidx >= fp->ctf_nsyms)
1880Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL));
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate if (sp->cts_entsize == sizeof (Elf32_Sym)) {
1910Sstevel@tonic-gate const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
1920Sstevel@tonic-gate if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
1930Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTDATA));
1940Sstevel@tonic-gate } else {
1950Sstevel@tonic-gate const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
1960Sstevel@tonic-gate if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
1970Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTDATA));
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (fp->ctf_sxlate[symidx] == -1u)
2010Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate type = *(ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
2040Sstevel@tonic-gate if (type == 0)
2050Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate return (type);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate * Return the pointer to the internal CTF type data corresponding to the
2120Sstevel@tonic-gate * given type ID. If the ID is invalid, the function returns NULL.
2130Sstevel@tonic-gate * This function is not exported outside of the library.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate const ctf_type_t *
ctf_lookup_by_id(ctf_file_t ** fpp,ctf_id_t type)2160Sstevel@tonic-gate ctf_lookup_by_id(ctf_file_t **fpp, ctf_id_t type)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate ctf_file_t *fp = *fpp; /* caller passes in starting CTF container */
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if ((fp->ctf_flags & LCTF_CHILD) && CTF_TYPE_ISPARENT(type) &&
2210Sstevel@tonic-gate (fp = fp->ctf_parent) == NULL) {
2220Sstevel@tonic-gate (void) ctf_set_errno(*fpp, ECTF_NOPARENT);
2230Sstevel@tonic-gate return (NULL);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate type = CTF_TYPE_TO_INDEX(type);
2270Sstevel@tonic-gate if (type > 0 && type <= fp->ctf_typemax) {
2280Sstevel@tonic-gate *fpp = fp; /* function returns ending CTF container */
2290Sstevel@tonic-gate return (LCTF_INDEX_TO_TYPEPTR(fp, type));
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_BADID);
2330Sstevel@tonic-gate return (NULL);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate * Given a symbol table index, return the info for the function described
2380Sstevel@tonic-gate * by the corresponding entry in the symbol table.
2390Sstevel@tonic-gate */
2400Sstevel@tonic-gate int
ctf_func_info(ctf_file_t * fp,ulong_t symidx,ctf_funcinfo_t * fip)2410Sstevel@tonic-gate ctf_func_info(ctf_file_t *fp, ulong_t symidx, ctf_funcinfo_t *fip)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate const ctf_sect_t *sp = &fp->ctf_symtab;
2440Sstevel@tonic-gate const ushort_t *dp;
2450Sstevel@tonic-gate ushort_t info, kind, n;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate if (sp->cts_data == NULL)
2480Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOSYMTAB));
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate if (symidx >= fp->ctf_nsyms)
2510Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL));
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate if (sp->cts_entsize == sizeof (Elf32_Sym)) {
2540Sstevel@tonic-gate const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
2550Sstevel@tonic-gate if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
2560Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTFUNC));
2570Sstevel@tonic-gate } else {
2580Sstevel@tonic-gate const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
2590Sstevel@tonic-gate if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
2600Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTFUNC));
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate if (fp->ctf_sxlate[symidx] == -1u)
2640Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate info = *dp++;
2690Sstevel@tonic-gate kind = LCTF_INFO_KIND(fp, info);
2700Sstevel@tonic-gate n = LCTF_INFO_VLEN(fp, info);
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (kind == CTF_K_UNKNOWN && n == 0)
2730Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate if (kind != CTF_K_FUNCTION)
2760Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_CORRUPT));
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate fip->ctc_return = *dp++;
2790Sstevel@tonic-gate fip->ctc_argc = n;
2800Sstevel@tonic-gate fip->ctc_flags = 0;
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate if (n != 0 && dp[n - 1] == 0) {
2830Sstevel@tonic-gate fip->ctc_flags |= CTF_FUNC_VARARG;
2840Sstevel@tonic-gate fip->ctc_argc--;
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate return (0);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * Given a symbol table index, return the arguments for the function described
2920Sstevel@tonic-gate * by the corresponding entry in the symbol table.
2930Sstevel@tonic-gate */
2940Sstevel@tonic-gate int
ctf_func_args(ctf_file_t * fp,ulong_t symidx,uint_t argc,ctf_id_t * argv)2950Sstevel@tonic-gate ctf_func_args(ctf_file_t *fp, ulong_t symidx, uint_t argc, ctf_id_t *argv)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate const ushort_t *dp;
2980Sstevel@tonic-gate ctf_funcinfo_t f;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate if (ctf_func_info(fp, symidx, &f) == CTF_ERR)
3010Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate * The argument data is two ushort_t's past the translation table
3050Sstevel@tonic-gate * offset: one for the function info, and one for the return type.
3060Sstevel@tonic-gate */
3070Sstevel@tonic-gate dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]) + 2;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate for (argc = MIN(argc, f.ctc_argc); argc != 0; argc--)
3100Sstevel@tonic-gate *argv++ = *dp++;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate return (0);
3130Sstevel@tonic-gate }
314