llvm-jitlink-elf.cpp
3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===---- llvm-jitlink-elf.cpp -- ELF parsing support for llvm-jitlink ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// ELF parsing support for llvm-jitlink.
//
//===----------------------------------------------------------------------===//
#include "llvm-jitlink.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Path.h"
#define DEBUG_TYPE "llvm_jitlink"
using namespace llvm;
using namespace llvm::jitlink;
namespace llvm {
Error registerELFGraphInfo(Session &S, LinkGraph &G) {
auto FileName = sys::path::filename(G.getName());
if (S.FileInfos.count(FileName)) {
return make_error<StringError>("When -check is passed, file names must be "
"distinct (duplicate: \"" +
FileName + "\")",
inconvertibleErrorCode());
}
auto &FileInfo = S.FileInfos[FileName];
LLVM_DEBUG({
dbgs() << "Registering ELF file info for \"" << FileName << "\"\n";
});
for (auto &Sec : G.sections()) {
LLVM_DEBUG({
dbgs() << " Section \"" << Sec.getName() << "\": "
<< (llvm::empty(Sec.symbols()) ? "empty. skipping."
: "processing...")
<< "\n";
});
// Skip empty sections.
if (llvm::empty(Sec.symbols()))
continue;
if (FileInfo.SectionInfos.count(Sec.getName()))
return make_error<StringError>("Encountered duplicate section name \"" +
Sec.getName() + "\" in \"" + FileName +
"\"",
inconvertibleErrorCode());
bool SectionContainsContent = false;
bool SectionContainsZeroFill = false;
auto *FirstSym = *Sec.symbols().begin();
auto *LastSym = FirstSym;
for (auto *Sym : Sec.symbols()) {
if (Sym->getAddress() < FirstSym->getAddress())
FirstSym = Sym;
if (Sym->getAddress() > LastSym->getAddress())
LastSym = Sym;
if (Sym->hasName()) {
dbgs() << "Symbol: " << Sym->getName() << "\n";
if (Sym->isSymbolZeroFill()) {
S.SymbolInfos[Sym->getName()] = {Sym->getSize(), Sym->getAddress()};
SectionContainsZeroFill = true;
} else {
S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),
Sym->getAddress()};
SectionContainsContent = true;
}
}
}
JITTargetAddress SecAddr = FirstSym->getAddress();
uint64_t SecSize =
(LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -
SecAddr;
if (SectionContainsZeroFill && SectionContainsContent)
return make_error<StringError>("Mixed zero-fill and content sections not "
"supported yet",
inconvertibleErrorCode());
if (SectionContainsZeroFill)
FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr};
else
FileInfo.SectionInfos[Sec.getName()] = {
StringRef(FirstSym->getBlock().getContent().data(), SecSize),
SecAddr};
}
return Error::success();
}
} // end namespace llvm