From 3c1914fc46e67ee7628428b68ab3497ca5d44d89 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 1 Jun 2017 14:26:55 -0400 Subject: [PATCH] cmd/compile: use file content, not suffix, to distinguish .a and .o files This allows reading from package storage systems that may not preserve the .a suffix (used with -importcfg). Fixes #20579 (combined with CLs earlier in stack). Change-Id: If2fc6a3d01bd0170a757e1f2ba9a22a4d9be7dbf Reviewed-on: https://go-review.googlesource.com/44853 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/compile/internal/gc/main.go | 41 ++++++++++++----------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index e123648d7f..8c3878e354 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -750,21 +750,6 @@ func arsize(b *bufio.Reader, name string) int { return i } -func skiptopkgdef(b *bufio.Reader) bool { - // archive header - p, err := b.ReadString('\n') - if err != nil { - log.Fatalf("reading input: %v", err) - } - if p != "!\n" { - return false - } - - // package export block should be first - sz := arsize(b, "__.PKGDEF") - return sz > 0 -} - var idirs []string func addidir(dir string) { @@ -975,14 +960,6 @@ func importfile(f *Val) *types.Pkg { defer impf.Close() imp := bufio.NewReader(impf) - const pkgSuffix = ".a" - if strings.HasSuffix(file, pkgSuffix) { - if !skiptopkgdef(imp) { - yyerror("import %s: not a package file", file) - errorexit() - } - } - // check object header p, err := imp.ReadString('\n') if err != nil { @@ -992,6 +969,22 @@ func importfile(f *Val) *types.Pkg { p = p[:len(p)-1] } + if p == "!" { // package archive + // package export block should be first + sz := arsize(imp, "__.PKGDEF") + if sz <= 0 { + yyerror("import %s: not a package file", file) + errorexit() + } + p, err = imp.ReadString('\n') + if err != nil { + log.Fatalf("reading input: %v", err) + } + if len(p) > 0 { + p = p[:len(p)-1] + } + } + if p != "empty archive" { if !strings.HasPrefix(p, "go object ") { yyerror("import %s: not a go object file: %s", file, p) @@ -1030,7 +1023,7 @@ func importfile(f *Val) *types.Pkg { Ctxt.AddImport(path_) } else { // For file "/Users/foo/go/pkg/darwin_amd64/math.a" record "math.a". - Ctxt.AddImport(file[len(file)-len(path_)-len(pkgSuffix):]) + Ctxt.AddImport(file[len(file)-len(path_)-len(".a"):]) } // In the importfile, if we find: -- 2.50.0