github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/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  See $GOROOT/misc/cgo/stdio and $GOROOT/misc/cgo/gmp for examples.  See
    24  "C? Go? Cgo!" for an introduction to using cgo:
    25  http://golang.org/doc/articles/c_go_cgo.html.
    26  
    27  CFLAGS, CPPFLAGS, CXXFLAGS and LDFLAGS may be defined with pseudo #cgo
    28  directives within these comments to tweak the behavior of the C or C++
    29  compiler.  Values defined in multiple directives are concatenated
    30  together.  The directive can include a list of build constraints limiting its
    31  effect to systems satisfying one of the constraints
    32  (see http://golang.org/pkg/go/build/#hdr-Build_Constraints for details about the constraint syntax).
    33  For example:
    34  
    35  	// #cgo CFLAGS: -DPNG_DEBUG=1
    36  	// #cgo amd64 386 CFLAGS: -DX86=1
    37  	// #cgo LDFLAGS: -lpng
    38  	// #include <png.h>
    39  	import "C"
    40  
    41  Alternatively, CPPFLAGS and LDFLAGS may be obtained via the pkg-config
    42  tool using a '#cgo pkg-config:' directive followed by the package names.
    43  For example:
    44  
    45  	// #cgo pkg-config: png cairo
    46  	// #include <png.h>
    47  	import "C"
    48  
    49  When building, the CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS and
    50  CGO_LDFLAGS environment variables are added to the flags derived from
    51  these directives.  Package-specific flags should be set using the
    52  directives, not the environment variables, so that builds work in
    53  unmodified environments.
    54  
    55  When the Go tool sees that one or more Go files use the special import
    56  "C", it will look for other non-Go files in the directory and compile
    57  them as part of the Go package.  Any .c, .s, or .S files will be
    58  compiled with the C compiler.  Any .cc, .cpp, or .cxx files will be
    59  compiled with the C++ compiler.  Any .h, .hh, .hpp, or .hxx files will
    60  not be compiled separately, but, if these header files are changed,
    61  the C and C++ files will be recompiled.  The default C and C++
    62  compilers may be changed by the CC and CXX environment variables,
    63  respectively; those environment variables may include command line
    64  options.
    65  
    66  Go references to C
    67  
    68  Within the Go file, C's struct field names that are keywords in Go
    69  can be accessed by prefixing them with an underscore: if x points at a C
    70  struct with a field named "type", x._type accesses the field.
    71  
    72  The standard C numeric types are available under the names
    73  C.char, C.schar (signed char), C.uchar (unsigned char),
    74  C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int),
    75  C.long, C.ulong (unsigned long), C.longlong (long long),
    76  C.ulonglong (unsigned long long), C.float, C.double.
    77  The C type void* is represented by Go's unsafe.Pointer.
    78  
    79  To access a struct, union, or enum type directly, prefix it with
    80  struct_, union_, or enum_, as in C.struct_stat.
    81  
    82  As Go doesn't have support for C's union type in the general case,
    83  C's union types are represented as a Go byte array with the same length.
    84  
    85  Go structs cannot embed fields with C types.
    86  
    87  Any C function (even void functions) may be called in a multiple
    88  assignment context to retrieve both the return value (if any) and the
    89  C errno variable as an error (use _ to skip the result value if the
    90  function returns void).  For example:
    91  
    92  	n, err := C.sqrt(-1)
    93  	_, err := C.voidFunc()
    94  
    95  Calling C function pointers is currently not supported, however you can
    96  declare Go variables which hold C function pointers and pass them
    97  back and forth between Go and C. C code may call function pointers
    98  received from Go. For example:
    99  
   100  	package main
   101  
   102  	// typedef int (*intFunc) ();
   103  	//
   104  	// int
   105  	// bridge_int_func(intFunc f)
   106  	// {
   107  	//		return f();
   108  	// }
   109  	//
   110  	// int fortytwo()
   111  	// {
   112  	//	    return 42;
   113  	// }
   114  	import "C"
   115  	import "fmt"
   116  
   117  	func main() {
   118  		f := C.intFunc(C.fortytwo)
   119  		fmt.Println(int(C.bridge_int_func(f)))
   120  		// Output: 42
   121  	}
   122  
   123  In C, a function argument written as a fixed size array
   124  actually requires a pointer to the first element of the array.
   125  C compilers are aware of this calling convention and adjust
   126  the call accordingly, but Go cannot.  In Go, you must pass
   127  the pointer to the first element explicitly: C.f(&x[0]).
   128  
   129  A few special functions convert between Go and C types
   130  by making copies of the data.  In pseudo-Go definitions:
   131  
   132  	// Go string to C string
   133  	// The C string is allocated in the C heap using malloc.
   134  	// It is the caller's responsibility to arrange for it to be
   135  	// freed, such as by calling C.free (be sure to include stdlib.h
   136  	// if C.free is needed).
   137  	func C.CString(string) *C.char
   138  
   139  	// C string to Go string
   140  	func C.GoString(*C.char) string
   141  
   142  	// C string, length to Go string
   143  	func C.GoStringN(*C.char, C.int) string
   144  
   145  	// C pointer, length to Go []byte
   146  	func C.GoBytes(unsafe.Pointer, C.int) []byte
   147  
   148  C references to Go
   149  
   150  Go functions can be exported for use by C code in the following way:
   151  
   152  	//export MyFunction
   153  	func MyFunction(arg1, arg2 int, arg3 string) int64 {...}
   154  
   155  	//export MyFunction2
   156  	func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
   157  
   158  They will be available in the C code as:
   159  
   160  	extern int64 MyFunction(int arg1, int arg2, GoString arg3);
   161  	extern struct MyFunction2_return MyFunction2(int arg1, int arg2, GoString arg3);
   162  
   163  found in the _cgo_export.h generated header, after any preambles
   164  copied from the cgo input files. Functions with multiple
   165  return values are mapped to functions returning a struct.
   166  Not all Go types can be mapped to C types in a useful way.
   167  
   168  Using //export in a file places a restriction on the preamble:
   169  since it is copied into two different C output files, it must not
   170  contain any definitions, only declarations. Definitions must be
   171  placed in preambles in other files, or in C source files.
   172  
   173  Using cgo directly
   174  
   175  Usage:
   176  	go tool cgo [cgo options] [-- compiler options] file.go
   177  
   178  Cgo transforms the input file.go into four output files: two Go source
   179  files, a C file for 6c (or 8c or 5c), and a C file for gcc.
   180  
   181  The compiler options are passed through uninterpreted when
   182  invoking the C compiler to compile the C parts of the package.
   183  
   184  The following options are available when running cgo directly:
   185  
   186  	-dynimport file
   187  		Write list of symbols imported by file. Write to
   188  		-dynout argument or to standard output. Used by go
   189  		build when building a cgo package.
   190  	-dynout file
   191  		Write -dynimport output to file.
   192  	-dynlinker
   193  		Write dynamic linker as part of -dynimport output.
   194  	-godefs
   195  		Write out input file in Go syntax replacing C package
   196  		names with real values. Used to generate files in the
   197  		syscall package when bootstrapping a new target.
   198  	-cdefs
   199  		Like -godefs, but write file in C syntax.
   200  		Used to generate files in the runtime package when
   201  		bootstrapping a new target.
   202  	-objdir directory
   203  		Put all generated files in directory.
   204  	-gccgo
   205  		Generate output for the gccgo compiler rather than the
   206  		gc compiler.
   207  	-gccgoprefix prefix
   208  		The -fgo-prefix option to be used with gccgo.
   209  	-gccgopkgpath path
   210  		The -fgo-pkgpath option to be used with gccgo.
   211  	-import_runtime_cgo
   212  		If set (which it is by default) import runtime/cgo in
   213  		generated output.
   214  	-import_syscall
   215  		If set (which it is by default) import syscall in
   216  		generated output.
   217  	-debug-define
   218  		Debugging option. Print #defines.
   219  	-debug-gcc
   220  		Debugging option. Trace C compiler execution and output.
   221  */
   222  package main
   223  
   224  /*
   225  Implementation details.
   226  
   227  Cgo provides a way for Go programs to call C code linked into the same
   228  address space. This comment explains the operation of cgo.
   229  
   230  Cgo reads a set of Go source files and looks for statements saying
   231  import "C". If the import has a doc comment, that comment is
   232  taken as literal C code to be used as a preamble to any C code
   233  generated by cgo. A typical preamble #includes necessary definitions:
   234  
   235  	// #include <stdio.h>
   236  	import "C"
   237  
   238  For more details about the usage of cgo, see the documentation
   239  comment at the top of this file.
   240  
   241  Understanding C
   242  
   243  Cgo scans the Go source files that import "C" for uses of that
   244  package, such as C.puts. It collects all such identifiers. The next
   245  step is to determine each kind of name. In C.xxx the xxx might refer
   246  to a type, a function, a constant, or a global variable. Cgo must
   247  decide which.
   248  
   249  The obvious thing for cgo to do is to process the preamble, expanding
   250  #includes and processing the corresponding C code. That would require
   251  a full C parser and type checker that was also aware of any extensions
   252  known to the system compiler (for example, all the GNU C extensions) as
   253  well as the system-specific header locations and system-specific
   254  pre-#defined macros. This is certainly possible to do, but it is an
   255  enormous amount of work.
   256  
   257  Cgo takes a different approach. It determines the meaning of C
   258  identifiers not by parsing C code but by feeding carefully constructed
   259  programs into the system C compiler and interpreting the generated
   260  error messages, debug information, and object files. In practice,
   261  parsing these is significantly less work and more robust than parsing
   262  C source.
   263  
   264  Cgo first invokes gcc -E -dM on the preamble, in order to find out
   265  about simple #defines for constants and the like. These are recorded
   266  for later use.
   267  
   268  Next, cgo needs to identify the kinds for each identifier. For the
   269  identifiers C.foo and C.bar, cgo generates this C program:
   270  
   271  	<preamble>
   272  	#line 1 "not-declared"
   273  	void __cgo_f_xxx_1(void) { __typeof__(foo) *__cgo_undefined__; }
   274  	#line 1 "not-type"
   275  	void __cgo_f_xxx_2(void) { foo *__cgo_undefined__; }
   276  	#line 1 "not-const"
   277  	void __cgo_f_xxx_3(void) { enum { __cgo_undefined__ = (foo)*1 }; }
   278  	#line 2 "not-declared"
   279  	void __cgo_f_xxx_1(void) { __typeof__(bar) *__cgo_undefined__; }
   280  	#line 2 "not-type"
   281  	void __cgo_f_xxx_2(void) { bar *__cgo_undefined__; }
   282  	#line 2 "not-const"
   283  	void __cgo_f_xxx_3(void) { enum { __cgo_undefined__ = (bar)*1 }; }
   284  
   285  This program will not compile, but cgo can use the presence or absence
   286  of an error message on a given line to deduce the information it
   287  needs. The program is syntactically valid regardless of whether each
   288  name is a type or an ordinary identifier, so there will be no syntax
   289  errors that might stop parsing early.
   290  
   291  An error on not-declared:1 indicates that foo is undeclared.
   292  An error on not-type:1 indicates that foo is not a type (if declared at all, it is an identifier).
   293  An error on not-const:1 indicates that foo is not an integer constant.
   294  
   295  The line number specifies the name involved. In the example, 1 is foo and 2 is bar.
   296  
   297  Next, cgo must learn the details of each type, variable, function, or
   298  constant. It can do this by reading object files. If cgo has decided
   299  that t1 is a type, v2 and v3 are variables or functions, and c4, c5,
   300  and c6 are constants, it generates:
   301  
   302  	<preamble>
   303  	__typeof__(t1) *__cgo__1;
   304  	__typeof__(v2) *__cgo__2;
   305  	__typeof__(v3) *__cgo__3;
   306  	__typeof__(c4) *__cgo__4;
   307  	enum { __cgo_enum__4 = c4 };
   308  	__typeof__(c5) *__cgo__5;
   309  	enum { __cgo_enum__5 = c5 };
   310  	__typeof__(c6) *__cgo__6;
   311  	enum { __cgo_enum__6 = c6 };
   312  
   313  	long long __cgo_debug_data[] = {
   314  		0, // t1
   315  		0, // v2
   316  		0, // v3
   317  		c4,
   318  		c5,
   319  		c6,
   320  		1
   321  	};
   322  
   323  and again invokes the system C compiler, to produce an object file
   324  containing debug information. Cgo parses the DWARF debug information
   325  for __cgo__N to learn the type of each identifier. (The types also
   326  distinguish functions from global variables.) If using a standard gcc,
   327  cgo can parse the DWARF debug information for the __cgo_enum__N to
   328  learn the identifier's value. The LLVM-based gcc on OS X emits
   329  incomplete DWARF information for enums; in that case cgo reads the
   330  constant values from the __cgo_debug_data from the object file's data
   331  segment.
   332  
   333  At this point cgo knows the meaning of each C.xxx well enough to start
   334  the translation process.
   335  
   336  Translating Go
   337  
   338  [The rest of this comment refers to 6g and 6c, the Go and C compilers
   339  that are part of the amd64 port of the gc Go toolchain. Everything here
   340  applies to another architecture's compilers as well.]
   341  
   342  Given the input Go files x.go and y.go, cgo generates these source
   343  files:
   344  
   345  	x.cgo1.go       # for 6g
   346  	y.cgo1.go       # for 6g
   347  	_cgo_gotypes.go # for 6g
   348  	_cgo_defun.c    # for 6c
   349  	x.cgo2.c        # for gcc
   350  	y.cgo2.c        # for gcc
   351  	_cgo_export.c   # for gcc
   352  	_cgo_main.c     # for gcc
   353  
   354  The file x.cgo1.go is a copy of x.go with the import "C" removed and
   355  references to C.xxx replaced with names like _Cfunc_xxx or _Ctype_xxx.
   356  The definitions of those identifiers, written as Go functions, types,
   357  or variables, are provided in _cgo_gotypes.go.
   358  
   359  Here is a _cgo_gotypes.go containing definitions for C.flush (provided
   360  in the preamble) and C.puts (from stdio):
   361  
   362  	type _Ctype_char int8
   363  	type _Ctype_int int32
   364  	type _Ctype_void [0]byte
   365  
   366  	func _Cfunc_CString(string) *_Ctype_char
   367  	func _Cfunc_flush() _Ctype_void
   368  	func _Cfunc_puts(*_Ctype_char) _Ctype_int
   369  
   370  For functions, cgo only writes an external declaration in the Go
   371  output. The implementation is in a combination of C for 6c (meaning
   372  any gc-toolchain compiler) and C for gcc.
   373  
   374  The 6c file contains the definitions of the functions. They all have
   375  similar bodies that invoke runtime·cgocall to make a switch from the
   376  Go runtime world to the system C (GCC-based) world.
   377  
   378  For example, here is the definition of _Cfunc_puts:
   379  
   380  	void _cgo_be59f0f25121_Cfunc_puts(void*);
   381  
   382  	void
   383  	·_Cfunc_puts(struct{uint8 x[1];}p)
   384  	{
   385  		runtime·cgocall(_cgo_be59f0f25121_Cfunc_puts, &p);
   386  	}
   387  
   388  The hexadecimal number is a hash of cgo's input, chosen to be
   389  deterministic yet unlikely to collide with other uses. The actual
   390  function _cgo_be59f0f25121_Cfunc_puts is implemented in a C source
   391  file compiled by gcc, the file x.cgo2.c:
   392  
   393  	void
   394  	_cgo_be59f0f25121_Cfunc_puts(void *v)
   395  	{
   396  		struct {
   397  			char* p0;
   398  			int r;
   399  			char __pad12[4];
   400  		} __attribute__((__packed__, __gcc_struct__)) *a = v;
   401  		a->r = puts((void*)a->p0);
   402  	}
   403  
   404  It extracts the arguments from the pointer to _Cfunc_puts's argument
   405  frame, invokes the system C function (in this case, puts), stores the
   406  result in the frame, and returns.
   407  
   408  Linking
   409  
   410  Once the _cgo_export.c and *.cgo2.c files have been compiled with gcc,
   411  they need to be linked into the final binary, along with the libraries
   412  they might depend on (in the case of puts, stdio). 6l has been
   413  extended to understand basic ELF files, but it does not understand ELF
   414  in the full complexity that modern C libraries embrace, so it cannot
   415  in general generate direct references to the system libraries.
   416  
   417  Instead, the build process generates an object file using dynamic
   418  linkage to the desired libraries. The main function is provided by
   419  _cgo_main.c:
   420  
   421  	int main() { return 0; }
   422  	void crosscall2(void(*fn)(void*, int), void *a, int c) { }
   423  	void _cgo_allocate(void *a, int c) { }
   424  	void _cgo_panic(void *a, int c) { }
   425  
   426  The extra functions here are stubs to satisfy the references in the C
   427  code generated for gcc. The build process links this stub, along with
   428  _cgo_export.c and *.cgo2.c, into a dynamic executable and then lets
   429  cgo examine the executable. Cgo records the list of shared library
   430  references and resolved names and writes them into a new file
   431  _cgo_import.c, which looks like:
   432  
   433  	#pragma cgo_dynamic_linker "/lib64/ld-linux-x86-64.so.2"
   434  	#pragma cgo_import_dynamic puts puts#GLIBC_2.2.5 "libc.so.6"
   435  	#pragma cgo_import_dynamic __libc_start_main __libc_start_main#GLIBC_2.2.5 "libc.so.6"
   436  	#pragma cgo_import_dynamic stdout stdout#GLIBC_2.2.5 "libc.so.6"
   437  	#pragma cgo_import_dynamic fflush fflush#GLIBC_2.2.5 "libc.so.6"
   438  	#pragma cgo_import_dynamic _ _ "libpthread.so.0"
   439  	#pragma cgo_import_dynamic _ _ "libc.so.6"
   440  
   441  In the end, the compiled Go package, which will eventually be
   442  presented to 6l as part of a larger program, contains:
   443  
   444  	_go_.6        # 6g-compiled object for _cgo_gotypes.go *.cgo1.go
   445  	_cgo_defun.6  # 6c-compiled object for _cgo_defun.c
   446  	_all.o        # gcc-compiled object for _cgo_export.c, *.cgo2.c
   447  	_cgo_import.6 # 6c-compiled object for _cgo_import.c
   448  
   449  The final program will be a dynamic executable, so that 6l can avoid
   450  needing to process arbitrary .o files. It only needs to process the .o
   451  files generated from C files that cgo writes, and those are much more
   452  limited in the ELF or other features that they use.
   453  
   454  In essence, the _cgo_import.6 file includes the extra linking
   455  directives that 6l is not sophisticated enough to derive from _all.o
   456  on its own. Similarly, the _all.o uses dynamic references to real
   457  system object code because 6l is not sophisticated enough to process
   458  the real code.
   459  
   460  The main benefits of this system are that 6l remains relatively simple
   461  (it does not need to implement a complete ELF and Mach-O linker) and
   462  that gcc is not needed after the package is compiled. For example,
   463  package net uses cgo for access to name resolution functions provided
   464  by libc. Although gcc is needed to compile package net, gcc is not
   465  needed to link programs that import package net.
   466  
   467  Runtime
   468  
   469  When using cgo, Go must not assume that it owns all details of the
   470  process. In particular it needs to coordinate with C in the use of
   471  threads and thread-local storage. The runtime package, in its own
   472  (6c-compiled) C code, declares a few uninitialized (default bss)
   473  variables:
   474  
   475  	bool	runtime·iscgo;
   476  	void	(*libcgo_thread_start)(void*);
   477  	void	(*initcgo)(G*);
   478  
   479  Any package using cgo imports "runtime/cgo", which provides
   480  initializations for these variables. It sets iscgo to 1, initcgo to a
   481  gcc-compiled function that can be called early during program startup,
   482  and libcgo_thread_start to a gcc-compiled function that can be used to
   483  create a new thread, in place of the runtime's usual direct system
   484  calls.
   485  
   486  Internal and External Linking
   487  
   488  The text above describes "internal" linking, in which 6l parses and
   489  links host object files (ELF, Mach-O, PE, and so on) into the final
   490  executable itself. Keeping 6l simple means we cannot possibly
   491  implement the full semantics of the host linker, so the kinds of
   492  objects that can be linked directly into the binary is limited (other
   493  code can only be used as a dynamic library). On the other hand, when
   494  using internal linking, 6l can generate Go binaries by itself.
   495  
   496  In order to allow linking arbitrary object files without requiring
   497  dynamic libraries, cgo will soon support an "external" linking mode
   498  too. In external linking mode, 6l does not process any host object
   499  files. Instead, it collects all the Go code and writes a single go.o
   500  object file containing it. Then it invokes the host linker (usually
   501  gcc) to combine the go.o object file and any supporting non-Go code
   502  into a final executable. External linking avoids the dynamic library
   503  requirement but introduces a requirement that the host linker be
   504  present to create such a binary.
   505  
   506  Most builds both compile source code and invoke the linker to create a
   507  binary. When cgo is involved, the compile step already requires gcc, so
   508  it is not problematic for the link step to require gcc too.
   509  
   510  An important exception is builds using a pre-compiled copy of the
   511  standard library. In particular, package net uses cgo on most systems,
   512  and we want to preserve the ability to compile pure Go code that
   513  imports net without requiring gcc to be present at link time. (In this
   514  case, the dynamic library requirement is less significant, because the
   515  only library involved is libc.so, which can usually be assumed
   516  present.)
   517  
   518  This conflict between functionality and the gcc requirement means we
   519  must support both internal and external linking, depending on the
   520  circumstances: if net is the only cgo-using package, then internal
   521  linking is probably fine, but if other packages are involved, so that there
   522  are dependencies on libraries beyond libc, external linking is likely
   523  to work better. The compilation of a package records the relevant
   524  information to support both linking modes, leaving the decision
   525  to be made when linking the final binary.
   526  
   527  Linking Directives
   528  
   529  In either linking mode, package-specific directives must be passed
   530  through to 6l. These are communicated by writing #pragma directives
   531  in a C source file compiled by 6c. The directives are copied into the .6 object file
   532  and then processed by the linker.
   533  
   534  The directives are:
   535  
   536  #pragma cgo_import_dynamic <local> [<remote> ["<library>"]]
   537  
   538  	In internal linking mode, allow an unresolved reference to
   539  	<local>, assuming it will be resolved by a dynamic library
   540  	symbol. The optional <remote> specifies the symbol's name and
   541  	possibly version in the dynamic library, and the optional "<library>"
   542  	names the specific library where the symbol should be found.
   543  
   544  	In the <remote>, # or @ can be used to introduce a symbol version.
   545  
   546  	Examples:
   547  	#pragma cgo_import_dynamic puts
   548  	#pragma cgo_import_dynamic puts puts#GLIBC_2.2.5
   549  	#pragma cgo_import_dynamic puts puts#GLIBC_2.2.5 "libc.so.6"
   550  
   551  	A side effect of the cgo_import_dynamic directive with a
   552  	library is to make the final binary depend on that dynamic
   553  	library. To get the dependency without importing any specific
   554  	symbols, use _ for local and remote.
   555  
   556  	Example:
   557  	#pragma cgo_import_dynamic _ _ "libc.so.6"
   558  
   559  	For compatibility with current versions of SWIG,
   560  	#pragma dynimport is an alias for #pragma cgo_import_dynamic.
   561  
   562  #pragma cgo_dynamic_linker "<path>"
   563  
   564  	In internal linking mode, use "<path>" as the dynamic linker
   565  	in the final binary. This directive is only needed from one
   566  	package when constructing a binary; by convention it is
   567  	supplied by runtime/cgo.
   568  
   569  	Example:
   570  	#pragma cgo_dynamic_linker "/lib/ld-linux.so.2"
   571  
   572  #pragma cgo_export_dynamic <local> <remote>
   573  
   574  	In internal linking mode, put the Go symbol
   575  	named <local> into the program's exported symbol table as
   576  	<remote>, so that C code can refer to it by that name. This
   577  	mechanism makes it possible for C code to call back into Go or
   578  	to share Go's data.
   579  
   580  	For compatibility with current versions of SWIG,
   581  	#pragma dynexport is an alias for #pragma cgo_export_dynamic.
   582  
   583  #pragma cgo_import_static <local>
   584  
   585  	In external linking mode, allow unresolved references to
   586  	<local> in the go.o object file prepared for the host linker,
   587  	under the assumption that <local> will be supplied by the
   588  	other object files that will be linked with go.o.
   589  
   590  	Example:
   591  	#pragma cgo_import_static puts_wrapper
   592  
   593  #pragma cgo_export_static <local> <remote>
   594  
   595  	In external linking mode, put the Go symbol
   596  	named <local> into the program's exported symbol table as
   597  	<remote>, so that C code can refer to it by that name. This
   598  	mechanism makes it possible for C code to call back into Go or
   599  	to share Go's data.
   600  
   601  #pragma cgo_ldflag "<arg>"
   602  
   603  	In external linking mode, invoke the host linker (usually gcc)
   604  	with "<arg>" as a command-line argument following the .o files.
   605  	Note that the arguments are for "gcc", not "ld".
   606  
   607  	Example:
   608  	#pragma cgo_ldflag "-lpthread"
   609  	#pragma cgo_ldflag "-L/usr/local/sqlite3/lib"
   610  
   611  A package compiled with cgo will include directives for both
   612  internal and external linking; the linker will select the appropriate
   613  subset for the chosen linking mode.
   614  
   615  Example
   616  
   617  As a simple example, consider a package that uses cgo to call C.sin.
   618  The following code will be generated by cgo:
   619  
   620  	// compiled by 6g
   621  
   622  	type _Ctype_double float64
   623  	func _Cfunc_sin(_Ctype_double) _Ctype_double
   624  
   625  	// compiled by 6c
   626  
   627  	#pragma cgo_import_dynamic sin sin#GLIBC_2.2.5 "libm.so.6"
   628  
   629  	#pragma cgo_import_static _cgo_gcc_Cfunc_sin
   630  	#pragma cgo_ldflag "-lm"
   631  
   632  	void _cgo_gcc_Cfunc_sin(void*);
   633  
   634  	void
   635  	·_Cfunc_sin(struct{uint8 x[16];}p)
   636  	{
   637  		runtime·cgocall(_cgo_gcc_Cfunc_sin, &p);
   638  	}
   639  
   640  	// compiled by gcc, into foo.cgo2.o
   641  
   642  	void
   643  	_cgo_gcc_Cfunc_sin(void *v)
   644  	{
   645  		struct {
   646  			double p0;
   647  			double r;
   648  		} __attribute__((__packed__)) *a = v;
   649  		a->r = sin(a->p0);
   650  	}
   651  
   652  What happens at link time depends on whether the final binary is linked
   653  using the internal or external mode. If other packages are compiled in
   654  "external only" mode, then the final link will be an external one.
   655  Otherwise the link will be an internal one.
   656  
   657  The directives in the 6c-compiled file are used according to the kind
   658  of final link used.
   659  
   660  In internal mode, 6l itself processes all the host object files, in
   661  particular foo.cgo2.o. To do so, it uses the cgo_import_dynamic and
   662  cgo_dynamic_linker directives to learn that the otherwise undefined
   663  reference to sin in foo.cgo2.o should be rewritten to refer to the
   664  symbol sin with version GLIBC_2.2.5 from the dynamic library
   665  "libm.so.6", and the binary should request "/lib/ld-linux.so.2" as its
   666  runtime dynamic linker.
   667  
   668  In external mode, 6l does not process any host object files, in
   669  particular foo.cgo2.o. It links together the 6g- and 6c-generated
   670  object files, along with any other Go code, into a go.o file. While
   671  doing that, 6l will discover that there is no definition for
   672  _cgo_gcc_Cfunc_sin, referred to by the 6c-compiled source file. This
   673  is okay, because 6l also processes the cgo_import_static directive and
   674  knows that _cgo_gcc_Cfunc_sin is expected to be supplied by a host
   675  object file, so 6l does not treat the missing symbol as an error when
   676  creating go.o. Indeed, the definition for _cgo_gcc_Cfunc_sin will be
   677  provided to the host linker by foo2.cgo.o, which in turn will need the
   678  symbol 'sin'. 6l also processes the cgo_ldflag directives, so that it
   679  knows that the eventual host link command must include the -lm
   680  argument, so that the host linker will be able to find 'sin' in the
   681  math library.
   682  
   683  6l Command Line Interface
   684  
   685  The go command and any other Go-aware build systems invoke 6l
   686  to link a collection of packages into a single binary. By default, 6l will
   687  present the same interface it does today:
   688  
   689  	6l main.a
   690  
   691  produces a file named 6.out, even if 6l does so by invoking the host
   692  linker in external linking mode.
   693  
   694  By default, 6l will decide the linking mode as follows: if the only
   695  packages using cgo are those on a whitelist of standard library
   696  packages (net, os/user, runtime/cgo), 6l will use internal linking
   697  mode. Otherwise, there are non-standard cgo packages involved, and 6l
   698  will use external linking mode. The first rule means that a build of
   699  the godoc binary, which uses net but no other cgo, can run without
   700  needing gcc available. The second rule means that a build of a
   701  cgo-wrapped library like sqlite3 can generate a standalone executable
   702  instead of needing to refer to a dynamic library. The specific choice
   703  can be overridden using a command line flag: 6l -linkmode=internal or
   704  6l -linkmode=external.
   705  
   706  In an external link, 6l will create a temporary directory, write any
   707  host object files found in package archives to that directory (renamed
   708  to avoid conflicts), write the go.o file to that directory, and invoke
   709  the host linker. The default value for the host linker is $CC, split
   710  into fields, or else "gcc". The specific host linker command line can
   711  be overridden using command line flags: 6l -extld=clang
   712  -extldflags='-ggdb -O3'.  If any package in a build includes a .cc or
   713  other file compiled by the C++ compiler, the go tool will use the
   714  -extld option to set the host linker to the C++ compiler.
   715  
   716  These defaults mean that Go-aware build systems can ignore the linking
   717  changes and keep running plain '6l' and get reasonable results, but
   718  they can also control the linking details if desired.
   719  
   720  */