27 using namespace object;
246 uintptr_t CommandAddr =
reinterpret_cast<uintptr_t
>(L.
Ptr);
254 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec *
SectionSize;
255 return reinterpret_cast<const char*
>(SectionAddr);
264 const char *
P =
reinterpret_cast<const char *
>(DRI.
p);
265 return getStruct<nlist_base>(O,
P);
316 if ((ec = SI->getAddress(Addr)))
318 if (Addr != Val)
continue;
319 if ((ec = SI->getName(Name)))
334 if ((ec = SI->getAddress(Addr)))
336 if (Addr != Val)
continue;
337 if ((ec = SI->getName(Name)))
343 fmt <<
format(
"0x%x", Val);
409 return (RE.
r_word0 >> 24) & 0xf;
423 bool IsLittleEndian,
bool Is64bits,
425 :
ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
426 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
428 MachO::LoadCommandType SegmentLoadType =
is64Bit() ?
429 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
432 for (
unsigned I = 0; ; ++
I) {
433 if (Load.
C.
cmd == MachO::LC_SYMTAB) {
434 assert(!SymtabLoadCmd &&
"Multiple symbol tables");
435 SymtabLoadCmd = Load.
Ptr;
436 }
else if (Load.
C.
cmd == MachO::LC_DYSYMTAB) {
437 assert(!DysymtabLoadCmd &&
"Multiple dynamic symbol tables");
438 DysymtabLoadCmd = Load.
Ptr;
439 }
else if (Load.
C.
cmd == MachO::LC_DATA_IN_CODE) {
440 assert(!DataInCodeLoadCmd &&
"Multiple data in code tables");
441 DataInCodeLoadCmd = Load.
Ptr;
442 }
else if (Load.
C.
cmd == SegmentLoadType) {
444 for (
unsigned J = 0; J < NumSections; ++J) {
450 if (
I == LoadCommandCount - 1)
459 unsigned SymbolTableEntrySize =
is64Bit() ?
462 Symb.
p += SymbolTableEntrySize;
471 const char *Start = &StringTable.
data()[Entry.
n_strx];
477 uint64_t &Res)
const {
490 uint64_t &Res)
const {
512 uint32_t &Result)
const {
515 if (flags & SymbolRef::SF_Common) {
525 uint64_t &Result)
const {
526 uint64_t BeginOffset;
527 uint64_t EndOffset = 0;
528 uint8_t SectionIndex;
536 SectionIndex = Entry.
n_sect;
538 uint32_t flags = SymbolRef::SF_None;
540 if (flags & SymbolRef::SF_Common)
554 if (Entry.
n_sect == SectionIndex && Value > BeginOffset)
555 if (!EndOffset || Value < EndOffset)
561 Sec.
d.
a = SectionIndex-1;
566 Result = EndOffset - BeginOffset;
573 uint8_t n_type = Entry.
n_type;
595 uint32_t &Result)
const {
598 uint8_t MachOType = Entry.
n_type;
599 uint16_t MachOFlags = Entry.
n_desc;
602 Result = SymbolRef::SF_None;
605 Result |= SymbolRef::SF_Undefined;
608 Result |= SymbolRef::SF_FormatSpecific;
611 Result |= SymbolRef::SF_Global;
616 Result |= SymbolRef::SF_Common;
621 Result |= SymbolRef::SF_Weak;
624 Result |= SymbolRef::SF_Absolute;
633 uint8_t index = Entry.
n_sect;
647 uint64_t &Val)
const {
720 Res = uint64_t(1) <<
Align;
727 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
745 bool &Result)
const {
752 bool &Result)
const {
761 unsigned SectionType = Flags & MachO::SECTION_TYPE;
768 bool &Result)
const {
780 bool &Result)
const {
788 uint64_t SectBegin, SectEnd;
791 SectEnd += SectBegin;
795 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
811 Ret.
p =
reinterpret_cast<uintptr_t
>(
getPtr(
this, Offset));
833 Ret.
p =
reinterpret_cast<uintptr_t
>(P + Num);
841 Rel.
p =
reinterpret_cast<uintptr_t
>(P + 1);
852 uint64_t &Res)
const {
867 unsigned SymbolTableEntrySize =
is64Bit() ?
870 uint64_t Offset = S.
symoff + SymbolIdx * SymbolTableEntrySize;
872 Sym.
p =
reinterpret_cast<uintptr_t
>(
getPtr(
this, Offset));
877 uint64_t &Res)
const {
890 unsigned Arch = this->
getArch();
894 static const char *
const Table[] = {
895 "GENERIC_RELOC_VANILLA",
896 "GENERIC_RELOC_PAIR",
897 "GENERIC_RELOC_SECTDIFF",
898 "GENERIC_RELOC_PB_LA_PTR",
899 "GENERIC_RELOC_LOCAL_SECTDIFF",
900 "GENERIC_RELOC_TLV" };
909 static const char *
const Table[] = {
910 "X86_64_RELOC_UNSIGNED",
911 "X86_64_RELOC_SIGNED",
912 "X86_64_RELOC_BRANCH",
913 "X86_64_RELOC_GOT_LOAD",
915 "X86_64_RELOC_SUBTRACTOR",
916 "X86_64_RELOC_SIGNED_1",
917 "X86_64_RELOC_SIGNED_2",
918 "X86_64_RELOC_SIGNED_4",
919 "X86_64_RELOC_TLV" };
928 static const char *
const Table[] = {
931 "ARM_RELOC_SECTDIFF",
932 "ARM_RELOC_LOCAL_SECTDIFF",
933 "ARM_RELOC_PB_LA_PTR",
935 "ARM_THUMB_RELOC_BR22",
936 "ARM_THUMB_32BIT_BRANCH",
938 "ARM_RELOC_HALF_SECTDIFF" };
947 static const char *
const Table[] = {
956 "PPC_RELOC_SECTDIFF",
957 "PPC_RELOC_PB_LA_PTR",
958 "PPC_RELOC_HI16_SECTDIFF",
959 "PPC_RELOC_LO16_SECTDIFF",
960 "PPC_RELOC_HA16_SECTDIFF",
962 "PPC_RELOC_LO14_SECTDIFF",
963 "PPC_RELOC_LOCAL_SECTDIFF" };
981 unsigned Arch = this->
getArch();
1000 if (isPCRel) fmt <<
"PCREL";
1014 "X86_64_RELOC_SUBTRACTOR.");
1026 if (isPCRel) fmt <<
"P";
1062 "GENERIC_RELOC_SECTDIFF.");
1083 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1093 if (IsPCRel) fmt <<
"P";
1108 fmt <<
":upper16:(";
1110 fmt <<
":lower16:(";
1148 Result.
append(fmtbuf.begin(), fmtbuf.end());
1196 DRI.
p =
reinterpret_cast<uintptr_t
>(
getPtr(
this, Symtab.
symoff));
1206 unsigned SymbolTableEntrySize =
is64Bit() ?
1209 unsigned Offset = Symtab.
symoff +
1210 Symtab.
nsyms * SymbolTableEntrySize;
1211 DRI.
p =
reinterpret_cast<uintptr_t
>(
getPtr(
this, Offset));
1232 DRI.
d.
a = Sections.
size();
1255 return "Mach-O 32-bit i386";
1257 return "Mach-O arm";
1259 return "Mach-O 32-bit ppc";
1261 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
1262 "64-bit object file when we're not 64-bit?");
1263 return "Mach-O 32-bit unknown";
1268 assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1269 == llvm::MachO::CPU_ARCH_ABI64 &&
1270 "32-bit object file when we're 64-bit?");
1274 return "Mach-O 64-bit x86-64";
1276 return "Mach-O 64-bit ppc64";
1278 return "Mach-O 64-bit unknown";
1322 if (!DataInCodeLoadCmd)
1326 DRI.
p =
reinterpret_cast<uintptr_t
>(
getPtr(
this, DicLC.
dataoff));
1332 if (!DataInCodeLoadCmd)
1337 DRI.
p =
reinterpret_cast<uintptr_t
>(
getPtr(
this, Offset));
1379 return (RE.
r_word1 >> 27) & 1;
1440 Load.
C = getStruct<MachO::load_command>(
this, Load.
Ptr);
1448 Next.
C = getStruct<MachO::load_command>(
this, Next.
Ptr);
1453 return getStruct<MachO::section>(
this, Sections[DRI.
d.
a]);
1457 return getStruct<MachO::section_64>(
this, Sections[DRI.
d.
a]);
1461 unsigned Index)
const {
1463 return getStruct<MachO::section>(
this, Sec);
1467 unsigned Index)
const {
1469 return getStruct<MachO::section_64>(
this, Sec);
1474 const char *
P =
reinterpret_cast<const char *
>(DRI.
p);
1475 return getStruct<MachO::nlist>(
this,
P);
1480 const char *
P =
reinterpret_cast<const char *
>(DRI.
p);
1481 return getStruct<MachO::nlist_64>(
this,
P);
1486 return getStruct<MachO::linkedit_data_command>(
this, L.
Ptr);
1491 return getStruct<MachO::segment_command>(
this, L.
Ptr);
1496 return getStruct<MachO::segment_command_64>(
this, L.
Ptr);
1501 return getStruct<MachO::linker_options_command>(
this, L.
Ptr);
1506 const char *
P =
reinterpret_cast<const char *
>(Rel.
p);
1507 return getStruct<MachO::any_relocation_info>(
this,
P);
1512 const char *
P =
reinterpret_cast<const char *
>(Rel.
p);
1513 return getStruct<MachO::data_in_code_entry>(
this,
P);
1517 return getStruct<MachO::mach_header>(
this,
getPtr(
this, 0));
1521 return getStruct<MachO::mach_header_64>(
this,
getPtr(
this, 0));
1526 unsigned Index)
const {
1528 return getStruct<uint32_t>(
this,
getPtr(
this, Offset));
1533 unsigned Index)
const {
1535 return getStruct<MachO::data_in_code_entry>(
this,
getPtr(
this, Offset));
1539 return getStruct<MachO::symtab_command>(
this, SymtabLoadCmd);
1543 return getStruct<MachO::dysymtab_command>(
this, DysymtabLoadCmd);
1548 if (DataInCodeLoadCmd)
1549 return getStruct<MachO::linkedit_data_command>(
this, DataInCodeLoadCmd);
1553 Cmd.
cmd = MachO::LC_DATA_IN_CODE;
1574 uint32_t offset = Index;
1576 while (uint64_t delta = extractor.
getULEB128(&offset)) {
1586 if (Magic ==
"\xFE\xED\xFA\xCE")
1588 else if (Magic ==
"\xCE\xFA\xED\xFE")
1590 else if (Magic ==
"\xFE\xED\xFA\xCF")
1592 else if (Magic ==
"\xCF\xFA\xED\xFE")
static uint32_t getSectionFlags(const MachOObjectFile *O, DataRefImpl Sec)
COFF::RelocationTypeX86 Type
void push_back(const T &Elt)
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const
static unsigned int getMachOType(bool isLE, bool is64Bits)
dice_iterator end_dices() const
ArrayRef< char > getSectionRawFinalSegmentName(DataRefImpl Sec) const
StringRef getStringTableData() const
static unsigned getPlainRelocationLength(const MachOObjectFile *O, const MachO::any_relocation_info &RE)
dice_iterator begin_dices() const
virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, bool &Res) const
StringRef substr(size_t Start, size_t N=npos) const
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const
virtual error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const
StringRef getBuffer() const
static uint32_t getPlainRelocationAddress(const MachO::any_relocation_info &RE)
static unsigned getCPUType(const MachOObjectFile *O)
MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
MachO::symtab_command getSymtabLoadCommand() const
static bool getPlainRelocationPCRel(const MachOObjectFile *O, const MachO::any_relocation_info &RE)
static unsigned getPlainRelocationType(const MachOObjectFile *O, const MachO::any_relocation_info &RE)
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const
MachO::linker_options_command getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const
virtual symbol_iterator begin_dynamic_symbols() const
virtual StringRef getFileFormatName() const
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
static const char * getPtr(const MachOObjectFile *O, size_t Offset)
virtual section_iterator end_sections() const
static const char * getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L, unsigned Sec)
virtual library_iterator end_libraries_needed() const
static void SwapValue(T &Value)
LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const
static error_code advance(T &it, size_t Val)
virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const
static uint32_t getSegmentLoadCommandNumSections(const MachOObjectFile *O, const MachOObjectFile::LoadCommandInfo &L)
MachO::linkedit_data_command getLinkeditDataLoadCommand(const LoadCommandInfo &L) const
StringRef getData() const
virtual section_iterator begin_sections() const
MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const
MachO::mach_header getHeader() const
format_object1< T > format(const char *Fmt, const T &Val)
virtual StringRef getLoadName() const
static const bool IsLittleEndianHost
SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const
virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const
const char * data() const
bool getScatteredRelocationScattered(const MachO::any_relocation_info &RE) const
virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const
virtual library_iterator begin_libraries_needed() const
virtual error_code getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
MachO::section_64 getSection64(DataRefImpl DRI) const
static unsigned getScatteredRelocationLength(const MachO::any_relocation_info &RE)
virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const
static void advanceTo(T &it, size_t Val)
virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const
content_iterator< SectionRef > section_iterator
virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const
static uint8_t GET_COMM_ALIGN(uint16_t n_desc)
unsigned int getType() const
MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset, unsigned Index) const
static StringRef parseSegmentOrSectionName(const char *P)
MachO::section getSection(DataRefImpl DRI) const
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
content_iterator< SymbolRef > symbol_iterator
virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const
MachO::mach_header_64 getHeader64() const
unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const
virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const
virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const
virtual error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const
MachO::dysymtab_command getDysymtabLoadCommand() const
virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const
void append(in_iter in_start, in_iter in_end)
content_iterator< RelocationRef > relocation_iterator
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const
static nlist_base getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI)
static void printRelocationTargetName(const MachOObjectFile *O, const MachO::any_relocation_info &RE, raw_string_ostream &fmt)
MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits, error_code &ec)
static const char *const Magic
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const
virtual unsigned getArch() const
content_iterator & increment(error_code &err)
MachO::linkedit_data_command getDataInCodeLoadCommand() const
virtual symbol_iterator begin_symbols() const
friend class RelocationRef
virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const
content_iterator< DiceRef > dice_iterator
virtual symbol_iterator end_symbols() const
virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const
unsigned char SwapByteOrder(unsigned char C)
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
const uint64_t UnknownAddressOrSize
virtual uint8_t getBytesInAddress() const
The number of bytes used to represent an address in this object file format.
struct llvm::object::DataRefImpl::@77 d
static unsigned getScatteredRelocationAddress(const MachO::any_relocation_info &RE)
std::string message() const
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
virtual symbol_iterator end_dynamic_symbols() const
LoadCommandInfo getFirstLoadCommandInfo() const
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, bool &Result) const
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const
static unsigned getScatteredRelocationType(const MachO::any_relocation_info &RE)
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const
virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const
virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const
unsigned getPlainRelocationSymbolNum(const MachO::any_relocation_info &RE) const
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const
virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const
bool isLittleEndian() const
virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const
static bool getScatteredRelocationPCRel(const MachOObjectFile *O, const MachO::any_relocation_info &RE)
MachO::data_in_code_entry getDice(DataRefImpl Rel) const
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
LLVM Value Representation.
MachO::segment_command getSegmentLoadCommand(const LoadCommandInfo &L) const
MachO::segment_command_64 getSegment64LoadCommand(const LoadCommandInfo &L) const
ArrayRef< char > getSectionRawName(DataRefImpl Sec) const
virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
StringRef getSectionFinalSegmentName(DataRefImpl Sec) const
void ReadULEB128s(uint64_t Index, SmallVectorImpl< uint64_t > &Out) const
StringRef slice(size_t Start, size_t End) const
virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const
static void SwapStruct(T &Value)
virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const
virtual error_code getRelocationValueString(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
T getStruct(const MachOObjectFile *O, const char *P)
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const
static ObjectFile * createMachOObjectFile(MemoryBuffer *Object)