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