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
) {
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());
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());
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());
61 static Expected
<Symbol
&> getMachOStubTarget(LinkGraph
&G
, Block
&B
) {
62 auto E
= getFirstRelocationEdge(G
, B
);
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());
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: \"" +
83 inconvertibleErrorCode());
86 auto &FileInfo
= S
.FileInfos
[FileName
];
88 dbgs() << "Registering MachO file info for \"" << FileName
<< "\"\n";
90 for (auto &Sec
: G
.sections()) {
92 dbgs() << " Section \"" << Sec
.getName() << "\": "
93 << (Sec
.symbols_empty() ? "empty. skipping." : "processing...")
97 // Skip empty sections.
98 if (Sec
.symbols_empty())
101 if (FileInfo
.SectionInfos
.count(Sec
.getName()))
102 return make_error
<StringError
>("Encountered duplicate section name \"" +
103 Sec
.getName() + "\" in \"" + FileName
+
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())
118 if (Sym
->getAddress() > LastSym
->getAddress())
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(),
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(),
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;
147 S
.SymbolInfos
[Sym
->getName()] = {Sym
->getSymbolContent(),
149 SectionContainsContent
= true;
154 JITTargetAddress SecAddr
= FirstSym
->getAddress();
156 (LastSym
->getBlock().getAddress() + LastSym
->getBlock().getSize()) -
159 if (SectionContainsZeroFill
&& SectionContainsContent
)
160 return make_error
<StringError
>("Mixed zero-fill and content sections not "
162 inconvertibleErrorCode());
163 if (SectionContainsZeroFill
)
164 FileInfo
.SectionInfos
[Sec
.getName()] = {SecSize
, SecAddr
};
166 FileInfo
.SectionInfos
[Sec
.getName()] = {
167 StringRef(FirstSym
->getBlock().getContent().data(), SecSize
),
171 return Error::success();
174 } // end namespace llvm