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 int debug_parse = DEBUG_PARSE;
80 parse_debug(int level, char *cp, const char *fmt, ...)
87 if (level > debug_level || !debug_parse)
91 for (i = 0; i < 30; i++) {
98 (void) snprintf(buf, sizeof (buf), "%s [cp='%s']\n", fmt, tmp);
105 vadebug(level, buf, ap);
109 /* Report unexpected syntax in stabs. */
112 const char *who, /* what function, or part thereof, is reporting */
113 const char *what, /* what was expected */
114 const char *where, /* where we were in the line of input */
117 fprintf(stderr, "%s, expecting \"%s\" at \"%s\"\n", who, what, where);
118 fprintf(stderr, "code line: %d, file %s\n", line,
119 (curhdr ? curhdr : "NO FILE"));
125 parse_init(tdata_t *td __unused)
129 for (i = 0; i < BUCKETS; i++) {
130 hash_table[i] = NULL;
131 name_table[i] = NULL;
134 if (typedbitfldmems != NULL) {
135 list_free(typedbitfldmems, NULL, NULL);
136 typedbitfldmems = NULL;
141 parse_finish(tdata_t *td)
143 td->td_nextid = ++faketypenumber;
151 tdp = xcalloc(sizeof (*tdp));
152 tdp->t_type = TYPEDEF_UNRES;
159 read_tid(char *cp, tdesc_t **tdpp)
169 if (!(cp = tdefdecl(cp + 1, tid, &tdp)))
171 if (tdp->t_id && tdp->t_id != tid) {
172 tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
174 ntdp->t_type = TYPEDEF;
179 } else if ((tdp = lookup(tid)) == NULL)
180 tdp = unres_new(tid);
187 parse_fun(char *cp, iidesc_t *ii)
191 tdesc_t **args = NULL;
197 * name:F global function
198 * name:f static function
202 iitype = II_NOT; /* not interesting */
214 expected("parse_nfun", "[PfF]", cp - 1);
217 if (!(cp = read_tid(cp, &tdp)))
221 args = xmalloc(sizeof (tdesc_t *) * FUNCARG_DEF);
223 while (*cp && *++cp) {
230 if (nargs > FUNCARG_DEF)
231 args = xrealloc(args, sizeof (tdesc_t *) * nargs);
232 if (!(cp = read_tid(cp, &args[nargs - 1])))
236 ii->ii_type = iitype;
238 ii->ii_nargs = nargs;
246 parse_sym(char *cp, iidesc_t *ii)
252 * name:G global variable
253 * name:S static variable
270 iitype = II_NOT; /* not interesting */
273 expected("parse_sym", "[GprSV(]", cp - 1);
276 if (!(cp = read_tid(cp, &tdp)))
279 ii->ii_type = iitype;
286 parse_type(char *cp, iidesc_t *ii)
292 expected("parse_type", "t (type)", cp - 1);
295 if ((tdp = lookup(tid)) == NULL) {
297 expected("parse_type", "= (definition)", cp - 1);
299 (void) tdefdecl(cp, tid, &tdp);
301 if (tdp->t_id == tid) {
302 assert(tdp->t_type != TYPEDEF);
303 assert(!lookup(tdp->t_id));
305 if (!streq(tdp->t_name, ii->ii_name)) {
306 ntdp = xcalloc(sizeof (*ntdp));
307 ntdp->t_name = xstrdup(ii->ii_name);
308 ntdp->t_type = TYPEDEF;
310 tdp->t_id = faketypenumber++;
313 } else if (tdp->t_id == 0) {
314 assert(tdp->t_type == FORWARD ||
315 tdp->t_type == INTRINSIC);
317 if (tdp->t_name && !streq(tdp->t_name, ii->ii_name)) {
318 ntdp = xcalloc(sizeof (*ntdp));
319 ntdp->t_name = xstrdup(ii->ii_name);
320 ntdp->t_type = TYPEDEF;
322 tdp->t_id = faketypenumber++;
325 } else if (tdp->t_id != tid) {
326 ntdp = xcalloc(sizeof (*ntdp));
327 ntdp->t_name = xstrdup(ii->ii_name);
328 ntdp->t_type = TYPEDEF;
333 if (tagadd(ii->ii_name, tid, tdp) < 0)
337 ii->ii_type = II_TYPE;
343 parse_sou(char *cp, iidesc_t *idp)
349 expected("parse_sou", "T (sou)", cp - 1);
353 expected("parse_sou", "= (definition)", cp - 1);
355 parse_debug(1, NULL, "parse_sou: declaring '%s'", idp->ii_name ?
356 idp->ii_name : "(anon)");
357 if ((rtdp = lookup(tid)) != NULL) {
358 if (idp->ii_name != NULL) {
359 if (rtdp->t_name != NULL &&
360 strcmp(rtdp->t_name, idp->ii_name) != 0) {
363 tdp = xcalloc(sizeof (*tdp));
364 tdp->t_name = xstrdup(idp->ii_name);
365 tdp->t_type = TYPEDEF;
367 addhash(tdp, tid); /* for *(x,y) types */
368 parse_debug(3, NULL, " %s defined as %s(%d)",
369 idp->ii_name, tdesc_name(rtdp), tid);
370 } else if (rtdp->t_name == NULL) {
371 rtdp->t_name = xstrdup(idp->ii_name);
376 rtdp = xcalloc(sizeof (*rtdp));
377 rtdp->t_name = idp->ii_name ? xstrdup(idp->ii_name) : NULL;
383 (void) soudef(cp, STRUCT, &rtdp);
386 (void) soudef(cp, UNION, &rtdp);
392 expected("parse_sou", "<tag type s/u/e>", cp - 1);
396 idp->ii_type = II_SOU;
397 idp->ii_dtype = rtdp;
402 parse_stab(stab_t *stab, char *cp, iidesc_t **iidescp)
405 iitype_t (*parse)(char *, iidesc_t *);
411 if (setjmp(resetbuf))
415 ii = iidesc_new(NULL);
416 cp = name(cp, &ii->ii_name);
418 switch (stab->n_type) {
441 parse_debug(1, cp, "Unknown stab type %#x", stab->n_type);
442 bzero(&resetbuf, sizeof (resetbuf));
447 bzero(&resetbuf, sizeof (resetbuf));
449 if (rc < 0 || ii->ii_type == II_NOT) {
450 iidesc_free(ii, NULL);
460 * Check if we have this node in the hash table already
465 int bucket = HASH(h);
466 tdesc_t *tdp = hash_table[bucket];
468 while (tdp != NULL) {
481 for (c = *cp++; isspace(c); c = *cp++)
488 name(char *cp, char **w)
497 else if (isalpha(c) || strchr("_.$#", c)) {
498 for (c = *cp++; isalnum(c) || strchr(" _.$#", c); c = *cp++)
504 while (orig < cp - 1)
507 *w = new - (len - 1);
515 number(char *cp, int *n)
519 *n = (int)strtol(cp, &next, 10);
521 expected("number", "<number>", cp);
530 if (*cp == '(') { /* SunPro style */
532 cp = number(cp, &n1);
534 expected("id", ",", cp - 1);
535 cp = number(cp, &n2);
537 expected("id", ")", cp - 1);
538 *h = MAKETYPEID(n1, n2);
539 } else if (isdigit(*cp)) { /* gcc style */
540 cp = number(cp, &n1);
543 expected("id", "(/0-9", cp);
549 tagadd(char *w, int h, tdesc_t *tdp)
554 if (!(otdp = lookup(h)))
556 else if (otdp != tdp) {
557 warning("duplicate entry\n");
558 warning(" old: %s %d (%d,%d)\n", tdesc_name(otdp),
559 otdp->t_type, TYPEFILE(otdp->t_id), TYPENUM(otdp->t_id));
560 warning(" new: %s %d (%d,%d)\n", tdesc_name(tdp),
561 tdp->t_type, TYPEFILE(tdp->t_id), TYPENUM(tdp->t_id));
569 tdefdecl(char *cp, int h, tdesc_t **rtdp)
576 parse_debug(3, cp, "tdefdecl h=%d", h);
579 switch (type = *cp) {
580 case 'b': /* integer */
582 cp = intrinsic(cp, rtdp);
584 case '(': /* equiv to another type */
588 if (ntdp != NULL && *cp == '=') {
589 if (ntdp->t_type == FORWARD && *(cp + 1) == 'x') {
591 * The 6.2 compiler, and possibly others, will
592 * sometimes emit the same stab for a forward
593 * declaration twice. That is, "(1,2)=xsfoo:"
594 * will sometimes show up in two different
595 * places. This is, of course, quite fun. We
596 * want CTF to work in spite of the compiler,
597 * so we'll let this one through.
602 if (!strchr("sue", *c2++)) {
603 expected("tdefdecl/x-redefine", "[sue]",
608 if (strcmp(nm, ntdp->t_name) != 0) {
609 terminate("Stabs error: Attempt to "
610 "redefine type (%d,%d) as "
611 "something else: %s\n",
612 TYPEFILE(h2), TYPENUM(h2),
617 h2 = faketypenumber++;
620 terminate("Stabs error: Attempting to "
621 "redefine type (%d,%d)\n", TYPEFILE(h2),
626 if (ntdp == NULL) { /* if that type isn't defined yet */
628 /* record it as unresolved */
629 parse_debug(3, NULL, "tdefdecl unres type %d",
631 *rtdp = calloc(sizeof (**rtdp), 1);
632 (*rtdp)->t_type = TYPEDEF_UNRES;
638 /* define a new type */
639 cp = tdefdecl(cp, h2, rtdp);
640 if ((*rtdp)->t_id && (*rtdp)->t_id != h2) {
641 ntdp = calloc(sizeof (*ntdp), 1);
642 ntdp->t_type = TYPEDEF;
643 ntdp->t_tdesc = *rtdp;
649 } else { /* that type is already defined */
650 if (ntdp->t_type != TYPEDEF || ntdp->t_name != NULL) {
654 "No duplicate typedef anon for ref");
661 cp = tdefdecl(cp + 1, h, &ntdp);
663 expected("tdefdecl/*", "id", cp);
666 ntdp->t_id = faketypenumber++;
668 *rtdp = xcalloc(sizeof (**rtdp));
669 (*rtdp)->t_type = POINTER;
672 (*rtdp)->t_tdesc = ntdp;
675 cp = tdefdecl(cp + 1, h, &ntdp);
676 *rtdp = xcalloc(sizeof (**rtdp));
677 (*rtdp)->t_type = FUNCTION;
680 (*rtdp)->t_fndef = xcalloc(sizeof (fndef_t));
682 * The 6.1 compiler will sometimes generate incorrect stabs for
683 * function pointers (it'll get the return type wrong). This
684 * causes merges to fail. We therefore treat function pointers
685 * as if they all point to functions that return int. When
686 * 4432549 is fixed, the lookupname() call below should be
687 * replaced with `ntdp'.
689 (*rtdp)->t_fndef->fn_ret = lookupname("int");
695 expected("tdefdecl/[az]", "r", cp - 1);
696 *rtdp = xcalloc(sizeof (**rtdp));
697 (*rtdp)->t_type = ARRAY;
699 cp = arraydef(cp, rtdp);
703 if (c != 's' && c != 'u' && c != 'e')
704 expected("tdefdecl/x", "[sue]", cp - 1);
705 cp = name(cp + 1, &w);
707 ntdp = xcalloc(sizeof (*ntdp));
708 ntdp->t_type = FORWARD;
711 * We explicitly don't set t_id here - the caller will do it.
712 * The caller may want to use a real type ID, or they may
713 * choose to make one up.
719 case 'B': /* volatile */
720 cp = tdefdecl(cp + 1, h, &ntdp);
723 ntdp->t_id = faketypenumber++;
725 *rtdp = xcalloc(sizeof (**rtdp));
726 (*rtdp)->t_type = VOLATILE;
728 (*rtdp)->t_tdesc = ntdp;
732 case 'k': /* const */
733 cp = tdefdecl(cp + 1, h, &ntdp);
736 ntdp->t_id = faketypenumber++;
738 *rtdp = xcalloc(sizeof (**rtdp));
739 (*rtdp)->t_type = CONST;
741 (*rtdp)->t_tdesc = ntdp;
745 case 'K': /* restricted */
746 cp = tdefdecl(cp + 1, h, &ntdp);
749 ntdp->t_id = faketypenumber++;
751 *rtdp = xcalloc(sizeof (**rtdp));
752 (*rtdp)->t_type = RESTRICT;
754 (*rtdp)->t_tdesc = ntdp;
762 *rtdp = xcalloc(sizeof (**rtdp));
763 (*rtdp)->t_name = NULL;
764 cp = soudef(cp, (type == 'u') ? UNION : STRUCT, rtdp);
767 expected("tdefdecl", "<type code>", cp);
773 intrinsic(char *cp, tdesc_t **rtdp)
775 intr_t *intr = xcalloc(sizeof (intr_t));
781 intr->intr_type = INTR_INT;
783 intr->intr_signed = 1;
785 expected("intrinsic/b", "[su]", cp);
788 if (strchr("cbv", *cp))
789 intr->intr_iformat = *cp++;
791 cp = number(cp, &width);
793 expected("intrinsic/b", "; (post-width)", cp - 1);
795 cp = number(cp, &intr->intr_offset);
797 expected("intrinsic/b", "; (post-offset)", cp - 1);
799 cp = number(cp, &intr->intr_nbits);
803 intr->intr_type = INTR_REAL;
804 for (fmt = 0, i = 0; isdigit(*(cp + i)); i++)
805 fmt = fmt * 10 + (*(cp + i) - '0');
807 if (fmt < 1 || fmt > CTF_FP_MAX)
808 expected("intrinsic/R", "number <= CTF_FP_MAX", cp);
810 intr->intr_fformat = fmt;
814 expected("intrinsic/R", ";", cp - 1);
815 cp = number(cp, &width);
817 intr->intr_nbits = width * 8;
821 tdp = xcalloc(sizeof (*tdp));
822 tdp->t_type = INTRINSIC;
826 parse_debug(3, NULL, "intrinsic: size=%d", width);
833 bitintrinsic(tdesc_t *template, int nbits)
835 tdesc_t *newtdp = xcalloc(sizeof (tdesc_t));
837 newtdp->t_name = xstrdup(template->t_name);
838 newtdp->t_id = faketypenumber++;
839 newtdp->t_type = INTRINSIC;
840 newtdp->t_size = template->t_size;
841 newtdp->t_intr = xmalloc(sizeof (intr_t));
842 bcopy(template->t_intr, newtdp->t_intr, sizeof (intr_t));
843 newtdp->t_intr->intr_nbits = nbits;
849 offsize(char *cp, mlist_t *mlp)
855 cp = number(cp, &offset);
857 expected("offsize/2", ",", cp - 1);
858 cp = number(cp, &size);
860 expected("offsize/3", ";", cp - 1);
861 mlp->ml_offset = offset;
867 find_intrinsic(tdesc_t *tdp)
870 switch (tdp->t_type) {
885 soudef(char *cp, stabtype_t type, tdesc_t **rtdp)
887 mlist_t *mlp, **prev;
893 cp = number(cp, &size);
894 (*rtdp)->t_size = size;
895 (*rtdp)->t_type = type; /* s or u */
898 * An '@' here indicates a bitmask follows. This is so the
899 * compiler can pass information to debuggers about how structures
900 * are passed in the v9 world. We don't need this information
901 * so we skip over it.
907 parse_debug(3, cp, "soudef: %s size=%d", tdesc_name(*rtdp),
910 prev = &((*rtdp)->t_members);
911 /* now fill up the fields */
912 while ((*cp != '\0') && (*cp != ';')) { /* signifies end of fields */
913 mlp = xcalloc(sizeof (*mlp));
919 * find the tdesc struct in the hash table for this type
920 * and stick a ptr in here
923 if (tdp == NULL) { /* not in hash list */
924 parse_debug(3, NULL, " defines %s (%d)", w, h);
928 " refers to %s (unresolved %d)",
929 (w ? w : "anon"), h);
931 cp = tdefdecl(cp, h, &tdp);
933 if (tdp->t_id && tdp->t_id != h) {
934 tdesc_t *ntdp = xcalloc(sizeof (*ntdp));
936 ntdp->t_type = TYPEDEF;
943 " soudef now looking at ");
947 parse_debug(3, NULL, " refers to %s (%d, %s)",
948 w ? w : "anon", h, tdesc_name(tdp));
951 cp = offsize(cp, mlp);
953 itdp = find_intrinsic(tdp);
954 if (itdp->t_type == INTRINSIC) {
955 if (mlp->ml_size != itdp->t_intr->intr_nbits) {
956 parse_debug(4, cp, "making %d bit intrinsic "
957 "from %s", mlp->ml_size, tdesc_name(itdp));
958 mlp->ml_type = bitintrinsic(itdp, mlp->ml_size);
961 } else if (itdp->t_type == TYPEDEF_UNRES) {
962 list_add(&typedbitfldmems, mlp);
968 /* cp is now pointing to next field */
969 prev = &mlp->ml_next;
975 arraydef(char *cp, tdesc_t **rtdp)
981 expected("arraydef/1", ";", cp - 1);
983 (*rtdp)->t_ardef = xcalloc(sizeof (ardef_t));
984 (*rtdp)->t_ardef->ad_idxtype = lookup(h);
986 cp = number(cp, &start); /* lower */
988 expected("arraydef/2", ";", cp - 1);
992 * variable length array - treat as null dimensioned
994 * For VLA variables on sparc, SS12 generated stab entry
996 * .stabs "buf:(0,28)=zr(0,4);0;S-12;(0,1)", 0x80, 0, 0, -16
997 * Whereas SS12u1 generated stab entry looks like this:
998 * .stabs "buf:(0,28)=zr(0,4);0;S0;(0,1)", 0x80, 0, 0, 0
999 * On x86, both versions generate the first type of entry.
1000 * We should be able to parse both.
1005 cp = number(cp, &end);
1009 * normal fixed-dimension array
1010 * Stab entry for this looks as follows :
1011 * .stabs "x:(0,28)=ar(0,4);0;9;(0,3)", 0x80, 0, 40, 0
1013 cp = number(cp, &end); /* upper */
1017 expected("arraydef/3", ";", cp - 1);
1018 (*rtdp)->t_ardef->ad_nelems = end - start + 1;
1019 cp = tdefdecl(cp, h, &((*rtdp)->t_ardef->ad_contents));
1021 parse_debug(3, cp, "defined array idx type %d %d-%d next ",
1028 enumdef(char *cp, tdesc_t **rtdp)
1030 elist_t *elp, **prev;
1033 (*rtdp)->t_type = ENUM;
1034 (*rtdp)->t_emem = NULL;
1036 prev = &((*rtdp)->t_emem);
1037 while (*cp != ';') {
1038 elp = xcalloc(sizeof (*elp));
1039 elp->el_next = NULL;
1043 cp = number(cp, &elp->el_number);
1044 parse_debug(3, NULL, "enum %s: %s=%d", tdesc_name(*rtdp),
1045 elp->el_name, elp->el_number);
1046 prev = &elp->el_next;
1048 expected("enumdef", ",", cp - 1);
1053 lookup_name(tdesc_t **hash, const char *name1)
1055 int bucket = compute_sum(name1);
1056 tdesc_t *tdp, *ttdp = NULL;
1058 for (tdp = hash[bucket]; tdp != NULL; tdp = tdp->t_next) {
1059 if (tdp->t_name != NULL && strcmp(tdp->t_name, name1) == 0) {
1060 if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
1061 tdp->t_type == ENUM || tdp->t_type == INTRINSIC)
1063 if (tdp->t_type == TYPEDEF)
1071 lookupname(const char *name1)
1073 return (lookup_name(name_table, name1));
1077 * Add a node to the hash queues.
1080 addhash(tdesc_t *tdp, int num)
1082 int hash = HASH(num);
1084 char added_num = 0, added_name = 0;
1087 * If it already exists in the hash table don't add it again
1088 * (but still check to see if the name should be hashed).
1094 tdp->t_hash = hash_table[hash];
1095 hash_table[hash] = tdp;
1099 if (tdp->t_name != NULL) {
1100 ttdp = lookupname(tdp->t_name);
1102 hash = compute_sum(tdp->t_name);
1103 tdp->t_next = name_table[hash];
1104 name_table[hash] = tdp;
1108 if (!added_num && !added_name) {
1109 terminate("stabs: broken hash\n");
1114 compute_sum(const char *w)
1119 for (sum = 0; (c = *w) != '\0'; sum += c, w++)
1127 longjmp(resetbuf, 1);
1136 printf("checking hash\n");
1137 for (i = 0; i < BUCKETS; i++) {
1138 if (hash_table[i]) {
1139 for (tdp = hash_table[i]->t_hash;
1140 tdp && tdp != hash_table[i];
1144 terminate("cycle in hash bucket %d\n", i);
1149 if (name_table[i]) {
1150 for (tdp = name_table[i]->t_next;
1151 tdp && tdp != name_table[i];
1155 terminate("cycle in name bucket %d\n", i);
1165 resolve_typed_bitfields_cb(void *arg, void *private __unused)
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 resolve_typed_bitfields_cb, NULL);