| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <byteswap.h> |
| 5 |
#include <elf.h> |
| 6 |
#include <elfutils/libdw.h> |
| 7 |
#include <gelf.h> |
| 8 |
#include <stdarg.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#include "array.h" |
| 15 |
#include "debug_info.h" |
| 16 |
#include "drgn_internal.h" |
| 17 |
#include "elf_file.h" |
| 18 |
#include "error.h" |
| 19 |
#include "minmax.h" |
| 20 |
#include "util.h" |
| 21 |
|
| 22 |
struct drgn_error *read_elf_section(Elf_Scn *scn, Elf_Data **ret) |
| 23 |
{ |
| 24 |
GElf_Shdr shdr_mem, *shdr; |
| 25 |
shdr = gelf_getshdr(scn, &shdr_mem); |
| 26 |
if (!shdr) |
| 27 |
return drgn_error_libelf(); |
| 28 |
if (shdr->sh_type == SHT_NOBITS) { |
| 29 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 30 |
"section has no data"); |
| 31 |
} |
| 32 |
if ((shdr->sh_flags & SHF_COMPRESSED) && elf_compress(scn, 0, 0) < 0) |
| 33 |
return drgn_error_libelf(); |
| 34 |
Elf_Data *data = elf_rawdata(scn, NULL); |
| 35 |
if (!data) |
| 36 |
return drgn_error_libelf(); |
| 37 |
*ret = data; |
| 38 |
return NULL; |
| 39 |
} |
| 40 |
|
| 41 |
void truncate_elf_string_data(Elf_Data *data) |
| 42 |
{ |
| 43 |
const char *buf = data->d_buf; |
| 44 |
const char *nul = memrchr(buf, '\0', data->d_size); |
| 45 |
if (nul) |
| 46 |
data->d_size = nul - buf + 1; |
| 47 |
else |
| 48 |
data->d_size = 0; |
| 49 |
} |
| 50 |
|
| 51 |
#include "drgn_section_name_to_index.inc" |
| 52 |
|
| 53 |
enum drgn_dwarf_file_type { |
| 54 |
DRGN_DWARF_FILE_NONE, |
| 55 |
DRGN_DWARF_FILE_GNU_LTO, |
| 56 |
DRGN_DWARF_FILE_DWO, |
| 57 |
DRGN_DWARF_FILE_PLAIN, |
| 58 |
}; |
| 59 |
|
| 60 |
struct drgn_error *drgn_elf_file_create(struct drgn_module *module, |
| 61 |
const char *path, int fd, char *image, |
| 62 |
Elf *elf, struct drgn_elf_file **ret) |
| 63 |
{ |
| 64 |
if (elf_kind(elf) != ELF_K_ELF) |
| 65 |
return drgn_error_create(DRGN_ERROR_OTHER, "not an ELF file"); |
| 66 |
|
| 67 |
GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr(elf, &ehdr_mem); |
| 68 |
if (!ehdr) |
| 69 |
return drgn_error_libelf(); |
| 70 |
|
| 71 |
_cleanup_free_ struct drgn_elf_file *file = calloc(1, sizeof(*file)); |
| 72 |
if (!file) |
| 73 |
return &drgn_enomem; |
| 74 |
|
| 75 |
if (ehdr->e_type == ET_EXEC || |
| 76 |
ehdr->e_type == ET_DYN || |
| 77 |
ehdr->e_type == ET_REL) { |
| 78 |
size_t shstrndx; |
| 79 |
if (elf_getshdrstrndx(elf, &shstrndx)) |
| 80 |
return drgn_error_libelf(); |
| 81 |
|
| 82 |
bool has_sections = false; |
| 83 |
bool has_alloc_section = false; |
| 84 |
// We mimic libdw's logic for choosing debug sections: we either |
| 85 |
// use all .debug_* or .zdebug_* sections |
| 86 |
// (DRGN_DWARF_FILE_PLAIN), all .debug_*.dwo or .zdebug_*.dwo |
| 87 |
// sections (DRGN_DWARF_FILE_DWO), or all .gnu.debuglto_.debug_* |
| 88 |
// sections (DRGN_DWARF_FILE_GNU_LTO), in that order of |
| 89 |
// preference. |
| 90 |
enum drgn_dwarf_file_type dwarf_file_type = DRGN_DWARF_FILE_NONE; |
| 91 |
Elf_Scn *scn = NULL; |
| 92 |
while ((scn = elf_nextscn(elf, scn))) { |
| 93 |
GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem); |
| 94 |
if (!shdr) |
| 95 |
return drgn_error_libelf(); |
| 96 |
|
| 97 |
has_sections = true; |
| 98 |
if (shdr->sh_type != SHT_NOBITS && |
| 99 |
shdr->sh_type != SHT_NOTE && |
| 100 |
(shdr->sh_flags & SHF_ALLOC)) |
| 101 |
has_alloc_section = true; |
| 102 |
|
| 103 |
const char *scnname = elf_strptr(elf, shstrndx, shdr->sh_name); |
| 104 |
if (!scnname) |
| 105 |
return drgn_error_libelf(); |
| 106 |
|
| 107 |
enum drgn_dwarf_file_type dwarf_section_type; |
| 108 |
if (strcmp(scnname, ".debug_cu_index") == 0 || |
| 109 |
strcmp(scnname, ".debug_tu_index") == 0) { |
| 110 |
dwarf_section_type = DRGN_DWARF_FILE_DWO; |
| 111 |
} else if (strstartswith(scnname, ".debug_") || |
| 112 |
strstartswith(scnname, ".zdebug_")) { |
| 113 |
if (strcmp(scnname + strlen(scnname) - 4, ".dwo") == 0) |
| 114 |
dwarf_section_type = DRGN_DWARF_FILE_DWO; |
| 115 |
else |
| 116 |
dwarf_section_type = DRGN_DWARF_FILE_PLAIN; |
| 117 |
} else if (strstartswith(scnname, ".gnu.debuglto_.debug")) { |
| 118 |
dwarf_section_type = DRGN_DWARF_FILE_GNU_LTO; |
| 119 |
} else { |
| 120 |
dwarf_section_type = DRGN_DWARF_FILE_NONE; |
| 121 |
} |
| 122 |
dwarf_file_type = max(dwarf_file_type, dwarf_section_type); |
| 123 |
} |
| 124 |
|
| 125 |
scn = NULL; |
| 126 |
while ((scn = elf_nextscn(elf, scn))) { |
| 127 |
GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem); |
| 128 |
if (!shdr) |
| 129 |
return drgn_error_libelf(); |
| 130 |
|
| 131 |
if (shdr->sh_type != SHT_PROGBITS) |
| 132 |
continue; |
| 133 |
|
| 134 |
const char *scnname = elf_strptr(elf, shstrndx, shdr->sh_name); |
| 135 |
if (!scnname) |
| 136 |
return drgn_error_libelf(); |
| 137 |
|
| 138 |
enum drgn_section_index index; |
| 139 |
if (strstartswith(scnname, ".debug_") || |
| 140 |
strstartswith(scnname, ".zdebug_")) { |
| 141 |
const char *subname; |
| 142 |
if (strstartswith(scnname, ".zdebug_")) |
| 143 |
subname = scnname + sizeof(".zdebug_") - 1; |
| 144 |
else |
| 145 |
subname = scnname + sizeof(".debug_") - 1; |
| 146 |
size_t len = strlen(subname); |
| 147 |
if (len >= 4 |
| 148 |
&& strcmp(subname + len - 4, ".dwo") == 0) { |
| 149 |
if (dwarf_file_type != DRGN_DWARF_FILE_DWO) |
| 150 |
continue; |
| 151 |
len -= 4; |
| 152 |
} else if (dwarf_file_type != DRGN_DWARF_FILE_PLAIN) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
index = drgn_debug_section_name_to_index(subname, len); |
| 156 |
} else if (strstartswith(scnname, ".gnu.debuglto_.debug_")) { |
| 157 |
if (dwarf_file_type != DRGN_DWARF_FILE_GNU_LTO) |
| 158 |
continue; |
| 159 |
const char *subname = |
| 160 |
scnname + sizeof(".gnu.debuglto_.debug_") - 1; |
| 161 |
index = drgn_debug_section_name_to_index(subname, |
| 162 |
strlen(subname)); |
| 163 |
} else if (strcmp(scnname, ".init.text") == 0) { |
| 164 |
// We consider a file to be vmlinux if it has an |
| 165 |
// .init.text section and is not relocatable |
| 166 |
// (which excludes kernel modules). |
| 167 |
// Keep this in sync with elf_is_vmlinux(). |
| 168 |
file->is_vmlinux = ehdr->e_type != ET_REL; |
| 169 |
index = DRGN_SECTION_INDEX_NUM; |
| 170 |
} else { |
| 171 |
index = drgn_non_debug_section_name_to_index(scnname); |
| 172 |
} |
| 173 |
if (index < DRGN_SECTION_INDEX_NUM && !file->scns[index]) |
| 174 |
file->scns[index] = scn; |
| 175 |
} |
| 176 |
|
| 177 |
if (ehdr->e_type == ET_REL) { |
| 178 |
// We consider a relocatable file "loadable" if it has |
| 179 |
// any allocated sections. |
| 180 |
file->is_loadable = has_alloc_section; |
| 181 |
file->is_relocatable = file->needs_relocation = true; |
| 182 |
} else { |
| 183 |
// We consider executable and shared object files |
| 184 |
// loadable if they have any loadable segments, and |
| 185 |
// either no sections or at least one allocated section. |
| 186 |
bool has_loadable_segment = false; |
| 187 |
size_t phnum; |
| 188 |
if (elf_getphdrnum(elf, &phnum) != 0) |
| 189 |
return drgn_error_libelf(); |
| 190 |
for (size_t i = 0; i < phnum; i++) { |
| 191 |
GElf_Phdr phdr_mem, *phdr = |
| 192 |
gelf_getphdr(elf, i, &phdr_mem); |
| 193 |
if (!phdr) |
| 194 |
return drgn_error_libelf(); |
| 195 |
if (phdr->p_type == PT_LOAD) { |
| 196 |
has_loadable_segment = true; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
file->is_loadable = |
| 201 |
has_loadable_segment && |
| 202 |
(!has_sections || has_alloc_section); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
file->module = module; |
| 207 |
file->path = strdup(path); |
| 208 |
if (!file->path) |
| 209 |
return &drgn_enomem; |
| 210 |
file->image = image; |
| 211 |
file->fd = fd; |
| 212 |
file->elf = elf; |
| 213 |
drgn_platform_from_elf(ehdr, &file->platform); |
| 214 |
*ret = no_cleanup_ptr(file); |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
void drgn_elf_file_destroy(struct drgn_elf_file *file) |
| 219 |
{ |
| 220 |
if (file) { |
| 221 |
free(file->sections_with_address); |
| 222 |
dwarf_end(file->_dwarf); |
| 223 |
elf_end(file->elf); |
| 224 |
if (file->fd >= 0) |
| 225 |
close(file->fd); |
| 226 |
free(file->image); |
| 227 |
free(file->path); |
| 228 |
free(file); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
static int should_apply_relocation_section(Elf *elf, size_t shstrndx, |
| 233 |
const GElf_Shdr *shdr) |
| 234 |
{ |
| 235 |
if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL) |
| 236 |
return 0; |
| 237 |
|
| 238 |
const char *scnname = elf_strptr(elf, shstrndx, shdr->sh_name); |
| 239 |
if (!scnname) |
| 240 |
return -1; |
| 241 |
if (shdr->sh_type == SHT_RELA) { |
| 242 |
if (!strstartswith(scnname, ".rela.")) |
| 243 |
return 0; |
| 244 |
scnname += sizeof(".rela.") - 1; |
| 245 |
} else { |
| 246 |
if (!strstartswith(scnname, ".rel.")) |
| 247 |
return 0; |
| 248 |
scnname += sizeof(".rel.") - 1; |
| 249 |
} |
| 250 |
return (strstartswith(scnname, "debug_") |
| 251 |
|| strstartswith(scnname, "orc_")); |
| 252 |
} |
| 253 |
|
| 254 |
static inline struct drgn_error *get_reloc_sym_value(const void *syms, |
| 255 |
size_t num_syms, |
| 256 |
const uint64_t *sh_addrs, |
| 257 |
size_t shdrnum, |
| 258 |
bool is_64_bit, |
| 259 |
bool bswap, |
| 260 |
uint32_t r_sym, |
| 261 |
uint64_t *ret) |
| 262 |
{ |
| 263 |
if (r_sym >= num_syms) { |
| 264 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 265 |
"invalid ELF relocation symbol"); |
| 266 |
} |
| 267 |
uint16_t st_shndx; |
| 268 |
uint64_t st_value; |
| 269 |
if (is_64_bit) { |
| 270 |
const Elf64_Sym *sym = (Elf64_Sym *)syms + r_sym; |
| 271 |
memcpy(&st_shndx, &sym->st_shndx, sizeof(st_shndx)); |
| 272 |
memcpy(&st_value, &sym->st_value, sizeof(st_value)); |
| 273 |
if (bswap) { |
| 274 |
st_shndx = bswap_16(st_shndx); |
| 275 |
st_value = bswap_64(st_value); |
| 276 |
} |
| 277 |
} else { |
| 278 |
const Elf32_Sym *sym = (Elf32_Sym *)syms + r_sym; |
| 279 |
memcpy(&st_shndx, &sym->st_shndx, sizeof(st_shndx)); |
| 280 |
uint32_t st_value32; |
| 281 |
memcpy(&st_value32, &sym->st_value, sizeof(st_value32)); |
| 282 |
if (bswap) { |
| 283 |
st_shndx = bswap_16(st_shndx); |
| 284 |
st_value32 = bswap_32(st_value32); |
| 285 |
} |
| 286 |
st_value = st_value32; |
| 287 |
} |
| 288 |
if (st_shndx >= shdrnum) { |
| 289 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 290 |
"invalid ELF symbol section index"); |
| 291 |
} |
| 292 |
*ret = sh_addrs[st_shndx] + st_value; |
| 293 |
return NULL; |
| 294 |
} |
| 295 |
|
| 296 |
static struct drgn_error * |
| 297 |
apply_elf_relas(const struct drgn_relocating_section *relocating, |
| 298 |
Elf_Data *reloc_data, Elf_Data *symtab_data, |
| 299 |
const uint64_t *sh_addrs, size_t shdrnum, |
| 300 |
const struct drgn_platform *platform) |
| 301 |
{ |
| 302 |
struct drgn_error *err; |
| 303 |
|
| 304 |
bool is_64_bit = drgn_platform_is_64_bit(platform); |
| 305 |
bool bswap = drgn_platform_bswap(platform); |
| 306 |
apply_elf_reloc_fn *apply_elf_reloc = platform->arch->apply_elf_reloc; |
| 307 |
|
| 308 |
const void *relocs = reloc_data->d_buf; |
| 309 |
size_t reloc_size = is_64_bit ? sizeof(Elf64_Rela) : sizeof(Elf32_Rela); |
| 310 |
size_t num_relocs = reloc_data->d_size / reloc_size; |
| 311 |
|
| 312 |
const void *syms = symtab_data->d_buf; |
| 313 |
size_t sym_size = is_64_bit ? sizeof(Elf64_Sym) : sizeof(Elf32_Sym); |
| 314 |
size_t num_syms = symtab_data->d_size / sym_size; |
| 315 |
|
| 316 |
for (size_t i = 0; i < num_relocs; i++) { |
| 317 |
uint64_t r_offset; |
| 318 |
uint32_t r_sym; |
| 319 |
uint32_t r_type; |
| 320 |
int64_t r_addend; |
| 321 |
if (is_64_bit) { |
| 322 |
const Elf64_Rela *rela = (Elf64_Rela *)relocs + i; |
| 323 |
uint64_t r_info; |
| 324 |
memcpy(&r_offset, &rela->r_offset, sizeof(r_offset)); |
| 325 |
memcpy(&r_info, &rela->r_info, sizeof(r_info)); |
| 326 |
memcpy(&r_addend, &rela->r_addend, sizeof(r_addend)); |
| 327 |
if (bswap) { |
| 328 |
r_offset = bswap_64(r_offset); |
| 329 |
r_info = bswap_64(r_info); |
| 330 |
r_addend = bswap_64(r_addend); |
| 331 |
} |
| 332 |
r_sym = ELF64_R_SYM(r_info); |
| 333 |
r_type = ELF64_R_TYPE(r_info); |
| 334 |
} else { |
| 335 |
const Elf32_Rela *rela32 = (Elf32_Rela *)relocs + i; |
| 336 |
uint32_t r_offset32; |
| 337 |
uint32_t r_info32; |
| 338 |
int32_t r_addend32; |
| 339 |
memcpy(&r_offset32, &rela32->r_offset, sizeof(r_offset32)); |
| 340 |
memcpy(&r_info32, &rela32->r_info, sizeof(r_info32)); |
| 341 |
memcpy(&r_addend32, &rela32->r_addend, sizeof(r_addend32)); |
| 342 |
if (bswap) { |
| 343 |
r_offset32 = bswap_32(r_offset32); |
| 344 |
r_info32 = bswap_32(r_info32); |
| 345 |
r_addend32 = bswap_32(r_addend32); |
| 346 |
} |
| 347 |
r_offset = r_offset32; |
| 348 |
r_sym = ELF32_R_SYM(r_info32); |
| 349 |
r_type = ELF32_R_TYPE(r_info32); |
| 350 |
r_addend = r_addend32; |
| 351 |
} |
| 352 |
uint64_t sym_value; |
| 353 |
err = get_reloc_sym_value(syms, num_syms, sh_addrs, shdrnum, |
| 354 |
is_64_bit, bswap, r_sym, &sym_value); |
| 355 |
if (err) |
| 356 |
return err; |
| 357 |
|
| 358 |
err = apply_elf_reloc(relocating, r_offset, r_type, &r_addend, |
| 359 |
sym_value); |
| 360 |
if (err) |
| 361 |
return err; |
| 362 |
} |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
static struct drgn_error * |
| 367 |
apply_elf_rels(const struct drgn_relocating_section *relocating, |
| 368 |
Elf_Data *reloc_data, Elf_Data *symtab_data, |
| 369 |
const uint64_t *sh_addrs, size_t shdrnum, |
| 370 |
const struct drgn_platform *platform) |
| 371 |
{ |
| 372 |
struct drgn_error *err; |
| 373 |
|
| 374 |
bool is_64_bit = drgn_platform_is_64_bit(platform); |
| 375 |
bool bswap = drgn_platform_bswap(platform); |
| 376 |
apply_elf_reloc_fn *apply_elf_reloc = platform->arch->apply_elf_reloc; |
| 377 |
|
| 378 |
const void *relocs = reloc_data->d_buf; |
| 379 |
size_t reloc_size = is_64_bit ? sizeof(Elf64_Rel) : sizeof(Elf32_Rel); |
| 380 |
size_t num_relocs = reloc_data->d_size / reloc_size; |
| 381 |
|
| 382 |
const void *syms = symtab_data->d_buf; |
| 383 |
size_t sym_size = is_64_bit ? sizeof(Elf64_Sym) : sizeof(Elf32_Sym); |
| 384 |
size_t num_syms = symtab_data->d_size / sym_size; |
| 385 |
|
| 386 |
for (size_t i = 0; i < num_relocs; i++) { |
| 387 |
uint64_t r_offset; |
| 388 |
uint32_t r_sym; |
| 389 |
uint32_t r_type; |
| 390 |
if (is_64_bit) { |
| 391 |
const Elf64_Rel *rel = (Elf64_Rel *)relocs + i; |
| 392 |
uint64_t r_info; |
| 393 |
memcpy(&r_offset, &rel->r_offset, sizeof(r_offset)); |
| 394 |
memcpy(&r_info, &rel->r_info, sizeof(r_info)); |
| 395 |
if (bswap) { |
| 396 |
r_offset = bswap_64(r_offset); |
| 397 |
r_info = bswap_64(r_info); |
| 398 |
} |
| 399 |
r_sym = ELF64_R_SYM(r_info); |
| 400 |
r_type = ELF64_R_TYPE(r_info); |
| 401 |
} else { |
| 402 |
const Elf32_Rel *rel32 = (Elf32_Rel *)relocs + i; |
| 403 |
uint32_t r_offset32; |
| 404 |
uint32_t r_info32; |
| 405 |
memcpy(&r_offset32, &rel32->r_offset, sizeof(r_offset32)); |
| 406 |
memcpy(&r_info32, &rel32->r_info, sizeof(r_info32)); |
| 407 |
if (bswap) { |
| 408 |
r_offset32 = bswap_32(r_offset32); |
| 409 |
r_info32 = bswap_32(r_info32); |
| 410 |
} |
| 411 |
r_offset = r_offset32; |
| 412 |
r_sym = ELF32_R_SYM(r_info32); |
| 413 |
r_type = ELF32_R_TYPE(r_info32); |
| 414 |
} |
| 415 |
uint64_t sym_value; |
| 416 |
err = get_reloc_sym_value(syms, num_syms, sh_addrs, shdrnum, |
| 417 |
is_64_bit, bswap, r_sym, &sym_value); |
| 418 |
if (err) |
| 419 |
return err; |
| 420 |
|
| 421 |
err = apply_elf_reloc(relocating, r_offset, r_type, NULL, |
| 422 |
sym_value); |
| 423 |
if (err) |
| 424 |
return err; |
| 425 |
} |
| 426 |
return NULL; |
| 427 |
} |
| 428 |
|
| 429 |
struct drgn_error * |
| 430 |
drgn_elf_file_apply_relocations(struct drgn_elf_file *file) |
| 431 |
{ |
| 432 |
struct drgn_error *err; |
| 433 |
|
| 434 |
if (!file->needs_relocation) |
| 435 |
return NULL; |
| 436 |
|
| 437 |
if (!file->platform.arch->apply_elf_reloc) { |
| 438 |
return drgn_error_format(DRGN_ERROR_NOT_IMPLEMENTED, |
| 439 |
"relocation support is not implemented for %s architecture", |
| 440 |
file->platform.arch->name); |
| 441 |
} |
| 442 |
|
| 443 |
Elf *elf = file->elf; |
| 444 |
size_t shdrnum; |
| 445 |
if (elf_getshdrnum(elf, &shdrnum)) |
| 446 |
return drgn_error_libelf(); |
| 447 |
_cleanup_free_ uint64_t *sh_addrs = |
| 448 |
calloc(shdrnum, sizeof(sh_addrs[0])); |
| 449 |
if (!sh_addrs && shdrnum > 0) |
| 450 |
return &drgn_enomem; |
| 451 |
|
| 452 |
Elf_Scn *scn = NULL; |
| 453 |
while ((scn = elf_nextscn(elf, scn))) { |
| 454 |
GElf_Shdr *shdr, shdr_mem; |
| 455 |
shdr = gelf_getshdr(scn, &shdr_mem); |
| 456 |
if (!shdr) |
| 457 |
return drgn_error_libelf(); |
| 458 |
sh_addrs[elf_ndxscn(scn)] = shdr->sh_addr; |
| 459 |
} |
| 460 |
|
| 461 |
size_t shstrndx; |
| 462 |
if (elf_getshdrstrndx(elf, &shstrndx)) |
| 463 |
return drgn_error_libelf(); |
| 464 |
|
| 465 |
Elf_Scn *reloc_scn = NULL; |
| 466 |
while ((reloc_scn = elf_nextscn(elf, reloc_scn))) { |
| 467 |
GElf_Shdr *reloc_shdr, reloc_shdr_mem; |
| 468 |
reloc_shdr = gelf_getshdr(reloc_scn, &reloc_shdr_mem); |
| 469 |
if (!reloc_shdr) |
| 470 |
return drgn_error_libelf(); |
| 471 |
|
| 472 |
int r = should_apply_relocation_section(elf, shstrndx, |
| 473 |
reloc_shdr); |
| 474 |
if (r < 0) |
| 475 |
return drgn_error_libelf(); |
| 476 |
if (r) { |
| 477 |
scn = elf_getscn(elf, reloc_shdr->sh_info); |
| 478 |
if (!scn) |
| 479 |
return drgn_error_libelf(); |
| 480 |
GElf_Shdr *shdr, shdr_mem; |
| 481 |
shdr = gelf_getshdr(scn, &shdr_mem); |
| 482 |
if (!shdr) |
| 483 |
return drgn_error_libelf(); |
| 484 |
if (shdr->sh_type == SHT_NOBITS) |
| 485 |
continue; |
| 486 |
|
| 487 |
Elf_Scn *symtab_scn = elf_getscn(elf, |
| 488 |
reloc_shdr->sh_link); |
| 489 |
if (!symtab_scn) |
| 490 |
return drgn_error_libelf(); |
| 491 |
shdr = gelf_getshdr(symtab_scn, &shdr_mem); |
| 492 |
if (!shdr) |
| 493 |
return drgn_error_libelf(); |
| 494 |
if (shdr->sh_type == SHT_NOBITS) { |
| 495 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 496 |
"relocation symbol table has no data"); |
| 497 |
} |
| 498 |
|
| 499 |
Elf_Data *data, *reloc_data, *symtab_data; |
| 500 |
if ((err = read_elf_section(scn, &data)) |
| 501 |
|| (err = read_elf_section(reloc_scn, &reloc_data)) |
| 502 |
|| (err = read_elf_section(symtab_scn, &symtab_data))) |
| 503 |
return err; |
| 504 |
|
| 505 |
struct drgn_relocating_section relocating = { |
| 506 |
.buf = data->d_buf, |
| 507 |
.buf_size = data->d_size, |
| 508 |
.addr = sh_addrs[elf_ndxscn(scn)], |
| 509 |
.bswap = drgn_platform_bswap(&file->platform), |
| 510 |
}; |
| 511 |
|
| 512 |
if (reloc_shdr->sh_type == SHT_RELA) { |
| 513 |
err = apply_elf_relas(&relocating, reloc_data, |
| 514 |
symtab_data, sh_addrs, |
| 515 |
shdrnum, &file->platform); |
| 516 |
} else { |
| 517 |
err = apply_elf_rels(&relocating, reloc_data, |
| 518 |
symtab_data, sh_addrs, |
| 519 |
shdrnum, &file->platform); |
| 520 |
} |
| 521 |
if (err) |
| 522 |
return err; |
| 523 |
} |
| 524 |
} |
| 525 |
file->needs_relocation = false; |
| 526 |
return NULL; |
| 527 |
} |
| 528 |
|
| 529 |
struct drgn_error *drgn_elf_file_read_section(struct drgn_elf_file *file, |
| 530 |
enum drgn_section_index scn, |
| 531 |
Elf_Data **ret) |
| 532 |
{ |
| 533 |
struct drgn_error *err; |
| 534 |
if (!file->scn_data[scn]) { |
| 535 |
err = drgn_elf_file_apply_relocations(file); |
| 536 |
if (err) |
| 537 |
return err; |
| 538 |
err = read_elf_section(file->scns[scn], &file->scn_data[scn]); |
| 539 |
if (err) |
| 540 |
return err; |
| 541 |
if (scn == DRGN_SCN_DEBUG_STR) |
| 542 |
truncate_elf_string_data(file->scn_data[scn]); |
| 543 |
} |
| 544 |
*ret = file->scn_data[scn]; |
| 545 |
return NULL; |
| 546 |
} |
| 547 |
|
| 548 |
struct drgn_error *drgn_elf_file_get_dwarf(struct drgn_elf_file *file, |
| 549 |
Dwarf **ret) |
| 550 |
{ |
| 551 |
struct drgn_error *err; |
| 552 |
if (!file->_dwarf) { |
| 553 |
struct drgn_elf_file *supplementary_file = |
| 554 |
file->module->supplementary_debug_file; |
| 555 |
if (supplementary_file) { |
| 556 |
supplementary_file->_dwarf = |
| 557 |
dwarf_begin_elf(supplementary_file->elf, |
| 558 |
DWARF_C_READ, NULL); |
| 559 |
if (!supplementary_file->_dwarf) |
| 560 |
return drgn_error_libdw(); |
| 561 |
} |
| 562 |
|
| 563 |
err = drgn_elf_file_apply_relocations(file); |
| 564 |
if (err) |
| 565 |
return err; |
| 566 |
|
| 567 |
file->_dwarf = dwarf_begin_elf(file->elf, DWARF_C_READ, NULL); |
| 568 |
if (!file->_dwarf) |
| 569 |
return drgn_error_libdw(); |
| 570 |
|
| 571 |
if (supplementary_file) |
| 572 |
dwarf_setalt(file->_dwarf, supplementary_file->_dwarf); |
| 573 |
} |
| 574 |
*ret = file->_dwarf; |
| 575 |
return NULL; |
| 576 |
} |
| 577 |
|
| 578 |
struct drgn_error * |
| 579 |
drgn_elf_file_section_error(struct drgn_elf_file *file, Elf_Scn *scn, |
| 580 |
Elf_Data *data, const char *ptr, |
| 581 |
const char *message) |
| 582 |
{ |
| 583 |
// If we don't know what section the pointer came from, try to find it |
| 584 |
// in the cached sections. |
| 585 |
if (!scn) { |
| 586 |
uintptr_t p = (uintptr_t)ptr; |
| 587 |
for (size_t i = 0; i < array_size(file->scn_data); i++) { |
| 588 |
if (!file->scn_data[i]) |
| 589 |
continue; |
| 590 |
uintptr_t start = (uintptr_t)file->scn_data[i]->d_buf; |
| 591 |
uintptr_t end = start + file->scn_data[i]->d_size; |
| 592 |
if (start <= p) { |
| 593 |
// If the pointer matches the end of a section, |
| 594 |
// remember the section but try to find a better |
| 595 |
// match. |
| 596 |
if (p <= end) { |
| 597 |
scn = file->scns[i]; |
| 598 |
data = file->scn_data[i]; |
| 599 |
} |
| 600 |
// If the pointer lies inside of the section, |
| 601 |
// we're done. |
| 602 |
if (p < end) |
| 603 |
break; |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
const char *scnname = NULL; |
| 608 |
size_t shstrndx; |
| 609 |
GElf_Shdr shdr_mem, *shdr; |
| 610 |
if (!elf_getshdrstrndx(file->elf, &shstrndx) && |
| 611 |
(shdr = gelf_getshdr(scn, &shdr_mem))) |
| 612 |
scnname = elf_strptr(file->elf, shstrndx, shdr->sh_name); |
| 613 |
|
| 614 |
if (scnname && data) { |
| 615 |
return drgn_error_format(DRGN_ERROR_OTHER, "%s: %s+%#tx: %s", |
| 616 |
file->path, scnname, |
| 617 |
ptr - (const char *)data->d_buf, |
| 618 |
message); |
| 619 |
} else if (scnname) { |
| 620 |
return drgn_error_format(DRGN_ERROR_OTHER, "%s: %s: %s", |
| 621 |
file->path, scnname, message); |
| 622 |
} else { |
| 623 |
return drgn_error_format(DRGN_ERROR_OTHER, "%s: %s", file->path, |
| 624 |
message); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
struct drgn_error * |
| 629 |
drgn_elf_file_section_errorf(struct drgn_elf_file *file, Elf_Scn *scn, |
| 630 |
Elf_Data *data, const char *ptr, |
| 631 |
const char *format, ...) |
| 632 |
{ |
| 633 |
va_list ap; |
| 634 |
va_start(ap, format); |
| 635 |
char *message; |
| 636 |
int ret = vasprintf(&message, format, ap); |
| 637 |
va_end(ap); |
| 638 |
if (ret < 0) |
| 639 |
return &drgn_enomem; |
| 640 |
struct drgn_error *err = drgn_elf_file_section_error(file, scn, data, |
| 641 |
ptr, message); |
| 642 |
free(message); |
| 643 |
return err; |
| 644 |
} |
| 645 |
|
| 646 |
struct drgn_error *drgn_elf_file_section_buffer_error(struct binary_buffer *bb, |
| 647 |
const char *ptr, |
| 648 |
const char *message) |
| 649 |
{ |
| 650 |
struct drgn_elf_file_section_buffer *buffer = |
| 651 |
container_of(bb, struct drgn_elf_file_section_buffer, bb); |
| 652 |
return drgn_elf_file_section_error(buffer->file, buffer->scn, |
| 653 |
buffer->data, ptr, message); |
| 654 |
} |
| 655 |
|
| 656 |
static bool elf_address_range_from_first_and_last_segment(Elf *elf, |
| 657 |
uint64_t *start_ret, |
| 658 |
uint64_t *end_ret) |
| 659 |
{ |
| 660 |
size_t phnum; |
| 661 |
if (elf_getphdrnum(elf, &phnum)) |
| 662 |
return false; |
| 663 |
|
| 664 |
uint64_t start; |
| 665 |
GElf_Phdr phdr_mem, *phdr; |
| 666 |
size_t i; |
| 667 |
for (i = 0; i < phnum; i++) { |
| 668 |
phdr = gelf_getphdr(elf, i, &phdr_mem); |
| 669 |
if (!phdr) |
| 670 |
return false; |
| 671 |
// Program headers with a p_memsz of 0 have been observed in |
| 672 |
// vmlinux since Linux kernel commit 3e86e4d74c04 ("kbuild: keep |
| 673 |
// .modinfo section in vmlinux.unstripped") (in v6.18). |
| 674 |
if (phdr->p_type == PT_LOAD && phdr->p_memsz > 0) { |
| 675 |
start = phdr->p_vaddr; |
| 676 |
break; |
| 677 |
} |
| 678 |
} |
| 679 |
if (i >= phnum) { |
| 680 |
*start_ret = *end_ret = 0; |
| 681 |
return true; |
| 682 |
} |
| 683 |
|
| 684 |
for (i = phnum; i-- > 0;) { |
| 685 |
phdr = gelf_getphdr(elf, i, &phdr_mem); |
| 686 |
if (!phdr) |
| 687 |
return false; |
| 688 |
|
| 689 |
if (phdr->p_type == PT_LOAD && phdr->p_memsz > 0) { |
| 690 |
uint64_t end = phdr->p_vaddr + phdr->p_memsz; |
| 691 |
if (start < end) { |
| 692 |
*start_ret = start; |
| 693 |
*end_ret = end; |
| 694 |
return true; |
| 695 |
} |
| 696 |
break; |
| 697 |
} |
| 698 |
} |
| 699 |
*start_ret = *end_ret = 0; |
| 700 |
return true; |
| 701 |
} |
| 702 |
|
| 703 |
static bool elf_address_range_from_min_and_max_segment(Elf *elf, |
| 704 |
uint64_t *start_ret, |
| 705 |
uint64_t *end_ret) |
| 706 |
{ |
| 707 |
size_t phnum; |
| 708 |
if (elf_getphdrnum(elf, &phnum)) |
| 709 |
return false; |
| 710 |
|
| 711 |
uint64_t start = UINT64_MAX, end = 0; |
| 712 |
for (size_t i = 0; i < phnum; i++) { |
| 713 |
GElf_Phdr phdr_mem, *phdr = gelf_getphdr(elf, i, &phdr_mem); |
| 714 |
if (!phdr) |
| 715 |
return false; |
| 716 |
if (phdr->p_type == PT_LOAD && phdr->p_memsz > 0) { |
| 717 |
start = min(start, phdr->p_vaddr); |
| 718 |
end = max(end, phdr->p_vaddr + phdr->p_memsz); |
| 719 |
} |
| 720 |
} |
| 721 |
if (start < end) { |
| 722 |
*start_ret = start; |
| 723 |
*end_ret = end; |
| 724 |
} else { |
| 725 |
*start_ret = *end_ret = 0; |
| 726 |
} |
| 727 |
return true; |
| 728 |
} |
| 729 |
|
| 730 |
bool drgn_elf_file_address_range(struct drgn_elf_file *file, |
| 731 |
uint64_t *start_ret, uint64_t *end_ret) |
| 732 |
{ |
| 733 |
// The ELF specification says that "loadable segment entries in the |
| 734 |
// program header table appear in ascending order, sorted on the p_vaddr |
| 735 |
// member." However, this is not the case in practice. |
| 736 |
// |
| 737 |
// vmlinux on some architectures contains special segments whose |
| 738 |
// addresses are not meaningful and break the sorted order (e.g., |
| 739 |
// segments corresponding to the .data..percpu section on x86-64 and the |
| 740 |
// .vectors and .stubs sections on Arm). It appears that segments in |
| 741 |
// vmlinux are sorted other than those special segments, and the special |
| 742 |
// segments are never the first or last segment. |
| 743 |
// |
| 744 |
// Userspace ELF loaders disagree about whether to assume sorted order: |
| 745 |
// |
| 746 |
// - As of Linux kernel commit 10b19249192a ("ELF: fix overflow in total |
| 747 |
// mapping size calculation") (in v5.18), the Linux kernel DOES NOT |
| 748 |
// assume sorting. Before that, it DOES. |
| 749 |
// - glibc as of v2.40 DOES assume sorting; see _dl_map_object_from_fd() |
| 750 |
// in elf/dl-load.c and _dl_map_segments() in elf/dl-map-segments.h. |
| 751 |
// - musl as of v1.2.5 DOES NOT assume sorting; see map_library() in |
| 752 |
// ldso/dynlink.c. |
| 753 |
// |
| 754 |
// So, we use a heuristic: if the file has an .init.text section, then |
| 755 |
// it is probably a vmlinux file, so we assume the sorted order, which |
| 756 |
// allows us to ignore the special segments in the middle. |
| 757 |
// |
| 758 |
// Otherwise, we don't assume the sorted order. |
| 759 |
if (file->is_vmlinux) { |
| 760 |
return elf_address_range_from_first_and_last_segment(file->elf, |
| 761 |
start_ret, |
| 762 |
end_ret); |
| 763 |
} else { |
| 764 |
return elf_address_range_from_min_and_max_segment(file->elf, |
| 765 |
start_ret, |
| 766 |
end_ret); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
// Keep this in sync with drgn_elf_file_create(). |
| 771 |
int elf_is_vmlinux(Elf *elf) |
| 772 |
{ |
| 773 |
GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr(elf, &ehdr_mem); |
| 774 |
if (!ehdr) |
| 775 |
return -1; |
| 776 |
|
| 777 |
if (ehdr->e_type == ET_REL) |
| 778 |
return 0; |
| 779 |
|
| 780 |
size_t shstrndx; |
| 781 |
if (elf_getshdrstrndx(elf, &shstrndx)) |
| 782 |
return -1; |
| 783 |
Elf_Scn *scn = NULL; |
| 784 |
while ((scn = elf_nextscn(elf, scn))) { |
| 785 |
GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem); |
| 786 |
if (!shdr) |
| 787 |
return -1; |
| 788 |
|
| 789 |
if (shdr->sh_type != SHT_PROGBITS) |
| 790 |
continue; |
| 791 |
|
| 792 |
const char *scnname = elf_strptr(elf, shstrndx, shdr->sh_name); |
| 793 |
if (!scnname) |
| 794 |
return -1; |
| 795 |
|
| 796 |
if (strcmp(scnname, ".init.text") == 0) |
| 797 |
return 1; |
| 798 |
} |
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
ssize_t elf_vmlinux_release(Elf *elf, const char **ret) |
| 803 |
{ |
| 804 |
Elf_Scn *scn = NULL; |
| 805 |
while ((scn = elf_nextscn(elf, scn))) { |
| 806 |
GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem); |
| 807 |
if (!shdr) |
| 808 |
return -1; |
| 809 |
|
| 810 |
if (shdr->sh_type != SHT_SYMTAB || shdr->sh_entsize == 0) |
| 811 |
continue; |
| 812 |
|
| 813 |
Elf_Data *data = elf_getdata(scn, NULL); |
| 814 |
if (!data) |
| 815 |
return -1; |
| 816 |
|
| 817 |
size_t num_syms = shdr->sh_size / shdr->sh_entsize; |
| 818 |
for (size_t i = 0; i < num_syms; i++) { |
| 819 |
GElf_Sym sym_mem, *sym = gelf_getsym(data, i, &sym_mem); |
| 820 |
if (!sym) |
| 821 |
return -1; |
| 822 |
|
| 823 |
static const char prefix[] = "Linux version "; |
| 824 |
|
| 825 |
if (GELF_ST_TYPE(sym->st_info) != STT_OBJECT |
| 826 |
|| GELF_ST_BIND(sym->st_info) != STB_GLOBAL |
| 827 |
|| sym->st_size < sizeof(prefix) - 1) |
| 828 |
continue; |
| 829 |
|
| 830 |
const char *name = elf_strptr(elf, shdr->sh_link, |
| 831 |
sym->st_name); |
| 832 |
if (!name) |
| 833 |
return -1; |
| 834 |
if (strcmp(name, "linux_banner") != 0) |
| 835 |
continue; |
| 836 |
|
| 837 |
GElf_Shdr sym_shdr_mem, *sym_shdr = |
| 838 |
gelf_getshdr(elf_getscn(elf, sym->st_shndx), |
| 839 |
&sym_shdr_mem); |
| 840 |
if (!sym_shdr) |
| 841 |
return -1; |
| 842 |
|
| 843 |
int64_t offset = sym_shdr->sh_offset |
| 844 |
+ sym->st_value - sym_shdr->sh_addr; |
| 845 |
Elf_Data *banner_data = |
| 846 |
elf_getdata_rawchunk(elf, offset, sym->st_size, |
| 847 |
ELF_T_BYTE); |
| 848 |
if (!banner_data) |
| 849 |
return -1; |
| 850 |
|
| 851 |
if (memcmp(banner_data->d_buf, prefix, |
| 852 |
sizeof(prefix) - 1) != 0) |
| 853 |
return 0; |
| 854 |
|
| 855 |
const char *release = (const char *)banner_data->d_buf |
| 856 |
+ (sizeof(prefix) - 1); |
| 857 |
const char *space = |
| 858 |
memchr(release, ' ', |
| 859 |
banner_data->d_size - (sizeof(prefix) - 1)); |
| 860 |
if (!space) |
| 861 |
return 0; |
| 862 |
*ret = release; |
| 863 |
return space - release; |
| 864 |
} |
| 865 |
} |
| 866 |
return 0; |
| 867 |
} |