ctf_lookup.c revision e083a0c2c99cea982dcf8e12ec3452cc575b5663
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync/*
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * CDDL HEADER START
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync *
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * The contents of this file are subject to the terms of the
2f14f4556ae74ad40719bc9690534693abb71868vboxsync * Common Development and Distribution License, Version 1.0 only
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * (the "License"). You may not use this file except in compliance
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * with the License.
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync *
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * or http://www.opensolaris.org/os/licensing.
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * See the License for the specific language governing permissions
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * and limitations under the License.
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync *
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * When distributing Covered Code, include this CDDL HEADER in each
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
94872a0e88ab4f38c786fcf617ddeb4b63a76957vboxsync * If applicable, add the following below this CDDL HEADER, with the
78a072e1b56619e3230735ae073668311232ec94vboxsync * fields enclosed by brackets "[]" replaced with your own identifying
78a072e1b56619e3230735ae073668311232ec94vboxsync * information: Portions Copyright [yyyy] [name of copyright owner]
78a072e1b56619e3230735ae073668311232ec94vboxsync *
78a072e1b56619e3230735ae073668311232ec94vboxsync * CDDL HEADER END
78a072e1b56619e3230735ae073668311232ec94vboxsync */
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync/*
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync * Use is subject to license terms.
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync */
c3d2b15ad840b405062f4c2c6b127d6fc107c7b2vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync#pragma ident "%Z%%M% %I% %E% SMI"
b0c4bd49112a99f00ef48b7a8eae6fb310a62bdavboxsync
b0c4bd49112a99f00ef48b7a8eae6fb310a62bdavboxsync#include <sys/sysmacros.h>
b0c4bd49112a99f00ef48b7a8eae6fb310a62bdavboxsync#include <ctf_impl.h>
78a072e1b56619e3230735ae073668311232ec94vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync/*
78a072e1b56619e3230735ae073668311232ec94vboxsync * Compare the given input string and length against a table of known C storage
b0c4bd49112a99f00ef48b7a8eae6fb310a62bdavboxsync * qualifier keywords. We just ignore these in ctf_lookup_by_name, below. To
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * do this quickly, we use a pre-computed Perfect Hash Function similar to the
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * technique originally described in the classic paper:
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync *
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * R.J. Cichelli, "Minimal Perfect Hash Functions Made Simple",
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * Communications of the ACM, Volume 23, Issue 1, January 1980, pp. 17-19.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync *
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * For an input string S of length N, we use hash H = S[N - 1] + N - 105, which
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * for the current set of qualifiers yields a unique H in the range [0 .. 20].
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * The hash can be modified when the keyword set changes as necessary. We also
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * store the length of each keyword and check it prior to the final strcmp().
78a072e1b56619e3230735ae073668311232ec94vboxsync */
d5d45608052fd506e4114bf112df6efae7fcd8a7vboxsyncstatic int
9be72bf71509dd721ffa1df70ead200abf30afd8vboxsyncisqualifier(const char *s, size_t len)
e7184fff6d89903aed623860629a05047960ac2dvboxsync{
e7184fff6d89903aed623860629a05047960ac2dvboxsync static const struct qual {
e7184fff6d89903aed623860629a05047960ac2dvboxsync const char *q_name;
78a072e1b56619e3230735ae073668311232ec94vboxsync size_t q_len;
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync } qhash[] = {
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "static", 6 }, { "", 0 }, { "", 0 }, { "", 0 },
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "volatile", 8 }, { "", 0 }, { "", 0 }, { "", 0 }, { "", 0 },
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "", 0 }, { "auto", 4 }, { "extern", 6 }, { "", 0 }, { "", 0 },
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "", 0 }, { "", 0 }, { "const", 5 }, { "register", 8 },
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync { "", 0 }, { "restrict", 8 }, { "_Restrict", 9 }
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync };
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync int h = s[len - 1] + (int)len - 105;
78a072e1b56619e3230735ae073668311232ec94vboxsync const struct qual *qp = &qhash[h];
78a072e1b56619e3230735ae073668311232ec94vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync return (h >= 0 && h < sizeof (qhash) / sizeof (qhash[0]) &&
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync len == qp->q_len && strncmp(qp->q_name, s, qp->q_len) == 0);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync}
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync/*
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * Attempt to convert the given C type name into the corresponding CTF type ID.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * It is not possible to do complete and proper conversion of type names
78a072e1b56619e3230735ae073668311232ec94vboxsync * without implementing a more full-fledged parser, which is necessary to
78a072e1b56619e3230735ae073668311232ec94vboxsync * handle things like types that are function pointers to functions that
78a072e1b56619e3230735ae073668311232ec94vboxsync * have arguments that are function pointers, and fun stuff like that.
78a072e1b56619e3230735ae073668311232ec94vboxsync * Instead, this function implements a very simple conversion algorithm that
78a072e1b56619e3230735ae073668311232ec94vboxsync * finds the things that we actually care about: structs, unions, enums,
78a072e1b56619e3230735ae073668311232ec94vboxsync * integers, floats, typedefs, and pointers to any of these named types.
78a072e1b56619e3230735ae073668311232ec94vboxsync */
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_id_t
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_lookup_by_name(ctf_file_t *fp, const char *name)
78a072e1b56619e3230735ae073668311232ec94vboxsync{
78a072e1b56619e3230735ae073668311232ec94vboxsync static const char delimiters[] = " \t\n\r\v\f*";
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync const ctf_lookup_t *lp;
78a072e1b56619e3230735ae073668311232ec94vboxsync const ctf_helem_t *hp;
78a072e1b56619e3230735ae073668311232ec94vboxsync const char *p, *q, *end;
78a072e1b56619e3230735ae073668311232ec94vboxsync ctf_id_t type = 0;
78a072e1b56619e3230735ae073668311232ec94vboxsync ctf_id_t ntype, ptype;
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (name == NULL)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, EINVAL));
78a072e1b56619e3230735ae073668311232ec94vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync for (p = name, end = name + strlen(name); *p != '\0'; p = q) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync while (isspace(*p))
08c4185261c17943cff6cc94522579696eeeb478vboxsync p++; /* skip leading ws */
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (p == end)
08c4185261c17943cff6cc94522579696eeeb478vboxsync break;
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync if ((q = strpbrk(p + 1, delimiters)) == NULL)
08c4185261c17943cff6cc94522579696eeeb478vboxsync q = end; /* compare until end */
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (*p == '*') {
08c4185261c17943cff6cc94522579696eeeb478vboxsync /*
08c4185261c17943cff6cc94522579696eeeb478vboxsync * Find a pointer to type by looking in fp->ctf_ptrtab.
08c4185261c17943cff6cc94522579696eeeb478vboxsync * If we can't find a pointer to the given type, see if
08c4185261c17943cff6cc94522579696eeeb478vboxsync * we can compute a pointer to the type resulting from
08c4185261c17943cff6cc94522579696eeeb478vboxsync * resolving the type down to its base type and use
08c4185261c17943cff6cc94522579696eeeb478vboxsync * that instead. This helps with cases where the CTF
08c4185261c17943cff6cc94522579696eeeb478vboxsync * data includes "struct foo *" but not "foo_t *" and
08c4185261c17943cff6cc94522579696eeeb478vboxsync * the user tries to access "foo_t *" in the debugger.
08c4185261c17943cff6cc94522579696eeeb478vboxsync */
08c4185261c17943cff6cc94522579696eeeb478vboxsync ntype = fp->ctf_ptrtab[CTF_TYPE_TO_INDEX(type)];
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (ntype == 0) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync ntype = ctf_type_resolve(fp, type);
78a072e1b56619e3230735ae073668311232ec94vboxsync if (ntype == CTF_ERR || (ntype = fp->ctf_ptrtab[
08c4185261c17943cff6cc94522579696eeeb478vboxsync CTF_TYPE_TO_INDEX(ntype)]) == 0) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync (void) ctf_set_errno(fp, ECTF_NOTYPE);
9726c89eba6e777f3eb4d57f65ca6171a2241d29vboxsync goto err;
08c4185261c17943cff6cc94522579696eeeb478vboxsync }
08c4185261c17943cff6cc94522579696eeeb478vboxsync }
08c4185261c17943cff6cc94522579696eeeb478vboxsync
da31d917654e0b617e7a9bf8b0cf786136edf8e8vboxsync type = CTF_INDEX_TO_TYPE(ntype,
08c4185261c17943cff6cc94522579696eeeb478vboxsync (fp->ctf_flags & LCTF_CHILD));
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync q = p + 1;
08c4185261c17943cff6cc94522579696eeeb478vboxsync continue;
08c4185261c17943cff6cc94522579696eeeb478vboxsync }
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (isqualifier(p, (size_t)(q - p)))
08c4185261c17943cff6cc94522579696eeeb478vboxsync continue; /* skip qualifier keyword */
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (lp->ctl_prefix[0] == '\0' ||
78a072e1b56619e3230735ae073668311232ec94vboxsync strncmp(p, lp->ctl_prefix, (size_t)(q - p)) == 0) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync for (p += lp->ctl_len; isspace(*p); p++)
08c4185261c17943cff6cc94522579696eeeb478vboxsync continue; /* skip prefix and next ws */
78a072e1b56619e3230735ae073668311232ec94vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync if ((q = strchr(p, '*')) == NULL)
08c4185261c17943cff6cc94522579696eeeb478vboxsync q = end; /* compare until end */
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync while (isspace(q[-1]))
78a072e1b56619e3230735ae073668311232ec94vboxsync q--; /* exclude trailing ws */
08c4185261c17943cff6cc94522579696eeeb478vboxsync
a6ab77f04b22f0de7691f50dfdee8196024ce26dvboxsync if ((hp = ctf_hash_lookup(lp->ctl_hash, fp, p,
034b3d77b1a08b77b1fa9bfd7489997421713f15vboxsync (size_t)(q - p))) == NULL) {
a6ab77f04b22f0de7691f50dfdee8196024ce26dvboxsync (void) ctf_set_errno(fp, ECTF_NOTYPE);
f001a45ec92f71f1e4c1015485fc1ddf84e8059cvboxsync goto err;
a6ab77f04b22f0de7691f50dfdee8196024ce26dvboxsync }
78a072e1b56619e3230735ae073668311232ec94vboxsync
9726c89eba6e777f3eb4d57f65ca6171a2241d29vboxsync type = hp->h_type;
08c4185261c17943cff6cc94522579696eeeb478vboxsync break;
08c4185261c17943cff6cc94522579696eeeb478vboxsync }
78a072e1b56619e3230735ae073668311232ec94vboxsync }
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (lp->ctl_prefix == NULL) {
08c4185261c17943cff6cc94522579696eeeb478vboxsync (void) ctf_set_errno(fp, ECTF_NOTYPE);
08c4185261c17943cff6cc94522579696eeeb478vboxsync goto err;
08c4185261c17943cff6cc94522579696eeeb478vboxsync }
08c4185261c17943cff6cc94522579696eeeb478vboxsync }
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (*p != '\0' || type == 0)
08c4185261c17943cff6cc94522579696eeeb478vboxsync return (ctf_set_errno(fp, ECTF_SYNTAX));
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync return (type);
da31d917654e0b617e7a9bf8b0cf786136edf8e8vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsyncerr:
08c4185261c17943cff6cc94522579696eeeb478vboxsync if (fp->ctf_parent != NULL &&
08c4185261c17943cff6cc94522579696eeeb478vboxsync (ptype = ctf_lookup_by_name(fp->ctf_parent, name)) != CTF_ERR)
08c4185261c17943cff6cc94522579696eeeb478vboxsync return (ptype);
9726c89eba6e777f3eb4d57f65ca6171a2241d29vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync return (CTF_ERR);
da31d917654e0b617e7a9bf8b0cf786136edf8e8vboxsync}
08c4185261c17943cff6cc94522579696eeeb478vboxsync
08c4185261c17943cff6cc94522579696eeeb478vboxsync/*
08c4185261c17943cff6cc94522579696eeeb478vboxsync * Given a symbol table index, return the type of the data object described
78a072e1b56619e3230735ae073668311232ec94vboxsync * by the corresponding entry in the symbol table.
78a072e1b56619e3230735ae073668311232ec94vboxsync */
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_id_t
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_lookup_by_symbol(ctf_file_t *fp, ulong_t symidx)
78a072e1b56619e3230735ae073668311232ec94vboxsync{
78a072e1b56619e3230735ae073668311232ec94vboxsync const ctf_sect_t *sp = &fp->ctf_symtab;
78a072e1b56619e3230735ae073668311232ec94vboxsync ctf_id_t type;
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (sp->cts_data == NULL)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOSYMTAB));
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync if (symidx >= fp->ctf_nsyms)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, EINVAL));
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync if (sp->cts_entsize == sizeof (Elf32_Sym)) {
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOTDATA));
78a072e1b56619e3230735ae073668311232ec94vboxsync } else {
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
78a072e1b56619e3230735ae073668311232ec94vboxsync if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOTDATA));
78a072e1b56619e3230735ae073668311232ec94vboxsync }
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (fp->ctf_sxlate[symidx] == -1u)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync type = *(ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync if (type == 0)
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync return (ctf_set_errno(fp, ECTF_NOTYPEDAT));
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync return (type);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync}
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync/*
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * Return the pointer to the internal CTF type data corresponding to the
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * given type ID. If the ID is invalid, the function returns NULL.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync * This function is not exported outside of the library.
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync */
cc260ed3418d1fd2771d0395f818f76808b60238vboxsyncconst ctf_type_t *
cc260ed3418d1fd2771d0395f818f76808b60238vboxsyncctf_lookup_by_id(ctf_file_t **fpp, ctf_id_t type)
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync{
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync ctf_file_t *fp = *fpp; /* caller passes in starting CTF container */
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync if ((fp->ctf_flags & LCTF_CHILD) && CTF_TYPE_ISPARENT(type) &&
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync (fp = fp->ctf_parent) == NULL) {
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync (void) ctf_set_errno(*fpp, ECTF_NOPARENT);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync return (NULL);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync }
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync type = CTF_TYPE_TO_INDEX(type);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync if (type > 0 && type <= fp->ctf_typemax) {
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync *fpp = fp; /* function returns ending CTF container */
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync return (LCTF_INDEX_TO_TYPEPTR(fp, type));
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync }
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync (void) ctf_set_errno(fp, ECTF_BADID);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync return (NULL);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync}
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync/*
78a072e1b56619e3230735ae073668311232ec94vboxsync * Given a symbol table index, return the info for the function described
78a072e1b56619e3230735ae073668311232ec94vboxsync * by the corresponding entry in the symbol table.
78a072e1b56619e3230735ae073668311232ec94vboxsync */
78a072e1b56619e3230735ae073668311232ec94vboxsyncint
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_func_info(ctf_file_t *fp, ulong_t symidx, ctf_funcinfo_t *fip)
78a072e1b56619e3230735ae073668311232ec94vboxsync{
78a072e1b56619e3230735ae073668311232ec94vboxsync const ctf_sect_t *sp = &fp->ctf_symtab;
78a072e1b56619e3230735ae073668311232ec94vboxsync const ushort_t *dp;
78a072e1b56619e3230735ae073668311232ec94vboxsync ushort_t info, kind, n;
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (sp->cts_data == NULL)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOSYMTAB));
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (symidx >= fp->ctf_nsyms)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, EINVAL));
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (sp->cts_entsize == sizeof (Elf32_Sym)) {
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx;
78a072e1b56619e3230735ae073668311232ec94vboxsync if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOTFUNC));
78a072e1b56619e3230735ae073668311232ec94vboxsync } else {
78a072e1b56619e3230735ae073668311232ec94vboxsync const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx;
78a072e1b56619e3230735ae073668311232ec94vboxsync if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOTFUNC));
78a072e1b56619e3230735ae073668311232ec94vboxsync }
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (fp->ctf_sxlate[symidx] == -1u)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]);
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync info = *dp++;
78a072e1b56619e3230735ae073668311232ec94vboxsync kind = LCTF_INFO_KIND(fp, info);
cc260ed3418d1fd2771d0395f818f76808b60238vboxsync n = LCTF_INFO_VLEN(fp, info);
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (kind == CTF_K_UNKNOWN && n == 0)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_NOFUNCDAT));
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (kind != CTF_K_FUNCTION)
78a072e1b56619e3230735ae073668311232ec94vboxsync return (ctf_set_errno(fp, ECTF_CORRUPT));
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync fip->ctc_return = *dp++;
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync fip->ctc_argc = n;
78a072e1b56619e3230735ae073668311232ec94vboxsync fip->ctc_flags = 0;
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync if (n != 0 && dp[n - 1] == 0) {
78a072e1b56619e3230735ae073668311232ec94vboxsync fip->ctc_flags |= CTF_FUNC_VARARG;
78a072e1b56619e3230735ae073668311232ec94vboxsync fip->ctc_argc--;
78a072e1b56619e3230735ae073668311232ec94vboxsync }
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync return (0);
78a072e1b56619e3230735ae073668311232ec94vboxsync}
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync/*
78a072e1b56619e3230735ae073668311232ec94vboxsync * Given a symbol table index, return the arguments for the function described
78a072e1b56619e3230735ae073668311232ec94vboxsync * by the corresponding entry in the symbol table.
78a072e1b56619e3230735ae073668311232ec94vboxsync */
78a072e1b56619e3230735ae073668311232ec94vboxsyncint
78a072e1b56619e3230735ae073668311232ec94vboxsyncctf_func_args(ctf_file_t *fp, ulong_t symidx, uint_t argc, ctf_id_t *argv)
78a072e1b56619e3230735ae073668311232ec94vboxsync{
78a072e1b56619e3230735ae073668311232ec94vboxsync const ushort_t *dp;
f001a45ec92f71f1e4c1015485fc1ddf84e8059cvboxsync ctf_funcinfo_t f;
f001a45ec92f71f1e4c1015485fc1ddf84e8059cvboxsync
f001a45ec92f71f1e4c1015485fc1ddf84e8059cvboxsync if (ctf_func_info(fp, symidx, &f) == CTF_ERR)
f001a45ec92f71f1e4c1015485fc1ddf84e8059cvboxsync return (CTF_ERR); /* errno is set for us */
78a072e1b56619e3230735ae073668311232ec94vboxsync
78a072e1b56619e3230735ae073668311232ec94vboxsync /*
78a072e1b56619e3230735ae073668311232ec94vboxsync * The argument data is two ushort_t's past the translation table
78a072e1b56619e3230735ae073668311232ec94vboxsync * offset: one for the function info, and one for the return type.
78a072e1b56619e3230735ae073668311232ec94vboxsync */
78a072e1b56619e3230735ae073668311232ec94vboxsync dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]) + 2;
78a072e1b56619e3230735ae073668311232ec94vboxsync
ae072e31d733f2a7c9cb1b2b4c4901b66197aadavboxsync for (argc = MIN(argc, f.ctc_argc); argc != 0; argc--)
ae072e31d733f2a7c9cb1b2b4c4901b66197aadavboxsync *argv++ = *dp++;
ae072e31d733f2a7c9cb1b2b4c4901b66197aadavboxsync
ae072e31d733f2a7c9cb1b2b4c4901b66197aadavboxsync return (0);
78a072e1b56619e3230735ae073668311232ec94vboxsync}
78a072e1b56619e3230735ae073668311232ec94vboxsync