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