1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package elf 6 7 import ( 8 "io" 9 "path" 10 "reflect" 11 "testing" 12 ) 13 14 // TODO: remove duplicate code 15 func TestSymbols(t *testing.T) { 16 do := func(file string, ts []Symbol, getfunc func(*File) ([]Symbol, error)) { 17 var f *File 18 var err error 19 if path.Ext(file) == ".gz" { 20 var r io.ReaderAt 21 if r, err = decompress(file); err == nil { 22 f, err = NewFile(r) 23 } 24 } else { 25 f, err = Open(file) 26 } 27 if err != nil { 28 t.Errorf("TestSymbols: cannot open file %s: %v", file, err) 29 return 30 } 31 defer f.Close() 32 fs, err := getfunc(f) 33 if err != nil && err != ErrNoSymbols { 34 t.Error(err) 35 return 36 } else if err == ErrNoSymbols { 37 fs = []Symbol{} 38 } 39 if !reflect.DeepEqual(ts, fs) { 40 t.Errorf("%s: Symbols = %v, want %v", file, ts, fs) 41 } 42 } 43 for file, ts := range symbolsGolden { 44 do(file, ts, (*File).Symbols) 45 } 46 for file, ts := range dynamicSymbolsGolden { 47 do(file, ts, (*File).DynamicSymbols) 48 } 49 } 50 51 // golden symbol table data generated by testdata/getgoldsym.c 52 53 var symbolsGolden = map[string][]Symbol{ 54 "testdata/gcc-amd64-linux-exec": { 55 Symbol{ 56 Name: "", 57 Info: 0x3, 58 Other: 0x0, 59 Section: 0x1, 60 Value: 0x400200, 61 Size: 0x0, 62 }, 63 Symbol{ 64 Name: "", 65 Info: 0x3, 66 Other: 0x0, 67 Section: 0x2, 68 Value: 0x40021C, 69 Size: 0x0, 70 }, 71 Symbol{ 72 Name: "", 73 Info: 0x3, 74 Other: 0x0, 75 Section: 0x3, 76 Value: 0x400240, 77 Size: 0x0, 78 }, 79 Symbol{ 80 Name: "", 81 Info: 0x3, 82 Other: 0x0, 83 Section: 0x4, 84 Value: 0x400268, 85 Size: 0x0, 86 }, 87 Symbol{ 88 Name: "", 89 Info: 0x3, 90 Other: 0x0, 91 Section: 0x5, 92 Value: 0x400288, 93 Size: 0x0, 94 }, 95 Symbol{ 96 Name: "", 97 Info: 0x3, 98 Other: 0x0, 99 Section: 0x6, 100 Value: 0x4002E8, 101 Size: 0x0, 102 }, 103 Symbol{ 104 Name: "", 105 Info: 0x3, 106 Other: 0x0, 107 Section: 0x7, 108 Value: 0x400326, 109 Size: 0x0, 110 }, 111 Symbol{ 112 Name: "", 113 Info: 0x3, 114 Other: 0x0, 115 Section: 0x8, 116 Value: 0x400330, 117 Size: 0x0, 118 }, 119 Symbol{ 120 Name: "", 121 Info: 0x3, 122 Other: 0x0, 123 Section: 0x9, 124 Value: 0x400350, 125 Size: 0x0, 126 }, 127 Symbol{ 128 Name: "", 129 Info: 0x3, 130 Other: 0x0, 131 Section: 0xA, 132 Value: 0x400368, 133 Size: 0x0, 134 }, 135 Symbol{ 136 Name: "", 137 Info: 0x3, 138 Other: 0x0, 139 Section: 0xB, 140 Value: 0x400398, 141 Size: 0x0, 142 }, 143 Symbol{ 144 Name: "", 145 Info: 0x3, 146 Other: 0x0, 147 Section: 0xC, 148 Value: 0x4003B0, 149 Size: 0x0, 150 }, 151 Symbol{ 152 Name: "", 153 Info: 0x3, 154 Other: 0x0, 155 Section: 0xD, 156 Value: 0x4003E0, 157 Size: 0x0, 158 }, 159 Symbol{ 160 Name: "", 161 Info: 0x3, 162 Other: 0x0, 163 Section: 0xE, 164 Value: 0x400594, 165 Size: 0x0, 166 }, 167 Symbol{ 168 Name: "", 169 Info: 0x3, 170 Other: 0x0, 171 Section: 0xF, 172 Value: 0x4005A4, 173 Size: 0x0, 174 }, 175 Symbol{ 176 Name: "", 177 Info: 0x3, 178 Other: 0x0, 179 Section: 0x10, 180 Value: 0x4005B8, 181 Size: 0x0, 182 }, 183 Symbol{ 184 Name: "", 185 Info: 0x3, 186 Other: 0x0, 187 Section: 0x11, 188 Value: 0x4005E0, 189 Size: 0x0, 190 }, 191 Symbol{ 192 Name: "", 193 Info: 0x3, 194 Other: 0x0, 195 Section: 0x12, 196 Value: 0x600688, 197 Size: 0x0, 198 }, 199 Symbol{ 200 Name: "", 201 Info: 0x3, 202 Other: 0x0, 203 Section: 0x13, 204 Value: 0x600698, 205 Size: 0x0, 206 }, 207 Symbol{ 208 Name: "", 209 Info: 0x3, 210 Other: 0x0, 211 Section: 0x14, 212 Value: 0x6006A8, 213 Size: 0x0, 214 }, 215 Symbol{ 216 Name: "", 217 Info: 0x3, 218 Other: 0x0, 219 Section: 0x15, 220 Value: 0x6006B0, 221 Size: 0x0, 222 }, 223 Symbol{ 224 Name: "", 225 Info: 0x3, 226 Other: 0x0, 227 Section: 0x16, 228 Value: 0x600850, 229 Size: 0x0, 230 }, 231 Symbol{ 232 Name: "", 233 Info: 0x3, 234 Other: 0x0, 235 Section: 0x17, 236 Value: 0x600858, 237 Size: 0x0, 238 }, 239 Symbol{ 240 Name: "", 241 Info: 0x3, 242 Other: 0x0, 243 Section: 0x18, 244 Value: 0x600880, 245 Size: 0x0, 246 }, 247 Symbol{ 248 Name: "", 249 Info: 0x3, 250 Other: 0x0, 251 Section: 0x19, 252 Value: 0x600898, 253 Size: 0x0, 254 }, 255 Symbol{ 256 Name: "", 257 Info: 0x3, 258 Other: 0x0, 259 Section: 0x1A, 260 Value: 0x0, 261 Size: 0x0, 262 }, 263 Symbol{ 264 Name: "", 265 Info: 0x3, 266 Other: 0x0, 267 Section: 0x1B, 268 Value: 0x0, 269 Size: 0x0, 270 }, 271 Symbol{ 272 Name: "", 273 Info: 0x3, 274 Other: 0x0, 275 Section: 0x1C, 276 Value: 0x0, 277 Size: 0x0, 278 }, 279 Symbol{ 280 Name: "", 281 Info: 0x3, 282 Other: 0x0, 283 Section: 0x1D, 284 Value: 0x0, 285 Size: 0x0, 286 }, 287 Symbol{ 288 Name: "", 289 Info: 0x3, 290 Other: 0x0, 291 Section: 0x1E, 292 Value: 0x0, 293 Size: 0x0, 294 }, 295 Symbol{ 296 Name: "", 297 Info: 0x3, 298 Other: 0x0, 299 Section: 0x1F, 300 Value: 0x0, 301 Size: 0x0, 302 }, 303 Symbol{ 304 Name: "", 305 Info: 0x3, 306 Other: 0x0, 307 Section: 0x20, 308 Value: 0x0, 309 Size: 0x0, 310 }, 311 Symbol{ 312 Name: "", 313 Info: 0x3, 314 Other: 0x0, 315 Section: 0x21, 316 Value: 0x0, 317 Size: 0x0, 318 }, 319 Symbol{ 320 Name: "init.c", 321 Info: 0x4, 322 Other: 0x0, 323 Section: 0xFFF1, 324 Value: 0x0, 325 Size: 0x0, 326 }, 327 Symbol{ 328 Name: "initfini.c", 329 Info: 0x4, 330 Other: 0x0, 331 Section: 0xFFF1, 332 Value: 0x0, 333 Size: 0x0, 334 }, 335 Symbol{ 336 Name: "call_gmon_start", 337 Info: 0x2, 338 Other: 0x0, 339 Section: 0xD, 340 Value: 0x40040C, 341 Size: 0x0, 342 }, 343 Symbol{ 344 Name: "crtstuff.c", 345 Info: 0x4, 346 Other: 0x0, 347 Section: 0xFFF1, 348 Value: 0x0, 349 Size: 0x0, 350 }, 351 Symbol{ 352 Name: "__CTOR_LIST__", 353 Info: 0x1, 354 Other: 0x0, 355 Section: 0x12, 356 Value: 0x600688, 357 Size: 0x0, 358 }, 359 Symbol{ 360 Name: "__DTOR_LIST__", 361 Info: 0x1, 362 Other: 0x0, 363 Section: 0x13, 364 Value: 0x600698, 365 Size: 0x0, 366 }, 367 Symbol{ 368 Name: "__JCR_LIST__", 369 Info: 0x1, 370 Other: 0x0, 371 Section: 0x14, 372 Value: 0x6006A8, 373 Size: 0x0, 374 }, 375 Symbol{ 376 Name: "__do_global_dtors_aux", 377 Info: 0x2, 378 Other: 0x0, 379 Section: 0xD, 380 Value: 0x400430, 381 Size: 0x0, 382 }, 383 Symbol{ 384 Name: "completed.6183", 385 Info: 0x1, 386 Other: 0x0, 387 Section: 0x19, 388 Value: 0x600898, 389 Size: 0x1, 390 }, 391 Symbol{ 392 Name: "p.6181", 393 Info: 0x1, 394 Other: 0x0, 395 Section: 0x18, 396 Value: 0x600890, 397 Size: 0x0, 398 }, 399 Symbol{ 400 Name: "frame_dummy", 401 Info: 0x2, 402 Other: 0x0, 403 Section: 0xD, 404 Value: 0x400470, 405 Size: 0x0, 406 }, 407 Symbol{ 408 Name: "crtstuff.c", 409 Info: 0x4, 410 Other: 0x0, 411 Section: 0xFFF1, 412 Value: 0x0, 413 Size: 0x0, 414 }, 415 Symbol{ 416 Name: "__CTOR_END__", 417 Info: 0x1, 418 Other: 0x0, 419 Section: 0x12, 420 Value: 0x600690, 421 Size: 0x0, 422 }, 423 Symbol{ 424 Name: "__DTOR_END__", 425 Info: 0x1, 426 Other: 0x0, 427 Section: 0x13, 428 Value: 0x6006A0, 429 Size: 0x0, 430 }, 431 Symbol{ 432 Name: "__FRAME_END__", 433 Info: 0x1, 434 Other: 0x0, 435 Section: 0x11, 436 Value: 0x400680, 437 Size: 0x0, 438 }, 439 Symbol{ 440 Name: "__JCR_END__", 441 Info: 0x1, 442 Other: 0x0, 443 Section: 0x14, 444 Value: 0x6006A8, 445 Size: 0x0, 446 }, 447 Symbol{ 448 Name: "__do_global_ctors_aux", 449 Info: 0x2, 450 Other: 0x0, 451 Section: 0xD, 452 Value: 0x400560, 453 Size: 0x0, 454 }, 455 Symbol{ 456 Name: "initfini.c", 457 Info: 0x4, 458 Other: 0x0, 459 Section: 0xFFF1, 460 Value: 0x0, 461 Size: 0x0, 462 }, 463 Symbol{ 464 Name: "hello.c", 465 Info: 0x4, 466 Other: 0x0, 467 Section: 0xFFF1, 468 Value: 0x0, 469 Size: 0x0, 470 }, 471 Symbol{ 472 Name: "_GLOBAL_OFFSET_TABLE_", 473 Info: 0x1, 474 Other: 0x2, 475 Section: 0x17, 476 Value: 0x600858, 477 Size: 0x0, 478 }, 479 Symbol{ 480 Name: "__init_array_end", 481 Info: 0x0, 482 Other: 0x2, 483 Section: 0x12, 484 Value: 0x600684, 485 Size: 0x0, 486 }, 487 Symbol{ 488 Name: "__init_array_start", 489 Info: 0x0, 490 Other: 0x2, 491 Section: 0x12, 492 Value: 0x600684, 493 Size: 0x0, 494 }, 495 Symbol{ 496 Name: "_DYNAMIC", 497 Info: 0x1, 498 Other: 0x2, 499 Section: 0x15, 500 Value: 0x6006B0, 501 Size: 0x0, 502 }, 503 Symbol{ 504 Name: "data_start", 505 Info: 0x20, 506 Other: 0x0, 507 Section: 0x18, 508 Value: 0x600880, 509 Size: 0x0, 510 }, 511 Symbol{ 512 Name: "__libc_csu_fini", 513 Info: 0x12, 514 Other: 0x0, 515 Section: 0xD, 516 Value: 0x4004C0, 517 Size: 0x2, 518 }, 519 Symbol{ 520 Name: "_start", 521 Info: 0x12, 522 Other: 0x0, 523 Section: 0xD, 524 Value: 0x4003E0, 525 Size: 0x0, 526 }, 527 Symbol{ 528 Name: "__gmon_start__", 529 Info: 0x20, 530 Other: 0x0, 531 Section: 0x0, 532 Value: 0x0, 533 Size: 0x0, 534 }, 535 Symbol{ 536 Name: "_Jv_RegisterClasses", 537 Info: 0x20, 538 Other: 0x0, 539 Section: 0x0, 540 Value: 0x0, 541 Size: 0x0, 542 }, 543 Symbol{ 544 Name: "puts@@GLIBC_2.2.5", 545 Info: 0x12, 546 Other: 0x0, 547 Section: 0x0, 548 Value: 0x0, 549 Size: 0x18C, 550 }, 551 Symbol{ 552 Name: "_fini", 553 Info: 0x12, 554 Other: 0x0, 555 Section: 0xE, 556 Value: 0x400594, 557 Size: 0x0, 558 }, 559 Symbol{ 560 Name: "__libc_start_main@@GLIBC_2.2.5", 561 Info: 0x12, 562 Other: 0x0, 563 Section: 0x0, 564 Value: 0x0, 565 Size: 0x1C2, 566 }, 567 Symbol{ 568 Name: "_IO_stdin_used", 569 Info: 0x11, 570 Other: 0x0, 571 Section: 0xF, 572 Value: 0x4005A4, 573 Size: 0x4, 574 }, 575 Symbol{ 576 Name: "__data_start", 577 Info: 0x10, 578 Other: 0x0, 579 Section: 0x18, 580 Value: 0x600880, 581 Size: 0x0, 582 }, 583 Symbol{ 584 Name: "__dso_handle", 585 Info: 0x11, 586 Other: 0x2, 587 Section: 0x18, 588 Value: 0x600888, 589 Size: 0x0, 590 }, 591 Symbol{ 592 Name: "__libc_csu_init", 593 Info: 0x12, 594 Other: 0x0, 595 Section: 0xD, 596 Value: 0x4004D0, 597 Size: 0x89, 598 }, 599 Symbol{ 600 Name: "__bss_start", 601 Info: 0x10, 602 Other: 0x0, 603 Section: 0xFFF1, 604 Value: 0x600898, 605 Size: 0x0, 606 }, 607 Symbol{ 608 Name: "_end", 609 Info: 0x10, 610 Other: 0x0, 611 Section: 0xFFF1, 612 Value: 0x6008A0, 613 Size: 0x0, 614 }, 615 Symbol{ 616 Name: "_edata", 617 Info: 0x10, 618 Other: 0x0, 619 Section: 0xFFF1, 620 Value: 0x600898, 621 Size: 0x0, 622 }, 623 Symbol{ 624 Name: "main", 625 Info: 0x12, 626 Other: 0x0, 627 Section: 0xD, 628 Value: 0x400498, 629 Size: 0x1B, 630 }, 631 Symbol{ 632 Name: "_init", 633 Info: 0x12, 634 Other: 0x0, 635 Section: 0xB, 636 Value: 0x400398, 637 Size: 0x0, 638 }, 639 }, 640 "testdata/go-relocation-test-clang-x86.obj": { 641 Symbol{ 642 Name: "go-relocation-test-clang.c", 643 Info: 0x4, 644 Other: 0x0, 645 Section: 0xFFF1, 646 Value: 0x0, 647 Size: 0x0, 648 }, 649 Symbol{ 650 Name: ".Linfo_string0", 651 Info: 0x0, 652 Other: 0x0, 653 Section: 0xC, 654 Value: 0x0, 655 Size: 0x0, 656 }, 657 Symbol{ 658 Name: ".Linfo_string1", 659 Info: 0x0, 660 Other: 0x0, 661 Section: 0xC, 662 Value: 0x2C, 663 Size: 0x0, 664 }, 665 Symbol{ 666 Name: ".Linfo_string2", 667 Info: 0x0, 668 Other: 0x0, 669 Section: 0xC, 670 Value: 0x47, 671 Size: 0x0, 672 }, 673 Symbol{ 674 Name: ".Linfo_string3", 675 Info: 0x0, 676 Other: 0x0, 677 Section: 0xC, 678 Value: 0x4C, 679 Size: 0x0, 680 }, 681 Symbol{ 682 Name: ".Linfo_string4", 683 Info: 0x0, 684 Other: 0x0, 685 Section: 0xC, 686 Value: 0x4E, 687 Size: 0x0, 688 }, 689 Symbol{ 690 Name: "", 691 Info: 0x3, 692 Other: 0x0, 693 Section: 0x1, 694 Value: 0x0, 695 Size: 0x0, 696 }, 697 Symbol{ 698 Name: "", 699 Info: 0x3, 700 Other: 0x0, 701 Section: 0x2, 702 Value: 0x0, 703 Size: 0x0, 704 }, 705 Symbol{ 706 Name: "", 707 Info: 0x3, 708 Other: 0x0, 709 Section: 0x3, 710 Value: 0x0, 711 Size: 0x0, 712 }, 713 Symbol{ 714 Name: "", 715 Info: 0x3, 716 Other: 0x0, 717 Section: 0x4, 718 Value: 0x0, 719 Size: 0x0, 720 }, 721 Symbol{ 722 Name: "", 723 Info: 0x3, 724 Other: 0x0, 725 Section: 0x6, 726 Value: 0x0, 727 Size: 0x0, 728 }, 729 Symbol{ 730 Name: "", 731 Info: 0x3, 732 Other: 0x0, 733 Section: 0x7, 734 Value: 0x0, 735 Size: 0x0, 736 }, 737 Symbol{ 738 Name: "", 739 Info: 0x3, 740 Other: 0x0, 741 Section: 0x8, 742 Value: 0x0, 743 Size: 0x0, 744 }, 745 Symbol{ 746 Name: "", 747 Info: 0x3, 748 Other: 0x0, 749 Section: 0xA, 750 Value: 0x0, 751 Size: 0x0, 752 }, 753 Symbol{ 754 Name: "", 755 Info: 0x3, 756 Other: 0x0, 757 Section: 0xC, 758 Value: 0x0, 759 Size: 0x0, 760 }, 761 Symbol{ 762 Name: "", 763 Info: 0x3, 764 Other: 0x0, 765 Section: 0xD, 766 Value: 0x0, 767 Size: 0x0, 768 }, 769 Symbol{ 770 Name: "", 771 Info: 0x3, 772 Other: 0x0, 773 Section: 0xE, 774 Value: 0x0, 775 Size: 0x0, 776 }, 777 Symbol{ 778 Name: "", 779 Info: 0x3, 780 Other: 0x0, 781 Section: 0xF, 782 Value: 0x0, 783 Size: 0x0, 784 }, 785 Symbol{ 786 Name: "", 787 Info: 0x3, 788 Other: 0x0, 789 Section: 0x10, 790 Value: 0x0, 791 Size: 0x0, 792 }, 793 Symbol{ 794 Name: "v", 795 Info: 0x11, 796 Other: 0x0, 797 Section: 0xFFF2, 798 Value: 0x4, 799 Size: 0x4, 800 }, 801 }, 802 "testdata/hello-world-core.gz": {}, 803 } 804 805 var dynamicSymbolsGolden = map[string][]Symbol{ 806 "testdata/gcc-amd64-linux-exec": { 807 Symbol{ 808 Name: "__gmon_start__", 809 Info: 0x20, 810 Other: 0x0, 811 Section: 0x0, 812 Value: 0x0, 813 Size: 0x0, 814 }, 815 Symbol{ 816 Name: "puts", 817 Info: 0x12, 818 Other: 0x0, 819 Section: 0x0, 820 Value: 0x0, 821 Size: 0x18C, 822 }, 823 Symbol{ 824 Name: "__libc_start_main", 825 Info: 0x12, 826 Other: 0x0, 827 Section: 0x0, 828 Value: 0x0, 829 Size: 0x1C2, 830 }, 831 }, 832 "testdata/go-relocation-test-clang-x86.obj": {}, 833 "testdata/hello-world-core.gz": {}, 834 } 835