1 /*- 2 * Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 #include <sys/types.h> 27 #include <assert.h> 28 #include <ctype.h> 29 #include <errno.h> 30 #include <libelftc.h> 31 #include <limits.h> 32 #include <stdbool.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 37 #include "_libelftc.h" 38 39 ELFTC_VCSID("$Id: libelftc_dem_gnu3.c 3512 2016-12-29 07:04:19Z kaiwang27 $"); 40 41 /** 42 * @file cpp_demangle.c 43 * @brief Decode IA-64 C++ ABI style implementation. 44 * 45 * IA-64 standard ABI(Itanium C++ ABI) references. 46 * 47 * http://www.codesourcery.com/cxx-abi/abi.html#mangling \n 48 * http://www.codesourcery.com/cxx-abi/abi-mangling.html 49 */ 50 51 enum type_qualifier { 52 TYPE_PTR, TYPE_REF, TYPE_CMX, TYPE_IMG, TYPE_EXT, TYPE_RST, TYPE_VAT, 53 TYPE_CST, TYPE_VEC 54 }; 55 56 struct vector_type_qualifier { 57 size_t size, capacity; 58 enum type_qualifier *q_container; 59 struct vector_str ext_name; 60 }; 61 62 enum read_cmd { 63 READ_FAIL, READ_NEST, READ_TMPL, READ_EXPR, READ_EXPL, READ_LOCAL, 64 READ_TYPE, READ_FUNC, READ_PTRMEM 65 }; 66 67 struct vector_read_cmd { 68 size_t size, capacity; 69 enum read_cmd *r_container; 70 }; 71 72 struct cpp_demangle_data { 73 struct vector_str output; /* output string vector */ 74 struct vector_str output_tmp; 75 struct vector_str subst; /* substitution string vector */ 76 struct vector_str tmpl; 77 struct vector_str class_type; 78 struct vector_read_cmd cmd; 79 bool paren; /* parenthesis opened */ 80 bool pfirst; /* first element of parameter */ 81 bool mem_rst; /* restrict member function */ 82 bool mem_vat; /* volatile member function */ 83 bool mem_cst; /* const member function */ 84 int func_type; 85 const char *cur; /* current mangled name ptr */ 86 const char *last_sname; /* last source name */ 87 int push_head; 88 }; 89 90 #define CPP_DEMANGLE_TRY_LIMIT 128 91 #define FLOAT_SPRINTF_TRY_LIMIT 5 92 #define FLOAT_QUADRUPLE_BYTES 16 93 #define FLOAT_EXTENED_BYTES 10 94 95 #define SIMPLE_HASH(x,y) (64 * x + y) 96 #define DEM_PUSH_STR(d,s) cpp_demangle_push_str((d), (s), strlen((s))) 97 #define VEC_PUSH_STR(d,s) vector_str_push((d), (s), strlen((s))) 98 99 static void cpp_demangle_data_dest(struct cpp_demangle_data *); 100 static int cpp_demangle_data_init(struct cpp_demangle_data *, 101 const char *); 102 static int cpp_demangle_get_subst(struct cpp_demangle_data *, size_t); 103 static int cpp_demangle_get_tmpl_param(struct cpp_demangle_data *, size_t); 104 static int cpp_demangle_push_fp(struct cpp_demangle_data *, 105 char *(*)(const char *, size_t)); 106 static int cpp_demangle_push_str(struct cpp_demangle_data *, const char *, 107 size_t); 108 static int cpp_demangle_push_subst(struct cpp_demangle_data *, 109 const char *, size_t); 110 static int cpp_demangle_push_subst_v(struct cpp_demangle_data *, 111 struct vector_str *); 112 static int cpp_demangle_push_type_qualifier(struct cpp_demangle_data *, 113 struct vector_type_qualifier *, const char *); 114 static int cpp_demangle_read_array(struct cpp_demangle_data *); 115 static int cpp_demangle_read_encoding(struct cpp_demangle_data *); 116 static int cpp_demangle_read_expr_primary(struct cpp_demangle_data *); 117 static int cpp_demangle_read_expression(struct cpp_demangle_data *); 118 static int cpp_demangle_read_expression_flat(struct cpp_demangle_data *, 119 char **); 120 static int cpp_demangle_read_expression_binary(struct cpp_demangle_data *, 121 const char *, size_t); 122 static int cpp_demangle_read_expression_unary(struct cpp_demangle_data *, 123 const char *, size_t); 124 static int cpp_demangle_read_expression_trinary(struct cpp_demangle_data *, 125 const char *, size_t, const char *, size_t); 126 static int cpp_demangle_read_function(struct cpp_demangle_data *, int *, 127 struct vector_type_qualifier *); 128 static int cpp_demangle_local_source_name(struct cpp_demangle_data *ddata); 129 static int cpp_demangle_read_local_name(struct cpp_demangle_data *); 130 static int cpp_demangle_read_name(struct cpp_demangle_data *); 131 static int cpp_demangle_read_name_flat(struct cpp_demangle_data *, 132 char**); 133 static int cpp_demangle_read_nested_name(struct cpp_demangle_data *); 134 static int cpp_demangle_read_number(struct cpp_demangle_data *, long *); 135 static int cpp_demangle_read_number_as_string(struct cpp_demangle_data *, 136 char **); 137 static int cpp_demangle_read_nv_offset(struct cpp_demangle_data *); 138 static int cpp_demangle_read_offset(struct cpp_demangle_data *); 139 static int cpp_demangle_read_offset_number(struct cpp_demangle_data *); 140 static int cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *); 141 static int cpp_demangle_read_sname(struct cpp_demangle_data *); 142 static int cpp_demangle_read_subst(struct cpp_demangle_data *); 143 static int cpp_demangle_read_subst_std(struct cpp_demangle_data *); 144 static int cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *, 145 const char *, size_t); 146 static int cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *); 147 static int cpp_demangle_read_tmpl_args(struct cpp_demangle_data *); 148 static int cpp_demangle_read_tmpl_param(struct cpp_demangle_data *); 149 static int cpp_demangle_read_type(struct cpp_demangle_data *, int); 150 static int cpp_demangle_read_type_flat(struct cpp_demangle_data *, 151 char **); 152 static int cpp_demangle_read_uqname(struct cpp_demangle_data *); 153 static int cpp_demangle_read_v_offset(struct cpp_demangle_data *); 154 static char *decode_fp_to_double(const char *, size_t); 155 static char *decode_fp_to_float(const char *, size_t); 156 static char *decode_fp_to_float128(const char *, size_t); 157 static char *decode_fp_to_float80(const char *, size_t); 158 static char *decode_fp_to_long_double(const char *, size_t); 159 static int hex_to_dec(char); 160 static void vector_read_cmd_dest(struct vector_read_cmd *); 161 static int vector_read_cmd_find(struct vector_read_cmd *, enum read_cmd); 162 static int vector_read_cmd_init(struct vector_read_cmd *); 163 static int vector_read_cmd_pop(struct vector_read_cmd *); 164 static int vector_read_cmd_push(struct vector_read_cmd *, enum read_cmd); 165 static void vector_type_qualifier_dest(struct vector_type_qualifier *); 166 static int vector_type_qualifier_init(struct vector_type_qualifier *); 167 static int vector_type_qualifier_push(struct vector_type_qualifier *, 168 enum type_qualifier); 169 170 /** 171 * @brief Decode the input string by IA-64 C++ ABI style. 172 * 173 * GNU GCC v3 use IA-64 standard ABI. 174 * @return New allocated demangled string or NULL if failed. 175 * @todo 1. Testing and more test case. 2. Code cleaning. 176 */ 177 char * 178 cpp_demangle_gnu3(const char *org) 179 { 180 struct cpp_demangle_data ddata; 181 ssize_t org_len; 182 unsigned int limit; 183 char *rtn; 184 185 if (org == NULL || (org_len = strlen(org)) < 2) 186 return (NULL); 187 188 if (org_len > 11 && !strncmp(org, "_GLOBAL__I_", 11)) { 189 if ((rtn = malloc(org_len + 19)) == NULL) 190 return (NULL); 191 snprintf(rtn, org_len + 19, 192 "global constructors keyed to %s", org + 11); 193 return (rtn); 194 } 195 196 if (org[0] != '_' || org[1] != 'Z') 197 return (NULL); 198 199 if (!cpp_demangle_data_init(&ddata, org + 2)) 200 return (NULL); 201 202 rtn = NULL; 203 204 if (!cpp_demangle_read_encoding(&ddata)) 205 goto clean; 206 207 limit = 0; 208 while (*ddata.cur != '\0') { 209 /* 210 * Breaking at some gcc info at tail. e.g) @@GLIBCXX_3.4 211 */ 212 if (*ddata.cur == '@' && *(ddata.cur + 1) == '@') 213 break; 214 if (!cpp_demangle_read_type(&ddata, 1)) 215 goto clean; 216 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 217 goto clean; 218 } 219 220 if (ddata.output.size == 0) 221 goto clean; 222 if (ddata.paren && !VEC_PUSH_STR(&ddata.output, ")")) 223 goto clean; 224 if (ddata.mem_vat && !VEC_PUSH_STR(&ddata.output, " volatile")) 225 goto clean; 226 if (ddata.mem_cst && !VEC_PUSH_STR(&ddata.output, " const")) 227 goto clean; 228 if (ddata.mem_rst && !VEC_PUSH_STR(&ddata.output, " restrict")) 229 goto clean; 230 231 rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL); 232 233 clean: 234 cpp_demangle_data_dest(&ddata); 235 236 return (rtn); 237 } 238 239 static void 240 cpp_demangle_data_dest(struct cpp_demangle_data *d) 241 { 242 243 if (d == NULL) 244 return; 245 246 vector_read_cmd_dest(&d->cmd); 247 vector_str_dest(&d->class_type); 248 vector_str_dest(&d->tmpl); 249 vector_str_dest(&d->subst); 250 vector_str_dest(&d->output_tmp); 251 vector_str_dest(&d->output); 252 } 253 254 static int 255 cpp_demangle_data_init(struct cpp_demangle_data *d, const char *cur) 256 { 257 258 if (d == NULL || cur == NULL) 259 return (0); 260 261 if (!vector_str_init(&d->output)) 262 return (0); 263 if (!vector_str_init(&d->output_tmp)) 264 goto clean1; 265 if (!vector_str_init(&d->subst)) 266 goto clean2; 267 if (!vector_str_init(&d->tmpl)) 268 goto clean3; 269 if (!vector_str_init(&d->class_type)) 270 goto clean4; 271 if (!vector_read_cmd_init(&d->cmd)) 272 goto clean5; 273 274 assert(d->output.container != NULL); 275 assert(d->output_tmp.container != NULL); 276 assert(d->subst.container != NULL); 277 assert(d->tmpl.container != NULL); 278 assert(d->class_type.container != NULL); 279 280 d->paren = false; 281 d->pfirst = false; 282 d->mem_rst = false; 283 d->mem_vat = false; 284 d->mem_cst = false; 285 d->func_type = 0; 286 d->cur = cur; 287 d->last_sname = NULL; 288 d->push_head = 0; 289 290 return (1); 291 292 clean5: 293 vector_str_dest(&d->class_type); 294 clean4: 295 vector_str_dest(&d->tmpl); 296 clean3: 297 vector_str_dest(&d->subst); 298 clean2: 299 vector_str_dest(&d->output_tmp); 300 clean1: 301 vector_str_dest(&d->output); 302 303 return (0); 304 } 305 306 static int 307 cpp_demangle_push_fp(struct cpp_demangle_data *ddata, 308 char *(*decoder)(const char *, size_t)) 309 { 310 size_t len; 311 int rtn; 312 const char *fp; 313 char *f; 314 315 if (ddata == NULL || decoder == NULL) 316 return (0); 317 318 fp = ddata->cur; 319 while (*ddata->cur != 'E') 320 ++ddata->cur; 321 322 if ((f = decoder(fp, ddata->cur - fp)) == NULL) 323 return (0); 324 325 rtn = 0; 326 if ((len = strlen(f)) > 0) 327 rtn = cpp_demangle_push_str(ddata, f, len); 328 329 free(f); 330 331 ++ddata->cur; 332 333 return (rtn); 334 } 335 336 static int 337 cpp_demangle_push_str(struct cpp_demangle_data *ddata, const char *str, 338 size_t len) 339 { 340 341 if (ddata == NULL || str == NULL || len == 0) 342 return (0); 343 344 if (ddata->push_head > 0) 345 return (vector_str_push(&ddata->output_tmp, str, len)); 346 347 return (vector_str_push(&ddata->output, str, len)); 348 } 349 350 static int 351 cpp_demangle_push_subst(struct cpp_demangle_data *ddata, const char *str, 352 size_t len) 353 { 354 355 if (ddata == NULL || str == NULL || len == 0) 356 return (0); 357 358 if (!vector_str_find(&ddata->subst, str, len)) 359 return (vector_str_push(&ddata->subst, str, len)); 360 361 return (1); 362 } 363 364 static int 365 cpp_demangle_push_subst_v(struct cpp_demangle_data *ddata, struct vector_str *v) 366 { 367 size_t str_len; 368 int rtn; 369 char *str; 370 371 if (ddata == NULL || v == NULL) 372 return (0); 373 374 if ((str = vector_str_get_flat(v, &str_len)) == NULL) 375 return (0); 376 377 rtn = cpp_demangle_push_subst(ddata, str, str_len); 378 379 free(str); 380 381 return (rtn); 382 } 383 384 static int 385 cpp_demangle_push_type_qualifier(struct cpp_demangle_data *ddata, 386 struct vector_type_qualifier *v, const char *type_str) 387 { 388 struct vector_str subst_v; 389 size_t idx, e_idx, e_len; 390 int rtn; 391 char *buf; 392 393 if (ddata == NULL || v == NULL) 394 return (0); 395 396 if ((idx = v->size) == 0) 397 return (1); 398 399 rtn = 0; 400 if (type_str != NULL) { 401 if (!vector_str_init(&subst_v)) 402 return (0); 403 if (!VEC_PUSH_STR(&subst_v, type_str)) 404 goto clean; 405 } 406 407 e_idx = 0; 408 while (idx > 0) { 409 switch (v->q_container[idx - 1]) { 410 case TYPE_PTR: 411 if (!DEM_PUSH_STR(ddata, "*")) 412 goto clean; 413 if (type_str != NULL) { 414 if (!VEC_PUSH_STR(&subst_v, "*")) 415 goto clean; 416 if (!cpp_demangle_push_subst_v(ddata, 417 &subst_v)) 418 goto clean; 419 } 420 break; 421 422 case TYPE_REF: 423 if (!DEM_PUSH_STR(ddata, "&")) 424 goto clean; 425 if (type_str != NULL) { 426 if (!VEC_PUSH_STR(&subst_v, "&")) 427 goto clean; 428 if (!cpp_demangle_push_subst_v(ddata, 429 &subst_v)) 430 goto clean; 431 } 432 break; 433 434 case TYPE_CMX: 435 if (!DEM_PUSH_STR(ddata, " complex")) 436 goto clean; 437 if (type_str != NULL) { 438 if (!VEC_PUSH_STR(&subst_v, " complex")) 439 goto clean; 440 if (!cpp_demangle_push_subst_v(ddata, 441 &subst_v)) 442 goto clean; 443 } 444 break; 445 446 case TYPE_IMG: 447 if (!DEM_PUSH_STR(ddata, " imaginary")) 448 goto clean; 449 if (type_str != NULL) { 450 if (!vector_str_push(&subst_v, " imaginary", 451 10)) 452 goto clean; 453 if (!cpp_demangle_push_subst_v(ddata, 454 &subst_v)) 455 goto clean; 456 } 457 break; 458 459 case TYPE_EXT: 460 if (v->ext_name.size == 0 || 461 e_idx > v->ext_name.size - 1) 462 goto clean; 463 if ((e_len = strlen(v->ext_name.container[e_idx])) == 464 0) 465 goto clean; 466 if ((buf = malloc(e_len + 2)) == NULL) 467 goto clean; 468 snprintf(buf, e_len + 2, " %s", 469 v->ext_name.container[e_idx]); 470 471 if (!DEM_PUSH_STR(ddata, buf)) { 472 free(buf); 473 goto clean; 474 } 475 476 if (type_str != NULL) { 477 if (!VEC_PUSH_STR(&subst_v, buf)) { 478 free(buf); 479 goto clean; 480 } 481 if (!cpp_demangle_push_subst_v(ddata, 482 &subst_v)) { 483 free(buf); 484 goto clean; 485 } 486 } 487 free(buf); 488 ++e_idx; 489 break; 490 491 case TYPE_RST: 492 if (!DEM_PUSH_STR(ddata, " restrict")) 493 goto clean; 494 if (type_str != NULL) { 495 if (!VEC_PUSH_STR(&subst_v, " restrict")) 496 goto clean; 497 if (!cpp_demangle_push_subst_v(ddata, 498 &subst_v)) 499 goto clean; 500 } 501 break; 502 503 case TYPE_VAT: 504 if (!DEM_PUSH_STR(ddata, " volatile")) 505 goto clean; 506 if (type_str != NULL) { 507 if (!VEC_PUSH_STR(&subst_v, " volatile")) 508 goto clean; 509 if (!cpp_demangle_push_subst_v(ddata, 510 &subst_v)) 511 goto clean; 512 } 513 break; 514 515 case TYPE_CST: 516 if (!DEM_PUSH_STR(ddata, " const")) 517 goto clean; 518 if (type_str != NULL) { 519 if (!VEC_PUSH_STR(&subst_v, " const")) 520 goto clean; 521 if (!cpp_demangle_push_subst_v(ddata, 522 &subst_v)) 523 goto clean; 524 } 525 break; 526 527 case TYPE_VEC: 528 if (v->ext_name.size == 0 || 529 e_idx > v->ext_name.size - 1) 530 goto clean; 531 if ((e_len = strlen(v->ext_name.container[e_idx])) == 532 0) 533 goto clean; 534 if ((buf = malloc(e_len + 12)) == NULL) 535 goto clean; 536 snprintf(buf, e_len + 12, " __vector(%s)", 537 v->ext_name.container[e_idx]); 538 if (!DEM_PUSH_STR(ddata, buf)) { 539 free(buf); 540 goto clean; 541 } 542 if (type_str != NULL) { 543 if (!VEC_PUSH_STR(&subst_v, buf)) { 544 free(buf); 545 goto clean; 546 } 547 if (!cpp_demangle_push_subst_v(ddata, 548 &subst_v)) { 549 free(buf); 550 goto clean; 551 } 552 } 553 free(buf); 554 ++e_idx; 555 break; 556 } 557 --idx; 558 } 559 560 rtn = 1; 561 clean: 562 if (type_str != NULL) 563 vector_str_dest(&subst_v); 564 565 return (rtn); 566 } 567 568 static int 569 cpp_demangle_get_subst(struct cpp_demangle_data *ddata, size_t idx) 570 { 571 size_t len; 572 573 if (ddata == NULL || ddata->subst.size <= idx) 574 return (0); 575 if ((len = strlen(ddata->subst.container[idx])) == 0) 576 return (0); 577 if (!cpp_demangle_push_str(ddata, ddata->subst.container[idx], len)) 578 return (0); 579 580 /* skip '_' */ 581 ++ddata->cur; 582 583 return (1); 584 } 585 586 static int 587 cpp_demangle_get_tmpl_param(struct cpp_demangle_data *ddata, size_t idx) 588 { 589 size_t len; 590 591 if (ddata == NULL || ddata->tmpl.size <= idx) 592 return (0); 593 if ((len = strlen(ddata->tmpl.container[idx])) == 0) 594 return (0); 595 if (!cpp_demangle_push_str(ddata, ddata->tmpl.container[idx], len)) 596 return (0); 597 598 ++ddata->cur; 599 600 return (1); 601 } 602 603 static int 604 cpp_demangle_read_array(struct cpp_demangle_data *ddata) 605 { 606 size_t i, num_len, exp_len, p_idx, idx; 607 const char *num; 608 char *exp; 609 610 if (ddata == NULL || *(++ddata->cur) == '\0') 611 return (0); 612 613 if (*ddata->cur == '_') { 614 if (*(++ddata->cur) == '\0') 615 return (0); 616 617 if (!cpp_demangle_read_type(ddata, 0)) 618 return (0); 619 620 if (!DEM_PUSH_STR(ddata, "[]")) 621 return (0); 622 } else { 623 if (ELFTC_ISDIGIT(*ddata->cur) != 0) { 624 num = ddata->cur; 625 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 626 ++ddata->cur; 627 if (*ddata->cur != '_') 628 return (0); 629 num_len = ddata->cur - num; 630 assert(num_len > 0); 631 if (*(++ddata->cur) == '\0') 632 return (0); 633 if (!cpp_demangle_read_type(ddata, 0)) 634 return (0); 635 if (!DEM_PUSH_STR(ddata, "[")) 636 return (0); 637 if (!cpp_demangle_push_str(ddata, num, num_len)) 638 return (0); 639 if (!DEM_PUSH_STR(ddata, "]")) 640 return (0); 641 } else { 642 p_idx = ddata->output.size; 643 if (!cpp_demangle_read_expression(ddata)) 644 return (0); 645 if ((exp = vector_str_substr(&ddata->output, p_idx, 646 ddata->output.size - 1, &exp_len)) == NULL) 647 return (0); 648 idx = ddata->output.size; 649 for (i = p_idx; i < idx; ++i) 650 if (!vector_str_pop(&ddata->output)) { 651 free(exp); 652 return (0); 653 } 654 if (*ddata->cur != '_') { 655 free(exp); 656 return (0); 657 } 658 ++ddata->cur; 659 if (*ddata->cur == '\0') { 660 free(exp); 661 return (0); 662 } 663 if (!cpp_demangle_read_type(ddata, 0)) { 664 free(exp); 665 return (0); 666 } 667 if (!DEM_PUSH_STR(ddata, "[")) { 668 free(exp); 669 return (0); 670 } 671 if (!cpp_demangle_push_str(ddata, exp, exp_len)) { 672 free(exp); 673 return (0); 674 } 675 if (!DEM_PUSH_STR(ddata, "]")) { 676 free(exp); 677 return (0); 678 } 679 free(exp); 680 } 681 } 682 683 return (1); 684 } 685 686 static int 687 cpp_demangle_read_expr_primary(struct cpp_demangle_data *ddata) 688 { 689 const char *num; 690 691 if (ddata == NULL || *(++ddata->cur) == '\0') 692 return (0); 693 694 if (*ddata->cur == '_' && *(ddata->cur + 1) == 'Z') { 695 ddata->cur += 2; 696 if (*ddata->cur == '\0') 697 return (0); 698 if (!cpp_demangle_read_encoding(ddata)) 699 return (0); 700 ++ddata->cur; 701 return (1); 702 } 703 704 switch (*ddata->cur) { 705 case 'b': 706 if (*(ddata->cur + 2) != 'E') 707 return (0); 708 switch (*(++ddata->cur)) { 709 case '0': 710 ddata->cur += 2; 711 return (DEM_PUSH_STR(ddata, "false")); 712 case '1': 713 ddata->cur += 2; 714 return (DEM_PUSH_STR(ddata, "true")); 715 default: 716 return (0); 717 } 718 719 case 'd': 720 ++ddata->cur; 721 return (cpp_demangle_push_fp(ddata, decode_fp_to_double)); 722 723 case 'e': 724 ++ddata->cur; 725 if (sizeof(long double) == 10) 726 return (cpp_demangle_push_fp(ddata, 727 decode_fp_to_double)); 728 return (cpp_demangle_push_fp(ddata, decode_fp_to_float80)); 729 730 case 'f': 731 ++ddata->cur; 732 return (cpp_demangle_push_fp(ddata, decode_fp_to_float)); 733 734 case 'g': 735 ++ddata->cur; 736 if (sizeof(long double) == 16) 737 return (cpp_demangle_push_fp(ddata, 738 decode_fp_to_double)); 739 return (cpp_demangle_push_fp(ddata, decode_fp_to_float128)); 740 741 case 'i': 742 case 'j': 743 case 'l': 744 case 'm': 745 case 'n': 746 case 's': 747 case 't': 748 case 'x': 749 case 'y': 750 if (*(++ddata->cur) == 'n') { 751 if (!DEM_PUSH_STR(ddata, "-")) 752 return (0); 753 ++ddata->cur; 754 } 755 num = ddata->cur; 756 while (*ddata->cur != 'E') { 757 if (!ELFTC_ISDIGIT(*ddata->cur)) 758 return (0); 759 ++ddata->cur; 760 } 761 ++ddata->cur; 762 return (cpp_demangle_push_str(ddata, num, 763 ddata->cur - num - 1)); 764 765 default: 766 return (0); 767 } 768 } 769 770 static int 771 cpp_demangle_read_expression(struct cpp_demangle_data *ddata) 772 { 773 774 if (ddata == NULL || *ddata->cur == '\0') 775 return (0); 776 777 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 778 case SIMPLE_HASH('s', 't'): 779 ddata->cur += 2; 780 return (cpp_demangle_read_type(ddata, 0)); 781 782 case SIMPLE_HASH('s', 'r'): 783 ddata->cur += 2; 784 if (!cpp_demangle_read_type(ddata, 0)) 785 return (0); 786 if (!cpp_demangle_read_uqname(ddata)) 787 return (0); 788 if (*ddata->cur == 'I') 789 return (cpp_demangle_read_tmpl_args(ddata)); 790 return (1); 791 792 case SIMPLE_HASH('a', 'a'): 793 /* operator && */ 794 ddata->cur += 2; 795 return (cpp_demangle_read_expression_binary(ddata, "&&", 2)); 796 797 case SIMPLE_HASH('a', 'd'): 798 /* operator & (unary) */ 799 ddata->cur += 2; 800 return (cpp_demangle_read_expression_unary(ddata, "&", 1)); 801 802 case SIMPLE_HASH('a', 'n'): 803 /* operator & */ 804 ddata->cur += 2; 805 return (cpp_demangle_read_expression_binary(ddata, "&", 1)); 806 807 case SIMPLE_HASH('a', 'N'): 808 /* operator &= */ 809 ddata->cur += 2; 810 return (cpp_demangle_read_expression_binary(ddata, "&=", 2)); 811 812 case SIMPLE_HASH('a', 'S'): 813 /* operator = */ 814 ddata->cur += 2; 815 return (cpp_demangle_read_expression_binary(ddata, "=", 1)); 816 817 case SIMPLE_HASH('c', 'l'): 818 /* operator () */ 819 ddata->cur += 2; 820 return (cpp_demangle_read_expression_binary(ddata, "()", 2)); 821 822 case SIMPLE_HASH('c', 'm'): 823 /* operator , */ 824 ddata->cur += 2; 825 return (cpp_demangle_read_expression_binary(ddata, ",", 1)); 826 827 case SIMPLE_HASH('c', 'o'): 828 /* operator ~ */ 829 ddata->cur += 2; 830 return (cpp_demangle_read_expression_binary(ddata, "~", 1)); 831 832 case SIMPLE_HASH('c', 'v'): 833 /* operator (cast) */ 834 ddata->cur += 2; 835 return (cpp_demangle_read_expression_binary(ddata, "(cast)", 6)); 836 837 case SIMPLE_HASH('d', 'a'): 838 /* operator delete [] */ 839 ddata->cur += 2; 840 return (cpp_demangle_read_expression_unary(ddata, "delete []", 9)); 841 842 case SIMPLE_HASH('d', 'e'): 843 /* operator * (unary) */ 844 ddata->cur += 2; 845 return (cpp_demangle_read_expression_unary(ddata, "*", 1)); 846 847 case SIMPLE_HASH('d', 'l'): 848 /* operator delete */ 849 ddata->cur += 2; 850 return (cpp_demangle_read_expression_unary(ddata, "delete", 6)); 851 852 case SIMPLE_HASH('d', 'v'): 853 /* operator / */ 854 ddata->cur += 2; 855 return (cpp_demangle_read_expression_binary(ddata, "/", 1)); 856 857 case SIMPLE_HASH('d', 'V'): 858 /* operator /= */ 859 ddata->cur += 2; 860 return (cpp_demangle_read_expression_binary(ddata, "/=", 2)); 861 862 case SIMPLE_HASH('e', 'o'): 863 /* operator ^ */ 864 ddata->cur += 2; 865 return (cpp_demangle_read_expression_binary(ddata, "^", 1)); 866 867 case SIMPLE_HASH('e', 'O'): 868 /* operator ^= */ 869 ddata->cur += 2; 870 return (cpp_demangle_read_expression_binary(ddata, "^=", 2)); 871 872 case SIMPLE_HASH('e', 'q'): 873 /* operator == */ 874 ddata->cur += 2; 875 return (cpp_demangle_read_expression_binary(ddata, "==", 2)); 876 877 case SIMPLE_HASH('g', 'e'): 878 /* operator >= */ 879 ddata->cur += 2; 880 return (cpp_demangle_read_expression_binary(ddata, ">=", 2)); 881 882 case SIMPLE_HASH('g', 't'): 883 /* operator > */ 884 ddata->cur += 2; 885 return (cpp_demangle_read_expression_binary(ddata, ">", 1)); 886 887 case SIMPLE_HASH('i', 'x'): 888 /* operator [] */ 889 ddata->cur += 2; 890 return (cpp_demangle_read_expression_binary(ddata, "[]", 2)); 891 892 case SIMPLE_HASH('l', 'e'): 893 /* operator <= */ 894 ddata->cur += 2; 895 return (cpp_demangle_read_expression_binary(ddata, "<=", 2)); 896 897 case SIMPLE_HASH('l', 's'): 898 /* operator << */ 899 ddata->cur += 2; 900 return (cpp_demangle_read_expression_binary(ddata, "<<", 2)); 901 902 case SIMPLE_HASH('l', 'S'): 903 /* operator <<= */ 904 ddata->cur += 2; 905 return (cpp_demangle_read_expression_binary(ddata, "<<=", 3)); 906 907 case SIMPLE_HASH('l', 't'): 908 /* operator < */ 909 ddata->cur += 2; 910 return (cpp_demangle_read_expression_binary(ddata, "<", 1)); 911 912 case SIMPLE_HASH('m', 'i'): 913 /* operator - */ 914 ddata->cur += 2; 915 return (cpp_demangle_read_expression_binary(ddata, "-", 1)); 916 917 case SIMPLE_HASH('m', 'I'): 918 /* operator -= */ 919 ddata->cur += 2; 920 return (cpp_demangle_read_expression_binary(ddata, "-=", 2)); 921 922 case SIMPLE_HASH('m', 'l'): 923 /* operator * */ 924 ddata->cur += 2; 925 return (cpp_demangle_read_expression_binary(ddata, "*", 1)); 926 927 case SIMPLE_HASH('m', 'L'): 928 /* operator *= */ 929 ddata->cur += 2; 930 return (cpp_demangle_read_expression_binary(ddata, "*=", 2)); 931 932 case SIMPLE_HASH('m', 'm'): 933 /* operator -- */ 934 ddata->cur += 2; 935 return (cpp_demangle_read_expression_binary(ddata, "--", 2)); 936 937 case SIMPLE_HASH('n', 'a'): 938 /* operator new[] */ 939 ddata->cur += 2; 940 return (cpp_demangle_read_expression_unary(ddata, "new []", 6)); 941 942 case SIMPLE_HASH('n', 'e'): 943 /* operator != */ 944 ddata->cur += 2; 945 return (cpp_demangle_read_expression_binary(ddata, "!=", 2)); 946 947 case SIMPLE_HASH('n', 'g'): 948 /* operator - (unary) */ 949 ddata->cur += 2; 950 return (cpp_demangle_read_expression_unary(ddata, "-", 1)); 951 952 case SIMPLE_HASH('n', 't'): 953 /* operator ! */ 954 ddata->cur += 2; 955 return (cpp_demangle_read_expression_binary(ddata, "!", 1)); 956 957 case SIMPLE_HASH('n', 'w'): 958 /* operator new */ 959 ddata->cur += 2; 960 return (cpp_demangle_read_expression_unary(ddata, "new", 3)); 961 962 case SIMPLE_HASH('o', 'o'): 963 /* operator || */ 964 ddata->cur += 2; 965 return (cpp_demangle_read_expression_binary(ddata, "||", 2)); 966 967 case SIMPLE_HASH('o', 'r'): 968 /* operator | */ 969 ddata->cur += 2; 970 return (cpp_demangle_read_expression_binary(ddata, "|", 1)); 971 972 case SIMPLE_HASH('o', 'R'): 973 /* operator |= */ 974 ddata->cur += 2; 975 return (cpp_demangle_read_expression_binary(ddata, "|=", 2)); 976 977 case SIMPLE_HASH('p', 'l'): 978 /* operator + */ 979 ddata->cur += 2; 980 return (cpp_demangle_read_expression_binary(ddata, "+", 1)); 981 982 case SIMPLE_HASH('p', 'L'): 983 /* operator += */ 984 ddata->cur += 2; 985 return (cpp_demangle_read_expression_binary(ddata, "+=", 2)); 986 987 case SIMPLE_HASH('p', 'm'): 988 /* operator ->* */ 989 ddata->cur += 2; 990 return (cpp_demangle_read_expression_binary(ddata, "->*", 3)); 991 992 case SIMPLE_HASH('p', 'p'): 993 /* operator ++ */ 994 ddata->cur += 2; 995 return (cpp_demangle_read_expression_binary(ddata, "++", 2)); 996 997 case SIMPLE_HASH('p', 's'): 998 /* operator + (unary) */ 999 ddata->cur += 2; 1000 return (cpp_demangle_read_expression_unary(ddata, "+", 1)); 1001 1002 case SIMPLE_HASH('p', 't'): 1003 /* operator -> */ 1004 ddata->cur += 2; 1005 return (cpp_demangle_read_expression_binary(ddata, "->", 2)); 1006 1007 case SIMPLE_HASH('q', 'u'): 1008 /* operator ? */ 1009 ddata->cur += 2; 1010 return (cpp_demangle_read_expression_trinary(ddata, "?", 1, 1011 ":", 1)); 1012 1013 case SIMPLE_HASH('r', 'm'): 1014 /* operator % */ 1015 ddata->cur += 2; 1016 return (cpp_demangle_read_expression_binary(ddata, "%", 1)); 1017 1018 case SIMPLE_HASH('r', 'M'): 1019 /* operator %= */ 1020 ddata->cur += 2; 1021 return (cpp_demangle_read_expression_binary(ddata, "%=", 2)); 1022 1023 case SIMPLE_HASH('r', 's'): 1024 /* operator >> */ 1025 ddata->cur += 2; 1026 return (cpp_demangle_read_expression_binary(ddata, ">>", 2)); 1027 1028 case SIMPLE_HASH('r', 'S'): 1029 /* operator >>= */ 1030 ddata->cur += 2; 1031 return (cpp_demangle_read_expression_binary(ddata, ">>=", 3)); 1032 1033 case SIMPLE_HASH('r', 'z'): 1034 /* operator sizeof */ 1035 ddata->cur += 2; 1036 return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6)); 1037 1038 case SIMPLE_HASH('s', 'v'): 1039 /* operator sizeof */ 1040 ddata->cur += 2; 1041 return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6)); 1042 } 1043 1044 switch (*ddata->cur) { 1045 case 'L': 1046 return (cpp_demangle_read_expr_primary(ddata)); 1047 case 'T': 1048 return (cpp_demangle_read_tmpl_param(ddata)); 1049 } 1050 1051 return (0); 1052 } 1053 1054 static int 1055 cpp_demangle_read_expression_flat(struct cpp_demangle_data *ddata, char **str) 1056 { 1057 struct vector_str *output; 1058 size_t i, p_idx, idx, exp_len; 1059 char *exp; 1060 1061 output = ddata->push_head > 0 ? &ddata->output_tmp : 1062 &ddata->output; 1063 1064 p_idx = output->size; 1065 1066 if (!cpp_demangle_read_expression(ddata)) 1067 return (0); 1068 1069 if ((exp = vector_str_substr(output, p_idx, output->size - 1, 1070 &exp_len)) == NULL) 1071 return (0); 1072 1073 idx = output->size; 1074 for (i = p_idx; i < idx; ++i) { 1075 if (!vector_str_pop(output)) { 1076 free(exp); 1077 return (0); 1078 } 1079 } 1080 1081 *str = exp; 1082 1083 return (1); 1084 } 1085 1086 static int 1087 cpp_demangle_read_expression_binary(struct cpp_demangle_data *ddata, 1088 const char *name, size_t len) 1089 { 1090 1091 if (ddata == NULL || name == NULL || len == 0) 1092 return (0); 1093 if (!cpp_demangle_read_expression(ddata)) 1094 return (0); 1095 if (!cpp_demangle_push_str(ddata, name, len)) 1096 return (0); 1097 1098 return (cpp_demangle_read_expression(ddata)); 1099 } 1100 1101 static int 1102 cpp_demangle_read_expression_unary(struct cpp_demangle_data *ddata, 1103 const char *name, size_t len) 1104 { 1105 1106 if (ddata == NULL || name == NULL || len == 0) 1107 return (0); 1108 if (!cpp_demangle_read_expression(ddata)) 1109 return (0); 1110 1111 return (cpp_demangle_push_str(ddata, name, len)); 1112 } 1113 1114 static int 1115 cpp_demangle_read_expression_trinary(struct cpp_demangle_data *ddata, 1116 const char *name1, size_t len1, const char *name2, size_t len2) 1117 { 1118 1119 if (ddata == NULL || name1 == NULL || len1 == 0 || name2 == NULL || 1120 len2 == 0) 1121 return (0); 1122 1123 if (!cpp_demangle_read_expression(ddata)) 1124 return (0); 1125 if (!cpp_demangle_push_str(ddata, name1, len1)) 1126 return (0); 1127 if (!cpp_demangle_read_expression(ddata)) 1128 return (0); 1129 if (!cpp_demangle_push_str(ddata, name2, len2)) 1130 return (0); 1131 1132 return (cpp_demangle_read_expression(ddata)); 1133 } 1134 1135 static int 1136 cpp_demangle_read_function(struct cpp_demangle_data *ddata, int *ext_c, 1137 struct vector_type_qualifier *v) 1138 { 1139 size_t class_type_size, class_type_len, limit; 1140 const char *class_type; 1141 1142 if (ddata == NULL || *ddata->cur != 'F' || v == NULL) 1143 return (0); 1144 1145 ++ddata->cur; 1146 if (*ddata->cur == 'Y') { 1147 if (ext_c != NULL) 1148 *ext_c = 1; 1149 ++ddata->cur; 1150 } 1151 if (!cpp_demangle_read_type(ddata, 0)) 1152 return (0); 1153 if (*ddata->cur != 'E') { 1154 if (!DEM_PUSH_STR(ddata, "(")) 1155 return (0); 1156 if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM)) { 1157 if ((class_type_size = ddata->class_type.size) == 0) 1158 return (0); 1159 class_type = 1160 ddata->class_type.container[class_type_size - 1]; 1161 if (class_type == NULL) 1162 return (0); 1163 if ((class_type_len = strlen(class_type)) == 0) 1164 return (0); 1165 if (!cpp_demangle_push_str(ddata, class_type, 1166 class_type_len)) 1167 return (0); 1168 if (!DEM_PUSH_STR(ddata, "::*")) 1169 return (0); 1170 ++ddata->func_type; 1171 } else { 1172 if (!cpp_demangle_push_type_qualifier(ddata, v, 1173 (const char *) NULL)) 1174 return (0); 1175 vector_type_qualifier_dest(v); 1176 if (!vector_type_qualifier_init(v)) 1177 return (0); 1178 } 1179 1180 if (!DEM_PUSH_STR(ddata, ")(")) 1181 return (0); 1182 1183 limit = 0; 1184 for (;;) { 1185 if (!cpp_demangle_read_type(ddata, 0)) 1186 return (0); 1187 if (*ddata->cur == 'E') 1188 break; 1189 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1190 return (0); 1191 } 1192 1193 if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM) == 1) { 1194 if (!cpp_demangle_push_type_qualifier(ddata, v, 1195 (const char *) NULL)) 1196 return (0); 1197 vector_type_qualifier_dest(v); 1198 if (!vector_type_qualifier_init(v)) 1199 return (0); 1200 } 1201 1202 if (!DEM_PUSH_STR(ddata, ")")) 1203 return (0); 1204 } 1205 1206 ++ddata->cur; 1207 1208 return (1); 1209 } 1210 1211 /* read encoding, encoding are function name, data name, special-name */ 1212 static int 1213 cpp_demangle_read_encoding(struct cpp_demangle_data *ddata) 1214 { 1215 char *name, *type, *num_str; 1216 long offset; 1217 int rtn; 1218 1219 if (ddata == NULL || *ddata->cur == '\0') 1220 return (0); 1221 1222 /* special name */ 1223 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 1224 case SIMPLE_HASH('G', 'A'): 1225 if (!DEM_PUSH_STR(ddata, "hidden alias for ")) 1226 return (0); 1227 ddata->cur += 2; 1228 if (*ddata->cur == '\0') 1229 return (0); 1230 return (cpp_demangle_read_encoding(ddata)); 1231 1232 case SIMPLE_HASH('G', 'R'): 1233 if (!DEM_PUSH_STR(ddata, "reference temporary #")) 1234 return (0); 1235 ddata->cur += 2; 1236 if (*ddata->cur == '\0') 1237 return (0); 1238 if (!cpp_demangle_read_name_flat(ddata, &name)) 1239 return (0); 1240 rtn = 0; 1241 if (!cpp_demangle_read_number_as_string(ddata, &num_str)) 1242 goto clean1; 1243 if (!DEM_PUSH_STR(ddata, num_str)) 1244 goto clean2; 1245 if (!DEM_PUSH_STR(ddata, " for ")) 1246 goto clean2; 1247 if (!DEM_PUSH_STR(ddata, name)) 1248 goto clean2; 1249 rtn = 1; 1250 clean2: 1251 free(num_str); 1252 clean1: 1253 free(name); 1254 return (rtn); 1255 1256 case SIMPLE_HASH('G', 'T'): 1257 ddata->cur += 2; 1258 if (*ddata->cur == '\0') 1259 return (0); 1260 switch (*ddata->cur) { 1261 case 'n': 1262 if (!DEM_PUSH_STR(ddata, "non-transaction clone for ")) 1263 return (0); 1264 break; 1265 case 't': 1266 default: 1267 if (!DEM_PUSH_STR(ddata, "transaction clone for ")) 1268 return (0); 1269 break; 1270 } 1271 ++ddata->cur; 1272 return (cpp_demangle_read_encoding(ddata)); 1273 1274 case SIMPLE_HASH('G', 'V'): 1275 /* sentry object for 1 time init */ 1276 if (!DEM_PUSH_STR(ddata, "guard variable for ")) 1277 return (0); 1278 ddata->cur += 2; 1279 break; 1280 1281 case SIMPLE_HASH('T', 'c'): 1282 /* virtual function covariant override thunk */ 1283 if (!DEM_PUSH_STR(ddata, 1284 "virtual function covariant override ")) 1285 return (0); 1286 ddata->cur += 2; 1287 if (*ddata->cur == '\0') 1288 return (0); 1289 if (!cpp_demangle_read_offset(ddata)) 1290 return (0); 1291 if (!cpp_demangle_read_offset(ddata)) 1292 return (0); 1293 return (cpp_demangle_read_encoding(ddata)); 1294 1295 case SIMPLE_HASH('T', 'C'): 1296 /* construction vtable */ 1297 if (!DEM_PUSH_STR(ddata, "construction vtable for ")) 1298 return (0); 1299 ddata->cur += 2; 1300 if (*ddata->cur == '\0') 1301 return (0); 1302 if (!cpp_demangle_read_type_flat(ddata, &type)) 1303 return (0); 1304 rtn = 0; 1305 if (!cpp_demangle_read_number(ddata, &offset)) 1306 goto clean3; 1307 if (*ddata->cur++ != '_') 1308 goto clean3; 1309 if (!cpp_demangle_read_type(ddata, 0)) 1310 goto clean3; 1311 if (!DEM_PUSH_STR(ddata, "-in-")) 1312 goto clean3; 1313 if (!DEM_PUSH_STR(ddata, type)) 1314 goto clean3; 1315 rtn = 1; 1316 clean3: 1317 free(type); 1318 return (rtn); 1319 1320 case SIMPLE_HASH('T', 'D'): 1321 /* typeinfo common proxy */ 1322 break; 1323 1324 case SIMPLE_HASH('T', 'F'): 1325 /* typeinfo fn */ 1326 if (!DEM_PUSH_STR(ddata, "typeinfo fn for ")) 1327 return (0); 1328 ddata->cur += 2; 1329 if (*ddata->cur == '\0') 1330 return (0); 1331 return (cpp_demangle_read_type(ddata, 0)); 1332 1333 case SIMPLE_HASH('T', 'h'): 1334 /* virtual function non-virtual override thunk */ 1335 if (!DEM_PUSH_STR(ddata, 1336 "virtual function non-virtual override ")) 1337 return (0); 1338 ddata->cur += 2; 1339 if (*ddata->cur == '\0') 1340 return (0); 1341 if (!cpp_demangle_read_nv_offset(ddata)) 1342 return (0); 1343 return (cpp_demangle_read_encoding(ddata)); 1344 1345 case SIMPLE_HASH('T', 'H'): 1346 /* TLS init function */ 1347 if (!DEM_PUSH_STR(ddata, "TLS init function for ")) 1348 return (0); 1349 ddata->cur += 2; 1350 if (*ddata->cur == '\0') 1351 return (0); 1352 break; 1353 1354 case SIMPLE_HASH('T', 'I'): 1355 /* typeinfo structure */ 1356 if (!DEM_PUSH_STR(ddata, "typeinfo for ")) 1357 return (0); 1358 ddata->cur += 2; 1359 if (*ddata->cur == '\0') 1360 return (0); 1361 return (cpp_demangle_read_type(ddata, 0)); 1362 1363 case SIMPLE_HASH('T', 'J'): 1364 /* java class */ 1365 if (!DEM_PUSH_STR(ddata, "java Class for ")) 1366 return (0); 1367 ddata->cur += 2; 1368 if (*ddata->cur == '\0') 1369 return (0); 1370 return (cpp_demangle_read_type(ddata, 0)); 1371 1372 case SIMPLE_HASH('T', 'S'): 1373 /* RTTI name (NTBS) */ 1374 if (!DEM_PUSH_STR(ddata, "typeinfo name for ")) 1375 return (0); 1376 ddata->cur += 2; 1377 if (*ddata->cur == '\0') 1378 return (0); 1379 return (cpp_demangle_read_type(ddata, 0)); 1380 1381 case SIMPLE_HASH('T', 'T'): 1382 /* VTT table */ 1383 if (!DEM_PUSH_STR(ddata, "VTT for ")) 1384 return (0); 1385 ddata->cur += 2; 1386 if (*ddata->cur == '\0') 1387 return (0); 1388 return (cpp_demangle_read_type(ddata, 0)); 1389 1390 case SIMPLE_HASH('T', 'v'): 1391 /* virtual function virtual override thunk */ 1392 if (!DEM_PUSH_STR(ddata, 1393 "virtual function virtual override ")) 1394 return (0); 1395 ddata->cur += 2; 1396 if (*ddata->cur == '\0') 1397 return (0); 1398 if (!cpp_demangle_read_v_offset(ddata)) 1399 return (0); 1400 return (cpp_demangle_read_encoding(ddata)); 1401 1402 case SIMPLE_HASH('T', 'V'): 1403 /* virtual table */ 1404 if (!DEM_PUSH_STR(ddata, "vtable for ")) 1405 return (0); 1406 ddata->cur += 2; 1407 if (*ddata->cur == '\0') 1408 return (0); 1409 return (cpp_demangle_read_type(ddata, 0)); 1410 1411 case SIMPLE_HASH('T', 'W'): 1412 /* TLS wrapper function */ 1413 if (!DEM_PUSH_STR(ddata, "TLS wrapper function for ")) 1414 return (0); 1415 ddata->cur += 2; 1416 if (*ddata->cur == '\0') 1417 return (0); 1418 break; 1419 } 1420 1421 return (cpp_demangle_read_name(ddata)); 1422 } 1423 1424 static int 1425 cpp_demangle_read_local_name(struct cpp_demangle_data *ddata) 1426 { 1427 size_t limit; 1428 1429 if (ddata == NULL) 1430 return (0); 1431 if (*(++ddata->cur) == '\0') 1432 return (0); 1433 if (!cpp_demangle_read_encoding(ddata)) 1434 return (0); 1435 1436 limit = 0; 1437 for (;;) { 1438 if (!cpp_demangle_read_type(ddata, 1)) 1439 return (0); 1440 if (*ddata->cur == 'E') 1441 break; 1442 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1443 return (0); 1444 } 1445 if (*(++ddata->cur) == '\0') 1446 return (0); 1447 if (ddata->paren == true) { 1448 if (!DEM_PUSH_STR(ddata, ")")) 1449 return (0); 1450 ddata->paren = false; 1451 } 1452 if (*ddata->cur == 's') 1453 ++ddata->cur; 1454 else { 1455 if (!DEM_PUSH_STR(ddata, "::")) 1456 return (0); 1457 if (!cpp_demangle_read_name(ddata)) 1458 return (0); 1459 } 1460 if (*ddata->cur == '_') { 1461 ++ddata->cur; 1462 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 1463 ++ddata->cur; 1464 } 1465 1466 return (1); 1467 } 1468 1469 static int 1470 cpp_demangle_read_name(struct cpp_demangle_data *ddata) 1471 { 1472 struct vector_str *output, v; 1473 size_t p_idx, subst_str_len; 1474 int rtn; 1475 char *subst_str; 1476 1477 if (ddata == NULL || *ddata->cur == '\0') 1478 return (0); 1479 1480 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 1481 1482 subst_str = NULL; 1483 1484 switch (*ddata->cur) { 1485 case 'S': 1486 return (cpp_demangle_read_subst(ddata)); 1487 case 'N': 1488 return (cpp_demangle_read_nested_name(ddata)); 1489 case 'Z': 1490 return (cpp_demangle_read_local_name(ddata)); 1491 } 1492 1493 if (!vector_str_init(&v)) 1494 return (0); 1495 1496 p_idx = output->size; 1497 rtn = 0; 1498 if (!cpp_demangle_read_uqname(ddata)) 1499 goto clean; 1500 if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, 1501 &subst_str_len)) == NULL) 1502 goto clean; 1503 if (subst_str_len > 8 && strstr(subst_str, "operator") != NULL) { 1504 rtn = 1; 1505 goto clean; 1506 } 1507 if (!vector_str_push(&v, subst_str, subst_str_len)) 1508 goto clean; 1509 if (!cpp_demangle_push_subst_v(ddata, &v)) 1510 goto clean; 1511 1512 if (*ddata->cur == 'I') { 1513 p_idx = output->size; 1514 if (!cpp_demangle_read_tmpl_args(ddata)) 1515 goto clean; 1516 free(subst_str); 1517 if ((subst_str = vector_str_substr(output, p_idx, 1518 output->size - 1, &subst_str_len)) == NULL) 1519 goto clean; 1520 if (!vector_str_push(&v, subst_str, subst_str_len)) 1521 goto clean; 1522 if (!cpp_demangle_push_subst_v(ddata, &v)) 1523 goto clean; 1524 } 1525 1526 rtn = 1; 1527 1528 clean: 1529 free(subst_str); 1530 vector_str_dest(&v); 1531 1532 return (rtn); 1533 } 1534 1535 static int 1536 cpp_demangle_read_name_flat(struct cpp_demangle_data *ddata, char **str) 1537 { 1538 struct vector_str *output; 1539 size_t i, p_idx, idx, name_len; 1540 char *name; 1541 1542 output = ddata->push_head > 0 ? &ddata->output_tmp : 1543 &ddata->output; 1544 1545 p_idx = output->size; 1546 1547 if (!cpp_demangle_read_name(ddata)) 1548 return (0); 1549 1550 if ((name = vector_str_substr(output, p_idx, output->size - 1, 1551 &name_len)) == NULL) 1552 return (0); 1553 1554 idx = output->size; 1555 for (i = p_idx; i < idx; ++i) { 1556 if (!vector_str_pop(output)) { 1557 free(name); 1558 return (0); 1559 } 1560 } 1561 1562 *str = name; 1563 1564 return (1); 1565 } 1566 1567 static int 1568 cpp_demangle_read_nested_name(struct cpp_demangle_data *ddata) 1569 { 1570 struct vector_str *output, v; 1571 size_t limit, p_idx, subst_str_len; 1572 int rtn; 1573 char *subst_str; 1574 1575 if (ddata == NULL || *ddata->cur != 'N') 1576 return (0); 1577 if (*(++ddata->cur) == '\0') 1578 return (0); 1579 1580 while (*ddata->cur == 'r' || *ddata->cur == 'V' || 1581 *ddata->cur == 'K') { 1582 switch (*ddata->cur) { 1583 case 'r': 1584 ddata->mem_rst = true; 1585 break; 1586 case 'V': 1587 ddata->mem_vat = true; 1588 break; 1589 case 'K': 1590 ddata->mem_cst = true; 1591 break; 1592 } 1593 ++ddata->cur; 1594 } 1595 1596 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 1597 if (!vector_str_init(&v)) 1598 return (0); 1599 1600 rtn = 0; 1601 limit = 0; 1602 for (;;) { 1603 p_idx = output->size; 1604 switch (*ddata->cur) { 1605 case 'I': 1606 if (!cpp_demangle_read_tmpl_args(ddata)) 1607 goto clean; 1608 break; 1609 case 'S': 1610 if (!cpp_demangle_read_subst(ddata)) 1611 goto clean; 1612 break; 1613 case 'T': 1614 if (!cpp_demangle_read_tmpl_param(ddata)) 1615 goto clean; 1616 break; 1617 default: 1618 if (!cpp_demangle_read_uqname(ddata)) 1619 goto clean; 1620 } 1621 1622 if ((subst_str = vector_str_substr(output, p_idx, 1623 output->size - 1, &subst_str_len)) == NULL) 1624 goto clean; 1625 if (!vector_str_push(&v, subst_str, subst_str_len)) { 1626 free(subst_str); 1627 goto clean; 1628 } 1629 free(subst_str); 1630 1631 if (!cpp_demangle_push_subst_v(ddata, &v)) 1632 goto clean; 1633 if (*ddata->cur == 'E') 1634 break; 1635 else if (*ddata->cur != 'I' && 1636 *ddata->cur != 'C' && *ddata->cur != 'D') { 1637 if (!DEM_PUSH_STR(ddata, "::")) 1638 goto clean; 1639 if (!VEC_PUSH_STR(&v, "::")) 1640 goto clean; 1641 } 1642 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 1643 goto clean; 1644 } 1645 1646 ++ddata->cur; 1647 rtn = 1; 1648 1649 clean: 1650 vector_str_dest(&v); 1651 1652 return (rtn); 1653 } 1654 1655 /* 1656 * read number 1657 * number ::= [n] <decimal> 1658 */ 1659 static int 1660 cpp_demangle_read_number(struct cpp_demangle_data *ddata, long *rtn) 1661 { 1662 long len, negative_factor; 1663 1664 if (ddata == NULL || rtn == NULL) 1665 return (0); 1666 1667 negative_factor = 1; 1668 if (*ddata->cur == 'n') { 1669 negative_factor = -1; 1670 1671 ++ddata->cur; 1672 } 1673 if (ELFTC_ISDIGIT(*ddata->cur) == 0) 1674 return (0); 1675 1676 errno = 0; 1677 if ((len = strtol(ddata->cur, (char **) NULL, 10)) == 0 && 1678 errno != 0) 1679 return (0); 1680 1681 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 1682 ++ddata->cur; 1683 1684 assert(len >= 0); 1685 assert(negative_factor == 1 || negative_factor == -1); 1686 1687 *rtn = len * negative_factor; 1688 1689 return (1); 1690 } 1691 1692 static int 1693 cpp_demangle_read_number_as_string(struct cpp_demangle_data *ddata, char **str) 1694 { 1695 long n; 1696 1697 if (!cpp_demangle_read_number(ddata, &n)) { 1698 *str = NULL; 1699 return (0); 1700 } 1701 1702 if (asprintf(str, "%ld", n) < 0) { 1703 *str = NULL; 1704 return (0); 1705 } 1706 1707 return (1); 1708 } 1709 1710 static int 1711 cpp_demangle_read_nv_offset(struct cpp_demangle_data *ddata) 1712 { 1713 1714 if (ddata == NULL) 1715 return (0); 1716 1717 if (!DEM_PUSH_STR(ddata, "offset : ")) 1718 return (0); 1719 1720 return (cpp_demangle_read_offset_number(ddata)); 1721 } 1722 1723 /* read offset, offset are nv-offset, v-offset */ 1724 static int 1725 cpp_demangle_read_offset(struct cpp_demangle_data *ddata) 1726 { 1727 1728 if (ddata == NULL) 1729 return (0); 1730 1731 if (*ddata->cur == 'h') { 1732 ++ddata->cur; 1733 return (cpp_demangle_read_nv_offset(ddata)); 1734 } else if (*ddata->cur == 'v') { 1735 ++ddata->cur; 1736 return (cpp_demangle_read_v_offset(ddata)); 1737 } 1738 1739 return (0); 1740 } 1741 1742 static int 1743 cpp_demangle_read_offset_number(struct cpp_demangle_data *ddata) 1744 { 1745 bool negative; 1746 const char *start; 1747 1748 if (ddata == NULL || *ddata->cur == '\0') 1749 return (0); 1750 1751 /* offset could be negative */ 1752 if (*ddata->cur == 'n') { 1753 negative = true; 1754 start = ddata->cur + 1; 1755 } else { 1756 negative = false; 1757 start = ddata->cur; 1758 } 1759 1760 while (*ddata->cur != '_') 1761 ++ddata->cur; 1762 1763 if (negative && !DEM_PUSH_STR(ddata, "-")) 1764 return (0); 1765 1766 assert(start != NULL); 1767 1768 if (!cpp_demangle_push_str(ddata, start, ddata->cur - start)) 1769 return (0); 1770 if (!DEM_PUSH_STR(ddata, " ")) 1771 return (0); 1772 1773 ++ddata->cur; 1774 1775 return (1); 1776 } 1777 1778 static int 1779 cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *ddata) 1780 { 1781 size_t class_type_len, i, idx, p_idx; 1782 int p_func_type, rtn; 1783 char *class_type; 1784 1785 if (ddata == NULL || *ddata->cur != 'M' || *(++ddata->cur) == '\0') 1786 return (0); 1787 1788 p_idx = ddata->output.size; 1789 if (!cpp_demangle_read_type(ddata, 0)) 1790 return (0); 1791 1792 if ((class_type = vector_str_substr(&ddata->output, p_idx, 1793 ddata->output.size - 1, &class_type_len)) == NULL) 1794 return (0); 1795 1796 rtn = 0; 1797 idx = ddata->output.size; 1798 for (i = p_idx; i < idx; ++i) 1799 if (!vector_str_pop(&ddata->output)) 1800 goto clean1; 1801 1802 if (!vector_read_cmd_push(&ddata->cmd, READ_PTRMEM)) 1803 goto clean1; 1804 1805 if (!vector_str_push(&ddata->class_type, class_type, class_type_len)) 1806 goto clean2; 1807 1808 p_func_type = ddata->func_type; 1809 if (!cpp_demangle_read_type(ddata, 0)) 1810 goto clean3; 1811 1812 if (p_func_type == ddata->func_type) { 1813 if (!DEM_PUSH_STR(ddata, " ")) 1814 goto clean3; 1815 if (!cpp_demangle_push_str(ddata, class_type, class_type_len)) 1816 goto clean3; 1817 if (!DEM_PUSH_STR(ddata, "::*")) 1818 goto clean3; 1819 } 1820 1821 rtn = 1; 1822 clean3: 1823 if (!vector_str_pop(&ddata->class_type)) 1824 rtn = 0; 1825 clean2: 1826 if (!vector_read_cmd_pop(&ddata->cmd)) 1827 rtn = 0; 1828 clean1: 1829 free(class_type); 1830 1831 return (rtn); 1832 } 1833 1834 /* read source-name, source-name is <len> <ID> */ 1835 static int 1836 cpp_demangle_read_sname(struct cpp_demangle_data *ddata) 1837 { 1838 long len; 1839 int err; 1840 1841 if (ddata == NULL || cpp_demangle_read_number(ddata, &len) == 0 || 1842 len <= 0) 1843 return (0); 1844 1845 if (len == 12 && (memcmp("_GLOBAL__N_1", ddata->cur, 12) == 0)) 1846 err = DEM_PUSH_STR(ddata, "(anonymous namespace)"); 1847 else 1848 err = cpp_demangle_push_str(ddata, ddata->cur, len); 1849 1850 if (err == 0) 1851 return (0); 1852 1853 assert(ddata->output.size > 0); 1854 if (vector_read_cmd_find(&ddata->cmd, READ_TMPL) == 0) 1855 ddata->last_sname = 1856 ddata->output.container[ddata->output.size - 1]; 1857 1858 ddata->cur += len; 1859 1860 return (1); 1861 } 1862 1863 static int 1864 cpp_demangle_read_subst(struct cpp_demangle_data *ddata) 1865 { 1866 long nth; 1867 1868 if (ddata == NULL || *ddata->cur == '\0') 1869 return (0); 1870 1871 /* abbreviations of the form Sx */ 1872 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 1873 case SIMPLE_HASH('S', 'a'): 1874 /* std::allocator */ 1875 if (!DEM_PUSH_STR(ddata, "std::allocator")) 1876 return (0); 1877 ddata->cur += 2; 1878 if (*ddata->cur == 'I') 1879 return (cpp_demangle_read_subst_stdtmpl(ddata, 1880 "std::allocator", 14)); 1881 return (1); 1882 1883 case SIMPLE_HASH('S', 'b'): 1884 /* std::basic_string */ 1885 if (!DEM_PUSH_STR(ddata, "std::basic_string")) 1886 return (0); 1887 ddata->cur += 2; 1888 if (*ddata->cur == 'I') 1889 return (cpp_demangle_read_subst_stdtmpl(ddata, 1890 "std::basic_string", 17)); 1891 return (1); 1892 1893 case SIMPLE_HASH('S', 'd'): 1894 /* std::basic_iostream<char, std::char_traits<char> > */ 1895 if (!DEM_PUSH_STR(ddata, "std::basic_iostream")) 1896 return (0); 1897 ddata->last_sname = "basic_iostream"; 1898 ddata->cur += 2; 1899 if (*ddata->cur == 'I') 1900 return (cpp_demangle_read_subst_stdtmpl(ddata, 1901 "std::basic_iostream", 19)); 1902 return (1); 1903 1904 case SIMPLE_HASH('S', 'i'): 1905 /* std::basic_istream<char, std::char_traits<char> > */ 1906 if (!DEM_PUSH_STR(ddata, "std::basic_istream")) 1907 return (0); 1908 ddata->last_sname = "basic_istream"; 1909 ddata->cur += 2; 1910 if (*ddata->cur == 'I') 1911 return (cpp_demangle_read_subst_stdtmpl(ddata, 1912 "std::basic_istream", 18)); 1913 return (1); 1914 1915 case SIMPLE_HASH('S', 'o'): 1916 /* std::basic_ostream<char, std::char_traits<char> > */ 1917 if (!DEM_PUSH_STR(ddata, "std::basic_ostream")) 1918 return (0); 1919 ddata->last_sname = "basic_ostream"; 1920 ddata->cur += 2; 1921 if (*ddata->cur == 'I') 1922 return (cpp_demangle_read_subst_stdtmpl(ddata, 1923 "std::basic_ostream", 18)); 1924 return (1); 1925 1926 case SIMPLE_HASH('S', 's'): 1927 /* 1928 * std::basic_string<char, std::char_traits<char>, 1929 * std::allocator<char> > 1930 * 1931 * a.k.a std::string 1932 */ 1933 if (!DEM_PUSH_STR(ddata, "std::string")) 1934 return (0); 1935 ddata->last_sname = "string"; 1936 ddata->cur += 2; 1937 if (*ddata->cur == 'I') 1938 return (cpp_demangle_read_subst_stdtmpl(ddata, 1939 "std::string", 11)); 1940 return (1); 1941 1942 case SIMPLE_HASH('S', 't'): 1943 /* std:: */ 1944 return (cpp_demangle_read_subst_std(ddata)); 1945 } 1946 1947 if (*(++ddata->cur) == '\0') 1948 return (0); 1949 1950 /* substitution */ 1951 if (*ddata->cur == '_') 1952 return (cpp_demangle_get_subst(ddata, 0)); 1953 else { 1954 errno = 0; 1955 /* substitution number is base 36 */ 1956 if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && 1957 errno != 0) 1958 return (0); 1959 1960 /* first was '_', so increase one */ 1961 ++nth; 1962 1963 while (*ddata->cur != '_') 1964 ++ddata->cur; 1965 1966 assert(nth > 0); 1967 1968 return (cpp_demangle_get_subst(ddata, nth)); 1969 } 1970 1971 /* NOTREACHED */ 1972 return (0); 1973 } 1974 1975 static int 1976 cpp_demangle_read_subst_std(struct cpp_demangle_data *ddata) 1977 { 1978 struct vector_str *output, v; 1979 size_t p_idx, subst_str_len; 1980 int rtn; 1981 char *subst_str; 1982 1983 if (ddata == NULL) 1984 return (0); 1985 1986 if (!vector_str_init(&v)) 1987 return (0); 1988 1989 subst_str = NULL; 1990 rtn = 0; 1991 if (!DEM_PUSH_STR(ddata, "std::")) 1992 goto clean; 1993 1994 if (!VEC_PUSH_STR(&v, "std::")) 1995 goto clean; 1996 1997 ddata->cur += 2; 1998 1999 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2000 2001 p_idx = output->size; 2002 if (!cpp_demangle_read_uqname(ddata)) 2003 goto clean; 2004 2005 if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, 2006 &subst_str_len)) == NULL) 2007 goto clean; 2008 2009 if (!vector_str_push(&v, subst_str, subst_str_len)) 2010 goto clean; 2011 2012 if (!cpp_demangle_push_subst_v(ddata, &v)) 2013 goto clean; 2014 2015 if (*ddata->cur == 'I') { 2016 p_idx = output->size; 2017 if (!cpp_demangle_read_tmpl_args(ddata)) 2018 goto clean; 2019 free(subst_str); 2020 if ((subst_str = vector_str_substr(output, p_idx, 2021 output->size - 1, &subst_str_len)) == NULL) 2022 goto clean; 2023 if (!vector_str_push(&v, subst_str, subst_str_len)) 2024 goto clean; 2025 if (!cpp_demangle_push_subst_v(ddata, &v)) 2026 goto clean; 2027 } 2028 2029 rtn = 1; 2030 clean: 2031 free(subst_str); 2032 vector_str_dest(&v); 2033 2034 return (rtn); 2035 } 2036 2037 static int 2038 cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *ddata, 2039 const char *str, size_t len) 2040 { 2041 struct vector_str *output; 2042 size_t p_idx, substr_len; 2043 int rtn; 2044 char *subst_str, *substr; 2045 2046 if (ddata == NULL || str == NULL || len == 0) 2047 return (0); 2048 2049 output = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2050 2051 p_idx = output->size; 2052 substr = NULL; 2053 subst_str = NULL; 2054 2055 if (!cpp_demangle_read_tmpl_args(ddata)) 2056 return (0); 2057 if ((substr = vector_str_substr(output, p_idx, output->size - 1, 2058 &substr_len)) == NULL) 2059 return (0); 2060 2061 rtn = 0; 2062 if ((subst_str = malloc(sizeof(char) * (substr_len + len + 1))) == 2063 NULL) 2064 goto clean; 2065 2066 memcpy(subst_str, str, len); 2067 memcpy(subst_str + len, substr, substr_len); 2068 subst_str[substr_len + len] = '\0'; 2069 2070 if (!cpp_demangle_push_subst(ddata, subst_str, substr_len + len)) 2071 goto clean; 2072 2073 rtn = 1; 2074 clean: 2075 free(subst_str); 2076 free(substr); 2077 2078 return (rtn); 2079 } 2080 2081 static int 2082 cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *ddata) 2083 { 2084 2085 if (ddata == NULL || *ddata->cur == '\0') 2086 return (0); 2087 2088 switch (*ddata->cur) { 2089 case 'L': 2090 return (cpp_demangle_read_expr_primary(ddata)); 2091 case 'X': 2092 return (cpp_demangle_read_expression(ddata)); 2093 } 2094 2095 return (cpp_demangle_read_type(ddata, 0)); 2096 } 2097 2098 static int 2099 cpp_demangle_read_tmpl_args(struct cpp_demangle_data *ddata) 2100 { 2101 struct vector_str *v; 2102 size_t arg_len, idx, limit, size; 2103 char *arg; 2104 2105 if (ddata == NULL || *ddata->cur == '\0') 2106 return (0); 2107 2108 ++ddata->cur; 2109 2110 if (!vector_read_cmd_push(&ddata->cmd, READ_TMPL)) 2111 return (0); 2112 2113 if (!DEM_PUSH_STR(ddata, "<")) 2114 return (0); 2115 2116 limit = 0; 2117 v = ddata->push_head > 0 ? &ddata->output_tmp : &ddata->output; 2118 for (;;) { 2119 idx = v->size; 2120 if (!cpp_demangle_read_tmpl_arg(ddata)) 2121 return (0); 2122 if ((arg = vector_str_substr(v, idx, v->size - 1, &arg_len)) == 2123 NULL) 2124 return (0); 2125 if (!vector_str_find(&ddata->tmpl, arg, arg_len) && 2126 !vector_str_push(&ddata->tmpl, arg, arg_len)) { 2127 free(arg); 2128 return (0); 2129 } 2130 2131 free(arg); 2132 2133 if (*ddata->cur == 'E') { 2134 ++ddata->cur; 2135 size = v->size; 2136 assert(size > 0); 2137 if (!strncmp(v->container[size - 1], ">", 1)) { 2138 if (!DEM_PUSH_STR(ddata, " >")) 2139 return (0); 2140 } else if (!DEM_PUSH_STR(ddata, ">")) 2141 return (0); 2142 break; 2143 } else if (*ddata->cur != 'I' && 2144 !DEM_PUSH_STR(ddata, ", ")) 2145 return (0); 2146 2147 if (limit++ > CPP_DEMANGLE_TRY_LIMIT) 2148 return (0); 2149 } 2150 2151 return (vector_read_cmd_pop(&ddata->cmd)); 2152 } 2153 2154 /* 2155 * Read template parameter that forms in 'T[number]_'. 2156 * This function much like to read_subst but only for types. 2157 */ 2158 static int 2159 cpp_demangle_read_tmpl_param(struct cpp_demangle_data *ddata) 2160 { 2161 long nth; 2162 2163 if (ddata == NULL || *ddata->cur != 'T') 2164 return (0); 2165 2166 ++ddata->cur; 2167 2168 if (*ddata->cur == '_') 2169 return (cpp_demangle_get_tmpl_param(ddata, 0)); 2170 else { 2171 2172 errno = 0; 2173 if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && 2174 errno != 0) 2175 return (0); 2176 2177 /* T_ is first */ 2178 ++nth; 2179 2180 while (*ddata->cur != '_') 2181 ++ddata->cur; 2182 2183 assert(nth > 0); 2184 2185 return (cpp_demangle_get_tmpl_param(ddata, nth)); 2186 } 2187 2188 /* NOTREACHED */ 2189 return (0); 2190 } 2191 2192 static int 2193 cpp_demangle_read_type(struct cpp_demangle_data *ddata, int delimit) 2194 { 2195 struct vector_type_qualifier v; 2196 struct vector_str *output; 2197 size_t p_idx, type_str_len; 2198 int extern_c, is_builtin; 2199 long len; 2200 char *type_str, *exp_str, *num_str; 2201 2202 if (ddata == NULL) 2203 return (0); 2204 2205 output = &ddata->output; 2206 if (!strncmp(ddata->output.container[ddata->output.size - 1], ">", 1)) { 2207 ddata->push_head++; 2208 output = &ddata->output_tmp; 2209 } else if (delimit == 1) { 2210 if (ddata->paren == false) { 2211 if (!DEM_PUSH_STR(ddata, "(")) 2212 return (0); 2213 if (ddata->output.size < 2) 2214 return (0); 2215 ddata->paren = true; 2216 ddata->pfirst = true; 2217 /* Need pop function name */ 2218 if (ddata->subst.size == 1 && 2219 !vector_str_pop(&ddata->subst)) 2220 return (0); 2221 } 2222 2223 if (ddata->pfirst) 2224 ddata->pfirst = false; 2225 else if (*ddata->cur != 'I' && 2226 !DEM_PUSH_STR(ddata, ", ")) 2227 return (0); 2228 } 2229 2230 assert(output != NULL); 2231 /* 2232 * [r, V, K] [P, R, C, G, U] builtin, function, class-enum, array 2233 * pointer-to-member, template-param, template-template-param, subst 2234 */ 2235 2236 if (!vector_type_qualifier_init(&v)) 2237 return (0); 2238 2239 extern_c = 0; 2240 is_builtin = 1; 2241 p_idx = output->size; 2242 type_str = exp_str = num_str = NULL; 2243 again: 2244 /* builtin type */ 2245 switch (*ddata->cur) { 2246 case 'a': 2247 /* signed char */ 2248 if (!DEM_PUSH_STR(ddata, "signed char")) 2249 goto clean; 2250 ++ddata->cur; 2251 goto rtn; 2252 2253 case 'A': 2254 /* array type */ 2255 if (!cpp_demangle_read_array(ddata)) 2256 goto clean; 2257 is_builtin = 0; 2258 goto rtn; 2259 2260 case 'b': 2261 /* bool */ 2262 if (!DEM_PUSH_STR(ddata, "bool")) 2263 goto clean; 2264 ++ddata->cur; 2265 goto rtn; 2266 2267 case 'C': 2268 /* complex pair */ 2269 if (!vector_type_qualifier_push(&v, TYPE_CMX)) 2270 goto clean; 2271 ++ddata->cur; 2272 goto again; 2273 2274 case 'c': 2275 /* char */ 2276 if (!DEM_PUSH_STR(ddata, "char")) 2277 goto clean; 2278 ++ddata->cur; 2279 goto rtn; 2280 2281 case 'd': 2282 /* double */ 2283 if (!DEM_PUSH_STR(ddata, "double")) 2284 goto clean; 2285 ++ddata->cur; 2286 goto rtn; 2287 2288 case 'D': 2289 ++ddata->cur; 2290 switch (*ddata->cur) { 2291 case 'd': 2292 /* IEEE 754r decimal floating point (64 bits) */ 2293 if (!DEM_PUSH_STR(ddata, "decimal64")) 2294 goto clean; 2295 ++ddata->cur; 2296 break; 2297 case 'e': 2298 /* IEEE 754r decimal floating point (128 bits) */ 2299 if (!DEM_PUSH_STR(ddata, "decimal128")) 2300 goto clean; 2301 ++ddata->cur; 2302 break; 2303 case 'f': 2304 /* IEEE 754r decimal floating point (32 bits) */ 2305 if (!DEM_PUSH_STR(ddata, "decimal32")) 2306 goto clean; 2307 ++ddata->cur; 2308 break; 2309 case 'h': 2310 /* IEEE 754r half-precision floating point (16 bits) */ 2311 if (!DEM_PUSH_STR(ddata, "half")) 2312 goto clean; 2313 ++ddata->cur; 2314 break; 2315 case 'i': 2316 /* char32_t */ 2317 if (!DEM_PUSH_STR(ddata, "char32_t")) 2318 goto clean; 2319 ++ddata->cur; 2320 break; 2321 case 'n': 2322 /* std::nullptr_t (i.e., decltype(nullptr)) */ 2323 if (!DEM_PUSH_STR(ddata, "decltype(nullptr)")) 2324 goto clean; 2325 ++ddata->cur; 2326 break; 2327 case 's': 2328 /* char16_t */ 2329 if (!DEM_PUSH_STR(ddata, "char16_t")) 2330 goto clean; 2331 ++ddata->cur; 2332 break; 2333 case 'v': 2334 /* gcc vector_size extension. */ 2335 ++ddata->cur; 2336 if (*ddata->cur == '_') { 2337 ++ddata->cur; 2338 if (!cpp_demangle_read_expression_flat(ddata, 2339 &exp_str)) 2340 goto clean; 2341 if (!VEC_PUSH_STR(&v.ext_name, exp_str)) 2342 goto clean; 2343 } else { 2344 if (!cpp_demangle_read_number_as_string(ddata, 2345 &num_str)) 2346 goto clean; 2347 if (!VEC_PUSH_STR(&v.ext_name, num_str)) 2348 goto clean; 2349 } 2350 if (*ddata->cur != '_') 2351 goto clean; 2352 ++ddata->cur; 2353 if (!vector_type_qualifier_push(&v, TYPE_VEC)) 2354 goto clean; 2355 goto again; 2356 default: 2357 goto clean; 2358 } 2359 goto rtn; 2360 2361 case 'e': 2362 /* long double */ 2363 if (!DEM_PUSH_STR(ddata, "long double")) 2364 goto clean; 2365 ++ddata->cur; 2366 goto rtn; 2367 2368 case 'f': 2369 /* float */ 2370 if (!DEM_PUSH_STR(ddata, "float")) 2371 goto clean; 2372 ++ddata->cur; 2373 goto rtn; 2374 2375 case 'F': 2376 /* function */ 2377 if (!cpp_demangle_read_function(ddata, &extern_c, &v)) 2378 goto clean; 2379 is_builtin = 0; 2380 goto rtn; 2381 2382 case 'g': 2383 /* __float128 */ 2384 if (!DEM_PUSH_STR(ddata, "__float128")) 2385 goto clean; 2386 ++ddata->cur; 2387 goto rtn; 2388 2389 case 'G': 2390 /* imaginary */ 2391 if (!vector_type_qualifier_push(&v, TYPE_IMG)) 2392 goto clean; 2393 ++ddata->cur; 2394 goto again; 2395 2396 case 'h': 2397 /* unsigned char */ 2398 if (!DEM_PUSH_STR(ddata, "unsigned char")) 2399 goto clean; 2400 ++ddata->cur; 2401 goto rtn; 2402 2403 case 'i': 2404 /* int */ 2405 if (!DEM_PUSH_STR(ddata, "int")) 2406 goto clean; 2407 ++ddata->cur; 2408 goto rtn; 2409 2410 case 'j': 2411 /* unsigned int */ 2412 if (!DEM_PUSH_STR(ddata, "unsigned int")) 2413 goto clean; 2414 ++ddata->cur; 2415 goto rtn; 2416 2417 case 'K': 2418 /* const */ 2419 if (!vector_type_qualifier_push(&v, TYPE_CST)) 2420 goto clean; 2421 ++ddata->cur; 2422 goto again; 2423 2424 case 'l': 2425 /* long */ 2426 if (!DEM_PUSH_STR(ddata, "long")) 2427 goto clean; 2428 ++ddata->cur; 2429 goto rtn; 2430 2431 case 'm': 2432 /* unsigned long */ 2433 if (!DEM_PUSH_STR(ddata, "unsigned long")) 2434 goto clean; 2435 2436 ++ddata->cur; 2437 2438 goto rtn; 2439 case 'M': 2440 /* pointer to member */ 2441 if (!cpp_demangle_read_pointer_to_member(ddata)) 2442 goto clean; 2443 is_builtin = 0; 2444 goto rtn; 2445 2446 case 'n': 2447 /* __int128 */ 2448 if (!DEM_PUSH_STR(ddata, "__int128")) 2449 goto clean; 2450 ++ddata->cur; 2451 goto rtn; 2452 2453 case 'o': 2454 /* unsigned __int128 */ 2455 if (!DEM_PUSH_STR(ddata, "unsigned __int128")) 2456 goto clean; 2457 ++ddata->cur; 2458 goto rtn; 2459 2460 case 'P': 2461 /* pointer */ 2462 if (!vector_type_qualifier_push(&v, TYPE_PTR)) 2463 goto clean; 2464 ++ddata->cur; 2465 goto again; 2466 2467 case 'r': 2468 /* restrict */ 2469 if (!vector_type_qualifier_push(&v, TYPE_RST)) 2470 goto clean; 2471 ++ddata->cur; 2472 goto again; 2473 2474 case 'R': 2475 /* reference */ 2476 if (!vector_type_qualifier_push(&v, TYPE_REF)) 2477 goto clean; 2478 ++ddata->cur; 2479 goto again; 2480 2481 case 's': 2482 /* short, local string */ 2483 if (!DEM_PUSH_STR(ddata, "short")) 2484 goto clean; 2485 ++ddata->cur; 2486 goto rtn; 2487 2488 case 'S': 2489 /* substitution */ 2490 if (!cpp_demangle_read_subst(ddata)) 2491 goto clean; 2492 is_builtin = 0; 2493 goto rtn; 2494 2495 case 't': 2496 /* unsigned short */ 2497 if (!DEM_PUSH_STR(ddata, "unsigned short")) 2498 goto clean; 2499 ++ddata->cur; 2500 goto rtn; 2501 2502 case 'T': 2503 /* template parameter */ 2504 if (!cpp_demangle_read_tmpl_param(ddata)) 2505 goto clean; 2506 is_builtin = 0; 2507 goto rtn; 2508 2509 case 'u': 2510 /* vendor extended builtin */ 2511 ++ddata->cur; 2512 if (!cpp_demangle_read_sname(ddata)) 2513 goto clean; 2514 is_builtin = 0; 2515 goto rtn; 2516 2517 case 'U': 2518 /* vendor extended type qualifier */ 2519 if (!cpp_demangle_read_number(ddata, &len)) 2520 goto clean; 2521 if (len <= 0) 2522 goto clean; 2523 if (!vector_str_push(&v.ext_name, ddata->cur, len)) 2524 return (0); 2525 ddata->cur += len; 2526 if (!vector_type_qualifier_push(&v, TYPE_EXT)) 2527 goto clean; 2528 goto again; 2529 2530 case 'v': 2531 /* void */ 2532 if (!DEM_PUSH_STR(ddata, "void")) 2533 goto clean; 2534 ++ddata->cur; 2535 goto rtn; 2536 2537 case 'V': 2538 /* volatile */ 2539 if (!vector_type_qualifier_push(&v, TYPE_VAT)) 2540 goto clean; 2541 ++ddata->cur; 2542 goto again; 2543 2544 case 'w': 2545 /* wchar_t */ 2546 if (!DEM_PUSH_STR(ddata, "wchar_t")) 2547 goto clean; 2548 ++ddata->cur; 2549 goto rtn; 2550 2551 case 'x': 2552 /* long long */ 2553 if (!DEM_PUSH_STR(ddata, "long long")) 2554 goto clean; 2555 ++ddata->cur; 2556 goto rtn; 2557 2558 case 'y': 2559 /* unsigned long long */ 2560 if (!DEM_PUSH_STR(ddata, "unsigned long long")) 2561 goto clean; 2562 ++ddata->cur; 2563 goto rtn; 2564 2565 case 'z': 2566 /* ellipsis */ 2567 if (!DEM_PUSH_STR(ddata, "...")) 2568 goto clean; 2569 ++ddata->cur; 2570 goto rtn; 2571 } 2572 2573 if (!cpp_demangle_read_name(ddata)) 2574 goto clean; 2575 2576 is_builtin = 0; 2577 rtn: 2578 if ((type_str = vector_str_substr(output, p_idx, output->size - 1, 2579 &type_str_len)) == NULL) 2580 goto clean; 2581 2582 if (is_builtin == 0) { 2583 if (!vector_str_find(&ddata->subst, type_str, type_str_len) && 2584 !vector_str_push(&ddata->subst, type_str, type_str_len)) 2585 goto clean; 2586 } 2587 2588 if (!cpp_demangle_push_type_qualifier(ddata, &v, type_str)) 2589 goto clean; 2590 2591 free(type_str); 2592 free(exp_str); 2593 free(num_str); 2594 vector_type_qualifier_dest(&v); 2595 2596 if (ddata->push_head > 0) { 2597 if (*ddata->cur == 'I' && cpp_demangle_read_tmpl_args(ddata) 2598 == 0) 2599 return (0); 2600 2601 if (--ddata->push_head > 0) 2602 return (1); 2603 2604 if (!VEC_PUSH_STR(&ddata->output_tmp, " ")) 2605 return (0); 2606 2607 if (!vector_str_push_vector_head(&ddata->output, 2608 &ddata->output_tmp)) 2609 return (0); 2610 2611 vector_str_dest(&ddata->output_tmp); 2612 if (!vector_str_init(&ddata->output_tmp)) 2613 return (0); 2614 2615 if (!DEM_PUSH_STR(ddata, "(")) 2616 return (0); 2617 2618 ddata->paren = true; 2619 ddata->pfirst = true; 2620 } 2621 2622 return (1); 2623 clean: 2624 free(type_str); 2625 free(exp_str); 2626 free(num_str); 2627 vector_type_qualifier_dest(&v); 2628 2629 return (0); 2630 } 2631 2632 static int 2633 cpp_demangle_read_type_flat(struct cpp_demangle_data *ddata, char **str) 2634 { 2635 struct vector_str *output; 2636 size_t i, p_idx, idx, type_len; 2637 char *type; 2638 2639 output = ddata->push_head > 0 ? &ddata->output_tmp : 2640 &ddata->output; 2641 2642 p_idx = output->size; 2643 2644 if (!cpp_demangle_read_type(ddata, 0)) 2645 return (0); 2646 2647 if ((type = vector_str_substr(output, p_idx, output->size - 1, 2648 &type_len)) == NULL) 2649 return (0); 2650 2651 idx = output->size; 2652 for (i = p_idx; i < idx; ++i) { 2653 if (!vector_str_pop(output)) { 2654 free(type); 2655 return (0); 2656 } 2657 } 2658 2659 *str = type; 2660 2661 return (1); 2662 } 2663 2664 /* 2665 * read unqualified-name, unqualified name are operator-name, ctor-dtor-name, 2666 * source-name 2667 */ 2668 static int 2669 cpp_demangle_read_uqname(struct cpp_demangle_data *ddata) 2670 { 2671 size_t len; 2672 2673 if (ddata == NULL || *ddata->cur == '\0') 2674 return (0); 2675 2676 /* operator name */ 2677 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 2678 case SIMPLE_HASH('a', 'a'): 2679 /* operator && */ 2680 if (!DEM_PUSH_STR(ddata, "operator&&")) 2681 return (0); 2682 ddata->cur += 2; 2683 return (1); 2684 2685 case SIMPLE_HASH('a', 'd'): 2686 /* operator & (unary) */ 2687 if (!DEM_PUSH_STR(ddata, "operator&")) 2688 return (0); 2689 ddata->cur += 2; 2690 return (1); 2691 2692 case SIMPLE_HASH('a', 'n'): 2693 /* operator & */ 2694 if (!DEM_PUSH_STR(ddata, "operator&")) 2695 return (0); 2696 ddata->cur += 2; 2697 return (1); 2698 2699 case SIMPLE_HASH('a', 'N'): 2700 /* operator &= */ 2701 if (!DEM_PUSH_STR(ddata, "operator&=")) 2702 return (0); 2703 ddata->cur += 2; 2704 return (1); 2705 2706 case SIMPLE_HASH('a', 'S'): 2707 /* operator = */ 2708 if (!DEM_PUSH_STR(ddata, "operator=")) 2709 return (0); 2710 ddata->cur += 2; 2711 return (1); 2712 2713 case SIMPLE_HASH('c', 'l'): 2714 /* operator () */ 2715 if (!DEM_PUSH_STR(ddata, "operator()")) 2716 return (0); 2717 ddata->cur += 2; 2718 return (1); 2719 2720 case SIMPLE_HASH('c', 'm'): 2721 /* operator , */ 2722 if (!DEM_PUSH_STR(ddata, "operator,")) 2723 return (0); 2724 ddata->cur += 2; 2725 return (1); 2726 2727 case SIMPLE_HASH('c', 'o'): 2728 /* operator ~ */ 2729 if (!DEM_PUSH_STR(ddata, "operator~")) 2730 return (0); 2731 ddata->cur += 2; 2732 return (1); 2733 2734 case SIMPLE_HASH('c', 'v'): 2735 /* operator (cast) */ 2736 if (!DEM_PUSH_STR(ddata, "operator(cast)")) 2737 return (0); 2738 ddata->cur += 2; 2739 return (cpp_demangle_read_type(ddata, 1)); 2740 2741 case SIMPLE_HASH('d', 'a'): 2742 /* operator delete [] */ 2743 if (!DEM_PUSH_STR(ddata, "operator delete []")) 2744 return (0); 2745 ddata->cur += 2; 2746 return (1); 2747 2748 case SIMPLE_HASH('d', 'e'): 2749 /* operator * (unary) */ 2750 if (!DEM_PUSH_STR(ddata, "operator*")) 2751 return (0); 2752 ddata->cur += 2; 2753 return (1); 2754 2755 case SIMPLE_HASH('d', 'l'): 2756 /* operator delete */ 2757 if (!DEM_PUSH_STR(ddata, "operator delete")) 2758 return (0); 2759 ddata->cur += 2; 2760 return (1); 2761 2762 case SIMPLE_HASH('d', 'v'): 2763 /* operator / */ 2764 if (!DEM_PUSH_STR(ddata, "operator/")) 2765 return (0); 2766 ddata->cur += 2; 2767 return (1); 2768 2769 case SIMPLE_HASH('d', 'V'): 2770 /* operator /= */ 2771 if (!DEM_PUSH_STR(ddata, "operator/=")) 2772 return (0); 2773 ddata->cur += 2; 2774 return (1); 2775 2776 case SIMPLE_HASH('e', 'o'): 2777 /* operator ^ */ 2778 if (!DEM_PUSH_STR(ddata, "operator^")) 2779 return (0); 2780 ddata->cur += 2; 2781 return (1); 2782 2783 case SIMPLE_HASH('e', 'O'): 2784 /* operator ^= */ 2785 if (!DEM_PUSH_STR(ddata, "operator^=")) 2786 return (0); 2787 ddata->cur += 2; 2788 return (1); 2789 2790 case SIMPLE_HASH('e', 'q'): 2791 /* operator == */ 2792 if (!DEM_PUSH_STR(ddata, "operator==")) 2793 return (0); 2794 ddata->cur += 2; 2795 return (1); 2796 2797 case SIMPLE_HASH('g', 'e'): 2798 /* operator >= */ 2799 if (!DEM_PUSH_STR(ddata, "operator>=")) 2800 return (0); 2801 ddata->cur += 2; 2802 return (1); 2803 2804 case SIMPLE_HASH('g', 't'): 2805 /* operator > */ 2806 if (!DEM_PUSH_STR(ddata, "operator>")) 2807 return (0); 2808 ddata->cur += 2; 2809 return (1); 2810 2811 case SIMPLE_HASH('i', 'x'): 2812 /* operator [] */ 2813 if (!DEM_PUSH_STR(ddata, "operator[]")) 2814 return (0); 2815 ddata->cur += 2; 2816 return (1); 2817 2818 case SIMPLE_HASH('l', 'e'): 2819 /* operator <= */ 2820 if (!DEM_PUSH_STR(ddata, "operator<=")) 2821 return (0); 2822 ddata->cur += 2; 2823 return (1); 2824 2825 case SIMPLE_HASH('l', 's'): 2826 /* operator << */ 2827 if (!DEM_PUSH_STR(ddata, "operator<<")) 2828 return (0); 2829 ddata->cur += 2; 2830 return (1); 2831 2832 case SIMPLE_HASH('l', 'S'): 2833 /* operator <<= */ 2834 if (!DEM_PUSH_STR(ddata, "operator<<=")) 2835 return (0); 2836 ddata->cur += 2; 2837 return (1); 2838 2839 case SIMPLE_HASH('l', 't'): 2840 /* operator < */ 2841 if (!DEM_PUSH_STR(ddata, "operator<")) 2842 return (0); 2843 ddata->cur += 2; 2844 return (1); 2845 2846 case SIMPLE_HASH('m', 'i'): 2847 /* operator - */ 2848 if (!DEM_PUSH_STR(ddata, "operator-")) 2849 return (0); 2850 ddata->cur += 2; 2851 return (1); 2852 2853 case SIMPLE_HASH('m', 'I'): 2854 /* operator -= */ 2855 if (!DEM_PUSH_STR(ddata, "operator-=")) 2856 return (0); 2857 ddata->cur += 2; 2858 return (1); 2859 2860 case SIMPLE_HASH('m', 'l'): 2861 /* operator * */ 2862 if (!DEM_PUSH_STR(ddata, "operator*")) 2863 return (0); 2864 ddata->cur += 2; 2865 return (1); 2866 2867 case SIMPLE_HASH('m', 'L'): 2868 /* operator *= */ 2869 if (!DEM_PUSH_STR(ddata, "operator*=")) 2870 return (0); 2871 ddata->cur += 2; 2872 return (1); 2873 2874 case SIMPLE_HASH('m', 'm'): 2875 /* operator -- */ 2876 if (!DEM_PUSH_STR(ddata, "operator--")) 2877 return (0); 2878 ddata->cur += 2; 2879 return (1); 2880 2881 case SIMPLE_HASH('n', 'a'): 2882 /* operator new[] */ 2883 if (!DEM_PUSH_STR(ddata, "operator new []")) 2884 return (0); 2885 ddata->cur += 2; 2886 return (1); 2887 2888 case SIMPLE_HASH('n', 'e'): 2889 /* operator != */ 2890 if (!DEM_PUSH_STR(ddata, "operator!=")) 2891 return (0); 2892 ddata->cur += 2; 2893 return (1); 2894 2895 case SIMPLE_HASH('n', 'g'): 2896 /* operator - (unary) */ 2897 if (!DEM_PUSH_STR(ddata, "operator-")) 2898 return (0); 2899 ddata->cur += 2; 2900 return (1); 2901 2902 case SIMPLE_HASH('n', 't'): 2903 /* operator ! */ 2904 if (!DEM_PUSH_STR(ddata, "operator!")) 2905 return (0); 2906 ddata->cur += 2; 2907 return (1); 2908 2909 case SIMPLE_HASH('n', 'w'): 2910 /* operator new */ 2911 if (!DEM_PUSH_STR(ddata, "operator new")) 2912 return (0); 2913 ddata->cur += 2; 2914 return (1); 2915 2916 case SIMPLE_HASH('o', 'o'): 2917 /* operator || */ 2918 if (!DEM_PUSH_STR(ddata, "operator||")) 2919 return (0); 2920 ddata->cur += 2; 2921 return (1); 2922 2923 case SIMPLE_HASH('o', 'r'): 2924 /* operator | */ 2925 if (!DEM_PUSH_STR(ddata, "operator|")) 2926 return (0); 2927 ddata->cur += 2; 2928 return (1); 2929 2930 case SIMPLE_HASH('o', 'R'): 2931 /* operator |= */ 2932 if (!DEM_PUSH_STR(ddata, "operator|=")) 2933 return (0); 2934 ddata->cur += 2; 2935 return (1); 2936 2937 case SIMPLE_HASH('p', 'l'): 2938 /* operator + */ 2939 if (!DEM_PUSH_STR(ddata, "operator+")) 2940 return (0); 2941 ddata->cur += 2; 2942 return (1); 2943 2944 case SIMPLE_HASH('p', 'L'): 2945 /* operator += */ 2946 if (!DEM_PUSH_STR(ddata, "operator+=")) 2947 return (0); 2948 ddata->cur += 2; 2949 return (1); 2950 2951 case SIMPLE_HASH('p', 'm'): 2952 /* operator ->* */ 2953 if (!DEM_PUSH_STR(ddata, "operator->*")) 2954 return (0); 2955 ddata->cur += 2; 2956 return (1); 2957 2958 case SIMPLE_HASH('p', 'p'): 2959 /* operator ++ */ 2960 if (!DEM_PUSH_STR(ddata, "operator++")) 2961 return (0); 2962 ddata->cur += 2; 2963 return (1); 2964 2965 case SIMPLE_HASH('p', 's'): 2966 /* operator + (unary) */ 2967 if (!DEM_PUSH_STR(ddata, "operator+")) 2968 return (0); 2969 ddata->cur += 2; 2970 return (1); 2971 2972 case SIMPLE_HASH('p', 't'): 2973 /* operator -> */ 2974 if (!DEM_PUSH_STR(ddata, "operator->")) 2975 return (0); 2976 ddata->cur += 2; 2977 return (1); 2978 2979 case SIMPLE_HASH('q', 'u'): 2980 /* operator ? */ 2981 if (!DEM_PUSH_STR(ddata, "operator?")) 2982 return (0); 2983 ddata->cur += 2; 2984 return (1); 2985 2986 case SIMPLE_HASH('r', 'm'): 2987 /* operator % */ 2988 if (!DEM_PUSH_STR(ddata, "operator%")) 2989 return (0); 2990 ddata->cur += 2; 2991 return (1); 2992 2993 case SIMPLE_HASH('r', 'M'): 2994 /* operator %= */ 2995 if (!DEM_PUSH_STR(ddata, "operator%=")) 2996 return (0); 2997 ddata->cur += 2; 2998 return (1); 2999 3000 case SIMPLE_HASH('r', 's'): 3001 /* operator >> */ 3002 if (!DEM_PUSH_STR(ddata, "operator>>")) 3003 return (0); 3004 ddata->cur += 2; 3005 return (1); 3006 3007 case SIMPLE_HASH('r', 'S'): 3008 /* operator >>= */ 3009 if (!DEM_PUSH_STR(ddata, "operator>>=")) 3010 return (0); 3011 ddata->cur += 2; 3012 return (1); 3013 3014 case SIMPLE_HASH('r', 'z'): 3015 /* operator sizeof */ 3016 if (!DEM_PUSH_STR(ddata, "operator sizeof ")) 3017 return (0); 3018 ddata->cur += 2; 3019 return (1); 3020 3021 case SIMPLE_HASH('s', 'r'): 3022 /* scope resolution operator */ 3023 if (!DEM_PUSH_STR(ddata, "scope resolution operator ")) 3024 return (0); 3025 ddata->cur += 2; 3026 return (1); 3027 3028 case SIMPLE_HASH('s', 'v'): 3029 /* operator sizeof */ 3030 if (!DEM_PUSH_STR(ddata, "operator sizeof ")) 3031 return (0); 3032 ddata->cur += 2; 3033 return (1); 3034 } 3035 3036 /* vendor extened operator */ 3037 if (*ddata->cur == 'v' && ELFTC_ISDIGIT(*(ddata->cur + 1))) { 3038 if (!DEM_PUSH_STR(ddata, "vendor extened operator ")) 3039 return (0); 3040 if (!cpp_demangle_push_str(ddata, ddata->cur + 1, 1)) 3041 return (0); 3042 ddata->cur += 2; 3043 return (cpp_demangle_read_sname(ddata)); 3044 } 3045 3046 /* ctor-dtor-name */ 3047 switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { 3048 case SIMPLE_HASH('C', '1'): 3049 /* FALLTHROUGH */ 3050 case SIMPLE_HASH('C', '2'): 3051 /* FALLTHROUGH */ 3052 case SIMPLE_HASH('C', '3'): 3053 if (ddata->last_sname == NULL) 3054 return (0); 3055 if ((len = strlen(ddata->last_sname)) == 0) 3056 return (0); 3057 if (!DEM_PUSH_STR(ddata, "::")) 3058 return (0); 3059 if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) 3060 return (0); 3061 ddata->cur +=2; 3062 return (1); 3063 3064 case SIMPLE_HASH('D', '0'): 3065 /* FALLTHROUGH */ 3066 case SIMPLE_HASH('D', '1'): 3067 /* FALLTHROUGH */ 3068 case SIMPLE_HASH('D', '2'): 3069 if (ddata->last_sname == NULL) 3070 return (0); 3071 if ((len = strlen(ddata->last_sname)) == 0) 3072 return (0); 3073 if (!DEM_PUSH_STR(ddata, "::~")) 3074 return (0); 3075 if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) 3076 return (0); 3077 ddata->cur +=2; 3078 return (1); 3079 } 3080 3081 /* source name */ 3082 if (ELFTC_ISDIGIT(*ddata->cur) != 0) 3083 return (cpp_demangle_read_sname(ddata)); 3084 3085 /* local source name */ 3086 if (*ddata->cur == 'L') 3087 return (cpp_demangle_local_source_name(ddata)); 3088 3089 return (1); 3090 } 3091 3092 /* 3093 * Read local source name. 3094 * 3095 * References: 3096 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775 3097 * http://gcc.gnu.org/viewcvs?view=rev&revision=124467 3098 */ 3099 static int 3100 cpp_demangle_local_source_name(struct cpp_demangle_data *ddata) 3101 { 3102 /* L */ 3103 if (ddata == NULL || *ddata->cur != 'L') 3104 return (0); 3105 ++ddata->cur; 3106 3107 /* source name */ 3108 if (!cpp_demangle_read_sname(ddata)) 3109 return (0); 3110 3111 /* discriminator */ 3112 if (*ddata->cur == '_') { 3113 ++ddata->cur; 3114 while (ELFTC_ISDIGIT(*ddata->cur) != 0) 3115 ++ddata->cur; 3116 } 3117 3118 return (1); 3119 } 3120 3121 static int 3122 cpp_demangle_read_v_offset(struct cpp_demangle_data *ddata) 3123 { 3124 3125 if (ddata == NULL) 3126 return (0); 3127 3128 if (!DEM_PUSH_STR(ddata, "offset : ")) 3129 return (0); 3130 3131 if (!cpp_demangle_read_offset_number(ddata)) 3132 return (0); 3133 3134 if (!DEM_PUSH_STR(ddata, "virtual offset : ")) 3135 return (0); 3136 3137 return (!cpp_demangle_read_offset_number(ddata)); 3138 } 3139 3140 /* 3141 * Decode floating point representation to string 3142 * Return new allocated string or NULL 3143 * 3144 * Todo 3145 * Replace these functions to macro. 3146 */ 3147 static char * 3148 decode_fp_to_double(const char *p, size_t len) 3149 { 3150 double f; 3151 size_t rtn_len, limit, i; 3152 int byte; 3153 char *rtn; 3154 3155 if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(double)) 3156 return (NULL); 3157 3158 memset(&f, 0, sizeof(double)); 3159 3160 for (i = 0; i < len / 2; ++i) { 3161 byte = hex_to_dec(p[len - i * 2 - 1]) + 3162 hex_to_dec(p[len - i * 2 - 2]) * 16; 3163 3164 if (byte < 0 || byte > 255) 3165 return (NULL); 3166 3167 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3168 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3169 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3170 ((unsigned char *)&f)[sizeof(double) - i - 1] = 3171 (unsigned char)(byte); 3172 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3173 } 3174 3175 rtn_len = 64; 3176 limit = 0; 3177 again: 3178 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3179 return (NULL); 3180 3181 if (snprintf(rtn, rtn_len, "%fld", f) >= (int)rtn_len) { 3182 free(rtn); 3183 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3184 return (NULL); 3185 rtn_len *= BUFFER_GROWFACTOR; 3186 goto again; 3187 } 3188 3189 return rtn; 3190 } 3191 3192 static char * 3193 decode_fp_to_float(const char *p, size_t len) 3194 { 3195 size_t i, rtn_len, limit; 3196 float f; 3197 int byte; 3198 char *rtn; 3199 3200 if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(float)) 3201 return (NULL); 3202 3203 memset(&f, 0, sizeof(float)); 3204 3205 for (i = 0; i < len / 2; ++i) { 3206 byte = hex_to_dec(p[len - i * 2 - 1]) + 3207 hex_to_dec(p[len - i * 2 - 2]) * 16; 3208 if (byte < 0 || byte > 255) 3209 return (NULL); 3210 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3211 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3212 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3213 ((unsigned char *)&f)[sizeof(float) - i - 1] = 3214 (unsigned char)(byte); 3215 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3216 } 3217 3218 rtn_len = 64; 3219 limit = 0; 3220 again: 3221 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3222 return (NULL); 3223 3224 if (snprintf(rtn, rtn_len, "%ff", f) >= (int)rtn_len) { 3225 free(rtn); 3226 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3227 return (NULL); 3228 rtn_len *= BUFFER_GROWFACTOR; 3229 goto again; 3230 } 3231 3232 return rtn; 3233 } 3234 3235 static char * 3236 decode_fp_to_float128(const char *p, size_t len) 3237 { 3238 long double f; 3239 size_t rtn_len, limit, i; 3240 int byte; 3241 unsigned char buf[FLOAT_QUADRUPLE_BYTES]; 3242 char *rtn; 3243 3244 switch(sizeof(long double)) { 3245 case FLOAT_QUADRUPLE_BYTES: 3246 return (decode_fp_to_long_double(p, len)); 3247 case FLOAT_EXTENED_BYTES: 3248 if (p == NULL || len == 0 || len % 2 != 0 || 3249 len / 2 > FLOAT_QUADRUPLE_BYTES) 3250 return (NULL); 3251 3252 memset(buf, 0, FLOAT_QUADRUPLE_BYTES); 3253 3254 for (i = 0; i < len / 2; ++i) { 3255 byte = hex_to_dec(p[len - i * 2 - 1]) + 3256 hex_to_dec(p[len - i * 2 - 2]) * 16; 3257 if (byte < 0 || byte > 255) 3258 return (NULL); 3259 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3260 buf[i] = (unsigned char)(byte); 3261 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3262 buf[FLOAT_QUADRUPLE_BYTES - i -1] = 3263 (unsigned char)(byte); 3264 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3265 } 3266 memset(&f, 0, FLOAT_EXTENED_BYTES); 3267 3268 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3269 memcpy(&f, buf, FLOAT_EXTENED_BYTES); 3270 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3271 memcpy(&f, buf + 6, FLOAT_EXTENED_BYTES); 3272 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3273 3274 rtn_len = 256; 3275 limit = 0; 3276 again: 3277 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3278 return (NULL); 3279 3280 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3281 free(rtn); 3282 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3283 return (NULL); 3284 rtn_len *= BUFFER_GROWFACTOR; 3285 goto again; 3286 } 3287 3288 return (rtn); 3289 default: 3290 return (NULL); 3291 } 3292 } 3293 3294 static char * 3295 decode_fp_to_float80(const char *p, size_t len) 3296 { 3297 long double f; 3298 size_t rtn_len, limit, i; 3299 int byte; 3300 unsigned char buf[FLOAT_EXTENED_BYTES]; 3301 char *rtn; 3302 3303 switch(sizeof(long double)) { 3304 case FLOAT_QUADRUPLE_BYTES: 3305 if (p == NULL || len == 0 || len % 2 != 0 || 3306 len / 2 > FLOAT_EXTENED_BYTES) 3307 return (NULL); 3308 3309 memset(buf, 0, FLOAT_EXTENED_BYTES); 3310 3311 for (i = 0; i < len / 2; ++i) { 3312 byte = hex_to_dec(p[len - i * 2 - 1]) + 3313 hex_to_dec(p[len - i * 2 - 2]) * 16; 3314 3315 if (byte < 0 || byte > 255) 3316 return (NULL); 3317 3318 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3319 buf[i] = (unsigned char)(byte); 3320 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3321 buf[FLOAT_EXTENED_BYTES - i -1] = 3322 (unsigned char)(byte); 3323 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3324 } 3325 3326 memset(&f, 0, FLOAT_QUADRUPLE_BYTES); 3327 3328 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3329 memcpy(&f, buf, FLOAT_EXTENED_BYTES); 3330 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3331 memcpy((unsigned char *)(&f) + 6, buf, FLOAT_EXTENED_BYTES); 3332 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3333 3334 rtn_len = 256; 3335 limit = 0; 3336 again: 3337 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3338 return (NULL); 3339 3340 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3341 free(rtn); 3342 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3343 return (NULL); 3344 rtn_len *= BUFFER_GROWFACTOR; 3345 goto again; 3346 } 3347 3348 return (rtn); 3349 case FLOAT_EXTENED_BYTES: 3350 return (decode_fp_to_long_double(p, len)); 3351 default: 3352 return (NULL); 3353 } 3354 } 3355 3356 static char * 3357 decode_fp_to_long_double(const char *p, size_t len) 3358 { 3359 long double f; 3360 size_t rtn_len, limit, i; 3361 int byte; 3362 char *rtn; 3363 3364 if (p == NULL || len == 0 || len % 2 != 0 || 3365 len / 2 > sizeof(long double)) 3366 return (NULL); 3367 3368 memset(&f, 0, sizeof(long double)); 3369 3370 for (i = 0; i < len / 2; ++i) { 3371 byte = hex_to_dec(p[len - i * 2 - 1]) + 3372 hex_to_dec(p[len - i * 2 - 2]) * 16; 3373 3374 if (byte < 0 || byte > 255) 3375 return (NULL); 3376 3377 #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN 3378 ((unsigned char *)&f)[i] = (unsigned char)(byte); 3379 #else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3380 ((unsigned char *)&f)[sizeof(long double) - i - 1] = 3381 (unsigned char)(byte); 3382 #endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ 3383 } 3384 3385 rtn_len = 256; 3386 limit = 0; 3387 again: 3388 if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) 3389 return (NULL); 3390 3391 if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { 3392 free(rtn); 3393 if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) 3394 return (NULL); 3395 rtn_len *= BUFFER_GROWFACTOR; 3396 goto again; 3397 } 3398 3399 return (rtn); 3400 } 3401 3402 /* Simple hex to integer function used by decode_to_* function. */ 3403 static int 3404 hex_to_dec(char c) 3405 { 3406 3407 switch (c) { 3408 case '0': 3409 return (0); 3410 case '1': 3411 return (1); 3412 case '2': 3413 return (2); 3414 case '3': 3415 return (3); 3416 case '4': 3417 return (4); 3418 case '5': 3419 return (5); 3420 case '6': 3421 return (6); 3422 case '7': 3423 return (7); 3424 case '8': 3425 return (8); 3426 case '9': 3427 return (9); 3428 case 'a': 3429 return (10); 3430 case 'b': 3431 return (11); 3432 case 'c': 3433 return (12); 3434 case 'd': 3435 return (13); 3436 case 'e': 3437 return (14); 3438 case 'f': 3439 return (15); 3440 default: 3441 return (-1); 3442 } 3443 } 3444 3445 /** 3446 * @brief Test input string is mangled by IA-64 C++ ABI style. 3447 * 3448 * Test string heads with "_Z" or "_GLOBAL__I_". 3449 * @return Return 0 at false. 3450 */ 3451 bool 3452 is_cpp_mangled_gnu3(const char *org) 3453 { 3454 size_t len; 3455 3456 len = strlen(org); 3457 return ((len > 2 && *org == '_' && *(org + 1) == 'Z') || 3458 (len > 11 && !strncmp(org, "_GLOBAL__I_", 11))); 3459 } 3460 3461 static void 3462 vector_read_cmd_dest(struct vector_read_cmd *v) 3463 { 3464 3465 if (v == NULL) 3466 return; 3467 3468 free(v->r_container); 3469 } 3470 3471 /* return -1 at failed, 0 at not found, 1 at found. */ 3472 static int 3473 vector_read_cmd_find(struct vector_read_cmd *v, enum read_cmd dst) 3474 { 3475 size_t i; 3476 3477 if (v == NULL || dst == READ_FAIL) 3478 return (-1); 3479 3480 for (i = 0; i < v->size; ++i) 3481 if (v->r_container[i] == dst) 3482 return (1); 3483 3484 return (0); 3485 } 3486 3487 static int 3488 vector_read_cmd_init(struct vector_read_cmd *v) 3489 { 3490 3491 if (v == NULL) 3492 return (0); 3493 3494 v->size = 0; 3495 v->capacity = VECTOR_DEF_CAPACITY; 3496 3497 if ((v->r_container = malloc(sizeof(enum read_cmd) * v->capacity)) 3498 == NULL) 3499 return (0); 3500 3501 return (1); 3502 } 3503 3504 static int 3505 vector_read_cmd_pop(struct vector_read_cmd *v) 3506 { 3507 3508 if (v == NULL || v->size == 0) 3509 return (0); 3510 3511 --v->size; 3512 v->r_container[v->size] = READ_FAIL; 3513 3514 return (1); 3515 } 3516 3517 static int 3518 vector_read_cmd_push(struct vector_read_cmd *v, enum read_cmd cmd) 3519 { 3520 enum read_cmd *tmp_r_ctn; 3521 size_t tmp_cap; 3522 size_t i; 3523 3524 if (v == NULL) 3525 return (0); 3526 3527 if (v->size == v->capacity) { 3528 tmp_cap = v->capacity * BUFFER_GROWFACTOR; 3529 if ((tmp_r_ctn = malloc(sizeof(enum read_cmd) * tmp_cap)) 3530 == NULL) 3531 return (0); 3532 for (i = 0; i < v->size; ++i) 3533 tmp_r_ctn[i] = v->r_container[i]; 3534 free(v->r_container); 3535 v->r_container = tmp_r_ctn; 3536 v->capacity = tmp_cap; 3537 } 3538 3539 v->r_container[v->size] = cmd; 3540 ++v->size; 3541 3542 return (1); 3543 } 3544 3545 static void 3546 vector_type_qualifier_dest(struct vector_type_qualifier *v) 3547 { 3548 3549 if (v == NULL) 3550 return; 3551 3552 free(v->q_container); 3553 vector_str_dest(&v->ext_name); 3554 } 3555 3556 /* size, capacity, ext_name */ 3557 static int 3558 vector_type_qualifier_init(struct vector_type_qualifier *v) 3559 { 3560 3561 if (v == NULL) 3562 return (0); 3563 3564 v->size = 0; 3565 v->capacity = VECTOR_DEF_CAPACITY; 3566 3567 if ((v->q_container = malloc(sizeof(enum type_qualifier) * v->capacity)) 3568 == NULL) 3569 return (0); 3570 3571 assert(v->q_container != NULL); 3572 3573 if (vector_str_init(&v->ext_name) == false) { 3574 free(v->q_container); 3575 return (0); 3576 } 3577 3578 return (1); 3579 } 3580 3581 static int 3582 vector_type_qualifier_push(struct vector_type_qualifier *v, 3583 enum type_qualifier t) 3584 { 3585 enum type_qualifier *tmp_ctn; 3586 size_t tmp_cap; 3587 size_t i; 3588 3589 if (v == NULL) 3590 return (0); 3591 3592 if (v->size == v->capacity) { 3593 tmp_cap = v->capacity * BUFFER_GROWFACTOR; 3594 if ((tmp_ctn = malloc(sizeof(enum type_qualifier) * tmp_cap)) 3595 == NULL) 3596 return (0); 3597 for (i = 0; i < v->size; ++i) 3598 tmp_ctn[i] = v->q_container[i]; 3599 free(v->q_container); 3600 v->q_container = tmp_ctn; 3601 v->capacity = tmp_cap; 3602 } 3603 3604 v->q_container[v->size] = t; 3605 ++v->size; 3606 3607 return (1); 3608 } 3609