def setRelativeAddress(source, instruction, target): # subtract the old instruction length instructionlength = length(instruction) res = target - source - instructionlength if res >= -0x80 and res < 0x80: result = promoteBranch_8(instruction) sz = length(result) - length(instruction) return setImmediate(result, encodeInteger(res-sz, 1)) # elif res >= -0x8000 and res < 0x8000: # result = promoteBranch_16(instruction) # sz = length(result) - length(instruction) # return setImmediate(result, encodeInteger(res-sz, 2)) elif res >= -0x80000000 and res < 0x80000000: result = promoteBranch_32(instruction) sz = length(result) - length(instruction) return setImmediate(result, encodeInteger(res-sz, 4)) raise NotImplementedError("Unable to figure out immediate value size for %x"% res)
def setRelativeAddress(source, instruction, target): # subtract the old instruction length instructionlength = length(instruction) res = target - source - instructionlength if res >= -0x80 and res < 0x80: result = promoteBranch_8(instruction) sz = length(result) - length(instruction) return setImmediate(result, encodeInteger(res - sz, 1)) # elif res >= -0x8000 and res < 0x8000: # result = promoteBranch_16(instruction) # sz = length(result) - length(instruction) # return setImmediate(result, encodeInteger(res-sz, 2)) elif res >= -0x80000000 and res < 0x80000000: result = promoteBranch_32(instruction) sz = length(result) - length(instruction) return setImmediate(result, encodeInteger(res - sz, 4)) raise NotImplementedError( "Unable to figure out immediate value size for %x" % res)
def promoteBranch_16(instruction): raise NotImplementedError("16-bit absolute branches not implemented really") result = promoteBranch_32(instruction) imm = getImmediate(result) offset = decodeInteger(imm, True) - length(result) # downgrade the opcode prefix = getPrefix(result) if '\x66' not in prefix: prefix += '\x66' result = setPrefix(result, prefix) offset += length(result) return setImmediate(result, encodeInteger(offset, 2))
def promoteBranch_16(instruction): raise NotImplementedError( "16-bit absolute branches not implemented really") result = promoteBranch_32(instruction) imm = getImmediate(result) offset = decodeInteger(imm, True) - length(result) # downgrade the opcode prefix = getPrefix(result) if '\x66' not in prefix: prefix += '\x66' result = setPrefix(result, prefix) offset += length(result) return setImmediate(result, encodeInteger(offset, 2))
def promoteBranch_32(instruction): imm = getImmediate(instruction) offset = decodeInteger(imm, True) + length(instruction) prefix = ''.join([x for x in getPrefix(instruction) if x != '\x66']) if isConditionalBranch8(instruction): column = ord(getOpcode(instruction)) & 0xf result = setOpcode(instruction, '\x0f'+chr(column | 0x80)) elif isUnconditionalBranch8(instruction): result = setOpcode(instruction, '\xe9') elif isRelativeCall(instruction) or isUnconditionalBranch(instruction) or isConditionalBranch(instruction): result = instruction else: raise NotImplementedError('Unable to promote a non-branch instruction to 32-bits: {!r}'.format(n)) result = setPrefix(setImmediate(result, '\x00\x00\x00\x00'), prefix) return setImmediate(result, encodeInteger(offset-length(result), 4))
def promoteBranch_8(instruction): '''Promote(?) instruction to an 8-bit branch''' imm = getImmediate(instruction) offset = decodeInteger(imm, True) prefix = ''.join([x for x in getPrefix(instruction) if x != '\x66']) if isConditionalBranch8(instruction) or isUnconditionalBranch8(instruction) or isRelativeCall(instruction): result = instruction offset += length(result) elif isUnconditionalBranch(instruction): result = setOpcode(instruction, '\xeb') elif isConditionalBranch(instruction): column = ord(getOpcode(instruction)[1]) & 0xf result = setOpcode(instruction, chr(column | 0x70)) else: raise NotImplementedError('Unable to promote a non-branch instruction to 8-bits: {!r}'.format(n)) result = setPrefix(setImmediate(result, '\x00'), prefix) return setImmediate(result, encodeInteger(offset-length(result), 1))
def promoteBranch_32(instruction): imm = getImmediate(instruction) offset = decodeInteger(imm, True) + length(instruction) prefix = ''.join([x for x in getPrefix(instruction) if x != '\x66']) if isConditionalBranch8(instruction): column = ord(getOpcode(instruction)) & 0xf result = setOpcode(instruction, '\x0f' + chr(column | 0x80)) elif isUnconditionalBranch8(instruction): result = setOpcode(instruction, '\xe9') elif isRelativeCall(instruction) or isUnconditionalBranch( instruction) or isConditionalBranch(instruction): result = instruction else: raise NotImplementedError( 'Unable to promote a non-branch instruction to 32-bits: {!r}'. format(n)) result = setPrefix(setImmediate(result, '\x00\x00\x00\x00'), prefix) return setImmediate(result, encodeInteger(offset - length(result), 4))
def promoteBranch_8(instruction): '''Promote(?) instruction to an 8-bit branch''' imm = getImmediate(instruction) offset = decodeInteger(imm, True) prefix = ''.join([x for x in getPrefix(instruction) if x != '\x66']) if isConditionalBranch8(instruction) or isUnconditionalBranch8( instruction) or isRelativeCall(instruction): result = instruction offset += length(result) elif isUnconditionalBranch(instruction): result = setOpcode(instruction, '\xeb') elif isConditionalBranch(instruction): column = ord(getOpcode(instruction)[1]) & 0xf result = setOpcode(instruction, chr(column | 0x70)) else: raise NotImplementedError( 'Unable to promote a non-branch instruction to 8-bits: {!r}'. format(n)) result = setPrefix(setImmediate(result, '\x00'), prefix) return setImmediate(result, encodeInteger(offset - length(result), 1))
def numberToString(number, bytes): '''This function name is deprecated in favor of encodeInteger''' return encodeInteger(number, bytes)