1 //===-- llvm-jitlink-macho.cpp -- MachO parsing support for llvm-jitlink --===// 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 // MachO parsing support for llvm-jitlink. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm-jitlink.h" 14 15 #include "llvm/Support/Error.h" 16 #include "llvm/Support/Path.h" 17 18 #define DEBUG_TYPE "llvm_jitlink" 19 20 using namespace llvm; 21 using namespace llvm::jitlink; 22 23 static bool isMachOGOTSection(Section &S) { return S.getName() == "$__GOT"; } 24 25 static bool isMachOStubsSection(Section &S) { 26 return S.getName() == "$__STUBS"; 27 } 28 29 static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) { 30 auto EItr = 31 llvm::find_if(B.edges(), [](Edge &E) { return E.isRelocation(); }); 32 if (EItr == B.edges().end()) 33 return make_error<StringError>("GOT entry in " + G.getName() + ", \"" + 34 B.getSection().getName() + 35 "\" has no relocations", 36 inconvertibleErrorCode()); 37 return *EItr; 38 } 39 40 static Expected<Symbol &> getMachOGOTTarget(LinkGraph &G, Block &B) { 41 auto E = getFirstRelocationEdge(G, B); 42 if (!E) 43 return E.takeError(); 44 auto &TargetSym = E->getTarget(); 45 if (!TargetSym.hasName()) 46 return make_error<StringError>( 47 "GOT entry in " + G.getName() + ", \"" + 48 TargetSym.getBlock().getSection().getName() + 49 "\" points to anonymous " 50 "symbol", 51 inconvertibleErrorCode()); 52 return TargetSym; 53 } 54 55 static Expected<Symbol &> getMachOStubTarget(LinkGraph &G, Block &B) { 56 auto E = getFirstRelocationEdge(G, B); 57 if (!E) 58 return E.takeError(); 59 auto &GOTSym = E->getTarget(); 60 if (!GOTSym.isDefined() || !isMachOGOTSection(GOTSym.getBlock().getSection())) 61 return make_error<StringError>( 62 "Stubs entry in " + G.getName() + ", \"" + 63 GOTSym.getBlock().getSection().getName() + 64 "\" does not point to GOT entry", 65 inconvertibleErrorCode()); 66 return getMachOGOTTarget(G, GOTSym.getBlock()); 67 } 68 69 namespace llvm { 70 71 Error registerMachOGraphInfo(Session &S, LinkGraph &G) { 72 std::lock_guard<std::mutex> Lock(S.M); 73 74 auto FileName = sys::path::filename(G.getName()); 75 if (S.FileInfos.count(FileName)) { 76 return make_error<StringError>("When -check is passed, file names must be " 77 "distinct (duplicate: \"" + 78 FileName + "\")", 79 inconvertibleErrorCode()); 80 } 81 82 auto &FileInfo = S.FileInfos[FileName]; 83 LLVM_DEBUG({ 84 dbgs() << "Registering MachO file info for \"" << FileName << "\"\n"; 85 }); 86 for (auto &Sec : G.sections()) { 87 LLVM_DEBUG({ 88 dbgs() << " Section \"" << Sec.getName() << "\": " 89 << (Sec.symbols().empty() ? "empty. skipping." : "processing...") 90 << "\n"; 91 }); 92 93 // Skip empty sections. 94 if (Sec.symbols().empty()) 95 continue; 96 97 if (FileInfo.SectionInfos.count(Sec.getName())) 98 return make_error<StringError>("Encountered duplicate section name \"" + 99 Sec.getName() + "\" in \"" + FileName + 100 "\"", 101 inconvertibleErrorCode()); 102 103 bool isGOTSection = isMachOGOTSection(Sec); 104 bool isStubsSection = isMachOStubsSection(Sec); 105 106 bool SectionContainsContent = false; 107 bool SectionContainsZeroFill = false; 108 109 auto *FirstSym = *Sec.symbols().begin(); 110 auto *LastSym = FirstSym; 111 for (auto *Sym : Sec.symbols()) { 112 if (Sym->getAddress() < FirstSym->getAddress()) 113 FirstSym = Sym; 114 if (Sym->getAddress() > LastSym->getAddress()) 115 LastSym = Sym; 116 if (isGOTSection || isStubsSection) { 117 Error Err = 118 isGOTSection 119 ? FileInfo.registerGOTEntry(G, *Sym, getMachOGOTTarget) 120 : FileInfo.registerStubEntry(G, *Sym, getMachOStubTarget); 121 if (Err) 122 return Err; 123 SectionContainsContent = true; 124 } else if (Sym->hasName()) { 125 if (Sym->isSymbolZeroFill()) { 126 S.SymbolInfos[Sym->getName()] = {Sym->getSize(), 127 Sym->getAddress().getValue()}; 128 SectionContainsZeroFill = true; 129 } else { 130 S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(), 131 Sym->getAddress().getValue(), 132 Sym->getTargetFlags()}; 133 SectionContainsContent = true; 134 } 135 } 136 } 137 138 auto SecAddr = FirstSym->getAddress(); 139 auto SecSize = 140 (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) - 141 SecAddr; 142 143 if (SectionContainsZeroFill && SectionContainsContent) 144 return make_error<StringError>("Mixed zero-fill and content sections not " 145 "supported yet", 146 inconvertibleErrorCode()); 147 if (SectionContainsZeroFill) 148 FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()}; 149 else 150 FileInfo.SectionInfos[Sec.getName()] = { 151 ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize), 152 SecAddr.getValue(), FirstSym->getTargetFlags()}; 153 } 154 155 return Error::success(); 156 } 157 158 } // end namespace llvm 159