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