github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/cmd/cgo/doc.go (about)

     1  // Copyright 2009 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  /*
     6  
     7  Cgo enables the creation of Go packages that call C code.
     8  
     9  Using cgo with the go command
    10  
    11  To use cgo write normal Go code that imports a pseudo-package "C".
    12  The Go code can then refer to types such as C.size_t, variables such
    13  as C.stdout, or functions such as C.putchar.
    14  
    15  If the import of "C" is immediately preceded by a comment, that
    16  comment, called the preamble, is used as a header when compiling
    17  the C parts of the package. For example:
    18  
    19  	// #include <stdio.h>
    20  	// #include <errno.h>
    21  	import "C"
    22  
    23  The preamble may contain any C code, including function and variable
    24  declarations and definitions. These may then be referred to from Go
    25  code as though they were defined in the package "C". All names
    26  declared in the preamble may be used, even if they start with a
    27  lower-case letter. Exception: static variables in the preamble may
    28  not be referenced from Go code; static functions are permitted.
    29  
    30  See $GOROOT/misc/cgo/stdio and $GOROOT/misc/cgo/gmp for examples. See
    31  "C? Go? Cgo!" for an introduction to using cgo:
    32  https://golang.org/doc/articles/c_go_cgo.html.
    33  
    34  CFLAGS, CPPFLAGS, CXXFLAGS, FFLAGS and LDFLAGS may be defined with pseudo
    35  #cgo directives within these comments to tweak the behavior of the C, C++
    36  or Fortran compiler. Values defined in multiple directives are concatenated
    37  together. The directive can include a list of build constraints limiting its
    38  effect to systems satisfying one of the constraints
    39  (see https://golang.org/pkg/go/build/#hdr-Build_Constraints for details about the constraint syntax).
    40  For example:
    41  
    42  	// #cgo CFLAGS: -DPNG_DEBUG=1
    43  	// #cgo amd64 386 CFLAGS: -DX86=1
    44  	// #cgo LDFLAGS: -lpng
    45  	// #include <png.h>
    46  	import "C"
    47  
    48  Alternatively, CPPFLAGS and LDFLAGS may be obtained via the pkg-config
    49  tool using a '#cgo pkg-config:' directive followed by the package names.
    50  For example:
    51  
    52  	// #cgo pkg-config: png cairo
    53  	// #include <png.h>
    54  	import "C"
    55  
    56  The default pkg-config tool may be changed by setting the PKG_CONFIG environment variable.
    57  
    58  When building, the CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS, CGO_FFLAGS and
    59  CGO_LDFLAGS environment variables are added to the flags derived from
    60  these directives. Package-specific flags should be set using the
    61  directives, not the environment variables, so that builds work in
    62  unmodified environments.
    63  
    64  All the cgo CPPFLAGS and CFLAGS directives in a package are concatenated and
    65  used to compile C files in that package. All the CPPFLAGS and CXXFLAGS
    66  directives in a package are concatenated and used to compile C++ files in that
    67  package. All the CPPFLAGS and FFLAGS directives in a package are concatenated
    68  and used to compile Fortran files in that package. All the LDFLAGS directives
    69  in any package in the program are concatenated and used at link time. All the
    70  pkg-config directives are concatenated and sent to pkg-config simultaneously
    71  to add to each appropriate set of command-line flags.
    72  
    73  When the cgo directives are parsed, any occurrence of the string ${SRCDIR}
    74  will be replaced by the absolute path to the directory containing the source
    75  file. This allows pre-compiled static libraries to be included in the package
    76  directory and linked properly.
    77  For example if package foo is in the directory /go/src/foo:
    78  
    79         // #cgo LDFLAGS: -L${SRCDIR}/libs -lfoo
    80  
    81  Will be expanded to:
    82  
    83         // #cgo LDFLAGS: -L/go/src/foo/libs -lfoo
    84  
    85  When the Go tool sees that one or more Go files use the special import
    86  "C", it will look for other non-Go files in the directory and compile
    87  them as part of the Go package. Any .c, .s, or .S files will be
    88  compiled with the C compiler. Any .cc, .cpp, or .cxx files will be
    89  compiled with the C++ compiler. Any .f, .F, .for or .f90 files will be
    90  compiled with the fortran compiler. Any .h, .hh, .hpp, or .hxx files will
    91  not be compiled separately, but, if these header files are changed,
    92  the C and C++ files will be recompiled. The default C and C++
    93  compilers may be changed by the CC and CXX environment variables,
    94  respectively; those environment variables may include command line
    95  options.
    96  
    97  The cgo tool is enabled by default for native builds on systems where
    98  it is expected to work. It is disabled by default when
    99  cross-compiling. You can control this by setting the CGO_ENABLED
   100  environment variable when running the go tool: set it to 1 to enable
   101  the use of cgo, and to 0 to disable it. The go tool will set the
   102  build constraint "cgo" if cgo is enabled.
   103  
   104  When cross-compiling, you must specify a C cross-compiler for cgo to
   105  use. You can do this by setting the generic CC_FOR_TARGET or the
   106  more specific CC_FOR_${GOOS}_${GOARCH} (for example, CC_FOR_linux_arm)
   107  environment variable when building the toolchain using make.bash,
   108  or you can set the CC environment variable any time you run the go tool.
   109  
   110  The CXX_FOR_TARGET, CXX_FOR_${GOOS}_${GOARCH}, and CXX
   111  environment variables work in a similar way for C++ code.
   112  
   113  Go references to C
   114  
   115  Within the Go file, C's struct field names that are keywords in Go
   116  can be accessed by prefixing them with an underscore: if x points at a C
   117  struct with a field named "type", x._type accesses the field.
   118  C struct fields that cannot be expressed in Go, such as bit fields
   119  or misaligned data, are omitted in the Go struct, replaced by
   120  appropriate padding to reach the next field or the end of the struct.
   121  
   122  The standard C numeric types are available under the names
   123  C.char, C.schar (signed char), C.uchar (unsigned char),
   124  C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int),
   125  C.long, C.ulong (unsigned long), C.longlong (long long),
   126  C.ulonglong (unsigned long long), C.float, C.double,
   127  C.complexfloat (complex float), and C.complexdouble (complex double).
   128  The C type void* is represented by Go's unsafe.Pointer.
   129  The C types __int128_t and __uint128_t are represented by [16]byte.
   130  
   131  To access a struct, union, or enum type directly, prefix it with
   132  struct_, union_, or enum_, as in C.struct_stat.
   133  
   134  The size of any C type T is available as C.sizeof_T, as in
   135  C.sizeof_struct_stat.
   136  
   137  As Go doesn't have support for C's union type in the general case,
   138  C's union types are represented as a Go byte array with the same length.
   139  
   140  Go structs cannot embed fields with C types.
   141  
   142  Go code cannot refer to zero-sized fields that occur at the end of
   143  non-empty C structs. To get the address of such a field (which is the
   144  only operation you can do with a zero-sized field) you must take the
   145  address of the struct and add the size of the struct.
   146  
   147  Cgo translates C types into equivalent unexported Go types.
   148  Because the translations are unexported, a Go package should not
   149  expose C types in its exported API: a C type used in one Go package
   150  is different from the same C type used in another.
   151  
   152  Any C function (even void functions) may be called in a multiple
   153  assignment context to retrieve both the return value (if any) and the
   154  C errno variable as an error (use _ to skip the result value if the
   155  function returns void). For example:
   156  
   157  	n, err = C.sqrt(-1)
   158  	_, err := C.voidFunc()
   159  	var n, err = C.sqrt(1)
   160  
   161  Calling C function pointers is currently not supported, however you can
   162  declare Go variables which hold C function pointers and pass them
   163  back and forth between Go and C. C code may call function pointers
   164  received from Go. For example:
   165  
   166  	package main
   167  
   168  	// typedef int (*intFunc) ();
   169  	//
   170  	// int
   171  	// bridge_int_func(intFunc f)
   172  	// {
   173  	//		return f();
   174  	// }
   175  	//
   176  	// int fortytwo()
   177  	// {
   178  	//	    return 42;
   179  	// }
   180  	import "C"
   181  	import "fmt"
   182  
   183  	func main() {
   184  		f := C.intFunc(C.fortytwo)
   185  		fmt.Println(int(C.bridge_int_func(f)))
   186  		// Output: 42
   187  	}
   188  
   189  In C, a function argument written as a fixed size array
   190  actually requires a pointer to the first element of the array.
   191  C compilers are aware of this calling convention and adjust
   192  the call accordingly, but Go cannot. In Go, you must pass
   193  the pointer to the first element explicitly: C.f(&C.x[0]).
   194  
   195  A few special functions convert between Go and C types
   196  by making copies of the data. In pseudo-Go definitions:
   197  
   198  	// Go string to C string
   199  	// The C string is allocated in the C heap using malloc.
   200  	// It is the caller's responsibility to arrange for it to be
   201  	// freed, such as by calling C.free (be sure to include stdlib.h
   202  	// if C.free is needed).
   203  	func C.CString(string) *C.char
   204  
   205  	// Go []byte slice to C array
   206  	// The C array is allocated in the C heap using malloc.
   207  	// It is the caller's responsibility to arrange for it to be
   208  	// freed, such as by calling C.free (be sure to include stdlib.h
   209  	// if C.free is needed).
   210  	func C.CBytes([]byte) unsafe.Pointer
   211  
   212  	// C string to Go string
   213  	func C.GoString(*C.char) string
   214  
   215  	// C data with explicit length to Go string
   216  	func C.GoStringN(*C.char, C.int) string
   217  
   218  	// C data with explicit length to Go []byte
   219  	func C.GoBytes(unsafe.Pointer, C.int) []byte
   220  
   221  As a special case, C.malloc does not call the C library malloc directly
   222  but instead calls a Go helper function that wraps the C library malloc
   223  but guarantees never to return nil. If C's malloc indicates out of memory,
   224  the helper function crashes the program, like when Go itself runs out
   225  of memory. Because C.malloc cannot fail, it has no two-result form
   226  that returns errno.
   227  
   228  C references to Go
   229  
   230  Go functions can be exported for use by C code in the following way:
   231  
   232  	//export MyFunction
   233  	func MyFunction(arg1, arg2 int, arg3 string) int64 {...}
   234  
   235  	//export MyFunction2
   236  	func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
   237  
   238  They will be available in the C code as:
   239  
   240  	extern int64 MyFunction(int arg1, int arg2, GoString arg3);
   241  	extern struct MyFunction2_return MyFunction2(int arg1, int arg2, GoString arg3);
   242  
   243  found in the _cgo_export.h generated header, after any preambles
   244  copied from the cgo input files. Functions with multiple
   245  return values are mapped to functions returning a struct.
   246  
   247  Not all Go types can be mapped to C types in a useful way.
   248  Go struct types are not supported; use a C struct type.
   249  Go array types are not supported; use a C pointer.
   250  
   251  Using //export in a file places a restriction on the preamble:
   252  since it is copied into two different C output files, it must not
   253  contain any definitions, only declarations. If a file contains both
   254  definitions and declarations, then the two output files will produce
   255  duplicate symbols and the linker will fail. To avoid this, definitions
   256  must be placed in preambles in other files, or in C source files.
   257  
   258  Passing pointers
   259  
   260  Go is a garbage collected language, and the garbage collector needs to
   261  know the location of every pointer to Go memory. Because of this,
   262  there are restrictions on passing pointers between Go and C.
   263  
   264  In this section the term Go pointer means a pointer to memory
   265  allocated by Go (such as by using the & operator or calling the
   266  predefined new function) and the term C pointer means a pointer to
   267  memory allocated by C (such as by a call to C.malloc). Whether a
   268  pointer is a Go pointer or a C pointer is a dynamic property
   269  determined by how the memory was allocated; it has nothing to do with
   270  the type of the pointer.
   271  
   272  Go code may pass a Go pointer to C provided the Go memory to which it
   273  points does not contain any Go pointers. The C code must preserve
   274  this property: it must not store any Go pointers in Go memory, even
   275  temporarily. When passing a pointer to a field in a struct, the Go
   276  memory in question is the memory occupied by the field, not the entire
   277  struct. When passing a pointer to an element in an array or slice,
   278  the Go memory in question is the entire array or the entire backing
   279  array of the slice.
   280  
   281  C code may not keep a copy of a Go pointer after the call returns.
   282  
   283  A Go function called by C code may not return a Go pointer. A Go
   284  function called by C code may take C pointers as arguments, and it may
   285  store non-pointer or C pointer data through those pointers, but it may
   286  not store a Go pointer in memory pointed to by a C pointer. A Go
   287  function called by C code may take a Go pointer as an argument, but it
   288  must preserve the property that the Go memory to which it points does
   289  not contain any Go pointers.
   290  
   291  Go code may not store a Go pointer in C memory. C code may store Go
   292  pointers in C memory, subject to the rule above: it must stop storing
   293  the Go pointer when the C function returns.
   294  
   295  These rules are checked dynamically at runtime. The checking is
   296  controlled by the cgocheck setting of the GODEBUG environment
   297  variable. The default setting is GODEBUG=cgocheck=1, which implements
   298  reasonably cheap dynamic checks. These checks may be disabled
   299  entirely using GODEBUG=cgocheck=0. Complete checking of pointer
   300  handling, at some cost in run time, is available via GODEBUG=cgocheck=2.
   301  
   302  It is possible to defeat this enforcement by using the unsafe package,
   303  and of course there is nothing stopping the C code from doing anything
   304  it likes. However, programs that break these rules are likely to fail
   305  in unexpected and unpredictable ways.
   306  
   307  Using cgo directly
   308  
   309  Usage:
   310  	go tool cgo [cgo options] [-- compiler options] gofiles...
   311  
   312  Cgo transforms the specified input Go source files into several output
   313  Go and C source files.
   314  
   315  The compiler options are passed through uninterpreted when
   316  invoking the C compiler to compile the C parts of the package.
   317  
   318  The following options are available when running cgo directly:
   319  
   320  	-V
   321  		Print cgo version and exit.
   322  	-debug-define
   323  		Debugging option. Print #defines.
   324  	-debug-gcc
   325  		Debugging option. Trace C compiler execution and output.
   326  	-dynimport file
   327  		Write list of symbols imported by file. Write to
   328  		-dynout argument or to standard output. Used by go
   329  		build when building a cgo package.
   330  	-dynlinker
   331  		Write dynamic linker as part of -dynimport output.
   332  	-dynout file
   333  		Write -dynimport output to file.
   334  	-dynpackage package
   335  		Set Go package for -dynimport output.
   336  	-exportheader file
   337  		If there are any exported functions, write the
   338  		generated export declarations to file.
   339  		C code can #include this to see the declarations.
   340  	-importpath string
   341  		The import path for the Go package. Optional; used for
   342  		nicer comments in the generated files.
   343  	-import_runtime_cgo
   344  		If set (which it is by default) import runtime/cgo in
   345  		generated output.
   346  	-import_syscall
   347  		If set (which it is by default) import syscall in
   348  		generated output.
   349  	-gccgo
   350  		Generate output for the gccgo compiler rather than the
   351  		gc compiler.
   352  	-gccgoprefix prefix
   353  		The -fgo-prefix option to be used with gccgo.
   354  	-gccgopkgpath path
   355  		The -fgo-pkgpath option to be used with gccgo.
   356  	-godefs
   357  		Write out input file in Go syntax replacing C package
   358  		names with real values. Used to generate files in the
   359  		syscall package when bootstrapping a new target.
   360  	-objdir directory
   361  		Put all generated files in directory.
   362  	-srcdir directory
   363  */
   364  package main
   365  
   366  /*
   367  Implementation details.
   368  
   369  Cgo provides a way for Go programs to call C code linked into the same
   370  address space. This comment explains the operation of cgo.
   371  
   372  Cgo reads a set of Go source files and looks for statements saying
   373  import "C". If the import has a doc comment, that comment is
   374  taken as literal C code to be used as a preamble to any C code
   375  generated by cgo. A typical preamble #includes necessary definitions:
   376  
   377  	// #include <stdio.h>
   378  	import "C"
   379  
   380  For more details about the usage of cgo, see the documentation
   381  comment at the top of this file.
   382  
   383  Understanding C
   384  
   385  Cgo scans the Go source files that import "C" for uses of that
   386  package, such as C.puts. It collects all such identifiers. The next
   387  step is to determine each kind of name. In C.xxx the xxx might refer
   388  to a type, a function, a constant, or a global variable. Cgo must
   389  decide which.
   390  
   391  The obvious thing for cgo to do is to process the preamble, expanding
   392  #includes and processing the corresponding C code. That would require
   393  a full C parser and type checker that was also aware of any extensions
   394  known to the system compiler (for example, all the GNU C extensions) as
   395  well as the system-specific header locations and system-specific
   396  pre-#defined macros. This is certainly possible to do, but it is an
   397  enormous amount of work.
   398  
   399  Cgo takes a different approach. It determines the meaning of C
   400  identifiers not by parsing C code but by feeding carefully constructed
   401  programs into the system C compiler and interpreting the generated
   402  error messages, debug information, and object files. In practice,
   403  parsing these is significantly less work and more robust than parsing
   404  C source.
   405  
   406  Cgo first invokes gcc -E -dM on the preamble, in order to find out
   407  about simple #defines for constants and the like. These are recorded
   408  for later use.
   409  
   410  Next, cgo needs to identify the kinds for each identifier. For the
   411  identifiers C.foo, cgo generates this C program:
   412  
   413  	<preamble>
   414  	#line 1 "not-declared"
   415  	void __cgo_f_1_1(void) { __typeof__(foo) *__cgo_undefined__1; }
   416  	#line 1 "not-type"
   417  	void __cgo_f_1_2(void) { foo *__cgo_undefined__2; }
   418  	#line 1 "not-int-const"
   419  	void __cgo_f_1_3(void) { enum { __cgo_undefined__3 = (foo)*1 }; }
   420  	#line 1 "not-num-const"
   421  	void __cgo_f_1_4(void) { static const double __cgo_undefined__4 = (foo); }
   422  	#line 1 "not-str-lit"
   423  	void __cgo_f_1_5(void) { static const char __cgo_undefined__5[] = (foo); }
   424  
   425  This program will not compile, but cgo can use the presence or absence
   426  of an error message on a given line to deduce the information it
   427  needs. The program is syntactically valid regardless of whether each
   428  name is a type or an ordinary identifier, so there will be no syntax
   429  errors that might stop parsing early.
   430  
   431  An error on not-declared:1 indicates that foo is undeclared.
   432  An error on not-type:1 indicates that foo is not a type (if declared at all, it is an identifier).
   433  An error on not-int-const:1 indicates that foo is not an integer constant.
   434  An error on not-num-const:1 indicates that foo is not a number constant.
   435  An error on not-str-lit:1 indicates that foo is not a string literal.
   436  An error on not-signed-int-const:1 indicates that foo is not a signed integer constant.
   437  
   438  The line number specifies the name involved. In the example, 1 is foo.
   439  
   440  Next, cgo must learn the details of each type, variable, function, or
   441  constant. It can do this by reading object files. If cgo has decided
   442  that t1 is a type, v2 and v3 are variables or functions, and i4, i5
   443  are integer constants, u6 is an unsigned integer constant, and f7 and f8
   444  are float constants, and s9 and s10 are string constants, it generates:
   445  
   446  	<preamble>
   447  	__typeof__(t1) *__cgo__1;
   448  	__typeof__(v2) *__cgo__2;
   449  	__typeof__(v3) *__cgo__3;
   450  	__typeof__(i4) *__cgo__4;
   451  	enum { __cgo_enum__4 = i4 };
   452  	__typeof__(i5) *__cgo__5;
   453  	enum { __cgo_enum__5 = i5 };
   454  	__typeof__(u6) *__cgo__6;
   455  	enum { __cgo_enum__6 = u6 };
   456  	__typeof__(f7) *__cgo__7;
   457  	__typeof__(f8) *__cgo__8;
   458  	__typeof__(s9) *__cgo__9;
   459  	__typeof__(s10) *__cgo__10;
   460  
   461  	long long __cgodebug_ints[] = {
   462  		0, // t1
   463  		0, // v2
   464  		0, // v3
   465  		i4,
   466  		i5,
   467  		u6,
   468  		0, // f7
   469  		0, // f8
   470  		0, // s9
   471  		0, // s10
   472  		1
   473  	};
   474  
   475  	double __cgodebug_floats[] = {
   476  		0, // t1
   477  		0, // v2
   478  		0, // v3
   479  		0, // i4
   480  		0, // i5
   481  		0, // u6
   482  		f7,
   483  		f8,
   484  		0, // s9
   485  		0, // s10
   486  		1
   487  	};
   488  
   489  	const char __cgodebug_str__9[] = s9;
   490  	const unsigned long long __cgodebug_strlen__9 = sizeof(s9)-1;
   491  	const char __cgodebug_str__10[] = s10;
   492  	const unsigned long long __cgodebug_strlen__10 = sizeof(s10)-1;
   493  
   494  and again invokes the system C compiler, to produce an object file
   495  containing debug information. Cgo parses the DWARF debug information
   496  for __cgo__N to learn the type of each identifier. (The types also
   497  distinguish functions from global variables.) Cgo reads the constant
   498  values from the __cgodebug_* from the object file's data segment.
   499  
   500  At this point cgo knows the meaning of each C.xxx well enough to start
   501  the translation process.
   502  
   503  Translating Go
   504  
   505  Given the input Go files x.go and y.go, cgo generates these source
   506  files:
   507  
   508  	x.cgo1.go       # for gc (cmd/compile)
   509  	y.cgo1.go       # for gc
   510  	_cgo_gotypes.go # for gc
   511  	_cgo_import.go  # for gc (if -dynout _cgo_import.go)
   512  	x.cgo2.c        # for gcc
   513  	y.cgo2.c        # for gcc
   514  	_cgo_defun.c    # for gcc (if -gccgo)
   515  	_cgo_export.c   # for gcc
   516  	_cgo_export.h   # for gcc
   517  	_cgo_main.c     # for gcc
   518  	_cgo_flags      # for alternative build tools
   519  
   520  The file x.cgo1.go is a copy of x.go with the import "C" removed and
   521  references to C.xxx replaced with names like _Cfunc_xxx or _Ctype_xxx.
   522  The definitions of those identifiers, written as Go functions, types,
   523  or variables, are provided in _cgo_gotypes.go.
   524  
   525  Here is a _cgo_gotypes.go containing definitions for needed C types:
   526  
   527  	type _Ctype_char int8
   528  	type _Ctype_int int32
   529  	type _Ctype_void [0]byte
   530  
   531  The _cgo_gotypes.go file also contains the definitions of the
   532  functions. They all have similar bodies that invoke runtime·cgocall
   533  to make a switch from the Go runtime world to the system C (GCC-based)
   534  world.
   535  
   536  For example, here is the definition of _Cfunc_puts:
   537  
   538  	//go:cgo_import_static _cgo_be59f0f25121_Cfunc_puts
   539  	//go:linkname __cgofn__cgo_be59f0f25121_Cfunc_puts _cgo_be59f0f25121_Cfunc_puts
   540  	var __cgofn__cgo_be59f0f25121_Cfunc_puts byte
   541  	var _cgo_be59f0f25121_Cfunc_puts = unsafe.Pointer(&__cgofn__cgo_be59f0f25121_Cfunc_puts)
   542  
   543  	func _Cfunc_puts(p0 *_Ctype_char) (r1 _Ctype_int) {
   544  		_cgo_runtime_cgocall(_cgo_be59f0f25121_Cfunc_puts, uintptr(unsafe.Pointer(&p0)))
   545  		return
   546  	}
   547  
   548  The hexadecimal number is a hash of cgo's input, chosen to be
   549  deterministic yet unlikely to collide with other uses. The actual
   550  function _cgo_be59f0f25121_Cfunc_puts is implemented in a C source
   551  file compiled by gcc, the file x.cgo2.c:
   552  
   553  	void
   554  	_cgo_be59f0f25121_Cfunc_puts(void *v)
   555  	{
   556  		struct {
   557  			char* p0;
   558  			int r;
   559  			char __pad12[4];
   560  		} __attribute__((__packed__, __gcc_struct__)) *a = v;
   561  		a->r = puts((void*)a->p0);
   562  	}
   563  
   564  It extracts the arguments from the pointer to _Cfunc_puts's argument
   565  frame, invokes the system C function (in this case, puts), stores the
   566  result in the frame, and returns.
   567  
   568  Linking
   569  
   570  Once the _cgo_export.c and *.cgo2.c files have been compiled with gcc,
   571  they need to be linked into the final binary, along with the libraries
   572  they might depend on (in the case of puts, stdio). cmd/link has been
   573  extended to understand basic ELF files, but it does not understand ELF
   574  in the full complexity that modern C libraries embrace, so it cannot
   575  in general generate direct references to the system libraries.
   576  
   577  Instead, the build process generates an object file using dynamic
   578  linkage to the desired libraries. The main function is provided by
   579  _cgo_main.c:
   580  
   581  	int main() { return 0; }
   582  	void crosscall2(void(*fn)(void*, int, uintptr_t), void *a, int c, uintptr_t ctxt) { }
   583  	uintptr_t _cgo_wait_runtime_init_done() { return 0; }
   584  	void _cgo_release_context(uintptr_t ctxt) { }
   585  	char* _cgo_topofstack(void) { return (char*)0; }
   586  	void _cgo_allocate(void *a, int c) { }
   587  	void _cgo_panic(void *a, int c) { }
   588  	void _cgo_reginit(void) { }
   589  
   590  The extra functions here are stubs to satisfy the references in the C
   591  code generated for gcc. The build process links this stub, along with
   592  _cgo_export.c and *.cgo2.c, into a dynamic executable and then lets
   593  cgo examine the executable. Cgo records the list of shared library
   594  references and resolved names and writes them into a new file
   595  _cgo_import.go, which looks like:
   596  
   597  	//go:cgo_dynamic_linker "/lib64/ld-linux-x86-64.so.2"
   598  	//go:cgo_import_dynamic puts puts#GLIBC_2.2.5 "libc.so.6"
   599  	//go:cgo_import_dynamic __libc_start_main __libc_start_main#GLIBC_2.2.5 "libc.so.6"
   600  	//go:cgo_import_dynamic stdout stdout#GLIBC_2.2.5 "libc.so.6"
   601  	//go:cgo_import_dynamic fflush fflush#GLIBC_2.2.5 "libc.so.6"
   602  	//go:cgo_import_dynamic _ _ "libpthread.so.0"
   603  	//go:cgo_import_dynamic _ _ "libc.so.6"
   604  
   605  In the end, the compiled Go package, which will eventually be
   606  presented to cmd/link as part of a larger program, contains:
   607  
   608  	_go_.o        # gc-compiled object for _cgo_gotypes.go, _cgo_import.go, *.cgo1.go
   609  	_all.o        # gcc-compiled object for _cgo_export.c, *.cgo2.c
   610  
   611  The final program will be a dynamic executable, so that cmd/link can avoid
   612  needing to process arbitrary .o files. It only needs to process the .o
   613  files generated from C files that cgo writes, and those are much more
   614  limited in the ELF or other features that they use.
   615  
   616  In essence, the _cgo_import.o file includes the extra linking
   617  directives that cmd/link is not sophisticated enough to derive from _all.o
   618  on its own. Similarly, the _all.o uses dynamic references to real
   619  system object code because cmd/link is not sophisticated enough to process
   620  the real code.
   621  
   622  The main benefits of this system are that cmd/link remains relatively simple
   623  (it does not need to implement a complete ELF and Mach-O linker) and
   624  that gcc is not needed after the package is compiled. For example,
   625  package net uses cgo for access to name resolution functions provided
   626  by libc. Although gcc is needed to compile package net, gcc is not
   627  needed to link programs that import package net.
   628  
   629  Runtime
   630  
   631  When using cgo, Go must not assume that it owns all details of the
   632  process. In particular it needs to coordinate with C in the use of
   633  threads and thread-local storage. The runtime package declares a few
   634  variables:
   635  
   636  	var (
   637  		iscgo             bool
   638  		_cgo_init         unsafe.Pointer
   639  		_cgo_thread_start unsafe.Pointer
   640  	)
   641  
   642  Any package using cgo imports "runtime/cgo", which provides
   643  initializations for these variables. It sets iscgo to true, _cgo_init
   644  to a gcc-compiled function that can be called early during program
   645  startup, and _cgo_thread_start to a gcc-compiled function that can be
   646  used to create a new thread, in place of the runtime's usual direct
   647  system calls.
   648  
   649  Internal and External Linking
   650  
   651  The text above describes "internal" linking, in which cmd/link parses and
   652  links host object files (ELF, Mach-O, PE, and so on) into the final
   653  executable itself. Keeping cmd/link simple means we cannot possibly
   654  implement the full semantics of the host linker, so the kinds of
   655  objects that can be linked directly into the binary is limited (other
   656  code can only be used as a dynamic library). On the other hand, when
   657  using internal linking, cmd/link can generate Go binaries by itself.
   658  
   659  In order to allow linking arbitrary object files without requiring
   660  dynamic libraries, cgo supports an "external" linking mode too. In
   661  external linking mode, cmd/link does not process any host object files.
   662  Instead, it collects all the Go code and writes a single go.o object
   663  file containing it. Then it invokes the host linker (usually gcc) to
   664  combine the go.o object file and any supporting non-Go code into a
   665  final executable. External linking avoids the dynamic library
   666  requirement but introduces a requirement that the host linker be
   667  present to create such a binary.
   668  
   669  Most builds both compile source code and invoke the linker to create a
   670  binary. When cgo is involved, the compile step already requires gcc, so
   671  it is not problematic for the link step to require gcc too.
   672  
   673  An important exception is builds using a pre-compiled copy of the
   674  standard library. In particular, package net uses cgo on most systems,
   675  and we want to preserve the ability to compile pure Go code that
   676  imports net without requiring gcc to be present at link time. (In this
   677  case, the dynamic library requirement is less significant, because the
   678  only library involved is libc.so, which can usually be assumed
   679  present.)
   680  
   681  This conflict between functionality and the gcc requirement means we
   682  must support both internal and external linking, depending on the
   683  circumstances: if net is the only cgo-using package, then internal
   684  linking is probably fine, but if other packages are involved, so that there
   685  are dependencies on libraries beyond libc, external linking is likely
   686  to work better. The compilation of a package records the relevant
   687  information to support both linking modes, leaving the decision
   688  to be made when linking the final binary.
   689  
   690  Linking Directives
   691  
   692  In either linking mode, package-specific directives must be passed
   693  through to cmd/link. These are communicated by writing //go: directives in a
   694  Go source file compiled by gc. The directives are copied into the .o
   695  object file and then processed by the linker.
   696  
   697  The directives are:
   698  
   699  //go:cgo_import_dynamic <local> [<remote> ["<library>"]]
   700  
   701  	In internal linking mode, allow an unresolved reference to
   702  	<local>, assuming it will be resolved by a dynamic library
   703  	symbol. The optional <remote> specifies the symbol's name and
   704  	possibly version in the dynamic library, and the optional "<library>"
   705  	names the specific library where the symbol should be found.
   706  
   707  	In the <remote>, # or @ can be used to introduce a symbol version.
   708  
   709  	Examples:
   710  	//go:cgo_import_dynamic puts
   711  	//go:cgo_import_dynamic puts puts#GLIBC_2.2.5
   712  	//go:cgo_import_dynamic puts puts#GLIBC_2.2.5 "libc.so.6"
   713  
   714  	A side effect of the cgo_import_dynamic directive with a
   715  	library is to make the final binary depend on that dynamic
   716  	library. To get the dependency without importing any specific
   717  	symbols, use _ for local and remote.
   718  
   719  	Example:
   720  	//go:cgo_import_dynamic _ _ "libc.so.6"
   721  
   722  	For compatibility with current versions of SWIG,
   723  	#pragma dynimport is an alias for //go:cgo_import_dynamic.
   724  
   725  //go:cgo_dynamic_linker "<path>"
   726  
   727  	In internal linking mode, use "<path>" as the dynamic linker
   728  	in the final binary. This directive is only needed from one
   729  	package when constructing a binary; by convention it is
   730  	supplied by runtime/cgo.
   731  
   732  	Example:
   733  	//go:cgo_dynamic_linker "/lib/ld-linux.so.2"
   734  
   735  //go:cgo_export_dynamic <local> <remote>
   736  
   737  	In internal linking mode, put the Go symbol
   738  	named <local> into the program's exported symbol table as
   739  	<remote>, so that C code can refer to it by that name. This
   740  	mechanism makes it possible for C code to call back into Go or
   741  	to share Go's data.
   742  
   743  	For compatibility with current versions of SWIG,
   744  	#pragma dynexport is an alias for //go:cgo_export_dynamic.
   745  
   746  //go:cgo_import_static <local>
   747  
   748  	In external linking mode, allow unresolved references to
   749  	<local> in the go.o object file prepared for the host linker,
   750  	under the assumption that <local> will be supplied by the
   751  	other object files that will be linked with go.o.
   752  
   753  	Example:
   754  	//go:cgo_import_static puts_wrapper
   755  
   756  //go:cgo_export_static <local> <remote>
   757  
   758  	In external linking mode, put the Go symbol
   759  	named <local> into the program's exported symbol table as
   760  	<remote>, so that C code can refer to it by that name. This
   761  	mechanism makes it possible for C code to call back into Go or
   762  	to share Go's data.
   763  
   764  //go:cgo_ldflag "<arg>"
   765  
   766  	In external linking mode, invoke the host linker (usually gcc)
   767  	with "<arg>" as a command-line argument following the .o files.
   768  	Note that the arguments are for "gcc", not "ld".
   769  
   770  	Example:
   771  	//go:cgo_ldflag "-lpthread"
   772  	//go:cgo_ldflag "-L/usr/local/sqlite3/lib"
   773  
   774  A package compiled with cgo will include directives for both
   775  internal and external linking; the linker will select the appropriate
   776  subset for the chosen linking mode.
   777  
   778  Example
   779  
   780  As a simple example, consider a package that uses cgo to call C.sin.
   781  The following code will be generated by cgo:
   782  
   783  	// compiled by gc
   784  
   785  	//go:cgo_ldflag "-lm"
   786  
   787  	type _Ctype_double float64
   788  
   789  	//go:cgo_import_static _cgo_gcc_Cfunc_sin
   790  	//go:linkname __cgo_gcc_Cfunc_sin _cgo_gcc_Cfunc_sin
   791  	var __cgo_gcc_Cfunc_sin byte
   792  	var _cgo_gcc_Cfunc_sin = unsafe.Pointer(&__cgo_gcc_Cfunc_sin)
   793  
   794  	func _Cfunc_sin(p0 _Ctype_double) (r1 _Ctype_double) {
   795  		_cgo_runtime_cgocall(_cgo_gcc_Cfunc_sin, uintptr(unsafe.Pointer(&p0)))
   796  		return
   797  	}
   798  
   799  	// compiled by gcc, into foo.cgo2.o
   800  
   801  	void
   802  	_cgo_gcc_Cfunc_sin(void *v)
   803  	{
   804  		struct {
   805  			double p0;
   806  			double r;
   807  		} __attribute__((__packed__)) *a = v;
   808  		a->r = sin(a->p0);
   809  	}
   810  
   811  What happens at link time depends on whether the final binary is linked
   812  using the internal or external mode. If other packages are compiled in
   813  "external only" mode, then the final link will be an external one.
   814  Otherwise the link will be an internal one.
   815  
   816  The linking directives are used according to the kind of final link
   817  used.
   818  
   819  In internal mode, cmd/link itself processes all the host object files, in
   820  particular foo.cgo2.o. To do so, it uses the cgo_import_dynamic and
   821  cgo_dynamic_linker directives to learn that the otherwise undefined
   822  reference to sin in foo.cgo2.o should be rewritten to refer to the
   823  symbol sin with version GLIBC_2.2.5 from the dynamic library
   824  "libm.so.6", and the binary should request "/lib/ld-linux.so.2" as its
   825  runtime dynamic linker.
   826  
   827  In external mode, cmd/link does not process any host object files, in
   828  particular foo.cgo2.o. It links together the gc-generated object
   829  files, along with any other Go code, into a go.o file. While doing
   830  that, cmd/link will discover that there is no definition for
   831  _cgo_gcc_Cfunc_sin, referred to by the gc-compiled source file. This
   832  is okay, because cmd/link also processes the cgo_import_static directive and
   833  knows that _cgo_gcc_Cfunc_sin is expected to be supplied by a host
   834  object file, so cmd/link does not treat the missing symbol as an error when
   835  creating go.o. Indeed, the definition for _cgo_gcc_Cfunc_sin will be
   836  provided to the host linker by foo2.cgo.o, which in turn will need the
   837  symbol 'sin'. cmd/link also processes the cgo_ldflag directives, so that it
   838  knows that the eventual host link command must include the -lm
   839  argument, so that the host linker will be able to find 'sin' in the
   840  math library.
   841  
   842  cmd/link Command Line Interface
   843  
   844  The go command and any other Go-aware build systems invoke cmd/link
   845  to link a collection of packages into a single binary. By default, cmd/link will
   846  present the same interface it does today:
   847  
   848  	cmd/link main.a
   849  
   850  produces a file named a.out, even if cmd/link does so by invoking the host
   851  linker in external linking mode.
   852  
   853  By default, cmd/link will decide the linking mode as follows: if the only
   854  packages using cgo are those on a whitelist of standard library
   855  packages (net, os/user, runtime/cgo), cmd/link will use internal linking
   856  mode. Otherwise, there are non-standard cgo packages involved, and cmd/link
   857  will use external linking mode. The first rule means that a build of
   858  the godoc binary, which uses net but no other cgo, can run without
   859  needing gcc available. The second rule means that a build of a
   860  cgo-wrapped library like sqlite3 can generate a standalone executable
   861  instead of needing to refer to a dynamic library. The specific choice
   862  can be overridden using a command line flag: cmd/link -linkmode=internal or
   863  cmd/link -linkmode=external.
   864  
   865  In an external link, cmd/link will create a temporary directory, write any
   866  host object files found in package archives to that directory (renamed
   867  to avoid conflicts), write the go.o file to that directory, and invoke
   868  the host linker. The default value for the host linker is $CC, split
   869  into fields, or else "gcc". The specific host linker command line can
   870  be overridden using command line flags: cmd/link -extld=clang
   871  -extldflags='-ggdb -O3'. If any package in a build includes a .cc or
   872  other file compiled by the C++ compiler, the go tool will use the
   873  -extld option to set the host linker to the C++ compiler.
   874  
   875  These defaults mean that Go-aware build systems can ignore the linking
   876  changes and keep running plain 'cmd/link' and get reasonable results, but
   877  they can also control the linking details if desired.
   878  
   879  */