aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/objwriter/main.go
blob: df83298ecca0b352f0a98ef42c8a2f55a2c345f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2015 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Objwriter reads an object file description in an unspecified format
// and writes a Go object file. It is invoked by parts of the toolchain
// that have not yet been converted from C to Go and should not be
// used otherwise.
package main

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"math"
	"os"
	"runtime/pprof"
	"strconv"
	"strings"

	"cmd/internal/obj"
	"cmd/internal/obj/arm"
	"cmd/internal/obj/i386"
	"cmd/internal/obj/ppc64"
	"cmd/internal/obj/x86"
)

var arch *obj.LinkArch
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
var memprofile = flag.String("memprofile", "", "write memory profile to this file")

func main() {
	log.SetPrefix("goobj: ")
	log.SetFlags(0)
	flag.Parse()

	if flag.NArg() == 1 && flag.Arg(0) == "ping" {
		// old invocation from liblink, just testing that objwriter exists
		return
	}

	if flag.NArg() != 4 {
		fmt.Fprintf(os.Stderr, "usage: goobj infile objfile offset goarch\n")
		os.Exit(2)
	}

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		defer pprof.WriteHeapProfile(f)
	}

	switch flag.Arg(3) {
	case "amd64":
		arch = &x86.Linkamd64
	case "amd64p32":
		arch = &x86.Linkamd64p32
	case "386":
		// TODO(rsc): Move Link386 to package x86.
		arch = &i386.Link386
	case "arm":
		arch = &arm.Linkarm
	case "ppc64":
		arch = &ppc64.Linkppc64
	case "ppc64le":
		arch = &ppc64.Linkppc64le
	}

	input()
}

const (
	// must match liblink/objfilego.c
	TypeEnd = iota
	TypeCtxt
	TypePlist
	TypeSym
	TypeProg
	TypeAddr
	TypeHist
)

var (
	ctxt   *obj.Link
	plists = map[int64]*obj.Plist{}
	syms   = map[int64]*obj.LSym{}
	progs  = map[int64]*obj.Prog{}
	hists  = map[int64]*obj.Hist{}
	undef  = map[interface{}]bool{}
)

