MachOObjectFile.cpp 74 KB

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