MachOObjectFile.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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/Triple.h"
  17. #include "llvm/Support/DataExtractor.h"
  18. #include "llvm/Support/Format.h"
  19. #include "llvm/Support/Host.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <cctype>
  23. #include <cstring>
  24. #include <limits>
  25. using namespace llvm;
  26. using namespace object;
  27. namespace llvm {
  28. namespace object {
  29. struct nlist_base {
  30. uint32_t n_strx;
  31. uint8_t n_type;
  32. uint8_t n_sect;
  33. uint16_t n_desc;
  34. };
  35. struct section_base {
  36. char sectname[16];
  37. char segname[16];
  38. };
  39. template<typename T>
  40. static void SwapValue(T &Value) {
  41. Value = sys::SwapByteOrder(Value);
  42. }
  43. template<typename T>
  44. static void SwapStruct(T &Value);
  45. template<>
  46. void SwapStruct(MachO::any_relocation_info &H) {
  47. SwapValue(H.r_word0);
  48. SwapValue(H.r_word1);
  49. }
  50. template<>
  51. void SwapStruct(MachO::load_command &L) {
  52. SwapValue(L.cmd);
  53. SwapValue(L.cmdsize);
  54. }
  55. template<>
  56. void SwapStruct(nlist_base &S) {
  57. SwapValue(S.n_strx);
  58. SwapValue(S.n_desc);
  59. }
  60. template<>
  61. void SwapStruct(MachO::section &S) {
  62. SwapValue(S.addr);
  63. SwapValue(S.size);
  64. SwapValue(S.offset);
  65. SwapValue(S.align);
  66. SwapValue(S.reloff);
  67. SwapValue(S.nreloc);
  68. SwapValue(S.flags);
  69. SwapValue(S.reserved1);
  70. SwapValue(S.reserved2);
  71. }
  72. template<>
  73. void SwapStruct(MachO::section_64 &S) {
  74. SwapValue(S.addr);
  75. SwapValue(S.size);
  76. SwapValue(S.offset);
  77. SwapValue(S.align);
  78. SwapValue(S.reloff);
  79. SwapValue(S.nreloc);
  80. SwapValue(S.flags);
  81. SwapValue(S.reserved1);
  82. SwapValue(S.reserved2);
  83. SwapValue(S.reserved3);
  84. }
  85. template<>
  86. void SwapStruct(MachO::nlist &S) {
  87. SwapValue(S.n_strx);
  88. SwapValue(S.n_desc);
  89. SwapValue(S.n_value);
  90. }
  91. template<>
  92. void SwapStruct(MachO::nlist_64 &S) {
  93. SwapValue(S.n_strx);
  94. SwapValue(S.n_desc);
  95. SwapValue(S.n_value);
  96. }
  97. template<>
  98. void SwapStruct(MachO::mach_header &H) {
  99. SwapValue(H.magic);
  100. SwapValue(H.cputype);
  101. SwapValue(H.cpusubtype);
  102. SwapValue(H.filetype);
  103. SwapValue(H.ncmds);
  104. SwapValue(H.sizeofcmds);
  105. SwapValue(H.flags);
  106. }
  107. template<>
  108. void SwapStruct(MachO::mach_header_64 &H) {
  109. SwapValue(H.magic);
  110. SwapValue(H.cputype);
  111. SwapValue(H.cpusubtype);
  112. SwapValue(H.filetype);
  113. SwapValue(H.ncmds);
  114. SwapValue(H.sizeofcmds);
  115. SwapValue(H.flags);
  116. SwapValue(H.reserved);
  117. }
  118. template<>
  119. void SwapStruct(MachO::symtab_command &C) {
  120. SwapValue(C.cmd);
  121. SwapValue(C.cmdsize);
  122. SwapValue(C.symoff);
  123. SwapValue(C.nsyms);
  124. SwapValue(C.stroff);
  125. SwapValue(C.strsize);
  126. }
  127. template<>
  128. void SwapStruct(MachO::dysymtab_command &C) {
  129. SwapValue(C.cmd);
  130. SwapValue(C.cmdsize);
  131. SwapValue(C.ilocalsym);
  132. SwapValue(C.nlocalsym);
  133. SwapValue(C.iextdefsym);
  134. SwapValue(C.nextdefsym);
  135. SwapValue(C.iundefsym);
  136. SwapValue(C.nundefsym);
  137. SwapValue(C.tocoff);
  138. SwapValue(C.ntoc);
  139. SwapValue(C.modtaboff);
  140. SwapValue(C.nmodtab);
  141. SwapValue(C.extrefsymoff);
  142. SwapValue(C.nextrefsyms);
  143. SwapValue(C.indirectsymoff);
  144. SwapValue(C.nindirectsyms);
  145. SwapValue(C.extreloff);
  146. SwapValue(C.nextrel);
  147. SwapValue(C.locreloff);
  148. SwapValue(C.nlocrel);
  149. }
  150. template<>
  151. void SwapStruct(MachO::linkedit_data_command &C) {
  152. SwapValue(C.cmd);
  153. SwapValue(C.cmdsize);
  154. SwapValue(C.dataoff);
  155. SwapValue(C.datasize);
  156. }
  157. template<>
  158. void SwapStruct(MachO::segment_command &C) {
  159. SwapValue(C.cmd);
  160. SwapValue(C.cmdsize);
  161. SwapValue(C.vmaddr);
  162. SwapValue(C.vmsize);
  163. SwapValue(C.fileoff);
  164. SwapValue(C.filesize);
  165. SwapValue(C.maxprot);
  166. SwapValue(C.initprot);
  167. SwapValue(C.nsects);
  168. SwapValue(C.flags);
  169. }
  170. template<>
  171. void SwapStruct(MachO::segment_command_64 &C) {
  172. SwapValue(C.cmd);
  173. SwapValue(C.cmdsize);
  174. SwapValue(C.vmaddr);
  175. SwapValue(C.vmsize);
  176. SwapValue(C.fileoff);
  177. SwapValue(C.filesize);
  178. SwapValue(C.maxprot);
  179. SwapValue(C.initprot);
  180. SwapValue(C.nsects);
  181. SwapValue(C.flags);
  182. }
  183. template<>
  184. void SwapStruct(uint32_t &C) {
  185. SwapValue(C);
  186. }
  187. template<>
  188. void SwapStruct(MachO::linker_options_command &C) {
  189. SwapValue(C.cmd);
  190. SwapValue(C.cmdsize);
  191. SwapValue(C.count);
  192. }
  193. template<>
  194. void SwapStruct(MachO::version_min_command&C) {
  195. SwapValue(C.cmd);
  196. SwapValue(C.cmdsize);
  197. SwapValue(C.version);
  198. SwapValue(C.reserved);
  199. }
  200. template<>
  201. void SwapStruct(MachO::data_in_code_entry &C) {
  202. SwapValue(C.offset);
  203. SwapValue(C.length);
  204. SwapValue(C.kind);
  205. }
  206. template<typename T>
  207. T getStruct(const MachOObjectFile *O, const char *P) {
  208. T Cmd;
  209. memcpy(&Cmd, P, sizeof(T));
  210. if (O->isLittleEndian() != sys::IsLittleEndianHost)
  211. SwapStruct(Cmd);
  212. return Cmd;
  213. }
  214. static uint32_t
  215. getSegmentLoadCommandNumSections(const MachOObjectFile *O,
  216. const MachOObjectFile::LoadCommandInfo &L) {
  217. if (O->is64Bit()) {
  218. MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
  219. return S.nsects;
  220. }
  221. MachO::segment_command S = O->getSegmentLoadCommand(L);
  222. return S.nsects;
  223. }
  224. static const char *
  225. getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
  226. unsigned Sec) {
  227. uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
  228. bool Is64 = O->is64Bit();
  229. unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
  230. sizeof(MachO::segment_command);
  231. unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
  232. sizeof(MachO::section);
  233. uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
  234. return reinterpret_cast<const char*>(SectionAddr);
  235. }
  236. static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
  237. return O->getData().substr(Offset, 1).data();
  238. }
  239. static nlist_base
  240. getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
  241. const char *P = reinterpret_cast<const char *>(DRI.p);
  242. return getStruct<nlist_base>(O, P);
  243. }
  244. static StringRef parseSegmentOrSectionName(const char *P) {
  245. if (P[15] == 0)
  246. // Null terminated.
  247. return P;
  248. // Not null terminated, so this is a 16 char string.
  249. return StringRef(P, 16);
  250. }
  251. // Helper to advance a section or symbol iterator multiple increments at a time.
  252. template<class T>
  253. static void advance(T &it, size_t Val) {
  254. while (Val--)
  255. ++it;
  256. }
  257. static unsigned getCPUType(const MachOObjectFile *O) {
  258. return O->getHeader().cputype;
  259. }
  260. static void printRelocationTargetName(const MachOObjectFile *O,
  261. const MachO::any_relocation_info &RE,
  262. raw_string_ostream &fmt) {
  263. bool IsScattered = O->isRelocationScattered(RE);
  264. // Target of a scattered relocation is an address. In the interest of
  265. // generating pretty output, scan through the symbol table looking for a
  266. // symbol that aligns with that address. If we find one, print it.
  267. // Otherwise, we just print the hex address of the target.
  268. if (IsScattered) {
  269. uint32_t Val = O->getPlainRelocationSymbolNum(RE);
  270. for (const SymbolRef &Symbol : O->symbols()) {
  271. error_code ec;
  272. uint64_t Addr;
  273. StringRef Name;
  274. if ((ec = Symbol.getAddress(Addr)))
  275. report_fatal_error(ec.message());
  276. if (Addr != Val)
  277. continue;
  278. if ((ec = Symbol.getName(Name)))
  279. report_fatal_error(ec.message());
  280. fmt << Name;
  281. return;
  282. }
  283. // If we couldn't find a symbol that this relocation refers to, try
  284. // to find a section beginning instead.
  285. for (const SectionRef &Section : O->sections()) {
  286. error_code ec;
  287. uint64_t Addr;
  288. StringRef Name;
  289. if ((ec = Section.getAddress(Addr)))
  290. report_fatal_error(ec.message());
  291. if (Addr != Val)
  292. continue;
  293. if ((ec = Section.getName(Name)))
  294. report_fatal_error(ec.message());
  295. fmt << Name;
  296. return;
  297. }
  298. fmt << format("0x%x", Val);
  299. return;
  300. }
  301. StringRef S;
  302. bool isExtern = O->getPlainRelocationExternal(RE);
  303. uint64_t Val = O->getPlainRelocationSymbolNum(RE);
  304. if (isExtern) {
  305. symbol_iterator SI = O->symbol_begin();
  306. advance(SI, Val);
  307. SI->getName(S);
  308. } else {
  309. section_iterator SI = O->section_begin();
  310. // Adjust for the fact that sections are 1-indexed.
  311. advance(SI, Val - 1);
  312. SI->getName(S);
  313. }
  314. fmt << S;
  315. }
  316. static uint32_t
  317. getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
  318. return RE.r_word0;
  319. }
  320. static unsigned
  321. getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
  322. return RE.r_word0 & 0xffffff;
  323. }
  324. static bool getPlainRelocationPCRel(const MachOObjectFile *O,
  325. const MachO::any_relocation_info &RE) {
  326. if (O->isLittleEndian())
  327. return (RE.r_word1 >> 24) & 1;
  328. return (RE.r_word1 >> 7) & 1;
  329. }
  330. static bool
  331. getScatteredRelocationPCRel(const MachOObjectFile *O,
  332. const MachO::any_relocation_info &RE) {
  333. return (RE.r_word0 >> 30) & 1;
  334. }
  335. static unsigned getPlainRelocationLength(const MachOObjectFile *O,
  336. const MachO::any_relocation_info &RE) {
  337. if (O->isLittleEndian())
  338. return (RE.r_word1 >> 25) & 3;
  339. return (RE.r_word1 >> 5) & 3;
  340. }
  341. static unsigned
  342. getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
  343. return (RE.r_word0 >> 28) & 3;
  344. }
  345. static unsigned getPlainRelocationType(const MachOObjectFile *O,
  346. const MachO::any_relocation_info &RE) {
  347. if (O->isLittleEndian())
  348. return RE.r_word1 >> 28;
  349. return RE.r_word1 & 0xf;
  350. }
  351. static unsigned
  352. getScatteredRelocationType(const MachO::any_relocation_info &RE) {
  353. return (RE.r_word0 >> 24) & 0xf;
  354. }
  355. static uint32_t getSectionFlags(const MachOObjectFile *O,
  356. DataRefImpl Sec) {
  357. if (O->is64Bit()) {
  358. MachO::section_64 Sect = O->getSection64(Sec);
  359. return Sect.flags;
  360. }
  361. MachO::section Sect = O->getSection(Sec);
  362. return Sect.flags;
  363. }
  364. MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
  365. bool Is64bits, error_code &EC,
  366. bool BufferOwned)
  367. : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned),
  368. SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
  369. DataInCodeLoadCmd(nullptr) {
  370. uint32_t LoadCommandCount = this->getHeader().ncmds;
  371. MachO::LoadCommandType SegmentLoadType = is64Bit() ?
  372. MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
  373. MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
  374. for (unsigned I = 0; ; ++I) {
  375. if (Load.C.cmd == MachO::LC_SYMTAB) {
  376. assert(!SymtabLoadCmd && "Multiple symbol tables");
  377. SymtabLoadCmd = Load.Ptr;
  378. } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
  379. assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
  380. DysymtabLoadCmd = Load.Ptr;
  381. } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
  382. assert(!DataInCodeLoadCmd && "Multiple data in code tables");
  383. DataInCodeLoadCmd = Load.Ptr;
  384. } else if (Load.C.cmd == SegmentLoadType) {
  385. uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
  386. for (unsigned J = 0; J < NumSections; ++J) {
  387. const char *Sec = getSectionPtr(this, Load, J);
  388. Sections.push_back(Sec);
  389. }
  390. }
  391. if (I == LoadCommandCount - 1)
  392. break;
  393. else
  394. Load = getNextLoadCommandInfo(Load);
  395. }
  396. }
  397. void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
  398. unsigned SymbolTableEntrySize = is64Bit() ?
  399. sizeof(MachO::nlist_64) :
  400. sizeof(MachO::nlist);
  401. Symb.p += SymbolTableEntrySize;
  402. }
  403. error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
  404. StringRef &Res) const {
  405. StringRef StringTable = getStringTableData();
  406. nlist_base Entry = getSymbolTableEntryBase(this, Symb);
  407. const char *Start = &StringTable.data()[Entry.n_strx];
  408. Res = StringRef(Start);
  409. return object_error::success;
  410. }
  411. error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
  412. uint64_t &Res) const {
  413. if (is64Bit()) {
  414. MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
  415. Res = Entry.n_value;
  416. } else {
  417. MachO::nlist Entry = getSymbolTableEntry(Symb);
  418. Res = Entry.n_value;
  419. }
  420. return object_error::success;
  421. }
  422. error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
  423. uint32_t &Result) const {
  424. uint32_t flags = getSymbolFlags(DRI);
  425. if (flags & SymbolRef::SF_Common) {
  426. nlist_base Entry = getSymbolTableEntryBase(this, DRI);
  427. Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
  428. } else {
  429. Result = 0;
  430. }
  431. return object_error::success;
  432. }
  433. error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
  434. uint64_t &Result) const {
  435. uint64_t BeginOffset;
  436. uint64_t EndOffset = 0;
  437. uint8_t SectionIndex;
  438. nlist_base Entry = getSymbolTableEntryBase(this, DRI);
  439. uint64_t Value;
  440. getSymbolAddress(DRI, Value);
  441. BeginOffset = Value;
  442. SectionIndex = Entry.n_sect;
  443. if (!SectionIndex) {
  444. uint32_t flags = getSymbolFlags(DRI);
  445. if (flags & SymbolRef::SF_Common)
  446. Result = Value;
  447. else
  448. Result = UnknownAddressOrSize;
  449. return object_error::success;
  450. }
  451. // Unfortunately symbols are unsorted so we need to touch all
  452. // symbols from load command
  453. for (const SymbolRef &Symbol : symbols()) {
  454. DataRefImpl DRI = Symbol.getRawDataRefImpl();
  455. Entry = getSymbolTableEntryBase(this, DRI);
  456. getSymbolAddress(DRI, Value);
  457. if (Entry.n_sect == SectionIndex && Value > BeginOffset)
  458. if (!EndOffset || Value < EndOffset)
  459. EndOffset = Value;
  460. }
  461. if (!EndOffset) {
  462. uint64_t Size;
  463. DataRefImpl Sec;
  464. Sec.d.a = SectionIndex-1;
  465. getSectionSize(Sec, Size);
  466. getSectionAddress(Sec, EndOffset);
  467. EndOffset += Size;
  468. }
  469. Result = EndOffset - BeginOffset;
  470. return object_error::success;
  471. }
  472. error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
  473. SymbolRef::Type &Res) const {
  474. nlist_base Entry = getSymbolTableEntryBase(this, Symb);
  475. uint8_t n_type = Entry.n_type;
  476. Res = SymbolRef::ST_Other;
  477. // If this is a STAB debugging symbol, we can do nothing more.
  478. if (n_type & MachO::N_STAB) {
  479. Res = SymbolRef::ST_Debug;
  480. return object_error::success;
  481. }
  482. switch (n_type & MachO::N_TYPE) {
  483. case MachO::N_UNDF :
  484. Res = SymbolRef::ST_Unknown;
  485. break;
  486. case MachO::N_SECT :
  487. Res = SymbolRef::ST_Function;
  488. break;
  489. }
  490. return object_error::success;
  491. }
  492. uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
  493. nlist_base Entry = getSymbolTableEntryBase(this, DRI);
  494. uint8_t MachOType = Entry.n_type;
  495. uint16_t MachOFlags = Entry.n_desc;
  496. uint32_t Result = SymbolRef::SF_None;
  497. if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
  498. Result |= SymbolRef::SF_Undefined;
  499. if (MachOType & MachO::N_STAB)
  500. Result |= SymbolRef::SF_FormatSpecific;
  501. if (MachOType & MachO::N_EXT) {
  502. Result |= SymbolRef::SF_Global;
  503. if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
  504. uint64_t Value;
  505. getSymbolAddress(DRI, Value);
  506. if (Value)
  507. Result |= SymbolRef::SF_Common;
  508. }
  509. }
  510. if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
  511. Result |= SymbolRef::SF_Weak;
  512. if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
  513. Result |= SymbolRef::SF_Absolute;
  514. return Result;
  515. }
  516. error_code
  517. MachOObjectFile::getSymbolSection(DataRefImpl Symb,
  518. section_iterator &Res) const {
  519. nlist_base Entry = getSymbolTableEntryBase(this, Symb);
  520. uint8_t index = Entry.n_sect;
  521. if (index == 0) {
  522. Res = section_end();
  523. } else {
  524. DataRefImpl DRI;
  525. DRI.d.a = index - 1;
  526. Res = section_iterator(SectionRef(DRI, this));
  527. }
  528. return object_error::success;
  529. }
  530. void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
  531. Sec.d.a++;
  532. }
  533. error_code
  534. MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
  535. ArrayRef<char> Raw = getSectionRawName(Sec);
  536. Result = parseSegmentOrSectionName(Raw.data());
  537. return object_error::success;
  538. }
  539. error_code
  540. MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
  541. if (is64Bit()) {
  542. MachO::section_64 Sect = getSection64(Sec);
  543. Res = Sect.addr;
  544. } else {
  545. MachO::section Sect = getSection(Sec);
  546. Res = Sect.addr;
  547. }
  548. return object_error::success;
  549. }
  550. error_code
  551. MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
  552. if (is64Bit()) {
  553. MachO::section_64 Sect = getSection64(Sec);
  554. Res = Sect.size;
  555. } else {
  556. MachO::section Sect = getSection(Sec);
  557. Res = Sect.size;
  558. }
  559. return object_error::success;
  560. }
  561. error_code
  562. MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
  563. uint32_t Offset;
  564. uint64_t Size;
  565. if (is64Bit()) {
  566. MachO::section_64 Sect = getSection64(Sec);
  567. Offset = Sect.offset;
  568. Size = Sect.size;
  569. } else {
  570. MachO::section Sect = getSection(Sec);
  571. Offset = Sect.offset;
  572. Size = Sect.size;
  573. }
  574. Res = this->getData().substr(Offset, Size);
  575. return object_error::success;
  576. }
  577. error_code
  578. MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
  579. uint32_t Align;
  580. if (is64Bit()) {
  581. MachO::section_64 Sect = getSection64(Sec);
  582. Align = Sect.align;
  583. } else {
  584. MachO::section Sect = getSection(Sec);
  585. Align = Sect.align;
  586. }
  587. Res = uint64_t(1) << Align;
  588. return object_error::success;
  589. }
  590. error_code
  591. MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
  592. uint32_t Flags = getSectionFlags(this, Sec);
  593. Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
  594. return object_error::success;
  595. }
  596. error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
  597. // FIXME: Unimplemented.
  598. Result = false;
  599. return object_error::success;
  600. }
  601. error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
  602. // FIXME: Unimplemented.
  603. Result = false;
  604. return object_error::success;
  605. }
  606. error_code
  607. MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
  608. bool &Result) const {
  609. // FIXME: Unimplemented.
  610. Result = true;
  611. return object_error::success;
  612. }
  613. error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
  614. bool &Result) const {
  615. // FIXME: Unimplemented.
  616. Result = false;
  617. return object_error::success;
  618. }
  619. error_code
  620. MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
  621. uint32_t Flags = getSectionFlags(this, Sec);
  622. unsigned SectionType = Flags & MachO::SECTION_TYPE;
  623. Res = SectionType == MachO::S_ZEROFILL ||
  624. SectionType == MachO::S_GB_ZEROFILL;
  625. return object_error::success;
  626. }
  627. error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
  628. bool &Result) const {
  629. // Consider using the code from isSectionText to look for __const sections.
  630. // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
  631. // to use section attributes to distinguish code from data.
  632. // FIXME: Unimplemented.
  633. Result = false;
  634. return object_error::success;
  635. }
  636. error_code
  637. MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
  638. bool &Result) const {
  639. SymbolRef::Type ST;
  640. this->getSymbolType(Symb, ST);
  641. if (ST == SymbolRef::ST_Unknown) {
  642. Result = false;
  643. return object_error::success;
  644. }
  645. uint64_t SectBegin, SectEnd;
  646. getSectionAddress(Sec, SectBegin);
  647. getSectionSize(Sec, SectEnd);
  648. SectEnd += SectBegin;
  649. uint64_t SymAddr;
  650. getSymbolAddress(Symb, SymAddr);
  651. Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
  652. return object_error::success;
  653. }
  654. relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
  655. DataRefImpl Ret;
  656. Ret.d.a = Sec.d.a;
  657. Ret.d.b = 0;
  658. return relocation_iterator(RelocationRef(Ret, this));
  659. }
  660. relocation_iterator
  661. MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
  662. uint32_t Num;
  663. if (is64Bit()) {
  664. MachO::section_64 Sect = getSection64(Sec);
  665. Num = Sect.nreloc;
  666. } else {
  667. MachO::section Sect = getSection(Sec);
  668. Num = Sect.nreloc;
  669. }
  670. DataRefImpl Ret;
  671. Ret.d.a = Sec.d.a;
  672. Ret.d.b = Num;
  673. return relocation_iterator(RelocationRef(Ret, this));
  674. }
  675. void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
  676. ++Rel.d.b;
  677. }
  678. error_code
  679. MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
  680. uint64_t Offset;
  681. getRelocationOffset(Rel, Offset);
  682. DataRefImpl Sec;
  683. Sec.d.a = Rel.d.a;
  684. uint64_t SecAddress;
  685. getSectionAddress(Sec, SecAddress);
  686. Res = SecAddress + Offset;
  687. return object_error::success;
  688. }
  689. error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
  690. uint64_t &Res) const {
  691. assert(getHeader().filetype == MachO::MH_OBJECT &&
  692. "Only implemented for MH_OBJECT");
  693. MachO::any_relocation_info RE = getRelocation(Rel);
  694. Res = getAnyRelocationAddress(RE);
  695. return object_error::success;
  696. }
  697. symbol_iterator
  698. MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
  699. MachO::any_relocation_info RE = getRelocation(Rel);
  700. uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
  701. bool isExtern = getPlainRelocationExternal(RE);
  702. if (!isExtern)
  703. return symbol_end();
  704. MachO::symtab_command S = getSymtabLoadCommand();
  705. unsigned SymbolTableEntrySize = is64Bit() ?
  706. sizeof(MachO::nlist_64) :
  707. sizeof(MachO::nlist);
  708. uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
  709. DataRefImpl Sym;
  710. Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
  711. return symbol_iterator(SymbolRef(Sym, this));
  712. }
  713. error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
  714. uint64_t &Res) const {
  715. MachO::any_relocation_info RE = getRelocation(Rel);
  716. Res = getAnyRelocationType(RE);
  717. return object_error::success;
  718. }
  719. error_code
  720. MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
  721. SmallVectorImpl<char> &Result) const {
  722. StringRef res;
  723. uint64_t RType;
  724. getRelocationType(Rel, RType);
  725. unsigned Arch = this->getArch();
  726. switch (Arch) {
  727. case Triple::x86: {
  728. static const char *const Table[] = {
  729. "GENERIC_RELOC_VANILLA",
  730. "GENERIC_RELOC_PAIR",
  731. "GENERIC_RELOC_SECTDIFF",
  732. "GENERIC_RELOC_PB_LA_PTR",
  733. "GENERIC_RELOC_LOCAL_SECTDIFF",
  734. "GENERIC_RELOC_TLV" };
  735. if (RType > 5)
  736. res = "Unknown";
  737. else
  738. res = Table[RType];
  739. break;
  740. }
  741. case Triple::x86_64: {
  742. static const char *const Table[] = {
  743. "X86_64_RELOC_UNSIGNED",
  744. "X86_64_RELOC_SIGNED",
  745. "X86_64_RELOC_BRANCH",
  746. "X86_64_RELOC_GOT_LOAD",
  747. "X86_64_RELOC_GOT",
  748. "X86_64_RELOC_SUBTRACTOR",
  749. "X86_64_RELOC_SIGNED_1",
  750. "X86_64_RELOC_SIGNED_2",
  751. "X86_64_RELOC_SIGNED_4",
  752. "X86_64_RELOC_TLV" };
  753. if (RType > 9)
  754. res = "Unknown";
  755. else
  756. res = Table[RType];
  757. break;
  758. }
  759. case Triple::arm: {
  760. static const char *const Table[] = {
  761. "ARM_RELOC_VANILLA",
  762. "ARM_RELOC_PAIR",
  763. "ARM_RELOC_SECTDIFF",
  764. "ARM_RELOC_LOCAL_SECTDIFF",
  765. "ARM_RELOC_PB_LA_PTR",
  766. "ARM_RELOC_BR24",
  767. "ARM_THUMB_RELOC_BR22",
  768. "ARM_THUMB_32BIT_BRANCH",
  769. "ARM_RELOC_HALF",
  770. "ARM_RELOC_HALF_SECTDIFF" };
  771. if (RType > 9)
  772. res = "Unknown";
  773. else
  774. res = Table[RType];
  775. break;
  776. }
  777. case Triple::arm64:
  778. case Triple::aarch64: {
  779. static const char *const Table[] = {
  780. "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
  781. "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
  782. "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
  783. "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
  784. "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
  785. "ARM64_RELOC_ADDEND"
  786. };
  787. if (RType >= array_lengthof(Table))
  788. res = "Unknown";
  789. else
  790. res = Table[RType];
  791. break;
  792. }
  793. case Triple::ppc: {
  794. static const char *const Table[] = {
  795. "PPC_RELOC_VANILLA",
  796. "PPC_RELOC_PAIR",
  797. "PPC_RELOC_BR14",
  798. "PPC_RELOC_BR24",
  799. "PPC_RELOC_HI16",
  800. "PPC_RELOC_LO16",
  801. "PPC_RELOC_HA16",
  802. "PPC_RELOC_LO14",
  803. "PPC_RELOC_SECTDIFF",
  804. "PPC_RELOC_PB_LA_PTR",
  805. "PPC_RELOC_HI16_SECTDIFF",
  806. "PPC_RELOC_LO16_SECTDIFF",
  807. "PPC_RELOC_HA16_SECTDIFF",
  808. "PPC_RELOC_JBSR",
  809. "PPC_RELOC_LO14_SECTDIFF",
  810. "PPC_RELOC_LOCAL_SECTDIFF" };
  811. if (RType > 15)
  812. res = "Unknown";
  813. else
  814. res = Table[RType];
  815. break;
  816. }
  817. case Triple::UnknownArch:
  818. res = "Unknown";
  819. break;
  820. }
  821. Result.append(res.begin(), res.end());
  822. return object_error::success;
  823. }
  824. error_code
  825. MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
  826. SmallVectorImpl<char> &Result) const {
  827. MachO::any_relocation_info RE = getRelocation(Rel);
  828. unsigned Arch = this->getArch();
  829. std::string fmtbuf;
  830. raw_string_ostream fmt(fmtbuf);
  831. unsigned Type = this->getAnyRelocationType(RE);
  832. bool IsPCRel = this->getAnyRelocationPCRel(RE);
  833. // Determine any addends that should be displayed with the relocation.
  834. // These require decoding the relocation type, which is triple-specific.
  835. // X86_64 has entirely custom relocation types.
  836. if (Arch == Triple::x86_64) {
  837. bool isPCRel = getAnyRelocationPCRel(RE);
  838. switch (Type) {
  839. case MachO::X86_64_RELOC_GOT_LOAD:
  840. case MachO::X86_64_RELOC_GOT: {
  841. printRelocationTargetName(this, RE, fmt);
  842. fmt << "@GOT";
  843. if (isPCRel) fmt << "PCREL";
  844. break;
  845. }
  846. case MachO::X86_64_RELOC_SUBTRACTOR: {
  847. DataRefImpl RelNext = Rel;
  848. moveRelocationNext(RelNext);
  849. MachO::any_relocation_info RENext = getRelocation(RelNext);
  850. // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
  851. // X86_64_RELOC_UNSIGNED.
  852. // NOTE: Scattered relocations don't exist on x86_64.
  853. unsigned RType = getAnyRelocationType(RENext);
  854. if (RType != MachO::X86_64_RELOC_UNSIGNED)
  855. report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
  856. "X86_64_RELOC_SUBTRACTOR.");
  857. // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
  858. // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
  859. printRelocationTargetName(this, RENext, fmt);
  860. fmt << "-";
  861. printRelocationTargetName(this, RE, fmt);
  862. break;
  863. }
  864. case MachO::X86_64_RELOC_TLV:
  865. printRelocationTargetName(this, RE, fmt);
  866. fmt << "@TLV";
  867. if (isPCRel) fmt << "P";
  868. break;
  869. case MachO::X86_64_RELOC_SIGNED_1:
  870. printRelocationTargetName(this, RE, fmt);
  871. fmt << "-1";
  872. break;
  873. case MachO::X86_64_RELOC_SIGNED_2:
  874. printRelocationTargetName(this, RE, fmt);
  875. fmt << "-2";
  876. break;
  877. case MachO::X86_64_RELOC_SIGNED_4:
  878. printRelocationTargetName(this, RE, fmt);
  879. fmt << "-4";
  880. break;
  881. default:
  882. printRelocationTargetName(this, RE, fmt);
  883. break;
  884. }
  885. // X86 and ARM share some relocation types in common.
  886. } else if (Arch == Triple::x86 || Arch == Triple::arm ||
  887. Arch == Triple::ppc) {
  888. // Generic relocation types...
  889. switch (Type) {
  890. case MachO::GENERIC_RELOC_PAIR: // prints no info
  891. return object_error::success;
  892. case MachO::GENERIC_RELOC_SECTDIFF: {
  893. DataRefImpl RelNext = Rel;
  894. moveRelocationNext(RelNext);
  895. MachO::any_relocation_info RENext = getRelocation(RelNext);
  896. // X86 sect diff's must be followed by a relocation of type
  897. // GENERIC_RELOC_PAIR.
  898. unsigned RType = getAnyRelocationType(RENext);
  899. if (RType != MachO::GENERIC_RELOC_PAIR)
  900. report_fatal_error("Expected GENERIC_RELOC_PAIR after "
  901. "GENERIC_RELOC_SECTDIFF.");
  902. printRelocationTargetName(this, RE, fmt);
  903. fmt << "-";
  904. printRelocationTargetName(this, RENext, fmt);
  905. break;
  906. }
  907. }
  908. if (Arch == Triple::x86 || Arch == Triple::ppc) {
  909. switch (Type) {
  910. case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
  911. DataRefImpl RelNext = Rel;
  912. moveRelocationNext(RelNext);
  913. MachO::any_relocation_info RENext = getRelocation(RelNext);
  914. // X86 sect diff's must be followed by a relocation of type
  915. // GENERIC_RELOC_PAIR.
  916. unsigned RType = getAnyRelocationType(RENext);
  917. if (RType != MachO::GENERIC_RELOC_PAIR)
  918. report_fatal_error("Expected GENERIC_RELOC_PAIR after "
  919. "GENERIC_RELOC_LOCAL_SECTDIFF.");
  920. printRelocationTargetName(this, RE, fmt);
  921. fmt << "-";
  922. printRelocationTargetName(this, RENext, fmt);
  923. break;
  924. }
  925. case MachO::GENERIC_RELOC_TLV: {
  926. printRelocationTargetName(this, RE, fmt);
  927. fmt << "@TLV";
  928. if (IsPCRel) fmt << "P";
  929. break;
  930. }
  931. default:
  932. printRelocationTargetName(this, RE, fmt);
  933. }
  934. } else { // ARM-specific relocations
  935. switch (Type) {
  936. case MachO::ARM_RELOC_HALF:
  937. case MachO::ARM_RELOC_HALF_SECTDIFF: {
  938. // Half relocations steal a bit from the length field to encode
  939. // whether this is an upper16 or a lower16 relocation.
  940. bool isUpper = getAnyRelocationLength(RE) >> 1;
  941. if (isUpper)
  942. fmt << ":upper16:(";
  943. else
  944. fmt << ":lower16:(";
  945. printRelocationTargetName(this, RE, fmt);
  946. DataRefImpl RelNext = Rel;
  947. moveRelocationNext(RelNext);
  948. MachO::any_relocation_info RENext = getRelocation(RelNext);
  949. // ARM half relocs must be followed by a relocation of type
  950. // ARM_RELOC_PAIR.
  951. unsigned RType = getAnyRelocationType(RENext);
  952. if (RType != MachO::ARM_RELOC_PAIR)
  953. report_fatal_error("Expected ARM_RELOC_PAIR after "
  954. "ARM_RELOC_HALF");
  955. // NOTE: The half of the target virtual address is stashed in the
  956. // address field of the secondary relocation, but we can't reverse
  957. // engineer the constant offset from it without decoding the movw/movt
  958. // instruction to find the other half in its immediate field.
  959. // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
  960. // symbol/section pointer of the follow-on relocation.
  961. if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
  962. fmt << "-";
  963. printRelocationTargetName(this, RENext, fmt);
  964. }
  965. fmt << ")";
  966. break;
  967. }
  968. default: {
  969. printRelocationTargetName(this, RE, fmt);
  970. }
  971. }
  972. }
  973. } else
  974. printRelocationTargetName(this, RE, fmt);
  975. fmt.flush();
  976. Result.append(fmtbuf.begin(), fmtbuf.end());
  977. return object_error::success;
  978. }
  979. error_code
  980. MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
  981. unsigned Arch = getArch();
  982. uint64_t Type;
  983. getRelocationType(Rel, Type);
  984. Result = false;
  985. // On arches that use the generic relocations, GENERIC_RELOC_PAIR
  986. // is always hidden.
  987. if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
  988. if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
  989. } else if (Arch == Triple::x86_64) {
  990. // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
  991. // an X86_64_RELOC_SUBTRACTOR.
  992. if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
  993. DataRefImpl RelPrev = Rel;
  994. RelPrev.d.a--;
  995. uint64_t PrevType;
  996. getRelocationType(RelPrev, PrevType);
  997. if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
  998. Result = true;
  999. }
  1000. }
  1001. return object_error::success;
  1002. }
  1003. error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
  1004. LibraryRef &Res) const {
  1005. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  1006. }
  1007. error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
  1008. StringRef &Res) const {
  1009. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  1010. }
  1011. basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
  1012. DataRefImpl DRI;
  1013. if (!SymtabLoadCmd)
  1014. return basic_symbol_iterator(SymbolRef(DRI, this));
  1015. MachO::symtab_command Symtab = getSymtabLoadCommand();
  1016. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
  1017. return basic_symbol_iterator(SymbolRef(DRI, this));
  1018. }
  1019. basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
  1020. DataRefImpl DRI;
  1021. if (!SymtabLoadCmd)
  1022. return basic_symbol_iterator(SymbolRef(DRI, this));
  1023. MachO::symtab_command Symtab = getSymtabLoadCommand();
  1024. unsigned SymbolTableEntrySize = is64Bit() ?
  1025. sizeof(MachO::nlist_64) :
  1026. sizeof(MachO::nlist);
  1027. unsigned Offset = Symtab.symoff +
  1028. Symtab.nsyms * SymbolTableEntrySize;
  1029. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
  1030. return basic_symbol_iterator(SymbolRef(DRI, this));
  1031. }
  1032. section_iterator MachOObjectFile::section_begin() const {
  1033. DataRefImpl DRI;
  1034. return section_iterator(SectionRef(DRI, this));
  1035. }
  1036. section_iterator MachOObjectFile::section_end() const {
  1037. DataRefImpl DRI;
  1038. DRI.d.a = Sections.size();
  1039. return section_iterator(SectionRef(DRI, this));
  1040. }
  1041. library_iterator MachOObjectFile::needed_library_begin() const {
  1042. // TODO: implement
  1043. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  1044. }
  1045. library_iterator MachOObjectFile::needed_library_end() const {
  1046. // TODO: implement
  1047. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  1048. }
  1049. uint8_t MachOObjectFile::getBytesInAddress() const {
  1050. return is64Bit() ? 8 : 4;
  1051. }
  1052. StringRef MachOObjectFile::getFileFormatName() const {
  1053. unsigned CPUType = getCPUType(this);
  1054. if (!is64Bit()) {
  1055. switch (CPUType) {
  1056. case llvm::MachO::CPU_TYPE_I386:
  1057. return "Mach-O 32-bit i386";
  1058. case llvm::MachO::CPU_TYPE_ARM:
  1059. return "Mach-O arm";
  1060. case llvm::MachO::CPU_TYPE_POWERPC:
  1061. return "Mach-O 32-bit ppc";
  1062. default:
  1063. assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
  1064. "64-bit object file when we're not 64-bit?");
  1065. return "Mach-O 32-bit unknown";
  1066. }
  1067. }
  1068. // Make sure the cpu type has the correct mask.
  1069. assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
  1070. == llvm::MachO::CPU_ARCH_ABI64 &&
  1071. "32-bit object file when we're 64-bit?");
  1072. switch (CPUType) {
  1073. case llvm::MachO::CPU_TYPE_X86_64:
  1074. return "Mach-O 64-bit x86-64";
  1075. case llvm::MachO::CPU_TYPE_ARM64:
  1076. return "Mach-O arm64";
  1077. case llvm::MachO::CPU_TYPE_POWERPC64:
  1078. return "Mach-O 64-bit ppc64";
  1079. default:
  1080. return "Mach-O 64-bit unknown";
  1081. }
  1082. }
  1083. Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
  1084. switch (CPUType) {
  1085. case llvm::MachO::CPU_TYPE_I386:
  1086. return Triple::x86;
  1087. case llvm::MachO::CPU_TYPE_X86_64:
  1088. return Triple::x86_64;
  1089. case llvm::MachO::CPU_TYPE_ARM:
  1090. return Triple::arm;
  1091. case llvm::MachO::CPU_TYPE_ARM64:
  1092. return Triple::arm64;
  1093. case llvm::MachO::CPU_TYPE_POWERPC:
  1094. return Triple::ppc;
  1095. case llvm::MachO::CPU_TYPE_POWERPC64:
  1096. return Triple::ppc64;
  1097. default:
  1098. return Triple::UnknownArch;
  1099. }
  1100. }
  1101. unsigned MachOObjectFile::getArch() const {
  1102. return getArch(getCPUType(this));
  1103. }
  1104. StringRef MachOObjectFile::getLoadName() const {
  1105. // TODO: Implement
  1106. report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
  1107. }
  1108. relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
  1109. DataRefImpl DRI;
  1110. DRI.d.a = Index;
  1111. return section_rel_begin(DRI);
  1112. }
  1113. relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
  1114. DataRefImpl DRI;
  1115. DRI.d.a = Index;
  1116. return section_rel_end(DRI);
  1117. }
  1118. dice_iterator MachOObjectFile::begin_dices() const {
  1119. DataRefImpl DRI;
  1120. if (!DataInCodeLoadCmd)
  1121. return dice_iterator(DiceRef(DRI, this));
  1122. MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
  1123. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
  1124. return dice_iterator(DiceRef(DRI, this));
  1125. }
  1126. dice_iterator MachOObjectFile::end_dices() const {
  1127. DataRefImpl DRI;
  1128. if (!DataInCodeLoadCmd)
  1129. return dice_iterator(DiceRef(DRI, this));
  1130. MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
  1131. unsigned Offset = DicLC.dataoff + DicLC.datasize;
  1132. DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
  1133. return dice_iterator(DiceRef(DRI, this));
  1134. }
  1135. StringRef
  1136. MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
  1137. ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
  1138. return parseSegmentOrSectionName(Raw.data());
  1139. }
  1140. ArrayRef<char>
  1141. MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
  1142. const section_base *Base =
  1143. reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
  1144. return ArrayRef<char>(Base->sectname);
  1145. }
  1146. ArrayRef<char>
  1147. MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
  1148. const section_base *Base =
  1149. reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
  1150. return ArrayRef<char>(Base->segname);
  1151. }
  1152. bool
  1153. MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
  1154. const {
  1155. if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
  1156. return false;
  1157. return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
  1158. }
  1159. unsigned MachOObjectFile::getPlainRelocationSymbolNum(
  1160. const MachO::any_relocation_info &RE) const {
  1161. if (isLittleEndian())
  1162. return RE.r_word1 & 0xffffff;
  1163. return RE.r_word1 >> 8;
  1164. }
  1165. bool MachOObjectFile::getPlainRelocationExternal(
  1166. const MachO::any_relocation_info &RE) const {
  1167. if (isLittleEndian())
  1168. return (RE.r_word1 >> 27) & 1;
  1169. return (RE.r_word1 >> 4) & 1;
  1170. }
  1171. bool MachOObjectFile::getScatteredRelocationScattered(
  1172. const MachO::any_relocation_info &RE) const {
  1173. return RE.r_word0 >> 31;
  1174. }
  1175. uint32_t MachOObjectFile::getScatteredRelocationValue(
  1176. const MachO::any_relocation_info &RE) const {
  1177. return RE.r_word1;
  1178. }
  1179. unsigned MachOObjectFile::getAnyRelocationAddress(
  1180. const MachO::any_relocation_info &RE) const {
  1181. if (isRelocationScattered(RE))
  1182. return getScatteredRelocationAddress(RE);
  1183. return getPlainRelocationAddress(RE);
  1184. }
  1185. unsigned MachOObjectFile::getAnyRelocationPCRel(
  1186. const MachO::any_relocation_info &RE) const {
  1187. if (isRelocationScattered(RE))
  1188. return getScatteredRelocationPCRel(this, RE);
  1189. return getPlainRelocationPCRel(this, RE);
  1190. }
  1191. unsigned MachOObjectFile::getAnyRelocationLength(
  1192. const MachO::any_relocation_info &RE) const {
  1193. if (isRelocationScattered(RE))
  1194. return getScatteredRelocationLength(RE);
  1195. return getPlainRelocationLength(this, RE);
  1196. }
  1197. unsigned
  1198. MachOObjectFile::getAnyRelocationType(
  1199. const MachO::any_relocation_info &RE) const {
  1200. if (isRelocationScattered(RE))
  1201. return getScatteredRelocationType(RE);
  1202. return getPlainRelocationType(this, RE);
  1203. }
  1204. SectionRef
  1205. MachOObjectFile::getRelocationSection(
  1206. const MachO::any_relocation_info &RE) const {
  1207. if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
  1208. return *section_end();
  1209. unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
  1210. DataRefImpl DRI;
  1211. DRI.d.a = SecNum;
  1212. return SectionRef(DRI, this);
  1213. }
  1214. MachOObjectFile::LoadCommandInfo
  1215. MachOObjectFile::getFirstLoadCommandInfo() const {
  1216. MachOObjectFile::LoadCommandInfo Load;
  1217. unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
  1218. sizeof(MachO::mach_header);
  1219. Load.Ptr = getPtr(this, HeaderSize);
  1220. Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
  1221. return Load;
  1222. }
  1223. MachOObjectFile::LoadCommandInfo
  1224. MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
  1225. MachOObjectFile::LoadCommandInfo Next;
  1226. Next.Ptr = L.Ptr + L.C.cmdsize;
  1227. Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
  1228. return Next;
  1229. }
  1230. MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
  1231. return getStruct<MachO::section>(this, Sections[DRI.d.a]);
  1232. }
  1233. MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
  1234. return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
  1235. }
  1236. MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
  1237. unsigned Index) const {
  1238. const char *Sec = getSectionPtr(this, L, Index);
  1239. return getStruct<MachO::section>(this, Sec);
  1240. }
  1241. MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
  1242. unsigned Index) const {
  1243. const char *Sec = getSectionPtr(this, L, Index);
  1244. return getStruct<MachO::section_64>(this, Sec);
  1245. }
  1246. MachO::nlist
  1247. MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
  1248. const char *P = reinterpret_cast<const char *>(DRI.p);
  1249. return getStruct<MachO::nlist>(this, P);
  1250. }
  1251. MachO::nlist_64
  1252. MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
  1253. const char *P = reinterpret_cast<const char *>(DRI.p);
  1254. return getStruct<MachO::nlist_64>(this, P);
  1255. }
  1256. MachO::linkedit_data_command
  1257. MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
  1258. return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
  1259. }
  1260. MachO::segment_command
  1261. MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
  1262. return getStruct<MachO::segment_command>(this, L.Ptr);
  1263. }
  1264. MachO::segment_command_64
  1265. MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
  1266. return getStruct<MachO::segment_command_64>(this, L.Ptr);
  1267. }
  1268. MachO::linker_options_command
  1269. MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
  1270. return getStruct<MachO::linker_options_command>(this, L.Ptr);
  1271. }
  1272. MachO::version_min_command
  1273. MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
  1274. return getStruct<MachO::version_min_command>(this, L.Ptr);
  1275. }
  1276. MachO::any_relocation_info
  1277. MachOObjectFile::getRelocation(DataRefImpl Rel) const {
  1278. DataRefImpl Sec;
  1279. Sec.d.a = Rel.d.a;
  1280. uint32_t Offset;
  1281. if (is64Bit()) {
  1282. MachO::section_64 Sect = getSection64(Sec);
  1283. Offset = Sect.reloff;
  1284. } else {
  1285. MachO::section Sect = getSection(Sec);
  1286. Offset = Sect.reloff;
  1287. }
  1288. auto P = reinterpret_cast<const MachO::any_relocation_info *>(
  1289. getPtr(this, Offset)) + Rel.d.b;
  1290. return getStruct<MachO::any_relocation_info>(
  1291. this, reinterpret_cast<const char *>(P));
  1292. }
  1293. MachO::data_in_code_entry
  1294. MachOObjectFile::getDice(DataRefImpl Rel) const {
  1295. const char *P = reinterpret_cast<const char *>(Rel.p);
  1296. return getStruct<MachO::data_in_code_entry>(this, P);
  1297. }
  1298. MachO::mach_header MachOObjectFile::getHeader() const {
  1299. return getStruct<MachO::mach_header>(this, getPtr(this, 0));
  1300. }
  1301. MachO::mach_header_64 MachOObjectFile::getHeader64() const {
  1302. return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
  1303. }
  1304. uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
  1305. const MachO::dysymtab_command &DLC,
  1306. unsigned Index) const {
  1307. uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
  1308. return getStruct<uint32_t>(this, getPtr(this, Offset));
  1309. }
  1310. MachO::data_in_code_entry
  1311. MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
  1312. unsigned Index) const {
  1313. uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
  1314. return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
  1315. }
  1316. MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
  1317. return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
  1318. }
  1319. MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
  1320. return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
  1321. }
  1322. MachO::linkedit_data_command
  1323. MachOObjectFile::getDataInCodeLoadCommand() const {
  1324. if (DataInCodeLoadCmd)
  1325. return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
  1326. // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
  1327. MachO::linkedit_data_command Cmd;
  1328. Cmd.cmd = MachO::LC_DATA_IN_CODE;
  1329. Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
  1330. Cmd.dataoff = 0;
  1331. Cmd.datasize = 0;
  1332. return Cmd;
  1333. }
  1334. StringRef MachOObjectFile::getStringTableData() const {
  1335. MachO::symtab_command S = getSymtabLoadCommand();
  1336. return getData().substr(S.stroff, S.strsize);
  1337. }
  1338. bool MachOObjectFile::is64Bit() const {
  1339. return getType() == getMachOType(false, true) ||
  1340. getType() == getMachOType(true, true);
  1341. }
  1342. void MachOObjectFile::ReadULEB128s(uint64_t Index,
  1343. SmallVectorImpl<uint64_t> &Out) const {
  1344. DataExtractor extractor(ObjectFile::getData(), true, 0);
  1345. uint32_t offset = Index;
  1346. uint64_t data = 0;
  1347. while (uint64_t delta = extractor.getULEB128(&offset)) {
  1348. data += delta;
  1349. Out.push_back(data);
  1350. }
  1351. }
  1352. ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
  1353. bool BufferOwned) {
  1354. StringRef Magic = Buffer->getBuffer().slice(0, 4);
  1355. error_code EC;
  1356. std::unique_ptr<MachOObjectFile> Ret;
  1357. if (Magic == "\xFE\xED\xFA\xCE")
  1358. Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
  1359. else if (Magic == "\xCE\xFA\xED\xFE")
  1360. Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
  1361. else if (Magic == "\xFE\xED\xFA\xCF")
  1362. Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
  1363. else if (Magic == "\xCF\xFA\xED\xFE")
  1364. Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
  1365. else {
  1366. delete Buffer;
  1367. return object_error::parse_failed;
  1368. }
  1369. if (EC)
  1370. return EC;
  1371. return Ret.release();
  1372. }
  1373. } // end namespace object
  1374. } // end namespace llvm