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