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