MachOObjectFile.cpp 73 KB

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