Skip to content

Commit

Permalink
x86-64: support Constant objects (apply relocations) in ExecutableFun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
Maratyszcza committed Sep 21, 2015
1 parent 4799cc9 commit 584ec3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions peachpy/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ def _nacl_copy_code(self, code_segment):
code_offset = 0
self.allocation.dyncode_create(code_segment, code_offset)

def copy_data(self, data_segment):
import ctypes
ctypes.memmove(self.data_address,
ctypes.c_char_p(bytes(data_segment)),
len(data_segment))

def __del__(self):
if self._release_memory is not None:
if self.code_address is not None:
Expand Down
27 changes: 25 additions & 2 deletions peachpy/x86_64/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,11 +1852,34 @@ def __init__(self, function):
raise ValueError("Function ABI (%s) does not match process ABI (%s)" %
(str(function.abi), str(process_abi)))

self.code_segment = function.code_section.content
self.code_segment = bytearray(function.code_section.content)
self.const_segment = bytearray(function.const_section.content)

import peachpy.loader
self.loader = peachpy.loader.Loader(len(self.code_segment))
self.loader = peachpy.loader.Loader(len(self.code_segment), len(self.const_segment))

# Apply relocations
from peachpy.x86_64.meta import RelocationType
from peachpy.util import is_sint32
for relocation in function.code_section.relocations:
assert relocation.type == RelocationType.rip_disp32
assert relocation.symbol in function.const_section.symbols
old_value = self.code_segment[relocation.offset] | \
(self.code_segment[relocation.offset + 1] << 8) | \
(self.code_segment[relocation.offset + 2] << 16) | \
(self.code_segment[relocation.offset + 3] << 24)
new_value = old_value + \
(self.loader.data_address + relocation.symbol.offset) - \
(self.loader.code_address + relocation.offset + 4)
assert is_sint32(new_value)
self.code_segment[relocation.offset] = new_value & 0xFF
self.code_segment[relocation.offset + 1] = (new_value >> 8) & 0xFF
self.code_segment[relocation.offset + 2] = (new_value >> 16) & 0xFF
self.code_segment[relocation.offset + 3] = (new_value >> 24) & 0xFF
assert not function.const_section.relocations

self.loader.copy_code(self.code_segment)
self.loader.copy_data(self.const_segment)

import ctypes
result_type = None if function.result_type is None else function.result_type.as_ctypes_type
Expand Down

0 comments on commit 584ec3d

Please sign in to comment.