mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.link] cmd/internal/goobj2: remove old-style types
They are no longer needed. Also rewrite the test, as the old one no longer meaningful. Change-Id: Id39ad6bb2a334cb6d61aa0a7c52837e0c3d62432 Reviewed-on: https://go-review.googlesource.com/c/go/+/227641 Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jeremy Faller <jeremy@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
parent
88d6d37b77
commit
e7c16412b7
2 changed files with 93 additions and 76 deletions
|
|
@ -185,14 +185,19 @@ func (h *Header) Size() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Symbol definition.
|
// Symbol definition.
|
||||||
type Sym struct {
|
//
|
||||||
Name string
|
// Serialized format:
|
||||||
ABI uint16
|
// Sym struct {
|
||||||
Type uint8
|
// Name string
|
||||||
Flag uint8
|
// ABI uint16
|
||||||
Siz uint32
|
// Type uint8
|
||||||
Align uint32
|
// Flag uint8
|
||||||
}
|
// Siz uint32
|
||||||
|
// Align uint32
|
||||||
|
// }
|
||||||
|
type Sym2 [SymSize]byte
|
||||||
|
|
||||||
|
const SymSize = stringRefSize + 2 + 1 + 1 + 4 + 4
|
||||||
|
|
||||||
const SymABIstatic = ^uint16(0)
|
const SymABIstatic = ^uint16(0)
|
||||||
|
|
||||||
|
|
@ -211,19 +216,6 @@ const (
|
||||||
SymFlagTopFrame
|
SymFlagTopFrame
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Sym) Write(w *Writer) {
|
|
||||||
w.StringRef(s.Name)
|
|
||||||
w.Uint16(s.ABI)
|
|
||||||
w.Uint8(s.Type)
|
|
||||||
w.Uint8(s.Flag)
|
|
||||||
w.Uint32(s.Siz)
|
|
||||||
w.Uint32(s.Align)
|
|
||||||
}
|
|
||||||
|
|
||||||
const SymSize = stringRefSize + 2 + 1 + 1 + 4 + 4
|
|
||||||
|
|
||||||
type Sym2 [SymSize]byte
|
|
||||||
|
|
||||||
func (s *Sym2) Name(r *Reader) string {
|
func (s *Sym2) Name(r *Reader) string {
|
||||||
len := binary.LittleEndian.Uint32(s[:])
|
len := binary.LittleEndian.Uint32(s[:])
|
||||||
off := binary.LittleEndian.Uint32(s[4:])
|
off := binary.LittleEndian.Uint32(s[4:])
|
||||||
|
|
@ -258,38 +250,29 @@ func (s *Sym2) SetAlign(x uint32) { binary.LittleEndian.PutUint32(s[16:], x) }
|
||||||
|
|
||||||
func (s *Sym2) Write(w *Writer) { w.Bytes(s[:]) }
|
func (s *Sym2) Write(w *Writer) { w.Bytes(s[:]) }
|
||||||
|
|
||||||
|
// for testing
|
||||||
|
func (s *Sym2) fromBytes(b []byte) { copy(s[:], b) }
|
||||||
|
|
||||||
// Symbol reference.
|
// Symbol reference.
|
||||||
type SymRef struct {
|
type SymRef struct {
|
||||||
PkgIdx uint32
|
PkgIdx uint32
|
||||||
SymIdx uint32
|
SymIdx uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SymRef) Write(w *Writer) {
|
|
||||||
w.Uint32(s.PkgIdx)
|
|
||||||
w.Uint32(s.SymIdx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Relocation.
|
// Relocation.
|
||||||
type Reloc struct {
|
//
|
||||||
Off int32
|
// Serialized format:
|
||||||
Siz uint8
|
// Reloc struct {
|
||||||
Type uint8
|
// Off int32
|
||||||
Add int64
|
// Siz uint8
|
||||||
Sym SymRef
|
// Type uint8
|
||||||
}
|
// Add int64
|
||||||
|
// Sym SymRef
|
||||||
func (r *Reloc) Write(w *Writer) {
|
// }
|
||||||
w.Uint32(uint32(r.Off))
|
type Reloc2 [RelocSize]byte
|
||||||
w.Uint8(r.Siz)
|
|
||||||
w.Uint8(r.Type)
|
|
||||||
w.Uint64(uint64(r.Add))
|
|
||||||
r.Sym.Write(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
const RelocSize = 4 + 1 + 1 + 8 + 8
|
const RelocSize = 4 + 1 + 1 + 8 + 8
|
||||||
|
|
||||||
type Reloc2 [RelocSize]byte
|
|
||||||
|
|
||||||
func (r *Reloc2) Off() int32 { return int32(binary.LittleEndian.Uint32(r[:])) }
|
func (r *Reloc2) Off() int32 { return int32(binary.LittleEndian.Uint32(r[:])) }
|
||||||
func (r *Reloc2) Siz() uint8 { return r[4] }
|
func (r *Reloc2) Siz() uint8 { return r[4] }
|
||||||
func (r *Reloc2) Type() uint8 { return r[5] }
|
func (r *Reloc2) Type() uint8 { return r[5] }
|
||||||
|
|
@ -317,11 +300,19 @@ func (r *Reloc2) Set(off int32, size uint8, typ uint8, add int64, sym SymRef) {
|
||||||
|
|
||||||
func (r *Reloc2) Write(w *Writer) { w.Bytes(r[:]) }
|
func (r *Reloc2) Write(w *Writer) { w.Bytes(r[:]) }
|
||||||
|
|
||||||
|
// for testing
|
||||||
|
func (r *Reloc2) fromBytes(b []byte) { copy(r[:], b) }
|
||||||
|
|
||||||
// Aux symbol info.
|
// Aux symbol info.
|
||||||
type Aux struct {
|
//
|
||||||
Type uint8
|
// Serialized format:
|
||||||
Sym SymRef
|
// Aux struct {
|
||||||
}
|
// Type uint8
|
||||||
|
// Sym SymRef
|
||||||
|
// }
|
||||||
|
type Aux2 [AuxSize]byte
|
||||||
|
|
||||||
|
const AuxSize = 1 + 8
|
||||||
|
|
||||||
// Aux Type
|
// Aux Type
|
||||||
const (
|
const (
|
||||||
|
|
@ -336,15 +327,6 @@ const (
|
||||||
// TODO: more. Pcdata?
|
// TODO: more. Pcdata?
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *Aux) Write(w *Writer) {
|
|
||||||
w.Uint8(a.Type)
|
|
||||||
a.Sym.Write(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
const AuxSize = 1 + 8
|
|
||||||
|
|
||||||
type Aux2 [AuxSize]byte
|
|
||||||
|
|
||||||
func (a *Aux2) Type() uint8 { return a[0] }
|
func (a *Aux2) Type() uint8 { return a[0] }
|
||||||
func (a *Aux2) Sym() SymRef {
|
func (a *Aux2) Sym() SymRef {
|
||||||
return SymRef{binary.LittleEndian.Uint32(a[1:]), binary.LittleEndian.Uint32(a[5:])}
|
return SymRef{binary.LittleEndian.Uint32(a[1:]), binary.LittleEndian.Uint32(a[5:])}
|
||||||
|
|
@ -358,6 +340,9 @@ func (a *Aux2) SetSym(x SymRef) {
|
||||||
|
|
||||||
func (a *Aux2) Write(w *Writer) { w.Bytes(a[:]) }
|
func (a *Aux2) Write(w *Writer) { w.Bytes(a[:]) }
|
||||||
|
|
||||||
|
// for testing
|
||||||
|
func (a *Aux2) fromBytes(b []byte) { copy(a[:], b) }
|
||||||
|
|
||||||
type Writer struct {
|
type Writer struct {
|
||||||
wr *bio.Writer
|
wr *bio.Writer
|
||||||
stringMap map[string]uint32
|
stringMap map[string]uint32
|
||||||
|
|
|
||||||
|
|
@ -8,32 +8,64 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"cmd/internal/bio"
|
"cmd/internal/bio"
|
||||||
|
"cmd/internal/objabi"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func dummyWriter() *Writer {
|
func dummyWriter(buf *bytes.Buffer) *Writer {
|
||||||
var buf bytes.Buffer
|
wr := &bio.Writer{Writer: bufio.NewWriter(buf)} // hacky: no file, so cannot seek
|
||||||
wr := &bio.Writer{Writer: bufio.NewWriter(&buf)} // hacky: no file, so cannot seek
|
|
||||||
return NewWriter(wr)
|
return NewWriter(wr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSize(t *testing.T) {
|
func TestReadWrite(t *testing.T) {
|
||||||
// This test checks that hard-coded sizes match the actual sizes
|
// Test that we get the same data in a write-read roundtrip.
|
||||||
// in the object file format.
|
|
||||||
tests := []struct {
|
// Write a symbol, a relocation, and an aux info.
|
||||||
x interface{ Write(*Writer) }
|
var buf bytes.Buffer
|
||||||
want uint32
|
w := dummyWriter(&buf)
|
||||||
}{
|
|
||||||
{&Reloc{}, RelocSize},
|
var s Sym2
|
||||||
{&Aux{}, AuxSize},
|
s.SetABI(1)
|
||||||
|
s.SetType(uint8(objabi.STEXT))
|
||||||
|
s.SetFlag(0x12)
|
||||||
|
s.SetSiz(12345)
|
||||||
|
s.SetAlign(8)
|
||||||
|
s.Write(w)
|
||||||
|
|
||||||
|
var r Reloc2
|
||||||
|
r.SetOff(12)
|
||||||
|
r.SetSiz(4)
|
||||||
|
r.SetType(uint8(objabi.R_ADDR))
|
||||||
|
r.SetAdd(54321)
|
||||||
|
r.SetSym(SymRef{11, 22})
|
||||||
|
r.Write(w)
|
||||||
|
|
||||||
|
var a Aux2
|
||||||
|
a.SetType(AuxFuncInfo)
|
||||||
|
a.SetSym(SymRef{33, 44})
|
||||||
|
a.Write(w)
|
||||||
|
|
||||||
|
w.wr.Flush()
|
||||||
|
|
||||||
|
// Read them back and check.
|
||||||
|
b := buf.Bytes()
|
||||||
|
var s2 Sym2
|
||||||
|
s2.fromBytes(b)
|
||||||
|
if s2.ABI() != 1 || s2.Type() != uint8(objabi.STEXT) || s2.Flag() != 0x12 || s2.Siz() != 12345 || s2.Align() != 8 {
|
||||||
|
t.Errorf("read Sym2 mismatch: got %v %v %v %v %v", s2.ABI(), s2.Type(), s2.Flag(), s2.Siz(), s2.Align())
|
||||||
}
|
}
|
||||||
w := dummyWriter()
|
|
||||||
for _, test := range tests {
|
b = b[SymSize:]
|
||||||
off0 := w.off
|
var r2 Reloc2
|
||||||
test.x.Write(w)
|
r2.fromBytes(b)
|
||||||
got := w.off - off0
|
if r2.Off() != 12 || r2.Siz() != 4 || r2.Type() != uint8(objabi.R_ADDR) || r2.Add() != 54321 || r2.Sym() != (SymRef{11, 22}) {
|
||||||
if got != test.want {
|
t.Errorf("read Reloc2 mismatch: got %v %v %v %v %v", r2.Off(), r2.Siz(), r2.Type(), r2.Add(), r2.Sym())
|
||||||
t.Errorf("size(%T) mismatch: %d bytes written, but size=%d", test.x, got, test.want)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b = b[RelocSize:]
|
||||||
|
var a2 Aux2
|
||||||
|
a2.fromBytes(b)
|
||||||
|
if a2.Type() != AuxFuncInfo || a2.Sym() != (SymRef{33, 44}) {
|
||||||
|
t.Errorf("read Aux2 mismatch: got %v %v", a2.Type(), a2.Sym())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue