2015-02-27 22:57:28 -05:00
|
|
|
// Inferno utils/6l/span.c
|
|
|
|
|
// http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
|
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
// Portions Copyright © 2009 The Go Authors. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// 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 ld
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
import (
|
|
|
|
|
"cmd/internal/obj"
|
2015-04-11 12:05:21 +08:00
|
|
|
"fmt"
|
|
|
|
|
"path/filepath"
|
2015-04-19 19:33:58 -07:00
|
|
|
"strings"
|
|
|
|
|
)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Symbol table.
|
|
|
|
|
|
|
|
|
|
func putelfstr(s string) int {
|
|
|
|
|
if len(Elfstrdat) == 0 && s != "" {
|
|
|
|
|
// first entry must be empty string
|
|
|
|
|
putelfstr("")
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
// When dynamically linking, we create LSym's by reading the names from
|
|
|
|
|
// the symbol tables of the shared libraries and so the names need to
|
|
|
|
|
// match exactly. Tools like DTrace will have to wait for now.
|
|
|
|
|
if !DynlinkingGo() {
|
|
|
|
|
// Rewrite · to . for ASCII-only tools like DTrace (sigh)
|
|
|
|
|
s = strings.Replace(s, "·", ".", -1)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
n := len(s) + 1
|
2015-02-27 22:57:28 -05:00
|
|
|
for len(Elfstrdat)+n > cap(Elfstrdat) {
|
|
|
|
|
Elfstrdat = append(Elfstrdat[:cap(Elfstrdat)], 0)[:len(Elfstrdat)]
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
off := len(Elfstrdat)
|
2015-02-27 22:57:28 -05:00
|
|
|
Elfstrdat = Elfstrdat[:off+n]
|
|
|
|
|
copy(Elfstrdat[off:], s)
|
|
|
|
|
|
|
|
|
|
return off
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func putelfsyment(off int, addr int64, size int64, info int, shndx int, other int) {
|
|
|
|
|
switch Thearch.Thechar {
|
2015-09-10 11:32:49 -04:00
|
|
|
case '0', '6', '7', '9':
|
2015-02-27 22:57:28 -05:00
|
|
|
Thearch.Lput(uint32(off))
|
|
|
|
|
Cput(uint8(info))
|
|
|
|
|
Cput(uint8(other))
|
|
|
|
|
Thearch.Wput(uint16(shndx))
|
|
|
|
|
Thearch.Vput(uint64(addr))
|
|
|
|
|
Thearch.Vput(uint64(size))
|
|
|
|
|
Symsize += ELF64SYMSIZE
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Thearch.Lput(uint32(off))
|
|
|
|
|
Thearch.Lput(uint32(addr))
|
|
|
|
|
Thearch.Lput(uint32(size))
|
|
|
|
|
Cput(uint8(info))
|
|
|
|
|
Cput(uint8(other))
|
|
|
|
|
Thearch.Wput(uint16(shndx))
|
|
|
|
|
Symsize += ELF32SYMSIZE
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var numelfsym int = 1 // 0 is reserved
|
|
|
|
|
|
|
|
|
|
var elfbind int
|
|
|
|
|
|
|
|
|
|
func putelfsym(x *LSym, s string, t int, addr int64, size int64, ver int, go_ *LSym) {
|
|
|
|
|
var type_ int
|
|
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
|
type_ = STT_FUNC
|
|
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
|
type_ = STT_OBJECT
|
|
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
|
type_ = STT_OBJECT
|
2015-03-29 21:03:05 +00:00
|
|
|
|
|
|
|
|
case 'U':
|
2015-05-05 16:10:12 +12:00
|
|
|
// ElfType is only set for symbols read from Go shared libraries, but
|
|
|
|
|
// for other symbols it is left as STT_NOTYPE which is fine.
|
|
|
|
|
type_ = int(x.ElfType)
|
2015-03-29 21:03:05 +00:00
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
|
type_ = STT_TLS
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
xo := x
|
2015-02-27 22:57:28 -05:00
|
|
|
for xo.Outer != nil {
|
|
|
|
|
xo = xo.Outer
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 21:03:05 +00:00
|
|
|
var elfshnum int
|
2015-04-19 19:33:58 -07:00
|
|
|
if xo.Type == obj.SDYNIMPORT || xo.Type == obj.SHOSTOBJ {
|
2015-03-29 21:03:05 +00:00
|
|
|
elfshnum = SHN_UNDEF
|
|
|
|
|
} else {
|
|
|
|
|
if xo.Sect == nil {
|
|
|
|
|
Ctxt.Cursym = x
|
|
|
|
|
Diag("missing section in putelfsym")
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-05-27 12:04:25 +12:00
|
|
|
if xo.Sect.Elfsect == nil {
|
2015-03-29 21:03:05 +00:00
|
|
|
Ctxt.Cursym = x
|
|
|
|
|
Diag("missing ELF section in putelfsym")
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-05-27 12:04:25 +12:00
|
|
|
elfshnum = xo.Sect.Elfsect.shnum
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// One pass for each binding: STB_LOCAL, STB_GLOBAL,
|
|
|
|
|
// maybe one day STB_WEAK.
|
2015-03-02 12:35:15 -05:00
|
|
|
bind := STB_GLOBAL
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
if ver != 0 || (x.Type&obj.SHIDDEN != 0) || x.Local {
|
2015-02-27 22:57:28 -05:00
|
|
|
bind = STB_LOCAL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// In external linking mode, we have to invoke gcc with -rdynamic
|
|
|
|
|
// to get the exported symbols put into the dynamic symbol table.
|
|
|
|
|
// To avoid filling the dynamic table with lots of unnecessary symbols,
|
|
|
|
|
// mark all Go symbols local (not global) in the final executable.
|
2015-03-30 02:59:10 +00:00
|
|
|
// But when we're dynamically linking, we need all those global symbols.
|
|
|
|
|
if !DynlinkingGo() && Linkmode == LinkExternal && x.Cgoexport&CgoExportStatic == 0 && elfshnum != SHN_UNDEF {
|
2015-02-27 22:57:28 -05:00
|
|
|
bind = STB_LOCAL
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 21:03:05 +00:00
|
|
|
if Linkmode == LinkExternal && elfshnum != SHN_UNDEF {
|
2015-05-27 12:04:25 +12:00
|
|
|
addr -= int64(xo.Sect.Vaddr)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-03-03 20:45:00 -05:00
|
|
|
other := STV_DEFAULT
|
2015-04-19 19:33:58 -07:00
|
|
|
if x.Type&obj.SHIDDEN != 0 {
|
2015-03-03 20:45:00 -05:00
|
|
|
other = STV_HIDDEN
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-11-05 09:55:44 +13:00
|
|
|
if (Buildmode == BuildmodePIE || DynlinkingGo()) && Thearch.Thechar == '9' && type_ == STT_FUNC && x.Name != "runtime.duffzero" && x.Name != "runtime.duffcopy" {
|
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
|
|
|
// On ppc64 the top three bits of the st_other field indicate how
|
|
|
|
|
// many instructions separate the global and local entry points. In
|
|
|
|
|
// our case it is two instructions, indicated by the value 3.
|
|
|
|
|
other |= 3 << 5
|
|
|
|
|
}
|
2015-10-29 12:17:43 +13:00
|
|
|
|
|
|
|
|
if DynlinkingGo() && bind == STB_GLOBAL && elfbind == STB_LOCAL && x.Type == obj.STEXT {
|
|
|
|
|
// When dynamically linking, we want references to functions defined
|
|
|
|
|
// in this module to always be to the function object, not to the
|
|
|
|
|
// PLT. We force this by writing an additional local symbol for every
|
|
|
|
|
// global function symbol and making all relocations against the
|
|
|
|
|
// global symbol refer to this local symbol instead (see
|
|
|
|
|
// (*LSym).ElfsymForReloc). This is approximately equivalent to the
|
|
|
|
|
// ELF linker -Bsymbolic-functions option, but that is buggy on
|
|
|
|
|
// several platforms.
|
|
|
|
|
putelfsyment(putelfstr("local."+s), addr, size, STB_LOCAL<<4|type_&0xf, elfshnum, other)
|
|
|
|
|
x.LocalElfsym = int32(numelfsym)
|
|
|
|
|
numelfsym++
|
2015-11-03 09:36:53 +13:00
|
|
|
return
|
2015-10-29 12:17:43 +13:00
|
|
|
} else if bind != elfbind {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
putelfsyment(putelfstr(s), addr, size, bind<<4|type_&0xf, elfshnum, other)
|
2015-02-27 22:57:28 -05:00
|
|
|
x.Elfsym = int32(numelfsym)
|
|
|
|
|
numelfsym++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func putelfsectionsym(s *LSym, shndx int) {
|
|
|
|
|
putelfsyment(0, 0, 0, STB_LOCAL<<4|STT_SECTION, shndx, 0)
|
|
|
|
|
s.Elfsym = int32(numelfsym)
|
|
|
|
|
numelfsym++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func putelfsymshndx(sympos int64, shndx int) {
|
2015-03-02 12:35:15 -05:00
|
|
|
here := Cpos()
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2015-02-27 22:57:28 -05:00
|
|
|
Cseek(sympos + 6)
|
2015-04-03 04:37:11 -04:00
|
|
|
} else {
|
2015-02-27 22:57:28 -05:00
|
|
|
Cseek(sympos + 14)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thearch.Wput(uint16(shndx))
|
|
|
|
|
Cseek(here)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Asmelfsym() {
|
|
|
|
|
// the first symbol entry is reserved
|
|
|
|
|
putelfsyment(0, 0, 0, STB_LOCAL<<4|STT_NOTYPE, 0, 0)
|
|
|
|
|
|
|
|
|
|
dwarfaddelfsectionsyms()
|
|
|
|
|
|
|
|
|
|
elfbind = STB_LOCAL
|
|
|
|
|
genasmsym(putelfsym)
|
|
|
|
|
|
|
|
|
|
elfbind = STB_GLOBAL
|
|
|
|
|
elfglobalsymndx = numelfsym
|
|
|
|
|
genasmsym(putelfsym)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func putplan9sym(x *LSym, s string, t int, addr int64, size int64, ver int, go_ *LSym) {
|
|
|
|
|
switch t {
|
2015-04-01 09:38:44 -07:00
|
|
|
case 'T', 'L', 'D', 'B':
|
2015-02-27 22:57:28 -05:00
|
|
|
if ver != 0 {
|
|
|
|
|
t += 'a' - 'A'
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
|
|
case 'a',
|
|
|
|
|
'p',
|
|
|
|
|
'f',
|
|
|
|
|
'z',
|
|
|
|
|
'Z',
|
|
|
|
|
'm':
|
2015-03-02 12:35:15 -05:00
|
|
|
l := 4
|
2015-04-19 19:33:58 -07:00
|
|
|
if HEADTYPE == obj.Hplan9 && Thearch.Thechar == '6' && Debug['8'] == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
Lputb(uint32(addr >> 32))
|
|
|
|
|
l = 8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Lputb(uint32(addr))
|
|
|
|
|
Cput(uint8(t + 0x80)) /* 0x80 is variable length */
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
var i int
|
2015-02-27 22:57:28 -05:00
|
|
|
if t == 'z' || t == 'Z' {
|
|
|
|
|
Cput(uint8(s[0]))
|
|
|
|
|
for i = 1; s[i] != 0 || s[i+1] != 0; i += 2 {
|
|
|
|
|
Cput(uint8(s[i]))
|
|
|
|
|
Cput(uint8(s[i+1]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cput(0)
|
|
|
|
|
Cput(0)
|
|
|
|
|
i++
|
|
|
|
|
} else {
|
|
|
|
|
/* skip the '<' in filenames */
|
|
|
|
|
if t == 'f' {
|
|
|
|
|
s = s[1:]
|
|
|
|
|
}
|
|
|
|
|
for i = 0; i < len(s); i++ {
|
|
|
|
|
Cput(uint8(s[i]))
|
|
|
|
|
}
|
|
|
|
|
Cput(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symsize += int32(l) + 1 + int32(i) + 1
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Asmplan9sym() {
|
|
|
|
|
genasmsym(putplan9sym)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var symt *LSym
|
|
|
|
|
|
|
|
|
|
func Wputl(w uint16) {
|
|
|
|
|
Cput(uint8(w))
|
|
|
|
|
Cput(uint8(w >> 8))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Wputb(w uint16) {
|
|
|
|
|
Cput(uint8(w >> 8))
|
|
|
|
|
Cput(uint8(w))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Lputb(l uint32) {
|
|
|
|
|
Cput(uint8(l >> 24))
|
|
|
|
|
Cput(uint8(l >> 16))
|
|
|
|
|
Cput(uint8(l >> 8))
|
|
|
|
|
Cput(uint8(l))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Lputl(l uint32) {
|
|
|
|
|
Cput(uint8(l))
|
|
|
|
|
Cput(uint8(l >> 8))
|
|
|
|
|
Cput(uint8(l >> 16))
|
|
|
|
|
Cput(uint8(l >> 24))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Vputb(v uint64) {
|
|
|
|
|
Lputb(uint32(v >> 32))
|
|
|
|
|
Lputb(uint32(v))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Vputl(v uint64) {
|
|
|
|
|
Lputl(uint32(v))
|
|
|
|
|
Lputl(uint32(v >> 32))
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-11 12:05:21 +08:00
|
|
|
type byPkg []*Library
|
|
|
|
|
|
|
|
|
|
func (libs byPkg) Len() int {
|
|
|
|
|
return len(libs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (libs byPkg) Less(a, b int) bool {
|
|
|
|
|
return libs[a].Pkg < libs[b].Pkg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (libs byPkg) Swap(a, b int) {
|
|
|
|
|
libs[a], libs[b] = libs[b], libs[a]
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
func symtab() {
|
|
|
|
|
dosymtype()
|
|
|
|
|
|
|
|
|
|
// Define these so that they'll get put into the symbol table.
|
|
|
|
|
// data.c:/^address will provide the actual values.
|
2015-04-19 19:33:58 -07:00
|
|
|
xdefine("runtime.text", obj.STEXT, 0)
|
|
|
|
|
|
|
|
|
|
xdefine("runtime.etext", obj.STEXT, 0)
|
|
|
|
|
xdefine("runtime.typelink", obj.SRODATA, 0)
|
|
|
|
|
xdefine("runtime.etypelink", obj.SRODATA, 0)
|
|
|
|
|
xdefine("runtime.rodata", obj.SRODATA, 0)
|
|
|
|
|
xdefine("runtime.erodata", obj.SRODATA, 0)
|
|
|
|
|
xdefine("runtime.noptrdata", obj.SNOPTRDATA, 0)
|
|
|
|
|
xdefine("runtime.enoptrdata", obj.SNOPTRDATA, 0)
|
|
|
|
|
xdefine("runtime.data", obj.SDATA, 0)
|
|
|
|
|
xdefine("runtime.edata", obj.SDATA, 0)
|
|
|
|
|
xdefine("runtime.bss", obj.SBSS, 0)
|
|
|
|
|
xdefine("runtime.ebss", obj.SBSS, 0)
|
|
|
|
|
xdefine("runtime.noptrbss", obj.SNOPTRBSS, 0)
|
|
|
|
|
xdefine("runtime.enoptrbss", obj.SNOPTRBSS, 0)
|
|
|
|
|
xdefine("runtime.end", obj.SBSS, 0)
|
|
|
|
|
xdefine("runtime.epclntab", obj.SRODATA, 0)
|
|
|
|
|
xdefine("runtime.esymtab", obj.SRODATA, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// garbage collection symbols
|
2015-03-02 12:35:15 -05:00
|
|
|
s := Linklookup(Ctxt, "runtime.gcdata", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SRODATA
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
2015-04-19 19:33:58 -07:00
|
|
|
xdefine("runtime.egcdata", obj.SRODATA, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
s = Linklookup(Ctxt, "runtime.gcbss", 0)
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SRODATA
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
2015-04-19 19:33:58 -07:00
|
|
|
xdefine("runtime.egcbss", obj.SRODATA, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// pseudo-symbols to mark locations of type, string, and go string data.
|
2015-03-30 02:59:10 +00:00
|
|
|
var symtype *LSym
|
2015-05-21 13:07:19 +12:00
|
|
|
var symtyperel *LSym
|
2015-10-19 12:53:36 -04:00
|
|
|
if UseRelro() && (Buildmode == BuildmodeCShared || Buildmode == BuildmodePIE) {
|
2015-05-21 13:07:19 +12:00
|
|
|
s = Linklookup(Ctxt, "type.*", 0)
|
|
|
|
|
|
|
|
|
|
s.Type = obj.STYPE
|
|
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
|
|
|
|
symtype = s
|
|
|
|
|
|
|
|
|
|
s = Linklookup(Ctxt, "typerel.*", 0)
|
|
|
|
|
|
|
|
|
|
s.Type = obj.STYPERELRO
|
|
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
|
|
|
|
symtyperel = s
|
|
|
|
|
} else if !DynlinkingGo() {
|
2015-03-30 02:59:10 +00:00
|
|
|
s = Linklookup(Ctxt, "type.*", 0)
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.STYPE
|
2015-03-30 02:59:10 +00:00
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
|
|
|
|
symtype = s
|
2015-05-21 13:07:19 +12:00
|
|
|
symtyperel = s
|
2015-03-30 02:59:10 +00:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
s = Linklookup(Ctxt, "go.string.*", 0)
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SGOSTRING
|
2015-03-30 02:59:10 +00:00
|
|
|
s.Local = true
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
2015-03-02 12:35:15 -05:00
|
|
|
symgostring := s
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
s = Linklookup(Ctxt, "go.func.*", 0)
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SGOFUNC
|
2015-03-30 02:59:10 +00:00
|
|
|
s.Local = true
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
2015-03-02 12:35:15 -05:00
|
|
|
symgofunc := s
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-07-22 14:59:56 -04:00
|
|
|
s = Linklookup(Ctxt, "runtime.gcbits.*", 0)
|
|
|
|
|
s.Type = obj.SGCBITS
|
|
|
|
|
s.Local = true
|
|
|
|
|
s.Size = 0
|
|
|
|
|
s.Reachable = true
|
|
|
|
|
symgcbits := s
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
symtypelink := Linklookup(Ctxt, "runtime.typelink", 0)
|
2015-05-21 13:07:19 +12:00
|
|
|
symtypelink.Type = obj.STYPELINK
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
symt = Linklookup(Ctxt, "runtime.symtab", 0)
|
2015-03-30 02:59:10 +00:00
|
|
|
symt.Local = true
|
2015-04-19 19:33:58 -07:00
|
|
|
symt.Type = obj.SSYMTAB
|
2015-02-27 22:57:28 -05:00
|
|
|
symt.Size = 0
|
|
|
|
|
symt.Reachable = true
|
|
|
|
|
|
2015-03-16 11:53:08 +13:00
|
|
|
ntypelinks := 0
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
// assign specific types so that they sort together.
|
|
|
|
|
// within a type they sort by size, so the .* symbols
|
|
|
|
|
// just defined above will be first.
|
|
|
|
|
// hide the specific symbols.
|
2015-03-02 12:35:15 -05:00
|
|
|
for s := Ctxt.Allsym; s != nil; s = s.Allsym {
|
2015-04-18 08:14:08 +12:00
|
|
|
if !s.Reachable || s.Special != 0 || s.Type != obj.SRODATA {
|
2015-04-11 14:04:17 +12:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
if strings.HasPrefix(s.Name, "type.") && !DynlinkingGo() {
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Hide = 1
|
2015-05-21 13:07:19 +12:00
|
|
|
if UseRelro() && len(s.R) > 0 {
|
|
|
|
|
s.Type = obj.STYPERELRO
|
|
|
|
|
s.Outer = symtyperel
|
|
|
|
|
} else {
|
|
|
|
|
s.Type = obj.STYPE
|
|
|
|
|
s.Outer = symtype
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(s.Name, "go.typelink.") {
|
2015-03-16 11:53:08 +13:00
|
|
|
ntypelinks++
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.STYPELINK
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Hide = 1
|
|
|
|
|
s.Outer = symtypelink
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(s.Name, "go.string.") {
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SGOSTRING
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Hide = 1
|
|
|
|
|
s.Outer = symgostring
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-22 14:59:56 -04:00
|
|
|
if strings.HasPrefix(s.Name, "runtime.gcbits.") {
|
|
|
|
|
s.Type = obj.SGCBITS
|
|
|
|
|
s.Hide = 1
|
|
|
|
|
s.Outer = symgcbits
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
if strings.HasPrefix(s.Name, "go.func.") {
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SGOFUNC
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Hide = 1
|
|
|
|
|
s.Outer = symgofunc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(s.Name, "gcargs.") || strings.HasPrefix(s.Name, "gclocals.") || strings.HasPrefix(s.Name, "gclocals·") {
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = obj.SGOFUNC
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Hide = 1
|
|
|
|
|
s.Outer = symgofunc
|
|
|
|
|
s.Align = 4
|
|
|
|
|
liveness += (s.Size + int64(s.Align) - 1) &^ (int64(s.Align) - 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-12 12:22:18 +13:00
|
|
|
|
2015-04-11 12:05:21 +08:00
|
|
|
if Buildmode == BuildmodeShared {
|
|
|
|
|
abihashgostr := Linklookup(Ctxt, "go.link.abihash."+filepath.Base(outfile), 0)
|
|
|
|
|
abihashgostr.Reachable = true
|
|
|
|
|
abihashgostr.Type = obj.SRODATA
|
2015-05-25 13:59:08 +12:00
|
|
|
hashsym := Linklookup(Ctxt, "go.link.abihashbytes", 0)
|
|
|
|
|
Addaddr(Ctxt, abihashgostr, hashsym)
|
|
|
|
|
adduint(Ctxt, abihashgostr, uint64(hashsym.Size))
|
2015-04-11 12:05:21 +08:00
|
|
|
}
|
|
|
|
|
|
2015-03-12 12:22:18 +13:00
|
|
|
// Information about the layout of the executable image for the
|
|
|
|
|
// runtime to use. Any changes here must be matched by changes to
|
|
|
|
|
// the definition of moduledata in runtime/symtab.go.
|
2015-03-16 11:53:08 +13:00
|
|
|
// This code uses several global variables that are set by pcln.go:pclntab.
|
2015-08-19 13:34:33 +12:00
|
|
|
moduledata := Ctxt.Moduledata
|
2015-03-16 11:53:08 +13:00
|
|
|
// The pclntab slice
|
2015-03-12 12:22:18 +13:00
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.pclntab", 0))
|
2015-03-16 11:53:08 +13:00
|
|
|
adduint(Ctxt, moduledata, uint64(Linklookup(Ctxt, "runtime.pclntab", 0).Size))
|
|
|
|
|
adduint(Ctxt, moduledata, uint64(Linklookup(Ctxt, "runtime.pclntab", 0).Size))
|
|
|
|
|
// The ftab slice
|
|
|
|
|
Addaddrplus(Ctxt, moduledata, Linklookup(Ctxt, "runtime.pclntab", 0), int64(pclntabPclntabOffset))
|
|
|
|
|
adduint(Ctxt, moduledata, uint64(pclntabNfunc+1))
|
|
|
|
|
adduint(Ctxt, moduledata, uint64(pclntabNfunc+1))
|
|
|
|
|
// The filetab slice
|
|
|
|
|
Addaddrplus(Ctxt, moduledata, Linklookup(Ctxt, "runtime.pclntab", 0), int64(pclntabFiletabOffset))
|
2015-10-27 14:49:51 +13:00
|
|
|
adduint(Ctxt, moduledata, uint64(Ctxt.Nhistfile)+1)
|
|
|
|
|
adduint(Ctxt, moduledata, uint64(Ctxt.Nhistfile)+1)
|
2015-03-16 11:53:08 +13:00
|
|
|
// findfunctab
|
2015-03-12 12:22:18 +13:00
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.findfunctab", 0))
|
2015-03-16 11:53:08 +13:00
|
|
|
// minpc, maxpc
|
|
|
|
|
Addaddr(Ctxt, moduledata, pclntabFirstFunc)
|
|
|
|
|
Addaddrplus(Ctxt, moduledata, pclntabLastFunc, pclntabLastFunc.Size)
|
|
|
|
|
// pointers to specific parts of the module
|
2015-03-12 12:22:18 +13:00
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.text", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.etext", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.noptrdata", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.enoptrdata", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.data", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.edata", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.bss", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.ebss", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.noptrbss", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.enoptrbss", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.end", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.gcdata", 0))
|
|
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.gcbss", 0))
|
2015-03-16 11:53:08 +13:00
|
|
|
// The typelinks slice
|
2015-03-12 12:22:18 +13:00
|
|
|
Addaddr(Ctxt, moduledata, Linklookup(Ctxt, "runtime.typelink", 0))
|
2015-03-16 11:53:08 +13:00
|
|
|
adduint(Ctxt, moduledata, uint64(ntypelinks))
|
|
|
|
|
adduint(Ctxt, moduledata, uint64(ntypelinks))
|
2015-04-11 12:05:21 +08:00
|
|
|
if len(Ctxt.Shlibs) > 0 {
|
|
|
|
|
thismodulename := filepath.Base(outfile)
|
2015-10-19 12:53:36 -04:00
|
|
|
switch Buildmode {
|
|
|
|
|
case BuildmodeExe, BuildmodePIE:
|
2015-04-11 12:05:21 +08:00
|
|
|
// When linking an executable, outfile is just "a.out". Make
|
|
|
|
|
// it something slightly more comprehensible.
|
|
|
|
|
thismodulename = "the executable"
|
|
|
|
|
}
|
|
|
|
|
addgostring(moduledata, "go.link.thismodulename", thismodulename)
|
|
|
|
|
|
|
|
|
|
modulehashes := Linklookup(Ctxt, "go.link.abihashes", 0)
|
|
|
|
|
modulehashes.Reachable = true
|
|
|
|
|
modulehashes.Local = true
|
|
|
|
|
modulehashes.Type = obj.SRODATA
|
|
|
|
|
|
|
|
|
|
for i, shlib := range Ctxt.Shlibs {
|
|
|
|
|
// modulehashes[i].modulename
|
|
|
|
|
modulename := filepath.Base(shlib.Path)
|
|
|
|
|
addgostring(modulehashes, fmt.Sprintf("go.link.libname.%d", i), modulename)
|
|
|
|
|
|
|
|
|
|
// modulehashes[i].linktimehash
|
|
|
|
|
addgostring(modulehashes, fmt.Sprintf("go.link.linkhash.%d", i), string(shlib.Hash))
|
|
|
|
|
|
|
|
|
|
// modulehashes[i].runtimehash
|
|
|
|
|
abihash := Linklookup(Ctxt, "go.link.abihash."+modulename, 0)
|
|
|
|
|
abihash.Reachable = true
|
|
|
|
|
Addaddr(Ctxt, modulehashes, abihash)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Addaddr(Ctxt, moduledata, modulehashes)
|
|
|
|
|
adduint(Ctxt, moduledata, uint64(len(Ctxt.Shlibs)))
|
|
|
|
|
adduint(Ctxt, moduledata, uint64(len(Ctxt.Shlibs)))
|
|
|
|
|
}
|
2015-03-29 21:59:00 +00:00
|
|
|
// The rest of moduledata is zero initialized.
|
2015-04-07 14:51:29 +12:00
|
|
|
// When linking an object that does not contain the runtime we are
|
|
|
|
|
// creating the moduledata from scratch and it does not have a
|
|
|
|
|
// compiler-provided size, so read it from the type data.
|
|
|
|
|
moduledatatype := Linkrlookup(Ctxt, "type.runtime.moduledata", 0)
|
|
|
|
|
moduledata.Size = decodetype_size(moduledatatype)
|
|
|
|
|
Symgrow(Ctxt, moduledata, moduledata.Size)
|
2015-04-01 14:17:43 +13:00
|
|
|
|
|
|
|
|
lastmoduledatap := Linklookup(Ctxt, "runtime.lastmoduledatap", 0)
|
2015-04-19 19:33:58 -07:00
|
|
|
if lastmoduledatap.Type != obj.SDYNIMPORT {
|
|
|
|
|
lastmoduledatap.Type = obj.SNOPTRDATA
|
2015-04-01 14:17:43 +13:00
|
|
|
lastmoduledatap.Size = 0 // overwrite existing value
|
|
|
|
|
Addaddr(Ctxt, lastmoduledatap, moduledata)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|