1 //===---- llvm-jitlink-elf.cpp -- ELF 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 // ELF 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 isELFGOTSection(Section
&S
) { return S
.getName() == "$__GOT"; }
25 static bool isELFStubsSection(Section
&S
) { return S
.getName() == "$__STUBS"; }
27 static Expected
<Edge
&> getFirstRelocationEdge(LinkGraph
&G
, Block
&B
) {
28 auto EItr
= std::find_if(B
.edges().begin(), B
.edges().end(),
29 [](Edge
&E
) { return E
.isRelocation(); });
30 if (EItr
== B
.edges().end())
31 return make_error
<StringError
>("GOT entry in " + G
.getName() + ", \"" +
32 B
.getSection().getName() +
33 "\" has no relocations",
34 inconvertibleErrorCode());
38 static Expected
<Symbol
&> getELFGOTTarget(LinkGraph
&G
, Block
&B
) {
39 auto E
= getFirstRelocationEdge(G
, B
);
42 auto &TargetSym
= E
->getTarget();
43 if (!TargetSym
.hasName())
44 return make_error
<StringError
>(
45 "GOT entry in " + G
.getName() + ", \"" +
46 TargetSym
.getBlock().getSection().getName() +
47 "\" points to anonymous "
49 inconvertibleErrorCode());
53 static Expected
<Symbol
&> getELFStubTarget(LinkGraph
&G
, Block
&B
) {
54 auto E
= getFirstRelocationEdge(G
, B
);
57 auto &GOTSym
= E
->getTarget();
58 if (!GOTSym
.isDefined() || !isELFGOTSection(GOTSym
.getBlock().getSection()))
59 return make_error
<StringError
>(
60 "Stubs entry in " + G
.getName() + ", \"" +
61 GOTSym
.getBlock().getSection().getName() +
62 "\" does not point to GOT entry",
63 inconvertibleErrorCode());
64 return getELFGOTTarget(G
, GOTSym
.getBlock());
69 Error
registerELFGraphInfo(Session
&S
, LinkGraph
&G
) {
70 auto FileName
= sys::path::filename(G
.getName());
71 if (S
.FileInfos
.count(FileName
)) {
72 return make_error
<StringError
>("When -check is passed, file names must be "
73 "distinct (duplicate: \"" +
75 inconvertibleErrorCode());
78 auto &FileInfo
= S
.FileInfos
[FileName
];
80 dbgs() << "Registering ELF file info for \"" << FileName
<< "\"\n";
82 for (auto &Sec
: G
.sections()) {
84 dbgs() << " Section \"" << Sec
.getName() << "\": "
85 << (llvm::empty(Sec
.symbols()) ? "empty. skipping."
90 // Skip empty sections.
91 if (llvm::empty(Sec
.symbols()))
94 if (FileInfo
.SectionInfos
.count(Sec
.getName()))
95 return make_error
<StringError
>("Encountered duplicate section name \"" +
96 Sec
.getName() + "\" in \"" + FileName
+
98 inconvertibleErrorCode());
100 bool isGOTSection
= isELFGOTSection(Sec
);
101 bool isStubsSection
= isELFStubsSection(Sec
);
103 bool SectionContainsContent
= false;
104 bool SectionContainsZeroFill
= false;
106 auto *FirstSym
= *Sec
.symbols().begin();
107 auto *LastSym
= FirstSym
;
108 for (auto *Sym
: Sec
.symbols()) {
109 if (Sym
->getAddress() < FirstSym
->getAddress())
111 if (Sym
->getAddress() > LastSym
->getAddress())
115 if (Sym
->isSymbolZeroFill())
116 return make_error
<StringError
>("zero-fill atom in GOT section",
117 inconvertibleErrorCode());
119 // If this is a GOT symbol with size (i.e. not the GOT start symbol)
120 // then add it to the GOT entry info table.
121 if (Sym
->getSize() != 0) {
122 if (auto TS
= getELFGOTTarget(G
, Sym
->getBlock()))
123 FileInfo
.GOTEntryInfos
[TS
->getName()] = {
124 Sym
->getSymbolContent(), Sym
->getAddress().getValue()};
126 return TS
.takeError();
128 SectionContainsContent
= true;
129 } else if (isStubsSection
) {
130 if (Sym
->isSymbolZeroFill())
131 return make_error
<StringError
>("zero-fill atom in Stub section",
132 inconvertibleErrorCode());
134 if (auto TS
= getELFStubTarget(G
, Sym
->getBlock()))
135 FileInfo
.StubInfos
[TS
->getName()] = {Sym
->getSymbolContent(),
136 Sym
->getAddress().getValue()};
138 return TS
.takeError();
139 SectionContainsContent
= true;
142 if (Sym
->hasName()) {
143 if (Sym
->isSymbolZeroFill()) {
144 S
.SymbolInfos
[Sym
->getName()] = {Sym
->getSize(),
145 Sym
->getAddress().getValue()};
146 SectionContainsZeroFill
= true;
148 S
.SymbolInfos
[Sym
->getName()] = {Sym
->getSymbolContent(),
149 Sym
->getAddress().getValue()};
150 SectionContainsContent
= true;
155 auto SecAddr
= FirstSym
->getAddress();
157 (LastSym
->getBlock().getAddress() + LastSym
->getBlock().getSize()) -
160 if (SectionContainsZeroFill
&& SectionContainsContent
)
161 return make_error
<StringError
>("Mixed zero-fill and content sections not "
163 inconvertibleErrorCode());
164 if (SectionContainsZeroFill
)
165 FileInfo
.SectionInfos
[Sec
.getName()] = {SecSize
, SecAddr
.getValue()};
167 FileInfo
.SectionInfos
[Sec
.getName()] = {
168 ArrayRef
<char>(FirstSym
->getBlock().getContent().data(), SecSize
),
172 return Error::success();
175 } // end namespace llvm