github.com/Rookout/GoSDK@v0.1.48/pkg/services/assembler/encoder_arm64.go (about)

     1  package assembler
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
     7  )
     8  
     9  const b = 0x14000000
    10  
    11  func abs(a int64) int64 {
    12  	if a < 0 {
    13  		return -a
    14  	}
    15  	return a
    16  }
    17  
    18  func EncodeJmp(src uintptr, dst uintptr) ([]byte, rookoutErrors.RookoutError) {
    19  	
    20  	relativeAddr := int64(dst-src) / int64(4)
    21  
    22  	
    23  	
    24  	
    25  	if relativeAddr%4 != 0 {
    26  		return nil, rookoutErrors.NewInvalidBranchDest(src, dst)
    27  	} else if abs(relativeAddr)&0b1111111111111111111111111 != abs(relativeAddr) {
    28  		return nil, rookoutErrors.NewBranchDestTooFar(src, dst)
    29  	}
    30  
    31  	
    32  	encodedOffset := uint32(int32(relativeAddr) & 0b11111111111111111111111111)
    33  
    34  	
    35  	encodedInst := uint32(b) | encodedOffset
    36  
    37  	
    38  	encodedBytes := make([]byte, 4)
    39  	binary.LittleEndian.PutUint32(encodedBytes, encodedInst)
    40  
    41  	return encodedBytes, nil
    42  }