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 (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
26 * This file is a sewer.
36 #include <uts/common/sys/ctf.h>
42 #define HASH(NUM) ((int)(NUM & (BUCKETS - 1)))
45 #define TYPEPAIRMULT 10000
46 #define MAKETYPEID(file, num) ((file) * TYPEPAIRMULT + num)
47 #define TYPEFILE(tid) ((tid) / TYPEPAIRMULT)
48 #define TYPENUM(tid) ((tid) % TYPEPAIRMULT)
50 #define expected(a, b, c) _expected(a, b, c, __LINE__)
52 static int faketypenumber = 100000000;
54 static tdesc_t *hash_table[BUCKETS];
55 static tdesc_t *name_table[BUCKETS];
57 list_t *typedbitfldmems;
59 static void reset(void);
60 static jmp_buf resetbuf;
62 static char *soudef(char *cp, stabtype_t type, tdesc_t **rtdp);
63 static void enumdef(char *cp, tdesc_t **rtdp);
64 static int compute_sum(const char *w);
66 static char *number(char *cp, int *n);
67 static char *name(char *cp, char **w);
68 static char *id(char *cp, int *h);
69 static char *whitesp(char *cp);
70 static void addhash(tdesc_t *tdp, int num);
71 static int tagadd(char *w, int h, tdesc_t *tdp);
72 static char *tdefdecl(char *cp, int h, tdesc_t **rtdp);
73 static char *intrinsic(char *cp, tdesc_t **rtdp);
74 static char *arraydef(char *cp, tdesc_t **rtdp);
76 extern int debug_level;
77 int debug_parse = DEBUG_PARSE;
81 parse_debug(int level, char *cp, char *fmt, ...)
88 if (level > debug_level || !debug_parse)
92 for (i = 0; i < 30; i++) {
99 (void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp);
106 vadebug(level, buf, ap);
110 /* Report unexpected syntax in stabs. */
113 char *who, /* what function, or part thereof, is reporting */
114 char *what, /* what was expected */
115 char *where, /* where we were in the line of input */
118 fprintf(stderr, "%s, expecting \"%s\" at \"%s\"\n", who, what, where);
119 fprintf(stderr, "code line: %d, file %s\n", line,
120 (curhdr ? curhdr : "NO FILE"));
126 parse_init(tdata_t *td)
130 for (i = 0; i < BUCKETS; i++) {
131 hash_table[i] = NULL;
132 name_table[i] = NULL;
135 if (typedbitfldmems != NULL) {
136 list_free(typedbitfldmems, NULL, NULL);
137 typedbitfldmems = NULL;
142 parse_finish(tdata_t *td)
144 td->td_nextid = ++faketypenumber;
152 tdp = xcalloc(sizeof (*tdp));
153 tdp->t_type = TYPEDEF_UNRES;
160 read_tid(char *cp, tdesc_t **tdpp)
170 if (!(cp = tdefdecl(cp + 1, tid, &tdp)))
172 if (tdp->t_id && tdp->t_id != tid) {
173 tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
175 ntdp->t_type = TYPEDEF;
180 } else if ((tdp = lookup(tid)) == NULL)
181 tdp = unres_new(tid);
188 parse_fun(char *cp, iidesc_t *ii)
192 tdesc_t **args = NULL;
198 * name:F global function
199 * name:f static function
203 iitype = II_NOT; /* not interesting */
215 expected("parse_nfun", "[PfF]", cp - 1);
218 if (!(cp = read_tid(cp, &tdp)))
222 args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
224 while (*cp && *++cp) {
231 if (nargs > FUNCARG_DEF)
232 args = xrealloc(args, sizeof (tdesc_t *) * nargs);
233 if (!(cp = read_tid(cp, &args[nargs - 1])))
237 ii->ii_type = iitype;
239 ii->ii_nargs = nargs;
247 parse_sym(char *cp, iidesc_t *ii)
253 * name:G global variable
254 * name:S static variable
271 iitype = II_NOT; /* not interesting */
274 expected("parse_sym", "[GprSV(]", cp - 1);
277 if (!(cp = read_tid(cp, &tdp)))
280 ii->ii_type = iitype;
287 parse_type(char *cp, iidesc_t *ii)
293 expected("parse_type", "t (type)", cp - 1);
296 if ((tdp = lookup(tid)) == NULL) {
298 expected("parse_type", "= (definition)", cp - 1);
300 (void) tdefdecl(cp, tid, &tdp);
302 if (tdp->t_id == tid) {
303 assert(tdp->t_type != TYPEDEF);
304 assert(!lookup(tdp->t_id));
306 if (!streq(tdp->t_name, ii->ii_name)) {
307 ntdp = xcalloc(sizeof (*ntdp));
308 ntdp->t_name = xstrdup(ii->ii_name);
309 ntdp->t_type = TYPEDEF;
311 tdp->t_id = faketypenumber++;
314 } else if (tdp->t_id == 0) {
315 assert(tdp->t_type == FORWARD ||
316 tdp->t_type == INTRINSIC);
318 if (tdp->t_name && !streq(tdp->t_name, ii->ii_name)) {
319 ntdp = xcalloc(sizeof (*ntdp));
320 ntdp->t_name = xstrdup(ii->ii_name);
321 ntdp->t_type = TYPEDEF;
323 tdp->t_id = faketypenumber++;
326 } else if (tdp->t_id != tid) {
327 ntdp = xcalloc(sizeof (*ntdp));
328 ntdp->t_name = xstrdup(ii->ii_name);
329 ntdp->t_type = TYPEDEF;
334 if (tagadd(ii->ii_name, tid, tdp) < 0)
338 ii->ii_type = II_TYPE;
344 parse_sou(char *cp, iidesc_t *idp)
350 expected("parse_sou", "T (sou)", cp - 1);
354 expected("parse_sou", "= (definition)", cp - 1);
356 parse_debug(1, NULL, "parse_sou: declaring '%s'", idp->ii_name ?
357 idp->ii_name : "(anon)");
358 if ((rtdp = lookup(tid)) != NULL) {
359 if (idp->ii_name != NULL) {
360 if (rtdp->t_name != NULL &&
361 strcmp(rtdp->t_name, idp->ii_name) != 0) {
364 tdp = xcalloc(sizeof (*tdp));
365 tdp->t_name = xstrdup(idp->ii_name);
366 tdp->t_type = TYPEDEF;
368 addhash(tdp, tid); /* for *(x,y) types */
369 parse_debug(3, NULL, " %s defined as %s(%d)",
370 idp->ii_name, tdesc_name(rtdp), tid);
371 } else if (rtdp->t_name == NULL) {
372 rtdp->t_name = xstrdup(idp->ii_name);
377 rtdp = xcalloc(sizeof (*rtdp));
378 rtdp->t_name = idp->ii_name ? xstrdup(idp->ii_name) : NULL;
384 (void) soudef(cp, STRUCT, &rtdp);
387 (void) soudef(cp, UNION, &rtdp);
393 expected("parse_sou", "<tag type s/u/e>", cp - 1);
397 idp->ii_type = II_SOU;
398 idp->ii_dtype = rtdp;
403 parse_stab(stab_t *stab, char *cp, iidesc_t **iidescp)
406 iitype_t (*parse)(char *, iidesc_t *);
412 if (setjmp(resetbuf))
416 ii = iidesc_new(NULL);
417 cp = name(cp, &ii->ii_name);
419 switch (stab->n_type) {
442 parse_debug(1, cp, "Unknown stab type %#x", stab->n_type);
443 bzero(&resetbuf, sizeof (resetbuf));
448 bzero(&resetbuf, sizeof (resetbuf));
450 if (rc < 0 || ii->ii_type == II_NOT) {
451 iidesc_free(ii, NULL);
461 * Check if we have this node in the hash table already
466 int bucket = HASH(h);
467 tdesc_t *tdp = hash_table[bucket];
469 while (tdp != NULL) {
482 for (c = *cp++; isspace(c); c = *cp++)
489 name(char *cp, char **w)
498 else if (isalpha(c) || strchr("_.$#", c)) {
499 for (c = *cp++; isalnum(c) || strchr(" _.$#", c); c = *cp++)
505 while (orig < cp - 1)
508 *w = new - (len - 1);
516 number(char *cp, int *n)
520 *n = (int)strtol(cp, &next, 10);
522 expected("number", "<number>", cp);
531 if (*cp == '(') { /* SunPro style */
533 cp = number(cp, &n1);
535 expected("id", ",", cp - 1);
536 cp = number(cp, &n2);
538 expected("id", ")", cp - 1);
539 *h = MAKETYPEID(n1, n2);
540 } else if (isdigit(*cp)) { /* gcc style */
541 cp = number(cp, &n1);
544 expected("id", "(/0-9", cp);
550 tagadd(char *w, int h, tdesc_t *tdp)
555 if (!(otdp = lookup(h)))
557 else if (otdp != tdp) {
558 warning("duplicate entry\n");
559 warning(" old: %s %d (%d,%d)\n", tdesc_name(otdp),
560 otdp->t_type, TYPEFILE(otdp->t_id), TYPENUM(otdp->t_id));
561 warning(" new: %s %d (%d,%d)\n", tdesc_name(tdp),
562 tdp->t_type, TYPEFILE(tdp->t_id), TYPENUM(tdp->t_id));
570 tdefdecl(char *cp, int h, tdesc_t **rtdp)
577 parse_debug(3, cp, "tdefdecl h=%d", h);
580 switch (type = *cp) {
581 case 'b': /* integer */
583 cp = intrinsic(cp, rtdp);
585 case '(': /* equiv to another type */
589 if (ntdp != NULL && *cp == '=') {
590 if (ntdp->t_type == FORWARD && *(cp + 1) == 'x') {
592 * The 6.2 compiler, and possibly others, will
593 * sometimes emit the same stab for a forward
594 * declaration twice. That is, "(1,2)=xsfoo:"
595 * will sometimes show up in two different
596 * places. This is, of course, quite fun. We
597 * want CTF to work in spite of the compiler,
598 * so we'll let this one through.
603 if (!strchr("sue", *c2++)) {
604 expected("tdefdecl/x-redefine", "[sue]",
609 if (strcmp(nm, ntdp->t_name) != 0) {
610 terminate("Stabs error: Attempt to "
611 "redefine type (%d,%d) as "
612 "something else: %s\n",
613 TYPEFILE(h2), TYPENUM(h2),
618 h2 = faketypenumber++;
621 terminate("Stabs error: Attempting to "
622 "redefine type (%d,%d)\n", TYPEFILE(h2),
627 if (ntdp == NULL) { /* if that type isn't defined yet */
629 /* record it as unresolved */
630 parse_debug(3, NULL, "tdefdecl unres type %d",
632 *rtdp = calloc(sizeof (**rtdp), 1);
633 (*rtdp)->t_type = TYPEDEF_UNRES;
639 /* define a new type */
640 cp = tdefdecl(cp, h2, rtdp);
641 if ((*rtdp)->t_id && (*rtdp)->t_id != h2) {
642 ntdp = calloc(sizeof (*ntdp), 1);
643 ntdp->t_type = TYPEDEF;
644 ntdp->t_tdesc = *rtdp;
650 } else { /* that type is already defined */
651 if (ntdp->t_type != TYPEDEF || ntdp->t_name != NULL) {
655 "No duplicate typedef anon for ref");
662 cp = tdefdecl(cp + 1, h, &ntdp);
664 expected("tdefdecl/*", "id", cp);
667 ntdp->t_id = faketypenumber++;
669 *rtdp = xcalloc(sizeof (**rtdp));
670 (*rtdp)->t_type = POINTER;
673 (*rtdp)->t_tdesc = ntdp;
676 cp = tdefdecl(cp + 1, h, &ntdp);
677 *rtdp = xcalloc(sizeof (**rtdp));
678 (*rtdp)->t_type = FUNCTION;
681 (*rtdp)->t_fndef = xcalloc(sizeof (fndef_t));
683 * The 6.1 compiler will sometimes generate incorrect stabs for
684 * function pointers (it'll get the return type wrong). This
685 * causes merges to fail. We therefore treat function pointers
686 * as if they all point to functions that return int. When
687 * 4432549 is fixed, the lookupname() call below should be
688 * replaced with `ntdp'.
690 (*rtdp)->t_fndef->fn_ret = lookupname("int");
696 expected("tdefdecl/[az]", "r", cp - 1);
697 *rtdp = xcalloc(sizeof (**rtdp));
698 (*rtdp)->t_type = ARRAY;
700 cp = arraydef(cp, rtdp);
704 if (c != 's' && c != 'u' && c != 'e')
705 expected("tdefdecl/x", "[sue]", cp - 1);
706 cp = name(cp + 1, &w);
708 ntdp = xcalloc(sizeof (*ntdp));
709 ntdp->t_type = FORWARD;
712 * We explicitly don't set t_id here - the caller will do it.
713 * The caller may want to use a real type ID, or they may
714 * choose to make one up.
720 case 'B': /* volatile */
721 cp = tdefdecl(cp + 1, h, &ntdp);
724 ntdp->t_id = faketypenumber++;
726 *rtdp = xcalloc(sizeof (**rtdp));
727 (*rtdp)->t_type = VOLATILE;
729 (*rtdp)->t_tdesc = ntdp;
733 case 'k': /* const */
734 cp = tdefdecl(cp + 1, h, &ntdp);
737 ntdp->t_id = faketypenumber++;
739 *rtdp = xcalloc(sizeof (**rtdp));
740 (*rtdp)->t_type = CONST;
742 (*rtdp)->t_tdesc = ntdp;
746 case 'K': /* restricted */
747 cp = tdefdecl(cp + 1, h, &ntdp);
750 ntdp->t_id = faketypenumber++;
752 *rtdp = xcalloc(sizeof (**rtdp));
753 (*rtdp)->t_type = RESTRICT;
755 (*rtdp)->t_tdesc = ntdp;
763 *rtdp = xcalloc(sizeof (**rtdp));
764 (*rtdp)->t_name = NULL;
765 cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
768 expected("tdefdecl", "<type code>", cp);
774 intrinsic(char *cp, tdesc_t **rtdp)
776 intr_t *intr = xcalloc(sizeof (intr_t));
782 intr->intr_type = INTR_INT;
784 intr->intr_signed = 1;
786 expected("intrinsic/b", "[su]", cp);
789 if (strchr("cbv", *cp))
790 intr->intr_iformat = *cp++;
792 cp = number(cp, &width);
794 expected("intrinsic/b", "; (post-width)", cp - 1);
796 cp = number(cp, &intr->intr_offset);
798 expected("intrinsic/b", "; (post-offset)", cp - 1);
800 cp = number(cp, &intr->intr_nbits);
804 intr->intr_type = INTR_REAL;
805 for (fmt = 0, i = 0; isdigit(*(cp + i)); i++)
806 fmt = fmt * 10 + (*(cp + i) - '0');
808 if (fmt < 1 || fmt > CTF_FP_MAX)
809 expected("intrinsic/R", "number <= CTF_FP_MAX", cp);
811 intr->intr_fformat = fmt;
815 expected("intrinsic/R", ";", cp - 1);
816 cp = number(cp, &width);
818 intr->intr_nbits = width * 8;
822 tdp = xcalloc(sizeof (*tdp));
823 tdp->t_type = INTRINSIC;
827 parse_debug(3, NULL, "intrinsic: size=%d", width);
834 bitintrinsic(tdesc_t *template, int nbits)
836 tdesc_t *newtdp = xcalloc(sizeof (tdesc_t));
838 newtdp->t_name = xstrdup(template->t_name);
839 newtdp->t_id = faketypenumber++;
840 newtdp->t_type = INTRINSIC;
841 newtdp->t_size = template->t_size;
842 newtdp->t_intr = xmalloc(sizeof (intr_t));
843 bcopy(template->t_intr, newtdp->t_intr, sizeof (intr_t));
844 newtdp->t_intr->intr_nbits = nbits;
850 offsize(char *cp, mlist_t *mlp)
856 cp = number(cp, &offset);
858 expected("offsize/2", ",", cp - 1);
859 cp = number(cp, &size);
861 expected("offsize/3", ";", cp - 1);
862 mlp->ml_offset = offset;
868 find_intrinsic(tdesc_t *tdp)
871 switch (tdp->t_type) {
886 soudef(char *cp, stabtype_t type, tdesc_t **rtdp)
888 mlist_t *mlp, **prev;
894 cp = number(cp, &size);
895 (*rtdp)->t_size = size;
896 (*rtdp)->t_type = type; /* s or u */
899 * An '@' here indicates a bitmask follows. This is so the
900 * compiler can pass information to debuggers about how structures
901 * are passed in the v9 world. We don't need this information
902 * so we skip over it.
908 parse_debug(3, cp, "soudef: %s size=%d", tdesc_name(*rtdp),
911 prev = &((*rtdp)->t_members);
912 /* now fill up the fields */
913 while ((*cp != '\0') && (*cp != ';')) { /* signifies end of fields */
914 mlp = xcalloc(sizeof (*mlp));
920 * find the tdesc struct in the hash table for this type
921 * and stick a ptr in here
924 if (tdp == NULL) { /* not in hash list */
925 parse_debug(3, NULL, " defines %s (%d)", w, h);
929 " refers to %s (unresolved %d)",
930 (w ? w : "anon"), h);
932 cp = tdefdecl(cp, h, &tdp);
934 if (tdp->t_id && tdp->t_id != h) {
935 tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
937 ntdp->t_type = TYPEDEF;
944 " soudef now looking at ");
948 parse_debug(3, NULL, " refers to %s (%d, %s)",
949 w ? w : "anon", h, tdesc_name(tdp));
952 cp = offsize(cp, mlp);
954 itdp = find_intrinsic(tdp);
955 if (itdp->t_type == INTRINSIC) {
956 if (mlp->ml_size != itdp->t_intr->intr_nbits) {
957 parse_debug(4, cp, "making %d bit intrinsic "
958 "from %s", mlp->ml_size, tdesc_name(itdp));
959 mlp->ml_type = bitintrinsic(itdp, mlp->ml_size);
962 } else if (itdp->t_type == TYPEDEF_UNRES) {
963 list_add(&typedbitfldmems, mlp);
969 /* cp is now pointing to next field */
970 prev = &mlp->ml_next;
976 arraydef(char *cp, tdesc_t **rtdp)
982 expected("arraydef/1", ";", cp - 1);
984 (*rtdp)->t_ardef = xcalloc(sizeof (ardef_t));
985 (*rtdp)->t_ardef->ad_idxtype = lookup(h);
987 cp = number(cp, &start); /* lower */
989 expected("arraydef/2", ";", cp - 1);
993 * variable length array - treat as null dimensioned
995 * For VLA variables on sparc, SS12 generated stab entry
997 * .stabs "buf:(0,28)=zr(0,4);0;S-12;(0,1)", 0x80, 0, 0, -16
998 * Whereas SS12u1 generated stab entry looks like this:
999 * .stabs "buf:(0,28)=zr(0,4);0;S0;(0,1)", 0x80, 0, 0, 0
1000 * On x86, both versions generate the first type of entry.
1001 * We should be able to parse both.
1006 cp = number(cp, &end);
1010 * normal fixed-dimension array
1011 * Stab entry for this looks as follows :
1012 * .stabs "x:(0,28)=ar(0,4);0;9;(0,3)", 0x80, 0, 40, 0
1014 cp = number(cp, &end); /* upper */
1018 expected("arraydef/3", ";", cp - 1);
1019 (*rtdp)->t_ardef->ad_nelems = end - start + 1;
1020 cp = tdefdecl(cp, h, &((*rtdp)->t_ardef->ad_contents));
1022 parse_debug(3, cp, "defined array idx type %d %d-%d next ",
1029 enumdef(char *cp, tdesc_t **rtdp)
1031 elist_t *elp, **prev;
1034 (*rtdp)->t_type = ENUM;
1035 (*rtdp)->t_emem = NULL;
1037 prev = &((*rtdp)->t_emem);
1038 while (*cp != ';') {
1039 elp = xcalloc(sizeof (*elp));
1040 elp->el_next = NULL;
1044 cp = number(cp, &elp->el_number);
1045 parse_debug(3, NULL, "enum %s: %s=%d", tdesc_name(*rtdp),
1046 elp->el_name, elp->el_number);
1047 prev = &elp->el_next;
1049 expected("enumdef", ",", cp - 1);
1054 lookup_name(tdesc_t **hash, const char *name)
1056 int bucket = compute_sum(name);
1057 tdesc_t *tdp, *ttdp = NULL;
1059 for (tdp = hash[bucket]; tdp != NULL; tdp = tdp->t_next) {
1060 if (tdp->t_name != NULL && strcmp(tdp->t_name, name) == 0) {
1061 if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1062 tdp->t_type == ENUM || tdp->t_type == INTRINSIC)
1064 if (tdp->t_type == TYPEDEF)
1072 lookupname(const char *name)
1074 return (lookup_name(name_table, name));
1078 * Add a node to the hash queues.
1081 addhash(tdesc_t *tdp, int num)
1083 int hash = HASH(num);
1085 char added_num = 0, added_name = 0;
1088 * If it already exists in the hash table don't add it again
1089 * (but still check to see if the name should be hashed).
1095 tdp->t_hash = hash_table[hash];
1096 hash_table[hash] = tdp;
1100 if (tdp->t_name != NULL) {
1101 ttdp = lookupname(tdp->t_name);
1103 hash = compute_sum(tdp->t_name);
1104 tdp->t_next = name_table[hash];
1105 name_table[hash] = tdp;
1109 if (!added_num && !added_name) {
1110 terminate("stabs: broken hash\n");
1115 compute_sum(const char *w)
1120 for (sum = 0; (c = *w) != '\0'; sum += c, w++)
1128 longjmp(resetbuf, 1);
1137 printf("checking hash\n");
1138 for (i = 0; i < BUCKETS; i++) {
1139 if (hash_table[i]) {
1140 for (tdp = hash_table[i]->t_hash;
1141 tdp && tdp != hash_table[i];
1145 terminate("cycle in hash bucket %d\n", i);
1150 if (name_table[i]) {
1151 for (tdp = name_table[i]->t_next;
1152 tdp && tdp != name_table[i];
1156 terminate("cycle in name bucket %d\n", i);
1166 resolve_typed_bitfields_cb(mlist_t *ml, void *private)
1168 tdesc_t *tdp = ml->ml_type;
1170 debug(3, "Resolving typed bitfields (member %s)\n",
1171 (ml->ml_name ? ml->ml_name : "(anon)"));
1174 switch (tdp->t_type) {
1176 if (ml->ml_size != tdp->t_intr->intr_nbits) {
1177 debug(3, "making %d bit intrinsic from %s",
1178 ml->ml_size, tdesc_name(tdp));
1179 ml->ml_type = bitintrinsic(tdp, ml->ml_size);
1181 debug(3, "using existing %d bit %s intrinsic",
1182 ml->ml_size, tdesc_name(tdp));
1200 terminate("type chain for bitfield member %s has a NULL", ml->ml_name);
1206 resolve_typed_bitfields(void)
1208 (void) list_iter(typedbitfldmems,
1209 (int (*)())resolve_typed_bitfields_cb, NULL);