xref: /titanic_44/usr/src/tools/ctf/cvt/st_parse.c (revision e429788e241121c1f81089f762558027000ea25f)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * This file is a sewer.
30  */
31 
32 #include <limits.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <assert.h>
36 #include <strings.h>
37 #include <setjmp.h>
38 #include <ctype.h>
39 #include <uts/common/sys/ctf.h>
40 
41 #include "ctftools.h"
42 #include "memory.h"
43 #include "list.h"
44 
45 #define	HASH(NUM)	((int)(NUM & (BUCKETS - 1)))
46 #define	BUCKETS		128
47 
48 #define	TYPEPAIRMULT	10000
49 #define	MAKETYPEID(file, num)	((file) * TYPEPAIRMULT + num)
50 #define	TYPEFILE(tid)		((tid) / TYPEPAIRMULT)
51 #define	TYPENUM(tid)		((tid) % TYPEPAIRMULT)
52 
53 #define	expected(a, b, c) _expected(a, b, c, __LINE__)
54 
55 static int faketypenumber = 100000000;
56 
57 static tdesc_t *hash_table[BUCKETS];
58 static tdesc_t *name_table[BUCKETS];
59 
60 list_t *typedbitfldmems;
61 
62 static void reset(void);
63 static jmp_buf	resetbuf;
64 
65 static char *soudef(char *cp, stabtype_t type, tdesc_t **rtdp);
66 static void enumdef(char *cp, tdesc_t **rtdp);
67 static int compute_sum(char *w);
68 tdesc_t *lookupname(char *name);
69 
70 static char *number(char *cp, int *n);
71 static char *name(char *cp, char **w);
72 static char *id(char *cp, int *h);
73 static char *whitesp(char *cp);
74 static void addhash(tdesc_t *tdp, int num);
75 static int tagadd(char *w, int h, tdesc_t *tdp);
76 static char *tdefdecl(char *cp, int h, tdesc_t **rtdp);
77 static char *intrinsic(char *cp, tdesc_t **rtdp);
78 static char *arraydef(char *cp, tdesc_t **rtdp);
79 
80 extern int debug_level;
81 int debug_parse = DEBUG_PARSE;
82 
83 /*PRINTFLIKE3*/
84 static void
85 parse_debug(int level, char *cp, char *fmt, ...)
86 {
87 	va_list ap;
88 	char buf[1024];
89 	char tmp[32];
90 	int i;
91 
92 	if (level > debug_level || !debug_parse)
93 		return;
94 
95 	if (cp != NULL) {
96 		for (i = 0; i < 30; i++) {
97 			if (cp[i] == '\0')
98 				break;
99 			if (!iscntrl(cp[i]))
100 				tmp[i] = cp[i];
101 		}
102 		tmp[i] = '\0';
103 		(void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp);
104 	} else {
105 		strcpy(buf, fmt);
106 		strcat(buf, "\n");
107 	}
108 
109 	va_start(ap, fmt);
110 	vadebug(level, buf, ap);
111 	va_end(ap);
112 }
113 
114 /* Report unexpected syntax in stabs. */
115 static void
116 _expected(
117 	char *who,	/* what function, or part thereof, is reporting */
118 	char *what,	/* what was expected */
119 	char *where,	/* where we were in the line of input */
120 	int line)
121 {
122 	fprintf(stderr, "%s, expecting \"%s\" at \"%s\"\n", who, what, where);
123 	fprintf(stderr, "code line: %d, file %s\n", line,
124 	    (curhdr ? curhdr : "NO FILE"));
125 	reset();
126 }
127 
128 /*ARGSUSED*/
129 void
130 parse_init(tdata_t *td)
131 {
132 	int i;
133 
134 	for (i = 0; i < BUCKETS; i++) {
135 		hash_table[i] = NULL;
136 		name_table[i] = NULL;
137 	}
138 
139 	if (typedbitfldmems != NULL) {
140 		list_free(typedbitfldmems, NULL, NULL);
141 		typedbitfldmems = NULL;
142 	}
143 }
144 
145 void
146 parse_finish(tdata_t *td)
147 {
148 	td->td_nextid = ++faketypenumber;
149 }
150 
151 static tdesc_t *
152 unres_new(int tid)
153 {
154 	tdesc_t *tdp;
155 
156 	tdp = xcalloc(sizeof (*tdp));
157 	tdp->t_type = TYPEDEF_UNRES;
158 	tdp->t_id = tid;
159 
160 	return (tdp);
161 }
162 
163 char *
164 read_tid(char *cp, tdesc_t **tdpp)
165 {
166 	tdesc_t *tdp;
167 	int tid;
168 
169 	cp = id(cp, &tid);
170 
171 	assert(tid != 0);
172 
173 	if (*cp == '=') {
174 		if (!(cp = tdefdecl(cp + 1, tid, &tdp)))
175 			return (NULL);
176 		if (tdp->t_id && tdp->t_id != tid) {
177 			tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
178 
179 			ntdp->t_type = TYPEDEF;
180 			ntdp->t_tdesc = tdp;
181 			tdp = ntdp;
182 		}
183 		addhash(tdp, tid);
184 	} else if ((tdp = lookup(tid)) == NULL)
185 		tdp = unres_new(tid);
186 
187 	*tdpp = tdp;
188 	return (cp);
189 }
190 
191 static iitype_t
192 parse_fun(char *cp, iidesc_t *ii)
193 {
194 	iitype_t iitype;
195 	tdesc_t *tdp;
196 	tdesc_t **args = NULL;
197 	int nargs = 0;
198 	int va = 0;
199 
200 	/*
201 	 * name:P		prototype
202 	 * name:F		global function
203 	 * name:f		static function
204 	 */
205 	switch (*cp++) {
206 	case 'P':
207 		iitype = II_NOT; /* not interesting */
208 		break;
209 
210 	case 'F':
211 		iitype = II_GFUN;
212 		break;
213 
214 	case 'f':
215 		iitype = II_SFUN;
216 		break;
217 
218 	default:
219 		expected("parse_nfun", "[PfF]", cp - 1);
220 	}
221 
222 	if (!(cp = read_tid(cp, &tdp)))
223 		return (-1);
224 
225 	if (*cp)
226 		args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
227 
228 	while (*cp && *++cp) {
229 		if (*cp == '0') {
230 			va = 1;
231 			continue;
232 		}
233 
234 		nargs++;
235 		if (nargs > FUNCARG_DEF)
236 			args = xrealloc(args, sizeof (tdesc_t *) * nargs);
237 		if (!(cp = read_tid(cp, &args[nargs - 1])))
238 			return (-1);
239 	}
240 
241 	ii->ii_type = iitype;
242 	ii->ii_dtype = tdp;
243 	ii->ii_nargs = nargs;
244 	ii->ii_args = args;
245 	ii->ii_vargs = va;
246 
247 	return (iitype);
248 }
249 
250 static iitype_t
251 parse_sym(char *cp, iidesc_t *ii)
252 {
253 	tdesc_t *tdp;
254 	iitype_t iitype;
255 
256 	/*
257 	 * name:G		global variable
258 	 * name:S		static variable
259 	 */
260 	switch (*cp++) {
261 	case 'G':
262 		iitype = II_GVAR;
263 		break;
264 	case 'S':
265 		iitype = II_SVAR;
266 		break;
267 	case 'p':
268 		iitype = II_PSYM;
269 		break;
270 	case '(':
271 		cp--;
272 		/*FALLTHROUGH*/
273 	case 'r':
274 	case 'V':
275 		iitype = II_NOT; /* not interesting */
276 		break;
277 	default:
278 		expected("parse_sym", "[GprSV(]", cp - 1);
279 	}
280 
281 	if (!(cp = read_tid(cp, &tdp)))
282 		return (-1);
283 
284 	ii->ii_type = iitype;
285 	ii->ii_dtype = tdp;
286 
287 	return (iitype);
288 }
289 
290 static iitype_t
291 parse_type(char *cp, iidesc_t *ii)
292 {
293 	tdesc_t *tdp, *ntdp;
294 	int tid;
295 
296 	if (*cp++ != 't')
297 		expected("parse_type", "t (type)", cp - 1);
298 
299 	cp = id(cp, &tid);
300 	if ((tdp = lookup(tid)) == NULL) {
301 		if (*cp++ != '=')
302 			expected("parse_type", "= (definition)", cp - 1);
303 
304 		(void) tdefdecl(cp, tid, &tdp);
305 
306 		if (tdp->t_id == tid) {
307 			assert(tdp->t_type != TYPEDEF);
308 			assert(!lookup(tdp->t_id));
309 
310 			if (!streq(tdp->t_name, ii->ii_name)) {
311 				ntdp = xcalloc(sizeof (*ntdp));
312 				ntdp->t_name = xstrdup(ii->ii_name);
313 				ntdp->t_type = TYPEDEF;
314 				ntdp->t_tdesc = tdp;
315 				tdp->t_id = faketypenumber++;
316 				tdp = ntdp;
317 			}
318 		} else if (tdp->t_id == 0) {
319 			assert(tdp->t_type == FORWARD ||
320 			    tdp->t_type == INTRINSIC);
321 
322 			if (tdp->t_name && !streq(tdp->t_name, ii->ii_name)) {
323 				ntdp = xcalloc(sizeof (*ntdp));
324 				ntdp->t_name = xstrdup(ii->ii_name);
325 				ntdp->t_type = TYPEDEF;
326 				ntdp->t_tdesc = tdp;
327 				tdp->t_id = faketypenumber++;
328 				tdp = ntdp;
329 			}
330 		} else if (tdp->t_id != tid) {
331 			ntdp = xcalloc(sizeof (*ntdp));
332 			ntdp->t_name = xstrdup(ii->ii_name);
333 			ntdp->t_type = TYPEDEF;
334 			ntdp->t_tdesc = tdp;
335 			tdp = ntdp;
336 		}
337 
338 		if (tagadd(ii->ii_name, tid, tdp) < 0)
339 			return (-1);
340 	}
341 
342 	ii->ii_type = II_TYPE;
343 	ii->ii_dtype = tdp;
344 	return (II_TYPE);
345 }
346 
347 static iitype_t
348 parse_sou(char *cp, iidesc_t *idp)
349 {
350 	tdesc_t *rtdp;
351 	int tid;
352 
353 	if (*cp++ != 'T')
354 		expected("parse_sou", "T (sou)", cp - 1);
355 
356 	cp = id(cp, &tid);
357 	if (*cp++ != '=')
358 		expected("parse_sou", "= (definition)", cp - 1);
359 
360 	parse_debug(1, NULL, "parse_sou: declaring '%s'", idp->ii_name ?
361 	    idp->ii_name : "(anon)");
362 	if ((rtdp = lookup(tid)) != NULL) {
363 		if (idp->ii_name != NULL) {
364 			if (rtdp->t_name != NULL &&
365 			    strcmp(rtdp->t_name, idp->ii_name) != 0) {
366 				tdesc_t *tdp;
367 
368 				tdp = xcalloc(sizeof (*tdp));
369 				tdp->t_name = xstrdup(idp->ii_name);
370 				tdp->t_type = TYPEDEF;
371 				tdp->t_tdesc = rtdp;
372 				addhash(tdp, tid); /* for *(x,y) types */
373 				parse_debug(3, NULL, "    %s defined as %s(%d)",
374 				    idp->ii_name, tdesc_name(rtdp), tid);
375 			} else if (rtdp->t_name == NULL) {
376 				rtdp->t_name = xstrdup(idp->ii_name);
377 				addhash(rtdp, tid);
378 			}
379 		}
380 	} else {
381 		rtdp = xcalloc(sizeof (*rtdp));
382 		rtdp->t_name = idp->ii_name ? xstrdup(idp->ii_name) : NULL;
383 		addhash(rtdp, tid);
384 	}
385 
386 	switch (*cp++) {
387 	case 's':
388 		(void) soudef(cp, STRUCT, &rtdp);
389 		break;
390 	case 'u':
391 		(void) soudef(cp, UNION, &rtdp);
392 		break;
393 	case 'e':
394 		enumdef(cp, &rtdp);
395 		break;
396 	default:
397 		expected("parse_sou", "<tag type s/u/e>", cp - 1);
398 		break;
399 	}
400 
401 	idp->ii_type = II_SOU;
402 	idp->ii_dtype = rtdp;
403 	return (II_SOU);
404 }
405 
406 int
407 parse_stab(stab_t *stab, char *cp, iidesc_t **iidescp)
408 {
409 	iidesc_t *ii = NULL;
410 	iitype_t (*parse)(char *, iidesc_t *);
411 	int rc;
412 
413 	/*
414 	 * set up for reset()
415 	 */
416 	if (setjmp(resetbuf))
417 		return (-1);
418 
419 	cp = whitesp(cp);
420 	ii = iidesc_new(NULL);
421 	cp = name(cp, &ii->ii_name);
422 
423 	switch (stab->n_type) {
424 	case N_FUN:
425 		parse = parse_fun;
426 		break;
427 
428 	case N_LSYM:
429 		if (*cp == 't')
430 			parse = parse_type;
431 		else if (*cp == 'T')
432 			parse = parse_sou;
433 		else
434 			parse = parse_sym;
435 		break;
436 
437 	case N_GSYM:
438 	case N_LCSYM:
439 	case N_PSYM:
440 	case N_ROSYM:
441 	case N_RSYM:
442 	case N_STSYM:
443 		parse = parse_sym;
444 		break;
445 	default:
446 		parse_debug(1, cp, "Unknown stab type %#x", stab->n_type);
447 		bzero(&resetbuf, sizeof (resetbuf));
448 		return (-1);
449 	}
450 
451 	rc = parse(cp, ii);
452 	bzero(&resetbuf, sizeof (resetbuf));
453 
454 	if (rc < 0 || ii->ii_type == II_NOT) {
455 		iidesc_free(ii, NULL);
456 		return (rc);
457 	}
458 
459 	*iidescp = ii;
460 
461 	return (1);
462 }
463 
464 /*
465  * Check if we have this node in the hash table already
466  */
467 tdesc_t *
468 lookup(int h)
469 {
470 	int bucket = HASH(h);
471 	tdesc_t *tdp = hash_table[bucket];
472 
473 	while (tdp != NULL) {
474 		if (tdp->t_id == h)
475 			return (tdp);
476 		tdp = tdp->t_hash;
477 	}
478 	return (NULL);
479 }
480 
481 static char *
482 whitesp(char *cp)
483 {
484 	char c;
485 
486 	for (c = *cp++; isspace(c); c = *cp++);
487 	--cp;
488 	return (cp);
489 }
490 
491 static char *
492 name(char *cp, char **w)
493 {
494 	char *new, *orig, c;
495 	int len;
496 
497 	orig = cp;
498 	c = *cp++;
499 	if (c == ':')
500 		*w = NULL;
501 	else if (isalpha(c) || strchr("_.$", c)) {
502 		for (c = *cp++; isalnum(c) || strchr(" _.$", c); c = *cp++)
503 			;
504 		if (c != ':')
505 			reset();
506 		len = cp - orig;
507 		new = xmalloc(len);
508 		while (orig < cp - 1)
509 			*new++ = *orig++;
510 		*new = '\0';
511 		*w = new - (len - 1);
512 	} else
513 		reset();
514 
515 	return (cp);
516 }
517 
518 static char *
519 number(char *cp, int *n)
520 {
521 	char *next;
522 
523 	*n = (int)strtol(cp, &next, 10);
524 	if (next == cp)
525 		expected("number", "<number>", cp);
526 	return (next);
527 }
528 
529 static char *
530 id(char *cp, int *h)
531 {
532 	int n1, n2;
533 
534 	if (*cp == '(') {	/* SunPro style */
535 		cp++;
536 		cp = number(cp, &n1);
537 		if (*cp++ != ',')
538 			expected("id", ",", cp - 1);
539 		cp = number(cp, &n2);
540 		if (*cp++ != ')')
541 			expected("id", ")", cp - 1);
542 		*h = MAKETYPEID(n1, n2);
543 	} else if (isdigit(*cp)) { /* gcc style */
544 		cp = number(cp, &n1);
545 		*h = n1;
546 	} else {
547 		expected("id", "(/0-9", cp);
548 	}
549 	return (cp);
550 }
551 
552 static int
553 tagadd(char *w, int h, tdesc_t *tdp)
554 {
555 	tdesc_t *otdp;
556 
557 	tdp->t_name = w;
558 	if (!(otdp = lookup(h)))
559 		addhash(tdp, h);
560 	else if (otdp != tdp) {
561 		warning("duplicate entry\n");
562 		warning("  old: %s %d (%d,%d)\n", tdesc_name(otdp),
563 		    otdp->t_type, TYPEFILE(otdp->t_id), TYPENUM(otdp->t_id));
564 		warning("  new: %s %d (%d,%d)\n", tdesc_name(tdp),
565 		    tdp->t_type, TYPEFILE(tdp->t_id), TYPENUM(tdp->t_id));
566 		return (-1);
567 	}
568 
569 	return (0);
570 }
571 
572 static char *
573 tdefdecl(char *cp, int h, tdesc_t **rtdp)
574 {
575 	tdesc_t *ntdp;
576 	char *w;
577 	int c, h2;
578 	char type;
579 
580 	parse_debug(3, cp, "tdefdecl h=%d", h);
581 
582 	/* Type codes */
583 	switch (type = *cp) {
584 	case 'b': /* integer */
585 	case 'R': /* fp */
586 		cp = intrinsic(cp, rtdp);
587 		break;
588 	case '(': /* equiv to another type */
589 		cp = id(cp, &h2);
590 		ntdp = lookup(h2);
591 
592 		if (ntdp != NULL && *cp == '=') {
593 			if (ntdp->t_type == FORWARD && *(cp + 1) == 'x') {
594 				/*
595 				 * The 6.2 compiler, and possibly others, will
596 				 * sometimes emit the same stab for a forward
597 				 * declaration twice.  That is, "(1,2)=xsfoo:"
598 				 * will sometimes show up in two different
599 				 * places.  This is, of course, quite fun.  We
600 				 * want CTF to work in spite of the compiler,
601 				 * so we'll let this one through.
602 				 */
603 				char *c2 = cp + 2;
604 				char *nm;
605 
606 				if (!strchr("sue", *c2++)) {
607 					expected("tdefdecl/x-redefine", "[sue]",
608 					    c2 - 1);
609 				}
610 
611 				c2 = name(c2, &nm);
612 				if (strcmp(nm, ntdp->t_name) != 0) {
613 					terminate("Stabs error: Attempt to "
614 					    "redefine type (%d,%d) as "
615 					    "something else: %s\n",
616 					    TYPEFILE(h2), TYPENUM(h2),
617 					    c2 - 1);
618 				}
619 				free(nm);
620 
621 				h2 = faketypenumber++;
622 				ntdp = NULL;
623 			} else {
624 				terminate("Stabs error: Attempting to "
625 				    "redefine type (%d,%d)\n", TYPEFILE(h2),
626 				    TYPENUM(h2));
627 			}
628 		}
629 
630 		if (ntdp == NULL) {  /* if that type isn't defined yet */
631 			if (*cp != '=') {
632 				/* record it as unresolved */
633 				parse_debug(3, NULL, "tdefdecl unres type %d",
634 				    h2);
635 				*rtdp = calloc(sizeof (**rtdp), 1);
636 				(*rtdp)->t_type = TYPEDEF_UNRES;
637 				(*rtdp)->t_id = h2;
638 				break;
639 			} else
640 				cp++;
641 
642 			/* define a new type */
643 			cp = tdefdecl(cp, h2, rtdp);
644 			if ((*rtdp)->t_id && (*rtdp)->t_id != h2) {
645 				ntdp = calloc(sizeof (*ntdp), 1);
646 				ntdp->t_type = TYPEDEF;
647 				ntdp->t_tdesc = *rtdp;
648 				*rtdp = ntdp;
649 			}
650 
651 			addhash(*rtdp, h2);
652 
653 		} else { /* that type is already defined */
654 			if (ntdp->t_type != TYPEDEF || ntdp->t_name != NULL) {
655 				*rtdp = ntdp;
656 			} else {
657 				parse_debug(3, NULL,
658 				    "No duplicate typedef anon for ref");
659 				*rtdp = ntdp;
660 			}
661 		}
662 		break;
663 	case '*':
664 		ntdp = NULL;
665 		cp = tdefdecl(cp + 1, h, &ntdp);
666 		if (ntdp == NULL)
667 			expected("tdefdecl/*", "id", cp);
668 
669 		if (!ntdp->t_id)
670 			ntdp->t_id = faketypenumber++;
671 
672 		*rtdp = xcalloc(sizeof (**rtdp));
673 		(*rtdp)->t_type = POINTER;
674 		(*rtdp)->t_size = 0;
675 		(*rtdp)->t_id = h;
676 		(*rtdp)->t_tdesc = ntdp;
677 		break;
678 	case 'f':
679 		cp = tdefdecl(cp + 1, h, &ntdp);
680 		*rtdp = xcalloc(sizeof (**rtdp));
681 		(*rtdp)->t_type = FUNCTION;
682 		(*rtdp)->t_size = 0;
683 		(*rtdp)->t_id = h;
684 		(*rtdp)->t_fndef = xcalloc(sizeof (fndef_t));
685 		/*
686 		 * The 6.1 compiler will sometimes generate incorrect stabs for
687 		 * function pointers (it'll get the return type wrong).  This
688 		 * causes merges to fail.  We therefore treat function pointers
689 		 * as if they all point to functions that return int.  When
690 		 * 4432549 is fixed, the lookupname() call below should be
691 		 * replaced with `ntdp'.
692 		 */
693 		(*rtdp)->t_fndef->fn_ret = lookupname("int");
694 		break;
695 	case 'a':
696 	case 'z':
697 		cp++;
698 		if (*cp++ != 'r')
699 			expected("tdefdecl/[az]", "r", cp - 1);
700 		*rtdp = xcalloc(sizeof (**rtdp));
701 		(*rtdp)->t_type = ARRAY;
702 		(*rtdp)->t_id = h;
703 		cp = arraydef(cp, rtdp);
704 		break;
705 	case 'x':
706 		c = *++cp;
707 		if (c != 's' && c != 'u' && c != 'e')
708 			expected("tdefdecl/x", "[sue]", cp - 1);
709 		cp = name(cp + 1, &w);
710 
711 		ntdp = xcalloc(sizeof (*ntdp));
712 		ntdp->t_type = FORWARD;
713 		ntdp->t_name = w;
714 		/*
715 		 * We explicitly don't set t_id here - the caller will do it.
716 		 * The caller may want to use a real type ID, or they may
717 		 * choose to make one up.
718 		 */
719 
720 		*rtdp = ntdp;
721 		break;
722 
723 	case 'B': /* volatile */
724 		cp = tdefdecl(cp + 1, h, &ntdp);
725 
726 		if (!ntdp->t_id)
727 			ntdp->t_id = faketypenumber++;
728 
729 		*rtdp = xcalloc(sizeof (**rtdp));
730 		(*rtdp)->t_type = VOLATILE;
731 		(*rtdp)->t_size = 0;
732 		(*rtdp)->t_tdesc = ntdp;
733 		(*rtdp)->t_id = h;
734 		break;
735 
736 	case 'k': /* const */
737 		cp = tdefdecl(cp + 1, h, &ntdp);
738 
739 		if (!ntdp->t_id)
740 			ntdp->t_id = faketypenumber++;
741 
742 		*rtdp = xcalloc(sizeof (**rtdp));
743 		(*rtdp)->t_type = CONST;
744 		(*rtdp)->t_size = 0;
745 		(*rtdp)->t_tdesc = ntdp;
746 		(*rtdp)->t_id = h;
747 		break;
748 
749 	case 'K': /* restricted */
750 		cp = tdefdecl(cp + 1, h, &ntdp);
751 
752 		if (!ntdp->t_id)
753 			ntdp->t_id = faketypenumber++;
754 
755 		*rtdp = xcalloc(sizeof (**rtdp));
756 		(*rtdp)->t_type = RESTRICT;
757 		(*rtdp)->t_size = 0;
758 		(*rtdp)->t_tdesc = ntdp;
759 		(*rtdp)->t_id = h;
760 		break;
761 
762 	case 'u':
763 	case 's':
764 		cp++;
765 
766 		*rtdp = xcalloc(sizeof (**rtdp));
767 		(*rtdp)->t_name = NULL;
768 		cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
769 		break;
770 	default:
771 		expected("tdefdecl", "<type code>", cp);
772 	}
773 	return (cp);
774 }
775 
776 static char *
777 intrinsic(char *cp, tdesc_t **rtdp)
778 {
779 	intr_t *intr = xcalloc(sizeof (intr_t));
780 	tdesc_t *tdp;
781 	int width, fmt, i;
782 
783 	switch (*cp++) {
784 	case 'b':
785 		intr->intr_type = INTR_INT;
786 		if (*cp == 's')
787 			intr->intr_signed = 1;
788 		else if (*cp != 'u')
789 			expected("intrinsic/b", "[su]", cp);
790 		cp++;
791 
792 		if (strchr("cbv", *cp))
793 			intr->intr_iformat = *cp++;
794 
795 		cp = number(cp, &width);
796 		if (*cp++ != ';')
797 			expected("intrinsic/b", "; (post-width)", cp - 1);
798 
799 		cp = number(cp, &intr->intr_offset);
800 		if (*cp++ != ';')
801 			expected("intrinsic/b", "; (post-offset)", cp - 1);
802 
803 		cp = number(cp, &intr->intr_nbits);
804 		break;
805 
806 	case 'R':
807 		intr->intr_type = INTR_REAL;
808 		for (fmt = 0, i = 0; isdigit(*(cp + i)); i++)
809 			fmt = fmt * 10 + (*(cp + i) - '0');
810 
811 		if (fmt < 1 || fmt > CTF_FP_MAX)
812 			expected("intrinsic/R", "number <= CTF_FP_MAX", cp);
813 
814 		intr->intr_fformat = fmt;
815 		cp += i;
816 
817 		if (*cp++ != ';')
818 			expected("intrinsic/R", ";", cp - 1);
819 		cp = number(cp, &width);
820 
821 		intr->intr_nbits = width * 8;
822 		break;
823 	}
824 
825 	tdp = xcalloc(sizeof (*tdp));
826 	tdp->t_type = INTRINSIC;
827 	tdp->t_size = width;
828 	tdp->t_name = NULL;
829 	tdp->t_intr = intr;
830 	parse_debug(3, NULL, "intrinsic: size=%d", width);
831 	*rtdp = tdp;
832 
833 	return (cp);
834 }
835 
836 static tdesc_t *
837 bitintrinsic(tdesc_t *template, int nbits)
838 {
839 	tdesc_t *newtdp = xcalloc(sizeof (tdesc_t));
840 
841 	newtdp->t_name = xstrdup(template->t_name);
842 	newtdp->t_id = faketypenumber++;
843 	newtdp->t_type = INTRINSIC;
844 	newtdp->t_size = template->t_size;
845 	newtdp->t_intr = xmalloc(sizeof (intr_t));
846 	bcopy(template->t_intr, newtdp->t_intr, sizeof (intr_t));
847 	newtdp->t_intr->intr_nbits = nbits;
848 
849 	return (newtdp);
850 }
851 
852 static char *
853 offsize(char *cp, mlist_t *mlp)
854 {
855 	int offset, size;
856 
857 	if (*cp == ',')
858 		cp++;
859 	cp = number(cp, &offset);
860 	if (*cp++ != ',')
861 		expected("offsize/2", ",", cp - 1);
862 	cp = number(cp, &size);
863 	if (*cp++ != ';')
864 		expected("offsize/3", ";", cp - 1);
865 	mlp->ml_offset = offset;
866 	mlp->ml_size = size;
867 	return (cp);
868 }
869 
870 static tdesc_t *
871 find_intrinsic(tdesc_t *tdp)
872 {
873 	for (;;) {
874 		switch (tdp->t_type) {
875 		case TYPEDEF:
876 		case VOLATILE:
877 		case CONST:
878 		case RESTRICT:
879 			tdp = tdp->t_tdesc;
880 			break;
881 
882 		default:
883 			return (tdp);
884 		}
885 	}
886 }
887 
888 static char *
889 soudef(char *cp, stabtype_t type, tdesc_t **rtdp)
890 {
891 	mlist_t *mlp, **prev;
892 	char *w;
893 	int h;
894 	int size;
895 	tdesc_t *tdp, *itdp;
896 
897 	cp = number(cp, &size);
898 	(*rtdp)->t_size = size;
899 	(*rtdp)->t_type = type; /* s or u */
900 
901 	/*
902 	 * An '@' here indicates a bitmask follows.   This is so the
903 	 * compiler can pass information to debuggers about how structures
904 	 * are passed in the v9 world.  We don't need this information
905 	 * so we skip over it.
906 	 */
907 	if (cp[0] == '@') {
908 		cp += 3;
909 	}
910 
911 	parse_debug(3, cp, "soudef: %s size=%d", tdesc_name(*rtdp),
912 	    (*rtdp)->t_size);
913 
914 	prev = &((*rtdp)->t_members);
915 	/* now fill up the fields */
916 	while ((*cp != '\0') && (*cp != ';')) { /* signifies end of fields */
917 		mlp = xcalloc(sizeof (*mlp));
918 		*prev = mlp;
919 		cp = name(cp, &w);
920 		mlp->ml_name = w;
921 		cp = id(cp, &h);
922 		/*
923 		 * find the tdesc struct in the hash table for this type
924 		 * and stick a ptr in here
925 		 */
926 		tdp = lookup(h);
927 		if (tdp == NULL) { /* not in hash list */
928 			parse_debug(3, NULL, "      defines %s (%d)", w, h);
929 			if (*cp++ != '=') {
930 				tdp = unres_new(h);
931 				parse_debug(3, NULL,
932 				    "      refers to %s (unresolved %d)",
933 				    (w ? w : "anon"), h);
934 			} else {
935 				cp = tdefdecl(cp, h, &tdp);
936 
937 				if (tdp->t_id && tdp->t_id != h) {
938 					tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
939 
940 					ntdp->t_type = TYPEDEF;
941 					ntdp->t_tdesc = tdp;
942 					tdp = ntdp;
943 				}
944 
945 				addhash(tdp, h);
946 				parse_debug(4, cp,
947 				    "     soudef now looking at    ");
948 				cp++;
949 			}
950 		} else {
951 			parse_debug(3, NULL, "      refers to %s (%d, %s)",
952 			    w ? w : "anon", h, tdesc_name(tdp));
953 		}
954 
955 		cp = offsize(cp, mlp);
956 
957 		itdp = find_intrinsic(tdp);
958 		if (itdp->t_type == INTRINSIC) {
959 			if (mlp->ml_size != itdp->t_intr->intr_nbits) {
960 				parse_debug(4, cp, "making %d bit intrinsic "
961 				    "from %s", mlp->ml_size, tdesc_name(itdp));
962 				mlp->ml_type = bitintrinsic(itdp, mlp->ml_size);
963 			} else
964 				mlp->ml_type = tdp;
965 		} else if (itdp->t_type == TYPEDEF_UNRES) {
966 			list_add(&typedbitfldmems, mlp);
967 			mlp->ml_type = tdp;
968 		} else {
969 			mlp->ml_type = tdp;
970 		}
971 
972 		/* cp is now pointing to next field */
973 		prev = &mlp->ml_next;
974 	}
975 	return (cp);
976 }
977 
978 static char *
979 arraydef(char *cp, tdesc_t **rtdp)
980 {
981 	int start, end, h;
982 
983 	cp = id(cp, &h);
984 	if (*cp++ != ';')
985 		expected("arraydef/1", ";", cp - 1);
986 
987 	(*rtdp)->t_ardef = xcalloc(sizeof (ardef_t));
988 	(*rtdp)->t_ardef->ad_idxtype = lookup(h);
989 
990 	cp = number(cp, &start); /* lower */
991 	if (*cp++ != ';')
992 		expected("arraydef/2", ";", cp - 1);
993 
994 	if (*cp == 'S') {
995 		/* variable length array - treat as null dimensioned */
996 		cp++;
997 		if (*cp++ != '-')
998 			expected("arraydef/fpoff-sep", "-", cp - 1);
999 		cp = number(cp, &end);
1000 		end = start;
1001 	} else {
1002 		/* normal fixed-dimension array */
1003 		cp = number(cp, &end);  /* upper */
1004 	}
1005 
1006 	if (*cp++ != ';')
1007 		expected("arraydef/3", ";", cp - 1);
1008 	(*rtdp)->t_ardef->ad_nelems = end - start + 1;
1009 	cp = tdefdecl(cp, h, &((*rtdp)->t_ardef->ad_contents));
1010 
1011 	parse_debug(3, cp, "defined array idx type %d %d-%d next ",
1012 	    h, start, end);
1013 
1014 	return (cp);
1015 }
1016 
1017 static void
1018 enumdef(char *cp, tdesc_t **rtdp)
1019 {
1020 	elist_t *elp, **prev;
1021 	char *w;
1022 
1023 	(*rtdp)->t_type = ENUM;
1024 	(*rtdp)->t_emem = NULL;
1025 
1026 	prev = &((*rtdp)->t_emem);
1027 	while (*cp != ';') {
1028 		elp = xcalloc(sizeof (*elp));
1029 		elp->el_next = NULL;
1030 		*prev = elp;
1031 		cp = name(cp, &w);
1032 		elp->el_name = w;
1033 		cp = number(cp, &elp->el_number);
1034 		parse_debug(3, NULL, "enum %s: %s=%d", tdesc_name(*rtdp),
1035 		    elp->el_name, elp->el_number);
1036 		prev = &elp->el_next;
1037 		if (*cp++ != ',')
1038 			expected("enumdef", ",", cp - 1);
1039 	}
1040 }
1041 
1042 tdesc_t *
1043 lookup_name(tdesc_t **hash, char *name)
1044 {
1045 	int bucket = compute_sum(name);
1046 	tdesc_t *tdp, *ttdp = NULL;
1047 
1048 	for (tdp = hash[bucket]; tdp != NULL; tdp = tdp->t_next) {
1049 		if (tdp->t_name != NULL && strcmp(tdp->t_name, name) == 0) {
1050 			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1051 			    tdp->t_type == ENUM || tdp->t_type == INTRINSIC)
1052 				return (tdp);
1053 			if (tdp->t_type == TYPEDEF)
1054 				ttdp = tdp;
1055 		}
1056 	}
1057 	return (ttdp);
1058 }
1059 
1060 tdesc_t *
1061 lookupname(char *name)
1062 {
1063 	return (lookup_name(name_table, name));
1064 }
1065 
1066 /*
1067  * Add a node to the hash queues.
1068  */
1069 static void
1070 addhash(tdesc_t *tdp, int num)
1071 {
1072 	int hash = HASH(num);
1073 	tdesc_t *ttdp;
1074 	char added_num = 0, added_name = 0;
1075 
1076 	/*
1077 	 * If it already exists in the hash table don't add it again
1078 	 * (but still check to see if the name should be hashed).
1079 	 */
1080 	ttdp = lookup(num);
1081 
1082 	if (ttdp == NULL) {
1083 		tdp->t_id = num;
1084 		tdp->t_hash = hash_table[hash];
1085 		hash_table[hash] = tdp;
1086 		added_num = 1;
1087 	}
1088 
1089 	if (tdp->t_name != NULL) {
1090 		ttdp = lookupname(tdp->t_name);
1091 		if (ttdp == NULL) {
1092 			hash = compute_sum(tdp->t_name);
1093 			tdp->t_next = name_table[hash];
1094 			name_table[hash] = tdp;
1095 			added_name = 1;
1096 		}
1097 	}
1098 	if (!added_num && !added_name) {
1099 		terminate("stabs: broken hash\n");
1100 	}
1101 }
1102 
1103 static int
1104 compute_sum(char *w)
1105 {
1106 	char c;
1107 	int sum;
1108 
1109 	for (sum = 0; (c = *w) != '\0'; sum += c, w++)
1110 		;
1111 	return (HASH(sum));
1112 }
1113 
1114 static void
1115 reset(void)
1116 {
1117 	longjmp(resetbuf, 1);
1118 }
1119 
1120 void
1121 check_hash(void)
1122 {
1123 	tdesc_t *tdp;
1124 	int i;
1125 
1126 	printf("checking hash\n");
1127 	for (i = 0; i < BUCKETS; i++) {
1128 		if (hash_table[i]) {
1129 			for (tdp = hash_table[i]->t_hash;
1130 			    tdp && tdp != hash_table[i];
1131 			    tdp = tdp->t_hash)
1132 				continue;
1133 			if (tdp) {
1134 				terminate("cycle in hash bucket %d\n", i);
1135 				return;
1136 			}
1137 		}
1138 
1139 		if (name_table[i]) {
1140 			for (tdp = name_table[i]->t_next;
1141 			    tdp && tdp != name_table[i];
1142 			    tdp = tdp->t_next)
1143 				continue;
1144 			if (tdp) {
1145 				terminate("cycle in name bucket %d\n", i);
1146 				return;
1147 			}
1148 		}
1149 	}
1150 	printf("done\n");
1151 }
1152 
1153 /*ARGSUSED1*/
1154 static int
1155 resolve_typed_bitfields_cb(mlist_t *ml, void *private)
1156 {
1157 	tdesc_t *tdp = ml->ml_type;
1158 
1159 	debug(3, "Resolving typed bitfields (member %s)\n",
1160 	    (ml->ml_name ? ml->ml_name : "(anon)"));
1161 
1162 	while (tdp) {
1163 		switch (tdp->t_type) {
1164 		case INTRINSIC:
1165 			if (ml->ml_size != tdp->t_intr->intr_nbits) {
1166 				debug(3, "making %d bit intrinsic from %s",
1167 				    ml->ml_size, tdesc_name(tdp));
1168 				ml->ml_type = bitintrinsic(tdp, ml->ml_size);
1169 			} else {
1170 				debug(3, "using existing %d bit %s intrinsic",
1171 				    ml->ml_size, tdesc_name(tdp));
1172 				ml->ml_type = tdp;
1173 			}
1174 			return (1);
1175 
1176 		case POINTER:
1177 		case TYPEDEF:
1178 		case VOLATILE:
1179 		case CONST:
1180 		case RESTRICT:
1181 			tdp = tdp->t_tdesc;
1182 			break;
1183 
1184 		default:
1185 			return (1);
1186 		}
1187 	}
1188 
1189 	terminate("type chain for bitfield member %s has a NULL", ml->ml_name);
1190 	/*NOTREACHED*/
1191 	return (0);
1192 }
1193 
1194 void
1195 resolve_typed_bitfields(void)
1196 {
1197 	(void) list_iter(typedbitfldmems,
1198 	    (int (*)())resolve_typed_bitfields_cb, NULL);
1199 }
1200