Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/link/internal/ld

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

View as plain text