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