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 package main
33
34 import (
35 "flag"
36 "fmt"
37 "log"
38 "os"
39 "regexp"
40 "strconv"
41 "strings"
42
43 "cmd/internal/objfile"
44 )
45
46 var printCode = flag.Bool("S", false, "print Go code alongside assembly")
47 var symregexp = flag.String("s", "", "only dump symbols matching this regexp")
48 var gnuAsm = flag.Bool("gnu", false, "print GNU assembly next to Go assembly (where supported)")
49 var symRE *regexp.Regexp
50
51 func usage() {
52 fmt.Fprintf(os.Stderr, "usage: go tool objdump [-S] [-gnu] [-s symregexp] binary [start end]\n\n")
53 flag.PrintDefaults()
54 os.Exit(2)
55 }
56
57 func main() {
58 log.SetFlags(0)
59 log.SetPrefix("objdump: ")
60
61 flag.Usage = usage
62 flag.Parse()
63 if flag.NArg() != 1 && flag.NArg() != 3 {
64 usage()
65 }
66
67 if *symregexp != "" {
68 re, err := regexp.Compile(*symregexp)
69 if err != nil {
70 log.Fatalf("invalid -s regexp: %v", err)
71 }
72 symRE = re
73 }
74
75 f, err := objfile.Open(flag.Arg(0))
76 if err != nil {
77 log.Fatal(err)
78 }
79 defer f.Close()
80
81 dis, err := f.Disasm()
82 if err != nil {
83 log.Fatalf("disassemble %s: %v", flag.Arg(0), err)
84 }
85
86 switch flag.NArg() {
87 default:
88 usage()
89 case 1:
90
91 dis.Print(os.Stdout, symRE, 0, ^uint64(0), *printCode, *gnuAsm)
92
93 case 3:
94
95 start, err := strconv.ParseUint(strings.TrimPrefix(flag.Arg(1), "0x"), 16, 64)
96 if err != nil {
97 log.Fatalf("invalid start PC: %v", err)
98 }
99 end, err := strconv.ParseUint(strings.TrimPrefix(flag.Arg(2), "0x"), 16, 64)
100 if err != nil {
101 log.Fatalf("invalid end PC: %v", err)
102 }
103 dis.Print(os.Stdout, symRE, start, end, *printCode, *gnuAsm)
104 }
105 }
106
View as plain text