[MIParser] Set RegClassOrRegBank during instruction parsing
[llvm-complete.git] / tools / llvm-jitlink / llvm-jitlink-macho.cpp
blob9488dfe9b75d9a940f7271492efceb54c6c1f102
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm-jitlink.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/Path.h"
18 #define DEBUG_TYPE "llvm-jitlink"
20 using namespace llvm;
21 using namespace llvm::jitlink;
23 static bool isMachOGOTSection(Section &S) { return S.getName() == "$__GOT"; }
25 static bool isMachOStubsSection(Section &S) {
26 return S.getName() == "$__STUBS";
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;
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;
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());
75 namespace llvm {
77 Error registerMachOStubsAndGOT(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());
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 << (Sec.symbols_empty() ? "empty. skipping." : "processing...")
94 << "\n";
95 });
97 // Skip empty sections.
98 if (Sec.symbols_empty())
99 continue;
101 if (FileInfo.SectionInfos.count(Sec.getName()))
102 return make_error<StringError>("Encountered duplicate section name \"" +
103 Sec.getName() + "\" in \"" + FileName +
104 "\"",
105 inconvertibleErrorCode());
107 bool isGOTSection = isMachOGOTSection(Sec);
108 bool isStubsSection = isMachOStubsSection(Sec);
110 bool SectionContainsContent = false;
111 bool SectionContainsZeroFill = false;
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;
120 if (isGOTSection) {
121 if (Sym->isSymbolZeroFill())
122 return make_error<StringError>("zero-fill atom in GOT section",
123 inconvertibleErrorCode());
125 if (auto TS = getMachOGOTTarget(G, Sym->getBlock()))
126 FileInfo.GOTEntryInfos[TS->getName()] = {Sym->getSymbolContent(),
127 Sym->getAddress()};
128 else
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());
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;
154 JITTargetAddress SecAddr = FirstSym->getAddress();
155 uint64_t SecSize =
156 (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
157 SecAddr;
159 if (SectionContainsZeroFill && SectionContainsContent)
160 return make_error<StringError>("Mixed zero-fill and content sections not "
161 "supported yet",
162 inconvertibleErrorCode());
163 if (SectionContainsZeroFill)
164 FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr};
165 else
166 FileInfo.SectionInfos[Sec.getName()] = {
167 StringRef(FirstSym->getBlock().getContent().data(), SecSize),
168 SecAddr};
171 return Error::success();
174 } // end namespace llvm