MachOObjectFile.cpp 75 KB

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