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