2015-01-19 14:34:58 -05:00
|
|
|
// Derived from Inferno utils/6l/l.h and related files.
|
2016-08-28 17:04:46 -07:00
|
|
|
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/l.h
|
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
|
|
|
|
|
|
2016-04-06 21:45:29 -07:00
|
|
|
import (
|
2016-04-08 19:30:41 +10:00
|
|
|
"bufio"
|
2016-12-09 12:34:01 -05:00
|
|
|
"cmd/internal/src"
|
2016-04-06 21:45:29 -07:00
|
|
|
"cmd/internal/sys"
|
2016-08-25 12:32:42 +10:00
|
|
|
"fmt"
|
2016-04-06 21:45:29 -07:00
|
|
|
)
|
2015-01-19 14:34:58 -05:00
|
|
|
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
// An Addr is an argument to an instruction.
|
|
|
|
|
// The general forms and their encodings are:
|
|
|
|
|
//
|
|
|
|
|
// sym±offset(symkind)(reg)(index*scale)
|
|
|
|
|
// Memory reference at address &sym(symkind) + offset + reg + index*scale.
|
|
|
|
|
// Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
|
|
|
|
|
// If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
|
|
|
|
|
// To force a parsing as index*scale, write (index*1).
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_MEM
|
|
|
|
|
// name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
|
|
|
|
|
// sym = sym
|
|
|
|
|
// offset = ±offset
|
|
|
|
|
// reg = reg (REG_*)
|
|
|
|
|
// index = index (REG_*)
|
|
|
|
|
// scale = scale (1, 2, 4, 8)
|
|
|
|
|
//
|
|
|
|
|
// $<mem>
|
|
|
|
|
// Effective address of memory reference <mem>, defined above.
|
|
|
|
|
// Encoding: same as memory reference, but type = TYPE_ADDR.
|
|
|
|
|
//
|
|
|
|
|
// $<±integer value>
|
|
|
|
|
// This is a special case of $<mem>, in which only ±offset is present.
|
|
|
|
|
// It has a separate type for easy recognition.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_CONST
|
|
|
|
|
// offset = ±integer value
|
|
|
|
|
//
|
|
|
|
|
// *<mem>
|
|
|
|
|
// Indirect reference through memory reference <mem>, defined above.
|
|
|
|
|
// Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
|
|
|
|
|
// pointer stored in the data word sym(SB), not a function named sym(SB).
|
|
|
|
|
// Encoding: same as above, but type = TYPE_INDIR.
|
|
|
|
|
//
|
|
|
|
|
// $*$<mem>
|
|
|
|
|
// No longer used.
|
|
|
|
|
// On machines with actual SB registers, $*$<mem> forced the
|
|
|
|
|
// instruction encoding to use a full 32-bit constant, never a
|
|
|
|
|
// reference relative to SB.
|
|
|
|
|
//
|
|
|
|
|
// $<floating point literal>
|
|
|
|
|
// Floating point constant value.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_FCONST
|
2015-03-16 15:54:44 -04:00
|
|
|
// val = floating point value
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
//
|
|
|
|
|
// $<string literal, up to 8 chars>
|
|
|
|
|
// String literal value (raw bytes used for DATA instruction).
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_SCONST
|
2015-03-16 15:54:44 -04:00
|
|
|
// val = string
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
//
|
|
|
|
|
// <register name>
|
|
|
|
|
// Any register: integer, floating point, control, segment, and so on.
|
|
|
|
|
// If looking for specific register kind, must check type and reg value range.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_REG
|
|
|
|
|
// reg = reg (REG_*)
|
|
|
|
|
//
|
|
|
|
|
// x(PC)
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_BRANCH
|
2015-03-16 15:54:44 -04:00
|
|
|
// val = Prog* reference OR ELSE offset = target pc (branch takes priority)
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
//
|
|
|
|
|
// $±x-±y
|
|
|
|
|
// Final argument to TEXT, specifying local frame size x and argument size y.
|
|
|
|
|
// In this form, x and y are integer literals only, not arbitrary expressions.
|
|
|
|
|
// This avoids parsing ambiguities due to the use of - as a separator.
|
|
|
|
|
// The ± are optional.
|
|
|
|
|
// If the final argument to TEXT omits the -±y, the encoding should still
|
|
|
|
|
// use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_TEXTSIZE
|
|
|
|
|
// offset = x
|
2015-03-16 15:54:44 -04:00
|
|
|
// val = int32(y)
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
//
|
|
|
|
|
// reg<<shift, reg>>shift, reg->shift, reg@>shift
|
2016-08-10 13:24:03 -04:00
|
|
|
// Shifted register value, for ARM and ARM64.
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
// In this form, reg must be a register and shift can be a register or an integer constant.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_SHIFT
|
2016-08-10 13:24:03 -04:00
|
|
|
// On ARM:
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
// offset = (reg&15) | shifttype<<5 | count
|
|
|
|
|
// shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
|
|
|
|
|
// count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
|
2016-08-10 13:24:03 -04:00
|
|
|
// On ARM64:
|
|
|
|
|
// offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10
|
|
|
|
|
// shifttype = 0, 1, 2 for <<, >>, ->
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
//
|
|
|
|
|
// (reg, reg)
|
|
|
|
|
// A destination register pair. When used as the last argument of an instruction,
|
|
|
|
|
// this form makes clear that both registers are destinations.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_REGREG
|
|
|
|
|
// reg = first register
|
|
|
|
|
// offset = second register
|
|
|
|
|
//
|
2015-02-27 13:50:26 -08:00
|
|
|
// [reg, reg, reg-reg]
|
|
|
|
|
// Register list for ARM.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_REGLIST
|
|
|
|
|
// offset = bit mask of registers in list; R0 is low bit.
|
|
|
|
|
//
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
// reg, reg
|
2015-03-05 10:39:23 -08:00
|
|
|
// Register pair for ARM.
|
|
|
|
|
// TYPE_REGREG2
|
|
|
|
|
//
|
|
|
|
|
// (reg+reg)
|
|
|
|
|
// Register pair for PPC64.
|
|
|
|
|
// Encoding:
|
|
|
|
|
// type = TYPE_MEM
|
|
|
|
|
// reg = first register
|
|
|
|
|
// index = second register
|
|
|
|
|
// scale = 1
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
//
|
2015-03-16 15:31:32 -04:00
|
|
|
type Addr struct {
|
|
|
|
|
Reg int16
|
|
|
|
|
Index int16
|
|
|
|
|
Scale int16 // Sometimes holds a register.
|
2015-05-14 20:11:28 -07:00
|
|
|
Type AddrType
|
2015-03-16 15:31:32 -04:00
|
|
|
Name int8
|
|
|
|
|
Class int8
|
2015-03-16 15:54:44 -04:00
|
|
|
Offset int64
|
|
|
|
|
Sym *LSym
|
|
|
|
|
|
|
|
|
|
// argument value:
|
|
|
|
|
// for TYPE_SCONST, a string
|
|
|
|
|
// for TYPE_FCONST, a float64
|
|
|
|
|
// for TYPE_BRANCH, a *Prog (optional)
|
|
|
|
|
// for TYPE_TEXTSIZE, an int32 (optional)
|
|
|
|
|
Val interface{}
|
|
|
|
|
|
|
|
|
|
Node interface{} // for use by compiler
|
2015-03-16 15:31:32 -04:00
|
|
|
}
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
|
2015-05-14 20:11:28 -07:00
|
|
|
type AddrType uint8
|
|
|
|
|
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
const (
|
|
|
|
|
NAME_NONE = 0 + iota
|
|
|
|
|
NAME_EXTERN
|
|
|
|
|
NAME_STATIC
|
|
|
|
|
NAME_AUTO
|
|
|
|
|
NAME_PARAM
|
2015-03-30 00:49:25 +00:00
|
|
|
// A reference to name@GOT(SB) is a reference to the entry in the global offset
|
|
|
|
|
// table for 'name'.
|
|
|
|
|
NAME_GOTREF
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2015-05-14 20:11:28 -07:00
|
|
|
TYPE_NONE AddrType = 0
|
2015-03-05 14:04:12 -05:00
|
|
|
|
2015-05-14 20:11:28 -07:00
|
|
|
TYPE_BRANCH AddrType = 5 + iota
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
TYPE_TEXTSIZE
|
|
|
|
|
TYPE_MEM
|
|
|
|
|
TYPE_CONST
|
|
|
|
|
TYPE_FCONST
|
|
|
|
|
TYPE_SCONST
|
|
|
|
|
TYPE_REG
|
|
|
|
|
TYPE_ADDR
|
|
|
|
|
TYPE_SHIFT
|
|
|
|
|
TYPE_REGREG
|
|
|
|
|
TYPE_REGREG2
|
|
|
|
|
TYPE_INDIR
|
2015-02-27 13:50:26 -08:00
|
|
|
TYPE_REGLIST
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
)
|
|
|
|
|
|
2016-02-17 23:06:56 -08:00
|
|
|
// Prog describes a single machine instruction.
|
|
|
|
|
//
|
|
|
|
|
// The general instruction form is:
|
|
|
|
|
//
|
|
|
|
|
// As.Scond From, Reg, From3, To, RegTo2
|
|
|
|
|
//
|
|
|
|
|
// where As is an opcode and the others are arguments:
|
|
|
|
|
// From, Reg, From3 are sources, and To, RegTo2 are destinations.
|
|
|
|
|
// Usually, not all arguments are present.
|
|
|
|
|
// For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
|
|
|
|
|
// The Scond field holds additional condition bits for systems (like arm)
|
|
|
|
|
// that have generalized conditional execution.
|
|
|
|
|
//
|
|
|
|
|
// Jump instructions use the Pcond field to point to the target instruction,
|
|
|
|
|
// which must be in the same linked list as the jump instruction.
|
|
|
|
|
//
|
|
|
|
|
// The Progs for a given function are arranged in a list linked through the Link field.
|
|
|
|
|
//
|
|
|
|
|
// Each Prog is charged to a specific source line in the debug information,
|
2016-12-28 17:58:51 -08:00
|
|
|
// specified by Pos.Line().
|
|
|
|
|
// Every Prog has a Ctxt field that defines its context.
|
2016-02-17 23:06:56 -08:00
|
|
|
// Progs should be allocated using ctxt.NewProg(), not new(Prog).
|
|
|
|
|
//
|
|
|
|
|
// The other fields not yet mentioned are for use by the back ends and should
|
|
|
|
|
// be left zeroed by creators of Prog lists.
|
2015-03-16 15:31:32 -04:00
|
|
|
type Prog struct {
|
2016-02-17 23:06:56 -08:00
|
|
|
Ctxt *Link // linker context
|
|
|
|
|
Link *Prog // next Prog in linked list
|
|
|
|
|
From Addr // first source operand
|
|
|
|
|
From3 *Addr // third source operand (second is Reg below)
|
|
|
|
|
To Addr // destination operand (second is RegTo2 below)
|
|
|
|
|
Pcond *Prog // target of conditional jump
|
|
|
|
|
Opt interface{} // available to optimization passes to hold per-Prog state
|
|
|
|
|
Forwd *Prog // for x86 back end
|
|
|
|
|
Rel *Prog // for x86, arm back ends
|
|
|
|
|
Pc int64 // for back ends or assembler: virtual or actual program counter, depending on phase
|
2016-12-15 17:17:01 -08:00
|
|
|
Pos src.XPos // source position of this instruction
|
2016-02-17 23:06:56 -08:00
|
|
|
Spadj int32 // effect of instruction on stack pointer (increment or decrement amount)
|
|
|
|
|
As As // assembler opcode
|
|
|
|
|
Reg int16 // 2nd source operand
|
|
|
|
|
RegTo2 int16 // 2nd destination operand
|
|
|
|
|
Mark uint16 // bitmask of arch-specific items
|
|
|
|
|
Optab uint16 // arch-specific opcode index
|
|
|
|
|
Scond uint8 // condition bits for conditional instruction (e.g., on ARM)
|
|
|
|
|
Back uint8 // for x86 back end: backwards branch state
|
|
|
|
|
Ft uint8 // for x86 back end: type index of Prog.From
|
|
|
|
|
Tt uint8 // for x86 back end: type index of Prog.To
|
|
|
|
|
Isize uint8 // for x86 back end: size of the instruction in bytes
|
|
|
|
|
Mode int8 // for x86 back end: 32- or 64-bit mode
|
2015-03-16 16:46:25 -04:00
|
|
|
}
|
|
|
|
|
|
2015-05-27 15:01:44 -04:00
|
|
|
// From3Type returns From3.Type, or TYPE_NONE when From3 is nil.
|
2015-05-14 20:11:28 -07:00
|
|
|
func (p *Prog) From3Type() AddrType {
|
2015-05-27 15:01:44 -04:00
|
|
|
if p.From3 == nil {
|
|
|
|
|
return TYPE_NONE
|
|
|
|
|
}
|
|
|
|
|
return p.From3.Type
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 10:00:45 -07:00
|
|
|
// From3Offset returns From3.Offset, or 0 when From3 is nil.
|
|
|
|
|
func (p *Prog) From3Offset() int64 {
|
|
|
|
|
if p.From3 == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return p.From3.Offset
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 18:00:08 -08:00
|
|
|
// An As denotes an assembler opcode.
|
|
|
|
|
// There are some portable opcodes, declared here in package obj,
|
|
|
|
|
// that are common to all architectures.
|
|
|
|
|
// However, the majority of opcodes are arch-specific
|
|
|
|
|
// and are declared in their respective architecture's subpackage.
|
|
|
|
|
type As int16
|
|
|
|
|
|
|
|
|
|
// These are the portable opcodes.
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
const (
|
2016-03-07 18:00:08 -08:00
|
|
|
AXXX As = iota
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
ACALL
|
|
|
|
|
ADUFFCOPY
|
|
|
|
|
ADUFFZERO
|
|
|
|
|
AEND
|
|
|
|
|
AFUNCDATA
|
|
|
|
|
AJMP
|
|
|
|
|
ANOP
|
|
|
|
|
APCDATA
|
|
|
|
|
ARET
|
|
|
|
|
ATEXT
|
|
|
|
|
AUNDEF
|
|
|
|
|
AUSEFIELD
|
|
|
|
|
AVARDEF
|
|
|
|
|
AVARKILL
|
cmd/compile: recognize Syscall-like functions for liveness analysis
Consider this code:
func f(*int)
func g() {
p := new(int)
f(p)
}
where f is an assembly function.
In general liveness analysis assumes that during the call to f, p is dead
in this frame. If f has retained p, p will be found alive in f's frame and keep
the new(int) from being garbage collected. This is all correct and works.
We use the Go func declaration for f to give the assembly function
liveness information (the arguments are assumed live for the entire call).
Now consider this code:
func h1() {
p := new(int)
syscall.Syscall(1, 2, 3, uintptr(unsafe.Pointer(p)))
}
Here syscall.Syscall is taking the place of f, but because its arguments
are uintptr, the liveness analysis and the garbage collector ignore them.
Since p is no longer live in h once the call starts, if the garbage collector
scans the stack while the system call is blocked, it will find no reference
to the new(int) and reclaim it. If the kernel is going to write to *p once
the call finishes, reclaiming the memory is a mistake.
We can't change the arguments or the liveness information for
syscall.Syscall itself, both for compatibility and because sometimes the
arguments really are integers, and the garbage collector will get quite upset
if it finds an integer where it expects a pointer. The problem is that
these arguments are fundamentally untyped.
The solution we have taken in the syscall package's wrappers in past
releases is to insert a call to a dummy function named "use", to make
it look like the argument is live during the call to syscall.Syscall:
func h2() {
p := new(int)
syscall.Syscall(1, 2, 3, uintptr(unsafe.Pointer(p)))
use(unsafe.Pointer(p))
}
Keeping p alive during the call means that if the garbage collector
scans the stack during the system call now, it will find the reference to p.
Unfortunately, this approach is not available to users outside syscall,
because 'use' is unexported, and people also have to realize they need
to use it and do so. There is much existing code using syscall.Syscall
without a 'use'-like function. That code will fail very occasionally in
mysterious ways (see #13372).
This CL fixes all that existing code by making the compiler do the right
thing automatically, without any code modifications. That is, it takes h1
above, which is incorrect code today, and makes it correct code.
Specifically, if the compiler sees a foreign func definition (one
without a body) that has uintptr arguments, it marks those arguments
as "unsafe uintptrs". If it later sees the function being called
with uintptr(unsafe.Pointer(x)) as an argument, it arranges to mark x
as having escaped, and it makes sure to hold x in a live temporary
variable until the call returns, so that the garbage collector cannot
reclaim whatever heap memory x points to.
For now I am leaving the explicit calls to use in package syscall,
but they can be removed early in a future cycle (likely Go 1.7).
The rule has no effect on escape analysis, only on liveness analysis.
Fixes #13372.
Change-Id: I2addb83f70d08db08c64d394f9d06ff0a063c500
Reviewed-on: https://go-review.googlesource.com/18584
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-01-13 00:46:28 -05:00
|
|
|
AVARLIVE
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
A_ARCHSPECIFIC
|
|
|
|
|
)
|
2015-01-19 14:34:58 -05:00
|
|
|
|
2016-03-07 18:00:08 -08:00
|
|
|
// Each architecture is allotted a distinct subspace of opcode values
|
|
|
|
|
// for declaring its arch-specific opcodes.
|
|
|
|
|
// Within this subspace, the first arch-specific opcode should be
|
|
|
|
|
// at offset A_ARCHSPECIFIC.
|
|
|
|
|
//
|
|
|
|
|
// Subspaces are aligned to a power of two so opcodes can be masked
|
|
|
|
|
// with AMask and used as compact array indices.
|
|
|
|
|
const (
|
2016-06-17 12:30:27 -07:00
|
|
|
ABase386 = (1 + iota) << 10
|
2016-03-07 18:00:08 -08:00
|
|
|
ABaseARM
|
|
|
|
|
ABaseAMD64
|
|
|
|
|
ABasePPC64
|
|
|
|
|
ABaseARM64
|
2016-10-18 23:50:37 +02:00
|
|
|
ABaseMIPS
|
2016-03-18 16:30:29 -04:00
|
|
|
ABaseS390X
|
2016-03-07 18:00:08 -08:00
|
|
|
|
2016-06-17 12:30:27 -07:00
|
|
|
AllowedOpCodes = 1 << 10 // The number of opcodes available for any given architecture.
|
2016-06-17 12:28:31 -07:00
|
|
|
AMask = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
|
2016-03-07 18:00:08 -08:00
|
|
|
)
|
|
|
|
|
|
2015-04-18 08:14:08 +12:00
|
|
|
// An LSym is the sort of symbol that is written to an object file.
|
2015-03-16 15:31:32 -04:00
|
|
|
type LSym struct {
|
2016-10-24 23:15:41 +03:00
|
|
|
Name string
|
|
|
|
|
Type SymKind
|
|
|
|
|
Version int16
|
|
|
|
|
Attribute
|
|
|
|
|
|
|
|
|
|
RefIdx int // Index of this symbol in the symbol reference list.
|
|
|
|
|
Args int32
|
|
|
|
|
Locals int32
|
|
|
|
|
Size int64
|
|
|
|
|
Gotype *LSym
|
|
|
|
|
Autom *Auto
|
|
|
|
|
Text *Prog
|
|
|
|
|
Pcln *Pcln
|
|
|
|
|
P []byte
|
|
|
|
|
R []Reloc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attribute is a set of symbol attributes.
|
|
|
|
|
type Attribute int16
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
AttrDuplicateOK Attribute = 1 << iota
|
|
|
|
|
AttrCFunc
|
|
|
|
|
AttrNoSplit
|
|
|
|
|
AttrLeaf
|
|
|
|
|
AttrSeenGlobl
|
|
|
|
|
AttrOnList
|
2016-03-10 16:15:26 -05:00
|
|
|
|
2016-10-19 07:33:16 +03:00
|
|
|
// MakeTypelink means that the type should have an entry in the typelink table.
|
2016-10-24 23:15:41 +03:00
|
|
|
AttrMakeTypelink
|
2016-10-19 07:33:16 +03:00
|
|
|
|
2016-03-10 16:15:26 -05:00
|
|
|
// ReflectMethod means the function may call reflect.Type.Method or
|
|
|
|
|
// reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
|
|
|
|
|
// can be used through a custom interface), so ReflectMethod may be
|
|
|
|
|
// set in some cases when the reflect package is not called.
|
|
|
|
|
//
|
|
|
|
|
// Used by the linker to determine what methods can be pruned.
|
2016-10-24 23:15:41 +03:00
|
|
|
AttrReflectMethod
|
2016-03-10 16:15:26 -05:00
|
|
|
|
2015-04-18 08:14:08 +12:00
|
|
|
// Local means make the symbol local even when compiling Go code to reference Go
|
|
|
|
|
// symbols in other shared libraries, as in this mode symbols are global by
|
|
|
|
|
// default. "local" here means in the sense of the dynamic linker, i.e. not
|
|
|
|
|
// visible outside of the module (shared library or executable) that contains its
|
|
|
|
|
// definition. (When not compiling to support Go shared libraries, all symbols are
|
|
|
|
|
// local in this sense unless there is a cgo_export_* directive).
|
2016-10-24 23:15:41 +03:00
|
|
|
AttrLocal
|
|
|
|
|
)
|
2016-03-16 12:41:55 -07:00
|
|
|
|
2016-10-24 23:15:41 +03:00
|
|
|
func (a Attribute) DuplicateOK() bool { return a&AttrDuplicateOK != 0 }
|
|
|
|
|
func (a Attribute) MakeTypelink() bool { return a&AttrMakeTypelink != 0 }
|
|
|
|
|
func (a Attribute) CFunc() bool { return a&AttrCFunc != 0 }
|
|
|
|
|
func (a Attribute) NoSplit() bool { return a&AttrNoSplit != 0 }
|
|
|
|
|
func (a Attribute) Leaf() bool { return a&AttrLeaf != 0 }
|
|
|
|
|
func (a Attribute) SeenGlobl() bool { return a&AttrSeenGlobl != 0 }
|
|
|
|
|
func (a Attribute) OnList() bool { return a&AttrOnList != 0 }
|
|
|
|
|
func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 }
|
|
|
|
|
func (a Attribute) Local() bool { return a&AttrLocal != 0 }
|
|
|
|
|
|
|
|
|
|
func (a *Attribute) Set(flag Attribute, value bool) {
|
|
|
|
|
if value {
|
|
|
|
|
*a |= flag
|
|
|
|
|
} else {
|
|
|
|
|
*a &^= flag
|
|
|
|
|
}
|
2015-03-16 15:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-16 22:22:58 -07:00
|
|
|
// The compiler needs LSym to satisfy fmt.Stringer, because it stores
|
|
|
|
|
// an LSym in ssa.ExternSymbol.
|
|
|
|
|
func (s *LSym) String() string {
|
|
|
|
|
return s.Name
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 15:31:32 -04:00
|
|
|
type Pcln struct {
|
|
|
|
|
Pcsp Pcdata
|
|
|
|
|
Pcfile Pcdata
|
|
|
|
|
Pcline Pcdata
|
|
|
|
|
Pcdata []Pcdata
|
|
|
|
|
Funcdata []*LSym
|
|
|
|
|
Funcdataoff []int64
|
|
|
|
|
File []*LSym
|
|
|
|
|
Lastfile *LSym
|
|
|
|
|
Lastindex int
|
|
|
|
|
}
|
2015-01-19 14:34:58 -05:00
|
|
|
|
2016-09-07 14:45:27 -04:00
|
|
|
// A SymKind describes the kind of memory represented by a symbol.
|
|
|
|
|
type SymKind int16
|
|
|
|
|
|
|
|
|
|
// Defined SymKind values.
|
|
|
|
|
//
|
|
|
|
|
// TODO(rsc): Give idiomatic Go names.
|
|
|
|
|
// TODO(rsc): Reduce the number of symbol types in the object files.
|
|
|
|
|
//go:generate stringer -type=SymKind
|
2015-01-19 14:34:58 -05:00
|
|
|
const (
|
2016-09-07 14:45:27 -04:00
|
|
|
Sxxx SymKind = iota
|
2015-01-19 14:34:58 -05:00
|
|
|
STEXT
|
|
|
|
|
SELFRXSECT
|
2015-05-21 13:07:19 +12:00
|
|
|
|
2016-09-05 23:29:16 -04:00
|
|
|
// Read-only sections.
|
2015-01-19 14:34:58 -05:00
|
|
|
STYPE
|
|
|
|
|
SSTRING
|
|
|
|
|
SGOSTRING
|
|
|
|
|
SGOFUNC
|
2015-07-22 14:59:56 -04:00
|
|
|
SGCBITS
|
2015-01-19 14:34:58 -05:00
|
|
|
SRODATA
|
|
|
|
|
SFUNCTAB
|
2015-05-21 13:07:19 +12:00
|
|
|
|
2016-09-05 23:29:16 -04:00
|
|
|
SELFROSECT
|
|
|
|
|
SMACHOPLT
|
|
|
|
|
|
|
|
|
|
// Read-only sections with relocations.
|
|
|
|
|
//
|
2015-05-21 13:07:19 +12:00
|
|
|
// Types STYPE-SFUNCTAB above are written to the .rodata section by default.
|
|
|
|
|
// When linking a shared object, some conceptually "read only" types need to
|
|
|
|
|
// be written to by relocations and putting them in a section called
|
|
|
|
|
// ".rodata" interacts poorly with the system linkers. The GNU linkers
|
|
|
|
|
// support this situation by arranging for sections of the name
|
|
|
|
|
// ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after
|
|
|
|
|
// relocations have applied, so when the Go linker is creating a shared
|
|
|
|
|
// object it checks all objects of the above types and bumps any object that
|
|
|
|
|
// has a relocation to it to the corresponding type below, which are then
|
|
|
|
|
// written to sections with appropriate magic names.
|
|
|
|
|
STYPERELRO
|
|
|
|
|
SSTRINGRELRO
|
|
|
|
|
SGOSTRINGRELRO
|
|
|
|
|
SGOFUNCRELRO
|
|
|
|
|
SGCBITSRELRO
|
|
|
|
|
SRODATARELRO
|
|
|
|
|
SFUNCTABRELRO
|
|
|
|
|
|
2016-09-05 23:29:16 -04:00
|
|
|
// Part of .data.rel.ro if it exists, otherwise part of .rodata.
|
2015-01-19 14:34:58 -05:00
|
|
|
STYPELINK
|
2016-03-17 07:00:33 -07:00
|
|
|
SITABLINK
|
2015-01-19 14:34:58 -05:00
|
|
|
SSYMTAB
|
|
|
|
|
SPCLNTAB
|
2016-09-05 23:29:16 -04:00
|
|
|
|
|
|
|
|
// Writable sections.
|
2015-01-19 14:34:58 -05:00
|
|
|
SELFSECT
|
|
|
|
|
SMACHO
|
|
|
|
|
SMACHOGOT
|
|
|
|
|
SWINDOWS
|
|
|
|
|
SELFGOT
|
|
|
|
|
SNOPTRDATA
|
|
|
|
|
SINITARR
|
|
|
|
|
SDATA
|
|
|
|
|
SBSS
|
|
|
|
|
SNOPTRBSS
|
|
|
|
|
STLSBSS
|
|
|
|
|
SXREF
|
|
|
|
|
SMACHOSYMSTR
|
|
|
|
|
SMACHOSYMTAB
|
|
|
|
|
SMACHOINDIRECTPLT
|
|
|
|
|
SMACHOINDIRECTGOT
|
|
|
|
|
SFILE
|
|
|
|
|
SFILEPATH
|
|
|
|
|
SCONST
|
|
|
|
|
SDYNIMPORT
|
|
|
|
|
SHOSTOBJ
|
2016-03-14 09:23:04 -07:00
|
|
|
SDWARFSECT
|
|
|
|
|
SDWARFINFO
|
2016-09-07 14:45:27 -04:00
|
|
|
SSUB = SymKind(1 << 8)
|
|
|
|
|
SMASK = SymKind(SSUB - 1)
|
|
|
|
|
SHIDDEN = SymKind(1 << 9)
|
|
|
|
|
SCONTAINER = SymKind(1 << 10) // has a sub-symbol
|
2015-01-19 14:34:58 -05:00
|
|
|
)
|
|
|
|
|
|
2016-09-07 14:45:27 -04:00
|
|
|
// ReadOnly are the symbol kinds that form read-only sections. In some
|
|
|
|
|
// cases, if they will require relocations, they are transformed into
|
|
|
|
|
// rel-ro sections using RelROMap.
|
|
|
|
|
var ReadOnly = []SymKind{
|
|
|
|
|
STYPE,
|
|
|
|
|
SSTRING,
|
|
|
|
|
SGOSTRING,
|
|
|
|
|
SGOFUNC,
|
|
|
|
|
SGCBITS,
|
|
|
|
|
SRODATA,
|
|
|
|
|
SFUNCTAB,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RelROMap describes the transformation of read-only symbols to rel-ro
|
|
|
|
|
// symbols.
|
|
|
|
|
var RelROMap = map[SymKind]SymKind{
|
2016-10-13 22:31:46 +03:00
|
|
|
STYPE: STYPERELRO,
|
|
|
|
|
SSTRING: SSTRINGRELRO,
|
|
|
|
|
SGOSTRING: SGOSTRINGRELRO,
|
|
|
|
|
SGOFUNC: SGOFUNCRELRO,
|
|
|
|
|
SGCBITS: SGCBITSRELRO,
|
|
|
|
|
SRODATA: SRODATARELRO,
|
|
|
|
|
SFUNCTAB: SFUNCTABRELRO,
|
2016-09-07 14:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
2015-03-16 15:31:32 -04:00
|
|
|
type Reloc struct {
|
2015-03-17 16:28:30 +13:00
|
|
|
Off int32
|
|
|
|
|
Siz uint8
|
2016-07-02 17:19:25 -07:00
|
|
|
Type RelocType
|
2015-03-17 16:28:30 +13:00
|
|
|
Add int64
|
|
|
|
|
Sym *LSym
|
2015-03-16 15:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-02 17:19:25 -07:00
|
|
|
type RelocType int32
|
|
|
|
|
|
|
|
|
|
//go:generate stringer -type=RelocType
|
2015-01-19 14:34:58 -05:00
|
|
|
const (
|
2016-07-02 17:19:25 -07:00
|
|
|
R_ADDR RelocType = 1 + iota
|
2015-09-08 15:21:58 +12:00
|
|
|
// R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit
|
|
|
|
|
// immediates in the low half of the instruction word), usually addis followed by
|
|
|
|
|
// another add or a load, inserting the "high adjusted" 16 bits of the address of
|
|
|
|
|
// the referenced symbol into the immediate field of the first instruction and the
|
|
|
|
|
// low 16 bits into that of the second instruction.
|
2015-01-19 14:34:58 -05:00
|
|
|
R_ADDRPOWER
|
2015-08-27 21:09:46 +12:00
|
|
|
// R_ADDRARM64 relocates an adrp, add pair to compute the address of the
|
|
|
|
|
// referenced symbol.
|
2015-04-03 04:37:18 -04:00
|
|
|
R_ADDRARM64
|
2016-10-18 23:50:37 +02:00
|
|
|
// R_ADDRMIPS (only used on mips/mips64) resolves to the low 16 bits of an external
|
2016-04-27 22:18:02 -04:00
|
|
|
// address, by encoding it into the instruction.
|
2015-09-10 11:13:00 -04:00
|
|
|
R_ADDRMIPS
|
cmd/compile, etc: store method tables as offsets
This CL introduces the typeOff type and a lookup method of the same
name that can turn a typeOff offset into an *rtype.
In a typical Go binary (built with buildmode=exe, pie, c-archive, or
c-shared), there is one moduledata and all typeOff values are offsets
relative to firstmoduledata.types. This makes computing the pointer
cheap in typical programs.
With buildmode=shared (and one day, buildmode=plugin) there are
multiple modules whose relative offset is determined at runtime.
We identify a type in the general case by the pair of the original
*rtype that references it and its typeOff value. We determine
the module from the original pointer, and then use the typeOff from
there to compute the final *rtype.
To ensure there is only one *rtype representing each type, the
runtime initializes a typemap for each module, using any identical
type from an earlier module when resolving that offset. This means
that types computed from an offset match the type mapped by the
pointer dynamic relocations.
A series of followup CLs will replace other *rtype values with typeOff
(and name/*string with nameOff).
For types created at runtime by reflect, type offsets are treated as
global IDs and reference into a reflect offset map kept by the runtime.
darwin/amd64:
cmd/go: -57KB (0.6%)
jujud: -557KB (0.8%)
linux/amd64 PIE:
cmd/go: -361KB (3.0%)
jujud: -3.5MB (4.2%)
For #6853.
Change-Id: Icf096fd884a0a0cb9f280f46f7a26c70a9006c96
Reviewed-on: https://go-review.googlesource.com/21285
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-28 10:32:27 -04:00
|
|
|
// R_ADDROFF resolves to a 32-bit offset from the beginning of the section
|
|
|
|
|
// holding the data being relocated to the referenced symbol.
|
2016-03-27 10:21:48 -04:00
|
|
|
R_ADDROFF
|
2016-11-21 16:58:55 -05:00
|
|
|
// R_WEAKADDROFF resolves just like R_ADDROFF but is a weak relocation.
|
|
|
|
|
// A weak relocation does not make the symbol it refers to reachable,
|
|
|
|
|
// and is only honored by the linker if the symbol is in some other way
|
|
|
|
|
// reachable.
|
|
|
|
|
R_WEAKADDROFF
|
2015-01-19 14:34:58 -05:00
|
|
|
R_SIZE
|
|
|
|
|
R_CALL
|
|
|
|
|
R_CALLARM
|
cmd/internal/obj, cmd/internal/obj/arm64: add support for GOARCH=arm64
ARM64 (ARMv8) has 32 general purpose, 64-bit integer registers
(R0-R31), 32 64-bit scalar floating point registers (F0-F31), and
32 128-bit vector registers (unused, V0-V31).
R31 is either the stack pointer (RSP), or the zero register (ZR),
depending on the instruction. Note the distinction between the
hardware stack pointer, RSP, and the virtual stack pointer SP.
The (hardware) stack pointer must be 16-byte aligned at all times;
the RSP register itself must be aligned, offset(RSP) only has to
have natural alignment.
Instructions are fixed-width, and are 32-bit wide. ARM64 supports
ARMv7 too (32-bit ARM), but not in the same process. In general,
there is not much in common between 32-bit ARM and ARM64, it's a
new architecture.
All implementations have floating point instructions.
This change adds a Prog.To3 field analogous to Prog.To. It is used
by exclusive load/store instructions such as STLXR which read from
one register, and write to both a register and a memory address.
STLXRW R1, (R0), R3
This will store the word contained in R1 to the memory address
pointed by R0. R3 will be updated with the status result of the
store. It is used to implement atomic operations.
No other changes are made to the portable Prog and Addr structures.
Change-Id: Ie839029aa5265bbad35769d9689eca11e1c48c47
Reviewed-on: https://go-review.googlesource.com/7046
Reviewed-by: Russ Cox <rsc@golang.org>
2015-03-08 13:58:16 +01:00
|
|
|
R_CALLARM64
|
2015-01-19 14:34:58 -05:00
|
|
|
R_CALLIND
|
|
|
|
|
R_CALLPOWER
|
2015-09-10 11:13:00 -04:00
|
|
|
// R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address
|
|
|
|
|
// of a CALL (JAL) instruction, by encoding the address into the instruction.
|
|
|
|
|
R_CALLMIPS
|
2015-01-19 14:34:58 -05:00
|
|
|
R_CONST
|
|
|
|
|
R_PCREL
|
2015-09-02 11:23:15 +12:00
|
|
|
// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
|
|
|
|
|
// thread-local symbol from the thread local base and is used to implement the
|
|
|
|
|
// "local exec" model for tls access (r.Sym is not set on intel platforms but is
|
|
|
|
|
// set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking).
|
2015-01-19 14:34:58 -05:00
|
|
|
R_TLS_LE
|
2015-09-02 11:23:15 +12:00
|
|
|
// R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT
|
|
|
|
|
// slot containing the offset from the thread-local symbol from the thread local
|
|
|
|
|
// base and is used to implemented the "initial exec" model for tls access (r.Sym
|
|
|
|
|
// is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in
|
|
|
|
|
// the linker when externally linking).
|
2015-01-19 14:34:58 -05:00
|
|
|
R_TLS_IE
|
|
|
|
|
R_GOTOFF
|
|
|
|
|
R_PLT0
|
|
|
|
|
R_PLT1
|
|
|
|
|
R_PLT2
|
|
|
|
|
R_USEFIELD
|
2016-02-18 06:31:57 -05:00
|
|
|
// R_USETYPE resolves to an *rtype, but no relocation is created. The
|
|
|
|
|
// linker uses this as a signal that the pointed-to type information
|
|
|
|
|
// should be linked into the final binary, even if there are no other
|
|
|
|
|
// direct references. (This is used for types reachable by reflection.)
|
|
|
|
|
R_USETYPE
|
cmd/compile, etc: store method tables as offsets
This CL introduces the typeOff type and a lookup method of the same
name that can turn a typeOff offset into an *rtype.
In a typical Go binary (built with buildmode=exe, pie, c-archive, or
c-shared), there is one moduledata and all typeOff values are offsets
relative to firstmoduledata.types. This makes computing the pointer
cheap in typical programs.
With buildmode=shared (and one day, buildmode=plugin) there are
multiple modules whose relative offset is determined at runtime.
We identify a type in the general case by the pair of the original
*rtype that references it and its typeOff value. We determine
the module from the original pointer, and then use the typeOff from
there to compute the final *rtype.
To ensure there is only one *rtype representing each type, the
runtime initializes a typemap for each module, using any identical
type from an earlier module when resolving that offset. This means
that types computed from an offset match the type mapped by the
pointer dynamic relocations.
A series of followup CLs will replace other *rtype values with typeOff
(and name/*string with nameOff).
For types created at runtime by reflect, type offsets are treated as
global IDs and reference into a reflect offset map kept by the runtime.
darwin/amd64:
cmd/go: -57KB (0.6%)
jujud: -557KB (0.8%)
linux/amd64 PIE:
cmd/go: -361KB (3.0%)
jujud: -3.5MB (4.2%)
For #6853.
Change-Id: Icf096fd884a0a0cb9f280f46f7a26c70a9006c96
Reviewed-on: https://go-review.googlesource.com/21285
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-28 10:32:27 -04:00
|
|
|
// R_METHODOFF resolves to a 32-bit offset from the beginning of the section
|
|
|
|
|
// holding the data being relocated to the referenced symbol.
|
|
|
|
|
// It is a variant of R_ADDROFF used when linking from the uncommonType of a
|
|
|
|
|
// *rtype, and may be set to zero by the linker if it determines the method
|
|
|
|
|
// text is unreachable by the linked program.
|
|
|
|
|
R_METHODOFF
|
2015-01-19 14:34:58 -05:00
|
|
|
R_POWER_TOC
|
2015-03-30 00:49:25 +00:00
|
|
|
R_GOTPCREL
|
2015-09-10 11:13:00 -04:00
|
|
|
// R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address
|
|
|
|
|
// of a JMP instruction, by encoding the address into the instruction.
|
|
|
|
|
// The stack nosplit check ignores this since it is not a function call.
|
|
|
|
|
R_JMPMIPS
|
2016-03-14 09:23:04 -07:00
|
|
|
// R_DWARFREF resolves to the offset of the symbol from its section.
|
|
|
|
|
R_DWARFREF
|
2015-08-11 14:10:03 +12:00
|
|
|
|
|
|
|
|
// Platform dependent relocations. Architectures with fixed width instructions
|
|
|
|
|
// have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be
|
|
|
|
|
// stuffed into a 32-bit instruction, so an address needs to be spread across
|
|
|
|
|
// several instructions, and in turn this requires a sequence of relocations, each
|
2016-03-01 23:21:55 +00:00
|
|
|
// updating a part of an instruction. This leads to relocation codes that are
|
2015-08-11 14:10:03 +12:00
|
|
|
// inherently processor specific.
|
|
|
|
|
|
|
|
|
|
// Arm64.
|
|
|
|
|
|
|
|
|
|
// Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread
|
|
|
|
|
// local base to the thread local variable defined by the referenced (thread
|
|
|
|
|
// local) symbol. Error if the offset does not fit into 16 bits.
|
|
|
|
|
R_ARM64_TLS_LE
|
2015-10-28 15:27:51 +13:00
|
|
|
|
|
|
|
|
// Relocates an ADRP; LD64 instruction sequence to load the offset between
|
|
|
|
|
// the thread local base and the thread local variable defined by the
|
|
|
|
|
// referenced (thread local) symbol from the GOT.
|
|
|
|
|
R_ARM64_TLS_IE
|
2015-10-30 10:13:13 +13:00
|
|
|
|
2015-08-27 21:09:46 +12:00
|
|
|
// R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT
|
|
|
|
|
// slot of the referenced symbol.
|
|
|
|
|
R_ARM64_GOTPCREL
|
|
|
|
|
|
2015-10-30 10:13:13 +13:00
|
|
|
// PPC64.
|
|
|
|
|
|
|
|
|
|
// R_POWER_TLS_LE is used to implement the "local exec" model for tls
|
|
|
|
|
// access. It resolves to the offset of the thread-local symbol from the
|
|
|
|
|
// thread pointer (R13) and inserts this value into the low 16 bits of an
|
|
|
|
|
// instruction word.
|
|
|
|
|
R_POWER_TLS_LE
|
2015-09-08 15:21:58 +12:00
|
|
|
|
2015-10-16 21:15:18 +13:00
|
|
|
// R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It
|
|
|
|
|
// relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It
|
|
|
|
|
// inserts to the offset of GOT slot for the thread-local symbol from the TOC (the
|
|
|
|
|
// GOT slot is filled by the dynamic linker with the offset of the thread-local
|
|
|
|
|
// symbol from the thread pointer (R13)).
|
|
|
|
|
R_POWER_TLS_IE
|
|
|
|
|
|
|
|
|
|
// R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as
|
|
|
|
|
// accessing a particular thread-local symbol. It does not affect code generation
|
|
|
|
|
// but is used by the system linker when relaxing "initial exec" model code to
|
|
|
|
|
// "local exec" model code.
|
|
|
|
|
R_POWER_TLS
|
|
|
|
|
|
2015-09-08 15:21:58 +12:00
|
|
|
// R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second
|
|
|
|
|
// instruction is a "DS-form" instruction, which has an immediate field occupying
|
|
|
|
|
// bits [15:2] of the instruction word. Bits [15:2] of the address of the
|
|
|
|
|
// relocated symbol are inserted into this field; it is an error if the last two
|
|
|
|
|
// bits of the address are not 0.
|
|
|
|
|
R_ADDRPOWER_DS
|
cmd/compile, cmd/link, runtime: on ppc64x, maintain the TOC pointer in R2 when compiling PIC
The PowerPC ISA does not have a PC-relative load instruction, which poses
obvious challenges when generating position-independent code. The way the ELFv2
ABI addresses this is to specify that r2 points to a per "module" (shared
library or executable) TOC pointer. Maintaining this pointer requires
cooperation between codegen and the system linker:
* Non-leaf functions leave space on the stack at r1+24 to save the TOC pointer.
* A call to a function that *might* have to go via a PLT stub must be followed
by a nop instruction that the system linker can replace with "ld r1, 24(r1)"
to restore the TOC pointer (only when dynamically linking Go code).
* When calling a function via a function pointer, the address of the function
must be in r12, and the first couple of instructions (the "global entry
point") of the called function use this to derive the address of the TOC
for the module it is in.
* When calling a function that is implemented in the same module, the system
linker adjusts the call to skip over the instructions mentioned above (the
"local entry point"), assuming that r2 is already correctly set.
So this changeset adds the global entry point instructions, sets the metadata so
the system linker knows where the local entry point is, inserts code to save the
TOC pointer at 24(r1), adds a nop after any call not known to be local and copes
with the odd non-local code transfer in the runtime (e.g. the stuff around
jmpdefer). It does not actually compile PIC yet.
Change-Id: I7522e22bdfd2f891745a900c60254fe9e372c854
Reviewed-on: https://go-review.googlesource.com/15967
Reviewed-by: Russ Cox <rsc@golang.org>
2015-10-16 15:42:09 +13:00
|
|
|
|
2015-10-16 15:54:10 +13:00
|
|
|
// R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like
|
|
|
|
|
// R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol
|
|
|
|
|
// from the TOC rather than the symbol's address.
|
|
|
|
|
R_ADDRPOWER_GOT
|
|
|
|
|
|
cmd/compile, cmd/link, runtime: on ppc64x, maintain the TOC pointer in R2 when compiling PIC
The PowerPC ISA does not have a PC-relative load instruction, which poses
obvious challenges when generating position-independent code. The way the ELFv2
ABI addresses this is to specify that r2 points to a per "module" (shared
library or executable) TOC pointer. Maintaining this pointer requires
cooperation between codegen and the system linker:
* Non-leaf functions leave space on the stack at r1+24 to save the TOC pointer.
* A call to a function that *might* have to go via a PLT stub must be followed
by a nop instruction that the system linker can replace with "ld r1, 24(r1)"
to restore the TOC pointer (only when dynamically linking Go code).
* When calling a function via a function pointer, the address of the function
must be in r12, and the first couple of instructions (the "global entry
point") of the called function use this to derive the address of the TOC
for the module it is in.
* When calling a function that is implemented in the same module, the system
linker adjusts the call to skip over the instructions mentioned above (the
"local entry point"), assuming that r2 is already correctly set.
So this changeset adds the global entry point instructions, sets the metadata so
the system linker knows where the local entry point is, inserts code to save the
TOC pointer at 24(r1), adds a nop after any call not known to be local and copes
with the odd non-local code transfer in the runtime (e.g. the stuff around
jmpdefer). It does not actually compile PIC yet.
Change-Id: I7522e22bdfd2f891745a900c60254fe9e372c854
Reviewed-on: https://go-review.googlesource.com/15967
Reviewed-by: Russ Cox <rsc@golang.org>
2015-10-16 15:42:09 +13:00
|
|
|
// R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but
|
|
|
|
|
// inserts the displacement from the place being relocated to the address of the
|
|
|
|
|
// the relocated symbol instead of just its address.
|
|
|
|
|
R_ADDRPOWER_PCREL
|
2015-10-16 20:48:56 +13:00
|
|
|
|
|
|
|
|
// R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but
|
|
|
|
|
// inserts the offset from the TOC to the address of the the relocated symbol
|
|
|
|
|
// rather than the symbol's address.
|
|
|
|
|
R_ADDRPOWER_TOCREL
|
|
|
|
|
|
|
|
|
|
// R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like
|
|
|
|
|
// R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the
|
|
|
|
|
// relocated symbol rather than the symbol's address.
|
|
|
|
|
R_ADDRPOWER_TOCREL_DS
|
2016-03-18 16:30:29 -04:00
|
|
|
|
|
|
|
|
// R_PCRELDBL relocates s390x 2-byte aligned PC-relative addresses.
|
|
|
|
|
// TODO(mundaym): remove once variants can be serialized - see issue 14218.
|
|
|
|
|
R_PCRELDBL
|
2016-04-27 22:18:02 -04:00
|
|
|
|
2016-10-18 23:50:37 +02:00
|
|
|
// R_ADDRMIPSU (only used on mips/mips64) resolves to the sign-adjusted "upper" 16
|
2016-04-27 22:18:02 -04:00
|
|
|
// bits (bit 16-31) of an external address, by encoding it into the instruction.
|
|
|
|
|
R_ADDRMIPSU
|
2016-04-27 22:18:14 -04:00
|
|
|
// R_ADDRMIPSTLS (only used on mips64) resolves to the low 16 bits of a TLS
|
|
|
|
|
// address (offset from thread pointer), by encoding it into the instruction.
|
|
|
|
|
R_ADDRMIPSTLS
|
2015-01-19 14:34:58 -05:00
|
|
|
)
|
|
|
|
|
|
2016-09-14 14:47:12 -04:00
|
|
|
// IsDirectJump returns whether r is a relocation for a direct jump.
|
|
|
|
|
// A direct jump is a CALL or JMP instruction that takes the target address
|
|
|
|
|
// as immediate. The address is embedded into the instruction, possibly
|
|
|
|
|
// with limited width.
|
|
|
|
|
// An indirect jump is a CALL or JMP instruction that takes the target address
|
|
|
|
|
// in register or memory.
|
|
|
|
|
func (r RelocType) IsDirectJump() bool {
|
|
|
|
|
switch r {
|
|
|
|
|
case R_CALL, R_CALLARM, R_CALLARM64, R_CALLPOWER, R_CALLMIPS, R_JMPMIPS:
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 15:31:32 -04:00
|
|
|
type Auto struct {
|
|
|
|
|
Asym *LSym
|
|
|
|
|
Link *Auto
|
|
|
|
|
Aoffset int32
|
|
|
|
|
Name int16
|
|
|
|
|
Gotype *LSym
|
|
|
|
|
}
|
|
|
|
|
|
[dev.cc] cmd/internal/obj: reconvert from liblink
cmd/internal/obj reconverted using rsc.io/c2go rev 2a95256.
- Brings in new, more regular Prog, Addr definitions
- Add Prog* argument to oclass in liblink/asm[68].c, for c2go conversion.
- Update objwriter for change in TEXT size encoding.
- Merge 5a, 6a, 8a, 9a changes into new5a, new6a, new8a, new9a (by hand).
- Add +build ignore to cmd/asm/internal/{addr,arch,asm}, cmd/asm.
They need to be updated for the changes.
- Reenable verifyAsm in cmd/go.
- Reenable GOOBJ=2 mode by default in liblink.
All architectures build successfully again.
Change-Id: I2c845c5d365aa484b570476898171bee657b626d
Reviewed-on: https://go-review.googlesource.com/3963
Reviewed-by: Rob Pike <r@golang.org>
2015-02-05 03:57:44 -05:00
|
|
|
// Auto.name
|
2015-01-19 14:34:58 -05:00
|
|
|
const (
|
|
|
|
|
A_AUTO = 1 + iota
|
|
|
|
|
A_PARAM
|
|
|
|
|
)
|
|
|
|
|
|
2015-03-16 15:31:32 -04:00
|
|
|
type Pcdata struct {
|
|
|
|
|
P []byte
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-19 14:34:58 -05:00
|
|
|
// symbol version, incremented each time a file is loaded.
|
|
|
|
|
// version==1 is reserved for savehist.
|
|
|
|
|
const (
|
|
|
|
|
HistVersion = 1
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Link holds the context for writing object code from a compiler
|
|
|
|
|
// to be linker input or for reading that input into the linker.
|
2015-03-16 15:31:32 -04:00
|
|
|
type Link struct {
|
2016-09-09 06:20:44 -04:00
|
|
|
Headtype HeadType
|
2016-03-13 11:54:14 -07:00
|
|
|
Arch *LinkArch
|
|
|
|
|
Debugasm int32
|
|
|
|
|
Debugvlog int32
|
|
|
|
|
Debugdivmod int32
|
|
|
|
|
Debugpcln int32
|
2016-04-13 18:41:59 -07:00
|
|
|
Flag_shared bool
|
2016-03-13 11:54:14 -07:00
|
|
|
Flag_dynlink bool
|
|
|
|
|
Flag_optimize bool
|
2016-04-08 19:30:41 +10:00
|
|
|
Bso *bufio.Writer
|
2016-03-13 11:54:14 -07:00
|
|
|
Pathname string
|
|
|
|
|
Hash map[SymVer]*LSym
|
2016-12-15 17:17:01 -08:00
|
|
|
PosTable src.PosTable
|
2016-03-13 11:54:14 -07:00
|
|
|
Imports []string
|
2016-09-16 18:31:25 -07:00
|
|
|
Plists []*Plist
|
2016-03-13 11:54:14 -07:00
|
|
|
Sym_div *LSym
|
|
|
|
|
Sym_divu *LSym
|
|
|
|
|
Sym_mod *LSym
|
|
|
|
|
Sym_modu *LSym
|
|
|
|
|
Plan9privates *LSym
|
|
|
|
|
Curp *Prog
|
|
|
|
|
Printp *Prog
|
|
|
|
|
Blitrl *Prog
|
|
|
|
|
Elitrl *Prog
|
|
|
|
|
Rexflag int
|
|
|
|
|
Vexflag int
|
|
|
|
|
Rep int
|
|
|
|
|
Repn int
|
|
|
|
|
Lock int
|
|
|
|
|
Asmode int
|
2016-03-15 17:26:28 -07:00
|
|
|
AsmBuf AsmBuf // instruction buffer for x86
|
2016-03-13 11:54:14 -07:00
|
|
|
Instoffset int64
|
|
|
|
|
Autosize int32
|
|
|
|
|
Armsize int32
|
|
|
|
|
Pc int64
|
|
|
|
|
DiagFunc func(string, ...interface{})
|
|
|
|
|
Mode int
|
|
|
|
|
Cursym *LSym
|
|
|
|
|
Version int
|
|
|
|
|
Errors int
|
2016-01-05 09:27:40 -05:00
|
|
|
|
2016-05-25 14:37:43 -04:00
|
|
|
Framepointer_enabled bool
|
|
|
|
|
|
2016-01-05 09:27:40 -05:00
|
|
|
// state for writing objects
|
2016-03-14 21:51:09 -07:00
|
|
|
Text []*LSym
|
|
|
|
|
Data []*LSym
|
2016-02-24 09:53:27 -08:00
|
|
|
|
|
|
|
|
// Cache of Progs
|
|
|
|
|
allocIdx int
|
|
|
|
|
progs [10000]Prog
|
2015-03-16 15:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-24 01:33:16 -05:00
|
|
|
func (ctxt *Link) Diag(format string, args ...interface{}) {
|
|
|
|
|
ctxt.Errors++
|
|
|
|
|
ctxt.DiagFunc(format, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 12:32:42 +10:00
|
|
|
func (ctxt *Link) Logf(format string, args ...interface{}) {
|
|
|
|
|
fmt.Fprintf(ctxt.Bso, format, args...)
|
|
|
|
|
ctxt.Bso.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-08 22:13:44 +13:00
|
|
|
// The smallest possible offset from the hardware stack pointer to a local
|
|
|
|
|
// variable on the stack. Architectures that use a link register save its value
|
|
|
|
|
// on the stack in the function prologue and so always have a pointer between
|
|
|
|
|
// the hardware stack pointer and the local variable area.
|
|
|
|
|
func (ctxt *Link) FixedFrameSize() int64 {
|
2016-04-06 12:01:40 -07:00
|
|
|
switch ctxt.Arch.Family {
|
|
|
|
|
case sys.AMD64, sys.I386:
|
2015-10-08 22:13:44 +13:00
|
|
|
return 0
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.PPC64:
|
2015-10-30 12:36:08 +13:00
|
|
|
// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
|
|
|
|
|
// just use that much stack always on ppc64x.
|
2016-04-06 12:01:40 -07:00
|
|
|
return int64(4 * ctxt.Arch.PtrSize)
|
2015-10-08 22:13:44 +13:00
|
|
|
default:
|
2016-04-06 12:01:40 -07:00
|
|
|
return int64(ctxt.Arch.PtrSize)
|
2015-10-08 22:13:44 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 15:31:32 -04:00
|
|
|
type SymVer struct {
|
|
|
|
|
Name string
|
2015-04-19 21:00:48 -07:00
|
|
|
Version int // TODO: make int16 to match LSym.Version?
|
2015-03-16 15:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
2015-01-19 14:34:58 -05:00
|
|
|
// LinkArch is the definition of a single architecture.
|
2015-03-16 15:31:32 -04:00
|
|
|
type LinkArch struct {
|
2016-04-06 12:01:40 -07:00
|
|
|
*sys.Arch
|
2015-03-16 15:31:32 -04:00
|
|
|
Preprocess func(*Link, *LSym)
|
|
|
|
|
Assemble func(*Link, *LSym)
|
|
|
|
|
Progedit func(*Link, *Prog)
|
2016-03-07 18:00:08 -08:00
|
|
|
UnaryDst map[As]bool // Instruction takes one operand, a destination.
|
2015-03-16 15:31:32 -04:00
|
|
|
}
|
2015-01-19 14:34:58 -05:00
|
|
|
|
2016-09-09 06:20:44 -04:00
|
|
|
// HeadType is the executable header type.
|
|
|
|
|
type HeadType uint8
|
|
|
|
|
|
2015-01-19 14:34:58 -05:00
|
|
|
const (
|
2016-09-09 06:20:44 -04:00
|
|
|
Hunknown HeadType = iota
|
2015-01-19 14:34:58 -05:00
|
|
|
Hdarwin
|
|
|
|
|
Hdragonfly
|
|
|
|
|
Hfreebsd
|
|
|
|
|
Hlinux
|
|
|
|
|
Hnacl
|
|
|
|
|
Hnetbsd
|
|
|
|
|
Hopenbsd
|
|
|
|
|
Hplan9
|
|
|
|
|
Hsolaris
|
|
|
|
|
Hwindows
|
2016-09-09 06:20:44 -04:00
|
|
|
Hwindowsgui
|
2015-01-19 14:34:58 -05:00
|
|
|
)
|
|
|
|
|
|
2016-09-09 06:20:44 -04:00
|
|
|
func (h *HeadType) Set(s string) error {
|
|
|
|
|
switch s {
|
|
|
|
|
case "darwin":
|
|
|
|
|
*h = Hdarwin
|
|
|
|
|
case "dragonfly":
|
|
|
|
|
*h = Hdragonfly
|
|
|
|
|
case "freebsd":
|
|
|
|
|
*h = Hfreebsd
|
|
|
|
|
case "linux", "android":
|
|
|
|
|
*h = Hlinux
|
|
|
|
|
case "nacl":
|
|
|
|
|
*h = Hnacl
|
|
|
|
|
case "netbsd":
|
|
|
|
|
*h = Hnetbsd
|
|
|
|
|
case "openbsd":
|
|
|
|
|
*h = Hopenbsd
|
|
|
|
|
case "plan9":
|
|
|
|
|
*h = Hplan9
|
|
|
|
|
case "solaris":
|
|
|
|
|
*h = Hsolaris
|
|
|
|
|
case "windows":
|
|
|
|
|
*h = Hwindows
|
|
|
|
|
case "windowsgui":
|
|
|
|
|
*h = Hwindowsgui
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("invalid headtype: %q", s)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *HeadType) String() string {
|
|
|
|
|
switch *h {
|
|
|
|
|
case Hdarwin:
|
|
|
|
|
return "darwin"
|
|
|
|
|
case Hdragonfly:
|
|
|
|
|
return "dragonfly"
|
|
|
|
|
case Hfreebsd:
|
|
|
|
|
return "freebsd"
|
|
|
|
|
case Hlinux:
|
|
|
|
|
return "linux"
|
|
|
|
|
case Hnacl:
|
|
|
|
|
return "nacl"
|
|
|
|
|
case Hnetbsd:
|
|
|
|
|
return "netbsd"
|
|
|
|
|
case Hopenbsd:
|
|
|
|
|
return "openbsd"
|
|
|
|
|
case Hplan9:
|
|
|
|
|
return "plan9"
|
|
|
|
|
case Hsolaris:
|
|
|
|
|
return "solaris"
|
|
|
|
|
case Hwindows:
|
|
|
|
|
return "windows"
|
|
|
|
|
case Hwindowsgui:
|
|
|
|
|
return "windowsgui"
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("HeadType(%d)", *h)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 17:26:28 -07:00
|
|
|
// AsmBuf is a simple buffer to assemble variable-length x86 instructions into.
|
|
|
|
|
type AsmBuf struct {
|
|
|
|
|
buf [100]byte
|
|
|
|
|
off int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put1 appends one byte to the end of the buffer.
|
|
|
|
|
func (a *AsmBuf) Put1(x byte) {
|
|
|
|
|
a.buf[a.off] = x
|
|
|
|
|
a.off++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put2 appends two bytes to the end of the buffer.
|
|
|
|
|
func (a *AsmBuf) Put2(x, y byte) {
|
|
|
|
|
a.buf[a.off+0] = x
|
|
|
|
|
a.buf[a.off+1] = y
|
|
|
|
|
a.off += 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put3 appends three bytes to the end of the buffer.
|
|
|
|
|
func (a *AsmBuf) Put3(x, y, z byte) {
|
|
|
|
|
a.buf[a.off+0] = x
|
|
|
|
|
a.buf[a.off+1] = y
|
|
|
|
|
a.buf[a.off+2] = z
|
|
|
|
|
a.off += 3
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put4 appends four bytes to the end of the buffer.
|
|
|
|
|
func (a *AsmBuf) Put4(x, y, z, w byte) {
|
|
|
|
|
a.buf[a.off+0] = x
|
|
|
|
|
a.buf[a.off+1] = y
|
|
|
|
|
a.buf[a.off+2] = z
|
|
|
|
|
a.buf[a.off+3] = w
|
|
|
|
|
a.off += 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PutInt16 writes v into the buffer using little-endian encoding.
|
|
|
|
|
func (a *AsmBuf) PutInt16(v int16) {
|
|
|
|
|
a.buf[a.off+0] = byte(v)
|
|
|
|
|
a.buf[a.off+1] = byte(v >> 8)
|
|
|
|
|
a.off += 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PutInt32 writes v into the buffer using little-endian encoding.
|
|
|
|
|
func (a *AsmBuf) PutInt32(v int32) {
|
|
|
|
|
a.buf[a.off+0] = byte(v)
|
|
|
|
|
a.buf[a.off+1] = byte(v >> 8)
|
|
|
|
|
a.buf[a.off+2] = byte(v >> 16)
|
|
|
|
|
a.buf[a.off+3] = byte(v >> 24)
|
|
|
|
|
a.off += 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PutInt64 writes v into the buffer using little-endian encoding.
|
|
|
|
|
func (a *AsmBuf) PutInt64(v int64) {
|
|
|
|
|
a.buf[a.off+0] = byte(v)
|
|
|
|
|
a.buf[a.off+1] = byte(v >> 8)
|
|
|
|
|
a.buf[a.off+2] = byte(v >> 16)
|
|
|
|
|
a.buf[a.off+3] = byte(v >> 24)
|
|
|
|
|
a.buf[a.off+4] = byte(v >> 32)
|
|
|
|
|
a.buf[a.off+5] = byte(v >> 40)
|
|
|
|
|
a.buf[a.off+6] = byte(v >> 48)
|
|
|
|
|
a.buf[a.off+7] = byte(v >> 56)
|
|
|
|
|
a.off += 8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Put copies b into the buffer.
|
|
|
|
|
func (a *AsmBuf) Put(b []byte) {
|
|
|
|
|
copy(a.buf[a.off:], b)
|
|
|
|
|
a.off += len(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert inserts b at offset i.
|
|
|
|
|
func (a *AsmBuf) Insert(i int, b byte) {
|
|
|
|
|
a.off++
|
|
|
|
|
copy(a.buf[i+1:a.off], a.buf[i:a.off-1])
|
|
|
|
|
a.buf[i] = b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Last returns the byte at the end of the buffer.
|
|
|
|
|
func (a *AsmBuf) Last() byte { return a.buf[a.off-1] }
|
|
|
|
|
|
|
|
|
|
// Len returns the length of the buffer.
|
|
|
|
|
func (a *AsmBuf) Len() int { return a.off }
|
|
|
|
|
|
|
|
|
|
// Bytes returns the contents of the buffer.
|
|
|
|
|
func (a *AsmBuf) Bytes() []byte { return a.buf[:a.off] }
|
|
|
|
|
|
|
|
|
|
// Reset empties the buffer.
|
|
|
|
|
func (a *AsmBuf) Reset() { a.off = 0 }
|
|
|
|
|
|
|
|
|
|
// Peek returns the byte at offset i.
|
|
|
|
|
func (a *AsmBuf) Peek(i int) byte { return a.buf[i] }
|