This moves the platform-specific runtime code (currently only used by the compiler) into its own package. Specifically, this moves the mmap logic, and in doing so makes it easier to test, for example new operating systems. This also backfills missing RATIONALE about x/sys and hints at a future possibility to allow a plugin. However, the next step is to get FreeBSD working natively on the compiler without any additional dependencies. See #607 Signed-off-by: Adrian Cole <adrian@tetrate.io>
33 lines
929 B
Go
33 lines
929 B
Go
// Package platform includes runtime-specific code needed for the compiler or otherwise.
|
|
//
|
|
// Note: This is a dependency-free alternative to depending on parts of Go's x/sys.
|
|
// See /RATIONALE.md for more context.
|
|
package platform
|
|
|
|
import (
|
|
"errors"
|
|
"runtime"
|
|
)
|
|
|
|
// MmapCodeSegment copies the code into the executable region and returns the byte slice of the region.
|
|
//
|
|
// See https://man7.org/linux/man-pages/man2/mmap.2.html for mmap API and flags.
|
|
func MmapCodeSegment(code []byte) ([]byte, error) {
|
|
if len(code) == 0 {
|
|
panic(errors.New("BUG: MmapCodeSegment with zero length"))
|
|
}
|
|
if runtime.GOARCH == "amd64" {
|
|
return mmapCodeSegmentAMD64(code)
|
|
} else {
|
|
return mmapCodeSegmentARM64(code)
|
|
}
|
|
}
|
|
|
|
// MunmapCodeSegment unmaps the given memory region.
|
|
func MunmapCodeSegment(code []byte) error {
|
|
if len(code) == 0 {
|
|
panic(errors.New("BUG: MunmapCodeSegment with zero length"))
|
|
}
|
|
return munmapCodeSegment(code)
|
|
}
|