Skip to content

Commit

Permalink
cmd/compile: use file content, not suffix, to distinguish .a and .o f…
Browse files Browse the repository at this point in the history
…iles

This allows reading from package storage systems that may not
preserve the .a suffix (used with -importcfg).

Fixes golang#20579 (combined with CLs earlier in stack).

Change-Id: If2fc6a3d01bd0170a757e1f2ba9a22a4d9be7dbf
Reviewed-on: https://go-review.googlesource.com/44853
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
rsc committed Jun 6, 2017
1 parent 4f2269e commit 3c1914f
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/cmd/compile/internal/gc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "!<arch>\n" {
return false
}

// package export block should be first
sz := arsize(b, "__.PKGDEF")
return sz > 0
}

var idirs []string

func addidir(dir string) {
Expand Down Expand Up @@ -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 {
Expand All @@ -992,6 +969,22 @@ func importfile(f *Val) *types.Pkg {
p = p[:len(p)-1]
}

if p == "!<arch>" { // 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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3c1914f

Please sign in to comment.