Skip to content

Commit

Permalink
Rebase addresses feature (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-f0 authored Jul 19, 2024
1 parent 1563f74 commit 00578b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Settings are stored in a file `.llef` located in your home directory formatted a
| show_threads | Boolean | Enable/disable threads output |
| show_trace | Boolean | Enable/disable trace output |
| force_arch | String | Force register display architecture (experimental) |
| rebase_addresses | Boolean | Enable/disable address rebase output |
| rebase_offset | Int | Set the rebase offset (default 0x100000) |

#### Context

Expand Down Expand Up @@ -106,6 +108,12 @@ aabacadaea
### Breakpoint hook
This is automatic and prints all the currently implemented information at a break point.

#### Address Rebasing
Configurable with the `rebase_addresses` setting the address rebasing feature performs a lookup for each code address presented in the output to display the associated binary and relative address. This relative address is offset by the value defined in setting `rebase_offset` which defaults to the Ghidra base address of `0x100000`. The result is an address output that can be easily copied and pasted into an IDE "Go To Address" feature without having to do the maths to convert from the runtime address.

Rebased addresses are shown in brackets after the runtime address:
![rebase address feature](assets/rebase-feature.png)

## 👷‍♂️ Troubleshooting LLDB Python support
LLDB comes bundled with python modules that are required for LLEF to run. If on launching LLDB with LLEF you encounter `ModuleNotFoundError` messages it is likely you will need to manually add the LLDB python modules on your python path.

Expand Down
Binary file added assets/rebase-feature.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 16 additions & 3 deletions common/context_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from typing import Dict, Type, Optional

from lldb import (
Expand Down Expand Up @@ -60,6 +62,16 @@ def output_line(self, line: str) -> None:
for term_color in TERM_COLORS:
line = line.replace(term_color.value, "")
print(line)

def generate_rebased_address_string(self, address: SBAddress) -> str:
module = address.GetModule()

if module is not None and self.settings.rebase_addresses is True:
file_name = os.path.basename(str(module.file))
rebased_address = address.GetFileAddress() + self.settings.rebase_offset
return f" {TERM_COLORS.GREY.value}({file_name} {rebased_address:#x}){TERM_COLORS.ENDC.value}"

return ""

def generate_printable_line_from_pointer(
self, pointer: SBValue, address_containing_pointer: Optional[int] = None
Expand All @@ -78,7 +90,7 @@ def generate_printable_line_from_pointer(
pointer_value.offset - pointer_value.symbol.GetStartAddress().offset
)
line += (
f" {GLYPHS.RIGHT_ARROW.value} {TERM_COLORS.GREY.value}"
f"{self.generate_rebased_address_string(pointer_value)} {GLYPHS.RIGHT_ARROW.value} {TERM_COLORS.GREY.value}"
+ f"<{pointer_value.symbol.name}+{offset}>{TERM_COLORS.ENDC.value}"
)

Expand Down Expand Up @@ -285,15 +297,16 @@ def display_trace(self) -> None:
current_frame = self.thread.GetFrameAtIndex(i)
pc_address = current_frame.GetPCAddress()
func = current_frame.GetFunction()
trace_address = pc_address.GetLoadAddress(self.target)

if func:
line += (
f"{pc_address.GetLoadAddress(self.target):#x} {GLYPHS.RIGHT_ARROW.value} "
f"{trace_address:#x}{self.generate_rebased_address_string(pc_address)} {GLYPHS.RIGHT_ARROW.value} "
+ f"{TERM_COLORS.GREEN.value}{func.GetName()}{TERM_COLORS.ENDC.value}"
)
else:
line += (
f"{pc_address.GetLoadAddress(self.target):#x} {GLYPHS.RIGHT_ARROW.value} "
f"{trace_address:#x}{self.generate_rebased_address_string(pc_address)} {GLYPHS.RIGHT_ARROW.value} "
+ f"{TERM_COLORS.GREEN.value}{current_frame.GetSymbol().GetName()}{TERM_COLORS.ENDC.value}"
)

Expand Down
8 changes: 8 additions & 0 deletions common/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def force_arch(self):
arch = self._RAW_CONFIG.get(GLOBAL_SECTION, "force_arch", fallback=None)
return None if arch not in supported_arch else arch

@property
def rebase_addresses(self):
return self._RAW_CONFIG.getboolean(GLOBAL_SECTION, "rebase_addresses", fallback=True)

@property
def rebase_offset(self):
return self._RAW_CONFIG.getint(GLOBAL_SECTION, "rebase_offset", fallback=0x100000)

@classmethod
def _get_setting_names(cls):
return [name for name, value in vars(cls).items() if isinstance(value, property)]
Expand Down

0 comments on commit 00578b8

Please sign in to comment.