diff options
author | Russ Cox <rsc@golang.org> | 2014-05-12 23:48:20 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2014-05-12 23:48:20 -0400 |
commit | 0184e13ac6c2e414821e24bfdc191ea6f25974ad (patch) | |
tree | f718c6417f7156b12d5c31edb0d0efeaebc027cf /src/cmd/cgo | |
parent | 79aad3273cccf7e15e4a133069988e3b3a546d74 (diff) | |
download | go-0184e13ac6c2e414821e24bfdc191ea6f25974ad.tar.gz |
cmd/cgo: omit misaligned struct fields, like we omit bitfields
Fixes issue 7560.
LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://codereview.appspot.com/96300045
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r-- | src/cmd/cgo/doc.go | 3 | ||||
-rw-r--r-- | src/cmd/cgo/gcc.go | 15 |
2 files changed, 15 insertions, 3 deletions
diff --git a/src/cmd/cgo/doc.go b/src/cmd/cgo/doc.go index 605bab6d2..057d25f5b 100644 --- a/src/cmd/cgo/doc.go +++ b/src/cmd/cgo/doc.go @@ -68,6 +68,9 @@ Go references to C Within the Go file, C's struct field names that are keywords in Go can be accessed by prefixing them with an underscore: if x points at a C struct with a field named "type", x._type accesses the field. +C struct fields that cannot be expressed in Go, such as bit fields +or misaligned data, are omitted in the Go struct, replaced by +appropriate padding to reach the next field or the end of the struct. The standard C numeric types are available under the names C.char, C.schar (signed char), C.uchar (unsigned char), diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index b8b783051..c5fcdfc3d 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -1499,7 +1499,7 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct t := c.Type(f.Type, pos) tgo := t.Go size := t.Size - + talign := t.Align if f.BitSize > 0 { if f.BitSize%8 != 0 { continue @@ -1512,8 +1512,17 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct name = "uint" } tgo = ast.NewIdent(name + fmt.Sprint(f.BitSize)) + talign = size } + if talign > 0 && f.ByteOffset%talign != 0 { + // Drop misaligned fields, the same way we drop integer bit fields. + // The goal is to make available what can be made available. + // Otherwise one bad and unneeded field in an otherwise okay struct + // makes the whole program not compile. Much of the time these + // structs are in system headers that cannot be corrected. + continue + } n := len(fld) fld = fld[0 : n+1] name := f.Name @@ -1528,8 +1537,8 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct buf.WriteString(" ") buf.WriteString(name) buf.WriteString("; ") - if t.Align > align { - align = t.Align + if talign > align { + align = talign } } if off < dt.ByteSize { |