github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/link/internal/ld/outbuf_mmap.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd linux openbsd
     6  
     7  package ld
     8  
     9  import (
    10  	"syscall"
    11  	"unsafe"
    12  )
    13  
    14  func (out *OutBuf) Mmap(filesize uint64) error {
    15  	err := out.f.Truncate(int64(filesize))
    16  	if err != nil {
    17  		Exitf("resize output file failed: %v", err)
    18  	}
    19  	out.buf, err = syscall.Mmap(int(out.f.Fd()), 0, int(filesize), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED|syscall.MAP_FILE)
    20  	return err
    21  }
    22  
    23  func (out *OutBuf) Munmap() {
    24  	err := out.Msync()
    25  	if err != nil {
    26  		Exitf("msync output file failed: %v", err)
    27  	}
    28  	syscall.Munmap(out.buf)
    29  	out.buf = nil
    30  	_, err = out.f.Seek(out.off, 0)
    31  	if err != nil {
    32  		Exitf("seek output file failed: %v", err)
    33  	}
    34  }
    35  
    36  func (out *OutBuf) Msync() error {
    37  	// TODO: netbsd supports mmap and msync, but the syscall package doesn't define MSYNC.
    38  	// It is excluded from the build tag for now.
    39  	_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(&out.buf[0])), uintptr(len(out.buf)), syscall.MS_SYNC)
    40  	if errno != 0 {
    41  		return errno
    42  	}
    43  	return nil
    44  }