MachO.h revision 263508
1//===- MachO.h - MachO object file implementation ---------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares the MachOObjectFile class, which implement the ObjectFile 11// interface for MachO files. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_OBJECT_MACHO_H 16#define LLVM_OBJECT_MACHO_H 17 18#include "llvm/ADT/ArrayRef.h" 19#include "llvm/ADT/SmallVector.h" 20#include "llvm/ADT/Triple.h" 21#include "llvm/Object/ObjectFile.h" 22#include "llvm/Support/MachO.h" 23 24namespace llvm { 25namespace object { 26 27/// DiceRef - This is a value type class that represents a single 28/// data in code entry in the table in a Mach-O object file. 29class DiceRef { 30 DataRefImpl DicePimpl; 31 const ObjectFile *OwningObject; 32 33public: 34 DiceRef() : OwningObject(NULL) { } 35 36 DiceRef(DataRefImpl DiceP, const ObjectFile *Owner); 37 38 bool operator==(const DiceRef &Other) const; 39 bool operator<(const DiceRef &Other) const; 40 41 error_code getNext(DiceRef &Result) const; 42 43 error_code getOffset(uint32_t &Result) const; 44 error_code getLength(uint16_t &Result) const; 45 error_code getKind(uint16_t &Result) const; 46 47 DataRefImpl getRawDataRefImpl() const; 48 const ObjectFile *getObjectFile() const; 49}; 50typedef content_iterator<DiceRef> dice_iterator; 51 52class MachOObjectFile : public ObjectFile { 53public: 54 struct LoadCommandInfo { 55 const char *Ptr; // Where in memory the load command is. 56 MachO::load_command C; // The command itself. 57 }; 58 59 MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits, 60 error_code &ec); 61 62 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; 63 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; 64 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; 65 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const; 66 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const; 67 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; 68 virtual error_code getSymbolType(DataRefImpl Symb, 69 SymbolRef::Type &Res) const; 70 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const; 71 virtual error_code getSymbolSection(DataRefImpl Symb, 72 section_iterator &Res) const; 73 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const; 74 75 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; 76 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; 77 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; 78 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; 79 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; 80 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; 81 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; 82 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; 83 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; 84 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, 85 bool &Res) const; 86 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const; 87 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const; 88 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const; 89 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 90 bool &Result) const; 91 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const; 92 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const; 93 94 virtual error_code getRelocationNext(DataRefImpl Rel, 95 RelocationRef &Res) const; 96 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const; 97 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const; 98 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const; 99 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const; 100 virtual error_code getRelocationTypeName(DataRefImpl Rel, 101 SmallVectorImpl<char> &Result) const; 102 virtual error_code getRelocationValueString(DataRefImpl Rel, 103 SmallVectorImpl<char> &Result) const; 104 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const; 105 106 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const; 107 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const; 108 109 // TODO: Would be useful to have an iterator based version 110 // of the load command interface too. 111 112 virtual symbol_iterator begin_symbols() const; 113 virtual symbol_iterator end_symbols() const; 114 115 virtual symbol_iterator begin_dynamic_symbols() const; 116 virtual symbol_iterator end_dynamic_symbols() const; 117 118 virtual section_iterator begin_sections() const; 119 virtual section_iterator end_sections() const; 120 121 virtual library_iterator begin_libraries_needed() const; 122 virtual library_iterator end_libraries_needed() const; 123 124 virtual uint8_t getBytesInAddress() const; 125 126 virtual StringRef getFileFormatName() const; 127 virtual unsigned getArch() const; 128 129 virtual StringRef getLoadName() const; 130 131 relocation_iterator section_rel_begin(unsigned Index) const; 132 relocation_iterator section_rel_end(unsigned Index) const; 133 134 dice_iterator begin_dices() const; 135 dice_iterator end_dices() const; 136 137 // In a MachO file, sections have a segment name. This is used in the .o 138 // files. They have a single segment, but this field specifies which segment 139 // a section should be put in in the final object. 140 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const; 141 142 // Names are stored as 16 bytes. These returns the raw 16 bytes without 143 // interpreting them as a C string. 144 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const; 145 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const; 146 147 // MachO specific Info about relocations. 148 bool isRelocationScattered(const MachO::any_relocation_info &RE) const; 149 unsigned getPlainRelocationSymbolNum( 150 const MachO::any_relocation_info &RE) const; 151 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const; 152 bool getScatteredRelocationScattered( 153 const MachO::any_relocation_info &RE) const; 154 uint32_t getScatteredRelocationValue( 155 const MachO::any_relocation_info &RE) const; 156 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const; 157 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const; 158 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const; 159 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const; 160 SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const; 161 162 // Walk load commands. 163 LoadCommandInfo getFirstLoadCommandInfo() const; 164 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const; 165 166 // MachO specific structures. 167 MachO::section getSection(DataRefImpl DRI) const; 168 MachO::section_64 getSection64(DataRefImpl DRI) const; 169 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const; 170 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const; 171 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const; 172 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const; 173 174 MachO::linkedit_data_command 175 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const; 176 MachO::segment_command 177 getSegmentLoadCommand(const LoadCommandInfo &L) const; 178 MachO::segment_command_64 179 getSegment64LoadCommand(const LoadCommandInfo &L) const; 180 MachO::linker_options_command 181 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const; 182 183 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const; 184 MachO::data_in_code_entry getDice(DataRefImpl Rel) const; 185 MachO::mach_header getHeader() const; 186 MachO::mach_header_64 getHeader64() const; 187 uint32_t 188 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, 189 unsigned Index) const; 190 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset, 191 unsigned Index) const; 192 MachO::symtab_command getSymtabLoadCommand() const; 193 MachO::dysymtab_command getDysymtabLoadCommand() const; 194 MachO::linkedit_data_command getDataInCodeLoadCommand() const; 195 196 StringRef getStringTableData() const; 197 bool is64Bit() const; 198 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const; 199 200 static Triple::ArchType getArch(uint32_t CPUType); 201 202 static bool classof(const Binary *v) { 203 return v->isMachO(); 204 } 205 206private: 207 typedef SmallVector<const char*, 1> SectionList; 208 SectionList Sections; 209 const char *SymtabLoadCmd; 210 const char *DysymtabLoadCmd; 211 const char *DataInCodeLoadCmd; 212}; 213 214/// DiceRef 215inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner) 216 : DicePimpl(DiceP) , OwningObject(Owner) {} 217 218inline bool DiceRef::operator==(const DiceRef &Other) const { 219 return DicePimpl == Other.DicePimpl; 220} 221 222inline bool DiceRef::operator<(const DiceRef &Other) const { 223 return DicePimpl < Other.DicePimpl; 224} 225 226inline error_code DiceRef::getNext(DiceRef &Result) const { 227 DataRefImpl Rel = DicePimpl; 228 const MachO::data_in_code_entry *P = 229 reinterpret_cast<const MachO::data_in_code_entry *>(Rel.p); 230 Rel.p = reinterpret_cast<uintptr_t>(P + 1); 231 Result = DiceRef(Rel, OwningObject); 232 return object_error::success; 233} 234 235// Since a Mach-O data in code reference, a DiceRef, can only be created when 236// the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for 237// the methods that get the values of the fields of the reference. 238 239inline error_code DiceRef::getOffset(uint32_t &Result) const { 240 const MachOObjectFile *MachOOF = 241 static_cast<const MachOObjectFile *>(OwningObject); 242 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl); 243 Result = Dice.offset; 244 return object_error::success; 245} 246 247inline error_code DiceRef::getLength(uint16_t &Result) const { 248 const MachOObjectFile *MachOOF = 249 static_cast<const MachOObjectFile *>(OwningObject); 250 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl); 251 Result = Dice.length; 252 return object_error::success; 253} 254 255inline error_code DiceRef::getKind(uint16_t &Result) const { 256 const MachOObjectFile *MachOOF = 257 static_cast<const MachOObjectFile *>(OwningObject); 258 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl); 259 Result = Dice.kind; 260 return object_error::success; 261} 262 263inline DataRefImpl DiceRef::getRawDataRefImpl() const { 264 return DicePimpl; 265} 266 267inline const ObjectFile *DiceRef::getObjectFile() const { 268 return OwningObject; 269} 270 271} 272} 273 274#endif 275 276