| 1 | #!/usr/bin/perl | ||||||||||||||||||
| 2 | # vim: set sw=2 expandtab : # | ||||||||||||||||||
| 3 | # Master version is at https://developer.berlios.de/projects/games-hack/ | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | package Games::Hack::Patch::x86_64; | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | $VERSION=0.13; | |||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 1 | require Exporter; | |||||||||||||||||
| 10 | 1 | @ISA = qw(Exporter); | |||||||||||||||||
| 11 | 1 | @EXPORT = qw(GetNOP); | |||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | sub GetNOP | ||||||||||||||||||
| 16 | 8 | ▶ | { | ||||||||||||||||
|
Subroutine
GetNOP: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||
| 17 | 8 | my($adr_start, $adr_end, $disass)=@_; | |||||||||||||||||
| 18 | 8 | my($binary, $diff); | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 8 | $binary=""; | |||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | ### Floating point store? Stores always %ST(0). | ||||||||||||||||||
| 23 | 8 | ▶ | if ($disass =~ m#^f[bi]?st(p)?s? #) | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 24 | { | ||||||||||||||||||
| 25 | 3 | ▶ | if ($1) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 26 | { | ||||||||||||||||||
| 27 | # If the "Pop" flag is given in the "fst" ("floating point store") | ||||||||||||||||||
| 28 | # operation, we need to clean the floating point stack. | ||||||||||||||||||
| 29 | # We use the "ffreep st(0)" instruction, . | ||||||||||||||||||
| 30 | 2 | $binary .= "\xdf\xc0"; | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 2 | $adr_start += 2; | |||||||||||||||||
| 33 | } | ||||||||||||||||||
| 34 | # Rest gets done by jump. | ||||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | # Popping values from the stack | ||||||||||||||||||
| 37 | elsif ($disass =~ m#^pop#) | ||||||||||||||||||
| 38 | { | ||||||||||||||||||
| 39 | # increment esp; rest done by jump. | ||||||||||||||||||
| 40 | # is there a 64bit pop? | ||||||||||||||||||
| 41 | 1 | $binary .= "\x83\xc4\x04"; | |||||||||||||||||
| 42 | 1 | $adr_start += 3; | |||||||||||||||||
| 43 | } | ||||||||||||||||||
| 44 | ### Integer move or direct modification | ||||||||||||||||||
| 45 | elsif ($disass =~ m#^(mov|or|and|add|sub|xor)#) | ||||||||||||||||||
| 46 | { | ||||||||||||||||||
| 47 | # done by jump. | ||||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | else | ||||||||||||||||||
| 50 | { | ||||||||||||||||||
| 51 | 2 | warn "Unknown instruction '" . $disass . "', patching unsafe!"; | |||||||||||||||||
| 52 | 2 | return undef; | |||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | ### short jump | ||||||||||||||||||
| 57 | 6 | ▶ | if (($diff=$adr_end-$adr_start) >= 2) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 58 | { | ||||||||||||||||||
| 59 | # The opcode needs two bytes; these are already consumed, and so the jump | ||||||||||||||||||
| 60 | # distance has to be reduced. | ||||||||||||||||||
| 61 | 4 | $binary .= "\xeb" . pack("C", $diff-2); | |||||||||||||||||
| 62 | 4 | $adr_end=$adr_start; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | ### NOP. | ||||||||||||||||||
| 66 | # That should never be needed ... all instructions involving memory access | ||||||||||||||||||
| 67 | # (even via a register) have at least 2 bytes. | ||||||||||||||||||
| 68 | # (If some value would be persistently stored in a register, we wouldn't | ||||||||||||||||||
| 69 | # find it by looking at the memory contents.) | ||||||||||||||||||
| 70 | 6 | ▶ | $binary .= "\x90" x $diff | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 71 | if ($diff=$adr_end-$adr_start) >= 1; | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | 6 | return $binary; | |||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||