diff options
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r-- | src/cmd/cgo/ast.go | 1 | ||||
-rw-r--r-- | src/cmd/cgo/gcc.go | 12 | ||||
-rw-r--r-- | src/cmd/cgo/main.go | 18 | ||||
-rw-r--r-- | src/cmd/cgo/out.go | 8 |
4 files changed, 27 insertions, 12 deletions
diff --git a/src/cmd/cgo/ast.go b/src/cmd/cgo/ast.go index 29fe55240..9b122676c 100644 --- a/src/cmd/cgo/ast.go +++ b/src/cmd/cgo/ast.go @@ -35,6 +35,7 @@ type Prog struct { Typedef map[string]ast.Expr; Vardef map[string]*Type; Funcdef map[string]*FuncType; + PtrSize int64; } // A Type collects information about a type in both the C and Go worlds. diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index 27090fdf4..e3f526845 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -20,7 +20,7 @@ import ( "strings"; ) -func (p *Prog) loadDebugInfo(ptrSize int64) { +func (p *Prog) loadDebugInfo() { // Construct a slice of unique names from p.Crefs. m := make(map[string]int); for _, c := range p.Crefs { @@ -57,7 +57,7 @@ func (p *Prog) loadDebugInfo(ptrSize int64) { b.WriteString("}\n"); kind := make(map[string]string); - _, stderr := gccDebug(b.Bytes()); + _, stderr := p.gccDebug(b.Bytes()); if stderr == "" { fatal("gcc produced no output"); } @@ -109,7 +109,7 @@ func (p *Prog) loadDebugInfo(ptrSize int64) { for i, n := range names { fmt.Fprintf(&b, "typeof(%s) *__cgo__%d;\n", n, i); } - d, stderr := gccDebug(b.Bytes()); + d, stderr := p.gccDebug(b.Bytes()); if d == nil { fatal("gcc failed:\n%s\non input:\n%s", stderr, b.Bytes()); } @@ -158,7 +158,7 @@ func (p *Prog) loadDebugInfo(ptrSize int64) { // Record types and typedef information in Crefs. var conv typeConv; - conv.Init(ptrSize); + conv.Init(p.PtrSize); for _, c := range p.Crefs { i := m[c.Name]; c.TypeName = kind[c.Name] == "type"; @@ -175,9 +175,9 @@ func (p *Prog) loadDebugInfo(ptrSize int64) { // gccDebug runs gcc -gdwarf-2 over the C program stdin and // returns the corresponding DWARF data and any messages // printed to standard error. -func gccDebug(stdin []byte) (*dwarf.Data, string) { +func (p *Prog) gccDebug(stdin []byte) (*dwarf.Data, string) { machine := "-m32"; - if os.Getenv("GOARCH") == "amd64" { + if p.PtrSize == 8 { machine = "-m64"; } diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 0832b3f40..b629f0a22 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -22,20 +22,34 @@ func usage() { flag.PrintDefaults(); } -const ptrSize = 8 // TODO +var ptrSizeMap = map[string]int64 { + "386": 4, + "amd64": 8, + "arm": 4 +} func main() { flag.Usage = usage; flag.Parse(); + arch := os.Getenv("GOARCH"); + if arch == "" { + fatal("$GOARCH is not set"); + } + ptrSize, ok := ptrSizeMap[arch]; + if !ok { + fatal("unknown architecture %s", arch); + } + args := flag.Args(); if len(args) != 1 { usage(); os.Exit(2); } p := openProg(args[0]); + p.PtrSize = ptrSize; p.Preamble = p.Preamble + "\n" + builtinProlog; - p.loadDebugInfo(ptrSize); + p.loadDebugInfo(); p.Vardef = make(map[string]*Type); p.Funcdef = make(map[string]*FuncType); diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 36fbe0349..91473abeb 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -100,8 +100,8 @@ func (p *Prog) writeOutput(srcfile string) { structType += fmt.Sprintf("\t\t%s p%d;\n", t.C, i); off += t.Size; } - if off%ptrSize != 0 { - pad := ptrSize - off%ptrSize; + if off%p.PtrSize != 0 { + pad := p.PtrSize - off%p.PtrSize; structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad); off += pad; npad++; @@ -116,8 +116,8 @@ func (p *Prog) writeOutput(srcfile string) { structType += fmt.Sprintf("\t\t%s r;\n", t.C); off += t.Size; } - if off%ptrSize != 0 { - pad := ptrSize - off%ptrSize; + if off%p.PtrSize != 0 { + pad := p.PtrSize - off%p.PtrSize; structType += fmt.Sprintf("\t\tchar __pad%d[%d];\n", npad, pad); off += pad; npad++; |