MachOObjectFile.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  1. //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- 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 defines the MachOObjectFile class, which binds the MachOObject
  11. // class to the generic ObjectFile wrapper.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Object/MachO.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/Support/DataExtractor.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/Format.h"
  21. #include "llvm/Support/Host.h"
  22. #include "llvm/Support/LEB128.h"
  23. #include "llvm/Support/MachO.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <cctype>
  27. #include <cstring>
  28. #include <limits>
  29. using namespace llvm;
  30. using namespace object;
  31. namespace {
  32. struct section_base {
  33. char sectname[16];
  34. char segname[16];
  35. };
  36. }
  37. // FIXME: Replace all uses of this function with getStructOrErr.
  38. template <typename T>
  39. static T getStruct(const MachOObjectFile *O, const char *P) {
  40. // Don't read before the beginning or past the end of the file
  41. if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
  42. report_fatal_error("Malformed MachO file.");
  43. T Cmd;
  44. memcpy(&Cmd, P, sizeof(T));
  45. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  46. MachO::swapStruct(Cmd);
  47. return Cmd;
  48. }
  49. template <typename T>
  50. static ErrorOr<T> getStructOrErr(const MachOObjectFile *O, const char *P) {
  51. // Don't read before the beginning or past the end of the file
  52. if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
  53. return object_error::parse_failed;
  54. T Cmd;
  55. memcpy(&Cmd, P, sizeof(T));
  56. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  57. MachO::swapStruct(Cmd);
  58. return Cmd;
  59. }
  60. static const char *
  61. getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
  62. unsigned Sec) {
  63. uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
  64. bool Is64 = O->is64Bit();
  65. unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
  66. sizeof(MachO::segment_command);
  67. unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
  68. sizeof(MachO::section);
  69. uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
  70. return reinterpret_cast<const char*>(SectionAddr);
  71. }
  72. static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
  73. return O->getData().substr(Offset, 1).data();
  74. }
  75. static MachO::nlist_base
  76. getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
  77. const char *P = reinterpret_cast<const char *>(DRI.p);
  78. return getStruct<MachO::nlist_base>(O, P);
  79. }
  80. static StringRef parseSegmentOrSectionName(const char *P) {
  81. if (P[15] == 0)
  82. // Null terminated.
  83. return P;
  84. // Not null terminated, so this is a 16 char string.
  85. return StringRef(P, 16);
  86. }
  87. // Helper to advance a section or symbol iterator multiple increments at a time.
  88. template<class T>
  89. static void advance(T &it, size_t Val) {
  90. while (Val--)
  91. ++it;
  92. }
  93. static unsigned getCPUType(const MachOObjectFile *O) {
  94. return O->getHeader().cputype;
  95. }
  96. static uint32_t
  97. getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
  98. return RE.r_word0;
  99. }
  100. static unsigned
  101. getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
  102. return RE.r_word0 & 0xffffff;
  103. }
  104. static bool getPlainRelocationPCRel(const MachOObjectFile *O,
  105. const MachO::any_relocation_info &RE) {
  106. if (O->isLittleEndian())
  107. return (RE.r_word1 >> 24) & 1;
  108. return (RE.r_word1 >> 7) & 1;
  109. }
  110. static bool
  111. getScatteredRelocationPCRel(const MachOObjectFile *O,
  112. const MachO::any_relocation_info &RE) {
  113. return (RE.r_word0 >> 30) & 1;
  114. }
  115. static unsigned getPlainRelocationLength(const MachOObjectFile *O,
  116. const MachO::any_relocation_info &RE) {
  117. if (O->isLittleEndian())
  118. return (RE.r_word1 >> 25) & 3;
  119. return (RE.r_word1 >> 5) & 3;
  120. }
  121. static unsigned
  122. getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
  123. return (RE.r_word0 >> 28) & 3;
  124. }
  125. static unsigned getPlainRelocationType(const MachOObjectFile *O,
  126. const MachO::any_relocation_info &RE) {
  127. if (O->isLittleEndian())
  128. return RE.r_word1 >> 28;
  129. return RE.r_word1 & 0xf;
  130. }
  131. static uint32_t getSectionFlags(const MachOObjectFile *O,
  132. DataRefImpl Sec) {
  133. if (O->is64Bit()) {
  134. MachO::section_64 Sect = O->getSection64(Sec);
  135. return Sect.flags;
  136. }
  137. MachO::section Sect = O->getSection(Sec);
  138. return Sect.flags;
  139. }
  140. static ErrorOr<MachOObjectFile::LoadCommandInfo>
  141. getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr) {
  142. auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr);
  143. if (!CmdOrErr)
  144. return CmdOrErr.getError();
  145. if (CmdOrErr->cmdsize < 8)
  146. return object_error::macho_small_load_command;
  147. MachOObjectFile::LoadCommandInfo Load;
  148. Load.Ptr = Ptr;
  149. Load.C = CmdOrErr.get();
  150. return Load;
  151. }
  152. static ErrorOr<MachOObjectFile::LoadCommandInfo>
  153. getFirstLoadCommandInfo(const MachOObjectFile *Obj) {
  154. unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
  155. : sizeof(MachO::mach_header);
  156. return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize));
  157. }
  158. static ErrorOr<MachOObjectFile::LoadCommandInfo>
  159. getNextLoadCommandInfo(const MachOObjectFile *Obj,
  160. const MachOObjectFile::LoadCommandInfo &L) {
  161. return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize);
  162. }
  163. template <typename T>
  164. static void parseHeader(const MachOObjectFile *Obj, T &Header,
  165. std::error_code &EC) {
  166. auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0));
  167. if (HeaderOrErr)
  168. Header = HeaderOrErr.get();
  169. else
  170. EC = HeaderOrErr.getError();
  171. }
  172. // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all
  173. // sections to \param Sections, and optionally sets
  174. // \param IsPageZeroSegment to true.
  175. template <typename SegmentCmd>
  176. static std::error_code parseSegmentLoadCommand(
  177. const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load,
  178. SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment) {
  179. const unsigned SegmentLoadSize = sizeof(SegmentCmd);
  180. if (Load.C.cmdsize < SegmentLoadSize)
  181. return object_error::macho_load_segment_too_small;
  182. auto SegOrErr = getStructOrErr<SegmentCmd>(Obj, Load.Ptr);
  183. if (!SegOrErr)
  184. return SegOrErr.getError();
  185. SegmentCmd S = SegOrErr.get();
  186. const unsigned SectionSize =
  187. Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section);
  188. if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
  189. S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize)
  190. return object_error::macho_load_segment_too_many_sections;
  191. for (unsigned J = 0; J < S.nsects; ++J) {
  192. const char *Sec = getSectionPtr(Obj, Load, J);
  193. Sections.push_back(Sec);
  194. }
  195. IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname);
  196. return std::error_code();
  197. }
  198. MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
  199. bool Is64bits, std::error_code &EC)
  200. : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
  201. SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
  202. DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr),
  203. DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr),
  204. HasPageZeroSegment(false) {
  205. if (is64Bit())
  206. parseHeader(this, Header64, EC);
  207. else
  208. parseHeader(this, Header, EC);
  209. if (EC)
  210. return;
  211. uint32_t LoadCommandCount = getHeader().ncmds;
  212. if (LoadCommandCount == 0)
  213. return;
  214. auto LoadOrErr = getFirstLoadCommandInfo(this);
  215. if (!LoadOrErr) {
  216. EC = LoadOrErr.getError();
  217. return;
  218. }
  219. LoadCommandInfo Load = LoadOrErr.get();
  220. for (unsigned I = 0; I < LoadCommandCount; ++I) {
  221. LoadCommands.push_back(Load);
  222. if (Load.C.cmd == MachO::LC_SYMTAB) {
  223. // Multiple symbol tables
  224. if (SymtabLoadCmd) {
  225. EC = object_error::parse_failed;
  226. return;
  227. }
  228. SymtabLoadCmd = Load.Ptr;
  229. } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
  230. // Multiple dynamic symbol tables
  231. if (DysymtabLoadCmd) {
  232. EC = object_error::parse_failed;
  233. return;
  234. }
  235. DysymtabLoadCmd = Load.Ptr;
  236. } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
  237. // Multiple data in code tables
  238. if (DataInCodeLoadCmd) {
  239. EC = object_error::parse_failed;
  240. return;
  241. }
  242. DataInCodeLoadCmd = Load.Ptr;
  243. } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
  244. // Multiple linker optimization hint tables
  245. if (LinkOptHintsLoadCmd) {
  246. EC = object_error::parse_failed;
  247. return;
  248. }
  249. LinkOptHintsLoadCmd = Load.Ptr;
  250. } else if (Load.C.cmd == MachO::LC_DYLD_INFO ||
  251. Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
  252. // Multiple dyldinfo load commands
  253. if (DyldInfoLoadCmd) {
  254. EC = object_error::parse_failed;
  255. return;
  256. }
  257. DyldInfoLoadCmd = Load.Ptr;
  258. } else if (Load.C.cmd == MachO::LC_UUID) {
  259. // Multiple UUID load commands
  260. if (UuidLoadCmd) {
  261. EC = object_error::parse_failed;
  262. return;
  263. }
  264. UuidLoadCmd = Load.Ptr;
  265. } else if (Load.C.cmd == MachO::LC_SEGMENT_64) {
  266. if ((EC = parseSegmentLoadCommand<MachO::segment_command_64>(
  267. this, Load, Sections, HasPageZeroSegment)))
  268. return;
  269. } else if (Load.C.cmd == MachO::LC_SEGMENT) {
  270. if ((EC = parseSegmentLoadCommand<MachO::segment_command>(
  271. this, Load, Sections, HasPageZeroSegment)))
  272. return;
  273. } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB ||
  274. Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
  275. Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
  276. Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
  277. Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
  278. Libraries.push_back(Load.Ptr);
  279. }
  280. if (I < LoadCommandCount - 1) {
  281. auto LoadOrErr = getNextLoadCommandInfo(this, Load);
  282. if (!LoadOrErr) {
  283. EC = LoadOrErr.getError();
  284. return;
  285. }
  286. Load = LoadOrErr.get();
  287. }
  288. }
  289. assert(LoadCommands.size() == LoadCommandCount);
  290. }
  291. void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
  292. unsigned SymbolTableEntrySize = is64Bit() ?
  293. sizeof(MachO::nlist_64) :
  294. sizeof(MachO::nlist);
  295. Symb.p += SymbolTableEntrySize;
  296. }
  297. ErrorOr<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
  298. StringRef StringTable = getStringTableData();
  299. MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
  300. const char *Start = &StringTable.data()[Entry.n_strx];
  301. if (Start < getData().begin() || Start >= getData().end())
  302. report_fatal_error(
  303. "Symbol name entry points before beginning or past end of file.");
  304. return StringRef(Start);
  305. }
  306. unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
  307. DataRefImpl DRI = Sec.getRawDataRefImpl();
  308. uint32_t Flags = getSectionFlags(this, DRI);
  309. return Flags & MachO::SECTION_TYPE;
  310. }
  311. uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const {
  312. if (is64Bit()) {
  313. MachO::nlist_64 Entry = getSymbol64TableEntry(Sym);
  314. return Entry.n_value;
  315. }
  316. MachO::nlist Entry = getSymbolTableEntry(Sym);
  317. return Entry.n_value;
  318. }
  319. // getIndirectName() returns the name of the alias'ed symbol who's string table
  320. // index is in the n_value field.
  321. std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
  322. StringRef &Res) const {
  323. StringRef StringTable = getStringTableData();
  324. MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
  325. if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
  326. return object_error::parse_failed;
  327. uint64_t NValue = getNValue(Symb);
  328. if (NValue >= StringTable.size())
  329. return object_error::parse_failed;
  330. const char *Start = &StringTable.data()[NValue];
  331. Res = StringRef(Start);
  332. return std::error_code();
  333. }
  334. uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
  335. return getNValue(Sym);
  336. }
  337. ErrorOr<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
  338. return getSymbolValue(Sym);
  339. }
  340. uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {
  341. uint32_t flags = getSymbolFlags(DRI);
  342. if (flags & SymbolRef::SF_Common) {
  343. MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
  344. return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
  345. }
  346. return 0;
  347. }
  348. uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const {
  349. return getNValue(DRI);
  350. }
  351. SymbolRef::Type MachOObjectFile::getSymbolType(DataRefImpl Symb) const {
  352. MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
  353. uint8_t n_type = Entry.n_type;
  354. // If this is a STAB debugging symbol, we can do nothing more.
  355. if (n_type & MachO::N_STAB)
  356. return SymbolRef::ST_Debug;
  357. switch (n_type & MachO::N_TYPE) {
  358. case MachO::N_UNDF :
  359. return SymbolRef::ST_Unknown;
  360. case MachO::N_SECT :
  361. return SymbolRef::ST_Function;
  362. }
  363. return SymbolRef::ST_Other;
  364. }
  365. uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
  366. MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
  367. uint8_t MachOType = Entry.n_type;
  368. uint16_t MachOFlags = Entry.n_desc;
  369. uint32_t Result = SymbolRef::SF_None;
  370. if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
  371. Result |= SymbolRef::SF_Indirect;
  372. if (MachOType & MachO::N_STAB)
  373. Result |= SymbolRef::SF_FormatSpecific;
  374. if (MachOType & MachO::N_EXT) {
  375. Result |= SymbolRef::SF_Global;
  376. if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
  377. if (getNValue(DRI))
  378. Result |= SymbolRef::SF_Common;
  379. else
  380. Result |= SymbolRef::SF_Undefined;
  381. }
  382. if (!(MachOType & MachO::N_PEXT))
  383. Result |= SymbolRef::SF_Exported;
  384. }
  385. if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
  386. Result |= SymbolRef::SF_Weak;
  387. if (MachOFlags & (MachO::N_ARM_THUMB_DEF))
  388. Result |= SymbolRef::SF_Thumb;
  389. if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
  390. Result |= SymbolRef::SF_Absolute;
  391. return Result;
  392. }
  393. std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
  394. section_iterator &Res) const {
  395. MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
  396. uint8_t index = Entry.n_sect;
  397. if (index == 0) {
  398. Res = section_end();
  399. } else {
  400. DataRefImpl DRI;
  401. DRI.d.a = index - 1;
  402. if (DRI.d.a >= Sections.size())
  403. report_fatal_error("getSymbolSection: Invalid section index.");
  404. Res = section_iterator(SectionRef(DRI, this));
  405. }
  406. return std::error_code();
  407. }
  408. unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {
  409. MachO::nlist_base Entry =
  410. getSymbolTableEntryBase(this, Sym.getRawDataRefImpl());
  411. return Entry.n_sect - 1;
  412. }
  413. void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
  414. Sec.d.a++;
  415. }
  416. std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
  417. StringRef &Result) const {
  418. ArrayRef<char> Raw = getSectionRawName(Sec);
  419. Result = parseSegmentOrSectionName(Raw.data());
  420. return std::error_code();
  421. }
  422. uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
  423. if (is64Bit())
  424. return getSection64(Sec).addr;
  425. return getSection(Sec).addr;
  426. }
  427. uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const {
  428. if (is64Bit())
  429. return getSection64(Sec).size;
  430. return getSection(Sec).size;
  431. }
  432. std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
  433. StringRef &Res) const {
  434. uint32_t Offset;
  435. uint64_t Size;
  436. if (is64Bit()) {
  437. MachO::section_64 Sect = getSection64(Sec);
  438. Offset = Sect.offset;
  439. Size = Sect.size;
  440. } else {
  441. MachO::section Sect = getSection(Sec);
  442. Offset = Sect.offset;
  443. Size = Sect.size;
  444. }
  445. Res = this->getData().substr(Offset, Size);
  446. return std::error_code();
  447. }
  448. uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
  449. uint32_t Align;
  450. if (is64Bit()) {
  451. MachO::section_64 Sect = getSection64(Sec);
  452. Align = Sect.align;
  453. } else {
  454. MachO::section Sect = getSection(Sec);
  455. Align = Sect.align;
  456. }
  457. return uint64_t(1) << Align;
  458. }
  459. bool MachOObjectFile::isSectionText(DataRefImpl Sec) const {
  460. uint32_t Flags = getSectionFlags(this, Sec);
  461. return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
  462. }
  463. bool MachOObjectFile::isSectionData(DataRefImpl Sec) const {
  464. uint32_t Flags = getSectionFlags(this, Sec);
  465. unsigned SectionType = Flags & MachO::SECTION_TYPE;
  466. return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
  467. !(SectionType == MachO::S_ZEROFILL ||
  468. SectionType == MachO::S_GB_ZEROFILL);
  469. }
  470. bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
  471. uint32_t Flags = getSectionFlags(this, Sec);
  472. unsigned SectionType = Flags & MachO::SECTION_TYPE;
  473. return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
  474. (SectionType == MachO::S_ZEROFILL ||
  475. SectionType == MachO::S_GB_ZEROFILL);
  476. }
  477. unsigned MachOObjectFile::getSectionID(SectionRef Sec) const {
  478. return Sec.getRawDataRefImpl().d.a;
  479. }
  480. bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
  481. // FIXME: Unimplemented.
  482. return false;
  483. }
  484. relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
  485. DataRefImpl Ret;
  486. Ret.d.a = Sec.d.a;
  487. Ret.d.b = 0;
  488. return relocation_iterator(RelocationRef(Ret, this));
  489. }
  490. relocation_iterator
  491. MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
  492. uint32_t Num;
  493. if (is64Bit()) {
  494. MachO::section_64 Sect = getSection64(Sec);
  495. Num = Sect.nreloc;
  496. } else {
  497. MachO::section Sect = getSection(Sec);
  498. Num = Sect.nreloc;
  499. }
  500. DataRefImpl Ret;
  501. Ret.d.a = Sec.d.a;
  502. Ret.d.b = Num;
  503. return relocation_iterator(RelocationRef(Ret, this));
  504. }
  505. void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
  506. ++Rel.d.b;
  507. }
  508. uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const {
  509. assert(getHeader().filetype == MachO::MH_OBJECT &&
  510. "Only implemented for MH_OBJECT");
  511. MachO::any_relocation_info RE = getRelocation(Rel);
  512. return getAnyRelocationAddress(RE);
  513. }
  514. symbol_iterator
  515. MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
  516. MachO::any_relocation_info RE = getRelocation(Rel);
  517. if (isRelocationScattered(RE))
  518. return symbol_end();
  519. uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
  520. bool isExtern = getPlainRelocationExternal(RE);
  521. if (!isExtern)
  522. return symbol_end();
  523. MachO::symtab_command S = getSymtabLoadCommand();
  524. unsigned SymbolTableEntrySize = is64Bit() ?
  525. sizeof(MachO::nlist_64) :
  526. sizeof(MachO::nlist);
  527. uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
  528. DataRefImpl Sym;
  529. Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
  530. return symbol_iterator(SymbolRef(Sym, this));
  531. }
  532. section_iterator
  533. MachOObjectFile::getRelocationSection(DataRefImpl Rel) const {
  534. return section_iterator(getAnyRelocationSection(getRelocation(Rel)));
  535. }
  536. uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const {
  537. MachO::any_relocation_info RE = getRelocation(Rel);
  538. return getAnyRelocationType(RE);
  539. }
  540. void MachOObjectFile::getRelocationTypeName(
  541. DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
  542. StringRef res;
  543. uint64_t RType = getRelocationType(Rel);
  544. unsigned Arch = this->getArch();
  545. switch (Arch) {
  546. case Triple::x86: {
  547. static const char *const Table[] = {
  548. "GENERIC_RELOC_VANILLA",
  549. "GENERIC_RELOC_PAIR",
  550. "GENERIC_RELOC_SECTDIFF",
  551. "GENERIC_RELOC_PB_LA_PTR",
  552. "GENERIC_RELOC_LOCAL_SECTDIFF",
  553. "GENERIC_RELOC_TLV" };
  554. if (RType > 5)
  555. res = "Unknown";
  556. else
  557. res = Table[RType];
  558. break;
  559. }
  560. case Triple::x86_64: {
  561. static const char *const Table[] = {
  562. "X86_64_RELOC_UNSIGNED",
  563. "X86_64_RELOC_SIGNED",
  564. "X86_64_RELOC_BRANCH",
  565. "X86_64_RELOC_GOT_LOAD",
  566. "X86_64_RELOC_GOT",
  567. "X86_64_RELOC_SUBTRACTOR",
  568. "X86_64_RELOC_SIGNED_1",
  569. "X86_64_RELOC_SIGNED_2",
  570. "X86_64_RELOC_SIGNED_4",
  571. "X86_64_RELOC_TLV" };
  572. if (RType > 9)
  573. res = "Unknown";
  574. else
  575. res = Table[RType];
  576. break;
  577. }
  578. case Triple::arm: {
  579. static const char *const Table[] = {
  580. "ARM_RELOC_VANILLA",
  581. "ARM_RELOC_PAIR",
  582. "ARM_RELOC_SECTDIFF",
  583. "ARM_RELOC_LOCAL_SECTDIFF",
  584. "ARM_RELOC_PB_LA_PTR",
  585. "ARM_RELOC_BR24",
  586. "ARM_THUMB_RELOC_BR22",
  587. "ARM_THUMB_32BIT_BRANCH",
  588. "ARM_RELOC_HALF",
  589. "ARM_RELOC_HALF_SECTDIFF" };
  590. if (RType > 9)
  591. res = "Unknown";
  592. else
  593. res = Table[RType];
  594. break;
  595. }
  596. case Triple::aarch64: {
  597. static const char *const Table[] = {
  598. "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
  599. "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
  600. "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
  601. "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
  602. "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
  603. "ARM64_RELOC_ADDEND"
  604. };
  605. if (RType >= array_lengthof(Table))
  606. res = "Unknown";
  607. else
  608. res = Table[RType];
  609. break;
  610. }
  611. case Triple::ppc: {
  612. static const char *const Table[] = {
  613. "PPC_RELOC_VANILLA",
  614. "PPC_RELOC_PAIR",
  615. "PPC_RELOC_BR14",
  616. "PPC_RELOC_BR24",
  617. "PPC_RELOC_HI16",
  618. "PPC_RELOC_LO16",
  619. "PPC_RELOC_HA16",
  620. "PPC_RELOC_LO14",
  621. "PPC_RELOC_SECTDIFF",
  622. "PPC_RELOC_PB_LA_PTR",
  623. "PPC_RELOC_HI16_SECTDIFF",
  624. "PPC_RELOC_LO16_SECTDIFF",
  625. "PPC_RELOC_HA16_SECTDIFF",
  626. "PPC_RELOC_JBSR",
  627. "PPC_RELOC_LO14_SECTDIFF",
  628. "PPC_RELOC_LOCAL_SECTDIFF" };
  629. if (RType > 15)
  630. res = "Unknown";
  631. else
  632. res = Table[RType];
  633. break;
  634. }
  635. case Triple::UnknownArch:
  636. res = "Unknown";
  637. break;
  638. }
  639. Result.append(res.begin(), res.end());
  640. }
  641. uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const {
  642. MachO::any_relocation_info RE = getRelocation(Rel);
  643. return getAnyRelocationLength(RE);
  644. }
  645. //
  646. // guessLibraryShortName() is passed a name of a dynamic library and returns a
  647. // guess on what the short name is. Then name is returned as a substring of the
  648. // StringRef Name passed in. The name of the dynamic library is recognized as
  649. // a framework if it has one of the two following forms:
  650. // Foo.framework/Versions/A/Foo
  651. // Foo.framework/Foo
  652. // Where A and Foo can be any string. And may contain a trailing suffix
  653. // starting with an underbar. If the Name is recognized as a framework then
  654. // isFramework is set to true else it is set to false. If the Name has a
  655. // suffix then Suffix is set to the substring in Name that contains the suffix
  656. // else it is set to a NULL StringRef.
  657. //
  658. // The Name of the dynamic library is recognized as a library name if it has
  659. // one of the two following forms:
  660. // libFoo.A.dylib
  661. // libFoo.dylib
  662. // The library may have a suffix trailing the name Foo of the form:
  663. // libFoo_profile.A.dylib
  664. // libFoo_profile.dylib
  665. //
  666. // The Name of the dynamic library is also recognized as a library name if it
  667. // has the following form:
  668. // Foo.qtx
  669. //
  670. // If the Name of the dynamic library is none of the forms above then a NULL
  671. // StringRef is returned.
  672. //
  673. StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
  674. bool &isFramework,
  675. StringRef &Suffix) {
  676. StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
  677. size_t a, b, c, d, Idx;
  678. isFramework = false;
  679. Suffix = StringRef();
  680. // Pull off the last component and make Foo point to it
  681. a = Name.rfind('/');
  682. if (a == Name.npos || a == 0)
  683. goto guess_library;
  684. Foo = Name.slice(a+1, Name.npos);
  685. // Look for a suffix starting with a '_'
  686. Idx = Foo.rfind('_');
  687. if (Idx != Foo.npos && Foo.size() >= 2) {
  688. Suffix = Foo.slice(Idx, Foo.npos);
  689. Foo = Foo.slice(0, Idx);
  690. }
  691. // First look for the form Foo.framework/Foo
  692. b = Name.rfind('/', a);
  693. if (b == Name.npos)
  694. Idx = 0;
  695. else
  696. Idx = b+1;
  697. F = Name.slice(Idx, Idx + Foo.size());
  698. DotFramework = Name.slice(Idx + Foo.size(),
  699. Idx + Foo.size() + sizeof(".framework/")-1);
  700. if (F == Foo && DotFramework == ".framework/") {
  701. isFramework = true;
  702. return Foo;
  703. }
  704. // Next look for the form Foo.framework/Versions/A/Foo
  705. if (b == Name.npos)
  706. goto guess_library;
  707. c = Name.rfind('/', b);
  708. if (c == Name.npos || c == 0)
  709. goto guess_library;
  710. V = Name.slice(c+1, Name.npos);
  711. if (!V.startswith("Versions/"))
  712. goto guess_library;
  713. d = Name.rfind('/', c);
  714. if (d == Name.npos)
  715. Idx = 0;
  716. else
  717. Idx = d+1;
  718. F = Name.slice(Idx, Idx + Foo.size());
  719. DotFramework = Name.slice(Idx + Foo.size(),
  720. Idx + Foo.size() + sizeof(".framework/")-1);
  721. if (F == Foo && DotFramework == ".framework/") {
  722. isFramework = true;
  723. return Foo;
  724. }
  725. guess_library:
  726. // pull off the suffix after the "." and make a point to it
  727. a = Name.rfind('.');
  728. if (a == Name.npos || a == 0)
  729. return StringRef();
  730. Dylib = Name.slice(a, Name.npos);
  731. if (Dylib != ".dylib")
  732. goto guess_qtx;
  733. // First pull off the version letter for the form Foo.A.dylib if any.
  734. if (a >= 3) {
  735. Dot = Name.slice(a-2, a-1);
  736. if (Dot == ".")
  737. a = a - 2;
  738. }
  739. b = Name.rfind('/', a);
  740. if (b == Name.npos)
  741. b = 0;
  742. else
  743. b = b+1;
  744. // ignore any suffix after an underbar like Foo_profile.A.dylib
  745. Idx = Name.find('_', b);
  746. if (Idx != Name.npos && Idx != b) {
  747. Lib = Name.slice(b, Idx);
  748. Suffix = Name.slice(Idx, a);
  749. }
  750. else
  751. Lib = Name.slice(b, a);
  752. // There are incorrect library names of the form:
  753. // libATS.A_profile.dylib so check for these.
  754. if (Lib.size() >= 3) {
  755. Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
  756. if (Dot == ".")
  757. Lib = Lib.slice(0, Lib.size()-2);
  758. }
  759. return Lib;
  760. guess_qtx:
  761. Qtx = Name.slice(a, Name.npos);
  762. if (Qtx != ".qtx")
  763. return StringRef();
  764. b = Name.rfind('/', a);
  765. if (b == Name.npos)
  766. Lib = Name.slice(0, a);
  767. else
  768. Lib = Name.slice(b+1, a);
  769. // There are library names of the form: QT.A.qtx so check for these.
  770. if (Lib.size() >= 3) {
  771. Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
  772. if (Dot == ".")
  773. Lib = Lib.slice(0, Lib.size()-2);
  774. }
  775. return Lib;
  776. }
  777. // getLibraryShortNameByIndex() is used to get the short name of the library
  778. // for an undefined symbol in a linked Mach-O binary that was linked with the
  779. // normal two-level namespace default (that is MH_TWOLEVEL in the header).
  780. // It is passed the index (0 - based) of the library as translated from
  781. // GET_LIBRARY_ORDINAL (1 - based).
  782. std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
  783. StringRef &Res) const {
  784. if (Index >= Libraries.size())
  785. return object_error::parse_failed;
  786. // If the cache of LibrariesShortNames is not built up do that first for
  787. // all the Libraries.
  788. if (LibrariesShortNames.size() == 0) {
  789. for (unsigned i = 0; i < Libraries.size(); i++) {
  790. MachO::dylib_command D =
  791. getStruct<MachO::dylib_command>(this, Libraries[i]);
  792. if (D.dylib.name >= D.cmdsize)
  793. return object_error::parse_failed;
  794. const char *P = (const char *)(Libraries[i]) + D.dylib.name;
  795. StringRef Name = StringRef(P);
  796. if (D.dylib.name+Name.size() >= D.cmdsize)
  797. return object_error::parse_failed;
  798. StringRef Suffix;
  799. bool isFramework;
  800. StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
  801. if (shortName.empty())
  802. LibrariesShortNames.push_back(Name);
  803. else
  804. LibrariesShortNames.push_back(shortName);
  805. }
  806. }
  807. Res = LibrariesShortNames[Index];
  808. return std::error_code();
  809. }
  810. section_iterator
  811. MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const {
  812. DataRefImpl Sec;
  813. Sec.d.a = Rel->getRawDataRefImpl().d.a;
  814. return section_iterator(SectionRef(Sec, this));
  815. }
  816. basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
  817. return getSymbolByIndex(0);
  818. }
  819. basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
  820. DataRefImpl DRI;
  821. if (!SymtabLoadCmd)
  822. return basic_symbol_iterator(SymbolRef(DRI, this));
  823. MachO::symtab_command Symtab = getSymtabLoadCommand();
  824. unsigned SymbolTableEntrySize = is64Bit() ?
  825. sizeof(MachO::nlist_64) :
  826. sizeof(MachO::nlist);
  827. unsigned Offset = Symtab.symoff +
  828. Symtab.nsyms * SymbolTableEntrySize;
  829. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
  830. return basic_symbol_iterator(SymbolRef(DRI, this));
  831. }
  832. basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
  833. DataRefImpl DRI;
  834. if (!SymtabLoadCmd)
  835. return basic_symbol_iterator(SymbolRef(DRI, this));
  836. MachO::symtab_command Symtab = getSymtabLoadCommand();
  837. if (Index >= Symtab.nsyms)
  838. report_fatal_error("Requested symbol index is out of range.");
  839. unsigned SymbolTableEntrySize =
  840. is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
  841. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
  842. DRI.p += Index * SymbolTableEntrySize;
  843. return basic_symbol_iterator(SymbolRef(DRI, this));
  844. }
  845. section_iterator MachOObjectFile::section_begin() const {
  846. DataRefImpl DRI;
  847. return section_iterator(SectionRef(DRI, this));
  848. }
  849. section_iterator MachOObjectFile::section_end() const {
  850. DataRefImpl DRI;
  851. DRI.d.a = Sections.size();
  852. return section_iterator(SectionRef(DRI, this));
  853. }
  854. uint8_t MachOObjectFile::getBytesInAddress() const {
  855. return is64Bit() ? 8 : 4;
  856. }
  857. StringRef MachOObjectFile::getFileFormatName() const {
  858. unsigned CPUType = getCPUType(this);
  859. if (!is64Bit()) {
  860. switch (CPUType) {
  861. case llvm::MachO::CPU_TYPE_I386:
  862. return "Mach-O 32-bit i386";
  863. case llvm::MachO::CPU_TYPE_ARM:
  864. return "Mach-O arm";
  865. case llvm::MachO::CPU_TYPE_POWERPC:
  866. return "Mach-O 32-bit ppc";
  867. default:
  868. return "Mach-O 32-bit unknown";
  869. }
  870. }
  871. switch (CPUType) {
  872. case llvm::MachO::CPU_TYPE_X86_64:
  873. return "Mach-O 64-bit x86-64";
  874. case llvm::MachO::CPU_TYPE_ARM64:
  875. return "Mach-O arm64";
  876. case llvm::MachO::CPU_TYPE_POWERPC64:
  877. return "Mach-O 64-bit ppc64";
  878. default:
  879. return "Mach-O 64-bit unknown";
  880. }
  881. }
  882. Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
  883. switch (CPUType) {
  884. case llvm::MachO::CPU_TYPE_I386:
  885. return Triple::x86;
  886. case llvm::MachO::CPU_TYPE_X86_64:
  887. return Triple::x86_64;
  888. case llvm::MachO::CPU_TYPE_ARM:
  889. return Triple::arm;
  890. case llvm::MachO::CPU_TYPE_ARM64:
  891. return Triple::aarch64;
  892. case llvm::MachO::CPU_TYPE_POWERPC:
  893. return Triple::ppc;
  894. case llvm::MachO::CPU_TYPE_POWERPC64:
  895. return Triple::ppc64;
  896. default:
  897. return Triple::UnknownArch;
  898. }
  899. }
  900. Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
  901. const char **McpuDefault) {
  902. if (McpuDefault)
  903. *McpuDefault = nullptr;
  904. switch (CPUType) {
  905. case MachO::CPU_TYPE_I386:
  906. switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
  907. case MachO::CPU_SUBTYPE_I386_ALL:
  908. return Triple("i386-apple-darwin");
  909. default:
  910. return Triple();
  911. }
  912. case MachO::CPU_TYPE_X86_64:
  913. switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
  914. case MachO::CPU_SUBTYPE_X86_64_ALL:
  915. return Triple("x86_64-apple-darwin");
  916. case MachO::CPU_SUBTYPE_X86_64_H:
  917. return Triple("x86_64h-apple-darwin");
  918. default:
  919. return Triple();
  920. }
  921. case MachO::CPU_TYPE_ARM:
  922. switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
  923. case MachO::CPU_SUBTYPE_ARM_V4T:
  924. return Triple("armv4t-apple-darwin");
  925. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  926. return Triple("armv5e-apple-darwin");
  927. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  928. return Triple("xscale-apple-darwin");
  929. case MachO::CPU_SUBTYPE_ARM_V6:
  930. return Triple("armv6-apple-darwin");
  931. case MachO::CPU_SUBTYPE_ARM_V6M:
  932. if (McpuDefault)
  933. *McpuDefault = "cortex-m0";
  934. return Triple("armv6m-apple-darwin");
  935. case MachO::CPU_SUBTYPE_ARM_V7:
  936. return Triple("armv7-apple-darwin");
  937. case MachO::CPU_SUBTYPE_ARM_V7EM:
  938. if (McpuDefault)
  939. *McpuDefault = "cortex-m4";
  940. return Triple("armv7em-apple-darwin");
  941. case MachO::CPU_SUBTYPE_ARM_V7K:
  942. return Triple("armv7k-apple-darwin");
  943. case MachO::CPU_SUBTYPE_ARM_V7M:
  944. if (McpuDefault)
  945. *McpuDefault = "cortex-m3";
  946. return Triple("armv7m-apple-darwin");
  947. case MachO::CPU_SUBTYPE_ARM_V7S:
  948. return Triple("armv7s-apple-darwin");
  949. default:
  950. return Triple();
  951. }
  952. case MachO::CPU_TYPE_ARM64:
  953. switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
  954. case MachO::CPU_SUBTYPE_ARM64_ALL:
  955. return Triple("arm64-apple-darwin");
  956. default:
  957. return Triple();
  958. }
  959. case MachO::CPU_TYPE_POWERPC:
  960. switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
  961. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  962. return Triple("ppc-apple-darwin");
  963. default:
  964. return Triple();
  965. }
  966. case MachO::CPU_TYPE_POWERPC64:
  967. switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
  968. case MachO::CPU_SUBTYPE_POWERPC_ALL:
  969. return Triple("ppc64-apple-darwin");
  970. default:
  971. return Triple();
  972. }
  973. default:
  974. return Triple();
  975. }
  976. }
  977. Triple MachOObjectFile::getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
  978. const char **McpuDefault) {
  979. if (McpuDefault)
  980. *McpuDefault = nullptr;
  981. switch (CPUType) {
  982. case MachO::CPU_TYPE_ARM:
  983. switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
  984. case MachO::CPU_SUBTYPE_ARM_V4T:
  985. return Triple("thumbv4t-apple-darwin");
  986. case MachO::CPU_SUBTYPE_ARM_V5TEJ:
  987. return Triple("thumbv5e-apple-darwin");
  988. case MachO::CPU_SUBTYPE_ARM_XSCALE:
  989. return Triple("xscale-apple-darwin");
  990. case MachO::CPU_SUBTYPE_ARM_V6:
  991. return Triple("thumbv6-apple-darwin");
  992. case MachO::CPU_SUBTYPE_ARM_V6M:
  993. if (McpuDefault)
  994. *McpuDefault = "cortex-m0";
  995. return Triple("thumbv6m-apple-darwin");
  996. case MachO::CPU_SUBTYPE_ARM_V7:
  997. return Triple("thumbv7-apple-darwin");
  998. case MachO::CPU_SUBTYPE_ARM_V7EM:
  999. if (McpuDefault)
  1000. *McpuDefault = "cortex-m4";
  1001. return Triple("thumbv7em-apple-darwin");
  1002. case MachO::CPU_SUBTYPE_ARM_V7K:
  1003. return Triple("thumbv7k-apple-darwin");
  1004. case MachO::CPU_SUBTYPE_ARM_V7M:
  1005. if (McpuDefault)
  1006. *McpuDefault = "cortex-m3";
  1007. return Triple("thumbv7m-apple-darwin");
  1008. case MachO::CPU_SUBTYPE_ARM_V7S:
  1009. return Triple("thumbv7s-apple-darwin");
  1010. default:
  1011. return Triple();
  1012. }
  1013. default:
  1014. return Triple();
  1015. }
  1016. }
  1017. Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
  1018. const char **McpuDefault,
  1019. Triple *ThumbTriple) {
  1020. Triple T = MachOObjectFile::getArch(CPUType, CPUSubType, McpuDefault);
  1021. *ThumbTriple = MachOObjectFile::getThumbArch(CPUType, CPUSubType,
  1022. McpuDefault);
  1023. return T;
  1024. }
  1025. Triple MachOObjectFile::getHostArch() {
  1026. return Triple(sys::getDefaultTargetTriple());
  1027. }
  1028. bool MachOObjectFile::isValidArch(StringRef ArchFlag) {
  1029. return StringSwitch<bool>(ArchFlag)
  1030. .Case("i386", true)
  1031. .Case("x86_64", true)
  1032. .Case("x86_64h", true)
  1033. .Case("armv4t", true)
  1034. .Case("arm", true)
  1035. .Case("armv5e", true)
  1036. .Case("armv6", true)
  1037. .Case("armv6m", true)
  1038. .Case("armv7", true)
  1039. .Case("armv7em", true)
  1040. .Case("armv7k", true)
  1041. .Case("armv7m", true)
  1042. .Case("armv7s", true)
  1043. .Case("arm64", true)
  1044. .Case("ppc", true)
  1045. .Case("ppc64", true)
  1046. .Default(false);
  1047. }
  1048. unsigned MachOObjectFile::getArch() const {
  1049. return getArch(getCPUType(this));
  1050. }
  1051. Triple MachOObjectFile::getArch(const char **McpuDefault,
  1052. Triple *ThumbTriple) const {
  1053. *ThumbTriple = getThumbArch(Header.cputype, Header.cpusubtype, McpuDefault);
  1054. return getArch(Header.cputype, Header.cpusubtype, McpuDefault);
  1055. }
  1056. relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
  1057. DataRefImpl DRI;
  1058. DRI.d.a = Index;
  1059. return section_rel_begin(DRI);
  1060. }
  1061. relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
  1062. DataRefImpl DRI;
  1063. DRI.d.a = Index;
  1064. return section_rel_end(DRI);
  1065. }
  1066. dice_iterator MachOObjectFile::begin_dices() const {
  1067. DataRefImpl DRI;
  1068. if (!DataInCodeLoadCmd)
  1069. return dice_iterator(DiceRef(DRI, this));
  1070. MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
  1071. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
  1072. return dice_iterator(DiceRef(DRI, this));
  1073. }
  1074. dice_iterator MachOObjectFile::end_dices() const {
  1075. DataRefImpl DRI;
  1076. if (!DataInCodeLoadCmd)
  1077. return dice_iterator(DiceRef(DRI, this));
  1078. MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
  1079. unsigned Offset = DicLC.dataoff + DicLC.datasize;
  1080. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
  1081. return dice_iterator(DiceRef(DRI, this));
  1082. }
  1083. ExportEntry::ExportEntry(ArrayRef<uint8_t> T)
  1084. : Trie(T), Malformed(false), Done(false) { }
  1085. void ExportEntry::moveToFirst() {
  1086. pushNode(0);
  1087. pushDownUntilBottom();
  1088. }
  1089. void ExportEntry::moveToEnd() {
  1090. Stack.clear();
  1091. Done = true;
  1092. }
  1093. bool ExportEntry::operator==(const ExportEntry &Other) const {
  1094. // Common case, one at end, other iterating from begin.
  1095. if (Done || Other.Done)
  1096. return (Done == Other.Done);
  1097. // Not equal if different stack sizes.
  1098. if (Stack.size() != Other.Stack.size())
  1099. return false;
  1100. // Not equal if different cumulative strings.
  1101. if (!CumulativeString.equals(Other.CumulativeString))
  1102. return false;
  1103. // Equal if all nodes in both stacks match.
  1104. for (unsigned i=0; i < Stack.size(); ++i) {
  1105. if (Stack[i].Start != Other.Stack[i].Start)
  1106. return false;
  1107. }
  1108. return true;
  1109. }
  1110. uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr) {
  1111. unsigned Count;
  1112. uint64_t Result = decodeULEB128(Ptr, &Count);
  1113. Ptr += Count;
  1114. if (Ptr > Trie.end()) {
  1115. Ptr = Trie.end();
  1116. Malformed = true;
  1117. }
  1118. return Result;
  1119. }
  1120. StringRef ExportEntry::name() const {
  1121. return CumulativeString;
  1122. }
  1123. uint64_t ExportEntry::flags() const {
  1124. return Stack.back().Flags;
  1125. }
  1126. uint64_t ExportEntry::address() const {
  1127. return Stack.back().Address;
  1128. }
  1129. uint64_t ExportEntry::other() const {
  1130. return Stack.back().Other;
  1131. }
  1132. StringRef ExportEntry::otherName() const {
  1133. const char* ImportName = Stack.back().ImportName;
  1134. if (ImportName)
  1135. return StringRef(ImportName);
  1136. return StringRef();
  1137. }
  1138. uint32_t ExportEntry::nodeOffset() const {
  1139. return Stack.back().Start - Trie.begin();
  1140. }
  1141. ExportEntry::NodeState::NodeState(const uint8_t *Ptr)
  1142. : Start(Ptr), Current(Ptr), Flags(0), Address(0), Other(0),
  1143. ImportName(nullptr), ChildCount(0), NextChildIndex(0),
  1144. ParentStringLength(0), IsExportNode(false) {
  1145. }
  1146. void ExportEntry::pushNode(uint64_t offset) {
  1147. const uint8_t *Ptr = Trie.begin() + offset;
  1148. NodeState State(Ptr);
  1149. uint64_t ExportInfoSize = readULEB128(State.Current);
  1150. State.IsExportNode = (ExportInfoSize != 0);
  1151. const uint8_t* Children = State.Current + ExportInfoSize;
  1152. if (State.IsExportNode) {
  1153. State.Flags = readULEB128(State.Current);
  1154. if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
  1155. State.Address = 0;
  1156. State.Other = readULEB128(State.Current); // dylib ordinal
  1157. State.ImportName = reinterpret_cast<const char*>(State.Current);
  1158. } else {
  1159. State.Address = readULEB128(State.Current);
  1160. if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
  1161. State.Other = readULEB128(State.Current);
  1162. }
  1163. }
  1164. State.ChildCount = *Children;
  1165. State.Current = Children + 1;
  1166. State.NextChildIndex = 0;
  1167. State.ParentStringLength = CumulativeString.size();
  1168. Stack.push_back(State);
  1169. }
  1170. void ExportEntry::pushDownUntilBottom() {
  1171. while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
  1172. NodeState &Top = Stack.back();
  1173. CumulativeString.resize(Top.ParentStringLength);
  1174. for (;*Top.Current != 0; Top.Current++) {
  1175. char C = *Top.Current;
  1176. CumulativeString.push_back(C);
  1177. }
  1178. Top.Current += 1;
  1179. uint64_t childNodeIndex = readULEB128(Top.Current);
  1180. Top.NextChildIndex += 1;
  1181. pushNode(childNodeIndex);
  1182. }
  1183. if (!Stack.back().IsExportNode) {
  1184. Malformed = true;
  1185. moveToEnd();
  1186. }
  1187. }
  1188. // We have a trie data structure and need a way to walk it that is compatible
  1189. // with the C++ iterator model. The solution is a non-recursive depth first
  1190. // traversal where the iterator contains a stack of parent nodes along with a
  1191. // string that is the accumulation of all edge strings along the parent chain
  1192. // to this point.
  1193. //
  1194. // There is one "export" node for each exported symbol. But because some
  1195. // symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export
  1196. // node may have child nodes too.
  1197. //
  1198. // The algorithm for moveNext() is to keep moving down the leftmost unvisited
  1199. // child until hitting a node with no children (which is an export node or
  1200. // else the trie is malformed). On the way down, each node is pushed on the
  1201. // stack ivar. If there is no more ways down, it pops up one and tries to go
  1202. // down a sibling path until a childless node is reached.
  1203. void ExportEntry::moveNext() {
  1204. if (Stack.empty() || !Stack.back().IsExportNode) {
  1205. Malformed = true;
  1206. moveToEnd();
  1207. return;
  1208. }
  1209. Stack.pop_back();
  1210. while (!Stack.empty()) {
  1211. NodeState &Top = Stack.back();
  1212. if (Top.NextChildIndex < Top.ChildCount) {
  1213. pushDownUntilBottom();
  1214. // Now at the next export node.
  1215. return;
  1216. } else {
  1217. if (Top.IsExportNode) {
  1218. // This node has no children but is itself an export node.
  1219. CumulativeString.resize(Top.ParentStringLength);
  1220. return;
  1221. }
  1222. Stack.pop_back();
  1223. }
  1224. }
  1225. Done = true;
  1226. }
  1227. iterator_range<export_iterator>
  1228. MachOObjectFile::exports(ArrayRef<uint8_t> Trie) {
  1229. ExportEntry Start(Trie);
  1230. if (Trie.size() == 0)
  1231. Start.moveToEnd();
  1232. else
  1233. Start.moveToFirst();
  1234. ExportEntry Finish(Trie);
  1235. Finish.moveToEnd();
  1236. return iterator_range<export_iterator>(export_iterator(Start),
  1237. export_iterator(Finish));
  1238. }
  1239. iterator_range<export_iterator> MachOObjectFile::exports() const {
  1240. return exports(getDyldInfoExportsTrie());
  1241. }
  1242. MachORebaseEntry::MachORebaseEntry(ArrayRef<uint8_t> Bytes, bool is64Bit)
  1243. : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
  1244. RemainingLoopCount(0), AdvanceAmount(0), RebaseType(0),
  1245. PointerSize(is64Bit ? 8 : 4), Malformed(false), Done(false) {}
  1246. void MachORebaseEntry::moveToFirst() {
  1247. Ptr = Opcodes.begin();
  1248. moveNext();
  1249. }
  1250. void MachORebaseEntry::moveToEnd() {
  1251. Ptr = Opcodes.end();
  1252. RemainingLoopCount = 0;
  1253. Done = true;
  1254. }
  1255. void MachORebaseEntry::moveNext() {
  1256. // If in the middle of some loop, move to next rebasing in loop.
  1257. SegmentOffset += AdvanceAmount;
  1258. if (RemainingLoopCount) {
  1259. --RemainingLoopCount;
  1260. return;
  1261. }
  1262. if (Ptr == Opcodes.end()) {
  1263. Done = true;
  1264. return;
  1265. }
  1266. bool More = true;
  1267. while (More && !Malformed) {
  1268. // Parse next opcode and set up next loop.
  1269. uint8_t Byte = *Ptr++;
  1270. uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK;
  1271. uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK;
  1272. switch (Opcode) {
  1273. case MachO::REBASE_OPCODE_DONE:
  1274. More = false;
  1275. Done = true;
  1276. moveToEnd();
  1277. DEBUG_WITH_TYPE("mach-o-rebase", llvm::dbgs() << "REBASE_OPCODE_DONE\n");
  1278. break;
  1279. case MachO::REBASE_OPCODE_SET_TYPE_IMM:
  1280. RebaseType = ImmValue;
  1281. DEBUG_WITH_TYPE(
  1282. "mach-o-rebase",
  1283. llvm::dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: "
  1284. << "RebaseType=" << (int) RebaseType << "\n");
  1285. break;
  1286. case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
  1287. SegmentIndex = ImmValue;
  1288. SegmentOffset = readULEB128();
  1289. DEBUG_WITH_TYPE(
  1290. "mach-o-rebase",
  1291. llvm::dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
  1292. << "SegmentIndex=" << SegmentIndex << ", "
  1293. << format("SegmentOffset=0x%06X", SegmentOffset)
  1294. << "\n");
  1295. break;
  1296. case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
  1297. SegmentOffset += readULEB128();
  1298. DEBUG_WITH_TYPE("mach-o-rebase",
  1299. llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: "
  1300. << format("SegmentOffset=0x%06X",
  1301. SegmentOffset) << "\n");
  1302. break;
  1303. case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
  1304. SegmentOffset += ImmValue * PointerSize;
  1305. DEBUG_WITH_TYPE("mach-o-rebase",
  1306. llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: "
  1307. << format("SegmentOffset=0x%06X",
  1308. SegmentOffset) << "\n");
  1309. break;
  1310. case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES:
  1311. AdvanceAmount = PointerSize;
  1312. RemainingLoopCount = ImmValue - 1;
  1313. DEBUG_WITH_TYPE(
  1314. "mach-o-rebase",
  1315. llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: "
  1316. << format("SegmentOffset=0x%06X", SegmentOffset)
  1317. << ", AdvanceAmount=" << AdvanceAmount
  1318. << ", RemainingLoopCount=" << RemainingLoopCount
  1319. << "\n");
  1320. return;
  1321. case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
  1322. AdvanceAmount = PointerSize;
  1323. RemainingLoopCount = readULEB128() - 1;
  1324. DEBUG_WITH_TYPE(
  1325. "mach-o-rebase",
  1326. llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: "
  1327. << format("SegmentOffset=0x%06X", SegmentOffset)
  1328. << ", AdvanceAmount=" << AdvanceAmount
  1329. << ", RemainingLoopCount=" << RemainingLoopCount
  1330. << "\n");
  1331. return;
  1332. case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
  1333. AdvanceAmount = readULEB128() + PointerSize;
  1334. RemainingLoopCount = 0;
  1335. DEBUG_WITH_TYPE(
  1336. "mach-o-rebase",
  1337. llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: "
  1338. << format("SegmentOffset=0x%06X", SegmentOffset)
  1339. << ", AdvanceAmount=" << AdvanceAmount
  1340. << ", RemainingLoopCount=" << RemainingLoopCount
  1341. << "\n");
  1342. return;
  1343. case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
  1344. RemainingLoopCount = readULEB128() - 1;
  1345. AdvanceAmount = readULEB128() + PointerSize;
  1346. DEBUG_WITH_TYPE(
  1347. "mach-o-rebase",
  1348. llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: "
  1349. << format("SegmentOffset=0x%06X", SegmentOffset)
  1350. << ", AdvanceAmount=" << AdvanceAmount
  1351. << ", RemainingLoopCount=" << RemainingLoopCount
  1352. << "\n");
  1353. return;
  1354. default:
  1355. Malformed = true;
  1356. }
  1357. }
  1358. }
  1359. uint64_t MachORebaseEntry::readULEB128() {
  1360. unsigned Count;
  1361. uint64_t Result = decodeULEB128(Ptr, &Count);
  1362. Ptr += Count;
  1363. if (Ptr > Opcodes.end()) {
  1364. Ptr = Opcodes.end();
  1365. Malformed = true;
  1366. }
  1367. return Result;
  1368. }
  1369. uint32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; }
  1370. uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; }
  1371. StringRef MachORebaseEntry::typeName() const {
  1372. switch (RebaseType) {
  1373. case MachO::REBASE_TYPE_POINTER:
  1374. return "pointer";
  1375. case MachO::REBASE_TYPE_TEXT_ABSOLUTE32:
  1376. return "text abs32";
  1377. case MachO::REBASE_TYPE_TEXT_PCREL32:
  1378. return "text rel32";
  1379. }
  1380. return "unknown";
  1381. }
  1382. bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const {
  1383. assert(Opcodes == Other.Opcodes && "compare iterators of different files");
  1384. return (Ptr == Other.Ptr) &&
  1385. (RemainingLoopCount == Other.RemainingLoopCount) &&
  1386. (Done == Other.Done);
  1387. }
  1388. iterator_range<rebase_iterator>
  1389. MachOObjectFile::rebaseTable(ArrayRef<uint8_t> Opcodes, bool is64) {
  1390. MachORebaseEntry Start(Opcodes, is64);
  1391. Start.moveToFirst();
  1392. MachORebaseEntry Finish(Opcodes, is64);
  1393. Finish.moveToEnd();
  1394. return iterator_range<rebase_iterator>(rebase_iterator(Start),
  1395. rebase_iterator(Finish));
  1396. }
  1397. iterator_range<rebase_iterator> MachOObjectFile::rebaseTable() const {
  1398. return rebaseTable(getDyldInfoRebaseOpcodes(), is64Bit());
  1399. }
  1400. MachOBindEntry::MachOBindEntry(ArrayRef<uint8_t> Bytes, bool is64Bit,
  1401. Kind BK)
  1402. : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
  1403. Ordinal(0), Flags(0), Addend(0), RemainingLoopCount(0), AdvanceAmount(0),
  1404. BindType(0), PointerSize(is64Bit ? 8 : 4),
  1405. TableKind(BK), Malformed(false), Done(false) {}
  1406. void MachOBindEntry::moveToFirst() {
  1407. Ptr = Opcodes.begin();
  1408. moveNext();
  1409. }
  1410. void MachOBindEntry::moveToEnd() {
  1411. Ptr = Opcodes.end();
  1412. RemainingLoopCount = 0;
  1413. Done = true;
  1414. }
  1415. void MachOBindEntry::moveNext() {
  1416. // If in the middle of some loop, move to next binding in loop.
  1417. SegmentOffset += AdvanceAmount;
  1418. if (RemainingLoopCount) {
  1419. --RemainingLoopCount;
  1420. return;
  1421. }
  1422. if (Ptr == Opcodes.end()) {
  1423. Done = true;
  1424. return;
  1425. }
  1426. bool More = true;
  1427. while (More && !Malformed) {
  1428. // Parse next opcode and set up next loop.
  1429. uint8_t Byte = *Ptr++;
  1430. uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK;
  1431. uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK;
  1432. int8_t SignExtended;
  1433. const uint8_t *SymStart;
  1434. switch (Opcode) {
  1435. case MachO::BIND_OPCODE_DONE:
  1436. if (TableKind == Kind::Lazy) {
  1437. // Lazying bindings have a DONE opcode between entries. Need to ignore
  1438. // it to advance to next entry. But need not if this is last entry.
  1439. bool NotLastEntry = false;
  1440. for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) {
  1441. if (*P) {
  1442. NotLastEntry = true;
  1443. }
  1444. }
  1445. if (NotLastEntry)
  1446. break;
  1447. }
  1448. More = false;
  1449. Done = true;
  1450. moveToEnd();
  1451. DEBUG_WITH_TYPE("mach-o-bind", llvm::dbgs() << "BIND_OPCODE_DONE\n");
  1452. break;
  1453. case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
  1454. Ordinal = ImmValue;
  1455. DEBUG_WITH_TYPE(
  1456. "mach-o-bind",
  1457. llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: "
  1458. << "Ordinal=" << Ordinal << "\n");
  1459. break;
  1460. case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
  1461. Ordinal = readULEB128();
  1462. DEBUG_WITH_TYPE(
  1463. "mach-o-bind",
  1464. llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: "
  1465. << "Ordinal=" << Ordinal << "\n");
  1466. break;
  1467. case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
  1468. if (ImmValue) {
  1469. SignExtended = MachO::BIND_OPCODE_MASK | ImmValue;
  1470. Ordinal = SignExtended;
  1471. } else
  1472. Ordinal = 0;
  1473. DEBUG_WITH_TYPE(
  1474. "mach-o-bind",
  1475. llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: "
  1476. << "Ordinal=" << Ordinal << "\n");
  1477. break;
  1478. case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
  1479. Flags = ImmValue;
  1480. SymStart = Ptr;
  1481. while (*Ptr) {
  1482. ++Ptr;
  1483. }
  1484. SymbolName = StringRef(reinterpret_cast<const char*>(SymStart),
  1485. Ptr-SymStart);
  1486. ++Ptr;
  1487. DEBUG_WITH_TYPE(
  1488. "mach-o-bind",
  1489. llvm::dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: "
  1490. << "SymbolName=" << SymbolName << "\n");
  1491. if (TableKind == Kind::Weak) {
  1492. if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
  1493. return;
  1494. }
  1495. break;
  1496. case MachO::BIND_OPCODE_SET_TYPE_IMM:
  1497. BindType = ImmValue;
  1498. DEBUG_WITH_TYPE(
  1499. "mach-o-bind",
  1500. llvm::dbgs() << "BIND_OPCODE_SET_TYPE_IMM: "
  1501. << "BindType=" << (int)BindType << "\n");
  1502. break;
  1503. case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
  1504. Addend = readSLEB128();
  1505. if (TableKind == Kind::Lazy)
  1506. Malformed = true;
  1507. DEBUG_WITH_TYPE(
  1508. "mach-o-bind",
  1509. llvm::dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: "
  1510. << "Addend=" << Addend << "\n");
  1511. break;
  1512. case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
  1513. SegmentIndex = ImmValue;
  1514. SegmentOffset = readULEB128();
  1515. DEBUG_WITH_TYPE(
  1516. "mach-o-bind",
  1517. llvm::dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
  1518. << "SegmentIndex=" << SegmentIndex << ", "
  1519. << format("SegmentOffset=0x%06X", SegmentOffset)
  1520. << "\n");
  1521. break;
  1522. case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
  1523. SegmentOffset += readULEB128();
  1524. DEBUG_WITH_TYPE("mach-o-bind",
  1525. llvm::dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: "
  1526. << format("SegmentOffset=0x%06X",
  1527. SegmentOffset) << "\n");
  1528. break;
  1529. case MachO::BIND_OPCODE_DO_BIND:
  1530. AdvanceAmount = PointerSize;
  1531. RemainingLoopCount = 0;
  1532. DEBUG_WITH_TYPE("mach-o-bind",
  1533. llvm::dbgs() << "BIND_OPCODE_DO_BIND: "
  1534. << format("SegmentOffset=0x%06X",
  1535. SegmentOffset) << "\n");
  1536. return;
  1537. case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
  1538. AdvanceAmount = readULEB128() + PointerSize;
  1539. RemainingLoopCount = 0;
  1540. if (TableKind == Kind::Lazy)
  1541. Malformed = true;
  1542. DEBUG_WITH_TYPE(
  1543. "mach-o-bind",
  1544. llvm::dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: "
  1545. << format("SegmentOffset=0x%06X", SegmentOffset)
  1546. << ", AdvanceAmount=" << AdvanceAmount
  1547. << ", RemainingLoopCount=" << RemainingLoopCount
  1548. << "\n");
  1549. return;
  1550. case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
  1551. AdvanceAmount = ImmValue * PointerSize + PointerSize;
  1552. RemainingLoopCount = 0;
  1553. if (TableKind == Kind::Lazy)
  1554. Malformed = true;
  1555. DEBUG_WITH_TYPE("mach-o-bind",
  1556. llvm::dbgs()
  1557. << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: "
  1558. << format("SegmentOffset=0x%06X",
  1559. SegmentOffset) << "\n");
  1560. return;
  1561. case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
  1562. RemainingLoopCount = readULEB128() - 1;
  1563. AdvanceAmount = readULEB128() + PointerSize;
  1564. if (TableKind == Kind::Lazy)
  1565. Malformed = true;
  1566. DEBUG_WITH_TYPE(
  1567. "mach-o-bind",
  1568. llvm::dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: "
  1569. << format("SegmentOffset=0x%06X", SegmentOffset)
  1570. << ", AdvanceAmount=" << AdvanceAmount
  1571. << ", RemainingLoopCount=" << RemainingLoopCount
  1572. << "\n");
  1573. return;
  1574. default:
  1575. Malformed = true;
  1576. }
  1577. }
  1578. }
  1579. uint64_t MachOBindEntry::readULEB128() {
  1580. unsigned Count;
  1581. uint64_t Result = decodeULEB128(Ptr, &Count);
  1582. Ptr += Count;
  1583. if (Ptr > Opcodes.end()) {
  1584. Ptr = Opcodes.end();
  1585. Malformed = true;
  1586. }
  1587. return Result;
  1588. }
  1589. int64_t MachOBindEntry::readSLEB128() {
  1590. unsigned Count;
  1591. int64_t Result = decodeSLEB128(Ptr, &Count);
  1592. Ptr += Count;
  1593. if (Ptr > Opcodes.end()) {
  1594. Ptr = Opcodes.end();
  1595. Malformed = true;
  1596. }
  1597. return Result;
  1598. }
  1599. uint32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; }
  1600. uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; }
  1601. StringRef MachOBindEntry::typeName() const {
  1602. switch (BindType) {
  1603. case MachO::BIND_TYPE_POINTER:
  1604. return "pointer";
  1605. case MachO::BIND_TYPE_TEXT_ABSOLUTE32:
  1606. return "text abs32";
  1607. case MachO::BIND_TYPE_TEXT_PCREL32:
  1608. return "text rel32";
  1609. }
  1610. return "unknown";
  1611. }
  1612. StringRef MachOBindEntry::symbolName() const { return SymbolName; }
  1613. int64_t MachOBindEntry::addend() const { return Addend; }
  1614. uint32_t MachOBindEntry::flags() const { return Flags; }
  1615. int MachOBindEntry::ordinal() const { return Ordinal; }
  1616. bool MachOBindEntry::operator==(const MachOBindEntry &Other) const {
  1617. assert(Opcodes == Other.Opcodes && "compare iterators of different files");
  1618. return (Ptr == Other.Ptr) &&
  1619. (RemainingLoopCount == Other.RemainingLoopCount) &&
  1620. (Done == Other.Done);
  1621. }
  1622. iterator_range<bind_iterator>
  1623. MachOObjectFile::bindTable(ArrayRef<uint8_t> Opcodes, bool is64,
  1624. MachOBindEntry::Kind BKind) {
  1625. MachOBindEntry Start(Opcodes, is64, BKind);
  1626. Start.moveToFirst();
  1627. MachOBindEntry Finish(Opcodes, is64, BKind);
  1628. Finish.moveToEnd();
  1629. return iterator_range<bind_iterator>(bind_iterator(Start),
  1630. bind_iterator(Finish));
  1631. }
  1632. iterator_range<bind_iterator> MachOObjectFile::bindTable() const {
  1633. return bindTable(getDyldInfoBindOpcodes(), is64Bit(),
  1634. MachOBindEntry::Kind::Regular);
  1635. }
  1636. iterator_range<bind_iterator> MachOObjectFile::lazyBindTable() const {
  1637. return bindTable(getDyldInfoLazyBindOpcodes(), is64Bit(),
  1638. MachOBindEntry::Kind::Lazy);
  1639. }
  1640. iterator_range<bind_iterator> MachOObjectFile::weakBindTable() const {
  1641. return bindTable(getDyldInfoWeakBindOpcodes(), is64Bit(),
  1642. MachOBindEntry::Kind::Weak);
  1643. }
  1644. MachOObjectFile::load_command_iterator
  1645. MachOObjectFile::begin_load_commands() const {
  1646. return LoadCommands.begin();
  1647. }
  1648. MachOObjectFile::load_command_iterator
  1649. MachOObjectFile::end_load_commands() const {
  1650. return LoadCommands.end();
  1651. }
  1652. iterator_range<MachOObjectFile::load_command_iterator>
  1653. MachOObjectFile::load_commands() const {
  1654. return iterator_range<load_command_iterator>(begin_load_commands(),
  1655. end_load_commands());
  1656. }
  1657. StringRef
  1658. MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
  1659. ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
  1660. return parseSegmentOrSectionName(Raw.data());
  1661. }
  1662. ArrayRef<char>
  1663. MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
  1664. assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
  1665. const section_base *Base =
  1666. reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
  1667. return makeArrayRef(Base->sectname);
  1668. }
  1669. ArrayRef<char>
  1670. MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
  1671. assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
  1672. const section_base *Base =
  1673. reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
  1674. return makeArrayRef(Base->segname);
  1675. }
  1676. bool
  1677. MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
  1678. const {
  1679. if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
  1680. return false;
  1681. return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
  1682. }
  1683. unsigned MachOObjectFile::getPlainRelocationSymbolNum(
  1684. const MachO::any_relocation_info &RE) const {
  1685. if (isLittleEndian())
  1686. return RE.r_word1 & 0xffffff;
  1687. return RE.r_word1 >> 8;
  1688. }
  1689. bool MachOObjectFile::getPlainRelocationExternal(
  1690. const MachO::any_relocation_info &RE) const {
  1691. if (isLittleEndian())
  1692. return (RE.r_word1 >> 27) & 1;
  1693. return (RE.r_word1 >> 4) & 1;
  1694. }
  1695. bool MachOObjectFile::getScatteredRelocationScattered(
  1696. const MachO::any_relocation_info &RE) const {
  1697. return RE.r_word0 >> 31;
  1698. }
  1699. uint32_t MachOObjectFile::getScatteredRelocationValue(
  1700. const MachO::any_relocation_info &RE) const {
  1701. return RE.r_word1;
  1702. }
  1703. uint32_t MachOObjectFile::getScatteredRelocationType(
  1704. const MachO::any_relocation_info &RE) const {
  1705. return (RE.r_word0 >> 24) & 0xf;
  1706. }
  1707. unsigned MachOObjectFile::getAnyRelocationAddress(
  1708. const MachO::any_relocation_info &RE) const {
  1709. if (isRelocationScattered(RE))
  1710. return getScatteredRelocationAddress(RE);
  1711. return getPlainRelocationAddress(RE);
  1712. }
  1713. unsigned MachOObjectFile::getAnyRelocationPCRel(
  1714. const MachO::any_relocation_info &RE) const {
  1715. if (isRelocationScattered(RE))
  1716. return getScatteredRelocationPCRel(this, RE);
  1717. return getPlainRelocationPCRel(this, RE);
  1718. }
  1719. unsigned MachOObjectFile::getAnyRelocationLength(
  1720. const MachO::any_relocation_info &RE) const {
  1721. if (isRelocationScattered(RE))
  1722. return getScatteredRelocationLength(RE);
  1723. return getPlainRelocationLength(this, RE);
  1724. }
  1725. unsigned
  1726. MachOObjectFile::getAnyRelocationType(
  1727. const MachO::any_relocation_info &RE) const {
  1728. if (isRelocationScattered(RE))
  1729. return getScatteredRelocationType(RE);
  1730. return getPlainRelocationType(this, RE);
  1731. }
  1732. SectionRef
  1733. MachOObjectFile::getAnyRelocationSection(
  1734. const MachO::any_relocation_info &RE) const {
  1735. if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
  1736. return *section_end();
  1737. unsigned SecNum = getPlainRelocationSymbolNum(RE);
  1738. if (SecNum == MachO::R_ABS || SecNum > Sections.size())
  1739. return *section_end();
  1740. DataRefImpl DRI;
  1741. DRI.d.a = SecNum - 1;
  1742. return SectionRef(DRI, this);
  1743. }
  1744. MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
  1745. assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
  1746. return getStruct<MachO::section>(this, Sections[DRI.d.a]);
  1747. }
  1748. MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
  1749. assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
  1750. return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
  1751. }
  1752. MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
  1753. unsigned Index) const {
  1754. const char *Sec = getSectionPtr(this, L, Index);
  1755. return getStruct<MachO::section>(this, Sec);
  1756. }
  1757. MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
  1758. unsigned Index) const {
  1759. const char *Sec = getSectionPtr(this, L, Index);
  1760. return getStruct<MachO::section_64>(this, Sec);
  1761. }
  1762. MachO::nlist
  1763. MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
  1764. const char *P = reinterpret_cast<const char *>(DRI.p);
  1765. return getStruct<MachO::nlist>(this, P);
  1766. }
  1767. MachO::nlist_64
  1768. MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
  1769. const char *P = reinterpret_cast<const char *>(DRI.p);
  1770. return getStruct<MachO::nlist_64>(this, P);
  1771. }
  1772. MachO::linkedit_data_command
  1773. MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
  1774. return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
  1775. }
  1776. MachO::segment_command
  1777. MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
  1778. return getStruct<MachO::segment_command>(this, L.Ptr);
  1779. }
  1780. MachO::segment_command_64
  1781. MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
  1782. return getStruct<MachO::segment_command_64>(this, L.Ptr);
  1783. }
  1784. MachO::linker_option_command
  1785. MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const {
  1786. return getStruct<MachO::linker_option_command>(this, L.Ptr);
  1787. }
  1788. MachO::version_min_command
  1789. MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
  1790. return getStruct<MachO::version_min_command>(this, L.Ptr);
  1791. }
  1792. MachO::dylib_command
  1793. MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const {
  1794. return getStruct<MachO::dylib_command>(this, L.Ptr);
  1795. }
  1796. MachO::dyld_info_command
  1797. MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const {
  1798. return getStruct<MachO::dyld_info_command>(this, L.Ptr);
  1799. }
  1800. MachO::dylinker_command
  1801. MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const {
  1802. return getStruct<MachO::dylinker_command>(this, L.Ptr);
  1803. }
  1804. MachO::uuid_command
  1805. MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const {
  1806. return getStruct<MachO::uuid_command>(this, L.Ptr);
  1807. }
  1808. MachO::rpath_command
  1809. MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const {
  1810. return getStruct<MachO::rpath_command>(this, L.Ptr);
  1811. }
  1812. MachO::source_version_command
  1813. MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const {
  1814. return getStruct<MachO::source_version_command>(this, L.Ptr);
  1815. }
  1816. MachO::entry_point_command
  1817. MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const {
  1818. return getStruct<MachO::entry_point_command>(this, L.Ptr);
  1819. }
  1820. MachO::encryption_info_command
  1821. MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const {
  1822. return getStruct<MachO::encryption_info_command>(this, L.Ptr);
  1823. }
  1824. MachO::encryption_info_command_64
  1825. MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const {
  1826. return getStruct<MachO::encryption_info_command_64>(this, L.Ptr);
  1827. }
  1828. MachO::sub_framework_command
  1829. MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const {
  1830. return getStruct<MachO::sub_framework_command>(this, L.Ptr);
  1831. }
  1832. MachO::sub_umbrella_command
  1833. MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const {
  1834. return getStruct<MachO::sub_umbrella_command>(this, L.Ptr);
  1835. }
  1836. MachO::sub_library_command
  1837. MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const {
  1838. return getStruct<MachO::sub_library_command>(this, L.Ptr);
  1839. }
  1840. MachO::sub_client_command
  1841. MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const {
  1842. return getStruct<MachO::sub_client_command>(this, L.Ptr);
  1843. }
  1844. MachO::routines_command
  1845. MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const {
  1846. return getStruct<MachO::routines_command>(this, L.Ptr);
  1847. }
  1848. MachO::routines_command_64
  1849. MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const {
  1850. return getStruct<MachO::routines_command_64>(this, L.Ptr);
  1851. }
  1852. MachO::thread_command
  1853. MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const {
  1854. return getStruct<MachO::thread_command>(this, L.Ptr);
  1855. }
  1856. MachO::any_relocation_info
  1857. MachOObjectFile::getRelocation(DataRefImpl Rel) const {
  1858. DataRefImpl Sec;
  1859. Sec.d.a = Rel.d.a;
  1860. uint32_t Offset;
  1861. if (is64Bit()) {
  1862. MachO::section_64 Sect = getSection64(Sec);
  1863. Offset = Sect.reloff;
  1864. } else {
  1865. MachO::section Sect = getSection(Sec);
  1866. Offset = Sect.reloff;
  1867. }
  1868. auto P = reinterpret_cast<const MachO::any_relocation_info *>(
  1869. getPtr(this, Offset)) + Rel.d.b;
  1870. return getStruct<MachO::any_relocation_info>(
  1871. this, reinterpret_cast<const char *>(P));
  1872. }
  1873. MachO::data_in_code_entry
  1874. MachOObjectFile::getDice(DataRefImpl Rel) const {
  1875. const char *P = reinterpret_cast<const char *>(Rel.p);
  1876. return getStruct<MachO::data_in_code_entry>(this, P);
  1877. }
  1878. const MachO::mach_header &MachOObjectFile::getHeader() const {
  1879. return Header;
  1880. }
  1881. const MachO::mach_header_64 &MachOObjectFile::getHeader64() const {
  1882. assert(is64Bit());
  1883. return Header64;
  1884. }
  1885. uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
  1886. const MachO::dysymtab_command &DLC,
  1887. unsigned Index) const {
  1888. uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
  1889. return getStruct<uint32_t>(this, getPtr(this, Offset));
  1890. }
  1891. MachO::data_in_code_entry
  1892. MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
  1893. unsigned Index) const {
  1894. uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
  1895. return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
  1896. }
  1897. MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
  1898. if (SymtabLoadCmd)
  1899. return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
  1900. // If there is no SymtabLoadCmd return a load command with zero'ed fields.
  1901. MachO::symtab_command Cmd;
  1902. Cmd.cmd = MachO::LC_SYMTAB;
  1903. Cmd.cmdsize = sizeof(MachO::symtab_command);
  1904. Cmd.symoff = 0;
  1905. Cmd.nsyms = 0;
  1906. Cmd.stroff = 0;
  1907. Cmd.strsize = 0;
  1908. return Cmd;
  1909. }
  1910. MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
  1911. if (DysymtabLoadCmd)
  1912. return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
  1913. // If there is no DysymtabLoadCmd return a load command with zero'ed fields.
  1914. MachO::dysymtab_command Cmd;
  1915. Cmd.cmd = MachO::LC_DYSYMTAB;
  1916. Cmd.cmdsize = sizeof(MachO::dysymtab_command);
  1917. Cmd.ilocalsym = 0;
  1918. Cmd.nlocalsym = 0;
  1919. Cmd.iextdefsym = 0;
  1920. Cmd.nextdefsym = 0;
  1921. Cmd.iundefsym = 0;
  1922. Cmd.nundefsym = 0;
  1923. Cmd.tocoff = 0;
  1924. Cmd.ntoc = 0;
  1925. Cmd.modtaboff = 0;
  1926. Cmd.nmodtab = 0;
  1927. Cmd.extrefsymoff = 0;
  1928. Cmd.nextrefsyms = 0;
  1929. Cmd.indirectsymoff = 0;
  1930. Cmd.nindirectsyms = 0;
  1931. Cmd.extreloff = 0;
  1932. Cmd.nextrel = 0;
  1933. Cmd.locreloff = 0;
  1934. Cmd.nlocrel = 0;
  1935. return Cmd;
  1936. }
  1937. MachO::linkedit_data_command
  1938. MachOObjectFile::getDataInCodeLoadCommand() const {
  1939. if (DataInCodeLoadCmd)
  1940. return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
  1941. // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
  1942. MachO::linkedit_data_command Cmd;
  1943. Cmd.cmd = MachO::LC_DATA_IN_CODE;
  1944. Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
  1945. Cmd.dataoff = 0;
  1946. Cmd.datasize = 0;
  1947. return Cmd;
  1948. }
  1949. MachO::linkedit_data_command
  1950. MachOObjectFile::getLinkOptHintsLoadCommand() const {
  1951. if (LinkOptHintsLoadCmd)
  1952. return getStruct<MachO::linkedit_data_command>(this, LinkOptHintsLoadCmd);
  1953. // If there is no LinkOptHintsLoadCmd return a load command with zero'ed
  1954. // fields.
  1955. MachO::linkedit_data_command Cmd;
  1956. Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT;
  1957. Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
  1958. Cmd.dataoff = 0;
  1959. Cmd.datasize = 0;
  1960. return Cmd;
  1961. }
  1962. ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const {
  1963. if (!DyldInfoLoadCmd)
  1964. return ArrayRef<uint8_t>();
  1965. MachO::dyld_info_command DyldInfo
  1966. = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
  1967. const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
  1968. getPtr(this, DyldInfo.rebase_off));
  1969. return ArrayRef<uint8_t>(Ptr, DyldInfo.rebase_size);
  1970. }
  1971. ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const {
  1972. if (!DyldInfoLoadCmd)
  1973. return ArrayRef<uint8_t>();
  1974. MachO::dyld_info_command DyldInfo
  1975. = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
  1976. const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
  1977. getPtr(this, DyldInfo.bind_off));
  1978. return ArrayRef<uint8_t>(Ptr, DyldInfo.bind_size);
  1979. }
  1980. ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const {
  1981. if (!DyldInfoLoadCmd)
  1982. return ArrayRef<uint8_t>();
  1983. MachO::dyld_info_command DyldInfo
  1984. = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
  1985. const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
  1986. getPtr(this, DyldInfo.weak_bind_off));
  1987. return ArrayRef<uint8_t>(Ptr, DyldInfo.weak_bind_size);
  1988. }
  1989. ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const {
  1990. if (!DyldInfoLoadCmd)
  1991. return ArrayRef<uint8_t>();
  1992. MachO::dyld_info_command DyldInfo
  1993. = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
  1994. const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
  1995. getPtr(this, DyldInfo.lazy_bind_off));
  1996. return ArrayRef<uint8_t>(Ptr, DyldInfo.lazy_bind_size);
  1997. }
  1998. ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const {
  1999. if (!DyldInfoLoadCmd)
  2000. return ArrayRef<uint8_t>();
  2001. MachO::dyld_info_command DyldInfo
  2002. = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
  2003. const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
  2004. getPtr(this, DyldInfo.export_off));
  2005. return ArrayRef<uint8_t>(Ptr, DyldInfo.export_size);
  2006. }
  2007. ArrayRef<uint8_t> MachOObjectFile::getUuid() const {
  2008. if (!UuidLoadCmd)
  2009. return ArrayRef<uint8_t>();
  2010. // Returning a pointer is fine as uuid doesn't need endian swapping.
  2011. const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid);
  2012. return ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Ptr), 16);
  2013. }
  2014. StringRef MachOObjectFile::getStringTableData() const {
  2015. MachO::symtab_command S = getSymtabLoadCommand();
  2016. return getData().substr(S.stroff, S.strsize);
  2017. }
  2018. bool MachOObjectFile::is64Bit() const {
  2019. return getType() == getMachOType(false, true) ||
  2020. getType() == getMachOType(true, true);
  2021. }
  2022. void MachOObjectFile::ReadULEB128s(uint64_t Index,
  2023. SmallVectorImpl<uint64_t> &Out) const {
  2024. DataExtractor extractor(ObjectFile::getData(), true, 0);
  2025. uint32_t offset = Index;
  2026. uint64_t data = 0;
  2027. while (uint64_t delta = extractor.getULEB128(&offset)) {
  2028. data += delta;
  2029. Out.push_back(data);
  2030. }
  2031. }
  2032. bool MachOObjectFile::isRelocatableObject() const {
  2033. return getHeader().filetype == MachO::MH_OBJECT;
  2034. }
  2035. ErrorOr<std::unique_ptr<MachOObjectFile>>
  2036. ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) {
  2037. StringRef Magic = Buffer.getBuffer().slice(0, 4);
  2038. std::error_code EC;
  2039. std::unique_ptr<MachOObjectFile> Ret;
  2040. if (Magic == "\xFE\xED\xFA\xCE")
  2041. Ret.reset(new MachOObjectFile(Buffer, false, false, EC));
  2042. else if (Magic == "\xCE\xFA\xED\xFE")
  2043. Ret.reset(new MachOObjectFile(Buffer, true, false, EC));
  2044. else if (Magic == "\xFE\xED\xFA\xCF")
  2045. Ret.reset(new MachOObjectFile(Buffer, false, true, EC));
  2046. else if (Magic == "\xCF\xFA\xED\xFE")
  2047. Ret.reset(new MachOObjectFile(Buffer, true, true, EC));
  2048. else
  2049. return object_error::parse_failed;
  2050. if (EC)
  2051. return EC;
  2052. return std::move(Ret);
  2053. }