forked from google/LLpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf_symbol.cc
279 lines (240 loc) · 6.48 KB
/
elf_symbol.cc
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Yonghyun Hwang <[email protected]>
*/
#include "elf_symbol.h"
#include <fcntl.h>
#include <gelf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits>
#include <string_view>
#include <system_error>
#include "elf_error.h"
#include "llvm/ADT/Twine.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace
{
StringRef kKLPLocalSym("klp.local.sym");
StringRef kLLpatchSym("__llpatch_symbol_");
void throw_gelf_error()
{
throw std::error_code{ static_cast<ElfErrorCode>(elf_errno()) };
}
} // namespace
ElfSymbol::ElfSymbol(Elf *elf) : elf_(elf)
{
Elf_Scn *scn = nullptr;
GElf_Shdr sym_sec_hdr;
while ((scn = elf_nextscn(elf_, scn))) {
gelf_getshdr(scn, &sym_sec_hdr);
if (sym_sec_hdr.sh_type == SHT_SYMTAB)
break;
}
if (!scn) {
throw std::error_code{ ElfErrorCode::NO_SYMTAB };
}
str_sec_idx_ = sym_sec_hdr.sh_link;
symtab_ = elf_getdata(scn, nullptr);
sym_count_ = sym_sec_hdr.sh_size / sym_sec_hdr.sh_entsize;
if (sym_count_ > 0) {
// sym_cursor_ starts with 0 while terminating condition for next
// iteration is sym_cursor_ == sym_count_.
sym_count_--;
}
}
ElfSymbol::Iterator ElfSymbol::begin()
{
return Iterator(this);
}
ElfSymbol::Iterator ElfSymbol::end()
{
return Iterator(nullptr);
}
std::string_view ElfSymbol::Name() const noexcept(false)
{
return Name(sym_cursor_);
}
std::string_view ElfSymbol::Name(size_t cursor) const noexcept(false)
{
GElf_Sym sym;
GetGElfSymbol(&sym, cursor);
return elf_strptr(elf_, str_sec_idx_, sym.st_name);
}
void ElfSymbol::Rename(uint32_t name_offset) noexcept(false)
{
GElf_Sym sym;
GetGElfSymbol(&sym);
sym.st_name = name_offset;
SetGElfSymbol(&sym);
}
ElfSymbol::SymbolType ElfSymbol::Type() noexcept(false)
{
return Type(sym_cursor_);
}
ElfSymbol::SymbolType ElfSymbol::Type(size_t cursor) noexcept(false)
{
GElf_Sym sym;
GetGElfSymbol(&sym, cursor);
return static_cast<ElfSymbol::SymbolType>(GELF_ST_TYPE(sym.st_info));
}
size_t ElfSymbol::GetStringSectionIndex()
{
return str_sec_idx_;
}
bool ElfSymbol::HasSectionIndex(ElfSymbol::SectionIndex idx)
{
return HasSectionIndex(idx, sym_cursor_);
}
bool ElfSymbol::HasSectionIndex(ElfSymbol::SectionIndex idx, size_t cursor)
{
GElf_Sym sym;
GetGElfSymbol(&sym, cursor);
return sym.st_shndx == static_cast<uint16_t>(idx);
}
void ElfSymbol::SetSectionIndex(ElfSymbol::SectionIndex idx)
{
SetSectionIndex(idx, sym_cursor_);
}
void ElfSymbol::SetSectionIndex(ElfSymbol::SectionIndex idx, size_t cursor)
{
GElf_Sym sym;
GetGElfSymbol(&sym, cursor);
sym.st_shndx = static_cast<uint16_t>(idx);
SetGElfSymbol(&sym, cursor);
}
void ElfSymbol::GetGElfSymbol(GElf_Sym *sym) const noexcept(false)
{
GetGElfSymbol(sym, sym_cursor_);
}
void ElfSymbol::GetGElfSymbol(GElf_Sym *sym, size_t cursor) const
noexcept(false)
{
if (cursor == std::numeric_limits<size_t>::max()) {
errs() << "sym cursor, " << cursor << ", is invalid\n";
throw std::error_code{ ElfErrorCode::INVALID_ELF_SYMBOL };
}
if (gelf_getsym(symtab_, cursor, sym) == nullptr) {
throw_gelf_error();
}
}
void ElfSymbol::SetGElfSymbol(GElf_Sym *sym) noexcept(false)
{
SetGElfSymbol(sym, sym_cursor_);
}
void ElfSymbol::SetGElfSymbol(GElf_Sym *sym, size_t cursor) noexcept(false)
{
if (cursor == std::numeric_limits<size_t>::max()) {
errs() << "sym cursor, " << cursor << ", is invalid\n";
throw std::error_code{ ElfErrorCode::INVALID_ELF_SYMBOL };
}
gelf_update_sym(symtab_, cursor, sym);
}
bool ElfSymbol::IsKLPLocalSymbol() const noexcept(false)
{
return StringRef(Name()).startswith(Twine(kKLPLocalSym, ":").str());
}
bool ElfSymbol::IsLLpatchSymbol() const noexcept(false)
{
return StringRef(Name()).startswith(kLLpatchSym);
}
std::string_view ElfSymbol::GetLLpatchSymbolAlias() const noexcept(false)
{
if (!IsLLpatchSymbol()) {
return "";
}
return Name().substr(kLLpatchSym.size(), std::string::npos);
}
std::string ElfSymbol::CreateKlpLocalSymName(StringRef sym_name)
{
return (Twine(kKLPLocalSym, ":") + Twine(sym_name)).str();
}
namespace
{
std::string RemoveBasePath(StringRef path, StringRef base_path)
{
return path.split(base_path).second.ltrim("./").str();
}
} // namespace
std::string ElfSymbol::CreateLivepatchedFunctionName(const Function &fn,
StringRef base_path)
{
return fn.getName().str() + ":" +
RemoveBasePath(fn.getParent()->getSourceFileName(), base_path);
}
// CreateLivepatchedSymbolName - Create a unique name for the global. The
// format we're using is:
//
// klp.local.sym:orig_name:source_filename
// ^ ^ ^ ^ ^ ^
// |___________| |_______| |_____________|
// [A] [B] [C]
//
// [A]: Prefix.
// [B]: The original symbol name.
// [C]: The source filename, to help with disambiguation.
std::string ElfSymbol::CreateLivepatchedSymbolName(StringRef orig_name,
StringRef filename,
StringRef base_path)
{
return ElfSymbol::CreateKlpLocalSymName(orig_name) + ":" +
RemoveBasePath(filename, base_path);
}
ElfSymbol::Iterator::Iterator(ElfSymbol *symbol) noexcept(false)
: symbol_(symbol)
{
if (symbol_ == nullptr) {
return;
}
// First symbol, sym_cursor = 0, is always a dummy symbol w/ a null
// name. So, skip it.
symbol_->sym_cursor_ = 1;
}
bool ElfSymbol::Iterator::operator==(const Iterator &other) const
{
if (symbol_ != other.symbol_) {
return false;
}
if (symbol_ == nullptr) {
return true;
}
return symbol_->elf_ == other.symbol_->elf_;
}
bool ElfSymbol::Iterator::operator!=(const Iterator &other) const
{
return !operator==(other);
}
ElfSymbol *ElfSymbol::Iterator::operator*() const
{
return symbol_;
}
ElfSymbol::Iterator &ElfSymbol::Iterator::operator++()
{
if (symbol_ == nullptr) {
return *this;
}
if (symbol_->sym_cursor_ >= symbol_->sym_count_) {
symbol_->sym_cursor_ = std::numeric_limits<size_t>::max();
symbol_ = nullptr;
return *this;
}
symbol_->sym_cursor_++;
return *this;
}