2015-02-27 22:57:28 -05:00
|
|
|
// Inferno utils/8l/asm.c
|
|
|
|
|
// http://code.google.com/p/inferno-os/source/browse/utils/8l/asm.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.
|
|
|
|
|
|
2015-05-21 13:28:10 -04:00
|
|
|
package x86
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"cmd/internal/obj"
|
2015-05-21 13:28:10 -04:00
|
|
|
"cmd/link/internal/ld"
|
2015-02-27 22:57:28 -05:00
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func gentext() {
|
2015-10-28 11:40:22 +13:00
|
|
|
if !ld.DynlinkingGo() && ld.Buildmode != ld.BuildmodePIE {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thunkfunc := ld.Linklookup(ld.Ctxt, "__x86.get_pc_thunk.cx", 0)
|
|
|
|
|
thunkfunc.Type = obj.STEXT
|
|
|
|
|
thunkfunc.Local = true
|
|
|
|
|
thunkfunc.Reachable = true
|
|
|
|
|
o := func(op ...uint8) {
|
|
|
|
|
for _, op1 := range op {
|
|
|
|
|
ld.Adduint8(ld.Ctxt, thunkfunc, op1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 8b 0c 24 mov (%esp),%ecx
|
|
|
|
|
o(0x8b, 0x0c, 0x24)
|
|
|
|
|
// c3 ret
|
|
|
|
|
o(0xc3)
|
|
|
|
|
|
|
|
|
|
if ld.Ctxt.Etextp != nil {
|
|
|
|
|
ld.Ctxt.Etextp.Next = thunkfunc
|
|
|
|
|
} else {
|
|
|
|
|
ld.Ctxt.Textp = thunkfunc
|
|
|
|
|
}
|
|
|
|
|
ld.Ctxt.Etextp = thunkfunc
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func adddynrela(rela *ld.LSym, s *ld.LSym, r *ld.Reloc) {
|
|
|
|
|
log.Fatalf("adddynrela not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func adddynrel(s *ld.LSym, r *ld.Reloc) {
|
2015-03-02 12:35:15 -05:00
|
|
|
targ := r.Sym
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Ctxt.Cursym = s
|
|
|
|
|
|
|
|
|
|
switch r.Type {
|
|
|
|
|
default:
|
|
|
|
|
if r.Type >= 256 {
|
|
|
|
|
ld.Diag("unexpected relocation type %d", r.Type)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle relocations found in ELF object files.
|
|
|
|
|
case 256 + ld.R_386_PC32:
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type == obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Diag("unexpected R_386_PC32 relocation for dynamic symbol %s", targ.Name)
|
|
|
|
|
}
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type == 0 || targ.Type == obj.SXREF {
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Diag("unknown symbol %s in pcrel", targ.Name)
|
|
|
|
|
}
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_PCREL
|
2015-02-27 22:57:28 -05:00
|
|
|
r.Add += 4
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 256 + ld.R_386_PLT32:
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_PCREL
|
2015-02-27 22:57:28 -05:00
|
|
|
r.Add += 4
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type == obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
addpltsym(ld.Ctxt, targ)
|
|
|
|
|
r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
|
|
|
|
|
r.Add += int64(targ.Plt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
2015-10-31 14:44:48 -07:00
|
|
|
case 256 + ld.R_386_GOT32, 256 + ld.R_386_GOT32X:
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type != obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
// have symbol
|
|
|
|
|
if r.Off >= 2 && s.P[r.Off-2] == 0x8b {
|
|
|
|
|
// turn MOVL of GOT entry into LEAL of symbol address, relative to GOT.
|
|
|
|
|
s.P[r.Off-2] = 0x8d
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_GOTOFF
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.Off >= 2 && s.P[r.Off-2] == 0xff && s.P[r.Off-1] == 0xb3 {
|
|
|
|
|
// turn PUSHL of GOT entry into PUSHL of symbol itself.
|
|
|
|
|
// use unnecessary SS prefix to keep instruction same length.
|
|
|
|
|
s.P[r.Off-2] = 0x36
|
|
|
|
|
|
|
|
|
|
s.P[r.Off-1] = 0x68
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_ADDR
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Diag("unexpected GOT reloc for non-dynamic symbol %s", targ.Name)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addgotsym(ld.Ctxt, targ)
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_CONST // write r->add during relocsym
|
2015-02-27 22:57:28 -05:00
|
|
|
r.Sym = nil
|
|
|
|
|
r.Add += int64(targ.Got)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 256 + ld.R_386_GOTOFF:
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_GOTOFF
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 256 + ld.R_386_GOTPC:
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_PCREL
|
2015-02-27 22:57:28 -05:00
|
|
|
r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0)
|
|
|
|
|
r.Add += 4
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 256 + ld.R_386_32:
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type == obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Diag("unexpected R_386_32 relocation for dynamic symbol %s", targ.Name)
|
|
|
|
|
}
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_ADDR
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 512 + ld.MACHO_GENERIC_RELOC_VANILLA*2 + 0:
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_ADDR
|
|
|
|
|
if targ.Type == obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Diag("unexpected reloc for dynamic symbol %s", targ.Name)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 512 + ld.MACHO_GENERIC_RELOC_VANILLA*2 + 1:
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type == obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
addpltsym(ld.Ctxt, targ)
|
|
|
|
|
r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
|
|
|
|
|
r.Add = int64(targ.Plt)
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_PCREL
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_PCREL
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
case 512 + ld.MACHO_FAKE_GOTPCREL:
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type != obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
// have symbol
|
|
|
|
|
// turn MOVL of GOT entry into LEAL of symbol itself
|
|
|
|
|
if r.Off < 2 || s.P[r.Off-2] != 0x8b {
|
|
|
|
|
ld.Diag("unexpected GOT reloc for non-dynamic symbol %s", targ.Name)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.P[r.Off-2] = 0x8d
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_PCREL
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addgotsym(ld.Ctxt, targ)
|
|
|
|
|
r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0)
|
|
|
|
|
r.Add += int64(targ.Got)
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_PCREL
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle references to ELF symbols from our own object files.
|
2015-04-19 19:33:58 -07:00
|
|
|
if targ.Type != obj.SDYNIMPORT {
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch r.Type {
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_CALL,
|
|
|
|
|
obj.R_PCREL:
|
2015-02-27 22:57:28 -05:00
|
|
|
addpltsym(ld.Ctxt, targ)
|
|
|
|
|
r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
|
|
|
|
|
r.Add = int64(targ.Plt)
|
|
|
|
|
return
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_ADDR:
|
|
|
|
|
if s.Type != obj.SDATA {
|
2015-02-27 22:57:28 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if ld.Iself {
|
2015-05-12 15:59:15 +12:00
|
|
|
ld.Adddynsym(ld.Ctxt, targ)
|
2015-03-02 12:35:15 -05:00
|
|
|
rel := ld.Linklookup(ld.Ctxt, ".rel", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Addaddrplus(ld.Ctxt, rel, s, int64(r.Off))
|
|
|
|
|
ld.Adduint32(ld.Ctxt, rel, ld.ELF32_R_INFO(uint32(targ.Dynid), ld.R_386_32))
|
2015-04-19 19:33:58 -07:00
|
|
|
r.Type = obj.R_CONST // write r->add during relocsym
|
2015-02-27 22:57:28 -05:00
|
|
|
r.Sym = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
if ld.HEADTYPE == obj.Hdarwin && s.Size == PtrSize && r.Off == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
// Mach-O relocations are a royal pain to lay out.
|
|
|
|
|
// They use a compact stateful bytecode representation
|
|
|
|
|
// that is too much bother to deal with.
|
|
|
|
|
// Instead, interpret the C declaration
|
|
|
|
|
// void *_Cvar_stderr = &stderr;
|
|
|
|
|
// as making _Cvar_stderr the name of a GOT entry
|
|
|
|
|
// for stderr. This is separate from the usual GOT entry,
|
|
|
|
|
// just in case the C code assigns to the variable,
|
|
|
|
|
// and of course it only works for single pointers,
|
|
|
|
|
// but we only need to support cgo and that's all it needs.
|
2015-05-12 15:59:15 +12:00
|
|
|
ld.Adddynsym(ld.Ctxt, targ)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
got := ld.Linklookup(ld.Ctxt, ".got", 0)
|
2015-04-19 19:33:58 -07:00
|
|
|
s.Type = got.Type | obj.SSUB
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Outer = got
|
|
|
|
|
s.Sub = got.Sub
|
|
|
|
|
got.Sub = s
|
|
|
|
|
s.Value = got.Size
|
|
|
|
|
ld.Adduint32(ld.Ctxt, got, 0)
|
|
|
|
|
ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(targ.Dynid))
|
|
|
|
|
r.Type = 256 // ignore during relocsym
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-03-09 03:05:40 -04:00
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
if ld.HEADTYPE == obj.Hwindows && s.Size == PtrSize {
|
2015-03-09 03:05:40 -04:00
|
|
|
// nothing to do, the relocation will be laid out in pereloc1
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Ctxt.Cursym = s
|
|
|
|
|
ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func elfreloc1(r *ld.Reloc, sectoff int64) int {
|
|
|
|
|
ld.Thearch.Lput(uint32(sectoff))
|
|
|
|
|
|
2015-10-29 12:17:43 +13:00
|
|
|
elfsym := r.Xsym.ElfsymForReloc()
|
2015-02-27 22:57:28 -05:00
|
|
|
switch r.Type {
|
|
|
|
|
default:
|
|
|
|
|
return -1
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_ADDR:
|
2015-02-27 22:57:28 -05:00
|
|
|
if r.Siz == 4 {
|
|
|
|
|
ld.Thearch.Lput(ld.R_386_32 | uint32(elfsym)<<8)
|
|
|
|
|
} else {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_CALL,
|
|
|
|
|
obj.R_PCREL:
|
2015-02-27 22:57:28 -05:00
|
|
|
if r.Siz == 4 {
|
|
|
|
|
ld.Thearch.Lput(ld.R_386_PC32 | uint32(elfsym)<<8)
|
|
|
|
|
} else {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-23 21:53:48 +12:00
|
|
|
case obj.R_TLS_LE:
|
2015-02-27 22:57:28 -05:00
|
|
|
if r.Siz == 4 {
|
|
|
|
|
ld.Thearch.Lput(ld.R_386_TLS_LE | uint32(elfsym)<<8)
|
|
|
|
|
} else {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2015-10-28 11:40:22 +13:00
|
|
|
|
|
|
|
|
case obj.R_TLS_IE:
|
|
|
|
|
if r.Siz == 4 {
|
|
|
|
|
ld.Thearch.Lput(ld.R_386_GOTPC)
|
|
|
|
|
ld.Thearch.Lput(uint32(sectoff))
|
|
|
|
|
ld.Thearch.Lput(ld.R_386_TLS_GOTIE | uint32(elfsym)<<8)
|
|
|
|
|
} else {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func machoreloc1(r *ld.Reloc, sectoff int64) int {
|
|
|
|
|
var v uint32
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
rs := r.Xsym
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
if rs.Type == obj.SHOSTOBJ {
|
2015-02-27 22:57:28 -05:00
|
|
|
if rs.Dynid < 0 {
|
|
|
|
|
ld.Diag("reloc %d to non-macho symbol %s type=%d", r.Type, rs.Name, rs.Type)
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = uint32(rs.Dynid)
|
|
|
|
|
v |= 1 << 27 // external relocation
|
|
|
|
|
} else {
|
2015-05-27 12:04:25 +12:00
|
|
|
v = uint32(rs.Sect.Extnum)
|
2015-02-27 22:57:28 -05:00
|
|
|
if v == 0 {
|
2015-05-27 12:04:25 +12:00
|
|
|
ld.Diag("reloc %d to symbol %s in non-macho section %s type=%d", r.Type, rs.Name, rs.Sect.Name, rs.Type)
|
2015-02-27 22:57:28 -05:00
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch r.Type {
|
|
|
|
|
default:
|
|
|
|
|
return -1
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_ADDR:
|
2015-02-27 22:57:28 -05:00
|
|
|
v |= ld.MACHO_GENERIC_RELOC_VANILLA << 28
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_CALL,
|
|
|
|
|
obj.R_PCREL:
|
2015-02-27 22:57:28 -05:00
|
|
|
v |= 1 << 24 // pc-relative bit
|
|
|
|
|
v |= ld.MACHO_GENERIC_RELOC_VANILLA << 28
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch r.Siz {
|
|
|
|
|
default:
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
v |= 0 << 25
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
v |= 1 << 25
|
|
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
v |= 2 << 25
|
|
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
|
v |= 3 << 25
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Thearch.Lput(uint32(sectoff))
|
|
|
|
|
ld.Thearch.Lput(v)
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 03:05:40 -04:00
|
|
|
func pereloc1(r *ld.Reloc, sectoff int64) bool {
|
|
|
|
|
var v uint32
|
|
|
|
|
|
|
|
|
|
rs := r.Xsym
|
|
|
|
|
|
|
|
|
|
if rs.Dynid < 0 {
|
|
|
|
|
ld.Diag("reloc %d to non-coff symbol %s type=%d", r.Type, rs.Name, rs.Type)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Thearch.Lput(uint32(sectoff))
|
|
|
|
|
ld.Thearch.Lput(uint32(rs.Dynid))
|
|
|
|
|
|
|
|
|
|
switch r.Type {
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_ADDR:
|
2015-03-09 03:05:40 -04:00
|
|
|
v = ld.IMAGE_REL_I386_DIR32
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_CALL,
|
|
|
|
|
obj.R_PCREL:
|
2015-03-09 03:05:40 -04:00
|
|
|
v = ld.IMAGE_REL_I386_REL32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Thearch.Wput(uint16(v))
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
|
|
|
|
|
if ld.Linkmode == ld.LinkExternal {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
switch r.Type {
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_CONST:
|
2015-02-27 22:57:28 -05:00
|
|
|
*val = r.Add
|
|
|
|
|
return 0
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.R_GOTOFF:
|
2015-02-27 22:57:28 -05:00
|
|
|
*val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ld.Linklookup(ld.Ctxt, ".got", 0))
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func archrelocvariant(r *ld.Reloc, s *ld.LSym, t int64) int64 {
|
|
|
|
|
log.Fatalf("unexpected relocation variant")
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func elfsetupplt() {
|
2015-03-02 12:35:15 -05:00
|
|
|
plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
|
|
|
|
|
got := ld.Linklookup(ld.Ctxt, ".got.plt", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
if plt.Size == 0 {
|
|
|
|
|
// pushl got+4
|
|
|
|
|
ld.Adduint8(ld.Ctxt, plt, 0xff)
|
|
|
|
|
|
|
|
|
|
ld.Adduint8(ld.Ctxt, plt, 0x35)
|
|
|
|
|
ld.Addaddrplus(ld.Ctxt, plt, got, 4)
|
|
|
|
|
|
|
|
|
|
// jmp *got+8
|
|
|
|
|
ld.Adduint8(ld.Ctxt, plt, 0xff)
|
|
|
|
|
|
|
|
|
|
ld.Adduint8(ld.Ctxt, plt, 0x25)
|
|
|
|
|
ld.Addaddrplus(ld.Ctxt, plt, got, 8)
|
|
|
|
|
|
|
|
|
|
// zero pad
|
|
|
|
|
ld.Adduint32(ld.Ctxt, plt, 0)
|
|
|
|
|
|
|
|
|
|
// assume got->size == 0 too
|
|
|
|
|
ld.Addaddrplus(ld.Ctxt, got, ld.Linklookup(ld.Ctxt, ".dynamic", 0), 0)
|
|
|
|
|
|
|
|
|
|
ld.Adduint32(ld.Ctxt, got, 0)
|
|
|
|
|
ld.Adduint32(ld.Ctxt, got, 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addpltsym(ctxt *ld.Link, s *ld.LSym) {
|
|
|
|
|
if s.Plt >= 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 15:59:15 +12:00
|
|
|
ld.Adddynsym(ctxt, s)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if ld.Iself {
|
2015-03-02 12:35:15 -05:00
|
|
|
plt := ld.Linklookup(ctxt, ".plt", 0)
|
|
|
|
|
got := ld.Linklookup(ctxt, ".got.plt", 0)
|
|
|
|
|
rel := ld.Linklookup(ctxt, ".rel.plt", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
if plt.Size == 0 {
|
|
|
|
|
elfsetupplt()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// jmpq *got+size
|
|
|
|
|
ld.Adduint8(ctxt, plt, 0xff)
|
|
|
|
|
|
|
|
|
|
ld.Adduint8(ctxt, plt, 0x25)
|
|
|
|
|
ld.Addaddrplus(ctxt, plt, got, got.Size)
|
|
|
|
|
|
|
|
|
|
// add to got: pointer to current pos in plt
|
|
|
|
|
ld.Addaddrplus(ctxt, got, plt, plt.Size)
|
|
|
|
|
|
|
|
|
|
// pushl $x
|
|
|
|
|
ld.Adduint8(ctxt, plt, 0x68)
|
|
|
|
|
|
|
|
|
|
ld.Adduint32(ctxt, plt, uint32(rel.Size))
|
|
|
|
|
|
|
|
|
|
// jmp .plt
|
|
|
|
|
ld.Adduint8(ctxt, plt, 0xe9)
|
|
|
|
|
|
|
|
|
|
ld.Adduint32(ctxt, plt, uint32(-(plt.Size + 4)))
|
|
|
|
|
|
|
|
|
|
// rel
|
|
|
|
|
ld.Addaddrplus(ctxt, rel, got, got.Size-4)
|
|
|
|
|
|
|
|
|
|
ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_386_JMP_SLOT))
|
|
|
|
|
|
|
|
|
|
s.Plt = int32(plt.Size - 16)
|
2015-04-19 19:33:58 -07:00
|
|
|
} else if ld.HEADTYPE == obj.Hdarwin {
|
2015-02-27 22:57:28 -05:00
|
|
|
// Same laziness as in 6l.
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
plt := ld.Linklookup(ctxt, ".plt", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
addgotsym(ctxt, s)
|
|
|
|
|
|
|
|
|
|
ld.Adduint32(ctxt, ld.Linklookup(ctxt, ".linkedit.plt", 0), uint32(s.Dynid))
|
|
|
|
|
|
|
|
|
|
// jmpq *got+size(IP)
|
|
|
|
|
s.Plt = int32(plt.Size)
|
|
|
|
|
|
|
|
|
|
ld.Adduint8(ctxt, plt, 0xff)
|
|
|
|
|
ld.Adduint8(ctxt, plt, 0x25)
|
|
|
|
|
ld.Addaddrplus(ctxt, plt, ld.Linklookup(ctxt, ".got", 0), int64(s.Got))
|
|
|
|
|
} else {
|
|
|
|
|
ld.Diag("addpltsym: unsupported binary format")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addgotsym(ctxt *ld.Link, s *ld.LSym) {
|
|
|
|
|
if s.Got >= 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-12 15:59:15 +12:00
|
|
|
ld.Adddynsym(ctxt, s)
|
2015-03-02 12:35:15 -05:00
|
|
|
got := ld.Linklookup(ctxt, ".got", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Got = int32(got.Size)
|
|
|
|
|
ld.Adduint32(ctxt, got, 0)
|
|
|
|
|
|
|
|
|
|
if ld.Iself {
|
2015-03-02 12:35:15 -05:00
|
|
|
rel := ld.Linklookup(ctxt, ".rel", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Addaddrplus(ctxt, rel, got, int64(s.Got))
|
|
|
|
|
ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_386_GLOB_DAT))
|
2015-04-19 19:33:58 -07:00
|
|
|
} else if ld.HEADTYPE == obj.Hdarwin {
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Adduint32(ctxt, ld.Linklookup(ctxt, ".linkedit.got", 0), uint32(s.Dynid))
|
|
|
|
|
} else {
|
|
|
|
|
ld.Diag("addgotsym: unsupported binary format")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func asmb() {
|
|
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f asmb\n", obj.Cputime())
|
|
|
|
|
}
|
2015-05-02 12:44:49 +10:00
|
|
|
ld.Bso.Flush()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if ld.Iself {
|
|
|
|
|
ld.Asmbelfsetup()
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
sect := ld.Segtext.Sect
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
|
|
|
|
|
ld.Codeblk(int64(sect.Vaddr), int64(sect.Length))
|
|
|
|
|
for sect = sect.Next; sect != nil; sect = sect.Next {
|
|
|
|
|
ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
|
|
|
|
|
ld.Datblk(int64(sect.Vaddr), int64(sect.Length))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ld.Segrodata.Filelen > 0 {
|
|
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
|
|
|
|
|
}
|
2015-05-02 12:44:49 +10:00
|
|
|
ld.Bso.Flush()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
ld.Cseek(int64(ld.Segrodata.Fileoff))
|
|
|
|
|
ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f datblk\n", obj.Cputime())
|
|
|
|
|
}
|
2015-05-02 12:44:49 +10:00
|
|
|
ld.Bso.Flush()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
ld.Cseek(int64(ld.Segdata.Fileoff))
|
|
|
|
|
ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
machlink := uint32(0)
|
2015-04-19 19:33:58 -07:00
|
|
|
if ld.HEADTYPE == obj.Hdarwin {
|
2015-02-27 22:57:28 -05:00
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime())
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
dwarfoff := uint32(ld.Rnd(int64(uint64(ld.HEADR)+ld.Segtext.Length), int64(ld.INITRND)) + ld.Rnd(int64(ld.Segdata.Filelen), int64(ld.INITRND)))
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Cseek(int64(dwarfoff))
|
|
|
|
|
|
|
|
|
|
ld.Segdwarf.Fileoff = uint64(ld.Cpos())
|
|
|
|
|
ld.Dwarfemitdebugsections()
|
|
|
|
|
ld.Segdwarf.Filelen = uint64(ld.Cpos()) - ld.Segdwarf.Fileoff
|
|
|
|
|
|
|
|
|
|
machlink = uint32(ld.Domacholink())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Symsize = 0
|
|
|
|
|
ld.Spsize = 0
|
|
|
|
|
ld.Lcsize = 0
|
2015-03-02 12:35:15 -05:00
|
|
|
symo := uint32(0)
|
2015-02-27 22:57:28 -05:00
|
|
|
if ld.Debug['s'] == 0 {
|
|
|
|
|
// TODO: rationalize
|
|
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime())
|
|
|
|
|
}
|
2015-05-02 12:44:49 +10:00
|
|
|
ld.Bso.Flush()
|
2015-02-27 22:57:28 -05:00
|
|
|
switch ld.HEADTYPE {
|
|
|
|
|
default:
|
|
|
|
|
if ld.Iself {
|
|
|
|
|
symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
|
|
|
|
|
symo = uint32(ld.Rnd(int64(symo), int64(ld.INITRND)))
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hplan9:
|
2015-02-27 22:57:28 -05:00
|
|
|
symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hdarwin:
|
2015-04-08 12:55:34 -07:00
|
|
|
symo = uint32(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(ld.INITRND))) + uint64(machlink))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hwindows:
|
2015-02-27 22:57:28 -05:00
|
|
|
symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
|
|
|
|
|
symo = uint32(ld.Rnd(int64(symo), ld.PEFILEALIGN))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Cseek(int64(symo))
|
|
|
|
|
switch ld.HEADTYPE {
|
|
|
|
|
default:
|
|
|
|
|
if ld.Iself {
|
|
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f elfsym\n", obj.Cputime())
|
|
|
|
|
}
|
|
|
|
|
ld.Asmelfsym()
|
|
|
|
|
ld.Cflush()
|
|
|
|
|
ld.Cwrite(ld.Elfstrdat)
|
|
|
|
|
|
|
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime())
|
|
|
|
|
}
|
|
|
|
|
ld.Dwarfemitdebugsections()
|
|
|
|
|
|
|
|
|
|
if ld.Linkmode == ld.LinkExternal {
|
|
|
|
|
ld.Elfemitreloc()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hplan9:
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Asmplan9sym()
|
|
|
|
|
ld.Cflush()
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
sym := ld.Linklookup(ld.Ctxt, "pclntab", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
if sym != nil {
|
|
|
|
|
ld.Lcsize = int32(len(sym.P))
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; int32(i) < ld.Lcsize; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Cput(uint8(sym.P[i]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Cflush()
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hwindows:
|
2015-02-27 22:57:28 -05:00
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime())
|
|
|
|
|
}
|
|
|
|
|
ld.Dwarfemitdebugsections()
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hdarwin:
|
2015-02-27 22:57:28 -05:00
|
|
|
if ld.Linkmode == ld.LinkExternal {
|
|
|
|
|
ld.Machoemitreloc()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ld.Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&ld.Bso, "%5.2f headr\n", obj.Cputime())
|
|
|
|
|
}
|
2015-05-02 12:44:49 +10:00
|
|
|
ld.Bso.Flush()
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Cseek(0)
|
|
|
|
|
switch ld.HEADTYPE {
|
|
|
|
|
default:
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hplan9: /* plan9 */
|
2015-03-02 12:35:15 -05:00
|
|
|
magic := int32(4*11*11 + 7)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
ld.Lputb(uint32(magic)) /* magic */
|
|
|
|
|
ld.Lputb(uint32(ld.Segtext.Filelen)) /* sizes */
|
|
|
|
|
ld.Lputb(uint32(ld.Segdata.Filelen))
|
|
|
|
|
ld.Lputb(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
|
|
|
|
|
ld.Lputb(uint32(ld.Symsize)) /* nsyms */
|
|
|
|
|
ld.Lputb(uint32(ld.Entryvalue())) /* va of entry */
|
|
|
|
|
ld.Lputb(uint32(ld.Spsize)) /* sp offsets */
|
|
|
|
|
ld.Lputb(uint32(ld.Lcsize)) /* line offsets */
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hdarwin:
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Asmbmacho()
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hlinux,
|
|
|
|
|
obj.Hfreebsd,
|
|
|
|
|
obj.Hnetbsd,
|
|
|
|
|
obj.Hopenbsd,
|
|
|
|
|
obj.Hnacl:
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Asmbelf(int64(symo))
|
|
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.Hwindows:
|
2015-02-27 22:57:28 -05:00
|
|
|
ld.Asmbpe()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ld.Cflush()
|
|
|
|
|
}
|