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 = std::find_if(B.edges().begin(), B.edges().end(), 31 [](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 if (TargetSym.isDefined() || TargetSym.isAbsolute()) 53 return make_error<StringError>( 54 "GOT entry \"" + TargetSym.getName() + "\" in " + G.getName() + ", \"" + 55 TargetSym.getBlock().getSection().getName() + 56 "\" does not point to an external symbol", 57 inconvertibleErrorCode()); 58 return TargetSym; 59 } 60 61 static Expected<Symbol &> getMachOStubTarget(LinkGraph &G, Block &B) { 62 auto E = getFirstRelocationEdge(G, B); 63 if (!E) 64 return E.takeError(); 65 auto &GOTSym = E->getTarget(); 66 if (!GOTSym.isDefined() || !isMachOGOTSection(GOTSym.getBlock().getSection())) 67 return make_error<StringError>( 68 "Stubs entry in " + G.getName() + ", \"" + 69 GOTSym.getBlock().getSection().getName() + 70 "\" does not point to GOT entry", 71 inconvertibleErrorCode()); 72 return getMachOGOTTarget(G, GOTSym.getBlock()); 73 } 74 75 namespace llvm { 76 77 Error registerMachOGraphInfo(Session &S, LinkGraph &G) { 78 auto FileName = sys::path::filename(G.getName()); 79 if (S.FileInfos.count(FileName)) { 80 return make_error<StringError>("When -check is passed, file names must be " 81 "distinct (duplicate: \"" + 82 FileName + "\")", 83 inconvertibleErrorCode()); 84 } 85 86 auto &FileInfo = S.FileInfos[FileName]; 87 LLVM_DEBUG({ 88 dbgs() << "Registering MachO file info for \"" << FileName << "\"\n"; 89 }); 90 for (auto &Sec : G.sections()) { 91 LLVM_DEBUG({ 92 dbgs() << " Section \"" << Sec.getName() << "\": " 93 << (llvm::empty(Sec.symbols()) ? "empty. skipping." 94 : "processing...") 95 << "\n"; 96 }); 97 98 // Skip empty sections. 99 if (llvm::empty(Sec.symbols())) 100 continue; 101 102 if (FileInfo.SectionInfos.count(Sec.getName())) 103 return make_error<StringError>("Encountered duplicate section name \"" + 104 Sec.getName() + "\" in \"" + FileName + 105 "\"", 106 inconvertibleErrorCode()); 107 108 bool isGOTSection = isMachOGOTSection(Sec); 109 bool isStubsSection = isMachOStubsSection(Sec); 110 111 bool SectionContainsContent = false; 112 bool SectionContainsZeroFill = false; 113 114 auto *FirstSym = *Sec.symbols().begin(); 115 auto *LastSym = FirstSym; 116 for (auto *Sym : Sec.symbols()) { 117 if (Sym->getAddress() < FirstSym->getAddress()) 118 FirstSym = Sym; 119 if (Sym->getAddress() > LastSym->getAddress()) 120 LastSym = Sym; 121 if (isGOTSection) { 122 if (Sym->isSymbolZeroFill()) 123 return make_error<StringError>("zero-fill atom in GOT section", 124 inconvertibleErrorCode()); 125 126 if (auto TS = getMachOGOTTarget(G, Sym->getBlock())) 127 FileInfo.GOTEntryInfos[TS->getName()] = {Sym->getSymbolContent(), 128 Sym->getAddress()}; 129 else 130 return TS.takeError(); 131 SectionContainsContent = true; 132 } else if (isStubsSection) { 133 if (Sym->isSymbolZeroFill()) 134 return make_error<StringError>("zero-fill atom in Stub section", 135 inconvertibleErrorCode()); 136 137 if (auto TS = getMachOStubTarget(G, Sym->getBlock())) 138 FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(), 139 Sym->getAddress()}; 140 else 141 return TS.takeError(); 142 SectionContainsContent = true; 143 } else if (Sym->hasName()) { 144 if (Sym->isSymbolZeroFill()) { 145 S.SymbolInfos[Sym->getName()] = {Sym->getSize(), Sym->getAddress()}; 146 SectionContainsZeroFill = true; 147 } else { 148 S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(), 149 Sym->getAddress()}; 150 SectionContainsContent = true; 151 } 152 } 153 } 154 155 JITTargetAddress SecAddr = FirstSym->getAddress(); 156 uint64_t SecSize = 157 (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) - 158 SecAddr; 159 160 if (SectionContainsZeroFill && SectionContainsContent) 161 return make_error<StringError>("Mixed zero-fill and content sections not " 162 "supported yet", 163 inconvertibleErrorCode()); 164 if (SectionContainsZeroFill) 165 FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr}; 166 else 167 FileInfo.SectionInfos[Sec.getName()] = { 168 StringRef(FirstSym->getBlock().getContent().data(), SecSize), 169 SecAddr}; 170 } 171 172 return Error::success(); 173 } 174 175 } // end namespace llvm 176