/usr/include/llvm-3.4/llvm/Object/ELFObjectFile.h is in llvm-3.4-dev 1:3.4-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 | //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the ELFObjectFile template class.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_OBJECT_ELF_OBJECT_FILE_H
#define LLVM_OBJECT_ELF_OBJECT_FILE_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Object/ELF.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ELF.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cctype>
#include <limits>
#include <utility>
namespace llvm {
namespace object {
template <class ELFT>
class ELFObjectFile : public ObjectFile {
public:
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
protected:
ELFFile<ELFT> EF;
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
virtual error_code getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Res) const;
virtual error_code getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const;
virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
bool &Res) const;
virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
bool &Result) const;
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const;
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const;
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
virtual error_code getRelocationNext(DataRefImpl Rel,
RelocationRef &Res) const;
virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
virtual error_code getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const;
virtual error_code
getRelocationValueString(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const;
uint64_t getROffset(DataRefImpl Rel) const;
StringRef getRelocationTypeName(uint32_t Type) const;
/// \brief Get the relocation section that contains \a Rel.
const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
return EF.getSection(Rel.d.a);
}
const Elf_Rel *getRel(DataRefImpl Rel) const;
const Elf_Rela *getRela(DataRefImpl Rela) const;
Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
bool IsDynamic = Symb.p & 1;
if (IsDynamic)
return Elf_Sym_Iter(
EF.begin_dynamic_symbols().getEntSize(),
reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
reinterpret_cast<const char *>(Symb.p), IsDynamic);
}
DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
DataRefImpl DRI;
DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
static_cast<uintptr_t>(Symb.isDynamic());
return DRI;
}
Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
reinterpret_cast<const char *>(Sec.p));
}
DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
DataRefImpl DRI;
DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
return DRI;
}
DataRefImpl toDRI(const Elf_Shdr *Sec) const {
DataRefImpl DRI;
DRI.p = reinterpret_cast<uintptr_t>(Sec);
return DRI;
}
Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
reinterpret_cast<const char *>(Dyn.p));
}
DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
DataRefImpl DRI;
DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
return DRI;
}
// This flag is used for classof, to distinguish ELFObjectFile from
// its subclass. If more subclasses will be created, this flag will
// have to become an enum.
bool isDyldELFObject;
public:
ELFObjectFile(MemoryBuffer *Object, error_code &ec);
const Elf_Sym *getSymbol(DataRefImpl Symb) const;
virtual symbol_iterator begin_symbols() const;
virtual symbol_iterator end_symbols() const;
virtual symbol_iterator begin_dynamic_symbols() const;
virtual symbol_iterator end_dynamic_symbols() const;
virtual section_iterator begin_sections() const;
virtual section_iterator end_sections() const;
virtual library_iterator begin_libraries_needed() const;
virtual library_iterator end_libraries_needed() const;
error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
bool &IsDefault) const;
virtual uint8_t getBytesInAddress() const;
virtual StringRef getFileFormatName() const;
virtual StringRef getObjectType() const { return "ELF"; }
virtual unsigned getArch() const;
virtual StringRef getLoadName() const;
const ELFFile<ELFT> *getELFFile() const { return &EF; }
bool isDyldType() const { return isDyldELFObject; }
static inline bool classof(const Binary *v) {
return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
ELFT::Is64Bits);
}
};
// Use an alignment of 2 for the typedefs since that is the worst case for
// ELF files in archives.
typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
SymbolRef &Result) const {
Result = SymbolRef(toDRI(++toELFSymIter(Symb)), this);
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
StringRef &Result) const {
ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
if (!Name)
return Name;
Result = *Name;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
StringRef &Version,
bool &IsDefault) const {
DataRefImpl Symb = SymRef.getRawDataRefImpl();
const Elf_Sym *symb = getSymbol(Symb);
ErrorOr<StringRef> Ver =
EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
if (!Ver)
return Ver;
Version = *Ver;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
uint64_t &Result) const {
const Elf_Sym *ESym = getSymbol(Symb);
const Elf_Shdr *ESec;
switch (EF.getSymbolTableIndex(ESym)) {
case ELF::SHN_COMMON:
// Unintialized symbols have no offset in the object file
case ELF::SHN_UNDEF:
Result = UnknownAddressOrSize;
return object_error::success;
case ELF::SHN_ABS:
Result = ESym->st_value;
return object_error::success;
default:
ESec = EF.getSection(ESym);
}
switch (ESym->getType()) {
case ELF::STT_SECTION:
Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
return object_error::success;
case ELF::STT_FUNC:
case ELF::STT_OBJECT:
case ELF::STT_NOTYPE:
Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
return object_error::success;
default:
Result = UnknownAddressOrSize;
return object_error::success;
}
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
uint64_t &Result) const {
const Elf_Sym *ESym = getSymbol(Symb);
const Elf_Shdr *ESec;
switch (EF.getSymbolTableIndex(ESym)) {
case ELF::SHN_COMMON:
case ELF::SHN_UNDEF:
Result = UnknownAddressOrSize;
return object_error::success;
case ELF::SHN_ABS:
Result = ESym->st_value;
return object_error::success;
default:
ESec = EF.getSection(ESym);
}
switch (ESym->getType()) {
case ELF::STT_SECTION:
Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
return object_error::success;
case ELF::STT_FUNC:
case ELF::STT_OBJECT:
case ELF::STT_NOTYPE:
bool IsRelocatable;
switch (EF.getHeader()->e_type) {
case ELF::ET_EXEC:
case ELF::ET_DYN:
IsRelocatable = false;
break;
default:
IsRelocatable = true;
}
Result = ESym->st_value;
// Clear the ARM/Thumb indicator flag.
if (EF.getHeader()->e_machine == ELF::EM_ARM)
Result &= ~1;
if (IsRelocatable && ESec != 0)
Result += ESec->sh_addr;
return object_error::success;
default:
Result = UnknownAddressOrSize;
return object_error::success;
}
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
uint32_t &Res) const {
Elf_Sym_Iter Sym = toELFSymIter(Symb);
if (Sym->st_shndx == ELF::SHN_COMMON)
Res = Sym->st_value;
else
Res = 0;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
uint64_t &Result) const {
Result = toELFSymIter(Symb)->st_size;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Result) const {
const Elf_Sym *ESym = getSymbol(Symb);
switch (ESym->getType()) {
case ELF::STT_NOTYPE:
Result = SymbolRef::ST_Unknown;
break;
case ELF::STT_SECTION:
Result = SymbolRef::ST_Debug;
break;
case ELF::STT_FILE:
Result = SymbolRef::ST_File;
break;
case ELF::STT_FUNC:
Result = SymbolRef::ST_Function;
break;
case ELF::STT_OBJECT:
case ELF::STT_COMMON:
case ELF::STT_TLS:
Result = SymbolRef::ST_Data;
break;
default:
Result = SymbolRef::ST_Other;
break;
}
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
uint32_t &Result) const {
const Elf_Sym *ESym = getSymbol(Symb);
Result = SymbolRef::SF_None;
if (ESym->getBinding() != ELF::STB_LOCAL)
Result |= SymbolRef::SF_Global;
if (ESym->getBinding() == ELF::STB_WEAK)
Result |= SymbolRef::SF_Weak;
if (ESym->st_shndx == ELF::SHN_ABS)
Result |= SymbolRef::SF_Absolute;
if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
ESym == &*EF.begin_symbols())
Result |= SymbolRef::SF_FormatSpecific;
if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
Result |= SymbolRef::SF_Undefined;
if (ESym->getType() == ELF::STT_COMMON ||
EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
Result |= SymbolRef::SF_Common;
if (ESym->getType() == ELF::STT_TLS)
Result |= SymbolRef::SF_ThreadLocal;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const {
const Elf_Sym *ESym = getSymbol(Symb);
const Elf_Shdr *ESec = EF.getSection(ESym);
if (!ESec)
Res = end_sections();
else {
DataRefImpl Sec;
Sec.p = reinterpret_cast<intptr_t>(ESec);
Res = section_iterator(SectionRef(Sec, this));
}
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
uint64_t &Val) const {
const Elf_Sym *ESym = getSymbol(Symb);
Val = ESym->st_value;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
SectionRef &Result) const {
Result = SectionRef(toDRI(++toELFShdrIter(Sec)), this);
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
StringRef &Result) const {
ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
if (!Name)
return Name;
Result = *Name;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
uint64_t &Result) const {
Result = toELFShdrIter(Sec)->sh_addr;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
uint64_t &Result) const {
Result = toELFShdrIter(Sec)->sh_size;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
StringRef &Result) const {
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
uint64_t &Result) const {
Result = toELFShdrIter(Sec)->sh_addralign;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
bool &Result) const {
Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
bool &Result) const {
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
EShdr->sh_type == ELF::SHT_PROGBITS;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
bool &Result) const {
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
EShdr->sh_type == ELF::SHT_NOBITS;
return object_error::success;
}
template <class ELFT>
error_code
ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
bool &Result) const {
Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
bool &Result) const {
Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
bool &Result) const {
Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
bool &Result) const {
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
DataRefImpl Symb,
bool &Result) const {
Elf_Sym_Iter ESym = toELFSymIter(Symb);
uintX_t Index = ESym->st_shndx;
bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
return object_error::success;
}
template <class ELFT>
relocation_iterator
ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
DataRefImpl RelData;
uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
RelData.d.b = 0;
return relocation_iterator(RelocationRef(RelData, this));
}
template <class ELFT>
relocation_iterator
ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
DataRefImpl RelData;
uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
RelData.d.b = 0;
else
RelData.d.b = S->sh_size / S->sh_entsize;
return relocation_iterator(RelocationRef(RelData, this));
}
template <class ELFT>
section_iterator
ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
if (EF.getHeader()->e_type != ELF::ET_REL)
return end_sections();
Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
uintX_t Type = EShdr->sh_type;
if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
return end_sections();
const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
return section_iterator(SectionRef(toDRI(R), this));
}
// Relocations
template <class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
RelocationRef &Result) const {
++Rel.d.b;
Result = RelocationRef(Rel, this);
return object_error::success;
}
template <class ELFT>
symbol_iterator
ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
uint32_t symbolIdx;
const Elf_Shdr *sec = getRelSection(Rel);
switch (sec->sh_type) {
default:
report_fatal_error("Invalid section type in Rel!");
case ELF::SHT_REL: {
symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
break;
}
case ELF::SHT_RELA: {
symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
break;
}
}
if (!symbolIdx)
return end_symbols();
const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
DataRefImpl SymbolData;
switch (SymSec->sh_type) {
default:
report_fatal_error("Invalid symbol table section type!");
case ELF::SHT_SYMTAB:
SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
break;
case ELF::SHT_DYNSYM:
SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
break;
}
return symbol_iterator(SymbolRef(SymbolData, this));
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
uint64_t &Result) const {
Result = getROffset(Rel);
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
uint64_t &Result) const {
Result = getROffset(Rel);
return object_error::success;
}
template <class ELFT>
uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
const Elf_Shdr *sec = getRelSection(Rel);
switch (sec->sh_type) {
default:
report_fatal_error("Invalid section type in Rel!");
case ELF::SHT_REL:
return getRel(Rel)->r_offset;
case ELF::SHT_RELA:
return getRela(Rel)->r_offset;
}
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
uint64_t &Result) const {
const Elf_Shdr *sec = getRelSection(Rel);
switch (sec->sh_type) {
default:
report_fatal_error("Invalid section type in Rel!");
case ELF::SHT_REL: {
Result = getRel(Rel)->getType(EF.isMips64EL());
break;
}
case ELF::SHT_RELA: {
Result = getRela(Rel)->getType(EF.isMips64EL());
break;
}
}
return object_error::success;
}
template <class ELFT>
StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationTypeName(
DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
const Elf_Shdr *sec = getRelSection(Rel);
uint32_t type;
switch (sec->sh_type) {
default:
return object_error::parse_failed;
case ELF::SHT_REL: {
type = getRel(Rel)->getType(EF.isMips64EL());
break;
}
case ELF::SHT_RELA: {
type = getRela(Rel)->getType(EF.isMips64EL());
break;
}
}
EF.getRelocationTypeName(type, Result);
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
int64_t &Result) const {
const Elf_Shdr *sec = getRelSection(Rel);
switch (sec->sh_type) {
default:
report_fatal_error("Invalid section type in Rel!");
case ELF::SHT_REL: {
Result = 0;
return object_error::success;
}
case ELF::SHT_RELA: {
Result = getRela(Rel)->r_addend;
return object_error::success;
}
}
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getRelocationValueString(
DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
const Elf_Shdr *sec = getRelSection(Rel);
uint8_t type;
StringRef res;
int64_t addend = 0;
uint16_t symbol_index = 0;
switch (sec->sh_type) {
default:
return object_error::parse_failed;
case ELF::SHT_REL: {
type = getRel(Rel)->getType(EF.isMips64EL());
symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
// TODO: Read implicit addend from section data.
break;
}
case ELF::SHT_RELA: {
type = getRela(Rel)->getType(EF.isMips64EL());
symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
addend = getRela(Rel)->r_addend;
break;
}
}
const Elf_Sym *symb =
EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
ErrorOr<StringRef> SymName =
EF.getSymbolName(EF.getSection(sec->sh_link), symb);
if (!SymName)
return SymName;
switch (EF.getHeader()->e_machine) {
case ELF::EM_X86_64:
switch (type) {
case ELF::R_X86_64_PC8:
case ELF::R_X86_64_PC16:
case ELF::R_X86_64_PC32: {
std::string fmtbuf;
raw_string_ostream fmt(fmtbuf);
fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
fmt.flush();
Result.append(fmtbuf.begin(), fmtbuf.end());
} break;
case ELF::R_X86_64_8:
case ELF::R_X86_64_16:
case ELF::R_X86_64_32:
case ELF::R_X86_64_32S:
case ELF::R_X86_64_64: {
std::string fmtbuf;
raw_string_ostream fmt(fmtbuf);
fmt << *SymName << (addend < 0 ? "" : "+") << addend;
fmt.flush();
Result.append(fmtbuf.begin(), fmtbuf.end());
} break;
default:
res = "Unknown";
}
break;
case ELF::EM_AARCH64: {
std::string fmtbuf;
raw_string_ostream fmt(fmtbuf);
fmt << *SymName;
if (addend != 0)
fmt << (addend < 0 ? "" : "+") << addend;
fmt.flush();
Result.append(fmtbuf.begin(), fmtbuf.end());
break;
}
case ELF::EM_ARM:
case ELF::EM_HEXAGON:
res = *SymName;
break;
default:
res = "Unknown";
}
if (Result.empty())
Result.append(res.begin(), res.end());
return object_error::success;
}
template <class ELFT>
const typename ELFFile<ELFT>::Elf_Sym *
ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
return &*toELFSymIter(Symb);
}
template <class ELFT>
const typename ELFObjectFile<ELFT>::Elf_Rel *
ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
}
template <class ELFT>
const typename ELFObjectFile<ELFT>::Elf_Rela *
ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
}
template <class ELFT>
ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
: ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
support::little,
ELFT::Is64Bits),
Object),
EF(Object, ec) {}
template <class ELFT>
symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
return symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
}
template <class ELFT>
symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
return symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
}
template <class ELFT>
symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
}
template <class ELFT>
symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
}
template <class ELFT>
section_iterator ELFObjectFile<ELFT>::begin_sections() const {
return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
}
template <class ELFT>
section_iterator ELFObjectFile<ELFT>::end_sections() const {
return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
}
template <class ELFT>
StringRef ELFObjectFile<ELFT>::getLoadName() const {
Elf_Dyn_Iter DI = EF.begin_dynamic_table();
Elf_Dyn_Iter DE = EF.end_dynamic_table();
while (DI != DE && DI->getTag() != ELF::DT_SONAME)
++DI;
if (DI != DE)
return EF.getDynamicString(DI->getVal());
return "";
}
template <class ELFT>
library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
Elf_Dyn_Iter DI = EF.begin_dynamic_table();
Elf_Dyn_Iter DE = EF.end_dynamic_table();
while (DI != DE && DI->getTag() != ELF::DT_SONAME)
++DI;
return library_iterator(LibraryRef(toDRI(DI), this));
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
LibraryRef &Result) const {
Elf_Dyn_Iter DI = toELFDynIter(Data);
Elf_Dyn_Iter DE = EF.end_dynamic_table();
// Skip to the next DT_NEEDED entry.
do
++DI;
while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
Result = LibraryRef(toDRI(DI), this);
return object_error::success;
}
template <class ELFT>
error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
StringRef &Res) const {
Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
return object_error::success;
}
template <class ELFT>
library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
}
template <class ELFT>
uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
return ELFT::Is64Bits ? 8 : 4;
}
template <class ELFT>
StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
case ELF::ELFCLASS32:
switch (EF.getHeader()->e_machine) {
case ELF::EM_386:
return "ELF32-i386";
case ELF::EM_X86_64:
return "ELF32-x86-64";
case ELF::EM_ARM:
return "ELF32-arm";
case ELF::EM_HEXAGON:
return "ELF32-hexagon";
case ELF::EM_MIPS:
return "ELF32-mips";
case ELF::EM_PPC:
return "ELF32-ppc";
default:
return "ELF32-unknown";
}
case ELF::ELFCLASS64:
switch (EF.getHeader()->e_machine) {
case ELF::EM_386:
return "ELF64-i386";
case ELF::EM_X86_64:
return "ELF64-x86-64";
case ELF::EM_AARCH64:
return "ELF64-aarch64";
case ELF::EM_PPC64:
return "ELF64-ppc64";
case ELF::EM_S390:
return "ELF64-s390";
default:
return "ELF64-unknown";
}
default:
// FIXME: Proper error handling.
report_fatal_error("Invalid ELFCLASS!");
}
}
template <class ELFT>
unsigned ELFObjectFile<ELFT>::getArch() const {
switch (EF.getHeader()->e_machine) {
case ELF::EM_386:
return Triple::x86;
case ELF::EM_X86_64:
return Triple::x86_64;
case ELF::EM_AARCH64:
return Triple::aarch64;
case ELF::EM_ARM:
return Triple::arm;
case ELF::EM_HEXAGON:
return Triple::hexagon;
case ELF::EM_MIPS:
return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
: Triple::mips;
case ELF::EM_PPC64:
return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
: Triple::ppc64;
case ELF::EM_S390:
return Triple::systemz;
default:
return Triple::UnknownArch;
}
}
/// FIXME: Maybe we should have a base ElfObjectFile that is not a template
/// and make these member functions?
static inline error_code getELFRelocationAddend(const RelocationRef R,
int64_t &Addend) {
const ObjectFile *Obj = R.getObjectFile();
DataRefImpl DRI = R.getRawDataRefImpl();
// Little-endian 32-bit
if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
// Big-endian 32-bit
if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
// Little-endian 64-bit
if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
// Big-endian 64-bit
if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
return ELFObj->getRelocationAddend(DRI, Addend);
llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
}
/// This is a generic interface for retrieving GNU symbol version
/// information from an ELFObjectFile.
static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
const SymbolRef &Sym,
StringRef &Version,
bool &IsDefault) {
// Little-endian 32-bit
if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
// Big-endian 32-bit
if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
// Little-endian 64-bit
if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
// Big-endian 64-bit
if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
}
}
}
#endif
|