obj

package
v0.0.0-...-fa1f52c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2017 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ABase386 = (1 + iota) << 11
	ABaseARM
	ABaseAMD64
	ABasePPC64
	ABaseARM64
	ABaseMIPS
	ABaseS390X

	AllowedOpCodes = 1 << 11            // The number of opcodes available for any given architecture.
	AMask          = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
)

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.

View Source
const (
	// Don't profile the marked routine.
	//
	// Deprecated: Not implemented, do not use.
	NOPROF = 1

	// It is ok for the linker to get multiple of these symbols. It will
	// pick one of the duplicates to use.
	DUPOK = 2

	// Don't insert stack check preamble.
	NOSPLIT = 4

	// Put this data in a read-only section.
	RODATA = 8

	// This data contains no pointers.
	NOPTR = 16

	// This is a wrapper function and should not count as disabling 'recover'.
	WRAPPER = 32

	// This function uses its incoming context register.
	NEEDCTXT = 64

	// When passed to ggloblsym, causes Local to be set to true on the LSym it creates.
	LOCAL = 128

	// Allocate a word of thread local storage and store the offset from the
	// thread local base to the thread local storage in this variable.
	TLSBSS = 256

	// Do not insert instructions to allocate a stack frame for this function.
	// Only valid on functions that declare a frame size of 0.
	// TODO(mwhudson): only implemented for ppc64x at present.
	NOFRAME = 512

	// Function can call reflect.Type.Method or reflect.Type.MethodByName.
	REFLECTMETHOD = 1024
)
View Source
const (
	C_SCOND     = (1 << 4) - 1
	C_SBIT      = 1 << 4
	C_PBIT      = 1 << 5
	C_WBIT      = 1 << 6
	C_FBIT      = 1 << 7
	C_UBIT      = 1 << 7
	C_SCOND_XOR = 14
)

ARM scond byte

View Source
const (
	// Because of masking operations in the encodings, each register
	// space should start at 0 modulo some power of 2.
	RBase386   = 1 * 1024
	RBaseAMD64 = 2 * 1024
	RBaseARM   = 3 * 1024
	RBasePPC64 = 4 * 1024  // range [4k, 8k)
	RBaseARM64 = 8 * 1024  // range [8k, 13k)
	RBaseMIPS  = 13 * 1024 // range [13k, 14k)
	RBaseS390X = 14 * 1024 // range [14k, 15k)
)
View Source
const (
	RegListARMLo = 0
	RegListARMHi = 1 << 16

	// arm64 uses the 60th bit to differentiate from other archs
	RegListARM64Lo = 1 << 60
	RegListARM64Hi = 1<<61 - 1
)

Each architecture is allotted a distinct subspace: [Lo, Hi) for declaring its arch-specific register list numbers.

View Source
const (
	LOG = 5
)
View Source
const REG_NONE = 0

Variables

View Source
var Anames = []string{
	"XXX",
	"CALL",
	"DUFFCOPY",
	"DUFFZERO",
	"END",
	"FUNCDATA",
	"JMP",
	"NOP",
	"PCDATA",
	"RET",
	"TEXT",
	"UNDEF",
}

Functions

func Bool2int

func Bool2int(b bool) int

func CConv

func CConv(s uint8) string

CConv formats ARM condition codes.

func Dconv

func Dconv(p *Prog, a *Addr) string

func Flushplist

func Flushplist(ctxt *Link, plist *Plist, newprog ProgAlloc)

func Mconv

func Mconv(a *Addr) string

func Nopout

func Nopout(p *Prog)

func RLconv

func RLconv(list int64) string

func Rconv

func Rconv(reg int) string

func RegisterOpcode

func RegisterOpcode(lo As, Anames []string)

RegisterOpcode binds a list of instruction names to a given instruction number range.

func RegisterRegister

func RegisterRegister(lo, hi int, Rconv func(int) string)

