8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / ctf / cvt / stabs.c
blobf7b3034c640662a5442f66b6a3f29dc1b6f5518d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Routines used to read stabs data from a file, and to build a tdata structure
30 * based on the interesting parts of that data.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <assert.h>
38 #include <string.h>
39 #include <libgen.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
44 #include "ctftools.h"
45 #include "list.h"
46 #include "stack.h"
47 #include "memory.h"
48 #include "traverse.h"
50 const char *curhdr;
53 * The stabs generator will sometimes reference types before they've been
54 * defined. If this is the case, a TYPEDEF_UNRES tdesc will be generated.
55 * Note that this is different from a forward declaration, in which the
56 * stab is defined, but is defined as something that doesn't exist yet.
57 * When we have read all of the stabs from the file, we can go back and
58 * fix up all of the unresolved types. We should be able to fix all of them.
60 /*ARGSUSED2*/
61 static int
62 resolve_tou_node(tdesc_t *node, tdesc_t **nodep, void *private)
64 tdesc_t *new;
66 debug(3, "Trying to resolve %s (%d)\n", tdesc_name(node), node->t_id);
67 new = lookup(node->t_id);
69 if (new == NULL) {
70 terminate("Couldn't resolve type %d\n", node->t_id);
73 debug(3, " Resolving to %d\n", new->t_id);
75 *nodep = new;
77 return (1);
80 /*ARGSUSED*/
81 static int
82 resolve_fwd_node(tdesc_t *node, tdesc_t **nodep, void *private)
84 tdesc_t *new = lookupname(node->t_name);
86 debug(3, "Trying to unforward %s (%d)\n", tdesc_name(node), node->t_id);
88 if (!new || (new->t_type != STRUCT && new->t_type != UNION))
89 return (0);
91 debug(3, " Unforwarded to %d\n", new->t_id);
93 *nodep = new;
95 return (1);
98 static tdtrav_cb_f resolve_cbs[] = {
99 NULL,
100 NULL, /* intrinsic */
101 NULL, /* pointer */
102 NULL, /* array */
103 NULL, /* function */
104 NULL, /* struct */
105 NULL, /* union */
106 NULL, /* enum */
107 resolve_fwd_node, /* forward */
108 NULL, /* typedef */
109 resolve_tou_node, /* typedef unres */
110 NULL, /* volatile */
111 NULL, /* const */
112 NULL, /* restrict */
115 static void
116 resolve_nodes(tdata_t *td)
118 debug(2, "Resolving unresolved stabs\n");
120 (void) iitraverse_hash(td->td_iihash, &td->td_curvgen, resolve_cbs,
121 NULL, NULL, td);
124 static char *
125 concat(char *s1, char *s2, int s2strip)
127 int savelen = strlen(s2) - s2strip;
128 int newlen = (s1 ? strlen(s1) : 0) + savelen + 1;
129 char *out;
131 out = xrealloc(s1, newlen);
132 if (s1)
133 strncpy(out + strlen(out), s2, savelen);
134 else
135 strncpy(out, s2, savelen);
137 out[newlen - 1] = '\0';
139 return (out);
143 * N_FUN stabs come with their arguments in promoted form. In order to get the
144 * actual arguments, we need to wait for the N_PSYM stabs that will come towards
145 * the end of the function. These routines free the arguments (fnarg_free) we
146 * got from the N_FUN stab and add (fnarg_add) the ones from the N_PSYM stabs.
148 static void
149 fnarg_add(iidesc_t *curfun, iidesc_t *arg)
151 curfun->ii_nargs++;
153 if (curfun->ii_nargs == 1)
154 curfun->ii_args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
155 else if (curfun->ii_nargs > FUNCARG_DEF) {
156 curfun->ii_args = xrealloc(curfun->ii_args,
157 sizeof (tdesc_t *) * curfun->ii_nargs);
160 curfun->ii_args[curfun->ii_nargs - 1] = arg->ii_dtype;
161 arg->ii_dtype = NULL;
164 static void
165 fnarg_free(iidesc_t *ii)
167 ii->ii_nargs = 0;
168 free(ii->ii_args);
169 ii->ii_args = NULL;
173 * Read the stabs from the stab ELF section, and turn them into a tdesc tree,
174 * assembled under an iidesc list.
177 stabs_read(tdata_t *td, Elf *elf, const char *file)
179 Elf_Scn *scn;
180 Elf_Data *data;
181 stab_t *stab;
182 stk_t *file_stack;
183 iidesc_t *iidescp;
184 iidesc_t *curfun = NULL;
185 char curpath[MAXPATHLEN];
186 char *curfile = NULL;
187 char *str;
188 char *fstr = NULL, *ofstr = NULL;
189 int stabidx, stabstridx;
190 int nstabs, rc, i;
191 int scope = 0;
193 if (!((stabidx = findelfsecidx(elf, file, ".stab.excl")) >= 0 &&
194 (stabstridx = findelfsecidx(elf, file, ".stab.exclstr")) >= 0) &&
195 !((stabidx = findelfsecidx(elf, file, ".stab")) >= 0 &&
196 (stabstridx = findelfsecidx(elf, file, ".stabstr")) >= 0)) {
197 errno = ENOENT;
198 return (-1);
201 file_stack = stack_new(free);
203 stack_push(file_stack, (void *)file);
204 curhdr = file;
206 debug(3, "Found stabs in %d, strings in %d\n", stabidx, stabstridx);
208 scn = elf_getscn(elf, stabidx);
209 data = elf_rawdata(scn, NULL);
210 nstabs = data->d_size / sizeof (stab_t);
212 parse_init(td);
213 for (i = 0; i < nstabs; i++) {
214 stab = &((stab_t *)data->d_buf)[i];
216 /* We don't want any local definitions */
217 if (stab->n_type == N_LBRAC) {
218 scope++;
219 debug(3, "stab %d: opening scope (%d)\n", i + 1, scope);
220 continue;
221 } else if (stab->n_type == N_RBRAC) {
222 scope--;
223 debug(3, "stab %d: closing scope (%d)\n", i + 1, scope);
224 continue;
225 } else if (stab->n_type == N_EINCL) {
227 * There's a bug in the 5.2 (Taz) compilers that causes
228 * them to emit an extra N_EINCL if there's no actual
229 * text in the file being compiled. To work around this
230 * bug, we explicitly check to make sure we're not
231 * trying to pop a stack that only has the outer scope
232 * on it.
234 if (stack_level(file_stack) != 1) {
235 str = (char *)stack_pop(file_stack);
236 free(str);
237 curhdr = (char *)stack_peek(file_stack);
241 /* We only care about a subset of the stabs */
242 if (!(stab->n_type == N_FUN || stab->n_type == N_GSYM ||
243 stab->n_type == N_LCSYM || stab->n_type == N_LSYM ||
244 stab->n_type == N_PSYM || stab->n_type == N_ROSYM ||
245 stab->n_type == N_RSYM ||
246 stab->n_type == N_STSYM || stab->n_type == N_BINCL ||
247 stab->n_type == N_SO || stab->n_type == N_OPT))
248 continue;
250 if ((str = elf_strptr(elf, stabstridx,
251 (size_t)stab->n_strx)) == NULL) {
252 terminate("%s: Can't find string at %u for stab %d\n",
253 file, stab->n_strx, i);
256 if (stab->n_type == N_BINCL) {
257 curhdr = xstrdup(str);
258 stack_push(file_stack, (void *)curhdr);
259 continue;
260 } else if (stab->n_type == N_SO) {
261 if (str[strlen(str) - 1] != '/') {
262 strcpy(curpath, str);
263 curfile = basename(curpath);
265 continue;
266 } else if (stab->n_type == N_OPT) {
267 if (strcmp(str, "gcc2_compiled.") == 0) {
268 terminate("%s: GCC-generated stabs are "
269 "unsupported. Use DWARF instead.\n", file);
271 continue;
274 if (str[strlen(str) - 1] == '\\') {
275 int offset = 1;
277 * There's a bug in the compilers that causes them to
278 * generate \ for continuations with just -g (this is
279 * ok), and \\ for continuations with -g -O (this is
280 * broken). This bug is "fixed" in the 6.2 compilers
281 * via the elimination of continuation stabs.
283 if (str[strlen(str) - 2] == '\\')
284 offset = 2;
285 fstr = concat(fstr, str, offset);
286 continue;
287 } else
288 fstr = concat(fstr, str, 0);
290 debug(3, "%4d: .stabs \"%s\", %#x, %d, %hd, %d (from %s)\n", i,
291 fstr, stab->n_type, 0, stab->n_desc,
292 stab->n_value, curhdr);
294 if (debug_level >= 3)
295 check_hash();
298 * Sometimes the compiler stutters, and emits the same stab
299 * twice. This is bad for the parser, which will attempt to
300 * redefine the type IDs indicated in the stabs. This is
301 * compiler bug 4433511.
303 if (ofstr && strcmp(fstr, ofstr) == 0) {
304 debug(3, "Stutter stab\n");
305 free(fstr);
306 fstr = NULL;
307 continue;
310 if (ofstr)
311 free(ofstr);
312 ofstr = fstr;
314 iidescp = NULL;
316 if ((rc = parse_stab(stab, fstr, &iidescp)) < 0) {
317 terminate("%s: Couldn't parse stab \"%s\" "
318 "(source file %s)\n", file, str, curhdr);
321 if (rc == 0)
322 goto parse_loop_end;
324 /* Make sure the scope tracking is working correctly */
325 assert(stab->n_type != N_FUN || (iidescp->ii_type != II_GFUN &&
326 iidescp->ii_type != II_SFUN) || scope == 0);
329 * The only things we care about that are in local scope are
330 * the N_PSYM stabs.
332 if (scope && stab->n_type != N_PSYM) {
333 if (iidescp)
334 iidesc_free(iidescp, NULL);
335 goto parse_loop_end;
338 switch (iidescp->ii_type) {
339 case II_SFUN:
340 iidescp->ii_owner = xstrdup(curfile);
341 /*FALLTHROUGH*/
342 case II_GFUN:
343 curfun = iidescp;
344 fnarg_free(iidescp);
345 iidesc_add(td->td_iihash, iidescp);
346 break;
348 case II_SVAR:
349 iidescp->ii_owner = xstrdup(curfile);
350 /*FALLTHROUGH*/
351 case II_GVAR:
352 case II_TYPE:
353 case II_SOU:
354 iidesc_add(td->td_iihash, iidescp);
355 break;
357 case II_PSYM:
358 fnarg_add(curfun, iidescp);
359 iidesc_free(iidescp, NULL);
360 break;
361 default:
362 aborterr("invalid ii_type %d for stab type %d",
363 iidescp->ii_type, stab->n_type);
366 parse_loop_end:
367 fstr = NULL;
370 if (ofstr)
371 free(ofstr);
373 resolve_nodes(td);
374 resolve_typed_bitfields();
375 parse_finish(td);
377 cvt_fixstabs(td);
378 cvt_fixups(td, elf_ptrsz(elf));
380 return (0);