1//===--- llvm-jitlink-coff.cpp -- COFF 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// COFF 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
20using namespace llvm;
21using namespace llvm::jitlink;
22
23static bool isCOFFGOTSection(Section &S) { return S.getName() == "$__GOT"; }
24
25static bool isCOFFStubsSection(Section &S) { return S.getName() == "$__STUBS"; }
26
27static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {
28 auto EItr =
29 llvm::find_if(Range: B.edges(), P: [](Edge &E) { return E.isRelocation(); });
30 if (EItr == B.edges().end())
31 return make_error<StringError>(Args: "GOT entry in " + G.getName() + ", \"" +
32 B.getSection().getName() +
33 "\" has no relocations",
34 Args: inconvertibleErrorCode());
35 return *EItr;
36}
37
38static Expected<Symbol &> getCOFFGOTTarget(LinkGraph &G, Block &B) {
39 auto E = getFirstRelocationEdge(G, B);
40 if (!E)
41 return E.takeError();
42 auto &TargetSym = E->getTarget();
43 if (!TargetSym.hasName())
44 return make_error<StringError>(
45 Args: "GOT entry in " + G.getName() + ", \"" +
46 TargetSym.getBlock().getSection().getName() +
47 "\" points to anonymous "
48 "symbol",
49 Args: inconvertibleErrorCode());
50 return TargetSym;
51}
52
53static Expected<Symbol &> getCOFFStubTarget(LinkGraph &G, Block &B) {
54 auto E = getFirstRelocationEdge(G, B);
55 if (!E)
56 return E.takeError();
57 auto &GOTSym = E->getTarget();
58 if (!GOTSym.isDefined() || !isCOFFGOTSection(S&: GOTSym.getBlock().getSection()))
59 return make_error<StringError>(
60 Args: "Stubs entry in " + G.getName() + ", \"" +
61 GOTSym.getBlock().getSection().getName() +
62 "\" does not point to GOT entry",
63 Args: inconvertibleErrorCode());
64 return getCOFFGOTTarget(G, B&: GOTSym.getBlock());
65}
66
67namespace llvm {
68Error registerCOFFGraphInfo(Session &S, LinkGraph &G) {
69 auto FileName = sys::path::filename(path: G.getName());
70 if (S.FileInfos.count(Key: FileName)) {
71 return make_error<StringError>(Args: "When -check is passed, file names must be "
72 "distinct (duplicate: \"" +
73 FileName + "\")",
74 Args: inconvertibleErrorCode());
75 }
76
77 auto &FileInfo = S.FileInfos[FileName];
78 LLVM_DEBUG(
79 { dbgs() << "Registering COFF file info for \"" << FileName << "\"\n"; });
80 for (auto &Sec : G.sections()) {
81 LLVM_DEBUG({
82 dbgs() << " Section \"" << Sec.getName() << "\": "
83 << (Sec.symbols().empty() ? "empty. skipping." : "processing...")
84 << "\n";
85 });
86
87 // Skip empty sections.
88 if (Sec.symbols().empty())
89 continue;
90
91 if (FileInfo.SectionInfos.count(Key: Sec.getName()))
92 return make_error<StringError>(Args: "Encountered duplicate section name \"" +
93 Sec.getName() + "\" in \"" + FileName +
94 "\"",
95 Args: inconvertibleErrorCode());
96
97 bool isGOTSection = isCOFFGOTSection(S&: Sec);
98 bool isStubsSection = isCOFFStubsSection(S&: Sec);
99
100 bool SectionContainsContent = false;
101 bool SectionContainsZeroFill = false;
102
103 auto *FirstSym = *Sec.symbols().begin();
104 auto *LastSym = FirstSym;
105 for (auto *Sym : Sec.symbols()) {
106 if (Sym->getAddress() < FirstSym->getAddress())
107 FirstSym = Sym;
108 if (Sym->getAddress() > LastSym->getAddress())
109 LastSym = Sym;
110
111 if (isGOTSection || isStubsSection) {
112 if (isGOTSection) {
113 // Skip the GOT start symbol
114 if (Sym->getSize() != 0)
115 if (Error E = FileInfo.registerGOTEntry(G, Sym&: *Sym, GetSymbolTarget: getCOFFGOTTarget))
116 return E;
117 } else {
118 if (Error E = FileInfo.registerStubEntry(G, Sym&: *Sym, GetSymbolTarget: getCOFFStubTarget))
119 return E;
120 }
121 SectionContainsContent = true;
122 }
123
124 if (Sym->hasName()) {
125 if (Sym->isSymbolZeroFill()) {
126 S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
127 Sym->getAddress().getValue()};
128 SectionContainsZeroFill = true;
129 } else {
130 S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
131 Sym->getAddress().getValue(),
132 Sym->getTargetFlags()};
133 SectionContainsContent = true;
134 }
135 }
136 }
137
138 auto SecAddr = FirstSym->getAddress();
139 auto SecSize =
140 (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
141 SecAddr;
142
143 if (SectionContainsZeroFill && SectionContainsContent)
144 return make_error<StringError>(Args: "Mixed zero-fill and content sections not "
145 "supported yet",
146 Args: inconvertibleErrorCode());
147
148 if (SectionContainsZeroFill)
149 FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};
150 else
151 FileInfo.SectionInfos[Sec.getName()] = {
152 ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),
153 SecAddr.getValue(), FirstSym->getTargetFlags()};
154 }
155
156 return Error::success();
157}
158
159} // end namespace llvm
160

source code of llvm/tools/llvm-jitlink/llvm-jitlink-coff.cpp