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(AtomGraph
&G
, DefinedAtom
&DA
) {
30 auto EItr
= std::find_if(DA
.edges().begin(), DA
.edges().end(),
31 [](Edge
&E
) { return E
.isRelocation(); });
32 if (EItr
== DA
.edges().end())
33 return make_error
<StringError
>("GOT entry in " + G
.getName() + ", \"" +
34 DA
.getSection().getName() +
35 "\" has no relocations",
36 inconvertibleErrorCode());
40 static Expected
<Atom
&> getMachOGOTTarget(AtomGraph
&G
, DefinedAtom
&DA
) {
41 auto E
= getFirstRelocationEdge(G
, DA
);
44 auto &TA
= E
->getTarget();
46 return make_error
<StringError
>("GOT entry in " + G
.getName() + ", \"" +
47 DA
.getSection().getName() +
48 "\" points to anonymous "
50 inconvertibleErrorCode());
51 if (TA
.isDefined() || TA
.isAbsolute())
52 return make_error
<StringError
>(
53 "GOT entry \"" + TA
.getName() + "\" in " + G
.getName() + ", \"" +
54 DA
.getSection().getName() + "\" does not point to an external atom",
55 inconvertibleErrorCode());
59 static Expected
<Atom
&> getMachOStubTarget(AtomGraph
&G
, DefinedAtom
&DA
) {
60 auto E
= getFirstRelocationEdge(G
, DA
);
63 auto &GOTA
= E
->getTarget();
64 if (!GOTA
.isDefined() ||
65 !isMachOGOTSection(static_cast<DefinedAtom
&>(GOTA
).getSection()))
66 return make_error
<StringError
>("Stubs entry in " + G
.getName() + ", \"" +
67 DA
.getSection().getName() +
68 "\" does not point to GOT entry",
69 inconvertibleErrorCode());
70 return getMachOGOTTarget(G
, static_cast<DefinedAtom
&>(GOTA
));
75 Error
registerMachOStubsAndGOT(Session
&S
, AtomGraph
&G
) {
76 auto FileName
= sys::path::filename(G
.getName());
77 if (S
.FileInfos
.count(FileName
)) {
78 return make_error
<StringError
>("When -check is passed, file names must be "
79 "distinct (duplicate: \"" +
81 inconvertibleErrorCode());
84 auto &FileInfo
= S
.FileInfos
[FileName
];
86 dbgs() << "Registering MachO file info for \"" << FileName
<< "\"\n";
88 for (auto &Sec
: G
.sections()) {
90 dbgs() << " Section \"" << Sec
.getName() << "\": "
91 << (Sec
.atoms_empty() ? "empty. skipping." : "processing...")
95 // Skip empty sections.
96 if (Sec
.atoms_empty())
99 if (FileInfo
.SectionInfos
.count(Sec
.getName()))
100 return make_error
<StringError
>("Encountered duplicate section name \"" +
101 Sec
.getName() + "\" in \"" + FileName
+
103 inconvertibleErrorCode());
105 bool isGOTSection
= isMachOGOTSection(Sec
);
106 bool isStubsSection
= isMachOStubsSection(Sec
);
108 auto *FirstAtom
= *Sec
.atoms().begin();
109 auto *LastAtom
= FirstAtom
;
110 for (auto *DA
: Sec
.atoms()) {
111 if (DA
->getAddress() < FirstAtom
->getAddress())
113 if (DA
->getAddress() > LastAtom
->getAddress())
116 if (Sec
.isZeroFill())
117 return make_error
<StringError
>("Content atom in zero-fill section",
118 inconvertibleErrorCode());
120 if (auto TA
= getMachOGOTTarget(G
, *DA
)) {
121 FileInfo
.GOTEntryInfos
[TA
->getName()] = {DA
->getContent(),
124 return TA
.takeError();
125 } else if (isStubsSection
) {
126 if (Sec
.isZeroFill())
127 return make_error
<StringError
>("Content atom in zero-fill section",
128 inconvertibleErrorCode());
130 if (auto TA
= getMachOStubTarget(G
, *DA
))
131 FileInfo
.StubInfos
[TA
->getName()] = {DA
->getContent(),
134 return TA
.takeError();
135 } else if (DA
->hasName() && DA
->isGlobal()) {
136 if (DA
->isZeroFill())
137 S
.SymbolInfos
[DA
->getName()] = {DA
->getSize(), DA
->getAddress()};
139 if (Sec
.isZeroFill())
140 return make_error
<StringError
>("Content atom in zero-fill section",
141 inconvertibleErrorCode());
142 S
.SymbolInfos
[DA
->getName()] = {DA
->getContent(), DA
->getAddress()};
147 JITTargetAddress SecAddr
= FirstAtom
->getAddress();
148 uint64_t SecSize
= (LastAtom
->getAddress() + LastAtom
->getSize()) -
149 FirstAtom
->getAddress();
151 if (Sec
.isZeroFill())
152 FileInfo
.SectionInfos
[Sec
.getName()] = {SecSize
, SecAddr
};
154 FileInfo
.SectionInfos
[Sec
.getName()] = {
155 StringRef(FirstAtom
->getContent().data(), SecSize
), SecAddr
};
158 return Error::success();
161 } // end namespace llvm