Skip to content

Commit

Permalink
arm64 replace call code use X17 register instead of X9
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujhd committed Mar 8, 2023
1 parent 7ba95be commit 2b7ad33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions asm_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ const (

// arm/arm64
var (
armcode = []byte{0x04, 0xF0, 0x1F, 0xE5} //LDR PC, [PC, #-4]
arm64code = []byte{
0x49, 0x00, 0x00, 0x58, // LDR X9 [PC+8]
0x20, 0x01, 0x1F, 0xD6} // BR X9
armReplaceCallCode = []byte{0x04, 0xF0, 0x1F, 0xE5} //LDR PC, [PC, #-4]
// X16 and X17 are the IP0 and IP1 intra-procedure-call corruptible registers -
// since Go only uses them for the stack prologue and epilogue calculations,
// and we should already be clear of that by the time we hit a R_CALLARM64,
// so we should be able to safely use them for far jumps
armReplace64CALLCode = []byte{
0x51, 0x00, 0x00, 0x58, // LDR X17 [PC+8] - read 64 bit address from PC+8 into X17
0x20, 0x02, 0x1f, 0xd6, // BR X17 - jump to address in X17
}
arm64Bcode = []byte{0x00, 0x00, 0x00, 0x14} // B [PC+0x0]
arm64LDRcode = []byte{0x00, 0x00, 0x40, 0xF9} // LDR XX [XX]
)
Expand Down
8 changes: 4 additions & 4 deletions relocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ func (linker *Linker) relocteCALLARM(addr uintptr, loc obj.Reloc, segment *segme
}
putUint24(segment.codeByte[loc.Offset:], off)
if loc.Type == reloctype.R_CALLARM64 {
copy(segment.codeByte[segment.codeOff:], arm64code)
segment.codeOff += len(arm64code)
copy(segment.codeByte[segment.codeOff:], armReplace64CALLCode)
segment.codeOff += len(armReplace64CALLCode)
} else {
copy(segment.codeByte[segment.codeOff:], armcode)
segment.codeOff += len(armcode)
copy(segment.codeByte[segment.codeOff:], armReplaceCallCode)
segment.codeOff += len(armReplaceCallCode)
}
putAddressAddOffset(byteorder, segment.codeByte, &segment.codeOff, uint64(int(addr)+add))
} else {
Expand Down

0 comments on commit 2b7ad33

Please sign in to comment.