2015-01-19 14:34:58 -05:00
|
|
|
// Derived from Inferno utils/6l/obj.c and utils/6l/span.c
|
2016-08-28 17:04:46 -07:00
|
|
|
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c
|
|
|
|
|
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/span.c
|
2015-01-19 14:34:58 -05:00
|
|
|
//
|
|
|
|
|
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
|
|
|
|
|
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
|
// Portions Copyright © 1997-1999 Vita Nuova Limited
|
|
|
|
|
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
|
|
|
|
|
// Portions Copyright © 2004,2006 Bruce Ellis
|
|
|
|
|
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
|
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
|
2016-04-10 14:32:26 -07:00
|
|
|
// Portions Copyright © 2009 The Go Authors. All rights reserved.
|
2015-01-19 14:34:58 -05:00
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
package obj
|
|
|
|
|
|
|
|
|
|
import (
|
2017-04-18 12:53:25 -07:00
|
|
|
"cmd/internal/objabi"
|
2015-01-19 14:34:58 -05:00
|
|
|
"log"
|
|
|
|
|
"math"
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-16 14:10:51 -07:00
|
|
|
// Grow increases the length of s.P to lsiz.
|
|
|
|
|
func (s *LSym) Grow(lsiz int64) {
|
2015-02-23 16:07:24 -05:00
|
|
|
siz := int(lsiz)
|
2015-01-19 14:34:58 -05:00
|
|
|
if int64(siz) != lsiz {
|
2016-03-16 14:10:51 -07:00
|
|
|
log.Fatalf("LSym.Grow size %d too long", lsiz)
|
2015-01-19 14:34:58 -05:00
|
|
|
}
|
|
|
|
|
if len(s.P) >= siz {
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-09-07 16:21:25 +10:00
|
|
|
// TODO(dfc) append cap-len at once, rather than
|
|
|
|
|
// one byte at a time.
|
2015-01-19 14:34:58 -05:00
|
|
|
for cap(s.P) < siz {
|
|
|
|
|
s.P = append(s.P[:cap(s.P)], 0)
|
|
|
|
|
}
|
|
|
|
|
s.P = s.P[:siz]
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 12:49:58 -07:00
|
|
|
// GrowCap increases the capacity of s.P to c.
|
|
|
|
|
func (s *LSym) GrowCap(c int64) {
|
|
|
|
|
if int64(cap(s.P)) >= c {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if s.P == nil {
|
|
|
|
|
s.P = make([]byte, 0, c)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b := make([]byte, len(s.P), c)
|
|
|
|
|
copy(b, s.P)
|
|
|
|
|
s.P = b
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-13 12:16:43 -07:00
|
|
|
// prepwrite prepares to write data of size siz into s at offset off.
|
2016-03-13 19:28:32 -07:00
|
|
|
func (s *LSym) prepwrite(ctxt *Link, off int64, siz int) {
|
2016-03-11 18:54:37 -08:00
|
|
|
if off < 0 || siz < 0 || off >= 1<<30 {
|
2017-03-09 22:41:32 -08:00
|
|
|
ctxt.Diag("prepwrite: bad off=%d siz=%d s=%v", off, siz, s)
|
2016-03-13 12:16:43 -07:00
|
|
|
}
|
cmd/internal/obj: fix LSym.Type during compilation, not linking
Prior to this CL, the compiler and assembler
were sloppy about the LSym.Type for LSyms
containing static data.
The linker then fixed this up, converting
Sxxx and SBSS to SDATA, and SNOPTRBSS to SNOPTRDATA
if it noticed that the symbol had associated data.
It is preferable to just get this right in cmd/compile
and cmd/asm, because it removes an unnecessary traversal
of the symbol table from the linker (see #14624).
Do this by touching up the LSym.Type fixes in
LSym.prepwrite and Link.Globl.
I have confirmed by instrumenting the linker
that the now-eliminated code paths were unreached.
And an additional check in the object file writing code
will help preserve that invariant.
There was a case in the Windows linker,
with internal linking and cgo,
where we were generating SNOPTRBSS symbols with data.
For now, convert those at the site at which they occur
into SNOPTRDATA, just like they were.
Does not pass toolstash-check,
but does generate identical linked binaries.
No compiler performance changes.
Change-Id: I77b071ab103685ff8e042cee9abb864385488872
Reviewed-on: https://go-review.googlesource.com/40864
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com>
2017-04-16 08:22:44 -07:00
|
|
|
switch s.Type {
|
|
|
|
|
case objabi.Sxxx, objabi.SBSS:
|
|
|
|
|
s.Type = objabi.SDATA
|
|
|
|
|
case objabi.SNOPTRBSS:
|
|
|
|
|
s.Type = objabi.SNOPTRDATA
|
|
|
|
|
case objabi.STLSBSS:
|
|
|
|
|
ctxt.Diag("cannot supply data for %v var %v", s.Type, s.Name)
|
2016-03-13 12:16:43 -07:00
|
|
|
}
|
2016-03-31 10:02:10 -04:00
|
|
|
l := off + int64(siz)
|
|
|
|
|
s.Grow(l)
|
|
|
|
|
if l > s.Size {
|
|
|
|
|
s.Size = l
|
|
|
|
|
}
|
2016-03-13 12:16:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteFloat32 writes f into s at offset off.
|
|
|
|
|
func (s *LSym) WriteFloat32(ctxt *Link, off int64, f float32) {
|
|
|
|
|
s.prepwrite(ctxt, off, 4)
|
|
|
|
|
ctxt.Arch.ByteOrder.PutUint32(s.P[off:], math.Float32bits(f))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteFloat64 writes f into s at offset off.
|
|
|
|
|
func (s *LSym) WriteFloat64(ctxt *Link, off int64, f float64) {
|
|
|
|
|
s.prepwrite(ctxt, off, 8)
|
|
|
|
|
ctxt.Arch.ByteOrder.PutUint64(s.P[off:], math.Float64bits(f))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteInt writes an integer i of size siz into s at offset off.
|
2016-03-13 19:28:32 -07:00
|
|
|
func (s *LSym) WriteInt(ctxt *Link, off int64, siz int, i int64) {
|
2016-03-13 12:16:43 -07:00
|
|
|
s.prepwrite(ctxt, off, siz)
|
|
|
|
|
switch siz {
|
|
|
|
|
default:
|
2016-03-11 18:54:37 -08:00
|
|
|
ctxt.Diag("WriteInt: bad integer size: %d", siz)
|
2016-03-13 12:16:43 -07:00
|
|
|
case 1:
|
|
|
|
|
s.P[off] = byte(i)
|
|
|
|
|
case 2:
|
|
|
|
|
ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i))
|
|
|
|
|
case 4:
|
|
|
|
|
ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(i))
|
|
|
|
|
case 8:
|
|
|
|
|
ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(i))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteAddr writes an address of size siz into s at offset off.
|
|
|
|
|
// rsym and roff specify the relocation for the address.
|
2016-03-13 19:28:32 -07:00
|
|
|
func (s *LSym) WriteAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64) {
|
2017-04-07 19:50:31 +00:00
|
|
|
if siz != ctxt.Arch.PtrSize {
|
2016-03-27 10:21:48 -04:00
|
|
|
ctxt.Diag("WriteAddr: bad address size %d in %s", siz, s.Name)
|
2016-03-11 18:54:37 -08:00
|
|
|
}
|
2016-03-13 12:16:43 -07:00
|
|
|
s.prepwrite(ctxt, off, siz)
|
|
|
|
|
r := Addrel(s)
|
|
|
|
|
r.Off = int32(off)
|
2016-03-27 10:21:48 -04:00
|
|
|
if int64(r.Off) != off {
|
|
|
|
|
ctxt.Diag("WriteAddr: off overflow %d in %s", off, s.Name)
|
|
|
|
|
}
|
2016-03-13 12:16:43 -07:00
|
|
|
r.Siz = uint8(siz)
|
|
|
|
|
r.Sym = rsym
|
2017-04-18 12:53:25 -07:00
|
|
|
r.Type = objabi.R_ADDR
|
2016-03-13 12:16:43 -07:00
|
|
|
r.Add = roff
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 10:21:48 -04:00
|
|
|
// WriteOff writes a 4 byte offset to rsym+roff into s at offset off.
|
|
|
|
|
// After linking the 4 bytes stored at s+off will be
|
|
|
|
|
// rsym+roff-(start of section that s is in).
|
|
|
|
|
func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
|
|
|
|
|
s.prepwrite(ctxt, off, 4)
|
|
|
|
|
r := Addrel(s)
|
|
|
|
|
r.Off = int32(off)
|
|
|
|
|
if int64(r.Off) != off {
|
|
|
|
|
ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
|
|
|
|
|
}
|
|
|
|
|
r.Siz = 4
|
|
|
|
|
r.Sym = rsym
|
2017-04-18 12:53:25 -07:00
|
|
|
r.Type = objabi.R_ADDROFF
|
2016-03-27 10:21:48 -04:00
|
|
|
r.Add = roff
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 16:58:55 -05:00
|
|
|
// WriteWeakOff writes a weak 4 byte offset to rsym+roff into s at offset off.
|
|
|
|
|
// After linking the 4 bytes stored at s+off will be
|
|
|
|
|
// rsym+roff-(start of section that s is in).
|
|
|
|
|
func (s *LSym) WriteWeakOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
|
|
|
|
|
s.prepwrite(ctxt, off, 4)
|
|
|
|
|
r := Addrel(s)
|
|
|
|
|
r.Off = int32(off)
|
|
|
|
|
if int64(r.Off) != off {
|
|
|
|
|
ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
|
|
|
|
|
}
|
|
|
|
|
r.Siz = 4
|
|
|
|
|
r.Sym = rsym
|
2017-04-18 12:53:25 -07:00
|
|
|
r.Type = objabi.R_WEAKADDROFF
|
2016-11-21 16:58:55 -05:00
|
|
|
r.Add = roff
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-13 12:16:43 -07:00
|
|
|
// WriteString writes a string of size siz into s at offset off.
|
2016-03-13 19:28:32 -07:00
|
|
|
func (s *LSym) WriteString(ctxt *Link, off int64, siz int, str string) {
|
|
|
|
|
if siz < len(str) {
|
2016-03-11 18:54:37 -08:00
|
|
|
ctxt.Diag("WriteString: bad string size: %d < %d", siz, len(str))
|
|
|
|
|
}
|
2016-03-13 12:16:43 -07:00
|
|
|
s.prepwrite(ctxt, off, siz)
|
2016-03-13 19:28:32 -07:00
|
|
|
copy(s.P[off:off+int64(siz)], str)
|
2016-03-13 12:16:43 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 10:02:10 -04:00
|
|
|
// WriteBytes writes a slice of bytes into s at offset off.
|
|
|
|
|
func (s *LSym) WriteBytes(ctxt *Link, off int64, b []byte) int64 {
|
|
|
|
|
s.prepwrite(ctxt, off, len(b))
|
|
|
|
|
copy(s.P[off:], b)
|
|
|
|
|
return off + int64(len(b))
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 14:34:58 -05:00
|
|
|
func Addrel(s *LSym) *Reloc {
|
|
|
|
|
s.R = append(s.R, Reloc{})
|
|
|
|
|
return &s.R[len(s.R)-1]
|
|
|
|
|
}
|