From 5e8e92cd8f403cbfebc4469a1aee252212e42045 Mon Sep 17 00:00:00 2001 From: Treeki Date: Tue, 27 Mar 2012 21:38:52 +0200 Subject: Kamek/Newer now support dynamic linking --- tools/hooks.py | 73 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 32 deletions(-) (limited to 'tools/hooks.py') 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) -- cgit v1.2.3