out.go 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  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. package main
  5. import (
  6. "bytes"
  7. "cmd/internal/pkgpath"
  8. "debug/elf"
  9. "debug/macho"
  10. "debug/pe"
  11. "fmt"
  12. "go/ast"
  13. "go/printer"
  14. "go/token"
  15. exec "internal/execabs"
  16. "internal/xcoff"
  17. "io"
  18. "os"
  19. "path/filepath"
  20. "regexp"
  21. "sort"
  22. "strings"
  23. "unicode"
  24. )
  25. var (
  26. conf = printer.Config{Mode: printer.SourcePos, Tabwidth: 8}
  27. noSourceConf = printer.Config{Tabwidth: 8}
  28. )
  29. // writeDefs creates output files to be compiled by gc and gcc.
  30. func (p *Package) writeDefs() {
  31. var fgo2, fc io.Writer
  32. f := creat(*objDir + "_cgo_gotypes.go")
  33. defer f.Close()
  34. fgo2 = f
  35. if *gccgo {
  36. f := creat(*objDir + "_cgo_defun.c")
  37. defer f.Close()
  38. fc = f
  39. }
  40. fm := creat(*objDir + "_cgo_main.c")
  41. var gccgoInit bytes.Buffer
  42. fflg := creat(*objDir + "_cgo_flags")
  43. var flags []string
  44. for k, v := range p.CgoFlags {
  45. flags = append(flags, fmt.Sprintf("_CGO_%s=%s", k, strings.Join(v, " ")))
  46. if k == "LDFLAGS" && !*gccgo {
  47. for _, arg := range v {
  48. fmt.Fprintf(fgo2, "//go:cgo_ldflag %q\n", arg)
  49. }
  50. }
  51. }
  52. sort.Strings(flags)
  53. for _, flag := range flags {
  54. fmt.Fprintln(fflg, flag)
  55. }
  56. fflg.Close()
  57. // Write C main file for using gcc to resolve imports.
  58. fmt.Fprintf(fm, "int main() { return 0; }\n")
  59. if *importRuntimeCgo {
  60. fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*) __attribute__((unused)), void *a __attribute__((unused)), int c __attribute__((unused)), __SIZE_TYPE__ ctxt __attribute__((unused))) { }\n")
  61. fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void) { return 0; }\n")
  62. fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__ ctxt __attribute__((unused))) { }\n")
  63. fmt.Fprintf(fm, "char* _cgo_topofstack(void) { return (char*)0; }\n")
  64. } else {
  65. // If we're not importing runtime/cgo, we *are* runtime/cgo,
  66. // which provides these functions. We just need a prototype.
  67. fmt.Fprintf(fm, "void crosscall2(void(*fn)(void*), void *a, int c, __SIZE_TYPE__ ctxt);\n")
  68. fmt.Fprintf(fm, "__SIZE_TYPE__ _cgo_wait_runtime_init_done(void);\n")
  69. fmt.Fprintf(fm, "void _cgo_release_context(__SIZE_TYPE__);\n")
  70. }
  71. fmt.Fprintf(fm, "void _cgo_allocate(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n")
  72. fmt.Fprintf(fm, "void _cgo_panic(void *a __attribute__((unused)), int c __attribute__((unused))) { }\n")
  73. fmt.Fprintf(fm, "void _cgo_reginit(void) { }\n")
  74. // Write second Go output: definitions of _C_xxx.
  75. // In a separate file so that the import of "unsafe" does not
  76. // pollute the original file.
  77. fmt.Fprintf(fgo2, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n")
  78. fmt.Fprintf(fgo2, "package %s\n\n", p.PackageName)
  79. fmt.Fprintf(fgo2, "import \"unsafe\"\n\n")
  80. if !*gccgo && *importRuntimeCgo {
  81. fmt.Fprintf(fgo2, "import _ \"runtime/cgo\"\n\n")
  82. }
  83. if *importSyscall {
  84. fmt.Fprintf(fgo2, "import \"syscall\"\n\n")
  85. fmt.Fprintf(fgo2, "var _ syscall.Errno\n")
  86. }
  87. fmt.Fprintf(fgo2, "func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }\n\n")
  88. if !*gccgo {
  89. fmt.Fprintf(fgo2, "//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse\n")
  90. fmt.Fprintf(fgo2, "var _Cgo_always_false bool\n")
  91. fmt.Fprintf(fgo2, "//go:linkname _Cgo_use runtime.cgoUse\n")
  92. fmt.Fprintf(fgo2, "func _Cgo_use(interface{})\n")
  93. }
  94. typedefNames := make([]string, 0, len(typedef))
  95. for name := range typedef {
  96. if name == "_Ctype_void" {
  97. // We provide an appropriate declaration for
  98. // _Ctype_void below (#39877).
  99. continue
  100. }
  101. typedefNames = append(typedefNames, name)
  102. }
  103. sort.Strings(typedefNames)
  104. for _, name := range typedefNames {
  105. def := typedef[name]
  106. if def.NotInHeap {
  107. fmt.Fprintf(fgo2, "//go:notinheap\n")
  108. }
  109. fmt.Fprintf(fgo2, "type %s ", name)
  110. // We don't have source info for these types, so write them out without source info.
  111. // Otherwise types would look like:
  112. //
  113. // type _Ctype_struct_cb struct {
  114. // //line :1
  115. // on_test *[0]byte
  116. // //line :1
  117. // }
  118. //
  119. // Which is not useful. Moreover we never override source info,
  120. // so subsequent source code uses the same source info.
  121. // Moreover, empty file name makes compile emit no source debug info at all.
  122. var buf bytes.Buffer
  123. noSourceConf.Fprint(&buf, fset, def.Go)
  124. if bytes.HasPrefix(buf.Bytes(), []byte("_Ctype_")) ||
  125. strings.HasPrefix(name, "_Ctype_enum_") ||
  126. strings.HasPrefix(name, "_Ctype_union_") {
  127. // This typedef is of the form `typedef a b` and should be an alias.
  128. fmt.Fprintf(fgo2, "= ")
  129. }
  130. fmt.Fprintf(fgo2, "%s", buf.Bytes())
  131. fmt.Fprintf(fgo2, "\n\n")
  132. }
  133. fmt.Fprintf(fgo2, "//go:notinheap\ntype _Ctype_void_notinheap struct{}\n\n")
  134. if *gccgo {
  135. fmt.Fprintf(fgo2, "type _Ctype_void byte\n")
  136. } else {
  137. fmt.Fprintf(fgo2, "type _Ctype_void [0]byte\n")
  138. }
  139. if *gccgo {
  140. fmt.Fprint(fgo2, gccgoGoProlog)
  141. fmt.Fprint(fc, p.cPrologGccgo())
  142. } else {
  143. fmt.Fprint(fgo2, goProlog)
  144. }
  145. if fc != nil {
  146. fmt.Fprintf(fc, "#line 1 \"cgo-generated-wrappers\"\n")
  147. }
  148. if fm != nil {
  149. fmt.Fprintf(fm, "#line 1 \"cgo-generated-wrappers\"\n")
  150. }
  151. gccgoSymbolPrefix := p.gccgoSymbolPrefix()
  152. cVars := make(map[string]bool)
  153. for _, key := range nameKeys(p.Name) {
  154. n := p.Name[key]
  155. if !n.IsVar() {
  156. continue
  157. }
  158. if !cVars[n.C] {
  159. if *gccgo {
  160. fmt.Fprintf(fc, "extern byte *%s;\n", n.C)
  161. } else {
  162. // Force a reference to all symbols so that
  163. // the external linker will add DT_NEEDED
  164. // entries as needed on ELF systems.
  165. // Treat function variables differently
  166. // to avoid type confict errors from LTO
  167. // (Link Time Optimization).
  168. if n.Kind == "fpvar" {
  169. fmt.Fprintf(fm, "extern void %s();\n", n.C)
  170. } else {
  171. fmt.Fprintf(fm, "extern char %s[];\n", n.C)
  172. fmt.Fprintf(fm, "void *_cgohack_%s = %s;\n\n", n.C, n.C)
  173. }
  174. fmt.Fprintf(fgo2, "//go:linkname __cgo_%s %s\n", n.C, n.C)
  175. fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", n.C)
  176. fmt.Fprintf(fgo2, "var __cgo_%s byte\n", n.C)
  177. }
  178. cVars[n.C] = true
  179. }
  180. var node ast.Node
  181. if n.Kind == "var" {
  182. node = &ast.StarExpr{X: n.Type.Go}
  183. } else if n.Kind == "fpvar" {
  184. node = n.Type.Go
  185. } else {
  186. panic(fmt.Errorf("invalid var kind %q", n.Kind))
  187. }
  188. if *gccgo {
  189. fmt.Fprintf(fc, `extern void *%s __asm__("%s.%s");`, n.Mangle, gccgoSymbolPrefix, gccgoToSymbol(n.Mangle))
  190. fmt.Fprintf(&gccgoInit, "\t%s = &%s;\n", n.Mangle, n.C)
  191. fmt.Fprintf(fc, "\n")
  192. }
  193. fmt.Fprintf(fgo2, "var %s ", n.Mangle)
  194. conf.Fprint(fgo2, fset, node)
  195. if !*gccgo {
  196. fmt.Fprintf(fgo2, " = (")
  197. conf.Fprint(fgo2, fset, node)
  198. fmt.Fprintf(fgo2, ")(unsafe.Pointer(&__cgo_%s))", n.C)
  199. }
  200. fmt.Fprintf(fgo2, "\n")
  201. }
  202. if *gccgo {
  203. fmt.Fprintf(fc, "\n")
  204. }
  205. for _, key := range nameKeys(p.Name) {
  206. n := p.Name[key]
  207. if n.Const != "" {
  208. fmt.Fprintf(fgo2, "const %s = %s\n", n.Mangle, n.Const)
  209. }
  210. }
  211. fmt.Fprintf(fgo2, "\n")
  212. callsMalloc := false
  213. for _, key := range nameKeys(p.Name) {
  214. n := p.Name[key]
  215. if n.FuncType != nil {
  216. p.writeDefsFunc(fgo2, n, &callsMalloc)
  217. }
  218. }
  219. fgcc := creat(*objDir + "_cgo_export.c")
  220. fgcch := creat(*objDir + "_cgo_export.h")
  221. if *gccgo {
  222. p.writeGccgoExports(fgo2, fm, fgcc, fgcch)
  223. } else {
  224. p.writeExports(fgo2, fm, fgcc, fgcch)
  225. }
  226. if callsMalloc && !*gccgo {
  227. fmt.Fprint(fgo2, strings.Replace(cMallocDefGo, "PREFIX", cPrefix, -1))
  228. fmt.Fprint(fgcc, strings.Replace(strings.Replace(cMallocDefC, "PREFIX", cPrefix, -1), "PACKED", p.packedAttribute(), -1))
  229. }
  230. if err := fgcc.Close(); err != nil {
  231. fatalf("%s", err)
  232. }
  233. if err := fgcch.Close(); err != nil {
  234. fatalf("%s", err)
  235. }
  236. if *exportHeader != "" && len(p.ExpFunc) > 0 {
  237. fexp := creat(*exportHeader)
  238. fgcch, err := os.Open(*objDir + "_cgo_export.h")
  239. if err != nil {
  240. fatalf("%s", err)
  241. }
  242. defer fgcch.Close()
  243. _, err = io.Copy(fexp, fgcch)
  244. if err != nil {
  245. fatalf("%s", err)
  246. }
  247. if err = fexp.Close(); err != nil {
  248. fatalf("%s", err)
  249. }
  250. }
  251. init := gccgoInit.String()
  252. if init != "" {
  253. // The init function does nothing but simple
  254. // assignments, so it won't use much stack space, so
  255. // it's OK to not split the stack. Splitting the stack
  256. // can run into a bug in clang (as of 2018-11-09):
  257. // this is a leaf function, and when clang sees a leaf
  258. // function it won't emit the split stack prologue for
  259. // the function. However, if this function refers to a
  260. // non-split-stack function, which will happen if the
  261. // cgo code refers to a C function not compiled with
  262. // -fsplit-stack, then the linker will think that it
  263. // needs to adjust the split stack prologue, but there
  264. // won't be one. Marking the function explicitly
  265. // no_split_stack works around this problem by telling
  266. // the linker that it's OK if there is no split stack
  267. // prologue.
  268. fmt.Fprintln(fc, "static void init(void) __attribute__ ((constructor, no_split_stack));")
  269. fmt.Fprintln(fc, "static void init(void) {")
  270. fmt.Fprint(fc, init)
  271. fmt.Fprintln(fc, "}")
  272. }
  273. }
  274. // elfImportedSymbols is like elf.File.ImportedSymbols, but it
  275. // includes weak symbols.
  276. //
  277. // A bug in some versions of LLD (at least LLD 8) cause it to emit
  278. // several pthreads symbols as weak, but we need to import those. See
  279. // issue #31912 or https://bugs.llvm.org/show_bug.cgi?id=42442.
  280. //
  281. // When doing external linking, we hand everything off to the external
  282. // linker, which will create its own dynamic symbol tables. For
  283. // internal linking, this may turn weak imports into strong imports,
  284. // which could cause dynamic linking to fail if a symbol really isn't
  285. // defined. However, the standard library depends on everything it
  286. // imports, and this is the primary use of dynamic symbol tables with
  287. // internal linking.
  288. func elfImportedSymbols(f *elf.File) []elf.ImportedSymbol {
  289. syms, _ := f.DynamicSymbols()
  290. var imports []elf.ImportedSymbol
  291. for _, s := range syms {
  292. if (elf.ST_BIND(s.Info) == elf.STB_GLOBAL || elf.ST_BIND(s.Info) == elf.STB_WEAK) && s.Section == elf.SHN_UNDEF {
  293. imports = append(imports, elf.ImportedSymbol{
  294. Name: s.Name,
  295. Library: s.Library,
  296. Version: s.Version,
  297. })
  298. }
  299. }
  300. return imports
  301. }
  302. func dynimport(obj string) {
  303. stdout := os.Stdout
  304. if *dynout != "" {
  305. f, err := os.Create(*dynout)
  306. if err != nil {
  307. fatalf("%s", err)
  308. }
  309. stdout = f
  310. }
  311. fmt.Fprintf(stdout, "package %s\n", *dynpackage)
  312. if f, err := elf.Open(obj); err == nil {
  313. if *dynlinker {
  314. // Emit the cgo_dynamic_linker line.
  315. if sec := f.Section(".interp"); sec != nil {
  316. if data, err := sec.Data(); err == nil && len(data) > 1 {
  317. // skip trailing \0 in data
  318. fmt.Fprintf(stdout, "//go:cgo_dynamic_linker %q\n", string(data[:len(data)-1]))
  319. }
  320. }
  321. }
  322. sym := elfImportedSymbols(f)
  323. for _, s := range sym {
  324. targ := s.Name
  325. if s.Version != "" {
  326. targ += "#" + s.Version
  327. }
  328. checkImportSymName(s.Name)
  329. checkImportSymName(targ)
  330. fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, targ, s.Library)
  331. }
  332. lib, _ := f.ImportedLibraries()
  333. for _, l := range lib {
  334. fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l)
  335. }
  336. return
  337. }
  338. if f, err := macho.Open(obj); err == nil {
  339. sym, _ := f.ImportedSymbols()
  340. for _, s := range sym {
  341. if len(s) > 0 && s[0] == '_' {
  342. s = s[1:]
  343. }
  344. checkImportSymName(s)
  345. fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "")
  346. }
  347. lib, _ := f.ImportedLibraries()
  348. for _, l := range lib {
  349. fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l)
  350. }
  351. return
  352. }
  353. if f, err := pe.Open(obj); err == nil {
  354. sym, _ := f.ImportedSymbols()
  355. for _, s := range sym {
  356. ss := strings.Split(s, ":")
  357. name := strings.Split(ss[0], "@")[0]
  358. checkImportSymName(name)
  359. checkImportSymName(ss[0])
  360. fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", name, ss[0], strings.ToLower(ss[1]))
  361. }
  362. return
  363. }
  364. if f, err := xcoff.Open(obj); err == nil {
  365. sym, err := f.ImportedSymbols()
  366. if err != nil {
  367. fatalf("cannot load imported symbols from XCOFF file %s: %v", obj, err)
  368. }
  369. for _, s := range sym {
  370. if s.Name == "runtime_rt0_go" || s.Name == "_rt0_ppc64_aix_lib" {
  371. // These symbols are imported by runtime/cgo but
  372. // must not be added to _cgo_import.go as there are
  373. // Go symbols.
  374. continue
  375. }
  376. checkImportSymName(s.Name)
  377. fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s.Name, s.Name, s.Library)
  378. }
  379. lib, err := f.ImportedLibraries()
  380. if err != nil {
  381. fatalf("cannot load imported libraries from XCOFF file %s: %v", obj, err)
  382. }
  383. for _, l := range lib {
  384. fmt.Fprintf(stdout, "//go:cgo_import_dynamic _ _ %q\n", l)
  385. }
  386. return
  387. }
  388. fatalf("cannot parse %s as ELF, Mach-O, PE or XCOFF", obj)
  389. }
  390. // checkImportSymName checks a symbol name we are going to emit as part
  391. // of a //go:cgo_import_dynamic pragma. These names come from object
  392. // files, so they may be corrupt. We are going to emit them unquoted,
  393. // so while they don't need to be valid symbol names (and in some cases,
  394. // involving symbol versions, they won't be) they must contain only
  395. // graphic characters and must not contain Go comments.
  396. func checkImportSymName(s string) {
  397. for _, c := range s {
  398. if !unicode.IsGraphic(c) || unicode.IsSpace(c) {
  399. fatalf("dynamic symbol %q contains unsupported character", s)
  400. }
  401. }
  402. if strings.Index(s, "//") >= 0 || strings.Index(s, "/*") >= 0 {
  403. fatalf("dynamic symbol %q contains Go comment")
  404. }
  405. }
  406. // Construct a gcc struct matching the gc argument frame.
  407. // Assumes that in gcc, char is 1 byte, short 2 bytes, int 4 bytes, long long 8 bytes.
  408. // These assumptions are checked by the gccProlog.
  409. // Also assumes that gc convention is to word-align the
  410. // input and output parameters.
  411. func (p *Package) structType(n *Name) (string, int64) {
  412. var buf bytes.Buffer
  413. fmt.Fprint(&buf, "struct {\n")
  414. off := int64(0)
  415. for i, t := range n.FuncType.Params {
  416. if off%t.Align != 0 {
  417. pad := t.Align - off%t.Align
  418. fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
  419. off += pad
  420. }
  421. c := t.Typedef
  422. if c == "" {
  423. c = t.C.String()
  424. }
  425. fmt.Fprintf(&buf, "\t\t%s p%d;\n", c, i)
  426. off += t.Size
  427. }
  428. if off%p.PtrSize != 0 {
  429. pad := p.PtrSize - off%p.PtrSize
  430. fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
  431. off += pad
  432. }
  433. if t := n.FuncType.Result; t != nil {
  434. if off%t.Align != 0 {
  435. pad := t.Align - off%t.Align
  436. fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
  437. off += pad
  438. }
  439. fmt.Fprintf(&buf, "\t\t%s r;\n", t.C)
  440. off += t.Size
  441. }
  442. if off%p.PtrSize != 0 {
  443. pad := p.PtrSize - off%p.PtrSize
  444. fmt.Fprintf(&buf, "\t\tchar __pad%d[%d];\n", off, pad)
  445. off += pad
  446. }
  447. if off == 0 {
  448. fmt.Fprintf(&buf, "\t\tchar unused;\n") // avoid empty struct
  449. }
  450. fmt.Fprintf(&buf, "\t}")
  451. return buf.String(), off
  452. }
  453. func (p *Package) writeDefsFunc(fgo2 io.Writer, n *Name, callsMalloc *bool) {
  454. name := n.Go
  455. gtype := n.FuncType.Go
  456. void := gtype.Results == nil || len(gtype.Results.List) == 0
  457. if n.AddError {
  458. // Add "error" to return type list.
  459. // Type list is known to be 0 or 1 element - it's a C function.
  460. err := &ast.Field{Type: ast.NewIdent("error")}
  461. l := gtype.Results.List
  462. if len(l) == 0 {
  463. l = []*ast.Field{err}
  464. } else {
  465. l = []*ast.Field{l[0], err}
  466. }
  467. t := new(ast.FuncType)
  468. *t = *gtype
  469. t.Results = &ast.FieldList{List: l}
  470. gtype = t
  471. }
  472. // Go func declaration.
  473. d := &ast.FuncDecl{
  474. Name: ast.NewIdent(n.Mangle),
  475. Type: gtype,
  476. }
  477. // Builtins defined in the C prolog.
  478. inProlog := builtinDefs[name] != ""
  479. cname := fmt.Sprintf("_cgo%s%s", cPrefix, n.Mangle)
  480. paramnames := []string(nil)
  481. if d.Type.Params != nil {
  482. for i, param := range d.Type.Params.List {
  483. paramName := fmt.Sprintf("p%d", i)
  484. param.Names = []*ast.Ident{ast.NewIdent(paramName)}
  485. paramnames = append(paramnames, paramName)
  486. }
  487. }
  488. if *gccgo {
  489. // Gccgo style hooks.
  490. fmt.Fprint(fgo2, "\n")
  491. conf.Fprint(fgo2, fset, d)
  492. fmt.Fprint(fgo2, " {\n")
  493. if !inProlog {
  494. fmt.Fprint(fgo2, "\tdefer syscall.CgocallDone()\n")
  495. fmt.Fprint(fgo2, "\tsyscall.Cgocall()\n")
  496. }
  497. if n.AddError {
  498. fmt.Fprint(fgo2, "\tsyscall.SetErrno(0)\n")
  499. }
  500. fmt.Fprint(fgo2, "\t")
  501. if !void {
  502. fmt.Fprint(fgo2, "r := ")
  503. }
  504. fmt.Fprintf(fgo2, "%s(%s)\n", cname, strings.Join(paramnames, ", "))
  505. if n.AddError {
  506. fmt.Fprint(fgo2, "\te := syscall.GetErrno()\n")
  507. fmt.Fprint(fgo2, "\tif e != 0 {\n")
  508. fmt.Fprint(fgo2, "\t\treturn ")
  509. if !void {
  510. fmt.Fprint(fgo2, "r, ")
  511. }
  512. fmt.Fprint(fgo2, "e\n")
  513. fmt.Fprint(fgo2, "\t}\n")
  514. fmt.Fprint(fgo2, "\treturn ")
  515. if !void {
  516. fmt.Fprint(fgo2, "r, ")
  517. }
  518. fmt.Fprint(fgo2, "nil\n")
  519. } else if !void {
  520. fmt.Fprint(fgo2, "\treturn r\n")
  521. }
  522. fmt.Fprint(fgo2, "}\n")
  523. // declare the C function.
  524. fmt.Fprintf(fgo2, "//extern %s\n", cname)
  525. d.Name = ast.NewIdent(cname)
  526. if n.AddError {
  527. l := d.Type.Results.List
  528. d.Type.Results.List = l[:len(l)-1]
  529. }
  530. conf.Fprint(fgo2, fset, d)
  531. fmt.Fprint(fgo2, "\n")
  532. return
  533. }
  534. if inProlog {
  535. fmt.Fprint(fgo2, builtinDefs[name])
  536. if strings.Contains(builtinDefs[name], "_cgo_cmalloc") {
  537. *callsMalloc = true
  538. }
  539. return
  540. }
  541. // Wrapper calls into gcc, passing a pointer to the argument frame.
  542. fmt.Fprintf(fgo2, "//go:cgo_import_static %s\n", cname)
  543. fmt.Fprintf(fgo2, "//go:linkname __cgofn_%s %s\n", cname, cname)
  544. fmt.Fprintf(fgo2, "var __cgofn_%s byte\n", cname)
  545. fmt.Fprintf(fgo2, "var %s = unsafe.Pointer(&__cgofn_%s)\n", cname, cname)
  546. nret := 0
  547. if !void {
  548. d.Type.Results.List[0].Names = []*ast.Ident{ast.NewIdent("r1")}
  549. nret = 1
  550. }
  551. if n.AddError {
  552. d.Type.Results.List[nret].Names = []*ast.Ident{ast.NewIdent("r2")}
  553. }
  554. fmt.Fprint(fgo2, "\n")
  555. fmt.Fprint(fgo2, "//go:cgo_unsafe_args\n")
  556. conf.Fprint(fgo2, fset, d)
  557. fmt.Fprint(fgo2, " {\n")
  558. // NOTE: Using uintptr to hide from escape analysis.
  559. arg := "0"
  560. if len(paramnames) > 0 {
  561. arg = "uintptr(unsafe.Pointer(&p0))"
  562. } else if !void {
  563. arg = "uintptr(unsafe.Pointer(&r1))"
  564. }
  565. prefix := ""
  566. if n.AddError {
  567. prefix = "errno := "
  568. }
  569. fmt.Fprintf(fgo2, "\t%s_cgo_runtime_cgocall(%s, %s)\n", prefix, cname, arg)
  570. if n.AddError {
  571. fmt.Fprintf(fgo2, "\tif errno != 0 { r2 = syscall.Errno(errno) }\n")
  572. }
  573. fmt.Fprintf(fgo2, "\tif _Cgo_always_false {\n")
  574. if d.Type.Params != nil {
  575. for i := range d.Type.Params.List {
  576. fmt.Fprintf(fgo2, "\t\t_Cgo_use(p%d)\n", i)
  577. }
  578. }
  579. fmt.Fprintf(fgo2, "\t}\n")
  580. fmt.Fprintf(fgo2, "\treturn\n")
  581. fmt.Fprintf(fgo2, "}\n")
  582. }
  583. // writeOutput creates stubs for a specific source file to be compiled by gc
  584. func (p *Package) writeOutput(f *File, srcfile string) {
  585. base := srcfile
  586. if strings.HasSuffix(base, ".go") {
  587. base = base[0 : len(base)-3]
  588. }
  589. base = filepath.Base(base)
  590. fgo1 := creat(*objDir + base + ".cgo1.go")
  591. fgcc := creat(*objDir + base + ".cgo2.c")
  592. p.GoFiles = append(p.GoFiles, base+".cgo1.go")
  593. p.GccFiles = append(p.GccFiles, base+".cgo2.c")
  594. // Write Go output: Go input with rewrites of C.xxx to _C_xxx.
  595. fmt.Fprintf(fgo1, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n")
  596. fmt.Fprintf(fgo1, "//line %s:1:1\n", srcfile)
  597. fgo1.Write(f.Edit.Bytes())
  598. // While we process the vars and funcs, also write gcc output.
  599. // Gcc output starts with the preamble.
  600. fmt.Fprintf(fgcc, "%s\n", builtinProlog)
  601. fmt.Fprintf(fgcc, "%s\n", f.Preamble)
  602. fmt.Fprintf(fgcc, "%s\n", gccProlog)
  603. fmt.Fprintf(fgcc, "%s\n", tsanProlog)
  604. fmt.Fprintf(fgcc, "%s\n", msanProlog)
  605. for _, key := range nameKeys(f.Name) {
  606. n := f.Name[key]
  607. if n.FuncType != nil {
  608. p.writeOutputFunc(fgcc, n)
  609. }
  610. }
  611. fgo1.Close()
  612. fgcc.Close()
  613. }
  614. // fixGo converts the internal Name.Go field into the name we should show
  615. // to users in error messages. There's only one for now: on input we rewrite
  616. // C.malloc into C._CMalloc, so change it back here.
  617. func fixGo(name string) string {
  618. if name == "_CMalloc" {
  619. return "malloc"
  620. }
  621. return name
  622. }
  623. var isBuiltin = map[string]bool{
  624. "_Cfunc_CString": true,
  625. "_Cfunc_CBytes": true,
  626. "_Cfunc_GoString": true,
  627. "_Cfunc_GoStringN": true,
  628. "_Cfunc_GoBytes": true,
  629. "_Cfunc__CMalloc": true,
  630. }
  631. func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
  632. name := n.Mangle
  633. if isBuiltin[name] || p.Written[name] {
  634. // The builtins are already defined in the C prolog, and we don't
  635. // want to duplicate function definitions we've already done.
  636. return
  637. }
  638. p.Written[name] = true
  639. if *gccgo {
  640. p.writeGccgoOutputFunc(fgcc, n)
  641. return
  642. }
  643. ctype, _ := p.structType(n)
  644. // Gcc wrapper unpacks the C argument struct
  645. // and calls the actual C function.
  646. fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n")
  647. if n.AddError {
  648. fmt.Fprintf(fgcc, "int\n")
  649. } else {
  650. fmt.Fprintf(fgcc, "void\n")
  651. }
  652. fmt.Fprintf(fgcc, "_cgo%s%s(void *v)\n", cPrefix, n.Mangle)
  653. fmt.Fprintf(fgcc, "{\n")
  654. if n.AddError {
  655. fmt.Fprintf(fgcc, "\tint _cgo_errno;\n")
  656. }
  657. // We're trying to write a gcc struct that matches gc's layout.
  658. // Use packed attribute to force no padding in this struct in case
  659. // gcc has different packing requirements.
  660. fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute())
  661. if n.FuncType.Result != nil {
  662. // Save the stack top for use below.
  663. fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n")
  664. }
  665. tr := n.FuncType.Result
  666. if tr != nil {
  667. fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n")
  668. }
  669. fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
  670. if n.AddError {
  671. fmt.Fprintf(fgcc, "\terrno = 0;\n")
  672. }
  673. fmt.Fprintf(fgcc, "\t")
  674. if tr != nil {
  675. fmt.Fprintf(fgcc, "_cgo_r = ")
  676. if c := tr.C.String(); c[len(c)-1] == '*' {
  677. fmt.Fprint(fgcc, "(__typeof__(_cgo_a->r)) ")
  678. }
  679. }
  680. if n.Kind == "macro" {
  681. fmt.Fprintf(fgcc, "%s;\n", n.C)
  682. } else {
  683. fmt.Fprintf(fgcc, "%s(", n.C)
  684. for i := range n.FuncType.Params {
  685. if i > 0 {
  686. fmt.Fprintf(fgcc, ", ")
  687. }
  688. fmt.Fprintf(fgcc, "_cgo_a->p%d", i)
  689. }
  690. fmt.Fprintf(fgcc, ");\n")
  691. }
  692. if n.AddError {
  693. fmt.Fprintf(fgcc, "\t_cgo_errno = errno;\n")
  694. }
  695. fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
  696. if n.FuncType.Result != nil {
  697. // The cgo call may have caused a stack copy (via a callback).
  698. // Adjust the return value pointer appropriately.
  699. fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n")
  700. // Save the return value.
  701. fmt.Fprintf(fgcc, "\t_cgo_a->r = _cgo_r;\n")
  702. // The return value is on the Go stack. If we are using msan,
  703. // and if the C value is partially or completely uninitialized,
  704. // the assignment will mark the Go stack as uninitialized.
  705. // The Go compiler does not update msan for changes to the
  706. // stack. It is possible that the stack will remain
  707. // uninitialized, and then later be used in a way that is
  708. // visible to msan, possibly leading to a false positive.
  709. // Mark the stack space as written, to avoid this problem.
  710. // See issue 26209.
  711. fmt.Fprintf(fgcc, "\t_cgo_msan_write(&_cgo_a->r, sizeof(_cgo_a->r));\n")
  712. }
  713. if n.AddError {
  714. fmt.Fprintf(fgcc, "\treturn _cgo_errno;\n")
  715. }
  716. fmt.Fprintf(fgcc, "}\n")
  717. fmt.Fprintf(fgcc, "\n")
  718. }
  719. // Write out a wrapper for a function when using gccgo. This is a
  720. // simple wrapper that just calls the real function. We only need a
  721. // wrapper to support static functions in the prologue--without a
  722. // wrapper, we can't refer to the function, since the reference is in
  723. // a different file.
  724. func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) {
  725. fmt.Fprintf(fgcc, "CGO_NO_SANITIZE_THREAD\n")
  726. if t := n.FuncType.Result; t != nil {
  727. fmt.Fprintf(fgcc, "%s\n", t.C.String())
  728. } else {
  729. fmt.Fprintf(fgcc, "void\n")
  730. }
  731. fmt.Fprintf(fgcc, "_cgo%s%s(", cPrefix, n.Mangle)
  732. for i, t := range n.FuncType.Params {
  733. if i > 0 {
  734. fmt.Fprintf(fgcc, ", ")
  735. }
  736. c := t.Typedef
  737. if c == "" {
  738. c = t.C.String()
  739. }
  740. fmt.Fprintf(fgcc, "%s p%d", c, i)
  741. }
  742. fmt.Fprintf(fgcc, ")\n")
  743. fmt.Fprintf(fgcc, "{\n")
  744. if t := n.FuncType.Result; t != nil {
  745. fmt.Fprintf(fgcc, "\t%s _cgo_r;\n", t.C.String())
  746. }
  747. fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
  748. fmt.Fprintf(fgcc, "\t")
  749. if t := n.FuncType.Result; t != nil {
  750. fmt.Fprintf(fgcc, "_cgo_r = ")
  751. // Cast to void* to avoid warnings due to omitted qualifiers.
  752. if c := t.C.String(); c[len(c)-1] == '*' {
  753. fmt.Fprintf(fgcc, "(void*)")
  754. }
  755. }
  756. if n.Kind == "macro" {
  757. fmt.Fprintf(fgcc, "%s;\n", n.C)
  758. } else {
  759. fmt.Fprintf(fgcc, "%s(", n.C)
  760. for i := range n.FuncType.Params {
  761. if i > 0 {
  762. fmt.Fprintf(fgcc, ", ")
  763. }
  764. fmt.Fprintf(fgcc, "p%d", i)
  765. }
  766. fmt.Fprintf(fgcc, ");\n")
  767. }
  768. fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
  769. if t := n.FuncType.Result; t != nil {
  770. fmt.Fprintf(fgcc, "\treturn ")
  771. // Cast to void* to avoid warnings due to omitted qualifiers
  772. // and explicit incompatible struct types.
  773. if c := t.C.String(); c[len(c)-1] == '*' {
  774. fmt.Fprintf(fgcc, "(void*)")
  775. }
  776. fmt.Fprintf(fgcc, "_cgo_r;\n")
  777. }
  778. fmt.Fprintf(fgcc, "}\n")
  779. fmt.Fprintf(fgcc, "\n")
  780. }
  781. // packedAttribute returns host compiler struct attribute that will be
  782. // used to match gc's struct layout. For example, on 386 Windows,
  783. // gcc wants to 8-align int64s, but gc does not.
  784. // Use __gcc_struct__ to work around https://gcc.gnu.org/PR52991 on x86,
  785. // and https://golang.org/issue/5603.
  786. func (p *Package) packedAttribute() string {
  787. s := "__attribute__((__packed__"
  788. if !p.GccIsClang && (goarch == "amd64" || goarch == "386") {
  789. s += ", __gcc_struct__"
  790. }
  791. return s + "))"
  792. }
  793. // exportParamName returns the value of param as it should be
  794. // displayed in a c header file. If param contains any non-ASCII
  795. // characters, this function will return the character p followed by
  796. // the value of position; otherwise, this function will return the
  797. // value of param.
  798. func exportParamName(param string, position int) string {
  799. if param == "" {
  800. return fmt.Sprintf("p%d", position)
  801. }
  802. pname := param
  803. for i := 0; i < len(param); i++ {
  804. if param[i] > unicode.MaxASCII {
  805. pname = fmt.Sprintf("p%d", position)
  806. break
  807. }
  808. }
  809. return pname
  810. }
  811. // Write out the various stubs we need to support functions exported
  812. // from Go so that they are callable from C.
  813. func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
  814. p.writeExportHeader(fgcch)
  815. fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
  816. fmt.Fprintf(fgcc, "#include <stdlib.h>\n")
  817. fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n\n")
  818. // We use packed structs, but they are always aligned.
  819. // The pragmas and address-of-packed-member are only recognized as
  820. // warning groups in clang 4.0+, so ignore unknown pragmas first.
  821. fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n")
  822. fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Wpragmas\"\n")
  823. fmt.Fprintf(fgcc, "#pragma GCC diagnostic ignored \"-Waddress-of-packed-member\"\n")
  824. fmt.Fprintf(fgcc, "extern void crosscall2(void (*fn)(void *), void *, int, __SIZE_TYPE__);\n")
  825. fmt.Fprintf(fgcc, "extern __SIZE_TYPE__ _cgo_wait_runtime_init_done(void);\n")
  826. fmt.Fprintf(fgcc, "extern void _cgo_release_context(__SIZE_TYPE__);\n\n")
  827. fmt.Fprintf(fgcc, "extern char* _cgo_topofstack(void);")
  828. fmt.Fprintf(fgcc, "%s\n", tsanProlog)
  829. fmt.Fprintf(fgcc, "%s\n", msanProlog)
  830. for _, exp := range p.ExpFunc {
  831. fn := exp.Func
  832. // Construct a struct that will be used to communicate
  833. // arguments from C to Go. The C and Go definitions
  834. // just have to agree. The gcc struct will be compiled
  835. // with __attribute__((packed)) so all padding must be
  836. // accounted for explicitly.
  837. ctype := "struct {\n"
  838. gotype := new(bytes.Buffer)
  839. fmt.Fprintf(gotype, "struct {\n")
  840. off := int64(0)
  841. npad := 0
  842. argField := func(typ ast.Expr, namePat string, args ...interface{}) {
  843. name := fmt.Sprintf(namePat, args...)
  844. t := p.cgoType(typ)
  845. if off%t.Align != 0 {
  846. pad := t.Align - off%t.Align
  847. ctype += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad)
  848. off += pad
  849. npad++
  850. }
  851. ctype += fmt.Sprintf("\t\t%s %s;\n", t.C, name)
  852. fmt.Fprintf(gotype, "\t\t%s ", name)
  853. noSourceConf.Fprint(gotype, fset, typ)
  854. fmt.Fprintf(gotype, "\n")
  855. off += t.Size
  856. }
  857. if fn.Recv != nil {
  858. argField(fn.Recv.List[0].Type, "recv")
  859. }
  860. fntype := fn.Type
  861. forFieldList(fntype.Params,
  862. func(i int, aname string, atype ast.Expr) {
  863. argField(atype, "p%d", i)
  864. })
  865. forFieldList(fntype.Results,
  866. func(i int, aname string, atype ast.Expr) {
  867. argField(atype, "r%d", i)
  868. })
  869. if ctype == "struct {\n" {
  870. ctype += "\t\tchar unused;\n" // avoid empty struct
  871. }
  872. ctype += "\t}"
  873. fmt.Fprintf(gotype, "\t}")
  874. // Get the return type of the wrapper function
  875. // compiled by gcc.
  876. gccResult := ""
  877. if fntype.Results == nil || len(fntype.Results.List) == 0 {
  878. gccResult = "void"
  879. } else if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
  880. gccResult = p.cgoType(fntype.Results.List[0].Type).C.String()
  881. } else {
  882. fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
  883. fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
  884. forFieldList(fntype.Results,
  885. func(i int, aname string, atype ast.Expr) {
  886. fmt.Fprintf(fgcch, "\t%s r%d;", p.cgoType(atype).C, i)
  887. if len(aname) > 0 {
  888. fmt.Fprintf(fgcch, " /* %s */", aname)
  889. }
  890. fmt.Fprint(fgcch, "\n")
  891. })
  892. fmt.Fprintf(fgcch, "};\n")
  893. gccResult = "struct " + exp.ExpName + "_return"
  894. }
  895. // Build the wrapper function compiled by gcc.
  896. gccExport := ""
  897. if goos == "windows" {
  898. gccExport = "__declspec(dllexport) "
  899. }
  900. s := fmt.Sprintf("%s%s %s(", gccExport, gccResult, exp.ExpName)
  901. if fn.Recv != nil {
  902. s += p.cgoType(fn.Recv.List[0].Type).C.String()
  903. s += " recv"
  904. }
  905. forFieldList(fntype.Params,
  906. func(i int, aname string, atype ast.Expr) {
  907. if i > 0 || fn.Recv != nil {
  908. s += ", "
  909. }
  910. s += fmt.Sprintf("%s %s", p.cgoType(atype).C, exportParamName(aname, i))
  911. })
  912. s += ")"
  913. if len(exp.Doc) > 0 {
  914. fmt.Fprintf(fgcch, "\n%s", exp.Doc)
  915. if !strings.HasSuffix(exp.Doc, "\n") {
  916. fmt.Fprint(fgcch, "\n")
  917. }
  918. }
  919. fmt.Fprintf(fgcch, "extern %s;\n", s)
  920. fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *);\n", cPrefix, exp.ExpName)
  921. fmt.Fprintf(fgcc, "\nCGO_NO_SANITIZE_THREAD")
  922. fmt.Fprintf(fgcc, "\n%s\n", s)
  923. fmt.Fprintf(fgcc, "{\n")
  924. fmt.Fprintf(fgcc, "\t__SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done();\n")
  925. // The results part of the argument structure must be
  926. // initialized to 0 so the write barriers generated by
  927. // the assignments to these fields in Go are safe.
  928. //
  929. // We use a local static variable to get the zeroed
  930. // value of the argument type. This avoids including
  931. // string.h for memset, and is also robust to C++
  932. // types with constructors. Both GCC and LLVM optimize
  933. // this into just zeroing _cgo_a.
  934. fmt.Fprintf(fgcc, "\ttypedef %s %v _cgo_argtype;\n", ctype, p.packedAttribute())
  935. fmt.Fprintf(fgcc, "\tstatic _cgo_argtype _cgo_zero;\n")
  936. fmt.Fprintf(fgcc, "\t_cgo_argtype _cgo_a = _cgo_zero;\n")
  937. if gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1) {
  938. fmt.Fprintf(fgcc, "\t%s r;\n", gccResult)
  939. }
  940. if fn.Recv != nil {
  941. fmt.Fprintf(fgcc, "\t_cgo_a.recv = recv;\n")
  942. }
  943. forFieldList(fntype.Params,
  944. func(i int, aname string, atype ast.Expr) {
  945. fmt.Fprintf(fgcc, "\t_cgo_a.p%d = %s;\n", i, exportParamName(aname, i))
  946. })
  947. fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
  948. fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &_cgo_a, %d, _cgo_ctxt);\n", cPrefix, exp.ExpName, off)
  949. fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
  950. fmt.Fprintf(fgcc, "\t_cgo_release_context(_cgo_ctxt);\n")
  951. if gccResult != "void" {
  952. if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
  953. fmt.Fprintf(fgcc, "\treturn _cgo_a.r0;\n")
  954. } else {
  955. forFieldList(fntype.Results,
  956. func(i int, aname string, atype ast.Expr) {
  957. fmt.Fprintf(fgcc, "\tr.r%d = _cgo_a.r%d;\n", i, i)
  958. })
  959. fmt.Fprintf(fgcc, "\treturn r;\n")
  960. }
  961. }
  962. fmt.Fprintf(fgcc, "}\n")
  963. // In internal linking mode, the Go linker sees both
  964. // the C wrapper written above and the Go wrapper it
  965. // references. Hence, export the C wrapper (e.g., for
  966. // if we're building a shared object). The Go linker
  967. // will resolve the C wrapper's reference to the Go
  968. // wrapper without a separate export.
  969. fmt.Fprintf(fgo2, "//go:cgo_export_dynamic %s\n", exp.ExpName)
  970. // cgo_export_static refers to a symbol by its linker
  971. // name, so set the linker name of the Go wrapper.
  972. fmt.Fprintf(fgo2, "//go:linkname _cgoexp%s_%s _cgoexp%s_%s\n", cPrefix, exp.ExpName, cPrefix, exp.ExpName)
  973. // In external linking mode, the Go linker sees the Go
  974. // wrapper, but not the C wrapper. For this case,
  975. // export the Go wrapper so the host linker can
  976. // resolve the reference from the C wrapper to the Go
  977. // wrapper.
  978. fmt.Fprintf(fgo2, "//go:cgo_export_static _cgoexp%s_%s\n", cPrefix, exp.ExpName)
  979. // Build the wrapper function compiled by cmd/compile.
  980. // This unpacks the argument struct above and calls the Go function.
  981. fmt.Fprintf(fgo2, "func _cgoexp%s_%s(a *%s) {\n", cPrefix, exp.ExpName, gotype)
  982. fmt.Fprintf(fm, "void _cgoexp%s_%s(void* p){}\n", cPrefix, exp.ExpName)
  983. fmt.Fprintf(fgo2, "\t")
  984. if gccResult != "void" {
  985. // Write results back to frame.
  986. forFieldList(fntype.Results,
  987. func(i int, aname string, atype ast.Expr) {
  988. if i > 0 {
  989. fmt.Fprintf(fgo2, ", ")
  990. }
  991. fmt.Fprintf(fgo2, "a.r%d", i)
  992. })
  993. fmt.Fprintf(fgo2, " = ")
  994. }
  995. if fn.Recv != nil {
  996. fmt.Fprintf(fgo2, "a.recv.")
  997. }
  998. fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
  999. forFieldList(fntype.Params,
  1000. func(i int, aname string, atype ast.Expr) {
  1001. if i > 0 {
  1002. fmt.Fprint(fgo2, ", ")
  1003. }
  1004. fmt.Fprintf(fgo2, "a.p%d", i)
  1005. })
  1006. fmt.Fprint(fgo2, ")\n")
  1007. if gccResult != "void" {
  1008. // Verify that any results don't contain any
  1009. // Go pointers.
  1010. forFieldList(fntype.Results,
  1011. func(i int, aname string, atype ast.Expr) {
  1012. if !p.hasPointer(nil, atype, false) {
  1013. return
  1014. }
  1015. fmt.Fprintf(fgo2, "\t_cgoCheckResult(a.r%d)\n", i)
  1016. })
  1017. }
  1018. fmt.Fprint(fgo2, "}\n")
  1019. }
  1020. fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog)
  1021. }
  1022. // Write out the C header allowing C code to call exported gccgo functions.
  1023. func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
  1024. gccgoSymbolPrefix := p.gccgoSymbolPrefix()
  1025. p.writeExportHeader(fgcch)
  1026. fmt.Fprintf(fgcc, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
  1027. fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
  1028. fmt.Fprintf(fgcc, "%s\n", gccgoExportFileProlog)
  1029. fmt.Fprintf(fgcc, "%s\n", tsanProlog)
  1030. fmt.Fprintf(fgcc, "%s\n", msanProlog)
  1031. for _, exp := range p.ExpFunc {
  1032. fn := exp.Func
  1033. fntype := fn.Type
  1034. cdeclBuf := new(bytes.Buffer)
  1035. resultCount := 0
  1036. forFieldList(fntype.Results,
  1037. func(i int, aname string, atype ast.Expr) { resultCount++ })
  1038. switch resultCount {
  1039. case 0:
  1040. fmt.Fprintf(cdeclBuf, "void")
  1041. case 1:
  1042. forFieldList(fntype.Results,
  1043. func(i int, aname string, atype ast.Expr) {
  1044. t := p.cgoType(atype)
  1045. fmt.Fprintf(cdeclBuf, "%s", t.C)
  1046. })
  1047. default:
  1048. // Declare a result struct.
  1049. fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
  1050. fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
  1051. forFieldList(fntype.Results,
  1052. func(i int, aname string, atype ast.Expr) {
  1053. t := p.cgoType(atype)
  1054. fmt.Fprintf(fgcch, "\t%s r%d;", t.C, i)
  1055. if len(aname) > 0 {
  1056. fmt.Fprintf(fgcch, " /* %s */", aname)
  1057. }
  1058. fmt.Fprint(fgcch, "\n")
  1059. })
  1060. fmt.Fprintf(fgcch, "};\n")
  1061. fmt.Fprintf(cdeclBuf, "struct %s_return", exp.ExpName)
  1062. }
  1063. cRet := cdeclBuf.String()
  1064. cdeclBuf = new(bytes.Buffer)
  1065. fmt.Fprintf(cdeclBuf, "(")
  1066. if fn.Recv != nil {
  1067. fmt.Fprintf(cdeclBuf, "%s recv", p.cgoType(fn.Recv.List[0].Type).C.String())
  1068. }
  1069. // Function parameters.
  1070. forFieldList(fntype.Params,
  1071. func(i int, aname string, atype ast.Expr) {
  1072. if i > 0 || fn.Recv != nil {
  1073. fmt.Fprintf(cdeclBuf, ", ")
  1074. }
  1075. t := p.cgoType(atype)
  1076. fmt.Fprintf(cdeclBuf, "%s p%d", t.C, i)
  1077. })
  1078. fmt.Fprintf(cdeclBuf, ")")
  1079. cParams := cdeclBuf.String()
  1080. if len(exp.Doc) > 0 {
  1081. fmt.Fprintf(fgcch, "\n%s", exp.Doc)
  1082. }
  1083. fmt.Fprintf(fgcch, "extern %s %s%s;\n", cRet, exp.ExpName, cParams)
  1084. // We need to use a name that will be exported by the
  1085. // Go code; otherwise gccgo will make it static and we
  1086. // will not be able to link against it from the C
  1087. // code.
  1088. goName := "Cgoexp_" + exp.ExpName
  1089. fmt.Fprintf(fgcc, `extern %s %s %s __asm__("%s.%s");`, cRet, goName, cParams, gccgoSymbolPrefix, gccgoToSymbol(goName))
  1090. fmt.Fprint(fgcc, "\n")
  1091. fmt.Fprint(fgcc, "\nCGO_NO_SANITIZE_THREAD\n")
  1092. fmt.Fprintf(fgcc, "%s %s %s {\n", cRet, exp.ExpName, cParams)
  1093. if resultCount > 0 {
  1094. fmt.Fprintf(fgcc, "\t%s r;\n", cRet)
  1095. }
  1096. fmt.Fprintf(fgcc, "\tif(_cgo_wait_runtime_init_done)\n")
  1097. fmt.Fprintf(fgcc, "\t\t_cgo_wait_runtime_init_done();\n")
  1098. fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
  1099. fmt.Fprint(fgcc, "\t")
  1100. if resultCount > 0 {
  1101. fmt.Fprint(fgcc, "r = ")
  1102. }
  1103. fmt.Fprintf(fgcc, "%s(", goName)
  1104. if fn.Recv != nil {
  1105. fmt.Fprint(fgcc, "recv")
  1106. }
  1107. forFieldList(fntype.Params,
  1108. func(i int, aname string, atype ast.Expr) {
  1109. if i > 0 || fn.Recv != nil {
  1110. fmt.Fprintf(fgcc, ", ")
  1111. }
  1112. fmt.Fprintf(fgcc, "p%d", i)
  1113. })
  1114. fmt.Fprint(fgcc, ");\n")
  1115. fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
  1116. if resultCount > 0 {
  1117. fmt.Fprint(fgcc, "\treturn r;\n")
  1118. }
  1119. fmt.Fprint(fgcc, "}\n")
  1120. // Dummy declaration for _cgo_main.c
  1121. fmt.Fprintf(fm, `char %s[1] __asm__("%s.%s");`, goName, gccgoSymbolPrefix, gccgoToSymbol(goName))
  1122. fmt.Fprint(fm, "\n")
  1123. // For gccgo we use a wrapper function in Go, in order
  1124. // to call CgocallBack and CgocallBackDone.
  1125. // This code uses printer.Fprint, not conf.Fprint,
  1126. // because we don't want //line comments in the middle
  1127. // of the function types.
  1128. fmt.Fprint(fgo2, "\n")
  1129. fmt.Fprintf(fgo2, "func %s(", goName)
  1130. if fn.Recv != nil {
  1131. fmt.Fprint(fgo2, "recv ")
  1132. printer.Fprint(fgo2, fset, fn.Recv.List[0].Type)
  1133. }
  1134. forFieldList(fntype.Params,
  1135. func(i int, aname string, atype ast.Expr) {
  1136. if i > 0 || fn.Recv != nil {
  1137. fmt.Fprintf(fgo2, ", ")
  1138. }
  1139. fmt.Fprintf(fgo2, "p%d ", i)
  1140. printer.Fprint(fgo2, fset, atype)
  1141. })
  1142. fmt.Fprintf(fgo2, ")")
  1143. if resultCount > 0 {
  1144. fmt.Fprintf(fgo2, " (")
  1145. forFieldList(fntype.Results,
  1146. func(i int, aname string, atype ast.Expr) {
  1147. if i > 0 {
  1148. fmt.Fprint(fgo2, ", ")
  1149. }
  1150. printer.Fprint(fgo2, fset, atype)
  1151. })
  1152. fmt.Fprint(fgo2, ")")
  1153. }
  1154. fmt.Fprint(fgo2, " {\n")
  1155. fmt.Fprint(fgo2, "\tsyscall.CgocallBack()\n")
  1156. fmt.Fprint(fgo2, "\tdefer syscall.CgocallBackDone()\n")
  1157. fmt.Fprint(fgo2, "\t")
  1158. if resultCount > 0 {
  1159. fmt.Fprint(fgo2, "return ")
  1160. }
  1161. if fn.Recv != nil {
  1162. fmt.Fprint(fgo2, "recv.")
  1163. }
  1164. fmt.Fprintf(fgo2, "%s(", exp.Func.Name)
  1165. forFieldList(fntype.Params,
  1166. func(i int, aname string, atype ast.Expr) {
  1167. if i > 0 {
  1168. fmt.Fprint(fgo2, ", ")
  1169. }
  1170. fmt.Fprintf(fgo2, "p%d", i)
  1171. })
  1172. fmt.Fprint(fgo2, ")\n")
  1173. fmt.Fprint(fgo2, "}\n")
  1174. }
  1175. fmt.Fprintf(fgcch, "%s", gccExportHeaderEpilog)
  1176. }
  1177. // writeExportHeader writes out the start of the _cgo_export.h file.
  1178. func (p *Package) writeExportHeader(fgcch io.Writer) {
  1179. fmt.Fprintf(fgcch, "/* Code generated by cmd/cgo; DO NOT EDIT. */\n\n")
  1180. pkg := *importPath
  1181. if pkg == "" {
  1182. pkg = p.PackagePath
  1183. }
  1184. fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg)
  1185. fmt.Fprintf(fgcch, "%s\n", builtinExportProlog)
  1186. // Remove absolute paths from #line comments in the preamble.
  1187. // They aren't useful for people using the header file,
  1188. // and they mean that the header files change based on the
  1189. // exact location of GOPATH.
  1190. re := regexp.MustCompile(`(?m)^(#line\s+[0-9]+\s+")[^"]*[/\\]([^"]*")`)
  1191. preamble := re.ReplaceAllString(p.Preamble, "$1$2")
  1192. fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments. */\n\n")
  1193. fmt.Fprintf(fgcch, "%s\n", preamble)
  1194. fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments. */\n\n")
  1195. fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
  1196. }
  1197. // gccgoToSymbol converts a name to a mangled symbol for gccgo.
  1198. func gccgoToSymbol(ppath string) string {
  1199. if gccgoMangler == nil {
  1200. var err error
  1201. cmd := os.Getenv("GCCGO")
  1202. if cmd == "" {
  1203. cmd, err = exec.LookPath("gccgo")
  1204. if err != nil {
  1205. fatalf("unable to locate gccgo: %v", err)
  1206. }
  1207. }
  1208. gccgoMangler, err = pkgpath.ToSymbolFunc(cmd, *objDir)
  1209. if err != nil {
  1210. fatalf("%v", err)
  1211. }
  1212. }
  1213. return gccgoMangler(ppath)
  1214. }
  1215. // Return the package prefix when using gccgo.
  1216. func (p *Package) gccgoSymbolPrefix() string {
  1217. if !*gccgo {
  1218. return ""
  1219. }
  1220. if *gccgopkgpath != "" {
  1221. return gccgoToSymbol(*gccgopkgpath)
  1222. }
  1223. if *gccgoprefix == "" && p.PackageName == "main" {
  1224. return "main"
  1225. }
  1226. prefix := gccgoToSymbol(*gccgoprefix)
  1227. if prefix == "" {
  1228. prefix = "go"
  1229. }
  1230. return prefix + "." + p.PackageName
  1231. }
  1232. // Call a function for each entry in an ast.FieldList, passing the
  1233. // index into the list, the name if any, and the type.
  1234. func forFieldList(fl *ast.FieldList, fn func(int, string, ast.Expr)) {
  1235. if fl == nil {
  1236. return
  1237. }
  1238. i := 0
  1239. for _, r := range fl.List {
  1240. if r.Names == nil {
  1241. fn(i, "", r.Type)
  1242. i++
  1243. } else {
  1244. for _, n := range r.Names {
  1245. fn(i, n.Name, r.Type)
  1246. i++
  1247. }
  1248. }
  1249. }
  1250. }
  1251. func c(repr string, args ...interface{}) *TypeRepr {
  1252. return &TypeRepr{repr, args}
  1253. }
  1254. // Map predeclared Go types to Type.
  1255. var goTypes = map[string]*Type{
  1256. "bool": {Size: 1, Align: 1, C: c("GoUint8")},
  1257. "byte": {Size: 1, Align: 1, C: c("GoUint8")},
  1258. "int": {Size: 0, Align: 0, C: c("GoInt")},
  1259. "uint": {Size: 0, Align: 0, C: c("GoUint")},
  1260. "rune": {Size: 4, Align: 4, C: c("GoInt32")},
  1261. "int8": {Size: 1, Align: 1, C: c("GoInt8")},
  1262. "uint8": {Size: 1, Align: 1, C: c("GoUint8")},
  1263. "int16": {Size: 2, Align: 2, C: c("GoInt16")},
  1264. "uint16": {Size: 2, Align: 2, C: c("GoUint16")},
  1265. "int32": {Size: 4, Align: 4, C: c("GoInt32")},
  1266. "uint32": {Size: 4, Align: 4, C: c("GoUint32")},
  1267. "int64": {Size: 8, Align: 8, C: c("GoInt64")},
  1268. "uint64": {Size: 8, Align: 8, C: c("GoUint64")},
  1269. "float32": {Size: 4, Align: 4, C: c("GoFloat32")},
  1270. "float64": {Size: 8, Align: 8, C: c("GoFloat64")},
  1271. "complex64": {Size: 8, Align: 4, C: c("GoComplex64")},
  1272. "complex128": {Size: 16, Align: 8, C: c("GoComplex128")},
  1273. }
  1274. // Map an ast type to a Type.
  1275. func (p *Package) cgoType(e ast.Expr) *Type {
  1276. switch t := e.(type) {
  1277. case *ast.StarExpr:
  1278. x := p.cgoType(t.X)
  1279. return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("%s*", x.C)}
  1280. case *ast.ArrayType:
  1281. if t.Len == nil {
  1282. // Slice: pointer, len, cap.
  1283. return &Type{Size: p.PtrSize * 3, Align: p.PtrSize, C: c("GoSlice")}
  1284. }
  1285. // Non-slice array types are not supported.
  1286. case *ast.StructType:
  1287. // Not supported.
  1288. case *ast.FuncType:
  1289. return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")}
  1290. case *ast.InterfaceType:
  1291. return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
  1292. case *ast.MapType:
  1293. return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoMap")}
  1294. case *ast.ChanType:
  1295. return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoChan")}
  1296. case *ast.Ident:
  1297. // Look up the type in the top level declarations.
  1298. // TODO: Handle types defined within a function.
  1299. for _, d := range p.Decl {
  1300. gd, ok := d.(*ast.GenDecl)
  1301. if !ok || gd.Tok != token.TYPE {
  1302. continue
  1303. }
  1304. for _, spec := range gd.Specs {
  1305. ts, ok := spec.(*ast.TypeSpec)
  1306. if !ok {
  1307. continue
  1308. }
  1309. if ts.Name.Name == t.Name {
  1310. return p.cgoType(ts.Type)
  1311. }
  1312. }
  1313. }
  1314. if def := typedef[t.Name]; def != nil {
  1315. return def
  1316. }
  1317. if t.Name == "uintptr" {
  1318. return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("GoUintptr")}
  1319. }
  1320. if t.Name == "string" {
  1321. // The string data is 1 pointer + 1 (pointer-sized) int.
  1322. return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoString")}
  1323. }
  1324. if t.Name == "error" {
  1325. return &Type{Size: 2 * p.PtrSize, Align: p.PtrSize, C: c("GoInterface")}
  1326. }
  1327. if r, ok := goTypes[t.Name]; ok {
  1328. if r.Size == 0 { // int or uint
  1329. rr := new(Type)
  1330. *rr = *r
  1331. rr.Size = p.IntSize
  1332. rr.Align = p.IntSize
  1333. r = rr
  1334. }
  1335. if r.Align > p.PtrSize {
  1336. r.Align = p.PtrSize
  1337. }
  1338. return r
  1339. }
  1340. error_(e.Pos(), "unrecognized Go type %s", t.Name)
  1341. return &Type{Size: 4, Align: 4, C: c("int")}
  1342. case *ast.SelectorExpr:
  1343. id, ok := t.X.(*ast.Ident)
  1344. if ok && id.Name == "unsafe" && t.Sel.Name == "Pointer" {
  1345. return &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*")}
  1346. }
  1347. }
  1348. error_(e.Pos(), "Go type not supported in export: %s", gofmt(e))
  1349. return &Type{Size: 4, Align: 4, C: c("int")}
  1350. }
  1351. const gccProlog = `
  1352. #line 1 "cgo-gcc-prolog"
  1353. /*
  1354. If x and y are not equal, the type will be invalid
  1355. (have a negative array count) and an inscrutable error will come
  1356. out of the compiler and hopefully mention "name".
  1357. */
  1358. #define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2UL+1UL];
  1359. /* Check at compile time that the sizes we use match our expectations. */
  1360. #define __cgo_size_assert(t, n) __cgo_compile_assert_eq(sizeof(t), (size_t)n, _cgo_sizeof_##t##_is_not_##n)
  1361. __cgo_size_assert(char, 1)
  1362. __cgo_size_assert(short, 2)
  1363. __cgo_size_assert(int, 4)
  1364. typedef long long __cgo_long_long;
  1365. __cgo_size_assert(__cgo_long_long, 8)
  1366. __cgo_size_assert(float, 4)
  1367. __cgo_size_assert(double, 8)
  1368. extern char* _cgo_topofstack(void);
  1369. /*
  1370. We use packed structs, but they are always aligned.
  1371. The pragmas and address-of-packed-member are only recognized as warning
  1372. groups in clang 4.0+, so ignore unknown pragmas first.
  1373. */
  1374. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  1375. #pragma GCC diagnostic ignored "-Wpragmas"
  1376. #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
  1377. #include <errno.h>
  1378. #include <string.h>
  1379. `
  1380. // Prologue defining TSAN functions in C.
  1381. const noTsanProlog = `
  1382. #define CGO_NO_SANITIZE_THREAD
  1383. #define _cgo_tsan_acquire()
  1384. #define _cgo_tsan_release()
  1385. `
  1386. // This must match the TSAN code in runtime/cgo/libcgo.h.
  1387. // This is used when the code is built with the C/C++ Thread SANitizer,
  1388. // which is not the same as the Go race detector.
  1389. // __tsan_acquire tells TSAN that we are acquiring a lock on a variable,
  1390. // in this case _cgo_sync. __tsan_release releases the lock.
  1391. // (There is no actual lock, we are just telling TSAN that there is.)
  1392. //
  1393. // When we call from Go to C we call _cgo_tsan_acquire.
  1394. // When the C function returns we call _cgo_tsan_release.
  1395. // Similarly, when C calls back into Go we call _cgo_tsan_release
  1396. // and then call _cgo_tsan_acquire when we return to C.
  1397. // These calls tell TSAN that there is a serialization point at the C call.
  1398. //
  1399. // This is necessary because TSAN, which is a C/C++ tool, can not see
  1400. // the synchronization in the Go code. Without these calls, when
  1401. // multiple goroutines call into C code, TSAN does not understand
  1402. // that the calls are properly synchronized on the Go side.
  1403. //
  1404. // To be clear, if the calls are not properly synchronized on the Go side,
  1405. // we will be hiding races. But when using TSAN on mixed Go C/C++ code
  1406. // it is more important to avoid false positives, which reduce confidence
  1407. // in the tool, than to avoid false negatives.
  1408. const yesTsanProlog = `
  1409. #line 1 "cgo-tsan-prolog"
  1410. #define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread))
  1411. long long _cgo_sync __attribute__ ((common));
  1412. extern void __tsan_acquire(void*);
  1413. extern void __tsan_release(void*);
  1414. __attribute__ ((unused))
  1415. static void _cgo_tsan_acquire() {
  1416. __tsan_acquire(&_cgo_sync);
  1417. }
  1418. __attribute__ ((unused))
  1419. static void _cgo_tsan_release() {
  1420. __tsan_release(&_cgo_sync);
  1421. }
  1422. `
  1423. // Set to yesTsanProlog if we see -fsanitize=thread in the flags for gcc.
  1424. var tsanProlog = noTsanProlog
  1425. // noMsanProlog is a prologue defining an MSAN function in C.
  1426. // This is used when not compiling with -fsanitize=memory.
  1427. const noMsanProlog = `
  1428. #define _cgo_msan_write(addr, sz)
  1429. `
  1430. // yesMsanProlog is a prologue defining an MSAN function in C.
  1431. // This is used when compiling with -fsanitize=memory.
  1432. // See the comment above where _cgo_msan_write is called.
  1433. const yesMsanProlog = `
  1434. extern void __msan_unpoison(const volatile void *, size_t);
  1435. #define _cgo_msan_write(addr, sz) __msan_unpoison((addr), (sz))
  1436. `
  1437. // msanProlog is set to yesMsanProlog if we see -fsanitize=memory in the flags
  1438. // for the C compiler.
  1439. var msanProlog = noMsanProlog
  1440. const builtinProlog = `
  1441. #line 1 "cgo-builtin-prolog"
  1442. #include <stddef.h> /* for ptrdiff_t and size_t below */
  1443. /* Define intgo when compiling with GCC. */
  1444. typedef ptrdiff_t intgo;
  1445. #define GO_CGO_GOSTRING_TYPEDEF
  1446. typedef struct { const char *p; intgo n; } _GoString_;
  1447. typedef struct { char *p; intgo n; intgo c; } _GoBytes_;
  1448. _GoString_ GoString(char *p);
  1449. _GoString_ GoStringN(char *p, int l);
  1450. _GoBytes_ GoBytes(void *p, int n);
  1451. char *CString(_GoString_);
  1452. void *CBytes(_GoBytes_);
  1453. void *_CMalloc(size_t);
  1454. __attribute__ ((unused))
  1455. static size_t _GoStringLen(_GoString_ s) { return (size_t)s.n; }
  1456. __attribute__ ((unused))
  1457. static const char *_GoStringPtr(_GoString_ s) { return s.p; }
  1458. `
  1459. const goProlog = `
  1460. //go:linkname _cgo_runtime_cgocall runtime.cgocall
  1461. func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32
  1462. //go:linkname _cgoCheckPointer runtime.cgoCheckPointer
  1463. func _cgoCheckPointer(interface{}, interface{})
  1464. //go:linkname _cgoCheckResult runtime.cgoCheckResult
  1465. func _cgoCheckResult(interface{})
  1466. `
  1467. const gccgoGoProlog = `
  1468. func _cgoCheckPointer(interface{}, interface{})
  1469. func _cgoCheckResult(interface{})
  1470. `
  1471. const goStringDef = `
  1472. //go:linkname _cgo_runtime_gostring runtime.gostring
  1473. func _cgo_runtime_gostring(*_Ctype_char) string
  1474. func _Cfunc_GoString(p *_Ctype_char) string {
  1475. return _cgo_runtime_gostring(p)
  1476. }
  1477. `
  1478. const goStringNDef = `
  1479. //go:linkname _cgo_runtime_gostringn runtime.gostringn
  1480. func _cgo_runtime_gostringn(*_Ctype_char, int) string
  1481. func _Cfunc_GoStringN(p *_Ctype_char, l _Ctype_int) string {
  1482. return _cgo_runtime_gostringn(p, int(l))
  1483. }
  1484. `
  1485. const goBytesDef = `
  1486. //go:linkname _cgo_runtime_gobytes runtime.gobytes
  1487. func _cgo_runtime_gobytes(unsafe.Pointer, int) []byte
  1488. func _Cfunc_GoBytes(p unsafe.Pointer, l _Ctype_int) []byte {
  1489. return _cgo_runtime_gobytes(p, int(l))
  1490. }
  1491. `
  1492. const cStringDef = `
  1493. func _Cfunc_CString(s string) *_Ctype_char {
  1494. p := _cgo_cmalloc(uint64(len(s)+1))
  1495. pp := (*[1<<30]byte)(p)
  1496. copy(pp[:], s)
  1497. pp[len(s)] = 0
  1498. return (*_Ctype_char)(p)
  1499. }
  1500. `
  1501. const cBytesDef = `
  1502. func _Cfunc_CBytes(b []byte) unsafe.Pointer {
  1503. p := _cgo_cmalloc(uint64(len(b)))
  1504. pp := (*[1<<30]byte)(p)
  1505. copy(pp[:], b)
  1506. return p
  1507. }
  1508. `
  1509. const cMallocDef = `
  1510. func _Cfunc__CMalloc(n _Ctype_size_t) unsafe.Pointer {
  1511. return _cgo_cmalloc(uint64(n))
  1512. }
  1513. `
  1514. var builtinDefs = map[string]string{
  1515. "GoString": goStringDef,
  1516. "GoStringN": goStringNDef,
  1517. "GoBytes": goBytesDef,
  1518. "CString": cStringDef,
  1519. "CBytes": cBytesDef,
  1520. "_CMalloc": cMallocDef,
  1521. }
  1522. // Definitions for C.malloc in Go and in C. We define it ourselves
  1523. // since we call it from functions we define, such as C.CString.
  1524. // Also, we have historically ensured that C.malloc does not return
  1525. // nil even for an allocation of 0.
  1526. const cMallocDefGo = `
  1527. //go:cgo_import_static _cgoPREFIX_Cfunc__Cmalloc
  1528. //go:linkname __cgofn__cgoPREFIX_Cfunc__Cmalloc _cgoPREFIX_Cfunc__Cmalloc
  1529. var __cgofn__cgoPREFIX_Cfunc__Cmalloc byte
  1530. var _cgoPREFIX_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgoPREFIX_Cfunc__Cmalloc)
  1531. //go:linkname runtime_throw runtime.throw
  1532. func runtime_throw(string)
  1533. //go:cgo_unsafe_args
  1534. func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
  1535. _cgo_runtime_cgocall(_cgoPREFIX_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
  1536. if r1 == nil {
  1537. runtime_throw("runtime: C malloc failed")
  1538. }
  1539. return
  1540. }
  1541. `
  1542. // cMallocDefC defines the C version of C.malloc for the gc compiler.
  1543. // It is defined here because C.CString and friends need a definition.
  1544. // We define it by hand, rather than simply inventing a reference to
  1545. // C.malloc, because <stdlib.h> may not have been included.
  1546. // This is approximately what writeOutputFunc would generate, but
  1547. // skips the cgo_topofstack code (which is only needed if the C code
  1548. // calls back into Go). This also avoids returning nil for an
  1549. // allocation of 0 bytes.
  1550. const cMallocDefC = `
  1551. CGO_NO_SANITIZE_THREAD
  1552. void _cgoPREFIX_Cfunc__Cmalloc(void *v) {
  1553. struct {
  1554. unsigned long long p0;
  1555. void *r1;
  1556. } PACKED *a = v;
  1557. void *ret;
  1558. _cgo_tsan_acquire();
  1559. ret = malloc(a->p0);
  1560. if (ret == 0 && a->p0 == 0) {
  1561. ret = malloc(1);
  1562. }
  1563. a->r1 = ret;
  1564. _cgo_tsan_release();
  1565. }
  1566. `
  1567. func (p *Package) cPrologGccgo() string {
  1568. r := strings.NewReplacer(
  1569. "PREFIX", cPrefix,
  1570. "GCCGOSYMBOLPREF", p.gccgoSymbolPrefix(),
  1571. "_cgoCheckPointer", gccgoToSymbol("_cgoCheckPointer"),
  1572. "_cgoCheckResult", gccgoToSymbol("_cgoCheckResult"))
  1573. return r.Replace(cPrologGccgo)
  1574. }
  1575. const cPrologGccgo = `
  1576. #line 1 "cgo-c-prolog-gccgo"
  1577. #include <stdint.h>
  1578. #include <stdlib.h>
  1579. #include <string.h>
  1580. typedef unsigned char byte;
  1581. typedef intptr_t intgo;
  1582. struct __go_string {
  1583. const unsigned char *__data;
  1584. intgo __length;
  1585. };
  1586. typedef struct __go_open_array {
  1587. void* __values;
  1588. intgo __count;
  1589. intgo __capacity;
  1590. } Slice;
  1591. struct __go_string __go_byte_array_to_string(const void* p, intgo len);
  1592. struct __go_open_array __go_string_to_byte_array (struct __go_string str);
  1593. extern void runtime_throw(const char *);
  1594. const char *_cgoPREFIX_Cfunc_CString(struct __go_string s) {
  1595. char *p = malloc(s.__length+1);
  1596. if(p == NULL)
  1597. runtime_throw("runtime: C malloc failed");
  1598. memmove(p, s.__data, s.__length);
  1599. p[s.__length] = 0;
  1600. return p;
  1601. }
  1602. void *_cgoPREFIX_Cfunc_CBytes(struct __go_open_array b) {
  1603. char *p = malloc(b.__count);
  1604. if(p == NULL)
  1605. runtime_throw("runtime: C malloc failed");
  1606. memmove(p, b.__values, b.__count);
  1607. return p;
  1608. }
  1609. struct __go_string _cgoPREFIX_Cfunc_GoString(char *p) {
  1610. intgo len = (p != NULL) ? strlen(p) : 0;
  1611. return __go_byte_array_to_string(p, len);
  1612. }
  1613. struct __go_string _cgoPREFIX_Cfunc_GoStringN(char *p, int32_t n) {
  1614. return __go_byte_array_to_string(p, n);
  1615. }
  1616. Slice _cgoPREFIX_Cfunc_GoBytes(char *p, int32_t n) {
  1617. struct __go_string s = { (const unsigned char *)p, n };
  1618. return __go_string_to_byte_array(s);
  1619. }
  1620. void *_cgoPREFIX_Cfunc__CMalloc(size_t n) {
  1621. void *p = malloc(n);
  1622. if(p == NULL && n == 0)
  1623. p = malloc(1);
  1624. if(p == NULL)
  1625. runtime_throw("runtime: C malloc failed");
  1626. return p;
  1627. }
  1628. struct __go_type_descriptor;
  1629. typedef struct __go_empty_interface {
  1630. const struct __go_type_descriptor *__type_descriptor;
  1631. void *__object;
  1632. } Eface;
  1633. extern void runtimeCgoCheckPointer(Eface, Eface)
  1634. __asm__("runtime.cgoCheckPointer")
  1635. __attribute__((weak));
  1636. extern void localCgoCheckPointer(Eface, Eface)
  1637. __asm__("GCCGOSYMBOLPREF._cgoCheckPointer");
  1638. void localCgoCheckPointer(Eface ptr, Eface arg) {
  1639. if(runtimeCgoCheckPointer) {
  1640. runtimeCgoCheckPointer(ptr, arg);
  1641. }
  1642. }
  1643. extern void runtimeCgoCheckResult(Eface)
  1644. __asm__("runtime.cgoCheckResult")
  1645. __attribute__((weak));
  1646. extern void localCgoCheckResult(Eface)
  1647. __asm__("GCCGOSYMBOLPREF._cgoCheckResult");
  1648. void localCgoCheckResult(Eface val) {
  1649. if(runtimeCgoCheckResult) {
  1650. runtimeCgoCheckResult(val);
  1651. }
  1652. }
  1653. `
  1654. // builtinExportProlog is a shorter version of builtinProlog,
  1655. // to be put into the _cgo_export.h file.
  1656. // For historical reasons we can't use builtinProlog in _cgo_export.h,
  1657. // because _cgo_export.h defines GoString as a struct while builtinProlog
  1658. // defines it as a function. We don't change this to avoid unnecessarily
  1659. // breaking existing code.
  1660. // The test of GO_CGO_GOSTRING_TYPEDEF avoids a duplicate definition
  1661. // error if a Go file with a cgo comment #include's the export header
  1662. // generated by a different package.
  1663. const builtinExportProlog = `
  1664. #line 1 "cgo-builtin-export-prolog"
  1665. #include <stddef.h> /* for ptrdiff_t below */
  1666. #ifndef GO_CGO_EXPORT_PROLOGUE_H
  1667. #define GO_CGO_EXPORT_PROLOGUE_H
  1668. #ifndef GO_CGO_GOSTRING_TYPEDEF
  1669. typedef struct { const char *p; ptrdiff_t n; } _GoString_;
  1670. #endif
  1671. #endif
  1672. `
  1673. func (p *Package) gccExportHeaderProlog() string {
  1674. return strings.Replace(gccExportHeaderProlog, "GOINTBITS", fmt.Sprint(8*p.IntSize), -1)
  1675. }
  1676. // gccExportHeaderProlog is written to the exported header, after the
  1677. // import "C" comment preamble but before the generated declarations
  1678. // of exported functions. This permits the generated declarations to
  1679. // use the type names that appear in goTypes, above.
  1680. //
  1681. // The test of GO_CGO_GOSTRING_TYPEDEF avoids a duplicate definition
  1682. // error if a Go file with a cgo comment #include's the export header
  1683. // generated by a different package. Unfortunately GoString means two
  1684. // different things: in this prolog it means a C name for the Go type,
  1685. // while in the prolog written into the start of the C code generated
  1686. // from a cgo-using Go file it means the C.GoString function. There is
  1687. // no way to resolve this conflict, but it also doesn't make much
  1688. // difference, as Go code never wants to refer to the latter meaning.
  1689. const gccExportHeaderProlog = `
  1690. /* Start of boilerplate cgo prologue. */
  1691. #line 1 "cgo-gcc-export-header-prolog"
  1692. #ifndef GO_CGO_PROLOGUE_H
  1693. #define GO_CGO_PROLOGUE_H
  1694. typedef signed char GoInt8;
  1695. typedef unsigned char GoUint8;
  1696. typedef short GoInt16;
  1697. typedef unsigned short GoUint16;
  1698. typedef int GoInt32;
  1699. typedef unsigned int GoUint32;
  1700. typedef long long GoInt64;
  1701. typedef unsigned long long GoUint64;
  1702. typedef GoIntGOINTBITS GoInt;
  1703. typedef GoUintGOINTBITS GoUint;
  1704. typedef __SIZE_TYPE__ GoUintptr;
  1705. typedef float GoFloat32;
  1706. typedef double GoFloat64;
  1707. typedef float _Complex GoComplex64;
  1708. typedef double _Complex GoComplex128;
  1709. /*
  1710. static assertion to make sure the file is being used on architecture
  1711. at least with matching size of GoInt.
  1712. */
  1713. typedef char _check_for_GOINTBITS_bit_pointer_matching_GoInt[sizeof(void*)==GOINTBITS/8 ? 1:-1];
  1714. #ifndef GO_CGO_GOSTRING_TYPEDEF
  1715. typedef _GoString_ GoString;
  1716. #endif
  1717. typedef void *GoMap;
  1718. typedef void *GoChan;
  1719. typedef struct { void *t; void *v; } GoInterface;
  1720. typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
  1721. #endif
  1722. /* End of boilerplate cgo prologue. */
  1723. #ifdef __cplusplus
  1724. extern "C" {
  1725. #endif
  1726. `
  1727. // gccExportHeaderEpilog goes at the end of the generated header file.
  1728. const gccExportHeaderEpilog = `
  1729. #ifdef __cplusplus
  1730. }
  1731. #endif
  1732. `
  1733. // gccgoExportFileProlog is written to the _cgo_export.c file when
  1734. // using gccgo.
  1735. // We use weak declarations, and test the addresses, so that this code
  1736. // works with older versions of gccgo.
  1737. const gccgoExportFileProlog = `
  1738. #line 1 "cgo-gccgo-export-file-prolog"
  1739. extern _Bool runtime_iscgo __attribute__ ((weak));
  1740. static void GoInit(void) __attribute__ ((constructor));
  1741. static void GoInit(void) {
  1742. if(&runtime_iscgo)
  1743. runtime_iscgo = 1;
  1744. }
  1745. extern __SIZE_TYPE__ _cgo_wait_runtime_init_done(void) __attribute__ ((weak));
  1746. `