Commit ea668e18 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: always write pack files

By always writing out pack files, the object file format can be
simplified somewhat. In particular, the export data format will no
longer require escaping, because the pack file provides appropriate
framing.

This CL does not affect build systems that use -pack, which includes
all major Go build systems (cmd/go, gb, bazel).

Also, existing package import logic already distinguishes pack/object
files based on file contents rather than file extension.

The only exception is cmd/pack, which specially handled object files
created by cmd/compile when used with the 'c' mode. This mode is
extended to now recognize the pack files produced by cmd/compile and
handle them as before.

Passes toolstash-check.

Updates #21705.
Updates #24512.

Change-Id: Idf131013bfebd73a5cde7e087eb19964503a9422
Reviewed-on: https://go-review.googlesource.com/102236
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 699b0d4e
...@@ -211,7 +211,7 @@ func Main(archInit func(*Arch)) { ...@@ -211,7 +211,7 @@ func Main(archInit func(*Arch)) {
flag.BoolVar(&nolocalimports, "nolocalimports", false, "reject local (relative) imports") flag.BoolVar(&nolocalimports, "nolocalimports", false, "reject local (relative) imports")
flag.StringVar(&outfile, "o", "", "write output to `file`") flag.StringVar(&outfile, "o", "", "write output to `file`")
flag.StringVar(&myimportpath, "p", "", "set expected package import `path`") flag.StringVar(&myimportpath, "p", "", "set expected package import `path`")
flag.BoolVar(&writearchive, "pack", false, "write package file instead of object file") flag.BoolVar(&writearchive, "pack", false, "write to file.a instead of file.o")
objabi.Flagcount("r", "debug generated wrappers", &Debug['r']) objabi.Flagcount("r", "debug generated wrappers", &Debug['r'])
flag.BoolVar(&flag_race, "race", false, "enable race detector") flag.BoolVar(&flag_race, "race", false, "enable race detector")
objabi.Flagcount("s", "warn about composite literals that can be simplified", &Debug['s']) objabi.Flagcount("s", "warn about composite literals that can be simplified", &Debug['s'])
......
...@@ -62,69 +62,68 @@ func dumpobj1(outfile string, mode int) { ...@@ -62,69 +62,68 @@ func dumpobj1(outfile string, mode int) {
errorexit() errorexit()
} }
defer bout.Close() defer bout.Close()
bout.WriteString("!<arch>\n")
startobj := int64(0) if mode&modeCompilerObj != 0 {
var arhdr [ArhdrSize]byte start := startArchiveEntry(bout)
if writearchive { dumpCompilerObj(bout)
bout.WriteString("!<arch>\n") finishArchiveEntry(bout, start, "__.PKGDEF")
arhdr = [ArhdrSize]byte{}
bout.Write(arhdr[:])
startobj = bout.Offset()
} }
if mode&modeLinkerObj != 0 {
start := startArchiveEntry(bout)
dumpLinkerObj(bout)
finishArchiveEntry(bout, start, "_go_.o")
}
}
printheader := func() { func printObjHeader(bout *bio.Writer) {
fmt.Fprintf(bout, "go object %s %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version, objabi.Expstring()) fmt.Fprintf(bout, "go object %s %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version, objabi.Expstring())
if buildid != "" { if buildid != "" {
fmt.Fprintf(bout, "build id %q\n", buildid) fmt.Fprintf(bout, "build id %q\n", buildid)
} }
if localpkg.Name == "main" { if localpkg.Name == "main" {
fmt.Fprintf(bout, "main\n") fmt.Fprintf(bout, "main\n")
} }
if safemode { if safemode {
fmt.Fprintf(bout, "safe\n") fmt.Fprintf(bout, "safe\n")
} else { } else {
fmt.Fprintf(bout, "----\n") // room for some other tool to write "safe" fmt.Fprintf(bout, "----\n") // room for some other tool to write "safe"
}
fmt.Fprintf(bout, "\n") // header ends with blank line
} }
fmt.Fprintf(bout, "\n") // header ends with blank line
}
printheader() func startArchiveEntry(bout *bio.Writer) int64 {
var arhdr [ArhdrSize]byte
bout.Write(arhdr[:])
return bout.Offset()
}
if mode&modeCompilerObj != 0 { func finishArchiveEntry(bout *bio.Writer, start int64, name string) {
dumpexport(bout) bout.Flush()
size := bout.Offset() - start
if size&1 != 0 {
bout.WriteByte(0)
} }
bout.Seek(start-ArhdrSize, 0)
if writearchive { var arhdr [ArhdrSize]byte
bout.Flush() formathdr(arhdr[:], name, size)
size := bout.Offset() - startobj bout.Write(arhdr[:])
if size&1 != 0 { bout.Flush()
bout.WriteByte(0) bout.Seek(start+size+(size&1), 0)
} }
bout.Seek(startobj-ArhdrSize, 0)
formathdr(arhdr[:], "__.PKGDEF", size)
bout.Write(arhdr[:])
bout.Flush()
bout.Seek(startobj+size+(size&1), 0)
}
if mode&modeLinkerObj == 0 { func dumpCompilerObj(bout *bio.Writer) {
return printObjHeader(bout)
} dumpexport(bout)
}
if writearchive { func dumpLinkerObj(bout *bio.Writer) {
// start object file printObjHeader(bout)
arhdr = [ArhdrSize]byte{}
bout.Write(arhdr[:])
startobj = bout.Offset()
printheader()
}
if pragcgobuf != "" { if pragcgobuf != "" {
if writearchive { // write empty export section; must be before cgo section
// write empty export section; must be before cgo section fmt.Fprintf(bout, "\n$$\n\n$$\n\n")
fmt.Fprintf(bout, "\n$$\n\n$$\n\n")
}
fmt.Fprintf(bout, "\n$$ // cgo\n") fmt.Fprintf(bout, "\n$$ // cgo\n")
fmt.Fprintf(bout, "%s\n$$\n\n", pragcgobuf) fmt.Fprintf(bout, "%s\n$$\n\n", pragcgobuf)
} }
...@@ -158,17 +157,6 @@ func dumpobj1(outfile string, mode int) { ...@@ -158,17 +157,6 @@ func dumpobj1(outfile string, mode int) {
addGCLocals() addGCLocals()
obj.WriteObjFile(Ctxt, bout.Writer) obj.WriteObjFile(Ctxt, bout.Writer)
if writearchive {
bout.Flush()
size := bout.Offset() - startobj
if size&1 != 0 {
bout.WriteByte(0)
}
bout.Seek(startobj-ArhdrSize, 0)
formathdr(arhdr[:], "_go_.o", size)
bout.Write(arhdr[:])
}
} }
func addptabs() { func addptabs() {
......
...@@ -5,13 +5,11 @@ ...@@ -5,13 +5,11 @@
package main package main
import ( import (
"bufio"
"bytes"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
...@@ -332,11 +330,26 @@ func (ar *Archive) addFiles() { ...@@ -332,11 +330,26 @@ func (ar *Archive) addFiles() {
if verbose { if verbose {
fmt.Printf("%s\n", file) fmt.Printf("%s\n", file)
} }
fd, err := os.Open(file)
if err != nil { if !isGoCompilerObjFile(file) {
log.Fatal(err) fd, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
ar.addFile(fd)
continue
} }
ar.addFile(fd)
aro := archive(file, os.O_RDONLY, nil)
aro.scan(func(entry *Entry) {
if entry.name != "_go_.o" {
aro.skip(entry)
return
}
ar.startFile(filepath.Base(file), 0, 0, 0, 0644, entry.size)
aro.output(entry, ar.fd)
ar.endFile()
})
} }
ar.files = nil ar.files = nil
} }
...@@ -397,61 +410,29 @@ func (ar *Archive) endFile() { ...@@ -397,61 +410,29 @@ func (ar *Archive) endFile() {
// from the first Go object file on the file list, if any. // from the first Go object file on the file list, if any.
// The archive is known to be empty. // The archive is known to be empty.
func (ar *Archive) addPkgdef() { func (ar *Archive) addPkgdef() {
done := false
for _, file := range ar.files { for _, file := range ar.files {
pkgdef, err := readPkgdef(file) if !isGoCompilerObjFile(file) {
if err != nil {
continue continue
} }
if verbose { aro := archive(file, os.O_RDONLY, nil)
fmt.Printf("__.PKGDEF # %s\n", file) aro.scan(func(entry *Entry) {
} if entry.name != "__.PKGDEF" {
ar.startFile("__.PKGDEF", 0, 0, 0, 0644, int64(len(pkgdef))) aro.skip(entry)
_, err = ar.fd.Write(pkgdef) return
if err != nil { }
log.Fatal("writing __.PKGDEF: ", err) if verbose {
} fmt.Printf("__.PKGDEF # %s\n", file)
ar.endFile() }
break ar.startFile("__.PKGDEF", 0, 0, 0, 0644, entry.size)
} aro.output(entry, ar.fd)
} ar.endFile()
done = true
// readPkgdef extracts the __.PKGDEF data from a Go object file. })
func readPkgdef(file string) (data []byte, err error) { if done {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
// Read from file, collecting header for __.PKGDEF.
// The header is from the beginning of the file until a line
// containing just "!". The first line must begin with "go object ".
//
// Note: It's possible for "\n!\n" to appear within the binary
// package export data format. To avoid truncating the package
// definition prematurely (issue 21703), we keep keep track of
// how many "$$" delimiters we've seen.
rbuf := bufio.NewReader(f)
var wbuf bytes.Buffer
markers := 0
for {
line, err := rbuf.ReadBytes('\n')
if err != nil {
return nil, err
}
if wbuf.Len() == 0 && !bytes.HasPrefix(line, []byte("go object ")) {
return nil, errors.New("not a Go object file")
}
if markers%2 == 0 && bytes.Equal(line, []byte("!\n")) {
break break
} }
if bytes.HasPrefix(line, []byte("$$")) {
markers++
}
wbuf.Write(line)
} }
return wbuf.Bytes(), nil
} }
// exactly16Bytes truncates the string if necessary so it is at most 16 bytes long, // exactly16Bytes truncates the string if necessary so it is at most 16 bytes long,
...@@ -514,3 +495,67 @@ func (ar *Archive) extractContents(entry *Entry) { ...@@ -514,3 +495,67 @@ func (ar *Archive) extractContents(entry *Entry) {
ar.skip(entry) ar.skip(entry)
} }
} }
// isGoCompilerObjFile reports whether file is an object file created
// by the Go compiler.
func isGoCompilerObjFile(file string) bool {
fd, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
// Check for "!<arch>\n" header.
buf := make([]byte, len(arHeader))
_, err = io.ReadFull(fd, buf)
if err != nil {
if err == io.EOF {
return false
}
log.Fatal(err)
}
if string(buf) != arHeader {
return false
}
// Check for exactly two entries: "__.PKGDEF" and "_go_.o".
match := []string{"__.PKGDEF", "_go_.o"}
buf = make([]byte, entryLen)
for {
_, err := io.ReadFull(fd, buf)
if err != nil {
if err == io.EOF {
// No entries left.
return true
}
log.Fatal(err)
}
if buf[entryLen-2] != '`' || buf[entryLen-1] != '\n' {
return false
}
name := strings.TrimRight(string(buf[:16]), " ")
for {
if len(match) == 0 {
return false
}
var next string
next, match = match[0], match[1:]
if name == next {
break
}
}
size, err := strconv.ParseInt(strings.TrimRight(string(buf[48:58]), " "), 10, 64)
if err != nil {
return false
}
if size&1 != 0 {
size++
}
_, err = fd.Seek(size, io.SeekCurrent)
if err != nil {
log.Fatal(err)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment