Source file src/cmd/link/internal/ld/outbuf_darwin.go

     1  // Copyright 2020 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  //go:build darwin && go1.12
     6  // +build darwin,go1.12
     7  
     8  package ld
     9  
    10  import (
    11  	"syscall"
    12  	"unsafe"
    13  )
    14  
    15  // Implemented in the syscall package.
    16  //go:linkname fcntl syscall.fcntl
    17  func fcntl(fd int, cmd int, arg int) (int, error)
    18  
    19  func (out *OutBuf) fallocate(size uint64) error {
    20  	stat, err := out.f.Stat()
    21  	if err != nil {
    22  		return err
    23  	}
    24  	// F_PEOFPOSMODE allocates from the end of the file, so we want the size difference.
    25  	// Apparently, it uses the end of the allocation, instead of the logical end of the
    26  	// the file.
    27  	cursize := uint64(stat.Sys().(*syscall.Stat_t).Blocks * 512) // allocated size
    28  	if size <= cursize {
    29  		return nil
    30  	}
    31  
    32  	store := &syscall.Fstore_t{
    33  		Flags:   syscall.F_ALLOCATEALL,
    34  		Posmode: syscall.F_PEOFPOSMODE,
    35  		Offset:  0,
    36  		Length:  int64(size - cursize),
    37  	}
    38  
    39  	_, err = fcntl(int(out.f.Fd()), syscall.F_PREALLOCATE, int(uintptr(unsafe.Pointer(store))))
    40  	return err
    41  }
    42  
    43  func (out *OutBuf) purgeSignatureCache() {
    44  	// Apparently, the Darwin kernel may cache the code signature at mmap.
    45  	// When we mmap the output buffer, it doesn't have a code signature
    46  	// (as we haven't generated one). Invalidate the kernel cache now that
    47  	// we have generated the signature. See issue #42684.
    48  	syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(&out.buf[0])), uintptr(len(out.buf)), syscall.MS_INVALIDATE)
    49  	// Best effort. Ignore error.
    50  }
    51  

View as plain text