RegisterRegister binds a pretty-printer (Rconv) for register numbers to a given register number range. Lo is inclusive, hi exclusive (valid registers are lo through hi-1).

func RegisterRegisterList

func RegisterRegisterList(lo, hi int64, rlconv func(int64) string)

RegisterRegisterList binds a pretty-printer (RLconv) for register list numbers to a given register list number range. Lo is inclusive, hi exclusive (valid register list are lo through hi-1).

func SortSlice

func SortSlice(slice interface{}, less func(i, j int) bool)

func WriteObjFile

func WriteObjFile(ctxt *Link, b *bufio.Writer)

Types

type Addr

type Addr struct {
	Reg    int16
	Index  int16
	Scale  int16 // Sometimes holds a register.
	Type   AddrType
	Name   AddrName
	Class  int8
	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{}
}

type AddrName

type AddrName int8
const (
	NAME_NONE AddrName = iota
	NAME_EXTERN
	NAME_STATIC
	NAME_AUTO
	NAME_PARAM
	// A reference to name@GOT(SB) is a reference to the entry in the global offset
	// table for 'name'.
	NAME_GOTREF
)

type AddrType

type AddrType uint8
const (
	TYPE_NONE AddrType = iota
	TYPE_BRANCH
	TYPE_TEXTSIZE
	TYPE_MEM
	TYPE_CONST
	TYPE_FCONST
	TYPE_SCONST
	TYPE_REG
	TYPE_ADDR
	TYPE_SHIFT
	TYPE_REGREG
	TYPE_REGREG2
	TYPE_INDIR
	TYPE_REGLIST
)

func (AddrType) String

func (i AddrType) String() string

type As

type As int16

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.

const (
	AXXX As = iota
	ACALL
	ADUFFCOPY
	ADUFFZERO
	AEND
	AFUNCDATA
	AJMP
	ANOP
	APCDATA
	ARET
	ATEXT
	AUNDEF
	A_ARCHSPECIFIC
)

These are the portable opcodes.

func (As) String

func (a As) String() string

type Attribute

type Attribute int16

Attribute is a set of symbol attributes.

const (
	AttrDuplicateOK Attribute = 1 << iota
	AttrCFunc
	AttrNoSplit
	AttrLeaf
	AttrWrapper
	AttrNeedCtxt
	AttrNoFrame
	AttrSeenGlobl
	AttrOnList
	AttrStatic

	// MakeTypelink means that the type should have an entry in the typelink table.
	AttrMakeTypelink

	// 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.
	AttrReflectMethod

	// 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).
	AttrLocal
)

func (Attribute) CFunc

func (a Attribute) CFunc() bool

func (Attribute) DuplicateOK

func (a Attribute) DuplicateOK() bool

func (Attribute) Leaf

func (a Attribute) Leaf() bool

func (Attribute) Local

func (a Attribute) Local() bool
func (a Attribute) MakeTypelink() bool

func (Attribute) NeedCtxt

func (a Attribute) NeedCtxt() bool

func (Attribute) NoFrame

func (a Attribute) NoFrame() bool

func (Attribute) NoSplit

func (a Attribute) NoSplit() bool

func (Attribute) OnList

func (a Attribute) OnList() bool

func (Attribute) ReflectMethod

func (a Attribute) ReflectMethod() bool

func (Attribute) SeenGlobl

func (a Attribute) SeenGlobl() bool

func (*Attribute) Set

func (a *Attribute) Set(flag Attribute, value bool)

func (Attribute) Static

func (a Attribute) Static() bool

func (Attribute) TextAttrString

func (a Attribute) TextAttrString() string

TextAttrString formats a for printing in as part of a TEXT prog.

func (Attribute) Wrapper

func (a Attribute) Wrapper() bool

type Auto

type Auto struct {
	Asym    *LSym
	Aoffset int32
	Name    AddrName
	Gotype  *LSym
}

type FuncInfo

