Path: blob/main/cddl/contrib/opensolaris/common/ctf/ctf_lookup.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*/2627#pragma ident "%Z%%M% %I% %E% SMI"2829#include <sys/sysmacros.h>30#include <ctf_impl.h>3132/*33* Compare the given input string and length against a table of known C storage34* qualifier keywords. We just ignore these in ctf_lookup_by_name, below. To35* do this quickly, we use a pre-computed Perfect Hash Function similar to the36* technique originally described in the classic paper:37*38* R.J. Cichelli, "Minimal Perfect Hash Functions Made Simple",39* Communications of the ACM, Volume 23, Issue 1, January 1980, pp. 17-19.40*41* For an input string S of length N, we use hash H = S[N - 1] + N - 105, which42* for the current set of qualifiers yields a unique H in the range [0 .. 20].43* The hash can be modified when the keyword set changes as necessary. We also44* store the length of each keyword and check it prior to the final strcmp().45*/46static int47isqualifier(const char *s, size_t len)48{49static const struct qual {50const char *q_name;51size_t q_len;52} qhash[] = {53{ "static", 6 }, { "", 0 }, { "", 0 }, { "", 0 },54{ "volatile", 8 }, { "", 0 }, { "", 0 }, { "", 0 }, { "", 0 },55{ "", 0 }, { "auto", 4 }, { "extern", 6 }, { "", 0 }, { "", 0 },56{ "", 0 }, { "", 0 }, { "const", 5 }, { "register", 8 },57{ "", 0 }, { "restrict", 8 }, { "_Restrict", 9 }58};5960int h = s[len - 1] + (int)len - 105;61const struct qual *qp;6263if (h < 0 || h >= sizeof (qhash) / sizeof (qhash[0]))64return (0);65qp = &qhash[h];66return (len == qp->q_len && strncmp(qp->q_name, s, qp->q_len) == 0);67}6869/*70* Attempt to convert the given C type name into the corresponding CTF type ID.71* It is not possible to do complete and proper conversion of type names72* without implementing a more full-fledged parser, which is necessary to73* handle things like types that are function pointers to functions that74* have arguments that are function pointers, and fun stuff like that.75* Instead, this function implements a very simple conversion algorithm that76* finds the things that we actually care about: structs, unions, enums,77* integers, floats, typedefs, and pointers to any of these named types.78*/79ctf_id_t80ctf_lookup_by_name(ctf_file_t *fp, const char *name)81{82static const char delimiters[] = " \t\n\r\v\f*";8384const ctf_lookup_t *lp;85const ctf_helem_t *hp;86const char *p, *q, *end;87ctf_id_t type = 0;88ctf_id_t ntype, ptype;8990if (name == NULL)91return (ctf_set_errno(fp, EINVAL));9293for (p = name, end = name + strlen(name); *p != '\0'; p = q) {94while (isspace(*p))95p++; /* skip leading ws */9697if (p == end)98break;99100if ((q = strpbrk(p + 1, delimiters)) == NULL)101q = end; /* compare until end */102103if (*p == '*') {104/*105* Find a pointer to type by looking in fp->ctf_ptrtab.106* If we can't find a pointer to the given type, see if107* we can compute a pointer to the type resulting from108* resolving the type down to its base type and use109* that instead. This helps with cases where the CTF110* data includes "struct foo *" but not "foo_t *" and111* the user tries to access "foo_t *" in the debugger.112*/113ntype = fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX(fp, type)];114if (ntype == 0) {115ntype = ctf_type_resolve(fp, type);116if (ntype == CTF_ERR || (ntype = fp->ctf_ptrtab[117LCTF_TYPE_TO_INDEX(fp, ntype)]) == 0) {118(void) ctf_set_errno(fp, ECTF_NOTYPE);119goto err;120}121}122123type = LCTF_INDEX_TO_TYPE(fp, ntype,124(fp->ctf_flags & LCTF_CHILD));125126q = p + 1;127continue;128}129130if (isqualifier(p, (size_t)(q - p)))131continue; /* skip qualifier keyword */132133for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++) {134if (lp->ctl_prefix[0] == '\0' ||135((size_t)(q - p) >= lp->ctl_len && strncmp(p,136lp->ctl_prefix, (size_t)(q - p)) == 0)) {137for (p += lp->ctl_len; isspace(*p); p++)138continue; /* skip prefix and next ws */139140if ((q = strchr(p, '*')) == NULL)141q = end; /* compare until end */142143while (isspace(q[-1]))144q--; /* exclude trailing ws */145146if ((hp = ctf_hash_lookup(lp->ctl_hash, fp, p,147(size_t)(q - p))) == NULL) {148(void) ctf_set_errno(fp, ECTF_NOTYPE);149goto err;150}151152type = hp->h_type;153break;154}155}156157if (lp->ctl_prefix == NULL) {158(void) ctf_set_errno(fp, ECTF_NOTYPE);159goto err;160}161}162163if (*p != '\0' || type == 0)164return (ctf_set_errno(fp, ECTF_SYNTAX));165166return (type);167168err:169if (fp->ctf_parent != NULL &&170(ptype = ctf_lookup_by_name(fp->ctf_parent, name)) != CTF_ERR)171return (ptype);172173return (CTF_ERR);174}175176/*177* Given a symbol table index, return the type of the data object described178* by the corresponding entry in the symbol table.179*/180ctf_id_t181ctf_lookup_by_symbol(ctf_file_t *fp, ulong_t symidx)182{183const ctf_sect_t *sp = &fp->ctf_symtab;184ctf_id_t type;185186if (sp->cts_data == NULL)187return (ctf_set_errno(fp, ECTF_NOSYMTAB));188189if (symidx >= fp->ctf_nsyms)190return (ctf_set_errno(fp, EINVAL));191192if (sp->cts_entsize == sizeof (Elf32_Sym)) {193const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;194if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)195return (ctf_set_errno(fp, ECTF_NOTDATA));196} else {197const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;198if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)199return (ctf_set_errno(fp, ECTF_NOTDATA));200}201202if (fp->ctf_sxlate[symidx] == -1u)203return (ctf_set_errno(fp, ECTF_NOTYPEDAT));204205type = *(uint_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);206if (type == 0)207return (ctf_set_errno(fp, ECTF_NOTYPEDAT));208209return (type);210}211212/*213* Return the pointer to the internal CTF type data corresponding to the214* given type ID. If the ID is invalid, the function returns NULL.215* This function is not exported outside of the library.216*/217const void *218ctf_lookup_by_id(ctf_file_t **fpp, ctf_id_t type)219{220ctf_file_t *fp = *fpp; /* caller passes in starting CTF container */221222if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT(fp, type)) {223if (fp->ctf_parent == NULL) {224(void) ctf_set_errno(*fpp, ECTF_NOPARENT);225return (NULL);226}227228/* The parent may be using a different CTF version. */229type = LCTF_TYPE_TO_INDEX(fp, type);230fp = fp->ctf_parent;231} else {232type = LCTF_TYPE_TO_INDEX(fp, type);233}234235if (type > 0 && type <= fp->ctf_typemax) {236*fpp = fp; /* function returns ending CTF container */237return (LCTF_INDEX_TO_TYPEPTR(fp, type));238}239240(void) ctf_set_errno(fp, ECTF_BADID);241return (NULL);242}243244/*245* Given a symbol table index, return the info for the function described246* by the corresponding entry in the symbol table.247*/248int249ctf_func_info(ctf_file_t *fp, ulong_t symidx, ctf_funcinfo_t *fip)250{251const ctf_sect_t *sp = &fp->ctf_symtab;252const uint_t *dp;253uint_t info, kind, n;254255if (sp->cts_data == NULL)256return (ctf_set_errno(fp, ECTF_NOSYMTAB));257258if (symidx >= fp->ctf_nsyms)259return (ctf_set_errno(fp, EINVAL));260261if (sp->cts_entsize == sizeof (Elf32_Sym)) {262const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;263if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)264return (ctf_set_errno(fp, ECTF_NOTFUNC));265} else {266const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;267if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)268return (ctf_set_errno(fp, ECTF_NOTFUNC));269}270271if (fp->ctf_sxlate[symidx] == -1u)272return (ctf_set_errno(fp, ECTF_NOFUNCDAT));273274dp = (uint_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);275276info = *dp++;277kind = LCTF_INFO_KIND(fp, info);278n = LCTF_INFO_VLEN(fp, info);279280if (kind == CTF_K_UNKNOWN && n == 0)281return (ctf_set_errno(fp, ECTF_NOFUNCDAT));282283if (kind != CTF_K_FUNCTION)284return (ctf_set_errno(fp, ECTF_CORRUPT));285286fip->ctc_return = *dp++;287fip->ctc_argc = n;288fip->ctc_flags = 0;289290if (n != 0 && dp[n - 1] == 0) {291fip->ctc_flags |= CTF_FUNC_VARARG;292fip->ctc_argc--;293}294295return (0);296}297298/*299* Given a symbol table index, return the arguments for the function described300* by the corresponding entry in the symbol table.301*/302int303ctf_func_args(ctf_file_t *fp, ulong_t symidx, uint_t argc, ctf_id_t *argv)304{305const uint_t *dp;306ctf_funcinfo_t f;307308if (ctf_func_info(fp, symidx, &f) == CTF_ERR)309return (CTF_ERR); /* errno is set for us */310311/*312* The argument data is two uint_t's past the translation table313* offset: one for the function info, and one for the return type.314*/315dp = (uint_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]) + 2;316317for (argc = MIN(argc, f.ctc_argc); argc != 0; argc--)318*argv++ = *dp++;319320return (0);321}322323324