| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <gelf.h> |
| 5 |
#include <libelf.h> |
| 6 |
#ifdef WITH_LZMA |
| 7 |
#include <lzma.h> |
| 8 |
#endif |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#include "bitmap.h" |
| 13 |
#include "cleanup.h" |
| 14 |
#include "debug_info.h" |
| 15 |
#include "elf_file.h" |
| 16 |
#include "elf_symtab.h" |
| 17 |
#include "error.h" |
| 18 |
#include "log.h" |
| 19 |
#include "minmax.h" |
| 20 |
#include "serialize.h" |
| 21 |
#include "string_builder.h" |
| 22 |
#include "util.h" |
| 23 |
|
| 24 |
static struct drgn_error *find_elf_file_symtab(struct drgn_elf_file *file, |
| 25 |
uint64_t bias, |
| 26 |
struct drgn_elf_file **file_ret, |
| 27 |
uint64_t *bias_ret, |
| 28 |
Elf_Scn **scn_ret, |
| 29 |
GElf_Word *strtab_idx_ret, |
| 30 |
GElf_Word *num_local_symbols_ret, |
| 31 |
bool *full_symtab_ret, |
| 32 |
Elf_Scn **gnu_debugdata_ret) |
| 33 |
{ |
| 34 |
Elf_Scn *scn = NULL; |
| 35 |
size_t shstrndx; |
| 36 |
if (elf_getshdrstrndx(file->elf, &shstrndx)) |
| 37 |
return drgn_error_libelf(); |
| 38 |
while ((scn = elf_nextscn(file->elf, scn))) { |
| 39 |
GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem); |
| 40 |
if (!shdr) |
| 41 |
return drgn_error_libelf(); |
| 42 |
|
| 43 |
const char *scnname = elf_strptr(file->elf, shstrndx, shdr->sh_name); |
| 44 |
if (scnname && gnu_debugdata_ret && shdr->sh_type == SHT_PROGBITS |
| 45 |
&& strcmp(".gnu_debugdata", scnname) == 0) |
| 46 |
*gnu_debugdata_ret = scn; |
| 47 |
|
| 48 |
if (shdr->sh_type == SHT_SYMTAB |
| 49 |
|| shdr->sh_type == SHT_DYNSYM) { |
| 50 |
*file_ret = file; |
| 51 |
*bias_ret = bias; |
| 52 |
*scn_ret = scn; |
| 53 |
*strtab_idx_ret = shdr->sh_link; |
| 54 |
*num_local_symbols_ret = shdr->sh_info; |
| 55 |
if (shdr->sh_type == SHT_SYMTAB) { |
| 56 |
*full_symtab_ret = true; |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
|
| 64 |
#ifdef WITH_LZMA |
| 65 |
static struct drgn_error * |
| 66 |
drgn_error_lzma(lzma_ret code) |
| 67 |
{ |
| 68 |
switch (code) { |
| 69 |
case LZMA_MEM_ERROR: |
| 70 |
return &drgn_enomem; |
| 71 |
case LZMA_OPTIONS_ERROR: |
| 72 |
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT, |
| 73 |
"lzma: invalid options"); |
| 74 |
case LZMA_FORMAT_ERROR: |
| 75 |
case LZMA_DATA_ERROR: |
| 76 |
case LZMA_BUF_ERROR: |
| 77 |
return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT, |
| 78 |
"lzma: format error (%d)", code); |
| 79 |
default: |
| 80 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 81 |
"lzma: unknown error (%d)", code); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static struct drgn_error * |
| 86 |
load_gnu_debugdata_file(struct drgn_module *module, Elf_Scn *gnu_debugdata_scn, |
| 87 |
struct drgn_elf_file **file_ret) |
| 88 |
{ |
| 89 |
Elf_Data *gnu_debugdata_data; |
| 90 |
struct drgn_error *err; |
| 91 |
err = read_elf_section(gnu_debugdata_scn, &gnu_debugdata_data); |
| 92 |
if (err) |
| 93 |
return err; |
| 94 |
|
| 95 |
_cleanup_(lzma_end) lzma_stream stream = LZMA_STREAM_INIT; |
| 96 |
lzma_ret ret = lzma_stream_decoder(&stream, UINT64_MAX, 0); |
| 97 |
if (ret != LZMA_OK) |
| 98 |
return drgn_error_lzma(ret); |
| 99 |
|
| 100 |
stream.next_in = gnu_debugdata_data->d_buf; |
| 101 |
stream.avail_in = gnu_debugdata_data->d_size; |
| 102 |
|
| 103 |
// Use the input buffer size as the initial capacity. We'll expand it as |
| 104 |
// needed. |
| 105 |
size_t capacity = gnu_debugdata_data->d_size; |
| 106 |
_cleanup_free_ void *data = malloc(capacity); |
| 107 |
if (!data) |
| 108 |
return &drgn_enomem; |
| 109 |
|
| 110 |
stream.next_out = data; |
| 111 |
stream.avail_out = capacity; |
| 112 |
|
| 113 |
size_t bytes_decoded; |
| 114 |
while (1) { |
| 115 |
ret = lzma_code(&stream, LZMA_RUN); |
| 116 |
if (ret != LZMA_OK && ret != LZMA_STREAM_END) |
| 117 |
return drgn_error_lzma(ret); |
| 118 |
|
| 119 |
bytes_decoded = (char *)stream.next_out - (char *)data; |
| 120 |
if (ret == LZMA_STREAM_END) { |
| 121 |
void *tmp = realloc(data, bytes_decoded); |
| 122 |
if (tmp) |
| 123 |
data = tmp; |
| 124 |
break; |
| 125 |
} else if (__builtin_mul_overflow(capacity, 2U, &capacity)) { |
| 126 |
return &drgn_enomem; |
| 127 |
} else { |
| 128 |
void *tmp = realloc(data, capacity); |
| 129 |
if (!tmp) |
| 130 |
return &drgn_enomem; |
| 131 |
data = tmp; |
| 132 |
stream.next_out = (uint8_t *)data + bytes_decoded; |
| 133 |
stream.avail_out = capacity - bytes_decoded; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
STRING_BUILDER(path); |
| 138 |
if (!string_builder_appendf(&path, "%s[.gnu_debugdata]", module->loaded_file->path) |
| 139 |
|| !string_builder_null_terminate(&path)) |
| 140 |
return &drgn_enomem; |
| 141 |
|
| 142 |
Elf *elf = elf_memory(data, bytes_decoded); |
| 143 |
if (!elf) |
| 144 |
return drgn_error_libelf(); |
| 145 |
|
| 146 |
err = drgn_elf_file_create(module, path.str, -1, data, elf, file_ret); |
| 147 |
if (err) |
| 148 |
elf_end(elf); |
| 149 |
else |
| 150 |
data = NULL; |
| 151 |
return err; |
| 152 |
} |
| 153 |
#else |
| 154 |
static struct drgn_error * |
| 155 |
load_gnu_debugdata_file(struct drgn_module *module, Elf_Scn *gnu_debugdata_scn, |
| 156 |
struct drgn_elf_file **ret) |
| 157 |
{ |
| 158 |
drgn_log_info(module->prog, |
| 159 |
"module \"%s\": .gnu_debugdata is available, but drgn was built without liblzma support", |
| 160 |
module->name); |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
static struct drgn_error * |
| 166 |
set_elf_symtab(struct drgn_elf_symbol_table *symtab, struct drgn_elf_file *file, |
| 167 |
uint64_t bias, Elf_Scn *symtab_scn, GElf_Word strtab_idx, |
| 168 |
GElf_Word num_local_symbols) |
| 169 |
{ |
| 170 |
Elf_Scn *strtab_scn = elf_getscn(file->elf, strtab_idx); |
| 171 |
if (!strtab_scn) |
| 172 |
return drgn_error_libelf(); |
| 173 |
|
| 174 |
struct drgn_error *err; |
| 175 |
Elf_Data *data, *strtab_data; |
| 176 |
if ((err = read_elf_section(symtab_scn, &data)) |
| 177 |
|| (err = read_elf_section(strtab_scn, &strtab_data))) |
| 178 |
return err; |
| 179 |
|
| 180 |
truncate_elf_string_data(strtab_data); |
| 181 |
|
| 182 |
Elf_Data *shndx_data = NULL; |
| 183 |
int shndx_idx = elf_scnshndx(symtab_scn); |
| 184 |
if (shndx_idx > 0) { |
| 185 |
Elf_Scn *shndx_scn = elf_getscn(file->elf, shndx_idx); |
| 186 |
if (!shndx_scn) |
| 187 |
return drgn_error_libelf(); |
| 188 |
err = read_elf_section(shndx_scn, &shndx_data); |
| 189 |
if (err) |
| 190 |
return err; |
| 191 |
} |
| 192 |
|
| 193 |
symtab->file = file; |
| 194 |
symtab->bias = bias; |
| 195 |
symtab->data = data->d_buf; |
| 196 |
symtab->num_symbols = |
| 197 |
data->d_size |
| 198 |
/ (drgn_elf_file_is_64_bit(file) |
| 199 |
? sizeof(Elf64_Sym) : sizeof(Elf32_Sym)); |
| 200 |
if (num_local_symbols < 1) |
| 201 |
num_local_symbols = 1; |
| 202 |
if (num_local_symbols > symtab->num_symbols) |
| 203 |
num_local_symbols = symtab->num_symbols; |
| 204 |
symtab->num_local_symbols = num_local_symbols; |
| 205 |
symtab->strtab = strtab_data; |
| 206 |
symtab->shndx = shndx_data; |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
static void |
| 211 |
cleanup_elf_file(struct drgn_elf_file **pfile) |
| 212 |
{ |
| 213 |
if (*pfile) { |
| 214 |
drgn_elf_file_destroy(*pfile); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
static struct drgn_error * |
| 219 |
find_module_elf_symtab(struct drgn_module *module) |
| 220 |
{ |
| 221 |
struct drgn_error *err; |
| 222 |
|
| 223 |
if (!module->elf_symtab_pending_files) |
| 224 |
return NULL; |
| 225 |
|
| 226 |
if (module->have_full_symtab) { |
| 227 |
module->elf_symtab_pending_files = 0; |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
|
| 231 |
// The goal is to have the most complete symbol information, which we |
| 232 |
// can get from the following, in order of preference: |
| 233 |
// 1. A .symtab from the loaded or debug file (i.e. module->full_symtab |
| 234 |
// is true) |
| 235 |
// 2. A .dynsym from the loaded file, as well as the .symtab from an |
| 236 |
// embedded .gnu_debugdata file. (The .gnu_debugdata usually only |
| 237 |
// contains complete function symbols, so we prefer #1 where |
| 238 |
// possible) |
| 239 |
// 3. A .dynsym and no .gnu_debugdata |
| 240 |
|
| 241 |
struct drgn_elf_file *file = NULL; |
| 242 |
uint64_t bias; |
| 243 |
Elf_Scn *symtab_scn; |
| 244 |
GElf_Word strtab_idx, num_local_symbols; |
| 245 |
bool full_symtab = false; |
| 246 |
|
| 247 |
if (module->elf_symtab_pending_files & DRGN_MODULE_FILE_MASK_DEBUG) { |
| 248 |
err = find_elf_file_symtab(module->debug_file, |
| 249 |
module->debug_file_bias, &file, |
| 250 |
&bias, &symtab_scn, &strtab_idx, |
| 251 |
&num_local_symbols, &full_symtab, |
| 252 |
NULL); |
| 253 |
if (err) |
| 254 |
return err; |
| 255 |
} |
| 256 |
|
| 257 |
Elf_Scn *gnu_debugdata_scn = NULL; |
| 258 |
if (!full_symtab && |
| 259 |
(module->elf_symtab_pending_files & DRGN_MODULE_FILE_MASK_LOADED)) { |
| 260 |
err = find_elf_file_symtab(module->loaded_file, |
| 261 |
module->loaded_file_bias, &file, |
| 262 |
&bias, &symtab_scn, &strtab_idx, |
| 263 |
&num_local_symbols, &full_symtab, |
| 264 |
&gnu_debugdata_scn); |
| 265 |
if (err) |
| 266 |
return err; |
| 267 |
} |
| 268 |
|
| 269 |
if (!file && !gnu_debugdata_scn) { |
| 270 |
drgn_log_debug(module->prog, "%s: no ELF symbol table", |
| 271 |
module->name); |
| 272 |
module->elf_symtab_pending_files = 0; |
| 273 |
return NULL; |
| 274 |
} |
| 275 |
|
| 276 |
// If we've found a dynamic symbol table, but we already saw a dynamic |
| 277 |
// table, don't bother replacing it, unless the new file contains |
| 278 |
// .gnu_debugdata (and thus the old one didn't). |
| 279 |
if (module->elf_symtab.num_symbols && !full_symtab && !gnu_debugdata_scn) { |
| 280 |
module->elf_symtab_pending_files = 0; |
| 281 |
return NULL; |
| 282 |
} |
| 283 |
|
| 284 |
if (file) { |
| 285 |
err = set_elf_symtab(&module->elf_symtab, file, bias, symtab_scn, |
| 286 |
strtab_idx, num_local_symbols); |
| 287 |
if (err) |
| 288 |
return err; |
| 289 |
|
| 290 |
module->have_full_symtab = full_symtab; |
| 291 |
drgn_log_debug(module->prog, |
| 292 |
"%s: found ELF %ssymbol table with %zu symbols", |
| 293 |
module->name, full_symtab ? "" : "dynamic ", |
| 294 |
module->elf_symtab.num_symbols); |
| 295 |
} |
| 296 |
|
| 297 |
if (full_symtab && module->gnu_debugdata_symtab.num_symbols) { |
| 298 |
// With a full symbol table (likely from the debug file), there |
| 299 |
// is no need to keep around the gnu_debugdata symbol table. |
| 300 |
// We cannot free the memory associated with it, because we may |
| 301 |
// have returned symbols that refer to the strings in this file. |
| 302 |
// Stop using the symbol table, but delay freeing until the |
| 303 |
// program is freed. |
| 304 |
memset(&module->gnu_debugdata_symtab, 0, |
| 305 |
sizeof(module->gnu_debugdata_symtab)); |
| 306 |
} else if (!full_symtab && gnu_debugdata_scn) { |
| 307 |
// We only search for .gnu_debugdata in the loaded file, not the |
| 308 |
// debug file. Once attached to a module, files can't be |
| 309 |
// detached, so there should be no case where we load |
| 310 |
// .gnu_debugdata twice. Let's assert that precondition here. |
| 311 |
assert(module->gnu_debugdata_file == NULL); |
| 312 |
|
| 313 |
_cleanup_(cleanup_elf_file) struct drgn_elf_file *gnu_debugdata_file = NULL; |
| 314 |
|
| 315 |
err = load_gnu_debugdata_file(module, gnu_debugdata_scn, |
| 316 |
&gnu_debugdata_file); |
| 317 |
if (err) |
| 318 |
return err; |
| 319 |
|
| 320 |
if (gnu_debugdata_file) { |
| 321 |
file = NULL; |
| 322 |
err = find_elf_file_symtab(gnu_debugdata_file, |
| 323 |
module->loaded_file_bias, &file, |
| 324 |
&bias, &symtab_scn, &strtab_idx, |
| 325 |
&num_local_symbols, &full_symtab, |
| 326 |
NULL); |
| 327 |
if (err) |
| 328 |
return err; |
| 329 |
|
| 330 |
if (file) { |
| 331 |
err = set_elf_symtab(&module->gnu_debugdata_symtab, |
| 332 |
file, bias, symtab_scn, |
| 333 |
strtab_idx, num_local_symbols); |
| 334 |
if (err) |
| 335 |
return err; |
| 336 |
|
| 337 |
module->gnu_debugdata_file = no_cleanup_ptr(gnu_debugdata_file); |
| 338 |
drgn_log_debug(module->prog, |
| 339 |
"%s: found ELF .gnu_debugdata symbol table with %zu symbols", |
| 340 |
module->name, module->gnu_debugdata_symtab.num_symbols); |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
module->elf_symtab_pending_files = 0; |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
|
| 349 |
static size_t elf_symbol_shndx(struct drgn_elf_symbol_table *symtab, |
| 350 |
size_t sym_idx, const GElf_Sym *sym) |
| 351 |
{ |
| 352 |
if (sym->st_shndx == SHN_XINDEX |
| 353 |
&& symtab->shndx |
| 354 |
&& sym_idx < symtab->shndx->d_size / sizeof(uint32_t)) { |
| 355 |
uint32_t tmp; |
| 356 |
memcpy(&tmp, |
| 357 |
(const char *)symtab->shndx->d_buf |
| 358 |
+ sym_idx * sizeof(uint32_t), |
| 359 |
sizeof(uint32_t)); |
| 360 |
if (drgn_elf_file_bswap(symtab->file)) |
| 361 |
tmp = bswap_32(tmp); |
| 362 |
return tmp; |
| 363 |
} |
| 364 |
return sym->st_shndx; |
| 365 |
} |
| 366 |
|
| 367 |
static bool elf_symbol_address(struct drgn_elf_symbol_table *symtab, |
| 368 |
size_t sym_idx, const GElf_Sym *sym, uint64_t *ret) |
| 369 |
{ |
| 370 |
uint64_t addr = sym->st_value; |
| 371 |
|
| 372 |
// On 32-bit Arm, the least significant bit of st_value in an STT_FUNC |
| 373 |
// symbol indicates whether it addresses a Thumb instruction. Clear it. |
| 374 |
// |
| 375 |
// P.S. If we need any more architecture-specific hacks, then we should |
| 376 |
// add a callback to drgn_architecture_info. Note that we don't |
| 377 |
// currently support V1 of the 64-bit PowerPC ELF ABI where st_value is |
| 378 |
// the address of a "function descriptor" instead of the function entry |
| 379 |
// point. |
| 380 |
if (symtab->file->platform.arch->arch == DRGN_ARCH_ARM |
| 381 |
&& GELF_ST_TYPE(sym->st_info) == STT_FUNC) |
| 382 |
addr &= ~1; |
| 383 |
|
| 384 |
// If the address is not in the module's address range, then it's |
| 385 |
// probably something special like a Linux per-CPU variable (which isn't |
| 386 |
// actually a variable address but an offset). Don't apply the bias in |
| 387 |
// that case. |
| 388 |
if (drgn_module_contains_address(symtab->file->module, |
| 389 |
addr + symtab->bias)) |
| 390 |
addr += symtab->bias; |
| 391 |
if (symtab->file->is_relocatable) { |
| 392 |
size_t shndx = elf_symbol_shndx(symtab, sym_idx, sym); |
| 393 |
if (shndx != SHN_UNDEF |
| 394 |
&& (shndx < SHN_LORESERVE || shndx > SHN_HIRESERVE)) { |
| 395 |
Elf_Scn *scn = elf_getscn(symtab->file->elf, shndx); |
| 396 |
if (!scn) |
| 397 |
return false; |
| 398 |
// If the section's address is not known, then the |
| 399 |
// symbol is essentially undefined, so ignore it. This |
| 400 |
// filters out things like symbols from the .init* |
| 401 |
// sections in Linux kernel modules which are unloaded |
| 402 |
// after initialization. |
| 403 |
// NB: shndx is bounds-checked by elf_getscn(). |
| 404 |
if (!drgn_bitmap_test_bit(symtab->file->sections_with_address, |
| 405 |
shndx)) |
| 406 |
return false; |
| 407 |
GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, |
| 408 |
&shdr_mem); |
| 409 |
if (!shdr) |
| 410 |
return false; |
| 411 |
addr += shdr->sh_addr; |
| 412 |
} else if (shndx != SHN_ABS) { |
| 413 |
return false; |
| 414 |
} |
| 415 |
} |
| 416 |
*ret = addr; |
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
// When searching for one symbol, if there are multiple matches, we break ties |
| 421 |
// based on the symbol binding. The order of precedence is: |
| 422 |
// GLOBAL = UNIQUE > WEAK > LOCAL = everything else |
| 423 |
static int drgn_symbol_binding_precedence(const struct drgn_symbol *sym) |
| 424 |
{ |
| 425 |
SWITCH_ENUM(sym->binding) { |
| 426 |
case DRGN_SYMBOL_BINDING_GLOBAL: |
| 427 |
case DRGN_SYMBOL_BINDING_UNIQUE: |
| 428 |
return 3; |
| 429 |
case DRGN_SYMBOL_BINDING_WEAK: |
| 430 |
return 2; |
| 431 |
case DRGN_SYMBOL_BINDING_LOCAL: |
| 432 |
case DRGN_SYMBOL_BINDING_UNKNOWN: |
| 433 |
return 1; |
| 434 |
default: |
| 435 |
UNREACHABLE(); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
static int elf_symbol_binding_precedence(const GElf_Sym *sym) |
| 440 |
{ |
| 441 |
switch (GELF_ST_BIND(sym->st_info)) { |
| 442 |
case STB_GLOBAL: |
| 443 |
case STB_GNU_UNIQUE: |
| 444 |
return 3; |
| 445 |
case STB_WEAK: |
| 446 |
return 2; |
| 447 |
default: |
| 448 |
return 1; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
// This assumes that both symbols contain the search address. |
| 453 |
static bool better_addr_match(const GElf_Sym *a, uint64_t a_addr, |
| 454 |
const struct drgn_symbol *b) |
| 455 |
{ |
| 456 |
// Prefer the symbol that starts closer to the search address. |
| 457 |
if (a_addr > b->address) |
| 458 |
return true; |
| 459 |
if (a_addr < b->address) |
| 460 |
return false; |
| 461 |
|
| 462 |
// If the symbols have the same start address, prefer the one that ends |
| 463 |
// closer to the search address. |
| 464 |
if (a->st_size < b->size) |
| 465 |
return true; |
| 466 |
if (a->st_size > b->size) |
| 467 |
return false; |
| 468 |
|
| 469 |
// If the symbols have the same start and end addresses, prefer the one |
| 470 |
// with the higher binding precedence. |
| 471 |
return elf_symbol_binding_precedence(a) |
| 472 |
> drgn_symbol_binding_precedence(b); |
| 473 |
} |
| 474 |
|
| 475 |
// This assumes that both symbols start before the search address and have size |
| 476 |
// 0. |
| 477 |
static bool better_sizeless_addr_match(const GElf_Sym *a, uint64_t a_addr, |
| 478 |
const GElf_Sym *b, uint64_t b_addr) |
| 479 |
{ |
| 480 |
// Prefer the symbol that starts closer to the search address. |
| 481 |
if (a_addr > b_addr) |
| 482 |
return true; |
| 483 |
if (a_addr < b_addr) |
| 484 |
return false; |
| 485 |
|
| 486 |
// If the symbols have the same start address, prefer the one with the |
| 487 |
// higher binding precedence. |
| 488 |
return elf_symbol_binding_precedence(a) |
| 489 |
> elf_symbol_binding_precedence(b); |
| 490 |
} |
| 491 |
|
| 492 |
static bool addr_in_sym_section(struct drgn_elf_symbol_table *symtab, |
| 493 |
size_t sym_idx, const GElf_Sym *sym, |
| 494 |
uint64_t unbiased_addr) |
| 495 |
{ |
| 496 |
size_t shndx = elf_symbol_shndx(symtab, sym_idx, sym); |
| 497 |
if (shndx == SHN_UNDEF |
| 498 |
|| (SHN_LORESERVE <= shndx && shndx <= SHN_HIRESERVE)) |
| 499 |
return false; |
| 500 |
Elf_Scn *scn = elf_getscn(symtab->file->elf, shndx); |
| 501 |
if (!scn) |
| 502 |
return false; |
| 503 |
GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem); |
| 504 |
if (!shdr) |
| 505 |
return false; |
| 506 |
return unbiased_addr >= shdr->sh_addr |
| 507 |
&& (unbiased_addr - shdr->sh_addr < shdr->sh_size); |
| 508 |
} |
| 509 |
|
| 510 |
struct elf_symtab_search_state { |
| 511 |
// Handwritten assembly functions may have a symbol size of 0 even |
| 512 |
// though logically they have a size. The best we can do is assume that |
| 513 |
// such a symbol extends until the next symbol. If we're searching by |
| 514 |
// address and we don't find any symbols containing the address, then we |
| 515 |
// will return a symbol with size 0 that could contain it based on this |
| 516 |
// assumption. |
| 517 |
const char *sizeless_name; |
| 518 |
uint64_t sizeless_addr; |
| 519 |
size_t sizeless_sym_idx; |
| 520 |
struct drgn_elf_symbol_table *sizeless_symtab; |
| 521 |
Elf64_Sym sizeless_sym; |
| 522 |
|
| 523 |
// If we're searching for one symbol, then we may already have a match, |
| 524 |
// but we still need to search for a better match. |
| 525 |
struct drgn_symbol *best_sym; |
| 526 |
|
| 527 |
// The maximum end address of any symbol starting before the given |
| 528 |
// address. Any symbol with size 0 starting before this is either |
| 529 |
// contained within another symbol or is assumed to end before this, so |
| 530 |
// it should be ignored. |
| 531 |
uint64_t max_end_addr; |
| 532 |
}; |
| 533 |
|
| 534 |
static struct drgn_error * |
| 535 |
drgn_elf_symbol_table_search(struct drgn_elf_symbol_table *symtab, const char *name, |
| 536 |
uint64_t addr, enum drgn_find_symbol_flags flags, |
| 537 |
struct elf_symtab_search_state *state, |
| 538 |
struct drgn_symbol_result_builder *builder) |
| 539 |
{ |
| 540 |
const bool is_64_bit = drgn_elf_file_is_64_bit(symtab->file); |
| 541 |
const bool bswap = drgn_elf_file_bswap(symtab->file); |
| 542 |
const size_t sym_size = |
| 543 |
is_64_bit ? sizeof(Elf64_Sym) : sizeof(Elf32_Sym); |
| 544 |
|
| 545 |
// If we already have a match, and we're not searching by address, then |
| 546 |
// we will never prefer a local symbol over that match, so we can skip |
| 547 |
// local symbols. For address searches, we can't skip local addresses, |
| 548 |
// because we prioritize the closest match to the address. |
| 549 |
// |
| 550 |
// Otherwise, skip the undefined symbol at index 0. |
| 551 |
for (size_t i = !(flags & DRGN_FIND_SYMBOL_ADDR) && state->best_sym |
| 552 |
? symtab->num_local_symbols : 1; |
| 553 |
i < symtab->num_symbols; i++) { |
| 554 |
Elf64_Sym elf_sym; |
| 555 |
#define visit_elf_sym_members(visit_scalar_member, visit_raw_member) do { \ |
| 556 |
visit_scalar_member(st_name); \ |
| 557 |
visit_scalar_member(st_info); \ |
| 558 |
visit_scalar_member(st_other); \ |
| 559 |
visit_scalar_member(st_shndx); \ |
| 560 |
visit_scalar_member(st_value); \ |
| 561 |
visit_scalar_member(st_size); \ |
| 562 |
} while (0) |
| 563 |
deserialize_struct64(&elf_sym, Elf32_Sym, visit_elf_sym_members, |
| 564 |
symtab->data + i * sym_size, |
| 565 |
is_64_bit, bswap); |
| 566 |
#undef visit_elf_sym_members |
| 567 |
|
| 568 |
// Ignore undefined symbols. |
| 569 |
if (elf_sym.st_shndx == SHN_UNDEF) |
| 570 |
continue; |
| 571 |
|
| 572 |
// Ignore symbols with an out-of-bounds name. |
| 573 |
if (elf_sym.st_name >= symtab->strtab->d_size) |
| 574 |
continue; |
| 575 |
const char *elf_sym_name = |
| 576 |
(const char *)symtab->strtab->d_buf |
| 577 |
+ elf_sym.st_name; |
| 578 |
|
| 579 |
if ((flags & DRGN_FIND_SYMBOL_NAME) |
| 580 |
&& strcmp(elf_sym_name, name) != 0) |
| 581 |
continue; |
| 582 |
|
| 583 |
if (flags & DRGN_FIND_SYMBOL_ADDR) { |
| 584 |
// Ignore these special symbol types for address |
| 585 |
// searches (before we bother computing the address). |
| 586 |
switch (GELF_ST_TYPE(elf_sym.st_info)) { |
| 587 |
case STT_SECTION: |
| 588 |
case STT_FILE: |
| 589 |
case STT_TLS: |
| 590 |
continue; |
| 591 |
default: |
| 592 |
break; |
| 593 |
} |
| 594 |
} else if (state->best_sym |
| 595 |
// This is a non-address search for one symbol. |
| 596 |
// Prefer the symbol with the higher binding |
| 597 |
// precedence. |
| 598 |
&& elf_symbol_binding_precedence(&elf_sym) |
| 599 |
<= drgn_symbol_binding_precedence(state->best_sym)) { |
| 600 |
continue; |
| 601 |
} |
| 602 |
|
| 603 |
uint64_t elf_sym_addr; |
| 604 |
if (!elf_symbol_address(symtab, i, &elf_sym, &elf_sym_addr)) |
| 605 |
continue; |
| 606 |
|
| 607 |
if (flags & DRGN_FIND_SYMBOL_ADDR) { |
| 608 |
if (elf_sym_addr > addr) |
| 609 |
continue; |
| 610 |
|
| 611 |
state->max_end_addr = max(state->max_end_addr, |
| 612 |
elf_sym_addr + elf_sym.st_size); |
| 613 |
|
| 614 |
if (elf_sym.st_size == 0) { |
| 615 |
if (!state->sizeless_name |
| 616 |
|| better_sizeless_addr_match(&elf_sym, |
| 617 |
elf_sym_addr, |
| 618 |
&state->sizeless_sym, |
| 619 |
state->sizeless_addr)) { |
| 620 |
state->sizeless_name = elf_sym_name; |
| 621 |
state->sizeless_addr = elf_sym_addr; |
| 622 |
state->sizeless_sym_idx = i; |
| 623 |
state->sizeless_sym = elf_sym; |
| 624 |
state->sizeless_symtab = symtab; |
| 625 |
} |
| 626 |
continue; |
| 627 |
} else if (addr - elf_sym_addr >= elf_sym.st_size |
| 628 |
|| (state->best_sym |
| 629 |
&& !better_addr_match(&elf_sym, |
| 630 |
elf_sym_addr, |
| 631 |
state->best_sym))) { |
| 632 |
continue; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
if (!drgn_symbol_result_builder_add_from_elf(builder, |
| 637 |
elf_sym_name, |
| 638 |
elf_sym_addr, |
| 639 |
&elf_sym)) |
| 640 |
return &drgn_enomem; |
| 641 |
|
| 642 |
if (flags & DRGN_FIND_SYMBOL_ONE) { |
| 643 |
state->best_sym = drgn_symbol_result_builder_single(builder); |
| 644 |
if (!(flags & DRGN_FIND_SYMBOL_ADDR)) { |
| 645 |
// If we're not searching by address and we find |
| 646 |
// a matching global symbol, then we don't need |
| 647 |
// to search anymore. |
| 648 |
if (state->best_sym->binding == DRGN_SYMBOL_BINDING_GLOBAL |
| 649 |
|| state->best_sym->binding == DRGN_SYMBOL_BINDING_UNIQUE) |
| 650 |
return &drgn_stop; |
| 651 |
// Otherwise, if we're not searching by address |
| 652 |
// and we find a matching local symbol, then we |
| 653 |
// can skip past the remaining local symbols. |
| 654 |
if (i < symtab->num_local_symbols) |
| 655 |
i = symtab->num_local_symbols - 1; |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
|
| 662 |
struct drgn_error * |
| 663 |
drgn_module_elf_symbols_search(struct drgn_module *module, const char *name, |
| 664 |
uint64_t addr, enum drgn_find_symbol_flags flags, |
| 665 |
struct drgn_symbol_result_builder *builder) |
| 666 |
{ |
| 667 |
struct drgn_error *err; |
| 668 |
|
| 669 |
err = find_module_elf_symtab(module); |
| 670 |
if (err) |
| 671 |
return err; |
| 672 |
|
| 673 |
struct elf_symtab_search_state state = {0}; |
| 674 |
if (flags & DRGN_FIND_SYMBOL_ONE) |
| 675 |
state.best_sym = drgn_symbol_result_builder_single(builder); |
| 676 |
|
| 677 |
if (module->elf_symtab.num_symbols) { |
| 678 |
err = drgn_elf_symbol_table_search(&module->elf_symtab, name, addr, |
| 679 |
flags, &state, builder); |
| 680 |
if (err) |
| 681 |
return err; |
| 682 |
} |
| 683 |
|
| 684 |
if (module->gnu_debugdata_symtab.num_symbols) { |
| 685 |
err = drgn_elf_symbol_table_search(&module->gnu_debugdata_symtab, name, |
| 686 |
addr, flags, &state, builder); |
| 687 |
if (err) |
| 688 |
return err; |
| 689 |
} |
| 690 |
|
| 691 |
if (state.sizeless_name |
| 692 |
&& drgn_symbol_result_builder_count(builder) == 0 |
| 693 |
&& state.sizeless_addr >= state.max_end_addr |
| 694 |
&& addr_in_sym_section(state.sizeless_symtab, state.sizeless_sym_idx, |
| 695 |
&state.sizeless_sym, addr - state.sizeless_symtab->bias) |
| 696 |
&& !drgn_symbol_result_builder_add_from_elf(builder, state.sizeless_name, |
| 697 |
state.sizeless_addr, |
| 698 |
&state.sizeless_sym)) |
| 699 |
return &drgn_enomem; |
| 700 |
|
| 701 |
return NULL; |
| 702 |
} |