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