Skip to content

Commit

Permalink
orc_info: apply debug file bias to pc_base at load
Browse files Browse the repository at this point in the history
When looking up CFI rules using ORC, we use module->debug_file_bias
unconditionally. This makes sense when the ORC is always loaded from an
ELF debug file. However, now that built-in ORC can be loaded, it is
possible that:

1. ORC is loaded from the built-in source, prior to loading the debug
   file. The module->debug_file_bias == 0, so the ORC is interpreted
   correctly.
2. Later, a debug file is loaded, updating debug_file_bias. However, the
   ORC hasn't been loaded from the debug file, so the bias is not
   applicable.
3. Future CFI lookups using ORC fail due to the extra bias.

To avoid this, apply the debug_file_bias once to module->orc.pc_base,
at the time we load the ORC sections out of the debug file. This ensures
that the bias is only applied to the ORC data when we know we need it.

Signed-off-by: Stephen Brennan <[email protected]>
  • Loading branch information
brenns10 committed Dec 20, 2024
1 parent b49d37d commit 2026435
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libdrgn/orc_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ struct drgn_error *drgn_module_parse_orc(struct drgn_module *module)

if (module->debug_file) {
err = drgn_read_orc_sections(module);
module->orc.pc_base += module->debug_file_bias;
} else {
// Buffers here are not from libelf, so we should free them.
// Since new copies are allocated below, they should always be
Expand Down Expand Up @@ -676,11 +677,10 @@ drgn_module_find_orc_cfi(struct drgn_module *module, uint64_t pc,
struct drgn_cfi_row **row_ret, bool *interrupted_ret,
drgn_register_number *ret_addr_regno_ret)
{
uint64_t unbiased_pc = pc - module->debug_file_bias;
#define less_than_orc_pc(a, b) \
(*(a) < drgn_orc_pc(module, (b) - module->orc.pc_offsets))
size_t i = binary_search_gt(module->orc.pc_offsets,
module->orc.num_entries, &unbiased_pc,
module->orc.num_entries, &pc,
less_than_orc_pc);
#undef less_than_orc_pc
// We can tell when the program counter is below the minimum program
Expand Down

0 comments on commit 2026435

Please sign in to comment.