2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
2015-02-27 22:57:28 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package ld
|
|
|
|
|
|
2016-03-14 22:57:58 +02:00
|
|
|
// Reading of Go object files.
|
2015-05-18 15:50:00 -04:00
|
|
|
//
|
|
|
|
|
// Originally, Go object files were Plan 9 object files, but no longer.
|
|
|
|
|
// Now they are more like standard object files, in that each symbol is defined
|
|
|
|
|
// by an associated memory image (bytes) and a list of relocations to apply
|
|
|
|
|
// during linking. We do not (yet?) use a standard file format, however.
|
|
|
|
|
// For now, the format is chosen to be as simple as possible to read and write.
|
|
|
|
|
// It may change for reasons of efficiency, or we may even switch to a
|
|
|
|
|
// standard file format if there are compelling benefits to doing so.
|
|
|
|
|
// See golang.org/s/go13linker for more background.
|
|
|
|
|
//
|
|
|
|
|
// The file format is:
|
|
|
|
|
//
|
|
|
|
|
// - magic header: "\x00\x00go13ld"
|
|
|
|
|
// - byte 1 - version number
|
|
|
|
|
// - sequence of strings giving dependencies (imported packages)
|
|
|
|
|
// - empty string (marks end of sequence)
|
2016-03-23 00:44:07 +02:00
|
|
|
// - sequence of symbol references used by the defined symbols
|
2016-03-14 22:57:58 +02:00
|
|
|
// - byte 0xff (marks end of sequence)
|
2016-03-23 00:44:07 +02:00
|
|
|
// - sequence of integer lengths:
|
|
|
|
|
// - total data length
|
|
|
|
|
// - total number of relocations
|
|
|
|
|
// - total number of pcdata
|
|
|
|
|
// - total number of automatics
|
|
|
|
|
// - total number of funcdata
|
|
|
|
|
// - total number of files
|
2016-03-21 10:55:20 +13:00
|
|
|
// - data, the content of the defined symbols
|
2015-05-18 15:50:00 -04:00
|
|
|
// - sequence of defined symbols
|
|
|
|
|
// - byte 0xff (marks end of sequence)
|
|
|
|
|
// - magic footer: "\xff\xffgo13ld"
|
|
|
|
|
//
|
|
|
|
|
// All integers are stored in a zigzag varint format.
|
|
|
|
|
// See golang.org/s/go12symtab for a definition.
|
|
|
|
|
//
|
|
|
|
|
// Data blocks and strings are both stored as an integer
|
|
|
|
|
// followed by that many bytes.
|
|
|
|
|
//
|
|
|
|
|
// A symbol reference is a string name followed by a version.
|
2016-03-14 22:57:58 +02:00
|
|
|
//
|
|
|
|
|
// A symbol points to other symbols using an index into the symbol
|
|
|
|
|
// reference sequence. Index 0 corresponds to a nil LSym* pointer.
|
|
|
|
|
// In the symbol layout described below "symref index" stands for this
|
|
|
|
|
// index.
|
2015-05-18 15:50:00 -04:00
|
|
|
//
|
|
|
|
|
// Each symbol is laid out as the following fields (taken from LSym*):
|
|
|
|
|
//
|
|
|
|
|
// - byte 0xfe (sanity check for synchronization)
|
|
|
|
|
// - type [int]
|
2016-03-14 22:57:58 +02:00
|
|
|
// - name & version [symref index]
|
2015-05-18 15:50:00 -04:00
|
|
|
// - flags [int]
|
|
|
|
|
// 1 dupok
|
|
|
|
|
// - size [int]
|
2016-03-14 22:57:58 +02:00
|
|
|
// - gotype [symref index]
|
2015-05-18 15:50:00 -04:00
|
|
|
// - p [data block]
|
|
|
|
|
// - nr [int]
|
|
|
|
|
// - r [nr relocations, sorted by off]
|
|
|
|
|
//
|
|
|
|
|
// If type == STEXT, there are a few more fields:
|
|
|
|
|
//
|
|
|
|
|
// - args [int]
|
|
|
|
|
// - locals [int]
|
|
|
|
|
// - nosplit [int]
|
|
|
|
|
// - flags [int]
|
2016-03-10 16:15:26 -05:00
|
|
|
// 1<<0 leaf
|
|
|
|
|
// 1<<1 C function
|
|
|
|
|
// 1<<2 function may call reflect.Type.Method
|
2015-05-18 15:50:00 -04:00
|
|
|
// - nlocal [int]
|
|
|
|
|
// - local [nlocal automatics]
|
|
|
|
|
// - pcln [pcln table]
|
|
|
|
|
//
|
|
|
|
|
// Each relocation has the encoding:
|
|
|
|
|
//
|
|
|
|
|
// - off [int]
|
|
|
|
|
// - siz [int]
|
|
|
|
|
// - type [int]
|
|
|
|
|
// - add [int]
|
2016-03-14 22:57:58 +02:00
|
|
|
// - sym [symref index]
|
2015-05-18 15:50:00 -04:00
|
|
|
//
|
|
|
|
|
// Each local has the encoding:
|
|
|
|
|
//
|
2016-03-14 22:57:58 +02:00
|
|
|
// - asym [symref index]
|
2015-05-18 15:50:00 -04:00
|
|
|
// - offset [int]
|
|
|
|
|
// - type [int]
|
2016-03-14 22:57:58 +02:00
|
|
|
// - gotype [symref index]
|
2015-05-18 15:50:00 -04:00
|
|
|
//
|
|
|
|
|
// The pcln table has the encoding:
|
|
|
|
|
//
|
|
|
|
|
// - pcsp [data block]
|
|
|
|
|
// - pcfile [data block]
|
|
|
|
|
// - pcline [data block]
|
|
|
|
|
// - npcdata [int]
|
|
|
|
|
// - pcdata [npcdata data blocks]
|
|
|
|
|
// - nfuncdata [int]
|
2016-03-14 22:57:58 +02:00
|
|
|
// - funcdata [nfuncdata symref index]
|
2015-05-18 15:50:00 -04:00
|
|
|
// - funcdatasym [nfuncdata ints]
|
|
|
|
|
// - nfile [int]
|
2016-03-14 22:57:58 +02:00
|
|
|
// - file [nfile symref index]
|
2015-05-18 15:50:00 -04:00
|
|
|
//
|
|
|
|
|
// The file layout and meaning of type integers are architecture-independent.
|
|
|
|
|
//
|
|
|
|
|
// TODO(rsc): The file format is good for a first pass but needs work.
|
|
|
|
|
// - There are SymID in the object file that should really just be strings.
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
import (
|
|
|
|
|
"bytes"
|
2015-04-19 19:33:58 -07:00
|
|
|
"cmd/internal/obj"
|
2015-02-27 22:57:28 -05:00
|
|
|
"log"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2015-03-06 11:42:53 -08:00
|
|
|
const (
|
|
|
|
|
startmagic = "\x00\x00go13ld"
|
|
|
|
|
endmagic = "\xff\xffgo13ld"
|
|
|
|
|
)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-05-02 12:44:49 +10:00
|
|
|
func ldobjfile(ctxt *Link, f *obj.Biobuf, pkg string, length int64, pn string) {
|
|
|
|
|
start := obj.Boffset(f)
|
2016-03-27 10:06:12 +03:00
|
|
|
ctxt.IncVersion()
|
2015-03-02 14:22:05 -05:00
|
|
|
var buf [8]uint8
|
2015-05-02 12:44:49 +10:00
|
|
|
obj.Bread(f, buf[:])
|
2015-02-27 22:57:28 -05:00
|
|
|
if string(buf[:]) != startmagic {
|
|
|
|
|
log.Fatalf("%s: invalid file start %x %x %x %x %x %x %x %x", pn, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7])
|
|
|
|
|
}
|
2015-05-02 12:44:49 +10:00
|
|
|
c := obj.Bgetc(f)
|
2015-02-27 22:57:28 -05:00
|
|
|
if c != 1 {
|
|
|
|
|
log.Fatalf("%s: invalid file version number %d", pn, c)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
var lib string
|
2015-02-27 22:57:28 -05:00
|
|
|
for {
|
|
|
|
|
lib = rdstring(f)
|
|
|
|
|
if lib == "" {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
addlib(ctxt, pkg, pn, lib)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 22:57:58 +02:00
|
|
|
ctxt.CurRefs = []*LSym{nil} // zeroth ref is nil
|
|
|
|
|
for {
|
|
|
|
|
c, err := f.Peek(1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("%s: peeking: %v", pn, err)
|
|
|
|
|
}
|
|
|
|
|
if c[0] == 0xff {
|
|
|
|
|
obj.Bgetc(f)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
readref(ctxt, f, pkg, pn)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 00:44:07 +02:00
|
|
|
sl := rdslices(f)
|
|
|
|
|
|
|
|
|
|
obj.Bread(f, sl.data)
|
2016-03-21 10:55:20 +13:00
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
for {
|
2015-05-02 12:44:49 +10:00
|
|
|
c, err := f.Peek(1)
|
2015-04-27 12:53:34 -04:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("%s: peeking: %v", pn, err)
|
|
|
|
|
}
|
|
|
|
|
if c[0] == 0xff {
|
2015-02-27 22:57:28 -05:00
|
|
|
break
|
|
|
|
|
}
|
2016-03-23 00:44:07 +02:00
|
|
|
readsym(ctxt, f, sl, pkg, pn)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf = [8]uint8{}
|
2015-05-02 12:44:49 +10:00
|
|
|
obj.Bread(f, buf[:])
|
2015-02-27 22:57:28 -05:00
|
|
|
if string(buf[:]) != endmagic {
|
|
|
|
|
log.Fatalf("%s: invalid file end", pn)
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-02 12:44:49 +10:00
|
|
|
if obj.Boffset(f) != start+length {
|
|
|
|
|
log.Fatalf("%s: unexpected end at %d, want %d", pn, int64(obj.Boffset(f)), int64(start+length))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-21 17:29:29 +13:00
|
|
|
var dupSym = &LSym{Name: ".dup"}
|
|
|
|
|
|
2016-03-23 00:44:07 +02:00
|
|
|
type slices struct {
|
|
|
|
|
data []byte
|
|
|
|
|
reloc []Reloc
|
|
|
|
|
pcdata []Pcdata
|
|
|
|
|
autom []Auto
|
|
|
|
|
funcdata []*LSym
|
|
|
|
|
funcdataoff []int64
|
|
|
|
|
file []*LSym
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rdslices(f *obj.Biobuf) *slices {
|
|
|
|
|
sl := &slices{}
|
|
|
|
|
|
|
|
|
|
n := rdint(f)
|
|
|
|
|
sl.data = make([]byte, n)
|
|
|
|
|
n = rdint(f)
|
|
|
|
|
sl.reloc = make([]Reloc, n)
|
|
|
|
|
n = rdint(f)
|
|
|
|
|
sl.pcdata = make([]Pcdata, n)
|
|
|
|
|
n = rdint(f)
|
|
|
|
|
sl.autom = make([]Auto, n)
|
|
|
|
|
n = rdint(f)
|
|
|
|
|
sl.funcdata = make([]*LSym, n)
|
|
|
|
|
sl.funcdataoff = make([]int64, n)
|
|
|
|
|
n = rdint(f)
|
|
|
|
|
sl.file = make([]*LSym, n)
|
|
|
|
|
return sl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readsym(ctxt *Link, f *obj.Biobuf, sl *slices, pkg string, pn string) {
|
2015-05-02 12:44:49 +10:00
|
|
|
if obj.Bgetc(f) != 0xfe {
|
2016-03-23 00:44:07 +02:00
|
|
|
log.Fatalln("readsym out of sync")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-07-10 14:38:35 -07:00
|
|
|
t := rdint(f)
|
2016-03-14 22:57:58 +02:00
|
|
|
s := rdsym(ctxt, f, pkg)
|
2015-07-10 14:38:35 -07:00
|
|
|
flags := rdint(f)
|
2016-03-02 07:59:49 -05:00
|
|
|
dupok := flags&1 != 0
|
|
|
|
|
local := flags&2 != 0
|
2015-07-10 14:38:35 -07:00
|
|
|
size := rdint(f)
|
2015-03-02 12:35:15 -05:00
|
|
|
typ := rdsym(ctxt, f, pkg)
|
2016-03-23 00:44:07 +02:00
|
|
|
data := rddata(f, &sl.data)
|
2015-07-10 14:38:35 -07:00
|
|
|
nreloc := rdint(f)
|
2016-03-23 00:44:07 +02:00
|
|
|
isdup := false
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 14:22:05 -05:00
|
|
|
var dup *LSym
|
2015-04-19 19:33:58 -07:00
|
|
|
if s.Type != 0 && s.Type != obj.SXREF {
|
|
|
|
|
if (t == obj.SDATA || t == obj.SBSS || t == obj.SNOPTRBSS) && len(data) == 0 && nreloc == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
if s.Size < int64(size) {
|
|
|
|
|
s.Size = int64(size)
|
|
|
|
|
}
|
|
|
|
|
if typ != nil && s.Gotype == nil {
|
|
|
|
|
s.Gotype = typ
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
if (s.Type == obj.SDATA || s.Type == obj.SBSS || s.Type == obj.SNOPTRBSS) && len(s.P) == 0 && len(s.R) == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
goto overwrite
|
|
|
|
|
}
|
2016-03-02 07:59:49 -05:00
|
|
|
if s.Type != obj.SBSS && s.Type != obj.SNOPTRBSS && !dupok && !s.Attr.DuplicateOK() {
|
2015-02-27 22:57:28 -05:00
|
|
|
log.Fatalf("duplicate symbol %s (types %d and %d) in %s and %s", s.Name, s.Type, t, s.File, pn)
|
|
|
|
|
}
|
|
|
|
|
if len(s.P) > 0 {
|
|
|
|
|
dup = s
|
2016-03-21 17:29:29 +13:00
|
|
|
s = dupSym
|
2016-03-23 00:44:07 +02:00
|
|
|
isdup = true
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
overwrite:
|
|
|
|
|
s.File = pkg
|
2016-03-02 07:59:49 -05:00
|
|
|
if dupok {
|
|
|
|
|
s.Attr |= AttrDuplicateOK
|
|
|
|
|
}
|
2015-04-19 19:33:58 -07:00
|
|
|
if t == obj.SXREF {
|
2015-02-27 22:57:28 -05:00
|
|
|
log.Fatalf("bad sxref")
|
|
|
|
|
}
|
|
|
|
|
if t == 0 {
|
2016-03-14 22:57:58 +02:00
|
|
|
log.Fatalf("missing type for %s in %s", s.Name, pn)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-04-19 19:33:58 -07:00
|
|
|
if t == obj.SBSS && (s.Type == obj.SRODATA || s.Type == obj.SNOPTRBSS) {
|
2015-02-27 22:57:28 -05:00
|
|
|
t = int(s.Type)
|
|
|
|
|
}
|
|
|
|
|
s.Type = int16(t)
|
|
|
|
|
if s.Size < int64(size) {
|
|
|
|
|
s.Size = int64(size)
|
|
|
|
|
}
|
2016-03-02 07:59:49 -05:00
|
|
|
s.Attr.Set(AttrLocal, local)
|
2016-03-21 17:29:29 +13:00
|
|
|
if typ != nil {
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Gotype = typ
|
|
|
|
|
}
|
2016-03-23 00:44:07 +02:00
|
|
|
if isdup && typ != nil { // if bss sym defined multiple times, take type from any one def
|
2015-02-27 22:57:28 -05:00
|
|
|
dup.Gotype = typ
|
|
|
|
|
}
|
|
|
|
|
s.P = data
|
|
|
|
|
if nreloc > 0 {
|
2016-03-23 00:44:07 +02:00
|
|
|
s.R = sl.reloc[:nreloc:nreloc]
|
|
|
|
|
if !isdup {
|
|
|
|
|
sl.reloc = sl.reloc[nreloc:]
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
var r *Reloc
|
|
|
|
|
for i := 0; i < nreloc; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
r = &s.R[i]
|
2015-07-10 14:38:35 -07:00
|
|
|
r.Off = rdint32(f)
|
|
|
|
|
r.Siz = rduint8(f)
|
|
|
|
|
r.Type = rdint32(f)
|
|
|
|
|
r.Add = rdint64(f)
|
2015-02-27 22:57:28 -05:00
|
|
|
r.Sym = rdsym(ctxt, f, pkg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
if s.Type == obj.STEXT {
|
2015-07-10 14:38:35 -07:00
|
|
|
s.Args = rdint32(f)
|
|
|
|
|
s.Locals = rdint32(f)
|
2016-03-02 07:59:49 -05:00
|
|
|
if rduint8(f) != 0 {
|
|
|
|
|
s.Attr |= AttrNoSplit
|
|
|
|
|
}
|
2016-03-10 16:15:26 -05:00
|
|
|
flags := rdint(f)
|
|
|
|
|
if flags&(1<<2) != 0 {
|
|
|
|
|
s.Attr |= AttrReflectMethod
|
|
|
|
|
}
|
2015-07-10 14:38:35 -07:00
|
|
|
n := rdint(f)
|
2016-03-23 00:44:07 +02:00
|
|
|
s.Autom = sl.autom[:n:n]
|
|
|
|
|
if !isdup {
|
|
|
|
|
sl.autom = sl.autom[n:]
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < n; i++ {
|
2016-03-02 22:38:42 -05:00
|
|
|
s.Autom[i] = Auto{
|
|
|
|
|
Asym: rdsym(ctxt, f, pkg),
|
|
|
|
|
Aoffset: rdint32(f),
|
|
|
|
|
Name: rdint16(f),
|
|
|
|
|
Gotype: rdsym(ctxt, f, pkg),
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.Pcln = new(Pcln)
|
2015-03-02 12:35:15 -05:00
|
|
|
pc := s.Pcln
|
2016-03-23 00:44:07 +02:00
|
|
|
pc.Pcsp.P = rddata(f, &sl.data)
|
|
|
|
|
pc.Pcfile.P = rddata(f, &sl.data)
|
|
|
|
|
pc.Pcline.P = rddata(f, &sl.data)
|
2015-07-10 14:38:35 -07:00
|
|
|
n = rdint(f)
|
2016-03-23 00:44:07 +02:00
|
|
|
pc.Pcdata = sl.pcdata[:n:n]
|
|
|
|
|
if !isdup {
|
|
|
|
|
sl.pcdata = sl.pcdata[n:]
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < n; i++ {
|
2016-03-23 00:44:07 +02:00
|
|
|
pc.Pcdata[i].P = rddata(f, &sl.data)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-07-10 14:38:35 -07:00
|
|
|
n = rdint(f)
|
2016-03-23 00:44:07 +02:00
|
|
|
pc.Funcdata = sl.funcdata[:n:n]
|
|
|
|
|
pc.Funcdataoff = sl.funcdataoff[:n:n]
|
|
|
|
|
if !isdup {
|
|
|
|
|
sl.funcdata = sl.funcdata[n:]
|
|
|
|
|
sl.funcdataoff = sl.funcdataoff[n:]
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < n; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
pc.Funcdata[i] = rdsym(ctxt, f, pkg)
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < n; i++ {
|
2015-07-10 14:38:35 -07:00
|
|
|
pc.Funcdataoff[i] = rdint64(f)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-07-10 14:38:35 -07:00
|
|
|
n = rdint(f)
|
2016-03-23 00:44:07 +02:00
|
|
|
pc.File = sl.file[:n:n]
|
|
|
|
|
if !isdup {
|
|
|
|
|
sl.file = sl.file[n:]
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < n; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
pc.File[i] = rdsym(ctxt, f, pkg)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 00:44:07 +02:00
|
|
|
if !isdup {
|
2016-03-02 07:59:49 -05:00
|
|
|
if s.Attr.OnList() {
|
2015-02-27 22:57:28 -05:00
|
|
|
log.Fatalf("symbol %s listed multiple times", s.Name)
|
|
|
|
|
}
|
2016-03-02 07:59:49 -05:00
|
|
|
s.Attr |= AttrOnList
|
2015-02-27 22:57:28 -05:00
|
|
|
if ctxt.Etextp != nil {
|
|
|
|
|
ctxt.Etextp.Next = s
|
|
|
|
|
} else {
|
|
|
|
|
ctxt.Textp = s
|
|
|
|
|
}
|
|
|
|
|
ctxt.Etextp = s
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 22:57:58 +02:00
|
|
|
func readref(ctxt *Link, f *obj.Biobuf, pkg string, pn string) {
|
|
|
|
|
if obj.Bgetc(f) != 0xfe {
|
|
|
|
|
log.Fatalf("readsym out of sync")
|
|
|
|
|
}
|
|
|
|
|
name := rdsymName(f, pkg)
|
|
|
|
|
v := rdint(f)
|
|
|
|
|
if v != 0 && v != 1 {
|
|
|
|
|
log.Fatalf("invalid symbol version %d", v)
|
|
|
|
|
}
|
|
|
|
|
if v == 1 {
|
|
|
|
|
v = ctxt.Version
|
|
|
|
|
}
|
2016-03-20 17:22:57 +02:00
|
|
|
s := Linklookup(ctxt, name, v)
|
|
|
|
|
ctxt.CurRefs = append(ctxt.CurRefs, s)
|
|
|
|
|
|
|
|
|
|
if s == nil || v != 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if s.Name[0] == '$' && len(s.Name) > 5 && s.Type == 0 && len(s.P) == 0 {
|
|
|
|
|
x, err := strconv.ParseUint(s.Name[5:], 16, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicf("failed to parse $-symbol %s: %v", s.Name, err)
|
|
|
|
|
}
|
|
|
|
|
s.Type = obj.SRODATA
|
|
|
|
|
s.Attr |= AttrLocal
|
|
|
|
|
switch s.Name[:5] {
|
|
|
|
|
case "$f32.":
|
|
|
|
|
if uint64(uint32(x)) != x {
|
|
|
|
|
log.Panicf("$-symbol %s too large: %d", s.Name, x)
|
|
|
|
|
}
|
|
|
|
|
Adduint32(ctxt, s, uint32(x))
|
|
|
|
|
case "$f64.", "$i64.":
|
|
|
|
|
Adduint64(ctxt, s, x)
|
|
|
|
|
default:
|
|
|
|
|
log.Panicf("unrecognized $-symbol: %s", s.Name)
|
|
|
|
|
}
|
|
|
|
|
s.Attr.Set(AttrReachable, false)
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(s.Name, "runtime.gcbits.") {
|
|
|
|
|
s.Attr |= AttrLocal
|
|
|
|
|
}
|
2016-03-14 22:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-10 14:38:35 -07:00
|
|
|
func rdint64(f *obj.Biobuf) int64 {
|
2016-03-19 23:27:41 +02:00
|
|
|
r := f.Reader()
|
2015-03-02 12:35:15 -05:00
|
|
|
uv := uint64(0)
|
2016-03-19 23:27:41 +02:00
|
|
|
for shift := uint(0); ; shift += 7 {
|
2015-02-27 22:57:28 -05:00
|
|
|
if shift >= 64 {
|
|
|
|
|
log.Fatalf("corrupt input")
|
|
|
|
|
}
|
2016-03-19 23:27:41 +02:00
|
|
|
c, err := r.ReadByte()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln("error reading input: ", err)
|
|
|
|
|
}
|
|
|
|
|
uv |= uint64(c&0x7F) << shift
|
2015-02-27 22:57:28 -05:00
|
|
|
if c&0x80 == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int64(uv>>1) ^ (int64(uint64(uv)<<63) >> 63)
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-10 14:38:35 -07:00
|
|
|
func rdint(f *obj.Biobuf) int {
|
|
|
|
|
n := rdint64(f)
|
|
|
|
|
if int64(int(n)) != n {
|
|
|
|
|
log.Panicf("%v out of range for int", n)
|
|
|
|
|
}
|
|
|
|
|
return int(n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rdint32(f *obj.Biobuf) int32 {
|
|
|
|
|
n := rdint64(f)
|
|
|
|
|
if int64(int32(n)) != n {
|
|
|
|
|
log.Panicf("%v out of range for int32", n)
|
|
|
|
|
}
|
|
|
|
|
return int32(n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rdint16(f *obj.Biobuf) int16 {
|
|
|
|
|
n := rdint64(f)
|
|
|
|
|
if int64(int16(n)) != n {
|
|
|
|
|
log.Panicf("%v out of range for int16", n)
|
|
|
|
|
}
|
|
|
|
|
return int16(n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rduint8(f *obj.Biobuf) uint8 {
|
|
|
|
|
n := rdint64(f)
|
|
|
|
|
if int64(uint8(n)) != n {
|
|
|
|
|
log.Panicf("%v out of range for uint8", n)
|
|
|
|
|
}
|
|
|
|
|
return uint8(n)
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:59:33 -09:00
|
|
|
// rdBuf is used by rdstring and rdsymName as scratch for reading strings.
|
|
|
|
|
var rdBuf []byte
|
|
|
|
|
var emptyPkg = []byte(`"".`)
|
|
|
|
|
|
2015-05-02 12:44:49 +10:00
|
|
|
func rdstring(f *obj.Biobuf) string {
|
2016-02-28 15:59:33 -09:00
|
|
|
n := rdint(f)
|
|
|
|
|
if len(rdBuf) < n {
|
|
|
|
|
rdBuf = make([]byte, n)
|
|
|
|
|
}
|
|
|
|
|
obj.Bread(f, rdBuf[:n])
|
|
|
|
|
return string(rdBuf[:n])
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-21 10:55:20 +13:00
|
|
|
func rddata(f *obj.Biobuf, buf *[]byte) []byte {
|
2016-02-28 17:58:57 -09:00
|
|
|
n := rdint(f)
|
2016-03-21 10:55:20 +13:00
|
|
|
p := (*buf)[:n:n]
|
|
|
|
|
*buf = (*buf)[n:]
|
2015-04-24 20:24:49 -07:00
|
|
|
return p
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:59:33 -09:00
|
|
|
// rdsymName reads a symbol name, replacing all "". with pkg.
|
|
|
|
|
func rdsymName(f *obj.Biobuf, pkg string) string {
|
2015-07-10 14:38:35 -07:00
|
|
|
n := rdint(f)
|
2015-02-27 22:57:28 -05:00
|
|
|
if n == 0 {
|
2015-07-10 14:38:35 -07:00
|
|
|
rdint64(f)
|
2016-02-28 15:59:33 -09:00
|
|
|
return ""
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:59:33 -09:00
|
|
|
if len(rdBuf) < n {
|
|
|
|
|
rdBuf = make([]byte, n, 2*n)
|
|
|
|
|
}
|
|
|
|
|
origName := rdBuf[:n]
|
|
|
|
|
obj.Bread(f, origName)
|
|
|
|
|
adjName := rdBuf[n:n]
|
|
|
|
|
for {
|
|
|
|
|
i := bytes.Index(origName, emptyPkg)
|
|
|
|
|
if i == -1 {
|
|
|
|
|
adjName = append(adjName, origName...)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
adjName = append(adjName, origName[:i]...)
|
|
|
|
|
adjName = append(adjName, pkg...)
|
|
|
|
|
adjName = append(adjName, '.')
|
|
|
|
|
origName = origName[i+len(emptyPkg):]
|
|
|
|
|
}
|
|
|
|
|
name := string(adjName)
|
|
|
|
|
if len(adjName) > len(rdBuf) {
|
|
|
|
|
rdBuf = adjName // save the larger buffer for reuse
|
|
|
|
|
}
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rdsym(ctxt *Link, f *obj.Biobuf, pkg string) *LSym {
|
2016-03-14 22:57:58 +02:00
|
|
|
i := rdint(f)
|
2016-03-20 17:22:57 +02:00
|
|
|
return ctxt.CurRefs[i]
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|