[llvm-exegesis] Add ability to assign perf counters to specific PID
[llvm-project.git] / llvm / tools / llvm-jitlink / llvm-jitlink-coff.cpp
blob415aee76b1863450211c809d792f29e397e4ded8
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm-jitlink.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/Path.h"
18 #define DEBUG_TYPE "llvm_jitlink"
20 using namespace llvm;
21 using namespace llvm::jitlink;
23 static bool isCOFFGOTSection(Section &S) { return S.getName() == "$__GOT"; }
25 static bool isCOFFStubsSection(Section &S) { return S.getName() == "$__STUBS"; }
27 static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {
28 auto EItr =
29 llvm::find_if(B.edges(), [](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());
35 return *EItr;
38 static 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 "GOT entry in " + G.getName() + ", \"" +
46 TargetSym.getBlock().getSection().getName() +
47 "\" points to anonymous "
48 "symbol",
49 inconvertibleErrorCode());
50 return TargetSym;
53 static 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(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 getCOFFGOTTarget(G, GOTSym.getBlock());
67 namespace llvm {
68 Error registerCOFFGraphInfo(Session &S, LinkGraph &G) {
69 auto FileName = sys::path::filename(G.getName());
70 if (S.FileInfos.count(FileName)) {
71 return make_error<StringError>("When -check is passed, file names must be "
72 "distinct (duplicate: \"" +
73 FileName + "\")",
74 inconvertibleErrorCode());
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 });
87 // Skip empty sections.
88 if (Sec.symbols().empty())
89 continue;
91 if (FileInfo.SectionInfos.count(Sec.getName()))
92 return make_error<StringError>("Encountered duplicate section name \"" +
93 Sec.getName() + "\" in \"" + FileName +
94 "\"",
95 inconvertibleErrorCode());
97 bool isGOTSection = isCOFFGOTSection(Sec);
98 bool isStubsSection = isCOFFStubsSection(Sec);
100 bool SectionContainsContent = false;
101 bool SectionContainsZeroFill = false;
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;
111 if (isGOTSection) {
112 if (Sym->isSymbolZeroFill())
113 return make_error<StringError>("zero-fill atom in GOT section",
114 inconvertibleErrorCode());
116 // If this is a GOT symbol with size (i.e. not the GOT start symbol)
117 // then add it to the GOT entry info table.
118 if (Sym->getSize() != 0) {
119 if (auto TS = getCOFFGOTTarget(G, Sym->getBlock()))
120 FileInfo.GOTEntryInfos[TS->getName()] = {
121 Sym->getSymbolContent(), Sym->getAddress().getValue()};
122 else
123 return TS.takeError();
125 SectionContainsContent = true;
126 } else if (isStubsSection) {
127 if (Sym->isSymbolZeroFill())
128 return make_error<StringError>("zero-fill atom in Stub section",
129 inconvertibleErrorCode());
131 if (auto TS = getCOFFStubTarget(G, Sym->getBlock()))
132 FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(),
133 Sym->getAddress().getValue()};
134 else
135 return TS.takeError();
136 SectionContainsContent = true;
139 if (Sym->hasName()) {
140 if (Sym->isSymbolZeroFill()) {
141 S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
142 Sym->getAddress().getValue()};
143 SectionContainsZeroFill = true;
144 } else {
145 S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
146 Sym->getAddress().getValue()};
147 SectionContainsContent = true;
152 auto SecAddr = FirstSym->getAddress();
153 auto SecSize =
154 (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
155 SecAddr;
157 if (SectionContainsZeroFill && SectionContainsContent)
158 return make_error<StringError>("Mixed zero-fill and content sections not "
159 "supported yet",
160 inconvertibleErrorCode());
162 if (SectionContainsZeroFill)
163 FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};
164 else
165 FileInfo.SectionInfos[Sec.getName()] = {
166 ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),
167 SecAddr.getValue()};
170 return Error::success();
173 } // end namespace llvm