blob: 428e94bc3ffa075cba9f4d19548d50817334e244 [file] [log] [blame]
Lang Hames11c8dfa52019-04-20 17:10:34 +00001//===-- 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 Hames23085ec2019-05-12 22:26:33 +00009// MachO parsing support for llvm-jitlink.
Lang Hames11c8dfa52019-04-20 17:10:34 +000010//
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
20using namespace llvm;
21using namespace llvm::jitlink;
22
23static bool isMachOGOTSection(Section &S) { return S.getName() == "$__GOT"; }
24
25static bool isMachOStubsSection(Section &S) {
26 return S.getName() == "$__STUBS";
27}
28
Lang Hames4e920e52019-10-04 03:55:26 +000029static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {
30 auto EItr = std::find_if(B.edges().begin(), B.edges().end(),
Lang Hames11c8dfa52019-04-20 17:10:34 +000031 [](Edge &E) { return E.isRelocation(); });
Lang Hames4e920e52019-10-04 03:55:26 +000032 if (EItr == B.edges().end())
Lang Hames11c8dfa52019-04-20 17:10:34 +000033 return make_error<StringError>("GOT entry in " + G.getName() + ", \"" +
Lang Hames4e920e52019-10-04 03:55:26 +000034 B.getSection().getName() +
Lang Hames11c8dfa52019-04-20 17:10:34 +000035 "\" has no relocations",
36 inconvertibleErrorCode());
37 return *EItr;
38}
39
Lang Hames4e920e52019-10-04 03:55:26 +000040static Expected<Symbol &> getMachOGOTTarget(LinkGraph &G, Block &B) {
41 auto E = getFirstRelocationEdge(G, B);
Lang Hames11c8dfa52019-04-20 17:10:34 +000042 if (!E)
43 return E.takeError();
Lang Hames4e920e52019-10-04 03:55:26 +000044 auto &TargetSym = E->getTarget();
45 if (!TargetSym.hasName())
Lang Hames11c8dfa52019-04-20 17:10:34 +000046 return make_error<StringError>(
Lang Hames4e920e52019-10-04 03:55:26 +000047 "GOT entry in " + G.getName() + ", \"" +
48 TargetSym.getBlock().getSection().getName() +
49 "\" points to anonymous "
50 "symbol",
Lang Hames11c8dfa52019-04-20 17:10:34 +000051 inconvertibleErrorCode());
Lang Hames4e920e52019-10-04 03:55:26 +000052 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 Hames11c8dfa52019-04-20 17:10:34 +000059}
60
Lang Hames4e920e52019-10-04 03:55:26 +000061static Expected<Symbol &> getMachOStubTarget(LinkGraph &G, Block &B) {
62 auto E = getFirstRelocationEdge(G, B);
Lang Hames11c8dfa52019-04-20 17:10:34 +000063 if (!E)
64 return E.takeError();
Lang Hames4e920e52019-10-04 03:55:26 +000065 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 Hames11c8dfa52019-04-20 17:10:34 +000073}
74
75namespace llvm {
76
Lang Hames4e920e52019-10-04 03:55:26 +000077Error registerMachOStubsAndGOT(Session &S, LinkGraph &G) {
Lang Hames11c8dfa52019-04-20 17:10:34 +000078 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 Hames8c4f0482019-12-05 19:57:51 -080093 << (llvm::empty(Sec.symbols()) ? "empty. skipping." : "processing...")
Lang Hames11c8dfa52019-04-20 17:10:34 +000094 << "\n";
95 });
96
97 // Skip empty sections.
Lang Hames8c4f0482019-12-05 19:57:51 -080098 if (llvm::empty(Sec.symbols()))
Lang Hames11c8dfa52019-04-20 17:10:34 +000099 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 Hames4e920e52019-10-04 03:55:26 +0000110 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 Hames11c8dfa52019-04-20 17:10:34 +0000120 if (isGOTSection) {
Lang Hames4e920e52019-10-04 03:55:26 +0000121 if (Sym->isSymbolZeroFill())
122 return make_error<StringError>("zero-fill atom in GOT section",
Lang Hames23085ec2019-05-12 22:26:33 +0000123 inconvertibleErrorCode());
124
Lang Hames4e920e52019-10-04 03:55:26 +0000125 if (auto TS = getMachOGOTTarget(G, Sym->getBlock()))
126 FileInfo.GOTEntryInfos[TS->getName()] = {Sym->getSymbolContent(),
127 Sym->getAddress()};
Lang Hames11c8dfa52019-04-20 17:10:34 +0000128 else
Lang Hames4e920e52019-10-04 03:55:26 +0000129 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 Hames23085ec2019-05-12 22:26:33 +0000150 }
151 }
Lang Hames11c8dfa52019-04-20 17:10:34 +0000152 }
Lang Hames23085ec2019-05-12 22:26:33 +0000153
Lang Hames4e920e52019-10-04 03:55:26 +0000154 JITTargetAddress SecAddr = FirstSym->getAddress();
155 uint64_t SecSize =
156 (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
157 SecAddr;
Lang Hames23085ec2019-05-12 22:26:33 +0000158
Lang Hames4e920e52019-10-04 03:55:26 +0000159 if (SectionContainsZeroFill && SectionContainsContent)
160 return make_error<StringError>("Mixed zero-fill and content sections not "
161 "supported yet",
162 inconvertibleErrorCode());
163 if (SectionContainsZeroFill)
Lang Hames23085ec2019-05-12 22:26:33 +0000164 FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr};
165 else
166 FileInfo.SectionInfos[Sec.getName()] = {
Lang Hames4e920e52019-10-04 03:55:26 +0000167 StringRef(FirstSym->getBlock().getContent().data(), SecSize),
168 SecAddr};
Lang Hames11c8dfa52019-04-20 17:10:34 +0000169 }
170
171 return Error::success();
172}
173
174} // end namespace llvm