go/gccgoexportdata: correctly handle archive files containing string tables

If the name of an archive member is longer than 16 bytes, an ELF archive
file contains a string table of file names under the name "//", and this
member has no mode.  Skip such members.

+ Test.

Change-Id: Ib10db1cc42816c9002433be6176240e490678560
Reviewed-on: https://go-review.googlesource.com/32973
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Alan Donovan 2016-11-09 12:56:38 -05:00
parent 44b4c5d044
commit 5061f921c7
4 changed files with 68 additions and 31 deletions

View File

@ -74,17 +74,14 @@ func NewReader(r io.Reader) (io.Reader, error) {
return sec.Open(), nil return sec.Open(), nil
} }
// firstSection returns the contents of the first non-empty section of the archive file. // firstSection returns the contents of the first regular file in an ELF
// archive (http://www.sco.com/developers/devspecs/gabi41.pdf, §7.2).
func firstSection(a []byte) ([]byte, error) { func firstSection(a []byte) ([]byte, error) {
for len(a) >= 60 { for len(a) >= 60 {
var hdr []byte var hdr []byte
hdr, a = a[:60], a[60:] hdr, a = a[:60], a[60:]
modeStr := string(string(hdr[40:48])) name := strings.TrimSpace(string(hdr[:16]))
mode, err := strconv.Atoi(strings.TrimSpace(modeStr))
if err != nil {
return nil, fmt.Errorf("invalid mode: %q", modeStr)
}
sizeStr := string(hdr[48:58]) sizeStr := string(hdr[48:58])
size, err := strconv.Atoi(strings.TrimSpace(sizeStr)) size, err := strconv.Atoi(strings.TrimSpace(sizeStr))
@ -92,16 +89,25 @@ func firstSection(a []byte) ([]byte, error) {
return nil, fmt.Errorf("invalid size: %q", sizeStr) return nil, fmt.Errorf("invalid size: %q", sizeStr)
} }
var payload []byte if len(a) < size {
payload, a = a[:size], a[size:] return nil, fmt.Errorf("invalid section size: %d", size)
}
if mode == 0 { // The payload is padded to an even number of bytes.
continue // skip "/" var payload []byte
payload, a = a[:size], a[size+size&1:]
// Skip special files:
// "/" archive symbol table
// "/SYM64/" archive symbol table on e.g. s390x
// "//" archive string table (if any filename is >15 bytes)
if name == "/" || name == "/SYM64/" || name == "//" {
continue
} }
return payload, nil return payload, nil
} }
return nil, fmt.Errorf("archive has no non-empty sections") return nil, fmt.Errorf("archive has no regular sections")
} }
// Read reads export data from in, decodes it, and returns type // Read reads export data from in, decodes it, and returns type

View File

@ -9,28 +9,59 @@ import (
) )
// Test ensures this package can read gccgo export data from the // Test ensures this package can read gccgo export data from the
// .go_export section of an ELF file. // .go_export from a standalone ELF file or such a file in an archive
// library.
//
// The testdata/{short,long}.a ELF archive files were produced by:
//
// $ echo 'package foo; func F()' > foo.go
// $ gccgo -c -fgo-pkgpath blah foo.go
// $ objcopy -j .go_export foo.o foo.gox
// $ ar q short.a foo.gox
// $ objcopy -j .go_export foo.o name-longer-than-16-bytes.gox
// $ ar q long.a name-longer-than-16-bytes.gox
//
// The file long.a contains an archive string table.
//
// The errors.gox file (an ELF object file) comes from the toolchain's
// standard library.
func Test(t *testing.T) { func Test(t *testing.T) {
f, err := os.Open("testdata/errors.gox") for _, test := range []struct {
if err != nil { filename, path, member, wantType string
t.Fatal(err) }{
} {"testdata/errors.gox", "errors", "New", "func(text string) error"},
defer f.Close() {"testdata/short.a", "short", "F", "func()"},
r, err := gccgoexportdata.NewReader(f) {"testdata/long.a", "long", "F", "func()"},
if err != nil { } {
t.Fatal(err) t.Logf("filename = %s", test.filename)
} f, err := os.Open(test.filename)
imports := make(map[string]*types.Package) if err != nil {
pkg, err := gccgoexportdata.Read(r, nil, imports, "errors") t.Error(err)
if err != nil { continue
}
defer f.Close()
r, err := gccgoexportdata.NewReader(f)
if err != nil {
t.Error(err)
continue
}
t.Fatal(err) imports := make(map[string]*types.Package)
} pkg, err := gccgoexportdata.Read(r, nil, imports, test.path)
if err != nil {
t.Error(err)
continue
}
// Check type of errors.New. // Check type of designated package member.
got := pkg.Scope().Lookup("New").Type().String() obj := pkg.Scope().Lookup(test.member)
want := "func(text string) error" if obj == nil {
if got != want { t.Errorf("%s.%s not found", test.path, test.member)
t.Errorf("New.Type = %s, want %s", got, want) continue
}
if obj.Type().String() != test.wantType {
t.Errorf("%s.%s.Type = %s, want %s",
test.path, test.member, obj.Type(), test.wantType)
}
} }
} }

BIN
go/gccgoexportdata/testdata/long.a vendored Normal file

Binary file not shown.

BIN
go/gccgoexportdata/testdata/short.a vendored Normal file

Binary file not shown.