1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/sysmacros.h> 30*0Sstevel@tonic-gate #include <ctf_impl.h> 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate /* 33*0Sstevel@tonic-gate * Compare the given input string and length against a table of known C storage 34*0Sstevel@tonic-gate * qualifier keywords. We just ignore these in ctf_lookup_by_name, below. 35*0Sstevel@tonic-gate */ 36*0Sstevel@tonic-gate static int 37*0Sstevel@tonic-gate isqualifier(const char *s, size_t len) 38*0Sstevel@tonic-gate { 39*0Sstevel@tonic-gate static const struct qual { 40*0Sstevel@tonic-gate const char *q_name; 41*0Sstevel@tonic-gate size_t q_len; 42*0Sstevel@tonic-gate } q[] = { 43*0Sstevel@tonic-gate { "auto", 4 }, 44*0Sstevel@tonic-gate { "const", 5 }, 45*0Sstevel@tonic-gate { "extern", 6 }, 46*0Sstevel@tonic-gate { "register", 8 }, 47*0Sstevel@tonic-gate { "restrict", 8 }, 48*0Sstevel@tonic-gate { "_Restrict", 9 }, 49*0Sstevel@tonic-gate { "static", 6 }, 50*0Sstevel@tonic-gate { "volatile", 8 }, 51*0Sstevel@tonic-gate { NULL, 0 } 52*0Sstevel@tonic-gate }; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate int i; 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate for (i = 0; q[i].q_name != NULL; i++) { 57*0Sstevel@tonic-gate if (len == q[i].q_len && strncmp(s, q[i].q_name, len) == 0) 58*0Sstevel@tonic-gate return (1); 59*0Sstevel@tonic-gate } 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate return (0); 62*0Sstevel@tonic-gate } 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /* 65*0Sstevel@tonic-gate * Attempt to convert the given C type name into the corresponding CTF type ID. 66*0Sstevel@tonic-gate * It is not possible to do complete and proper conversion of type names 67*0Sstevel@tonic-gate * without implementing a more full-fledged parser, which is necessary to 68*0Sstevel@tonic-gate * handle things like types that are function pointers to functions that 69*0Sstevel@tonic-gate * have arguments that are function pointers, and fun stuff like that. 70*0Sstevel@tonic-gate * Instead, this function implements a very simple conversion algorithm that 71*0Sstevel@tonic-gate * finds the things that we actually care about: structs, unions, enums, 72*0Sstevel@tonic-gate * integers, floats, typedefs, and pointers to any of these named types. 73*0Sstevel@tonic-gate */ 74*0Sstevel@tonic-gate ctf_id_t 75*0Sstevel@tonic-gate ctf_lookup_by_name(ctf_file_t *fp, const char *name) 76*0Sstevel@tonic-gate { 77*0Sstevel@tonic-gate static const char delimiters[] = " \t\n\r\v\f*"; 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate const ctf_lookup_t *lp; 80*0Sstevel@tonic-gate const ctf_helem_t *hp; 81*0Sstevel@tonic-gate const char *p, *q, *end; 82*0Sstevel@tonic-gate ctf_id_t type = 0; 83*0Sstevel@tonic-gate ctf_id_t ntype, ptype; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate if (name == NULL) 86*0Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate for (p = name, end = name + strlen(name); *p != '\0'; p = q) { 89*0Sstevel@tonic-gate while (isspace(*p)) 90*0Sstevel@tonic-gate p++; /* skip leading ws */ 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate if (p == end) 93*0Sstevel@tonic-gate break; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate if ((q = strpbrk(p + 1, delimiters)) == NULL) 96*0Sstevel@tonic-gate q = end; /* compare until end */ 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate if (*p == '*') { 99*0Sstevel@tonic-gate /* 100*0Sstevel@tonic-gate * Find a pointer to type by looking in fp->ctf_ptrtab. 101*0Sstevel@tonic-gate * If we can't find a pointer to the given type, see if 102*0Sstevel@tonic-gate * we can compute a pointer to the type resulting from 103*0Sstevel@tonic-gate * resolving the type down to its base type and use 104*0Sstevel@tonic-gate * that instead. This helps with cases where the CTF 105*0Sstevel@tonic-gate * data includes "struct foo *" but not "foo_t *" and 106*0Sstevel@tonic-gate * the user tries to access "foo_t *" in the debugger. 107*0Sstevel@tonic-gate */ 108*0Sstevel@tonic-gate ntype = fp->ctf_ptrtab[CTF_TYPE_TO_INDEX(type)]; 109*0Sstevel@tonic-gate if (ntype == 0) { 110*0Sstevel@tonic-gate ntype = ctf_type_resolve(fp, type); 111*0Sstevel@tonic-gate if (ntype == CTF_ERR || (ntype = fp->ctf_ptrtab[ 112*0Sstevel@tonic-gate CTF_TYPE_TO_INDEX(ntype)]) == 0) { 113*0Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_NOTYPE); 114*0Sstevel@tonic-gate goto err; 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate type = CTF_INDEX_TO_TYPE(ntype, 119*0Sstevel@tonic-gate (fp->ctf_flags & LCTF_CHILD)); 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate q = p + 1; 122*0Sstevel@tonic-gate continue; 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate if (isqualifier(p, (size_t)(q - p))) 126*0Sstevel@tonic-gate continue; /* skip qualifier keyword */ 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate for (lp = fp->ctf_lookups; lp->ctl_prefix != NULL; lp++) { 129*0Sstevel@tonic-gate if (lp->ctl_prefix[0] == '\0' || 130*0Sstevel@tonic-gate strncmp(p, lp->ctl_prefix, (size_t)(q - p)) == 0) { 131*0Sstevel@tonic-gate for (p += lp->ctl_len; isspace(*p); p++) 132*0Sstevel@tonic-gate continue; /* skip prefix and next ws */ 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate if ((q = strchr(p, '*')) == NULL) 135*0Sstevel@tonic-gate q = end; /* compare until end */ 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate while (isspace(q[-1])) 138*0Sstevel@tonic-gate q--; /* exclude trailing ws */ 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate if ((hp = ctf_hash_lookup(lp->ctl_hash, fp, p, 141*0Sstevel@tonic-gate (size_t)(q - p))) == NULL) { 142*0Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_NOTYPE); 143*0Sstevel@tonic-gate goto err; 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate type = hp->h_type; 147*0Sstevel@tonic-gate break; 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate if (lp->ctl_prefix == NULL) { 152*0Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_NOTYPE); 153*0Sstevel@tonic-gate goto err; 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate if (*p != '\0' || type == 0) 158*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_SYNTAX)); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate return (type); 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate err: 163*0Sstevel@tonic-gate if (fp->ctf_parent != NULL && 164*0Sstevel@tonic-gate (ptype = ctf_lookup_by_name(fp->ctf_parent, name)) != CTF_ERR) 165*0Sstevel@tonic-gate return (ptype); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate return (CTF_ERR); 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate /* 171*0Sstevel@tonic-gate * Given a symbol table index, return the type of the data object described 172*0Sstevel@tonic-gate * by the corresponding entry in the symbol table. 173*0Sstevel@tonic-gate */ 174*0Sstevel@tonic-gate ctf_id_t 175*0Sstevel@tonic-gate ctf_lookup_by_symbol(ctf_file_t *fp, ulong_t symidx) 176*0Sstevel@tonic-gate { 177*0Sstevel@tonic-gate const ctf_sect_t *sp = &fp->ctf_symtab; 178*0Sstevel@tonic-gate ctf_id_t type; 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate if (sp->cts_data == NULL) 181*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOSYMTAB)); 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate if (symidx >= fp->ctf_nsyms) 184*0Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate if (sp->cts_entsize == sizeof (Elf32_Sym)) { 187*0Sstevel@tonic-gate const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx; 188*0Sstevel@tonic-gate if (ELF32_ST_TYPE(symp->st_info) != STT_OBJECT) 189*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTDATA)); 190*0Sstevel@tonic-gate } else { 191*0Sstevel@tonic-gate const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx; 192*0Sstevel@tonic-gate if (ELF64_ST_TYPE(symp->st_info) != STT_OBJECT) 193*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTDATA)); 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate if (fp->ctf_sxlate[symidx] == -1u) 197*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTYPEDAT)); 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate type = *(ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]); 200*0Sstevel@tonic-gate if (type == 0) 201*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTYPEDAT)); 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate return (type); 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate /* 207*0Sstevel@tonic-gate * Return the pointer to the internal CTF type data corresponding to the 208*0Sstevel@tonic-gate * given type ID. If the ID is invalid, the function returns NULL. 209*0Sstevel@tonic-gate * This function is not exported outside of the library. 210*0Sstevel@tonic-gate */ 211*0Sstevel@tonic-gate const ctf_type_t * 212*0Sstevel@tonic-gate ctf_lookup_by_id(ctf_file_t **fpp, ctf_id_t type) 213*0Sstevel@tonic-gate { 214*0Sstevel@tonic-gate ctf_file_t *fp = *fpp; /* caller passes in starting CTF container */ 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate if ((fp->ctf_flags & LCTF_CHILD) && CTF_TYPE_ISPARENT(type) && 217*0Sstevel@tonic-gate (fp = fp->ctf_parent) == NULL) { 218*0Sstevel@tonic-gate (void) ctf_set_errno(*fpp, ECTF_NOPARENT); 219*0Sstevel@tonic-gate return (NULL); 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate type = CTF_TYPE_TO_INDEX(type); 223*0Sstevel@tonic-gate if (type > 0 && type <= fp->ctf_typemax) { 224*0Sstevel@tonic-gate *fpp = fp; /* function returns ending CTF container */ 225*0Sstevel@tonic-gate return (LCTF_INDEX_TO_TYPEPTR(fp, type)); 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate (void) ctf_set_errno(fp, ECTF_BADID); 229*0Sstevel@tonic-gate return (NULL); 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate /* 233*0Sstevel@tonic-gate * Given a symbol table index, return the info for the function described 234*0Sstevel@tonic-gate * by the corresponding entry in the symbol table. 235*0Sstevel@tonic-gate */ 236*0Sstevel@tonic-gate int 237*0Sstevel@tonic-gate ctf_func_info(ctf_file_t *fp, ulong_t symidx, ctf_funcinfo_t *fip) 238*0Sstevel@tonic-gate { 239*0Sstevel@tonic-gate const ctf_sect_t *sp = &fp->ctf_symtab; 240*0Sstevel@tonic-gate const ushort_t *dp; 241*0Sstevel@tonic-gate ushort_t info, kind, n; 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate if (sp->cts_data == NULL) 244*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOSYMTAB)); 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate if (symidx >= fp->ctf_nsyms) 247*0Sstevel@tonic-gate return (ctf_set_errno(fp, EINVAL)); 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate if (sp->cts_entsize == sizeof (Elf32_Sym)) { 250*0Sstevel@tonic-gate const Elf32_Sym *symp = (Elf32_Sym *)sp->cts_data + symidx; 251*0Sstevel@tonic-gate if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC) 252*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTFUNC)); 253*0Sstevel@tonic-gate } else { 254*0Sstevel@tonic-gate const Elf64_Sym *symp = (Elf64_Sym *)sp->cts_data + symidx; 255*0Sstevel@tonic-gate if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC) 256*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOTFUNC)); 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate if (fp->ctf_sxlate[symidx] == -1u) 260*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOFUNCDAT)); 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]); 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate info = *dp++; 265*0Sstevel@tonic-gate kind = LCTF_INFO_KIND(fp, info); 266*0Sstevel@tonic-gate n = LCTF_INFO_VLEN(fp, info); 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate if (kind == CTF_K_UNKNOWN && n == 0) 269*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_NOFUNCDAT)); 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate if (kind != CTF_K_FUNCTION) 272*0Sstevel@tonic-gate return (ctf_set_errno(fp, ECTF_CORRUPT)); 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate fip->ctc_return = *dp++; 275*0Sstevel@tonic-gate fip->ctc_argc = n; 276*0Sstevel@tonic-gate fip->ctc_flags = 0; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate if (n != 0 && dp[n - 1] == 0) { 279*0Sstevel@tonic-gate fip->ctc_flags |= CTF_FUNC_VARARG; 280*0Sstevel@tonic-gate fip->ctc_argc--; 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate return (0); 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate /* 287*0Sstevel@tonic-gate * Given a symbol table index, return the arguments for the function described 288*0Sstevel@tonic-gate * by the corresponding entry in the symbol table. 289*0Sstevel@tonic-gate */ 290*0Sstevel@tonic-gate int 291*0Sstevel@tonic-gate ctf_func_args(ctf_file_t *fp, ulong_t symidx, uint_t argc, ctf_id_t *argv) 292*0Sstevel@tonic-gate { 293*0Sstevel@tonic-gate const ushort_t *dp; 294*0Sstevel@tonic-gate ctf_funcinfo_t f; 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate if (ctf_func_info(fp, symidx, &f) == CTF_ERR) 297*0Sstevel@tonic-gate return (CTF_ERR); /* errno is set for us */ 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate /* 300*0Sstevel@tonic-gate * The argument data is two ushort_t's past the translation table 301*0Sstevel@tonic-gate * offset: one for the function info, and one for the return type. 302*0Sstevel@tonic-gate */ 303*0Sstevel@tonic-gate dp = (ushort_t *)((uintptr_t)fp->ctf_buf + fp->ctf_sxlate[symidx]) + 2; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate for (argc = MIN(argc, f.ctc_argc); argc != 0; argc--) 306*0Sstevel@tonic-gate *argv++ = *dp++; 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate return (0); 309*0Sstevel@tonic-gate } 310