type FuncInfo struct {
	Args   int32
	Locals int32
	Text   *Prog
	Autom  []*Auto
	Pcln   Pcln

	GCArgs   LSym
	GCLocals LSym
	// contains filtered or unexported fields
}

A FuncInfo contains extra fields for STEXT symbols.

type InlTree

type InlTree struct {
	// contains filtered or unexported fields
}

InlTree s a collection of inlined calls. The Parent field of an InlinedCall is the index of another InlinedCall in InlTree.

The compiler maintains a global inlining tree and adds a node to it every time a function is inlined. For example, suppose f() calls g() and g has two calls to h(), and that f, g, and h are inlineable:

1 func main() {
2     f()
3 }
4 func f() {
5     g()
6 }
7 func g() {
8     h()
9     h()

10 }

Assuming the global tree starts empty, inlining will produce the following tree:

[]InlinedCall{
  {Parent: -1, Func: "f", Pos: <line 2>},
  {Parent:  0, Func: "g", Pos: <line 5>},
  {Parent:  1, Func: "h", Pos: <line 8>},
  {Parent:  1, Func: "h", Pos: <line 9>},
}

The nodes of h inlined into main will have inlining indexes 2 and 3.

Eventually, the compiler extracts a per-function inlining tree from the global inlining tree (see pcln.go).

func (*InlTree) Add

func (tree *InlTree) Add(parent int, pos src.XPos, func_ *LSym) int

Add adds a new call to the tree, returning its index.

type InlinedCall

type InlinedCall struct {
	Parent int      // index of the parent in the InlTree or < 0 if outermost call
	Pos    src.XPos // position of the inlined call
	Func   *LSym    // function that was inlined
}

InlinedCall is a node in an InlTree.

type LSym

type LSym struct {
	Name string
	Type objabi.SymKind
	Attribute

	RefIdx int // Index of this symbol in the symbol reference list.
	Size   int64
	Gotype *LSym
	P      []byte
	R      []Reloc

	Func *FuncInfo
}

An LSym is the sort of symbol that is written to an object file.

func (*LSym) Grow

func (s *LSym) Grow(lsiz int64)

Grow increases the length of s.P to lsiz.

func (*LSym) GrowCap

func (s *LSym) GrowCap(c int64)

GrowCap increases the capacity of s.P to c.

func (*LSym) Len

func (s *LSym) Len() int64

func (*LSym) String

func (s *LSym) String() string

The compiler needs LSym to satisfy fmt.Stringer, because it stores an LSym in ssa.ExternSymbol.

func (*LSym) WriteAddr

func (s *LSym) WriteAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64)

WriteAddr writes an address of size siz into s at offset off. rsym and roff specify the relocation for the address.

func (*LSym) WriteBytes

func (s *LSym) WriteBytes(ctxt *Link, off int64, b []byte) int64

WriteBytes writes a slice of bytes into s at offset off.

func (*LSym) WriteCURelativeAddr

func (s *LSym) WriteCURelativeAddr(ctxt *Link, off int64, rsym *LSym, roff int64)

WriteCURelativeAddr writes a pointer-sized address into s at offset off. rsym and roff specify the relocation for the address which will be resolved by the linker to an offset from the DW_AT_low_pc attribute of the DWARF Compile Unit of rsym.

func (*LSym) WriteFloat32

func (s *LSym) WriteFloat32(ctxt *Link, off int64, f float32)

WriteFloat32 writes f into s at offset off.

func (*LSym) WriteFloat64

func (s *LSym) WriteFloat64(ctxt *Link, off int64, f float64)

WriteFloat64 writes f into s at offset off.

func (*LSym) WriteInt

func (s *LSym) WriteInt(ctxt *Link, off int64, siz int, i int64)

WriteInt writes an integer i of size siz into s at offset off.

func (*LSym) WriteOff

func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64)

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 (*LSym) WriteString

func (s *LSym) WriteString(ctxt *Link, off int64, siz int, str string)

