MachOObjectFile.cpp 75 KB

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