Commit 928623ca authored by Cherry Zhang's avatar Cherry Zhang

[dev.link] cmd/internal/goobj2: separate Autolib from Pkglist in new object file

In CL 196030 we decided to combine the imported package list
(Autolib) and referenced package list (PkgIdx, or Pkglist).
However, in some cases the Autolib list may contain file name,
instead of package path, e.g.
https://go.googlesource.com/go/+/refs/heads/dev.link/src/cmd/compile/internal/gc/main.go#1181
And the linker needs that to locate the file. This mostly happens
with direct invocation of the compiler and linker (i.e., not
through "go build").

Instead of letting the linker make guess of the file name based
on the package path, make Autolib a separate list.

Change-Id: If195a69462d04db515346ee67cdec925f5a69e2e
Reviewed-on: https://go-review.googlesource.com/c/go/+/200157
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJeremy Faller <jeremy@golang.org>
parent cfa421c9
...@@ -21,8 +21,9 @@ func (r *objReader) readNew() { ...@@ -21,8 +21,9 @@ func (r *objReader) readNew() {
} }
// Imports // Imports
r.p.Imports = rr.Autolib()
pkglist := rr.Pkglist() pkglist := rr.Pkglist()
r.p.Imports = pkglist[1:] // index 0 is a dummy invalid package
abiToVer := func(abi uint16) int64 { abiToVer := func(abi uint16) int64 {
var vers int64 var vers int64
......
...@@ -29,7 +29,8 @@ import ( ...@@ -29,7 +29,8 @@ import (
// Data [...]byte // Data [...]byte
// } // }
// //
// PkgIndex [...]stringOff // TODO: add fingerprints // Autolib [...]stringOff // imported packages (for file loading) // TODO: add fingerprints
// PkgIndex [...]stringOff // referenced packages by index
// //
// DwarfFiles [...]stringOff // XXX as a separate block for now // DwarfFiles [...]stringOff // XXX as a separate block for now
// //
...@@ -127,7 +128,8 @@ const ( ...@@ -127,7 +128,8 @@ const (
// Blocks // Blocks
const ( const (
BlkPkgIdx = iota BlkAutolib = iota
BlkPkgIdx
BlkDwarfFile BlkDwarfFile
BlkSymdef BlkSymdef
BlkNonpkgdef BlkNonpkgdef
...@@ -469,6 +471,16 @@ func (r *Reader) StringRef(off uint32) string { ...@@ -469,6 +471,16 @@ func (r *Reader) StringRef(off uint32) string {
return r.StringAt(r.uint32At(off)) return r.StringAt(r.uint32At(off))
} }
func (r *Reader) Autolib() []string {
n := (r.h.Offsets[BlkAutolib+1] - r.h.Offsets[BlkAutolib]) / 4
s := make([]string, n)
for i := range s {
off := r.h.Offsets[BlkAutolib] + uint32(i)*4
s[i] = r.StringRef(off)
}
return s
}
func (r *Reader) Pkglist() []string { func (r *Reader) Pkglist() []string {
n := (r.h.Offsets[BlkPkgIdx+1] - r.h.Offsets[BlkPkgIdx]) / 4 n := (r.h.Offsets[BlkPkgIdx+1] - r.h.Offsets[BlkPkgIdx]) / 4
s := make([]string, n) s := make([]string, n)
......
...@@ -40,6 +40,12 @@ func WriteObjFile2(ctxt *Link, b *bio.Writer, pkgpath string) { ...@@ -40,6 +40,12 @@ func WriteObjFile2(ctxt *Link, b *bio.Writer, pkgpath string) {
// String table // String table
w.StringTable() w.StringTable()
// Autolib
h.Offsets[goobj2.BlkAutolib] = w.Offset()
for _, pkg := range ctxt.Imports {
w.StringRef(pkg)
}
// Package references // Package references
h.Offsets[goobj2.BlkPkgIdx] = w.Offset() h.Offsets[goobj2.BlkPkgIdx] = w.Offset()
for _, pkg := range w.pkglist { for _, pkg := range w.pkglist {
...@@ -172,13 +178,6 @@ func (w *writer) init() { ...@@ -172,13 +178,6 @@ func (w *writer) init() {
for pkg, i := range w.ctxt.pkgIdx { for pkg, i := range w.ctxt.pkgIdx {
w.pkglist[i] = pkg w.pkglist[i] = pkg
} }
// Also make sure imported packages appear in the list (even if no symbol is referenced).
for _, pkg := range w.ctxt.Imports {
if _, ok := w.ctxt.pkgIdx[pkg]; !ok {
w.pkglist = append(w.pkglist, pkg)
}
}
} }
func (w *writer) StringTable() { func (w *writer) StringTable() {
......
...@@ -366,11 +366,7 @@ func LoadNew(l *Loader, arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *s ...@@ -366,11 +366,7 @@ func LoadNew(l *Loader, arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, lib *s
or := &oReader{r, unit, localSymVersion, pkgprefix} or := &oReader{r, unit, localSymVersion, pkgprefix}
// Autolib // Autolib
npkg := r.NPkg() lib.ImportStrings = append(lib.ImportStrings, r.Autolib()...)
lib.ImportStrings = append(lib.ImportStrings, make([]string, npkg-1)...)[:len(lib.ImportStrings)]
for i := 1; i < npkg; i++ {
lib.ImportStrings = append(lib.ImportStrings, r.Pkg(i))
}
// DWARF file table // DWARF file table
nfile := r.NDwarfFile() nfile := r.NDwarfFile()
......
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