WriteString writes a string of size siz into s at offset off.

func (*LSym) WriteWeakOff

func (s *LSym) WriteWeakOff(ctxt *Link, off int64, rsym *LSym, roff int64)

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).

type Link struct {
	Headtype           objabi.HeadType
	Arch               *LinkArch
	Debugasm           bool
	Debugvlog          bool
	Debugpcln          string
	Flag_shared        bool
	Flag_dynlink       bool
	Flag_optimize      bool
	Flag_locationlists bool
	Bso                *bufio.Writer
	Pathname           string

	PosTable  src.PosTable
	InlTree   InlTree // global inlining tree used by gc/inl.go
	Imports   []string
	DiagFunc  func(string, ...interface{})
	DiagFlush func()
	DebugInfo func(fn *LSym, curfn interface{}) []dwarf.Scope // if non-nil, curfn is a *gc.Node
	Errors    int

	Framepointer_enabled bool

	// state for writing objects
	Text []*LSym
	Data []*LSym
	// contains filtered or unexported fields
}

Link holds the context for writing object code from a compiler to be linker input or for reading that input into the linker.

func Linknew

func Linknew(arch *LinkArch) *Link

func (*Link) AddImport

func (ctxt *Link) AddImport(pkg string)

AddImport adds a package to the list of imported packages.

func (*Link) CanReuseProgs

func (ctxt *Link) CanReuseProgs() bool

func (*Link) Dconv

func (ctxt *Link) Dconv(a *Addr) string

func (*Link) Diag

func (ctxt *Link) Diag(format string, args ...interface{})

func (*Link) DwarfIntConst

func (ctxt *Link) DwarfIntConst(myimportpath, name, typename string, val int64)

DwarfIntConst creates a link symbol for an integer constant with the given name, type and value.

func (*Link) FixedFrameSize

func (ctxt *Link) FixedFrameSize() int64

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 (*Link) Float32Sym

func (ctxt *Link) Float32Sym(f float32) *LSym

func (*Link) Float64Sym

func (ctxt *Link) Float64Sym(f float64) *LSym

func (*Link) Globl

func (ctxt *Link) Globl(s *LSym, size int64, flag int)

func (*Link) InitTextSym

func (ctxt *Link) InitTextSym(s *LSym, flag int)

func (*Link) Int64Sym

func (ctxt *Link) Int64Sym(i int64) *LSym

func (*Link) Logf

func (ctxt *Link) Logf(format string, args ...interface{})

func (*Link) Lookup

func (ctxt *Link) Lookup(name string) *LSym

Lookup looks up the symbol with name name. If it does not exist, it creates it.

func (*Link) LookupDerived

func (ctxt *Link) LookupDerived(s *LSym, name string) *LSym

LookupDerived looks up or creates the symbol with name name derived from symbol s. The resulting symbol will be static iff s is.

func (*Link) LookupInit

func (ctxt *Link) LookupInit(name string, init func(s *LSym)) *LSym

LookupInit looks up the symbol with name name. If it does not exist, it creates it and passes it to init for one-time initialization.

func (*Link) LookupStatic

func (ctxt *Link) LookupStatic(name string) *LSym

LookupStatic looks up the static symbol with name name. If it does not exist, it creates it.

func (*Link) NewProg

func (ctxt *Link) NewProg() *Prog

func (*Link) OutermostPos

func (ctxt *Link) OutermostPos(xpos src.XPos) src.Pos

OutermostPos returns the outermost position corresponding to xpos, which is where xpos was ultimately inlined to. In the example for InlTree, main() contains inlined AST nodes from h(), but the outermost position for those nodes is line 2.

type LinkArch

type LinkArch struct {
	*sys.Arch
	Init           func(*Link)
	Preprocess     func(*Link, *LSym, ProgAlloc)
	Assemble       func(*Link, *LSym, ProgAlloc)
	Progedit       func(*Link, *Prog, ProgAlloc)
	UnaryDst       map[As]bool // Instruction takes one operand, a destination.
	DWARFRegisters map[int16]int16
}

