Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 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 | // |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 9 | // MachO parsing support for llvm-jitlink. |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 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 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 29 | static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) { |
| 30 | auto EItr = std::find_if(B.edges().begin(), B.edges().end(), |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 31 | [](Edge &E) { return E.isRelocation(); }); |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 32 | if (EItr == B.edges().end()) |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 33 | return make_error<StringError>("GOT entry in " + G.getName() + ", \"" + |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 34 | B.getSection().getName() + |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 35 | "\" has no relocations", |
| 36 | inconvertibleErrorCode()); |
| 37 | return *EItr; |
| 38 | } |
| 39 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 40 | static Expected<Symbol &> getMachOGOTTarget(LinkGraph &G, Block &B) { |
| 41 | auto E = getFirstRelocationEdge(G, B); |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 42 | if (!E) |
| 43 | return E.takeError(); |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 44 | auto &TargetSym = E->getTarget(); |
| 45 | if (!TargetSym.hasName()) |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 46 | return make_error<StringError>( |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 47 | "GOT entry in " + G.getName() + ", \"" + |
| 48 | TargetSym.getBlock().getSection().getName() + |
| 49 | "\" points to anonymous " |
| 50 | "symbol", |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 51 | inconvertibleErrorCode()); |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 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; |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 61 | static Expected<Symbol &> getMachOStubTarget(LinkGraph &G, Block &B) { |
| 62 | auto E = getFirstRelocationEdge(G, B); |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 63 | if (!E) |
| 64 | return E.takeError(); |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 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()); |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | namespace llvm { |
| 76 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 77 | Error registerMachOStubsAndGOT(Session &S, LinkGraph &G) { |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 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() << "\": " |
Lang Hames | 8c4f048 | 2019-12-05 19:57:51 -0800 | [diff] [blame^] | 93 | << (llvm::empty(Sec.symbols()) ? "empty. skipping." : "processing...") |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 94 | << "\n"; |
| 95 | }); |
| 96 | |
| 97 | // Skip empty sections. |
Lang Hames | 8c4f048 | 2019-12-05 19:57:51 -0800 | [diff] [blame^] | 98 | if (llvm::empty(Sec.symbols())) |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 99 | continue; |
| 100 | |
| 101 | if (FileInfo.SectionInfos.count(Sec.getName())) |
| 102 | return make_error<StringError>("Encountered duplicate section name \"" + |
| 103 | Sec.getName() + "\" in \"" + FileName + |
| 104 | "\"", |
| 105 | inconvertibleErrorCode()); |
| 106 | |
| 107 | bool isGOTSection = isMachOGOTSection(Sec); |
| 108 | bool isStubsSection = isMachOStubsSection(Sec); |
| 109 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 110 | bool SectionContainsContent = false; |
| 111 | bool SectionContainsZeroFill = false; |
| 112 | |
| 113 | auto *FirstSym = *Sec.symbols().begin(); |
| 114 | auto *LastSym = FirstSym; |
| 115 | for (auto *Sym : Sec.symbols()) { |
| 116 | if (Sym->getAddress() < FirstSym->getAddress()) |
| 117 | FirstSym = Sym; |
| 118 | if (Sym->getAddress() > LastSym->getAddress()) |
| 119 | LastSym = Sym; |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 120 | if (isGOTSection) { |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 121 | if (Sym->isSymbolZeroFill()) |
| 122 | return make_error<StringError>("zero-fill atom in GOT section", |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 123 | inconvertibleErrorCode()); |
| 124 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 125 | if (auto TS = getMachOGOTTarget(G, Sym->getBlock())) |
| 126 | FileInfo.GOTEntryInfos[TS->getName()] = {Sym->getSymbolContent(), |
| 127 | Sym->getAddress()}; |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 128 | else |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 129 | return TS.takeError(); |
| 130 | SectionContainsContent = true; |
| 131 | } else if (isStubsSection) { |
| 132 | if (Sym->isSymbolZeroFill()) |
| 133 | return make_error<StringError>("zero-fill atom in Stub section", |
| 134 | inconvertibleErrorCode()); |
| 135 | |
| 136 | if (auto TS = getMachOStubTarget(G, Sym->getBlock())) |
| 137 | FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(), |
| 138 | Sym->getAddress()}; |
| 139 | else |
| 140 | return TS.takeError(); |
| 141 | SectionContainsContent = true; |
| 142 | } else if (Sym->hasName()) { |
| 143 | if (Sym->isSymbolZeroFill()) { |
| 144 | S.SymbolInfos[Sym->getName()] = {Sym->getSize(), Sym->getAddress()}; |
| 145 | SectionContainsZeroFill = true; |
| 146 | } else { |
| 147 | S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(), |
| 148 | Sym->getAddress()}; |
| 149 | SectionContainsContent = true; |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 152 | } |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 153 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 154 | JITTargetAddress SecAddr = FirstSym->getAddress(); |
| 155 | uint64_t SecSize = |
| 156 | (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) - |
| 157 | SecAddr; |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 158 | |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 159 | if (SectionContainsZeroFill && SectionContainsContent) |
| 160 | return make_error<StringError>("Mixed zero-fill and content sections not " |
| 161 | "supported yet", |
| 162 | inconvertibleErrorCode()); |
| 163 | if (SectionContainsZeroFill) |
Lang Hames | 23085ec | 2019-05-12 22:26:33 +0000 | [diff] [blame] | 164 | FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr}; |
| 165 | else |
| 166 | FileInfo.SectionInfos[Sec.getName()] = { |
Lang Hames | 4e920e5 | 2019-10-04 03:55:26 +0000 | [diff] [blame] | 167 | StringRef(FirstSym->getBlock().getContent().data(), SecSize), |
| 168 | SecAddr}; |
Lang Hames | 11c8dfa5 | 2019-04-20 17:10:34 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return Error::success(); |
| 172 | } |
| 173 | |
| 174 | } // end namespace llvm |