Source file src/runtime/cgo/cgo_linux.go

     1  // Copyright 2026 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 goexperiment.cgobindings && linux && !android && (amd64 || arm64)
     6  
     7  package cgo
     8  
     9  import "unsafe"
    10  
    11  //go:linkname _libc_mmap libc_mmap
    12  var _libc_mmap byte
    13  var libc_mmap = unsafe.Pointer(&_libc_mmap)
    14  
    15  //go:linkname _libc_munmap libc_munmap
    16  var _libc_munmap byte
    17  var libc_munmap = unsafe.Pointer(&_libc_munmap)
    18  
    19  //go:linkname _libc_errno_location libc_errno_location
    20  var _libc_errno_location byte
    21  var libc_errno_location = unsafe.Pointer(&_libc_errno_location)
    22  
    23  //go:linknamestd _cgoMmapFn runtime.cgoMmapFn
    24  var _cgoMmapFn = cgoMmapFn
    25  
    26  //go:nosplit
    27  //go:norace
    28  //go:nocheckptr
    29  func cgoMmapFn(addr unsafe.Pointer, length uintptr, prot, flags, fd int32, offset uint32) uintptr {
    30  	tsanAcquire()
    31  	p := callC(uintptr(libc_mmap), uintptr(addr), length, uintptr(uint32(prot)), uintptr(uint32(flags)), uintptr(fd), uintptr(offset))
    32  	if int(p) == -1 {
    33  		errnoPtr := callC(uintptr(libc_errno_location), 0, 0, 0, 0, 0, 0)
    34  		errno := uintptr(*(*int32)(unsafe.Pointer(errnoPtr)))
    35  		tsanRelease()
    36  		return errno
    37  	}
    38  	tsanRelease()
    39  	return p
    40  }
    41  
    42  //go:linknamestd _cgoMunmapFn runtime.cgoMunmapFn
    43  var _cgoMunmapFn = cgoMunmapFn
    44  
    45  //go:nosplit
    46  //go:norace
    47  func cgoMunmapFn(addr unsafe.Pointer, length uintptr) int32 {
    48  	tsanAcquire()
    49  	r := int32(callC(uintptr(libc_munmap), uintptr(addr), length, 0, 0, 0, 0))
    50  	tsanRelease()
    51  	return r
    52  }
    53  
    54  // Specify "libc.so.6" so internal linking without C host objects emits a
    55  // DT_NEEDED entry for glibc's dynamic loader; external linking ignores the
    56  // library field, !android excludes bionic, and musl treats any "libc.so.*"
    57  // SONAME as libc itself.
    58  //go:cgo_import_dynamic libc_mmap mmap "libc.so.6"
    59  //go:cgo_import_dynamic libc_munmap munmap "libc.so.6"
    60  //go:cgo_import_dynamic libc_errno_location __errno_location "libc.so.6"
    61  

View as plain text