func input() {
	args := flag.Args()
	ctxt = obj.Linknew(arch)
	ctxt.Debugasm = 1
	ctxt.Bso = obj.Binitw(os.Stdout)
	defer obj.Bflush(ctxt.Bso)
	ctxt.Diag = log.Fatalf
	f, err := os.Open(args[0])
	if err != nil {
		log.Fatal(err)
	}

	b := bufio.NewReaderSize(f, 1<<20)
	if v := rdint(b); v != TypeCtxt {
		log.Fatalf("invalid input - missing ctxt - got %d", v)
	}
	name := rdstring(b)
	if name != ctxt.Arch.Name {
		log.Fatalf("bad arch %s - want %s", name, ctxt.Arch.Name)
	}

	ctxt.Goarm = int32(rdint(b))
	ctxt.Debugasm = int32(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 := int64(TypeCtxt)

Loop:
	for {
		t := rdint(b)
		switch t {
		default:
			log.Fatalf("unexpected input after type %d: %v", last, t)
		case TypeEnd:
			break Loop
		case TypePlist:
			readplist(b, rdplist(b))
		case TypeSym:
			readsym(b, rdsym(b))
		case TypeProg:
			readprog(b, rdprog(b))
		case TypeHist:
			readhist(b, rdhist(b))
		}
		last = t
	}

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

	var buf bytes.Buffer
	obuf := obj.Binitw(&buf)
	obj.Writeobjdirect(ctxt, obuf)
	obj.Bflush(obuf)

	data, err := ioutil.ReadFile(args[1])
	if err != nil {
		log.Fatal(err)
	}

	offset, err := strconv.Atoi(args[2])
	if err != nil {
		log.Fatalf("bad offset: %v", err)
	}
	if offset > len(data) {
		log.Fatalf("offset too large: %v > %v", offset, len(data))
	}

	old := data[offset:]
	if len(old) > 0 && !bytes.Equal(old, buf.Bytes()) {
		out := strings.TrimSuffix(args[0], ".in") + ".out"
		if err := ioutil.WriteFile(out, append(data[:offset:offset], buf.Bytes()...), 0666); err != nil {
			log.Fatal(err)
		}
		log.Fatalf("goobj produced different output:\n\toriginal: %s\n\tgoobj: %s", args[1], out)
	}

	if len(old) == 0 {
		data = append(data, buf.Bytes()...)
		if err := ioutil.WriteFile(args[1], data, 0666); err != nil {
			log.Fatal(err)
		}
	}
}

func rdstring(b *bufio.Reader) string {
	v := rdint(b)
	buf := make([]byte, v)
	io.ReadFull(b, buf)
	return string(buf)
}

func rdint(b *bufio.Reader) int64 {
	var v uint64
	shift := uint(0)
	for {
		b, err := b.ReadByte()
		if err != nil {
			log.Fatal(err)
		}
		v |= uint64(b&0x7F) << shift
		shift += 7
		if b&0x80 == 0 {
			break
		}
	}
	return int64(v>>1) ^ int64(v<<63)>>63
}

func rdplist(b *bufio.Reader) *obj.Plist {
	id := rdint(b)
	if id == 0 {
		return nil
	}
	pl := plists[id]
	if pl == nil {
		pl = new(obj.Plist)
		plists[id] = pl
		undef[pl] = true
	}
	return pl
}

func rdsym(b *bufio.Reader) *obj.LSym {
	id := rdint(b)
	if id == 0 {
		return nil
	}
	sym := syms[id]
	if sym == nil {
		sym = new(obj.LSym)
		syms[id] = sym
		undef[sym] = true
	}
	return sym
}

func rdprog(b *bufio.Reader) *obj.Prog {
	id := rdint(b)
	if id == 0 {
		return nil
	}
	prog := progs[id]
	if prog == nil {
		prog = new(obj.Prog)
		prog.Ctxt = ctxt
		progs[id] = prog
		undef[prog] = true
	}
	return prog
}

func rdhist(b *bufio.Reader) *obj.Hist {
	id := rdint(b)
	if id == 0 {
		return nil
	}
	h := hists[id]
	if h == nil {
		h = new(obj.Hist)
		hists[id] = h
		undef[h] = true
	}
	return h
}

func readplist(b *bufio.Reader, pl *obj.Plist) {
	if !undef[pl] {
		panic("double-def")
	}
	delete(undef, pl)
	pl.Recur = int(rdint(b))
	pl.Name = rdsym(b)
	pl.Firstpc = rdprog(b)
	pl.Link = rdplist(b)
}

func readsym(b *bufio.Reader, s *obj.LSym) {
	if !undef[s] {
		panic("double-def")
	}
	delete(undef, s)
	s.Name = rdstring(b)
	s.Extname = rdstring(b)
	s.Type = int16(rdint(b))
	s.Version = int16(rdint(b))
	s.Dupok = uint8(rdint(b))
	s.External = uint8(rdint(b))
	s.Nosplit = uint8(rdint(b))
	s.Reachable = uint8(rdint(b))
	s.Cgoexport = uint8(rdint(b))
	s.Special = uint8(rdint(b))
	s.Stkcheck = uint8(rdint(b))
	s.Hide = uint8(rdint(b))
	s.Leaf = uint8(rdint(b))
	s.Fnptr = uint8(rdint(b))
	s.Seenglobl = uint8(rdint(b))
	s.Onlist = uint8(rdint(b))
	s.Symid = int16(rdint(b))
	s.Dynid = int32(rdint(b))
	s.Sig = int32(rdint(b))
	s.Plt = int32(rdint(b))
	s.Got = int32(rdint(b))
	s.Align = int32(rdint(b))
	s.Elfsym = int32(rdint(b))
	s.Args = int32(rdint(b))
	s.Locals = int32(rdint(b))
	s.Value = rdint(b)
	s.Size = rdint(b)
	s.Hash = rdsym(b)
	s.Allsym = rdsym(b)
	s.Next = rdsym(b)
	s.Sub = rdsym(b)
	s.Outer = rdsym(b)
	s.Gotype = rdsym(b)
	s.Reachparent = rdsym(b)
	s.Queue = rdsym(b)
	s.File = rdstring(b)
	s.Dynimplib = rdstring(b)
	s.Dynimpvers = rdstring(b)
	s.Text = rdprog(b)
	s.Etext = rdprog(b)
	n := int(rdint(b))
	if n > 0 {
		s.P = make([]byte, n)
		io.ReadFull(b, s.P)
	}
	s.R = make([]obj.Reloc, int(rdint(b)))
	for i := range s.R {
		r := &s.R[i]
		r.Off = int32(rdint(b))
		r.Siz = uint8(rdint(b))
		r.Done = uint8(rdint(b))
		r.Type = int32(rdint(b))
		r.Add = rdint(b)
		r.Xadd = rdint(b)
		r.Sym = rdsym(b)
		r.Xsym = rdsym(b)
	}
}

func readprog(b *bufio.Reader, p *obj.Prog) {
	if !undef[p] {
		panic("double-def")
	}
	delete(undef, p)
	p.Pc = rdint(b)
	p.Lineno = int32(rdint(b))
	p.Link = rdprog(b)
	p.As = int16(rdint(b))
	p.Reg = int16(rdint(b))
	p.Scond = uint8(rdint(b))
	p.Width = int8(rdint(b))
	readaddr(b, &p.From)
	readaddr(b, &p.From3)
	readaddr(b, &p.To)
}

func readaddr(b *bufio.Reader, a *obj.Addr) {
	if rdint(b) != TypeAddr {
		log.Fatal("out of sync")
	}
	a.Offset = rdint(b)
	a.U.Dval = rdfloat(b)
	buf := make([]byte, 8)
	io.ReadFull(b, buf)
	a.U.Sval = string(buf)
	a.U.Branch = rdprog(b)
	a.Sym = rdsym(b)
	a.Gotype = rdsym(b)
	a.Type = int16(rdint(b))
	a.Index = int16(rdint(b))
	a.Scale = int8(rdint(b))
	a.Reg = int16(rdint(b))
	a.Name = int8(rdint(b))
	a.Class = int8(rdint(b))
	a.Etype = uint8(rdint(b))
	a.U.Argsize = int32(rdint(b))
	a.Width = rdint(b)
}

func readhist(b *bufio.Reader, h *obj.Hist) {
	if !undef[h] {
		panic("double-def")
	}
	delete(undef, h)
	h.Link = rdhist(b)
	h.Name = rdstring(b)
	h.Line = int32(rdint(b))
	h.Offset = int32(rdint(b))
}

func rdfloat(b *bufio.Reader) float64 {
	return math.Float64frombits(uint64(rdint(b)))
}