Source file src/cmd/cgo/internal/test/unsigned_reloc_darwin.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 darwin
     6  
     7  package cgotest
     8  
     9  /*
    10  #include <stdio.h>
    11  
    12  // Global function pointer to a dynamically-linked libc function.
    13  // When compiled to a Mach-O object, this produces a RELOC_UNSIGNED
    14  // relocation targeting the external symbol _puts.
    15  int (*_cgo_test_dynref_puts)(const char *) = puts;
    16  
    17  static int cgo_call_dynref(void) {
    18  	if (_cgo_test_dynref_puts == 0) {
    19  		return -1;
    20  	}
    21  	// Call the resolved function pointer. puts returns a non-negative
    22  	// value on success.
    23  	return _cgo_test_dynref_puts("cgo unsigned reloc test");
    24  }
    25  */
    26  import "C"
    27  
    28  import "testing"
    29  
    30  // unsignedRelocDynimport verifies that the Go internal linker correctly
    31  // handles Mach-O UNSIGNED relocations targeting dynamic import symbols.
    32  // The C preamble above contains a global function pointer initialized
    33  // to puts, which produces a RELOC_UNSIGNED relocation to the external
    34  // symbol _puts. If the linker can't handle this, the test binary
    35  // won't link at all.
    36  func unsignedRelocDynimport(t *testing.T) {
    37  	got := C.cgo_call_dynref()
    38  	if got < 0 {
    39  		t.Fatal("C function pointer to puts not resolved")
    40  	}
    41  }
    42  

View as plain text