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]
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.
41 #include <sys/types.h>
42 #include <sys/param.h>
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.
62 resolve_tou_node(tdesc_t
*node
, tdesc_t
**nodep
, void *private)
66 debug(3, "Trying to resolve %s (%d)\n", tdesc_name(node
), node
->t_id
);
67 new = lookup(node
->t_id
);
70 terminate("Couldn't resolve type %d\n", node
->t_id
);
73 debug(3, " Resolving to %d\n", new->t_id
);
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
))
91 debug(3, " Unforwarded to %d\n", new->t_id
);
98 static tdtrav_cb_f resolve_cbs
[] = {
100 NULL
, /* intrinsic */
107 resolve_fwd_node
, /* forward */
109 resolve_tou_node
, /* typedef unres */
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
,
125 concat(char *s1
, char *s2
, int s2strip
)
127 int savelen
= strlen(s2
) - s2strip
;
128 int newlen
= (s1
? strlen(s1
) : 0) + savelen
+ 1;
131 out
= xrealloc(s1
, newlen
);
133 strncpy(out
+ strlen(out
), s2
, savelen
);
135 strncpy(out
, s2
, savelen
);
137 out
[newlen
- 1] = '\0';
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.
149 fnarg_add(iidesc_t
*curfun
, iidesc_t
*arg
)
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
;
165 fnarg_free(iidesc_t
*ii
)
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
)
184 iidesc_t
*curfun
= NULL
;
185 char curpath
[MAXPATHLEN
];
186 char *curfile
= NULL
;
188 char *fstr
= NULL
, *ofstr
= NULL
;
189 int stabidx
, stabstridx
;
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)) {
201 file_stack
= stack_new(free
);
203 stack_push(file_stack
, (void *)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
);
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
) {
219 debug(3, "stab %d: opening scope (%d)\n", i
+ 1, scope
);
221 } else if (stab
->n_type
== N_RBRAC
) {
223 debug(3, "stab %d: closing scope (%d)\n", i
+ 1, scope
);
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
234 if (stack_level(file_stack
) != 1) {
235 str
= (char *)stack_pop(file_stack
);
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
))
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
);
260 } else if (stab
->n_type
== N_SO
) {
261 if (str
[strlen(str
) - 1] != '/') {
262 strcpy(curpath
, str
);
263 curfile
= basename(curpath
);
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
);
274 if (str
[strlen(str
) - 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] == '\\')
285 fstr
= concat(fstr
, str
, offset
);
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)
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");
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
);
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
332 if (scope
&& stab
->n_type
!= N_PSYM
) {
334 iidesc_free(iidescp
, NULL
);
338 switch (iidescp
->ii_type
) {
340 iidescp
->ii_owner
= xstrdup(curfile
);
345 iidesc_add(td
->td_iihash
, iidescp
);
349 iidescp
->ii_owner
= xstrdup(curfile
);
354 iidesc_add(td
->td_iihash
, iidescp
);
358 fnarg_add(curfun
, iidescp
);
359 iidesc_free(iidescp
, NULL
);
362 aborterr("invalid ii_type %d for stab type %d",
363 iidescp
->ii_type
, stab
->n_type
);
374 resolve_typed_bitfields();
378 cvt_fixups(td
, elf_ptrsz(elf
));