LinkArch is the definition of a single architecture.

type Pcdata

type Pcdata struct {
	P []byte
}

type Pcln

type Pcln struct {
	Pcsp        Pcdata
	Pcfile      Pcdata
	Pcline      Pcdata
	Pcinline    Pcdata
	Pcdata      []Pcdata
	Funcdata    []*LSym
	Funcdataoff []int64
	File        []string
	Lastfile    string
	Lastindex   int
	InlTree     InlTree // per-function inlining tree extracted from the global tree
}

type Plist

type Plist struct {
	Firstpc *Prog
	Curfn   interface{} // holds a *gc.Node, if non-nil
}

type Prog

type Prog struct {
	Ctxt     *Link    // linker context
	Link     *Prog    // next Prog in linked list
	From     Addr     // first source operand
	RestArgs []Addr   // can pack any operands that not fit into {Prog.From, Prog.To}
	To       Addr     // destination operand (second is RegTo2 below)
	Pcond    *Prog    // target of conditional jump
	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
	Pos      src.XPos // source position of this instruction
	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
}

Prog describes a single machine instruction.

The general instruction form is:

(1) As.Scond From [, ...RestArgs], To
(2) As.Scond From, Reg [, ...RestArgs], To, RegTo2

where As is an opcode and the others are arguments: From, Reg are sources, and To, RegTo2 are destinations. RestArgs can hold additional sources and 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. (2) form is present for compatibility with older code, to avoid too much changes in a single swing. (1) scheme is enough to express any kind of operand combination.

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, specified by Pos.Line(). Every Prog has a Ctxt field that defines its context. For performance reasons, Progs usually are usually bulk allocated, cached, and reused; those bulk allocators should always be used, rather than 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.

func Appendp

func Appendp(q *Prog, newprog ProgAlloc) *Prog

func (*Prog) FileName

func (p *Prog) FileName() string

FileName returns a string containing the filename for p's position

func (*Prog) From3Type deprecated

func (p *Prog) From3Type() AddrType

From3Type returns p.GetFrom3().Type, or TYPE_NONE when p.GetFrom3() returns nil.

Deprecated: for the same reasons as Prog.GetFrom3.

func (*Prog) GetFrom3 deprecated

func (p *Prog) GetFrom3() *Addr

GetFrom3 returns second source operand (the first is Prog.From). In combination with Prog.From and Prog.To it makes common 3 operand case easier to use.

Should be used only when RestArgs is set with SetFrom3.

Deprecated: better use RestArgs directly or define backend-specific getters. Introduced to simplify transition to []Addr. Usage of this is discouraged due to fragility and lack of guarantees.

func (*Prog) InstructionString

func (p *Prog) InstructionString() string

InstructionString returns a string representation of the instruction without preceding program counter or file and line number.

func (*Prog) Line

func (p *Prog) Line() string

Line returns a string containing the filename and line number for p

func (*Prog) LineNumber

func (p *Prog) LineNumber() string

LineNumber returns a string containing the line number for p's position

func (*Prog) SetFrom3 deprecated

func (p *Prog) SetFrom3(a Addr)

SetFrom3 assigns []Addr{a} to p.RestArgs. In pair with Prog.GetFrom3 it can help in emulation of Prog.From3.

Deprecated: for the same reasons as Prog.GetFrom3.

func (*Prog) String

func (p *Prog) String() string

type ProgAlloc

type ProgAlloc func() *Prog

ProgAlloc is a function that allocates Progs. It is used to provide access to cached/bulk-allocated Progs to the assemblers.

type Reloc

type Reloc struct {
	Off  int32
	Siz  uint8
	Type objabi.RelocType
	Add  int64
	Sym  *LSym
}

func Addrel

func Addrel(s *LSym) *Reloc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL