| 1 |
//===- ElfCXX.cpp - C++ Wrapper for libelf ----------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// This project is licensed 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 |
/// \file |
| 10 |
/// A C++ wrapper for libelf. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/* Authors: Giuliano Belinassi, Marcos Paulo de Souza */ |
| 15 |
|
| 16 |
#include "ElfCXX.hh" |
| 17 |
#include "NonLLVMMisc.hh" |
| 18 |
#include <fcntl.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include <assert.h> |
| 21 |
#include <stdexcept> |
| 22 |
#include <iostream> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <zlib.h> |
| 26 |
#include <zstd.h> |
| 27 |
|
| 28 |
const char *ElfSymbol::Get_Name(void) |
| 29 |
{ |
| 30 |
struct Elf *elf = ElfObj.Get_Wrapped_Object(); |
| 31 |
return elf_strptr(elf, Shlink, Symbol.st_name); |
| 32 |
} |
| 33 |
|
| 34 |
const char *ElfSymbol::Type_As_String(unsigned info) |
| 35 |
{ |
| 36 |
/** Table with all possible valuies of info. */ |
| 37 |
static const char *const tbl[16] = { |
| 38 |
"NOTYPE", // No known type. |
| 39 |
"OBJECT", // Is an object. |
| 40 |
"FUNC", // Is a function. |
| 41 |
"SECTION", // Is a section. |
| 42 |
"FILE", // Is a file. |
| 43 |
"COMMON", // ?? |
| 44 |
"TLS", // Thread Locas Storage variable. |
| 45 |
"NUM", // Constant number; |
| 46 |
nullptr, |
| 47 |
nullptr, |
| 48 |
"IFUNC", // Store a reference to a function |
| 49 |
nullptr, |
| 50 |
"HIOS", // ?? |
| 51 |
"LOPROC", |
| 52 |
nullptr, // ?? |
| 53 |
"HIPROC", |
| 54 |
}; |
| 55 |
|
| 56 |
return tbl[info]; |
| 57 |
} |
| 58 |
|
| 59 |
const char *ElfSymbol::Bind_As_String(unsigned link) |
| 60 |
{ |
| 61 |
/** Table with all possible valuies of bind. */ |
| 62 |
static const char *const tbl[14] = { |
| 63 |
"LOCL", // Available in the ELF, but not linkable. |
| 64 |
"GLOB", // Available in ELF and is linkable. |
| 65 |
"WEAK", // Not available in ELF, will be provided by a library. |
| 66 |
"NUM", // ?? |
| 67 |
nullptr, |
| 68 |
nullptr, |
| 69 |
nullptr, |
| 70 |
nullptr, |
| 71 |
nullptr, |
| 72 |
nullptr, |
| 73 |
"LOOS", // ?? |
| 74 |
"HIOS", // ?? |
| 75 |
"LOPROC", // ?? |
| 76 |
"HIPROC", // ?? |
| 77 |
}; |
| 78 |
|
| 79 |
return tbl[link]; |
| 80 |
} |
| 81 |
|
| 82 |
/* Build the ELF object from the given path. */ |
| 83 |
ElfObject::ElfObject(const char *path) |
| 84 |
: Parser(path), |
| 85 |
ElfObj(nullptr), |
| 86 |
DecompressedObj(nullptr), |
| 87 |
ElfFd(-1) |
| 88 |
{ |
| 89 |
/* Libelf quirks. If not present it fails to load current Linux binaries. */ |
| 90 |
elf_version(EV_CURRENT); |
| 91 |
|
| 92 |
/* Open using Unix File Descriptor, as required by libelf. */ |
| 93 |
ElfFd = open(parser_path.c_str(), O_RDONLY); |
| 94 |
if (ElfFd == -1) { |
| 95 |
throw std::runtime_error("ELF file not found: " + parser_path); |
| 96 |
} |
| 97 |
|
| 98 |
enum FileHandling::FileType ft = FileHandling::Get_File_Type(ElfFd); |
| 99 |
|
| 100 |
switch (ft) { |
| 101 |
case FileHandling::FILE_TYPE_ELF: { |
| 102 |
/* Create libelf object and store it in the class variable. */ |
| 103 |
ElfObj = elf_begin(ElfFd, ELF_C_READ, nullptr); |
| 104 |
if (ElfObj == nullptr) { |
| 105 |
close(ElfFd); |
| 106 |
throw std::runtime_error("libelf error on file " + parser_path + ": " |
| 107 |
+ std::string(elf_errmsg(elf_errno()))); |
| 108 |
} |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
/* gzip magic number (zlib) */ |
| 113 |
case FileHandling::FILE_TYPE_GZ: { |
| 114 |
try { |
| 115 |
ElfObj = decompress_gz(); |
| 116 |
close(ElfFd); |
| 117 |
ElfFd = -1; |
| 118 |
} catch (const std::runtime_error &error) { |
| 119 |
ElfObject::~ElfObject(); |
| 120 |
throw error; |
| 121 |
} |
| 122 |
break; |
| 123 |
} |
| 124 |
|
| 125 |
/* zstd magic number */ |
| 126 |
case FileHandling::FILE_TYPE_ZSTD: { |
| 127 |
try { |
| 128 |
ElfObj = decompress_zstd(); |
| 129 |
close(ElfFd); |
| 130 |
ElfFd = -1; |
| 131 |
} catch (const std::runtime_error &error) { |
| 132 |
ElfObject::~ElfObject(); |
| 133 |
throw error; |
| 134 |
} |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
default: |
| 139 |
close(ElfFd); |
| 140 |
throw std::runtime_error("Format not recognized: " + parser_path + "\n"); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* Destroy inner libelf structures. */ |
| 145 |
ElfObject::~ElfObject(void) |
| 146 |
{ |
| 147 |
if (ElfObj) { |
| 148 |
/* ELF object is valid. Destroy it. */ |
| 149 |
elf_end(ElfObj); |
| 150 |
} |
| 151 |
|
| 152 |
if (DecompressedObj) |
| 153 |
free(DecompressedObj); |
| 154 |
|
| 155 |
if (ElfFd != -1) { |
| 156 |
/* File Descriptor is still open. */ |
| 157 |
close(ElfFd); |
| 158 |
ElfFd = -1; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
Elf *ElfObject::decompress_gz(void) |
| 163 |
{ |
| 164 |
const size_t CHUNK = 16384; |
| 165 |
|
| 166 |
unsigned have; |
| 167 |
unsigned char in[CHUNK]; |
| 168 |
unsigned char out[CHUNK]; |
| 169 |
|
| 170 |
unsigned long dest_size = CHUNK; |
| 171 |
DecompressedObj = (unsigned char *)malloc(CHUNK); |
| 172 |
if (!DecompressedObj) |
| 173 |
throw std::runtime_error("zlib malloc failed\n"); |
| 174 |
|
| 175 |
unsigned long dest_current = 0; |
| 176 |
|
| 177 |
z_stream strm; |
| 178 |
memset(&strm, 0, sizeof(strm)); |
| 179 |
|
| 180 |
/* Allocate inflate state. The size is related to how zlib inflates gzip files. */ |
| 181 |
int ret = inflateInit2(&strm, 16+MAX_WBITS); |
| 182 |
if (ret != Z_OK) |
| 183 |
throw std::runtime_error("zlib inflateInit failed\n"); |
| 184 |
|
| 185 |
/* decompress until deflate stream ends or end of file */ |
| 186 |
do { |
| 187 |
strm.avail_in = read(ElfFd, in, CHUNK); |
| 188 |
if (strm.avail_in < 0) |
| 189 |
throw std::runtime_error("zlib read failed: " + std::to_string(strm.avail_in) + "\n"); |
| 190 |
|
| 191 |
if (strm.avail_in == 0) |
| 192 |
break; |
| 193 |
strm.next_in = in; |
| 194 |
|
| 195 |
/* run inflate() on input until output buffer not full */ |
| 196 |
do { |
| 197 |
strm.avail_out = CHUNK; |
| 198 |
strm.next_out = out; |
| 199 |
ret = inflate(&strm, Z_NO_FLUSH); |
| 200 |
|
| 201 |
switch (ret) { |
| 202 |
case Z_NEED_DICT: |
| 203 |
ret = Z_DATA_ERROR; /* and fall through */ |
| 204 |
case Z_DATA_ERROR: |
| 205 |
case Z_MEM_ERROR: |
| 206 |
inflateEnd(&strm); |
| 207 |
throw std::runtime_error("zlib inflate error: " + std::to_string(ret) + "\n"); |
| 208 |
} |
| 209 |
|
| 210 |
have = CHUNK - strm.avail_out; |
| 211 |
|
| 212 |
/* double the buffer when needed */ |
| 213 |
if (have > dest_size - dest_current) { |
| 214 |
dest_size = dest_size * 2; |
| 215 |
DecompressedObj = (unsigned char *)realloc(DecompressedObj, dest_size); |
| 216 |
} |
| 217 |
|
| 218 |
memcpy(DecompressedObj + dest_current, out, have); |
| 219 |
dest_current += have; |
| 220 |
} while (strm.avail_out == 0); |
| 221 |
|
| 222 |
/* done when inflate() says it's done */ |
| 223 |
} while (ret != Z_STREAM_END); |
| 224 |
|
| 225 |
inflateEnd(&strm); |
| 226 |
|
| 227 |
if (ret != Z_STREAM_END) |
| 228 |
throw std::runtime_error("zlib inflateEnd error: " + std::to_string(ret) + "\n"); |
| 229 |
|
| 230 |
Elf *elf = elf_memory((char *)DecompressedObj, ret); |
| 231 |
if (elf == nullptr) |
| 232 |
throw std::runtime_error("libelf elf_memory error: " + std::string(elf_errmsg(elf_errno()))); |
| 233 |
|
| 234 |
return elf; |
| 235 |
} |
| 236 |
|
| 237 |
Elf *ElfObject::decompress_zstd() |
| 238 |
{ |
| 239 |
size_t buffInSize = ZSTD_DStreamInSize(); |
| 240 |
unsigned char buffIn[buffInSize]; |
| 241 |
|
| 242 |
size_t buffOutSize = ZSTD_DStreamOutSize(); |
| 243 |
unsigned char buffOut[buffOutSize]; |
| 244 |
|
| 245 |
unsigned long dest_size = buffOutSize; |
| 246 |
DecompressedObj = (unsigned char *)malloc(dest_size); |
| 247 |
if (!DecompressedObj) |
| 248 |
throw std::runtime_error("zstd malloc failed\n"); |
| 249 |
|
| 250 |
unsigned long dest_current = 0; |
| 251 |
|
| 252 |
ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 253 |
if (!dctx) |
| 254 |
throw std::runtime_error("zstd createDCtx failed\n"); |
| 255 |
|
| 256 |
size_t bytes_read; |
| 257 |
while ((bytes_read = read(ElfFd, buffIn, buffInSize)) ) { |
| 258 |
|
| 259 |
if (bytes_read < 0) { |
| 260 |
ZSTD_freeDCtx(dctx); |
| 261 |
throw std::runtime_error("ZSTD read failed: " + std::to_string(bytes_read) + "\n"); |
| 262 |
} |
| 263 |
|
| 264 |
ZSTD_inBuffer input = { buffIn, bytes_read, 0 }; |
| 265 |
|
| 266 |
while (input.pos < input.size) { |
| 267 |
ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; |
| 268 |
|
| 269 |
size_t ret = ZSTD_decompressStream(dctx, &output , &input); |
| 270 |
if (ZSTD_isError(ret)) { |
| 271 |
ZSTD_freeDCtx(dctx); |
| 272 |
throw std::runtime_error("ZSTD_decompressStream failed: " + std::string(ZSTD_getErrorName(ret)) + "\n"); |
| 273 |
} |
| 274 |
|
| 275 |
if (output.size > dest_size - dest_current) { |
| 276 |
dest_size = dest_size * 2; |
| 277 |
DecompressedObj = (unsigned char *)realloc(DecompressedObj, dest_size); |
| 278 |
} |
| 279 |
|
| 280 |
memcpy(DecompressedObj + dest_current, buffOut, output.pos); |
| 281 |
dest_current += output.size; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
ZSTD_freeDCtx(dctx); |
| 286 |
|
| 287 |
Elf *elf = elf_memory((char *)DecompressedObj, dest_size); |
| 288 |
if (elf == nullptr) |
| 289 |
throw std::runtime_error("libelf elf_memory error: " + std::string(elf_errmsg(elf_errno()))); |
| 290 |
|
| 291 |
return elf; |
| 292 |
} |
| 293 |
|
| 294 |
/** Get the next ELF section. ELF is a multisection file and we need to |
| 295 |
iterate if we want to know certain information. */ |
| 296 |
ElfSection ElfSection::Get_Next_Section(void) |
| 297 |
{ |
| 298 |
return ElfSection(ElfObj, elf_nextscn(ElfObj.Get_Wrapped_Object(), Section)); |
| 299 |
} |
| 300 |
|
| 301 |
/** Go to the next ELF section without rebuilding this object. */ |
| 302 |
void ElfSection::To_Next_Section(void) |
| 303 |
{ |
| 304 |
Section = elf_nextscn(ElfObj.Get_Wrapped_Object(), Section); |
| 305 |
gelf_getshdr(Section, &SectionHeader); |
| 306 |
} |
| 307 |
|
| 308 |
/** Get the name of the section. */ |
| 309 |
const char *ElfSection::Get_Name(void) |
| 310 |
{ |
| 311 |
/* TODO: This can be optimized. */ |
| 312 |
size_t shstrndx; |
| 313 |
struct Elf *elf = ElfObj.Get_Wrapped_Object(); |
| 314 |
if (elf_getshdrstrndx(elf, &shstrndx)) { |
| 315 |
return nullptr; |
| 316 |
} |
| 317 |
|
| 318 |
return elf_strptr(elf, shstrndx, SectionHeader.sh_name); |
| 319 |
} |
| 320 |
|
| 321 |
/* Cache the symbols from section. */ |
| 322 |
void ElfSymbolCache::Insert_Symbols_Into_Hash(SymbolTableHash &map, ElfSection §ion) |
| 323 |
{ |
| 324 |
size_t n = section.Get_Num_Symbols(); |
| 325 |
for (size_t i = 0; i < n; i++) { |
| 326 |
ElfSymbol symbol = section.Get_Symbol(i); |
| 327 |
if (symbol.Get_Type() == STT_FILE) { |
| 328 |
/* Skip the file symbols, we don't need them. */ |
| 329 |
continue; |
| 330 |
} |
| 331 |
|
| 332 |
std::string symbol_name(symbol.Get_Name()); |
| 333 |
map[symbol_name] = symbol.Get_Info(); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
ElfSymbolCache::ElfSymbolCache(ElfObject &eo) |
| 338 |
: ElfSymbolCache() |
| 339 |
{ |
| 340 |
Analyze_ELF(eo); |
| 341 |
} |
| 342 |
|
| 343 |
void ElfSymbolCache::Analyze_ELF(ElfObject &eo) |
| 344 |
{ |
| 345 |
/* Look for dynsym and symtab sections. */ |
| 346 |
for (auto it = eo.section_begin(); it != eo.section_end(); ++it) |
| 347 |
{ |
| 348 |
ElfSection §ion = *it; |
| 349 |
switch (section.Get_Section_Type()) { |
| 350 |
case SHT_DYNSYM: |
| 351 |
Insert_Symbols_Into_Hash(DynsymMap, section); |
| 352 |
|
| 353 |
assert(ObjectPath == "" && "Multiple libraries passed as debuginfo?"); |
| 354 |
ObjectPath = eo.Get_Path(); |
| 355 |
break; |
| 356 |
|
| 357 |
case SHT_SYMTAB: |
| 358 |
Insert_Symbols_Into_Hash(SymtabMap, section); |
| 359 |
|
| 360 |
/* We can also have symtab in the .so library. */ |
| 361 |
DebuginfoPath = eo.Get_Path(); |
| 362 |
break; |
| 363 |
|
| 364 |
case SHT_PROGBITS: |
| 365 |
/* |
| 366 |
* Search for the module name, if the ELF object is a kernel module. The |
| 367 |
* module name starts at offset 24 for 64bit kernels. |
| 368 |
*/ |
| 369 |
if (!strncmp(section.Get_Name(), ".gnu.linkonce.this_module", 25)) |
| 370 |
Mod = std::string((char *)section.Get_Data()->d_buf + 24); |
| 371 |
|
| 372 |
break; |
| 373 |
default: |
| 374 |
break; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
/** Get symbol if available in either symtab. Returns in which symtab this |
| 380 |
symbol was found. */ |
| 381 |
std::pair<unsigned char, ElfSymtabType> |
| 382 |
ElfSymbolCache::Get_Symbol_Info(const std::string &sym) |
| 383 |
{ |
| 384 |
unsigned char ret = 0; |
| 385 |
|
| 386 |
/* Try the dynsym first, which means this symbol is most likely public |
| 387 |
visible. */ |
| 388 |
ret = Get_Symbol_Info_Dynsym(sym); |
| 389 |
if (ret != 0) { |
| 390 |
return std::make_pair(ret, ElfSymtabType::DYNSYM); |
| 391 |
} |
| 392 |
|
| 393 |
/* Symbol not available on dynsym. Try symtab. */ |
| 394 |
ret = Get_Symbol_Info_Symtab(sym); |
| 395 |
if (ret != 0) { |
| 396 |
return std::make_pair(ret, ElfSymtabType::SYMTAB); |
| 397 |
} |
| 398 |
|
| 399 |
/* If symbol is not here there is nothing I can do. */ |
| 400 |
return std::make_pair(0, ElfSymtabType::TAB_NONE); |
| 401 |
} |
| 402 |
|
| 403 |
std::vector<std::string> ElfSymbolCache::Get_All_Symbols(void) |
| 404 |
{ |
| 405 |
std::vector<std::string> vec; |
| 406 |
|
| 407 |
for (auto const &i : DynsymMap) { |
| 408 |
vec.push_back(i.first); |
| 409 |
} |
| 410 |
|
| 411 |
for (auto const &i : SymtabMap) { |
| 412 |
vec.push_back(i.first); |
| 413 |
} |
| 414 |
|
| 415 |
return vec; |
| 416 |
} |
| 417 |
|
| 418 |
void Print_Section(ElfSection §ion) |
| 419 |
{ |
| 420 |
size_t n = section.Get_Num_Symbols(); |
| 421 |
|
| 422 |
for (unsigned i = 0; i < n; i++) { |
| 423 |
ElfSymbol symbol = section.Get_Symbol(i); |
| 424 |
std::cout << symbol.Get_Name() << '\n'; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
void ElfSymbolCache::Dump_Cache(void) |
| 429 |
{ |
| 430 |
std::cout << "DynsymMap:\n"; |
| 431 |
for (auto const &i : DynsymMap) { |
| 432 |
std::cout << i.first << " " << ElfSymbol::Type_As_String(ELF64_ST_TYPE(i.second)) |
| 433 |
<< " " << ElfSymbol::Bind_As_String(ELF64_ST_BIND(i.second)) |
| 434 |
<< '\n'; |
| 435 |
} |
| 436 |
|
| 437 |
std::cout << "SymtabMap:\n"; |
| 438 |
for (auto const &i : SymtabMap) { |
| 439 |
std::cout << i.first << " " << ElfSymbol::Type_As_String(ELF64_ST_TYPE(i.second)) |
| 440 |
<< " " << ElfSymbol::Bind_As_String(ELF64_ST_BIND(i.second)) |
| 441 |
<< '\n'; |
| 442 |
} |
| 443 |
} |