gcc.go 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397
  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. // Annotate Ref in Prog with C types by parsing gcc debug output.
  5. // Conversion of debug output to Go types.
  6. package main
  7. import (
  8. "bytes"
  9. "debug/dwarf"
  10. "debug/elf"
  11. "debug/macho"
  12. "debug/pe"
  13. "encoding/binary"
  14. "errors"
  15. "flag"
  16. "fmt"
  17. "go/ast"
  18. "go/parser"
  19. "go/token"
  20. "internal/xcoff"
  21. "math"
  22. "os"
  23. "os/exec"
  24. "strconv"
  25. "strings"
  26. "unicode"
  27. "unicode/utf8"
  28. "cmd/internal/quoted"
  29. )
  30. var debugDefine = flag.Bool("debug-define", false, "print relevant #defines")
  31. var debugGcc = flag.Bool("debug-gcc", false, "print gcc invocations")
  32. var nameToC = map[string]string{
  33. "schar": "signed char",
  34. "uchar": "unsigned char",
  35. "ushort": "unsigned short",
  36. "uint": "unsigned int",
  37. "ulong": "unsigned long",
  38. "longlong": "long long",
  39. "ulonglong": "unsigned long long",
  40. "complexfloat": "float _Complex",
  41. "complexdouble": "double _Complex",
  42. }
  43. // cname returns the C name to use for C.s.
  44. // The expansions are listed in nameToC and also
  45. // struct_foo becomes "struct foo", and similarly for
  46. // union and enum.
  47. func cname(s string) string {
  48. if t, ok := nameToC[s]; ok {
  49. return t
  50. }
  51. if strings.HasPrefix(s, "struct_") {
  52. return "struct " + s[len("struct_"):]
  53. }
  54. if strings.HasPrefix(s, "union_") {
  55. return "union " + s[len("union_"):]
  56. }
  57. if strings.HasPrefix(s, "enum_") {
  58. return "enum " + s[len("enum_"):]
  59. }
  60. if strings.HasPrefix(s, "sizeof_") {
  61. return "sizeof(" + cname(s[len("sizeof_"):]) + ")"
  62. }
  63. return s
  64. }
  65. // DiscardCgoDirectives processes the import C preamble, and discards
  66. // all #cgo CFLAGS and LDFLAGS directives, so they don't make their
  67. // way into _cgo_export.h.
  68. func (f *File) DiscardCgoDirectives() {
  69. linesIn := strings.Split(f.Preamble, "\n")
  70. linesOut := make([]string, 0, len(linesIn))
  71. for _, line := range linesIn {
  72. l := strings.TrimSpace(line)
  73. if len(l) < 5 || l[:4] != "#cgo" || !unicode.IsSpace(rune(l[4])) {
  74. linesOut = append(linesOut, line)
  75. } else {
  76. linesOut = append(linesOut, "")
  77. }
  78. }
  79. f.Preamble = strings.Join(linesOut, "\n")
  80. }
  81. // addToFlag appends args to flag. All flags are later written out onto the
  82. // _cgo_flags file for the build system to use.
  83. func (p *Package) addToFlag(flag string, args []string) {
  84. if flag == "CFLAGS" {
  85. // We'll also need these when preprocessing for dwarf information.
  86. // However, discard any -g options: we need to be able
  87. // to parse the debug info, so stick to what we expect.
  88. for _, arg := range args {
  89. if !strings.HasPrefix(arg, "-g") {
  90. p.GccOptions = append(p.GccOptions, arg)
  91. }
  92. }
  93. }
  94. skip := false
  95. for i, arg := range args {
  96. // The go tool will pass us a -I option pointing to objdir;
  97. // we don't need to record that for later, as the objdir
  98. // will disappear anyhow.
  99. if skip {
  100. // Discard argument in "-I objdir" case.
  101. skip = false
  102. } else if strings.HasPrefix(arg, "-I") && strings.HasPrefix(arg[2:], *objDir) {
  103. // This is -Iobjdir. Don't save this argument.
  104. } else if arg == "-I" && i+1 < len(args) && strings.HasPrefix(args[i+1], *objDir) {
  105. // This is -I objdir. Don't save this argument
  106. // or the next one.
  107. skip = true
  108. } else {
  109. p.CgoFlags[flag] = append(p.CgoFlags[flag], arg)
  110. }
  111. }
  112. }
  113. // splitQuoted splits the string s around each instance of one or more consecutive
  114. // white space characters while taking into account quotes and escaping, and
  115. // returns an array of substrings of s or an empty list if s contains only white space.
  116. // Single quotes and double quotes are recognized to prevent splitting within the
  117. // quoted region, and are removed from the resulting substrings. If a quote in s
  118. // isn't closed err will be set and r will have the unclosed argument as the
  119. // last element. The backslash is used for escaping.
  120. //
  121. // For example, the following string:
  122. //
  123. // `a b:"c d" 'e''f' "g\""`
  124. //
  125. // Would be parsed as:
  126. //
  127. // []string{"a", "b:c d", "ef", `g"`}
  128. //
  129. func splitQuoted(s string) (r []string, err error) {
  130. var args []string
  131. arg := make([]rune, len(s))
  132. escaped := false
  133. quoted := false
  134. quote := '\x00'
  135. i := 0
  136. for _, r := range s {
  137. switch {
  138. case escaped:
  139. escaped = false
  140. case r == '\\':
  141. escaped = true
  142. continue
  143. case quote != 0:
  144. if r == quote {
  145. quote = 0
  146. continue
  147. }
  148. case r == '"' || r == '\'':
  149. quoted = true
  150. quote = r
  151. continue
  152. case unicode.IsSpace(r):
  153. if quoted || i > 0 {
  154. quoted = false
  155. args = append(args, string(arg[:i]))
  156. i = 0
  157. }
  158. continue
  159. }
  160. arg[i] = r
  161. i++
  162. }
  163. if quoted || i > 0 {
  164. args = append(args, string(arg[:i]))
  165. }
  166. if quote != 0 {
  167. err = errors.New("unclosed quote")
  168. } else if escaped {
  169. err = errors.New("unfinished escaping")
  170. }
  171. return args, err
  172. }
  173. // Translate rewrites f.AST, the original Go input, to remove
  174. // references to the imported package C, replacing them with
  175. // references to the equivalent Go types, functions, and variables.
  176. func (p *Package) Translate(f *File) {
  177. for _, cref := range f.Ref {
  178. // Convert C.ulong to C.unsigned long, etc.
  179. cref.Name.C = cname(cref.Name.Go)
  180. }
  181. var conv typeConv
  182. conv.Init(p.PtrSize, p.IntSize)
  183. p.loadDefines(f)
  184. p.typedefs = map[string]bool{}
  185. p.typedefList = nil
  186. numTypedefs := -1
  187. for len(p.typedefs) > numTypedefs {
  188. numTypedefs = len(p.typedefs)
  189. // Also ask about any typedefs we've seen so far.
  190. for _, info := range p.typedefList {
  191. if f.Name[info.typedef] != nil {
  192. continue
  193. }
  194. n := &Name{
  195. Go: info.typedef,
  196. C: info.typedef,
  197. }
  198. f.Name[info.typedef] = n
  199. f.NamePos[n] = info.pos
  200. }
  201. needType := p.guessKinds(f)
  202. if len(needType) > 0 {
  203. p.loadDWARF(f, &conv, needType)
  204. }
  205. // In godefs mode we're OK with the typedefs, which
  206. // will presumably also be defined in the file, we
  207. // don't want to resolve them to their base types.
  208. if *godefs {
  209. break
  210. }
  211. }
  212. p.prepareNames(f)
  213. if p.rewriteCalls(f) {
  214. // Add `import _cgo_unsafe "unsafe"` after the package statement.
  215. f.Edit.Insert(f.offset(f.AST.Name.End()), "; import _cgo_unsafe \"unsafe\"")
  216. }
  217. p.rewriteRef(f)
  218. }
  219. // loadDefines coerces gcc into spitting out the #defines in use
  220. // in the file f and saves relevant renamings in f.Name[name].Define.
  221. func (p *Package) loadDefines(f *File) {
  222. var b bytes.Buffer
  223. b.WriteString(builtinProlog)
  224. b.WriteString(f.Preamble)
  225. stdout := p.gccDefines(b.Bytes())
  226. for _, line := range strings.Split(stdout, "\n") {
  227. if len(line) < 9 || line[0:7] != "#define" {
  228. continue
  229. }
  230. line = strings.TrimSpace(line[8:])
  231. var key, val string
  232. spaceIndex := strings.Index(line, " ")
  233. tabIndex := strings.Index(line, "\t")
  234. if spaceIndex == -1 && tabIndex == -1 {
  235. continue
  236. } else if tabIndex == -1 || (spaceIndex != -1 && spaceIndex < tabIndex) {
  237. key = line[0:spaceIndex]
  238. val = strings.TrimSpace(line[spaceIndex:])
  239. } else {
  240. key = line[0:tabIndex]
  241. val = strings.TrimSpace(line[tabIndex:])
  242. }
  243. if key == "__clang__" {
  244. p.GccIsClang = true
  245. }
  246. if n := f.Name[key]; n != nil {
  247. if *debugDefine {
  248. fmt.Fprintf(os.Stderr, "#define %s %s\n", key, val)
  249. }
  250. n.Define = val
  251. }
  252. }
  253. }
  254. // guessKinds tricks gcc into revealing the kind of each
  255. // name xxx for the references C.xxx in the Go input.
  256. // The kind is either a constant, type, or variable.
  257. func (p *Package) guessKinds(f *File) []*Name {
  258. // Determine kinds for names we already know about,
  259. // like #defines or 'struct foo', before bothering with gcc.
  260. var names, needType []*Name
  261. optional := map[*Name]bool{}
  262. for _, key := range nameKeys(f.Name) {
  263. n := f.Name[key]
  264. // If we've already found this name as a #define
  265. // and we can translate it as a constant value, do so.
  266. if n.Define != "" {
  267. if i, err := strconv.ParseInt(n.Define, 0, 64); err == nil {
  268. n.Kind = "iconst"
  269. // Turn decimal into hex, just for consistency
  270. // with enum-derived constants. Otherwise
  271. // in the cgo -godefs output half the constants
  272. // are in hex and half are in whatever the #define used.
  273. n.Const = fmt.Sprintf("%#x", i)
  274. } else if n.Define[0] == '\'' {
  275. if _, err := parser.ParseExpr(n.Define); err == nil {
  276. n.Kind = "iconst"
  277. n.Const = n.Define
  278. }
  279. } else if n.Define[0] == '"' {
  280. if _, err := parser.ParseExpr(n.Define); err == nil {
  281. n.Kind = "sconst"
  282. n.Const = n.Define
  283. }
  284. }
  285. if n.IsConst() {
  286. continue
  287. }
  288. }
  289. // If this is a struct, union, or enum type name, no need to guess the kind.
  290. if strings.HasPrefix(n.C, "struct ") || strings.HasPrefix(n.C, "union ") || strings.HasPrefix(n.C, "enum ") {
  291. n.Kind = "type"
  292. needType = append(needType, n)
  293. continue
  294. }
  295. if (goos == "darwin" || goos == "ios") && strings.HasSuffix(n.C, "Ref") {
  296. // For FooRef, find out if FooGetTypeID exists.
  297. s := n.C[:len(n.C)-3] + "GetTypeID"
  298. n := &Name{Go: s, C: s}
  299. names = append(names, n)
  300. optional[n] = true
  301. }
  302. // Otherwise, we'll need to find out from gcc.
  303. names = append(names, n)
  304. }
  305. // Bypass gcc if there's nothing left to find out.
  306. if len(names) == 0 {
  307. return needType
  308. }
  309. // Coerce gcc into telling us whether each name is a type, a value, or undeclared.
  310. // For names, find out whether they are integer constants.
  311. // We used to look at specific warning or error messages here, but that tied the
  312. // behavior too closely to specific versions of the compilers.
  313. // Instead, arrange that we can infer what we need from only the presence or absence
  314. // of an error on a specific line.
  315. //
  316. // For each name, we generate these lines, where xxx is the index in toSniff plus one.
  317. //
  318. // #line xxx "not-declared"
  319. // void __cgo_f_xxx_1(void) { __typeof__(name) *__cgo_undefined__1; }
  320. // #line xxx "not-type"
  321. // void __cgo_f_xxx_2(void) { name *__cgo_undefined__2; }
  322. // #line xxx "not-int-const"
  323. // void __cgo_f_xxx_3(void) { enum { __cgo_undefined__3 = (name)*1 }; }
  324. // #line xxx "not-num-const"
  325. // void __cgo_f_xxx_4(void) { static const double __cgo_undefined__4 = (name); }
  326. // #line xxx "not-str-lit"
  327. // void __cgo_f_xxx_5(void) { static const char __cgo_undefined__5[] = (name); }
  328. //
  329. // If we see an error at not-declared:xxx, the corresponding name is not declared.
  330. // If we see an error at not-type:xxx, the corresponding name is not a type.
  331. // If we see an error at not-int-const:xxx, the corresponding name is not an integer constant.
  332. // If we see an error at not-num-const:xxx, the corresponding name is not a number constant.
  333. // If we see an error at not-str-lit:xxx, the corresponding name is not a string literal.
  334. //
  335. // The specific input forms are chosen so that they are valid C syntax regardless of
  336. // whether name denotes a type or an expression.
  337. var b bytes.Buffer
  338. b.WriteString(builtinProlog)
  339. b.WriteString(f.Preamble)
  340. for i, n := range names {
  341. fmt.Fprintf(&b, "#line %d \"not-declared\"\n"+
  342. "void __cgo_f_%d_1(void) { __typeof__(%s) *__cgo_undefined__1; }\n"+
  343. "#line %d \"not-type\"\n"+
  344. "void __cgo_f_%d_2(void) { %s *__cgo_undefined__2; }\n"+
  345. "#line %d \"not-int-const\"\n"+
  346. "void __cgo_f_%d_3(void) { enum { __cgo_undefined__3 = (%s)*1 }; }\n"+
  347. "#line %d \"not-num-const\"\n"+
  348. "void __cgo_f_%d_4(void) { static const double __cgo_undefined__4 = (%s); }\n"+
  349. "#line %d \"not-str-lit\"\n"+
  350. "void __cgo_f_%d_5(void) { static const char __cgo_undefined__5[] = (%s); }\n",
  351. i+1, i+1, n.C,
  352. i+1, i+1, n.C,
  353. i+1, i+1, n.C,
  354. i+1, i+1, n.C,
  355. i+1, i+1, n.C,
  356. )
  357. }
  358. fmt.Fprintf(&b, "#line 1 \"completed\"\n"+
  359. "int __cgo__1 = __cgo__2;\n")
  360. // We need to parse the output from this gcc command, so ensure that it
  361. // doesn't have any ANSI escape sequences in it. (TERM=dumb is
  362. // insufficient; if the user specifies CGO_CFLAGS=-fdiagnostics-color,
  363. // GCC will ignore TERM, and GCC can also be configured at compile-time
  364. // to ignore TERM.)
  365. stderr := p.gccErrors(b.Bytes(), "-fdiagnostics-color=never")
  366. if strings.Contains(stderr, "unrecognized command line option") {
  367. // We're using an old version of GCC that doesn't understand
  368. // -fdiagnostics-color. Those versions can't print color anyway,
  369. // so just rerun without that option.
  370. stderr = p.gccErrors(b.Bytes())
  371. }
  372. if stderr == "" {
  373. fatalf("%s produced no output\non input:\n%s", gccBaseCmd[0], b.Bytes())
  374. }
  375. completed := false
  376. sniff := make([]int, len(names))
  377. const (
  378. notType = 1 << iota
  379. notIntConst
  380. notNumConst
  381. notStrLiteral
  382. notDeclared
  383. )
  384. sawUnmatchedErrors := false
  385. for _, line := range strings.Split(stderr, "\n") {
  386. // Ignore warnings and random comments, with one
  387. // exception: newer GCC versions will sometimes emit
  388. // an error on a macro #define with a note referring
  389. // to where the expansion occurs. We care about where
  390. // the expansion occurs, so in that case treat the note
  391. // as an error.
  392. isError := strings.Contains(line, ": error:")
  393. isErrorNote := strings.Contains(line, ": note:") && sawUnmatchedErrors
  394. if !isError && !isErrorNote {
  395. continue
  396. }
  397. c1 := strings.Index(line, ":")
  398. if c1 < 0 {
  399. continue
  400. }
  401. c2 := strings.Index(line[c1+1:], ":")
  402. if c2 < 0 {
  403. continue
  404. }
  405. c2 += c1 + 1
  406. filename := line[:c1]
  407. i, _ := strconv.Atoi(line[c1+1 : c2])
  408. i--
  409. if i < 0 || i >= len(names) {
  410. if isError {
  411. sawUnmatchedErrors = true
  412. }
  413. continue
  414. }
  415. switch filename {
  416. case "completed":
  417. // Strictly speaking, there is no guarantee that seeing the error at completed:1
  418. // (at the end of the file) means we've seen all the errors from earlier in the file,
  419. // but usually it does. Certainly if we don't see the completed:1 error, we did
  420. // not get all the errors we expected.
  421. completed = true
  422. case "not-declared":
  423. sniff[i] |= notDeclared
  424. case "not-type":
  425. sniff[i] |= notType
  426. case "not-int-const":
  427. sniff[i] |= notIntConst
  428. case "not-num-const":
  429. sniff[i] |= notNumConst
  430. case "not-str-lit":
  431. sniff[i] |= notStrLiteral
  432. default:
  433. if isError {
  434. sawUnmatchedErrors = true
  435. }
  436. continue
  437. }
  438. sawUnmatchedErrors = false
  439. }
  440. if !completed {
  441. fatalf("%s did not produce error at completed:1\non input:\n%s\nfull error output:\n%s", gccBaseCmd[0], b.Bytes(), stderr)
  442. }
  443. for i, n := range names {
  444. switch sniff[i] {
  445. default:
  446. if sniff[i]&notDeclared != 0 && optional[n] {
  447. // Ignore optional undeclared identifiers.
  448. // Don't report an error, and skip adding n to the needType array.
  449. continue
  450. }
  451. error_(f.NamePos[n], "could not determine kind of name for C.%s", fixGo(n.Go))
  452. case notStrLiteral | notType:
  453. n.Kind = "iconst"
  454. case notIntConst | notStrLiteral | notType:
  455. n.Kind = "fconst"
  456. case notIntConst | notNumConst | notType:
  457. n.Kind = "sconst"
  458. case notIntConst | notNumConst | notStrLiteral:
  459. n.Kind = "type"
  460. case notIntConst | notNumConst | notStrLiteral | notType:
  461. n.Kind = "not-type"
  462. }
  463. needType = append(needType, n)
  464. }
  465. if nerrors > 0 {
  466. // Check if compiling the preamble by itself causes any errors,
  467. // because the messages we've printed out so far aren't helpful
  468. // to users debugging preamble mistakes. See issue 8442.
  469. preambleErrors := p.gccErrors([]byte(f.Preamble))
  470. if len(preambleErrors) > 0 {
  471. error_(token.NoPos, "\n%s errors for preamble:\n%s", gccBaseCmd[0], preambleErrors)
  472. }
  473. fatalf("unresolved names")
  474. }
  475. return needType
  476. }
  477. // loadDWARF parses the DWARF debug information generated
  478. // by gcc to learn the details of the constants, variables, and types
  479. // being referred to as C.xxx.
  480. func (p *Package) loadDWARF(f *File, conv *typeConv, names []*Name) {
  481. // Extract the types from the DWARF section of an object
  482. // from a well-formed C program. Gcc only generates DWARF info
  483. // for symbols in the object file, so it is not enough to print the
  484. // preamble and hope the symbols we care about will be there.
  485. // Instead, emit
  486. // __typeof__(names[i]) *__cgo__i;
  487. // for each entry in names and then dereference the type we
  488. // learn for __cgo__i.
  489. var b bytes.Buffer
  490. b.WriteString(builtinProlog)
  491. b.WriteString(f.Preamble)
  492. b.WriteString("#line 1 \"cgo-dwarf-inference\"\n")
  493. for i, n := range names {
  494. fmt.Fprintf(&b, "__typeof__(%s) *__cgo__%d;\n", n.C, i)
  495. if n.Kind == "iconst" {
  496. fmt.Fprintf(&b, "enum { __cgo_enum__%d = %s };\n", i, n.C)
  497. }
  498. }
  499. // We create a data block initialized with the values,
  500. // so we can read them out of the object file.
  501. fmt.Fprintf(&b, "long long __cgodebug_ints[] = {\n")
  502. for _, n := range names {
  503. if n.Kind == "iconst" {
  504. fmt.Fprintf(&b, "\t%s,\n", n.C)
  505. } else {
  506. fmt.Fprintf(&b, "\t0,\n")
  507. }
  508. }
  509. // for the last entry, we cannot use 0, otherwise
  510. // in case all __cgodebug_data is zero initialized,
  511. // LLVM-based gcc will place the it in the __DATA.__common
  512. // zero-filled section (our debug/macho doesn't support
  513. // this)
  514. fmt.Fprintf(&b, "\t1\n")
  515. fmt.Fprintf(&b, "};\n")
  516. // do the same work for floats.
  517. fmt.Fprintf(&b, "double __cgodebug_floats[] = {\n")
  518. for _, n := range names {
  519. if n.Kind == "fconst" {
  520. fmt.Fprintf(&b, "\t%s,\n", n.C)
  521. } else {
  522. fmt.Fprintf(&b, "\t0,\n")
  523. }
  524. }
  525. fmt.Fprintf(&b, "\t1\n")
  526. fmt.Fprintf(&b, "};\n")
  527. // do the same work for strings.
  528. for i, n := range names {
  529. if n.Kind == "sconst" {
  530. fmt.Fprintf(&b, "const char __cgodebug_str__%d[] = %s;\n", i, n.C)
  531. fmt.Fprintf(&b, "const unsigned long long __cgodebug_strlen__%d = sizeof(%s)-1;\n", i, n.C)
  532. }
  533. }
  534. d, ints, floats, strs := p.gccDebug(b.Bytes(), len(names))
  535. // Scan DWARF info for top-level TagVariable entries with AttrName __cgo__i.
  536. types := make([]dwarf.Type, len(names))
  537. r := d.Reader()
  538. for {
  539. e, err := r.Next()
  540. if err != nil {
  541. fatalf("reading DWARF entry: %s", err)
  542. }
  543. if e == nil {
  544. break
  545. }
  546. switch e.Tag {
  547. case dwarf.TagVariable:
  548. name, _ := e.Val(dwarf.AttrName).(string)
  549. typOff, _ := e.Val(dwarf.AttrType).(dwarf.Offset)
  550. if name == "" || typOff == 0 {
  551. if e.Val(dwarf.AttrSpecification) != nil {
  552. // Since we are reading all the DWARF,
  553. // assume we will see the variable elsewhere.
  554. break
  555. }
  556. fatalf("malformed DWARF TagVariable entry")
  557. }
  558. if !strings.HasPrefix(name, "__cgo__") {
  559. break
  560. }
  561. typ, err := d.Type(typOff)
  562. if err != nil {
  563. fatalf("loading DWARF type: %s", err)
  564. }
  565. t, ok := typ.(*dwarf.PtrType)
  566. if !ok || t == nil {
  567. fatalf("internal error: %s has non-pointer type", name)
  568. }
  569. i, err := strconv.Atoi(name[7:])
  570. if err != nil {
  571. fatalf("malformed __cgo__ name: %s", name)
  572. }
  573. types[i] = t.Type
  574. p.recordTypedefs(t.Type, f.NamePos[names[i]])
  575. }
  576. if e.Tag != dwarf.TagCompileUnit {
  577. r.SkipChildren()
  578. }
  579. }
  580. // Record types and typedef information.
  581. for i, n := range names {
  582. if strings.HasSuffix(n.Go, "GetTypeID") && types[i].String() == "func() CFTypeID" {
  583. conv.getTypeIDs[n.Go[:len(n.Go)-9]] = true
  584. }
  585. }
  586. for i, n := range names {
  587. if types[i] == nil {
  588. continue
  589. }
  590. pos := f.NamePos[n]
  591. f, fok := types[i].(*dwarf.FuncType)
  592. if n.Kind != "type" && fok {
  593. n.Kind = "func"
  594. n.FuncType = conv.FuncType(f, pos)
  595. } else {
  596. n.Type = conv.Type(types[i], pos)
  597. switch n.Kind {
  598. case "iconst":
  599. if i < len(ints) {
  600. if _, ok := types[i].(*dwarf.UintType); ok {
  601. n.Const = fmt.Sprintf("%#x", uint64(ints[i]))
  602. } else {
  603. n.Const = fmt.Sprintf("%#x", ints[i])
  604. }
  605. }
  606. case "fconst":
  607. if i >= len(floats) {
  608. break
  609. }
  610. switch base(types[i]).(type) {
  611. case *dwarf.IntType, *dwarf.UintType:
  612. // This has an integer type so it's
  613. // not really a floating point
  614. // constant. This can happen when the
  615. // C compiler complains about using
  616. // the value as an integer constant,
  617. // but not as a general constant.
  618. // Treat this as a variable of the
  619. // appropriate type, not a constant,
  620. // to get C-style type handling,
  621. // avoiding the problem that C permits
  622. // uint64(-1) but Go does not.
  623. // See issue 26066.
  624. n.Kind = "var"
  625. default:
  626. n.Const = fmt.Sprintf("%f", floats[i])
  627. }
  628. case "sconst":
  629. if i < len(strs) {
  630. n.Const = fmt.Sprintf("%q", strs[i])
  631. }
  632. }
  633. }
  634. conv.FinishType(pos)
  635. }
  636. }
  637. // recordTypedefs remembers in p.typedefs all the typedefs used in dtypes and its children.
  638. func (p *Package) recordTypedefs(dtype dwarf.Type, pos token.Pos) {
  639. p.recordTypedefs1(dtype, pos, map[dwarf.Type]bool{})
  640. }
  641. func (p *Package) recordTypedefs1(dtype dwarf.Type, pos token.Pos, visited map[dwarf.Type]bool) {
  642. if dtype == nil {
  643. return
  644. }
  645. if visited[dtype] {
  646. return
  647. }
  648. visited[dtype] = true
  649. switch dt := dtype.(type) {
  650. case *dwarf.TypedefType:
  651. if strings.HasPrefix(dt.Name, "__builtin") {
  652. // Don't look inside builtin types. There be dragons.
  653. return
  654. }
  655. if !p.typedefs[dt.Name] {
  656. p.typedefs[dt.Name] = true
  657. p.typedefList = append(p.typedefList, typedefInfo{dt.Name, pos})
  658. p.recordTypedefs1(dt.Type, pos, visited)
  659. }
  660. case *dwarf.PtrType:
  661. p.recordTypedefs1(dt.Type, pos, visited)
  662. case *dwarf.ArrayType:
  663. p.recordTypedefs1(dt.Type, pos, visited)
  664. case *dwarf.QualType:
  665. p.recordTypedefs1(dt.Type, pos, visited)
  666. case *dwarf.FuncType:
  667. p.recordTypedefs1(dt.ReturnType, pos, visited)
  668. for _, a := range dt.ParamType {
  669. p.recordTypedefs1(a, pos, visited)
  670. }
  671. case *dwarf.StructType:
  672. for _, f := range dt.Field {
  673. p.recordTypedefs1(f.Type, pos, visited)
  674. }
  675. }
  676. }
  677. // prepareNames finalizes the Kind field of not-type names and sets
  678. // the mangled name of all names.
  679. func (p *Package) prepareNames(f *File) {
  680. for _, n := range f.Name {
  681. if n.Kind == "not-type" {
  682. if n.Define == "" {
  683. n.Kind = "var"
  684. } else {
  685. n.Kind = "macro"
  686. n.FuncType = &FuncType{
  687. Result: n.Type,
  688. Go: &ast.FuncType{
  689. Results: &ast.FieldList{List: []*ast.Field{{Type: n.Type.Go}}},
  690. },
  691. }
  692. }
  693. }
  694. p.mangleName(n)
  695. if n.Kind == "type" && typedef[n.Mangle] == nil {
  696. typedef[n.Mangle] = n.Type
  697. }
  698. }
  699. }
  700. // mangleName does name mangling to translate names
  701. // from the original Go source files to the names
  702. // used in the final Go files generated by cgo.
  703. func (p *Package) mangleName(n *Name) {
  704. // When using gccgo variables have to be
  705. // exported so that they become global symbols
  706. // that the C code can refer to.
  707. prefix := "_C"
  708. if *gccgo && n.IsVar() {
  709. prefix = "C"
  710. }
  711. n.Mangle = prefix + n.Kind + "_" + n.Go
  712. }
  713. func (f *File) isMangledName(s string) bool {
  714. prefix := "_C"
  715. if strings.HasPrefix(s, prefix) {
  716. t := s[len(prefix):]
  717. for _, k := range nameKinds {
  718. if strings.HasPrefix(t, k+"_") {
  719. return true
  720. }
  721. }
  722. }
  723. return false
  724. }
  725. // rewriteCalls rewrites all calls that pass pointers to check that
  726. // they follow the rules for passing pointers between Go and C.
  727. // This reports whether the package needs to import unsafe as _cgo_unsafe.
  728. func (p *Package) rewriteCalls(f *File) bool {
  729. needsUnsafe := false
  730. // Walk backward so that in C.f1(C.f2()) we rewrite C.f2 first.
  731. for _, call := range f.Calls {
  732. if call.Done {
  733. continue
  734. }
  735. start := f.offset(call.Call.Pos())
  736. end := f.offset(call.Call.End())
  737. str, nu := p.rewriteCall(f, call)
  738. if str != "" {
  739. f.Edit.Replace(start, end, str)
  740. if nu {
  741. needsUnsafe = true
  742. }
  743. }
  744. }
  745. return needsUnsafe
  746. }
  747. // rewriteCall rewrites one call to add pointer checks.
  748. // If any pointer checks are required, we rewrite the call into a
  749. // function literal that calls _cgoCheckPointer for each pointer
  750. // argument and then calls the original function.
  751. // This returns the rewritten call and whether the package needs to
  752. // import unsafe as _cgo_unsafe.
  753. // If it returns the empty string, the call did not need to be rewritten.
  754. func (p *Package) rewriteCall(f *File, call *Call) (string, bool) {
  755. // This is a call to C.xxx; set goname to "xxx".
  756. // It may have already been mangled by rewriteName.
  757. var goname string
  758. switch fun := call.Call.Fun.(type) {
  759. case *ast.SelectorExpr:
  760. goname = fun.Sel.Name
  761. case *ast.Ident:
  762. goname = strings.TrimPrefix(fun.Name, "_C2func_")
  763. goname = strings.TrimPrefix(goname, "_Cfunc_")
  764. }
  765. if goname == "" || goname == "malloc" {
  766. return "", false
  767. }
  768. name := f.Name[goname]
  769. if name == nil || name.Kind != "func" {
  770. // Probably a type conversion.
  771. return "", false
  772. }
  773. params := name.FuncType.Params
  774. args := call.Call.Args
  775. // Avoid a crash if the number of arguments doesn't match
  776. // the number of parameters.
  777. // This will be caught when the generated file is compiled.
  778. if len(args) != len(params) {
  779. return "", false
  780. }
  781. any := false
  782. for i, param := range params {
  783. if p.needsPointerCheck(f, param.Go, args[i]) {
  784. any = true
  785. break
  786. }
  787. }
  788. if !any {
  789. return "", false
  790. }
  791. // We need to rewrite this call.
  792. //
  793. // Rewrite C.f(p) to
  794. // func() {
  795. // _cgo0 := p
  796. // _cgoCheckPointer(_cgo0, nil)
  797. // C.f(_cgo0)
  798. // }()
  799. // Using a function literal like this lets us evaluate the
  800. // function arguments only once while doing pointer checks.
  801. // This is particularly useful when passing additional arguments
  802. // to _cgoCheckPointer, as done in checkIndex and checkAddr.
  803. //
  804. // When the function argument is a conversion to unsafe.Pointer,
  805. // we unwrap the conversion before checking the pointer,
  806. // and then wrap again when calling C.f. This lets us check
  807. // the real type of the pointer in some cases. See issue #25941.
  808. //
  809. // When the call to C.f is deferred, we use an additional function
  810. // literal to evaluate the arguments at the right time.
  811. // defer func() func() {
  812. // _cgo0 := p
  813. // return func() {
  814. // _cgoCheckPointer(_cgo0, nil)
  815. // C.f(_cgo0)
  816. // }
  817. // }()()
  818. // This works because the defer statement evaluates the first
  819. // function literal in order to get the function to call.
  820. var sb bytes.Buffer
  821. sb.WriteString("func() ")
  822. if call.Deferred {
  823. sb.WriteString("func() ")
  824. }
  825. needsUnsafe := false
  826. result := false
  827. twoResults := false
  828. if !call.Deferred {
  829. // Check whether this call expects two results.
  830. for _, ref := range f.Ref {
  831. if ref.Expr != &call.Call.Fun {
  832. continue
  833. }
  834. if ref.Context == ctxCall2 {
  835. sb.WriteString("(")
  836. result = true
  837. twoResults = true
  838. }
  839. break
  840. }
  841. // Add the result type, if any.
  842. if name.FuncType.Result != nil {
  843. rtype := p.rewriteUnsafe(name.FuncType.Result.Go)
  844. if rtype != name.FuncType.Result.Go {
  845. needsUnsafe = true
  846. }
  847. sb.WriteString(gofmtLine(rtype))
  848. result = true
  849. }
  850. // Add the second result type, if any.
  851. if twoResults {
  852. if name.FuncType.Result == nil {
  853. // An explicit void result looks odd but it
  854. // seems to be how cgo has worked historically.
  855. sb.WriteString("_Ctype_void")
  856. }
  857. sb.WriteString(", error)")
  858. }
  859. }
  860. sb.WriteString("{ ")
  861. // Define _cgoN for each argument value.
  862. // Write _cgoCheckPointer calls to sbCheck.
  863. var sbCheck bytes.Buffer
  864. for i, param := range params {
  865. origArg := args[i]
  866. arg, nu := p.mangle(f, &args[i], true)
  867. if nu {
  868. needsUnsafe = true
  869. }
  870. // Use "var x T = ..." syntax to explicitly convert untyped
  871. // constants to the parameter type, to avoid a type mismatch.
  872. ptype := p.rewriteUnsafe(param.Go)
  873. if !p.needsPointerCheck(f, param.Go, args[i]) || param.BadPointer {
  874. if ptype != param.Go {
  875. needsUnsafe = true
  876. }
  877. fmt.Fprintf(&sb, "var _cgo%d %s = %s; ", i,
  878. gofmtLine(ptype), gofmtPos(arg, origArg.Pos()))
  879. continue
  880. }
  881. // Check for &a[i].
  882. if p.checkIndex(&sb, &sbCheck, arg, i) {
  883. continue
  884. }
  885. // Check for &x.
  886. if p.checkAddr(&sb, &sbCheck, arg, i) {
  887. continue
  888. }
  889. fmt.Fprintf(&sb, "_cgo%d := %s; ", i, gofmtPos(arg, origArg.Pos()))
  890. fmt.Fprintf(&sbCheck, "_cgoCheckPointer(_cgo%d, nil); ", i)
  891. }
  892. if call.Deferred {
  893. sb.WriteString("return func() { ")
  894. }
  895. // Write out the calls to _cgoCheckPointer.
  896. sb.WriteString(sbCheck.String())
  897. if result {
  898. sb.WriteString("return ")
  899. }
  900. m, nu := p.mangle(f, &call.Call.Fun, false)
  901. if nu {
  902. needsUnsafe = true
  903. }
  904. sb.WriteString(gofmtLine(m))
  905. sb.WriteString("(")
  906. for i := range params {
  907. if i > 0 {
  908. sb.WriteString(", ")
  909. }
  910. fmt.Fprintf(&sb, "_cgo%d", i)
  911. }
  912. sb.WriteString("); ")
  913. if call.Deferred {
  914. sb.WriteString("}")
  915. }
  916. sb.WriteString("}")
  917. if call.Deferred {
  918. sb.WriteString("()")
  919. }
  920. sb.WriteString("()")
  921. return sb.String(), needsUnsafe
  922. }
  923. // needsPointerCheck reports whether the type t needs a pointer check.
  924. // This is true if t is a pointer and if the value to which it points
  925. // might contain a pointer.
  926. func (p *Package) needsPointerCheck(f *File, t ast.Expr, arg ast.Expr) bool {
  927. // An untyped nil does not need a pointer check, and when
  928. // _cgoCheckPointer returns the untyped nil the type assertion we
  929. // are going to insert will fail. Easier to just skip nil arguments.
  930. // TODO: Note that this fails if nil is shadowed.
  931. if id, ok := arg.(*ast.Ident); ok && id.Name == "nil" {
  932. return false
  933. }
  934. return p.hasPointer(f, t, true)
  935. }
  936. // hasPointer is used by needsPointerCheck. If top is true it returns
  937. // whether t is or contains a pointer that might point to a pointer.
  938. // If top is false it reports whether t is or contains a pointer.
  939. // f may be nil.
  940. func (p *Package) hasPointer(f *File, t ast.Expr, top bool) bool {
  941. switch t := t.(type) {
  942. case *ast.ArrayType:
  943. if t.Len == nil {
  944. if !top {
  945. return true
  946. }
  947. return p.hasPointer(f, t.Elt, false)
  948. }
  949. return p.hasPointer(f, t.Elt, top)
  950. case *ast.StructType:
  951. for _, field := range t.Fields.List {
  952. if p.hasPointer(f, field.Type, top) {
  953. return true
  954. }
  955. }
  956. return false
  957. case *ast.StarExpr: // Pointer type.
  958. if !top {
  959. return true
  960. }
  961. // Check whether this is a pointer to a C union (or class)
  962. // type that contains a pointer.
  963. if unionWithPointer[t.X] {
  964. return true
  965. }
  966. return p.hasPointer(f, t.X, false)
  967. case *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.ChanType:
  968. return true
  969. case *ast.Ident:
  970. // TODO: Handle types defined within function.
  971. for _, d := range p.Decl {
  972. gd, ok := d.(*ast.GenDecl)
  973. if !ok || gd.Tok != token.TYPE {
  974. continue
  975. }
  976. for _, spec := range gd.Specs {
  977. ts, ok := spec.(*ast.TypeSpec)
  978. if !ok {
  979. continue
  980. }
  981. if ts.Name.Name == t.Name {
  982. return p.hasPointer(f, ts.Type, top)
  983. }
  984. }
  985. }
  986. if def := typedef[t.Name]; def != nil {
  987. return p.hasPointer(f, def.Go, top)
  988. }
  989. if t.Name == "string" {
  990. return !top
  991. }
  992. if t.Name == "error" {
  993. return true
  994. }
  995. if goTypes[t.Name] != nil {
  996. return false
  997. }
  998. // We can't figure out the type. Conservative
  999. // approach is to assume it has a pointer.
  1000. return true
  1001. case *ast.SelectorExpr:
  1002. if l, ok := t.X.(*ast.Ident); !ok || l.Name != "C" {
  1003. // Type defined in a different package.
  1004. // Conservative approach is to assume it has a
  1005. // pointer.
  1006. return true
  1007. }
  1008. if f == nil {
  1009. // Conservative approach: assume pointer.
  1010. return true
  1011. }
  1012. name := f.Name[t.Sel.Name]
  1013. if name != nil && name.Kind == "type" && name.Type != nil && name.Type.Go != nil {
  1014. return p.hasPointer(f, name.Type.Go, top)
  1015. }
  1016. // We can't figure out the type. Conservative
  1017. // approach is to assume it has a pointer.
  1018. return true
  1019. default:
  1020. error_(t.Pos(), "could not understand type %s", gofmt(t))
  1021. return true
  1022. }
  1023. }
  1024. // mangle replaces references to C names in arg with the mangled names,
  1025. // rewriting calls when it finds them.
  1026. // It removes the corresponding references in f.Ref and f.Calls, so that we
  1027. // don't try to do the replacement again in rewriteRef or rewriteCall.
  1028. // If addPosition is true, add position info to the idents of C names in arg.
  1029. func (p *Package) mangle(f *File, arg *ast.Expr, addPosition bool) (ast.Expr, bool) {
  1030. needsUnsafe := false
  1031. f.walk(arg, ctxExpr, func(f *File, arg interface{}, context astContext) {
  1032. px, ok := arg.(*ast.Expr)
  1033. if !ok {
  1034. return
  1035. }
  1036. sel, ok := (*px).(*ast.SelectorExpr)
  1037. if ok {
  1038. if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" {
  1039. return
  1040. }
  1041. for _, r := range f.Ref {
  1042. if r.Expr == px {
  1043. *px = p.rewriteName(f, r, addPosition)
  1044. r.Done = true
  1045. break
  1046. }
  1047. }
  1048. return
  1049. }
  1050. call, ok := (*px).(*ast.CallExpr)
  1051. if !ok {
  1052. return
  1053. }
  1054. for _, c := range f.Calls {
  1055. if !c.Done && c.Call.Lparen == call.Lparen {
  1056. cstr, nu := p.rewriteCall(f, c)
  1057. if cstr != "" {
  1058. // Smuggle the rewritten call through an ident.
  1059. *px = ast.NewIdent(cstr)
  1060. if nu {
  1061. needsUnsafe = true
  1062. }
  1063. c.Done = true
  1064. }
  1065. }
  1066. }
  1067. })
  1068. return *arg, needsUnsafe
  1069. }
  1070. // checkIndex checks whether arg has the form &a[i], possibly inside
  1071. // type conversions. If so, then in the general case it writes
  1072. // _cgoIndexNN := a
  1073. // _cgoNN := &cgoIndexNN[i] // with type conversions, if any
  1074. // to sb, and writes
  1075. // _cgoCheckPointer(_cgoNN, _cgoIndexNN)
  1076. // to sbCheck, and returns true. If a is a simple variable or field reference,
  1077. // it writes
  1078. // _cgoIndexNN := &a
  1079. // and dereferences the uses of _cgoIndexNN. Taking the address avoids
  1080. // making a copy of an array.
  1081. //
  1082. // This tells _cgoCheckPointer to check the complete contents of the
  1083. // slice or array being indexed, but no other part of the memory allocation.
  1084. func (p *Package) checkIndex(sb, sbCheck *bytes.Buffer, arg ast.Expr, i int) bool {
  1085. // Strip type conversions.
  1086. x := arg
  1087. for {
  1088. c, ok := x.(*ast.CallExpr)
  1089. if !ok || len(c.Args) != 1 || !p.isType(c.Fun) {
  1090. break
  1091. }
  1092. x = c.Args[0]
  1093. }
  1094. u, ok := x.(*ast.UnaryExpr)
  1095. if !ok || u.Op != token.AND {
  1096. return false
  1097. }
  1098. index, ok := u.X.(*ast.IndexExpr)
  1099. if !ok {
  1100. return false
  1101. }
  1102. addr := ""
  1103. deref := ""
  1104. if p.isVariable(index.X) {
  1105. addr = "&"
  1106. deref = "*"
  1107. }
  1108. fmt.Fprintf(sb, "_cgoIndex%d := %s%s; ", i, addr, gofmtPos(index.X, index.X.Pos()))
  1109. origX := index.X
  1110. index.X = ast.NewIdent(fmt.Sprintf("_cgoIndex%d", i))
  1111. if deref == "*" {
  1112. index.X = &ast.StarExpr{X: index.X}
  1113. }
  1114. fmt.Fprintf(sb, "_cgo%d := %s; ", i, gofmtPos(arg, arg.Pos()))
  1115. index.X = origX
  1116. fmt.Fprintf(sbCheck, "_cgoCheckPointer(_cgo%d, %s_cgoIndex%d); ", i, deref, i)
  1117. return true
  1118. }
  1119. // checkAddr checks whether arg has the form &x, possibly inside type
  1120. // conversions. If so, it writes
  1121. // _cgoBaseNN := &x
  1122. // _cgoNN := _cgoBaseNN // with type conversions, if any
  1123. // to sb, and writes
  1124. // _cgoCheckPointer(_cgoBaseNN, true)
  1125. // to sbCheck, and returns true. This tells _cgoCheckPointer to check
  1126. // just the contents of the pointer being passed, not any other part
  1127. // of the memory allocation. This is run after checkIndex, which looks
  1128. // for the special case of &a[i], which requires different checks.
  1129. func (p *Package) checkAddr(sb, sbCheck *bytes.Buffer, arg ast.Expr, i int) bool {
  1130. // Strip type conversions.
  1131. px := &arg
  1132. for {
  1133. c, ok := (*px).(*ast.CallExpr)
  1134. if !ok || len(c.Args) != 1 || !p.isType(c.Fun) {
  1135. break
  1136. }
  1137. px = &c.Args[0]
  1138. }
  1139. if u, ok := (*px).(*ast.UnaryExpr); !ok || u.Op != token.AND {
  1140. return false
  1141. }
  1142. fmt.Fprintf(sb, "_cgoBase%d := %s; ", i, gofmtPos(*px, (*px).Pos()))
  1143. origX := *px
  1144. *px = ast.NewIdent(fmt.Sprintf("_cgoBase%d", i))
  1145. fmt.Fprintf(sb, "_cgo%d := %s; ", i, gofmtPos(arg, arg.Pos()))
  1146. *px = origX
  1147. // Use "0 == 0" to do the right thing in the unlikely event
  1148. // that "true" is shadowed.
  1149. fmt.Fprintf(sbCheck, "_cgoCheckPointer(_cgoBase%d, 0 == 0); ", i)
  1150. return true
  1151. }
  1152. // isType reports whether the expression is definitely a type.
  1153. // This is conservative--it returns false for an unknown identifier.
  1154. func (p *Package) isType(t ast.Expr) bool {
  1155. switch t := t.(type) {
  1156. case *ast.SelectorExpr:
  1157. id, ok := t.X.(*ast.Ident)
  1158. if !ok {
  1159. return false
  1160. }
  1161. if id.Name == "unsafe" && t.Sel.Name == "Pointer" {
  1162. return true
  1163. }
  1164. if id.Name == "C" && typedef["_Ctype_"+t.Sel.Name] != nil {
  1165. return true
  1166. }
  1167. return false
  1168. case *ast.Ident:
  1169. // TODO: This ignores shadowing.
  1170. switch t.Name {
  1171. case "unsafe.Pointer", "bool", "byte",
  1172. "complex64", "complex128",
  1173. "error",
  1174. "float32", "float64",
  1175. "int", "int8", "int16", "int32", "int64",
  1176. "rune", "string",
  1177. "uint", "uint8", "uint16", "uint32", "uint64", "uintptr":
  1178. return true
  1179. }
  1180. if strings.HasPrefix(t.Name, "_Ctype_") {
  1181. return true
  1182. }
  1183. case *ast.ParenExpr:
  1184. return p.isType(t.X)
  1185. case *ast.StarExpr:
  1186. return p.isType(t.X)
  1187. case *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType,
  1188. *ast.MapType, *ast.ChanType:
  1189. return true
  1190. }
  1191. return false
  1192. }
  1193. // isVariable reports whether x is a variable, possibly with field references.
  1194. func (p *Package) isVariable(x ast.Expr) bool {
  1195. switch x := x.(type) {
  1196. case *ast.Ident:
  1197. return true
  1198. case *ast.SelectorExpr:
  1199. return p.isVariable(x.X)
  1200. case *ast.IndexExpr:
  1201. return true
  1202. }
  1203. return false
  1204. }
  1205. // rewriteUnsafe returns a version of t with references to unsafe.Pointer
  1206. // rewritten to use _cgo_unsafe.Pointer instead.
  1207. func (p *Package) rewriteUnsafe(t ast.Expr) ast.Expr {
  1208. switch t := t.(type) {
  1209. case *ast.Ident:
  1210. // We don't see a SelectorExpr for unsafe.Pointer;
  1211. // this is created by code in this file.
  1212. if t.Name == "unsafe.Pointer" {
  1213. return ast.NewIdent("_cgo_unsafe.Pointer")
  1214. }
  1215. case *ast.ArrayType:
  1216. t1 := p.rewriteUnsafe(t.Elt)
  1217. if t1 != t.Elt {
  1218. r := *t
  1219. r.Elt = t1
  1220. return &r
  1221. }
  1222. case *ast.StructType:
  1223. changed := false
  1224. fields := *t.Fields
  1225. fields.List = nil
  1226. for _, f := range t.Fields.List {
  1227. ft := p.rewriteUnsafe(f.Type)
  1228. if ft == f.Type {
  1229. fields.List = append(fields.List, f)
  1230. } else {
  1231. fn := *f
  1232. fn.Type = ft
  1233. fields.List = append(fields.List, &fn)
  1234. changed = true
  1235. }
  1236. }
  1237. if changed {
  1238. r := *t
  1239. r.Fields = &fields
  1240. return &r
  1241. }
  1242. case *ast.StarExpr: // Pointer type.
  1243. x1 := p.rewriteUnsafe(t.X)
  1244. if x1 != t.X {
  1245. r := *t
  1246. r.X = x1
  1247. return &r
  1248. }
  1249. }
  1250. return t
  1251. }
  1252. // rewriteRef rewrites all the C.xxx references in f.AST to refer to the
  1253. // Go equivalents, now that we have figured out the meaning of all
  1254. // the xxx. In *godefs mode, rewriteRef replaces the names
  1255. // with full definitions instead of mangled names.
  1256. func (p *Package) rewriteRef(f *File) {
  1257. // Keep a list of all the functions, to remove the ones
  1258. // only used as expressions and avoid generating bridge
  1259. // code for them.
  1260. functions := make(map[string]bool)
  1261. for _, n := range f.Name {
  1262. if n.Kind == "func" {
  1263. functions[n.Go] = false
  1264. }
  1265. }
  1266. // Now that we have all the name types filled in,
  1267. // scan through the Refs to identify the ones that
  1268. // are trying to do a ,err call. Also check that
  1269. // functions are only used in calls.
  1270. for _, r := range f.Ref {
  1271. if r.Name.IsConst() && r.Name.Const == "" {
  1272. error_(r.Pos(), "unable to find value of constant C.%s", fixGo(r.Name.Go))
  1273. }
  1274. if r.Name.Kind == "func" {
  1275. switch r.Context {
  1276. case ctxCall, ctxCall2:
  1277. functions[r.Name.Go] = true
  1278. }
  1279. }
  1280. expr := p.rewriteName(f, r, false)
  1281. if *godefs {
  1282. // Substitute definition for mangled type name.
  1283. if r.Name.Type != nil && r.Name.Kind == "type" {
  1284. expr = r.Name.Type.Go
  1285. }
  1286. if id, ok := expr.(*ast.Ident); ok {
  1287. if t := typedef[id.Name]; t != nil {
  1288. expr = t.Go
  1289. }
  1290. if id.Name == r.Name.Mangle && r.Name.Const != "" {
  1291. expr = ast.NewIdent(r.Name.Const)
  1292. }
  1293. }
  1294. }
  1295. // Copy position information from old expr into new expr,
  1296. // in case expression being replaced is first on line.
  1297. // See golang.org/issue/6563.
  1298. pos := (*r.Expr).Pos()
  1299. if x, ok := expr.(*ast.Ident); ok {
  1300. expr = &ast.Ident{NamePos: pos, Name: x.Name}
  1301. }
  1302. // Change AST, because some later processing depends on it,
  1303. // and also because -godefs mode still prints the AST.
  1304. old := *r.Expr
  1305. *r.Expr = expr
  1306. // Record source-level edit for cgo output.
  1307. if !r.Done {
  1308. // Prepend a space in case the earlier code ends
  1309. // with '/', which would give us a "//" comment.
  1310. repl := " " + gofmtPos(expr, old.Pos())
  1311. end := fset.Position(old.End())
  1312. // Subtract 1 from the column if we are going to
  1313. // append a close parenthesis. That will set the
  1314. // correct column for the following characters.
  1315. sub := 0
  1316. if r.Name.Kind != "type" {
  1317. sub = 1
  1318. }
  1319. if end.Column > sub {
  1320. repl = fmt.Sprintf("%s /*line :%d:%d*/", repl, end.Line, end.Column-sub)
  1321. }
  1322. if r.Name.Kind != "type" {
  1323. repl = "(" + repl + ")"
  1324. }
  1325. f.Edit.Replace(f.offset(old.Pos()), f.offset(old.End()), repl)
  1326. }
  1327. }
  1328. // Remove functions only used as expressions, so their respective
  1329. // bridge functions are not generated.
  1330. for name, used := range functions {
  1331. if !used {
  1332. delete(f.Name, name)
  1333. }
  1334. }
  1335. }
  1336. // rewriteName returns the expression used to rewrite a reference.
  1337. // If addPosition is true, add position info in the ident name.
  1338. func (p *Package) rewriteName(f *File, r *Ref, addPosition bool) ast.Expr {
  1339. getNewIdent := ast.NewIdent
  1340. if addPosition {
  1341. getNewIdent = func(newName string) *ast.Ident {
  1342. mangledIdent := ast.NewIdent(newName)
  1343. if len(newName) == len(r.Name.Go) {
  1344. return mangledIdent
  1345. }
  1346. p := fset.Position((*r.Expr).End())
  1347. if p.Column == 0 {
  1348. return mangledIdent
  1349. }
  1350. return ast.NewIdent(fmt.Sprintf("%s /*line :%d:%d*/", newName, p.Line, p.Column))
  1351. }
  1352. }
  1353. var expr ast.Expr = getNewIdent(r.Name.Mangle) // default
  1354. switch r.Context {
  1355. case ctxCall, ctxCall2:
  1356. if r.Name.Kind != "func" {
  1357. if r.Name.Kind == "type" {
  1358. r.Context = ctxType
  1359. if r.Name.Type == nil {
  1360. error_(r.Pos(), "invalid conversion to C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
  1361. }
  1362. break
  1363. }
  1364. error_(r.Pos(), "call of non-function C.%s", fixGo(r.Name.Go))
  1365. break
  1366. }
  1367. if r.Context == ctxCall2 {
  1368. if r.Name.Go == "_CMalloc" {
  1369. error_(r.Pos(), "no two-result form for C.malloc")
  1370. break
  1371. }
  1372. // Invent new Name for the two-result function.
  1373. n := f.Name["2"+r.Name.Go]
  1374. if n == nil {
  1375. n = new(Name)
  1376. *n = *r.Name
  1377. n.AddError = true
  1378. n.Mangle = "_C2func_" + n.Go
  1379. f.Name["2"+r.Name.Go] = n
  1380. }
  1381. expr = getNewIdent(n.Mangle)
  1382. r.Name = n
  1383. break
  1384. }
  1385. case ctxExpr:
  1386. switch r.Name.Kind {
  1387. case "func":
  1388. if builtinDefs[r.Name.C] != "" {
  1389. error_(r.Pos(), "use of builtin '%s' not in function call", fixGo(r.Name.C))
  1390. }
  1391. // Function is being used in an expression, to e.g. pass around a C function pointer.
  1392. // Create a new Name for this Ref which causes the variable to be declared in Go land.
  1393. fpName := "fp_" + r.Name.Go
  1394. name := f.Name[fpName]
  1395. if name == nil {
  1396. name = &Name{
  1397. Go: fpName,
  1398. C: r.Name.C,
  1399. Kind: "fpvar",
  1400. Type: &Type{Size: p.PtrSize, Align: p.PtrSize, C: c("void*"), Go: ast.NewIdent("unsafe.Pointer")},
  1401. }
  1402. p.mangleName(name)
  1403. f.Name[fpName] = name
  1404. }
  1405. r.Name = name
  1406. // Rewrite into call to _Cgo_ptr to prevent assignments. The _Cgo_ptr
  1407. // function is defined in out.go and simply returns its argument. See
  1408. // issue 7757.
  1409. expr = &ast.CallExpr{
  1410. Fun: &ast.Ident{NamePos: (*r.Expr).Pos(), Name: "_Cgo_ptr"},
  1411. Args: []ast.Expr{getNewIdent(name.Mangle)},
  1412. }
  1413. case "type":
  1414. // Okay - might be new(T), T(x), Generic[T], etc.
  1415. if r.Name.Type == nil {
  1416. error_(r.Pos(), "expression C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
  1417. }
  1418. case "var":
  1419. expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr}
  1420. case "macro":
  1421. expr = &ast.CallExpr{Fun: expr}
  1422. }
  1423. case ctxSelector:
  1424. if r.Name.Kind == "var" {
  1425. expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr}
  1426. } else {
  1427. error_(r.Pos(), "only C variables allowed in selector expression %s", fixGo(r.Name.Go))
  1428. }
  1429. case ctxType:
  1430. if r.Name.Kind != "type" {
  1431. error_(r.Pos(), "expression C.%s used as type", fixGo(r.Name.Go))
  1432. } else if r.Name.Type == nil {
  1433. // Use of C.enum_x, C.struct_x or C.union_x without C definition.
  1434. // GCC won't raise an error when using pointers to such unknown types.
  1435. error_(r.Pos(), "type C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
  1436. }
  1437. default:
  1438. if r.Name.Kind == "func" {
  1439. error_(r.Pos(), "must call C.%s", fixGo(r.Name.Go))
  1440. }
  1441. }
  1442. return expr
  1443. }
  1444. // gofmtPos returns the gofmt-formatted string for an AST node,
  1445. // with a comment setting the position before the node.
  1446. func gofmtPos(n ast.Expr, pos token.Pos) string {
  1447. s := gofmtLine(n)
  1448. p := fset.Position(pos)
  1449. if p.Column == 0 {
  1450. return s
  1451. }
  1452. return fmt.Sprintf("/*line :%d:%d*/%s", p.Line, p.Column, s)
  1453. }
  1454. // checkGCCBaseCmd returns the start of the compiler command line.
  1455. // It uses $CC if set, or else $GCC, or else the compiler recorded
  1456. // during the initial build as defaultCC.
  1457. // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
  1458. //
  1459. // The compiler command line is split into arguments on whitespace. Quotes
  1460. // are understood, so arguments may contain whitespace.
  1461. //
  1462. // checkGCCBaseCmd confirms that the compiler exists in PATH, returning
  1463. // an error if it does not.
  1464. func checkGCCBaseCmd() ([]string, error) {
  1465. // Use $CC if set, since that's what the build uses.
  1466. value := os.Getenv("CC")
  1467. if value == "" {
  1468. // Try $GCC if set, since that's what we used to use.
  1469. value = os.Getenv("GCC")
  1470. }
  1471. if value == "" {
  1472. value = defaultCC(goos, goarch)
  1473. }
  1474. args, err := quoted.Split(value)
  1475. if err != nil {
  1476. return nil, err
  1477. }
  1478. if len(args) == 0 {
  1479. return nil, errors.New("CC not set and no default found")
  1480. }
  1481. if _, err := exec.LookPath(args[0]); err != nil {
  1482. return nil, fmt.Errorf("C compiler %q not found: %v", args[0], err)
  1483. }
  1484. return args[:len(args):len(args)], nil
  1485. }
  1486. // gccMachine returns the gcc -m flag to use, either "-m32", "-m64" or "-marm".
  1487. func (p *Package) gccMachine() []string {
  1488. switch goarch {
  1489. case "amd64":
  1490. if goos == "darwin" {
  1491. return []string{"-arch", "x86_64", "-m64"}
  1492. }
  1493. return []string{"-m64"}
  1494. case "arm64":
  1495. if goos == "darwin" {
  1496. return []string{"-arch", "arm64"}
  1497. }
  1498. case "386":
  1499. return []string{"-m32"}
  1500. case "arm":
  1501. return []string{"-marm"} // not thumb
  1502. case "s390":
  1503. return []string{"-m31"}
  1504. case "s390x":
  1505. return []string{"-m64"}
  1506. case "mips64", "mips64le":
  1507. if gomips64 == "hardfloat" {
  1508. return []string{"-mabi=64", "-mhard-float"}
  1509. } else if gomips64 == "softfloat" {
  1510. return []string{"-mabi=64", "-msoft-float"}
  1511. }
  1512. case "mips", "mipsle":
  1513. if gomips == "hardfloat" {
  1514. return []string{"-mabi=32", "-mfp32", "-mhard-float", "-mno-odd-spreg"}
  1515. } else if gomips == "softfloat" {
  1516. return []string{"-mabi=32", "-msoft-float"}
  1517. }
  1518. case "ppc64":
  1519. if goos == "aix" {
  1520. return []string{"-maix64"}
  1521. }
  1522. case "ppc":
  1523. if goos == "aix" {
  1524. return []string{"-maix32"}
  1525. }
  1526. }
  1527. return nil
  1528. }
  1529. func gccTmp() string {
  1530. return *objDir + "_cgo_.o"
  1531. }
  1532. // gccCmd returns the gcc command line to use for compiling
  1533. // the input.
  1534. func (p *Package) gccCmd() []string {
  1535. c := append(gccBaseCmd,
  1536. "-w", // no warnings
  1537. "-Wno-error", // warnings are not errors
  1538. "-o"+gccTmp(), // write object to tmp
  1539. "-gdwarf-2", // generate DWARF v2 debugging symbols
  1540. "-c", // do not link
  1541. "-xc", // input language is C
  1542. )
  1543. if p.GccIsClang {
  1544. c = append(c,
  1545. "-ferror-limit=0",
  1546. // Apple clang version 1.7 (tags/Apple/clang-77) (based on LLVM 2.9svn)
  1547. // doesn't have -Wno-unneeded-internal-declaration, so we need yet another
  1548. // flag to disable the warning. Yes, really good diagnostics, clang.
  1549. "-Wno-unknown-warning-option",
  1550. "-Wno-unneeded-internal-declaration",
  1551. "-Wno-unused-function",
  1552. "-Qunused-arguments",
  1553. // Clang embeds prototypes for some builtin functions,
  1554. // like malloc and calloc, but all size_t parameters are
  1555. // incorrectly typed unsigned long. We work around that
  1556. // by disabling the builtin functions (this is safe as
  1557. // it won't affect the actual compilation of the C code).
  1558. // See: https://golang.org/issue/6506.
  1559. "-fno-builtin",
  1560. )
  1561. }
  1562. c = append(c, p.GccOptions...)
  1563. c = append(c, p.gccMachine()...)
  1564. if goos == "aix" {
  1565. c = append(c, "-mcmodel=large")
  1566. }
  1567. // disable LTO so we get an object whose symbols we can read
  1568. c = append(c, "-fno-lto")
  1569. c = append(c, "-") //read input from standard input
  1570. return c
  1571. }
  1572. // gccDebug runs gcc -gdwarf-2 over the C program stdin and
  1573. // returns the corresponding DWARF data and, if present, debug data block.
  1574. func (p *Package) gccDebug(stdin []byte, nnames int) (d *dwarf.Data, ints []int64, floats []float64, strs []string) {
  1575. runGcc(stdin, p.gccCmd())
  1576. isDebugInts := func(s string) bool {
  1577. // Some systems use leading _ to denote non-assembly symbols.
  1578. return s == "__cgodebug_ints" || s == "___cgodebug_ints"
  1579. }
  1580. isDebugFloats := func(s string) bool {
  1581. // Some systems use leading _ to denote non-assembly symbols.
  1582. return s == "__cgodebug_floats" || s == "___cgodebug_floats"
  1583. }
  1584. indexOfDebugStr := func(s string) int {
  1585. // Some systems use leading _ to denote non-assembly symbols.
  1586. if strings.HasPrefix(s, "___") {
  1587. s = s[1:]
  1588. }
  1589. if strings.HasPrefix(s, "__cgodebug_str__") {
  1590. if n, err := strconv.Atoi(s[len("__cgodebug_str__"):]); err == nil {
  1591. return n
  1592. }
  1593. }
  1594. return -1
  1595. }
  1596. indexOfDebugStrlen := func(s string) int {
  1597. // Some systems use leading _ to denote non-assembly symbols.
  1598. if strings.HasPrefix(s, "___") {
  1599. s = s[1:]
  1600. }
  1601. if strings.HasPrefix(s, "__cgodebug_strlen__") {
  1602. if n, err := strconv.Atoi(s[len("__cgodebug_strlen__"):]); err == nil {
  1603. return n
  1604. }
  1605. }
  1606. return -1
  1607. }
  1608. strs = make([]string, nnames)
  1609. strdata := make(map[int]string, nnames)
  1610. strlens := make(map[int]int, nnames)
  1611. buildStrings := func() {
  1612. for n, strlen := range strlens {
  1613. data := strdata[n]
  1614. if len(data) <= strlen {
  1615. fatalf("invalid string literal")
  1616. }
  1617. strs[n] = data[:strlen]
  1618. }
  1619. }
  1620. if f, err := macho.Open(gccTmp()); err == nil {
  1621. defer f.Close()
  1622. d, err := f.DWARF()
  1623. if err != nil {
  1624. fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1625. }
  1626. bo := f.ByteOrder
  1627. if f.Symtab != nil {
  1628. for i := range f.Symtab.Syms {
  1629. s := &f.Symtab.Syms[i]
  1630. switch {
  1631. case isDebugInts(s.Name):
  1632. // Found it. Now find data section.
  1633. if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1634. sect := f.Sections[i]
  1635. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1636. if sdat, err := sect.Data(); err == nil {
  1637. data := sdat[s.Value-sect.Addr:]
  1638. ints = make([]int64, len(data)/8)
  1639. for i := range ints {
  1640. ints[i] = int64(bo.Uint64(data[i*8:]))
  1641. }
  1642. }
  1643. }
  1644. }
  1645. case isDebugFloats(s.Name):
  1646. // Found it. Now find data section.
  1647. if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1648. sect := f.Sections[i]
  1649. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1650. if sdat, err := sect.Data(); err == nil {
  1651. data := sdat[s.Value-sect.Addr:]
  1652. floats = make([]float64, len(data)/8)
  1653. for i := range floats {
  1654. floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1655. }
  1656. }
  1657. }
  1658. }
  1659. default:
  1660. if n := indexOfDebugStr(s.Name); n != -1 {
  1661. // Found it. Now find data section.
  1662. if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1663. sect := f.Sections[i]
  1664. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1665. if sdat, err := sect.Data(); err == nil {
  1666. data := sdat[s.Value-sect.Addr:]
  1667. strdata[n] = string(data)
  1668. }
  1669. }
  1670. }
  1671. break
  1672. }
  1673. if n := indexOfDebugStrlen(s.Name); n != -1 {
  1674. // Found it. Now find data section.
  1675. if i := int(s.Sect) - 1; 0 <= i && i < len(f.Sections) {
  1676. sect := f.Sections[i]
  1677. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1678. if sdat, err := sect.Data(); err == nil {
  1679. data := sdat[s.Value-sect.Addr:]
  1680. strlen := bo.Uint64(data[:8])
  1681. if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1682. fatalf("string literal too big")
  1683. }
  1684. strlens[n] = int(strlen)
  1685. }
  1686. }
  1687. }
  1688. break
  1689. }
  1690. }
  1691. }
  1692. buildStrings()
  1693. }
  1694. return d, ints, floats, strs
  1695. }
  1696. if f, err := elf.Open(gccTmp()); err == nil {
  1697. defer f.Close()
  1698. d, err := f.DWARF()
  1699. if err != nil {
  1700. fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1701. }
  1702. bo := f.ByteOrder
  1703. symtab, err := f.Symbols()
  1704. if err == nil {
  1705. for i := range symtab {
  1706. s := &symtab[i]
  1707. switch {
  1708. case isDebugInts(s.Name):
  1709. // Found it. Now find data section.
  1710. if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1711. sect := f.Sections[i]
  1712. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1713. if sdat, err := sect.Data(); err == nil {
  1714. data := sdat[s.Value-sect.Addr:]
  1715. ints = make([]int64, len(data)/8)
  1716. for i := range ints {
  1717. ints[i] = int64(bo.Uint64(data[i*8:]))
  1718. }
  1719. }
  1720. }
  1721. }
  1722. case isDebugFloats(s.Name):
  1723. // Found it. Now find data section.
  1724. if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1725. sect := f.Sections[i]
  1726. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1727. if sdat, err := sect.Data(); err == nil {
  1728. data := sdat[s.Value-sect.Addr:]
  1729. floats = make([]float64, len(data)/8)
  1730. for i := range floats {
  1731. floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1732. }
  1733. }
  1734. }
  1735. }
  1736. default:
  1737. if n := indexOfDebugStr(s.Name); n != -1 {
  1738. // Found it. Now find data section.
  1739. if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1740. sect := f.Sections[i]
  1741. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1742. if sdat, err := sect.Data(); err == nil {
  1743. data := sdat[s.Value-sect.Addr:]
  1744. strdata[n] = string(data)
  1745. }
  1746. }
  1747. }
  1748. break
  1749. }
  1750. if n := indexOfDebugStrlen(s.Name); n != -1 {
  1751. // Found it. Now find data section.
  1752. if i := int(s.Section); 0 <= i && i < len(f.Sections) {
  1753. sect := f.Sections[i]
  1754. if sect.Addr <= s.Value && s.Value < sect.Addr+sect.Size {
  1755. if sdat, err := sect.Data(); err == nil {
  1756. data := sdat[s.Value-sect.Addr:]
  1757. strlen := bo.Uint64(data[:8])
  1758. if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1759. fatalf("string literal too big")
  1760. }
  1761. strlens[n] = int(strlen)
  1762. }
  1763. }
  1764. }
  1765. break
  1766. }
  1767. }
  1768. }
  1769. buildStrings()
  1770. }
  1771. return d, ints, floats, strs
  1772. }
  1773. if f, err := pe.Open(gccTmp()); err == nil {
  1774. defer f.Close()
  1775. d, err := f.DWARF()
  1776. if err != nil {
  1777. fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1778. }
  1779. bo := binary.LittleEndian
  1780. for _, s := range f.Symbols {
  1781. switch {
  1782. case isDebugInts(s.Name):
  1783. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1784. sect := f.Sections[i]
  1785. if s.Value < sect.Size {
  1786. if sdat, err := sect.Data(); err == nil {
  1787. data := sdat[s.Value:]
  1788. ints = make([]int64, len(data)/8)
  1789. for i := range ints {
  1790. ints[i] = int64(bo.Uint64(data[i*8:]))
  1791. }
  1792. }
  1793. }
  1794. }
  1795. case isDebugFloats(s.Name):
  1796. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1797. sect := f.Sections[i]
  1798. if s.Value < sect.Size {
  1799. if sdat, err := sect.Data(); err == nil {
  1800. data := sdat[s.Value:]
  1801. floats = make([]float64, len(data)/8)
  1802. for i := range floats {
  1803. floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1804. }
  1805. }
  1806. }
  1807. }
  1808. default:
  1809. if n := indexOfDebugStr(s.Name); n != -1 {
  1810. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1811. sect := f.Sections[i]
  1812. if s.Value < sect.Size {
  1813. if sdat, err := sect.Data(); err == nil {
  1814. data := sdat[s.Value:]
  1815. strdata[n] = string(data)
  1816. }
  1817. }
  1818. }
  1819. break
  1820. }
  1821. if n := indexOfDebugStrlen(s.Name); n != -1 {
  1822. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1823. sect := f.Sections[i]
  1824. if s.Value < sect.Size {
  1825. if sdat, err := sect.Data(); err == nil {
  1826. data := sdat[s.Value:]
  1827. strlen := bo.Uint64(data[:8])
  1828. if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1829. fatalf("string literal too big")
  1830. }
  1831. strlens[n] = int(strlen)
  1832. }
  1833. }
  1834. }
  1835. break
  1836. }
  1837. }
  1838. }
  1839. buildStrings()
  1840. return d, ints, floats, strs
  1841. }
  1842. if f, err := xcoff.Open(gccTmp()); err == nil {
  1843. defer f.Close()
  1844. d, err := f.DWARF()
  1845. if err != nil {
  1846. fatalf("cannot load DWARF output from %s: %v", gccTmp(), err)
  1847. }
  1848. bo := binary.BigEndian
  1849. for _, s := range f.Symbols {
  1850. switch {
  1851. case isDebugInts(s.Name):
  1852. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1853. sect := f.Sections[i]
  1854. if s.Value < sect.Size {
  1855. if sdat, err := sect.Data(); err == nil {
  1856. data := sdat[s.Value:]
  1857. ints = make([]int64, len(data)/8)
  1858. for i := range ints {
  1859. ints[i] = int64(bo.Uint64(data[i*8:]))
  1860. }
  1861. }
  1862. }
  1863. }
  1864. case isDebugFloats(s.Name):
  1865. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1866. sect := f.Sections[i]
  1867. if s.Value < sect.Size {
  1868. if sdat, err := sect.Data(); err == nil {
  1869. data := sdat[s.Value:]
  1870. floats = make([]float64, len(data)/8)
  1871. for i := range floats {
  1872. floats[i] = math.Float64frombits(bo.Uint64(data[i*8:]))
  1873. }
  1874. }
  1875. }
  1876. }
  1877. default:
  1878. if n := indexOfDebugStr(s.Name); n != -1 {
  1879. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1880. sect := f.Sections[i]
  1881. if s.Value < sect.Size {
  1882. if sdat, err := sect.Data(); err == nil {
  1883. data := sdat[s.Value:]
  1884. strdata[n] = string(data)
  1885. }
  1886. }
  1887. }
  1888. break
  1889. }
  1890. if n := indexOfDebugStrlen(s.Name); n != -1 {
  1891. if i := int(s.SectionNumber) - 1; 0 <= i && i < len(f.Sections) {
  1892. sect := f.Sections[i]
  1893. if s.Value < sect.Size {
  1894. if sdat, err := sect.Data(); err == nil {
  1895. data := sdat[s.Value:]
  1896. strlen := bo.Uint64(data[:8])
  1897. if strlen > (1<<(uint(p.IntSize*8)-1) - 1) { // greater than MaxInt?
  1898. fatalf("string literal too big")
  1899. }
  1900. strlens[n] = int(strlen)
  1901. }
  1902. }
  1903. }
  1904. break
  1905. }
  1906. }
  1907. }
  1908. buildStrings()
  1909. return d, ints, floats, strs
  1910. }
  1911. fatalf("cannot parse gcc output %s as ELF, Mach-O, PE, XCOFF object", gccTmp())
  1912. panic("not reached")
  1913. }
  1914. // gccDefines runs gcc -E -dM -xc - over the C program stdin
  1915. // and returns the corresponding standard output, which is the
  1916. // #defines that gcc encountered while processing the input
  1917. // and its included files.
  1918. func (p *Package) gccDefines(stdin []byte) string {
  1919. base := append(gccBaseCmd, "-E", "-dM", "-xc")
  1920. base = append(base, p.gccMachine()...)
  1921. stdout, _ := runGcc(stdin, append(append(base, p.GccOptions...), "-"))
  1922. return stdout
  1923. }
  1924. // gccErrors runs gcc over the C program stdin and returns
  1925. // the errors that gcc prints. That is, this function expects
  1926. // gcc to fail.
  1927. func (p *Package) gccErrors(stdin []byte, extraArgs ...string) string {
  1928. // TODO(rsc): require failure
  1929. args := p.gccCmd()
  1930. // Optimization options can confuse the error messages; remove them.
  1931. nargs := make([]string, 0, len(args)+len(extraArgs))
  1932. for _, arg := range args {
  1933. if !strings.HasPrefix(arg, "-O") {
  1934. nargs = append(nargs, arg)
  1935. }
  1936. }
  1937. // Force -O0 optimization and append extra arguments, but keep the
  1938. // trailing "-" at the end.
  1939. li := len(nargs) - 1
  1940. last := nargs[li]
  1941. nargs[li] = "-O0"
  1942. nargs = append(nargs, extraArgs...)
  1943. nargs = append(nargs, last)
  1944. if *debugGcc {
  1945. fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(nargs, " "))
  1946. os.Stderr.Write(stdin)
  1947. fmt.Fprint(os.Stderr, "EOF\n")
  1948. }
  1949. stdout, stderr, _ := run(stdin, nargs)
  1950. if *debugGcc {
  1951. os.Stderr.Write(stdout)
  1952. os.Stderr.Write(stderr)
  1953. }
  1954. return string(stderr)
  1955. }
  1956. // runGcc runs the gcc command line args with stdin on standard input.
  1957. // If the command exits with a non-zero exit status, runGcc prints
  1958. // details about what was run and exits.
  1959. // Otherwise runGcc returns the data written to standard output and standard error.
  1960. // Note that for some of the uses we expect useful data back
  1961. // on standard error, but for those uses gcc must still exit 0.
  1962. func runGcc(stdin []byte, args []string) (string, string) {
  1963. if *debugGcc {
  1964. fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(args, " "))
  1965. os.Stderr.Write(stdin)
  1966. fmt.Fprint(os.Stderr, "EOF\n")
  1967. }
  1968. stdout, stderr, ok := run(stdin, args)
  1969. if *debugGcc {
  1970. os.Stderr.Write(stdout)
  1971. os.Stderr.Write(stderr)
  1972. }
  1973. if !ok {
  1974. os.Stderr.Write(stderr)
  1975. os.Exit(2)
  1976. }
  1977. return string(stdout), string(stderr)
  1978. }
  1979. // A typeConv is a translator from dwarf types to Go types
  1980. // with equivalent memory layout.
  1981. type typeConv struct {
  1982. // Cache of already-translated or in-progress types.
  1983. m map[string]*Type
  1984. // Map from types to incomplete pointers to those types.
  1985. ptrs map[string][]*Type
  1986. // Keys of ptrs in insertion order (deterministic worklist)
  1987. // ptrKeys contains exactly the keys in ptrs.
  1988. ptrKeys []dwarf.Type
  1989. // Type names X for which there exists an XGetTypeID function with type func() CFTypeID.
  1990. getTypeIDs map[string]bool
  1991. // badStructs contains C structs that should be marked NotInHeap.
  1992. notInHeapStructs map[string]bool
  1993. // Predeclared types.
  1994. bool ast.Expr
  1995. byte ast.Expr // denotes padding
  1996. int8, int16, int32, int64 ast.Expr
  1997. uint8, uint16, uint32, uint64, uintptr ast.Expr
  1998. float32, float64 ast.Expr
  1999. complex64, complex128 ast.Expr
  2000. void ast.Expr
  2001. string ast.Expr
  2002. goVoid ast.Expr // _Ctype_void, denotes C's void
  2003. goVoidPtr ast.Expr // unsafe.Pointer or *byte
  2004. goVoidPtrNoHeap ast.Expr // *_Ctype_void_notinheap, like goVoidPtr but marked NotInHeap
  2005. ptrSize int64
  2006. intSize int64
  2007. }
  2008. var tagGen int
  2009. var typedef = make(map[string]*Type)
  2010. var goIdent = make(map[string]*ast.Ident)
  2011. // unionWithPointer is true for a Go type that represents a C union (or class)
  2012. // that may contain a pointer. This is used for cgo pointer checking.
  2013. var unionWithPointer = make(map[ast.Expr]bool)
  2014. // anonymousStructTag provides a consistent tag for an anonymous struct.
  2015. // The same dwarf.StructType pointer will always get the same tag.
  2016. var anonymousStructTag = make(map[*dwarf.StructType]string)
  2017. func (c *typeConv) Init(ptrSize, intSize int64) {
  2018. c.ptrSize = ptrSize
  2019. c.intSize = intSize
  2020. c.m = make(map[string]*Type)
  2021. c.ptrs = make(map[string][]*Type)
  2022. c.getTypeIDs = make(map[string]bool)
  2023. c.notInHeapStructs = make(map[string]bool)
  2024. c.bool = c.Ident("bool")
  2025. c.byte = c.Ident("byte")
  2026. c.int8 = c.Ident("int8")
  2027. c.int16 = c.Ident("int16")
  2028. c.int32 = c.Ident("int32")
  2029. c.int64 = c.Ident("int64")
  2030. c.uint8 = c.Ident("uint8")
  2031. c.uint16 = c.Ident("uint16")
  2032. c.uint32 = c.Ident("uint32")
  2033. c.uint64 = c.Ident("uint64")
  2034. c.uintptr = c.Ident("uintptr")
  2035. c.float32 = c.Ident("float32")
  2036. c.float64 = c.Ident("float64")
  2037. c.complex64 = c.Ident("complex64")
  2038. c.complex128 = c.Ident("complex128")
  2039. c.void = c.Ident("void")
  2040. c.string = c.Ident("string")
  2041. c.goVoid = c.Ident("_Ctype_void")
  2042. c.goVoidPtrNoHeap = c.Ident("*_Ctype_void_notinheap")
  2043. // Normally cgo translates void* to unsafe.Pointer,
  2044. // but for historical reasons -godefs uses *byte instead.
  2045. if *godefs {
  2046. c.goVoidPtr = &ast.StarExpr{X: c.byte}
  2047. } else {
  2048. c.goVoidPtr = c.Ident("unsafe.Pointer")
  2049. }
  2050. }
  2051. // base strips away qualifiers and typedefs to get the underlying type
  2052. func base(dt dwarf.Type) dwarf.Type {
  2053. for {
  2054. if d, ok := dt.(*dwarf.QualType); ok {
  2055. dt = d.Type
  2056. continue
  2057. }
  2058. if d, ok := dt.(*dwarf.TypedefType); ok {
  2059. dt = d.Type
  2060. continue
  2061. }
  2062. break
  2063. }
  2064. return dt
  2065. }
  2066. // unqual strips away qualifiers from a DWARF type.
  2067. // In general we don't care about top-level qualifiers.
  2068. func unqual(dt dwarf.Type) dwarf.Type {
  2069. for {
  2070. if d, ok := dt.(*dwarf.QualType); ok {
  2071. dt = d.Type
  2072. } else {
  2073. break
  2074. }
  2075. }
  2076. return dt
  2077. }
  2078. // Map from dwarf text names to aliases we use in package "C".
  2079. var dwarfToName = map[string]string{
  2080. "long int": "long",
  2081. "long unsigned int": "ulong",
  2082. "unsigned int": "uint",
  2083. "short unsigned int": "ushort",
  2084. "unsigned short": "ushort", // Used by Clang; issue 13129.
  2085. "short int": "short",
  2086. "long long int": "longlong",
  2087. "long long unsigned int": "ulonglong",
  2088. "signed char": "schar",
  2089. "unsigned char": "uchar",
  2090. }
  2091. const signedDelta = 64
  2092. // String returns the current type representation. Format arguments
  2093. // are assembled within this method so that any changes in mutable
  2094. // values are taken into account.
  2095. func (tr *TypeRepr) String() string {
  2096. if len(tr.Repr) == 0 {
  2097. return ""
  2098. }
  2099. if len(tr.FormatArgs) == 0 {
  2100. return tr.Repr
  2101. }
  2102. return fmt.Sprintf(tr.Repr, tr.FormatArgs...)
  2103. }
  2104. // Empty reports whether the result of String would be "".
  2105. func (tr *TypeRepr) Empty() bool {
  2106. return len(tr.Repr) == 0
  2107. }
  2108. // Set modifies the type representation.
  2109. // If fargs are provided, repr is used as a format for fmt.Sprintf.
  2110. // Otherwise, repr is used unprocessed as the type representation.
  2111. func (tr *TypeRepr) Set(repr string, fargs ...interface{}) {
  2112. tr.Repr = repr
  2113. tr.FormatArgs = fargs
  2114. }
  2115. // FinishType completes any outstanding type mapping work.
  2116. // In particular, it resolves incomplete pointer types.
  2117. func (c *typeConv) FinishType(pos token.Pos) {
  2118. // Completing one pointer type might produce more to complete.
  2119. // Keep looping until they're all done.
  2120. for len(c.ptrKeys) > 0 {
  2121. dtype := c.ptrKeys[0]
  2122. dtypeKey := dtype.String()
  2123. c.ptrKeys = c.ptrKeys[1:]
  2124. ptrs := c.ptrs[dtypeKey]
  2125. delete(c.ptrs, dtypeKey)
  2126. // Note Type might invalidate c.ptrs[dtypeKey].
  2127. t := c.Type(dtype, pos)
  2128. for _, ptr := range ptrs {
  2129. ptr.Go.(*ast.StarExpr).X = t.Go
  2130. ptr.C.Set("%s*", t.C)
  2131. }
  2132. }
  2133. }
  2134. // Type returns a *Type with the same memory layout as
  2135. // dtype when used as the type of a variable or a struct field.
  2136. func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
  2137. return c.loadType(dtype, pos, "")
  2138. }
  2139. // loadType recursively loads the requested dtype and its dependency graph.
  2140. func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Type {
  2141. // Always recompute bad pointer typedefs, as the set of such
  2142. // typedefs changes as we see more types.
  2143. checkCache := true
  2144. if dtt, ok := dtype.(*dwarf.TypedefType); ok && c.badPointerTypedef(dtt) {
  2145. checkCache = false
  2146. }
  2147. // The cache key should be relative to its parent.
  2148. // See issue https://golang.org/issue/31891
  2149. key := parent + " > " + dtype.String()
  2150. if checkCache {
  2151. if t, ok := c.m[key]; ok {
  2152. if t.Go == nil {
  2153. fatalf("%s: type conversion loop at %s", lineno(pos), dtype)
  2154. }
  2155. return t
  2156. }
  2157. }
  2158. t := new(Type)
  2159. t.Size = dtype.Size() // note: wrong for array of pointers, corrected below
  2160. t.Align = -1
  2161. t.C = &TypeRepr{Repr: dtype.Common().Name}
  2162. c.m[key] = t
  2163. switch dt := dtype.(type) {
  2164. default:
  2165. fatalf("%s: unexpected type: %s", lineno(pos), dtype)
  2166. case *dwarf.AddrType:
  2167. if t.Size != c.ptrSize {
  2168. fatalf("%s: unexpected: %d-byte address type - %s", lineno(pos), t.Size, dtype)
  2169. }
  2170. t.Go = c.uintptr
  2171. t.Align = t.Size
  2172. case *dwarf.ArrayType:
  2173. if dt.StrideBitSize > 0 {
  2174. // Cannot represent bit-sized elements in Go.
  2175. t.Go = c.Opaque(t.Size)
  2176. break
  2177. }
  2178. count := dt.Count
  2179. if count == -1 {
  2180. // Indicates flexible array member, which Go doesn't support.
  2181. // Translate to zero-length array instead.
  2182. count = 0
  2183. }
  2184. sub := c.Type(dt.Type, pos)
  2185. t.Align = sub.Align
  2186. t.Go = &ast.ArrayType{
  2187. Len: c.intExpr(count),
  2188. Elt: sub.Go,
  2189. }
  2190. // Recalculate t.Size now that we know sub.Size.
  2191. t.Size = count * sub.Size
  2192. t.C.Set("__typeof__(%s[%d])", sub.C, dt.Count)
  2193. case *dwarf.BoolType:
  2194. t.Go = c.bool
  2195. t.Align = 1
  2196. case *dwarf.CharType:
  2197. if t.Size != 1 {
  2198. fatalf("%s: unexpected: %d-byte char type - %s", lineno(pos), t.Size, dtype)
  2199. }
  2200. t.Go = c.int8
  2201. t.Align = 1
  2202. case *dwarf.EnumType:
  2203. if t.Align = t.Size; t.Align >= c.ptrSize {
  2204. t.Align = c.ptrSize
  2205. }
  2206. t.C.Set("enum " + dt.EnumName)
  2207. signed := 0
  2208. t.EnumValues = make(map[string]int64)
  2209. for _, ev := range dt.Val {
  2210. t.EnumValues[ev.Name] = ev.Val
  2211. if ev.Val < 0 {
  2212. signed = signedDelta
  2213. }
  2214. }
  2215. switch t.Size + int64(signed) {
  2216. default:
  2217. fatalf("%s: unexpected: %d-byte enum type - %s", lineno(pos), t.Size, dtype)
  2218. case 1:
  2219. t.Go = c.uint8
  2220. case 2:
  2221. t.Go = c.uint16
  2222. case 4:
  2223. t.Go = c.uint32
  2224. case 8:
  2225. t.Go = c.uint64
  2226. case 1 + signedDelta:
  2227. t.Go = c.int8
  2228. case 2 + signedDelta:
  2229. t.Go = c.int16
  2230. case 4 + signedDelta:
  2231. t.Go = c.int32
  2232. case 8 + signedDelta:
  2233. t.Go = c.int64
  2234. }
  2235. case *dwarf.FloatType:
  2236. switch t.Size {
  2237. default:
  2238. fatalf("%s: unexpected: %d-byte float type - %s", lineno(pos), t.Size, dtype)
  2239. case 4:
  2240. t.Go = c.float32
  2241. case 8:
  2242. t.Go = c.float64
  2243. }
  2244. if t.Align = t.Size; t.Align >= c.ptrSize {
  2245. t.Align = c.ptrSize
  2246. }
  2247. case *dwarf.ComplexType:
  2248. switch t.Size {
  2249. default:
  2250. fatalf("%s: unexpected: %d-byte complex type - %s", lineno(pos), t.Size, dtype)
  2251. case 8:
  2252. t.Go = c.complex64
  2253. case 16:
  2254. t.Go = c.complex128
  2255. }
  2256. if t.Align = t.Size / 2; t.Align >= c.ptrSize {
  2257. t.Align = c.ptrSize
  2258. }
  2259. case *dwarf.FuncType:
  2260. // No attempt at translation: would enable calls
  2261. // directly between worlds, but we need to moderate those.
  2262. t.Go = c.uintptr
  2263. t.Align = c.ptrSize
  2264. case *dwarf.IntType:
  2265. if dt.BitSize > 0 {
  2266. fatalf("%s: unexpected: %d-bit int type - %s", lineno(pos), dt.BitSize, dtype)
  2267. }
  2268. switch t.Size {
  2269. default:
  2270. fatalf("%s: unexpected: %d-byte int type - %s", lineno(pos), t.Size, dtype)
  2271. case 1:
  2272. t.Go = c.int8
  2273. case 2:
  2274. t.Go = c.int16
  2275. case 4:
  2276. t.Go = c.int32
  2277. case 8:
  2278. t.Go = c.int64
  2279. case 16:
  2280. t.Go = &ast.ArrayType{
  2281. Len: c.intExpr(t.Size),
  2282. Elt: c.uint8,
  2283. }
  2284. }
  2285. if t.Align = t.Size; t.Align >= c.ptrSize {
  2286. t.Align = c.ptrSize
  2287. }
  2288. case *dwarf.PtrType:
  2289. // Clang doesn't emit DW_AT_byte_size for pointer types.
  2290. if t.Size != c.ptrSize && t.Size != -1 {
  2291. fatalf("%s: unexpected: %d-byte pointer type - %s", lineno(pos), t.Size, dtype)
  2292. }
  2293. t.Size = c.ptrSize
  2294. t.Align = c.ptrSize
  2295. if _, ok := base(dt.Type).(*dwarf.VoidType); ok {
  2296. t.Go = c.goVoidPtr
  2297. t.C.Set("void*")
  2298. dq := dt.Type
  2299. for {
  2300. if d, ok := dq.(*dwarf.QualType); ok {
  2301. t.C.Set(d.Qual + " " + t.C.String())
  2302. dq = d.Type
  2303. } else {
  2304. break
  2305. }
  2306. }
  2307. break
  2308. }
  2309. // Placeholder initialization; completed in FinishType.
  2310. t.Go = &ast.StarExpr{}
  2311. t.C.Set("<incomplete>*")
  2312. key := dt.Type.String()
  2313. if _, ok := c.ptrs[key]; !ok {
  2314. c.ptrKeys = append(c.ptrKeys, dt.Type)
  2315. }
  2316. c.ptrs[key] = append(c.ptrs[key], t)
  2317. case *dwarf.QualType:
  2318. t1 := c.Type(dt.Type, pos)
  2319. t.Size = t1.Size
  2320. t.Align = t1.Align
  2321. t.Go = t1.Go
  2322. if unionWithPointer[t1.Go] {
  2323. unionWithPointer[t.Go] = true
  2324. }
  2325. t.EnumValues = nil
  2326. t.Typedef = ""
  2327. t.C.Set("%s "+dt.Qual, t1.C)
  2328. return t
  2329. case *dwarf.StructType:
  2330. // Convert to Go struct, being careful about alignment.
  2331. // Have to give it a name to simulate C "struct foo" references.
  2332. tag := dt.StructName
  2333. if dt.ByteSize < 0 && tag == "" { // opaque unnamed struct - should not be possible
  2334. break
  2335. }
  2336. if tag == "" {
  2337. tag = anonymousStructTag[dt]
  2338. if tag == "" {
  2339. tag = "__" + strconv.Itoa(tagGen)
  2340. tagGen++
  2341. anonymousStructTag[dt] = tag
  2342. }
  2343. } else if t.C.Empty() {
  2344. t.C.Set(dt.Kind + " " + tag)
  2345. }
  2346. name := c.Ident("_Ctype_" + dt.Kind + "_" + tag)
  2347. t.Go = name // publish before recursive calls
  2348. goIdent[name.Name] = name
  2349. if dt.ByteSize < 0 {
  2350. // Size calculation in c.Struct/c.Opaque will die with size=-1 (unknown),
  2351. // so execute the basic things that the struct case would do
  2352. // other than try to determine a Go representation.
  2353. tt := *t
  2354. tt.C = &TypeRepr{"%s %s", []interface{}{dt.Kind, tag}}
  2355. tt.Go = c.Ident("struct{}")
  2356. if dt.Kind == "struct" {
  2357. // We don't know what the representation of this struct is, so don't let
  2358. // anyone allocate one on the Go side. As a side effect of this annotation,
  2359. // pointers to this type will not be considered pointers in Go. They won't
  2360. // get writebarrier-ed or adjusted during a stack copy. This should handle
  2361. // all the cases badPointerTypedef used to handle, but hopefully will
  2362. // continue to work going forward without any more need for cgo changes.
  2363. tt.NotInHeap = true
  2364. // TODO: we should probably do the same for unions. Unions can't live
  2365. // on the Go heap, right? It currently doesn't work for unions because
  2366. // they are defined as a type alias for struct{}, not a defined type.
  2367. }
  2368. typedef[name.Name] = &tt
  2369. break
  2370. }
  2371. switch dt.Kind {
  2372. case "class", "union":
  2373. t.Go = c.Opaque(t.Size)
  2374. if c.dwarfHasPointer(dt, pos) {
  2375. unionWithPointer[t.Go] = true
  2376. }
  2377. if t.C.Empty() {
  2378. t.C.Set("__typeof__(unsigned char[%d])", t.Size)
  2379. }
  2380. t.Align = 1 // TODO: should probably base this on field alignment.
  2381. typedef[name.Name] = t
  2382. case "struct":
  2383. g, csyntax, align := c.Struct(dt, pos)
  2384. if t.C.Empty() {
  2385. t.C.Set(csyntax)
  2386. }
  2387. t.Align = align
  2388. tt := *t
  2389. if tag != "" {
  2390. tt.C = &TypeRepr{"struct %s", []interface{}{tag}}
  2391. }
  2392. tt.Go = g
  2393. tt.NotInHeap = c.notInHeapStructs[tag]
  2394. typedef[name.Name] = &tt
  2395. }
  2396. case *dwarf.TypedefType:
  2397. // Record typedef for printing.
  2398. if dt.Name == "_GoString_" {
  2399. // Special C name for Go string type.
  2400. // Knows string layout used by compilers: pointer plus length,
  2401. // which rounds up to 2 pointers after alignment.
  2402. t.Go = c.string
  2403. t.Size = c.ptrSize * 2
  2404. t.Align = c.ptrSize
  2405. break
  2406. }
  2407. if dt.Name == "_GoBytes_" {
  2408. // Special C name for Go []byte type.
  2409. // Knows slice layout used by compilers: pointer, length, cap.
  2410. t.Go = c.Ident("[]byte")
  2411. t.Size = c.ptrSize + 4 + 4
  2412. t.Align = c.ptrSize
  2413. break
  2414. }
  2415. name := c.Ident("_Ctype_" + dt.Name)
  2416. goIdent[name.Name] = name
  2417. akey := ""
  2418. if c.anonymousStructTypedef(dt) {
  2419. // only load type recursively for typedefs of anonymous
  2420. // structs, see issues 37479 and 37621.
  2421. akey = key
  2422. }
  2423. sub := c.loadType(dt.Type, pos, akey)
  2424. if c.badPointerTypedef(dt) {
  2425. // Treat this typedef as a uintptr.
  2426. s := *sub
  2427. s.Go = c.uintptr
  2428. s.BadPointer = true
  2429. sub = &s
  2430. // Make sure we update any previously computed type.
  2431. if oldType := typedef[name.Name]; oldType != nil {
  2432. oldType.Go = sub.Go
  2433. oldType.BadPointer = true
  2434. }
  2435. }
  2436. if c.badVoidPointerTypedef(dt) {
  2437. // Treat this typedef as a pointer to a NotInHeap void.
  2438. s := *sub
  2439. s.Go = c.goVoidPtrNoHeap
  2440. sub = &s
  2441. // Make sure we update any previously computed type.
  2442. if oldType := typedef[name.Name]; oldType != nil {
  2443. oldType.Go = sub.Go
  2444. }
  2445. }
  2446. // Check for non-pointer "struct <tag>{...}; typedef struct <tag> *<name>"
  2447. // typedefs that should be marked NotInHeap.
  2448. if ptr, ok := dt.Type.(*dwarf.PtrType); ok {
  2449. if strct, ok := ptr.Type.(*dwarf.StructType); ok {
  2450. if c.badStructPointerTypedef(dt.Name, strct) {
  2451. c.notInHeapStructs[strct.StructName] = true
  2452. // Make sure we update any previously computed type.
  2453. name := "_Ctype_struct_" + strct.StructName
  2454. if oldType := typedef[name]; oldType != nil {
  2455. oldType.NotInHeap = true
  2456. }
  2457. }
  2458. }
  2459. }
  2460. t.Go = name
  2461. t.BadPointer = sub.BadPointer
  2462. t.NotInHeap = sub.NotInHeap
  2463. if unionWithPointer[sub.Go] {
  2464. unionWithPointer[t.Go] = true
  2465. }
  2466. t.Size = sub.Size
  2467. t.Align = sub.Align
  2468. oldType := typedef[name.Name]
  2469. if oldType == nil {
  2470. tt := *t
  2471. tt.Go = sub.Go
  2472. tt.BadPointer = sub.BadPointer
  2473. tt.NotInHeap = sub.NotInHeap
  2474. typedef[name.Name] = &tt
  2475. }
  2476. // If sub.Go.Name is "_Ctype_struct_foo" or "_Ctype_union_foo" or "_Ctype_class_foo",
  2477. // use that as the Go form for this typedef too, so that the typedef will be interchangeable
  2478. // with the base type.
  2479. // In -godefs mode, do this for all typedefs.
  2480. if isStructUnionClass(sub.Go) || *godefs {
  2481. t.Go = sub.Go
  2482. if isStructUnionClass(sub.Go) {
  2483. // Use the typedef name for C code.
  2484. typedef[sub.Go.(*ast.Ident).Name].C = t.C
  2485. }
  2486. // If we've seen this typedef before, and it
  2487. // was an anonymous struct/union/class before
  2488. // too, use the old definition.
  2489. // TODO: it would be safer to only do this if
  2490. // we verify that the types are the same.
  2491. if oldType != nil && isStructUnionClass(oldType.Go) {
  2492. t.Go = oldType.Go
  2493. }
  2494. }
  2495. case *dwarf.UcharType:
  2496. if t.Size != 1 {
  2497. fatalf("%s: unexpected: %d-byte uchar type - %s", lineno(pos), t.Size, dtype)
  2498. }
  2499. t.Go = c.uint8
  2500. t.Align = 1
  2501. case *dwarf.UintType:
  2502. if dt.BitSize > 0 {
  2503. fatalf("%s: unexpected: %d-bit uint type - %s", lineno(pos), dt.BitSize, dtype)
  2504. }
  2505. switch t.Size {
  2506. default:
  2507. fatalf("%s: unexpected: %d-byte uint type - %s", lineno(pos), t.Size, dtype)
  2508. case 1:
  2509. t.Go = c.uint8
  2510. case 2:
  2511. t.Go = c.uint16
  2512. case 4:
  2513. t.Go = c.uint32
  2514. case 8:
  2515. t.Go = c.uint64
  2516. case 16:
  2517. t.Go = &ast.ArrayType{
  2518. Len: c.intExpr(t.Size),
  2519. Elt: c.uint8,
  2520. }
  2521. }
  2522. if t.Align = t.Size; t.Align >= c.ptrSize {
  2523. t.Align = c.ptrSize
  2524. }
  2525. case *dwarf.VoidType:
  2526. t.Go = c.goVoid
  2527. t.C.Set("void")
  2528. t.Align = 1
  2529. }
  2530. switch dtype.(type) {
  2531. case *dwarf.AddrType, *dwarf.BoolType, *dwarf.CharType, *dwarf.ComplexType, *dwarf.IntType, *dwarf.FloatType, *dwarf.UcharType, *dwarf.UintType:
  2532. s := dtype.Common().Name
  2533. if s != "" {
  2534. if ss, ok := dwarfToName[s]; ok {
  2535. s = ss
  2536. }
  2537. s = strings.Replace(s, " ", "", -1)
  2538. name := c.Ident("_Ctype_" + s)
  2539. tt := *t
  2540. typedef[name.Name] = &tt
  2541. if !*godefs {
  2542. t.Go = name
  2543. }
  2544. }
  2545. }
  2546. if t.Size < 0 {
  2547. // Unsized types are [0]byte, unless they're typedefs of other types
  2548. // or structs with tags.
  2549. // if so, use the name we've already defined.
  2550. t.Size = 0
  2551. switch dt := dtype.(type) {
  2552. case *dwarf.TypedefType:
  2553. // ok
  2554. case *dwarf.StructType:
  2555. if dt.StructName != "" {
  2556. break
  2557. }
  2558. t.Go = c.Opaque(0)
  2559. default:
  2560. t.Go = c.Opaque(0)
  2561. }
  2562. if t.C.Empty() {
  2563. t.C.Set("void")
  2564. }
  2565. }
  2566. if t.C.Empty() {
  2567. fatalf("%s: internal error: did not create C name for %s", lineno(pos), dtype)
  2568. }
  2569. return t
  2570. }
  2571. // isStructUnionClass reports whether the type described by the Go syntax x
  2572. // is a struct, union, or class with a tag.
  2573. func isStructUnionClass(x ast.Expr) bool {
  2574. id, ok := x.(*ast.Ident)
  2575. if !ok {
  2576. return false
  2577. }
  2578. name := id.Name
  2579. return strings.HasPrefix(name, "_Ctype_struct_") ||
  2580. strings.HasPrefix(name, "_Ctype_union_") ||
  2581. strings.HasPrefix(name, "_Ctype_class_")
  2582. }
  2583. // FuncArg returns a Go type with the same memory layout as
  2584. // dtype when used as the type of a C function argument.
  2585. func (c *typeConv) FuncArg(dtype dwarf.Type, pos token.Pos) *Type {
  2586. t := c.Type(unqual(dtype), pos)
  2587. switch dt := dtype.(type) {
  2588. case *dwarf.ArrayType:
  2589. // Arrays are passed implicitly as pointers in C.
  2590. // In Go, we must be explicit.
  2591. tr := &TypeRepr{}
  2592. tr.Set("%s*", t.C)
  2593. return &Type{
  2594. Size: c.ptrSize,
  2595. Align: c.ptrSize,
  2596. Go: &ast.StarExpr{X: t.Go},
  2597. C: tr,
  2598. }
  2599. case *dwarf.TypedefType:
  2600. // C has much more relaxed rules than Go for
  2601. // implicit type conversions. When the parameter
  2602. // is type T defined as *X, simulate a little of the
  2603. // laxness of C by making the argument *X instead of T.
  2604. if ptr, ok := base(dt.Type).(*dwarf.PtrType); ok {
  2605. // Unless the typedef happens to point to void* since
  2606. // Go has special rules around using unsafe.Pointer.
  2607. if _, void := base(ptr.Type).(*dwarf.VoidType); void {
  2608. break
  2609. }
  2610. // ...or the typedef is one in which we expect bad pointers.
  2611. // It will be a uintptr instead of *X.
  2612. if c.baseBadPointerTypedef(dt) {
  2613. break
  2614. }
  2615. t = c.Type(ptr, pos)
  2616. if t == nil {
  2617. return nil
  2618. }
  2619. // For a struct/union/class, remember the C spelling,
  2620. // in case it has __attribute__((unavailable)).
  2621. // See issue 2888.
  2622. if isStructUnionClass(t.Go) {
  2623. t.Typedef = dt.Name
  2624. }
  2625. }
  2626. }
  2627. return t
  2628. }
  2629. // FuncType returns the Go type analogous to dtype.
  2630. // There is no guarantee about matching memory layout.
  2631. func (c *typeConv) FuncType(dtype *dwarf.FuncType, pos token.Pos) *FuncType {
  2632. p := make([]*Type, len(dtype.ParamType))
  2633. gp := make([]*ast.Field, len(dtype.ParamType))
  2634. for i, f := range dtype.ParamType {
  2635. // gcc's DWARF generator outputs a single DotDotDotType parameter for
  2636. // function pointers that specify no parameters (e.g. void
  2637. // (*__cgo_0)()). Treat this special case as void. This case is
  2638. // invalid according to ISO C anyway (i.e. void (*__cgo_1)(...) is not
  2639. // legal).
  2640. if _, ok := f.(*dwarf.DotDotDotType); ok && i == 0 {
  2641. p, gp = nil, nil
  2642. break
  2643. }
  2644. p[i] = c.FuncArg(f, pos)
  2645. gp[i] = &ast.Field{Type: p[i].Go}
  2646. }
  2647. var r *Type
  2648. var gr []*ast.Field
  2649. if _, ok := base(dtype.ReturnType).(*dwarf.VoidType); ok {
  2650. gr = []*ast.Field{{Type: c.goVoid}}
  2651. } else if dtype.ReturnType != nil {
  2652. r = c.Type(unqual(dtype.ReturnType), pos)
  2653. gr = []*ast.Field{{Type: r.Go}}
  2654. }
  2655. return &FuncType{
  2656. Params: p,
  2657. Result: r,
  2658. Go: &ast.FuncType{
  2659. Params: &ast.FieldList{List: gp},
  2660. Results: &ast.FieldList{List: gr},
  2661. },
  2662. }
  2663. }
  2664. // Identifier
  2665. func (c *typeConv) Ident(s string) *ast.Ident {
  2666. return ast.NewIdent(s)
  2667. }
  2668. // Opaque type of n bytes.
  2669. func (c *typeConv) Opaque(n int64) ast.Expr {
  2670. return &ast.ArrayType{
  2671. Len: c.intExpr(n),
  2672. Elt: c.byte,
  2673. }
  2674. }
  2675. // Expr for integer n.
  2676. func (c *typeConv) intExpr(n int64) ast.Expr {
  2677. return &ast.BasicLit{
  2678. Kind: token.INT,
  2679. Value: strconv.FormatInt(n, 10),
  2680. }
  2681. }
  2682. // Add padding of given size to fld.
  2683. func (c *typeConv) pad(fld []*ast.Field, sizes []int64, size int64) ([]*ast.Field, []int64) {
  2684. n := len(fld)
  2685. fld = fld[0 : n+1]
  2686. fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident("_")}, Type: c.Opaque(size)}
  2687. sizes = sizes[0 : n+1]
  2688. sizes[n] = size
  2689. return fld, sizes
  2690. }
  2691. // Struct conversion: return Go and (gc) C syntax for type.
  2692. func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.StructType, csyntax string, align int64) {
  2693. // Minimum alignment for a struct is 1 byte.
  2694. align = 1
  2695. var buf bytes.Buffer
  2696. buf.WriteString("struct {")
  2697. fld := make([]*ast.Field, 0, 2*len(dt.Field)+1) // enough for padding around every field
  2698. sizes := make([]int64, 0, 2*len(dt.Field)+1)
  2699. off := int64(0)
  2700. // Rename struct fields that happen to be named Go keywords into
  2701. // _{keyword}. Create a map from C ident -> Go ident. The Go ident will
  2702. // be mangled. Any existing identifier that already has the same name on
  2703. // the C-side will cause the Go-mangled version to be prefixed with _.
  2704. // (e.g. in a struct with fields '_type' and 'type', the latter would be
  2705. // rendered as '__type' in Go).
  2706. ident := make(map[string]string)
  2707. used := make(map[string]bool)
  2708. for _, f := range dt.Field {
  2709. ident[f.Name] = f.Name
  2710. used[f.Name] = true
  2711. }
  2712. if !*godefs {
  2713. for cid, goid := range ident {
  2714. if token.Lookup(goid).IsKeyword() {
  2715. // Avoid keyword
  2716. goid = "_" + goid
  2717. // Also avoid existing fields
  2718. for _, exist := used[goid]; exist; _, exist = used[goid] {
  2719. goid = "_" + goid
  2720. }
  2721. used[goid] = true
  2722. ident[cid] = goid
  2723. }
  2724. }
  2725. }
  2726. anon := 0
  2727. for _, f := range dt.Field {
  2728. name := f.Name
  2729. ft := f.Type
  2730. // In godefs mode, if this field is a C11
  2731. // anonymous union then treat the first field in the
  2732. // union as the field in the struct. This handles
  2733. // cases like the glibc <sys/resource.h> file; see
  2734. // issue 6677.
  2735. if *godefs {
  2736. if st, ok := f.Type.(*dwarf.StructType); ok && name == "" && st.Kind == "union" && len(st.Field) > 0 && !used[st.Field[0].Name] {
  2737. name = st.Field[0].Name
  2738. ident[name] = name
  2739. ft = st.Field[0].Type
  2740. }
  2741. }
  2742. // TODO: Handle fields that are anonymous structs by
  2743. // promoting the fields of the inner struct.
  2744. t := c.Type(ft, pos)
  2745. tgo := t.Go
  2746. size := t.Size
  2747. talign := t.Align
  2748. if f.BitOffset > 0 || f.BitSize > 0 {
  2749. // The layout of bitfields is implementation defined,
  2750. // so we don't know how they correspond to Go fields
  2751. // even if they are aligned at byte boundaries.
  2752. continue
  2753. }
  2754. if talign > 0 && f.ByteOffset%talign != 0 {
  2755. // Drop misaligned fields, the same way we drop integer bit fields.
  2756. // The goal is to make available what can be made available.
  2757. // Otherwise one bad and unneeded field in an otherwise okay struct
  2758. // makes the whole program not compile. Much of the time these
  2759. // structs are in system headers that cannot be corrected.
  2760. continue
  2761. }
  2762. // Round off up to talign, assumed to be a power of 2.
  2763. off = (off + talign - 1) &^ (talign - 1)
  2764. if f.ByteOffset > off {
  2765. fld, sizes = c.pad(fld, sizes, f.ByteOffset-off)
  2766. off = f.ByteOffset
  2767. }
  2768. if f.ByteOffset < off {
  2769. // Drop a packed field that we can't represent.
  2770. continue
  2771. }
  2772. n := len(fld)
  2773. fld = fld[0 : n+1]
  2774. if name == "" {
  2775. name = fmt.Sprintf("anon%d", anon)
  2776. anon++
  2777. ident[name] = name
  2778. }
  2779. fld[n] = &ast.Field{Names: []*ast.Ident{c.Ident(ident[name])}, Type: tgo}
  2780. sizes = sizes[0 : n+1]
  2781. sizes[n] = size
  2782. off += size
  2783. buf.WriteString(t.C.String())
  2784. buf.WriteString(" ")
  2785. buf.WriteString(name)
  2786. buf.WriteString("; ")
  2787. if talign > align {
  2788. align = talign
  2789. }
  2790. }
  2791. if off < dt.ByteSize {
  2792. fld, sizes = c.pad(fld, sizes, dt.ByteSize-off)
  2793. off = dt.ByteSize
  2794. }
  2795. // If the last field in a non-zero-sized struct is zero-sized
  2796. // the compiler is going to pad it by one (see issue 9401).
  2797. // We can't permit that, because then the size of the Go
  2798. // struct will not be the same as the size of the C struct.
  2799. // Our only option in such a case is to remove the field,
  2800. // which means that it cannot be referenced from Go.
  2801. for off > 0 && sizes[len(sizes)-1] == 0 {
  2802. n := len(sizes)
  2803. fld = fld[0 : n-1]
  2804. sizes = sizes[0 : n-1]
  2805. }
  2806. if off != dt.ByteSize {
  2807. fatalf("%s: struct size calculation error off=%d bytesize=%d", lineno(pos), off, dt.ByteSize)
  2808. }
  2809. buf.WriteString("}")
  2810. csyntax = buf.String()
  2811. if *godefs {
  2812. godefsFields(fld)
  2813. }
  2814. expr = &ast.StructType{Fields: &ast.FieldList{List: fld}}
  2815. return
  2816. }
  2817. // dwarfHasPointer reports whether the DWARF type dt contains a pointer.
  2818. func (c *typeConv) dwarfHasPointer(dt dwarf.Type, pos token.Pos) bool {
  2819. switch dt := dt.(type) {
  2820. default:
  2821. fatalf("%s: unexpected type: %s", lineno(pos), dt)
  2822. return false
  2823. case *dwarf.AddrType, *dwarf.BoolType, *dwarf.CharType, *dwarf.EnumType,
  2824. *dwarf.FloatType, *dwarf.ComplexType, *dwarf.FuncType,
  2825. *dwarf.IntType, *dwarf.UcharType, *dwarf.UintType, *dwarf.VoidType:
  2826. return false
  2827. case *dwarf.ArrayType:
  2828. return c.dwarfHasPointer(dt.Type, pos)
  2829. case *dwarf.PtrType:
  2830. return true
  2831. case *dwarf.QualType:
  2832. return c.dwarfHasPointer(dt.Type, pos)
  2833. case *dwarf.StructType:
  2834. for _, f := range dt.Field {
  2835. if c.dwarfHasPointer(f.Type, pos) {
  2836. return true
  2837. }
  2838. }
  2839. return false
  2840. case *dwarf.TypedefType:
  2841. if dt.Name == "_GoString_" || dt.Name == "_GoBytes_" {
  2842. return true
  2843. }
  2844. return c.dwarfHasPointer(dt.Type, pos)
  2845. }
  2846. }
  2847. func upper(s string) string {
  2848. if s == "" {
  2849. return ""
  2850. }
  2851. r, size := utf8.DecodeRuneInString(s)
  2852. if r == '_' {
  2853. return "X" + s
  2854. }
  2855. return string(unicode.ToUpper(r)) + s[size:]
  2856. }
  2857. // godefsFields rewrites field names for use in Go or C definitions.
  2858. // It strips leading common prefixes (like tv_ in tv_sec, tv_usec)
  2859. // converts names to upper case, and rewrites _ into Pad_godefs_n,
  2860. // so that all fields are exported.
  2861. func godefsFields(fld []*ast.Field) {
  2862. prefix := fieldPrefix(fld)
  2863. // Issue 48396: check for duplicate field names.
  2864. if prefix != "" {
  2865. names := make(map[string]bool)
  2866. fldLoop:
  2867. for _, f := range fld {
  2868. for _, n := range f.Names {
  2869. name := n.Name
  2870. if name == "_" {
  2871. continue
  2872. }
  2873. if name != prefix {
  2874. name = strings.TrimPrefix(n.Name, prefix)
  2875. }
  2876. name = upper(name)
  2877. if names[name] {
  2878. // Field name conflict: don't remove prefix.
  2879. prefix = ""
  2880. break fldLoop
  2881. }
  2882. names[name] = true
  2883. }
  2884. }
  2885. }
  2886. npad := 0
  2887. for _, f := range fld {
  2888. for _, n := range f.Names {
  2889. if n.Name != prefix {
  2890. n.Name = strings.TrimPrefix(n.Name, prefix)
  2891. }
  2892. if n.Name == "_" {
  2893. // Use exported name instead.
  2894. n.Name = "Pad_cgo_" + strconv.Itoa(npad)
  2895. npad++
  2896. }
  2897. n.Name = upper(n.Name)
  2898. }
  2899. }
  2900. }
  2901. // fieldPrefix returns the prefix that should be removed from all the
  2902. // field names when generating the C or Go code. For generated
  2903. // C, we leave the names as is (tv_sec, tv_usec), since that's what
  2904. // people are used to seeing in C. For generated Go code, such as
  2905. // package syscall's data structures, we drop a common prefix
  2906. // (so sec, usec, which will get turned into Sec, Usec for exporting).
  2907. func fieldPrefix(fld []*ast.Field) string {
  2908. prefix := ""
  2909. for _, f := range fld {
  2910. for _, n := range f.Names {
  2911. // Ignore field names that don't have the prefix we're
  2912. // looking for. It is common in C headers to have fields
  2913. // named, say, _pad in an otherwise prefixed header.
  2914. // If the struct has 3 fields tv_sec, tv_usec, _pad1, then we
  2915. // still want to remove the tv_ prefix.
  2916. // The check for "orig_" here handles orig_eax in the
  2917. // x86 ptrace register sets, which otherwise have all fields
  2918. // with reg_ prefixes.
  2919. if strings.HasPrefix(n.Name, "orig_") || strings.HasPrefix(n.Name, "_") {
  2920. continue
  2921. }
  2922. i := strings.Index(n.Name, "_")
  2923. if i < 0 {
  2924. continue
  2925. }
  2926. if prefix == "" {
  2927. prefix = n.Name[:i+1]
  2928. } else if prefix != n.Name[:i+1] {
  2929. return ""
  2930. }
  2931. }
  2932. }
  2933. return prefix
  2934. }
  2935. // anonymousStructTypedef reports whether dt is a C typedef for an anonymous
  2936. // struct.
  2937. func (c *typeConv) anonymousStructTypedef(dt *dwarf.TypedefType) bool {
  2938. st, ok := dt.Type.(*dwarf.StructType)
  2939. return ok && st.StructName == ""
  2940. }
  2941. // badPointerTypedef reports whether dt is a C typedef that should not be
  2942. // considered a pointer in Go. A typedef is bad if C code sometimes stores
  2943. // non-pointers in this type.
  2944. // TODO: Currently our best solution is to find these manually and list them as
  2945. // they come up. A better solution is desired.
  2946. // Note: DEPRECATED. There is now a better solution. Search for NotInHeap in this file.
  2947. func (c *typeConv) badPointerTypedef(dt *dwarf.TypedefType) bool {
  2948. if c.badCFType(dt) {
  2949. return true
  2950. }
  2951. if c.badJNI(dt) {
  2952. return true
  2953. }
  2954. if c.badEGLType(dt) {
  2955. return true
  2956. }
  2957. return false
  2958. }
  2959. // badVoidPointerTypedef is like badPointerTypeDef, but for "void *" typedefs that should be NotInHeap.
  2960. func (c *typeConv) badVoidPointerTypedef(dt *dwarf.TypedefType) bool {
  2961. // Match the Windows HANDLE type (#42018).
  2962. if goos != "windows" || dt.Name != "HANDLE" {
  2963. return false
  2964. }
  2965. // Check that the typedef is "typedef void *<name>".
  2966. if ptr, ok := dt.Type.(*dwarf.PtrType); ok {
  2967. if _, ok := ptr.Type.(*dwarf.VoidType); ok {
  2968. return true
  2969. }
  2970. }
  2971. return false
  2972. }
  2973. // badStructPointerTypedef is like badVoidPointerTypedefs but for structs.
  2974. func (c *typeConv) badStructPointerTypedef(name string, dt *dwarf.StructType) bool {
  2975. // Windows handle types can all potentially contain non-pointers.
  2976. // badVoidPointerTypedef handles the "void *" HANDLE type, but other
  2977. // handles are defined as
  2978. //
  2979. // struct <name>__{int unused;}; typedef struct <name>__ *name;
  2980. //
  2981. // by the DECLARE_HANDLE macro in STRICT mode. The macro is declared in
  2982. // the Windows ntdef.h header,
  2983. //
  2984. // https://github.com/tpn/winsdk-10/blob/master/Include/10.0.16299.0/shared/ntdef.h#L779
  2985. if goos != "windows" {
  2986. return false
  2987. }
  2988. if len(dt.Field) != 1 {
  2989. return false
  2990. }
  2991. if dt.StructName != name+"__" {
  2992. return false
  2993. }
  2994. if f := dt.Field[0]; f.Name != "unused" || f.Type.Common().Name != "int" {
  2995. return false
  2996. }
  2997. return true
  2998. }
  2999. // baseBadPointerTypedef reports whether the base of a chain of typedefs is a bad typedef
  3000. // as badPointerTypedef reports.
  3001. func (c *typeConv) baseBadPointerTypedef(dt *dwarf.TypedefType) bool {
  3002. for {
  3003. if t, ok := dt.Type.(*dwarf.TypedefType); ok {
  3004. dt = t
  3005. continue
  3006. }
  3007. break
  3008. }
  3009. return c.badPointerTypedef(dt)
  3010. }
  3011. func (c *typeConv) badCFType(dt *dwarf.TypedefType) bool {
  3012. // The real bad types are CFNumberRef and CFDateRef.
  3013. // Sometimes non-pointers are stored in these types.
  3014. // CFTypeRef is a supertype of those, so it can have bad pointers in it as well.
  3015. // We return true for the other *Ref types just so casting between them is easier.
  3016. // We identify the correct set of types as those ending in Ref and for which
  3017. // there exists a corresponding GetTypeID function.
  3018. // See comment below for details about the bad pointers.
  3019. if goos != "darwin" && goos != "ios" {
  3020. return false
  3021. }
  3022. s := dt.Name
  3023. if !strings.HasSuffix(s, "Ref") {
  3024. return false
  3025. }
  3026. s = s[:len(s)-3]
  3027. if s == "CFType" {
  3028. return true
  3029. }
  3030. if c.getTypeIDs[s] {
  3031. return true
  3032. }
  3033. if i := strings.Index(s, "Mutable"); i >= 0 && c.getTypeIDs[s[:i]+s[i+7:]] {
  3034. // Mutable and immutable variants share a type ID.
  3035. return true
  3036. }
  3037. return false
  3038. }
  3039. // Comment from Darwin's CFInternal.h
  3040. /*
  3041. // Tagged pointer support
  3042. // Low-bit set means tagged object, next 3 bits (currently)
  3043. // define the tagged object class, next 4 bits are for type
  3044. // information for the specific tagged object class. Thus,
  3045. // the low byte is for type info, and the rest of a pointer
  3046. // (32 or 64-bit) is for payload, whatever the tagged class.
  3047. //
  3048. // Note that the specific integers used to identify the
  3049. // specific tagged classes can and will change from release
  3050. // to release (that's why this stuff is in CF*Internal*.h),
  3051. // as can the definition of type info vs payload above.
  3052. //
  3053. #if __LP64__
  3054. #define CF_IS_TAGGED_OBJ(PTR) ((uintptr_t)(PTR) & 0x1)
  3055. #define CF_TAGGED_OBJ_TYPE(PTR) ((uintptr_t)(PTR) & 0xF)
  3056. #else
  3057. #define CF_IS_TAGGED_OBJ(PTR) 0
  3058. #define CF_TAGGED_OBJ_TYPE(PTR) 0
  3059. #endif
  3060. enum {
  3061. kCFTaggedObjectID_Invalid = 0,
  3062. kCFTaggedObjectID_Atom = (0 << 1) + 1,
  3063. kCFTaggedObjectID_Undefined3 = (1 << 1) + 1,
  3064. kCFTaggedObjectID_Undefined2 = (2 << 1) + 1,
  3065. kCFTaggedObjectID_Integer = (3 << 1) + 1,
  3066. kCFTaggedObjectID_DateTS = (4 << 1) + 1,
  3067. kCFTaggedObjectID_ManagedObjectID = (5 << 1) + 1, // Core Data
  3068. kCFTaggedObjectID_Date = (6 << 1) + 1,
  3069. kCFTaggedObjectID_Undefined7 = (7 << 1) + 1,
  3070. };
  3071. */
  3072. func (c *typeConv) badJNI(dt *dwarf.TypedefType) bool {
  3073. // In Dalvik and ART, the jobject type in the JNI interface of the JVM has the
  3074. // property that it is sometimes (always?) a small integer instead of a real pointer.
  3075. // Note: although only the android JVMs are bad in this respect, we declare the JNI types
  3076. // bad regardless of platform, so the same Go code compiles on both android and non-android.
  3077. if parent, ok := jniTypes[dt.Name]; ok {
  3078. // Try to make sure we're talking about a JNI type, not just some random user's
  3079. // type that happens to use the same name.
  3080. // C doesn't have the notion of a package, so it's hard to be certain.
  3081. // Walk up to jobject, checking each typedef on the way.
  3082. w := dt
  3083. for parent != "" {
  3084. t, ok := w.Type.(*dwarf.TypedefType)
  3085. if !ok || t.Name != parent {
  3086. return false
  3087. }
  3088. w = t
  3089. parent, ok = jniTypes[w.Name]
  3090. if !ok {
  3091. return false
  3092. }
  3093. }
  3094. // Check that the typedef is either:
  3095. // 1:
  3096. // struct _jobject;
  3097. // typedef struct _jobject *jobject;
  3098. // 2: (in NDK16 in C++)
  3099. // class _jobject {};
  3100. // typedef _jobject* jobject;
  3101. // 3: (in NDK16 in C)
  3102. // typedef void* jobject;
  3103. if ptr, ok := w.Type.(*dwarf.PtrType); ok {
  3104. switch v := ptr.Type.(type) {
  3105. case *dwarf.VoidType:
  3106. return true
  3107. case *dwarf.StructType:
  3108. if v.StructName == "_jobject" && len(v.Field) == 0 {
  3109. switch v.Kind {
  3110. case "struct":
  3111. if v.Incomplete {
  3112. return true
  3113. }
  3114. case "class":
  3115. if !v.Incomplete {
  3116. return true
  3117. }
  3118. }
  3119. }
  3120. }
  3121. }
  3122. }
  3123. return false
  3124. }
  3125. func (c *typeConv) badEGLType(dt *dwarf.TypedefType) bool {
  3126. if dt.Name != "EGLDisplay" && dt.Name != "EGLConfig" {
  3127. return false
  3128. }
  3129. // Check that the typedef is "typedef void *<name>".
  3130. if ptr, ok := dt.Type.(*dwarf.PtrType); ok {
  3131. if _, ok := ptr.Type.(*dwarf.VoidType); ok {
  3132. return true
  3133. }
  3134. }
  3135. return false
  3136. }
  3137. // jniTypes maps from JNI types that we want to be uintptrs, to the underlying type to which
  3138. // they are mapped. The base "jobject" maps to the empty string.
  3139. var jniTypes = map[string]string{
  3140. "jobject": "",
  3141. "jclass": "jobject",
  3142. "jthrowable": "jobject",
  3143. "jstring": "jobject",
  3144. "jarray": "jobject",
  3145. "jbooleanArray": "jarray",
  3146. "jbyteArray": "jarray",
  3147. "jcharArray": "jarray",
  3148. "jshortArray": "jarray",
  3149. "jintArray": "jarray",
  3150. "jlongArray": "jarray",
  3151. "jfloatArray": "jarray",
  3152. "jdoubleArray": "jarray",
  3153. "jobjectArray": "jarray",
  3154. "jweak": "jobject",
  3155. }