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(Range: B.edges(), P: [](Edge &E) { return E.isRelocation(); }); |
32 | if (EItr == B.edges().end()) |
33 | return make_error<StringError>(Args: "GOT entry in " + G.getName() + ", \"" + |
34 | B.getSection().getName() + |
35 | "\" has no relocations" , |
36 | Args: 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 | Args: "GOT entry in " + G.getName() + ", \"" + |
48 | TargetSym.getBlock().getSection().getName() + |
49 | "\" points to anonymous " |
50 | "symbol" , |
51 | Args: 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(S&: GOTSym.getBlock().getSection())) |
61 | return make_error<StringError>( |
62 | Args: "Stubs entry in " + G.getName() + ", \"" + |
63 | GOTSym.getBlock().getSection().getName() + |
64 | "\" does not point to GOT entry" , |
65 | Args: inconvertibleErrorCode()); |
66 | return getMachOGOTTarget(G, B&: GOTSym.getBlock()); |
67 | } |
68 | |
69 | namespace llvm { |
70 | |
71 | Error registerMachOGraphInfo(Session &S, LinkGraph &G) { |
72 | auto FileName = sys::path::filename(path: G.getName()); |
73 | if (S.FileInfos.count(Key: FileName)) { |
74 | return make_error<StringError>(Args: "When -check is passed, file names must be " |
75 | "distinct (duplicate: \"" + |
76 | FileName + "\")" , |
77 | Args: inconvertibleErrorCode()); |
78 | } |
79 | |
80 | auto &FileInfo = S.FileInfos[FileName]; |
81 | LLVM_DEBUG({ |
82 | dbgs() << "Registering MachO file info for \"" << FileName << "\"\n" ; |
83 | }); |
84 | for (auto &Sec : G.sections()) { |
85 | LLVM_DEBUG({ |
86 | dbgs() << " Section \"" << Sec.getName() << "\": " |
87 | << (Sec.symbols().empty() ? "empty. skipping." : "processing..." ) |
88 | << "\n" ; |
89 | }); |
90 | |
91 | // Skip empty sections. |
92 | if (Sec.symbols().empty()) |
93 | continue; |
94 | |
95 | if (FileInfo.SectionInfos.count(Key: Sec.getName())) |
96 | return make_error<StringError>(Args: "Encountered duplicate section name \"" + |
97 | Sec.getName() + "\" in \"" + FileName + |
98 | "\"" , |
99 | Args: inconvertibleErrorCode()); |
100 | |
101 | bool isGOTSection = isMachOGOTSection(S&: Sec); |
102 | bool isStubsSection = isMachOStubsSection(S&: Sec); |
103 | |
104 | bool SectionContainsContent = false; |
105 | bool SectionContainsZeroFill = false; |
106 | |
107 | auto *FirstSym = *Sec.symbols().begin(); |
108 | auto *LastSym = FirstSym; |
109 | for (auto *Sym : Sec.symbols()) { |
110 | if (Sym->getAddress() < FirstSym->getAddress()) |
111 | FirstSym = Sym; |
112 | if (Sym->getAddress() > LastSym->getAddress()) |
113 | LastSym = Sym; |
114 | if (isGOTSection || isStubsSection) { |
115 | Error Err = |
116 | isGOTSection |
117 | ? FileInfo.registerGOTEntry(G, Sym&: *Sym, GetSymbolTarget: getMachOGOTTarget) |
118 | : FileInfo.registerStubEntry(G, Sym&: *Sym, GetSymbolTarget: getMachOStubTarget); |
119 | if (Err) |
120 | return Err; |
121 | SectionContainsContent = true; |
122 | } else if (Sym->hasName()) { |
123 | if (Sym->isSymbolZeroFill()) { |
124 | S.SymbolInfos[Sym->getName()] = {Sym->getSize(), |
125 | Sym->getAddress().getValue()}; |
126 | SectionContainsZeroFill = true; |
127 | } else { |
128 | S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(), |
129 | Sym->getAddress().getValue(), |
130 | Sym->getTargetFlags()}; |
131 | SectionContainsContent = true; |
132 | } |
133 | } |
134 | } |
135 | |
136 | auto SecAddr = FirstSym->getAddress(); |
137 | auto SecSize = |
138 | (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) - |
139 | SecAddr; |
140 | |
141 | if (SectionContainsZeroFill && SectionContainsContent) |
142 | return make_error<StringError>(Args: "Mixed zero-fill and content sections not " |
143 | "supported yet" , |
144 | Args: inconvertibleErrorCode()); |
145 | if (SectionContainsZeroFill) |
146 | FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()}; |
147 | else |
148 | FileInfo.SectionInfos[Sec.getName()] = { |
149 | ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize), |
150 | SecAddr.getValue(), FirstSym->getTargetFlags()}; |
151 | } |
152 | |
153 | return Error::success(); |
154 | } |
155 | |
156 | } // end namespace llvm |
157 | |