| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include <byteswap.h> |
| 6 |
#include <elf.h> |
| 7 |
#include <elfutils/libdw.h> |
| 8 |
#include <elfutils/version.h> |
| 9 |
#include <gelf.h> |
| 10 |
#include <inttypes.h> |
| 11 |
#include <limits.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include "array.h" |
| 16 |
#include "binary_buffer.h" |
| 17 |
#include "binary_search.h" |
| 18 |
#include "cleanup.h" |
| 19 |
#include "debug_info.h" // IWYU pragma: associated |
| 20 |
#include "dwarf_constants.h" |
| 21 |
#include "elf_file.h" |
| 22 |
#include "error.h" |
| 23 |
#include "language.h" |
| 24 |
#include "lazy_object.h" |
| 25 |
#include "log.h" |
| 26 |
#include "minmax.h" |
| 27 |
#include "object.h" |
| 28 |
#include "openmp.h" |
| 29 |
#include "path.h" |
| 30 |
#include "program.h" |
| 31 |
#include "platform.h" |
| 32 |
#include "register_state.h" |
| 33 |
#include "serialize.h" |
| 34 |
#include "type.h" |
| 35 |
#include "util.h" |
| 36 |
|
| 37 |
#if !_ELFUTILS_PREREQ(0, 191) |
| 38 |
static inline int dwarf_cu_dwp_section_info(Dwarf_CU *cu, unsigned int section, |
| 39 |
Dwarf_Off *offsetp, |
| 40 |
Dwarf_Off *sizep) |
| 41 |
{ |
| 42 |
*offsetp = 0; |
| 43 |
if (sizep) |
| 44 |
*sizep = 0; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
#endif |
| 48 |
|
| 49 |
void drgn_module_dwarf_info_deinit(struct drgn_module *module) |
| 50 |
{ |
| 51 |
free(module->dwarf.eh_frame.fdes); |
| 52 |
free(module->dwarf.eh_frame.cies); |
| 53 |
free(module->dwarf.debug_frame.fdes); |
| 54 |
free(module->dwarf.debug_frame.cies); |
| 55 |
} |
| 56 |
|
| 57 |
DEFINE_VECTOR_FUNCTIONS(drgn_dwarf_index_die_vector); |
| 58 |
|
| 59 |
DEFINE_HASH_MAP_FUNCTIONS(drgn_dwarf_index_die_map, nstring_hash_pair, |
| 60 |
nstring_eq); |
| 61 |
|
| 62 |
static inline struct nstring |
| 63 |
drgn_namespace_key(struct drgn_namespace_dwarf_index * const *entry) |
| 64 |
{ |
| 65 |
return (struct nstring){ (*entry)->name, (*entry)->name_len }; |
| 66 |
} |
| 67 |
DEFINE_HASH_TABLE_FUNCTIONS(drgn_namespace_table, drgn_namespace_key, |
| 68 |
nstring_hash_pair, nstring_eq); |
| 69 |
|
| 70 |
DEFINE_HASH_MAP_FUNCTIONS(drgn_dwarf_base_type_map, nstring_hash_pair, |
| 71 |
nstring_eq); |
| 72 |
|
| 73 |
DEFINE_HASH_MAP_FUNCTIONS(drgn_dwarf_specification_map, int_key_hash_pair, |
| 74 |
scalar_key_eq); |
| 75 |
|
| 76 |
/** DWARF compilation unit indexed in a @ref drgn_namespace_dwarf_index. */ |
| 77 |
struct drgn_dwarf_index_cu { |
| 78 |
/** File containing CU. */ |
| 79 |
struct drgn_elf_file *file; |
| 80 |
/** Address of CU data. */ |
| 81 |
const char *buf; |
| 82 |
/** Length of CU data. */ |
| 83 |
size_t len; |
| 84 |
/** DWARF version from CU header. */ |
| 85 |
uint8_t version; |
| 86 |
/** `DW_UT_*` type from CU header. */ |
| 87 |
uint8_t unit_type; |
| 88 |
/** Address size from CU header. */ |
| 89 |
uint8_t address_size; |
| 90 |
/** Whether CU uses 64-bit DWARF format. */ |
| 91 |
bool is_64_bit; |
| 92 |
/** |
| 93 |
* Section containing CU (@ref DRGN_SCN_DEBUG_INFO or @ref |
| 94 |
* DRGN_SCN_DEBUG_TYPES). |
| 95 |
*/ |
| 96 |
enum drgn_section_index scn; |
| 97 |
/** |
| 98 |
* Mapping from DWARF abbreviation code to instructions for that |
| 99 |
* abbreviation. |
| 100 |
* |
| 101 |
* This is indexed on the DWARF abbreviation code minus one. I.e., |
| 102 |
* `abbrev_insns[abbrev_decls[abbrev_code - 1]]` is the first |
| 103 |
* instruction for that abbreviation code. |
| 104 |
* |
| 105 |
* Technically, abbreviation codes don't have to be sequential. In |
| 106 |
* practice, GCC and Clang seem to always generate sequential codes |
| 107 |
* starting at one, so we can get away with a flat array. |
| 108 |
*/ |
| 109 |
uint32_t *abbrev_decls; |
| 110 |
union { |
| 111 |
/** Number of abbreviation codes. */ |
| 112 |
size_t num_abbrev_decls; |
| 113 |
/** |
| 114 |
* Pointer in .debug_abbrev for this CU. |
| 115 |
* |
| 116 |
* This is only used before indexing, then it is replaced by @c |
| 117 |
* abbrev_decls, @c num_abbrev_decls, and @c abbrev_insns. It is |
| 118 |
* a union with @c num_abbrev_decls rather than one of the other |
| 119 |
* two fields because that way we don't need to worry about |
| 120 |
* accidentally freeing it. |
| 121 |
*/ |
| 122 |
const char *pending_abbrev; |
| 123 |
}; |
| 124 |
/** |
| 125 |
* Buffer of @ref drgn_dwarf_index_abbrev_insn instructions for all |
| 126 |
* abbreviation codes. |
| 127 |
* |
| 128 |
* These are all stored in one array for cache locality. |
| 129 |
*/ |
| 130 |
uint8_t *abbrev_insns; |
| 131 |
/** |
| 132 |
* Pointer in `.debug_str_offsets` section to string offset entries for |
| 133 |
* this CU. |
| 134 |
*/ |
| 135 |
const char *str_offsets; |
| 136 |
/** libdw structure for this CU. */ |
| 137 |
Dwarf_CU *libdw_cu; |
| 138 |
}; |
| 139 |
|
| 140 |
/** Indexed CU lookup table entry. */ |
| 141 |
struct drgn_dwarf_index_cu_lookup { |
| 142 |
/** Address of CU data (@ref drgn_dwarf_index_cu::buf). */ |
| 143 |
uintptr_t buf; |
| 144 |
/** Index of CU in @ref drgn_dwarf_info::index_cus. */ |
| 145 |
size_t index; |
| 146 |
}; |
| 147 |
|
| 148 |
DEFINE_VECTOR_FUNCTIONS(drgn_dwarf_index_cu_vector); |
| 149 |
DEFINE_VECTOR(drgn_module_vector, struct drgn_module *); |
| 150 |
|
| 151 |
DEFINE_HASH_MAP_FUNCTIONS(drgn_dwarf_type_map, ptr_key_hash_pair, |
| 152 |
scalar_key_eq); |
| 153 |
|
| 154 |
static void |
| 155 |
drgn_namespace_dwarf_index_init(struct drgn_namespace_dwarf_index *dindex, |
| 156 |
const char *name, size_t name_len, |
| 157 |
struct drgn_namespace_dwarf_index *parent) |
| 158 |
{ |
| 159 |
dindex->dbinfo = parent->dbinfo; |
| 160 |
dindex->name = name; |
| 161 |
dindex->name_len = name_len; |
| 162 |
dindex->parent = parent; |
| 163 |
drgn_namespace_table_init(&dindex->children); |
| 164 |
array_for_each(tag_map, dindex->map) |
| 165 |
drgn_dwarf_index_die_map_init(tag_map); |
| 166 |
dindex->cus_indexed = 0; |
| 167 |
memset(dindex->dies_indexed, 0, sizeof(dindex->dies_indexed)); |
| 168 |
dindex->saved_err = NULL; |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
drgn_namespace_dwarf_index_deinit(struct drgn_namespace_dwarf_index *dindex) |
| 173 |
{ |
| 174 |
drgn_error_destroy(dindex->saved_err); |
| 175 |
array_for_each(tag_map, dindex->map) { |
| 176 |
hash_table_for_each(drgn_dwarf_index_die_map, it, tag_map) |
| 177 |
drgn_dwarf_index_die_vector_deinit(&it.entry->value); |
| 178 |
drgn_dwarf_index_die_map_deinit(tag_map); |
| 179 |
} |
| 180 |
hash_table_for_each(drgn_namespace_table, it, &dindex->children) { |
| 181 |
drgn_namespace_dwarf_index_deinit(*it.entry); |
| 182 |
free(*it.entry); |
| 183 |
} |
| 184 |
drgn_namespace_table_deinit(&dindex->children); |
| 185 |
} |
| 186 |
|
| 187 |
void drgn_dwarf_info_init(struct drgn_debug_info *dbinfo) |
| 188 |
{ |
| 189 |
dbinfo->dwarf.global.dbinfo = dbinfo; |
| 190 |
drgn_namespace_dwarf_index_init(&dbinfo->dwarf.global, "", 0, |
| 191 |
&dbinfo->dwarf.global); |
| 192 |
dbinfo->dwarf.global.parent = NULL; |
| 193 |
drgn_dwarf_base_type_map_init(&dbinfo->dwarf.base_types); |
| 194 |
drgn_dwarf_specification_map_init(&dbinfo->dwarf.specifications); |
| 195 |
free(dbinfo->dwarf.index_cu_lookup); |
| 196 |
drgn_dwarf_index_cu_vector_init(&dbinfo->dwarf.index_cus); |
| 197 |
drgn_dwarf_type_map_init(&dbinfo->dwarf.types); |
| 198 |
drgn_dwarf_type_map_init(&dbinfo->dwarf.cant_be_incomplete_array_types); |
| 199 |
} |
| 200 |
|
| 201 |
static void drgn_dwarf_index_cu_deinit(struct drgn_dwarf_index_cu *cu) |
| 202 |
{ |
| 203 |
free(cu->abbrev_insns); |
| 204 |
free(cu->abbrev_decls); |
| 205 |
} |
| 206 |
|
| 207 |
void drgn_dwarf_info_deinit(struct drgn_debug_info *dbinfo) |
| 208 |
{ |
| 209 |
drgn_dwarf_type_map_deinit(&dbinfo->dwarf.cant_be_incomplete_array_types); |
| 210 |
drgn_dwarf_type_map_deinit(&dbinfo->dwarf.types); |
| 211 |
vector_for_each(drgn_dwarf_index_cu_vector, cu, |
| 212 |
&dbinfo->dwarf.index_cus) |
| 213 |
drgn_dwarf_index_cu_deinit(cu); |
| 214 |
drgn_dwarf_index_cu_vector_deinit(&dbinfo->dwarf.index_cus); |
| 215 |
drgn_dwarf_specification_map_deinit(&dbinfo->dwarf.specifications); |
| 216 |
drgn_dwarf_base_type_map_deinit(&dbinfo->dwarf.base_types); |
| 217 |
drgn_namespace_dwarf_index_deinit(&dbinfo->dwarf.global); |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* Diagnostics. |
| 222 |
*/ |
| 223 |
|
| 224 |
/** Like @ref dw_tag_str(), but takes a @c Dwarf_Die. */ |
| 225 |
static const char *dwarf_tag_str(Dwarf_Die *die, char buf[DW_TAG_STR_BUF_LEN]) |
| 226 |
{ |
| 227 |
return dw_tag_str(dwarf_tag(die), buf); |
| 228 |
} |
| 229 |
|
| 230 |
static inline struct drgn_error *drgn_check_address_size(uint8_t address_size) |
| 231 |
{ |
| 232 |
if (address_size < 1 || address_size > 8) { |
| 233 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 234 |
"unsupported address size %" PRIu8, |
| 235 |
address_size); |
| 236 |
} |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
* Indexing. |
| 242 |
* |
| 243 |
* A core part of debugger functionality is looking up types, variables, etc. by |
| 244 |
* name. DWARF information can be very large, so scanning through all of it for |
| 245 |
* every lookup would be too slow. Instead, when we load debugging information, |
| 246 |
* we build an index of DIEs by name. |
| 247 |
* |
| 248 |
* This indexing step is parallelized and highly optimized. It is implemented as |
| 249 |
* a bespoke DWARF parser specialized for the task of scanning over DIEs |
| 250 |
* quickly. |
| 251 |
* |
| 252 |
* Although the DWARF standard defines ".debug_pubnames" and ".debug_names" |
| 253 |
* sections, GCC and Clang currently don't emit them by default, so we don't use |
| 254 |
* them. |
| 255 |
* |
| 256 |
* Every namespace has a separate index (@ref drgn_namespace_dwarf_index). The |
| 257 |
* global namespace is indexed immediately upon loading debugging information. |
| 258 |
* Other namespaces are indexed when they are first accessed. |
| 259 |
*/ |
| 260 |
|
| 261 |
/** |
| 262 |
* DWARF abbreviation table instructions. |
| 263 |
* |
| 264 |
* The DWARF abbreviation table can be large and contains more information than |
| 265 |
* is strictly necessary for indexing. So, we translate the table into a series |
| 266 |
* of instructions which specify how to process a DIE. This instruction stream |
| 267 |
* omits unnecessary information and is more compact (and thus more cache |
| 268 |
* friendly), which is important for the tight DIE parsing loop. |
| 269 |
*/ |
| 270 |
enum drgn_dwarf_index_abbrev_insn { |
| 271 |
// Instructions > 0 and <= INSN_MAX_SKIP indicate a number of bytes to |
| 272 |
// be skipped over. |
| 273 |
INSN_MAX_SKIP = 219, |
| 274 |
|
| 275 |
// These instructions indicate an attribute that can be skipped over. |
| 276 |
INSN_SKIP_BLOCK, |
| 277 |
INSN_SKIP_BLOCK1, |
| 278 |
INSN_SKIP_BLOCK2, |
| 279 |
INSN_SKIP_BLOCK4, |
| 280 |
INSN_SKIP_LEB128, |
| 281 |
INSN_SKIP_STRING, |
| 282 |
|
| 283 |
// These instructions indicate an attribute that should be parsed. |
| 284 |
INSN_SIBLING_REF1, |
| 285 |
INSN_SIBLING_REF2, |
| 286 |
INSN_SIBLING_REF4, |
| 287 |
INSN_SIBLING_REF8, |
| 288 |
INSN_SIBLING_REF_UDATA, |
| 289 |
INSN_NAME_STRP4, |
| 290 |
INSN_NAME_STRP8, |
| 291 |
INSN_NAME_STRING, |
| 292 |
INSN_NAME_STRX, |
| 293 |
INSN_NAME_STRX1, |
| 294 |
INSN_NAME_STRX2, |
| 295 |
INSN_NAME_STRX3, |
| 296 |
INSN_NAME_STRX4, |
| 297 |
INSN_NAME_STRP_ALT4, |
| 298 |
INSN_NAME_STRP_ALT8, |
| 299 |
INSN_DECLARATION_FLAG, |
| 300 |
// "Specification" is overloaded to mean DW_AT_specification, |
| 301 |
// DW_AT_abstract_origin, or DW_AT_import. |
| 302 |
INSN_SPECIFICATION_REF1, |
| 303 |
INSN_SPECIFICATION_REF2, |
| 304 |
INSN_SPECIFICATION_REF4, |
| 305 |
INSN_SPECIFICATION_REF8, |
| 306 |
INSN_SPECIFICATION_REF_UDATA, |
| 307 |
INSN_SPECIFICATION_REF_ADDR4, |
| 308 |
INSN_SPECIFICATION_REF_ADDR8, |
| 309 |
INSN_SPECIFICATION_REF_ALT4, |
| 310 |
INSN_SPECIFICATION_REF_ALT8, |
| 311 |
INSN_INDIRECT, |
| 312 |
INSN_SIBLING_INDIRECT, |
| 313 |
INSN_NAME_INDIRECT, |
| 314 |
INSN_DECLARATION_INDIRECT, |
| 315 |
INSN_SPECIFICATION_INDIRECT, |
| 316 |
|
| 317 |
NUM_INSNS, |
| 318 |
|
| 319 |
// Every sequence of instructions for a DIE is terminated by a zero |
| 320 |
// byte. |
| 321 |
INSN_END = 0, |
| 322 |
|
| 323 |
// The byte after INSN_END contains the DIE flags, which are a bitmask |
| 324 |
// of flags combined with the tag (either a drgn_dwarf_index_tag or one |
| 325 |
// of the special INSN_DIE_TAG_ tags below). |
| 326 |
INSN_DIE_FLAG_TAG_MASK = 0x1f, |
| 327 |
|
| 328 |
// Tags that need special handling but don't need to be indexed |
| 329 |
// themselves. |
| 330 |
INSN_DIE_TAG_imported_unit = DRGN_DWARF_INDEX_NUM_TAGS, |
| 331 |
INSN_DIE_NUM_TAGS, |
| 332 |
|
| 333 |
// DIE is DW_TAG_subprogram with no DW_AT_low_pc or DW_AT_ranges. |
| 334 |
INSN_DIE_FLAG_SUBPROGRAM_NO_PC = 0x20, |
| 335 |
// DIE is a declaration. |
| 336 |
INSN_DIE_FLAG_DECLARATION = 0x40, |
| 337 |
// DIE has children. |
| 338 |
INSN_DIE_FLAG_CHILDREN = 0x80, |
| 339 |
}; |
| 340 |
|
| 341 |
// We use INSN_DIE_FLAG_TAG_MASK as a sentinel when the DIE shouldn't be |
| 342 |
// indexed, so this is < and not <=. |
| 343 |
static_assert((int)INSN_DIE_NUM_TAGS < (int)INSN_DIE_FLAG_TAG_MASK, |
| 344 |
"too many instruction DIE tags"); |
| 345 |
|
| 346 |
// Instructions are 8 bits. |
| 347 |
static_assert(NUM_INSNS - 1 == UINT8_MAX, |
| 348 |
"maximum DWARF index instruction is invalid"); |
| 349 |
|
| 350 |
DEFINE_VECTOR(uint8_vector, uint8_t); |
| 351 |
DEFINE_VECTOR(uint32_vector, uint32_t); |
| 352 |
DEFINE_VECTOR(uint64_vector, uint64_t); |
| 353 |
|
| 354 |
struct drgn_dwarf_index_cu_buffer { |
| 355 |
struct binary_buffer bb; |
| 356 |
struct drgn_dwarf_index_cu *cu; |
| 357 |
// Depth of current DIE relative to starting DIE, which has depth 0. |
| 358 |
unsigned int depth; |
| 359 |
}; |
| 360 |
|
| 361 |
static struct drgn_error * |
| 362 |
drgn_dwarf_index_cu_buffer_error(struct binary_buffer *bb, const char *pos, |
| 363 |
const char *message) |
| 364 |
{ |
| 365 |
struct drgn_dwarf_index_cu_buffer *buffer = |
| 366 |
container_of(bb, struct drgn_dwarf_index_cu_buffer, bb); |
| 367 |
return drgn_elf_file_section_error(buffer->cu->file, |
| 368 |
buffer->cu->file->scns[buffer->cu->scn], |
| 369 |
buffer->cu->file->scn_data[buffer->cu->scn], |
| 370 |
pos, message); |
| 371 |
} |
| 372 |
|
| 373 |
static void |
| 374 |
drgn_dwarf_index_cu_buffer_init(struct drgn_dwarf_index_cu_buffer *buffer, |
| 375 |
struct drgn_dwarf_index_cu *cu) |
| 376 |
{ |
| 377 |
binary_buffer_init(&buffer->bb, cu->buf, cu->len, |
| 378 |
drgn_elf_file_is_little_endian(cu->file), |
| 379 |
drgn_dwarf_index_cu_buffer_error); |
| 380 |
buffer->cu = cu; |
| 381 |
buffer->depth = 0; |
| 382 |
} |
| 383 |
|
| 384 |
// Returns NULL if die_addr is not from an indexed CU. |
| 385 |
static struct drgn_dwarf_index_cu * |
| 386 |
drgn_dwarf_index_find_cu(struct drgn_debug_info *dbinfo, uintptr_t die_addr) |
| 387 |
{ |
| 388 |
struct drgn_dwarf_index_cu_lookup *lookup = |
| 389 |
dbinfo->dwarf.index_cu_lookup; |
| 390 |
#define less_than_cu_lookup_buf(a, b) (*(a) < (b)->buf) |
| 391 |
size_t i = binary_search_gt(lookup, |
| 392 |
drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus), |
| 393 |
&die_addr, less_than_cu_lookup_buf); |
| 394 |
#undef less_than_cu_buf |
| 395 |
if (i == 0) |
| 396 |
return NULL; |
| 397 |
struct drgn_dwarf_index_cu *cu = |
| 398 |
drgn_dwarf_index_cu_vector_at(&dbinfo->dwarf.index_cus, |
| 399 |
lookup[i - 1].index); |
| 400 |
if (die_addr - lookup[i - 1].buf >= cu->len) |
| 401 |
return NULL; |
| 402 |
return cu; |
| 403 |
} |
| 404 |
|
| 405 |
static const char *drgn_dwarf_dwo_name(Dwarf_Die *die) |
| 406 |
{ |
| 407 |
Dwarf_Attribute attr_mem, *attr; |
| 408 |
if ((attr = dwarf_attr(die, DW_AT_dwo_name, &attr_mem)) |
| 409 |
|| (attr = dwarf_attr(die, DW_AT_GNU_dwo_name, &attr_mem))) |
| 410 |
return dwarf_formstring(attr); |
| 411 |
return NULL; |
| 412 |
} |
| 413 |
|
| 414 |
static struct drgn_error * |
| 415 |
drgn_dwarf_index_read_file(struct drgn_elf_file *file, |
| 416 |
struct drgn_dwarf_index_cu_vector *cus, |
| 417 |
struct drgn_dwarf_index_cu_vector *partial_units); |
| 418 |
|
| 419 |
static struct drgn_error * |
| 420 |
drgn_dwarf_index_read_cus(struct drgn_elf_file *file, |
| 421 |
enum drgn_section_index scn, |
| 422 |
struct drgn_dwarf_index_cu_vector *cus, |
| 423 |
struct drgn_dwarf_index_cu_vector *partial_units) |
| 424 |
{ |
| 425 |
struct drgn_error *err; |
| 426 |
|
| 427 |
Dwarf *dwarf; |
| 428 |
err = drgn_elf_file_get_dwarf(file, &dwarf); |
| 429 |
if (err) |
| 430 |
return err; |
| 431 |
Dwarf_Off off, next_off; |
| 432 |
size_t header_size; |
| 433 |
Dwarf_Half version; |
| 434 |
Dwarf_Off abbrev_offset; |
| 435 |
uint8_t address_size; |
| 436 |
uint8_t offset_size; |
| 437 |
uint64_t v4_type_signature; |
| 438 |
uint64_t *v4_type_signaturep = |
| 439 |
scn == DRGN_SCN_DEBUG_TYPES ? &v4_type_signature : NULL; |
| 440 |
int ret; |
| 441 |
for (off = 0; |
| 442 |
(ret = dwarf_next_unit(dwarf, off, &next_off, &header_size, |
| 443 |
&version, &abbrev_offset, &address_size, |
| 444 |
&offset_size, v4_type_signaturep, |
| 445 |
NULL)) == 0; |
| 446 |
off = next_off) { |
| 447 |
Dwarf_Die cudie; |
| 448 |
if (scn == DRGN_SCN_DEBUG_TYPES) { |
| 449 |
if (!dwarf_offdie_types(dwarf, off + header_size, |
| 450 |
&cudie)) |
| 451 |
return drgn_error_libdw(); |
| 452 |
} else { |
| 453 |
if (!dwarf_offdie(dwarf, off + header_size, &cudie)) |
| 454 |
return drgn_error_libdw(); |
| 455 |
} |
| 456 |
uint8_t unit_type; |
| 457 |
#if _ELFUTILS_PREREQ(0, 171) |
| 458 |
Dwarf_Die subdie; |
| 459 |
if (dwarf_cu_info(cudie.cu, NULL, &unit_type, &cudie, &subdie, |
| 460 |
NULL, NULL, NULL)) |
| 461 |
return drgn_error_libdw(); |
| 462 |
|
| 463 |
if (unit_type == DW_UT_skeleton && subdie.cu) { |
| 464 |
Dwarf *split_dwarf = dwarf_cu_getdwarf(subdie.cu); |
| 465 |
struct drgn_elf_file *split_file = |
| 466 |
drgn_module_find_dwarf_file(file->module, |
| 467 |
split_dwarf); |
| 468 |
if (!split_file) { |
| 469 |
const char *dwo_name = |
| 470 |
drgn_dwarf_dwo_name(&cudie); |
| 471 |
if (!dwo_name) |
| 472 |
dwo_name = ""; |
| 473 |
err = drgn_module_create_split_dwarf_file(file->module, |
| 474 |
dwo_name, |
| 475 |
split_dwarf, |
| 476 |
&split_file); |
| 477 |
if (err) |
| 478 |
return err; |
| 479 |
err = drgn_dwarf_index_read_file(split_file, |
| 480 |
cus, |
| 481 |
partial_units); |
| 482 |
if (err) |
| 483 |
return err; |
| 484 |
} |
| 485 |
continue; |
| 486 |
} else if (unit_type == DW_UT_skeleton) { |
| 487 |
if (drgn_log_is_enabled(file->module->prog, |
| 488 |
DRGN_LOG_WARNING)) { |
| 489 |
const char *dwo_name = |
| 490 |
drgn_dwarf_dwo_name(&cudie); |
| 491 |
drgn_log_warning(file->module->prog, |
| 492 |
"%s: split DWARF file%s%s not found", |
| 493 |
file->path ?: "", |
| 494 |
dwo_name ? " " : "", |
| 495 |
dwo_name ? dwo_name : ""); |
| 496 |
} |
| 497 |
continue; |
| 498 |
} else { |
| 499 |
Dwarf_Off dwp_offset; |
| 500 |
if (dwarf_cu_dwp_section_info(cudie.cu, DW_SECT_ABBREV, |
| 501 |
&dwp_offset, NULL)) |
| 502 |
return drgn_error_libdw(); |
| 503 |
abbrev_offset += dwp_offset; |
| 504 |
} |
| 505 |
#else |
| 506 |
switch (dwarf_tag(&cudie)) { |
| 507 |
case DW_TAG_type_unit: |
| 508 |
unit_type = DW_UT_type; |
| 509 |
break; |
| 510 |
case DW_TAG_partial_unit: |
| 511 |
unit_type = DW_UT_partial; |
| 512 |
break; |
| 513 |
default: |
| 514 |
unit_type = DW_UT_compile; |
| 515 |
break; |
| 516 |
} |
| 517 |
#endif |
| 518 |
|
| 519 |
if (!elf_data_contains_ptr(file->scn_data[scn], |
| 520 |
cudie.addr)) { |
| 521 |
return drgn_elf_file_section_error(file, NULL, NULL, |
| 522 |
cudie.addr, |
| 523 |
"unit DIE from unexpected section"); |
| 524 |
} |
| 525 |
const char *cu_buf = (char *)file->scn_data[scn]->d_buf + off; |
| 526 |
if (version < 2 || version > 5) { |
| 527 |
return drgn_elf_file_section_errorf(file, |
| 528 |
file->scns[scn], |
| 529 |
file->scn_data[scn], |
| 530 |
cu_buf, |
| 531 |
"unknown DWARF unit version %" PRIu16, |
| 532 |
version); |
| 533 |
} |
| 534 |
if (address_size > 8) { |
| 535 |
return drgn_elf_file_section_errorf(file, |
| 536 |
file->scns[scn], |
| 537 |
file->scn_data[scn], |
| 538 |
cu_buf, |
| 539 |
"unsupported DWARF unit address size %" PRIu8, |
| 540 |
address_size); |
| 541 |
} |
| 542 |
|
| 543 |
Elf_Data *debug_abbrev = file->scn_data[DRGN_SCN_DEBUG_ABBREV]; |
| 544 |
if (abbrev_offset > debug_abbrev->d_size) { |
| 545 |
return drgn_elf_file_section_error(file, |
| 546 |
file->scns[scn], |
| 547 |
file->scn_data[scn], |
| 548 |
cu_buf, |
| 549 |
"debug_abbrev_offset is out of bounds"); |
| 550 |
} |
| 551 |
const char *pending_abbrev = |
| 552 |
(char *)debug_abbrev->d_buf + abbrev_offset; |
| 553 |
|
| 554 |
Elf_Data *debug_str_offsets = |
| 555 |
file->scn_data[DRGN_SCN_DEBUG_STR_OFFSETS]; |
| 556 |
const char *str_offsets = NULL; |
| 557 |
if (debug_str_offsets) { |
| 558 |
Dwarf_Word str_offsets_base; |
| 559 |
if (version >= 5) { |
| 560 |
Dwarf_Attribute attr_mem, *attr; |
| 561 |
if ((attr = dwarf_attr(&cudie, |
| 562 |
DW_AT_str_offsets_base, |
| 563 |
&attr_mem))) { |
| 564 |
if (dwarf_formudata(attr, |
| 565 |
&str_offsets_base)) |
| 566 |
return drgn_error_libdw(); |
| 567 |
} else { |
| 568 |
// The default str_offsets_base is the |
| 569 |
// first entry in .debug_str_offsets |
| 570 |
// after the first header. (This isn't |
| 571 |
// explicit in the DWARF 5 |
| 572 |
// specification, but it seems to be the |
| 573 |
// consensus.) |
| 574 |
str_offsets_base = 2 * offset_size; |
| 575 |
} |
| 576 |
} else { |
| 577 |
// GNU Debug Fission doesn't have |
| 578 |
// DW_AT_str_offsets_base; the base is always 0. |
| 579 |
str_offsets_base = 0; |
| 580 |
} |
| 581 |
Dwarf_Off dwp_offset; |
| 582 |
if (dwarf_cu_dwp_section_info(cudie.cu, |
| 583 |
DW_SECT_STR_OFFSETS, |
| 584 |
&dwp_offset, NULL)) |
| 585 |
return drgn_error_libdw(); |
| 586 |
str_offsets_base += dwp_offset; |
| 587 |
if (str_offsets_base > debug_str_offsets->d_size) { |
| 588 |
return drgn_elf_file_section_error(file, |
| 589 |
file->scns[scn], |
| 590 |
file->scn_data[scn], |
| 591 |
cudie.addr, |
| 592 |
".debug_str_offsets base is out of bounds"); |
| 593 |
} |
| 594 |
str_offsets = |
| 595 |
(char *)debug_str_offsets->d_buf |
| 596 |
+ str_offsets_base; |
| 597 |
} |
| 598 |
|
| 599 |
struct drgn_dwarf_index_cu *cu = |
| 600 |
drgn_dwarf_index_cu_vector_append_entry(unit_type == DW_UT_partial |
| 601 |
? partial_units : cus); |
| 602 |
if (!cu) |
| 603 |
return &drgn_enomem; |
| 604 |
*cu = (struct drgn_dwarf_index_cu){ |
| 605 |
.file = file, |
| 606 |
.buf = cu_buf, |
| 607 |
.len = min(next_off, (Dwarf_Off)file->scn_data[scn]->d_size) - off, |
| 608 |
.version = version, |
| 609 |
.unit_type = unit_type, |
| 610 |
.address_size = address_size, |
| 611 |
.is_64_bit = offset_size == 8, |
| 612 |
.scn = scn, |
| 613 |
.pending_abbrev = pending_abbrev, |
| 614 |
.str_offsets = str_offsets, |
| 615 |
.libdw_cu = cudie.cu, |
| 616 |
}; |
| 617 |
} |
| 618 |
if (ret < 0) |
| 619 |
return drgn_error_libdw(); |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
|
| 623 |
static struct drgn_error * |
| 624 |
drgn_dwarf_index_read_file(struct drgn_elf_file *file, |
| 625 |
struct drgn_dwarf_index_cu_vector *cus, |
| 626 |
struct drgn_dwarf_index_cu_vector *partial_units) |
| 627 |
{ |
| 628 |
struct drgn_error *err; |
| 629 |
|
| 630 |
for (int scn = 0; scn < DRGN_SECTION_INDEX_NUM_DWARF_INDEX; scn++) { |
| 631 |
if (file->scns[scn]) { |
| 632 |
Elf_Data *data; |
| 633 |
err = drgn_elf_file_read_section(file, scn, &data); |
| 634 |
if (err) |
| 635 |
return err; |
| 636 |
} |
| 637 |
} |
| 638 |
err = drgn_dwarf_index_read_cus(file, DRGN_SCN_DEBUG_INFO, cus, |
| 639 |
partial_units); |
| 640 |
if (err) |
| 641 |
return err; |
| 642 |
if (file->scns[DRGN_SCN_DEBUG_TYPES]) { |
| 643 |
err = drgn_dwarf_index_read_cus(file, DRGN_SCN_DEBUG_TYPES, |
| 644 |
cus, partial_units); |
| 645 |
if (err) |
| 646 |
return err; |
| 647 |
} |
| 648 |
if (file == file->module->debug_file |
| 649 |
&& file->module->supplementary_debug_file) { |
| 650 |
err = drgn_dwarf_index_read_file(file->module->supplementary_debug_file, |
| 651 |
cus, partial_units); |
| 652 |
if (err) |
| 653 |
return err; |
| 654 |
file->alt_debug_info_data = |
| 655 |
file->module->supplementary_debug_file->scn_data[DRGN_SCN_DEBUG_INFO]; |
| 656 |
file->alt_debug_str_data = |
| 657 |
file->module->supplementary_debug_file->scn_data[DRGN_SCN_DEBUG_STR]; |
| 658 |
} |
| 659 |
return err; |
| 660 |
} |
| 661 |
|
| 662 |
static struct drgn_error *read_strx(struct drgn_dwarf_index_cu_buffer *buffer, |
| 663 |
uint64_t strx, const char **ret) |
| 664 |
{ |
| 665 |
if (!buffer->cu->str_offsets) { |
| 666 |
return binary_buffer_error(&buffer->bb, |
| 667 |
"string index without .debug_str_offsets section"); |
| 668 |
} |
| 669 |
Elf_Data *debug_str_offsets = |
| 670 |
buffer->cu->file->scn_data[DRGN_SCN_DEBUG_STR_OFFSETS]; |
| 671 |
size_t offset_size = buffer->cu->is_64_bit ? 8 : 4; |
| 672 |
if (((char *)debug_str_offsets->d_buf + debug_str_offsets->d_size |
| 673 |
- buffer->cu->str_offsets) |
| 674 |
/ offset_size <= strx) { |
| 675 |
return binary_buffer_error(&buffer->bb, |
| 676 |
"string index out of bounds"); |
| 677 |
} |
| 678 |
uint64_t strp; |
| 679 |
if (buffer->cu->is_64_bit) { |
| 680 |
memcpy(&strp, (uint64_t *)buffer->cu->str_offsets + strx, |
| 681 |
sizeof(strp)); |
| 682 |
if (buffer->bb.bswap) |
| 683 |
strp = bswap_64(strp); |
| 684 |
} else { |
| 685 |
uint32_t strp32; |
| 686 |
memcpy(&strp32, (uint32_t *)buffer->cu->str_offsets + strx, |
| 687 |
sizeof(strp32)); |
| 688 |
if (buffer->bb.bswap) |
| 689 |
strp32 = bswap_32(strp32); |
| 690 |
strp = strp32; |
| 691 |
} |
| 692 |
if (strp >= buffer->cu->file->scn_data[DRGN_SCN_DEBUG_STR]->d_size) { |
| 693 |
return binary_buffer_error(&buffer->bb, |
| 694 |
"indirect string is out of bounds"); |
| 695 |
} |
| 696 |
*ret = ((char *)buffer->cu->file->scn_data[DRGN_SCN_DEBUG_STR]->d_buf |
| 697 |
+ strp); |
| 698 |
return NULL; |
| 699 |
} |
| 700 |
|
| 701 |
static struct drgn_error *dw_form_to_insn(struct drgn_dwarf_index_cu *cu, |
| 702 |
struct binary_buffer *bb, |
| 703 |
uint64_t form, uint8_t *insn_ret) |
| 704 |
{ |
| 705 |
struct drgn_error *err; |
| 706 |
switch (form) { |
| 707 |
case DW_FORM_addr: |
| 708 |
*insn_ret = cu->address_size; |
| 709 |
return NULL; |
| 710 |
case DW_FORM_data1: |
| 711 |
case DW_FORM_ref1: |
| 712 |
case DW_FORM_flag: |
| 713 |
case DW_FORM_strx1: |
| 714 |
case DW_FORM_addrx1: |
| 715 |
*insn_ret = 1; |
| 716 |
return NULL; |
| 717 |
case DW_FORM_data2: |
| 718 |
case DW_FORM_ref2: |
| 719 |
case DW_FORM_strx2: |
| 720 |
case DW_FORM_addrx2: |
| 721 |
*insn_ret = 2; |
| 722 |
return NULL; |
| 723 |
case DW_FORM_strx3: |
| 724 |
case DW_FORM_addrx3: |
| 725 |
*insn_ret = 3; |
| 726 |
return NULL; |
| 727 |
case DW_FORM_data4: |
| 728 |
case DW_FORM_ref4: |
| 729 |
case DW_FORM_ref_sup4: |
| 730 |
case DW_FORM_strx4: |
| 731 |
case DW_FORM_addrx4: |
| 732 |
*insn_ret = 4; |
| 733 |
return NULL; |
| 734 |
case DW_FORM_data8: |
| 735 |
case DW_FORM_ref8: |
| 736 |
case DW_FORM_ref_sig8: |
| 737 |
case DW_FORM_ref_sup8: |
| 738 |
*insn_ret = 8; |
| 739 |
return NULL; |
| 740 |
case DW_FORM_data16: |
| 741 |
*insn_ret = 16; |
| 742 |
return NULL; |
| 743 |
case DW_FORM_block: |
| 744 |
case DW_FORM_exprloc: |
| 745 |
*insn_ret = INSN_SKIP_BLOCK; |
| 746 |
return NULL; |
| 747 |
case DW_FORM_block1: |
| 748 |
*insn_ret = INSN_SKIP_BLOCK1; |
| 749 |
return NULL; |
| 750 |
case DW_FORM_block2: |
| 751 |
*insn_ret = INSN_SKIP_BLOCK2; |
| 752 |
return NULL; |
| 753 |
case DW_FORM_block4: |
| 754 |
*insn_ret = INSN_SKIP_BLOCK4; |
| 755 |
return NULL; |
| 756 |
case DW_FORM_sdata: |
| 757 |
case DW_FORM_udata: |
| 758 |
case DW_FORM_ref_udata: |
| 759 |
case DW_FORM_strx: |
| 760 |
case DW_FORM_addrx: |
| 761 |
case DW_FORM_loclistx: |
| 762 |
case DW_FORM_rnglistx: |
| 763 |
case DW_FORM_GNU_addr_index: |
| 764 |
case DW_FORM_GNU_str_index: |
| 765 |
*insn_ret = INSN_SKIP_LEB128; |
| 766 |
return NULL; |
| 767 |
case DW_FORM_ref_addr: |
| 768 |
if (cu->version < 3) { |
| 769 |
*insn_ret = cu->address_size; |
| 770 |
return NULL; |
| 771 |
} |
| 772 |
fallthrough; |
| 773 |
case DW_FORM_sec_offset: |
| 774 |
case DW_FORM_strp: |
| 775 |
case DW_FORM_strp_sup: |
| 776 |
case DW_FORM_line_strp: |
| 777 |
case DW_FORM_GNU_ref_alt: |
| 778 |
case DW_FORM_GNU_strp_alt: |
| 779 |
*insn_ret = cu->is_64_bit ? 8 : 4; |
| 780 |
return NULL; |
| 781 |
case DW_FORM_string: |
| 782 |
*insn_ret = INSN_SKIP_STRING; |
| 783 |
return NULL; |
| 784 |
case DW_FORM_implicit_const: |
| 785 |
if ((err = binary_buffer_skip_leb128(bb))) |
| 786 |
return err; |
| 787 |
fallthrough; |
| 788 |
case DW_FORM_flag_present: |
| 789 |
*insn_ret = 0; |
| 790 |
return NULL; |
| 791 |
case DW_FORM_indirect: |
| 792 |
*insn_ret = INSN_INDIRECT; |
| 793 |
return NULL; |
| 794 |
default: |
| 795 |
return binary_buffer_error(bb, |
| 796 |
"unknown attribute form %#" PRIx64, |
| 797 |
form); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
static struct drgn_error *dw_at_sibling_to_insn(struct binary_buffer *bb, |
| 802 |
uint64_t form, |
| 803 |
uint8_t *insn_ret) |
| 804 |
{ |
| 805 |
switch (form) { |
| 806 |
case DW_FORM_ref1: |
| 807 |
*insn_ret = INSN_SIBLING_REF1; |
| 808 |
return NULL; |
| 809 |
case DW_FORM_ref2: |
| 810 |
*insn_ret = INSN_SIBLING_REF2; |
| 811 |
return NULL; |
| 812 |
case DW_FORM_ref4: |
| 813 |
*insn_ret = INSN_SIBLING_REF4; |
| 814 |
return NULL; |
| 815 |
case DW_FORM_ref8: |
| 816 |
*insn_ret = INSN_SIBLING_REF8; |
| 817 |
return NULL; |
| 818 |
case DW_FORM_ref_udata: |
| 819 |
*insn_ret = INSN_SIBLING_REF_UDATA; |
| 820 |
return NULL; |
| 821 |
case DW_FORM_indirect: |
| 822 |
*insn_ret = INSN_SIBLING_INDIRECT; |
| 823 |
return NULL; |
| 824 |
default: |
| 825 |
return binary_buffer_error(bb, |
| 826 |
"unknown attribute form %#" PRIx64 " for DW_AT_sibling", |
| 827 |
form); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
static struct drgn_error *dw_at_name_to_insn(struct drgn_dwarf_index_cu *cu, |
| 832 |
struct binary_buffer *bb, |
| 833 |
uint64_t form, uint8_t *insn_ret) |
| 834 |
{ |
| 835 |
switch (form) { |
| 836 |
case DW_FORM_strp: |
| 837 |
if (!cu->file->scn_data[DRGN_SCN_DEBUG_STR]) { |
| 838 |
return binary_buffer_error(bb, |
| 839 |
"DW_FORM_strp without .debug_str section"); |
| 840 |
} |
| 841 |
if (cu->is_64_bit) |
| 842 |
*insn_ret = INSN_NAME_STRP8; |
| 843 |
else |
| 844 |
*insn_ret = INSN_NAME_STRP4; |
| 845 |
return NULL; |
| 846 |
case DW_FORM_string: |
| 847 |
*insn_ret = INSN_NAME_STRING; |
| 848 |
return NULL; |
| 849 |
case DW_FORM_strx: |
| 850 |
case DW_FORM_GNU_str_index: |
| 851 |
*insn_ret = INSN_NAME_STRX; |
| 852 |
return NULL; |
| 853 |
case DW_FORM_strx1: |
| 854 |
*insn_ret = INSN_NAME_STRX1; |
| 855 |
return NULL; |
| 856 |
case DW_FORM_strx2: |
| 857 |
*insn_ret = INSN_NAME_STRX2; |
| 858 |
return NULL; |
| 859 |
case DW_FORM_strx3: |
| 860 |
*insn_ret = INSN_NAME_STRX3; |
| 861 |
return NULL; |
| 862 |
case DW_FORM_strx4: |
| 863 |
*insn_ret = INSN_NAME_STRX4; |
| 864 |
return NULL; |
| 865 |
case DW_FORM_GNU_strp_alt: |
| 866 |
if (!cu->file->alt_debug_str_data) { |
| 867 |
return binary_buffer_error(bb, |
| 868 |
"DW_FORM_GNU_strp_alt without alternate .debug_str section"); |
| 869 |
} |
| 870 |
if (cu->is_64_bit) |
| 871 |
*insn_ret = INSN_NAME_STRP_ALT8; |
| 872 |
else |
| 873 |
*insn_ret = INSN_NAME_STRP_ALT4; |
| 874 |
return NULL; |
| 875 |
case DW_FORM_indirect: |
| 876 |
*insn_ret = INSN_NAME_INDIRECT; |
| 877 |
return NULL; |
| 878 |
default: |
| 879 |
return binary_buffer_error(bb, |
| 880 |
"unknown attribute form %#" PRIx64 " for DW_AT_name", |
| 881 |
form); |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
static struct drgn_error * |
| 886 |
dw_at_declaration_to_insn(struct binary_buffer *bb, uint64_t form, |
| 887 |
uint8_t *insn_ret, uint8_t *die_flags) |
| 888 |
{ |
| 889 |
switch (form) { |
| 890 |
case DW_FORM_flag: |
| 891 |
*insn_ret = INSN_DECLARATION_FLAG; |
| 892 |
return NULL; |
| 893 |
case DW_FORM_flag_present: |
| 894 |
/* |
| 895 |
* This could be an instruction, but as long as we have a free |
| 896 |
* DIE flag bit, we might as well use it. |
| 897 |
*/ |
| 898 |
*insn_ret = 0; |
| 899 |
*die_flags |= INSN_DIE_FLAG_DECLARATION; |
| 900 |
return NULL; |
| 901 |
case DW_FORM_indirect: |
| 902 |
*insn_ret = INSN_DECLARATION_INDIRECT; |
| 903 |
return NULL; |
| 904 |
default: |
| 905 |
return binary_buffer_error(bb, |
| 906 |
"unknown attribute form %#" PRIx64 " for DW_AT_declaration", |
| 907 |
form); |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
static struct drgn_error * |
| 912 |
dw_at_specification_to_insn(struct drgn_dwarf_index_cu *cu, |
| 913 |
struct binary_buffer *bb, uint64_t form, |
| 914 |
uint8_t *insn_ret) |
| 915 |
{ |
| 916 |
switch (form) { |
| 917 |
case DW_FORM_ref1: |
| 918 |
*insn_ret = INSN_SPECIFICATION_REF1; |
| 919 |
return NULL; |
| 920 |
case DW_FORM_ref2: |
| 921 |
*insn_ret = INSN_SPECIFICATION_REF2; |
| 922 |
return NULL; |
| 923 |
case DW_FORM_ref4: |
| 924 |
*insn_ret = INSN_SPECIFICATION_REF4; |
| 925 |
return NULL; |
| 926 |
case DW_FORM_ref8: |
| 927 |
*insn_ret = INSN_SPECIFICATION_REF8; |
| 928 |
return NULL; |
| 929 |
case DW_FORM_ref_udata: |
| 930 |
*insn_ret = INSN_SPECIFICATION_REF_UDATA; |
| 931 |
return NULL; |
| 932 |
case DW_FORM_ref_addr: |
| 933 |
if (cu->version >= 3) { |
| 934 |
if (cu->is_64_bit) |
| 935 |
*insn_ret = INSN_SPECIFICATION_REF_ADDR8; |
| 936 |
else |
| 937 |
*insn_ret = INSN_SPECIFICATION_REF_ADDR4; |
| 938 |
} else { |
| 939 |
if (cu->address_size == 8) |
| 940 |
*insn_ret = INSN_SPECIFICATION_REF_ADDR8; |
| 941 |
else if (cu->address_size == 4) |
| 942 |
*insn_ret = INSN_SPECIFICATION_REF_ADDR4; |
| 943 |
else |
| 944 |
return binary_buffer_error(bb, |
| 945 |
"unsupported address size %" PRIu8 " for DW_FORM_ref_addr", |
| 946 |
cu->address_size); |
| 947 |
} |
| 948 |
return NULL; |
| 949 |
case DW_FORM_GNU_ref_alt: |
| 950 |
if (!cu->file->alt_debug_info_data) { |
| 951 |
return binary_buffer_error(bb, |
| 952 |
"DW_FORM_GNU_ref_alt without alternate .debug_info section"); |
| 953 |
} |
| 954 |
if (cu->is_64_bit) |
| 955 |
*insn_ret = INSN_SPECIFICATION_REF_ALT8; |
| 956 |
else |
| 957 |
*insn_ret = INSN_SPECIFICATION_REF_ALT4; |
| 958 |
return NULL; |
| 959 |
case DW_FORM_indirect: |
| 960 |
*insn_ret = INSN_SPECIFICATION_INDIRECT; |
| 961 |
return NULL; |
| 962 |
default: |
| 963 |
return binary_buffer_error(bb, |
| 964 |
"unknown attribute form %#" PRIx64 " for DW_AT_specification, DW_AT_abstract_origin, or DW_AT_import", |
| 965 |
form); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
static struct drgn_error * |
| 970 |
read_abbrev_decl(struct drgn_elf_file_section_buffer *buffer, |
| 971 |
struct drgn_dwarf_index_cu *cu, struct uint32_vector *decls, |
| 972 |
struct uint8_vector *insns) |
| 973 |
{ |
| 974 |
struct drgn_error *err; |
| 975 |
|
| 976 |
uint64_t code; |
| 977 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &code))) |
| 978 |
return err; |
| 979 |
if (code == 0) |
| 980 |
return &drgn_stop; |
| 981 |
if (code != uint32_vector_size(decls) + 1) { |
| 982 |
return binary_buffer_error(&buffer->bb, |
| 983 |
"DWARF abbreviation table is not sequential"); |
| 984 |
} |
| 985 |
|
| 986 |
uint32_t insn_index = uint8_vector_size(insns); |
| 987 |
if (!uint32_vector_append(decls, &insn_index)) |
| 988 |
return &drgn_enomem; |
| 989 |
|
| 990 |
uint64_t tag; |
| 991 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &tag))) |
| 992 |
return err; |
| 993 |
|
| 994 |
uint8_t die_flags; |
| 995 |
bool should_index = true; |
| 996 |
switch (tag) { |
| 997 |
#define X(name) case DW_TAG_##name: die_flags = DRGN_DWARF_INDEX_##name; break; |
| 998 |
DRGN_DWARF_INDEX_TAGS |
| 999 |
#undef X |
| 1000 |
case DW_TAG_imported_unit: |
| 1001 |
die_flags = INSN_DIE_TAG_imported_unit; |
| 1002 |
should_index = false; |
| 1003 |
break; |
| 1004 |
default: |
| 1005 |
die_flags = INSN_DIE_FLAG_TAG_MASK; |
| 1006 |
should_index = false; |
| 1007 |
break; |
| 1008 |
} |
| 1009 |
if (tag == DW_TAG_subprogram) |
| 1010 |
die_flags |= INSN_DIE_FLAG_SUBPROGRAM_NO_PC; |
| 1011 |
|
| 1012 |
uint8_t children; |
| 1013 |
if ((err = binary_buffer_next_u8(&buffer->bb, &children))) |
| 1014 |
return err; |
| 1015 |
if (children) |
| 1016 |
die_flags |= INSN_DIE_FLAG_CHILDREN; |
| 1017 |
|
| 1018 |
uint8_t insn, last_insn = UINT8_MAX; |
| 1019 |
for (;;) { |
| 1020 |
uint64_t name, form; |
| 1021 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &name))) |
| 1022 |
return err; |
| 1023 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &form))) |
| 1024 |
return err; |
| 1025 |
if (name == 0 && form == 0) |
| 1026 |
break; |
| 1027 |
|
| 1028 |
if (name == DW_AT_sibling) { |
| 1029 |
err = dw_at_sibling_to_insn(&buffer->bb, form, &insn); |
| 1030 |
} else if (name == DW_AT_name && should_index) { |
| 1031 |
err = dw_at_name_to_insn(cu, &buffer->bb, form, &insn); |
| 1032 |
} else if (name == DW_AT_declaration && should_index) { |
| 1033 |
err = dw_at_declaration_to_insn(&buffer->bb, form, |
| 1034 |
&insn, &die_flags); |
| 1035 |
} else if ((should_index |
| 1036 |
&& (name == DW_AT_specification |
| 1037 |
|| (tag == DW_TAG_subprogram |
| 1038 |
&& name == DW_AT_abstract_origin))) |
| 1039 |
|| (tag == DW_TAG_imported_unit |
| 1040 |
&& name == DW_AT_import)) { |
| 1041 |
err = dw_at_specification_to_insn(cu, &buffer->bb, form, |
| 1042 |
&insn); |
| 1043 |
} else { |
| 1044 |
if (name == DW_AT_low_pc || name == DW_AT_ranges) |
| 1045 |
die_flags &= ~INSN_DIE_FLAG_SUBPROGRAM_NO_PC; |
| 1046 |
err = dw_form_to_insn(cu, &buffer->bb, form, &insn); |
| 1047 |
} |
| 1048 |
if (err) |
| 1049 |
return err; |
| 1050 |
|
| 1051 |
if (insn != 0) { |
| 1052 |
if (insn <= INSN_MAX_SKIP) { |
| 1053 |
if (last_insn + insn <= INSN_MAX_SKIP) { |
| 1054 |
*uint8_vector_last(insns) += insn; |
| 1055 |
continue; |
| 1056 |
} else if (last_insn < INSN_MAX_SKIP) { |
| 1057 |
insn = last_insn + insn - INSN_MAX_SKIP; |
| 1058 |
*uint8_vector_last(insns) = INSN_MAX_SKIP; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
last_insn = insn; |
| 1062 |
|
| 1063 |
if (!uint8_vector_append(insns, &insn)) |
| 1064 |
return &drgn_enomem; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
insn = INSN_END; |
| 1068 |
if (!uint8_vector_append(insns, &insn) || |
| 1069 |
!uint8_vector_append(insns, &die_flags)) |
| 1070 |
return &drgn_enomem; |
| 1071 |
return NULL; |
| 1072 |
} |
| 1073 |
|
| 1074 |
static struct drgn_error *read_cu(struct drgn_dwarf_index_cu *cu) |
| 1075 |
{ |
| 1076 |
struct drgn_elf_file_section_buffer buffer; |
| 1077 |
drgn_elf_file_section_buffer_init_index(&buffer, cu->file, |
| 1078 |
DRGN_SCN_DEBUG_ABBREV); |
| 1079 |
buffer.bb.pos = cu->pending_abbrev; |
| 1080 |
struct uint32_vector decls = VECTOR_INIT; |
| 1081 |
struct uint8_vector insns = VECTOR_INIT; |
| 1082 |
for (;;) { |
| 1083 |
struct drgn_error *err = read_abbrev_decl(&buffer, cu, &decls, |
| 1084 |
&insns); |
| 1085 |
if (err == &drgn_stop) { |
| 1086 |
break; |
| 1087 |
} else if (err) { |
| 1088 |
uint8_vector_deinit(&insns); |
| 1089 |
uint32_vector_deinit(&decls); |
| 1090 |
return err; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
uint8_vector_shrink_to_fit(&insns); |
| 1094 |
uint32_vector_shrink_to_fit(&decls); |
| 1095 |
uint32_vector_steal(&decls, &cu->abbrev_decls, &cu->num_abbrev_decls); |
| 1096 |
uint8_vector_steal(&insns, &cu->abbrev_insns, NULL); |
| 1097 |
return NULL; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/* Get the size of a unit header beyond that of a normal compilation unit. */ |
| 1101 |
static size_t cu_header_extra_size(struct drgn_dwarf_index_cu *cu) |
| 1102 |
{ |
| 1103 |
switch (cu->unit_type) { |
| 1104 |
case DW_UT_compile: |
| 1105 |
case DW_UT_partial: |
| 1106 |
return 0; |
| 1107 |
case DW_UT_skeleton: |
| 1108 |
case DW_UT_split_compile: |
| 1109 |
/* dwo_id */ |
| 1110 |
return cu->version >= 5 ? 8 : 0; |
| 1111 |
case DW_UT_type: |
| 1112 |
case DW_UT_split_type: |
| 1113 |
/* type_signature and type_offset */ |
| 1114 |
return cu->is_64_bit ? 16 : 12; |
| 1115 |
default: |
| 1116 |
UNREACHABLE(); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
static size_t cu_header_size(struct drgn_dwarf_index_cu *cu) |
| 1121 |
{ |
| 1122 |
size_t size = cu->is_64_bit ? 23 : 11; |
| 1123 |
if (cu->version >= 5) |
| 1124 |
size++; |
| 1125 |
size += cu_header_extra_size(cu); |
| 1126 |
return size; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static bool |
| 1130 |
index_specification(struct drgn_dwarf_specification_map *specifications, |
| 1131 |
uintptr_t declaration, uintptr_t addr) |
| 1132 |
{ |
| 1133 |
struct drgn_dwarf_specification_map_entry entry = { |
| 1134 |
.key = declaration, |
| 1135 |
.value = addr, |
| 1136 |
}; |
| 1137 |
struct hash_pair hp = drgn_dwarf_specification_map_hash(&declaration); |
| 1138 |
// There may be duplicates if multiple DIEs reference one declaration, |
| 1139 |
// but we ignore them. |
| 1140 |
return drgn_dwarf_specification_map_insert_hashed(specifications, &entry, |
| 1141 |
hp, NULL) >= 0; |
| 1142 |
} |
| 1143 |
|
| 1144 |
static struct drgn_error *read_indirect_insn(struct drgn_dwarf_index_cu *cu, |
| 1145 |
struct binary_buffer *bb, |
| 1146 |
uint8_t insn, uint8_t *insn_ret, |
| 1147 |
uint8_t *die_flags) |
| 1148 |
{ |
| 1149 |
struct drgn_error *err; |
| 1150 |
uint64_t form; |
| 1151 |
if ((err = binary_buffer_next_uleb128(bb, &form))) |
| 1152 |
return err; |
| 1153 |
if (form == DW_FORM_implicit_const) { |
| 1154 |
return binary_buffer_error(bb, |
| 1155 |
"DW_FORM_implicit_const in DW_FORM_indirect"); |
| 1156 |
} |
| 1157 |
switch (insn) { |
| 1158 |
case INSN_INDIRECT: |
| 1159 |
return dw_form_to_insn(cu, bb, form, insn_ret); |
| 1160 |
case INSN_SIBLING_INDIRECT: |
| 1161 |
return dw_at_sibling_to_insn(bb, form, insn_ret); |
| 1162 |
case INSN_NAME_INDIRECT: |
| 1163 |
return dw_at_name_to_insn(cu, bb, form, insn_ret); |
| 1164 |
case INSN_DECLARATION_INDIRECT: |
| 1165 |
return dw_at_declaration_to_insn(bb, form, insn_ret, die_flags); |
| 1166 |
case INSN_SPECIFICATION_INDIRECT: |
| 1167 |
return dw_at_specification_to_insn(cu, bb, form, insn_ret); |
| 1168 |
default: |
| 1169 |
UNREACHABLE(); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
// Stack of CU buffers. The bottom is the initial unit/DIE, and |
| 1174 |
// DW_TAG_imported_unit DIEs push additional buffers. We use an inline size of 1 |
| 1175 |
// to avoid an allocation in the common case of no imports. |
| 1176 |
DEFINE_VECTOR(drgn_dwarf_index_cu_buffer_stack, |
| 1177 |
struct drgn_dwarf_index_cu_buffer, 1); |
| 1178 |
static const size_t MAX_IMPORTED_UNIT_DEPTH = 128; |
| 1179 |
|
| 1180 |
/* |
| 1181 |
* First pass: index DIEs with DW_AT_specification and DW_AT_abstract_origin. |
| 1182 |
* This recurses into namespaces. |
| 1183 |
*/ |
| 1184 |
static struct drgn_error * |
| 1185 |
index_cu_first_pass(struct drgn_dwarf_specification_map *specifications, |
| 1186 |
struct drgn_dwarf_index_cu_buffer_stack *stack) |
| 1187 |
{ |
| 1188 |
struct drgn_error *err; |
| 1189 |
struct drgn_dwarf_index_cu_buffer *buffer = |
| 1190 |
drgn_dwarf_index_cu_buffer_stack_last(stack); |
| 1191 |
struct drgn_dwarf_index_cu *cu = buffer->cu; |
| 1192 |
for (;;) { |
| 1193 |
uintptr_t die_addr = (uintptr_t)buffer->bb.pos; |
| 1194 |
|
| 1195 |
uint64_t code; |
| 1196 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &code))) |
| 1197 |
return err; |
| 1198 |
if (code == 0) { |
| 1199 |
if (buffer->depth > 1) { |
| 1200 |
buffer->depth--; |
| 1201 |
} else { |
| 1202 |
pop: |
| 1203 |
drgn_dwarf_index_cu_buffer_stack_pop(stack); |
| 1204 |
if (drgn_dwarf_index_cu_buffer_stack_empty(stack)) |
| 1205 |
break; |
| 1206 |
buffer = drgn_dwarf_index_cu_buffer_stack_last(stack); |
| 1207 |
cu = buffer->cu; |
| 1208 |
} |
| 1209 |
continue; |
| 1210 |
} else if (code > cu->num_abbrev_decls) { |
| 1211 |
return binary_buffer_error(&buffer->bb, |
| 1212 |
"unknown abbreviation code %" PRIu64, |
| 1213 |
code); |
| 1214 |
} |
| 1215 |
|
| 1216 |
uint8_t *insnp = &cu->abbrev_insns[cu->abbrev_decls[code - 1]]; |
| 1217 |
bool declaration = false; |
| 1218 |
const char *specification = NULL; |
| 1219 |
const char *sibling = NULL; |
| 1220 |
uint8_t insn; |
| 1221 |
uint8_t extra_die_flags = 0; |
| 1222 |
while ((insn = *insnp++) != INSN_END) { |
| 1223 |
indirect_insn:; |
| 1224 |
uint64_t skip, tmp; |
| 1225 |
switch (insn) { |
| 1226 |
case INSN_SKIP_BLOCK: |
| 1227 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, |
| 1228 |
&skip))) |
| 1229 |
return err; |
| 1230 |
goto skip; |
| 1231 |
case INSN_SKIP_BLOCK1: |
| 1232 |
if ((err = binary_buffer_next_u8_into_u64(&buffer->bb, |
| 1233 |
&skip))) |
| 1234 |
return err; |
| 1235 |
goto skip; |
| 1236 |
case INSN_SKIP_BLOCK2: |
| 1237 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 1238 |
&skip))) |
| 1239 |
return err; |
| 1240 |
goto skip; |
| 1241 |
case INSN_SKIP_BLOCK4: |
| 1242 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1243 |
&skip))) |
| 1244 |
return err; |
| 1245 |
goto skip; |
| 1246 |
case INSN_SKIP_LEB128: |
| 1247 |
case INSN_NAME_STRX: |
| 1248 |
if ((err = binary_buffer_skip_leb128(&buffer->bb))) |
| 1249 |
return err; |
| 1250 |
break; |
| 1251 |
case INSN_SKIP_STRING: |
| 1252 |
case INSN_NAME_STRING: |
| 1253 |
if ((err = binary_buffer_skip_string(&buffer->bb))) |
| 1254 |
return err; |
| 1255 |
break; |
| 1256 |
case INSN_SIBLING_REF1: |
| 1257 |
if ((err = binary_buffer_next_u8_into_u64(&buffer->bb, |
| 1258 |
&tmp))) |
| 1259 |
return err; |
| 1260 |
goto sibling; |
| 1261 |
case INSN_SIBLING_REF2: |
| 1262 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 1263 |
&tmp))) |
| 1264 |
return err; |
| 1265 |
goto sibling; |
| 1266 |
case INSN_SIBLING_REF4: |
| 1267 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1268 |
&tmp))) |
| 1269 |
return err; |
| 1270 |
goto sibling; |
| 1271 |
case INSN_SIBLING_REF8: |
| 1272 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1273 |
&tmp))) |
| 1274 |
return err; |
| 1275 |
goto sibling; |
| 1276 |
case INSN_SIBLING_REF_UDATA: |
| 1277 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, |
| 1278 |
&tmp))) |
| 1279 |
return err; |
| 1280 |
sibling: |
| 1281 |
if (tmp > cu->len) { |
| 1282 |
return binary_buffer_error(&buffer->bb, |
| 1283 |
"DW_AT_sibling is out of bounds"); |
| 1284 |
} |
| 1285 |
sibling = cu->buf + tmp; |
| 1286 |
__builtin_prefetch(sibling); |
| 1287 |
if (sibling < buffer->bb.pos) { |
| 1288 |
return binary_buffer_error(&buffer->bb, |
| 1289 |
"DW_AT_sibling points backwards"); |
| 1290 |
} |
| 1291 |
break; |
| 1292 |
case INSN_NAME_STRX1: |
| 1293 |
skip = 1; |
| 1294 |
goto skip; |
| 1295 |
case INSN_NAME_STRX2: |
| 1296 |
skip = 2; |
| 1297 |
goto skip; |
| 1298 |
case INSN_NAME_STRX3: |
| 1299 |
skip = 3; |
| 1300 |
goto skip; |
| 1301 |
case INSN_NAME_STRP4: |
| 1302 |
case INSN_NAME_STRX4: |
| 1303 |
case INSN_NAME_STRP_ALT4: |
| 1304 |
skip = 4; |
| 1305 |
goto skip; |
| 1306 |
case INSN_NAME_STRP8: |
| 1307 |
case INSN_NAME_STRP_ALT8: |
| 1308 |
skip = 8; |
| 1309 |
goto skip; |
| 1310 |
case INSN_DECLARATION_FLAG: { |
| 1311 |
uint8_t flag; |
| 1312 |
if ((err = binary_buffer_next_u8(&buffer->bb, |
| 1313 |
&flag))) |
| 1314 |
return err; |
| 1315 |
if (flag) |
| 1316 |
declaration = true; |
| 1317 |
break; |
| 1318 |
} |
| 1319 |
case INSN_SPECIFICATION_REF1: |
| 1320 |
if ((err = binary_buffer_next_u8_into_u64(&buffer->bb, |
| 1321 |
&tmp))) |
| 1322 |
return err; |
| 1323 |
goto specification; |
| 1324 |
case INSN_SPECIFICATION_REF2: |
| 1325 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 1326 |
&tmp))) |
| 1327 |
return err; |
| 1328 |
goto specification; |
| 1329 |
case INSN_SPECIFICATION_REF4: |
| 1330 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1331 |
&tmp))) |
| 1332 |
return err; |
| 1333 |
goto specification; |
| 1334 |
case INSN_SPECIFICATION_REF8: |
| 1335 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1336 |
&tmp))) |
| 1337 |
return err; |
| 1338 |
goto specification; |
| 1339 |
case INSN_SPECIFICATION_REF_UDATA: |
| 1340 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, |
| 1341 |
&tmp))) |
| 1342 |
return err; |
| 1343 |
specification: |
| 1344 |
if (tmp >= cu->len) { |
| 1345 |
return binary_buffer_error(&buffer->bb, |
| 1346 |
"reference is out of bounds"); |
| 1347 |
} |
| 1348 |
specification = cu->buf + tmp; |
| 1349 |
break; |
| 1350 |
case INSN_SPECIFICATION_REF_ADDR4: |
| 1351 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1352 |
&tmp))) |
| 1353 |
return err; |
| 1354 |
goto specification_ref_addr; |
| 1355 |
case INSN_SPECIFICATION_REF_ADDR8: |
| 1356 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1357 |
&tmp))) |
| 1358 |
return err; |
| 1359 |
specification_ref_addr: |
| 1360 |
if (tmp >= cu->file->scn_data[cu->scn]->d_size) { |
| 1361 |
return binary_buffer_error(&buffer->bb, |
| 1362 |
"reference is out of bounds"); |
| 1363 |
} |
| 1364 |
specification = (char *)cu->file->scn_data[cu->scn]->d_buf |
| 1365 |
+ tmp; |
| 1366 |
break; |
| 1367 |
case INSN_SPECIFICATION_REF_ALT4: |
| 1368 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1369 |
&tmp))) |
| 1370 |
return err; |
| 1371 |
goto specification_ref_alt; |
| 1372 |
case INSN_SPECIFICATION_REF_ALT8: |
| 1373 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1374 |
&tmp))) |
| 1375 |
return err; |
| 1376 |
specification_ref_alt: |
| 1377 |
if (tmp >= cu->file->alt_debug_info_data->d_size) { |
| 1378 |
return binary_buffer_error(&buffer->bb, |
| 1379 |
"reference is out of bounds"); |
| 1380 |
} |
| 1381 |
specification = (char *)cu->file->alt_debug_info_data->d_buf |
| 1382 |
+ tmp; |
| 1383 |
break; |
| 1384 |
case INSN_INDIRECT: |
| 1385 |
case INSN_SIBLING_INDIRECT: |
| 1386 |
case INSN_NAME_INDIRECT: |
| 1387 |
case INSN_DECLARATION_INDIRECT: |
| 1388 |
case INSN_SPECIFICATION_INDIRECT: |
| 1389 |
if ((err = read_indirect_insn(cu, &buffer->bb, |
| 1390 |
insn, &insn, |
| 1391 |
&extra_die_flags))) |
| 1392 |
return err; |
| 1393 |
if (insn) |
| 1394 |
goto indirect_insn; |
| 1395 |
else |
| 1396 |
continue; |
| 1397 |
default: |
| 1398 |
skip = insn; |
| 1399 |
skip: |
| 1400 |
if ((err = binary_buffer_skip(&buffer->bb, |
| 1401 |
skip))) |
| 1402 |
return err; |
| 1403 |
break; |
| 1404 |
} |
| 1405 |
} |
| 1406 |
insn = *insnp | extra_die_flags; |
| 1407 |
|
| 1408 |
uint8_t tag = insn & INSN_DIE_FLAG_TAG_MASK; |
| 1409 |
if (specification && tag != INSN_DIE_TAG_imported_unit) { |
| 1410 |
if (insn & INSN_DIE_FLAG_DECLARATION) |
| 1411 |
declaration = true; |
| 1412 |
/* |
| 1413 |
* For now, we don't handle DIEs with |
| 1414 |
* DW_AT_specification which are themselves |
| 1415 |
* declarations. We may need to handle |
| 1416 |
* DW_AT_specification "chains" in the future. |
| 1417 |
*/ |
| 1418 |
if (!declaration |
| 1419 |
&& !index_specification(specifications, |
| 1420 |
(uintptr_t)specification, |
| 1421 |
die_addr)) |
| 1422 |
return &drgn_enomem; |
| 1423 |
} |
| 1424 |
|
| 1425 |
unsigned int orig_depth = buffer->depth; |
| 1426 |
if (insn & INSN_DIE_FLAG_CHILDREN) { |
| 1427 |
// We descend into a DIE's children in these cases: |
| 1428 |
// 1. The DIE doesn't have a sibling pointer, in which |
| 1429 |
// case we have no choice. |
| 1430 |
// 2. The DIE is the unit that we're indexing. |
| 1431 |
// 3. The DIE is a namespace. |
| 1432 |
// In cases 2 and 3, we ignore the DIE's sibling pointer |
| 1433 |
// if it has one. |
| 1434 |
// |
| 1435 |
// Otherwise, we skip over the DIE's children by |
| 1436 |
// following the sibling pointer. |
| 1437 |
if (!sibling |
| 1438 |
|| buffer->depth == 0 |
| 1439 |
|| tag == DRGN_DWARF_INDEX_namespace) |
| 1440 |
buffer->depth++; |
| 1441 |
else |
| 1442 |
buffer->bb.pos = sibling; |
| 1443 |
} else if (buffer->depth == 0) { |
| 1444 |
goto pop; |
| 1445 |
} |
| 1446 |
|
| 1447 |
// We only need to follow imported_unit DIEs whose parent is a |
| 1448 |
// unit or namespace. To do that, we'd need to track extra |
| 1449 |
// information. In practice, imported_unit DIEs are mainly used |
| 1450 |
// in that case anyways, so we don't bother checking and take |
| 1451 |
// the risk of unnecessary imports. |
| 1452 |
// |
| 1453 |
// imported_unit DIEs at depth 0 are malformed, so we ignore |
| 1454 |
// those. |
| 1455 |
if (tag == INSN_DIE_TAG_imported_unit && orig_depth > 0) { |
| 1456 |
if (!specification) { |
| 1457 |
return binary_buffer_error(&buffer->bb, |
| 1458 |
"DW_TAG_imported_unit is missing DW_AT_import"); |
| 1459 |
} |
| 1460 |
cu = drgn_dwarf_index_find_cu(&cu->file->module->prog->dbinfo, |
| 1461 |
(uintptr_t)specification); |
| 1462 |
if (!cu) { |
| 1463 |
return binary_buffer_error(&buffer->bb, |
| 1464 |
"imported unit not found"); |
| 1465 |
} |
| 1466 |
if (drgn_dwarf_index_cu_buffer_stack_size(stack) |
| 1467 |
>= MAX_IMPORTED_UNIT_DEPTH) { |
| 1468 |
return binary_buffer_error(&buffer->bb, |
| 1469 |
"maximum DWARF imported unit depth exceeded"); |
| 1470 |
} |
| 1471 |
buffer = drgn_dwarf_index_cu_buffer_stack_append_entry(stack); |
| 1472 |
if (!buffer) |
| 1473 |
return &drgn_enomem; |
| 1474 |
drgn_dwarf_index_cu_buffer_init(buffer, cu); |
| 1475 |
buffer->bb.pos = specification; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
return NULL; |
| 1479 |
} |
| 1480 |
|
| 1481 |
/** |
| 1482 |
* Find the address of a top-level DIE with a @c DW_AT_specification or @c |
| 1483 |
* DW_AT_abstract_origin attribute that refers to the given DIE address. |
| 1484 |
* |
| 1485 |
* This can be used to find the definition of a declaration or the concrete |
| 1486 |
* out-of-line instance of an abstract instance root. |
| 1487 |
* |
| 1488 |
* @param[in] die_addr Address of a DIE. |
| 1489 |
* @param[out] ret Returned address of the definition DIE. |
| 1490 |
* @return @c true if a definition DIE was found, @c false if not (in which case |
| 1491 |
* `*ret` is not modified). |
| 1492 |
*/ |
| 1493 |
static bool drgn_dwarf_find_definition(struct drgn_debug_info *dbinfo, |
| 1494 |
uintptr_t die_addr, uintptr_t *ret) |
| 1495 |
{ |
| 1496 |
struct drgn_dwarf_specification_map_iterator it = |
| 1497 |
drgn_dwarf_specification_map_search(&dbinfo->dwarf.specifications, |
| 1498 |
&die_addr); |
| 1499 |
if (!it.entry) |
| 1500 |
return false; |
| 1501 |
*ret = it.entry->value; |
| 1502 |
return true; |
| 1503 |
} |
| 1504 |
|
| 1505 |
static bool |
| 1506 |
index_die(struct drgn_dwarf_index_die_map map[static DRGN_DWARF_INDEX_MAP_SIZE], |
| 1507 |
struct drgn_dwarf_base_type_map *base_types, const char *name, |
| 1508 |
int tag, uintptr_t addr) |
| 1509 |
{ |
| 1510 |
size_t name_len = strlen(name); |
| 1511 |
if (tag != DRGN_DWARF_INDEX_base_type) { |
| 1512 |
struct drgn_dwarf_index_die_map_entry entry = { |
| 1513 |
.key = { name, name_len }, |
| 1514 |
.value = VECTOR_INIT, |
| 1515 |
}; |
| 1516 |
struct hash_pair hp = drgn_dwarf_index_die_map_hash(&entry.key); |
| 1517 |
auto it = drgn_dwarf_index_die_map_search_hashed(&map[tag], |
| 1518 |
&entry.key, |
| 1519 |
hp); |
| 1520 |
if (!it.entry |
| 1521 |
&& drgn_dwarf_index_die_map_insert_searched(&map[tag], |
| 1522 |
&entry, hp, |
| 1523 |
&it) < 0) |
| 1524 |
return false; |
| 1525 |
return drgn_dwarf_index_die_vector_append(&it.entry->value, |
| 1526 |
&addr); |
| 1527 |
} else if (base_types) { |
| 1528 |
struct drgn_dwarf_base_type_map_entry entry = { |
| 1529 |
.key = { name, name_len }, |
| 1530 |
.value = addr, |
| 1531 |
}; |
| 1532 |
struct hash_pair hp = drgn_dwarf_base_type_map_hash(&entry.key); |
| 1533 |
return drgn_dwarf_base_type_map_insert_hashed(base_types, |
| 1534 |
&entry, hp, |
| 1535 |
NULL) >= 0; |
| 1536 |
} |
| 1537 |
return true; |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* Second pass: index the actual DIEs. */ |
| 1541 |
static struct drgn_error * |
| 1542 |
index_cu_second_pass(struct drgn_debug_info *dbinfo, |
| 1543 |
struct drgn_dwarf_index_die_map map[static DRGN_DWARF_INDEX_MAP_SIZE], |
| 1544 |
struct drgn_dwarf_base_type_map *base_types, |
| 1545 |
struct drgn_dwarf_index_cu_buffer_stack *stack) |
| 1546 |
{ |
| 1547 |
struct drgn_error *err; |
| 1548 |
struct drgn_dwarf_index_cu_buffer *buffer = |
| 1549 |
drgn_dwarf_index_cu_buffer_stack_last(stack); |
| 1550 |
struct drgn_dwarf_index_cu *cu = buffer->cu; |
| 1551 |
uint8_t depth1_tag = 0; |
| 1552 |
uintptr_t depth1_addr = 0; |
| 1553 |
for (;;) { |
| 1554 |
uintptr_t die_addr = (uintptr_t)buffer->bb.pos; |
| 1555 |
|
| 1556 |
uint64_t code; |
| 1557 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &code))) |
| 1558 |
return err; |
| 1559 |
if (code == 0) { |
| 1560 |
if (buffer->depth > 1) { |
| 1561 |
buffer->depth--; |
| 1562 |
} else { |
| 1563 |
pop: |
| 1564 |
drgn_dwarf_index_cu_buffer_stack_pop(stack); |
| 1565 |
if (drgn_dwarf_index_cu_buffer_stack_empty(stack)) |
| 1566 |
break; |
| 1567 |
buffer = drgn_dwarf_index_cu_buffer_stack_last(stack); |
| 1568 |
cu = buffer->cu; |
| 1569 |
} |
| 1570 |
continue; |
| 1571 |
} else if (code > cu->num_abbrev_decls) { |
| 1572 |
return binary_buffer_error(&buffer->bb, |
| 1573 |
"unknown abbreviation code %" PRIu64, |
| 1574 |
code); |
| 1575 |
} |
| 1576 |
|
| 1577 |
uint8_t *insnp = &cu->abbrev_insns[cu->abbrev_decls[code - 1]]; |
| 1578 |
const char *name = NULL; |
| 1579 |
bool declaration = false; |
| 1580 |
const char *specification = NULL; |
| 1581 |
const char *sibling = NULL; |
| 1582 |
uint8_t insn; |
| 1583 |
uint8_t extra_die_flags = 0; |
| 1584 |
while ((insn = *insnp++) != INSN_END) { |
| 1585 |
indirect_insn:; |
| 1586 |
uint64_t skip, tmp; |
| 1587 |
switch (insn) { |
| 1588 |
case INSN_SKIP_BLOCK: |
| 1589 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, |
| 1590 |
&skip))) |
| 1591 |
return err; |
| 1592 |
goto skip; |
| 1593 |
case INSN_SKIP_BLOCK1: |
| 1594 |
if ((err = binary_buffer_next_u8_into_u64(&buffer->bb, |
| 1595 |
&skip))) |
| 1596 |
return err; |
| 1597 |
goto skip; |
| 1598 |
case INSN_SKIP_BLOCK2: |
| 1599 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 1600 |
&skip))) |
| 1601 |
return err; |
| 1602 |
goto skip; |
| 1603 |
case INSN_SKIP_BLOCK4: |
| 1604 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1605 |
&skip))) |
| 1606 |
return err; |
| 1607 |
goto skip; |
| 1608 |
case INSN_SKIP_LEB128: |
| 1609 |
if ((err = binary_buffer_skip_leb128(&buffer->bb))) |
| 1610 |
return err; |
| 1611 |
break; |
| 1612 |
case INSN_NAME_STRING: |
| 1613 |
name = buffer->bb.pos; |
| 1614 |
fallthrough; |
| 1615 |
case INSN_SKIP_STRING: |
| 1616 |
if ((err = binary_buffer_skip_string(&buffer->bb))) |
| 1617 |
return err; |
| 1618 |
break; |
| 1619 |
case INSN_SIBLING_REF1: |
| 1620 |
if ((err = binary_buffer_next_u8_into_u64(&buffer->bb, |
| 1621 |
&tmp))) |
| 1622 |
return err; |
| 1623 |
goto sibling; |
| 1624 |
case INSN_SIBLING_REF2: |
| 1625 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 1626 |
&tmp))) |
| 1627 |
return err; |
| 1628 |
goto sibling; |
| 1629 |
case INSN_SIBLING_REF4: |
| 1630 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1631 |
&tmp))) |
| 1632 |
return err; |
| 1633 |
goto sibling; |
| 1634 |
case INSN_SIBLING_REF8: |
| 1635 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1636 |
&tmp))) |
| 1637 |
return err; |
| 1638 |
goto sibling; |
| 1639 |
case INSN_SIBLING_REF_UDATA: |
| 1640 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, |
| 1641 |
&tmp))) |
| 1642 |
return err; |
| 1643 |
sibling: |
| 1644 |
if (tmp > cu->len) { |
| 1645 |
return binary_buffer_error(&buffer->bb, |
| 1646 |
"DW_AT_sibling is out of bounds"); |
| 1647 |
} |
| 1648 |
sibling = cu->buf + tmp; |
| 1649 |
__builtin_prefetch(sibling); |
| 1650 |
if (sibling < buffer->bb.pos) { |
| 1651 |
return binary_buffer_error(&buffer->bb, |
| 1652 |
"DW_AT_sibling points backwards"); |
| 1653 |
} |
| 1654 |
break; |
| 1655 |
case INSN_NAME_STRP4: |
| 1656 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1657 |
&tmp))) |
| 1658 |
return err; |
| 1659 |
goto strp; |
| 1660 |
case INSN_NAME_STRP8: |
| 1661 |
if ((err = binary_buffer_next_u64(&buffer->bb, &tmp))) |
| 1662 |
return err; |
| 1663 |
strp: |
| 1664 |
if (tmp >= cu->file->scn_data[DRGN_SCN_DEBUG_STR]->d_size) { |
| 1665 |
return binary_buffer_error(&buffer->bb, |
| 1666 |
"DW_AT_name is out of bounds"); |
| 1667 |
} |
| 1668 |
name = (const char *)cu->file->scn_data[DRGN_SCN_DEBUG_STR]->d_buf |
| 1669 |
+ tmp; |
| 1670 |
__builtin_prefetch(name); |
| 1671 |
break; |
| 1672 |
case INSN_NAME_STRX: |
| 1673 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, |
| 1674 |
&tmp))) |
| 1675 |
return err; |
| 1676 |
goto name_strx; |
| 1677 |
case INSN_NAME_STRX1: |
| 1678 |
if ((err = binary_buffer_next_u8_into_u64(&buffer->bb, |
| 1679 |
&tmp))) |
| 1680 |
return err; |
| 1681 |
goto name_strx; |
| 1682 |
case INSN_NAME_STRX2: |
| 1683 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 1684 |
&tmp))) |
| 1685 |
return err; |
| 1686 |
goto name_strx; |
| 1687 |
case INSN_NAME_STRX3: |
| 1688 |
if ((err = binary_buffer_next_uint(&buffer->bb, |
| 1689 |
3, &tmp))) |
| 1690 |
return err; |
| 1691 |
goto name_strx; |
| 1692 |
case INSN_NAME_STRX4: |
| 1693 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1694 |
&tmp))) |
| 1695 |
return err; |
| 1696 |
name_strx: |
| 1697 |
if ((err = read_strx(buffer, tmp, &name))) |
| 1698 |
return err; |
| 1699 |
__builtin_prefetch(name); |
| 1700 |
break; |
| 1701 |
case INSN_NAME_STRP_ALT4: |
| 1702 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1703 |
&tmp))) |
| 1704 |
return err; |
| 1705 |
goto name_alt_strp; |
| 1706 |
case INSN_NAME_STRP_ALT8: |
| 1707 |
if ((err = binary_buffer_next_u64(&buffer->bb, &tmp))) |
| 1708 |
return err; |
| 1709 |
name_alt_strp: |
| 1710 |
if (tmp >= cu->file->alt_debug_str_data->d_size) { |
| 1711 |
return binary_buffer_error(&buffer->bb, |
| 1712 |
"DW_AT_name is out of bounds"); |
| 1713 |
} |
| 1714 |
name = (const char *)cu->file->alt_debug_str_data->d_buf + tmp; |
| 1715 |
__builtin_prefetch(name); |
| 1716 |
break; |
| 1717 |
case INSN_DECLARATION_FLAG: { |
| 1718 |
uint8_t flag; |
| 1719 |
if ((err = binary_buffer_next_u8(&buffer->bb, |
| 1720 |
&flag))) |
| 1721 |
return err; |
| 1722 |
if (flag) |
| 1723 |
declaration = true; |
| 1724 |
break; |
| 1725 |
} |
| 1726 |
case INSN_SPECIFICATION_REF1: |
| 1727 |
if ((err = binary_buffer_next_u8_into_u64(&buffer->bb, |
| 1728 |
&tmp))) |
| 1729 |
return err; |
| 1730 |
goto specification; |
| 1731 |
case INSN_SPECIFICATION_REF2: |
| 1732 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 1733 |
&tmp))) |
| 1734 |
return err; |
| 1735 |
goto specification; |
| 1736 |
case INSN_SPECIFICATION_REF4: |
| 1737 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1738 |
&tmp))) |
| 1739 |
return err; |
| 1740 |
goto specification; |
| 1741 |
case INSN_SPECIFICATION_REF8: |
| 1742 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1743 |
&tmp))) |
| 1744 |
return err; |
| 1745 |
goto specification; |
| 1746 |
case INSN_SPECIFICATION_REF_UDATA: |
| 1747 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, |
| 1748 |
&tmp))) |
| 1749 |
return err; |
| 1750 |
specification: |
| 1751 |
if (tmp >= cu->len) { |
| 1752 |
return binary_buffer_error(&buffer->bb, |
| 1753 |
"reference is out of bounds"); |
| 1754 |
} |
| 1755 |
specification = cu->buf + tmp; |
| 1756 |
break; |
| 1757 |
case INSN_SPECIFICATION_REF_ADDR4: |
| 1758 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1759 |
&tmp))) |
| 1760 |
return err; |
| 1761 |
goto specification_ref_addr; |
| 1762 |
case INSN_SPECIFICATION_REF_ADDR8: |
| 1763 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1764 |
&tmp))) |
| 1765 |
return err; |
| 1766 |
specification_ref_addr: |
| 1767 |
if (tmp >= cu->file->scn_data[cu->scn]->d_size) { |
| 1768 |
return binary_buffer_error(&buffer->bb, |
| 1769 |
"reference is out of bounds"); |
| 1770 |
} |
| 1771 |
specification = (char *)cu->file->scn_data[cu->scn]->d_buf |
| 1772 |
+ tmp; |
| 1773 |
break; |
| 1774 |
case INSN_SPECIFICATION_REF_ALT4: |
| 1775 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 1776 |
&tmp))) |
| 1777 |
return err; |
| 1778 |
goto specification_ref_alt; |
| 1779 |
case INSN_SPECIFICATION_REF_ALT8: |
| 1780 |
if ((err = binary_buffer_next_u64(&buffer->bb, |
| 1781 |
&tmp))) |
| 1782 |
return err; |
| 1783 |
specification_ref_alt: |
| 1784 |
if (tmp >= cu->file->alt_debug_info_data->d_size) { |
| 1785 |
return binary_buffer_error(&buffer->bb, |
| 1786 |
"reference is out of bounds"); |
| 1787 |
} |
| 1788 |
specification = (char *)cu->file->alt_debug_info_data->d_buf |
| 1789 |
+ tmp; |
| 1790 |
break; |
| 1791 |
case INSN_INDIRECT: |
| 1792 |
case INSN_SIBLING_INDIRECT: |
| 1793 |
case INSN_NAME_INDIRECT: |
| 1794 |
case INSN_DECLARATION_INDIRECT: |
| 1795 |
case INSN_SPECIFICATION_INDIRECT: |
| 1796 |
if ((err = read_indirect_insn(cu, &buffer->bb, |
| 1797 |
insn, &insn, |
| 1798 |
&extra_die_flags))) |
| 1799 |
return err; |
| 1800 |
if (insn) |
| 1801 |
goto indirect_insn; |
| 1802 |
else |
| 1803 |
continue; |
| 1804 |
default: |
| 1805 |
skip = insn; |
| 1806 |
skip: |
| 1807 |
if ((err = binary_buffer_skip(&buffer->bb, |
| 1808 |
skip))) |
| 1809 |
return err; |
| 1810 |
break; |
| 1811 |
} |
| 1812 |
} |
| 1813 |
insn = *insnp | extra_die_flags; |
| 1814 |
|
| 1815 |
uint8_t tag = insn & INSN_DIE_FLAG_TAG_MASK; |
| 1816 |
if (buffer->depth == 1) { |
| 1817 |
depth1_tag = tag; |
| 1818 |
depth1_addr = die_addr; |
| 1819 |
} |
| 1820 |
if (buffer->depth == (tag == DRGN_DWARF_INDEX_enumerator ? 2 : 1) |
| 1821 |
&& name && !specification) { |
| 1822 |
if (insn & INSN_DIE_FLAG_DECLARATION) |
| 1823 |
declaration = true; |
| 1824 |
if (tag == DRGN_DWARF_INDEX_enumerator) { |
| 1825 |
if (depth1_tag != DRGN_DWARF_INDEX_enumeration_type) |
| 1826 |
goto next; |
| 1827 |
/* |
| 1828 |
* NB: the enumerator name points to the |
| 1829 |
* enumeration_type DIE. Also, enumerators can't |
| 1830 |
* be declared in C/C++, so we don't check for |
| 1831 |
* that. |
| 1832 |
*/ |
| 1833 |
die_addr = depth1_addr; |
| 1834 |
} else if (declaration) { |
| 1835 |
// Declaration class, struct, and union DIEs |
| 1836 |
// with children are treated like namespaces. |
| 1837 |
if ((insn & INSN_DIE_FLAG_CHILDREN) |
| 1838 |
&& (tag == DRGN_DWARF_INDEX_class_type |
| 1839 |
|| tag == DRGN_DWARF_INDEX_structure_type |
| 1840 |
|| tag == DRGN_DWARF_INDEX_union_type) |
| 1841 |
&& !index_die(map, base_types, name, |
| 1842 |
DRGN_DWARF_INDEX_namespace, |
| 1843 |
die_addr)) |
| 1844 |
return &drgn_enomem; |
| 1845 |
if (!drgn_dwarf_find_definition(dbinfo, |
| 1846 |
die_addr, |
| 1847 |
&die_addr)) |
| 1848 |
goto next; |
| 1849 |
} |
| 1850 |
|
| 1851 |
// A subprogram DIE without an address may be the |
| 1852 |
// abstract instance root for an inlined function, or a |
| 1853 |
// subprogram DIE in a supplementary file. Check for a |
| 1854 |
// concrete instance or a definition in the main debug |
| 1855 |
// file, respectively. |
| 1856 |
// |
| 1857 |
// Note that if the original DIE was a declaration, then |
| 1858 |
// this is technically checking whether the declaration |
| 1859 |
// itself has an address, not the definition. Since |
| 1860 |
// declarations don't have an address, this always does |
| 1861 |
// an extra lookup for definitions of declarations. |
| 1862 |
// |
| 1863 |
// The extra lookup is redundant for normal definitions, |
| 1864 |
// but we actually need it in the case that the |
| 1865 |
// definition is an abstract instance root (so we need |
| 1866 |
// to go from declaration -> abstract instance root -> |
| 1867 |
// concrete instance). Avoiding redundant lookups would |
| 1868 |
// require storing an extra flag in the specification |
| 1869 |
// map, which empirically isn't worth it. |
| 1870 |
if (insn & INSN_DIE_FLAG_SUBPROGRAM_NO_PC) { |
| 1871 |
drgn_dwarf_find_definition(dbinfo, die_addr, |
| 1872 |
&die_addr); |
| 1873 |
} |
| 1874 |
|
| 1875 |
if (!index_die(map, base_types, name, tag, die_addr)) |
| 1876 |
return &drgn_enomem; |
| 1877 |
} |
| 1878 |
|
| 1879 |
next:; |
| 1880 |
unsigned int orig_depth = buffer->depth; |
| 1881 |
if (insn & INSN_DIE_FLAG_CHILDREN) { |
| 1882 |
// We descend into a DIE's children in these cases: |
| 1883 |
// 1. The DIE doesn't have a sibling pointer, in which |
| 1884 |
// case we have no choice. |
| 1885 |
// 2. The DIE is the unit or namespace that we're |
| 1886 |
// indexing. |
| 1887 |
// 3. The DIE is a top-level enumeration_type DIE, so we |
| 1888 |
// want to index its children enumerator DIEs. |
| 1889 |
// In cases 2 and 3, we ignore the DIE's sibling pointer |
| 1890 |
// if it has one. |
| 1891 |
// |
| 1892 |
// Otherwise, we skip over the DIE's children by |
| 1893 |
// following the sibling pointer. |
| 1894 |
if (!sibling |
| 1895 |
|| buffer->depth == 0 |
| 1896 |
|| (buffer->depth == 1 && tag == DRGN_DWARF_INDEX_enumeration_type)) |
| 1897 |
buffer->depth++; |
| 1898 |
else |
| 1899 |
buffer->bb.pos = sibling; |
| 1900 |
} else if (buffer->depth == 0) { |
| 1901 |
goto pop; |
| 1902 |
} |
| 1903 |
|
| 1904 |
// Each buffer actually has two depths: the physical depth in |
| 1905 |
// the file of the current DIE relative to where the buffer was |
| 1906 |
// initialized (either the partial unit that we imported or the |
| 1907 |
// unit or namespace DIE where we started indexing), and the |
| 1908 |
// logical depth, treating the children of a partial unit as if |
| 1909 |
// they were siblings of the imported_unit DIE. Therefore, the |
| 1910 |
// logical depth of the children of a partial unit is equal to |
| 1911 |
// the logical depth of the imported_unit DIE, and the logical |
| 1912 |
// depth of the partial unit itself is the logical depth of the |
| 1913 |
// imported_unit DIE minus 1. |
| 1914 |
// |
| 1915 |
// Other than enumerator DIEs, we only index DIEs at logical |
| 1916 |
// depth 1. We assume that partial units will not have top-level |
| 1917 |
// enumerator DIEs, or alternatively that an enumeration_type |
| 1918 |
// DIE will not have an imported_unit DIE child. |
| 1919 |
// |
| 1920 |
// imported_unit DIEs at logical depth > 1 can only contain DIEs |
| 1921 |
// at logical depth > 1, which we would ignore anyways. |
| 1922 |
// imported_unit DIEs at depth 0 are malformed. Therefore, we |
| 1923 |
// only follow imported_unit DIEs at logical depth 1 and ignore |
| 1924 |
// others. |
| 1925 |
// |
| 1926 |
// This lets us avoid tracking the depth and logical depth |
| 1927 |
// separately: since we only follow imports at logical depth 1, |
| 1928 |
// depth == logical depth. |
| 1929 |
// |
| 1930 |
// If our assumption about enumerator DIEs is incorrect, then we |
| 1931 |
// will need to track depth and logical depth separately, update |
| 1932 |
// everything to use the appropriate one, and also take imports |
| 1933 |
// into account for depth1_{tag,addr}. |
| 1934 |
if (tag == INSN_DIE_TAG_imported_unit && orig_depth == 1) { |
| 1935 |
if (!specification) { |
| 1936 |
return binary_buffer_error(&buffer->bb, |
| 1937 |
"DW_TAG_imported_unit is missing DW_AT_import"); |
| 1938 |
} |
| 1939 |
cu = drgn_dwarf_index_find_cu(&cu->file->module->prog->dbinfo, |
| 1940 |
(uintptr_t)specification); |
| 1941 |
if (!cu) { |
| 1942 |
return binary_buffer_error(&buffer->bb, |
| 1943 |
"imported unit not found"); |
| 1944 |
} |
| 1945 |
if (drgn_dwarf_index_cu_buffer_stack_size(stack) |
| 1946 |
>= MAX_IMPORTED_UNIT_DEPTH) { |
| 1947 |
return binary_buffer_error(&buffer->bb, |
| 1948 |
"maximum DWARF imported unit depth exceeded"); |
| 1949 |
} |
| 1950 |
buffer = drgn_dwarf_index_cu_buffer_stack_append_entry(stack); |
| 1951 |
if (!buffer) |
| 1952 |
return &drgn_enomem; |
| 1953 |
drgn_dwarf_index_cu_buffer_init(buffer, cu); |
| 1954 |
buffer->bb.pos = specification; |
| 1955 |
} |
| 1956 |
} |
| 1957 |
return NULL; |
| 1958 |
} |
| 1959 |
|
| 1960 |
static inline int drgn_dwarf_index_cu_lookup_cmp(const void *_a, const void *_b) |
| 1961 |
{ |
| 1962 |
uintptr_t a = ((struct drgn_dwarf_index_cu_lookup *)_a)->buf; |
| 1963 |
uintptr_t b = ((struct drgn_dwarf_index_cu_lookup *)_b)->buf; |
| 1964 |
return (a > b) - (a < b); |
| 1965 |
} |
| 1966 |
|
| 1967 |
static void |
| 1968 |
drgn_dwarf_index_cus_merge_partial(struct drgn_dwarf_index_cu_vector *dst, |
| 1969 |
struct drgn_dwarf_index_cu_vector *src_partial, |
| 1970 |
size_t *partial_pos) |
| 1971 |
{ |
| 1972 |
if (!drgn_dwarf_index_cu_vector_empty(src_partial)) { |
| 1973 |
memcpy(drgn_dwarf_index_cu_vector_at(dst, *partial_pos), |
| 1974 |
drgn_dwarf_index_cu_vector_begin(src_partial), |
| 1975 |
drgn_dwarf_index_cu_vector_size(src_partial) |
| 1976 |
* sizeof(struct drgn_dwarf_index_cu)); |
| 1977 |
*partial_pos += drgn_dwarf_index_cu_vector_size(src_partial); |
| 1978 |
} |
| 1979 |
drgn_dwarf_index_cu_vector_deinit(src_partial); |
| 1980 |
} |
| 1981 |
|
| 1982 |
static void |
| 1983 |
drgn_dwarf_index_cus_merge(struct drgn_dwarf_index_cu_vector *dst, |
| 1984 |
struct drgn_dwarf_index_cu_vector *src, |
| 1985 |
struct drgn_dwarf_index_cu_vector *src_partial, |
| 1986 |
size_t *pos, size_t *partial_pos) |
| 1987 |
{ |
| 1988 |
if (!drgn_dwarf_index_cu_vector_empty(src)) { |
| 1989 |
memcpy(drgn_dwarf_index_cu_vector_at(dst, *pos), |
| 1990 |
drgn_dwarf_index_cu_vector_begin(src), |
| 1991 |
drgn_dwarf_index_cu_vector_size(src) |
| 1992 |
* sizeof(struct drgn_dwarf_index_cu)); |
| 1993 |
*pos += drgn_dwarf_index_cu_vector_size(src); |
| 1994 |
} |
| 1995 |
drgn_dwarf_index_cu_vector_deinit(src); |
| 1996 |
drgn_dwarf_index_cus_merge_partial(dst, src_partial, partial_pos); |
| 1997 |
} |
| 1998 |
|
| 1999 |
// If there wasn't already an error, merge src into dst, and return an error if |
| 2000 |
// that fails. If there was already an error, return the original error. Free |
| 2001 |
// src whether or not there was an error. |
| 2002 |
static struct drgn_error * |
| 2003 |
drgn_dwarf_specification_map_merge(struct drgn_dwarf_specification_map *dst, |
| 2004 |
struct drgn_dwarf_specification_map *src, |
| 2005 |
struct drgn_error *err) |
| 2006 |
{ |
| 2007 |
if (!err) { |
| 2008 |
hash_table_for_each(drgn_dwarf_specification_map, it, src) { |
| 2009 |
if (drgn_dwarf_specification_map_insert(dst, it.entry, |
| 2010 |
NULL) < 0) { |
| 2011 |
err = &drgn_enomem; |
| 2012 |
break; |
| 2013 |
} |
| 2014 |
} |
| 2015 |
} |
| 2016 |
drgn_dwarf_specification_map_deinit(src); |
| 2017 |
return err; |
| 2018 |
} |
| 2019 |
|
| 2020 |
static struct drgn_error * |
| 2021 |
drgn_dwarf_index_die_map_merge(struct drgn_dwarf_index_die_map *dst, |
| 2022 |
struct drgn_dwarf_index_die_map *src, |
| 2023 |
struct drgn_error *err) |
| 2024 |
{ |
| 2025 |
auto src_it = drgn_dwarf_index_die_map_first(src); |
| 2026 |
for (; !err && src_it.entry; |
| 2027 |
src_it = drgn_dwarf_index_die_map_next(src_it)) { |
| 2028 |
struct drgn_dwarf_index_die_vector *src_vector = |
| 2029 |
&src_it.entry->value; |
| 2030 |
|
| 2031 |
struct hash_pair hp = |
| 2032 |
drgn_dwarf_index_die_map_hash(&src_it.entry->key); |
| 2033 |
auto dst_it = drgn_dwarf_index_die_map_search_hashed(dst, |
| 2034 |
&src_it.entry->key, |
| 2035 |
hp); |
| 2036 |
// If dst already has the key, extend its vector with |
| 2037 |
// src_vector, and free src_vector. Otherwise, move src_vector |
| 2038 |
// into dst. |
| 2039 |
if (dst_it.entry) { |
| 2040 |
if (!drgn_dwarf_index_die_vector_extend(&dst_it.entry->value, |
| 2041 |
src_vector)) |
| 2042 |
err = &drgn_enomem; |
| 2043 |
} else if (drgn_dwarf_index_die_map_insert_searched(dst, |
| 2044 |
src_it.entry, |
| 2045 |
hp, |
| 2046 |
NULL) < 0) { |
| 2047 |
err = &drgn_enomem; |
| 2048 |
} else { |
| 2049 |
// We stole src_vector; don't free it. |
| 2050 |
continue; |
| 2051 |
} |
| 2052 |
drgn_dwarf_index_die_vector_deinit(src_vector); |
| 2053 |
} |
| 2054 |
for (; src_it.entry; src_it = drgn_dwarf_index_die_map_next(src_it)) |
| 2055 |
drgn_dwarf_index_die_vector_deinit(&src_it.entry->value); |
| 2056 |
drgn_dwarf_index_die_map_deinit(src); |
| 2057 |
return err; |
| 2058 |
} |
| 2059 |
|
| 2060 |
static struct drgn_error * |
| 2061 |
drgn_dwarf_base_type_map_merge(struct drgn_dwarf_base_type_map *dst, |
| 2062 |
struct drgn_dwarf_base_type_map *src, |
| 2063 |
struct drgn_error *err) |
| 2064 |
{ |
| 2065 |
if (!err) { |
| 2066 |
hash_table_for_each(drgn_dwarf_base_type_map, it, src) { |
| 2067 |
if (drgn_dwarf_base_type_map_insert(dst, it.entry, NULL) |
| 2068 |
< 0) { |
| 2069 |
err = &drgn_enomem; |
| 2070 |
break; |
| 2071 |
} |
| 2072 |
} |
| 2073 |
} |
| 2074 |
drgn_dwarf_base_type_map_deinit(src); |
| 2075 |
return err; |
| 2076 |
} |
| 2077 |
|
| 2078 |
static struct drgn_error * |
| 2079 |
drgn_dwarf_index_update(struct drgn_debug_info *dbinfo) |
| 2080 |
{ |
| 2081 |
if (!dbinfo->modules_pending_indexing) |
| 2082 |
return NULL; |
| 2083 |
|
| 2084 |
if (dbinfo->dwarf.global.saved_err) |
| 2085 |
return drgn_error_copy(dbinfo->dwarf.global.saved_err); |
| 2086 |
|
| 2087 |
drgn_init_num_threads(); |
| 2088 |
|
| 2089 |
// Gather linked list of modules into a vector that we can parallelize. |
| 2090 |
VECTOR(drgn_module_vector, modules); |
| 2091 |
{ |
| 2092 |
struct drgn_module *module = dbinfo->modules_pending_indexing; |
| 2093 |
do { |
| 2094 |
if (!drgn_module_vector_append(&modules, &module)) |
| 2095 |
return &drgn_enomem; |
| 2096 |
module = module->pending_indexing_next; |
| 2097 |
} while (module); |
| 2098 |
} |
| 2099 |
|
| 2100 |
// Per-thread structures to populate. Thread 0 uses the structures in |
| 2101 |
// the dbinfo directly. These are merged into the dbinfo and freed. |
| 2102 |
_cleanup_free_ union { |
| 2103 |
// For reading modules. |
| 2104 |
struct { |
| 2105 |
struct drgn_dwarf_index_cu_vector cus; |
| 2106 |
struct drgn_dwarf_index_cu_vector partial_units; |
| 2107 |
}; |
| 2108 |
// For first pass. |
| 2109 |
struct drgn_dwarf_specification_map specifications; |
| 2110 |
// For second pass. |
| 2111 |
struct { |
| 2112 |
struct drgn_dwarf_index_die_map map[DRGN_DWARF_INDEX_MAP_SIZE]; |
| 2113 |
struct drgn_dwarf_base_type_map base_types; |
| 2114 |
}; |
| 2115 |
} *threads = NULL; |
| 2116 |
if (drgn_num_threads > 1) { |
| 2117 |
threads = malloc_array(drgn_num_threads - 1, sizeof(threads[0])); |
| 2118 |
if (!threads) |
| 2119 |
return &drgn_enomem; |
| 2120 |
} |
| 2121 |
|
| 2122 |
// Thread 0 needs its own temporary partial_units vector. |
| 2123 |
struct drgn_dwarf_index_cu_vector partial_units0; |
| 2124 |
|
| 2125 |
struct drgn_error *err = NULL; |
| 2126 |
size_t new_cus_size; |
| 2127 |
#pragma omp parallel num_threads(drgn_num_threads) |
| 2128 |
{ |
| 2129 |
struct drgn_error *thread_err = NULL; |
| 2130 |
int thread_num = omp_get_thread_num(); |
| 2131 |
|
| 2132 |
// Enumerate CUs in new modules. |
| 2133 |
struct drgn_dwarf_index_cu_vector *cus, *partial_units; |
| 2134 |
if (thread_num == 0) { |
| 2135 |
cus = &dbinfo->dwarf.index_cus; |
| 2136 |
partial_units = &partial_units0; |
| 2137 |
} else { |
| 2138 |
cus = &threads[thread_num - 1].cus; |
| 2139 |
partial_units = &threads[thread_num - 1].partial_units; |
| 2140 |
drgn_dwarf_index_cu_vector_init(cus); |
| 2141 |
} |
| 2142 |
drgn_dwarf_index_cu_vector_init(partial_units); |
| 2143 |
|
| 2144 |
#pragma omp for schedule(dynamic) nowait |
| 2145 |
for (size_t i = 0; i < drgn_module_vector_size(&modules); i++) { |
| 2146 |
if (thread_err) |
| 2147 |
continue; |
| 2148 |
struct drgn_module *module = |
| 2149 |
*drgn_module_vector_at(&modules, i); |
| 2150 |
thread_err = |
| 2151 |
drgn_dwarf_index_read_file(module->debug_file, |
| 2152 |
cus, partial_units); |
| 2153 |
} |
| 2154 |
if (thread_err) { |
| 2155 |
#pragma omp critical(drgn_dwarf_info_update_index_error) |
| 2156 |
if (err) |
| 2157 |
drgn_error_destroy(thread_err); |
| 2158 |
else |
| 2159 |
err = thread_err; |
| 2160 |
thread_err = NULL; |
| 2161 |
} |
| 2162 |
#pragma omp barrier |
| 2163 |
|
| 2164 |
// Merge the per-thread CUs into dbinfo (and free them). Partial |
| 2165 |
// units are placed at the end and excluded from new_cus_size so |
| 2166 |
// that they are not indexed. |
| 2167 |
#pragma omp master |
| 2168 |
{ |
| 2169 |
if (!err) { |
| 2170 |
size_t cus_pos = new_cus_size = |
| 2171 |
drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus); |
| 2172 |
size_t new_partial_units = |
| 2173 |
drgn_dwarf_index_cu_vector_size(&partial_units0); |
| 2174 |
for (int i = 0; i < drgn_num_threads - 1; i++) { |
| 2175 |
new_cus_size += drgn_dwarf_index_cu_vector_size(&threads[i].cus); |
| 2176 |
new_partial_units += drgn_dwarf_index_cu_vector_size(&threads[i].partial_units); |
| 2177 |
} |
| 2178 |
|
| 2179 |
if (new_cus_size + new_partial_units |
| 2180 |
> dbinfo->dwarf.global.cus_indexed) { |
| 2181 |
if (drgn_dwarf_index_cu_vector_resize(&dbinfo->dwarf.index_cus, |
| 2182 |
new_cus_size |
| 2183 |
+ new_partial_units)) { |
| 2184 |
size_t partial_pos = new_cus_size; |
| 2185 |
drgn_dwarf_index_cus_merge_partial(&dbinfo->dwarf.index_cus, |
| 2186 |
&partial_units0, |
| 2187 |
&partial_pos); |
| 2188 |
for (int i = 0; i < drgn_num_threads - 1; i++) { |
| 2189 |
drgn_dwarf_index_cus_merge(&dbinfo->dwarf.index_cus, |
| 2190 |
&threads[i].cus, |
| 2191 |
&threads[i].partial_units, |
| 2192 |
&cus_pos, |
| 2193 |
&partial_pos); |
| 2194 |
} |
| 2195 |
} else { |
| 2196 |
err = &drgn_enomem; |
| 2197 |
} |
| 2198 |
} |
| 2199 |
} |
| 2200 |
if (err) { |
| 2201 |
for (int i = 0; i < drgn_num_threads - 1; i++) { |
| 2202 |
drgn_dwarf_index_cu_vector_deinit(&threads[i].partial_units); |
| 2203 |
drgn_dwarf_index_cu_vector_deinit(&threads[i].cus); |
| 2204 |
} |
| 2205 |
drgn_dwarf_index_cu_vector_deinit(&partial_units0); |
| 2206 |
// If there was an error, we'd like to avoid |
| 2207 |
// doing any more work, but we can't break out |
| 2208 |
// of an OpenMP parallel region. Set the number |
| 2209 |
// of CUs to the old number so the remaining |
| 2210 |
// loops are essentially no-ops. |
| 2211 |
new_cus_size = dbinfo->dwarf.global.cus_indexed; |
| 2212 |
drgn_dwarf_index_cu_vector_resize(&dbinfo->dwarf.index_cus, |
| 2213 |
new_cus_size); |
| 2214 |
} |
| 2215 |
} |
| 2216 |
#pragma omp barrier |
| 2217 |
|
| 2218 |
// Update the CU lookup table. This can be done by one thread in |
| 2219 |
// parallel with reading CUs. |
| 2220 |
#pragma omp master |
| 2221 |
if (drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus) |
| 2222 |
> dbinfo->dwarf.global.cus_indexed) { |
| 2223 |
struct drgn_dwarf_index_cu_lookup *lookup = |
| 2224 |
realloc_array(dbinfo->dwarf.index_cu_lookup, |
| 2225 |
drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus), |
| 2226 |
sizeof(lookup[0])); |
| 2227 |
if (lookup) { |
| 2228 |
dbinfo->dwarf.index_cu_lookup = lookup; |
| 2229 |
for (size_t i = dbinfo->dwarf.global.cus_indexed; |
| 2230 |
i < drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus); |
| 2231 |
i++) { |
| 2232 |
struct drgn_dwarf_index_cu *cu = |
| 2233 |
drgn_dwarf_index_cu_vector_at(&dbinfo->dwarf.index_cus, i); |
| 2234 |
lookup[i].buf = (uintptr_t)cu->buf; |
| 2235 |
lookup[i].index = i; |
| 2236 |
} |
| 2237 |
qsort(lookup, |
| 2238 |
drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus), |
| 2239 |
sizeof(lookup[0]), |
| 2240 |
drgn_dwarf_index_cu_lookup_cmp); |
| 2241 |
} else { |
| 2242 |
thread_err = &drgn_enomem; |
| 2243 |
} |
| 2244 |
} |
| 2245 |
|
| 2246 |
// Read the abbreviation tables of new CUs. |
| 2247 |
#pragma omp for schedule(dynamic) nowait |
| 2248 |
for (size_t i = dbinfo->dwarf.global.cus_indexed; |
| 2249 |
i < drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus); |
| 2250 |
i++) { |
| 2251 |
if (thread_err) |
| 2252 |
continue; |
| 2253 |
struct drgn_dwarf_index_cu *cu = |
| 2254 |
drgn_dwarf_index_cu_vector_at(&dbinfo->dwarf.index_cus, i); |
| 2255 |
thread_err = read_cu(cu); |
| 2256 |
} |
| 2257 |
if (thread_err) { |
| 2258 |
#pragma omp critical(drgn_dwarf_info_update_index_error) |
| 2259 |
{ |
| 2260 |
if (err) |
| 2261 |
drgn_error_destroy(thread_err); |
| 2262 |
else |
| 2263 |
err = thread_err; |
| 2264 |
// Same error handling trick as above, except |
| 2265 |
// that we can't resize the vector anymore for a |
| 2266 |
// couple of reasons: the CUs now need to be |
| 2267 |
// properly deinitialized by |
| 2268 |
// drgn_dwarf_index_cu_deinit(), and we can't |
| 2269 |
// change the iteration count of the above loop |
| 2270 |
// while it is running on other threads. |
| 2271 |
new_cus_size = dbinfo->dwarf.global.cus_indexed; |
| 2272 |
} |
| 2273 |
thread_err = NULL; |
| 2274 |
} |
| 2275 |
#pragma omp barrier |
| 2276 |
|
| 2277 |
// Do the first indexing pass. |
| 2278 |
struct drgn_dwarf_specification_map *specifications; |
| 2279 |
if (thread_num == 0) { |
| 2280 |
specifications = &dbinfo->dwarf.specifications; |
| 2281 |
} else { |
| 2282 |
specifications = &threads[thread_num - 1].specifications; |
| 2283 |
drgn_dwarf_specification_map_init(specifications); |
| 2284 |
} |
| 2285 |
VECTOR(drgn_dwarf_index_cu_buffer_stack, buffer_stack); |
| 2286 |
|
| 2287 |
#pragma omp for schedule(dynamic) nowait |
| 2288 |
for (size_t i = dbinfo->dwarf.global.cus_indexed; |
| 2289 |
i < new_cus_size; i++) { |
| 2290 |
if (thread_err) |
| 2291 |
continue; |
| 2292 |
struct drgn_dwarf_index_cu *cu = |
| 2293 |
drgn_dwarf_index_cu_vector_at(&dbinfo->dwarf.index_cus, i); |
| 2294 |
drgn_dwarf_index_cu_buffer_stack_clear(&buffer_stack); |
| 2295 |
struct drgn_dwarf_index_cu_buffer *buffer = |
| 2296 |
drgn_dwarf_index_cu_buffer_stack_append_entry(&buffer_stack); |
| 2297 |
drgn_dwarf_index_cu_buffer_init(buffer, cu); |
| 2298 |
buffer->bb.pos += cu_header_size(cu); |
| 2299 |
thread_err = index_cu_first_pass(specifications, |
| 2300 |
&buffer_stack); |
| 2301 |
} |
| 2302 |
if (thread_err) { |
| 2303 |
#pragma omp critical(drgn_dwarf_info_update_index_error) |
| 2304 |
if (err) |
| 2305 |
drgn_error_destroy(thread_err); |
| 2306 |
else |
| 2307 |
err = thread_err; |
| 2308 |
thread_err = NULL; |
| 2309 |
} |
| 2310 |
#pragma omp barrier |
| 2311 |
|
| 2312 |
// Merge the per-thread specification maps into dbinfo (and free |
| 2313 |
// them). |
| 2314 |
#pragma omp master |
| 2315 |
{ |
| 2316 |
for (int i = 0; i < drgn_num_threads - 1; i++) { |
| 2317 |
err = drgn_dwarf_specification_map_merge(&dbinfo->dwarf.specifications, |
| 2318 |
&threads[i].specifications, |
| 2319 |
err); |
| 2320 |
} |
| 2321 |
// Same error handling trick as above. |
| 2322 |
if (err) |
| 2323 |
new_cus_size = dbinfo->dwarf.global.cus_indexed; |
| 2324 |
} |
| 2325 |
#pragma omp barrier |
| 2326 |
|
| 2327 |
// Do the second indexing pass. |
| 2328 |
struct drgn_dwarf_index_die_map *map; |
| 2329 |
struct drgn_dwarf_base_type_map *base_types; |
| 2330 |
if (thread_num == 0) { |
| 2331 |
map = dbinfo->dwarf.global.map; |
| 2332 |
base_types = &dbinfo->dwarf.base_types; |
| 2333 |
} else { |
| 2334 |
array_for_each(tag_map, threads[thread_num - 1].map) |
| 2335 |
drgn_dwarf_index_die_map_init(tag_map); |
| 2336 |
map = threads[thread_num - 1].map; |
| 2337 |
base_types = &threads[thread_num - 1].base_types; |
| 2338 |
drgn_dwarf_base_type_map_init(base_types); |
| 2339 |
} |
| 2340 |
|
| 2341 |
#pragma omp for schedule(dynamic) |
| 2342 |
for (size_t i = dbinfo->dwarf.global.cus_indexed; |
| 2343 |
i < new_cus_size; i++) { |
| 2344 |
if (thread_err) |
| 2345 |
continue; |
| 2346 |
struct drgn_dwarf_index_cu *cu = |
| 2347 |
drgn_dwarf_index_cu_vector_at(&dbinfo->dwarf.index_cus, i); |
| 2348 |
drgn_dwarf_index_cu_buffer_stack_clear(&buffer_stack); |
| 2349 |
struct drgn_dwarf_index_cu_buffer *buffer = |
| 2350 |
drgn_dwarf_index_cu_buffer_stack_append_entry(&buffer_stack); |
| 2351 |
drgn_dwarf_index_cu_buffer_init(buffer, cu); |
| 2352 |
buffer->bb.pos += cu_header_size(cu); |
| 2353 |
thread_err = index_cu_second_pass(dbinfo, map, |
| 2354 |
base_types, |
| 2355 |
&buffer_stack); |
| 2356 |
} |
| 2357 |
|
| 2358 |
// Merge the per-thread DIE and base type maps into dbinfo (and |
| 2359 |
// free them). |
| 2360 |
#pragma omp for schedule(dynamic) nowait |
| 2361 |
for (size_t i = 0; i <= array_size(dbinfo->dwarf.global.map); i++) { |
| 2362 |
if (i < array_size(dbinfo->dwarf.global.map)) { |
| 2363 |
for (int j = 0; j < drgn_num_threads - 1; j++) { |
| 2364 |
thread_err = |
| 2365 |
drgn_dwarf_index_die_map_merge(&dbinfo->dwarf.global.map[i], |
| 2366 |
&threads[j].map[i], |
| 2367 |
thread_err); |
| 2368 |
} |
| 2369 |
} else { |
| 2370 |
for (int j = 0; j < drgn_num_threads - 1; j++) { |
| 2371 |
thread_err = |
| 2372 |
drgn_dwarf_base_type_map_merge(&dbinfo->dwarf.base_types, |
| 2373 |
&threads[j].base_types, |
| 2374 |
thread_err); |
| 2375 |
} |
| 2376 |
} |
| 2377 |
} |
| 2378 |
if (thread_err) { |
| 2379 |
#pragma omp critical(drgn_dwarf_info_update_index_error) |
| 2380 |
if (err) |
| 2381 |
drgn_error_destroy(thread_err); |
| 2382 |
else |
| 2383 |
err = thread_err; |
| 2384 |
} |
| 2385 |
} |
| 2386 |
|
| 2387 |
if (err) { |
| 2388 |
dbinfo->dwarf.global.saved_err = err; |
| 2389 |
return drgn_error_copy(err); |
| 2390 |
} |
| 2391 |
dbinfo->modules_pending_indexing = NULL; |
| 2392 |
dbinfo->dwarf.global.cus_indexed = |
| 2393 |
drgn_dwarf_index_cu_vector_size(&dbinfo->dwarf.index_cus); |
| 2394 |
return NULL; |
| 2395 |
} |
| 2396 |
|
| 2397 |
static struct drgn_error *index_namespace_impl(struct drgn_namespace_dwarf_index *ns) |
| 2398 |
{ |
| 2399 |
struct drgn_error *err; |
| 2400 |
|
| 2401 |
size_t num_index_cus = |
| 2402 |
drgn_dwarf_index_cu_vector_size(&ns->dbinfo->dwarf.index_cus); |
| 2403 |
if (ns->cus_indexed >= num_index_cus) |
| 2404 |
return NULL; |
| 2405 |
|
| 2406 |
if (ns->saved_err) |
| 2407 |
return drgn_error_copy(ns->saved_err); |
| 2408 |
|
| 2409 |
// The parent namespace must be indexed first so that the DIEs for this |
| 2410 |
// namespace are populated. |
| 2411 |
err = index_namespace_impl(ns->parent); |
| 2412 |
if (err) |
| 2413 |
return err; |
| 2414 |
|
| 2415 |
struct drgn_dwarf_index_die_vector |
| 2416 |
*die_vectors_to_index[DRGN_DWARF_INDEX_NUM_NAMESPACE_TAGS]; |
| 2417 |
int tags_to_index[DRGN_DWARF_INDEX_NUM_NAMESPACE_TAGS]; |
| 2418 |
int num_tags_to_index = 0; |
| 2419 |
struct nstring key = { ns->name, ns->name_len }; |
| 2420 |
struct hash_pair hp = drgn_dwarf_index_die_map_hash(&key); |
| 2421 |
for (int i = 0; i < DRGN_DWARF_INDEX_NUM_NAMESPACE_TAGS; i++) { |
| 2422 |
auto it = drgn_dwarf_index_die_map_search_hashed(&ns->parent->map[i], |
| 2423 |
&key, hp); |
| 2424 |
if (!it.entry) |
| 2425 |
continue; |
| 2426 |
struct drgn_dwarf_index_die_vector *dies = &it.entry->value; |
| 2427 |
if (ns->dies_indexed[i] |
| 2428 |
>= drgn_dwarf_index_die_vector_size(dies)) |
| 2429 |
continue; |
| 2430 |
|
| 2431 |
die_vectors_to_index[num_tags_to_index] = dies; |
| 2432 |
tags_to_index[num_tags_to_index] = i; |
| 2433 |
num_tags_to_index++; |
| 2434 |
} |
| 2435 |
if (num_tags_to_index == 0) { |
| 2436 |
ns->cus_indexed = num_index_cus; |
| 2437 |
return NULL; |
| 2438 |
} |
| 2439 |
|
| 2440 |
_cleanup_free_ struct drgn_dwarf_index_die_map |
| 2441 |
(*maps)[DRGN_DWARF_INDEX_MAP_SIZE] = NULL; |
| 2442 |
if (drgn_num_threads > 1) { |
| 2443 |
maps = malloc_array(drgn_num_threads - 1, sizeof(maps[0])); |
| 2444 |
if (!maps) |
| 2445 |
return &drgn_enomem; |
| 2446 |
} |
| 2447 |
|
| 2448 |
err = NULL; |
| 2449 |
#pragma omp parallel num_threads(drgn_num_threads) |
| 2450 |
{ |
| 2451 |
struct drgn_error *thread_err = NULL; |
| 2452 |
struct drgn_dwarf_index_die_map *map; |
| 2453 |
int thread_num = omp_get_thread_num(); |
| 2454 |
if (thread_num == 0) { |
| 2455 |
map = ns->map; |
| 2456 |
} else { |
| 2457 |
array_for_each(tag_map, maps[thread_num - 1]) |
| 2458 |
drgn_dwarf_index_die_map_init(tag_map); |
| 2459 |
map = maps[thread_num - 1]; |
| 2460 |
} |
| 2461 |
VECTOR(drgn_dwarf_index_cu_buffer_stack, buffer_stack); |
| 2462 |
|
| 2463 |
for (int i = 0; i < num_tags_to_index; i++) { |
| 2464 |
struct drgn_dwarf_index_die_vector *dies = |
| 2465 |
die_vectors_to_index[i]; |
| 2466 |
#pragma omp for schedule(dynamic) nowait |
| 2467 |
for (uint32_t j = ns->dies_indexed[tags_to_index[i]]; |
| 2468 |
j < drgn_dwarf_index_die_vector_size(dies); j++) { |
| 2469 |
if (thread_err) |
| 2470 |
continue; |
| 2471 |
uintptr_t die_addr = |
| 2472 |
*drgn_dwarf_index_die_vector_at(dies, j); |
| 2473 |
struct drgn_dwarf_index_cu *cu = |
| 2474 |
drgn_dwarf_index_find_cu(ns->dbinfo, die_addr); |
| 2475 |
drgn_dwarf_index_cu_buffer_stack_clear(&buffer_stack); |
| 2476 |
struct drgn_dwarf_index_cu_buffer *buffer = |
| 2477 |
drgn_dwarf_index_cu_buffer_stack_append_entry(&buffer_stack); |
| 2478 |
drgn_dwarf_index_cu_buffer_init(buffer, cu); |
| 2479 |
buffer->bb.pos = (void *)die_addr; |
| 2480 |
thread_err = index_cu_second_pass(ns->dbinfo, |
| 2481 |
map, NULL, |
| 2482 |
&buffer_stack); |
| 2483 |
} |
| 2484 |
} |
| 2485 |
#pragma omp barrier |
| 2486 |
|
| 2487 |
#pragma omp for schedule(dynamic) nowait |
| 2488 |
for (size_t i = 0; i < array_size(ns->map); i++) { |
| 2489 |
for (int j = 0; j < drgn_num_threads - 1; j++) { |
| 2490 |
thread_err = |
| 2491 |
drgn_dwarf_index_die_map_merge(&ns->map[i], |
| 2492 |
&maps[j][i], |
| 2493 |
thread_err); |
| 2494 |
} |
| 2495 |
} |
| 2496 |
if (thread_err) { |
| 2497 |
#pragma omp critical(drgn_index_namespace_error) |
| 2498 |
if (err) |
| 2499 |
drgn_error_destroy(thread_err); |
| 2500 |
else |
| 2501 |
err = thread_err; |
| 2502 |
} |
| 2503 |
} |
| 2504 |
if (err) { |
| 2505 |
ns->saved_err = err; |
| 2506 |
return drgn_error_copy(ns->saved_err); |
| 2507 |
} |
| 2508 |
ns->cus_indexed = num_index_cus; |
| 2509 |
for (int i = 0; i < num_tags_to_index; i++) { |
| 2510 |
ns->dies_indexed[tags_to_index[i]] = |
| 2511 |
drgn_dwarf_index_die_vector_size(die_vectors_to_index[i]); |
| 2512 |
} |
| 2513 |
return NULL; |
| 2514 |
} |
| 2515 |
|
| 2516 |
static struct drgn_error *index_namespace(struct drgn_namespace_dwarf_index *ns) |
| 2517 |
{ |
| 2518 |
if (!ns->dbinfo->modules_pending_indexing |
| 2519 |
&& (ns->cus_indexed |
| 2520 |
>= drgn_dwarf_index_cu_vector_size(&ns->dbinfo->dwarf.index_cus))) |
| 2521 |
return NULL; |
| 2522 |
|
| 2523 |
drgn_blocking_guard(blocking_state); |
| 2524 |
|
| 2525 |
struct drgn_error *err = drgn_dwarf_index_update(ns->dbinfo); |
| 2526 |
if (err) |
| 2527 |
return err; |
| 2528 |
return index_namespace_impl(ns); |
| 2529 |
} |
| 2530 |
|
| 2531 |
struct drgn_error *drgn_dwarf_info_update_index(struct drgn_debug_info *dbinfo) |
| 2532 |
{ |
| 2533 |
return index_namespace(&dbinfo->dwarf.global); |
| 2534 |
} |
| 2535 |
|
| 2536 |
/** |
| 2537 |
* Iterator over DWARF debugging information. |
| 2538 |
* |
| 2539 |
* An iterator is initialized with @ref drgn_dwarf_index_iterator_init(). It is |
| 2540 |
* advanced with @ref drgn_dwarf_index_iterator_next(). |
| 2541 |
*/ |
| 2542 |
struct drgn_dwarf_index_iterator { |
| 2543 |
struct drgn_namespace_dwarf_index *ns; |
| 2544 |
const char *name; |
| 2545 |
size_t name_len; |
| 2546 |
const enum drgn_dwarf_index_tag *tags; |
| 2547 |
size_t num_tags; |
| 2548 |
struct drgn_dwarf_index_die_vector *dies; |
| 2549 |
uint32_t index; |
| 2550 |
}; |
| 2551 |
|
| 2552 |
/** |
| 2553 |
* Create an iterator over DIEs in a DWARF index namespace. |
| 2554 |
* |
| 2555 |
* @param[out] it DWARF index iterator to initialize. |
| 2556 |
* @param[in] ns Namespace DWARF index. |
| 2557 |
* @param[in] name Name of DIE to search for. |
| 2558 |
* @param[in] name_len Length of @c name. |
| 2559 |
* @param[in] tags List of DIE tags to search for. |
| 2560 |
* @param[in] num_tags Number of tags in @p tags, or zero to search for any tag. |
| 2561 |
* @return @c NULL on success, non-@c NULL on error. |
| 2562 |
*/ |
| 2563 |
static struct drgn_error * |
| 2564 |
drgn_dwarf_index_iterator_init(struct drgn_dwarf_index_iterator *it, |
| 2565 |
struct drgn_namespace_dwarf_index *ns, |
| 2566 |
const char *name, size_t name_len, |
| 2567 |
const enum drgn_dwarf_index_tag *tags, |
| 2568 |
size_t num_tags) |
| 2569 |
{ |
| 2570 |
struct drgn_error *err = index_namespace(ns); |
| 2571 |
if (err) |
| 2572 |
return err; |
| 2573 |
it->ns = ns; |
| 2574 |
it->name = name; |
| 2575 |
it->name_len = name_len; |
| 2576 |
it->tags = tags; |
| 2577 |
it->num_tags = num_tags; |
| 2578 |
// Sentinel to simplify first iteration. |
| 2579 |
static const struct drgn_dwarf_index_die_vector empty_dies = VECTOR_INIT; |
| 2580 |
it->dies = (struct drgn_dwarf_index_die_vector *)&empty_dies; |
| 2581 |
it->index = 0; |
| 2582 |
return NULL; |
| 2583 |
} |
| 2584 |
|
| 2585 |
/** |
| 2586 |
* Get the next matching DIE from a DWARF index iterator. |
| 2587 |
* |
| 2588 |
* Note the quirks in @ref drgn_namespace_dwarf_index::map about |
| 2589 |
* `DW_TAG_enumerator` and `DW_TAG_namespace`. |
| 2590 |
* |
| 2591 |
* @param[in] it DWARF index iterator. |
| 2592 |
* @param[out] die_ret Returned DIE. |
| 2593 |
* @param[out] file_ret If not @c NULL, returned file that DIE came from. |
| 2594 |
* @return @c true on success, @c false if there are no more matching DIEs. |
| 2595 |
*/ |
| 2596 |
static bool |
| 2597 |
drgn_dwarf_index_iterator_next(struct drgn_dwarf_index_iterator *it, |
| 2598 |
Dwarf_Die *die_ret, |
| 2599 |
struct drgn_elf_file **file_ret) |
| 2600 |
{ |
| 2601 |
uintptr_t die_addr; |
| 2602 |
if (it->index < drgn_dwarf_index_die_vector_size(it->dies)) { |
| 2603 |
die_addr = *drgn_dwarf_index_die_vector_at(it->dies, |
| 2604 |
it->index++); |
| 2605 |
} else { |
| 2606 |
for (;;) { |
| 2607 |
if (it->num_tags == 0) |
| 2608 |
return false; |
| 2609 |
|
| 2610 |
int tag = *it->tags++; |
| 2611 |
it->num_tags--; |
| 2612 |
if (tag == DRGN_DWARF_INDEX_base_type) { |
| 2613 |
// Only look up base types in the global |
| 2614 |
// namespace. |
| 2615 |
if (it->ns->parent) |
| 2616 |
continue; |
| 2617 |
struct nstring key = { it->name, it->name_len }; |
| 2618 |
auto map_it = drgn_dwarf_base_type_map_search(&it->ns->dbinfo->dwarf.base_types, |
| 2619 |
&key); |
| 2620 |
if (map_it.entry) { |
| 2621 |
die_addr = map_it.entry->value; |
| 2622 |
break; |
| 2623 |
} |
| 2624 |
} else { |
| 2625 |
struct nstring key = { it->name, it->name_len }; |
| 2626 |
auto map_it = |
| 2627 |
drgn_dwarf_index_die_map_search(&it->ns->map[tag], |
| 2628 |
&key); |
| 2629 |
if (map_it.entry) { |
| 2630 |
die_addr = *drgn_dwarf_index_die_vector_first(&map_it.entry->value); |
| 2631 |
it->dies = &map_it.entry->value; |
| 2632 |
it->index = 1; |
| 2633 |
break; |
| 2634 |
} |
| 2635 |
} |
| 2636 |
} |
| 2637 |
} |
| 2638 |
|
| 2639 |
struct drgn_dwarf_index_cu *cu = |
| 2640 |
drgn_dwarf_index_find_cu(it->ns->dbinfo, die_addr); |
| 2641 |
*die_ret = (Dwarf_Die){ |
| 2642 |
.addr = (void *)die_addr, |
| 2643 |
.cu = cu->libdw_cu, |
| 2644 |
}; |
| 2645 |
if (file_ret) |
| 2646 |
*file_ret = cu->file; |
| 2647 |
return true; |
| 2648 |
} |
| 2649 |
|
| 2650 |
static struct drgn_error * |
| 2651 |
drgn_namespace_find_child(struct drgn_namespace_dwarf_index *ns, |
| 2652 |
const char *name, size_t name_len, |
| 2653 |
struct drgn_namespace_dwarf_index **ret) |
| 2654 |
{ |
| 2655 |
struct drgn_error *err = index_namespace(ns); |
| 2656 |
if (err) |
| 2657 |
return err; |
| 2658 |
|
| 2659 |
struct nstring key = { name, name_len }; |
| 2660 |
struct hash_pair hp = nstring_hash_pair(&key); |
| 2661 |
auto it = drgn_namespace_table_search_hashed(&ns->children, &key, hp); |
| 2662 |
if (it.entry) { |
| 2663 |
*ret = *it.entry; |
| 2664 |
return NULL; |
| 2665 |
} |
| 2666 |
|
| 2667 |
for (int i = 0; i < DRGN_DWARF_INDEX_NUM_NAMESPACE_TAGS; i++) { |
| 2668 |
auto die_it = |
| 2669 |
drgn_dwarf_index_die_map_search_hashed(&ns->map[i], |
| 2670 |
&key, hp); |
| 2671 |
if (die_it.entry) { |
| 2672 |
struct drgn_namespace_dwarf_index *new_ns = |
| 2673 |
malloc(sizeof(*new_ns)); |
| 2674 |
if (!new_ns) |
| 2675 |
return &drgn_enomem; |
| 2676 |
// Use the name from the DIE map, which has the same |
| 2677 |
// lifetime as the namespace table. |
| 2678 |
drgn_namespace_dwarf_index_init(new_ns, |
| 2679 |
die_it.entry->key.str, |
| 2680 |
die_it.entry->key.len, |
| 2681 |
ns); |
| 2682 |
if (drgn_namespace_table_insert_searched(&ns->children, |
| 2683 |
&new_ns, hp, |
| 2684 |
NULL) < 0) { |
| 2685 |
drgn_namespace_dwarf_index_deinit(new_ns); |
| 2686 |
free(new_ns); |
| 2687 |
return &drgn_enomem; |
| 2688 |
} |
| 2689 |
*ret = new_ns; |
| 2690 |
return NULL; |
| 2691 |
} |
| 2692 |
} |
| 2693 |
return &drgn_not_found; |
| 2694 |
} |
| 2695 |
|
| 2696 |
/* |
| 2697 |
* Language support. |
| 2698 |
*/ |
| 2699 |
|
| 2700 |
/** |
| 2701 |
* Return the @ref drgn_language of the CU of the given DIE. |
| 2702 |
* |
| 2703 |
* @param[in] fall_back Whether to fall back if the language is not found or |
| 2704 |
* unknown. If @c true, @ref drgn_default_language is returned in this case. If |
| 2705 |
* @c false, @c NULL is returned. |
| 2706 |
* @param[out] ret Returned language. |
| 2707 |
* @return @c NULL on success, non-@c NULL on error. |
| 2708 |
*/ |
| 2709 |
static struct drgn_error *drgn_language_from_die(Dwarf_Die *die, bool fall_back, |
| 2710 |
const struct drgn_language **ret) |
| 2711 |
{ |
| 2712 |
Dwarf_Die cudie; |
| 2713 |
if (!dwarf_cu_die(die->cu, &cudie, NULL, NULL, NULL, NULL, NULL, NULL)) |
| 2714 |
return drgn_error_libdw(); |
| 2715 |
switch (dwarf_srclang(&cudie)) { |
| 2716 |
case DW_LANG_C: |
| 2717 |
case DW_LANG_C89: |
| 2718 |
case DW_LANG_C99: |
| 2719 |
case DW_LANG_C11: |
| 2720 |
*ret = &drgn_language_c; |
| 2721 |
break; |
| 2722 |
case DW_LANG_C_plus_plus: |
| 2723 |
case DW_LANG_C_plus_plus_03: |
| 2724 |
case DW_LANG_C_plus_plus_11: |
| 2725 |
case DW_LANG_C_plus_plus_14: |
| 2726 |
*ret = &drgn_language_cpp; |
| 2727 |
break; |
| 2728 |
default: |
| 2729 |
*ret = fall_back ? &drgn_default_language : NULL; |
| 2730 |
break; |
| 2731 |
} |
| 2732 |
return NULL; |
| 2733 |
} |
| 2734 |
|
| 2735 |
const struct drgn_language * |
| 2736 |
drgn_debug_info_main_language(struct drgn_debug_info *dbinfo) |
| 2737 |
{ |
| 2738 |
struct drgn_error *err; |
| 2739 |
struct drgn_dwarf_index_iterator it; |
| 2740 |
const enum drgn_dwarf_index_tag tag = DRGN_DWARF_INDEX_subprogram; |
| 2741 |
err = drgn_dwarf_index_iterator_init(&it, &dbinfo->dwarf.global, "main", |
| 2742 |
strlen("main"), &tag, 1); |
| 2743 |
if (err) { |
| 2744 |
drgn_error_destroy(err); |
| 2745 |
return NULL; |
| 2746 |
} |
| 2747 |
Dwarf_Die die; |
| 2748 |
while (drgn_dwarf_index_iterator_next(&it, &die, NULL)) { |
| 2749 |
const struct drgn_language *lang; |
| 2750 |
err = drgn_language_from_die(&die, false, &lang); |
| 2751 |
if (err) { |
| 2752 |
drgn_error_destroy(err); |
| 2753 |
continue; |
| 2754 |
} |
| 2755 |
if (lang) |
| 2756 |
return lang; |
| 2757 |
} |
| 2758 |
return NULL; |
| 2759 |
} |
| 2760 |
|
| 2761 |
/* |
| 2762 |
* DIE iteration. |
| 2763 |
*/ |
| 2764 |
|
| 2765 |
DEFINE_VECTOR(dwarf_die_vector, Dwarf_Die); |
| 2766 |
|
| 2767 |
/** Iterator over DWARF DIEs in a @ref drgn_module. */ |
| 2768 |
struct drgn_dwarf_die_iterator { |
| 2769 |
/** Stack of current DIE and its ancestors. */ |
| 2770 |
struct dwarf_die_vector dies; |
| 2771 |
/** |
| 2772 |
* Dwarf handle that we're iterating over. For split DWARF, this is the |
| 2773 |
* main file. |
| 2774 |
*/ |
| 2775 |
Dwarf *dwarf; |
| 2776 |
/** |
| 2777 |
* End of current CU (for bounds checking). For split DWARF, this is in |
| 2778 |
* the split file. |
| 2779 |
*/ |
| 2780 |
const char *cu_end; |
| 2781 |
/** Offset of next CU. For split DWARF, this is in the main file. */ |
| 2782 |
Dwarf_Off next_cu_off; |
| 2783 |
/** Whether current CU is from .debug_types. */ |
| 2784 |
bool debug_types; |
| 2785 |
}; |
| 2786 |
|
| 2787 |
static void drgn_dwarf_die_iterator_init(struct drgn_dwarf_die_iterator *it, |
| 2788 |
Dwarf *dwarf) |
| 2789 |
{ |
| 2790 |
dwarf_die_vector_init(&it->dies); |
| 2791 |
it->dwarf = dwarf; |
| 2792 |
it->next_cu_off = 0; |
| 2793 |
it->debug_types = false; |
| 2794 |
} |
| 2795 |
|
| 2796 |
static void drgn_dwarf_die_iterator_deinit(struct drgn_dwarf_die_iterator *it) |
| 2797 |
{ |
| 2798 |
dwarf_die_vector_deinit(&it->dies); |
| 2799 |
} |
| 2800 |
|
| 2801 |
/** |
| 2802 |
* Return the next DWARF DIE in a @ref drgn_dwarf_die_iterator. |
| 2803 |
* |
| 2804 |
* The first call returns the top-level DIE for the first unit in the module. |
| 2805 |
* Subsequent calls return children, siblings, and unit DIEs. |
| 2806 |
* |
| 2807 |
* This includes the .debug_types section. |
| 2808 |
* |
| 2809 |
* @param[in,out] it Iterator containing the returned DIE and its ancestors. The |
| 2810 |
* last entry in `it->dies` is the DIE itself, the entry before that is its |
| 2811 |
* parent, the entry before that is its grandparent, etc. |
| 2812 |
* @param[in] children If @c true and the last returned DIE has children, return |
| 2813 |
* its first child (this is a pre-order traversal). Otherwise, return the next |
| 2814 |
* DIE at the level less than or equal to the last returned DIE, i.e., the last |
| 2815 |
* returned DIE's sibling, or its ancestor's sibling, or the next top-level unit |
| 2816 |
* DIE. |
| 2817 |
* @param[in] subtree If zero, iterate over all DIEs in all units. If non-zero, |
| 2818 |
* stop after returning all DIEs in the subtree rooted at the DIE that was |
| 2819 |
* returned in the last call as entry `subtree - 1` in `it->dies`. |
| 2820 |
* @return @c NULL on success, `&drgn_stop` if there are no more DIEs, in which |
| 2821 |
* case the size of `it->dies` equals @p subtree and `it->dies` refers to the |
| 2822 |
* root of the iterated subtree, non-@c NULL on error, in which case this should |
| 2823 |
* not be called again. |
| 2824 |
*/ |
| 2825 |
static struct drgn_error * |
| 2826 |
drgn_dwarf_die_iterator_next(struct drgn_dwarf_die_iterator *it, bool children, |
| 2827 |
size_t subtree) |
| 2828 |
{ |
| 2829 |
#define TOP() (dwarf_die_vector_last(&it->dies)) |
| 2830 |
int r; |
| 2831 |
Dwarf_Die die; |
| 2832 |
assert(subtree <= dwarf_die_vector_size(&it->dies)); |
| 2833 |
if (dwarf_die_vector_empty(&it->dies)) { |
| 2834 |
/* This is the first call. Get the first unit DIE. */ |
| 2835 |
if (!dwarf_die_vector_append_entry(&it->dies)) |
| 2836 |
return &drgn_enomem; |
| 2837 |
} else { |
| 2838 |
if (children) { |
| 2839 |
r = dwarf_child(TOP(), &die); |
| 2840 |
if (r == 0) { |
| 2841 |
/* The previous DIE has a child. Return it. */ |
| 2842 |
if (!dwarf_die_vector_append(&it->dies, &die)) |
| 2843 |
return &drgn_enomem; |
| 2844 |
return NULL; |
| 2845 |
} else if (r < 0) { |
| 2846 |
return drgn_error_libdw(); |
| 2847 |
} |
| 2848 |
/* The previous DIE has no children. */ |
| 2849 |
} |
| 2850 |
|
| 2851 |
if (dwarf_die_vector_size(&it->dies) == subtree) { |
| 2852 |
/* |
| 2853 |
* The previous DIE is the root of the subtree. We're |
| 2854 |
* done. |
| 2855 |
*/ |
| 2856 |
return &drgn_stop; |
| 2857 |
} |
| 2858 |
|
| 2859 |
if (dwarf_die_vector_size(&it->dies) > 1) { |
| 2860 |
r = dwarf_siblingof(TOP(), &die); |
| 2861 |
if (r == 0) { |
| 2862 |
/* The previous DIE has a sibling. Return it. */ |
| 2863 |
*TOP() = die; |
| 2864 |
return NULL; |
| 2865 |
} else if (r > 0) { |
| 2866 |
if (!die.addr) |
| 2867 |
goto next_unit; |
| 2868 |
/* |
| 2869 |
* The previous DIE is the last child of its |
| 2870 |
* parent. |
| 2871 |
*/ |
| 2872 |
char *addr = die.addr; |
| 2873 |
do { |
| 2874 |
/* |
| 2875 |
* addr points to the null terminator |
| 2876 |
* for the list of siblings. Go back up |
| 2877 |
* to its parent. The next byte is |
| 2878 |
* either the parent's sibling or |
| 2879 |
* another null terminator. |
| 2880 |
*/ |
| 2881 |
dwarf_die_vector_pop(&it->dies); |
| 2882 |
addr++; |
| 2883 |
if (dwarf_die_vector_size(&it->dies) |
| 2884 |
== subtree) { |
| 2885 |
/* |
| 2886 |
* We're back to the root of the |
| 2887 |
* subtree. We're done. |
| 2888 |
*/ |
| 2889 |
return &drgn_stop; |
| 2890 |
} |
| 2891 |
if (dwarf_die_vector_size(&it->dies) == 1 |
| 2892 |
|| addr >= it->cu_end) |
| 2893 |
goto next_unit; |
| 2894 |
} while (*addr == '\0'); |
| 2895 |
/* |
| 2896 |
* addr now points to the next DIE. Return it. |
| 2897 |
*/ |
| 2898 |
*TOP() = (Dwarf_Die){ |
| 2899 |
.cu = dwarf_die_vector_first(&it->dies)->cu, |
| 2900 |
.addr = addr, |
| 2901 |
}; |
| 2902 |
return NULL; |
| 2903 |
} else { |
| 2904 |
return drgn_error_libdw(); |
| 2905 |
} |
| 2906 |
} |
| 2907 |
} |
| 2908 |
|
| 2909 |
next_unit:; |
| 2910 |
/* There are no more DIEs in the current unit. */ |
| 2911 |
Dwarf_Off cu_off = it->next_cu_off; |
| 2912 |
size_t cu_header_size; |
| 2913 |
uint64_t type_signature; |
| 2914 |
r = dwarf_next_unit(it->dwarf, cu_off, &it->next_cu_off, |
| 2915 |
&cu_header_size, NULL, NULL, NULL, NULL, |
| 2916 |
it->debug_types ? &type_signature : NULL, NULL); |
| 2917 |
if (r == 0) { |
| 2918 |
/* Got the next unit. Return the unit DIE. */ |
| 2919 |
Dwarf_Off offset = cu_off + cu_header_size; |
| 2920 |
if (it->debug_types) |
| 2921 |
r = !dwarf_offdie_types(it->dwarf, offset, TOP()); |
| 2922 |
else |
| 2923 |
r = !dwarf_offdie(it->dwarf, offset, TOP()); |
| 2924 |
if (r) |
| 2925 |
return drgn_error_libdw(); |
| 2926 |
Dwarf_Off cu_end_off = it->next_cu_off; |
| 2927 |
#if _ELFUTILS_PREREQ(0, 171) |
| 2928 |
// If the unit is a skeleton, replace it with the split unit. |
| 2929 |
Dwarf_Die subdie; |
| 2930 |
uint8_t unit_type; |
| 2931 |
if (dwarf_cu_info(TOP()->cu, NULL, &unit_type, NULL, &subdie, |
| 2932 |
NULL, NULL, NULL)) |
| 2933 |
return drgn_error_libdw(); |
| 2934 |
if (unit_type == DW_UT_skeleton && subdie.cu) { |
| 2935 |
offset = dwarf_dieoffset(&subdie); |
| 2936 |
if (dwarf_next_unit(dwarf_cu_getdwarf(subdie.cu), |
| 2937 |
offset - dwarf_cuoffset(&subdie), |
| 2938 |
&cu_end_off, NULL, NULL, NULL, NULL, |
| 2939 |
NULL, NULL, NULL)) |
| 2940 |
return drgn_error_libdw(); |
| 2941 |
*TOP() = subdie; |
| 2942 |
} |
| 2943 |
#endif |
| 2944 |
it->cu_end = ((const char *)TOP()->addr |
| 2945 |
- offset |
| 2946 |
+ cu_end_off); |
| 2947 |
return NULL; |
| 2948 |
} else if (r > 0) { |
| 2949 |
// Note that in split DWARF, there are no skeleton units for |
| 2950 |
// type units, and the main file doesn't have a .debug_types |
| 2951 |
// section. Instead, the split files contain a .debug_types.dwo |
| 2952 |
// section per type unit. elfutils as of 0.189 doesn't support |
| 2953 |
// multiple sections with the same name, so we don't support |
| 2954 |
// type units with split DWARF. |
| 2955 |
if (!it->debug_types) { |
| 2956 |
it->next_cu_off = 0; |
| 2957 |
it->debug_types = true; |
| 2958 |
goto next_unit; |
| 2959 |
} |
| 2960 |
/* There are no more units. */ |
| 2961 |
dwarf_die_vector_pop(&it->dies); |
| 2962 |
return &drgn_stop; |
| 2963 |
} else { |
| 2964 |
return drgn_error_libdw(); |
| 2965 |
} |
| 2966 |
#undef TOP |
| 2967 |
} |
| 2968 |
|
| 2969 |
struct drgn_error *drgn_module_find_dwarf_scopes(struct drgn_module *module, |
| 2970 |
uint64_t pc, |
| 2971 |
uint64_t *bias_ret, |
| 2972 |
Dwarf_Die **dies_ret, |
| 2973 |
size_t *length_ret) |
| 2974 |
{ |
| 2975 |
struct drgn_error *err; |
| 2976 |
|
| 2977 |
if (!module->debug_file) { |
| 2978 |
*bias_ret = 0; |
| 2979 |
*dies_ret = NULL; |
| 2980 |
*length_ret = 0; |
| 2981 |
return NULL; |
| 2982 |
} |
| 2983 |
Dwarf *dwarf; |
| 2984 |
err = drgn_elf_file_get_dwarf(module->debug_file, &dwarf); |
| 2985 |
if (err) |
| 2986 |
return err; |
| 2987 |
*bias_ret = module->debug_file_bias; |
| 2988 |
pc -= module->debug_file_bias; |
| 2989 |
|
| 2990 |
/* First, try to get the CU containing the PC. */ |
| 2991 |
Dwarf_Aranges *aranges; |
| 2992 |
size_t naranges; |
| 2993 |
if (dwarf_getaranges(dwarf, &aranges, &naranges) < 0) |
| 2994 |
return drgn_error_libdw(); |
| 2995 |
|
| 2996 |
_cleanup_(drgn_dwarf_die_iterator_deinit) |
| 2997 |
struct drgn_dwarf_die_iterator it; |
| 2998 |
drgn_dwarf_die_iterator_init(&it, dwarf); |
| 2999 |
size_t subtree; |
| 3000 |
Dwarf_Off offset; |
| 3001 |
if (dwarf_getarangeinfo(dwarf_getarange_addr(aranges, pc), NULL, NULL, |
| 3002 |
&offset) >= 0) { |
| 3003 |
Dwarf_Die *cu_die = dwarf_die_vector_append_entry(&it.dies); |
| 3004 |
if (!cu_die) |
| 3005 |
return &drgn_enomem; |
| 3006 |
if (!dwarf_offdie(dwarf, offset, cu_die)) |
| 3007 |
return drgn_error_libdw(); |
| 3008 |
#if _ELFUTILS_PREREQ(0, 171) |
| 3009 |
// If the unit is a skeleton, replace it with the split unit. |
| 3010 |
Dwarf_Die subdie; |
| 3011 |
uint8_t unit_type; |
| 3012 |
if (dwarf_cu_info(cu_die->cu, NULL, &unit_type, NULL, &subdie, |
| 3013 |
NULL, NULL, NULL)) |
| 3014 |
return drgn_error_libdw(); |
| 3015 |
if (unit_type == DW_UT_skeleton && subdie.cu) { |
| 3016 |
dwarf = dwarf_cu_getdwarf(subdie.cu); |
| 3017 |
offset = dwarf_dieoffset(&subdie); |
| 3018 |
*cu_die = subdie; |
| 3019 |
} |
| 3020 |
#endif |
| 3021 |
if (dwarf_next_unit(dwarf, offset - dwarf_cuoffset(cu_die), |
| 3022 |
&it.next_cu_off, NULL, NULL, NULL, NULL, |
| 3023 |
NULL, NULL, NULL)) |
| 3024 |
return drgn_error_libdw(); |
| 3025 |
it.cu_end = ((const char *)cu_die->addr |
| 3026 |
- offset |
| 3027 |
+ it.next_cu_off); |
| 3028 |
subtree = 1; |
| 3029 |
} else { |
| 3030 |
/* |
| 3031 |
* Range was not found. .debug_aranges could be missing or |
| 3032 |
* incomplete, so fall back to checking each CU. |
| 3033 |
*/ |
| 3034 |
subtree = 0; |
| 3035 |
} |
| 3036 |
|
| 3037 |
/* |
| 3038 |
* Now find the most specific DIE containing the PC. Entry subtree - 1 |
| 3039 |
* in it.dies is the most specific DIE we have found so far. We iterate |
| 3040 |
* over all of its children until we find a more specific DIE, then we |
| 3041 |
* descend into that one. |
| 3042 |
* |
| 3043 |
* We only want to descend into children DIEs when the last DIE |
| 3044 |
* contained the PC, which is when size(it.dies) == subtree. |
| 3045 |
*/ |
| 3046 |
while (!(err = drgn_dwarf_die_iterator_next(&it, |
| 3047 |
dwarf_die_vector_size(&it.dies) |
| 3048 |
== subtree, |
| 3049 |
subtree))) { |
| 3050 |
int r = dwarf_haspc(dwarf_die_vector_last(&it.dies), pc); |
| 3051 |
if (r > 0) { |
| 3052 |
subtree = dwarf_die_vector_size(&it.dies); |
| 3053 |
} else if (r < 0) { |
| 3054 |
return drgn_error_libdw(); |
| 3055 |
} |
| 3056 |
} |
| 3057 |
if (err != &drgn_stop) |
| 3058 |
return err; |
| 3059 |
|
| 3060 |
dwarf_die_vector_steal(&it.dies, dies_ret, length_ret); |
| 3061 |
return NULL; |
| 3062 |
} |
| 3063 |
|
| 3064 |
struct drgn_error *drgn_find_die_ancestors(Dwarf_Die *die, Dwarf_Die **dies_ret, |
| 3065 |
size_t *length_ret) |
| 3066 |
{ |
| 3067 |
Dwarf *dwarf = dwarf_cu_getdwarf(die->cu); |
| 3068 |
if (!dwarf) |
| 3069 |
return drgn_error_libdw(); |
| 3070 |
|
| 3071 |
VECTOR(dwarf_die_vector, dies); |
| 3072 |
Dwarf_Die *cu_die = dwarf_die_vector_append_entry(&dies); |
| 3073 |
if (!cu_die) |
| 3074 |
return &drgn_enomem; |
| 3075 |
|
| 3076 |
Dwarf_Half cu_version; |
| 3077 |
Dwarf_Off type_offset; |
| 3078 |
if (!dwarf_cu_die(die->cu, cu_die, &cu_version, NULL, NULL, NULL, NULL, |
| 3079 |
&type_offset)) |
| 3080 |
return drgn_error_libdw(); |
| 3081 |
Dwarf_Off cu_die_offset = dwarf_dieoffset(cu_die); |
| 3082 |
bool debug_types = cu_version == 4 && type_offset != 0; |
| 3083 |
Dwarf_Off next_cu_offset; |
| 3084 |
uint64_t type_signature; |
| 3085 |
if (dwarf_next_unit(dwarf, cu_die_offset - dwarf_cuoffset(cu_die), |
| 3086 |
&next_cu_offset, NULL, NULL, NULL, NULL, NULL, |
| 3087 |
debug_types ? &type_signature : NULL, NULL)) |
| 3088 |
return drgn_error_libdw(); |
| 3089 |
const unsigned char *cu_end = |
| 3090 |
(unsigned char *)cu_die->addr - cu_die_offset + next_cu_offset; |
| 3091 |
|
| 3092 |
#define TOP() (dwarf_die_vector_last(&dies)) |
| 3093 |
while ((char *)TOP()->addr <= (char *)die->addr) { |
| 3094 |
if (TOP()->addr == die->addr) { |
| 3095 |
dwarf_die_vector_steal(&dies, dies_ret, length_ret); |
| 3096 |
(*length_ret)--; |
| 3097 |
return NULL; |
| 3098 |
} |
| 3099 |
|
| 3100 |
Dwarf_Attribute attr; |
| 3101 |
if (dwarf_attr(TOP(), DW_AT_sibling, &attr)) { |
| 3102 |
/* The top DIE has a DW_AT_sibling attribute. */ |
| 3103 |
Dwarf_Die sibling; |
| 3104 |
if (!dwarf_formref_die(&attr, &sibling)) |
| 3105 |
return drgn_error_libdw(); |
| 3106 |
if (sibling.cu != TOP()->cu || |
| 3107 |
(char *)sibling.addr <= (char *)TOP()->addr) |
| 3108 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3109 |
"invalid DW_AT_sibling"); |
| 3110 |
|
| 3111 |
if ((char *)sibling.addr > (char *)die->addr) { |
| 3112 |
/* |
| 3113 |
* The top DIE's sibling is after the target |
| 3114 |
* DIE. Therefore, the target DIE must be a |
| 3115 |
* descendant of the top DIE. |
| 3116 |
*/ |
| 3117 |
Dwarf_Die *child = |
| 3118 |
dwarf_die_vector_append_entry(&dies); |
| 3119 |
if (!child) |
| 3120 |
return &drgn_enomem; |
| 3121 |
int r = dwarf_child(TOP() - 1, child); |
| 3122 |
if (r < 0) { |
| 3123 |
return drgn_error_libdw(); |
| 3124 |
} else if (r > 0) { |
| 3125 |
/* |
| 3126 |
* The top DIE didn't have any children, |
| 3127 |
* which should be impossible. |
| 3128 |
*/ |
| 3129 |
goto not_found; |
| 3130 |
} |
| 3131 |
} else { |
| 3132 |
/* |
| 3133 |
* The top DIE's sibling is before or equal to |
| 3134 |
* the target DIE. Therefore, the target DIE |
| 3135 |
* isn't a descendant of the top DIE. Skip to |
| 3136 |
* the sibling. |
| 3137 |
*/ |
| 3138 |
*TOP() = sibling; |
| 3139 |
} |
| 3140 |
} else { |
| 3141 |
/* |
| 3142 |
* The top DIE does not have a DW_AT_sibling attribute. |
| 3143 |
* Instead, we found the end of the top DIE. |
| 3144 |
*/ |
| 3145 |
unsigned char *addr = attr.valp; |
| 3146 |
if (!addr || addr >= cu_end) |
| 3147 |
goto not_found; |
| 3148 |
|
| 3149 |
/* |
| 3150 |
* If the top DIE has children, then addr is its first |
| 3151 |
* child. Otherwise, then addr is its sibling. (Unless |
| 3152 |
* it is a null terminator.) |
| 3153 |
*/ |
| 3154 |
size_t new_size = dwarf_die_vector_size(&dies); |
| 3155 |
if (dwarf_haschildren(TOP()) > 0) |
| 3156 |
new_size++; |
| 3157 |
|
| 3158 |
while (*addr == '\0') { |
| 3159 |
/* |
| 3160 |
* addr points to the null terminator for the |
| 3161 |
* list of siblings. Go back up to its parent. |
| 3162 |
* The next byte is either the parent's sibling |
| 3163 |
* or another null terminator. |
| 3164 |
*/ |
| 3165 |
new_size--; |
| 3166 |
addr++; |
| 3167 |
if (new_size <= 1 || addr >= cu_end) |
| 3168 |
goto not_found; |
| 3169 |
} |
| 3170 |
|
| 3171 |
/* addr now points to the next DIE. Go to it. */ |
| 3172 |
if (new_size > dwarf_die_vector_size(&dies)) { |
| 3173 |
if (!dwarf_die_vector_append_entry(&dies)) |
| 3174 |
return &drgn_enomem; |
| 3175 |
} else { |
| 3176 |
dwarf_die_vector_resize(&dies, new_size); |
| 3177 |
} |
| 3178 |
*TOP() = (Dwarf_Die){ |
| 3179 |
.cu = dwarf_die_vector_first(&dies)->cu, |
| 3180 |
.addr = addr, |
| 3181 |
}; |
| 3182 |
} |
| 3183 |
} |
| 3184 |
#undef TOP |
| 3185 |
|
| 3186 |
not_found: |
| 3187 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3188 |
"could not find DWARF DIE ancestors"); |
| 3189 |
} |
| 3190 |
|
| 3191 |
/* |
| 3192 |
* Location lists. |
| 3193 |
*/ |
| 3194 |
|
| 3195 |
static struct drgn_error *drgn_dwarf_next_addrx(struct binary_buffer *bb, |
| 3196 |
struct drgn_elf_file *file, |
| 3197 |
Dwarf_Die *cu_die, |
| 3198 |
uint8_t address_size, |
| 3199 |
const char **addr_base, |
| 3200 |
uint64_t *ret) |
| 3201 |
{ |
| 3202 |
struct drgn_error *err; |
| 3203 |
|
| 3204 |
// For split DWARF, .debug_addr is in the main debug file. |
| 3205 |
file = file->module->debug_file; |
| 3206 |
|
| 3207 |
// addr_base is a cache of the address table base for the compilation |
| 3208 |
// unit. |
| 3209 |
if (!*addr_base) { |
| 3210 |
Dwarf_Attribute attr_mem, *attr; |
| 3211 |
if (!(attr = dwarf_attr_integrate(cu_die, DW_AT_addr_base, |
| 3212 |
&attr_mem)) && |
| 3213 |
!(attr = dwarf_attr_integrate(cu_die, DW_AT_GNU_addr_base, |
| 3214 |
&attr_mem))) { |
| 3215 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3216 |
"indirect address without DW_AT_addr_base"); |
| 3217 |
} |
| 3218 |
Dwarf_Word base; |
| 3219 |
if (dwarf_formudata(attr, &base)) |
| 3220 |
return drgn_error_libdw(); |
| 3221 |
|
| 3222 |
if (!file->scns[DRGN_SCN_DEBUG_ADDR]) { |
| 3223 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3224 |
"indirect address without .debug_addr section"); |
| 3225 |
} |
| 3226 |
Elf_Data *data; |
| 3227 |
err = drgn_elf_file_read_section(file, DRGN_SCN_DEBUG_ADDR, &data); |
| 3228 |
if (err) |
| 3229 |
return err; |
| 3230 |
|
| 3231 |
if (base > data->d_size) { |
| 3232 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3233 |
"DW_AT_addr_base is out of bounds"); |
| 3234 |
} |
| 3235 |
|
| 3236 |
*addr_base = (char *)data->d_buf + base; |
| 3237 |
// In DWARF 5, there is a header immediately before addr_base, |
| 3238 |
// which ends with a segment selector size. We don't support a |
| 3239 |
// segment selector yet. In GNU Debug Fission, .debug_addr |
| 3240 |
// doesn't contain any headers or segment selectors. |
| 3241 |
if (attr->code != DW_AT_GNU_addr_base) { |
| 3242 |
if (base == 0) { |
| 3243 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3244 |
"DW_AT_addr_base is out of bounds"); |
| 3245 |
} |
| 3246 |
uint8_t segment_selector_size = ((uint8_t *)*addr_base)[-1]; |
| 3247 |
if (segment_selector_size != 0) { |
| 3248 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 3249 |
"unsupported segment selector size %" PRIu8, |
| 3250 |
segment_selector_size); |
| 3251 |
} |
| 3252 |
} |
| 3253 |
} |
| 3254 |
|
| 3255 |
uint64_t index; |
| 3256 |
if ((err = binary_buffer_next_uleb128(bb, &index))) |
| 3257 |
return err; |
| 3258 |
|
| 3259 |
// The data must was cached when we cached addr_base. |
| 3260 |
Elf_Data *data = file->scn_data[DRGN_SCN_DEBUG_ADDR]; |
| 3261 |
if (index >= |
| 3262 |
((char *)data->d_buf + data->d_size - *addr_base) / address_size) { |
| 3263 |
return binary_buffer_error(bb, |
| 3264 |
"address index is out of bounds"); |
| 3265 |
} |
| 3266 |
copy_lsbytes(ret, sizeof(*ret), HOST_LITTLE_ENDIAN, |
| 3267 |
*addr_base + index * address_size, address_size, |
| 3268 |
drgn_elf_file_is_little_endian(file)); |
| 3269 |
return NULL; |
| 3270 |
} |
| 3271 |
|
| 3272 |
static struct drgn_error *drgn_dwarf_read_loclistx(struct drgn_elf_file *file, |
| 3273 |
Dwarf_Die *cu_die, |
| 3274 |
uint8_t offset_size, |
| 3275 |
Dwarf_Word index, |
| 3276 |
Dwarf_Word *ret) |
| 3277 |
{ |
| 3278 |
struct drgn_error *err; |
| 3279 |
|
| 3280 |
assert(offset_size == 4 || offset_size == 8); |
| 3281 |
|
| 3282 |
Dwarf_Attribute attr_mem, *attr; |
| 3283 |
Dwarf_Word base; |
| 3284 |
if ((attr = dwarf_attr(cu_die, DW_AT_loclists_base, &attr_mem))) { |
| 3285 |
if (dwarf_formudata(attr, &base)) |
| 3286 |
return drgn_error_libdw(); |
| 3287 |
} else { |
| 3288 |
// The DWARF 5 specification doesn't say what it means if there |
| 3289 |
// is no DW_AT_loclists_base. In practice, it seems like split |
| 3290 |
// units don't have DW_AT_loclist_base, and the base is intended |
| 3291 |
// to be the first entry in .debug_loclists immediately after |
| 3292 |
// the first header. |
| 3293 |
base = offset_size == 8 ? 20 : 12; |
| 3294 |
} |
| 3295 |
Dwarf_Off dwp_offset; |
| 3296 |
if (dwarf_cu_dwp_section_info(cu_die->cu, DW_SECT_LOCLISTS, &dwp_offset, |
| 3297 |
NULL)) |
| 3298 |
return drgn_error_libdw(); |
| 3299 |
base += dwp_offset; |
| 3300 |
|
| 3301 |
if (!file->scns[DRGN_SCN_DEBUG_LOCLISTS]) { |
| 3302 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3303 |
"DW_FORM_loclistx without .debug_loclists section"); |
| 3304 |
} |
| 3305 |
Elf_Data *data; |
| 3306 |
err = drgn_elf_file_read_section(file, DRGN_SCN_DEBUG_LOCLISTS, &data); |
| 3307 |
if (err) |
| 3308 |
return err; |
| 3309 |
|
| 3310 |
if (base > data->d_size) { |
| 3311 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3312 |
"DW_AT_loclists_base is out of bounds"); |
| 3313 |
} |
| 3314 |
if (index >= (data->d_size - base) / offset_size) { |
| 3315 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3316 |
"DW_FORM_loclistx is out of bounds"); |
| 3317 |
} |
| 3318 |
const char *basep = (char *)data->d_buf + base; |
| 3319 |
if (offset_size == 8) { |
| 3320 |
uint64_t offset; |
| 3321 |
memcpy(&offset, (uint64_t *)basep + index, sizeof(offset)); |
| 3322 |
if (drgn_elf_file_bswap(file)) |
| 3323 |
offset = bswap_64(offset); |
| 3324 |
*ret = base + offset; |
| 3325 |
} else { |
| 3326 |
uint32_t offset; |
| 3327 |
memcpy(&offset, (uint32_t *)basep + index, sizeof(offset)); |
| 3328 |
if (drgn_elf_file_bswap(file)) |
| 3329 |
offset = bswap_32(offset); |
| 3330 |
*ret = base + offset; |
| 3331 |
} |
| 3332 |
return NULL; |
| 3333 |
} |
| 3334 |
|
| 3335 |
static struct drgn_error *drgn_dwarf5_location_list(struct drgn_elf_file *file, |
| 3336 |
Dwarf_Word offset, |
| 3337 |
Dwarf_Die *cu_die, |
| 3338 |
uint8_t address_size, |
| 3339 |
uint64_t pc, |
| 3340 |
const char **expr_ret, |
| 3341 |
size_t *expr_size_ret) |
| 3342 |
{ |
| 3343 |
struct drgn_error *err; |
| 3344 |
|
| 3345 |
if (!file->scns[DRGN_SCN_DEBUG_LOCLISTS]) { |
| 3346 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3347 |
"loclist without .debug_loclists section"); |
| 3348 |
} |
| 3349 |
struct drgn_elf_file_section_buffer buffer; |
| 3350 |
err = drgn_elf_file_section_buffer_read(&buffer, file, |
| 3351 |
DRGN_SCN_DEBUG_LOCLISTS); |
| 3352 |
if (err) |
| 3353 |
return err; |
| 3354 |
if (offset > buffer.bb.end - buffer.bb.pos) { |
| 3355 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3356 |
"loclist is out of bounds"); |
| 3357 |
} |
| 3358 |
buffer.bb.pos += offset; |
| 3359 |
|
| 3360 |
const char *addr_base = NULL; |
| 3361 |
uint64_t base; |
| 3362 |
bool base_valid = false; |
| 3363 |
/* Default is unknown. May be overridden by DW_LLE_default_location. */ |
| 3364 |
*expr_ret = NULL; |
| 3365 |
*expr_size_ret = 0; |
| 3366 |
for (;;) { |
| 3367 |
uint8_t kind; |
| 3368 |
if ((err = binary_buffer_next_u8(&buffer.bb, &kind))) |
| 3369 |
return err; |
| 3370 |
uint64_t start, length, expr_size; |
| 3371 |
switch (kind) { |
| 3372 |
case DW_LLE_end_of_list: |
| 3373 |
return NULL; |
| 3374 |
case DW_LLE_base_addressx: |
| 3375 |
if ((err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3376 |
cu_die, address_size, |
| 3377 |
&addr_base, &base))) |
| 3378 |
return err; |
| 3379 |
base_valid = true; |
| 3380 |
break; |
| 3381 |
case DW_LLE_startx_endx: |
| 3382 |
if ((err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3383 |
cu_die, address_size, |
| 3384 |
&addr_base, &start)) || |
| 3385 |
(err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3386 |
cu_die, address_size, |
| 3387 |
&addr_base, &length))) |
| 3388 |
return err; |
| 3389 |
length -= start; |
| 3390 |
counted_location_description: |
| 3391 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 3392 |
&expr_size))) |
| 3393 |
return err; |
| 3394 |
if (expr_size > buffer.bb.end - buffer.bb.pos) { |
| 3395 |
return binary_buffer_error(&buffer.bb, |
| 3396 |
"location description size is out of bounds"); |
| 3397 |
} |
| 3398 |
if (pc >= start && pc - start < length) { |
| 3399 |
*expr_ret = buffer.bb.pos; |
| 3400 |
*expr_size_ret = expr_size; |
| 3401 |
return NULL; |
| 3402 |
} |
| 3403 |
buffer.bb.pos += expr_size; |
| 3404 |
break; |
| 3405 |
case DW_LLE_startx_length: |
| 3406 |
if ((err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3407 |
cu_die, address_size, |
| 3408 |
&addr_base, &start)) || |
| 3409 |
(err = binary_buffer_next_uleb128(&buffer.bb, |
| 3410 |
&length))) |
| 3411 |
return err; |
| 3412 |
goto counted_location_description; |
| 3413 |
case DW_LLE_offset_pair: |
| 3414 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 3415 |
&start)) || |
| 3416 |
(err = binary_buffer_next_uleb128(&buffer.bb, |
| 3417 |
&length))) |
| 3418 |
return err; |
| 3419 |
length -= start; |
| 3420 |
if (!base_valid) { |
| 3421 |
Dwarf_Addr low_pc; |
| 3422 |
if (dwarf_lowpc(cu_die, &low_pc)) |
| 3423 |
return drgn_error_libdw(); |
| 3424 |
base = low_pc; |
| 3425 |
base_valid = true; |
| 3426 |
} |
| 3427 |
start += base; |
| 3428 |
goto counted_location_description; |
| 3429 |
case DW_LLE_default_location: |
| 3430 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 3431 |
&expr_size))) |
| 3432 |
return err; |
| 3433 |
if (expr_size > buffer.bb.end - buffer.bb.pos) { |
| 3434 |
return binary_buffer_error(&buffer.bb, |
| 3435 |
"location description size is out of bounds"); |
| 3436 |
} |
| 3437 |
*expr_ret = buffer.bb.pos; |
| 3438 |
*expr_size_ret = expr_size; |
| 3439 |
buffer.bb.pos += expr_size; |
| 3440 |
break; |
| 3441 |
case DW_LLE_base_address: |
| 3442 |
if ((err = binary_buffer_next_uint(&buffer.bb, |
| 3443 |
address_size, |
| 3444 |
&base))) |
| 3445 |
return err; |
| 3446 |
base_valid = true; |
| 3447 |
break; |
| 3448 |
case DW_LLE_start_end: |
| 3449 |
if ((err = binary_buffer_next_uint(&buffer.bb, |
| 3450 |
address_size, |
| 3451 |
&start)) || |
| 3452 |
(err = binary_buffer_next_uint(&buffer.bb, |
| 3453 |
address_size, |
| 3454 |
&length))) |
| 3455 |
return err; |
| 3456 |
length -= start; |
| 3457 |
goto counted_location_description; |
| 3458 |
case DW_LLE_start_length: |
| 3459 |
if ((err = binary_buffer_next_uint(&buffer.bb, |
| 3460 |
address_size, |
| 3461 |
&start)) || |
| 3462 |
(err = binary_buffer_next_uleb128(&buffer.bb, |
| 3463 |
&length))) |
| 3464 |
return err; |
| 3465 |
goto counted_location_description; |
| 3466 |
default: |
| 3467 |
return binary_buffer_error(&buffer.bb, |
| 3468 |
"unknown location list entry kind %#" PRIx8, |
| 3469 |
kind); |
| 3470 |
} |
| 3471 |
} |
| 3472 |
} |
| 3473 |
|
| 3474 |
static struct drgn_error * |
| 3475 |
drgn_dwarf4_split_location_list(struct drgn_elf_file *file, Dwarf_Word offset, |
| 3476 |
Dwarf_Die *cu_die, uint8_t address_size, |
| 3477 |
uint64_t pc, const char **expr_ret, |
| 3478 |
size_t *expr_size_ret) |
| 3479 |
{ |
| 3480 |
struct drgn_error *err; |
| 3481 |
|
| 3482 |
if (!file->scns[DRGN_SCN_DEBUG_LOC]) { |
| 3483 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3484 |
"loclistptr without .debug_loc section"); |
| 3485 |
} |
| 3486 |
Dwarf_Off dwp_offset; |
| 3487 |
if (dwarf_cu_dwp_section_info(cu_die->cu, DW_SECT_LOCLISTS, &dwp_offset, |
| 3488 |
NULL)) |
| 3489 |
return drgn_error_libdw(); |
| 3490 |
offset += dwp_offset; |
| 3491 |
struct drgn_elf_file_section_buffer buffer; |
| 3492 |
err = drgn_elf_file_section_buffer_read(&buffer, file, |
| 3493 |
DRGN_SCN_DEBUG_LOC); |
| 3494 |
if (err) |
| 3495 |
return err; |
| 3496 |
if (offset > buffer.bb.end - buffer.bb.pos) { |
| 3497 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3498 |
"loclistptr is out of bounds"); |
| 3499 |
} |
| 3500 |
buffer.bb.pos += offset; |
| 3501 |
|
| 3502 |
const char *addr_base = NULL; |
| 3503 |
uint64_t base; |
| 3504 |
bool base_valid = false; |
| 3505 |
*expr_ret = NULL; |
| 3506 |
*expr_size_ret = 0; |
| 3507 |
for (;;) { |
| 3508 |
uint8_t kind; |
| 3509 |
if ((err = binary_buffer_next_u8(&buffer.bb, &kind))) |
| 3510 |
return err; |
| 3511 |
uint64_t start, length; |
| 3512 |
switch (kind) { |
| 3513 |
// The GNU Debug Fission design document |
| 3514 |
// (https://gcc.gnu.org/wiki/DebugFission) uses slightly |
| 3515 |
// different names for entry kinds than DWARF 5, but they're the |
| 3516 |
// same as the DWARF 5 versions except as noted below. |
| 3517 |
case DW_LLE_end_of_list: // DW_LLE_end_of_list_entry |
| 3518 |
return NULL; |
| 3519 |
case DW_LLE_base_addressx: // DW_LLE_base_address_selection_entry |
| 3520 |
if ((err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3521 |
cu_die, address_size, |
| 3522 |
&addr_base, &base))) |
| 3523 |
return err; |
| 3524 |
base_valid = true; |
| 3525 |
break; |
| 3526 |
case DW_LLE_startx_endx: // DW_LLE_start_end_entry |
| 3527 |
if ((err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3528 |
cu_die, address_size, |
| 3529 |
&addr_base, &start)) || |
| 3530 |
(err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3531 |
cu_die, address_size, |
| 3532 |
&addr_base, &length))) |
| 3533 |
return err; |
| 3534 |
length -= start; |
| 3535 |
counted_location_description:; |
| 3536 |
// Note: this is ULEB128 in DWARF 5. |
| 3537 |
uint16_t expr_size; |
| 3538 |
if ((err = binary_buffer_next_u16(&buffer.bb, |
| 3539 |
&expr_size))) |
| 3540 |
return err; |
| 3541 |
if (expr_size > buffer.bb.end - buffer.bb.pos) { |
| 3542 |
return binary_buffer_error(&buffer.bb, |
| 3543 |
"location description size is out of bounds"); |
| 3544 |
} |
| 3545 |
if (pc >= start && pc - start < length) { |
| 3546 |
*expr_ret = buffer.bb.pos; |
| 3547 |
*expr_size_ret = expr_size; |
| 3548 |
return NULL; |
| 3549 |
} |
| 3550 |
buffer.bb.pos += expr_size; |
| 3551 |
break; |
| 3552 |
case DW_LLE_startx_length: // DW_LLE_start_length_entry |
| 3553 |
if ((err = drgn_dwarf_next_addrx(&buffer.bb, file, |
| 3554 |
cu_die, address_size, |
| 3555 |
&addr_base, &start)) || |
| 3556 |
// Note: this is ULEB128 in DWARF 5. |
| 3557 |
(err = binary_buffer_next_u32_into_u64(&buffer.bb, |
| 3558 |
&length))) |
| 3559 |
return err; |
| 3560 |
goto counted_location_description; |
| 3561 |
case DW_LLE_offset_pair: // DW_LLE_offset_pair_entry |
| 3562 |
// Note: these are ULEB128 in DWARF 5. |
| 3563 |
if ((err = binary_buffer_next_u32_into_u64(&buffer.bb, |
| 3564 |
&start)) || |
| 3565 |
(err = binary_buffer_next_u32_into_u64(&buffer.bb, |
| 3566 |
&length))) |
| 3567 |
return err; |
| 3568 |
length -= start; |
| 3569 |
if (!base_valid) { |
| 3570 |
Dwarf_Addr low_pc; |
| 3571 |
if (dwarf_lowpc(cu_die, &low_pc)) |
| 3572 |
return drgn_error_libdw(); |
| 3573 |
base = low_pc; |
| 3574 |
base_valid = true; |
| 3575 |
} |
| 3576 |
start += base; |
| 3577 |
goto counted_location_description; |
| 3578 |
default: |
| 3579 |
return binary_buffer_error(&buffer.bb, |
| 3580 |
"unknown location list entry kind %#" PRIx8, |
| 3581 |
kind); |
| 3582 |
} |
| 3583 |
} |
| 3584 |
} |
| 3585 |
|
| 3586 |
static struct drgn_error *drgn_dwarf4_location_list(struct drgn_elf_file *file, |
| 3587 |
Dwarf_Word offset, |
| 3588 |
Dwarf_Die *cu_die, |
| 3589 |
uint8_t address_size, |
| 3590 |
uint64_t pc, |
| 3591 |
const char **expr_ret, |
| 3592 |
size_t *expr_size_ret) |
| 3593 |
{ |
| 3594 |
struct drgn_error *err; |
| 3595 |
|
| 3596 |
if (!file->scns[DRGN_SCN_DEBUG_LOC]) { |
| 3597 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3598 |
"loclistptr without .debug_loc section"); |
| 3599 |
} |
| 3600 |
struct drgn_elf_file_section_buffer buffer; |
| 3601 |
err = drgn_elf_file_section_buffer_read(&buffer, file, |
| 3602 |
DRGN_SCN_DEBUG_LOC); |
| 3603 |
if (err) |
| 3604 |
return err; |
| 3605 |
if (offset > buffer.bb.end - buffer.bb.pos) { |
| 3606 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 3607 |
"loclistptr is out of bounds"); |
| 3608 |
} |
| 3609 |
buffer.bb.pos += offset; |
| 3610 |
|
| 3611 |
uint64_t address_max = uint_max(address_size); |
| 3612 |
uint64_t base; |
| 3613 |
bool base_valid = false; |
| 3614 |
for (;;) { |
| 3615 |
uint64_t start, end; |
| 3616 |
if ((err = binary_buffer_next_uint(&buffer.bb, address_size, |
| 3617 |
&start)) || |
| 3618 |
(err = binary_buffer_next_uint(&buffer.bb, address_size, |
| 3619 |
&end))) |
| 3620 |
return err; |
| 3621 |
if (start == 0 && end == 0) { |
| 3622 |
*expr_ret = NULL; |
| 3623 |
*expr_size_ret = 0; |
| 3624 |
return NULL; |
| 3625 |
} else if (start == address_max) { |
| 3626 |
base = end; |
| 3627 |
base_valid = true; |
| 3628 |
} else { |
| 3629 |
if (!base_valid) { |
| 3630 |
Dwarf_Addr low_pc; |
| 3631 |
if (dwarf_lowpc(cu_die, &low_pc)) |
| 3632 |
return drgn_error_libdw(); |
| 3633 |
base = low_pc; |
| 3634 |
base_valid = true; |
| 3635 |
} |
| 3636 |
uint16_t expr_size; |
| 3637 |
if ((err = binary_buffer_next_u16(&buffer.bb, |
| 3638 |
&expr_size))) |
| 3639 |
return err; |
| 3640 |
if (expr_size > buffer.bb.end - buffer.bb.pos) { |
| 3641 |
return binary_buffer_error(&buffer.bb, |
| 3642 |
"location description size is out of bounds"); |
| 3643 |
} |
| 3644 |
if (base + start <= pc && pc < base + end) { |
| 3645 |
*expr_ret = buffer.bb.pos; |
| 3646 |
*expr_size_ret = expr_size; |
| 3647 |
return NULL; |
| 3648 |
} |
| 3649 |
buffer.bb.pos += expr_size; |
| 3650 |
} |
| 3651 |
} |
| 3652 |
} |
| 3653 |
|
| 3654 |
static struct drgn_error * |
| 3655 |
drgn_dwarf_location(struct drgn_elf_file *file, Dwarf_Attribute *attr, |
| 3656 |
const struct drgn_register_state *regs, |
| 3657 |
const char **expr_ret, size_t *expr_size_ret) |
| 3658 |
{ |
| 3659 |
struct drgn_error *err; |
| 3660 |
switch (attr->form) { |
| 3661 |
/* DWARF 3 */ |
| 3662 |
case DW_FORM_data4: |
| 3663 |
case DW_FORM_data8: |
| 3664 |
/* DWARF 4-5 */ |
| 3665 |
case DW_FORM_sec_offset: |
| 3666 |
/* DWARF 5 */ |
| 3667 |
case DW_FORM_loclistx: { |
| 3668 |
Dwarf_Half cu_version; |
| 3669 |
uint8_t unit_type; |
| 3670 |
Dwarf_Die cu_die; |
| 3671 |
uint8_t address_size; |
| 3672 |
uint8_t offset_size; |
| 3673 |
#if _ELFUTILS_PREREQ(0, 171) |
| 3674 |
if (dwarf_cu_info(attr->cu, &cu_version, &unit_type, &cu_die, |
| 3675 |
NULL, NULL, &address_size, &offset_size)) |
| 3676 |
return drgn_error_libdw(); |
| 3677 |
#else |
| 3678 |
unit_type = DW_UT_compile; |
| 3679 |
if (!dwarf_cu_die(attr->cu, &cu_die, &cu_version, NULL, |
| 3680 |
&address_size, &offset_size, NULL, NULL)) |
| 3681 |
return drgn_error_libdw(); |
| 3682 |
#endif |
| 3683 |
if ((err = drgn_check_address_size(address_size))) |
| 3684 |
return err; |
| 3685 |
|
| 3686 |
Dwarf_Word offset; |
| 3687 |
if (dwarf_formudata(attr, &offset)) |
| 3688 |
return drgn_error_libdw(); |
| 3689 |
if (attr->form == DW_FORM_loclistx && |
| 3690 |
((err = drgn_dwarf_read_loclistx(file, &cu_die, offset_size, |
| 3691 |
offset, &offset)))) |
| 3692 |
return err; |
| 3693 |
|
| 3694 |
struct optional_uint64 pc; |
| 3695 |
if (!regs || |
| 3696 |
!(pc = drgn_register_state_get_pc(regs)).has_value) { |
| 3697 |
*expr_ret = NULL; |
| 3698 |
*expr_size_ret = 0; |
| 3699 |
return NULL; |
| 3700 |
} |
| 3701 |
pc.value -= !regs->interrupted + file->module->debug_file_bias; |
| 3702 |
|
| 3703 |
if (cu_version >= 5) { |
| 3704 |
return drgn_dwarf5_location_list(file, offset, &cu_die, |
| 3705 |
address_size, pc.value, |
| 3706 |
expr_ret, |
| 3707 |
expr_size_ret); |
| 3708 |
} else if (unit_type == DW_UT_split_compile |
| 3709 |
|| unit_type == DW_UT_split_type) { |
| 3710 |
return drgn_dwarf4_split_location_list(file, offset, |
| 3711 |
&cu_die, |
| 3712 |
address_size, |
| 3713 |
pc.value, |
| 3714 |
expr_ret, |
| 3715 |
expr_size_ret); |
| 3716 |
} else { |
| 3717 |
return drgn_dwarf4_location_list(file, offset, &cu_die, |
| 3718 |
address_size, pc.value, |
| 3719 |
expr_ret, |
| 3720 |
expr_size_ret); |
| 3721 |
} |
| 3722 |
} |
| 3723 |
default: { |
| 3724 |
Dwarf_Block block; |
| 3725 |
if (dwarf_formblock(attr, &block)) |
| 3726 |
return drgn_error_libdw(); |
| 3727 |
*expr_ret = (char *)block.data; |
| 3728 |
*expr_size_ret = block.length; |
| 3729 |
return NULL; |
| 3730 |
} |
| 3731 |
} |
| 3732 |
} |
| 3733 |
|
| 3734 |
/* |
| 3735 |
* DWARF expressions. |
| 3736 |
*/ |
| 3737 |
|
| 3738 |
/** |
| 3739 |
* Arbitrary limit for number of operations to execute in a DWARF expression to |
| 3740 |
* avoid infinite loops. |
| 3741 |
*/ |
| 3742 |
static const int MAX_DWARF_EXPR_OPS = 10000; |
| 3743 |
|
| 3744 |
/* A DWARF expression and the context it is being evaluated in. */ |
| 3745 |
struct drgn_dwarf_expression_context { |
| 3746 |
struct binary_buffer bb; |
| 3747 |
const char *start; |
| 3748 |
struct drgn_program *prog; |
| 3749 |
struct drgn_elf_file *file; |
| 3750 |
uint8_t address_size; |
| 3751 |
Dwarf_Die cu_die; |
| 3752 |
const char *cu_addr_base; |
| 3753 |
Dwarf_Die *function; |
| 3754 |
const struct drgn_register_state *regs; |
| 3755 |
}; |
| 3756 |
|
| 3757 |
static struct drgn_error * |
| 3758 |
drgn_dwarf_expression_buffer_error(struct binary_buffer *bb, const char *pos, |
| 3759 |
const char *message) |
| 3760 |
{ |
| 3761 |
struct drgn_dwarf_expression_context *ctx = |
| 3762 |
container_of(bb, struct drgn_dwarf_expression_context, bb); |
| 3763 |
return drgn_elf_file_section_error(ctx->file, NULL, NULL, pos, message); |
| 3764 |
} |
| 3765 |
|
| 3766 |
static inline struct drgn_error * |
| 3767 |
drgn_dwarf_expression_context_init(struct drgn_dwarf_expression_context *ctx, |
| 3768 |
struct drgn_program *prog, |
| 3769 |
struct drgn_elf_file *file, Dwarf_CU *cu, |
| 3770 |
Dwarf_Die *function, |
| 3771 |
const struct drgn_register_state *regs, |
| 3772 |
const char *expr, size_t expr_size) |
| 3773 |
{ |
| 3774 |
struct drgn_error *err; |
| 3775 |
binary_buffer_init(&ctx->bb, expr, expr_size, |
| 3776 |
drgn_elf_file_is_little_endian(file), |
| 3777 |
drgn_dwarf_expression_buffer_error); |
| 3778 |
ctx->start = expr; |
| 3779 |
ctx->prog = prog; |
| 3780 |
ctx->file = file; |
| 3781 |
if (cu) { |
| 3782 |
if (!dwarf_cu_die(cu, &ctx->cu_die, NULL, NULL, |
| 3783 |
&ctx->address_size, NULL, NULL, NULL)) |
| 3784 |
return drgn_error_libdw(); |
| 3785 |
if ((err = drgn_check_address_size(ctx->address_size))) |
| 3786 |
return err; |
| 3787 |
} else { |
| 3788 |
ctx->cu_die.addr = NULL; |
| 3789 |
ctx->address_size = drgn_elf_file_address_size(file); |
| 3790 |
} |
| 3791 |
ctx->cu_addr_base = NULL; |
| 3792 |
ctx->function = function; |
| 3793 |
ctx->regs = regs; |
| 3794 |
return NULL; |
| 3795 |
} |
| 3796 |
|
| 3797 |
static struct drgn_error * |
| 3798 |
drgn_dwarf_frame_base(struct drgn_program *prog, struct drgn_elf_file *file, |
| 3799 |
Dwarf_Die *die, const struct drgn_register_state *regs, |
| 3800 |
int *remaining_ops, uint64_t *ret); |
| 3801 |
|
| 3802 |
static struct drgn_error drgn_unknown_dwarf_opcode = { |
| 3803 |
.code = DRGN_ERROR_NOT_IMPLEMENTED, |
| 3804 |
.message = "unknown DWARF expression opcode", |
| 3805 |
}; |
| 3806 |
|
| 3807 |
static bool drgn_dwarf_opcode_is_known(uint8_t opcode) |
| 3808 |
{ |
| 3809 |
#define X(name, _) if (opcode == name) return true; |
| 3810 |
DW_OP_DEFINITIONS |
| 3811 |
#undef X |
| 3812 |
return false; |
| 3813 |
} |
| 3814 |
|
| 3815 |
static struct drgn_error * |
| 3816 |
drgn_handle_unknown_dwarf_opcode(struct drgn_dwarf_expression_context *ctx, |
| 3817 |
uint8_t opcode, |
| 3818 |
bool after_simple_location_description) |
| 3819 |
{ |
| 3820 |
// We warn the first time that we see an opcode that appears to be |
| 3821 |
// valid. |
| 3822 |
static bool warned; |
| 3823 |
enum drgn_log_level log_level = DRGN_LOG_DEBUG; |
| 3824 |
if (drgn_dwarf_opcode_is_known(opcode) |
| 3825 |
&& !__atomic_test_and_set(&warned, __ATOMIC_SEQ_CST)) |
| 3826 |
log_level = DRGN_LOG_WARNING; |
| 3827 |
if (drgn_log_is_enabled(ctx->prog, log_level)) { |
| 3828 |
struct drgn_error *err; |
| 3829 |
char op_buf[DW_OP_STR_BUF_LEN]; |
| 3830 |
err = binary_buffer_error(&ctx->bb, |
| 3831 |
"unknown DWARF expression opcode %s%s; " |
| 3832 |
"please report this to %s", |
| 3833 |
dw_op_str(opcode, op_buf), |
| 3834 |
after_simple_location_description |
| 3835 |
? " after simple location description" |
| 3836 |
: "", |
| 3837 |
PACKAGE_BUGREPORT); |
| 3838 |
drgn_error_log(log_level, ctx->prog, err, ""); |
| 3839 |
drgn_error_destroy(err); |
| 3840 |
} |
| 3841 |
return &drgn_unknown_dwarf_opcode; |
| 3842 |
} |
| 3843 |
|
| 3844 |
/* |
| 3845 |
* Evaluate a DWARF expression up to the next location description operation or |
| 3846 |
* operation that can't be evaluated in the given context. |
| 3847 |
* |
| 3848 |
* Returns &drgn_not_found if it tried to use an unknown register value. |
| 3849 |
*/ |
| 3850 |
static struct drgn_error * |
| 3851 |
drgn_eval_dwarf_expression(struct drgn_dwarf_expression_context *ctx, |
| 3852 |
struct uint64_vector *stack, |
| 3853 |
int *remaining_ops) |
| 3854 |
{ |
| 3855 |
struct drgn_error *err; |
| 3856 |
bool little_endian = |
| 3857 |
drgn_elf_file_is_little_endian(ctx->file); |
| 3858 |
uint8_t address_size = ctx->address_size; |
| 3859 |
uint8_t address_bits = address_size * CHAR_BIT; |
| 3860 |
uint64_t address_mask = uint_max(address_size); |
| 3861 |
const struct drgn_register_layout *register_layout = |
| 3862 |
ctx->file->platform.arch->register_layout; |
| 3863 |
drgn_register_number (*dwarf_regno_to_internal)(uint64_t) = |
| 3864 |
ctx->file->platform.arch->dwarf_regno_to_internal; |
| 3865 |
|
| 3866 |
#define CHECK(n) do { \ |
| 3867 |
size_t _n = (n); \ |
| 3868 |
if (uint64_vector_size(stack) < _n) { \ |
| 3869 |
return binary_buffer_error(&ctx->bb, \ |
| 3870 |
"DWARF expression stack underflow"); \ |
| 3871 |
} \ |
| 3872 |
} while (0) |
| 3873 |
|
| 3874 |
#define ELEM(i) *uint64_vector_at(stack, uint64_vector_size(stack) - 1 - (i)) |
| 3875 |
|
| 3876 |
#define PUSH(x) do { \ |
| 3877 |
uint64_t push = (x); \ |
| 3878 |
if (!uint64_vector_append(stack, &push)) \ |
| 3879 |
return &drgn_enomem; \ |
| 3880 |
} while (0) |
| 3881 |
|
| 3882 |
#define PUSH_MASK(x) PUSH((x) & address_mask) |
| 3883 |
|
| 3884 |
while (binary_buffer_has_next(&ctx->bb)) { |
| 3885 |
if (*remaining_ops <= 0) { |
| 3886 |
return binary_buffer_error(&ctx->bb, |
| 3887 |
"DWARF expression executed too many operations"); |
| 3888 |
} |
| 3889 |
(*remaining_ops)--; |
| 3890 |
uint8_t opcode; |
| 3891 |
if ((err = binary_buffer_next_u8(&ctx->bb, &opcode))) |
| 3892 |
return err; |
| 3893 |
uint64_t uvalue; |
| 3894 |
uint64_t dwarf_regno; |
| 3895 |
uint8_t deref_size; |
| 3896 |
switch (opcode) { |
| 3897 |
/* Literal encodings. */ |
| 3898 |
case DW_OP_lit0 ... DW_OP_lit31: |
| 3899 |
PUSH(opcode - DW_OP_lit0); |
| 3900 |
break; |
| 3901 |
case DW_OP_addr: |
| 3902 |
if ((err = binary_buffer_next_uint(&ctx->bb, |
| 3903 |
address_size, |
| 3904 |
&uvalue))) |
| 3905 |
return err; |
| 3906 |
addr: |
| 3907 |
/* |
| 3908 |
* If the address is not in the module's address range, |
| 3909 |
* then it's probably something special like a Linux |
| 3910 |
* per-CPU variable (which isn't actually a variable |
| 3911 |
* address but an offset). Don't apply the bias in that |
| 3912 |
* case. |
| 3913 |
*/ |
| 3914 |
if (drgn_module_contains_address(ctx->file->module, |
| 3915 |
uvalue + ctx->file->module->debug_file_bias)) |
| 3916 |
uvalue += ctx->file->module->debug_file_bias; |
| 3917 |
PUSH(uvalue); |
| 3918 |
break; |
| 3919 |
case DW_OP_const1u: |
| 3920 |
if ((err = binary_buffer_next_u8_into_u64(&ctx->bb, |
| 3921 |
&uvalue))) |
| 3922 |
return err; |
| 3923 |
PUSH(uvalue); |
| 3924 |
break; |
| 3925 |
case DW_OP_const2u: |
| 3926 |
if ((err = binary_buffer_next_u16_into_u64(&ctx->bb, |
| 3927 |
&uvalue))) |
| 3928 |
return err; |
| 3929 |
PUSH_MASK(uvalue); |
| 3930 |
break; |
| 3931 |
case DW_OP_const4u: |
| 3932 |
if ((err = binary_buffer_next_u32_into_u64(&ctx->bb, |
| 3933 |
&uvalue))) |
| 3934 |
return err; |
| 3935 |
PUSH_MASK(uvalue); |
| 3936 |
break; |
| 3937 |
case DW_OP_const8u: |
| 3938 |
if ((err = binary_buffer_next_u64(&ctx->bb, &uvalue))) |
| 3939 |
return err; |
| 3940 |
PUSH_MASK(uvalue); |
| 3941 |
break; |
| 3942 |
case DW_OP_const1s: |
| 3943 |
if ((err = binary_buffer_next_s8_into_u64(&ctx->bb, |
| 3944 |
&uvalue))) |
| 3945 |
return err; |
| 3946 |
PUSH_MASK(uvalue); |
| 3947 |
break; |
| 3948 |
case DW_OP_const2s: |
| 3949 |
if ((err = binary_buffer_next_s16_into_u64(&ctx->bb, |
| 3950 |
&uvalue))) |
| 3951 |
return err; |
| 3952 |
PUSH_MASK(uvalue); |
| 3953 |
break; |
| 3954 |
case DW_OP_const4s: |
| 3955 |
if ((err = binary_buffer_next_s32_into_u64(&ctx->bb, |
| 3956 |
&uvalue))) |
| 3957 |
return err; |
| 3958 |
PUSH_MASK(uvalue); |
| 3959 |
break; |
| 3960 |
case DW_OP_const8s: |
| 3961 |
if ((err = binary_buffer_next_s64_into_u64(&ctx->bb, |
| 3962 |
&uvalue))) |
| 3963 |
return err; |
| 3964 |
PUSH_MASK(uvalue); |
| 3965 |
break; |
| 3966 |
case DW_OP_constu: |
| 3967 |
if ((err = binary_buffer_next_uleb128(&ctx->bb, |
| 3968 |
&uvalue))) |
| 3969 |
return err; |
| 3970 |
PUSH_MASK(uvalue); |
| 3971 |
break; |
| 3972 |
case DW_OP_consts: |
| 3973 |
if ((err = binary_buffer_next_sleb128_into_u64(&ctx->bb, |
| 3974 |
&uvalue))) |
| 3975 |
return err; |
| 3976 |
PUSH_MASK(uvalue); |
| 3977 |
break; |
| 3978 |
case DW_OP_addrx: |
| 3979 |
case DW_OP_GNU_addr_index: |
| 3980 |
if (!ctx->cu_die.addr) { |
| 3981 |
ctx->bb.pos = ctx->bb.prev; |
| 3982 |
return NULL; |
| 3983 |
} |
| 3984 |
if ((err = drgn_dwarf_next_addrx(&ctx->bb, ctx->file, |
| 3985 |
&ctx->cu_die, |
| 3986 |
address_size, |
| 3987 |
&ctx->cu_addr_base, |
| 3988 |
&uvalue))) |
| 3989 |
return err; |
| 3990 |
goto addr; |
| 3991 |
case DW_OP_constx: |
| 3992 |
case DW_OP_GNU_const_index: |
| 3993 |
if (!ctx->cu_die.addr) { |
| 3994 |
ctx->bb.pos = ctx->bb.prev; |
| 3995 |
return NULL; |
| 3996 |
} |
| 3997 |
if ((err = drgn_dwarf_next_addrx(&ctx->bb, ctx->file, |
| 3998 |
&ctx->cu_die, |
| 3999 |
address_size, |
| 4000 |
&ctx->cu_addr_base, |
| 4001 |
&uvalue))) |
| 4002 |
return err; |
| 4003 |
PUSH(uvalue); |
| 4004 |
break; |
| 4005 |
/* Register values. */ |
| 4006 |
case DW_OP_fbreg: { |
| 4007 |
err = drgn_dwarf_frame_base(ctx->prog, ctx->file, |
| 4008 |
ctx->function, ctx->regs, |
| 4009 |
remaining_ops, &uvalue); |
| 4010 |
if (err) |
| 4011 |
return err; |
| 4012 |
int64_t svalue; |
| 4013 |
if ((err = binary_buffer_next_sleb128(&ctx->bb, |
| 4014 |
&svalue))) |
| 4015 |
return err; |
| 4016 |
PUSH_MASK(uvalue + svalue); |
| 4017 |
break; |
| 4018 |
} |
| 4019 |
case DW_OP_breg0 ... DW_OP_breg31: |
| 4020 |
dwarf_regno = opcode - DW_OP_breg0; |
| 4021 |
goto breg; |
| 4022 |
case DW_OP_bregx: |
| 4023 |
if ((err = binary_buffer_next_uleb128(&ctx->bb, |
| 4024 |
&dwarf_regno))) |
| 4025 |
return err; |
| 4026 |
breg: |
| 4027 |
{ |
| 4028 |
if (!ctx->regs) |
| 4029 |
return &drgn_not_found; |
| 4030 |
drgn_register_number regno = |
| 4031 |
dwarf_regno_to_internal(dwarf_regno); |
| 4032 |
if (!drgn_register_state_has_register(ctx->regs, regno)) |
| 4033 |
return &drgn_not_found; |
| 4034 |
const struct drgn_register_layout *layout = |
| 4035 |
®ister_layout[regno]; |
| 4036 |
copy_lsbytes(&uvalue, sizeof(uvalue), |
| 4037 |
HOST_LITTLE_ENDIAN, |
| 4038 |
&ctx->regs->buf[layout->offset], |
| 4039 |
layout->size, little_endian); |
| 4040 |
int64_t svalue; |
| 4041 |
if ((err = binary_buffer_next_sleb128(&ctx->bb, |
| 4042 |
&svalue))) |
| 4043 |
return err; |
| 4044 |
PUSH_MASK(uvalue + svalue); |
| 4045 |
break; |
| 4046 |
} |
| 4047 |
/* Stack operations. */ |
| 4048 |
case DW_OP_dup: |
| 4049 |
CHECK(1); |
| 4050 |
PUSH(ELEM(0)); |
| 4051 |
break; |
| 4052 |
case DW_OP_drop: |
| 4053 |
CHECK(1); |
| 4054 |
uint64_vector_pop(stack); |
| 4055 |
break; |
| 4056 |
case DW_OP_pick: { |
| 4057 |
uint8_t index; |
| 4058 |
if ((err = binary_buffer_next_u8(&ctx->bb, &index))) |
| 4059 |
return err; |
| 4060 |
CHECK(index + 1); |
| 4061 |
PUSH(ELEM(index)); |
| 4062 |
break; |
| 4063 |
} |
| 4064 |
case DW_OP_over: |
| 4065 |
CHECK(2); |
| 4066 |
PUSH(ELEM(1)); |
| 4067 |
break; |
| 4068 |
case DW_OP_swap: |
| 4069 |
CHECK(2); |
| 4070 |
uvalue = ELEM(0); |
| 4071 |
ELEM(0) = ELEM(1); |
| 4072 |
ELEM(1) = uvalue; |
| 4073 |
break; |
| 4074 |
case DW_OP_rot: |
| 4075 |
CHECK(3); |
| 4076 |
uvalue = ELEM(0); |
| 4077 |
ELEM(0) = ELEM(1); |
| 4078 |
ELEM(1) = ELEM(2); |
| 4079 |
ELEM(2) = uvalue; |
| 4080 |
break; |
| 4081 |
case DW_OP_deref: |
| 4082 |
deref_size = address_size; |
| 4083 |
goto deref; |
| 4084 |
case DW_OP_deref_size: |
| 4085 |
if ((err = binary_buffer_next_u8(&ctx->bb, |
| 4086 |
&deref_size))) |
| 4087 |
return err; |
| 4088 |
if (deref_size > address_size) { |
| 4089 |
return binary_buffer_error(&ctx->bb, |
| 4090 |
"DW_OP_deref_size has invalid size"); |
| 4091 |
} |
| 4092 |
deref: |
| 4093 |
{ |
| 4094 |
CHECK(1); |
| 4095 |
char deref_buf[8]; |
| 4096 |
err = drgn_program_read_memory(ctx->prog, deref_buf, |
| 4097 |
ELEM(0), deref_size, |
| 4098 |
false); |
| 4099 |
if (err) |
| 4100 |
return err; |
| 4101 |
copy_lsbytes(&ELEM(0), sizeof(ELEM(0)), |
| 4102 |
HOST_LITTLE_ENDIAN, deref_buf, deref_size, |
| 4103 |
little_endian); |
| 4104 |
break; |
| 4105 |
} |
| 4106 |
case DW_OP_call_frame_cfa: { |
| 4107 |
if (!ctx->regs) |
| 4108 |
return &drgn_not_found; |
| 4109 |
/* |
| 4110 |
* The DWARF 5 specification says that |
| 4111 |
* DW_OP_call_frame_cfa cannot be used for CFI. For |
| 4112 |
* DW_CFA_def_cfa_expression, it is clearly invalid to |
| 4113 |
* define the CFA in terms of the CFA, and it will fail |
| 4114 |
* naturally below. This restriction doesn't make sense |
| 4115 |
* for DW_CFA_expression and DW_CFA_val_expression, as |
| 4116 |
* they push the CFA and thus depend on it anyways, so |
| 4117 |
* we don't bother enforcing it. |
| 4118 |
*/ |
| 4119 |
struct optional_uint64 cfa = |
| 4120 |
drgn_register_state_get_cfa(ctx->regs); |
| 4121 |
if (!cfa.has_value) |
| 4122 |
return &drgn_not_found; |
| 4123 |
PUSH(cfa.value); |
| 4124 |
break; |
| 4125 |
} |
| 4126 |
/* Arithmetic and logical operations. */ |
| 4127 |
#define UNOP_MASK(op) do { \ |
| 4128 |
CHECK(1); \ |
| 4129 |
ELEM(0) = (op ELEM(0)) & address_mask; \ |
| 4130 |
} while (0) |
| 4131 |
#define BINOP(op) do { \ |
| 4132 |
CHECK(2); \ |
| 4133 |
ELEM(1) = ELEM(1) op ELEM(0); \ |
| 4134 |
uint64_vector_pop(stack); \ |
| 4135 |
} while (0) |
| 4136 |
#define BINOP_MASK(op) do { \ |
| 4137 |
CHECK(2); \ |
| 4138 |
ELEM(1) = (ELEM(1) op ELEM(0)) & address_mask; \ |
| 4139 |
uint64_vector_pop(stack); \ |
| 4140 |
} while (0) |
| 4141 |
case DW_OP_abs: |
| 4142 |
CHECK(1); |
| 4143 |
if (ELEM(0) & (UINT64_C(1) << (address_bits - 1))) |
| 4144 |
ELEM(0) = -ELEM(0) & address_mask; |
| 4145 |
break; |
| 4146 |
case DW_OP_and: |
| 4147 |
BINOP(&); |
| 4148 |
break; |
| 4149 |
case DW_OP_div: |
| 4150 |
CHECK(2); |
| 4151 |
if (ELEM(0) == 0) { |
| 4152 |
return binary_buffer_error(&ctx->bb, |
| 4153 |
"division by zero in DWARF expression"); |
| 4154 |
} |
| 4155 |
ELEM(1) = ((truncate_signed(ELEM(1), address_bits) |
| 4156 |
/ truncate_signed(ELEM(0), address_bits)) |
| 4157 |
& address_mask); |
| 4158 |
uint64_vector_pop(stack); |
| 4159 |
break; |
| 4160 |
case DW_OP_minus: |
| 4161 |
BINOP_MASK(-); |
| 4162 |
break; |
| 4163 |
case DW_OP_mod: |
| 4164 |
CHECK(2); |
| 4165 |
if (ELEM(0) == 0) { |
| 4166 |
return binary_buffer_error(&ctx->bb, |
| 4167 |
"modulo by zero in DWARF expression"); |
| 4168 |
} |
| 4169 |
ELEM(1) = ELEM(1) % ELEM(0); |
| 4170 |
uint64_vector_pop(stack); |
| 4171 |
break; |
| 4172 |
case DW_OP_mul: |
| 4173 |
BINOP_MASK(*); |
| 4174 |
break; |
| 4175 |
case DW_OP_neg: |
| 4176 |
UNOP_MASK(-); |
| 4177 |
break; |
| 4178 |
case DW_OP_not: |
| 4179 |
UNOP_MASK(~); |
| 4180 |
break; |
| 4181 |
case DW_OP_or: |
| 4182 |
BINOP(|); |
| 4183 |
break; |
| 4184 |
case DW_OP_plus: |
| 4185 |
BINOP_MASK(+); |
| 4186 |
break; |
| 4187 |
case DW_OP_plus_uconst: |
| 4188 |
CHECK(1); |
| 4189 |
if ((err = binary_buffer_next_uleb128(&ctx->bb, |
| 4190 |
&uvalue))) |
| 4191 |
return err; |
| 4192 |
ELEM(0) = (ELEM(0) + uvalue) & address_mask; |
| 4193 |
break; |
| 4194 |
case DW_OP_shl: |
| 4195 |
CHECK(2); |
| 4196 |
if (ELEM(0) < address_bits) |
| 4197 |
ELEM(1) = (ELEM(1) << ELEM(0)) & address_mask; |
| 4198 |
else |
| 4199 |
ELEM(1) = 0; |
| 4200 |
uint64_vector_pop(stack); |
| 4201 |
break; |
| 4202 |
case DW_OP_shr: |
| 4203 |
CHECK(2); |
| 4204 |
if (ELEM(0) < address_bits) |
| 4205 |
ELEM(1) >>= ELEM(0); |
| 4206 |
else |
| 4207 |
ELEM(1) = 0; |
| 4208 |
uint64_vector_pop(stack); |
| 4209 |
break; |
| 4210 |
case DW_OP_shra: |
| 4211 |
CHECK(2); |
| 4212 |
if (ELEM(0) < address_bits) { |
| 4213 |
ELEM(1) = ((truncate_signed(ELEM(1), address_bits) |
| 4214 |
>> ELEM(0)) |
| 4215 |
& address_mask); |
| 4216 |
} else if (ELEM(1) & (UINT64_C(1) << (address_bits - 1))) { |
| 4217 |
ELEM(1) = -INT64_C(1) & address_mask; |
| 4218 |
} else { |
| 4219 |
ELEM(1) = 0; |
| 4220 |
} |
| 4221 |
uint64_vector_pop(stack); |
| 4222 |
break; |
| 4223 |
case DW_OP_xor: |
| 4224 |
BINOP(^); |
| 4225 |
break; |
| 4226 |
#undef BINOP_MASK |
| 4227 |
#undef BINOP |
| 4228 |
#undef UNOP_MASK |
| 4229 |
/* Control flow operations. */ |
| 4230 |
#define RELOP(op) do { \ |
| 4231 |
CHECK(2); \ |
| 4232 |
ELEM(1) = (truncate_signed(ELEM(1), address_bits) op \ |
| 4233 |
truncate_signed(ELEM(0), address_bits)); \ |
| 4234 |
uint64_vector_pop(stack); \ |
| 4235 |
} while (0) |
| 4236 |
case DW_OP_le: |
| 4237 |
RELOP(<=); |
| 4238 |
break; |
| 4239 |
case DW_OP_ge: |
| 4240 |
RELOP(>=); |
| 4241 |
break; |
| 4242 |
case DW_OP_eq: |
| 4243 |
RELOP(==); |
| 4244 |
break; |
| 4245 |
case DW_OP_lt: |
| 4246 |
RELOP(<); |
| 4247 |
break; |
| 4248 |
case DW_OP_gt: |
| 4249 |
RELOP(>); |
| 4250 |
break; |
| 4251 |
case DW_OP_ne: |
| 4252 |
RELOP(!=); |
| 4253 |
break; |
| 4254 |
#undef RELOP |
| 4255 |
case DW_OP_skip: |
| 4256 |
branch: |
| 4257 |
{ |
| 4258 |
int16_t skip; |
| 4259 |
if ((err = binary_buffer_next_s16(&ctx->bb, &skip))) |
| 4260 |
return err; |
| 4261 |
if ((skip >= 0 && skip > ctx->bb.end - ctx->bb.pos) || |
| 4262 |
(skip < 0 && -skip > ctx->bb.pos - ctx->start)) { |
| 4263 |
return binary_buffer_error(&ctx->bb, |
| 4264 |
"DWARF expression branch is out of bounds"); |
| 4265 |
} |
| 4266 |
ctx->bb.pos += skip; |
| 4267 |
break; |
| 4268 |
} |
| 4269 |
case DW_OP_bra: |
| 4270 |
CHECK(1); |
| 4271 |
if (ELEM(0)) { |
| 4272 |
uint64_vector_pop(stack); |
| 4273 |
goto branch; |
| 4274 |
} else { |
| 4275 |
uint64_vector_pop(stack); |
| 4276 |
if ((err = binary_buffer_skip(&ctx->bb, 2))) |
| 4277 |
return err; |
| 4278 |
} |
| 4279 |
break; |
| 4280 |
/* Special operations. */ |
| 4281 |
case DW_OP_nop: |
| 4282 |
break; |
| 4283 |
case DW_OP_entry_value: |
| 4284 |
case DW_OP_GNU_entry_value: |
| 4285 |
// TODO: DW_OP_(GNU_)entry_value followed by |
| 4286 |
// DW_OP_reg<n> means the value of the register when the |
| 4287 |
// current subprogram was entered. We could recover this |
| 4288 |
// by finding the DW_TAG_(GNU_)call_site for the return |
| 4289 |
// address and using the DW_AT_(GNU_)call_value of a |
| 4290 |
// DW_TAG_(GNU_)call_parameter with a DW_AT_location |
| 4291 |
// matching that register. |
| 4292 |
if (drgn_log_is_enabled(ctx->prog, DRGN_LOG_DEBUG)) { |
| 4293 |
char op_buf[DW_OP_STR_BUF_LEN]; |
| 4294 |
err = binary_buffer_error(&ctx->bb, |
| 4295 |
"unimplemented DWARF expression opcode %s; " |
| 4296 |
"please upvote https://github.com/osandov/drgn/issues/337", |
| 4297 |
dw_op_str(opcode, op_buf)); |
| 4298 |
drgn_error_log_debug(ctx->prog, err, ""); |
| 4299 |
drgn_error_destroy(err); |
| 4300 |
} |
| 4301 |
return &drgn_not_found; |
| 4302 |
/* Location description operations. */ |
| 4303 |
case DW_OP_reg0 ... DW_OP_reg31: |
| 4304 |
case DW_OP_regx: |
| 4305 |
case DW_OP_implicit_value: |
| 4306 |
case DW_OP_stack_value: |
| 4307 |
case DW_OP_piece: |
| 4308 |
case DW_OP_bit_piece: |
| 4309 |
/* The caller must handle it. */ |
| 4310 |
ctx->bb.pos = ctx->bb.prev; |
| 4311 |
return NULL; |
| 4312 |
/* |
| 4313 |
* We don't yet support: |
| 4314 |
* |
| 4315 |
* - DW_OP_push_object_address |
| 4316 |
* - DW_OP_form_tls_address |
| 4317 |
* DW_OP_implicit_pointer |
| 4318 |
* - Procedure calls: DW_OP_call2, DW_OP_call4, DW_OP_call_ref. |
| 4319 |
* - Typed operations: DW_OP_const_type, DW_OP_regval_type, |
| 4320 |
* DW_OP_deref_type, DW_OP_convert, DW_OP_reinterpret. |
| 4321 |
* - Operations for multiple address spaces: DW_OP_xderef, |
| 4322 |
* DW_OP_xderef_size, DW_OP_xderef_type. |
| 4323 |
*/ |
| 4324 |
default: |
| 4325 |
return drgn_handle_unknown_dwarf_opcode(ctx, opcode, |
| 4326 |
false); |
| 4327 |
} |
| 4328 |
} |
| 4329 |
|
| 4330 |
#undef PUSH_MASK |
| 4331 |
#undef PUSH |
| 4332 |
#undef ELEM |
| 4333 |
#undef CHECK |
| 4334 |
|
| 4335 |
return NULL; |
| 4336 |
} |
| 4337 |
|
| 4338 |
static struct drgn_error * |
| 4339 |
drgn_dwarf_frame_base(struct drgn_program *prog, struct drgn_elf_file *file, |
| 4340 |
Dwarf_Die *die, const struct drgn_register_state *regs, |
| 4341 |
int *remaining_ops, uint64_t *ret) |
| 4342 |
{ |
| 4343 |
struct drgn_error *err; |
| 4344 |
bool little_endian = drgn_elf_file_is_little_endian(file); |
| 4345 |
const struct drgn_register_layout *register_layout = |
| 4346 |
file->platform.arch->register_layout; |
| 4347 |
drgn_register_number (*dwarf_regno_to_internal)(uint64_t) = |
| 4348 |
file->platform.arch->dwarf_regno_to_internal; |
| 4349 |
|
| 4350 |
if (!die) |
| 4351 |
return &drgn_not_found; |
| 4352 |
Dwarf_Attribute attr_mem, *attr; |
| 4353 |
if (!(attr = dwarf_attr_integrate(die, DW_AT_frame_base, &attr_mem))) |
| 4354 |
return &drgn_not_found; |
| 4355 |
const char *expr; |
| 4356 |
size_t expr_size; |
| 4357 |
err = drgn_dwarf_location(file, attr, regs, &expr, &expr_size); |
| 4358 |
if (err) |
| 4359 |
return err; |
| 4360 |
|
| 4361 |
struct drgn_dwarf_expression_context ctx; |
| 4362 |
if ((err = drgn_dwarf_expression_context_init(&ctx, prog, file, die->cu, |
| 4363 |
NULL, regs, expr, |
| 4364 |
expr_size))) |
| 4365 |
return err; |
| 4366 |
VECTOR(uint64_vector, stack); |
| 4367 |
for (;;) { |
| 4368 |
err = drgn_eval_dwarf_expression(&ctx, &stack, remaining_ops); |
| 4369 |
if (err) |
| 4370 |
return err; |
| 4371 |
if (binary_buffer_has_next(&ctx.bb)) { |
| 4372 |
uint8_t opcode; |
| 4373 |
if ((err = binary_buffer_next_u8(&ctx.bb, &opcode))) |
| 4374 |
return err; |
| 4375 |
|
| 4376 |
uint64_t dwarf_regno; |
| 4377 |
switch (opcode) { |
| 4378 |
case DW_OP_reg0 ... DW_OP_reg31: |
| 4379 |
dwarf_regno = opcode - DW_OP_reg0; |
| 4380 |
goto reg; |
| 4381 |
case DW_OP_regx: |
| 4382 |
if ((err = binary_buffer_next_uleb128(&ctx.bb, |
| 4383 |
&dwarf_regno))) |
| 4384 |
return err; |
| 4385 |
reg: |
| 4386 |
{ |
| 4387 |
if (!regs) |
| 4388 |
return &drgn_not_found; |
| 4389 |
drgn_register_number regno = |
| 4390 |
dwarf_regno_to_internal(dwarf_regno); |
| 4391 |
if (!drgn_register_state_has_register(regs, |
| 4392 |
regno)) |
| 4393 |
return &drgn_not_found; |
| 4394 |
const struct drgn_register_layout *layout = |
| 4395 |
®ister_layout[regno]; |
| 4396 |
/* |
| 4397 |
* Note that this doesn't mask the address since |
| 4398 |
* the caller does that. |
| 4399 |
*/ |
| 4400 |
copy_lsbytes(ret, sizeof(*ret), |
| 4401 |
HOST_LITTLE_ENDIAN, |
| 4402 |
®s->buf[layout->offset], |
| 4403 |
layout->size, little_endian); |
| 4404 |
if (binary_buffer_has_next(&ctx.bb)) { |
| 4405 |
return binary_buffer_error(&ctx.bb, |
| 4406 |
"stray operations in DW_AT_frame_base expression"); |
| 4407 |
} else { |
| 4408 |
return NULL; |
| 4409 |
} |
| 4410 |
} |
| 4411 |
default: |
| 4412 |
return binary_buffer_error(&ctx.bb, |
| 4413 |
"invalid opcode %#" PRIx8 " for DW_AT_frame_base expression", |
| 4414 |
opcode); |
| 4415 |
} |
| 4416 |
} else if (!uint64_vector_empty(&stack)) { |
| 4417 |
*ret = *uint64_vector_last(&stack); |
| 4418 |
return NULL; |
| 4419 |
} else { |
| 4420 |
return &drgn_not_found; |
| 4421 |
} |
| 4422 |
} |
| 4423 |
} |
| 4424 |
|
| 4425 |
/* |
| 4426 |
* Type and object parsing. |
| 4427 |
*/ |
| 4428 |
|
| 4429 |
/** |
| 4430 |
* Return whether a DWARF DIE is little-endian. |
| 4431 |
* |
| 4432 |
* @param[in] check_attr Whether to check the DW_AT_endianity attribute. If @c |
| 4433 |
* false, only the ELF header is checked and this function cannot fail. |
| 4434 |
* @return @c NULL on success, non-@c NULL on error. |
| 4435 |
*/ |
| 4436 |
static struct drgn_error *dwarf_die_is_little_endian(Dwarf_Die *die, |
| 4437 |
bool check_attr, bool *ret) |
| 4438 |
{ |
| 4439 |
Dwarf_Attribute endianity_attr_mem, *endianity_attr; |
| 4440 |
Dwarf_Word endianity; |
| 4441 |
if (check_attr && |
| 4442 |
(endianity_attr = dwarf_attr_integrate(die, DW_AT_endianity, |
| 4443 |
&endianity_attr_mem))) { |
| 4444 |
if (dwarf_formudata(endianity_attr, &endianity)) { |
| 4445 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 4446 |
"invalid DW_AT_endianity"); |
| 4447 |
} |
| 4448 |
} else { |
| 4449 |
endianity = DW_END_default; |
| 4450 |
} |
| 4451 |
switch (endianity) { |
| 4452 |
case DW_END_default: { |
| 4453 |
Elf *elf = dwarf_getelf(dwarf_cu_getdwarf(die->cu)); |
| 4454 |
*ret = elf_getident(elf, NULL)[EI_DATA] == ELFDATA2LSB; |
| 4455 |
return NULL; |
| 4456 |
} |
| 4457 |
case DW_END_little: |
| 4458 |
*ret = true; |
| 4459 |
return NULL; |
| 4460 |
case DW_END_big: |
| 4461 |
*ret = false; |
| 4462 |
return NULL; |
| 4463 |
default: |
| 4464 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 4465 |
"unknown DW_AT_endianity"); |
| 4466 |
} |
| 4467 |
} |
| 4468 |
|
| 4469 |
/** Like dwarf_die_is_little_endian(), but returns a @ref drgn_byte_order. */ |
| 4470 |
static struct drgn_error *dwarf_die_byte_order(Dwarf_Die *die, bool check_attr, |
| 4471 |
enum drgn_byte_order *ret) |
| 4472 |
{ |
| 4473 |
bool little_endian; |
| 4474 |
struct drgn_error *err = dwarf_die_is_little_endian(die, check_attr, |
| 4475 |
&little_endian); |
| 4476 |
/* |
| 4477 |
* dwarf_die_is_little_endian() can't fail if check_attr is false, so |
| 4478 |
* the !check_attr test suppresses maybe-uninitialized warnings. |
| 4479 |
*/ |
| 4480 |
if (!err || !check_attr) |
| 4481 |
*ret = drgn_byte_order_from_little_endian(little_endian); |
| 4482 |
return err; |
| 4483 |
} |
| 4484 |
|
| 4485 |
static int dwarf_type(Dwarf_Die *die, Dwarf_Die *ret) |
| 4486 |
{ |
| 4487 |
Dwarf_Attribute attr_mem; |
| 4488 |
Dwarf_Attribute *attr; |
| 4489 |
|
| 4490 |
if (!(attr = dwarf_attr_integrate(die, DW_AT_type, &attr_mem))) |
| 4491 |
return 1; |
| 4492 |
|
| 4493 |
return dwarf_formref_die(attr, ret) ? 0 : -1; |
| 4494 |
} |
| 4495 |
|
| 4496 |
static int dwarf_flag(Dwarf_Die *die, unsigned int name, bool *ret) |
| 4497 |
{ |
| 4498 |
Dwarf_Attribute attr_mem; |
| 4499 |
Dwarf_Attribute *attr; |
| 4500 |
|
| 4501 |
if (!(attr = dwarf_attr(die, name, &attr_mem))) { |
| 4502 |
*ret = false; |
| 4503 |
return 0; |
| 4504 |
} |
| 4505 |
return dwarf_formflag(attr, ret); |
| 4506 |
} |
| 4507 |
|
| 4508 |
static int dwarf_flag_integrate(Dwarf_Die *die, unsigned int name, bool *ret) |
| 4509 |
{ |
| 4510 |
Dwarf_Attribute attr_mem; |
| 4511 |
Dwarf_Attribute *attr; |
| 4512 |
|
| 4513 |
if (!(attr = dwarf_attr_integrate(die, name, &attr_mem))) { |
| 4514 |
*ret = false; |
| 4515 |
return 0; |
| 4516 |
} |
| 4517 |
return dwarf_formflag(attr, ret); |
| 4518 |
} |
| 4519 |
|
| 4520 |
struct drgn_error *drgn_dwarf_type_alignment(struct drgn_type *type, |
| 4521 |
uint64_t *ret) |
| 4522 |
{ |
| 4523 |
uintptr_t die_addr = drgn_type_die_addr(type); |
| 4524 |
if (!die_addr) |
| 4525 |
return &drgn_not_found; |
| 4526 |
struct drgn_dwarf_index_cu *cu = |
| 4527 |
drgn_dwarf_index_find_cu(&drgn_type_program(type)->dbinfo, |
| 4528 |
die_addr); |
| 4529 |
if (!cu) { |
| 4530 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 4531 |
"DIE from unknown DWARF CU"); |
| 4532 |
} |
| 4533 |
Dwarf_Die die = { |
| 4534 |
.addr = (void *)die_addr, |
| 4535 |
.cu = cu->libdw_cu, |
| 4536 |
}; |
| 4537 |
Dwarf_Attribute attr_mem, *attr; |
| 4538 |
if (!(attr = dwarf_attr_integrate(&die, DW_AT_alignment, &attr_mem))) |
| 4539 |
return &drgn_not_found; |
| 4540 |
Dwarf_Word alignment; |
| 4541 |
if (dwarf_formudata(attr, &alignment) || alignment <= 0) { |
| 4542 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 4543 |
"invalid DW_AT_alignment"); |
| 4544 |
} |
| 4545 |
*ret = alignment; |
| 4546 |
return NULL; |
| 4547 |
} |
| 4548 |
|
| 4549 |
/** |
| 4550 |
* Parse a type from a DWARF debugging information entry. |
| 4551 |
* |
| 4552 |
* This is the same as @ref drgn_type_from_dwarf() except that it can be used to |
| 4553 |
* work around a bug in GCC < 9.0 that zero length array types are encoded the |
| 4554 |
* same as incomplete array types. There are a few places where GCC allows |
| 4555 |
* zero-length arrays but not incomplete arrays: |
| 4556 |
* |
| 4557 |
* - As the type of a member of a structure with only one member. |
| 4558 |
* - As the type of a structure member other than the last member. |
| 4559 |
* - As the type of a union member. |
| 4560 |
* - As the element type of an array. |
| 4561 |
* |
| 4562 |
* In these cases, we know that what appears to be an incomplete array type must |
| 4563 |
* actually have a length of zero. In other cases, a subrange DIE without |
| 4564 |
* DW_AT_count or DW_AT_upper_bound is ambiguous; we return an incomplete array |
| 4565 |
* type. |
| 4566 |
* |
| 4567 |
* @param[in] dbinfo Debugging information. |
| 4568 |
* @param[in] file File containing @p die. |
| 4569 |
* @param[in] die DIE to parse. |
| 4570 |
* @param[in] can_be_incomplete_array Whether the type can be an incomplete |
| 4571 |
* array type. If this is @c false and the type appears to be an incomplete |
| 4572 |
* array type, its length is set to zero instead. |
| 4573 |
* @param[out] is_incomplete_array_ret Whether the encoded type is an incomplete |
| 4574 |
* array type or a typedef of an incomplete array type (regardless of @p |
| 4575 |
* can_be_incomplete_array). |
| 4576 |
* @param[out] ret Returned type. |
| 4577 |
* @return @c NULL on success, non-@c NULL on error. |
| 4578 |
*/ |
| 4579 |
static struct drgn_error * |
| 4580 |
drgn_type_from_dwarf_internal(struct drgn_debug_info *dbinfo, |
| 4581 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 4582 |
bool can_be_incomplete_array, |
| 4583 |
bool *is_incomplete_array_ret, |
| 4584 |
struct drgn_qualified_type *ret); |
| 4585 |
|
| 4586 |
/** |
| 4587 |
* Parse a type from a DWARF debugging information entry. |
| 4588 |
* |
| 4589 |
* @param[in] dbinfo Debugging information. |
| 4590 |
* @param[in] file File containing @p die. |
| 4591 |
* @param[in] die DIE to parse. |
| 4592 |
* @param[out] ret Returned type. |
| 4593 |
* @return @c NULL on success, non-@c NULL on error. |
| 4594 |
*/ |
| 4595 |
static inline struct drgn_error * |
| 4596 |
drgn_type_from_dwarf(struct drgn_debug_info *dbinfo, struct drgn_elf_file *file, |
| 4597 |
Dwarf_Die *die, struct drgn_qualified_type *ret) |
| 4598 |
{ |
| 4599 |
return drgn_type_from_dwarf_internal(dbinfo, file, die, true, NULL, |
| 4600 |
ret); |
| 4601 |
} |
| 4602 |
|
| 4603 |
/** |
| 4604 |
* Parse a type from the @c DW_AT_type attribute of a DWARF debugging |
| 4605 |
* information entry. |
| 4606 |
* |
| 4607 |
* @param[in] dbinfo Debugging information. |
| 4608 |
* @param[in] file File containing @p die. |
| 4609 |
* @param[in] die DIE with @c DW_AT_type attribute. |
| 4610 |
* @param[in] lang Language of @p die if it is already known, @c NULL if it |
| 4611 |
* should be determined from @p die. |
| 4612 |
* @param[in] can_be_void Whether the @c DW_AT_type attribute may be missing, |
| 4613 |
* which is interpreted as a void type. If this is false and the @c DW_AT_type |
| 4614 |
* attribute is missing, an error is returned. |
| 4615 |
* @param[in] can_be_incomplete_array See @ref drgn_type_from_dwarf_internal(). |
| 4616 |
* @param[in] is_incomplete_array_ret See @ref drgn_type_from_dwarf_internal(). |
| 4617 |
* @param[out] ret Returned type. |
| 4618 |
* @return @c NULL on success, non-@c NULL on error. |
| 4619 |
*/ |
| 4620 |
static struct drgn_error * |
| 4621 |
drgn_type_from_dwarf_attr(struct drgn_debug_info *dbinfo, |
| 4622 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 4623 |
const struct drgn_language *lang, |
| 4624 |
bool can_be_void, bool can_be_incomplete_array, |
| 4625 |
bool *is_incomplete_array_ret, |
| 4626 |
struct drgn_qualified_type *ret) |
| 4627 |
{ |
| 4628 |
struct drgn_error *err; |
| 4629 |
char tag_buf[DW_TAG_STR_BUF_LEN]; |
| 4630 |
|
| 4631 |
Dwarf_Attribute attr_mem; |
| 4632 |
Dwarf_Attribute *attr; |
| 4633 |
if (!(attr = dwarf_attr_integrate(die, DW_AT_type, &attr_mem))) { |
| 4634 |
if (can_be_void) { |
| 4635 |
if (!lang) { |
| 4636 |
err = drgn_language_from_die(die, true, &lang); |
| 4637 |
if (err) |
| 4638 |
return err; |
| 4639 |
} |
| 4640 |
ret->type = drgn_void_type(dbinfo->prog, lang); |
| 4641 |
ret->qualifiers = 0; |
| 4642 |
return NULL; |
| 4643 |
} else { |
| 4644 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 4645 |
"%s is missing DW_AT_type", |
| 4646 |
dwarf_tag_str(die, tag_buf)); |
| 4647 |
} |
| 4648 |
} |
| 4649 |
|
| 4650 |
Dwarf_Die type_die; |
| 4651 |
if (!dwarf_formref_die(attr, &type_die)) { |
| 4652 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 4653 |
"%s has invalid DW_AT_type", |
| 4654 |
dwarf_tag_str(die, tag_buf)); |
| 4655 |
} |
| 4656 |
|
| 4657 |
return drgn_type_from_dwarf_internal(dbinfo, file, &type_die, |
| 4658 |
can_be_incomplete_array, |
| 4659 |
is_incomplete_array_ret, ret); |
| 4660 |
} |
| 4661 |
|
| 4662 |
static struct drgn_error * |
| 4663 |
drgn_object_from_dwarf_enumerator(struct drgn_debug_info *dbinfo, |
| 4664 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 4665 |
const char *name, struct drgn_object *ret) |
| 4666 |
{ |
| 4667 |
struct drgn_error *err; |
| 4668 |
struct drgn_qualified_type qualified_type; |
| 4669 |
err = drgn_type_from_dwarf(dbinfo, file, die, &qualified_type); |
| 4670 |
if (err) |
| 4671 |
return err; |
| 4672 |
const struct drgn_type_enumerator *enumerators = |
| 4673 |
drgn_type_enumerators(qualified_type.type); |
| 4674 |
size_t num_enumerators = drgn_type_num_enumerators(qualified_type.type); |
| 4675 |
for (size_t i = 0; i < num_enumerators; i++) { |
| 4676 |
if (strcmp(enumerators[i].name, name) != 0) |
| 4677 |
continue; |
| 4678 |
|
| 4679 |
if (drgn_enum_type_is_signed(qualified_type.type)) { |
| 4680 |
return drgn_object_set_signed(ret, qualified_type, |
| 4681 |
enumerators[i].svalue, 0); |
| 4682 |
} else { |
| 4683 |
return drgn_object_set_unsigned(ret, qualified_type, |
| 4684 |
enumerators[i].uvalue, |
| 4685 |
0); |
| 4686 |
} |
| 4687 |
} |
| 4688 |
UNREACHABLE(); |
| 4689 |
} |
| 4690 |
|
| 4691 |
static struct drgn_error * |
| 4692 |
drgn_object_from_dwarf_subprogram(struct drgn_debug_info *dbinfo, |
| 4693 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 4694 |
struct drgn_object *ret) |
| 4695 |
{ |
| 4696 |
struct drgn_qualified_type qualified_type; |
| 4697 |
struct drgn_error *err = drgn_type_from_dwarf(dbinfo, file, die, |
| 4698 |
&qualified_type); |
| 4699 |
if (err) |
| 4700 |
return err; |
| 4701 |
Dwarf_Addr low_pc; |
| 4702 |
if (dwarf_lowpc(die, &low_pc) == -1) { |
| 4703 |
return drgn_object_set_absent(ret, qualified_type, |
| 4704 |
DRGN_ABSENCE_REASON_OPTIMIZED_OUT, |
| 4705 |
0); |
| 4706 |
} |
| 4707 |
return drgn_object_set_reference(ret, qualified_type, |
| 4708 |
low_pc + file->module->debug_file_bias, |
| 4709 |
0, 0); |
| 4710 |
} |
| 4711 |
|
| 4712 |
static struct drgn_error *read_bits(struct drgn_program *prog, void *dst, |
| 4713 |
unsigned int dst_bit_offset, uint64_t src, |
| 4714 |
unsigned int src_bit_offset, |
| 4715 |
uint64_t bit_size, bool lsb0) |
| 4716 |
{ |
| 4717 |
struct drgn_error *err; |
| 4718 |
|
| 4719 |
assert(dst_bit_offset < 8); |
| 4720 |
assert(src_bit_offset < 8); |
| 4721 |
|
| 4722 |
if (bit_size == 0) |
| 4723 |
return NULL; |
| 4724 |
|
| 4725 |
if (dst_bit_offset == src_bit_offset) { |
| 4726 |
/* |
| 4727 |
* We can read directly into the the destination buffer, but we |
| 4728 |
* may have to preserve some bits at the start and/or end. |
| 4729 |
*/ |
| 4730 |
uint8_t *d = dst; |
| 4731 |
uint64_t last_bit = dst_bit_offset + bit_size - 1; |
| 4732 |
uint8_t first_byte = d[0]; |
| 4733 |
uint8_t last_byte = d[last_bit / 8]; |
| 4734 |
err = drgn_program_read_memory(prog, d, src, last_bit / 8 + 1, |
| 4735 |
false); |
| 4736 |
if (err) |
| 4737 |
return err; |
| 4738 |
if (dst_bit_offset != 0) { |
| 4739 |
uint8_t mask = |
| 4740 |
copy_bits_first_mask(dst_bit_offset, lsb0); |
| 4741 |
d[0] = (first_byte & ~mask) | (d[0] & mask); |
| 4742 |
} |
| 4743 |
if (last_bit % 8 != 7) { |
| 4744 |
uint8_t mask = copy_bits_last_mask(last_bit, lsb0); |
| 4745 |
d[last_bit / 8] = ((last_byte & ~mask) |
| 4746 |
| (d[last_bit / 8] & mask)); |
| 4747 |
} |
| 4748 |
return NULL; |
| 4749 |
} else { |
| 4750 |
/* |
| 4751 |
* If the source and destination have different offsets, then |
| 4752 |
* depending on the size and source offset, we may have to read |
| 4753 |
* one more byte than is available in the destination. To keep |
| 4754 |
* things simple, we always read into a temporary buffer (rather |
| 4755 |
* than adding a special case for reading directly into the |
| 4756 |
* destination and shifting bits around). |
| 4757 |
*/ |
| 4758 |
uint64_t src_bytes = (src_bit_offset + bit_size - 1) / 8 + 1; |
| 4759 |
char stack_tmp[16], *tmp; |
| 4760 |
if (src_bytes <= sizeof(stack_tmp)) { |
| 4761 |
tmp = stack_tmp; |
| 4762 |
} else { |
| 4763 |
tmp = malloc64(src_bytes); |
| 4764 |
if (!tmp) |
| 4765 |
return &drgn_enomem; |
| 4766 |
} |
| 4767 |
err = drgn_program_read_memory(prog, tmp, src, src_bytes, |
| 4768 |
false); |
| 4769 |
if (!err) { |
| 4770 |
copy_bits(dst, dst_bit_offset, tmp, src_bit_offset, |
| 4771 |
bit_size, lsb0); |
| 4772 |
} |
| 4773 |
if (src_bytes > sizeof(stack_tmp)) |
| 4774 |
free(tmp); |
| 4775 |
return err; |
| 4776 |
} |
| 4777 |
} |
| 4778 |
|
| 4779 |
static struct drgn_error * |
| 4780 |
drgn_object_from_dwarf_location(struct drgn_program *prog, |
| 4781 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 4782 |
struct drgn_qualified_type qualified_type, |
| 4783 |
const char *expr, size_t expr_size, |
| 4784 |
Dwarf_Die *function_die, |
| 4785 |
const struct drgn_register_state *regs, |
| 4786 |
struct drgn_object *ret) |
| 4787 |
{ |
| 4788 |
struct drgn_error *err; |
| 4789 |
bool little_endian = drgn_elf_file_is_little_endian(file); |
| 4790 |
uint64_t address_mask = drgn_elf_file_address_mask(file); |
| 4791 |
const struct drgn_register_layout *register_layout = |
| 4792 |
file->platform.arch->register_layout; |
| 4793 |
drgn_register_number (*dwarf_regno_to_internal)(uint64_t) = |
| 4794 |
file->platform.arch->dwarf_regno_to_internal; |
| 4795 |
|
| 4796 |
struct drgn_object_type type; |
| 4797 |
err = drgn_object_type(qualified_type, 0, &type); |
| 4798 |
if (err) |
| 4799 |
return err; |
| 4800 |
|
| 4801 |
union drgn_value value; |
| 4802 |
char *value_buf = NULL; |
| 4803 |
|
| 4804 |
uint64_t address = 0; /* GCC thinks this may be used uninitialized. */ |
| 4805 |
int bit_offset = -1; /* -1 means that we don't have an address. */ |
| 4806 |
|
| 4807 |
enum drgn_absence_reason absence_reason = |
| 4808 |
DRGN_ABSENCE_REASON_OPTIMIZED_OUT; |
| 4809 |
|
| 4810 |
uint64_t bit_pos = 0; |
| 4811 |
|
| 4812 |
int remaining_ops = MAX_DWARF_EXPR_OPS; |
| 4813 |
struct drgn_dwarf_expression_context ctx; |
| 4814 |
if ((err = drgn_dwarf_expression_context_init(&ctx, prog, file, die->cu, |
| 4815 |
function_die, regs, expr, |
| 4816 |
expr_size))) |
| 4817 |
return err; |
| 4818 |
struct uint64_vector stack = VECTOR_INIT; |
| 4819 |
do { |
| 4820 |
uint64_vector_clear(&stack); |
| 4821 |
err = drgn_eval_dwarf_expression(&ctx, &stack, &remaining_ops); |
| 4822 |
if (err) { |
| 4823 |
if (err == &drgn_unknown_dwarf_opcode) |
| 4824 |
absence_reason = DRGN_ABSENCE_REASON_NOT_IMPLEMENTED; |
| 4825 |
else if (err != &drgn_not_found) |
| 4826 |
goto out; |
| 4827 |
goto absent; |
| 4828 |
} |
| 4829 |
if (err == &drgn_not_found) |
| 4830 |
goto absent; |
| 4831 |
else if (err) |
| 4832 |
goto out; |
| 4833 |
|
| 4834 |
const void *src = NULL; |
| 4835 |
// Silence -Wmaybe-uninitialized false positive last seen with |
| 4836 |
// GCC 13. |
| 4837 |
size_t src_size = 0; |
| 4838 |
|
| 4839 |
if (binary_buffer_has_next(&ctx.bb)) { |
| 4840 |
uint8_t opcode; |
| 4841 |
if ((err = binary_buffer_next_u8(&ctx.bb, &opcode))) |
| 4842 |
goto out; |
| 4843 |
|
| 4844 |
uint64_t uvalue; |
| 4845 |
uint64_t dwarf_regno; |
| 4846 |
drgn_register_number regno; |
| 4847 |
switch (opcode) { |
| 4848 |
case DW_OP_reg0 ... DW_OP_reg31: |
| 4849 |
dwarf_regno = opcode - DW_OP_reg0; |
| 4850 |
goto reg; |
| 4851 |
case DW_OP_regx: |
| 4852 |
if ((err = binary_buffer_next_uleb128(&ctx.bb, |
| 4853 |
&dwarf_regno))) |
| 4854 |
goto out; |
| 4855 |
reg: |
| 4856 |
if (!regs) |
| 4857 |
goto absent; |
| 4858 |
regno = dwarf_regno_to_internal(dwarf_regno); |
| 4859 |
if (!drgn_register_state_has_register(regs, |
| 4860 |
regno)) |
| 4861 |
goto absent; |
| 4862 |
const struct drgn_register_layout *layout = |
| 4863 |
®ister_layout[regno]; |
| 4864 |
src = ®s->buf[layout->offset]; |
| 4865 |
src_size = layout->size; |
| 4866 |
break; |
| 4867 |
case DW_OP_implicit_value: |
| 4868 |
if ((err = binary_buffer_next_uleb128(&ctx.bb, |
| 4869 |
&uvalue))) |
| 4870 |
goto out; |
| 4871 |
if (uvalue > ctx.bb.end - ctx.bb.pos) { |
| 4872 |
err = binary_buffer_error(&ctx.bb, |
| 4873 |
"DW_OP_implicit_value size is out of bounds"); |
| 4874 |
goto out; |
| 4875 |
} |
| 4876 |
src = ctx.bb.pos; |
| 4877 |
src_size = uvalue; |
| 4878 |
ctx.bb.pos += uvalue; |
| 4879 |
break; |
| 4880 |
case DW_OP_stack_value: |
| 4881 |
if (uint64_vector_empty(&stack)) |
| 4882 |
goto absent; |
| 4883 |
if (little_endian != HOST_LITTLE_ENDIAN) { |
| 4884 |
*uint64_vector_last(&stack) = |
| 4885 |
bswap_64(*uint64_vector_last(&stack)); |
| 4886 |
} |
| 4887 |
src = uint64_vector_last(&stack); |
| 4888 |
src_size = sizeof(uint64_t); |
| 4889 |
break; |
| 4890 |
default: |
| 4891 |
ctx.bb.pos = ctx.bb.prev; |
| 4892 |
break; |
| 4893 |
} |
| 4894 |
} |
| 4895 |
|
| 4896 |
uint64_t piece_bit_size; |
| 4897 |
uint64_t piece_bit_offset; |
| 4898 |
if (binary_buffer_has_next(&ctx.bb)) { |
| 4899 |
uint8_t opcode; |
| 4900 |
if ((err = binary_buffer_next_u8(&ctx.bb, &opcode))) |
| 4901 |
goto out; |
| 4902 |
|
| 4903 |
switch (opcode) { |
| 4904 |
case DW_OP_piece: |
| 4905 |
if ((err = binary_buffer_next_uleb128(&ctx.bb, |
| 4906 |
&piece_bit_size))) |
| 4907 |
goto out; |
| 4908 |
/* |
| 4909 |
* It's probably bogus for the piece size to be |
| 4910 |
* larger than the remaining value size, but |
| 4911 |
* that's not explicitly stated in the DWARF 5 |
| 4912 |
* specification, so clamp it instead. |
| 4913 |
*/ |
| 4914 |
if (__builtin_mul_overflow(piece_bit_size, 8U, |
| 4915 |
&piece_bit_size) || |
| 4916 |
piece_bit_size > type.bit_size - bit_pos) |
| 4917 |
piece_bit_size = type.bit_size - bit_pos; |
| 4918 |
piece_bit_offset = 0; |
| 4919 |
break; |
| 4920 |
case DW_OP_bit_piece: |
| 4921 |
if ((err = binary_buffer_next_uleb128(&ctx.bb, |
| 4922 |
&piece_bit_size)) || |
| 4923 |
(err = binary_buffer_next_uleb128(&ctx.bb, |
| 4924 |
&piece_bit_offset))) |
| 4925 |
goto out; |
| 4926 |
if (piece_bit_size > type.bit_size - bit_pos) |
| 4927 |
piece_bit_size = type.bit_size - bit_pos; |
| 4928 |
break; |
| 4929 |
default: |
| 4930 |
drgn_handle_unknown_dwarf_opcode(&ctx, opcode, |
| 4931 |
true); |
| 4932 |
absence_reason = DRGN_ABSENCE_REASON_NOT_IMPLEMENTED; |
| 4933 |
goto absent; |
| 4934 |
} |
| 4935 |
} else { |
| 4936 |
piece_bit_size = type.bit_size - bit_pos; |
| 4937 |
piece_bit_offset = 0; |
| 4938 |
} |
| 4939 |
|
| 4940 |
/* |
| 4941 |
* TODO: there are a few cases that a DWARF location can |
| 4942 |
* describe that can't be represented in drgn's object model: |
| 4943 |
* |
| 4944 |
* 1. An object that is partially known and partially unknown. |
| 4945 |
* 2. An object that is partially in memory and partially a |
| 4946 |
* value. |
| 4947 |
* 3. An object that is in memory at non-contiguous addresses. |
| 4948 |
* 4. A pointer object whose pointer value is not known but |
| 4949 |
* whose referenced value is known (DW_OP_implicit_pointer). |
| 4950 |
* |
| 4951 |
* For case 1, we consider the whole object as absent. For cases |
| 4952 |
* 2 and 3, we convert the whole object to a value. Case 4 is |
| 4953 |
* not supported at all. We should add a way to represent all of |
| 4954 |
* these situations precisely. |
| 4955 |
*/ |
| 4956 |
if (src && piece_bit_size == 0) { |
| 4957 |
/* Ignore empty value. */ |
| 4958 |
} else if (src) { |
| 4959 |
if (!value_buf && |
| 4960 |
!drgn_value_zalloc(drgn_value_size(type.bit_size), |
| 4961 |
&value, &value_buf)) { |
| 4962 |
err = &drgn_enomem; |
| 4963 |
goto out; |
| 4964 |
} |
| 4965 |
if (bit_offset >= 0) { |
| 4966 |
/* |
| 4967 |
* We previously had an address. Read it into |
| 4968 |
* the value. |
| 4969 |
*/ |
| 4970 |
err = read_bits(prog, value_buf, 0, address, |
| 4971 |
bit_offset, bit_pos, |
| 4972 |
little_endian); |
| 4973 |
if (err) |
| 4974 |
goto out; |
| 4975 |
bit_offset = -1; |
| 4976 |
} |
| 4977 |
/* |
| 4978 |
* It's probably safe to assume that we don't have an |
| 4979 |
* implicit value larger than 2 exabytes. |
| 4980 |
*/ |
| 4981 |
assert(src_size <= UINT64_MAX / 8); |
| 4982 |
uint64_t src_bit_size = UINT64_C(8) * src_size; |
| 4983 |
if (piece_bit_offset > src_bit_size) |
| 4984 |
piece_bit_offset = src_bit_size; |
| 4985 |
uint64_t copy_bit_size = |
| 4986 |
min(piece_bit_size, |
| 4987 |
src_bit_size - piece_bit_offset); |
| 4988 |
uint64_t copy_bit_offset = bit_pos; |
| 4989 |
if (!little_endian) { |
| 4990 |
copy_bit_offset += piece_bit_size - copy_bit_size; |
| 4991 |
piece_bit_offset = (src_bit_size |
| 4992 |
- copy_bit_size |
| 4993 |
- piece_bit_offset); |
| 4994 |
} |
| 4995 |
copy_bits(&value_buf[copy_bit_offset / 8], |
| 4996 |
copy_bit_offset % 8, |
| 4997 |
(const char *)src + (piece_bit_offset / 8), |
| 4998 |
piece_bit_offset % 8, copy_bit_size, |
| 4999 |
little_endian); |
| 5000 |
} else if (!uint64_vector_empty(&stack)) { |
| 5001 |
uint64_t piece_address = |
| 5002 |
((*uint64_vector_last(&stack) |
| 5003 |
+ piece_bit_offset / 8) |
| 5004 |
& address_mask); |
| 5005 |
piece_bit_offset %= 8; |
| 5006 |
if (bit_pos > 0 && bit_offset >= 0) { |
| 5007 |
/* |
| 5008 |
* We already had an address. Merge the pieces |
| 5009 |
* if the addresses are contiguous, otherwise |
| 5010 |
* convert to a value. |
| 5011 |
* |
| 5012 |
* The obvious way to write this is |
| 5013 |
* (address + (bit_pos + bit_offset) / 8), but |
| 5014 |
* (bit_pos + bit_offset) can overflow uint64_t. |
| 5015 |
*/ |
| 5016 |
uint64_t end_address = |
| 5017 |
((address |
| 5018 |
+ bit_pos / 8 |
| 5019 |
+ (bit_pos % 8 + bit_offset) / 8) |
| 5020 |
& address_mask); |
| 5021 |
unsigned int end_bit_offset = |
| 5022 |
(bit_offset + bit_pos) % 8; |
| 5023 |
if (piece_bit_size == 0 || |
| 5024 |
(piece_address == end_address && |
| 5025 |
piece_bit_offset == end_bit_offset)) { |
| 5026 |
/* Piece is contiguous. */ |
| 5027 |
piece_address = address; |
| 5028 |
piece_bit_offset = bit_offset; |
| 5029 |
} else { |
| 5030 |
if (!drgn_value_zalloc(drgn_value_size(type.bit_size), |
| 5031 |
&value, |
| 5032 |
&value_buf)) { |
| 5033 |
err = &drgn_enomem; |
| 5034 |
goto out; |
| 5035 |
} |
| 5036 |
err = read_bits(prog, value_buf, 0, |
| 5037 |
address, bit_offset, |
| 5038 |
bit_pos, little_endian); |
| 5039 |
if (err) |
| 5040 |
goto out; |
| 5041 |
bit_offset = -1; |
| 5042 |
} |
| 5043 |
} |
| 5044 |
if (value_buf) { |
| 5045 |
/* We already have a value. Read into it. */ |
| 5046 |
err = read_bits(prog, &value_buf[bit_pos / 8], |
| 5047 |
bit_pos % 8, piece_address, |
| 5048 |
piece_bit_offset, |
| 5049 |
piece_bit_size, little_endian); |
| 5050 |
if (err) |
| 5051 |
goto out; |
| 5052 |
} else { |
| 5053 |
address = piece_address; |
| 5054 |
bit_offset = piece_bit_offset; |
| 5055 |
} |
| 5056 |
} else if (piece_bit_size > 0) { |
| 5057 |
goto absent; |
| 5058 |
} |
| 5059 |
bit_pos += piece_bit_size; |
| 5060 |
} while (binary_buffer_has_next(&ctx.bb)); |
| 5061 |
|
| 5062 |
if (bit_pos < type.bit_size || (bit_offset < 0 && !value_buf)) { |
| 5063 |
absent: |
| 5064 |
if (dwarf_tag(die) == DW_TAG_template_value_parameter) { |
| 5065 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5066 |
"DW_AT_template_value_parameter is missing value"); |
| 5067 |
} |
| 5068 |
drgn_object_set_absent_internal(ret, &type, absence_reason); |
| 5069 |
err = NULL; |
| 5070 |
} else if (bit_offset >= 0) { |
| 5071 |
err = drgn_object_set_reference_internal(ret, &type, address, |
| 5072 |
bit_offset); |
| 5073 |
} else if (type.encoding == DRGN_OBJECT_ENCODING_BUFFER) { |
| 5074 |
drgn_object_reinit(ret, &type, DRGN_OBJECT_VALUE); |
| 5075 |
ret->value = value; |
| 5076 |
value_buf = NULL; |
| 5077 |
err = NULL; |
| 5078 |
} else { |
| 5079 |
err = drgn_object_set_from_buffer_internal(ret, &type, |
| 5080 |
value_buf, 0); |
| 5081 |
} |
| 5082 |
|
| 5083 |
out: |
| 5084 |
if (value_buf != value.ibuf) |
| 5085 |
free(value_buf); |
| 5086 |
uint64_vector_deinit(&stack); |
| 5087 |
return err; |
| 5088 |
} |
| 5089 |
|
| 5090 |
static struct drgn_error * |
| 5091 |
drgn_object_from_dwarf_constant(struct drgn_debug_info *dbinfo, Dwarf_Die *die, |
| 5092 |
struct drgn_qualified_type qualified_type, |
| 5093 |
Dwarf_Attribute *attr, struct drgn_object *ret) |
| 5094 |
{ |
| 5095 |
struct drgn_object_type type; |
| 5096 |
struct drgn_error *err = drgn_object_type(qualified_type, 0, &type); |
| 5097 |
if (err) |
| 5098 |
return err; |
| 5099 |
Dwarf_Block block; |
| 5100 |
if (dwarf_formblock(attr, &block) == 0) { |
| 5101 |
if (block.length < drgn_value_size(type.bit_size)) { |
| 5102 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5103 |
"DW_AT_const_value block is too small"); |
| 5104 |
} |
| 5105 |
return drgn_object_set_from_buffer_internal(ret, &type, |
| 5106 |
block.data, 0); |
| 5107 |
} else if (type.encoding == DRGN_OBJECT_ENCODING_SIGNED) { |
| 5108 |
Dwarf_Sword svalue; |
| 5109 |
if (dwarf_formsdata(attr, &svalue)) { |
| 5110 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5111 |
"invalid DW_AT_const_value"); |
| 5112 |
} |
| 5113 |
return drgn_object_set_signed_internal(ret, &type, svalue); |
| 5114 |
} else if (type.encoding == DRGN_OBJECT_ENCODING_UNSIGNED) { |
| 5115 |
Dwarf_Word uvalue; |
| 5116 |
if (dwarf_formudata(attr, &uvalue)) { |
| 5117 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5118 |
"invalid DW_AT_const_value"); |
| 5119 |
} |
| 5120 |
return drgn_object_set_unsigned_internal(ret, &type, uvalue); |
| 5121 |
} else { |
| 5122 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5123 |
"unknown DW_AT_const_value form"); |
| 5124 |
} |
| 5125 |
} |
| 5126 |
|
| 5127 |
struct drgn_error * |
| 5128 |
drgn_object_from_dwarf(struct drgn_debug_info *dbinfo, |
| 5129 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 5130 |
Dwarf_Die *type_die, Dwarf_Die *function_die, |
| 5131 |
const struct drgn_register_state *regs, |
| 5132 |
struct drgn_object *ret) |
| 5133 |
{ |
| 5134 |
struct drgn_error *err; |
| 5135 |
if (dwarf_tag(die) == DW_TAG_subprogram) { |
| 5136 |
return drgn_object_from_dwarf_subprogram(dbinfo, file, die, |
| 5137 |
ret); |
| 5138 |
} |
| 5139 |
/* |
| 5140 |
* The DWARF 5 specifications mentions that data object entries can have |
| 5141 |
* DW_AT_endianity, but that doesn't seem to be used in practice. It |
| 5142 |
* would be inconvenient to support, so ignore it for now. |
| 5143 |
*/ |
| 5144 |
struct drgn_qualified_type qualified_type; |
| 5145 |
if (type_die) { |
| 5146 |
err = drgn_type_from_dwarf(dbinfo, file, type_die, |
| 5147 |
&qualified_type); |
| 5148 |
} else { |
| 5149 |
err = drgn_type_from_dwarf_attr(dbinfo, file, die, NULL, true, |
| 5150 |
true, NULL, &qualified_type); |
| 5151 |
} |
| 5152 |
if (err) |
| 5153 |
return err; |
| 5154 |
Dwarf_Attribute attr_mem, *attr; |
| 5155 |
const char *expr; |
| 5156 |
size_t expr_size; |
| 5157 |
if ((attr = dwarf_attr_integrate(die, DW_AT_location, &attr_mem))) { |
| 5158 |
err = drgn_dwarf_location(file, attr, regs, &expr, &expr_size); |
| 5159 |
if (err) |
| 5160 |
return err; |
| 5161 |
} else if ((attr = dwarf_attr_integrate(die, DW_AT_const_value, |
| 5162 |
&attr_mem))) { |
| 5163 |
return drgn_object_from_dwarf_constant(dbinfo, die, |
| 5164 |
qualified_type, attr, |
| 5165 |
ret); |
| 5166 |
} else { |
| 5167 |
expr = NULL; |
| 5168 |
expr_size = 0; |
| 5169 |
} |
| 5170 |
return drgn_object_from_dwarf_location(dbinfo->prog, file, die, |
| 5171 |
qualified_type, expr, expr_size, |
| 5172 |
function_die, regs, ret); |
| 5173 |
} |
| 5174 |
|
| 5175 |
DEFINE_VECTOR(const_char_p_vector, const char *); |
| 5176 |
|
| 5177 |
static struct drgn_error *add_dwarf_enumerators(Dwarf_Die *enumeration_type, |
| 5178 |
struct const_char_p_vector *vec) |
| 5179 |
{ |
| 5180 |
Dwarf_Die child; |
| 5181 |
int r = dwarf_child(enumeration_type, &child); |
| 5182 |
while (r == 0) { |
| 5183 |
if (dwarf_tag(&child) == DW_TAG_enumerator) { |
| 5184 |
const char *die_name = dwarf_diename(&child); |
| 5185 |
if (!die_name) |
| 5186 |
continue; |
| 5187 |
if (!const_char_p_vector_append(vec, &die_name)) |
| 5188 |
return &drgn_enomem; |
| 5189 |
} |
| 5190 |
r = dwarf_siblingof(&child, &child); |
| 5191 |
} |
| 5192 |
if (r < 0) |
| 5193 |
return drgn_error_libdw(); |
| 5194 |
return NULL; |
| 5195 |
} |
| 5196 |
|
| 5197 |
struct drgn_error *drgn_dwarf_scopes_names(Dwarf_Die *scopes, |
| 5198 |
size_t num_scopes, |
| 5199 |
const char ***names_ret, |
| 5200 |
size_t *count_ret) |
| 5201 |
{ |
| 5202 |
struct drgn_error *err; |
| 5203 |
Dwarf_Die die; |
| 5204 |
VECTOR(const_char_p_vector, vec); |
| 5205 |
for (size_t scope = 0; scope < num_scopes; scope++) { |
| 5206 |
if (dwarf_child(&scopes[scope], &die) != 0) |
| 5207 |
continue; |
| 5208 |
do { |
| 5209 |
switch (dwarf_tag(&die)) { |
| 5210 |
case DW_TAG_variable: |
| 5211 |
case DW_TAG_formal_parameter: |
| 5212 |
case DW_TAG_subprogram: { |
| 5213 |
const char *die_name = dwarf_diename(&die); |
| 5214 |
if (!die_name) |
| 5215 |
continue; |
| 5216 |
if (!const_char_p_vector_append(&vec, |
| 5217 |
&die_name)) |
| 5218 |
return &drgn_enomem; |
| 5219 |
break; |
| 5220 |
} |
| 5221 |
case DW_TAG_enumeration_type: { |
| 5222 |
bool enum_class; |
| 5223 |
if (dwarf_flag_integrate(&die, DW_AT_enum_class, |
| 5224 |
&enum_class)) |
| 5225 |
return drgn_error_libdw(); |
| 5226 |
if (!enum_class) { |
| 5227 |
err = add_dwarf_enumerators(&die, &vec); |
| 5228 |
if (err) |
| 5229 |
return err; |
| 5230 |
} |
| 5231 |
break; |
| 5232 |
} |
| 5233 |
default: |
| 5234 |
continue; |
| 5235 |
} |
| 5236 |
} while (dwarf_siblingof(&die, &die) == 0); |
| 5237 |
} |
| 5238 |
const_char_p_vector_shrink_to_fit(&vec); |
| 5239 |
const_char_p_vector_steal(&vec, names_ret, count_ret); |
| 5240 |
return NULL; |
| 5241 |
} |
| 5242 |
|
| 5243 |
static struct drgn_error *find_dwarf_enumerator(Dwarf_Die *enumeration_type, |
| 5244 |
const char *name, |
| 5245 |
Dwarf_Die *ret) |
| 5246 |
{ |
| 5247 |
int r = dwarf_child(enumeration_type, ret); |
| 5248 |
while (r == 0) { |
| 5249 |
if (dwarf_tag(ret) == DW_TAG_enumerator) { |
| 5250 |
const char *die_name = dwarf_diename(ret); |
| 5251 |
if (die_name && strcmp(die_name, name) == 0) |
| 5252 |
return NULL; |
| 5253 |
} |
| 5254 |
r = dwarf_siblingof(ret, ret); |
| 5255 |
} |
| 5256 |
if (r < 0) |
| 5257 |
return drgn_error_libdw(); |
| 5258 |
ret->addr = NULL; |
| 5259 |
return NULL; |
| 5260 |
} |
| 5261 |
|
| 5262 |
struct drgn_error *drgn_find_in_dwarf_scopes(Dwarf_Die *scopes, |
| 5263 |
size_t num_scopes, |
| 5264 |
const char *name, |
| 5265 |
Dwarf_Die *die_ret, |
| 5266 |
Dwarf_Die *type_ret) |
| 5267 |
{ |
| 5268 |
struct drgn_error *err; |
| 5269 |
Dwarf_Die die; |
| 5270 |
for (size_t scope = num_scopes; scope--;) { |
| 5271 |
bool have_declaration = false; |
| 5272 |
if (dwarf_child(&scopes[scope], &die) != 0) |
| 5273 |
continue; |
| 5274 |
do { |
| 5275 |
switch (dwarf_tag(&die)) { |
| 5276 |
case DW_TAG_variable: |
| 5277 |
case DW_TAG_formal_parameter: |
| 5278 |
case DW_TAG_subprogram: { |
| 5279 |
const char *die_name = dwarf_diename(&die); |
| 5280 |
if (die_name && strcmp(die_name, name) == 0) { |
| 5281 |
*die_ret = die; |
| 5282 |
bool declaration; |
| 5283 |
if (dwarf_flag(&die, DW_AT_declaration, |
| 5284 |
&declaration)) |
| 5285 |
return drgn_error_libdw(); |
| 5286 |
if (declaration) |
| 5287 |
have_declaration = true; |
| 5288 |
else |
| 5289 |
return NULL; |
| 5290 |
} |
| 5291 |
break; |
| 5292 |
} |
| 5293 |
case DW_TAG_enumeration_type: { |
| 5294 |
bool enum_class; |
| 5295 |
if (dwarf_flag_integrate(&die, DW_AT_enum_class, |
| 5296 |
&enum_class)) |
| 5297 |
return drgn_error_libdw(); |
| 5298 |
if (!enum_class) { |
| 5299 |
Dwarf_Die enumerator; |
| 5300 |
err = find_dwarf_enumerator(&die, name, |
| 5301 |
&enumerator); |
| 5302 |
if (err) |
| 5303 |
return err; |
| 5304 |
if (enumerator.addr) { |
| 5305 |
*die_ret = enumerator; |
| 5306 |
*type_ret = die; |
| 5307 |
return NULL; |
| 5308 |
} |
| 5309 |
} |
| 5310 |
break; |
| 5311 |
} |
| 5312 |
default: |
| 5313 |
continue; |
| 5314 |
} |
| 5315 |
} while (dwarf_siblingof(&die, &die) == 0); |
| 5316 |
if (have_declaration) |
| 5317 |
return NULL; |
| 5318 |
} |
| 5319 |
die_ret->addr = NULL; |
| 5320 |
return NULL; |
| 5321 |
} |
| 5322 |
|
| 5323 |
static struct drgn_error * |
| 5324 |
drgn_base_type_from_dwarf(struct drgn_debug_info *dbinfo, |
| 5325 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 5326 |
const struct drgn_language *lang, |
| 5327 |
struct drgn_type **ret) |
| 5328 |
{ |
| 5329 |
struct drgn_error *err; |
| 5330 |
|
| 5331 |
const char *name = dwarf_diename(die); |
| 5332 |
if (!name) { |
| 5333 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5334 |
"DW_TAG_base_type has missing or invalid DW_AT_name"); |
| 5335 |
} |
| 5336 |
|
| 5337 |
Dwarf_Attribute attr; |
| 5338 |
Dwarf_Word encoding; |
| 5339 |
if (!dwarf_attr_integrate(die, DW_AT_encoding, &attr) || |
| 5340 |
dwarf_formudata(&attr, &encoding)) { |
| 5341 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5342 |
"DW_TAG_base_type has missing or invalid DW_AT_encoding"); |
| 5343 |
} |
| 5344 |
int size = dwarf_bytesize(die); |
| 5345 |
if (size == -1) { |
| 5346 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5347 |
"DW_TAG_base_type has missing or invalid DW_AT_byte_size"); |
| 5348 |
} |
| 5349 |
|
| 5350 |
enum drgn_byte_order byte_order; |
| 5351 |
err = dwarf_die_byte_order(die, true, &byte_order); |
| 5352 |
if (err) |
| 5353 |
return err; |
| 5354 |
|
| 5355 |
switch (encoding) { |
| 5356 |
case DW_ATE_boolean: |
| 5357 |
return drgn_bool_type_create(dbinfo->prog, name, size, |
| 5358 |
byte_order, lang, ret); |
| 5359 |
case DW_ATE_float: |
| 5360 |
return drgn_float_type_create(dbinfo->prog, name, size, |
| 5361 |
byte_order, lang, ret); |
| 5362 |
case DW_ATE_signed: |
| 5363 |
case DW_ATE_signed_char: |
| 5364 |
return drgn_int_type_create(dbinfo->prog, name, size, true, |
| 5365 |
byte_order, lang, ret); |
| 5366 |
case DW_ATE_unsigned: |
| 5367 |
case DW_ATE_unsigned_char: |
| 5368 |
case DW_ATE_UTF: |
| 5369 |
return drgn_int_type_create(dbinfo->prog, name, size, false, |
| 5370 |
byte_order, lang, ret); |
| 5371 |
/* We don't support complex types yet. */ |
| 5372 |
case DW_ATE_complex_float: |
| 5373 |
default: |
| 5374 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 5375 |
"DW_TAG_base_type has unknown DWARF encoding 0x%llx", |
| 5376 |
(unsigned long long)encoding); |
| 5377 |
} |
| 5378 |
} |
| 5379 |
|
| 5380 |
static struct drgn_error * |
| 5381 |
find_namespace_containing_die(struct drgn_debug_info *dbinfo, |
| 5382 |
Dwarf_Die *die, const struct drgn_language *lang, |
| 5383 |
struct drgn_namespace_dwarf_index **ret) |
| 5384 |
{ |
| 5385 |
struct drgn_error *err; |
| 5386 |
|
| 5387 |
struct drgn_namespace_dwarf_index *ns = &dbinfo->dwarf.global; |
| 5388 |
if (!lang->has_namespaces) { |
| 5389 |
*ret = ns; |
| 5390 |
return NULL; |
| 5391 |
} |
| 5392 |
|
| 5393 |
Dwarf_Die *ancestors; |
| 5394 |
size_t num_ancestors; |
| 5395 |
err = drgn_find_die_ancestors(die, &ancestors, &num_ancestors); |
| 5396 |
if (err) |
| 5397 |
return err; |
| 5398 |
|
| 5399 |
for (size_t i = 0; i < num_ancestors; i++) { |
| 5400 |
switch (dwarf_tag(&ancestors[i])) { |
| 5401 |
#define X(name) case DW_TAG_##name: break; |
| 5402 |
DRGN_DWARF_INDEX_NAMESPACE_TAGS |
| 5403 |
#undef X |
| 5404 |
default: |
| 5405 |
continue; |
| 5406 |
} |
| 5407 |
|
| 5408 |
Dwarf_Attribute attr_mem, *attr; |
| 5409 |
if (!(attr = dwarf_attr_integrate(&ancestors[i], DW_AT_name, |
| 5410 |
&attr_mem))) |
| 5411 |
continue; |
| 5412 |
const char *name = dwarf_formstring(attr); |
| 5413 |
if (!name) { |
| 5414 |
err = drgn_error_libdw(); |
| 5415 |
goto out; |
| 5416 |
} |
| 5417 |
|
| 5418 |
err = drgn_namespace_find_child(ns, name, strlen(name), &ns); |
| 5419 |
if (err) |
| 5420 |
goto out; |
| 5421 |
} |
| 5422 |
*ret = ns; |
| 5423 |
out: |
| 5424 |
free(ancestors); |
| 5425 |
return err; |
| 5426 |
} |
| 5427 |
|
| 5428 |
/* |
| 5429 |
* DW_TAG_structure_type, DW_TAG_union_type, DW_TAG_class_type, and |
| 5430 |
* DW_TAG_enumeration_type can be incomplete (i.e., have a DW_AT_declaration of |
| 5431 |
* true). This tries to find the complete type. If it succeeds, it returns NULL. |
| 5432 |
* If it can't find a complete type, it returns &drgn_not_found. Otherwise, it |
| 5433 |
* returns an error. |
| 5434 |
*/ |
| 5435 |
static struct drgn_error * |
| 5436 |
drgn_debug_info_find_complete(struct drgn_debug_info *dbinfo, int tag, |
| 5437 |
const char *name, Dwarf_Die *incomplete_die, |
| 5438 |
const struct drgn_language *lang, |
| 5439 |
struct drgn_type **ret) |
| 5440 |
{ |
| 5441 |
struct drgn_error *err; |
| 5442 |
|
| 5443 |
struct drgn_namespace_dwarf_index *ns; |
| 5444 |
err = find_namespace_containing_die(dbinfo, incomplete_die, lang, &ns); |
| 5445 |
if (err) |
| 5446 |
return err; |
| 5447 |
|
| 5448 |
enum drgn_dwarf_index_tag dwarf_index_tag; |
| 5449 |
switch (tag) { |
| 5450 |
#define X(name) case DW_TAG_##name: dwarf_index_tag = DRGN_DWARF_INDEX_##name; break; |
| 5451 |
DRGN_DWARF_INDEX_TAGS |
| 5452 |
#undef X |
| 5453 |
default: |
| 5454 |
return NULL; |
| 5455 |
} |
| 5456 |
struct drgn_dwarf_index_iterator it; |
| 5457 |
err = drgn_dwarf_index_iterator_init(&it, ns, name, strlen(name), |
| 5458 |
&dwarf_index_tag, 1); |
| 5459 |
if (err) |
| 5460 |
return err; |
| 5461 |
|
| 5462 |
/* |
| 5463 |
* Find a matching DIE. Note that drgn_namespace_dwarf_index does not |
| 5464 |
* contain DIEs with DW_AT_declaration, so this will always be a |
| 5465 |
* complete type. |
| 5466 |
*/ |
| 5467 |
Dwarf_Die die; |
| 5468 |
struct drgn_elf_file *file; |
| 5469 |
if (!drgn_dwarf_index_iterator_next(&it, &die, &file)) |
| 5470 |
return &drgn_not_found; |
| 5471 |
|
| 5472 |
struct drgn_qualified_type qualified_type; |
| 5473 |
err = drgn_type_from_dwarf(dbinfo, file, &die, &qualified_type); |
| 5474 |
if (err) |
| 5475 |
return err; |
| 5476 |
*ret = qualified_type.type; |
| 5477 |
return NULL; |
| 5478 |
} |
| 5479 |
|
| 5480 |
struct drgn_dwarf_member_thunk_arg { |
| 5481 |
struct drgn_elf_file *file; |
| 5482 |
Dwarf_Die die; |
| 5483 |
bool can_be_incomplete_array; |
| 5484 |
}; |
| 5485 |
|
| 5486 |
static struct drgn_error * |
| 5487 |
drgn_dwarf_member_thunk_fn(struct drgn_object *res, void *arg_) |
| 5488 |
{ |
| 5489 |
struct drgn_error *err; |
| 5490 |
struct drgn_dwarf_member_thunk_arg *arg = arg_; |
| 5491 |
if (res) { |
| 5492 |
struct drgn_qualified_type qualified_type; |
| 5493 |
err = drgn_type_from_dwarf_attr(&drgn_object_program(res)->dbinfo, |
| 5494 |
arg->file, &arg->die, NULL, |
| 5495 |
false, |
| 5496 |
arg->can_be_incomplete_array, |
| 5497 |
NULL, &qualified_type); |
| 5498 |
if (err) |
| 5499 |
return err; |
| 5500 |
|
| 5501 |
Dwarf_Attribute attr_mem, *attr; |
| 5502 |
uint64_t bit_field_size; |
| 5503 |
if ((attr = dwarf_attr_integrate(&arg->die, DW_AT_bit_size, |
| 5504 |
&attr_mem))) { |
| 5505 |
Dwarf_Word bit_size; |
| 5506 |
if (dwarf_formudata(attr, &bit_size)) { |
| 5507 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5508 |
"DW_TAG_member has invalid DW_AT_bit_size"); |
| 5509 |
} |
| 5510 |
bit_field_size = bit_size; |
| 5511 |
} else { |
| 5512 |
bit_field_size = 0; |
| 5513 |
} |
| 5514 |
|
| 5515 |
err = drgn_object_set_absent(res, qualified_type, |
| 5516 |
DRGN_ABSENCE_REASON_OTHER, |
| 5517 |
bit_field_size); |
| 5518 |
if (err) |
| 5519 |
return err; |
| 5520 |
} |
| 5521 |
free(arg); |
| 5522 |
return NULL; |
| 5523 |
} |
| 5524 |
|
| 5525 |
static inline bool drgn_dwarf_attribute_is_block(Dwarf_Attribute *attr) |
| 5526 |
{ |
| 5527 |
switch (attr->form) { |
| 5528 |
case DW_FORM_block1: |
| 5529 |
case DW_FORM_block2: |
| 5530 |
case DW_FORM_block4: |
| 5531 |
case DW_FORM_block: |
| 5532 |
return true; |
| 5533 |
default: |
| 5534 |
return false; |
| 5535 |
} |
| 5536 |
} |
| 5537 |
|
| 5538 |
static inline bool drgn_dwarf_attribute_is_ptr(Dwarf_Attribute *attr) |
| 5539 |
{ |
| 5540 |
switch (attr->form) { |
| 5541 |
case DW_FORM_sec_offset: |
| 5542 |
return true; |
| 5543 |
case DW_FORM_data4: |
| 5544 |
case DW_FORM_data8: { |
| 5545 |
/* |
| 5546 |
* dwarf_cu_die() always returns the DIE. We should use |
| 5547 |
* dwarf_cu_info(), but that requires elfutils >= 0.171. |
| 5548 |
*/ |
| 5549 |
Dwarf_Die unused; |
| 5550 |
Dwarf_Half cu_version; |
| 5551 |
dwarf_cu_die(attr->cu, &unused, &cu_version, NULL, NULL, NULL, |
| 5552 |
NULL, NULL); |
| 5553 |
return cu_version <= 3; |
| 5554 |
} |
| 5555 |
default: |
| 5556 |
return false; |
| 5557 |
} |
| 5558 |
} |
| 5559 |
|
| 5560 |
static struct drgn_error *invalid_data_member_location(struct binary_buffer *bb, |
| 5561 |
const char *pos, |
| 5562 |
const char *message) |
| 5563 |
{ |
| 5564 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5565 |
"DW_TAG_member has invalid DW_AT_data_member_location"); |
| 5566 |
} |
| 5567 |
|
| 5568 |
static struct drgn_error * |
| 5569 |
drgn_parse_dwarf_data_member_location(Dwarf_Attribute *attr, uint64_t *ret) |
| 5570 |
{ |
| 5571 |
struct drgn_error *err; |
| 5572 |
|
| 5573 |
if (drgn_dwarf_attribute_is_block(attr)) { |
| 5574 |
Dwarf_Block block; |
| 5575 |
if (dwarf_formblock(attr, &block)) |
| 5576 |
return drgn_error_libdw(); |
| 5577 |
/* |
| 5578 |
* In DWARF 2, DW_AT_data_member_location is always a location |
| 5579 |
* description. We can translate a DW_OP_plus_uconst expression |
| 5580 |
* into a constant offset; other expressions aren't supported |
| 5581 |
* yet. |
| 5582 |
*/ |
| 5583 |
struct binary_buffer bb; |
| 5584 |
/* |
| 5585 |
* Right now we only parse u8 and ULEB128, so the byte order |
| 5586 |
* doesn't matter. |
| 5587 |
*/ |
| 5588 |
binary_buffer_init(&bb, block.data, block.length, |
| 5589 |
HOST_LITTLE_ENDIAN, |
| 5590 |
invalid_data_member_location); |
| 5591 |
uint8_t opcode; |
| 5592 |
err = binary_buffer_next_u8(&bb, &opcode); |
| 5593 |
if (err) |
| 5594 |
return err; |
| 5595 |
if (opcode != DW_OP_plus_uconst) { |
| 5596 |
unsupported: |
| 5597 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5598 |
"DW_TAG_member has unsupported DW_AT_data_member_location"); |
| 5599 |
} |
| 5600 |
err = binary_buffer_next_uleb128(&bb, ret); |
| 5601 |
if (err) |
| 5602 |
return err; |
| 5603 |
if (binary_buffer_has_next(&bb)) |
| 5604 |
goto unsupported; |
| 5605 |
} else if (drgn_dwarf_attribute_is_ptr(attr)) { |
| 5606 |
goto unsupported; |
| 5607 |
} else { |
| 5608 |
|
| 5609 |
Dwarf_Word word; |
| 5610 |
if (dwarf_formudata(attr, &word)) |
| 5611 |
return invalid_data_member_location(NULL, NULL, NULL); |
| 5612 |
*ret = word; |
| 5613 |
} |
| 5614 |
return NULL; |
| 5615 |
} |
| 5616 |
|
| 5617 |
static struct drgn_error * |
| 5618 |
parse_member_offset(Dwarf_Die *die, union drgn_lazy_object *member_object, |
| 5619 |
bool little_endian, uint64_t *ret) |
| 5620 |
{ |
| 5621 |
struct drgn_error *err; |
| 5622 |
Dwarf_Attribute attr_mem; |
| 5623 |
Dwarf_Attribute *attr; |
| 5624 |
|
| 5625 |
/* |
| 5626 |
* The simplest case is when we have DW_AT_data_bit_offset, which is |
| 5627 |
* already the offset in bits from the beginning of the containing |
| 5628 |
* object to the beginning of the member (which may be a bit field). |
| 5629 |
*/ |
| 5630 |
attr = dwarf_attr_integrate(die, DW_AT_data_bit_offset, &attr_mem); |
| 5631 |
if (attr) { |
| 5632 |
Dwarf_Word bit_offset; |
| 5633 |
if (dwarf_formudata(attr, &bit_offset)) { |
| 5634 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5635 |
"DW_TAG_member has invalid DW_AT_data_bit_offset"); |
| 5636 |
} |
| 5637 |
*ret = bit_offset; |
| 5638 |
return NULL; |
| 5639 |
} |
| 5640 |
|
| 5641 |
/* |
| 5642 |
* Otherwise, we might have DW_AT_data_member_location, which is the |
| 5643 |
* offset in bytes from the beginning of the containing object. |
| 5644 |
*/ |
| 5645 |
attr = dwarf_attr_integrate(die, DW_AT_data_member_location, &attr_mem); |
| 5646 |
if (attr) { |
| 5647 |
err = drgn_parse_dwarf_data_member_location(attr, ret); |
| 5648 |
if (err) |
| 5649 |
return err; |
| 5650 |
*ret *= 8; |
| 5651 |
} else { |
| 5652 |
*ret = 0; |
| 5653 |
} |
| 5654 |
|
| 5655 |
/* |
| 5656 |
* In addition to DW_AT_data_member_location, a bit field might have |
| 5657 |
* DW_AT_bit_offset, which is the offset in bits of the most significant |
| 5658 |
* bit of the bit field from the most significant bit of the containing |
| 5659 |
* object. |
| 5660 |
*/ |
| 5661 |
attr = dwarf_attr_integrate(die, DW_AT_bit_offset, &attr_mem); |
| 5662 |
if (attr) { |
| 5663 |
Dwarf_Word bit_offset; |
| 5664 |
if (dwarf_formudata(attr, &bit_offset)) { |
| 5665 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5666 |
"DW_TAG_member has invalid DW_AT_bit_offset"); |
| 5667 |
} |
| 5668 |
|
| 5669 |
/* |
| 5670 |
* If the architecture is little-endian, then we must compute |
| 5671 |
* the location of the most significant bit from the size of the |
| 5672 |
* member, then subtract the bit offset and bit size to get the |
| 5673 |
* location of the beginning of the bit field. |
| 5674 |
* |
| 5675 |
* If the architecture is big-endian, then the most significant |
| 5676 |
* bit of the bit field is the beginning. |
| 5677 |
*/ |
| 5678 |
if (little_endian) { |
| 5679 |
err = drgn_lazy_object_evaluate(member_object); |
| 5680 |
if (err) |
| 5681 |
return err; |
| 5682 |
|
| 5683 |
attr = dwarf_attr_integrate(die, DW_AT_byte_size, |
| 5684 |
&attr_mem); |
| 5685 |
/* |
| 5686 |
* If the member has an explicit byte size, we can use |
| 5687 |
* that. Otherwise, we have to get it from the member |
| 5688 |
* type. |
| 5689 |
*/ |
| 5690 |
uint64_t byte_size; |
| 5691 |
if (attr) { |
| 5692 |
Dwarf_Word word; |
| 5693 |
if (dwarf_formudata(attr, &word)) { |
| 5694 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5695 |
"DW_TAG_member has invalid DW_AT_byte_size"); |
| 5696 |
} |
| 5697 |
byte_size = word; |
| 5698 |
} else { |
| 5699 |
if (!drgn_type_has_size(member_object->obj.type)) { |
| 5700 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5701 |
"DW_TAG_member bit field type does not have size"); |
| 5702 |
} |
| 5703 |
err = drgn_type_sizeof(member_object->obj.type, |
| 5704 |
&byte_size); |
| 5705 |
if (err) |
| 5706 |
return err; |
| 5707 |
} |
| 5708 |
*ret += 8 * byte_size - bit_offset - member_object->obj.bit_size; |
| 5709 |
} else { |
| 5710 |
*ret += bit_offset; |
| 5711 |
} |
| 5712 |
} |
| 5713 |
|
| 5714 |
return NULL; |
| 5715 |
} |
| 5716 |
|
| 5717 |
static struct drgn_error * |
| 5718 |
parse_member(struct drgn_debug_info *dbinfo, struct drgn_elf_file *file, |
| 5719 |
Dwarf_Die *die, bool little_endian, bool can_be_incomplete_array, |
| 5720 |
struct drgn_compound_type_builder *builder) |
| 5721 |
{ |
| 5722 |
struct drgn_error *err; |
| 5723 |
|
| 5724 |
Dwarf_Attribute attr_mem, *attr; |
| 5725 |
const char *name; |
| 5726 |
if ((attr = dwarf_attr_integrate(die, DW_AT_name, &attr_mem))) { |
| 5727 |
name = dwarf_formstring(attr); |
| 5728 |
if (!name) { |
| 5729 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5730 |
"DW_TAG_member has invalid DW_AT_name"); |
| 5731 |
} |
| 5732 |
} else { |
| 5733 |
name = NULL; |
| 5734 |
} |
| 5735 |
|
| 5736 |
struct drgn_dwarf_member_thunk_arg *thunk_arg = |
| 5737 |
malloc(sizeof(*thunk_arg)); |
| 5738 |
if (!thunk_arg) |
| 5739 |
return &drgn_enomem; |
| 5740 |
thunk_arg->file = file; |
| 5741 |
thunk_arg->die = *die; |
| 5742 |
thunk_arg->can_be_incomplete_array = can_be_incomplete_array; |
| 5743 |
|
| 5744 |
union drgn_lazy_object member_object; |
| 5745 |
drgn_lazy_object_init_thunk(&member_object, dbinfo->prog, |
| 5746 |
drgn_dwarf_member_thunk_fn, thunk_arg); |
| 5747 |
|
| 5748 |
uint64_t bit_offset; |
| 5749 |
err = parse_member_offset(die, &member_object, little_endian, |
| 5750 |
&bit_offset); |
| 5751 |
if (err) |
| 5752 |
goto err; |
| 5753 |
|
| 5754 |
err = drgn_compound_type_builder_add_member(builder, &member_object, |
| 5755 |
name, bit_offset); |
| 5756 |
if (err) |
| 5757 |
goto err; |
| 5758 |
return NULL; |
| 5759 |
|
| 5760 |
err: |
| 5761 |
drgn_lazy_object_deinit(&member_object); |
| 5762 |
return err; |
| 5763 |
} |
| 5764 |
|
| 5765 |
struct drgn_dwarf_die_thunk_arg { |
| 5766 |
struct drgn_elf_file *file; |
| 5767 |
Dwarf_Die die; |
| 5768 |
}; |
| 5769 |
|
| 5770 |
static struct drgn_error * |
| 5771 |
drgn_dwarf_template_type_parameter_thunk_fn(struct drgn_object *res, void *arg_) |
| 5772 |
{ |
| 5773 |
struct drgn_error *err; |
| 5774 |
struct drgn_dwarf_die_thunk_arg *arg = arg_; |
| 5775 |
if (res) { |
| 5776 |
struct drgn_qualified_type qualified_type; |
| 5777 |
err = drgn_type_from_dwarf_attr(&drgn_object_program(res)->dbinfo, |
| 5778 |
arg->file, &arg->die, NULL, |
| 5779 |
true, true, NULL, |
| 5780 |
&qualified_type); |
| 5781 |
if (err) |
| 5782 |
return err; |
| 5783 |
|
| 5784 |
err = drgn_object_set_absent(res, qualified_type, |
| 5785 |
DRGN_ABSENCE_REASON_OTHER, 0); |
| 5786 |
if (err) |
| 5787 |
return err; |
| 5788 |
} |
| 5789 |
free(arg); |
| 5790 |
return NULL; |
| 5791 |
} |
| 5792 |
|
| 5793 |
static struct drgn_error * |
| 5794 |
drgn_dwarf_template_value_parameter_thunk_fn(struct drgn_object *res, |
| 5795 |
void *arg_) |
| 5796 |
{ |
| 5797 |
struct drgn_error *err; |
| 5798 |
struct drgn_dwarf_die_thunk_arg *arg = arg_; |
| 5799 |
if (res) { |
| 5800 |
err = drgn_object_from_dwarf(&drgn_object_program(res)->dbinfo, |
| 5801 |
arg->file, &arg->die, NULL, NULL, |
| 5802 |
NULL, res); |
| 5803 |
if (err) |
| 5804 |
return err; |
| 5805 |
} |
| 5806 |
free(arg); |
| 5807 |
return NULL; |
| 5808 |
} |
| 5809 |
|
| 5810 |
static struct drgn_error * |
| 5811 |
maybe_parse_template_parameter(struct drgn_debug_info *dbinfo, |
| 5812 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 5813 |
struct drgn_template_parameters_builder *builder) |
| 5814 |
{ |
| 5815 |
drgn_object_thunk_fn *thunk_fn; |
| 5816 |
switch (dwarf_tag(die)) { |
| 5817 |
case DW_TAG_template_type_parameter: |
| 5818 |
thunk_fn = drgn_dwarf_template_type_parameter_thunk_fn; |
| 5819 |
break; |
| 5820 |
case DW_TAG_template_value_parameter: |
| 5821 |
thunk_fn = drgn_dwarf_template_value_parameter_thunk_fn; |
| 5822 |
break; |
| 5823 |
default: |
| 5824 |
return NULL; |
| 5825 |
} |
| 5826 |
|
| 5827 |
char tag_buf[DW_TAG_STR_BUF_LEN]; |
| 5828 |
|
| 5829 |
Dwarf_Attribute attr_mem, *attr; |
| 5830 |
const char *name; |
| 5831 |
if ((attr = dwarf_attr_integrate(die, DW_AT_name, &attr_mem))) { |
| 5832 |
name = dwarf_formstring(attr); |
| 5833 |
if (!name) { |
| 5834 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 5835 |
"%s has invalid DW_AT_name", |
| 5836 |
dwarf_tag_str(die, tag_buf)); |
| 5837 |
} |
| 5838 |
} else { |
| 5839 |
name = NULL; |
| 5840 |
} |
| 5841 |
|
| 5842 |
bool defaulted; |
| 5843 |
if (dwarf_flag_integrate(die, DW_AT_default_value, &defaulted)) { |
| 5844 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 5845 |
"%s has invalid DW_AT_default_value", |
| 5846 |
dwarf_tag_str(die, tag_buf)); |
| 5847 |
} |
| 5848 |
|
| 5849 |
struct drgn_dwarf_die_thunk_arg *thunk_arg = |
| 5850 |
malloc(sizeof(*thunk_arg)); |
| 5851 |
if (!thunk_arg) |
| 5852 |
return &drgn_enomem; |
| 5853 |
thunk_arg->file = file; |
| 5854 |
thunk_arg->die = *die; |
| 5855 |
|
| 5856 |
union drgn_lazy_object argument; |
| 5857 |
drgn_lazy_object_init_thunk(&argument, dbinfo->prog, thunk_fn, |
| 5858 |
thunk_arg); |
| 5859 |
|
| 5860 |
struct drgn_error *err = |
| 5861 |
drgn_template_parameters_builder_add(builder, &argument, name, |
| 5862 |
defaulted); |
| 5863 |
if (err) |
| 5864 |
drgn_lazy_object_deinit(&argument); |
| 5865 |
return err; |
| 5866 |
} |
| 5867 |
|
| 5868 |
static struct drgn_error * |
| 5869 |
drgn_parse_template_parameter_pack(struct drgn_debug_info *dbinfo, |
| 5870 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 5871 |
struct drgn_template_parameters_builder *builder) |
| 5872 |
{ |
| 5873 |
struct drgn_error *err; |
| 5874 |
Dwarf_Die child; |
| 5875 |
int r = dwarf_child(die, &child); |
| 5876 |
while (r == 0) { |
| 5877 |
err = maybe_parse_template_parameter(dbinfo, file, &child, builder); |
| 5878 |
if (err) |
| 5879 |
return err; |
| 5880 |
r = dwarf_siblingof(&child, &child); |
| 5881 |
} |
| 5882 |
if (r == -1) { |
| 5883 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 5884 |
"libdw could not parse DIE children"); |
| 5885 |
} |
| 5886 |
return NULL; |
| 5887 |
} |
| 5888 |
|
| 5889 |
static struct drgn_error * |
| 5890 |
drgn_compound_type_from_dwarf(struct drgn_debug_info *dbinfo, |
| 5891 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 5892 |
const struct drgn_language *lang, |
| 5893 |
enum drgn_type_kind kind, struct drgn_type **ret) |
| 5894 |
{ |
| 5895 |
struct drgn_error *err; |
| 5896 |
char tag_buf[DW_TAG_STR_BUF_LEN]; |
| 5897 |
|
| 5898 |
Dwarf_Attribute attr_mem; |
| 5899 |
Dwarf_Attribute *attr = dwarf_attr_integrate(die, DW_AT_name, |
| 5900 |
&attr_mem); |
| 5901 |
const char *tag; |
| 5902 |
if (attr) { |
| 5903 |
tag = dwarf_formstring(attr); |
| 5904 |
if (!tag) { |
| 5905 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 5906 |
"%s has invalid DW_AT_name", |
| 5907 |
dwarf_tag_str(die, tag_buf)); |
| 5908 |
} |
| 5909 |
} else { |
| 5910 |
tag = NULL; |
| 5911 |
} |
| 5912 |
|
| 5913 |
bool declaration; |
| 5914 |
if (dwarf_flag(die, DW_AT_declaration, &declaration)) { |
| 5915 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 5916 |
"%s has invalid DW_AT_declaration", |
| 5917 |
dwarf_tag_str(die, tag_buf)); |
| 5918 |
} |
| 5919 |
if (declaration && tag) { |
| 5920 |
err = drgn_debug_info_find_complete(dbinfo, dwarf_tag(die), tag, |
| 5921 |
die, lang, ret); |
| 5922 |
if (err != &drgn_not_found) |
| 5923 |
return err; |
| 5924 |
} |
| 5925 |
|
| 5926 |
struct drgn_compound_type_builder builder; |
| 5927 |
drgn_compound_type_builder_init(&builder, dbinfo->prog, kind); |
| 5928 |
|
| 5929 |
int size; |
| 5930 |
bool little_endian; |
| 5931 |
if (declaration) { |
| 5932 |
size = 0; |
| 5933 |
} else { |
| 5934 |
size = dwarf_bytesize(die); |
| 5935 |
if (size == -1) { |
| 5936 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 5937 |
"%s has missing or invalid DW_AT_byte_size", |
| 5938 |
dwarf_tag_str(die, tag_buf)); |
| 5939 |
} |
| 5940 |
dwarf_die_is_little_endian(die, false, &little_endian); |
| 5941 |
} |
| 5942 |
|
| 5943 |
Dwarf_Die member = {}, child; |
| 5944 |
bool first_member = true; |
| 5945 |
int r = dwarf_child(die, &child); |
| 5946 |
while (r == 0) { |
| 5947 |
switch (dwarf_tag(&child)) { |
| 5948 |
case DW_TAG_member: |
| 5949 |
if (!declaration) { |
| 5950 |
if (member.addr) { |
| 5951 |
err = parse_member(dbinfo, file, |
| 5952 |
&member, |
| 5953 |
little_endian, false, |
| 5954 |
&builder); |
| 5955 |
if (err) |
| 5956 |
goto err; |
| 5957 |
first_member = false; |
| 5958 |
} |
| 5959 |
member = child; |
| 5960 |
} |
| 5961 |
break; |
| 5962 |
case DW_TAG_template_type_parameter: |
| 5963 |
case DW_TAG_template_value_parameter: |
| 5964 |
err = maybe_parse_template_parameter(dbinfo, file, &child, |
| 5965 |
&builder.template_builder); |
| 5966 |
if (err) |
| 5967 |
goto err; |
| 5968 |
break; |
| 5969 |
case DW_TAG_GNU_template_parameter_pack: |
| 5970 |
err = drgn_parse_template_parameter_pack(dbinfo, file, &child, |
| 5971 |
&builder.template_builder); |
| 5972 |
if (err) |
| 5973 |
goto err; |
| 5974 |
break; |
| 5975 |
default: |
| 5976 |
break; |
| 5977 |
} |
| 5978 |
r = dwarf_siblingof(&child, &child); |
| 5979 |
} |
| 5980 |
if (r == -1) { |
| 5981 |
err = drgn_error_create(DRGN_ERROR_OTHER, |
| 5982 |
"libdw could not parse DIE children"); |
| 5983 |
goto err; |
| 5984 |
} |
| 5985 |
/* |
| 5986 |
* Flexible array members are only allowed as the last member of a |
| 5987 |
* structure with at least one other member. |
| 5988 |
*/ |
| 5989 |
if (member.addr) { |
| 5990 |
err = parse_member(dbinfo, file, &member, little_endian, |
| 5991 |
kind != DRGN_TYPE_UNION && !first_member, |
| 5992 |
&builder); |
| 5993 |
if (err) |
| 5994 |
goto err; |
| 5995 |
} |
| 5996 |
|
| 5997 |
err = drgn_compound_type_create(&builder, tag, size, !declaration, lang, |
| 5998 |
ret); |
| 5999 |
if (err) |
| 6000 |
goto err; |
| 6001 |
return NULL; |
| 6002 |
|
| 6003 |
err: |
| 6004 |
drgn_compound_type_builder_deinit(&builder); |
| 6005 |
return err; |
| 6006 |
} |
| 6007 |
|
| 6008 |
static struct drgn_error * |
| 6009 |
parse_enumerator(Dwarf_Die *die, struct drgn_enum_type_builder *builder, |
| 6010 |
bool *is_signed) |
| 6011 |
{ |
| 6012 |
const char *name = dwarf_diename(die); |
| 6013 |
if (!name) { |
| 6014 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6015 |
"DW_TAG_enumerator has missing or invalid DW_AT_name"); |
| 6016 |
} |
| 6017 |
|
| 6018 |
Dwarf_Attribute attr_mem, *attr; |
| 6019 |
if (!(attr = dwarf_attr_integrate(die, DW_AT_const_value, &attr_mem))) { |
| 6020 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6021 |
"DW_TAG_enumerator is missing DW_AT_const_value"); |
| 6022 |
} |
| 6023 |
struct drgn_error *err; |
| 6024 |
if (attr->form == DW_FORM_sdata || |
| 6025 |
attr->form == DW_FORM_implicit_const) { |
| 6026 |
Dwarf_Sword svalue; |
| 6027 |
if (dwarf_formsdata(attr, &svalue)) |
| 6028 |
goto invalid; |
| 6029 |
err = drgn_enum_type_builder_add_signed(builder, name, |
| 6030 |
svalue); |
| 6031 |
/* |
| 6032 |
* GCC before 7.1 didn't include DW_AT_encoding for |
| 6033 |
* DW_TAG_enumeration_type DIEs, so we have to guess the sign |
| 6034 |
* for enum_compatible_type_fallback(). |
| 6035 |
*/ |
| 6036 |
if (!err && svalue < 0) |
| 6037 |
*is_signed = true; |
| 6038 |
} else { |
| 6039 |
Dwarf_Word uvalue; |
| 6040 |
if (dwarf_formudata(attr, &uvalue)) |
| 6041 |
goto invalid; |
| 6042 |
err = drgn_enum_type_builder_add_unsigned(builder, name, |
| 6043 |
uvalue); |
| 6044 |
} |
| 6045 |
return err; |
| 6046 |
|
| 6047 |
invalid: |
| 6048 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6049 |
"DW_TAG_enumerator has invalid DW_AT_const_value"); |
| 6050 |
} |
| 6051 |
|
| 6052 |
/* |
| 6053 |
* GCC before 5.1 did not include DW_AT_type for DW_TAG_enumeration_type DIEs, |
| 6054 |
* so we have to fabricate the compatible type. |
| 6055 |
*/ |
| 6056 |
static struct drgn_error * |
| 6057 |
enum_compatible_type_fallback(struct drgn_debug_info *dbinfo, |
| 6058 |
Dwarf_Die *die, bool is_signed, |
| 6059 |
const struct drgn_language *lang, |
| 6060 |
struct drgn_type **ret) |
| 6061 |
{ |
| 6062 |
int size = dwarf_bytesize(die); |
| 6063 |
if (size == -1) { |
| 6064 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6065 |
"DW_TAG_enumeration_type has missing or invalid DW_AT_byte_size"); |
| 6066 |
} |
| 6067 |
enum drgn_byte_order byte_order; |
| 6068 |
dwarf_die_byte_order(die, false, &byte_order); |
| 6069 |
return drgn_int_type_create(dbinfo->prog, "<unknown>", size, is_signed, |
| 6070 |
byte_order, lang, ret); |
| 6071 |
} |
| 6072 |
|
| 6073 |
static struct drgn_error * |
| 6074 |
drgn_enum_type_from_dwarf(struct drgn_debug_info *dbinfo, |
| 6075 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 6076 |
const struct drgn_language *lang, |
| 6077 |
struct drgn_type **ret) |
| 6078 |
{ |
| 6079 |
struct drgn_error *err; |
| 6080 |
|
| 6081 |
Dwarf_Attribute attr_mem; |
| 6082 |
Dwarf_Attribute *attr = dwarf_attr_integrate(die, DW_AT_name, |
| 6083 |
&attr_mem); |
| 6084 |
const char *tag; |
| 6085 |
if (attr) { |
| 6086 |
tag = dwarf_formstring(attr); |
| 6087 |
if (!tag) |
| 6088 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6089 |
"DW_TAG_enumeration_type has invalid DW_AT_name"); |
| 6090 |
} else { |
| 6091 |
tag = NULL; |
| 6092 |
} |
| 6093 |
|
| 6094 |
bool declaration; |
| 6095 |
if (dwarf_flag(die, DW_AT_declaration, &declaration)) { |
| 6096 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6097 |
"DW_TAG_enumeration_type has invalid DW_AT_declaration"); |
| 6098 |
} |
| 6099 |
if (declaration && tag) { |
| 6100 |
err = drgn_debug_info_find_complete(dbinfo, |
| 6101 |
DW_TAG_enumeration_type, |
| 6102 |
tag, die, lang, ret); |
| 6103 |
if (err != &drgn_not_found) |
| 6104 |
return err; |
| 6105 |
} |
| 6106 |
|
| 6107 |
if (declaration) { |
| 6108 |
return drgn_incomplete_enum_type_create(dbinfo->prog, tag, lang, |
| 6109 |
ret); |
| 6110 |
} |
| 6111 |
|
| 6112 |
struct drgn_enum_type_builder builder; |
| 6113 |
drgn_enum_type_builder_init(&builder, dbinfo->prog); |
| 6114 |
bool is_signed = false; |
| 6115 |
Dwarf_Die child; |
| 6116 |
int r = dwarf_child(die, &child); |
| 6117 |
while (r == 0) { |
| 6118 |
if (dwarf_tag(&child) == DW_TAG_enumerator) { |
| 6119 |
err = parse_enumerator(&child, &builder, &is_signed); |
| 6120 |
if (err) |
| 6121 |
goto err; |
| 6122 |
} |
| 6123 |
r = dwarf_siblingof(&child, &child); |
| 6124 |
} |
| 6125 |
if (r == -1) { |
| 6126 |
err = drgn_error_create(DRGN_ERROR_OTHER, |
| 6127 |
"libdw could not parse DIE children"); |
| 6128 |
goto err; |
| 6129 |
} |
| 6130 |
|
| 6131 |
struct drgn_type *compatible_type; |
| 6132 |
r = dwarf_type(die, &child); |
| 6133 |
if (r == -1) { |
| 6134 |
err = drgn_error_create(DRGN_ERROR_OTHER, |
| 6135 |
"DW_TAG_enumeration_type has invalid DW_AT_type"); |
| 6136 |
goto err; |
| 6137 |
} else if (r) { |
| 6138 |
err = enum_compatible_type_fallback(dbinfo, die, is_signed, |
| 6139 |
lang, &compatible_type); |
| 6140 |
if (err) |
| 6141 |
goto err; |
| 6142 |
} else { |
| 6143 |
struct drgn_qualified_type qualified_compatible_type; |
| 6144 |
err = drgn_type_from_dwarf(dbinfo, file, &child, |
| 6145 |
&qualified_compatible_type); |
| 6146 |
if (err) |
| 6147 |
goto err; |
| 6148 |
compatible_type = |
| 6149 |
drgn_underlying_type(qualified_compatible_type.type); |
| 6150 |
if (drgn_type_kind(compatible_type) != DRGN_TYPE_INT) { |
| 6151 |
err = drgn_error_create(DRGN_ERROR_OTHER, |
| 6152 |
"DW_AT_type of DW_TAG_enumeration_type is not an integer type"); |
| 6153 |
goto err; |
| 6154 |
} |
| 6155 |
} |
| 6156 |
|
| 6157 |
err = drgn_enum_type_create(&builder, tag, compatible_type, lang, ret); |
| 6158 |
if (err) |
| 6159 |
goto err; |
| 6160 |
return NULL; |
| 6161 |
|
| 6162 |
err: |
| 6163 |
drgn_enum_type_builder_deinit(&builder); |
| 6164 |
return err; |
| 6165 |
} |
| 6166 |
|
| 6167 |
static struct drgn_error * |
| 6168 |
drgn_typedef_type_from_dwarf(struct drgn_debug_info *dbinfo, |
| 6169 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 6170 |
const struct drgn_language *lang, |
| 6171 |
bool can_be_incomplete_array, |
| 6172 |
bool *is_incomplete_array_ret, |
| 6173 |
struct drgn_type **ret) |
| 6174 |
{ |
| 6175 |
const char *name = dwarf_diename(die); |
| 6176 |
if (!name) { |
| 6177 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6178 |
"DW_TAG_typedef has missing or invalid DW_AT_name"); |
| 6179 |
} |
| 6180 |
|
| 6181 |
struct drgn_qualified_type aliased_type; |
| 6182 |
struct drgn_error *err = drgn_type_from_dwarf_attr(dbinfo, file, die, |
| 6183 |
lang, true, |
| 6184 |
can_be_incomplete_array, |
| 6185 |
is_incomplete_array_ret, |
| 6186 |
&aliased_type); |
| 6187 |
if (err) |
| 6188 |
return err; |
| 6189 |
|
| 6190 |
return drgn_typedef_type_create(dbinfo->prog, name, aliased_type, lang, |
| 6191 |
ret); |
| 6192 |
} |
| 6193 |
|
| 6194 |
static struct drgn_error * |
| 6195 |
drgn_pointer_type_from_dwarf(struct drgn_debug_info *dbinfo, |
| 6196 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 6197 |
const struct drgn_language *lang, |
| 6198 |
struct drgn_type **ret) |
| 6199 |
{ |
| 6200 |
struct drgn_qualified_type referenced_type; |
| 6201 |
struct drgn_error *err = drgn_type_from_dwarf_attr(dbinfo, file, die, |
| 6202 |
lang, true, true, |
| 6203 |
NULL, |
| 6204 |
&referenced_type); |
| 6205 |
if (err) |
| 6206 |
return err; |
| 6207 |
|
| 6208 |
Dwarf_Attribute attr_mem, *attr; |
| 6209 |
uint64_t size; |
| 6210 |
if ((attr = dwarf_attr_integrate(die, DW_AT_byte_size, &attr_mem))) { |
| 6211 |
Dwarf_Word word; |
| 6212 |
if (dwarf_formudata(attr, &word)) { |
| 6213 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 6214 |
"DW_TAG_pointer_type has invalid DW_AT_byte_size"); |
| 6215 |
} |
| 6216 |
size = word; |
| 6217 |
} else { |
| 6218 |
// dwarf_diecu() always returns the DIE. We should use |
| 6219 |
// dwarf_cu_info(), but that requires elfutils >= 0.171. |
| 6220 |
Dwarf_Die unused; |
| 6221 |
uint8_t address_size; |
| 6222 |
dwarf_diecu(die, &unused, &address_size, NULL); |
| 6223 |
size = address_size; |
| 6224 |
} |
| 6225 |
|
| 6226 |
/* |
| 6227 |
* The DWARF 5 specification doesn't mention DW_AT_endianity for |
| 6228 |
* DW_TAG_pointer_type DIEs, and GCC as of version 10.2 doesn't emit it |
| 6229 |
* even for pointers stored in the opposite byte order (e.g., when using |
| 6230 |
* scalar_storage_order), but it probably should. |
| 6231 |
*/ |
| 6232 |
enum drgn_byte_order byte_order; |
| 6233 |
dwarf_die_byte_order(die, false, &byte_order); |
| 6234 |
return drgn_pointer_type_create(dbinfo->prog, referenced_type, size, |
| 6235 |
byte_order, lang, ret); |
| 6236 |
} |
| 6237 |
|
| 6238 |
struct array_dimension { |
| 6239 |
uint64_t length; |
| 6240 |
bool is_complete; |
| 6241 |
}; |
| 6242 |
|
| 6243 |
DEFINE_VECTOR(array_dimension_vector, struct array_dimension); |
| 6244 |
|
| 6245 |
static struct drgn_error *subrange_length(Dwarf_Die *die, |
| 6246 |
struct array_dimension *dimension) |
| 6247 |
{ |
| 6248 |
Dwarf_Attribute attr_mem; |
| 6249 |
Dwarf_Attribute *attr; |
| 6250 |
Dwarf_Word word; |
| 6251 |
|
| 6252 |
if (!(attr = dwarf_attr_integrate(die, DW_AT_upper_bound, &attr_mem)) && |
| 6253 |
!(attr = dwarf_attr_integrate(die, DW_AT_count, &attr_mem))) { |
| 6254 |
dimension->is_complete = false; |
| 6255 |
return NULL; |
| 6256 |
} |
| 6257 |
|
| 6258 |
if (dwarf_formudata(attr, &word)) { |
| 6259 |
return drgn_error_format(DRGN_ERROR_OTHER, |
| 6260 |
"DW_TAG_subrange_type has invalid %s", |
| 6261 |
attr->code == DW_AT_upper_bound ? |
| 6262 |
"DW_AT_upper_bound" : |
| 6263 |
"DW_AT_count"); |
| 6264 |
} |
| 6265 |
|
| 6266 |
dimension->is_complete = true; |
| 6267 |
/* |
| 6268 |
* GCC emits a DW_FORM_sdata or DW_FORM_data8 DW_AT_upper_bound of -1 |
| 6269 |
* for empty array variables without an explicit size |
| 6270 |
* (e.g., `int arr[] = {};`). |
| 6271 |
*/ |
| 6272 |
if (attr->code == DW_AT_upper_bound && word == (Dwarf_Word)-1) { |
| 6273 |
dimension->length = 0; |
| 6274 |
} else if (attr->code == DW_AT_upper_bound) { |
| 6275 |
if (word >= UINT64_MAX) { |
| 6276 |
return drgn_error_create(DRGN_ERROR_OVERFLOW, |
| 6277 |
"DW_AT_upper_bound is too large"); |
| 6278 |
} |
| 6279 |
dimension->length = (uint64_t)word + 1; |
| 6280 |
} else { |
| 6281 |
if (word > UINT64_MAX) { |
| 6282 |
return drgn_error_create(DRGN_ERROR_OVERFLOW, |
| 6283 |
"DW_AT_count is too large"); |
| 6284 |
} |
| 6285 |
dimension->length = word; |
| 6286 |
} |
| 6287 |
return NULL; |
| 6288 |
} |
| 6289 |
|
| 6290 |
static struct drgn_error * |
| 6291 |
drgn_array_type_from_dwarf(struct drgn_debug_info *dbinfo, |
| 6292 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 6293 |
const struct drgn_language *lang, |
| 6294 |
bool can_be_incomplete_array, |
| 6295 |
bool *is_incomplete_array_ret, |
| 6296 |
struct drgn_type **ret) |
| 6297 |
{ |
| 6298 |
struct drgn_error *err; |
| 6299 |
VECTOR(array_dimension_vector, dimensions); |
| 6300 |
struct array_dimension *dimension; |
| 6301 |
Dwarf_Die child; |
| 6302 |
int r = dwarf_child(die, &child); |
| 6303 |
while (r == 0) { |
| 6304 |
if (dwarf_tag(&child) == DW_TAG_subrange_type) { |
| 6305 |
dimension = array_dimension_vector_append_entry(&dimensions); |
| 6306 |
if (!dimension) |
| 6307 |
return &drgn_enomem; |
| 6308 |
err = subrange_length(&child, dimension); |
| 6309 |
if (err) |
| 6310 |
return err; |
| 6311 |
} |
| 6312 |
r = dwarf_siblingof(&child, &child); |
| 6313 |
} |
| 6314 |
if (r == -1) { |
| 6315 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6316 |
"libdw could not parse DIE children"); |
| 6317 |
} |
| 6318 |
if (array_dimension_vector_empty(&dimensions)) { |
| 6319 |
dimension = array_dimension_vector_append_entry(&dimensions); |
| 6320 |
if (!dimension) |
| 6321 |
return &drgn_enomem; |
| 6322 |
dimension->is_complete = false; |
| 6323 |
} |
| 6324 |
|
| 6325 |
struct drgn_qualified_type element_type; |
| 6326 |
err = drgn_type_from_dwarf_attr(dbinfo, file, die, lang, false, false, |
| 6327 |
NULL, &element_type); |
| 6328 |
if (err) |
| 6329 |
return err; |
| 6330 |
|
| 6331 |
*is_incomplete_array_ret = |
| 6332 |
!array_dimension_vector_first(&dimensions)->is_complete; |
| 6333 |
struct drgn_type *type; |
| 6334 |
do { |
| 6335 |
dimension = array_dimension_vector_pop(&dimensions); |
| 6336 |
if (dimension->is_complete) { |
| 6337 |
err = drgn_array_type_create(dbinfo->prog, element_type, |
| 6338 |
dimension->length, lang, |
| 6339 |
&type); |
| 6340 |
} else if (!array_dimension_vector_empty(&dimensions) |
| 6341 |
|| !can_be_incomplete_array) { |
| 6342 |
err = drgn_array_type_create(dbinfo->prog, element_type, |
| 6343 |
0, lang, &type); |
| 6344 |
} else { |
| 6345 |
err = drgn_incomplete_array_type_create(dbinfo->prog, |
| 6346 |
element_type, |
| 6347 |
lang, &type); |
| 6348 |
} |
| 6349 |
if (err) |
| 6350 |
return err; |
| 6351 |
|
| 6352 |
element_type.type = type; |
| 6353 |
element_type.qualifiers = 0; |
| 6354 |
} while (!array_dimension_vector_empty(&dimensions)); |
| 6355 |
|
| 6356 |
*ret = type; |
| 6357 |
return NULL; |
| 6358 |
} |
| 6359 |
|
| 6360 |
static struct drgn_error * |
| 6361 |
drgn_dwarf_formal_parameter_thunk_fn(struct drgn_object *res, void *arg_) |
| 6362 |
{ |
| 6363 |
struct drgn_error *err; |
| 6364 |
struct drgn_dwarf_die_thunk_arg *arg = arg_; |
| 6365 |
if (res) { |
| 6366 |
struct drgn_qualified_type qualified_type; |
| 6367 |
err = drgn_type_from_dwarf_attr(&drgn_object_program(res)->dbinfo, |
| 6368 |
arg->file, &arg->die, NULL, |
| 6369 |
false, true, NULL, |
| 6370 |
&qualified_type); |
| 6371 |
if (err) |
| 6372 |
return err; |
| 6373 |
|
| 6374 |
err = drgn_object_set_absent(res, qualified_type, |
| 6375 |
DRGN_ABSENCE_REASON_OTHER, 0); |
| 6376 |
if (err) |
| 6377 |
return err; |
| 6378 |
} |
| 6379 |
free(arg); |
| 6380 |
return NULL; |
| 6381 |
} |
| 6382 |
|
| 6383 |
static struct drgn_error * |
| 6384 |
parse_formal_parameter(struct drgn_debug_info *dbinfo, |
| 6385 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 6386 |
struct drgn_function_type_builder *builder) |
| 6387 |
{ |
| 6388 |
Dwarf_Attribute attr_mem, *attr; |
| 6389 |
const char *name; |
| 6390 |
if ((attr = dwarf_attr_integrate(die, DW_AT_name, &attr_mem))) { |
| 6391 |
name = dwarf_formstring(attr); |
| 6392 |
if (!name) { |
| 6393 |
return drgn_error_create(DRGN_ERROR_OTHER, |
| 6394 |
"DW_TAG_formal_parameter has invalid DW_AT_name"); |
| 6395 |
} |
| 6396 |
} else { |
| 6397 |
name = NULL; |
| 6398 |
} |
| 6399 |
|
| 6400 |
struct drgn_dwarf_die_thunk_arg *thunk_arg = |
| 6401 |
malloc(sizeof(*thunk_arg)); |
| 6402 |
if (!thunk_arg) |
| 6403 |
return &drgn_enomem; |
| 6404 |
thunk_arg->file = file; |
| 6405 |
thunk_arg->die = *die; |
| 6406 |
|
| 6407 |
union drgn_lazy_object default_argument; |
| 6408 |
drgn_lazy_object_init_thunk(&default_argument, dbinfo->prog, |
| 6409 |
drgn_dwarf_formal_parameter_thunk_fn, |
| 6410 |
thunk_arg); |
| 6411 |
|
| 6412 |
struct drgn_error *err = |
| 6413 |
drgn_function_type_builder_add_parameter(builder, |
| 6414 |
&default_argument, |
| 6415 |
name); |
| 6416 |
if (err) |
| 6417 |
drgn_lazy_object_deinit(&default_argument); |
| 6418 |
return err; |
| 6419 |
} |
| 6420 |
|
| 6421 |
static struct drgn_error * |
| 6422 |
drgn_function_type_from_dwarf(struct drgn_debug_info *dbinfo, |
| 6423 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 6424 |
const struct drgn_language *lang, |
| 6425 |
struct drgn_type **ret) |
| 6426 |
{ |
| 6427 |
struct drgn_error *err; |
| 6428 |
char tag_buf[DW_TAG_STR_BUF_LEN]; |
| 6429 |
|
| 6430 |
struct drgn_function_type_builder builder; |
| 6431 |
drgn_function_type_builder_init(&builder, dbinfo->prog); |
| 6432 |
bool is_variadic = false; |
| 6433 |
Dwarf_Die child; |
| 6434 |
int r = dwarf_child(die, &child); |
| 6435 |
while (r == 0) { |
| 6436 |
switch (dwarf_tag(&child)) { |
| 6437 |
case DW_TAG_formal_parameter: |
| 6438 |
if (is_variadic) { |
| 6439 |
err = drgn_error_format(DRGN_ERROR_OTHER, |
| 6440 |
"%s has DW_TAG_formal_parameter child after DW_TAG_unspecified_parameters child", |
| 6441 |
dwarf_tag_str(die, |
| 6442 |
tag_buf)); |
| 6443 |
goto err; |
| 6444 |
} |
| 6445 |
err = parse_formal_parameter(dbinfo, file, &child, |
| 6446 |
&builder); |
| 6447 |
if (err) |
| 6448 |
goto err; |
| 6449 |
break; |
| 6450 |
case DW_TAG_unspecified_parameters: |
| 6451 |
if (is_variadic) { |
| 6452 |
err = drgn_error_format(DRGN_ERROR_OTHER, |
| 6453 |
"%s has multiple DW_TAG_unspecified_parameters children", |
| 6454 |
dwarf_tag_str(die, |
| 6455 |
tag_buf)); |
| 6456 |
goto err; |
| 6457 |
} |
| 6458 |
is_variadic = true; |
| 6459 |
break; |
| 6460 |
case DW_TAG_template_type_parameter: |
| 6461 |
case DW_TAG_template_value_parameter: |
| 6462 |
err = maybe_parse_template_parameter(dbinfo, file, &child, |
| 6463 |
&builder.template_builder); |
| 6464 |
if (err) |
| 6465 |
goto err; |
| 6466 |
break; |
| 6467 |
case DW_TAG_GNU_template_parameter_pack: |
| 6468 |
err = drgn_parse_template_parameter_pack(dbinfo, file, &child, |
| 6469 |
&builder.template_builder); |
| 6470 |
if (err) |
| 6471 |
goto err; |
| 6472 |
break; |
| 6473 |
default: |
| 6474 |
break; |
| 6475 |
} |
| 6476 |
r = dwarf_siblingof(&child, &child); |
| 6477 |
} |
| 6478 |
if (r == -1) { |
| 6479 |
err = drgn_error_create(DRGN_ERROR_OTHER, |
| 6480 |
"libdw could not parse DIE children"); |
| 6481 |
goto err; |
| 6482 |
} |
| 6483 |
|
| 6484 |
struct drgn_qualified_type return_type; |
| 6485 |
err = drgn_type_from_dwarf_attr(dbinfo, file, die, lang, true, true, |
| 6486 |
NULL, &return_type); |
| 6487 |
if (err) |
| 6488 |
goto err; |
| 6489 |
|
| 6490 |
err = drgn_function_type_create(&builder, return_type, is_variadic, |
| 6491 |
lang, ret); |
| 6492 |
if (err) |
| 6493 |
goto err; |
| 6494 |
return NULL; |
| 6495 |
|
| 6496 |
err: |
| 6497 |
drgn_function_type_builder_deinit(&builder); |
| 6498 |
return err; |
| 6499 |
} |
| 6500 |
|
| 6501 |
static struct drgn_error * |
| 6502 |
drgn_type_from_dwarf_internal(struct drgn_debug_info *dbinfo, |
| 6503 |
struct drgn_elf_file *file, Dwarf_Die *die, |
| 6504 |
bool can_be_incomplete_array, |
| 6505 |
bool *is_incomplete_array_ret, |
| 6506 |
struct drgn_qualified_type *ret) |
| 6507 |
{ |
| 6508 |
drgn_recursion_guard(1000, "maximum DWARF type parsing depth exceeded"); |
| 6509 |
|
| 6510 |
/* If the DIE has a type unit signature, follow it. */ |
| 6511 |
Dwarf_Die definition_die; |
| 6512 |
{ |
| 6513 |
Dwarf_Attribute attr_mem, *attr; |
| 6514 |
if ((attr = dwarf_attr_integrate(die, DW_AT_signature, |
| 6515 |
&attr_mem))) { |
| 6516 |
if (!dwarf_formref_die(attr, &definition_die)) |
| 6517 |
return drgn_error_libdw(); |
| 6518 |
die = &definition_die; |
| 6519 |
} |
| 6520 |
} |
| 6521 |
|
| 6522 |
/* If we got a declaration, try to find the definition. */ |
| 6523 |
bool declaration; |
| 6524 |
if (dwarf_flag(die, DW_AT_declaration, &declaration)) |
| 6525 |
return drgn_error_libdw(); |
| 6526 |
if (declaration) { |
| 6527 |
uintptr_t die_addr; |
| 6528 |
if (drgn_dwarf_find_definition(dbinfo, (uintptr_t)die->addr, |
| 6529 |
&die_addr)) { |
| 6530 |
struct drgn_dwarf_index_cu *cu = |
| 6531 |
drgn_dwarf_index_find_cu(dbinfo, die_addr); |
| 6532 |
definition_die = (Dwarf_Die){ |
| 6533 |
.addr = (void *)die_addr, |
| 6534 |
.cu = cu->libdw_cu, |
| 6535 |
}; |
| 6536 |
die = &definition_die; |
| 6537 |
file = cu->file; |
| 6538 |
} |
| 6539 |
} |
| 6540 |
|
| 6541 |
struct drgn_dwarf_type_map_entry entry = { |
| 6542 |
.key = die->addr, |
| 6543 |
}; |
| 6544 |
struct hash_pair hp = drgn_dwarf_type_map_hash(&entry.key); |
| 6545 |
struct drgn_dwarf_type_map_iterator it = |
| 6546 |
drgn_dwarf_type_map_search_hashed(&dbinfo->dwarf.types, |
| 6547 |
&entry.key, hp); |
| 6548 |
if (it.entry) { |
| 6549 |
if (!can_be_incomplete_array && |
| 6550 |
it.entry->value.is_incomplete_array) { |
| 6551 |
it = drgn_dwarf_type_map_search_hashed(&dbinfo->dwarf.cant_be_incomplete_array_types, |
| 6552 |
&entry.key, hp); |
| 6553 |
} |
| 6554 |
if (it.entry) { |
| 6555 |
ret->type = it.entry->value.type; |
| 6556 |
ret->qualifiers = it.entry->value.qualifiers; |
| 6557 |
return NULL; |
| 6558 |
} |
| 6559 |
} |
| 6560 |
|
| 6561 |
const struct drgn_language *lang; |
| 6562 |
struct drgn_error *err = drgn_language_from_die(die, true, &lang); |
| 6563 |
if (err) |
| 6564 |
return err; |
| 6565 |
|
| 6566 |
ret->qualifiers = 0; |
| 6567 |
entry.value.is_incomplete_array = false; |
| 6568 |
switch (dwarf_tag(die)) { |
| 6569 |
case DW_TAG_const_type: |
| 6570 |
err = drgn_type_from_dwarf_attr(dbinfo, file, die, lang, true, |
| 6571 |
can_be_incomplete_array, |
| 6572 |
&entry.value.is_incomplete_array, |
| 6573 |
ret); |
| 6574 |
ret->qualifiers |= DRGN_QUALIFIER_CONST; |
| 6575 |
break; |
| 6576 |
case DW_TAG_restrict_type: |
| 6577 |
err = drgn_type_from_dwarf_attr(dbinfo, file, die, lang, true, |
| 6578 |
can_be_incomplete_array, |
| 6579 |
&entry.value.is_incomplete_array, |
| 6580 |
ret); |
| 6581 |
ret->qualifiers |= DRGN_QUALIFIER_RESTRICT; |
| 6582 |
break; |
| 6583 |
case DW_TAG_volatile_type: |
| 6584 |
err = drgn_type_from_dwarf_attr(dbinfo, file, die, lang, true, |
| 6585 |
can_be_incomplete_array, |
| 6586 |
&entry.value.is_incomplete_array, |
| 6587 |
ret); |
| 6588 |
ret->qualifiers |= DRGN_QUALIFIER_VOLATILE; |
| 6589 |
break; |
| 6590 |
case DW_TAG_atomic_type: |
| 6591 |
err = drgn_type_from_dwarf_attr(dbinfo, file, die, lang, true, |
| 6592 |
can_be_incomplete_array, |
| 6593 |
&entry.value.is_incomplete_array, |
| 6594 |
ret); |
| 6595 |
ret->qualifiers |= DRGN_QUALIFIER_ATOMIC; |
| 6596 |
break; |
| 6597 |
case DW_TAG_base_type: |
| 6598 |
err = drgn_base_type_from_dwarf(dbinfo, file, die, lang, |
| 6599 |
&ret->type); |
| 6600 |
break; |
| 6601 |
case DW_TAG_structure_type: |
| 6602 |
err = drgn_compound_type_from_dwarf(dbinfo, file, die, lang, |
| 6603 |
DRGN_TYPE_STRUCT, |
| 6604 |
&ret->type); |
| 6605 |
break; |
| 6606 |
case DW_TAG_union_type: |
| 6607 |
err = drgn_compound_type_from_dwarf(dbinfo, file, die, lang, |
| 6608 |
DRGN_TYPE_UNION, |
| 6609 |
&ret->type); |
| 6610 |
break; |
| 6611 |
case DW_TAG_class_type: |
| 6612 |
err = drgn_compound_type_from_dwarf(dbinfo, file, die, lang, |
| 6613 |
DRGN_TYPE_CLASS, |
| 6614 |
&ret->type); |
| 6615 |
break; |
| 6616 |
case DW_TAG_enumeration_type: |
| 6617 |
err = drgn_enum_type_from_dwarf(dbinfo, file, die, lang, |
| 6618 |
&ret->type); |
| 6619 |
break; |
| 6620 |
case DW_TAG_typedef: |
| 6621 |
err = drgn_typedef_type_from_dwarf(dbinfo, file, die, lang, |
| 6622 |
can_be_incomplete_array, |
| 6623 |
&entry.value.is_incomplete_array, |
| 6624 |
&ret->type); |
| 6625 |
break; |
| 6626 |
case DW_TAG_pointer_type: |
| 6627 |
err = drgn_pointer_type_from_dwarf(dbinfo, file, die, lang, |
| 6628 |
&ret->type); |
| 6629 |
break; |
| 6630 |
case DW_TAG_array_type: |
| 6631 |
err = drgn_array_type_from_dwarf(dbinfo, file, die, lang, |
| 6632 |
can_be_incomplete_array, |
| 6633 |
&entry.value.is_incomplete_array, |
| 6634 |
&ret->type); |
| 6635 |
break; |
| 6636 |
case DW_TAG_subroutine_type: |
| 6637 |
case DW_TAG_subprogram: |
| 6638 |
err = drgn_function_type_from_dwarf(dbinfo, file, die, lang, |
| 6639 |
&ret->type); |
| 6640 |
break; |
| 6641 |
default: |
| 6642 |
err = drgn_error_format(DRGN_ERROR_OTHER, |
| 6643 |
"unknown DWARF type tag 0x%x", |
| 6644 |
dwarf_tag(die)); |
| 6645 |
break; |
| 6646 |
} |
| 6647 |
if (err) |
| 6648 |
return err; |
| 6649 |
if (drgn_type_has_die_addr(ret->type)) |
| 6650 |
drgn_type_init_die_addr(ret->type, (uintptr_t)die->addr); |
| 6651 |
|
| 6652 |
entry.value.type = ret->type; |
| 6653 |
entry.value.qualifiers = ret->qualifiers; |
| 6654 |
struct drgn_dwarf_type_map *map; |
| 6655 |
if (!can_be_incomplete_array && entry.value.is_incomplete_array) |
| 6656 |
map = &dbinfo->dwarf.cant_be_incomplete_array_types; |
| 6657 |
else |
| 6658 |
map = &dbinfo->dwarf.types; |
| 6659 |
if (drgn_dwarf_type_map_insert_searched(map, &entry, hp, NULL) == -1) { |
| 6660 |
/* |
| 6661 |
* This will "leak" the type we created, but it'll still be |
| 6662 |
* cleaned up when the program is freed. |
| 6663 |
*/ |
| 6664 |
return &drgn_enomem; |
| 6665 |
} |
| 6666 |
if (is_incomplete_array_ret) |
| 6667 |
*is_incomplete_array_ret = entry.value.is_incomplete_array; |
| 6668 |
return NULL; |
| 6669 |
} |
| 6670 |
|
| 6671 |
static struct drgn_error * |
| 6672 |
find_enclosing_namespace(struct drgn_namespace_dwarf_index *global_namespace, |
| 6673 |
const char **name, size_t *name_len, |
| 6674 |
struct drgn_namespace_dwarf_index **namespace_ret) |
| 6675 |
{ |
| 6676 |
struct drgn_error *err; |
| 6677 |
|
| 6678 |
*namespace_ret = global_namespace; |
| 6679 |
if (*name_len >= 2 && memcmp(*name, "::", 2) == 0) { |
| 6680 |
/* Explicit global namespace. */ |
| 6681 |
*name_len -= 2; |
| 6682 |
*name += 2; |
| 6683 |
} |
| 6684 |
|
| 6685 |
const char *template_parameters_start = memchr(*name, '<', *name_len); |
| 6686 |
ptrdiff_t searchable_len = |
| 6687 |
template_parameters_start ? |
| 6688 |
template_parameters_start - *name: |
| 6689 |
*name_len; |
| 6690 |
const char *colons; |
| 6691 |
while ((colons = memmem(*name, searchable_len, "::", 2))) { |
| 6692 |
err = drgn_namespace_find_child(*namespace_ret, *name, |
| 6693 |
colons - *name, namespace_ret); |
| 6694 |
if (err) |
| 6695 |
return err; |
| 6696 |
|
| 6697 |
size_t chars_consumed = colons + 2 - *name; |
| 6698 |
searchable_len -= chars_consumed; |
| 6699 |
*name_len -= chars_consumed; |
| 6700 |
*name = colons + 2; |
| 6701 |
} |
| 6702 |
|
| 6703 |
return NULL; |
| 6704 |
} |
| 6705 |
|
| 6706 |
struct drgn_error *drgn_debug_info_find_type(uint64_t kinds, const char *name, |
| 6707 |
size_t name_len, |
| 6708 |
const char *filename, void *arg, |
| 6709 |
struct drgn_qualified_type *ret) |
| 6710 |
{ |
| 6711 |
struct drgn_error *err; |
| 6712 |
struct drgn_debug_info *dbinfo = arg; |
| 6713 |
|
| 6714 |
enum drgn_dwarf_index_tag tags[6]; |
| 6715 |
size_t num_tags = 0; |
| 6716 |
if (kinds & ((1 << DRGN_TYPE_INT) |
| 6717 |
| (1 << DRGN_TYPE_BOOL) |
| 6718 |
| (1 << DRGN_TYPE_FLOAT))) |
| 6719 |
tags[num_tags++] = DRGN_DWARF_INDEX_base_type; |
| 6720 |
if (kinds & (1 << DRGN_TYPE_STRUCT)) |
| 6721 |
tags[num_tags++] = DRGN_DWARF_INDEX_structure_type; |
| 6722 |
if (kinds & (1 << DRGN_TYPE_UNION)) |
| 6723 |
tags[num_tags++] = DRGN_DWARF_INDEX_union_type; |
| 6724 |
if (kinds & (1 << DRGN_TYPE_CLASS)) |
| 6725 |
tags[num_tags++] = DRGN_DWARF_INDEX_class_type; |
| 6726 |
if (kinds & (1 << DRGN_TYPE_ENUM)) |
| 6727 |
tags[num_tags++] = DRGN_DWARF_INDEX_enumeration_type; |
| 6728 |
if (kinds & (1 << DRGN_TYPE_TYPEDEF)) |
| 6729 |
tags[num_tags++] = DRGN_DWARF_INDEX_typedef; |
| 6730 |
|
| 6731 |
struct drgn_namespace_dwarf_index *namespace; |
| 6732 |
err = find_enclosing_namespace(&dbinfo->dwarf.global, |
| 6733 |
&name, &name_len, &namespace); |
| 6734 |
if (err) |
| 6735 |
return err; |
| 6736 |
struct drgn_dwarf_index_iterator it; |
| 6737 |
err = drgn_dwarf_index_iterator_init(&it, namespace, name, name_len, |
| 6738 |
tags, num_tags); |
| 6739 |
if (err) |
| 6740 |
return err; |
| 6741 |
Dwarf_Die die; |
| 6742 |
struct drgn_elf_file *file; |
| 6743 |
while (drgn_dwarf_index_iterator_next(&it, &die, &file)) { |
| 6744 |
if (die_matches_filename(&die, filename)) { |
| 6745 |
err = drgn_type_from_dwarf(dbinfo, file, &die, ret); |
| 6746 |
if (err) |
| 6747 |
return err; |
| 6748 |
/* |
| 6749 |
* For base_type, we need to check that the type we |
| 6750 |
* found was the right kind. |
| 6751 |
*/ |
| 6752 |
if (kinds & (UINT64_C(1) << drgn_type_kind(ret->type))) |
| 6753 |
return NULL; |
| 6754 |
} |
| 6755 |
} |
| 6756 |
return &drgn_not_found; |
| 6757 |
} |
| 6758 |
|
| 6759 |
struct drgn_error * |
| 6760 |
drgn_debug_info_find_object(const char *name, size_t name_len, |
| 6761 |
const char *filename, |
| 6762 |
enum drgn_find_object_flags flags, void *arg, |
| 6763 |
struct drgn_object *ret) |
| 6764 |
{ |
| 6765 |
struct drgn_error *err; |
| 6766 |
struct drgn_debug_info *dbinfo = arg; |
| 6767 |
|
| 6768 |
struct drgn_namespace_dwarf_index *ns; |
| 6769 |
err = find_enclosing_namespace(&dbinfo->dwarf.global, |
| 6770 |
&name, &name_len, &ns); |
| 6771 |
if (err) |
| 6772 |
return err; |
| 6773 |
|
| 6774 |
enum drgn_dwarf_index_tag tags[3]; |
| 6775 |
size_t num_tags = 0; |
| 6776 |
if (flags & DRGN_FIND_OBJECT_CONSTANT) |
| 6777 |
tags[num_tags++] = DRGN_DWARF_INDEX_enumerator; |
| 6778 |
if (flags & DRGN_FIND_OBJECT_FUNCTION) |
| 6779 |
tags[num_tags++] = DRGN_DWARF_INDEX_subprogram; |
| 6780 |
if (flags & DRGN_FIND_OBJECT_VARIABLE) |
| 6781 |
tags[num_tags++] = DRGN_DWARF_INDEX_variable; |
| 6782 |
|
| 6783 |
struct drgn_dwarf_index_iterator it; |
| 6784 |
err = drgn_dwarf_index_iterator_init(&it, ns, name, name_len, tags, |
| 6785 |
num_tags); |
| 6786 |
if (err) |
| 6787 |
return err; |
| 6788 |
Dwarf_Die die; |
| 6789 |
struct drgn_elf_file *file; |
| 6790 |
while (drgn_dwarf_index_iterator_next(&it, &die, &file)) { |
| 6791 |
if (!die_matches_filename(&die, filename)) |
| 6792 |
continue; |
| 6793 |
if (dwarf_tag(&die) == DW_TAG_enumeration_type) { |
| 6794 |
return drgn_object_from_dwarf_enumerator(dbinfo, file, |
| 6795 |
&die, name, |
| 6796 |
ret); |
| 6797 |
} else { |
| 6798 |
return drgn_object_from_dwarf(dbinfo, file, &die, NULL, |
| 6799 |
NULL, NULL, ret); |
| 6800 |
} |
| 6801 |
} |
| 6802 |
return &drgn_not_found; |
| 6803 |
} |
| 6804 |
|
| 6805 |
/* |
| 6806 |
* Call frame information. |
| 6807 |
*/ |
| 6808 |
|
| 6809 |
struct drgn_dwarf_cie { |
| 6810 |
/* Size of an address in this CIE in bytes. */ |
| 6811 |
uint8_t address_size; |
| 6812 |
/* DW_EH_PE_* encoding of addresses in this CIE. */ |
| 6813 |
uint8_t address_encoding; |
| 6814 |
/* Whether this CIE has a 'z' augmentation. */ |
| 6815 |
bool have_augmentation_length; |
| 6816 |
/* Whether this CIE is for a signal handler ('S' augmentation). */ |
| 6817 |
bool signal_frame; |
| 6818 |
drgn_register_number return_address_register; |
| 6819 |
uint64_t code_alignment_factor; |
| 6820 |
int64_t data_alignment_factor; |
| 6821 |
const char *initial_instructions; |
| 6822 |
size_t initial_instructions_size; |
| 6823 |
}; |
| 6824 |
|
| 6825 |
DEFINE_VECTOR(drgn_dwarf_fde_vector, struct drgn_dwarf_fde); |
| 6826 |
DEFINE_VECTOR(drgn_dwarf_cie_vector, struct drgn_dwarf_cie); |
| 6827 |
DEFINE_HASH_MAP(drgn_dwarf_cie_map, size_t, size_t, int_key_hash_pair, |
| 6828 |
scalar_key_eq); |
| 6829 |
|
| 6830 |
static struct drgn_error * |
| 6831 |
drgn_dwarf_cfi_next_encoded(struct drgn_elf_file_section_buffer *buffer, |
| 6832 |
uint8_t address_size, uint8_t encoding, |
| 6833 |
uint64_t func_addr, uint64_t *ret) |
| 6834 |
{ |
| 6835 |
struct drgn_error *err; |
| 6836 |
|
| 6837 |
/* Not currently used for CFI. */ |
| 6838 |
if (encoding & DW_EH_PE_indirect) { |
| 6839 |
unknown_fde_encoding: |
| 6840 |
return binary_buffer_error(&buffer->bb, |
| 6841 |
"unknown EH encoding %#" PRIx8, |
| 6842 |
encoding); |
| 6843 |
} |
| 6844 |
|
| 6845 |
size_t pos = buffer->bb.pos - (char *)buffer->data->d_buf; |
| 6846 |
uint64_t base; |
| 6847 |
switch (encoding & 0x70) { |
| 6848 |
case DW_EH_PE_absptr: |
| 6849 |
base = 0; |
| 6850 |
break; |
| 6851 |
case DW_EH_PE_pcrel: |
| 6852 |
base = buffer->file->module->dwarf.pcrel_base + pos; |
| 6853 |
break; |
| 6854 |
case DW_EH_PE_textrel: |
| 6855 |
base = buffer->file->module->dwarf.textrel_base; |
| 6856 |
break; |
| 6857 |
case DW_EH_PE_datarel: |
| 6858 |
base = buffer->file->module->dwarf.datarel_base; |
| 6859 |
break; |
| 6860 |
case DW_EH_PE_funcrel: |
| 6861 |
/* Relative to the FDE's initial location. */ |
| 6862 |
base = func_addr; |
| 6863 |
break; |
| 6864 |
case DW_EH_PE_aligned: |
| 6865 |
base = 0; |
| 6866 |
if (pos % address_size != 0 && |
| 6867 |
(err = binary_buffer_skip(&buffer->bb, |
| 6868 |
address_size - pos % address_size))) |
| 6869 |
return err; |
| 6870 |
break; |
| 6871 |
default: |
| 6872 |
goto unknown_fde_encoding; |
| 6873 |
} |
| 6874 |
|
| 6875 |
uint64_t offset; |
| 6876 |
switch (encoding & 0xf) { |
| 6877 |
case DW_EH_PE_absptr: |
| 6878 |
if ((err = binary_buffer_next_uint(&buffer->bb, address_size, |
| 6879 |
&offset))) |
| 6880 |
return err; |
| 6881 |
break; |
| 6882 |
case DW_EH_PE_uleb128: |
| 6883 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &offset))) |
| 6884 |
return err; |
| 6885 |
break; |
| 6886 |
case DW_EH_PE_udata2: |
| 6887 |
if ((err = binary_buffer_next_u16_into_u64(&buffer->bb, |
| 6888 |
&offset))) |
| 6889 |
return err; |
| 6890 |
break; |
| 6891 |
case DW_EH_PE_udata4: |
| 6892 |
if ((err = binary_buffer_next_u32_into_u64(&buffer->bb, |
| 6893 |
&offset))) |
| 6894 |
return err; |
| 6895 |
break; |
| 6896 |
case DW_EH_PE_udata8: |
| 6897 |
if ((err = binary_buffer_next_u64(&buffer->bb, &offset))) |
| 6898 |
return err; |
| 6899 |
break; |
| 6900 |
case DW_EH_PE_sleb128: |
| 6901 |
if ((err = binary_buffer_next_sleb128_into_u64(&buffer->bb, |
| 6902 |
&offset))) |
| 6903 |
return err; |
| 6904 |
break; |
| 6905 |
case DW_EH_PE_sdata2: |
| 6906 |
if ((err = binary_buffer_next_s16_into_u64(&buffer->bb, |
| 6907 |
&offset))) |
| 6908 |
return err; |
| 6909 |
break; |
| 6910 |
case DW_EH_PE_sdata4: |
| 6911 |
if ((err = binary_buffer_next_s32_into_u64(&buffer->bb, |
| 6912 |
&offset))) |
| 6913 |
return err; |
| 6914 |
break; |
| 6915 |
case DW_EH_PE_sdata8: |
| 6916 |
if ((err = binary_buffer_next_s64_into_u64(&buffer->bb, |
| 6917 |
&offset))) |
| 6918 |
return err; |
| 6919 |
break; |
| 6920 |
default: |
| 6921 |
goto unknown_fde_encoding; |
| 6922 |
} |
| 6923 |
*ret = (base + offset) & uint_max(address_size); |
| 6924 |
|
| 6925 |
return NULL; |
| 6926 |
} |
| 6927 |
|
| 6928 |
static struct drgn_error *drgn_parse_dwarf_cie(struct drgn_elf_file *file, |
| 6929 |
enum drgn_section_index scn, |
| 6930 |
size_t cie_pointer, |
| 6931 |
struct drgn_dwarf_cie *cie) |
| 6932 |
{ |
| 6933 |
bool is_eh = scn == DRGN_SCN_EH_FRAME; |
| 6934 |
struct drgn_error *err; |
| 6935 |
|
| 6936 |
struct drgn_elf_file_section_buffer buffer; |
| 6937 |
drgn_elf_file_section_buffer_init_index(&buffer, file, scn); |
| 6938 |
buffer.bb.pos += cie_pointer; |
| 6939 |
|
| 6940 |
uint32_t tmp; |
| 6941 |
if ((err = binary_buffer_next_u32(&buffer.bb, &tmp))) |
| 6942 |
return err; |
| 6943 |
bool is_64_bit = tmp == UINT32_C(0xffffffff); |
| 6944 |
uint64_t length; |
| 6945 |
if (is_64_bit) { |
| 6946 |
if ((err = binary_buffer_next_u64(&buffer.bb, &length))) |
| 6947 |
return err; |
| 6948 |
} else { |
| 6949 |
length = tmp; |
| 6950 |
} |
| 6951 |
if (length > buffer.bb.end - buffer.bb.pos) { |
| 6952 |
return binary_buffer_error(&buffer.bb, |
| 6953 |
"entry length is out of bounds"); |
| 6954 |
} |
| 6955 |
buffer.bb.end = buffer.bb.pos + length; |
| 6956 |
|
| 6957 |
uint64_t cie_id, expected_cie_id; |
| 6958 |
if (is_64_bit) { |
| 6959 |
if ((err = binary_buffer_next_u64(&buffer.bb, &cie_id))) |
| 6960 |
return err; |
| 6961 |
expected_cie_id = is_eh ? 0 : UINT64_C(0xffffffffffffffff); |
| 6962 |
} else { |
| 6963 |
if ((err = binary_buffer_next_u32_into_u64(&buffer.bb, |
| 6964 |
&cie_id))) |
| 6965 |
return err; |
| 6966 |
expected_cie_id = is_eh ? 0 : UINT64_C(0xffffffff); |
| 6967 |
} |
| 6968 |
if (cie_id != expected_cie_id) |
| 6969 |
return binary_buffer_error(&buffer.bb, "invalid CIE ID"); |
| 6970 |
|
| 6971 |
uint8_t version; |
| 6972 |
if ((err = binary_buffer_next_u8(&buffer.bb, &version))) |
| 6973 |
return err; |
| 6974 |
if (version < 1 || version == 2 || version > 4) { |
| 6975 |
return binary_buffer_error(&buffer.bb, |
| 6976 |
"unknown CIE version %" PRIu8, |
| 6977 |
version); |
| 6978 |
} |
| 6979 |
|
| 6980 |
const char *augmentation; |
| 6981 |
size_t augmentation_len; |
| 6982 |
if ((err = binary_buffer_next_string(&buffer.bb, &augmentation, |
| 6983 |
&augmentation_len))) |
| 6984 |
return err; |
| 6985 |
cie->have_augmentation_length = augmentation[0] == 'z'; |
| 6986 |
cie->signal_frame = false; |
| 6987 |
for (size_t i = 0; i < augmentation_len; i++) { |
| 6988 |
switch (augmentation[i]) { |
| 6989 |
case 'z': |
| 6990 |
if (i != 0) |
| 6991 |
goto unknown_augmentation; |
| 6992 |
break; |
| 6993 |
case 'L': |
| 6994 |
case 'P': |
| 6995 |
case 'R': |
| 6996 |
if (augmentation[0] != 'z' || !is_eh) |
| 6997 |
goto unknown_augmentation; |
| 6998 |
break; |
| 6999 |
case 'S': |
| 7000 |
cie->signal_frame = true; |
| 7001 |
break; |
| 7002 |
default: |
| 7003 |
unknown_augmentation: |
| 7004 |
/* |
| 7005 |
* We could ignore this CIE and all FDEs that reference |
| 7006 |
* it or skip the augmentation if we have its length, |
| 7007 |
* but let's fail loudly so that we find out about |
| 7008 |
* missing support. |
| 7009 |
*/ |
| 7010 |
return binary_buffer_error_at(&buffer.bb, |
| 7011 |
&augmentation[i], |
| 7012 |
"unknown CFI augmentation %s", |
| 7013 |
augmentation); |
| 7014 |
} |
| 7015 |
} |
| 7016 |
|
| 7017 |
if (version >= 4) { |
| 7018 |
if ((err = binary_buffer_next_u8(&buffer.bb, |
| 7019 |
&cie->address_size))) |
| 7020 |
return err; |
| 7021 |
if (cie->address_size < 1 || cie->address_size > 8) { |
| 7022 |
return binary_buffer_error(&buffer.bb, |
| 7023 |
"unsupported address size %" PRIu8, |
| 7024 |
cie->address_size); |
| 7025 |
} |
| 7026 |
uint8_t segment_selector_size; |
| 7027 |
if ((err = binary_buffer_next_u8(&buffer.bb, |
| 7028 |
&segment_selector_size))) |
| 7029 |
return err; |
| 7030 |
if (segment_selector_size) { |
| 7031 |
return binary_buffer_error(&buffer.bb, |
| 7032 |
"unsupported segment selector size %" PRIu8, |
| 7033 |
segment_selector_size); |
| 7034 |
} |
| 7035 |
} else { |
| 7036 |
cie->address_size = drgn_elf_file_address_size(file); |
| 7037 |
} |
| 7038 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7039 |
&cie->code_alignment_factor)) || |
| 7040 |
(err = binary_buffer_next_sleb128(&buffer.bb, |
| 7041 |
&cie->data_alignment_factor))) |
| 7042 |
return err; |
| 7043 |
uint64_t return_address_register; |
| 7044 |
if (version >= 3) { |
| 7045 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7046 |
&return_address_register))) |
| 7047 |
return err; |
| 7048 |
} else { |
| 7049 |
if ((err = binary_buffer_next_u8_into_u64(&buffer.bb, |
| 7050 |
&return_address_register))) |
| 7051 |
return err; |
| 7052 |
} |
| 7053 |
cie->return_address_register = |
| 7054 |
file->platform.arch->dwarf_regno_to_internal(return_address_register); |
| 7055 |
if (cie->return_address_register == DRGN_REGISTER_NUMBER_UNKNOWN) { |
| 7056 |
return binary_buffer_error(&buffer.bb, |
| 7057 |
"unknown return address register"); |
| 7058 |
} |
| 7059 |
cie->address_encoding = DW_EH_PE_absptr; |
| 7060 |
if (augmentation[0] == 'z') { |
| 7061 |
for (size_t i = 0; i < augmentation_len; i++) { |
| 7062 |
switch (augmentation[i]) { |
| 7063 |
case 'z': |
| 7064 |
if ((err = binary_buffer_skip_leb128(&buffer.bb))) |
| 7065 |
return err; |
| 7066 |
break; |
| 7067 |
case 'L': |
| 7068 |
if ((err = binary_buffer_skip(&buffer.bb, 1))) |
| 7069 |
return err; |
| 7070 |
break; |
| 7071 |
case 'P': { |
| 7072 |
uint8_t encoding; |
| 7073 |
if ((err = binary_buffer_next_u8(&buffer.bb, &encoding))) |
| 7074 |
return err; |
| 7075 |
/* |
| 7076 |
* We don't need the result, so don't bother |
| 7077 |
* dereferencing. |
| 7078 |
*/ |
| 7079 |
encoding &= ~DW_EH_PE_indirect; |
| 7080 |
uint64_t unused; |
| 7081 |
if ((err = drgn_dwarf_cfi_next_encoded(&buffer, |
| 7082 |
cie->address_size, |
| 7083 |
encoding, |
| 7084 |
0, |
| 7085 |
&unused))) |
| 7086 |
return err; |
| 7087 |
break; |
| 7088 |
} |
| 7089 |
case 'R': |
| 7090 |
if ((err = binary_buffer_next_u8(&buffer.bb, |
| 7091 |
&cie->address_encoding))) |
| 7092 |
return err; |
| 7093 |
break; |
| 7094 |
} |
| 7095 |
} |
| 7096 |
} |
| 7097 |
cie->initial_instructions = buffer.bb.pos; |
| 7098 |
cie->initial_instructions_size = buffer.bb.end - buffer.bb.pos; |
| 7099 |
return NULL; |
| 7100 |
} |
| 7101 |
|
| 7102 |
static void drgn_debug_info_cache_sh_addr(struct drgn_elf_file *file, |
| 7103 |
enum drgn_section_index scn, |
| 7104 |
uint64_t *addr) |
| 7105 |
{ |
| 7106 |
if (file->scns[scn]) { |
| 7107 |
GElf_Shdr shdr_mem; |
| 7108 |
GElf_Shdr *shdr = gelf_getshdr(file->scns[scn], &shdr_mem); |
| 7109 |
if (shdr) |
| 7110 |
*addr = shdr->sh_addr; |
| 7111 |
} |
| 7112 |
} |
| 7113 |
|
| 7114 |
static int drgn_dwarf_fde_compar(const void *_a, const void *_b) |
| 7115 |
{ |
| 7116 |
const struct drgn_dwarf_fde *a = _a; |
| 7117 |
const struct drgn_dwarf_fde *b = _b; |
| 7118 |
if (a->initial_location < b->initial_location) |
| 7119 |
return -1; |
| 7120 |
else if (a->initial_location > b->initial_location) |
| 7121 |
return 1; |
| 7122 |
else |
| 7123 |
return 0; |
| 7124 |
} |
| 7125 |
|
| 7126 |
static struct drgn_error *drgn_parse_dwarf_cfi(struct drgn_dwarf_cfi *cfi, |
| 7127 |
struct drgn_elf_file *file, |
| 7128 |
enum drgn_section_index scn) |
| 7129 |
{ |
| 7130 |
const bool is_eh = scn == DRGN_SCN_EH_FRAME; |
| 7131 |
struct drgn_error *err; |
| 7132 |
|
| 7133 |
if (!file->scns[scn]) |
| 7134 |
return NULL; |
| 7135 |
|
| 7136 |
if (is_eh) { |
| 7137 |
drgn_debug_info_cache_sh_addr(file, DRGN_SCN_EH_FRAME, |
| 7138 |
&file->module->dwarf.pcrel_base); |
| 7139 |
drgn_debug_info_cache_sh_addr(file, DRGN_SCN_TEXT, |
| 7140 |
&file->module->dwarf.textrel_base); |
| 7141 |
drgn_debug_info_cache_sh_addr(file, DRGN_SCN_GOT, |
| 7142 |
&file->module->dwarf.datarel_base); |
| 7143 |
} |
| 7144 |
|
| 7145 |
VECTOR(drgn_dwarf_cie_vector, cies); |
| 7146 |
VECTOR(drgn_dwarf_fde_vector, fdes); |
| 7147 |
HASH_TABLE(drgn_dwarf_cie_map, cie_map); |
| 7148 |
|
| 7149 |
struct drgn_elf_file_section_buffer buffer; |
| 7150 |
err = drgn_elf_file_section_buffer_read(&buffer, file, scn); |
| 7151 |
if (err) |
| 7152 |
return err; |
| 7153 |
while (binary_buffer_has_next(&buffer.bb)) { |
| 7154 |
uint32_t tmp; |
| 7155 |
if ((err = binary_buffer_next_u32(&buffer.bb, &tmp))) |
| 7156 |
return err; |
| 7157 |
bool is_64_bit = tmp == UINT32_C(0xffffffff); |
| 7158 |
uint64_t length; |
| 7159 |
if (is_64_bit) { |
| 7160 |
if ((err = binary_buffer_next_u64(&buffer.bb, &length))) |
| 7161 |
return err; |
| 7162 |
} else { |
| 7163 |
length = tmp; |
| 7164 |
} |
| 7165 |
/* |
| 7166 |
* Technically, a length of zero is only a terminator in |
| 7167 |
* .eh_frame, but other consumers (binutils, elfutils, GDB) |
| 7168 |
* handle it the same way in .debug_frame. |
| 7169 |
*/ |
| 7170 |
if (length == 0) |
| 7171 |
break; |
| 7172 |
if (length > buffer.bb.end - buffer.bb.pos) { |
| 7173 |
return binary_buffer_error(&buffer.bb, |
| 7174 |
"entry length is out of bounds"); |
| 7175 |
} |
| 7176 |
buffer.bb.end = buffer.bb.pos + length; |
| 7177 |
|
| 7178 |
/* |
| 7179 |
* The Linux Standard Base Core Specification [1] states that |
| 7180 |
* the CIE ID in .eh_frame is always 4 bytes. However, other |
| 7181 |
* consumers handle it the same as in .debug_frame (8 bytes for |
| 7182 |
* the 64-bit format). |
| 7183 |
* |
| 7184 |
* 1: https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html |
| 7185 |
*/ |
| 7186 |
uint64_t cie_pointer, cie_id; |
| 7187 |
if (is_64_bit) { |
| 7188 |
if ((err = binary_buffer_next_u64(&buffer.bb, |
| 7189 |
&cie_pointer))) |
| 7190 |
return err; |
| 7191 |
cie_id = is_eh ? 0 : UINT64_C(0xffffffffffffffff); |
| 7192 |
} else { |
| 7193 |
if ((err = binary_buffer_next_u32_into_u64(&buffer.bb, |
| 7194 |
&cie_pointer))) |
| 7195 |
return err; |
| 7196 |
cie_id = is_eh ? 0 : UINT64_C(0xffffffff); |
| 7197 |
} |
| 7198 |
|
| 7199 |
if (cie_pointer != cie_id) { |
| 7200 |
if (is_eh) { |
| 7201 |
size_t pointer_offset = |
| 7202 |
(buffer.bb.pos |
| 7203 |
- (is_64_bit ? 8 : 4) |
| 7204 |
- (char *)buffer.data->d_buf); |
| 7205 |
if (cie_pointer > pointer_offset) { |
| 7206 |
return binary_buffer_error(&buffer.bb, |
| 7207 |
"CIE pointer is out of bounds"); |
| 7208 |
} |
| 7209 |
cie_pointer = pointer_offset - cie_pointer; |
| 7210 |
} else if (cie_pointer > buffer.data->d_size) { |
| 7211 |
return binary_buffer_error(&buffer.bb, |
| 7212 |
"CIE pointer is out of bounds"); |
| 7213 |
} |
| 7214 |
struct drgn_dwarf_fde *fde = |
| 7215 |
drgn_dwarf_fde_vector_append_entry(&fdes); |
| 7216 |
if (!fde) |
| 7217 |
return &drgn_enomem; |
| 7218 |
struct drgn_dwarf_cie_map_entry entry = { |
| 7219 |
.key = cie_pointer, |
| 7220 |
.value = drgn_dwarf_cie_vector_size(&cies), |
| 7221 |
}; |
| 7222 |
struct drgn_dwarf_cie_map_iterator it; |
| 7223 |
int r = drgn_dwarf_cie_map_insert(&cie_map, &entry, |
| 7224 |
&it); |
| 7225 |
struct drgn_dwarf_cie *cie; |
| 7226 |
if (r > 0) { |
| 7227 |
cie = drgn_dwarf_cie_vector_append_entry(&cies); |
| 7228 |
if (!cie) |
| 7229 |
return &drgn_enomem; |
| 7230 |
err = drgn_parse_dwarf_cie(file, scn, |
| 7231 |
cie_pointer, cie); |
| 7232 |
if (err) |
| 7233 |
return err; |
| 7234 |
} else if (r == 0) { |
| 7235 |
cie = drgn_dwarf_cie_vector_at(&cies, |
| 7236 |
it.entry->value); |
| 7237 |
} else { |
| 7238 |
return &drgn_enomem; |
| 7239 |
} |
| 7240 |
if ((err = drgn_dwarf_cfi_next_encoded(&buffer, |
| 7241 |
cie->address_size, |
| 7242 |
cie->address_encoding, |
| 7243 |
0, |
| 7244 |
&fde->initial_location)) || |
| 7245 |
(err = drgn_dwarf_cfi_next_encoded(&buffer, |
| 7246 |
cie->address_size, |
| 7247 |
cie->address_encoding & 0xf, |
| 7248 |
0, |
| 7249 |
&fde->address_range))) |
| 7250 |
return err; |
| 7251 |
if (cie->have_augmentation_length) { |
| 7252 |
uint64_t augmentation_length; |
| 7253 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7254 |
&augmentation_length))) |
| 7255 |
return err; |
| 7256 |
if (augmentation_length > |
| 7257 |
buffer.bb.end - buffer.bb.pos) { |
| 7258 |
return binary_buffer_error(&buffer.bb, |
| 7259 |
"augmentation length is out of bounds"); |
| 7260 |
} |
| 7261 |
buffer.bb.pos += augmentation_length; |
| 7262 |
} |
| 7263 |
fde->cie = it.entry->value; |
| 7264 |
fde->instructions = buffer.bb.pos; |
| 7265 |
fde->instructions_size = buffer.bb.end - buffer.bb.pos; |
| 7266 |
} |
| 7267 |
|
| 7268 |
buffer.bb.pos = buffer.bb.end; |
| 7269 |
buffer.bb.end = (const char *)buffer.data->d_buf |
| 7270 |
+ buffer.data->d_size; |
| 7271 |
} |
| 7272 |
|
| 7273 |
drgn_dwarf_cie_vector_shrink_to_fit(&cies); |
| 7274 |
drgn_dwarf_fde_vector_shrink_to_fit(&fdes); |
| 7275 |
qsort(drgn_dwarf_fde_vector_begin(&fdes), |
| 7276 |
drgn_dwarf_fde_vector_size(&fdes), sizeof(struct drgn_dwarf_fde), |
| 7277 |
drgn_dwarf_fde_compar); |
| 7278 |
drgn_dwarf_cie_vector_steal(&cies, &cfi->cies, NULL); |
| 7279 |
drgn_dwarf_fde_vector_steal(&fdes, &cfi->fdes, &cfi->num_fdes); |
| 7280 |
return NULL; |
| 7281 |
} |
| 7282 |
|
| 7283 |
static struct drgn_dwarf_fde *drgn_find_dwarf_fde(struct drgn_dwarf_cfi *cfi, |
| 7284 |
uint64_t unbiased_pc) |
| 7285 |
{ |
| 7286 |
#define less_than_initial_location(a, b) (*(a) < (b)->initial_location) |
| 7287 |
size_t i = binary_search_gt(cfi->fdes, cfi->num_fdes, &unbiased_pc, |
| 7288 |
less_than_initial_location); |
| 7289 |
#undef less_than_initial_location |
| 7290 |
if (i == 0 |
| 7291 |
|| (unbiased_pc - cfi->fdes[i - 1].initial_location |
| 7292 |
>= cfi->fdes[i - 1].address_range)) |
| 7293 |
return NULL; |
| 7294 |
return &cfi->fdes[i - 1]; |
| 7295 |
|
| 7296 |
} |
| 7297 |
|
| 7298 |
static struct drgn_error * |
| 7299 |
drgn_dwarf_cfi_next_offset(struct drgn_elf_file_section_buffer *buffer, |
| 7300 |
int64_t *ret) |
| 7301 |
{ |
| 7302 |
struct drgn_error *err; |
| 7303 |
uint64_t offset; |
| 7304 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &offset))) |
| 7305 |
return err; |
| 7306 |
if (offset > INT64_MAX) |
| 7307 |
return binary_buffer_error(&buffer->bb, "offset is too large"); |
| 7308 |
*ret = offset; |
| 7309 |
return NULL; |
| 7310 |
} |
| 7311 |
|
| 7312 |
static struct drgn_error * |
| 7313 |
drgn_dwarf_cfi_next_offset_sf(struct drgn_elf_file_section_buffer *buffer, |
| 7314 |
struct drgn_dwarf_cie *cie, int64_t *ret) |
| 7315 |
{ |
| 7316 |
struct drgn_error *err; |
| 7317 |
int64_t factored; |
| 7318 |
if ((err = binary_buffer_next_sleb128(&buffer->bb, &factored))) |
| 7319 |
return err; |
| 7320 |
if (__builtin_mul_overflow(factored, cie->data_alignment_factor, ret)) |
| 7321 |
return binary_buffer_error(&buffer->bb, "offset is too large"); |
| 7322 |
return NULL; |
| 7323 |
} |
| 7324 |
|
| 7325 |
static struct drgn_error * |
| 7326 |
drgn_dwarf_cfi_next_offset_f(struct drgn_elf_file_section_buffer *buffer, |
| 7327 |
struct drgn_dwarf_cie *cie, int64_t *ret) |
| 7328 |
{ |
| 7329 |
struct drgn_error *err; |
| 7330 |
uint64_t factored; |
| 7331 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &factored))) |
| 7332 |
return err; |
| 7333 |
if (__builtin_mul_overflow(factored, cie->data_alignment_factor, ret)) |
| 7334 |
return binary_buffer_error(&buffer->bb, "offset is too large"); |
| 7335 |
return NULL; |
| 7336 |
} |
| 7337 |
|
| 7338 |
static struct drgn_error * |
| 7339 |
drgn_dwarf_cfi_next_block(struct drgn_elf_file_section_buffer *buffer, |
| 7340 |
const char **buf_ret, size_t *size_ret) |
| 7341 |
{ |
| 7342 |
struct drgn_error *err; |
| 7343 |
uint64_t size; |
| 7344 |
if ((err = binary_buffer_next_uleb128(&buffer->bb, &size))) |
| 7345 |
return err; |
| 7346 |
if (size > buffer->bb.end - buffer->bb.pos) { |
| 7347 |
return binary_buffer_error(&buffer->bb, |
| 7348 |
"block is out of bounds"); |
| 7349 |
} |
| 7350 |
*buf_ret = buffer->bb.pos; |
| 7351 |
buffer->bb.pos += size; |
| 7352 |
*size_ret = size; |
| 7353 |
return NULL; |
| 7354 |
} |
| 7355 |
|
| 7356 |
DEFINE_VECTOR(drgn_cfi_row_vector, struct drgn_cfi_row *); |
| 7357 |
|
| 7358 |
static struct drgn_error * |
| 7359 |
drgn_eval_dwarf_cfi(struct drgn_elf_file *file, enum drgn_section_index scn, |
| 7360 |
struct drgn_dwarf_cie *cie, struct drgn_dwarf_fde *fde, |
| 7361 |
const struct drgn_cfi_row *initial_row, uint64_t target, |
| 7362 |
const char *instructions, size_t instructions_size, |
| 7363 |
struct drgn_cfi_row **row) |
| 7364 |
{ |
| 7365 |
struct drgn_error *err; |
| 7366 |
drgn_register_number (*dwarf_regno_to_internal)(uint64_t) = |
| 7367 |
file->platform.arch->dwarf_regno_to_internal; |
| 7368 |
uint64_t pc = fde->initial_location; |
| 7369 |
|
| 7370 |
struct drgn_cfi_row_vector state_stack = VECTOR_INIT; |
| 7371 |
struct drgn_elf_file_section_buffer buffer; |
| 7372 |
drgn_elf_file_section_buffer_init_index(&buffer, file, scn); |
| 7373 |
buffer.bb.pos = instructions; |
| 7374 |
buffer.bb.end = instructions + instructions_size; |
| 7375 |
while (binary_buffer_has_next(&buffer.bb)) { |
| 7376 |
uint8_t opcode; |
| 7377 |
if ((err = binary_buffer_next_u8(&buffer.bb, &opcode))) |
| 7378 |
goto out; |
| 7379 |
|
| 7380 |
uint64_t dwarf_regno; |
| 7381 |
drgn_register_number regno; |
| 7382 |
struct drgn_cfi_rule rule; |
| 7383 |
uint64_t tmp; |
| 7384 |
switch ((opcode & 0xc0) ? (opcode & 0xc0) : opcode) { |
| 7385 |
case DW_CFA_set_loc: |
| 7386 |
if (!initial_row) |
| 7387 |
goto invalid_for_initial; |
| 7388 |
if ((err = drgn_dwarf_cfi_next_encoded(&buffer, |
| 7389 |
cie->address_size, |
| 7390 |
cie->address_encoding, |
| 7391 |
fde->initial_location, |
| 7392 |
&tmp))) |
| 7393 |
goto out; |
| 7394 |
if (tmp <= pc) { |
| 7395 |
err = binary_buffer_error(&buffer.bb, |
| 7396 |
"DW_CFA_set_loc location is not greater than current location"); |
| 7397 |
goto out; |
| 7398 |
} |
| 7399 |
pc = tmp; |
| 7400 |
if (pc > target) |
| 7401 |
goto found; |
| 7402 |
break; |
| 7403 |
case DW_CFA_advance_loc: |
| 7404 |
if (!initial_row) |
| 7405 |
goto invalid_for_initial; |
| 7406 |
tmp = opcode & 0x3f; |
| 7407 |
goto advance_loc; |
| 7408 |
case DW_CFA_advance_loc1: |
| 7409 |
if (!initial_row) |
| 7410 |
goto invalid_for_initial; |
| 7411 |
if ((err = binary_buffer_next_u8_into_u64(&buffer.bb, |
| 7412 |
&tmp))) |
| 7413 |
goto out; |
| 7414 |
goto advance_loc; |
| 7415 |
case DW_CFA_advance_loc2: |
| 7416 |
if (!initial_row) |
| 7417 |
goto invalid_for_initial; |
| 7418 |
if ((err = binary_buffer_next_u16_into_u64(&buffer.bb, |
| 7419 |
&tmp))) |
| 7420 |
goto out; |
| 7421 |
goto advance_loc; |
| 7422 |
case DW_CFA_advance_loc4: |
| 7423 |
if (!initial_row) |
| 7424 |
goto invalid_for_initial; |
| 7425 |
if ((err = binary_buffer_next_u32_into_u64(&buffer.bb, |
| 7426 |
&tmp))) |
| 7427 |
goto out; |
| 7428 |
advance_loc: |
| 7429 |
if (__builtin_mul_overflow(tmp, |
| 7430 |
cie->code_alignment_factor, |
| 7431 |
&tmp) || |
| 7432 |
__builtin_add_overflow(pc, tmp, &pc) || |
| 7433 |
pc > uint_max(cie->address_size)) { |
| 7434 |
err = drgn_error_create(DRGN_ERROR_OTHER, |
| 7435 |
"DW_CFA_advance_loc* overflows location"); |
| 7436 |
goto out; |
| 7437 |
} |
| 7438 |
if (pc > target) |
| 7439 |
goto found; |
| 7440 |
break; |
| 7441 |
case DW_CFA_def_cfa: |
| 7442 |
rule.kind = DRGN_CFI_RULE_REGISTER_PLUS_OFFSET; |
| 7443 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7444 |
&dwarf_regno)) || |
| 7445 |
(err = drgn_dwarf_cfi_next_offset(&buffer, &rule.offset))) |
| 7446 |
goto out; |
| 7447 |
if ((rule.regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7448 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7449 |
rule.kind = DRGN_CFI_RULE_UNDEFINED; |
| 7450 |
goto set_cfa; |
| 7451 |
case DW_CFA_def_cfa_sf: |
| 7452 |
rule.kind = DRGN_CFI_RULE_REGISTER_PLUS_OFFSET; |
| 7453 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7454 |
&dwarf_regno)) || |
| 7455 |
(err = drgn_dwarf_cfi_next_offset_sf(&buffer, cie, |
| 7456 |
&rule.offset))) |
| 7457 |
goto out; |
| 7458 |
if ((rule.regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7459 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7460 |
rule.kind = DRGN_CFI_RULE_UNDEFINED; |
| 7461 |
goto set_cfa; |
| 7462 |
case DW_CFA_def_cfa_register: |
| 7463 |
drgn_cfi_row_get_cfa(*row, &rule); |
| 7464 |
if (rule.kind != DRGN_CFI_RULE_REGISTER_PLUS_OFFSET) { |
| 7465 |
err = binary_buffer_error(&buffer.bb, |
| 7466 |
"DW_CFA_def_cfa_register with incompatible CFA rule"); |
| 7467 |
goto out; |
| 7468 |
} |
| 7469 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7470 |
&dwarf_regno))) |
| 7471 |
goto out; |
| 7472 |
if ((rule.regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7473 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7474 |
rule.kind = DRGN_CFI_RULE_UNDEFINED; |
| 7475 |
goto set_cfa; |
| 7476 |
case DW_CFA_def_cfa_offset: |
| 7477 |
drgn_cfi_row_get_cfa(*row, &rule); |
| 7478 |
if (rule.kind != DRGN_CFI_RULE_REGISTER_PLUS_OFFSET) { |
| 7479 |
err = binary_buffer_error(&buffer.bb, |
| 7480 |
"DW_CFA_def_cfa_offset with incompatible CFA rule"); |
| 7481 |
goto out; |
| 7482 |
} |
| 7483 |
if ((err = drgn_dwarf_cfi_next_offset(&buffer, |
| 7484 |
&rule.offset))) |
| 7485 |
goto out; |
| 7486 |
goto set_cfa; |
| 7487 |
case DW_CFA_def_cfa_offset_sf: |
| 7488 |
drgn_cfi_row_get_cfa(*row, &rule); |
| 7489 |
if (rule.kind != DRGN_CFI_RULE_REGISTER_PLUS_OFFSET) { |
| 7490 |
err = binary_buffer_error(&buffer.bb, |
| 7491 |
"DW_CFA_def_cfa_offset_sf with incompatible CFA rule"); |
| 7492 |
goto out; |
| 7493 |
} |
| 7494 |
if ((err = drgn_dwarf_cfi_next_offset_sf(&buffer, cie, |
| 7495 |
&rule.offset))) |
| 7496 |
goto out; |
| 7497 |
goto set_cfa; |
| 7498 |
case DW_CFA_def_cfa_expression: |
| 7499 |
rule.kind = DRGN_CFI_RULE_DWARF_EXPRESSION; |
| 7500 |
rule.push_cfa = false; |
| 7501 |
if ((err = drgn_dwarf_cfi_next_block(&buffer, |
| 7502 |
&rule.expr, |
| 7503 |
&rule.expr_size))) |
| 7504 |
goto out; |
| 7505 |
set_cfa: |
| 7506 |
if (!drgn_cfi_row_set_cfa(row, &rule)) { |
| 7507 |
err = &drgn_enomem; |
| 7508 |
goto out; |
| 7509 |
} |
| 7510 |
break; |
| 7511 |
case DW_CFA_undefined: |
| 7512 |
rule.kind = DRGN_CFI_RULE_UNDEFINED; |
| 7513 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7514 |
&dwarf_regno))) |
| 7515 |
goto out; |
| 7516 |
if ((regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7517 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7518 |
break; |
| 7519 |
goto set_reg; |
| 7520 |
case DW_CFA_same_value: |
| 7521 |
rule.kind = DRGN_CFI_RULE_REGISTER_PLUS_OFFSET; |
| 7522 |
rule.offset = 0; |
| 7523 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7524 |
&dwarf_regno))) |
| 7525 |
goto out; |
| 7526 |
if ((regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7527 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7528 |
break; |
| 7529 |
rule.regno = regno; |
| 7530 |
goto set_reg; |
| 7531 |
case DW_CFA_offset: |
| 7532 |
rule.kind = DRGN_CFI_RULE_AT_CFA_PLUS_OFFSET; |
| 7533 |
if ((err = drgn_dwarf_cfi_next_offset_f(&buffer, cie, |
| 7534 |
&rule.offset))) |
| 7535 |
goto out; |
| 7536 |
if ((regno = dwarf_regno_to_internal(opcode & 0x3f)) == |
| 7537 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7538 |
break; |
| 7539 |
goto set_reg; |
| 7540 |
case DW_CFA_offset_extended: |
| 7541 |
rule.kind = DRGN_CFI_RULE_AT_CFA_PLUS_OFFSET; |
| 7542 |
goto reg_offset_f; |
| 7543 |
case DW_CFA_offset_extended_sf: |
| 7544 |
rule.kind = DRGN_CFI_RULE_AT_CFA_PLUS_OFFSET; |
| 7545 |
goto reg_offset_sf; |
| 7546 |
case DW_CFA_val_offset: |
| 7547 |
rule.kind = DRGN_CFI_RULE_CFA_PLUS_OFFSET; |
| 7548 |
reg_offset_f: |
| 7549 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7550 |
&dwarf_regno)) || |
| 7551 |
(err = drgn_dwarf_cfi_next_offset_f(&buffer, cie, |
| 7552 |
&rule.offset))) |
| 7553 |
goto out; |
| 7554 |
if ((regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7555 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7556 |
break; |
| 7557 |
goto set_reg; |
| 7558 |
case DW_CFA_val_offset_sf: |
| 7559 |
rule.kind = DRGN_CFI_RULE_CFA_PLUS_OFFSET; |
| 7560 |
reg_offset_sf: |
| 7561 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7562 |
&dwarf_regno)) || |
| 7563 |
(err = drgn_dwarf_cfi_next_offset_sf(&buffer, cie, |
| 7564 |
&rule.offset))) |
| 7565 |
goto out; |
| 7566 |
if ((regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7567 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7568 |
break; |
| 7569 |
goto set_reg; |
| 7570 |
case DW_CFA_register: { |
| 7571 |
rule.kind = DRGN_CFI_RULE_REGISTER_PLUS_OFFSET; |
| 7572 |
rule.offset = 0; |
| 7573 |
uint64_t dwarf_regno2; |
| 7574 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7575 |
&dwarf_regno)) || |
| 7576 |
(err = binary_buffer_next_uleb128(&buffer.bb, |
| 7577 |
&dwarf_regno2))) |
| 7578 |
goto out; |
| 7579 |
if ((regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7580 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7581 |
break; |
| 7582 |
if ((rule.regno = dwarf_regno_to_internal(dwarf_regno2)) == |
| 7583 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7584 |
rule.kind = DRGN_CFI_RULE_UNDEFINED; |
| 7585 |
goto set_reg; |
| 7586 |
} |
| 7587 |
case DW_CFA_expression: |
| 7588 |
rule.kind = DRGN_CFI_RULE_AT_DWARF_EXPRESSION; |
| 7589 |
goto reg_expression; |
| 7590 |
case DW_CFA_val_expression: |
| 7591 |
rule.kind = DRGN_CFI_RULE_DWARF_EXPRESSION; |
| 7592 |
reg_expression: |
| 7593 |
rule.push_cfa = true; |
| 7594 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7595 |
&dwarf_regno)) || |
| 7596 |
(err = drgn_dwarf_cfi_next_block(&buffer, |
| 7597 |
&rule.expr, |
| 7598 |
&rule.expr_size))) |
| 7599 |
goto out; |
| 7600 |
if ((regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7601 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7602 |
break; |
| 7603 |
goto set_reg; |
| 7604 |
case DW_CFA_restore: |
| 7605 |
if (!initial_row) |
| 7606 |
goto invalid_for_initial; |
| 7607 |
dwarf_regno = opcode & 0x3f; |
| 7608 |
goto restore; |
| 7609 |
case DW_CFA_restore_extended: |
| 7610 |
if (!initial_row) { |
| 7611 |
invalid_for_initial: |
| 7612 |
err = binary_buffer_error(&buffer.bb, |
| 7613 |
"invalid initial DWARF CFI opcode %#" PRIx8, |
| 7614 |
opcode); |
| 7615 |
goto out; |
| 7616 |
} |
| 7617 |
if ((err = binary_buffer_next_uleb128(&buffer.bb, |
| 7618 |
&dwarf_regno))) |
| 7619 |
goto out; |
| 7620 |
restore: |
| 7621 |
if ((regno = dwarf_regno_to_internal(dwarf_regno)) == |
| 7622 |
DRGN_REGISTER_NUMBER_UNKNOWN) |
| 7623 |
break; |
| 7624 |
drgn_cfi_row_get_register(initial_row, regno, &rule); |
| 7625 |
set_reg: |
| 7626 |
if (!drgn_cfi_row_set_register(row, regno, &rule)) { |
| 7627 |
err = &drgn_enomem; |
| 7628 |
goto out; |
| 7629 |
} |
| 7630 |
break; |
| 7631 |
case DW_CFA_remember_state: { |
| 7632 |
struct drgn_cfi_row **state = |
| 7633 |
drgn_cfi_row_vector_append_entry(&state_stack); |
| 7634 |
if (!state) { |
| 7635 |
err = &drgn_enomem; |
| 7636 |
goto out; |
| 7637 |
} |
| 7638 |
*state = drgn_empty_cfi_row; |
| 7639 |
if (!drgn_cfi_row_copy(state, *row)) { |
| 7640 |
err = &drgn_enomem; |
| 7641 |
goto out; |
| 7642 |
} |
| 7643 |
break; |
| 7644 |
} |
| 7645 |
case DW_CFA_restore_state: |
| 7646 |
if (drgn_cfi_row_vector_empty(&state_stack)) { |
| 7647 |
err = binary_buffer_error(&buffer.bb, |
| 7648 |
"DW_CFA_restore_state with empty state stack"); |
| 7649 |
goto out; |
| 7650 |
} |
| 7651 |
drgn_cfi_row_destroy(*row); |
| 7652 |
*row = *drgn_cfi_row_vector_pop(&state_stack); |
| 7653 |
break; |
| 7654 |
case DW_CFA_nop: |
| 7655 |
break; |
| 7656 |
// Note that this is the same opcode as DW_CFA_GNU_window_save, |
| 7657 |
// which is used on Sparc. |
| 7658 |
case DW_CFA_AARCH64_negate_ra_state: |
| 7659 |
if (drgn_platform_arch(&file->platform) |
| 7660 |
== DRGN_ARCH_AARCH64) { |
| 7661 |
regno = DRGN_AARCH64_RA_SIGN_STATE_REGNO; |
| 7662 |
drgn_cfi_row_get_register(*row, regno, &rule); |
| 7663 |
if (rule.kind != DRGN_CFI_RULE_CONSTANT) { |
| 7664 |
err = binary_buffer_error(&buffer.bb, |
| 7665 |
"DW_CFA_AARCH64_negate_ra_state mixed with another rule"); |
| 7666 |
goto out; |
| 7667 |
} |
| 7668 |
rule.constant ^= 1; |
| 7669 |
goto set_reg; |
| 7670 |
} |
| 7671 |
fallthrough; |
| 7672 |
case DW_CFA_GNU_args_size: |
| 7673 |
// We have no use for this. Skip it. |
| 7674 |
if ((err = binary_buffer_skip_leb128(&buffer.bb))) |
| 7675 |
goto out; |
| 7676 |
break; |
| 7677 |
default: |
| 7678 |
err = binary_buffer_error(&buffer.bb, |
| 7679 |
"unknown DWARF CFI opcode %#" PRIx8, |
| 7680 |
opcode); |
| 7681 |
goto out; |
| 7682 |
} |
| 7683 |
} |
| 7684 |
found: |
| 7685 |
err = NULL; |
| 7686 |
out: |
| 7687 |
vector_for_each(drgn_cfi_row_vector, it, &state_stack) |
| 7688 |
drgn_cfi_row_destroy(*it); |
| 7689 |
drgn_cfi_row_vector_deinit(&state_stack); |
| 7690 |
return err; |
| 7691 |
} |
| 7692 |
|
| 7693 |
static struct drgn_error * |
| 7694 |
drgn_find_cfi_row_in_dwarf_fde(struct drgn_dwarf_cfi *cfi, |
| 7695 |
struct drgn_elf_file *file, |
| 7696 |
enum drgn_section_index scn, |
| 7697 |
struct drgn_dwarf_fde *fde, uint64_t unbiased_pc, |
| 7698 |
struct drgn_cfi_row **ret) |
| 7699 |
{ |
| 7700 |
struct drgn_error *err; |
| 7701 |
struct drgn_dwarf_cie *cie = &cfi->cies[fde->cie]; |
| 7702 |
struct drgn_cfi_row *initial_row = |
| 7703 |
(struct drgn_cfi_row *)file->platform.arch->default_dwarf_cfi_row; |
| 7704 |
err = drgn_eval_dwarf_cfi(file, scn, cie, fde, NULL, unbiased_pc, |
| 7705 |
cie->initial_instructions, |
| 7706 |
cie->initial_instructions_size, &initial_row); |
| 7707 |
if (err) |
| 7708 |
goto out; |
| 7709 |
if (!drgn_cfi_row_copy(ret, initial_row)) { |
| 7710 |
err = &drgn_enomem; |
| 7711 |
goto out; |
| 7712 |
} |
| 7713 |
err = drgn_eval_dwarf_cfi(file, scn, cie, fde, initial_row, unbiased_pc, |
| 7714 |
fde->instructions, fde->instructions_size, |
| 7715 |
ret); |
| 7716 |
out: |
| 7717 |
drgn_cfi_row_destroy(initial_row); |
| 7718 |
return err; |
| 7719 |
} |
| 7720 |
|
| 7721 |
static struct drgn_error * |
| 7722 |
drgn_find_dwarf_cfi(struct drgn_dwarf_cfi *cfi, struct drgn_elf_file *file, |
| 7723 |
enum drgn_section_index scn, uint64_t unbiased_pc, |
| 7724 |
struct drgn_cfi_row **row_ret, bool *interrupted_ret, |
| 7725 |
drgn_register_number *ret_addr_regno_ret) |
| 7726 |
{ |
| 7727 |
struct drgn_error *err; |
| 7728 |
|
| 7729 |
struct drgn_dwarf_fde *fde = drgn_find_dwarf_fde(cfi, unbiased_pc); |
| 7730 |
if (!fde) |
| 7731 |
return &drgn_not_found; |
| 7732 |
err = drgn_find_cfi_row_in_dwarf_fde(cfi, file, scn, fde, unbiased_pc, |
| 7733 |
row_ret); |
| 7734 |
if (err) |
| 7735 |
return err; |
| 7736 |
*interrupted_ret = cfi->cies[fde->cie].signal_frame; |
| 7737 |
*ret_addr_regno_ret = cfi->cies[fde->cie].return_address_register; |
| 7738 |
return NULL; |
| 7739 |
} |
| 7740 |
|
| 7741 |
struct drgn_error *drgn_module_parse_debug_frame(struct drgn_module *module) |
| 7742 |
{ |
| 7743 |
return drgn_parse_dwarf_cfi(&module->dwarf.debug_frame, |
| 7744 |
module->debug_file, DRGN_SCN_DEBUG_FRAME); |
| 7745 |
} |
| 7746 |
|
| 7747 |
struct drgn_error * |
| 7748 |
drgn_module_find_dwarf_cfi(struct drgn_module *module, uint64_t pc, |
| 7749 |
struct drgn_cfi_row **row_ret, bool *interrupted_ret, |
| 7750 |
drgn_register_number *ret_addr_regno_ret) |
| 7751 |
{ |
| 7752 |
return drgn_find_dwarf_cfi(&module->dwarf.debug_frame, |
| 7753 |
module->debug_file, DRGN_SCN_DEBUG_FRAME, |
| 7754 |
pc - module->debug_file_bias, row_ret, |
| 7755 |
interrupted_ret, ret_addr_regno_ret); |
| 7756 |
} |
| 7757 |
|
| 7758 |
struct drgn_error *drgn_module_parse_eh_frame(struct drgn_module *module) |
| 7759 |
{ |
| 7760 |
return drgn_parse_dwarf_cfi(&module->dwarf.eh_frame, |
| 7761 |
module->loaded_file, DRGN_SCN_EH_FRAME); |
| 7762 |
} |
| 7763 |
|
| 7764 |
struct drgn_error * |
| 7765 |
drgn_module_find_eh_cfi(struct drgn_module *module, uint64_t pc, |
| 7766 |
struct drgn_cfi_row **row_ret, bool *interrupted_ret, |
| 7767 |
drgn_register_number *ret_addr_regno_ret) |
| 7768 |
{ |
| 7769 |
return drgn_find_dwarf_cfi(&module->dwarf.eh_frame, module->loaded_file, |
| 7770 |
DRGN_SCN_EH_FRAME, |
| 7771 |
pc - module->loaded_file_bias, row_ret, |
| 7772 |
interrupted_ret, ret_addr_regno_ret); |
| 7773 |
} |
| 7774 |
|
| 7775 |
struct drgn_error * |
| 7776 |
drgn_eval_cfi_dwarf_expression(struct drgn_program *prog, |
| 7777 |
struct drgn_elf_file *file, |
| 7778 |
const struct drgn_cfi_rule *rule, |
| 7779 |
const struct drgn_register_state *regs, |
| 7780 |
void *buf, size_t size) |
| 7781 |
{ |
| 7782 |
struct drgn_error *err; |
| 7783 |
VECTOR(uint64_vector, stack); |
| 7784 |
|
| 7785 |
if (rule->push_cfa) { |
| 7786 |
struct optional_uint64 cfa = drgn_register_state_get_cfa(regs); |
| 7787 |
if (!cfa.has_value) |
| 7788 |
return &drgn_not_found; |
| 7789 |
if (!uint64_vector_append(&stack, &cfa.value)) |
| 7790 |
return &drgn_enomem; |
| 7791 |
} |
| 7792 |
|
| 7793 |
int remaining_ops = MAX_DWARF_EXPR_OPS; |
| 7794 |
struct drgn_dwarf_expression_context ctx; |
| 7795 |
drgn_dwarf_expression_context_init(&ctx, prog, file, NULL, NULL, regs, |
| 7796 |
rule->expr, rule->expr_size); |
| 7797 |
err = drgn_eval_dwarf_expression(&ctx, &stack, &remaining_ops); |
| 7798 |
if (err) { |
| 7799 |
if (err == &drgn_unknown_dwarf_opcode) |
| 7800 |
err = &drgn_not_found; |
| 7801 |
return err; |
| 7802 |
} |
| 7803 |
if (binary_buffer_has_next(&ctx.bb)) { |
| 7804 |
uint8_t opcode; |
| 7805 |
err = binary_buffer_next_u8(&ctx.bb, &opcode); |
| 7806 |
if (!err) { |
| 7807 |
err = binary_buffer_error(&ctx.bb, |
| 7808 |
"invalid opcode %#" PRIx8 " for CFI expression", |
| 7809 |
opcode); |
| 7810 |
} |
| 7811 |
return err; |
| 7812 |
} |
| 7813 |
if (uint64_vector_empty(&stack)) { |
| 7814 |
return &drgn_not_found; |
| 7815 |
} else if (rule->kind == DRGN_CFI_RULE_AT_DWARF_EXPRESSION) { |
| 7816 |
return drgn_program_read_memory(prog, buf, |
| 7817 |
*uint64_vector_last(&stack), |
| 7818 |
size, false); |
| 7819 |
} else { |
| 7820 |
copy_lsbytes(buf, size, drgn_elf_file_is_little_endian(file), |
| 7821 |
uint64_vector_last(&stack), sizeof(uint64_t), |
| 7822 |
HOST_LITTLE_ENDIAN); |
| 7823 |
return NULL; |
| 7824 |
} |
| 7825 |
} |