Skip to content

Commit

Permalink
Backend: Save load args for future uses. (#499)
Browse files Browse the repository at this point in the history
* Backend: Save load args for future uses.

* Call set_load_args() in more backends.
  • Loading branch information
ltfish authored Jul 27, 2024
1 parent e3e87a0 commit 7cdad9a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cle/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ def __init__(
:param binary_stream: The open stream to this binary. The reference to this will be held until you call close.
:param is_main_bin: Whether this binary should be loaded as the main executable
"""

self.load_args: dict[str, Any] = {} | kwargs
self.set_load_args(
loader=loader,
is_main_bin=is_main_bin,
entry_point=entry_point,
arch=arch,
base_addr=base_addr,
force_rebase=force_rebase,
has_memory=has_memory,
)
self.binary = binary
self._binary_stream: BufferedReader = binary_stream
if self.binary is not None:
Expand Down Expand Up @@ -292,6 +303,9 @@ def set_arch(self, arch):
self._arch = arch
self.memory = Clemory(arch) # Private virtual address space, without relocations

def set_load_args(self, **kwargs) -> None:
self.load_args |= kwargs

@property
def image_base_delta(self):
return self.mapped_base - self.linked_base
Expand Down
1 change: 1 addition & 0 deletions cle/backends/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, *args, offset=None, segments=None, **kwargs):
offset = kwargs.pop("custom_offset")
log.critical("Deprecation warning: the custom_offset parameter has been renamed to offset")
super().__init__(*args, **kwargs)
self.set_load_args(offset=offset, segments=segments)

if self._arch is None:
raise CLEError("Must specify arch when loading blob!")
Expand Down
1 change: 1 addition & 0 deletions cle/backends/cartfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, binary, binary_stream, *args, arc4_key=None, **kwargs):
"Please install the cart Python package before loading a CART file. You may run `pip install cart`."
)
super().__init__(binary, binary_stream, *args, **kwargs)
self.set_load_args(arc4_key=arc4_key)

# hack: we are using a loader internal method in a non-kosher way which will cause our children to be
# marked as the main binary if we are also the main binary
Expand Down
6 changes: 6 additions & 0 deletions cle/backends/elf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def __init__(
**kwargs,
):
super().__init__(*args, **kwargs)
self.set_load_args(
addend=addend,
debug_symbols=debug_symbols,
discard_section_headers=discard_section_headers,
discard_program_headers=discard_program_headers,
)
patch_undo = []
try:
self._reader = elffile.ELFFile(self._binary_stream)
Expand Down
1 change: 1 addition & 0 deletions cle/backends/ihex.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def coalesce_regions(regions):

def __init__(self, *args, ignore_missing_arch: bool = False, **kwargs):
super().__init__(*args, **kwargs)
self.set_load_args(ignore_missing_arch=ignore_missing_arch)

if self._arch is None:
if ignore_missing_arch:
Expand Down
1 change: 1 addition & 0 deletions cle/backends/pe/pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PE(Backend):

def __init__(self, *args, debug_symbols=None, **kwargs):
super().__init__(*args, **kwargs)
self.set_load_args(debug_symbols=debug_symbols)
self.segments = self.sections # in a PE, sections and segments have the same meaning
self.os = "windows"
if self.binary is None:
Expand Down
1 change: 1 addition & 0 deletions cle/backends/srec.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def coalesce_regions(regions):

def __init__(self, *args, ignore_missing_arch: bool = False, **kwargs):
super().__init__(*args, **kwargs)
self.set_load_args(ignore_missing_arch=ignore_missing_arch)

if self._arch is None:
if ignore_missing_arch:
Expand Down

0 comments on commit 7cdad9a

Please sign in to comment.