github.com/eh-steve/goloader@v0.0.0-20240111193454-90ff3cfdae39/mmap/mmap_unix.go (about)

     1  //go:build (darwin && !arm64) || (linux && !arm64) || dragonfly || freebsd || openbsd || solaris || netbsd
     2  // +build darwin,!arm64 linux,!arm64 dragonfly freebsd openbsd solaris netbsd
     3  
     4  package mmap
     5  
     6  import (
     7  	"os"
     8  	"syscall"
     9  )
    10  
    11  func MakeThreadJITCodeExecutable(ptr uintptr, len int) {
    12  }
    13  
    14  func Mmap(size int) ([]byte, error) {
    15  	return AcquireMapping(size, mmapCode)
    16  }
    17  
    18  func MmapData(size int) ([]byte, error) {
    19  	return AcquireMapping(size, mmapData)
    20  }
    21  
    22  func mmapCode(size int, addr uintptr) ([]byte, error) {
    23  	fixed := 0
    24  	if addr != 0 {
    25  		fixed = syscall.MAP_FIXED
    26  	}
    27  	data, err := mapper.Mmap(
    28  		addr,
    29  		0,
    30  		0,
    31  		size,
    32  		syscall.PROT_READ|syscall.PROT_WRITE|syscall.PROT_EXEC,
    33  		syscall.MAP_PRIVATE|syscall.MAP_ANON|fixed)
    34  	if err != nil {
    35  		err = os.NewSyscallError("syscall.Mmap", err)
    36  	}
    37  	return data, err
    38  }
    39  
    40  func mmapData(size int, addr uintptr) ([]byte, error) {
    41  	fixed := 0
    42  	if addr != 0 {
    43  		fixed = syscall.MAP_FIXED
    44  	}
    45  	data, err := mapper.Mmap(
    46  		addr,
    47  		0,
    48  		0,
    49  		size,
    50  		syscall.PROT_READ|syscall.PROT_WRITE,
    51  		syscall.MAP_PRIVATE|syscall.MAP_ANON|fixed)
    52  	if err != nil {
    53  		err = os.NewSyscallError("syscall.Mmap", err)
    54  	}
    55  	return data, err
    56  }
    57  
    58  func Munmap(b []byte) (err error) {
    59  	err = mapper.Munmap(b)
    60  	if err != nil {
    61  		err = os.NewSyscallError("syscall.Munmap", err)
    62  	}
    63  	return
    64  }