Example #1
0
func main() {
	switch build.Default.GOARCH {
	case "amd64":
		arch = &amd64.Linkamd64
	case "amd64p32":
		arch = &amd64.Linkamd64p32
	case "386":
		arch = &x86.Link386
	case "arm":
		arch = &arm.Linkarm
	}
	if len(os.Args) == 3 {
		input()
		return
	}
	f, err := os.Create("x.6")
	if err != nil {
		log.Fatal(err)
	}
	ctxt := liblink.Linknew(arch)
	ctxt.Debugasm = 1
	ctxt.Bso = liblink.Binitw(os.Stdout)
	defer liblink.Bflush(ctxt.Bso)
	ctxt.Diag = log.Fatalf
	obuf := liblink.Binitw(f)
	liblink.Bprint(obuf, "go object %s %s %s\n", liblink.Getgoos(), liblink.Getgoarch(), liblink.Getgoversion())
	liblink.Bprint(obuf, "!\n")
	p1 := &liblink.Prog{
		Ctxt:   ctxt,
		As:     amd64.ATEXT,
		Lineno: 1,
		From: liblink.Addr{
			Typ:   amd64.D_EXTERN,
			Index: amd64.D_NONE,
			Sym:   liblink.Linklookup(ctxt, "main.Main", 0),
			Scale: 0,
		},
		To: liblink.Addr{
			Typ:   amd64.D_CONST,
			Index: amd64.D_NONE,
		},
	}
	p2 := &liblink.Prog{
		Ctxt: ctxt,
		As:   amd64.ARET,
		From: liblink.Addr{
			Typ:   amd64.D_NONE,
			Index: amd64.D_NONE,
		},
		To: liblink.Addr{
			Typ:   amd64.D_NONE,
			Index: amd64.D_NONE,
		},
	}
	p3 := &liblink.Prog{
		Ctxt:   ctxt,
		As:     amd64.ATEXT,
		Lineno: 1,
		From: liblink.Addr{
			Typ:   amd64.D_EXTERN,
			Index: amd64.D_NONE,
			Sym:   liblink.Linklookup(ctxt, "main.Init", 0),
			Scale: 0,
		},
		To: liblink.Addr{
			Typ:   amd64.D_CONST,
			Index: amd64.D_NONE,
		},
	}
	p4 := &liblink.Prog{
		Ctxt: ctxt,
		As:   amd64.ARET,
		From: liblink.Addr{
			Typ:   amd64.D_NONE,
			Index: amd64.D_NONE,
		},
		To: liblink.Addr{
			Typ:   amd64.D_NONE,
			Index: amd64.D_NONE,
		},
	}
	pl := liblink.Linknewplist(ctxt)
	pl.Firstpc = p1
	p1.Link = p2
	p2.Link = p3
	p3.Link = p4
	liblink.Writeobj(ctxt, obuf)
	liblink.Bflush(obuf)
}
Example #2
0
func input() {
	ctxt = liblink.Linknew(arch)
	//ctxt.Debugasm = 1
	ctxt.Bso = liblink.Binitw(os.Stdout)
	defer liblink.Bflush(ctxt.Bso)
	ctxt.Diag = log.Fatalf
	f, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}

	b := bufio.NewReader(f)
	if rdstring(b) != "ctxt" {
		log.Fatal("invalid input - missing ctxt")
	}
	name := rdstring(b)
	if name != ctxt.Arch.Name {
		log.Fatalf("bad arch %s - want %s", name, ctxt.Arch.Name)
	}

	ctxt.Goarm = int(rdint(b))
	ctxt.Debugasm = int(rdint(b))
	ctxt.Trimpath = rdstring(b)
	ctxt.Plist = rdplist(b)
	ctxt.Plast = rdplist(b)
	ctxt.Hist = rdhist(b)
	ctxt.Ehist = rdhist(b)
	for {
		i := rdint(b)
		if i < 0 {
			break
		}
		ctxt.Hash[i] = rdsym(b)
	}
	last := "ctxt"

Loop:
	for {
		s := rdstring(b)
		switch s {
		default:
			log.Fatalf("unexpected input after %s: %v", s, last)
		case "end":
			break Loop
		case "plist":
			readplist(b, rdplist(b))
		case "sym":
			readsym(b, rdsym(b))
		case "prog":
			readprog(b, rdprog(b))
		case "hist":
			readhist(b, rdhist(b))
		}
		last = s
	}

	if len(undef) > 0 {
		panic("missing definitions")
	}

	ff, err := os.Create(os.Args[2])
	obuf := liblink.Binitw(ff)
	liblink.Writeobj(ctxt, obuf)
	liblink.Bflush(obuf)
}