MachOLinkGraphBuilder.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. //=--------- MachOLinkGraphBuilder.cpp - MachO LinkGraph builder ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Generic MachO LinkGraph buliding code.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MachOLinkGraphBuilder.h"
  13. #define DEBUG_TYPE "jitlink"
  14. static const char *CommonSectionName = "__common";
  15. namespace llvm {
  16. namespace jitlink {
  17. MachOLinkGraphBuilder::~MachOLinkGraphBuilder() {}
  18. Expected<std::unique_ptr<LinkGraph>> MachOLinkGraphBuilder::buildGraph() {
  19. // We only operate on relocatable objects.
  20. if (!Obj.isRelocatableObject())
  21. return make_error<JITLinkError>("Object is not a relocatable MachO");
  22. if (auto Err = createNormalizedSections())
  23. return std::move(Err);
  24. if (auto Err = createNormalizedSymbols())
  25. return std::move(Err);
  26. if (auto Err = graphifyRegularSymbols())
  27. return std::move(Err);
  28. if (auto Err = graphifySectionsWithCustomParsers())
  29. return std::move(Err);
  30. if (auto Err = addRelocations())
  31. return std::move(Err);
  32. return std::move(G);
  33. }
  34. MachOLinkGraphBuilder::MachOLinkGraphBuilder(
  35. const object::MachOObjectFile &Obj, Triple TT,
  36. LinkGraph::GetEdgeKindNameFunction GetEdgeKindName)
  37. : Obj(Obj),
  38. G(std::make_unique<LinkGraph>(
  39. std::string(Obj.getFileName()), std::move(TT), getPointerSize(Obj),
  40. getEndianness(Obj), std::move(GetEdgeKindName))) {}
  41. void MachOLinkGraphBuilder::addCustomSectionParser(
  42. StringRef SectionName, SectionParserFunction Parser) {
  43. assert(!CustomSectionParserFunctions.count(SectionName) &&
  44. "Custom parser for this section already exists");
  45. CustomSectionParserFunctions[SectionName] = std::move(Parser);
  46. }
  47. Linkage MachOLinkGraphBuilder::getLinkage(uint16_t Desc) {
  48. if ((Desc & MachO::N_WEAK_DEF) || (Desc & MachO::N_WEAK_REF))
  49. return Linkage::Weak;
  50. return Linkage::Strong;
  51. }
  52. Scope MachOLinkGraphBuilder::getScope(StringRef Name, uint8_t Type) {
  53. if (Type & MachO::N_EXT) {
  54. if ((Type & MachO::N_PEXT) || Name.startswith("l"))
  55. return Scope::Hidden;
  56. else
  57. return Scope::Default;
  58. }
  59. return Scope::Local;
  60. }
  61. bool MachOLinkGraphBuilder::isAltEntry(const NormalizedSymbol &NSym) {
  62. return NSym.Desc & MachO::N_ALT_ENTRY;
  63. }
  64. bool MachOLinkGraphBuilder::isDebugSection(const NormalizedSection &NSec) {
  65. return (NSec.Flags & MachO::S_ATTR_DEBUG &&
  66. strcmp(NSec.SegName, "__DWARF") == 0);
  67. }
  68. bool MachOLinkGraphBuilder::isZeroFillSection(const NormalizedSection &NSec) {
  69. switch (NSec.Flags & MachO::SECTION_TYPE) {
  70. case MachO::S_ZEROFILL:
  71. case MachO::S_GB_ZEROFILL:
  72. case MachO::S_THREAD_LOCAL_ZEROFILL:
  73. return true;
  74. default:
  75. return false;
  76. }
  77. }
  78. unsigned
  79. MachOLinkGraphBuilder::getPointerSize(const object::MachOObjectFile &Obj) {
  80. return Obj.is64Bit() ? 8 : 4;
  81. }
  82. support::endianness
  83. MachOLinkGraphBuilder::getEndianness(const object::MachOObjectFile &Obj) {
  84. return Obj.isLittleEndian() ? support::little : support::big;
  85. }
  86. Section &MachOLinkGraphBuilder::getCommonSection() {
  87. if (!CommonSection)
  88. CommonSection =
  89. &G->createSection(CommonSectionName, MemProt::Read | MemProt::Write);
  90. return *CommonSection;
  91. }
  92. Error MachOLinkGraphBuilder::createNormalizedSections() {
  93. // Build normalized sections. Verifies that section data is in-range (for
  94. // sections with content) and that address ranges are non-overlapping.
  95. LLVM_DEBUG(dbgs() << "Creating normalized sections...\n");
  96. for (auto &SecRef : Obj.sections()) {
  97. NormalizedSection NSec;
  98. uint32_t DataOffset = 0;
  99. auto SecIndex = Obj.getSectionIndex(SecRef.getRawDataRefImpl());
  100. if (Obj.is64Bit()) {
  101. const MachO::section_64 &Sec64 =
  102. Obj.getSection64(SecRef.getRawDataRefImpl());
  103. memcpy(&NSec.SectName, &Sec64.sectname, 16);
  104. NSec.SectName[16] = '\0';
  105. memcpy(&NSec.SegName, Sec64.segname, 16);
  106. NSec.SegName[16] = '\0';
  107. NSec.Address = orc::ExecutorAddr(Sec64.addr);
  108. NSec.Size = Sec64.size;
  109. NSec.Alignment = 1ULL << Sec64.align;
  110. NSec.Flags = Sec64.flags;
  111. DataOffset = Sec64.offset;
  112. } else {
  113. const MachO::section &Sec32 = Obj.getSection(SecRef.getRawDataRefImpl());
  114. memcpy(&NSec.SectName, &Sec32.sectname, 16);
  115. NSec.SectName[16] = '\0';
  116. memcpy(&NSec.SegName, Sec32.segname, 16);
  117. NSec.SegName[16] = '\0';
  118. NSec.Address = orc::ExecutorAddr(Sec32.addr);
  119. NSec.Size = Sec32.size;
  120. NSec.Alignment = 1ULL << Sec32.align;
  121. NSec.Flags = Sec32.flags;
  122. DataOffset = Sec32.offset;
  123. }
  124. LLVM_DEBUG({
  125. dbgs() << " " << NSec.SegName << "," << NSec.SectName << ": "
  126. << formatv("{0:x16}", NSec.Address) << " -- "
  127. << formatv("{0:x16}", NSec.Address + NSec.Size)
  128. << ", align: " << NSec.Alignment << ", index: " << SecIndex
  129. << "\n";
  130. });
  131. // Get the section data if any.
  132. if (!isZeroFillSection(NSec)) {
  133. if (DataOffset + NSec.Size > Obj.getData().size())
  134. return make_error<JITLinkError>(
  135. "Section data extends past end of file");
  136. NSec.Data = Obj.getData().data() + DataOffset;
  137. }
  138. // Get prot flags.
  139. // FIXME: Make sure this test is correct (it's probably missing cases
  140. // as-is).
  141. MemProt Prot;
  142. if (NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS)
  143. Prot = MemProt::Read | MemProt::Exec;
  144. else
  145. Prot = MemProt::Read | MemProt::Write;
  146. auto FullyQualifiedName =
  147. G->allocateString(StringRef(NSec.SegName) + "," + NSec.SectName);
  148. NSec.GraphSection = &G->createSection(
  149. StringRef(FullyQualifiedName.data(), FullyQualifiedName.size()), Prot);
  150. IndexToSection.insert(std::make_pair(SecIndex, std::move(NSec)));
  151. }
  152. std::vector<NormalizedSection *> Sections;
  153. Sections.reserve(IndexToSection.size());
  154. for (auto &KV : IndexToSection)
  155. Sections.push_back(&KV.second);
  156. // If we didn't end up creating any sections then bail out. The code below
  157. // assumes that we have at least one section.
  158. if (Sections.empty())
  159. return Error::success();
  160. llvm::sort(Sections,
  161. [](const NormalizedSection *LHS, const NormalizedSection *RHS) {
  162. assert(LHS && RHS && "Null section?");
  163. if (LHS->Address != RHS->Address)
  164. return LHS->Address < RHS->Address;
  165. return LHS->Size < RHS->Size;
  166. });
  167. for (unsigned I = 0, E = Sections.size() - 1; I != E; ++I) {
  168. auto &Cur = *Sections[I];
  169. auto &Next = *Sections[I + 1];
  170. if (Next.Address < Cur.Address + Cur.Size)
  171. return make_error<JITLinkError>(
  172. "Address range for section " +
  173. formatv("\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Cur.SegName,
  174. Cur.SectName, Cur.Address, Cur.Address + Cur.Size) +
  175. "overlaps section \"" + Next.SegName + "/" + Next.SectName + "\"" +
  176. formatv("\"{0}/{1}\" [ {2:x16} -- {3:x16} ] ", Next.SegName,
  177. Next.SectName, Next.Address, Next.Address + Next.Size));
  178. }
  179. return Error::success();
  180. }
  181. Error MachOLinkGraphBuilder::createNormalizedSymbols() {
  182. LLVM_DEBUG(dbgs() << "Creating normalized symbols...\n");
  183. for (auto &SymRef : Obj.symbols()) {
  184. unsigned SymbolIndex = Obj.getSymbolIndex(SymRef.getRawDataRefImpl());
  185. uint64_t Value;
  186. uint32_t NStrX;
  187. uint8_t Type;
  188. uint8_t Sect;
  189. uint16_t Desc;
  190. if (Obj.is64Bit()) {
  191. const MachO::nlist_64 &NL64 =
  192. Obj.getSymbol64TableEntry(SymRef.getRawDataRefImpl());
  193. Value = NL64.n_value;
  194. NStrX = NL64.n_strx;
  195. Type = NL64.n_type;
  196. Sect = NL64.n_sect;
  197. Desc = NL64.n_desc;
  198. } else {
  199. const MachO::nlist &NL32 =
  200. Obj.getSymbolTableEntry(SymRef.getRawDataRefImpl());
  201. Value = NL32.n_value;
  202. NStrX = NL32.n_strx;
  203. Type = NL32.n_type;
  204. Sect = NL32.n_sect;
  205. Desc = NL32.n_desc;
  206. }
  207. // Skip stabs.
  208. // FIXME: Are there other symbols we should be skipping?
  209. if (Type & MachO::N_STAB)
  210. continue;
  211. Optional<StringRef> Name;
  212. if (NStrX) {
  213. if (auto NameOrErr = SymRef.getName())
  214. Name = *NameOrErr;
  215. else
  216. return NameOrErr.takeError();
  217. }
  218. LLVM_DEBUG({
  219. dbgs() << " ";
  220. if (!Name)
  221. dbgs() << "<anonymous symbol>";
  222. else
  223. dbgs() << *Name;
  224. dbgs() << ": value = " << formatv("{0:x16}", Value)
  225. << ", type = " << formatv("{0:x2}", Type)
  226. << ", desc = " << formatv("{0:x4}", Desc) << ", sect = ";
  227. if (Sect)
  228. dbgs() << static_cast<unsigned>(Sect - 1);
  229. else
  230. dbgs() << "none";
  231. dbgs() << "\n";
  232. });
  233. // If this symbol has a section, verify that the addresses line up.
  234. if (Sect != 0) {
  235. auto NSec = findSectionByIndex(Sect - 1);
  236. if (!NSec)
  237. return NSec.takeError();
  238. if (orc::ExecutorAddr(Value) < NSec->Address ||
  239. orc::ExecutorAddr(Value) > NSec->Address + NSec->Size)
  240. return make_error<JITLinkError>("Address " + formatv("{0:x}", Value) +
  241. " for symbol " + *Name +
  242. " does not fall within section");
  243. if (!NSec->GraphSection) {
  244. LLVM_DEBUG({
  245. dbgs() << " Skipping: Symbol is in section " << NSec->SegName << "/"
  246. << NSec->SectName
  247. << " which has no associated graph section.\n";
  248. });
  249. continue;
  250. }
  251. }
  252. IndexToSymbol[SymbolIndex] =
  253. &createNormalizedSymbol(*Name, Value, Type, Sect, Desc,
  254. getLinkage(Desc), getScope(*Name, Type));
  255. }
  256. return Error::success();
  257. }
  258. void MachOLinkGraphBuilder::addSectionStartSymAndBlock(
  259. unsigned SecIndex, Section &GraphSec, orc::ExecutorAddr Address,
  260. const char *Data, orc::ExecutorAddrDiff Size, uint32_t Alignment,
  261. bool IsLive) {
  262. Block &B =
  263. Data ? G->createContentBlock(GraphSec, ArrayRef<char>(Data, Size),
  264. Address, Alignment, 0)
  265. : G->createZeroFillBlock(GraphSec, Size, Address, Alignment, 0);
  266. auto &Sym = G->addAnonymousSymbol(B, 0, Size, false, IsLive);
  267. auto SecI = IndexToSection.find(SecIndex);
  268. assert(SecI != IndexToSection.end() && "SecIndex invalid");
  269. auto &NSec = SecI->second;
  270. assert(!NSec.CanonicalSymbols.count(Sym.getAddress()) &&
  271. "Anonymous block start symbol clashes with existing symbol address");
  272. NSec.CanonicalSymbols[Sym.getAddress()] = &Sym;
  273. }
  274. Error MachOLinkGraphBuilder::graphifyRegularSymbols() {
  275. LLVM_DEBUG(dbgs() << "Creating graph symbols...\n");
  276. /// We only have 256 section indexes: Use a vector rather than a map.
  277. std::vector<std::vector<NormalizedSymbol *>> SecIndexToSymbols;
  278. SecIndexToSymbols.resize(256);
  279. // Create commons, externs, and absolutes, and partition all other symbols by
  280. // section.
  281. for (auto &KV : IndexToSymbol) {
  282. auto &NSym = *KV.second;
  283. switch (NSym.Type & MachO::N_TYPE) {
  284. case MachO::N_UNDF:
  285. if (NSym.Value) {
  286. if (!NSym.Name)
  287. return make_error<JITLinkError>("Anonymous common symbol at index " +
  288. Twine(KV.first));
  289. NSym.GraphSymbol = &G->addCommonSymbol(
  290. *NSym.Name, NSym.S, getCommonSection(), orc::ExecutorAddr(),
  291. orc::ExecutorAddrDiff(NSym.Value),
  292. 1ull << MachO::GET_COMM_ALIGN(NSym.Desc),
  293. NSym.Desc & MachO::N_NO_DEAD_STRIP);
  294. } else {
  295. if (!NSym.Name)
  296. return make_error<JITLinkError>("Anonymous external symbol at "
  297. "index " +
  298. Twine(KV.first));
  299. NSym.GraphSymbol = &G->addExternalSymbol(
  300. *NSym.Name, 0,
  301. NSym.Desc & MachO::N_WEAK_REF ? Linkage::Weak : Linkage::Strong);
  302. }
  303. break;
  304. case MachO::N_ABS:
  305. if (!NSym.Name)
  306. return make_error<JITLinkError>("Anonymous absolute symbol at index " +
  307. Twine(KV.first));
  308. NSym.GraphSymbol = &G->addAbsoluteSymbol(
  309. *NSym.Name, orc::ExecutorAddr(NSym.Value), 0, Linkage::Strong,
  310. Scope::Default, NSym.Desc & MachO::N_NO_DEAD_STRIP);
  311. break;
  312. case MachO::N_SECT:
  313. SecIndexToSymbols[NSym.Sect - 1].push_back(&NSym);
  314. break;
  315. case MachO::N_PBUD:
  316. return make_error<JITLinkError>(
  317. "Unupported N_PBUD symbol " +
  318. (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) +
  319. " at index " + Twine(KV.first));
  320. case MachO::N_INDR:
  321. return make_error<JITLinkError>(
  322. "Unupported N_INDR symbol " +
  323. (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) +
  324. " at index " + Twine(KV.first));
  325. default:
  326. return make_error<JITLinkError>(
  327. "Unrecognized symbol type " + Twine(NSym.Type & MachO::N_TYPE) +
  328. " for symbol " +
  329. (NSym.Name ? ("\"" + *NSym.Name + "\"") : Twine("<anon>")) +
  330. " at index " + Twine(KV.first));
  331. }
  332. }
  333. // Loop over sections performing regular graphification for those that
  334. // don't have custom parsers.
  335. for (auto &KV : IndexToSection) {
  336. auto SecIndex = KV.first;
  337. auto &NSec = KV.second;
  338. if (!NSec.GraphSection) {
  339. LLVM_DEBUG({
  340. dbgs() << " " << NSec.SegName << "/" << NSec.SectName
  341. << " has no graph section. Skipping.\n";
  342. });
  343. continue;
  344. }
  345. // Skip sections with custom parsers.
  346. if (CustomSectionParserFunctions.count(NSec.GraphSection->getName())) {
  347. LLVM_DEBUG({
  348. dbgs() << " Skipping section " << NSec.GraphSection->getName()
  349. << " as it has a custom parser.\n";
  350. });
  351. continue;
  352. } else if ((NSec.Flags & MachO::SECTION_TYPE) ==
  353. MachO::S_CSTRING_LITERALS) {
  354. if (auto Err = graphifyCStringSection(
  355. NSec, std::move(SecIndexToSymbols[SecIndex])))
  356. return Err;
  357. continue;
  358. } else
  359. LLVM_DEBUG({
  360. dbgs() << " Graphifying regular section "
  361. << NSec.GraphSection->getName() << "...\n";
  362. });
  363. bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP;
  364. bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
  365. auto &SecNSymStack = SecIndexToSymbols[SecIndex];
  366. // If this section is non-empty but there are no symbols covering it then
  367. // create one block and anonymous symbol to cover the entire section.
  368. if (SecNSymStack.empty()) {
  369. if (NSec.Size > 0) {
  370. LLVM_DEBUG({
  371. dbgs() << " Section non-empty, but contains no symbols. "
  372. "Creating anonymous block to cover "
  373. << formatv("{0:x16}", NSec.Address) << " -- "
  374. << formatv("{0:x16}", NSec.Address + NSec.Size) << "\n";
  375. });
  376. addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address,
  377. NSec.Data, NSec.Size, NSec.Alignment,
  378. SectionIsNoDeadStrip);
  379. } else
  380. LLVM_DEBUG({
  381. dbgs() << " Section empty and contains no symbols. Skipping.\n";
  382. });
  383. continue;
  384. }
  385. // Sort the symbol stack in by address, alt-entry status, scope, and name.
  386. // We sort in reverse order so that symbols will be visited in the right
  387. // order when we pop off the stack below.
  388. llvm::sort(SecNSymStack, [](const NormalizedSymbol *LHS,
  389. const NormalizedSymbol *RHS) {
  390. if (LHS->Value != RHS->Value)
  391. return LHS->Value > RHS->Value;
  392. if (isAltEntry(*LHS) != isAltEntry(*RHS))
  393. return isAltEntry(*RHS);
  394. if (LHS->S != RHS->S)
  395. return static_cast<uint8_t>(LHS->S) < static_cast<uint8_t>(RHS->S);
  396. return LHS->Name < RHS->Name;
  397. });
  398. // The first symbol in a section can not be an alt-entry symbol.
  399. if (!SecNSymStack.empty() && isAltEntry(*SecNSymStack.back()))
  400. return make_error<JITLinkError>(
  401. "First symbol in " + NSec.GraphSection->getName() + " is alt-entry");
  402. // If the section is non-empty but there is no symbol covering the start
  403. // address then add an anonymous one.
  404. if (orc::ExecutorAddr(SecNSymStack.back()->Value) != NSec.Address) {
  405. auto AnonBlockSize =
  406. orc::ExecutorAddr(SecNSymStack.back()->Value) - NSec.Address;
  407. LLVM_DEBUG({
  408. dbgs() << " Section start not covered by symbol. "
  409. << "Creating anonymous block to cover [ " << NSec.Address
  410. << " -- " << (NSec.Address + AnonBlockSize) << " ]\n";
  411. });
  412. addSectionStartSymAndBlock(SecIndex, *NSec.GraphSection, NSec.Address,
  413. NSec.Data, AnonBlockSize, NSec.Alignment,
  414. SectionIsNoDeadStrip);
  415. }
  416. // Visit section symbols in order by popping off the reverse-sorted stack,
  417. // building blocks for each alt-entry chain and creating symbols as we go.
  418. while (!SecNSymStack.empty()) {
  419. SmallVector<NormalizedSymbol *, 8> BlockSyms;
  420. BlockSyms.push_back(SecNSymStack.back());
  421. SecNSymStack.pop_back();
  422. while (!SecNSymStack.empty() &&
  423. (isAltEntry(*SecNSymStack.back()) ||
  424. SecNSymStack.back()->Value == BlockSyms.back()->Value)) {
  425. BlockSyms.push_back(SecNSymStack.back());
  426. SecNSymStack.pop_back();
  427. }
  428. // BlockNSyms now contains the block symbols in reverse canonical order.
  429. auto BlockStart = orc::ExecutorAddr(BlockSyms.front()->Value);
  430. orc::ExecutorAddr BlockEnd =
  431. SecNSymStack.empty() ? NSec.Address + NSec.Size
  432. : orc::ExecutorAddr(SecNSymStack.back()->Value);
  433. orc::ExecutorAddrDiff BlockOffset = BlockStart - NSec.Address;
  434. orc::ExecutorAddrDiff BlockSize = BlockEnd - BlockStart;
  435. LLVM_DEBUG({
  436. dbgs() << " Creating block for " << formatv("{0:x16}", BlockStart)
  437. << " -- " << formatv("{0:x16}", BlockEnd) << ": "
  438. << NSec.GraphSection->getName() << " + "
  439. << formatv("{0:x16}", BlockOffset) << " with "
  440. << BlockSyms.size() << " symbol(s)...\n";
  441. });
  442. Block &B =
  443. NSec.Data
  444. ? G->createContentBlock(
  445. *NSec.GraphSection,
  446. ArrayRef<char>(NSec.Data + BlockOffset, BlockSize),
  447. BlockStart, NSec.Alignment, BlockStart % NSec.Alignment)
  448. : G->createZeroFillBlock(*NSec.GraphSection, BlockSize,
  449. BlockStart, NSec.Alignment,
  450. BlockStart % NSec.Alignment);
  451. Optional<orc::ExecutorAddr> LastCanonicalAddr;
  452. auto SymEnd = BlockEnd;
  453. while (!BlockSyms.empty()) {
  454. auto &NSym = *BlockSyms.back();
  455. BlockSyms.pop_back();
  456. bool SymLive =
  457. (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip;
  458. auto &Sym = createStandardGraphSymbol(
  459. NSym, B, SymEnd - orc::ExecutorAddr(NSym.Value), SectionIsText,
  460. SymLive, LastCanonicalAddr != orc::ExecutorAddr(NSym.Value));
  461. if (LastCanonicalAddr != Sym.getAddress()) {
  462. if (LastCanonicalAddr)
  463. SymEnd = *LastCanonicalAddr;
  464. LastCanonicalAddr = Sym.getAddress();
  465. }
  466. }
  467. }
  468. }
  469. return Error::success();
  470. }
  471. Symbol &MachOLinkGraphBuilder::createStandardGraphSymbol(NormalizedSymbol &NSym,
  472. Block &B, size_t Size,
  473. bool IsText,
  474. bool IsNoDeadStrip,
  475. bool IsCanonical) {
  476. LLVM_DEBUG({
  477. dbgs() << " " << formatv("{0:x16}", NSym.Value) << " -- "
  478. << formatv("{0:x16}", NSym.Value + Size) << ": ";
  479. if (!NSym.Name)
  480. dbgs() << "<anonymous symbol>";
  481. else
  482. dbgs() << NSym.Name;
  483. if (IsText)
  484. dbgs() << " [text]";
  485. if (IsNoDeadStrip)
  486. dbgs() << " [no-dead-strip]";
  487. if (!IsCanonical)
  488. dbgs() << " [non-canonical]";
  489. dbgs() << "\n";
  490. });
  491. auto SymOffset = orc::ExecutorAddr(NSym.Value) - B.getAddress();
  492. auto &Sym =
  493. NSym.Name
  494. ? G->addDefinedSymbol(B, SymOffset, *NSym.Name, Size, NSym.L, NSym.S,
  495. IsText, IsNoDeadStrip)
  496. : G->addAnonymousSymbol(B, SymOffset, Size, IsText, IsNoDeadStrip);
  497. NSym.GraphSymbol = &Sym;
  498. if (IsCanonical)
  499. setCanonicalSymbol(getSectionByIndex(NSym.Sect - 1), Sym);
  500. return Sym;
  501. }
  502. Error MachOLinkGraphBuilder::graphifySectionsWithCustomParsers() {
  503. // Graphify special sections.
  504. for (auto &KV : IndexToSection) {
  505. auto &NSec = KV.second;
  506. // Skip non-graph sections.
  507. if (!NSec.GraphSection)
  508. continue;
  509. auto HI = CustomSectionParserFunctions.find(NSec.GraphSection->getName());
  510. if (HI != CustomSectionParserFunctions.end()) {
  511. auto &Parse = HI->second;
  512. if (auto Err = Parse(NSec))
  513. return Err;
  514. }
  515. }
  516. return Error::success();
  517. }
  518. Error MachOLinkGraphBuilder::graphifyCStringSection(
  519. NormalizedSection &NSec, std::vector<NormalizedSymbol *> NSyms) {
  520. assert(NSec.GraphSection && "C string literal section missing graph section");
  521. assert(NSec.Data && "C string literal section has no data");
  522. LLVM_DEBUG({
  523. dbgs() << " Graphifying C-string literal section "
  524. << NSec.GraphSection->getName() << "\n";
  525. });
  526. if (NSec.Data[NSec.Size - 1] != '\0')
  527. return make_error<JITLinkError>("C string literal section " +
  528. NSec.GraphSection->getName() +
  529. " does not end with null terminator");
  530. /// Sort into reverse order to use as a stack.
  531. llvm::sort(NSyms,
  532. [](const NormalizedSymbol *LHS, const NormalizedSymbol *RHS) {
  533. if (LHS->Value != RHS->Value)
  534. return LHS->Value > RHS->Value;
  535. if (LHS->L != RHS->L)
  536. return LHS->L > RHS->L;
  537. if (LHS->S != RHS->S)
  538. return LHS->S > RHS->S;
  539. if (RHS->Name) {
  540. if (!LHS->Name)
  541. return true;
  542. return *LHS->Name > *RHS->Name;
  543. }
  544. return false;
  545. });
  546. bool SectionIsNoDeadStrip = NSec.Flags & MachO::S_ATTR_NO_DEAD_STRIP;
  547. bool SectionIsText = NSec.Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
  548. orc::ExecutorAddrDiff BlockStart = 0;
  549. // Scan section for null characters.
  550. for (size_t I = 0; I != NSec.Size; ++I)
  551. if (NSec.Data[I] == '\0') {
  552. orc::ExecutorAddrDiff BlockEnd = I + 1;
  553. size_t BlockSize = BlockEnd - BlockStart;
  554. // Create a block for this null terminated string.
  555. auto &B = G->createContentBlock(*NSec.GraphSection,
  556. {NSec.Data + BlockStart, BlockSize},
  557. NSec.Address + BlockStart, 1, 0);
  558. LLVM_DEBUG({
  559. dbgs() << " Created block " << formatv("{0:x}", B.getAddress())
  560. << " -- " << formatv("{0:x}", B.getAddress() + B.getSize())
  561. << " for \"" << StringRef(B.getContent().data()) << "\"\n";
  562. });
  563. // If there's no symbol at the start of this block then create one.
  564. if (NSyms.empty() ||
  565. orc::ExecutorAddr(NSyms.back()->Value) != B.getAddress()) {
  566. auto &S = G->addAnonymousSymbol(B, 0, BlockSize, false, false);
  567. setCanonicalSymbol(NSec, S);
  568. LLVM_DEBUG({
  569. dbgs() << " Adding anonymous symbol for c-string block "
  570. << formatv("{0:x16} -- {1:x16}", S.getAddress(),
  571. S.getAddress() + BlockSize)
  572. << "\n";
  573. });
  574. }
  575. // Process any remaining symbols that point into this block.
  576. auto LastCanonicalAddr = B.getAddress() + BlockEnd;
  577. while (!NSyms.empty() && orc::ExecutorAddr(NSyms.back()->Value) <
  578. B.getAddress() + BlockSize) {
  579. auto &NSym = *NSyms.back();
  580. size_t SymSize = (B.getAddress() + BlockSize) -
  581. orc::ExecutorAddr(NSyms.back()->Value);
  582. bool SymLive =
  583. (NSym.Desc & MachO::N_NO_DEAD_STRIP) || SectionIsNoDeadStrip;
  584. bool IsCanonical = false;
  585. if (LastCanonicalAddr != orc::ExecutorAddr(NSym.Value)) {
  586. IsCanonical = true;
  587. LastCanonicalAddr = orc::ExecutorAddr(NSym.Value);
  588. }
  589. createStandardGraphSymbol(NSym, B, SymSize, SectionIsText, SymLive,
  590. IsCanonical);
  591. NSyms.pop_back();
  592. }
  593. BlockStart += BlockSize;
  594. }
  595. return Error::success();
  596. }
  597. Error CompactUnwindSplitter::operator()(LinkGraph &G) {
  598. auto *CUSec = G.findSectionByName(CompactUnwindSectionName);
  599. if (!CUSec)
  600. return Error::success();
  601. if (!G.getTargetTriple().isOSBinFormatMachO())
  602. return make_error<JITLinkError>(
  603. "Error linking " + G.getName() +
  604. ": compact unwind splitting not supported on non-macho target " +
  605. G.getTargetTriple().str());
  606. unsigned CURecordSize = 0;
  607. unsigned PersonalityEdgeOffset = 0;
  608. unsigned LSDAEdgeOffset = 0;
  609. switch (G.getTargetTriple().getArch()) {
  610. case Triple::aarch64:
  611. case Triple::x86_64:
  612. // 64-bit compact-unwind record format:
  613. // Range start: 8 bytes.
  614. // Range size: 4 bytes.
  615. // CU encoding: 4 bytes.
  616. // Personality: 8 bytes.
  617. // LSDA: 8 bytes.
  618. CURecordSize = 32;
  619. PersonalityEdgeOffset = 16;
  620. LSDAEdgeOffset = 24;
  621. break;
  622. default:
  623. return make_error<JITLinkError>(
  624. "Error linking " + G.getName() +
  625. ": compact unwind splitting not supported on " +
  626. G.getTargetTriple().getArchName());
  627. }
  628. std::vector<Block *> OriginalBlocks(CUSec->blocks().begin(),
  629. CUSec->blocks().end());
  630. LLVM_DEBUG({
  631. dbgs() << "In " << G.getName() << " splitting compact unwind section "
  632. << CompactUnwindSectionName << " containing "
  633. << OriginalBlocks.size() << " initial blocks...\n";
  634. });
  635. while (!OriginalBlocks.empty()) {
  636. auto *B = OriginalBlocks.back();
  637. OriginalBlocks.pop_back();
  638. if (B->getSize() == 0) {
  639. LLVM_DEBUG({
  640. dbgs() << " Skipping empty block at "
  641. << formatv("{0:x16}", B->getAddress()) << "\n";
  642. });
  643. continue;
  644. }
  645. LLVM_DEBUG({
  646. dbgs() << " Splitting block at " << formatv("{0:x16}", B->getAddress())
  647. << " into " << (B->getSize() / CURecordSize)
  648. << " compact unwind record(s)\n";
  649. });
  650. if (B->getSize() % CURecordSize)
  651. return make_error<JITLinkError>(
  652. "Error splitting compact unwind record in " + G.getName() +
  653. ": block at " + formatv("{0:x}", B->getAddress()) + " has size " +
  654. formatv("{0:x}", B->getSize()) +
  655. " (not a multiple of CU record size of " +
  656. formatv("{0:x}", CURecordSize) + ")");
  657. unsigned NumBlocks = B->getSize() / CURecordSize;
  658. LinkGraph::SplitBlockCache C;
  659. for (unsigned I = 0; I != NumBlocks; ++I) {
  660. auto &CURec = G.splitBlock(*B, CURecordSize, &C);
  661. bool AddedKeepAlive = false;
  662. for (auto &E : CURec.edges()) {
  663. if (E.getOffset() == 0) {
  664. LLVM_DEBUG({
  665. dbgs() << " Updating compact unwind record at "
  666. << formatv("{0:x16}", CURec.getAddress()) << " to point to "
  667. << (E.getTarget().hasName() ? E.getTarget().getName()
  668. : StringRef())
  669. << " (at " << formatv("{0:x16}", E.getTarget().getAddress())
  670. << ")\n";
  671. });
  672. if (E.getTarget().isExternal())
  673. return make_error<JITLinkError>(
  674. "Error adding keep-alive edge for compact unwind record at " +
  675. formatv("{0:x}", CURec.getAddress()) + ": target " +
  676. E.getTarget().getName() + " is an external symbol");
  677. auto &TgtBlock = E.getTarget().getBlock();
  678. auto &CURecSym =
  679. G.addAnonymousSymbol(CURec, 0, CURecordSize, false, false);
  680. TgtBlock.addEdge(Edge::KeepAlive, 0, CURecSym, 0);
  681. AddedKeepAlive = true;
  682. } else if (E.getOffset() != PersonalityEdgeOffset &&
  683. E.getOffset() != LSDAEdgeOffset)
  684. return make_error<JITLinkError>("Unexpected edge at offset " +
  685. formatv("{0:x}", E.getOffset()) +
  686. " in compact unwind record at " +
  687. formatv("{0:x}", CURec.getAddress()));
  688. }
  689. if (!AddedKeepAlive)
  690. return make_error<JITLinkError>(
  691. "Error adding keep-alive edge for compact unwind record at " +
  692. formatv("{0:x}", CURec.getAddress()) +
  693. ": no outgoing target edge at offset 0");
  694. }
  695. }
  696. return Error::success();
  697. }
  698. } // end namespace jitlink
  699. } // end namespace llvm