diff options
author | Colin Noga <Tempus@chronometry.ca> | 2012-03-29 11:11:11 -0500 |
---|---|---|
committer | Colin Noga <Tempus@chronometry.ca> | 2012-03-29 11:11:11 -0500 |
commit | 2e5153002bccc3722dbacaa62b729493d2c2bf89 (patch) | |
tree | 0d235a689a0d7c831293dff206833f2d8efb277a /tools/hooks.py | |
parent | 128ab59a90b26c54e18f8823adc26d37115fb5f5 (diff) | |
parent | 7bbab722a388bc63ec195a48c22c7060e3fab999 (diff) | |
download | kamek-2e5153002bccc3722dbacaa62b729493d2c2bf89.tar.gz kamek-2e5153002bccc3722dbacaa62b729493d2c2bf89.zip |
merging changes to latest pull
Diffstat (limited to '')
-rwxr-xr-x | tools/hooks.py | 73 |
1 files changed, 41 insertions, 32 deletions
diff --git a/tools/hooks.py b/tools/hooks.py index 13c4e19..388efcb 100755 --- a/tools/hooks.py +++ b/tools/hooks.py @@ -3,14 +3,18 @@ import struct u32 = struct.Struct('>I')
+BRANCH_TYPES = ['b', 'bl', 'ba', 'bla']
def make_branch_insn(fromAddr, toAddr, branchType):
- branchTypes = ['b', 'bl', 'ba', 'bla']
- if branchType not in branchTypes:
+ if branchType not in BRANCH_TYPES:
raise ValueError, 'invalid branch type: %s' % branchType
- extra = branchTypes.index(branchType)
+ extra = BRANCH_TYPES.index(branchType)
- distance = toAddr - fromAddr
+ if toAddr == -1:
+ distance = 0 # placeholder, will be added later by relocs
+ else:
+ distance = toAddr - fromAddr
+
if distance >= 0x2000000 or distance <= -0x2000000:
raise ValueError, 'branching too far: %08x to %08x' % (fromAddr, toAddr)
@@ -44,7 +48,7 @@ class Hook(object): self.context.hooks.append(self)
# validate the hook's data
- current_config_name = builder._config_short_name
+ current_config_name = builder.config_short_name
for field in self.required_data:
field = field.replace('%CONFIG%', current_config_name)
@@ -65,7 +69,7 @@ class BasicPatchHook(Hook): Hook.__init__(self, builder, module, data)
def create_patches(self):
- addr = self.data['addr_%s' % self.builder._config_short_name]
+ addr = self.data['addr_%s' % self.builder.config_short_name]
hex_data = self.data['data']
@@ -75,7 +79,7 @@ class BasicPatchHook(Hook): patch = binascii.unhexlify(hex_data)
- self.builder._add_patch(addr, patch)
+ self.builder.add_patch(addr, patch)
@@ -91,19 +95,23 @@ class BranchInsnHook(Hook): try:
target_func = self.data['target_func']
except KeyError:
- target_func = self.data['target_func_%s' % self.builder._config_short_name]
-
- if isinstance(target_func, str):
- target_func = self.builder._find_func_by_symbol(target_func)
+ target_func = self.data['target_func_%s' % self.builder.config_short_name]
+
+ src_addr = self.data['src_addr_%s' % self.builder.config_short_name]
+ is_symbol_name = isinstance(target_func, str)
+
+ if is_symbol_name:
+ target_func = self.builder.find_func_by_symbol(target_func)
+
+ if is_symbol_name and self.builder.dynamic_link:
+ branch_insn = make_branch_insn(src_addr, -1, self.data['branch_type'])
+ self.builder.add_patch(src_addr, u32.pack(branch_insn))
+
+ dylink = self.builder.dynamic_link
+ dylink.add_reloc(dylink.R_PPC_REL24, src_addr, target_func)
else:
- # assume it's an address
- pass
-
-
- src_addr = self.data['src_addr_%s' % self.builder._config_short_name]
- branch_insn = make_branch_insn(src_addr, target_func, self.data['branch_type'])
-
- self.builder._add_patch(src_addr, u32.pack(branch_insn))
+ branch_insn = make_branch_insn(src_addr, target_func, self.data['branch_type'])
+ self.builder.add_patch(src_addr, u32.pack(branch_insn))
@@ -119,18 +127,19 @@ class AddFunctionPointerHook(Hook): try:
target_func = self.data['target_func']
except KeyError:
- target_func = self.data['target_func_%s' % self.builder._config_short_name]
-
- if isinstance(target_func, str):
- target_func = self.builder._find_func_by_symbol(target_func)
+ target_func = self.data['target_func_%s' % self.builder.config_short_name]
+
+ src_addr = self.data['src_addr_%s' % self.builder.config_short_name]
+ is_symbol_name = isinstance(target_func, str)
+
+ if is_symbol_name:
+ target_func = self.builder.find_func_by_symbol(target_func)
+
+ if is_symbol_name and self.builder.dynamic_link:
+ dylink = self.builder.dynamic_link
+ dylink.add_reloc(dylink.R_PPC_ADDR32, src_addr, target_func)
else:
- # assume it's an address
- pass
-
-
- src_addr = self.data['src_addr_%s' % self.builder._config_short_name]
-
- self.builder._add_patch(src_addr, u32.pack(target_func))
+ self.builder.add_patch(src_addr, u32.pack(target_func))
@@ -143,7 +152,7 @@ class NopInsnHook(Hook): Hook.__init__(self, builder, module, data)
def create_patches(self):
- area = self.data['area_%s' % self.builder._config_short_name]
+ area = self.data['area_%s' % self.builder.config_short_name]
if isinstance(area, list):
addr, end = area
@@ -153,7 +162,7 @@ class NopInsnHook(Hook): addr = area
nop_patch = '\x60\x00\x00\x00'
- self.builder._add_patch(addr, nop_patch)
+ self.builder.add_patch(addr, nop_patch)
|