Skip to content

Commit

Permalink
Removed loader.architecture from py mapped view
Browse files Browse the repository at this point in the history
Also updated Python API docs references to loader.architecture
  • Loading branch information
zznop committed May 16, 2024
1 parent 5088feb commit b421487
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def load(*args, **kwargs) -> BinaryView:
...
134
>>> with load(bytes.fromhex('5054ebfe'), options={'loader.architecture' : 'x86'}) as bv:
>>> with load(bytes.fromhex('5054ebfe'), options={'loader.platform' : 'x86'}) as bv:
... print(len(list(bv.functions)))
...
1
Expand Down
2 changes: 1 addition & 1 deletion python/binaryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ def serialize(cls, image_base: int, start: int, length: int, data_offset: int=0,
>>> rom_base = 0xffff0000
>>> segments = Segment.serialize(image_base=base, start=base, length=0x1000, data_offset=0, data_length=0x1000, flags=SegmentFlag.SegmentReadable|SegmentFlag.SegmentExecutable)
>>> segments = Segment.serialize(image_base=base, start=rom_base, length=0x1000, flags=SegmentFlag.SegmentReadable, segments=segments)
>>> view = load(bytes.fromhex('5054ebfe'), options={'loader.imageBase': base, 'loader.architecture': 'x86', 'loader.segments': segments})
>>> view = load(bytes.fromhex('5054ebfe'), options={'loader.imageBase': base, 'loader.platform': 'x86', 'loader.segments': segments})
```
"""
segments_list = json.loads(segments)
Expand Down
11 changes: 6 additions & 5 deletions python/examples/mappedview.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from binaryninja.log import log_error
from binaryninja import _binaryninjacore as core
from binaryninja.architecture import Architecture
from binaryninja.platform import Platform
from binaryninja.binaryview import BinaryView
from binaryninja.binaryview import BinaryViewType
from binaryninja.enums import SegmentFlag
Expand Down Expand Up @@ -53,7 +54,7 @@ def is_valid_for_data(data):
def get_load_settings_for_data(cls, data):
# This method is optional. If provided this is where the Load Settings for a BinaryViewType are specified. Binary Ninja provides
# some default read-only load settings which are:
# ["loader.architecture", "loader.platform", "loader.entryPointOffset", "loader.imageBase", "loader.segments", "loader.sections"]
# ["loader.platform", "loader.entryPointOffset", "loader.imageBase", "loader.segments", "loader.sections"]
# The default load settings are provided for consistency and convenience.
# The default load settings are always generated with a read-only indication which is respected by the UI.
# The read-only indication is a property that consists of a JSON name/value pair ("readOnly" : true).
Expand All @@ -75,7 +76,7 @@ def get_load_settings_for_data(cls, data):
load_settings = registered_view.get_default_load_settings_for_data(view)

# Specify default load settings that can be overridden (from the UI).
overrides = ["loader.architecture", "loader.platform", "loader.entryPointOffset", "loader.imageBase", "loader.segments", "loader.sections"]
overrides = ["loader.platform", "loader.entryPointOffset", "loader.imageBase", "loader.segments", "loader.sections"]
for override in overrides:
if load_settings.contains(override):
load_settings.update_property(override, json.dumps({'readOnly': False}))
Expand Down Expand Up @@ -127,9 +128,9 @@ def init(self):
# This allows us to generate default load options for the BinaryView. This step is not required but can be useful.
load_settings = self.__class__.get_load_settings_for_data(self.parent_view)

arch = load_settings.get_string("loader.architecture", self)
self.arch = Architecture[arch] # type: ignore
self.platform = Architecture[arch].standalone_platform # type: ignore
platform = load_settings.get_string("loader.platform", self)
self.platform = Platform[platform]
self.arch = self.platform.arch # type: ignore
self.load_address = load_settings.get_integer("loader.imageBase", self)
self.add_auto_segment(
self.load_address, len(self.parent_view), 0, len(self.parent_view),
Expand Down
6 changes: 4 additions & 2 deletions view/pe/teview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ bool TEView::Init()
ReadTEImageHeader(reader, header);
ReadTEImageSectionHeaders(reader, header.numberOfSections);
m_headersOffset = header.strippedSize - EFI_TE_IMAGE_HEADER_SIZE;

// m_imageBase represents the base of the original PE image (before headers were stripped), not the base of the
// TE image (bv.start)
m_imageBase = header.imageBase;

// Set architecture and platform
Expand All @@ -223,8 +226,7 @@ bool TEView::Init()
if (settings->Contains("loader.imageBase"))
{
uint64_t baseOverride = settings->Get<uint64_t>("loader.imageBase", this);
// TE image bases represent the base of the original PE (before headers are stripped) - make this
// adjustment for the user
// Apply the headers offset adjustment to compute the base of the original PE image
m_imageBase = baseOverride - m_headersOffset;
}

Expand Down

0 comments on commit b421487

Please sign in to comment.