[llvm-exegesis] Add ability to assign perf counters to specific PID
[llvm-project.git] / llvm / tools / llvm-jitlink / llvm-jitlink-elf.cpp
blob5200dbcf903661979d651544b593238a812068eb
1 //===---- llvm-jitlink-elf.cpp -- ELF 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 // 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"
20 using namespace llvm;
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 =
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 &> getELFGOTTarget(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 &> getELFStubTarget(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() || !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());
67 namespace llvm {
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: \"" +
74 FileName + "\")",
75 inconvertibleErrorCode());
78 auto &FileInfo = S.FileInfos[FileName];
79 LLVM_DEBUG({
80 dbgs() << "Registering ELF file info for \"" << FileName << "\"\n";
81 });
82 for (auto &Sec : G.sections()) {
83 LLVM_DEBUG({
84 dbgs() << " Section \"" << Sec.getName() << "\": "
85 << (Sec.symbols().empty() ? "empty. skipping." : "processing...")
86 << "\n";
87 });
89 // Skip empty sections.
90 if (Sec.symbols().empty())
91 continue;
93 if (FileInfo.SectionInfos.count(Sec.getName()))
94 return make_error<StringError>("Encountered duplicate section name \"" +
95 Sec.getName() + "\" in \"" + FileName +
96 "\"",
97 inconvertibleErrorCode());
99 bool isGOTSection = isELFGOTSection(Sec);
100 bool isStubsSection = isELFStubsSection(Sec);
102 bool SectionContainsContent = false;
103 bool SectionContainsZeroFill = false;
105 auto *FirstSym = *Sec.symbols().begin();
106 auto *LastSym = FirstSym;
107 for (auto *Sym : Sec.symbols()) {
108 if (Sym->getAddress() < FirstSym->getAddress())
109 FirstSym = Sym;
110 if (Sym->getAddress() > LastSym->getAddress())
111 LastSym = Sym;
113 if (isGOTSection) {
114 if (Sym->isSymbolZeroFill())
115 return make_error<StringError>("zero-fill atom in GOT section",
116 inconvertibleErrorCode());
118 // If this is a GOT symbol with size (i.e. not the GOT start symbol)
119 // then add it to the GOT entry info table.
120 if (Sym->getSize() != 0) {
121 if (auto TS = getELFGOTTarget(G, Sym->getBlock()))
122 FileInfo.GOTEntryInfos[TS->getName()] = {
123 Sym->getSymbolContent(), Sym->getAddress().getValue()};
124 else
125 return TS.takeError();
127 SectionContainsContent = true;
128 } else if (isStubsSection) {
129 if (Sym->isSymbolZeroFill())
130 return make_error<StringError>("zero-fill atom in Stub section",
131 inconvertibleErrorCode());
133 if (auto TS = getELFStubTarget(G, Sym->getBlock()))
134 FileInfo.StubInfos[TS->getName()] = {Sym->getSymbolContent(),
135 Sym->getAddress().getValue()};
136 else
137 return TS.takeError();
138 SectionContainsContent = true;
141 if (Sym->hasName()) {
142 if (Sym->isSymbolZeroFill()) {
143 S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
144 Sym->getAddress().getValue()};
145 SectionContainsZeroFill = true;
146 } else {
147 S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
148 Sym->getAddress().getValue()};
149 SectionContainsContent = true;
154 // Add symbol info for absolute symbols.
155 for (auto *Sym : G.absolute_symbols())
156 S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
157 Sym->getAddress().getValue()};
159 auto SecAddr = FirstSym->getAddress();
160 auto SecSize =
161 (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
162 SecAddr;
164 if (SectionContainsZeroFill && SectionContainsContent)
165 return make_error<StringError>("Mixed zero-fill and content sections not "
166 "supported yet",
167 inconvertibleErrorCode());
168 if (SectionContainsZeroFill)
169 FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};
170 else
171 FileInfo.SectionInfos[Sec.getName()] = {
172 ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),
173 SecAddr.getValue()};
176 return Error::success();
179 } // end namespace llvm