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