2019-08-22 17:26:41 -04:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Go new object file format, reading and writing.
|
|
|
|
|
|
|
|
|
|
package goobj2 // TODO: replace the goobj package?
|
|
|
|
|
|
|
|
|
|
import (
|
2019-09-27 14:49:44 -04:00
|
|
|
"bytes"
|
2019-08-22 17:26:41 -04:00
|
|
|
"cmd/internal/bio"
|
2020-07-10 18:49:01 -04:00
|
|
|
"crypto/sha1"
|
2019-08-22 17:26:41 -04:00
|
|
|
"encoding/binary"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2020-04-30 17:05:59 -04:00
|
|
|
"internal/unsafeheader"
|
2019-08-22 17:26:41 -04:00
|
|
|
"io"
|
2019-09-27 14:49:44 -04:00
|
|
|
"unsafe"
|
2019-08-22 17:26:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// New object file format.
|
|
|
|
|
//
|
|
|
|
|
// Header struct {
|
2020-07-21 15:53:30 -04:00
|
|
|
// Magic [...]byte // "\x00go116ld"
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
// Fingerprint [8]byte
|
|
|
|
|
// Flags uint32
|
|
|
|
|
// Offsets [...]uint32 // byte offset of each block below
|
2019-08-22 17:26:41 -04:00
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// Strings [...]struct {
|
|
|
|
|
// Data [...]byte
|
|
|
|
|
// }
|
|
|
|
|
//
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
// Autolib [...]struct { // imported packages (for file loading)
|
|
|
|
|
// Pkg string
|
|
|
|
|
// Fingerprint [8]byte
|
|
|
|
|
// }
|
|
|
|
|
//
|
2020-03-31 20:01:46 -04:00
|
|
|
// PkgIndex [...]string // referenced packages by index
|
2019-08-22 17:26:41 -04:00
|
|
|
//
|
2020-07-29 11:06:02 -04:00
|
|
|
// Files [...]string
|
2019-09-30 11:43:41 -04:00
|
|
|
//
|
2019-08-22 17:26:41 -04:00
|
|
|
// SymbolDefs [...]struct {
|
[dev.link] cmd/compile, cmd/link: reference type symbol of defined type by index
The type descriptor symbol of a defined (named) type (and pointer
to it) is defined only in the package that defines the type. It
is not dupOK, unlike other type descriptors. So it can be
referenced by index. Currently it is referenced by name for
cross-package references, because the index is not exported and
so not known to the referencing package.
This CL passes the index through the export data, so the symbol
can be referenced by index, and does not need to be looked up by
name. This also makes such symbol references consistent: it is
referenced by index within the defining package and also cross-
package, which makes it easier for content hashing (in later CLs).
One complication is that we need to set flags on referenced
symbols (specifically, the UsedInIface flag). Before, they are
non-package refs, which naturally carry flags in the object file.
For indexed refs, we currently don't put their flags in the
object file. Introduce a new block for this.
Change-Id: I8126f8e318ac4e6609eb2ac136201fd6c264c256
Reviewed-on: https://go-review.googlesource.com/c/go/+/245718
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-07-27 14:46:16 -04:00
|
|
|
// Name string
|
|
|
|
|
// ABI uint16
|
|
|
|
|
// Type uint8
|
|
|
|
|
// Flag uint8
|
|
|
|
|
// Flag2 uint8
|
|
|
|
|
// Size uint32
|
2019-08-22 17:26:41 -04:00
|
|
|
// }
|
2020-07-13 15:05:09 -04:00
|
|
|
// Hashed64Defs [...]struct { // short hashed (content-addressable) symbol definitions
|
|
|
|
|
// ... // same as SymbolDefs
|
|
|
|
|
// }
|
2020-07-10 18:49:01 -04:00
|
|
|
// HashedDefs [...]struct { // hashed (content-addressable) symbol definitions
|
|
|
|
|
// ... // same as SymbolDefs
|
|
|
|
|
// }
|
2019-08-22 17:26:41 -04:00
|
|
|
// NonPkgDefs [...]struct { // non-pkg symbol definitions
|
|
|
|
|
// ... // same as SymbolDefs
|
|
|
|
|
// }
|
|
|
|
|
// NonPkgRefs [...]struct { // non-pkg symbol references
|
|
|
|
|
// ... // same as SymbolDefs
|
|
|
|
|
// }
|
|
|
|
|
//
|
[dev.link] cmd/compile, cmd/link: reference type symbol of defined type by index
The type descriptor symbol of a defined (named) type (and pointer
to it) is defined only in the package that defines the type. It
is not dupOK, unlike other type descriptors. So it can be
referenced by index. Currently it is referenced by name for
cross-package references, because the index is not exported and
so not known to the referencing package.
This CL passes the index through the export data, so the symbol
can be referenced by index, and does not need to be looked up by
name. This also makes such symbol references consistent: it is
referenced by index within the defining package and also cross-
package, which makes it easier for content hashing (in later CLs).
One complication is that we need to set flags on referenced
symbols (specifically, the UsedInIface flag). Before, they are
non-package refs, which naturally carry flags in the object file.
For indexed refs, we currently don't put their flags in the
object file. Introduce a new block for this.
Change-Id: I8126f8e318ac4e6609eb2ac136201fd6c264c256
Reviewed-on: https://go-review.googlesource.com/c/go/+/245718
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-07-27 14:46:16 -04:00
|
|
|
// RefFlags [...]struct { // referenced symbol flags
|
|
|
|
|
// Sym symRef
|
|
|
|
|
// Flag uint8
|
|
|
|
|
// Flag2 uint8
|
|
|
|
|
// }
|
|
|
|
|
//
|
2020-07-13 15:05:09 -04:00
|
|
|
// Hash64 [...][8]byte
|
|
|
|
|
// Hash [...][N]byte
|
2020-07-10 18:49:01 -04:00
|
|
|
//
|
2019-08-22 17:26:41 -04:00
|
|
|
// RelocIndex [...]uint32 // index to Relocs
|
|
|
|
|
// AuxIndex [...]uint32 // index to Aux
|
|
|
|
|
// DataIndex [...]uint32 // offset to Data
|
|
|
|
|
//
|
|
|
|
|
// Relocs [...]struct {
|
|
|
|
|
// Off int32
|
|
|
|
|
// Size uint8
|
|
|
|
|
// Type uint8
|
|
|
|
|
// Add int64
|
|
|
|
|
// Sym symRef
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// Aux [...]struct {
|
|
|
|
|
// Type uint8
|
|
|
|
|
// Sym symRef
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// Data [...]byte
|
|
|
|
|
// Pcdata [...]byte
|
|
|
|
|
//
|
2020-06-02 17:45:57 -04:00
|
|
|
// // blocks only used by tools (objdump, nm)
|
|
|
|
|
//
|
|
|
|
|
// RefNames [...]struct { // referenced symbol names
|
|
|
|
|
// Sym symRef
|
|
|
|
|
// Name string
|
|
|
|
|
// // TODO: include ABI version as well?
|
|
|
|
|
// }
|
|
|
|
|
//
|
2020-03-31 20:01:46 -04:00
|
|
|
// string is encoded as is a uint32 length followed by a uint32 offset
|
|
|
|
|
// that points to the corresponding string bytes.
|
2019-08-22 17:26:41 -04:00
|
|
|
//
|
|
|
|
|
// symRef is struct { PkgIdx, SymIdx uint32 }.
|
|
|
|
|
//
|
|
|
|
|
// Slice type (e.g. []symRef) is encoded as a length prefix (uint32)
|
|
|
|
|
// followed by that number of elements.
|
|
|
|
|
//
|
|
|
|
|
// The types below correspond to the encoded data structure in the
|
|
|
|
|
// object file.
|
|
|
|
|
|
|
|
|
|
// Symbol indexing.
|
|
|
|
|
//
|
|
|
|
|
// Each symbol is referenced with a pair of indices, { PkgIdx, SymIdx },
|
|
|
|
|
// as the symRef struct above.
|
|
|
|
|
//
|
|
|
|
|
// PkgIdx is either a predeclared index (see PkgIdxNone below) or
|
|
|
|
|
// an index of an imported package. For the latter case, PkgIdx is the
|
|
|
|
|
// index of the package in the PkgIndex array. 0 is an invalid index.
|
|
|
|
|
//
|
|
|
|
|
// SymIdx is the index of the symbol in the given package.
|
|
|
|
|
// - If PkgIdx is PkgIdxSelf, SymIdx is the index of the symbol in the
|
|
|
|
|
// SymbolDefs array.
|
2020-07-13 15:05:09 -04:00
|
|
|
// - If PkgIdx is PkgIdxHashed64, SymIdx is the index of the symbol in the
|
|
|
|
|
// Hashed64Defs array.
|
2020-07-10 18:49:01 -04:00
|
|
|
// - If PkgIdx is PkgIdxHashed, SymIdx is the index of the symbol in the
|
|
|
|
|
// HashedDefs array.
|
2019-08-22 17:26:41 -04:00
|
|
|
// - If PkgIdx is PkgIdxNone, SymIdx is the index of the symbol in the
|
|
|
|
|
// NonPkgDefs array (could natually overflow to NonPkgRefs array).
|
|
|
|
|
// - Otherwise, SymIdx is the index of the symbol in some other package's
|
|
|
|
|
// SymbolDefs array.
|
|
|
|
|
//
|
|
|
|
|
// {0, 0} represents a nil symbol. Otherwise PkgIdx should not be 0.
|
|
|
|
|
//
|
2020-07-10 18:49:01 -04:00
|
|
|
// Hash contains the content hashes of content-addressable symbols, of
|
|
|
|
|
// which PkgIdx is PkgIdxHashed, in the same order of HashedDefs array.
|
2020-07-13 15:05:09 -04:00
|
|
|
// Hash64 is similar, for PkgIdxHashed64 symbols.
|
2020-07-10 18:49:01 -04:00
|
|
|
//
|
2019-08-22 17:26:41 -04:00
|
|
|
// RelocIndex, AuxIndex, and DataIndex contains indices/offsets to
|
|
|
|
|
// Relocs/Aux/Data blocks, one element per symbol, first for all the
|
2020-07-10 18:49:01 -04:00
|
|
|
// defined symbols, then all the defined hashed and non-package symbols,
|
2020-07-13 15:05:09 -04:00
|
|
|
// in the same order of SymbolDefs/Hashed64Defs/HashedDefs/NonPkgDefs
|
|
|
|
|
// arrays. For N total defined symbols, the array is of length N+1. The
|
|
|
|
|
// last element is the total number of relocations (aux symbols, data
|
|
|
|
|
// blocks, etc.).
|
2019-08-22 17:26:41 -04:00
|
|
|
//
|
|
|
|
|
// They can be accessed by index. For the i-th symbol, its relocations
|
|
|
|
|
// are the RelocIndex[i]-th (inclusive) to RelocIndex[i+1]-th (exclusive)
|
|
|
|
|
// elements in the Relocs array. Aux/Data are likewise. (The index is
|
|
|
|
|
// 0-based.)
|
|
|
|
|
|
|
|
|
|
// Auxiliary symbols.
|
|
|
|
|
//
|
|
|
|
|
// Each symbol may (or may not) be associated with a number of auxiliary
|
|
|
|
|
// symbols. They are described in the Aux block. See Aux struct below.
|
2020-07-10 18:49:01 -04:00
|
|
|
// Currently a symbol's Gotype, FuncInfo, and associated DWARF symbols
|
|
|
|
|
// are auxiliary symbols.
|
2019-08-22 17:26:41 -04:00
|
|
|
|
2020-03-31 20:01:46 -04:00
|
|
|
const stringRefSize = 8 // two uint32s
|
|
|
|
|
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
type FingerprintType [8]byte
|
|
|
|
|
|
2020-04-22 22:20:44 -04:00
|
|
|
func (fp FingerprintType) IsZero() bool { return fp == FingerprintType{} }
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// Package Index.
|
|
|
|
|
const (
|
2020-07-13 15:05:09 -04:00
|
|
|
PkgIdxNone = (1<<31 - 1) - iota // Non-package symbols
|
|
|
|
|
PkgIdxHashed64 // Short hashed (content-addressable) symbols
|
|
|
|
|
PkgIdxHashed // Hashed (content-addressable) symbols
|
|
|
|
|
PkgIdxBuiltin // Predefined runtime symbols (ex: runtime.newobject)
|
|
|
|
|
PkgIdxSelf // Symbols defined in the current package
|
|
|
|
|
PkgIdxInvalid = 0
|
2019-08-22 17:26:41 -04:00
|
|
|
// The index of other referenced packages starts from 1.
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Blocks
|
|
|
|
|
const (
|
2019-10-09 10:22:02 -04:00
|
|
|
BlkAutolib = iota
|
|
|
|
|
BlkPkgIdx
|
2020-07-29 11:06:02 -04:00
|
|
|
BlkFile
|
2019-08-22 17:26:41 -04:00
|
|
|
BlkSymdef
|
2020-07-13 15:05:09 -04:00
|
|
|
BlkHashed64def
|
2020-07-10 18:49:01 -04:00
|
|
|
BlkHasheddef
|
2019-08-22 17:26:41 -04:00
|
|
|
BlkNonpkgdef
|
|
|
|
|
BlkNonpkgref
|
[dev.link] cmd/compile, cmd/link: reference type symbol of defined type by index
The type descriptor symbol of a defined (named) type (and pointer
to it) is defined only in the package that defines the type. It
is not dupOK, unlike other type descriptors. So it can be
referenced by index. Currently it is referenced by name for
cross-package references, because the index is not exported and
so not known to the referencing package.
This CL passes the index through the export data, so the symbol
can be referenced by index, and does not need to be looked up by
name. This also makes such symbol references consistent: it is
referenced by index within the defining package and also cross-
package, which makes it easier for content hashing (in later CLs).
One complication is that we need to set flags on referenced
symbols (specifically, the UsedInIface flag). Before, they are
non-package refs, which naturally carry flags in the object file.
For indexed refs, we currently don't put their flags in the
object file. Introduce a new block for this.
Change-Id: I8126f8e318ac4e6609eb2ac136201fd6c264c256
Reviewed-on: https://go-review.googlesource.com/c/go/+/245718
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-07-27 14:46:16 -04:00
|
|
|
BlkRefFlags
|
2020-07-13 15:05:09 -04:00
|
|
|
BlkHash64
|
2020-07-10 18:49:01 -04:00
|
|
|
BlkHash
|
2019-08-22 17:26:41 -04:00
|
|
|
BlkRelocIdx
|
|
|
|
|
BlkAuxIdx
|
|
|
|
|
BlkDataIdx
|
|
|
|
|
BlkReloc
|
|
|
|
|
BlkAux
|
|
|
|
|
BlkData
|
|
|
|
|
BlkPcdata
|
2020-06-02 17:45:57 -04:00
|
|
|
BlkRefName
|
|
|
|
|
BlkEnd
|
2019-08-22 17:26:41 -04:00
|
|
|
NBlk
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// File header.
|
|
|
|
|
// TODO: probably no need to export this.
|
|
|
|
|
type Header struct {
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
Magic string
|
|
|
|
|
Fingerprint FingerprintType
|
|
|
|
|
Flags uint32
|
|
|
|
|
Offsets [NBlk]uint32
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-21 15:53:30 -04:00
|
|
|
const Magic = "\x00go116ld"
|
2019-08-22 17:26:41 -04:00
|
|
|
|
|
|
|
|
func (h *Header) Write(w *Writer) {
|
|
|
|
|
w.RawString(h.Magic)
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
w.Bytes(h.Fingerprint[:])
|
2019-10-16 08:54:58 -04:00
|
|
|
w.Uint32(h.Flags)
|
2019-08-22 17:26:41 -04:00
|
|
|
for _, x := range h.Offsets {
|
|
|
|
|
w.Uint32(x)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Header) Read(r *Reader) error {
|
|
|
|
|
b := r.BytesAt(0, len(Magic))
|
|
|
|
|
h.Magic = string(b)
|
|
|
|
|
if h.Magic != Magic {
|
|
|
|
|
return errors.New("wrong magic, not a Go object file")
|
|
|
|
|
}
|
|
|
|
|
off := uint32(len(h.Magic))
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
copy(h.Fingerprint[:], r.BytesAt(off, len(h.Fingerprint)))
|
|
|
|
|
off += 8
|
2019-10-16 08:54:58 -04:00
|
|
|
h.Flags = r.uint32At(off)
|
|
|
|
|
off += 4
|
2019-08-22 17:26:41 -04:00
|
|
|
for i := range h.Offsets {
|
|
|
|
|
h.Offsets[i] = r.uint32At(off)
|
|
|
|
|
off += 4
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Header) Size() int {
|
2019-10-16 08:54:58 -04:00
|
|
|
return len(h.Magic) + 4 + 4*len(h.Offsets)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
// Autolib
|
|
|
|
|
type ImportedPkg struct {
|
|
|
|
|
Pkg string
|
|
|
|
|
Fingerprint FingerprintType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const importedPkgSize = stringRefSize + 8
|
|
|
|
|
|
|
|
|
|
func (p *ImportedPkg) Write(w *Writer) {
|
|
|
|
|
w.StringRef(p.Pkg)
|
|
|
|
|
w.Bytes(p.Fingerprint[:])
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// Symbol definition.
|
2020-04-08 14:30:55 -04:00
|
|
|
//
|
|
|
|
|
// Serialized format:
|
|
|
|
|
// Sym struct {
|
|
|
|
|
// Name string
|
|
|
|
|
// ABI uint16
|
|
|
|
|
// Type uint8
|
|
|
|
|
// Flag uint8
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
// Flag2 uint8
|
2020-04-08 14:30:55 -04:00
|
|
|
// Siz uint32
|
|
|
|
|
// Align uint32
|
|
|
|
|
// }
|
2020-04-09 20:45:14 -04:00
|
|
|
type Sym [SymSize]byte
|
2020-04-08 14:30:55 -04:00
|
|
|
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
const SymSize = stringRefSize + 2 + 1 + 1 + 1 + 4 + 4
|
2019-08-22 17:26:41 -04:00
|
|
|
|
|
|
|
|
const SymABIstatic = ^uint16(0)
|
|
|
|
|
|
2019-10-16 08:54:58 -04:00
|
|
|
const (
|
2020-05-18 18:20:18 -04:00
|
|
|
ObjFlagShared = 1 << iota // this object is built with -shared
|
|
|
|
|
ObjFlagNeedNameExpansion // the linker needs to expand `"".` to package path in symbol names
|
2020-06-30 10:22:13 -04:00
|
|
|
ObjFlagFromAssembly // object is from asm src, not go
|
2019-10-16 08:54:58 -04:00
|
|
|
)
|
|
|
|
|
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
// Sym.Flag
|
2019-08-22 17:26:41 -04:00
|
|
|
const (
|
|
|
|
|
SymFlagDupok = 1 << iota
|
|
|
|
|
SymFlagLocal
|
|
|
|
|
SymFlagTypelink
|
2019-10-08 18:21:22 -04:00
|
|
|
SymFlagLeaf
|
2020-02-17 01:27:11 -05:00
|
|
|
SymFlagNoSplit
|
2019-10-08 18:21:22 -04:00
|
|
|
SymFlagReflectMethod
|
2019-10-16 09:13:59 -04:00
|
|
|
SymFlagGoType
|
2019-10-08 18:21:22 -04:00
|
|
|
SymFlagTopFrame
|
2019-08-22 17:26:41 -04:00
|
|
|
)
|
|
|
|
|
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
// Sym.Flag2
|
|
|
|
|
const (
|
|
|
|
|
SymFlagUsedInIface = 1 << iota
|
2020-07-30 17:19:13 -04:00
|
|
|
SymFlagItab
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
)
|
|
|
|
|
|
2020-07-16 16:18:49 -04:00
|
|
|
// Returns the length of the name of the symbol.
|
|
|
|
|
func (s *Sym) NameLen(r *Reader) int {
|
|
|
|
|
return int(binary.LittleEndian.Uint32(s[:]))
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (s *Sym) Name(r *Reader) string {
|
2020-03-31 20:56:10 -04:00
|
|
|
len := binary.LittleEndian.Uint32(s[:])
|
|
|
|
|
off := binary.LittleEndian.Uint32(s[4:])
|
|
|
|
|
return r.StringAt(off, len)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (s *Sym) ABI() uint16 { return binary.LittleEndian.Uint16(s[8:]) }
|
|
|
|
|
func (s *Sym) Type() uint8 { return s[10] }
|
|
|
|
|
func (s *Sym) Flag() uint8 { return s[11] }
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
func (s *Sym) Flag2() uint8 { return s[12] }
|
|
|
|
|
func (s *Sym) Siz() uint32 { return binary.LittleEndian.Uint32(s[13:]) }
|
|
|
|
|
func (s *Sym) Align() uint32 { return binary.LittleEndian.Uint32(s[17:]) }
|
2020-03-31 20:56:10 -04:00
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (s *Sym) Dupok() bool { return s.Flag()&SymFlagDupok != 0 }
|
|
|
|
|
func (s *Sym) Local() bool { return s.Flag()&SymFlagLocal != 0 }
|
|
|
|
|
func (s *Sym) Typelink() bool { return s.Flag()&SymFlagTypelink != 0 }
|
|
|
|
|
func (s *Sym) Leaf() bool { return s.Flag()&SymFlagLeaf != 0 }
|
|
|
|
|
func (s *Sym) NoSplit() bool { return s.Flag()&SymFlagNoSplit != 0 }
|
|
|
|
|
func (s *Sym) ReflectMethod() bool { return s.Flag()&SymFlagReflectMethod != 0 }
|
|
|
|
|
func (s *Sym) IsGoType() bool { return s.Flag()&SymFlagGoType != 0 }
|
|
|
|
|
func (s *Sym) TopFrame() bool { return s.Flag()&SymFlagTopFrame != 0 }
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
func (s *Sym) UsedInIface() bool { return s.Flag2()&SymFlagUsedInIface != 0 }
|
2020-07-30 17:19:13 -04:00
|
|
|
func (s *Sym) IsItab() bool { return s.Flag2()&SymFlagItab != 0 }
|
2020-03-31 20:56:10 -04:00
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (s *Sym) SetName(x string, w *Writer) {
|
2020-04-08 13:24:27 -04:00
|
|
|
binary.LittleEndian.PutUint32(s[:], uint32(len(x)))
|
|
|
|
|
binary.LittleEndian.PutUint32(s[4:], w.stringOff(x))
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (s *Sym) SetABI(x uint16) { binary.LittleEndian.PutUint16(s[8:], x) }
|
|
|
|
|
func (s *Sym) SetType(x uint8) { s[10] = x }
|
|
|
|
|
func (s *Sym) SetFlag(x uint8) { s[11] = x }
|
[dev.link] cmd/compile, cmd/link: remove dead methods if type is not used in interface
Currently, a method of a reachable type is live if it matches a
method of a reachable interface. In fact, we only need to retain
the method if the type is actually converted to an interface. If
the type is never converted to an interface, there is no way to
call the method through an interface method call (but the type
descriptor could still be used, e.g. in calling
runtime.newobject).
A type can be used in an interface in two ways:
- directly converted to interface. (Any interface counts, as it
is possible to convert one interface to another.)
- obtained by reflection from a related type (e.g. obtaining an
interface of T from []T).
For the former, we let the compiler emit a marker on the type
descriptor symbol when it is converted to an interface. In the
linker, we only need to check methods of marked types.
For the latter, when the linker visits a marked type, it needs to
visit all its "child" types as marked (i.e. potentially could be
converted to interface).
This reduces binary size:
cmd/compile 18792016 18706096 (-0.5%)
cmd/go 14120572 13398948 (-5.1%)
Change-Id: I4465c7eeabf575f4dc84017214c610fa05ae31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/237298
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-06-08 18:38:59 -04:00
|
|
|
func (s *Sym) SetFlag2(x uint8) { s[12] = x }
|
|
|
|
|
func (s *Sym) SetSiz(x uint32) { binary.LittleEndian.PutUint32(s[13:], x) }
|
|
|
|
|
func (s *Sym) SetAlign(x uint32) { binary.LittleEndian.PutUint32(s[17:], x) }
|
2020-04-08 13:24:27 -04:00
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (s *Sym) Write(w *Writer) { w.Bytes(s[:]) }
|
2020-04-08 13:24:27 -04:00
|
|
|
|
2020-04-08 14:30:55 -04:00
|
|
|
// for testing
|
2020-04-09 20:45:14 -04:00
|
|
|
func (s *Sym) fromBytes(b []byte) { copy(s[:], b) }
|
2020-04-08 14:30:55 -04:00
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// Symbol reference.
|
|
|
|
|
type SymRef struct {
|
|
|
|
|
PkgIdx uint32
|
|
|
|
|
SymIdx uint32
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:05:09 -04:00
|
|
|
// Hash64
|
|
|
|
|
type Hash64Type [Hash64Size]byte
|
|
|
|
|
|
|
|
|
|
const Hash64Size = 8
|
|
|
|
|
|
2020-07-10 18:49:01 -04:00
|
|
|
// Hash
|
|
|
|
|
type HashType [HashSize]byte
|
|
|
|
|
|
|
|
|
|
const HashSize = sha1.Size
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// Relocation.
|
2020-04-08 14:30:55 -04:00
|
|
|
//
|
|
|
|
|
// Serialized format:
|
|
|
|
|
// Reloc struct {
|
|
|
|
|
// Off int32
|
|
|
|
|
// Siz uint8
|
|
|
|
|
// Type uint8
|
|
|
|
|
// Add int64
|
|
|
|
|
// Sym SymRef
|
|
|
|
|
// }
|
2020-04-09 20:45:14 -04:00
|
|
|
type Reloc [RelocSize]byte
|
2019-08-22 17:26:41 -04:00
|
|
|
|
2020-04-01 11:17:11 -04:00
|
|
|
const RelocSize = 4 + 1 + 1 + 8 + 8
|
2020-03-05 11:29:24 -05:00
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (r *Reloc) Off() int32 { return int32(binary.LittleEndian.Uint32(r[:])) }
|
|
|
|
|
func (r *Reloc) Siz() uint8 { return r[4] }
|
|
|
|
|
func (r *Reloc) Type() uint8 { return r[5] }
|
|
|
|
|
func (r *Reloc) Add() int64 { return int64(binary.LittleEndian.Uint64(r[6:])) }
|
|
|
|
|
func (r *Reloc) Sym() SymRef {
|
2020-03-05 11:29:24 -05:00
|
|
|
return SymRef{binary.LittleEndian.Uint32(r[14:]), binary.LittleEndian.Uint32(r[18:])}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (r *Reloc) SetOff(x int32) { binary.LittleEndian.PutUint32(r[:], uint32(x)) }
|
|
|
|
|
func (r *Reloc) SetSiz(x uint8) { r[4] = x }
|
|
|
|
|
func (r *Reloc) SetType(x uint8) { r[5] = x }
|
|
|
|
|
func (r *Reloc) SetAdd(x int64) { binary.LittleEndian.PutUint64(r[6:], uint64(x)) }
|
|
|
|
|
func (r *Reloc) SetSym(x SymRef) {
|
2020-03-28 16:46:47 -04:00
|
|
|
binary.LittleEndian.PutUint32(r[14:], x.PkgIdx)
|
|
|
|
|
binary.LittleEndian.PutUint32(r[18:], x.SymIdx)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (r *Reloc) Set(off int32, size uint8, typ uint8, add int64, sym SymRef) {
|
2020-03-28 16:46:47 -04:00
|
|
|
r.SetOff(off)
|
|
|
|
|
r.SetSiz(size)
|
|
|
|
|
r.SetType(typ)
|
|
|
|
|
r.SetAdd(add)
|
|
|
|
|
r.SetSym(sym)
|
2020-03-05 16:43:37 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (r *Reloc) Write(w *Writer) { w.Bytes(r[:]) }
|
2020-04-08 13:24:27 -04:00
|
|
|
|
2020-04-08 14:30:55 -04:00
|
|
|
// for testing
|
2020-04-09 20:45:14 -04:00
|
|
|
func (r *Reloc) fromBytes(b []byte) { copy(r[:], b) }
|
2020-04-08 14:30:55 -04:00
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// Aux symbol info.
|
2020-04-08 14:30:55 -04:00
|
|
|
//
|
|
|
|
|
// Serialized format:
|
|
|
|
|
// Aux struct {
|
|
|
|
|
// Type uint8
|
|
|
|
|
// Sym SymRef
|
|
|
|
|
// }
|
2020-04-09 20:45:14 -04:00
|
|
|
type Aux [AuxSize]byte
|
2020-04-08 14:30:55 -04:00
|
|
|
|
|
|
|
|
const AuxSize = 1 + 8
|
2019-08-22 17:26:41 -04:00
|
|
|
|
|
|
|
|
// Aux Type
|
|
|
|
|
const (
|
|
|
|
|
AuxGotype = iota
|
|
|
|
|
AuxFuncInfo
|
|
|
|
|
AuxFuncdata
|
2019-10-07 21:10:41 -04:00
|
|
|
AuxDwarfInfo
|
|
|
|
|
AuxDwarfLoc
|
|
|
|
|
AuxDwarfRanges
|
|
|
|
|
AuxDwarfLines
|
2019-08-22 17:26:41 -04:00
|
|
|
|
2019-10-07 21:10:41 -04:00
|
|
|
// TODO: more. Pcdata?
|
2019-08-22 17:26:41 -04:00
|
|
|
)
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (a *Aux) Type() uint8 { return a[0] }
|
|
|
|
|
func (a *Aux) Sym() SymRef {
|
2020-03-06 01:15:07 -05:00
|
|
|
return SymRef{binary.LittleEndian.Uint32(a[1:]), binary.LittleEndian.Uint32(a[5:])}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (a *Aux) SetType(x uint8) { a[0] = x }
|
|
|
|
|
func (a *Aux) SetSym(x SymRef) {
|
2020-04-08 13:24:27 -04:00
|
|
|
binary.LittleEndian.PutUint32(a[1:], x.PkgIdx)
|
|
|
|
|
binary.LittleEndian.PutUint32(a[5:], x.SymIdx)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
func (a *Aux) Write(w *Writer) { w.Bytes(a[:]) }
|
2020-04-08 13:24:27 -04:00
|
|
|
|
2020-04-08 14:30:55 -04:00
|
|
|
// for testing
|
2020-04-09 20:45:14 -04:00
|
|
|
func (a *Aux) fromBytes(b []byte) { copy(a[:], b) }
|
2020-04-08 14:30:55 -04:00
|
|
|
|
[dev.link] cmd/compile, cmd/link: reference type symbol of defined type by index
The type descriptor symbol of a defined (named) type (and pointer
to it) is defined only in the package that defines the type. It
is not dupOK, unlike other type descriptors. So it can be
referenced by index. Currently it is referenced by name for
cross-package references, because the index is not exported and
so not known to the referencing package.
This CL passes the index through the export data, so the symbol
can be referenced by index, and does not need to be looked up by
name. This also makes such symbol references consistent: it is
referenced by index within the defining package and also cross-
package, which makes it easier for content hashing (in later CLs).
One complication is that we need to set flags on referenced
symbols (specifically, the UsedInIface flag). Before, they are
non-package refs, which naturally carry flags in the object file.
For indexed refs, we currently don't put their flags in the
object file. Introduce a new block for this.
Change-Id: I8126f8e318ac4e6609eb2ac136201fd6c264c256
Reviewed-on: https://go-review.googlesource.com/c/go/+/245718
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-07-27 14:46:16 -04:00
|
|
|
// Referenced symbol flags.
|
|
|
|
|
//
|
|
|
|
|
// Serialized format:
|
|
|
|
|
// RefFlags struct {
|
|
|
|
|
// Sym symRef
|
|
|
|
|
// Flag uint8
|
|
|
|
|
// Flag2 uint8
|
|
|
|
|
// }
|
|
|
|
|
type RefFlags [RefFlagsSize]byte
|
|
|
|
|
|
|
|
|
|
const RefFlagsSize = 8 + 1 + 1
|
|
|
|
|
|
|
|
|
|
func (r *RefFlags) Sym() SymRef {
|
|
|
|
|
return SymRef{binary.LittleEndian.Uint32(r[:]), binary.LittleEndian.Uint32(r[4:])}
|
|
|
|
|
}
|
|
|
|
|
func (r *RefFlags) Flag() uint8 { return r[8] }
|
|
|
|
|
func (r *RefFlags) Flag2() uint8 { return r[9] }
|
|
|
|
|
|
|
|
|
|
func (r *RefFlags) SetSym(x SymRef) {
|
|
|
|
|
binary.LittleEndian.PutUint32(r[:], x.PkgIdx)
|
|
|
|
|
binary.LittleEndian.PutUint32(r[4:], x.SymIdx)
|
|
|
|
|
}
|
|
|
|
|
func (r *RefFlags) SetFlag(x uint8) { r[8] = x }
|
|
|
|
|
func (r *RefFlags) SetFlag2(x uint8) { r[9] = x }
|
|
|
|
|
|
|
|
|
|
func (r *RefFlags) Write(w *Writer) { w.Bytes(r[:]) }
|
|
|
|
|
|
2020-06-02 17:45:57 -04:00
|
|
|
// Referenced symbol name.
|
|
|
|
|
//
|
|
|
|
|
// Serialized format:
|
|
|
|
|
// RefName struct {
|
|
|
|
|
// Sym symRef
|
|
|
|
|
// Name string
|
|
|
|
|
// }
|
|
|
|
|
type RefName [RefNameSize]byte
|
|
|
|
|
|
|
|
|
|
const RefNameSize = 8 + stringRefSize
|
|
|
|
|
|
|
|
|
|
func (n *RefName) Sym() SymRef {
|
|
|
|
|
return SymRef{binary.LittleEndian.Uint32(n[:]), binary.LittleEndian.Uint32(n[4:])}
|
|
|
|
|
}
|
|
|
|
|
func (n *RefName) Name(r *Reader) string {
|
|
|
|
|
len := binary.LittleEndian.Uint32(n[8:])
|
|
|
|
|
off := binary.LittleEndian.Uint32(n[12:])
|
|
|
|
|
return r.StringAt(off, len)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *RefName) SetSym(x SymRef) {
|
|
|
|
|
binary.LittleEndian.PutUint32(n[:], x.PkgIdx)
|
|
|
|
|
binary.LittleEndian.PutUint32(n[4:], x.SymIdx)
|
|
|
|
|
}
|
|
|
|
|
func (n *RefName) SetName(x string, w *Writer) {
|
|
|
|
|
binary.LittleEndian.PutUint32(n[8:], uint32(len(x)))
|
|
|
|
|
binary.LittleEndian.PutUint32(n[12:], w.stringOff(x))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *RefName) Write(w *Writer) { w.Bytes(n[:]) }
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
type Writer struct {
|
|
|
|
|
wr *bio.Writer
|
|
|
|
|
stringMap map[string]uint32
|
|
|
|
|
off uint32 // running offset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewWriter(wr *bio.Writer) *Writer {
|
|
|
|
|
return &Writer{wr: wr, stringMap: make(map[string]uint32)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) AddString(s string) {
|
|
|
|
|
if _, ok := w.stringMap[s]; ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.stringMap[s] = w.off
|
|
|
|
|
w.RawString(s)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 13:24:27 -04:00
|
|
|
func (w *Writer) stringOff(s string) uint32 {
|
2019-08-22 17:26:41 -04:00
|
|
|
off, ok := w.stringMap[s]
|
|
|
|
|
if !ok {
|
|
|
|
|
panic(fmt.Sprintf("writeStringRef: string not added: %q", s))
|
|
|
|
|
}
|
2020-04-08 13:24:27 -04:00
|
|
|
return off
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) StringRef(s string) {
|
2020-03-31 20:01:46 -04:00
|
|
|
w.Uint32(uint32(len(s)))
|
2020-04-08 13:24:27 -04:00
|
|
|
w.Uint32(w.stringOff(s))
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) RawString(s string) {
|
|
|
|
|
w.wr.WriteString(s)
|
|
|
|
|
w.off += uint32(len(s))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) Bytes(s []byte) {
|
|
|
|
|
w.wr.Write(s)
|
|
|
|
|
w.off += uint32(len(s))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) Uint64(x uint64) {
|
|
|
|
|
var b [8]byte
|
|
|
|
|
binary.LittleEndian.PutUint64(b[:], x)
|
|
|
|
|
w.wr.Write(b[:])
|
|
|
|
|
w.off += 8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) Uint32(x uint32) {
|
|
|
|
|
var b [4]byte
|
|
|
|
|
binary.LittleEndian.PutUint32(b[:], x)
|
|
|
|
|
w.wr.Write(b[:])
|
|
|
|
|
w.off += 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) Uint16(x uint16) {
|
|
|
|
|
var b [2]byte
|
|
|
|
|
binary.LittleEndian.PutUint16(b[:], x)
|
|
|
|
|
w.wr.Write(b[:])
|
|
|
|
|
w.off += 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) Uint8(x uint8) {
|
|
|
|
|
w.wr.WriteByte(x)
|
|
|
|
|
w.off++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *Writer) Offset() uint32 {
|
|
|
|
|
return w.off
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Reader struct {
|
2019-09-27 14:49:44 -04:00
|
|
|
b []byte // mmapped bytes, if not nil
|
|
|
|
|
readonly bool // whether b is backed with read-only memory
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
rd io.ReaderAt
|
|
|
|
|
start uint32
|
|
|
|
|
h Header // keep block offsets
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 14:49:44 -04:00
|
|
|
func NewReaderFromBytes(b []byte, readonly bool) *Reader {
|
|
|
|
|
r := &Reader{b: b, readonly: readonly, rd: bytes.NewReader(b), start: 0}
|
|
|
|
|
err := r.h.Read(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
func (r *Reader) BytesAt(off uint32, len int) []byte {
|
2019-09-27 14:49:44 -04:00
|
|
|
if len == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-10-16 10:39:58 -04:00
|
|
|
end := int(off) + len
|
|
|
|
|
return r.b[int(off):end:end]
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) uint64At(off uint32) uint64 {
|
2019-09-27 14:49:44 -04:00
|
|
|
b := r.BytesAt(off, 8)
|
|
|
|
|
return binary.LittleEndian.Uint64(b)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) int64At(off uint32) int64 {
|
|
|
|
|
return int64(r.uint64At(off))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) uint32At(off uint32) uint32 {
|
2019-09-27 14:49:44 -04:00
|
|
|
b := r.BytesAt(off, 4)
|
|
|
|
|
return binary.LittleEndian.Uint32(b)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) int32At(off uint32) int32 {
|
|
|
|
|
return int32(r.uint32At(off))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) uint16At(off uint32) uint16 {
|
2019-09-27 14:49:44 -04:00
|
|
|
b := r.BytesAt(off, 2)
|
|
|
|
|
return binary.LittleEndian.Uint16(b)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) uint8At(off uint32) uint8 {
|
2019-09-27 14:49:44 -04:00
|
|
|
b := r.BytesAt(off, 1)
|
2019-08-22 17:26:41 -04:00
|
|
|
return b[0]
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 20:01:46 -04:00
|
|
|
func (r *Reader) StringAt(off uint32, len uint32) string {
|
|
|
|
|
b := r.b[off : off+len]
|
2019-10-16 10:39:58 -04:00
|
|
|
if r.readonly {
|
|
|
|
|
return toString(b) // backed by RO memory, ok to make unsafe string
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
return string(b)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 14:49:44 -04:00
|
|
|
func toString(b []byte) string {
|
|
|
|
|
if len(b) == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2020-04-30 17:05:59 -04:00
|
|
|
|
|
|
|
|
var s string
|
|
|
|
|
hdr := (*unsafeheader.String)(unsafe.Pointer(&s))
|
|
|
|
|
hdr.Data = unsafe.Pointer(&b[0])
|
|
|
|
|
hdr.Len = len(b)
|
|
|
|
|
|
2019-09-27 14:49:44 -04:00
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
func (r *Reader) StringRef(off uint32) string {
|
2020-03-31 20:01:46 -04:00
|
|
|
l := r.uint32At(off)
|
|
|
|
|
return r.StringAt(r.uint32At(off+4), l)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
func (r *Reader) Fingerprint() FingerprintType {
|
|
|
|
|
return r.h.Fingerprint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) Autolib() []ImportedPkg {
|
|
|
|
|
n := (r.h.Offsets[BlkAutolib+1] - r.h.Offsets[BlkAutolib]) / importedPkgSize
|
|
|
|
|
s := make([]ImportedPkg, n)
|
|
|
|
|
off := r.h.Offsets[BlkAutolib]
|
2019-10-09 10:22:02 -04:00
|
|
|
for i := range s {
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
s[i].Pkg = r.StringRef(off)
|
|
|
|
|
copy(s[i].Fingerprint[:], r.BytesAt(off+stringRefSize, len(s[i].Fingerprint)))
|
|
|
|
|
off += importedPkgSize
|
2019-10-09 10:22:02 -04:00
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
func (r *Reader) Pkglist() []string {
|
2020-03-31 20:01:46 -04:00
|
|
|
n := (r.h.Offsets[BlkPkgIdx+1] - r.h.Offsets[BlkPkgIdx]) / stringRefSize
|
2019-08-22 17:26:41 -04:00
|
|
|
s := make([]string, n)
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
off := r.h.Offsets[BlkPkgIdx]
|
2019-08-22 17:26:41 -04:00
|
|
|
for i := range s {
|
|
|
|
|
s[i] = r.StringRef(off)
|
[dev.link] cmd/internal/goobj2: add index fingerprint to object file
The new object files use indices for symbol references, instead
of names. Fundamental to the design, it requires that the
importing and imported packages have consistent view of symbol
indices. The Go command should already ensure this, when using
"go build". But in case it goes wrong, it could lead to obscure
errors like run-time crashes. It would be better to check the
index consistency at build time.
To do that, we add a fingerprint to each object file, which is
a hash of symbol indices. In the object file it records the
fingerprints of all imported packages, as well as its own
fingerprint. At link time, the linker checks that a package's
fingerprint matches the fingerprint recorded in the importing
packages, and issue an error if they don't match.
This CL does the first part: introducing the fingerprint in the
object file, and propagating fingerprints through
importing/exporting by the compiler. It is not yet used by the
linker. Next CL will do.
Change-Id: I0aa372da652e4afb11f2867cb71689a3e3f9966e
Reviewed-on: https://go-review.googlesource.com/c/go/+/229617
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-04-22 19:21:30 -04:00
|
|
|
off += stringRefSize
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-30 11:43:41 -04:00
|
|
|
func (r *Reader) NPkg() int {
|
2020-03-31 20:01:46 -04:00
|
|
|
return int(r.h.Offsets[BlkPkgIdx+1]-r.h.Offsets[BlkPkgIdx]) / stringRefSize
|
2019-09-30 11:43:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) Pkg(i int) string {
|
2020-03-31 20:01:46 -04:00
|
|
|
off := r.h.Offsets[BlkPkgIdx] + uint32(i)*stringRefSize
|
2019-09-30 11:43:41 -04:00
|
|
|
return r.StringRef(off)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 11:06:02 -04:00
|
|
|
func (r *Reader) NFile() int {
|
|
|
|
|
return int(r.h.Offsets[BlkFile+1]-r.h.Offsets[BlkFile]) / stringRefSize
|
2019-09-30 11:43:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-29 11:06:02 -04:00
|
|
|
func (r *Reader) File(i int) string {
|
|
|
|
|
off := r.h.Offsets[BlkFile] + uint32(i)*stringRefSize
|
2019-09-30 11:43:41 -04:00
|
|
|
return r.StringRef(off)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
func (r *Reader) NSym() int {
|
2020-04-01 11:17:11 -04:00
|
|
|
return int(r.h.Offsets[BlkSymdef+1]-r.h.Offsets[BlkSymdef]) / SymSize
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:05:09 -04:00
|
|
|
func (r *Reader) NHashed64def() int {
|
|
|
|
|
return int(r.h.Offsets[BlkHashed64def+1]-r.h.Offsets[BlkHashed64def]) / SymSize
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 18:49:01 -04:00
|
|
|
func (r *Reader) NHasheddef() int {
|
|
|
|
|
return int(r.h.Offsets[BlkHasheddef+1]-r.h.Offsets[BlkHasheddef]) / SymSize
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
func (r *Reader) NNonpkgdef() int {
|
2020-04-01 11:17:11 -04:00
|
|
|
return int(r.h.Offsets[BlkNonpkgdef+1]-r.h.Offsets[BlkNonpkgdef]) / SymSize
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Reader) NNonpkgref() int {
|
2020-04-01 11:17:11 -04:00
|
|
|
return int(r.h.Offsets[BlkNonpkgref+1]-r.h.Offsets[BlkNonpkgref]) / SymSize
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SymOff returns the offset of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) SymOff(i uint32) uint32 {
|
2020-04-01 11:17:11 -04:00
|
|
|
return r.h.Offsets[BlkSymdef] + uint32(i*SymSize)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
// Sym returns a pointer to the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) Sym(i uint32) *Sym {
|
2020-03-31 20:56:10 -04:00
|
|
|
off := r.SymOff(i)
|
2020-04-09 20:45:14 -04:00
|
|
|
return (*Sym)(unsafe.Pointer(&r.b[off]))
|
2020-03-31 20:56:10 -04:00
|
|
|
}
|
|
|
|
|
|
[dev.link] cmd/compile, cmd/link: reference type symbol of defined type by index
The type descriptor symbol of a defined (named) type (and pointer
to it) is defined only in the package that defines the type. It
is not dupOK, unlike other type descriptors. So it can be
referenced by index. Currently it is referenced by name for
cross-package references, because the index is not exported and
so not known to the referencing package.
This CL passes the index through the export data, so the symbol
can be referenced by index, and does not need to be looked up by
name. This also makes such symbol references consistent: it is
referenced by index within the defining package and also cross-
package, which makes it easier for content hashing (in later CLs).
One complication is that we need to set flags on referenced
symbols (specifically, the UsedInIface flag). Before, they are
non-package refs, which naturally carry flags in the object file.
For indexed refs, we currently don't put their flags in the
object file. Introduce a new block for this.
Change-Id: I8126f8e318ac4e6609eb2ac136201fd6c264c256
Reviewed-on: https://go-review.googlesource.com/c/go/+/245718
Reviewed-by: Jeremy Faller <jeremy@golang.org>
2020-07-27 14:46:16 -04:00
|
|
|
// NRefFlags returns the number of referenced symbol flags.
|
|
|
|
|
func (r *Reader) NRefFlags() int {
|
|
|
|
|
return int(r.h.Offsets[BlkRefFlags+1]-r.h.Offsets[BlkRefFlags]) / RefFlagsSize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RefFlags returns a pointer to the i-th referenced symbol flags.
|
|
|
|
|
// Note: here i is not a local symbol index, just a counter.
|
|
|
|
|
func (r *Reader) RefFlags(i int) *RefFlags {
|
|
|
|
|
off := r.h.Offsets[BlkRefFlags] + uint32(i*RefFlagsSize)
|
|
|
|
|
return (*RefFlags)(unsafe.Pointer(&r.b[off]))
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:05:09 -04:00
|
|
|
// Hash64 returns the i-th short hashed symbol's hash.
|
|
|
|
|
// Note: here i is the index of short hashed symbols, not all symbols
|
|
|
|
|
// (unlike other accessors).
|
|
|
|
|
func (r *Reader) Hash64(i uint32) uint64 {
|
|
|
|
|
off := r.h.Offsets[BlkHash64] + uint32(i*Hash64Size)
|
|
|
|
|
return r.uint64At(off)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 18:49:01 -04:00
|
|
|
// Hash returns a pointer to the i-th hashed symbol's hash.
|
|
|
|
|
// Note: here i is the index of hashed symbols, not all symbols
|
|
|
|
|
// (unlike other accessors).
|
|
|
|
|
func (r *Reader) Hash(i uint32) *HashType {
|
|
|
|
|
off := r.h.Offsets[BlkHash] + uint32(i*HashSize)
|
|
|
|
|
return (*HashType)(unsafe.Pointer(&r.b[off]))
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// NReloc returns the number of relocations of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) NReloc(i uint32) int {
|
2019-08-22 17:26:41 -04:00
|
|
|
relocIdxOff := r.h.Offsets[BlkRelocIdx] + uint32(i*4)
|
|
|
|
|
return int(r.uint32At(relocIdxOff+4) - r.uint32At(relocIdxOff))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RelocOff returns the offset of the j-th relocation of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) RelocOff(i uint32, j int) uint32 {
|
2019-08-22 17:26:41 -04:00
|
|
|
relocIdxOff := r.h.Offsets[BlkRelocIdx] + uint32(i*4)
|
|
|
|
|
relocIdx := r.uint32At(relocIdxOff)
|
2020-04-01 11:17:11 -04:00
|
|
|
return r.h.Offsets[BlkReloc] + (relocIdx+uint32(j))*uint32(RelocSize)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
// Reloc returns a pointer to the j-th relocation of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) Reloc(i uint32, j int) *Reloc {
|
2020-03-05 11:29:24 -05:00
|
|
|
off := r.RelocOff(i, j)
|
2020-04-09 20:45:14 -04:00
|
|
|
return (*Reloc)(unsafe.Pointer(&r.b[off]))
|
2020-03-05 11:29:24 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
// Relocs returns a pointer to the relocations of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) Relocs(i uint32) []Reloc {
|
2020-03-11 17:00:08 -04:00
|
|
|
off := r.RelocOff(i, 0)
|
|
|
|
|
n := r.NReloc(i)
|
2020-04-09 20:45:14 -04:00
|
|
|
return (*[1 << 20]Reloc)(unsafe.Pointer(&r.b[off]))[:n:n]
|
2020-03-11 17:00:08 -04:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// NAux returns the number of aux symbols of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) NAux(i uint32) int {
|
|
|
|
|
auxIdxOff := r.h.Offsets[BlkAuxIdx] + i*4
|
2019-08-22 17:26:41 -04:00
|
|
|
return int(r.uint32At(auxIdxOff+4) - r.uint32At(auxIdxOff))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AuxOff returns the offset of the j-th aux symbol of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) AuxOff(i uint32, j int) uint32 {
|
|
|
|
|
auxIdxOff := r.h.Offsets[BlkAuxIdx] + i*4
|
2019-08-22 17:26:41 -04:00
|
|
|
auxIdx := r.uint32At(auxIdxOff)
|
2020-04-01 11:17:11 -04:00
|
|
|
return r.h.Offsets[BlkAux] + (auxIdx+uint32(j))*uint32(AuxSize)
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
// Aux returns a pointer to the j-th aux symbol of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) Aux(i uint32, j int) *Aux {
|
2020-03-06 01:15:07 -05:00
|
|
|
off := r.AuxOff(i, j)
|
2020-04-09 20:45:14 -04:00
|
|
|
return (*Aux)(unsafe.Pointer(&r.b[off]))
|
2020-03-06 01:15:07 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:45:14 -04:00
|
|
|
// Auxs returns the aux symbols of the i-th symbol.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) Auxs(i uint32) []Aux {
|
2020-03-06 01:15:07 -05:00
|
|
|
off := r.AuxOff(i, 0)
|
|
|
|
|
n := r.NAux(i)
|
2020-04-09 20:45:14 -04:00
|
|
|
return (*[1 << 20]Aux)(unsafe.Pointer(&r.b[off]))[:n:n]
|
2020-03-06 01:15:07 -05:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// DataOff returns the offset of the i-th symbol's data.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) DataOff(i uint32) uint32 {
|
|
|
|
|
dataIdxOff := r.h.Offsets[BlkDataIdx] + i*4
|
2019-08-22 17:26:41 -04:00
|
|
|
return r.h.Offsets[BlkData] + r.uint32At(dataIdxOff)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DataSize returns the size of the i-th symbol's data.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) DataSize(i uint32) int {
|
|
|
|
|
dataIdxOff := r.h.Offsets[BlkDataIdx] + i*4
|
2020-03-06 12:36:58 -05:00
|
|
|
return int(r.uint32At(dataIdxOff+4) - r.uint32At(dataIdxOff))
|
2019-08-22 17:26:41 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 11:43:41 -04:00
|
|
|
// Data returns the i-th symbol's data.
|
2020-05-12 20:08:27 -04:00
|
|
|
func (r *Reader) Data(i uint32) []byte {
|
|
|
|
|
dataIdxOff := r.h.Offsets[BlkDataIdx] + i*4
|
2020-03-06 12:36:58 -05:00
|
|
|
base := r.h.Offsets[BlkData]
|
|
|
|
|
off := r.uint32At(dataIdxOff)
|
|
|
|
|
end := r.uint32At(dataIdxOff + 4)
|
|
|
|
|
return r.BytesAt(base+off, int(end-off))
|
2019-09-30 11:43:41 -04:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:26:41 -04:00
|
|
|
// AuxDataBase returns the base offset of the aux data block.
|
|
|
|
|
func (r *Reader) PcdataBase() uint32 {
|
|
|
|
|
return r.h.Offsets[BlkPcdata]
|
|
|
|
|
}
|
2019-09-27 14:49:44 -04:00
|
|
|
|
2020-06-02 17:45:57 -04:00
|
|
|
// NRefName returns the number of referenced symbol names.
|
|
|
|
|
func (r *Reader) NRefName() int {
|
|
|
|
|
return int(r.h.Offsets[BlkRefName+1]-r.h.Offsets[BlkRefName]) / RefNameSize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RefName returns a pointer to the i-th referenced symbol name.
|
|
|
|
|
// Note: here i is not a local symbol index, just a counter.
|
|
|
|
|
func (r *Reader) RefName(i int) *RefName {
|
|
|
|
|
off := r.h.Offsets[BlkRefName] + uint32(i*RefNameSize)
|
|
|
|
|
return (*RefName)(unsafe.Pointer(&r.b[off]))
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 14:49:44 -04:00
|
|
|
// ReadOnly returns whether r.BytesAt returns read-only bytes.
|
|
|
|
|
func (r *Reader) ReadOnly() bool {
|
|
|
|
|
return r.readonly
|
|
|
|
|
}
|
2019-10-16 08:54:58 -04:00
|
|
|
|
|
|
|
|
// Flags returns the flag bits read from the object file header.
|
|
|
|
|
func (r *Reader) Flags() uint32 {
|
|
|
|
|
return r.h.Flags
|
|
|
|
|
}
|
2020-05-18 18:20:18 -04:00
|
|
|
|
|
|
|
|
func (r *Reader) Shared() bool { return r.Flags()&ObjFlagShared != 0 }
|
|
|
|
|
func (r *Reader) NeedNameExpansion() bool { return r.Flags()&ObjFlagNeedNameExpansion != 0 }
|
2020-06-30 10:22:13 -04:00
|
|
|
func (r *Reader) FromAssembly() bool { return r.Flags()&ObjFlagFromAssembly != 0 }
|