MachOObjectFile.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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/Triple.h"
  16. #include "llvm/Object/MachOFormat.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include <cctype>
  20. #include <cstring>
  21. #include <limits>
  22. using namespace llvm;
  23. using namespace object;
  24. namespace llvm {
  25. namespace object {
  26. MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
  27. error_code &ec)
  28. : ObjectFile(Binary::ID_MachO, Object, ec),
  29. MachOObj(MOO),
  30. RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
  31. DataRefImpl DRI;
  32. moveToNextSection(DRI);
  33. uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
  34. while (DRI.d.a < LoadCommandCount) {
  35. Sections.push_back(DRI);
  36. DRI.d.b++;
  37. moveToNextSection(DRI);
  38. }
  39. }
  40. ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
  41. error_code ec;
  42. std::string Err;
  43. MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
  44. if (!MachOObj)
  45. return NULL;
  46. // MachOObject takes ownership of the Buffer we passed to it, and
  47. // MachOObjectFile does, too, so we need to make sure they don't get the
  48. // same object. A MemoryBuffer is cheap (it's just a reference to memory,
  49. // not a copy of the memory itself), so just make a new copy here for
  50. // the MachOObjectFile.
  51. MemoryBuffer *NewBuffer =
  52. MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
  53. Buffer->getBufferIdentifier(), false);
  54. return new MachOObjectFile(NewBuffer, MachOObj, ec);
  55. }
  56. /*===-- Symbols -----------------------------------------------------------===*/
  57. void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
  58. uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
  59. while (DRI.d.a < LoadCommandCount) {
  60. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  61. if (LCI.Command.Type == macho::LCT_Symtab) {
  62. InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
  63. MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
  64. if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
  65. return;
  66. }
  67. DRI.d.a++;
  68. DRI.d.b = 0;
  69. }
  70. }
  71. void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
  72. InMemoryStruct<macho::SymbolTableEntry> &Res) const {
  73. InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
  74. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  75. MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
  76. if (RegisteredStringTable != DRI.d.a) {
  77. MachOObj->RegisterStringTable(*SymtabLoadCmd);
  78. RegisteredStringTable = DRI.d.a;
  79. }
  80. MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
  81. Res);
  82. }
  83. void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
  84. InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
  85. InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
  86. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  87. MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
  88. if (RegisteredStringTable != DRI.d.a) {
  89. MachOObj->RegisterStringTable(*SymtabLoadCmd);
  90. RegisteredStringTable = DRI.d.a;
  91. }
  92. MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
  93. Res);
  94. }
  95. error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
  96. SymbolRef &Result) const {
  97. DRI.d.b++;
  98. moveToNextSymbol(DRI);
  99. Result = SymbolRef(DRI, this);
  100. return object_error::success;
  101. }
  102. error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
  103. StringRef &Result) const {
  104. if (MachOObj->is64Bit()) {
  105. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  106. getSymbol64TableEntry(DRI, Entry);
  107. Result = MachOObj->getStringAtIndex(Entry->StringIndex);
  108. } else {
  109. InMemoryStruct<macho::SymbolTableEntry> Entry;
  110. getSymbolTableEntry(DRI, Entry);
  111. Result = MachOObj->getStringAtIndex(Entry->StringIndex);
  112. }
  113. return object_error::success;
  114. }
  115. error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
  116. uint64_t &Result) const {
  117. if (MachOObj->is64Bit()) {
  118. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  119. getSymbol64TableEntry(DRI, Entry);
  120. Result = Entry->Value;
  121. if (Entry->SectionIndex) {
  122. InMemoryStruct<macho::Section64> Section;
  123. getSection64(Sections[Entry->SectionIndex-1], Section);
  124. Result += Section->Offset - Section->Address;
  125. }
  126. } else {
  127. InMemoryStruct<macho::SymbolTableEntry> Entry;
  128. getSymbolTableEntry(DRI, Entry);
  129. Result = Entry->Value;
  130. if (Entry->SectionIndex) {
  131. InMemoryStruct<macho::Section> Section;
  132. getSection(Sections[Entry->SectionIndex-1], Section);
  133. Result += Section->Offset - Section->Address;
  134. }
  135. }
  136. return object_error::success;
  137. }
  138. error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
  139. uint64_t &Result) const {
  140. if (MachOObj->is64Bit()) {
  141. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  142. getSymbol64TableEntry(DRI, Entry);
  143. Result = Entry->Value;
  144. } else {
  145. InMemoryStruct<macho::SymbolTableEntry> Entry;
  146. getSymbolTableEntry(DRI, Entry);
  147. Result = Entry->Value;
  148. }
  149. return object_error::success;
  150. }
  151. error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
  152. uint64_t &Result) const {
  153. uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
  154. uint64_t BeginOffset;
  155. uint64_t EndOffset = 0;
  156. uint8_t SectionIndex;
  157. if (MachOObj->is64Bit()) {
  158. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  159. getSymbol64TableEntry(DRI, Entry);
  160. BeginOffset = Entry->Value;
  161. SectionIndex = Entry->SectionIndex;
  162. if (!SectionIndex) {
  163. uint32_t flags = SymbolRef::SF_None;
  164. getSymbolFlags(DRI, flags);
  165. if (flags & SymbolRef::SF_Common)
  166. Result = Entry->Value;
  167. else
  168. Result = UnknownAddressOrSize;
  169. return object_error::success;
  170. }
  171. // Unfortunately symbols are unsorted so we need to touch all
  172. // symbols from load command
  173. DRI.d.b = 0;
  174. uint32_t Command = DRI.d.a;
  175. while (Command == DRI.d.a) {
  176. moveToNextSymbol(DRI);
  177. if (DRI.d.a < LoadCommandCount) {
  178. getSymbol64TableEntry(DRI, Entry);
  179. if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
  180. if (!EndOffset || Entry->Value < EndOffset)
  181. EndOffset = Entry->Value;
  182. }
  183. DRI.d.b++;
  184. }
  185. } else {
  186. InMemoryStruct<macho::SymbolTableEntry> Entry;
  187. getSymbolTableEntry(DRI, Entry);
  188. BeginOffset = Entry->Value;
  189. SectionIndex = Entry->SectionIndex;
  190. if (!SectionIndex) {
  191. uint32_t flags = SymbolRef::SF_None;
  192. getSymbolFlags(DRI, flags);
  193. if (flags & SymbolRef::SF_Common)
  194. Result = Entry->Value;
  195. else
  196. Result = UnknownAddressOrSize;
  197. return object_error::success;
  198. }
  199. // Unfortunately symbols are unsorted so we need to touch all
  200. // symbols from load command
  201. DRI.d.b = 0;
  202. uint32_t Command = DRI.d.a;
  203. while (Command == DRI.d.a) {
  204. moveToNextSymbol(DRI);
  205. if (DRI.d.a < LoadCommandCount) {
  206. getSymbolTableEntry(DRI, Entry);
  207. if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
  208. if (!EndOffset || Entry->Value < EndOffset)
  209. EndOffset = Entry->Value;
  210. }
  211. DRI.d.b++;
  212. }
  213. }
  214. if (!EndOffset) {
  215. uint64_t Size;
  216. getSectionSize(Sections[SectionIndex-1], Size);
  217. getSectionAddress(Sections[SectionIndex-1], EndOffset);
  218. EndOffset += Size;
  219. }
  220. Result = EndOffset - BeginOffset;
  221. return object_error::success;
  222. }
  223. error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
  224. char &Result) const {
  225. uint8_t Type, Flags;
  226. if (MachOObj->is64Bit()) {
  227. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  228. getSymbol64TableEntry(DRI, Entry);
  229. Type = Entry->Type;
  230. Flags = Entry->Flags;
  231. } else {
  232. InMemoryStruct<macho::SymbolTableEntry> Entry;
  233. getSymbolTableEntry(DRI, Entry);
  234. Type = Entry->Type;
  235. Flags = Entry->Flags;
  236. }
  237. char Char;
  238. switch (Type & macho::STF_TypeMask) {
  239. case macho::STT_Undefined:
  240. Char = 'u';
  241. break;
  242. case macho::STT_Absolute:
  243. case macho::STT_Section:
  244. Char = 's';
  245. break;
  246. default:
  247. Char = '?';
  248. break;
  249. }
  250. if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
  251. Char = toupper(Char);
  252. Result = Char;
  253. return object_error::success;
  254. }
  255. error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
  256. uint32_t &Result) const {
  257. uint16_t MachOFlags;
  258. uint8_t MachOType;
  259. if (MachOObj->is64Bit()) {
  260. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  261. getSymbol64TableEntry(DRI, Entry);
  262. MachOFlags = Entry->Flags;
  263. MachOType = Entry->Type;
  264. } else {
  265. InMemoryStruct<macho::SymbolTableEntry> Entry;
  266. getSymbolTableEntry(DRI, Entry);
  267. MachOFlags = Entry->Flags;
  268. MachOType = Entry->Type;
  269. }
  270. // TODO: Correctly set SF_ThreadLocal
  271. Result = SymbolRef::SF_None;
  272. if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
  273. Result |= SymbolRef::SF_Undefined;
  274. if (MachOFlags & macho::STF_StabsEntryMask)
  275. Result |= SymbolRef::SF_FormatSpecific;
  276. if (MachOType & MachO::NlistMaskExternal) {
  277. Result |= SymbolRef::SF_Global;
  278. if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
  279. Result |= SymbolRef::SF_Common;
  280. }
  281. if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
  282. Result |= SymbolRef::SF_Weak;
  283. if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
  284. Result |= SymbolRef::SF_Absolute;
  285. return object_error::success;
  286. }
  287. error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
  288. section_iterator &Res) const {
  289. uint8_t index;
  290. if (MachOObj->is64Bit()) {
  291. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  292. getSymbol64TableEntry(Symb, Entry);
  293. index = Entry->SectionIndex;
  294. } else {
  295. InMemoryStruct<macho::SymbolTableEntry> Entry;
  296. getSymbolTableEntry(Symb, Entry);
  297. index = Entry->SectionIndex;
  298. }
  299. if (index == 0)
  300. Res = end_sections();
  301. else
  302. Res = section_iterator(SectionRef(Sections[index-1], this));
  303. return object_error::success;
  304. }
  305. error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
  306. SymbolRef::Type &Res) const {
  307. uint8_t n_type;
  308. if (MachOObj->is64Bit()) {
  309. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  310. getSymbol64TableEntry(Symb, Entry);
  311. n_type = Entry->Type;
  312. } else {
  313. InMemoryStruct<macho::SymbolTableEntry> Entry;
  314. getSymbolTableEntry(Symb, Entry);
  315. n_type = Entry->Type;
  316. }
  317. Res = SymbolRef::ST_Other;
  318. // If this is a STAB debugging symbol, we can do nothing more.
  319. if (n_type & MachO::NlistMaskStab) {
  320. Res = SymbolRef::ST_Debug;
  321. return object_error::success;
  322. }
  323. switch (n_type & MachO::NlistMaskType) {
  324. case MachO::NListTypeUndefined :
  325. Res = SymbolRef::ST_Unknown;
  326. break;
  327. case MachO::NListTypeSection :
  328. Res = SymbolRef::ST_Function;
  329. break;
  330. }
  331. return object_error::success;
  332. }
  333. error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
  334. uint64_t &Val) const {
  335. report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
  336. }
  337. symbol_iterator MachOObjectFile::begin_symbols() const {
  338. // DRI.d.a = segment number; DRI.d.b = symbol index.
  339. DataRefImpl DRI;
  340. moveToNextSymbol(DRI);
  341. return symbol_iterator(SymbolRef(DRI, this));
  342. }
  343. symbol_iterator MachOObjectFile::end_symbols() const {
  344. DataRefImpl DRI;
  345. DRI.d.a = MachOObj->getHeader().NumLoadCommands;
  346. return symbol_iterator(SymbolRef(DRI, this));
  347. }
  348. symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
  349. // TODO: implement
  350. report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
  351. }
  352. symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
  353. // TODO: implement
  354. report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
  355. }
  356. library_iterator MachOObjectFile::begin_libraries_needed() const {
  357. // TODO: implement
  358. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  359. }
  360. library_iterator MachOObjectFile::end_libraries_needed() const {
  361. // TODO: implement
  362. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  363. }
  364. StringRef MachOObjectFile::getLoadName() const {
  365. // TODO: Implement
  366. report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
  367. }
  368. /*===-- Sections ----------------------------------------------------------===*/
  369. void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
  370. uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
  371. while (DRI.d.a < LoadCommandCount) {
  372. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  373. if (LCI.Command.Type == macho::LCT_Segment) {
  374. InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
  375. MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
  376. if (DRI.d.b < SegmentLoadCmd->NumSections)
  377. return;
  378. } else if (LCI.Command.Type == macho::LCT_Segment64) {
  379. InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
  380. MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
  381. if (DRI.d.b < Segment64LoadCmd->NumSections)
  382. return;
  383. }
  384. DRI.d.a++;
  385. DRI.d.b = 0;
  386. }
  387. }
  388. error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
  389. SectionRef &Result) const {
  390. DRI.d.b++;
  391. moveToNextSection(DRI);
  392. Result = SectionRef(DRI, this);
  393. return object_error::success;
  394. }
  395. void
  396. MachOObjectFile::getSection(DataRefImpl DRI,
  397. InMemoryStruct<macho::Section> &Res) const {
  398. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  399. MachOObj->ReadSection(LCI, DRI.d.b, Res);
  400. }
  401. std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
  402. SectionList::const_iterator loc =
  403. std::find(Sections.begin(), Sections.end(), Sec);
  404. assert(loc != Sections.end() && "Sec is not a valid section!");
  405. return std::distance(Sections.begin(), loc);
  406. }
  407. void
  408. MachOObjectFile::getSection64(DataRefImpl DRI,
  409. InMemoryStruct<macho::Section64> &Res) const {
  410. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  411. MachOObj->ReadSection64(LCI, DRI.d.b, Res);
  412. }
  413. static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
  414. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  415. if (LCI.Command.Type == macho::LCT_Segment64)
  416. return true;
  417. assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
  418. return false;
  419. }
  420. static StringRef parseSegmentOrSectionName(const char *P) {
  421. if (P[15] == 0)
  422. // Null terminated.
  423. return P;
  424. // Not null terminated, so this is a 16 char string.
  425. return StringRef(P, 16);
  426. }
  427. error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
  428. StringRef &Result) const {
  429. if (is64BitLoadCommand(MachOObj.get(), DRI)) {
  430. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  431. InMemoryStruct<macho::Section64> Sect;
  432. MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
  433. Result = parseSegmentOrSectionName(Sect->Name);
  434. } else {
  435. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
  436. InMemoryStruct<macho::Section> Sect;
  437. MachOObj->ReadSection(LCI, DRI.d.b, Sect);
  438. Result = parseSegmentOrSectionName(Sect->Name);
  439. }
  440. return object_error::success;
  441. }
  442. error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
  443. StringRef &Res) const {
  444. if (is64BitLoadCommand(MachOObj.get(), Sec)) {
  445. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
  446. InMemoryStruct<macho::Section64> Sect;
  447. MachOObj->ReadSection64(LCI, Sec.d.b, Sect);
  448. Res = parseSegmentOrSectionName(Sect->SegmentName);
  449. } else {
  450. LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
  451. InMemoryStruct<macho::Section> Sect;
  452. MachOObj->ReadSection(LCI, Sec.d.b, Sect);
  453. Res = parseSegmentOrSectionName(Sect->SegmentName);
  454. }
  455. return object_error::success;
  456. }
  457. error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
  458. uint64_t &Result) const {
  459. if (is64BitLoadCommand(MachOObj.get(), DRI)) {
  460. InMemoryStruct<macho::Section64> Sect;
  461. getSection64(DRI, Sect);
  462. Result = Sect->Address;
  463. } else {
  464. InMemoryStruct<macho::Section> Sect;
  465. getSection(DRI, Sect);
  466. Result = Sect->Address;
  467. }
  468. return object_error::success;
  469. }
  470. error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
  471. uint64_t &Result) const {
  472. if (is64BitLoadCommand(MachOObj.get(), DRI)) {
  473. InMemoryStruct<macho::Section64> Sect;
  474. getSection64(DRI, Sect);
  475. Result = Sect->Size;
  476. } else {
  477. InMemoryStruct<macho::Section> Sect;
  478. getSection(DRI, Sect);
  479. Result = Sect->Size;
  480. }
  481. return object_error::success;
  482. }
  483. error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
  484. StringRef &Result) const {
  485. if (is64BitLoadCommand(MachOObj.get(), DRI)) {
  486. InMemoryStruct<macho::Section64> Sect;
  487. getSection64(DRI, Sect);
  488. Result = MachOObj->getData(Sect->Offset, Sect->Size);
  489. } else {
  490. InMemoryStruct<macho::Section> Sect;
  491. getSection(DRI, Sect);
  492. Result = MachOObj->getData(Sect->Offset, Sect->Size);
  493. }
  494. return object_error::success;
  495. }
  496. error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
  497. uint64_t &Result) const {
  498. if (is64BitLoadCommand(MachOObj.get(), DRI)) {
  499. InMemoryStruct<macho::Section64> Sect;
  500. getSection64(DRI, Sect);
  501. Result = uint64_t(1) << Sect->Align;
  502. } else {
  503. InMemoryStruct<macho::Section> Sect;
  504. getSection(DRI, Sect);
  505. Result = uint64_t(1) << Sect->Align;
  506. }
  507. return object_error::success;
  508. }
  509. error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
  510. bool &Result) const {
  511. if (is64BitLoadCommand(MachOObj.get(), DRI)) {
  512. InMemoryStruct<macho::Section64> Sect;
  513. getSection64(DRI, Sect);
  514. Result = !strcmp(Sect->Name, "__text");
  515. } else {
  516. InMemoryStruct<macho::Section> Sect;
  517. getSection(DRI, Sect);
  518. Result = !strcmp(Sect->Name, "__text");
  519. }
  520. return object_error::success;
  521. }
  522. error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
  523. bool &Result) const {
  524. // FIXME: Unimplemented.
  525. Result = false;
  526. return object_error::success;
  527. }
  528. error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
  529. bool &Result) const {
  530. // FIXME: Unimplemented.
  531. Result = false;
  532. return object_error::success;
  533. }
  534. error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
  535. bool &Result) const {
  536. // FIXME: Unimplemented.
  537. Result = true;
  538. return object_error::success;
  539. }
  540. error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
  541. bool &Result) const {
  542. // FIXME: Unimplemented.
  543. Result = false;
  544. return object_error::success;
  545. }
  546. error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
  547. bool &Result) const {
  548. if (MachOObj->is64Bit()) {
  549. InMemoryStruct<macho::Section64> Sect;
  550. getSection64(DRI, Sect);
  551. unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
  552. Result = (SectionType == MachO::SectionTypeZeroFill ||
  553. SectionType == MachO::SectionTypeZeroFillLarge);
  554. } else {
  555. InMemoryStruct<macho::Section> Sect;
  556. getSection(DRI, Sect);
  557. unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
  558. Result = (SectionType == MachO::SectionTypeZeroFill ||
  559. SectionType == MachO::SectionTypeZeroFillLarge);
  560. }
  561. return object_error::success;
  562. }
  563. error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
  564. bool &Result) const {
  565. // Consider using the code from isSectionText to look for __const sections.
  566. // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
  567. // to use section attributes to distinguish code from data.
  568. // FIXME: Unimplemented.
  569. Result = false;
  570. return object_error::success;
  571. }
  572. error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
  573. DataRefImpl Symb,
  574. bool &Result) const {
  575. SymbolRef::Type ST;
  576. getSymbolType(Symb, ST);
  577. if (ST == SymbolRef::ST_Unknown) {
  578. Result = false;
  579. return object_error::success;
  580. }
  581. uint64_t SectBegin, SectEnd;
  582. getSectionAddress(Sec, SectBegin);
  583. getSectionSize(Sec, SectEnd);
  584. SectEnd += SectBegin;
  585. if (MachOObj->is64Bit()) {
  586. InMemoryStruct<macho::Symbol64TableEntry> Entry;
  587. getSymbol64TableEntry(Symb, Entry);
  588. uint64_t SymAddr= Entry->Value;
  589. Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
  590. } else {
  591. InMemoryStruct<macho::SymbolTableEntry> Entry;
  592. getSymbolTableEntry(Symb, Entry);
  593. uint64_t SymAddr= Entry->Value;
  594. Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
  595. }
  596. return object_error::success;
  597. }
  598. relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
  599. DataRefImpl ret;
  600. ret.d.b = getSectionIndex(Sec);
  601. return relocation_iterator(RelocationRef(ret, this));
  602. }
  603. relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
  604. uint32_t last_reloc;
  605. if (is64BitLoadCommand(MachOObj.get(), Sec)) {
  606. InMemoryStruct<macho::Section64> Sect;
  607. getSection64(Sec, Sect);
  608. last_reloc = Sect->NumRelocationTableEntries;
  609. } else {
  610. InMemoryStruct<macho::Section> Sect;
  611. getSection(Sec, Sect);
  612. last_reloc = Sect->NumRelocationTableEntries;
  613. }
  614. DataRefImpl ret;
  615. ret.d.a = last_reloc;
  616. ret.d.b = getSectionIndex(Sec);
  617. return relocation_iterator(RelocationRef(ret, this));
  618. }
  619. section_iterator MachOObjectFile::begin_sections() const {
  620. DataRefImpl DRI;
  621. moveToNextSection(DRI);
  622. return section_iterator(SectionRef(DRI, this));
  623. }
  624. section_iterator MachOObjectFile::end_sections() const {
  625. DataRefImpl DRI;
  626. DRI.d.a = MachOObj->getHeader().NumLoadCommands;
  627. return section_iterator(SectionRef(DRI, this));
  628. }
  629. /*===-- Relocations -------------------------------------------------------===*/
  630. void MachOObjectFile::
  631. getRelocation(DataRefImpl Rel,
  632. InMemoryStruct<macho::RelocationEntry> &Res) const {
  633. uint32_t relOffset;
  634. if (MachOObj->is64Bit()) {
  635. InMemoryStruct<macho::Section64> Sect;
  636. getSection64(Sections[Rel.d.b], Sect);
  637. relOffset = Sect->RelocationTableOffset;
  638. } else {
  639. InMemoryStruct<macho::Section> Sect;
  640. getSection(Sections[Rel.d.b], Sect);
  641. relOffset = Sect->RelocationTableOffset;
  642. }
  643. MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
  644. }
  645. error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
  646. RelocationRef &Res) const {
  647. ++Rel.d.a;
  648. Res = RelocationRef(Rel, this);
  649. return object_error::success;
  650. }
  651. error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
  652. uint64_t &Res) const {
  653. const uint8_t* sectAddress = 0;
  654. if (MachOObj->is64Bit()) {
  655. InMemoryStruct<macho::Section64> Sect;
  656. getSection64(Sections[Rel.d.b], Sect);
  657. sectAddress += Sect->Address;
  658. } else {
  659. InMemoryStruct<macho::Section> Sect;
  660. getSection(Sections[Rel.d.b], Sect);
  661. sectAddress += Sect->Address;
  662. }
  663. InMemoryStruct<macho::RelocationEntry> RE;
  664. getRelocation(Rel, RE);
  665. unsigned Arch = getArch();
  666. bool isScattered = (Arch != Triple::x86_64) &&
  667. (RE->Word0 & macho::RF_Scattered);
  668. uint64_t RelAddr = 0;
  669. if (isScattered)
  670. RelAddr = RE->Word0 & 0xFFFFFF;
  671. else
  672. RelAddr = RE->Word0;
  673. Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
  674. return object_error::success;
  675. }
  676. error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
  677. uint64_t &Res) const {
  678. InMemoryStruct<macho::RelocationEntry> RE;
  679. getRelocation(Rel, RE);
  680. unsigned Arch = getArch();
  681. bool isScattered = (Arch != Triple::x86_64) &&
  682. (RE->Word0 & macho::RF_Scattered);
  683. if (isScattered)
  684. Res = RE->Word0 & 0xFFFFFF;
  685. else
  686. Res = RE->Word0;
  687. return object_error::success;
  688. }
  689. error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
  690. SymbolRef &Res) const {
  691. InMemoryStruct<macho::RelocationEntry> RE;
  692. getRelocation(Rel, RE);
  693. uint32_t SymbolIdx = RE->Word1 & 0xffffff;
  694. bool isExtern = (RE->Word1 >> 27) & 1;
  695. DataRefImpl Sym;
  696. moveToNextSymbol(Sym);
  697. if (isExtern) {
  698. for (unsigned i = 0; i < SymbolIdx; i++) {
  699. Sym.d.b++;
  700. moveToNextSymbol(Sym);
  701. assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
  702. "Relocation symbol index out of range!");
  703. }
  704. }
  705. Res = SymbolRef(Sym, this);
  706. return object_error::success;
  707. }
  708. error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
  709. uint64_t &Res) const {
  710. InMemoryStruct<macho::RelocationEntry> RE;
  711. getRelocation(Rel, RE);
  712. Res = RE->Word0;
  713. Res <<= 32;
  714. Res |= RE->Word1;
  715. return object_error::success;
  716. }
  717. error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
  718. SmallVectorImpl<char> &Result) const {
  719. // TODO: Support scattered relocations.
  720. StringRef res;
  721. InMemoryStruct<macho::RelocationEntry> RE;
  722. getRelocation(Rel, RE);
  723. unsigned Arch = getArch();
  724. bool isScattered = (Arch != Triple::x86_64) &&
  725. (RE->Word0 & macho::RF_Scattered);
  726. unsigned r_type;
  727. if (isScattered)
  728. r_type = (RE->Word0 >> 24) & 0xF;
  729. else
  730. r_type = (RE->Word1 >> 28) & 0xF;
  731. switch (Arch) {
  732. case Triple::x86: {
  733. static const char *const Table[] = {
  734. "GENERIC_RELOC_VANILLA",
  735. "GENERIC_RELOC_PAIR",
  736. "GENERIC_RELOC_SECTDIFF",
  737. "GENERIC_RELOC_PB_LA_PTR",
  738. "GENERIC_RELOC_LOCAL_SECTDIFF",
  739. "GENERIC_RELOC_TLV" };
  740. if (r_type > 6)
  741. res = "Unknown";
  742. else
  743. res = Table[r_type];
  744. break;
  745. }
  746. case Triple::x86_64: {
  747. static const char *const Table[] = {
  748. "X86_64_RELOC_UNSIGNED",
  749. "X86_64_RELOC_SIGNED",
  750. "X86_64_RELOC_BRANCH",
  751. "X86_64_RELOC_GOT_LOAD",
  752. "X86_64_RELOC_GOT",
  753. "X86_64_RELOC_SUBTRACTOR",
  754. "X86_64_RELOC_SIGNED_1",
  755. "X86_64_RELOC_SIGNED_2",
  756. "X86_64_RELOC_SIGNED_4",
  757. "X86_64_RELOC_TLV" };
  758. if (r_type > 9)
  759. res = "Unknown";
  760. else
  761. res = Table[r_type];
  762. break;
  763. }
  764. case Triple::arm: {
  765. static const char *const Table[] = {
  766. "ARM_RELOC_VANILLA",
  767. "ARM_RELOC_PAIR",
  768. "ARM_RELOC_SECTDIFF",
  769. "ARM_RELOC_LOCAL_SECTDIFF",
  770. "ARM_RELOC_PB_LA_PTR",
  771. "ARM_RELOC_BR24",
  772. "ARM_THUMB_RELOC_BR22",
  773. "ARM_THUMB_32BIT_BRANCH",
  774. "ARM_RELOC_HALF",
  775. "ARM_RELOC_HALF_SECTDIFF" };
  776. if (r_type > 9)
  777. res = "Unknown";
  778. else
  779. res = Table[r_type];
  780. break;
  781. }
  782. case Triple::ppc: {
  783. static const char *const Table[] = {
  784. "PPC_RELOC_VANILLA",
  785. "PPC_RELOC_PAIR",
  786. "PPC_RELOC_BR14",
  787. "PPC_RELOC_BR24",
  788. "PPC_RELOC_HI16",
  789. "PPC_RELOC_LO16",
  790. "PPC_RELOC_HA16",
  791. "PPC_RELOC_LO14",
  792. "PPC_RELOC_SECTDIFF",
  793. "PPC_RELOC_PB_LA_PTR",
  794. "PPC_RELOC_HI16_SECTDIFF",
  795. "PPC_RELOC_LO16_SECTDIFF",
  796. "PPC_RELOC_HA16_SECTDIFF",
  797. "PPC_RELOC_JBSR",
  798. "PPC_RELOC_LO14_SECTDIFF",
  799. "PPC_RELOC_LOCAL_SECTDIFF" };
  800. res = Table[r_type];
  801. break;
  802. }
  803. case Triple::UnknownArch:
  804. res = "Unknown";
  805. break;
  806. }
  807. Result.append(res.begin(), res.end());
  808. return object_error::success;
  809. }
  810. error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
  811. int64_t &Res) const {
  812. InMemoryStruct<macho::RelocationEntry> RE;
  813. getRelocation(Rel, RE);
  814. bool isExtern = (RE->Word1 >> 27) & 1;
  815. Res = 0;
  816. if (!isExtern) {
  817. const uint8_t* sectAddress = base();
  818. if (MachOObj->is64Bit()) {
  819. InMemoryStruct<macho::Section64> Sect;
  820. getSection64(Sections[Rel.d.b], Sect);
  821. sectAddress += Sect->Offset;
  822. } else {
  823. InMemoryStruct<macho::Section> Sect;
  824. getSection(Sections[Rel.d.b], Sect);
  825. sectAddress += Sect->Offset;
  826. }
  827. Res = reinterpret_cast<uintptr_t>(sectAddress);
  828. }
  829. return object_error::success;
  830. }
  831. // Helper to advance a section or symbol iterator multiple increments at a time.
  832. template<class T>
  833. error_code advance(T &it, size_t Val) {
  834. error_code ec;
  835. while (Val--) {
  836. it.increment(ec);
  837. }
  838. return ec;
  839. }
  840. template<class T>
  841. void advanceTo(T &it, size_t Val) {
  842. if (error_code ec = advance(it, Val))
  843. report_fatal_error(ec.message());
  844. }
  845. void MachOObjectFile::printRelocationTargetName(
  846. InMemoryStruct<macho::RelocationEntry>& RE,
  847. raw_string_ostream &fmt) const {
  848. unsigned Arch = getArch();
  849. bool isScattered = (Arch != Triple::x86_64) &&
  850. (RE->Word0 & macho::RF_Scattered);
  851. // Target of a scattered relocation is an address. In the interest of
  852. // generating pretty output, scan through the symbol table looking for a
  853. // symbol that aligns with that address. If we find one, print it.
  854. // Otherwise, we just print the hex address of the target.
  855. if (isScattered) {
  856. uint32_t Val = RE->Word1;
  857. error_code ec;
  858. for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
  859. SI.increment(ec)) {
  860. if (ec) report_fatal_error(ec.message());
  861. uint64_t Addr;
  862. StringRef Name;
  863. if ((ec = SI->getAddress(Addr)))
  864. report_fatal_error(ec.message());
  865. if (Addr != Val) continue;
  866. if ((ec = SI->getName(Name)))
  867. report_fatal_error(ec.message());
  868. fmt << Name;
  869. return;
  870. }
  871. // If we couldn't find a symbol that this relocation refers to, try
  872. // to find a section beginning instead.
  873. for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
  874. SI.increment(ec)) {
  875. if (ec) report_fatal_error(ec.message());
  876. uint64_t Addr;
  877. StringRef Name;
  878. if ((ec = SI->getAddress(Addr)))
  879. report_fatal_error(ec.message());
  880. if (Addr != Val) continue;
  881. if ((ec = SI->getName(Name)))
  882. report_fatal_error(ec.message());
  883. fmt << Name;
  884. return;
  885. }
  886. fmt << format("0x%x", Val);
  887. return;
  888. }
  889. StringRef S;
  890. bool isExtern = (RE->Word1 >> 27) & 1;
  891. uint32_t Val = RE->Word1 & 0xFFFFFF;
  892. if (isExtern) {
  893. symbol_iterator SI = begin_symbols();
  894. advanceTo(SI, Val);
  895. SI->getName(S);
  896. } else {
  897. section_iterator SI = begin_sections();
  898. advanceTo(SI, Val);
  899. SI->getName(S);
  900. }
  901. fmt << S;
  902. }
  903. error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
  904. SmallVectorImpl<char> &Result) const {
  905. InMemoryStruct<macho::RelocationEntry> RE;
  906. getRelocation(Rel, RE);
  907. unsigned Arch = getArch();
  908. bool isScattered = (Arch != Triple::x86_64) &&
  909. (RE->Word0 & macho::RF_Scattered);
  910. std::string fmtbuf;
  911. raw_string_ostream fmt(fmtbuf);
  912. unsigned Type;
  913. if (isScattered)
  914. Type = (RE->Word0 >> 24) & 0xF;
  915. else
  916. Type = (RE->Word1 >> 28) & 0xF;
  917. bool isPCRel;
  918. if (isScattered)
  919. isPCRel = ((RE->Word0 >> 30) & 1);
  920. else
  921. isPCRel = ((RE->Word1 >> 24) & 1);
  922. // Determine any addends that should be displayed with the relocation.
  923. // These require decoding the relocation type, which is triple-specific.
  924. // X86_64 has entirely custom relocation types.
  925. if (Arch == Triple::x86_64) {
  926. bool isPCRel = ((RE->Word1 >> 24) & 1);
  927. switch (Type) {
  928. case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
  929. case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
  930. printRelocationTargetName(RE, fmt);
  931. fmt << "@GOT";
  932. if (isPCRel) fmt << "PCREL";
  933. break;
  934. }
  935. case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
  936. InMemoryStruct<macho::RelocationEntry> RENext;
  937. DataRefImpl RelNext = Rel;
  938. RelNext.d.a++;
  939. getRelocation(RelNext, RENext);
  940. // X86_64_SUBTRACTOR must be followed by a relocation of type
  941. // X86_64_RELOC_UNSIGNED.
  942. // NOTE: Scattered relocations don't exist on x86_64.
  943. unsigned RType = (RENext->Word1 >> 28) & 0xF;
  944. if (RType != 0)
  945. report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
  946. "X86_64_RELOC_SUBTRACTOR.");
  947. // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
  948. // X86_64_SUBTRACTOR contains to the subtrahend.
  949. printRelocationTargetName(RENext, fmt);
  950. fmt << "-";
  951. printRelocationTargetName(RE, fmt);
  952. }
  953. case macho::RIT_X86_64_TLV:
  954. printRelocationTargetName(RE, fmt);
  955. fmt << "@TLV";
  956. if (isPCRel) fmt << "P";
  957. break;
  958. case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
  959. printRelocationTargetName(RE, fmt);
  960. fmt << "-1";
  961. break;
  962. case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
  963. printRelocationTargetName(RE, fmt);
  964. fmt << "-2";
  965. break;
  966. case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
  967. printRelocationTargetName(RE, fmt);
  968. fmt << "-4";
  969. break;
  970. default:
  971. printRelocationTargetName(RE, fmt);
  972. break;
  973. }
  974. // X86 and ARM share some relocation types in common.
  975. } else if (Arch == Triple::x86 || Arch == Triple::arm) {
  976. // Generic relocation types...
  977. switch (Type) {
  978. case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
  979. return object_error::success;
  980. case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
  981. InMemoryStruct<macho::RelocationEntry> RENext;
  982. DataRefImpl RelNext = Rel;
  983. RelNext.d.a++;
  984. getRelocation(RelNext, RENext);
  985. // X86 sect diff's must be followed by a relocation of type
  986. // GENERIC_RELOC_PAIR.
  987. bool isNextScattered = (Arch != Triple::x86_64) &&
  988. (RENext->Word0 & macho::RF_Scattered);
  989. unsigned RType;
  990. if (isNextScattered)
  991. RType = (RENext->Word0 >> 24) & 0xF;
  992. else
  993. RType = (RENext->Word1 >> 28) & 0xF;
  994. if (RType != 1)
  995. report_fatal_error("Expected GENERIC_RELOC_PAIR after "
  996. "GENERIC_RELOC_SECTDIFF.");
  997. printRelocationTargetName(RE, fmt);
  998. fmt << "-";
  999. printRelocationTargetName(RENext, fmt);
  1000. break;
  1001. }
  1002. }
  1003. if (Arch == Triple::x86) {
  1004. // All X86 relocations that need special printing were already
  1005. // handled in the generic code.
  1006. switch (Type) {
  1007. case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
  1008. InMemoryStruct<macho::RelocationEntry> RENext;
  1009. DataRefImpl RelNext = Rel;
  1010. RelNext.d.a++;
  1011. getRelocation(RelNext, RENext);
  1012. // X86 sect diff's must be followed by a relocation of type
  1013. // GENERIC_RELOC_PAIR.
  1014. bool isNextScattered = (Arch != Triple::x86_64) &&
  1015. (RENext->Word0 & macho::RF_Scattered);
  1016. unsigned RType;
  1017. if (isNextScattered)
  1018. RType = (RENext->Word0 >> 24) & 0xF;
  1019. else
  1020. RType = (RENext->Word1 >> 28) & 0xF;
  1021. if (RType != 1)
  1022. report_fatal_error("Expected GENERIC_RELOC_PAIR after "
  1023. "GENERIC_RELOC_LOCAL_SECTDIFF.");
  1024. printRelocationTargetName(RE, fmt);
  1025. fmt << "-";
  1026. printRelocationTargetName(RENext, fmt);
  1027. break;
  1028. }
  1029. case macho::RIT_Generic_TLV: {
  1030. printRelocationTargetName(RE, fmt);
  1031. fmt << "@TLV";
  1032. if (isPCRel) fmt << "P";
  1033. break;
  1034. }
  1035. default:
  1036. printRelocationTargetName(RE, fmt);
  1037. }
  1038. } else { // ARM-specific relocations
  1039. switch (Type) {
  1040. case macho::RIT_ARM_Half: // ARM_RELOC_HALF
  1041. case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
  1042. // Half relocations steal a bit from the length field to encode
  1043. // whether this is an upper16 or a lower16 relocation.
  1044. bool isUpper;
  1045. if (isScattered)
  1046. isUpper = (RE->Word0 >> 28) & 1;
  1047. else
  1048. isUpper = (RE->Word1 >> 25) & 1;
  1049. if (isUpper)
  1050. fmt << ":upper16:(";
  1051. else
  1052. fmt << ":lower16:(";
  1053. printRelocationTargetName(RE, fmt);
  1054. InMemoryStruct<macho::RelocationEntry> RENext;
  1055. DataRefImpl RelNext = Rel;
  1056. RelNext.d.a++;
  1057. getRelocation(RelNext, RENext);
  1058. // ARM half relocs must be followed by a relocation of type
  1059. // ARM_RELOC_PAIR.
  1060. bool isNextScattered = (Arch != Triple::x86_64) &&
  1061. (RENext->Word0 & macho::RF_Scattered);
  1062. unsigned RType;
  1063. if (isNextScattered)
  1064. RType = (RENext->Word0 >> 24) & 0xF;
  1065. else
  1066. RType = (RENext->Word1 >> 28) & 0xF;
  1067. if (RType != 1)
  1068. report_fatal_error("Expected ARM_RELOC_PAIR after "
  1069. "GENERIC_RELOC_HALF");
  1070. // NOTE: The half of the target virtual address is stashed in the
  1071. // address field of the secondary relocation, but we can't reverse
  1072. // engineer the constant offset from it without decoding the movw/movt
  1073. // instruction to find the other half in its immediate field.
  1074. // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
  1075. // symbol/section pointer of the follow-on relocation.
  1076. if (Type == macho::RIT_ARM_HalfDifference) {
  1077. fmt << "-";
  1078. printRelocationTargetName(RENext, fmt);
  1079. }
  1080. fmt << ")";
  1081. break;
  1082. }
  1083. default: {
  1084. printRelocationTargetName(RE, fmt);
  1085. }
  1086. }
  1087. }
  1088. } else
  1089. printRelocationTargetName(RE, fmt);
  1090. fmt.flush();
  1091. Result.append(fmtbuf.begin(), fmtbuf.end());
  1092. return object_error::success;
  1093. }
  1094. error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
  1095. bool &Result) const {
  1096. InMemoryStruct<macho::RelocationEntry> RE;
  1097. getRelocation(Rel, RE);
  1098. unsigned Arch = getArch();
  1099. bool isScattered = (Arch != Triple::x86_64) &&
  1100. (RE->Word0 & macho::RF_Scattered);
  1101. unsigned Type;
  1102. if (isScattered)
  1103. Type = (RE->Word0 >> 24) & 0xF;
  1104. else
  1105. Type = (RE->Word1 >> 28) & 0xF;
  1106. Result = false;
  1107. // On arches that use the generic relocations, GENERIC_RELOC_PAIR
  1108. // is always hidden.
  1109. if (Arch == Triple::x86 || Arch == Triple::arm) {
  1110. if (Type == macho::RIT_Pair) Result = true;
  1111. } else if (Arch == Triple::x86_64) {
  1112. // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
  1113. // an X864_64_RELOC_SUBTRACTOR.
  1114. if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
  1115. DataRefImpl RelPrev = Rel;
  1116. RelPrev.d.a--;
  1117. InMemoryStruct<macho::RelocationEntry> REPrev;
  1118. getRelocation(RelPrev, REPrev);
  1119. unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
  1120. if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
  1121. }
  1122. }
  1123. return object_error::success;
  1124. }
  1125. error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
  1126. LibraryRef &Res) const {
  1127. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  1128. }
  1129. error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
  1130. StringRef &Res) const {
  1131. report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
  1132. }
  1133. /*===-- Miscellaneous -----------------------------------------------------===*/
  1134. uint8_t MachOObjectFile::getBytesInAddress() const {
  1135. return MachOObj->is64Bit() ? 8 : 4;
  1136. }
  1137. StringRef MachOObjectFile::getFileFormatName() const {
  1138. if (!MachOObj->is64Bit()) {
  1139. switch (MachOObj->getHeader().CPUType) {
  1140. case llvm::MachO::CPUTypeI386:
  1141. return "Mach-O 32-bit i386";
  1142. case llvm::MachO::CPUTypeARM:
  1143. return "Mach-O arm";
  1144. case llvm::MachO::CPUTypePowerPC:
  1145. return "Mach-O 32-bit ppc";
  1146. default:
  1147. assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
  1148. "64-bit object file when we're not 64-bit?");
  1149. return "Mach-O 32-bit unknown";
  1150. }
  1151. }
  1152. switch (MachOObj->getHeader().CPUType) {
  1153. case llvm::MachO::CPUTypeX86_64:
  1154. return "Mach-O 64-bit x86-64";
  1155. case llvm::MachO::CPUTypePowerPC64:
  1156. return "Mach-O 64-bit ppc64";
  1157. default:
  1158. assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
  1159. "32-bit object file when we're 64-bit?");
  1160. return "Mach-O 64-bit unknown";
  1161. }
  1162. }
  1163. unsigned MachOObjectFile::getArch() const {
  1164. switch (MachOObj->getHeader().CPUType) {
  1165. case llvm::MachO::CPUTypeI386:
  1166. return Triple::x86;
  1167. case llvm::MachO::CPUTypeX86_64:
  1168. return Triple::x86_64;
  1169. case llvm::MachO::CPUTypeARM:
  1170. return Triple::arm;
  1171. case llvm::MachO::CPUTypePowerPC:
  1172. return Triple::ppc;
  1173. case llvm::MachO::CPUTypePowerPC64:
  1174. return Triple::ppc64;
  1175. default:
  1176. return Triple::UnknownArch;
  1177. }
  1178. }
  1179. } // end namespace object
  1180. } // end namespace llvm