Esempio n. 1
0
func elfsetupplt() {
	plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
	got := ld.Linklookup(ld.Ctxt, ".got.plt", 0)
	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)
	}
}
Esempio n. 2
0
File: asm.go Progetto: tidatida/go
func elfsetupplt() {
	plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
	got := ld.Linklookup(ld.Ctxt, ".got.plt", 0)
	if plt.Size == 0 {
		// pushq got+8(IP)
		ld.Adduint8(ld.Ctxt, plt, 0xff)

		ld.Adduint8(ld.Ctxt, plt, 0x35)
		ld.Addpcrelplus(ld.Ctxt, plt, got, 8)

		// jmpq got+16(IP)
		ld.Adduint8(ld.Ctxt, plt, 0xff)

		ld.Adduint8(ld.Ctxt, plt, 0x25)
		ld.Addpcrelplus(ld.Ctxt, plt, got, 16)

		// nopl 0(AX)
		ld.Adduint32(ld.Ctxt, plt, 0x00401f0f)

		// assume got->size == 0 too
		ld.Addaddrplus(ld.Ctxt, got, ld.Linklookup(ld.Ctxt, ".dynamic", 0), 0)

		ld.Adduint64(ld.Ctxt, got, 0)
		ld.Adduint64(ld.Ctxt, got, 0)
	}
}
Esempio n. 3
0
File: asm.go Progetto: tidatida/go
func adddynsym(ctxt *ld.Link, s *ld.LSym) {
	if s.Dynid >= 0 {
		return
	}

	if ld.Iself {
		s.Dynid = int32(ld.Nelfsym)
		ld.Nelfsym++

		d := ld.Linklookup(ctxt, ".dynsym", 0)

		name := s.Extname
		ld.Adduint32(ctxt, d, uint32(ld.Addstring(ld.Linklookup(ctxt, ".dynstr", 0), name)))

		/* type */
		t := ld.STB_GLOBAL << 4

		if s.Cgoexport != 0 && s.Type&ld.SMASK == ld.STEXT {
			t |= ld.STT_FUNC
		} else {
			t |= ld.STT_OBJECT
		}
		ld.Adduint8(ctxt, d, uint8(t))

		/* reserved */
		ld.Adduint8(ctxt, d, 0)

		/* section where symbol is defined */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint16(ctxt, d, ld.SHN_UNDEF)
		} else {
			ld.Adduint16(ctxt, d, 1)
		}

		/* value */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint64(ctxt, d, 0)
		} else {
			ld.Addaddr(ctxt, d, s)
		}

		/* size of object */
		ld.Adduint64(ctxt, d, uint64(s.Size))

		if s.Cgoexport&ld.CgoExportDynamic == 0 && s.Dynimplib != "" && needlib(s.Dynimplib) != 0 {
			ld.Elfwritedynent(ld.Linklookup(ctxt, ".dynamic", 0), ld.DT_NEEDED, uint64(ld.Addstring(ld.Linklookup(ctxt, ".dynstr", 0), s.Dynimplib)))
		}
	} else if ld.HEADTYPE == ld.Hdarwin {
		ld.Diag("adddynsym: missed symbol %s (%s)", s.Name, s.Extname)
	} else if ld.HEADTYPE == ld.Hwindows {
	} else // already taken care of
	{
		ld.Diag("adddynsym: unsupported binary format")
	}
}
Esempio n. 4
0
func adddynsym(ctxt *ld.Link, s *ld.LSym) {
	if s.Dynid >= 0 {
		return
	}

	if ld.Iself {
		s.Dynid = int32(ld.Nelfsym)
		ld.Nelfsym++

		d := ld.Linklookup(ctxt, ".dynsym", 0)

		/* name */
		name := s.Extname

		ld.Adduint32(ctxt, d, uint32(ld.Addstring(ld.Linklookup(ctxt, ".dynstr", 0), name)))

		/* value */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint32(ctxt, d, 0)
		} else {
			ld.Addaddr(ctxt, d, s)
		}

		/* size */
		ld.Adduint32(ctxt, d, 0)

		/* type */
		t := ld.STB_GLOBAL << 4

		if s.Cgoexport != 0 && s.Type&ld.SMASK == ld.STEXT {
			t |= ld.STT_FUNC
		} else {
			t |= ld.STT_OBJECT
		}
		ld.Adduint8(ctxt, d, uint8(t))
		ld.Adduint8(ctxt, d, 0)

		/* shndx */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint16(ctxt, d, ld.SHN_UNDEF)
		} else {
			ld.Adduint16(ctxt, d, 1)
		}
	} else if ld.HEADTYPE == ld.Hdarwin {
		ld.Diag("adddynsym: missed symbol %s (%s)", s.Name, s.Extname)
	} else if ld.HEADTYPE == ld.Hwindows {
	} else // already taken care of
	{
		ld.Diag("adddynsym: unsupported binary format")
	}
}
Esempio n. 5
0
func adddynsym(ctxt *ld.Link, s *ld.LSym) {
	if s.Dynid >= 0 {
		return
	}

	if ld.Iself {
		s.Dynid = int32(ld.Nelfsym)
		ld.Nelfsym++

		d := ld.Linklookup(ctxt, ".dynsym", 0)

		/* name */
		name := s.Extname

		ld.Adduint32(ctxt, d, uint32(ld.Addstring(ld.Linklookup(ctxt, ".dynstr", 0), name)))

		/* value */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint32(ctxt, d, 0)
		} else {
			ld.Addaddr(ctxt, d, s)
		}

		/* size */
		ld.Adduint32(ctxt, d, 0)

		/* type */
		t := ld.STB_GLOBAL << 4

		if (s.Cgoexport&ld.CgoExportDynamic != 0) && s.Type&ld.SMASK == ld.STEXT {
			t |= ld.STT_FUNC
		} else {
			t |= ld.STT_OBJECT
		}
		ld.Adduint8(ctxt, d, uint8(t))
		ld.Adduint8(ctxt, d, 0)

		/* shndx */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint16(ctxt, d, ld.SHN_UNDEF)
		} else {
			ld.Adduint16(ctxt, d, 1)
		}
	} else {
		ld.Diag("adddynsym: unsupported binary format")
	}
}
Esempio n. 6
0
func adddynsym(ctxt *ld.Link, s *ld.LSym) {
	if s.Dynid >= 0 {
		return
	}

	if ld.Iself {
		s.Dynid = int32(ld.Nelfsym)
		ld.Nelfsym++

		d := ld.Linklookup(ctxt, ".dynsym", 0)

		name := s.Extname
		ld.Adduint32(ctxt, d, uint32(ld.Addstring(ld.Linklookup(ctxt, ".dynstr", 0), name)))

		/* type */
		t := ld.STB_GLOBAL << 4

		if s.Cgoexport != 0 && s.Type&ld.SMASK == ld.STEXT {
			t |= ld.STT_FUNC
		} else {
			t |= ld.STT_OBJECT
		}
		ld.Adduint8(ctxt, d, uint8(t))

		/* reserved */
		ld.Adduint8(ctxt, d, 0)

		/* section where symbol is defined */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint16(ctxt, d, ld.SHN_UNDEF)
		} else {
			ld.Adduint16(ctxt, d, 1)
		}

		/* value */
		if s.Type == ld.SDYNIMPORT {
			ld.Adduint64(ctxt, d, 0)
		} else {
			ld.Addaddr(ctxt, d, s)
		}

		/* size of object */
		ld.Adduint64(ctxt, d, uint64(s.Size))
	} else {
		ld.Diag("adddynsym: unsupported binary format")
	}
}
Esempio n. 7
0
func addpltsym(ctxt *ld.Link, s *ld.LSym) {
	if s.Plt >= 0 {
		return
	}

	adddynsym(ctxt, s)

	if ld.Iself {
		plt := ld.Linklookup(ctxt, ".plt", 0)
		got := ld.Linklookup(ctxt, ".got.plt", 0)
		rel := ld.Linklookup(ctxt, ".rel.plt", 0)
		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)
	} else if ld.HEADTYPE == ld.Hdarwin {
		// Same laziness as in 6l.

		plt := ld.Linklookup(ctxt, ".plt", 0)

		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")
	}
}
Esempio n. 8
0
func gentext() {
	if !ld.DynlinkingGo() {
		return
	}
	addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0)
	if addmoduledata.Type == ld.STEXT {
		// we're linking a module containing the runtime -> no need for
		// an init function
		return
	}
	addmoduledata.Reachable = true
	initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0)
	initfunc.Type = ld.STEXT
	initfunc.Local = true
	initfunc.Reachable = true
	o := func(op ...uint8) {
		for _, op1 := range op {
			ld.Adduint8(ld.Ctxt, initfunc, op1)
		}
	}
	// 0000000000000000 <local.dso_init>:
	//    0:	48 8d 3d 00 00 00 00 	lea    0x0(%rip),%rdi        # 7 <local.dso_init+0x7>
	// 			3: R_X86_64_PC32	runtime.firstmoduledata-0x4
	o(0x48, 0x8d, 0x3d)
	ld.Addpcrelplus(ld.Ctxt, initfunc, ld.Linklookup(ld.Ctxt, "runtime.firstmoduledata", 0), 0)
	//    7:	e8 00 00 00 00       	callq  c <local.dso_init+0xc>
	// 			8: R_X86_64_PLT32	runtime.addmoduledata-0x4
	o(0xe8)
	Addcall(ld.Ctxt, initfunc, addmoduledata)
	//    c:	c3                   	retq
	o(0xc3)
	if ld.Ctxt.Etextp != nil {
		ld.Ctxt.Etextp.Next = initfunc
	} else {
		ld.Ctxt.Textp = initfunc
	}
	ld.Ctxt.Etextp = initfunc
	initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0)
	initarray_entry.Reachable = true
	initarray_entry.Local = true
	initarray_entry.Type = ld.SINITARR
	ld.Addaddr(ld.Ctxt, initarray_entry, initfunc)
}
Esempio n. 9
0
File: asm.go Progetto: tidatida/go
func addpltsym(s *ld.LSym) {
	if s.Plt >= 0 {
		return
	}

	adddynsym(ld.Ctxt, s)

	if ld.Iself {
		plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
		got := ld.Linklookup(ld.Ctxt, ".got.plt", 0)
		rela := ld.Linklookup(ld.Ctxt, ".rela.plt", 0)
		if plt.Size == 0 {
			elfsetupplt()
		}

		// jmpq *got+size(IP)
		ld.Adduint8(ld.Ctxt, plt, 0xff)

		ld.Adduint8(ld.Ctxt, plt, 0x25)
		ld.Addpcrelplus(ld.Ctxt, plt, got, got.Size)

		// add to got: pointer to current pos in plt
		ld.Addaddrplus(ld.Ctxt, got, plt, plt.Size)

		// pushq $x
		ld.Adduint8(ld.Ctxt, plt, 0x68)

		ld.Adduint32(ld.Ctxt, plt, uint32((got.Size-24-8)/8))

		// jmpq .plt
		ld.Adduint8(ld.Ctxt, plt, 0xe9)

		ld.Adduint32(ld.Ctxt, plt, uint32(-(plt.Size + 4)))

		// rela
		ld.Addaddrplus(ld.Ctxt, rela, got, got.Size-8)

		ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_JMP_SLOT))
		ld.Adduint64(ld.Ctxt, rela, 0)

		s.Plt = int32(plt.Size - 16)
	} else if ld.HEADTYPE == ld.Hdarwin {
		// To do lazy symbol lookup right, we're supposed
		// to tell the dynamic loader which library each
		// symbol comes from and format the link info
		// section just so.  I'm too lazy (ha!) to do that
		// so for now we'll just use non-lazy pointers,
		// which don't need to be told which library to use.
		//
		// http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html
		// has details about what we're avoiding.

		addgotsym(s)
		plt := ld.Linklookup(ld.Ctxt, ".plt", 0)

		ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.plt", 0), uint32(s.Dynid))

		// jmpq *got+size(IP)
		s.Plt = int32(plt.Size)

		ld.Adduint8(ld.Ctxt, plt, 0xff)
		ld.Adduint8(ld.Ctxt, plt, 0x25)
		ld.Addpcrelplus(ld.Ctxt, plt, ld.Linklookup(ld.Ctxt, ".got", 0), int64(s.Got))
	} else {
		ld.Diag("addpltsym: unsupported binary format")
	}
}
Esempio n. 10
0
func archinit() {
	// getgoextlinkenabled is based on GO_EXTLINK_ENABLED when
	// Go was built; see ../../make.bash.
	if ld.Linkmode == ld.LinkAuto && obj.Getgoextlinkenabled() == "0" {
		ld.Linkmode = ld.LinkInternal
	}

	switch ld.HEADTYPE {
	default:
		if ld.Linkmode == ld.LinkAuto {
			ld.Linkmode = ld.LinkInternal
		}
		if ld.Linkmode == ld.LinkExternal && obj.Getgoextlinkenabled() != "1" {
			log.Fatalf("cannot use -linkmode=external with -H %s", ld.Headstr(int(ld.HEADTYPE)))
		}

	case ld.Hlinux,
		ld.Hfreebsd,
		ld.Hnacl,
		ld.Hdarwin:
		break
	}

	switch ld.HEADTYPE {
	default:
		ld.Diag("unknown -H option")
		ld.Errorexit()
		fallthrough

	case ld.Hplan9: /* plan 9 */
		ld.HEADR = 32

		if ld.INITTEXT == -1 {
			ld.INITTEXT = 4128
		}
		if ld.INITDAT == -1 {
			ld.INITDAT = 0
		}
		if ld.INITRND == -1 {
			ld.INITRND = 4096
		}

	case ld.Hlinux, /* arm elf */
		ld.Hfreebsd,
		ld.Hnetbsd,
		ld.Hopenbsd:
		ld.Debug['d'] = 0
		// with dynamic linking
		ld.Elfinit()
		ld.HEADR = ld.ELFRESERVE
		if ld.INITTEXT == -1 {
			ld.INITTEXT = 0x10000 + int64(ld.HEADR)
		}
		if ld.INITDAT == -1 {
			ld.INITDAT = 0
		}
		if ld.INITRND == -1 {
			ld.INITRND = 4096
		}

	case ld.Hnacl:
		ld.Elfinit()
		ld.HEADR = 0x10000
		ld.Funcalign = 16
		if ld.INITTEXT == -1 {
			ld.INITTEXT = 0x20000
		}
		if ld.INITDAT == -1 {
			ld.INITDAT = 0
		}
		if ld.INITRND == -1 {
			ld.INITRND = 0x10000
		}

	case ld.Hdarwin: /* apple MACH */
		ld.Debug['w'] = 1 // disable DWARF generataion
		ld.Machoinit()
		ld.HEADR = ld.INITIAL_MACHO_HEADR
		if ld.INITTEXT == -1 {
			ld.INITTEXT = 4096 + int64(ld.HEADR)
		}
		if ld.INITDAT == -1 {
			ld.INITDAT = 0
		}
		if ld.INITRND == -1 {
			ld.INITRND = 4096
		}
	}

	if ld.INITDAT != 0 && ld.INITRND != 0 {
		fmt.Printf("warning: -D0x%x is ignored because of -R0x%x\n", uint64(ld.INITDAT), uint32(ld.INITRND))
	}

	// embed goarm to runtime.goarm
	s := ld.Linklookup(ld.Ctxt, "runtime.goarm", 0)

	s.Type = ld.SRODATA
	ld.Adduint8(ld.Ctxt, s, uint8(ld.Ctxt.Goarm))
}