1 //===-- llvm-jitlink-macho.cpp -- MachO parsing support for llvm-jitlink --===//
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
7 //===----------------------------------------------------------------------===//
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"
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
) {
31 llvm::find_if(B
.edges(), [](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());
40 static Expected
<Symbol
&> getMachOGOTTarget(LinkGraph
&G
, Block
&B
) {
41 auto E
= getFirstRelocationEdge(G
, B
);
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 "
51 inconvertibleErrorCode());
55 static Expected
<Symbol
&> getMachOStubTarget(LinkGraph
&G
, Block
&B
) {
56 auto E
= getFirstRelocationEdge(G
, B
);
59 auto &GOTSym
= E
->getTarget();
60 if (!GOTSym
.isDefined() || !isMachOGOTSection(GOTSym
.getBlock().getSection()))
61 return make_error
<StringError
>(
62 "Stubs entry in " + G
.getName() + ", \"" +
63 GOTSym
.getBlock().getSection().getName() +
64 "\" does not point to GOT entry",
65 inconvertibleErrorCode());
66 return getMachOGOTTarget(G
, GOTSym
.getBlock());
71 Error
registerMachOGraphInfo(Session
&S
, LinkGraph
&G
) {
72 auto FileName
= sys::path::filename(G
.getName());
73 if (S
.FileInfos
.count(FileName
)) {
74 return make_error
<StringError
>("When -check is passed, file names must be "
75 "distinct (duplicate: \"" +
77 inconvertibleErrorCode());
80 auto &FileInfo
= S
.FileInfos
[FileName
];
82 dbgs() << "Registering MachO file info for \"" << FileName
<< "\"\n";
84 for (auto &Sec
: G
.sections()) {
86 dbgs() << " Section \"" << Sec
.getName() << "\": "
87 << (Sec
.symbols().empty() ? "empty. skipping." : "processing...")
91 // Skip empty sections.
92 if (Sec
.symbols().empty())
95 if (FileInfo
.SectionInfos
.count(Sec
.getName()))
96 return make_error
<StringError
>("Encountered duplicate section name \"" +
97 Sec
.getName() + "\" in \"" + FileName
+
99 inconvertibleErrorCode());
101 bool isGOTSection
= isMachOGOTSection(Sec
);
102 bool isStubsSection
= isMachOStubsSection(Sec
);
104 bool SectionContainsContent
= false;
105 bool SectionContainsZeroFill
= false;
107 auto *FirstSym
= *Sec
.symbols().begin();
108 auto *LastSym
= FirstSym
;
109 for (auto *Sym
: Sec
.symbols()) {
110 if (Sym
->getAddress() < FirstSym
->getAddress())
112 if (Sym
->getAddress() > LastSym
->getAddress())
115 if (Sym
->isSymbolZeroFill())
116 return make_error
<StringError
>("zero-fill atom in GOT section",
117 inconvertibleErrorCode());
119 if (auto TS
= getMachOGOTTarget(G
, Sym
->getBlock()))
120 FileInfo
.GOTEntryInfos
[TS
->getName()] = {
121 Sym
->getSymbolContent(), Sym
->getAddress().getValue()};
123 return TS
.takeError();
124 SectionContainsContent
= true;
125 } else if (isStubsSection
) {
126 if (Sym
->isSymbolZeroFill())
127 return make_error
<StringError
>("zero-fill atom in Stub section",
128 inconvertibleErrorCode());
130 if (auto TS
= getMachOStubTarget(G
, Sym
->getBlock()))
131 FileInfo
.StubInfos
[TS
->getName()] = {Sym
->getSymbolContent(),
132 Sym
->getAddress().getValue()};
134 return TS
.takeError();
135 SectionContainsContent
= true;
136 } else if (Sym
->hasName()) {
137 if (Sym
->isSymbolZeroFill()) {
138 S
.SymbolInfos
[Sym
->getName()] = {Sym
->getSize(),
139 Sym
->getAddress().getValue()};
140 SectionContainsZeroFill
= true;
142 S
.SymbolInfos
[Sym
->getName()] = {Sym
->getSymbolContent(),
143 Sym
->getAddress().getValue()};
144 SectionContainsContent
= true;
149 auto SecAddr
= FirstSym
->getAddress();
151 (LastSym
->getBlock().getAddress() + LastSym
->getBlock().getSize()) -
154 if (SectionContainsZeroFill
&& SectionContainsContent
)
155 return make_error
<StringError
>("Mixed zero-fill and content sections not "
157 inconvertibleErrorCode());
158 if (SectionContainsZeroFill
)
159 FileInfo
.SectionInfos
[Sec
.getName()] = {SecSize
, SecAddr
.getValue()};
161 FileInfo
.SectionInfos
[Sec
.getName()] = {
162 ArrayRef
<char>(FirstSym
->getBlock().getContent().data(), SecSize
),
166 return Error::success();
169 } // end namespace llvm