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 51882Sjohnlev * Common Development and Distribution License (the "License"). 61882Sjohnlev * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221882Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Routines used to read stabs data from a file, and to build a tdata structure 300Sstevel@tonic-gate * based on the interesting parts of that data. 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <stdio.h> 340Sstevel@tonic-gate #include <stdlib.h> 350Sstevel@tonic-gate #include <fcntl.h> 360Sstevel@tonic-gate #include <unistd.h> 370Sstevel@tonic-gate #include <assert.h> 380Sstevel@tonic-gate #include <string.h> 390Sstevel@tonic-gate #include <libgen.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate #include <sys/types.h> 420Sstevel@tonic-gate #include <sys/param.h> 430Sstevel@tonic-gate 440Sstevel@tonic-gate #include "ctftools.h" 450Sstevel@tonic-gate #include "list.h" 460Sstevel@tonic-gate #include "stack.h" 470Sstevel@tonic-gate #include "memory.h" 480Sstevel@tonic-gate #include "traverse.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate const char *curhdr; 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * The stabs generator will sometimes reference types before they've been 540Sstevel@tonic-gate * defined. If this is the case, a TYPEDEF_UNRES tdesc will be generated. 550Sstevel@tonic-gate * Note that this is different from a forward declaration, in which the 560Sstevel@tonic-gate * stab is defined, but is defined as something that doesn't exist yet. 570Sstevel@tonic-gate * When we have read all of the stabs from the file, we can go back and 580Sstevel@tonic-gate * fix up all of the unresolved types. We should be able to fix all of them. 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate /*ARGSUSED2*/ 610Sstevel@tonic-gate static int 620Sstevel@tonic-gate resolve_tou_node(tdesc_t *node, tdesc_t **nodep, void *private) 630Sstevel@tonic-gate { 640Sstevel@tonic-gate tdesc_t *new; 650Sstevel@tonic-gate 661882Sjohnlev debug(3, "Trying to resolve %s (%d)\n", tdesc_name(node), node->t_id); 670Sstevel@tonic-gate new = lookup(node->t_id); 680Sstevel@tonic-gate 690Sstevel@tonic-gate if (new == NULL) { 700Sstevel@tonic-gate terminate("Couldn't resolve type %d\n", node->t_id); 710Sstevel@tonic-gate } 720Sstevel@tonic-gate 730Sstevel@tonic-gate debug(3, " Resolving to %d\n", new->t_id); 740Sstevel@tonic-gate 750Sstevel@tonic-gate *nodep = new; 760Sstevel@tonic-gate 770Sstevel@tonic-gate return (1); 780Sstevel@tonic-gate } 790Sstevel@tonic-gate 800Sstevel@tonic-gate /*ARGSUSED*/ 810Sstevel@tonic-gate static int 820Sstevel@tonic-gate resolve_fwd_node(tdesc_t *node, tdesc_t **nodep, void *private) 830Sstevel@tonic-gate { 840Sstevel@tonic-gate tdesc_t *new = lookupname(node->t_name); 850Sstevel@tonic-gate 861882Sjohnlev debug(3, "Trying to unforward %s (%d)\n", tdesc_name(node), node->t_id); 870Sstevel@tonic-gate 880Sstevel@tonic-gate if (!new || (new->t_type != STRUCT && new->t_type != UNION)) 890Sstevel@tonic-gate return (0); 900Sstevel@tonic-gate 910Sstevel@tonic-gate debug(3, " Unforwarded to %d\n", new->t_id); 920Sstevel@tonic-gate 930Sstevel@tonic-gate *nodep = new; 940Sstevel@tonic-gate 950Sstevel@tonic-gate return (1); 960Sstevel@tonic-gate } 970Sstevel@tonic-gate 980Sstevel@tonic-gate static tdtrav_cb_f resolve_cbs[] = { 990Sstevel@tonic-gate NULL, 1000Sstevel@tonic-gate NULL, /* intrinsic */ 1010Sstevel@tonic-gate NULL, /* pointer */ 1020Sstevel@tonic-gate NULL, /* array */ 1030Sstevel@tonic-gate NULL, /* function */ 1040Sstevel@tonic-gate NULL, /* struct */ 1050Sstevel@tonic-gate NULL, /* union */ 1060Sstevel@tonic-gate NULL, /* enum */ 1070Sstevel@tonic-gate resolve_fwd_node, /* forward */ 1080Sstevel@tonic-gate NULL, /* typedef */ 1090Sstevel@tonic-gate resolve_tou_node, /* typedef unres */ 1100Sstevel@tonic-gate NULL, /* volatile */ 1110Sstevel@tonic-gate NULL, /* const */ 1120Sstevel@tonic-gate NULL, /* restrict */ 1130Sstevel@tonic-gate }; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate static void 1160Sstevel@tonic-gate resolve_nodes(tdata_t *td) 1170Sstevel@tonic-gate { 1180Sstevel@tonic-gate debug(2, "Resolving unresolved stabs\n"); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate (void) iitraverse_hash(td->td_iihash, &td->td_curvgen, resolve_cbs, 1210Sstevel@tonic-gate NULL, NULL, td); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate static char * 1250Sstevel@tonic-gate concat(char *s1, char *s2, int s2strip) 1260Sstevel@tonic-gate { 1270Sstevel@tonic-gate int savelen = strlen(s2) - s2strip; 1280Sstevel@tonic-gate int newlen = (s1 ? strlen(s1) : 0) + savelen + 1; 1290Sstevel@tonic-gate char *out; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate out = xrealloc(s1, newlen); 1320Sstevel@tonic-gate if (s1) 1330Sstevel@tonic-gate strncpy(out + strlen(out), s2, savelen); 1340Sstevel@tonic-gate else 1350Sstevel@tonic-gate strncpy(out, s2, savelen); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate out[newlen - 1] = '\0'; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate return (out); 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * N_FUN stabs come with their arguments in promoted form. In order to get the 1440Sstevel@tonic-gate * actual arguments, we need to wait for the N_PSYM stabs that will come towards 1450Sstevel@tonic-gate * the end of the function. These routines free the arguments (fnarg_free) we 1460Sstevel@tonic-gate * got from the N_FUN stab and add (fnarg_add) the ones from the N_PSYM stabs. 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate static void 1490Sstevel@tonic-gate fnarg_add(iidesc_t *curfun, iidesc_t *arg) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate curfun->ii_nargs++; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate if (curfun->ii_nargs == 1) 1540Sstevel@tonic-gate curfun->ii_args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF); 1550Sstevel@tonic-gate else if (curfun->ii_nargs > FUNCARG_DEF) { 1560Sstevel@tonic-gate curfun->ii_args = xrealloc(curfun->ii_args, 1570Sstevel@tonic-gate sizeof (tdesc_t *) * curfun->ii_nargs); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate curfun->ii_args[curfun->ii_nargs - 1] = arg->ii_dtype; 1610Sstevel@tonic-gate arg->ii_dtype = NULL; 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static void 1650Sstevel@tonic-gate fnarg_free(iidesc_t *ii) 1660Sstevel@tonic-gate { 1670Sstevel@tonic-gate ii->ii_nargs = 0; 1680Sstevel@tonic-gate free(ii->ii_args); 1690Sstevel@tonic-gate ii->ii_args = NULL; 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* 1730Sstevel@tonic-gate * Read the stabs from the stab ELF section, and turn them into a tdesc tree, 1740Sstevel@tonic-gate * assembled under an iidesc list. 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate int 177*1951Sjohnlev stabs_read(tdata_t *td, Elf *elf, const char *file) 1780Sstevel@tonic-gate { 1790Sstevel@tonic-gate Elf_Scn *scn; 1800Sstevel@tonic-gate Elf_Data *data; 1810Sstevel@tonic-gate stab_t *stab; 1820Sstevel@tonic-gate stk_t *file_stack; 1830Sstevel@tonic-gate iidesc_t *iidescp; 1840Sstevel@tonic-gate iidesc_t *curfun = NULL; 1850Sstevel@tonic-gate char curpath[MAXPATHLEN]; 1860Sstevel@tonic-gate char *curfile = NULL; 1870Sstevel@tonic-gate char *str; 1880Sstevel@tonic-gate char *fstr = NULL, *ofstr = NULL; 1890Sstevel@tonic-gate int stabidx, stabstridx; 1900Sstevel@tonic-gate int nstabs, rc, i; 1910Sstevel@tonic-gate int scope = 0; 1920Sstevel@tonic-gate 193*1951Sjohnlev if (!((stabidx = findelfsecidx(elf, file, ".stab.excl")) >= 0 && 194*1951Sjohnlev (stabstridx = findelfsecidx(elf, file, ".stab.exclstr")) >= 0) && 195*1951Sjohnlev !((stabidx = findelfsecidx(elf, file, ".stab")) >= 0 && 196*1951Sjohnlev (stabstridx = findelfsecidx(elf, file, ".stabstr")) >= 0)) { 1970Sstevel@tonic-gate errno = ENOENT; 1980Sstevel@tonic-gate return (-1); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate file_stack = stack_new(free); 2020Sstevel@tonic-gate 203*1951Sjohnlev stack_push(file_stack, (void *)file); 204*1951Sjohnlev curhdr = file; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate debug(3, "Found stabs in %d, strings in %d\n", stabidx, stabstridx); 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate scn = elf_getscn(elf, stabidx); 2090Sstevel@tonic-gate data = elf_rawdata(scn, NULL); 2100Sstevel@tonic-gate nstabs = data->d_size / sizeof (stab_t); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate parse_init(td); 2130Sstevel@tonic-gate for (i = 0; i < nstabs; i++) { 2140Sstevel@tonic-gate stab = &((stab_t *)data->d_buf)[i]; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* We don't want any local definitions */ 2170Sstevel@tonic-gate if (stab->n_type == N_LBRAC) { 2180Sstevel@tonic-gate scope++; 2190Sstevel@tonic-gate debug(3, "stab %d: opening scope (%d)\n", i + 1, scope); 2200Sstevel@tonic-gate continue; 2210Sstevel@tonic-gate } else if (stab->n_type == N_RBRAC) { 2220Sstevel@tonic-gate scope--; 2230Sstevel@tonic-gate debug(3, "stab %d: closing scope (%d)\n", i + 1, scope); 2240Sstevel@tonic-gate continue; 2250Sstevel@tonic-gate } else if (stab->n_type == N_EINCL) { 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * There's a bug in the 5.2 (Taz) compilers that causes 2280Sstevel@tonic-gate * them to emit an extra N_EINCL if there's no actual 2290Sstevel@tonic-gate * text in the file being compiled. To work around this 2300Sstevel@tonic-gate * bug, we explicitly check to make sure we're not 2310Sstevel@tonic-gate * trying to pop a stack that only has the outer scope 2320Sstevel@tonic-gate * on it. 2330Sstevel@tonic-gate */ 2340Sstevel@tonic-gate if (stack_level(file_stack) != 1) { 2350Sstevel@tonic-gate str = (char *)stack_pop(file_stack); 2360Sstevel@tonic-gate free(str); 2370Sstevel@tonic-gate curhdr = (char *)stack_peek(file_stack); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* We only care about a subset of the stabs */ 2420Sstevel@tonic-gate if (!(stab->n_type == N_FUN || stab->n_type == N_GSYM || 2430Sstevel@tonic-gate stab->n_type == N_LCSYM || stab->n_type == N_LSYM || 2440Sstevel@tonic-gate stab->n_type == N_PSYM || stab->n_type == N_ROSYM || 2450Sstevel@tonic-gate stab->n_type == N_RSYM || 2460Sstevel@tonic-gate stab->n_type == N_STSYM || stab->n_type == N_BINCL || 2470Sstevel@tonic-gate stab->n_type == N_SO || stab->n_type == N_OPT)) 2480Sstevel@tonic-gate continue; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate if ((str = elf_strptr(elf, stabstridx, 2510Sstevel@tonic-gate (size_t)stab->n_strx)) == NULL) { 252*1951Sjohnlev terminate("%s: Can't find string at %u for stab %d\n", 253*1951Sjohnlev file, stab->n_strx, i); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if (stab->n_type == N_BINCL) { 2570Sstevel@tonic-gate curhdr = xstrdup(str); 2580Sstevel@tonic-gate stack_push(file_stack, (void *)curhdr); 2590Sstevel@tonic-gate continue; 2600Sstevel@tonic-gate } else if (stab->n_type == N_SO) { 2610Sstevel@tonic-gate if (str[strlen(str) - 1] != '/') { 2620Sstevel@tonic-gate strcpy(curpath, str); 2630Sstevel@tonic-gate curfile = basename(curpath); 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate continue; 2660Sstevel@tonic-gate } else if (stab->n_type == N_OPT) { 2670Sstevel@tonic-gate if (strcmp(str, "gcc2_compiled.") == 0) { 268*1951Sjohnlev terminate("%s: GCC-generated stabs are " 269*1951Sjohnlev "unsupported. Use DWARF instead.\n", file); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate continue; 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if (str[strlen(str) - 1] == '\\') { 2750Sstevel@tonic-gate int offset = 1; 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * There's a bug in the compilers that causes them to 2780Sstevel@tonic-gate * generate \ for continuations with just -g (this is 2790Sstevel@tonic-gate * ok), and \\ for continuations with -g -O (this is 2800Sstevel@tonic-gate * broken). This bug is "fixed" in the 6.2 compilers 2810Sstevel@tonic-gate * via the elimination of continuation stabs. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate if (str[strlen(str) - 2] == '\\') 2840Sstevel@tonic-gate offset = 2; 2850Sstevel@tonic-gate fstr = concat(fstr, str, offset); 2860Sstevel@tonic-gate continue; 2870Sstevel@tonic-gate } else 2880Sstevel@tonic-gate fstr = concat(fstr, str, 0); 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate debug(3, "%4d: .stabs \"%s\", %#x, %d, %hd, %d (from %s)\n", i, 2910Sstevel@tonic-gate fstr, stab->n_type, 0, stab->n_desc, 2920Sstevel@tonic-gate stab->n_value, curhdr); 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate if (debug_level >= 3) 2950Sstevel@tonic-gate check_hash(); 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* 2980Sstevel@tonic-gate * Sometimes the compiler stutters, and emits the same stab 2990Sstevel@tonic-gate * twice. This is bad for the parser, which will attempt to 3000Sstevel@tonic-gate * redefine the type IDs indicated in the stabs. This is 3010Sstevel@tonic-gate * compiler bug 4433511. 3020Sstevel@tonic-gate */ 3030Sstevel@tonic-gate if (ofstr && strcmp(fstr, ofstr) == 0) { 3040Sstevel@tonic-gate debug(3, "Stutter stab\n"); 3050Sstevel@tonic-gate free(fstr); 3060Sstevel@tonic-gate fstr = NULL; 3070Sstevel@tonic-gate continue; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (ofstr) 3110Sstevel@tonic-gate free(ofstr); 3120Sstevel@tonic-gate ofstr = fstr; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate iidescp = NULL; 315*1951Sjohnlev 316*1951Sjohnlev if ((rc = parse_stab(stab, fstr, &iidescp)) < 0) { 317*1951Sjohnlev terminate("%s: Couldn't parse stab \"%s\" " 318*1951Sjohnlev "(source file %s)\n", file, str, curhdr); 319*1951Sjohnlev } 320*1951Sjohnlev 3210Sstevel@tonic-gate if (rc == 0) 3220Sstevel@tonic-gate goto parse_loop_end; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* Make sure the scope tracking is working correctly */ 3250Sstevel@tonic-gate assert(stab->n_type != N_FUN || (iidescp->ii_type != II_GFUN && 3260Sstevel@tonic-gate iidescp->ii_type != II_SFUN) || scope == 0); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * The only things we care about that are in local scope are 3300Sstevel@tonic-gate * the N_PSYM stabs. 3310Sstevel@tonic-gate */ 3320Sstevel@tonic-gate if (scope && stab->n_type != N_PSYM) { 3330Sstevel@tonic-gate if (iidescp) 3340Sstevel@tonic-gate iidesc_free(iidescp, NULL); 3350Sstevel@tonic-gate goto parse_loop_end; 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate switch (iidescp->ii_type) { 3390Sstevel@tonic-gate case II_SFUN: 3400Sstevel@tonic-gate iidescp->ii_owner = xstrdup(curfile); 3410Sstevel@tonic-gate /*FALLTHROUGH*/ 3420Sstevel@tonic-gate case II_GFUN: 3430Sstevel@tonic-gate curfun = iidescp; 3440Sstevel@tonic-gate fnarg_free(iidescp); 3450Sstevel@tonic-gate iidesc_add(td->td_iihash, iidescp); 3460Sstevel@tonic-gate break; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate case II_SVAR: 3490Sstevel@tonic-gate iidescp->ii_owner = xstrdup(curfile); 3500Sstevel@tonic-gate /*FALLTHROUGH*/ 3510Sstevel@tonic-gate case II_GVAR: 3520Sstevel@tonic-gate case II_TYPE: 3530Sstevel@tonic-gate case II_SOU: 3540Sstevel@tonic-gate iidesc_add(td->td_iihash, iidescp); 3550Sstevel@tonic-gate break; 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate case II_PSYM: 3580Sstevel@tonic-gate fnarg_add(curfun, iidescp); 3590Sstevel@tonic-gate iidesc_free(iidescp, NULL); 3600Sstevel@tonic-gate break; 3610Sstevel@tonic-gate default: 362*1951Sjohnlev aborterr("invalid ii_type %d for stab type %d", 363*1951Sjohnlev iidescp->ii_type, stab->n_type); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate parse_loop_end: 3670Sstevel@tonic-gate fstr = NULL; 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate if (ofstr) 3710Sstevel@tonic-gate free(ofstr); 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate resolve_nodes(td); 3740Sstevel@tonic-gate resolve_typed_bitfields(); 3750Sstevel@tonic-gate parse_finish(td); 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate cvt_fixbugs(td); 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate return (0); 3800Sstevel@tonic-gate } 381