summaryrefslogtreecommitdiff
path: root/src/cmd/objdump/main.c
blob: b684be7fbe1e8857703c28ad65ee5adff6623f6e (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
// Copyright 2012 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.

/*
 * objdump simulation - only enough to make pprof work on Macs
 */

#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>

void
usage(void)
{
	fprint(2, "usage: objdump binary start stop\n");
	fprint(2, "Disassembles binary from PC start up to stop.\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	int fd, n;
	uvlong pc, start, stop;
	Fhdr fhdr;
	Biobuf bout;
	char buf[1024];
	Map *text;

	ARGBEGIN{
	default:
		usage();
	}ARGEND

	if(argc != 3)
		usage();
	start = strtoull(argv[1], 0, 16);
	stop = strtoull(argv[2], 0, 16);

	fd = open(argv[0], OREAD);
	if(fd < 0)
		sysfatal("open %s: %r", argv[0]);
	if(crackhdr(fd, &fhdr) <= 0)
		sysfatal("crackhdr: %r");
	machbytype(fhdr.type);
	if(syminit(fd, &fhdr) <= 0)
		sysfatal("syminit: %r");
	text = loadmap(nil, fd, &fhdr);
	if(text == nil)
		sysfatal("loadmap: %r");

	Binit(&bout, 1, OWRITE);
	for(pc=start; pc<stop; ) {
		if(fileline(buf, sizeof buf, pc))
			Bprint(&bout, "%s\n", buf);
		buf[0] = '\0';
		machdata->das(text, pc, 0, buf, sizeof buf);
		Bprint(&bout, " %llx: %s\n", pc, buf);
		n = machdata->instsize(text, pc);
		if(n <= 0)
			break;
		pc += n;
	}
	Bflush(&bout);
	exits(0);
}