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