@@ -529,15 +529,8 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
529
529
}
530
530
// We're trying to write a gcc struct that matches 6c/8c/5c's layout.
531
531
// Use packed attribute to force no padding in this struct in case
532
- // gcc has different packing requirements. For example,
533
- // on 386 Windows, gcc wants to 8-align int64s, but 8c does not.
534
- // Use __gcc_struct__ to work around http://gcc.gnu.org/PR52991 on x86,
535
- // and http://golang.org/issue/5603.
536
- extraAttr := ""
537
- if ! strings .Contains (p .gccBaseCmd ()[0 ], "clang" ) && (goarch == "amd64" || goarch == "386" ) {
538
- extraAttr = ", __gcc_struct__"
539
- }
540
- fmt .Fprintf (fgcc , "\t %s __attribute__((__packed__%v)) *a = v;\n " , ctype , extraAttr )
532
+ // gcc has different packing requirements.
533
+ fmt .Fprintf (fgcc , "\t %s %v *a = v;\n " , ctype , p .packedAttribute ())
541
534
fmt .Fprintf (fgcc , "\t " )
542
535
if t := n .FuncType .Result ; t != nil {
543
536
fmt .Fprintf (fgcc , "a->r = " )
@@ -618,6 +611,19 @@ func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) {
618
611
fmt .Fprintf (fgcc , "\n " )
619
612
}
620
613
614
+ // packedAttribute returns host compiler struct attribute that will be
615
+ // used to match 6c/8c/5c's struct layout. For example, on 386 Windows,
616
+ // gcc wants to 8-align int64s, but 8c does not.
617
+ // Use __gcc_struct__ to work around http://gcc.gnu.org/PR52991 on x86,
618
+ // and http://golang.org/issue/5603.
619
+ func (p * Package ) packedAttribute () string {
620
+ s := "__attribute__((__packed__"
621
+ if ! strings .Contains (p .gccBaseCmd ()[0 ], "clang" ) && (goarch == "amd64" || goarch == "386" ) {
622
+ s += ", __gcc_struct__"
623
+ }
624
+ return s + "))"
625
+ }
626
+
621
627
// Write out the various stubs we need to support functions exported
622
628
// from Go so that they are callable from C.
623
629
func (p * Package ) writeExports (fgo2 , fc , fm * os.File ) {
@@ -727,7 +733,7 @@ func (p *Package) writeExports(fgo2, fc, fm *os.File) {
727
733
fmt .Fprintf (fgcc , "extern void _cgoexp%s_%s(void *, int);\n " , cPrefix , exp .ExpName )
728
734
fmt .Fprintf (fgcc , "\n %s\n " , s )
729
735
fmt .Fprintf (fgcc , "{\n " )
730
- fmt .Fprintf (fgcc , "\t %s __attribute__((packed)) a;\n " , ctype )
736
+ fmt .Fprintf (fgcc , "\t %s %v a;\n " , ctype , p . packedAttribute () )
731
737
if gccResult != "void" && (len (fntype .Results .List ) > 1 || len (fntype .Results .List [0 ].Names ) > 1 ) {
732
738
fmt .Fprintf (fgcc , "\t %s r;\n " , gccResult )
733
739
}
0 commit comments