Skip to content
Snippets Groups Projects
main.go 2.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Russ Cox's avatar
    Russ Cox committed
    // 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.
    
    
    Russ Cox's avatar
    Russ Cox committed
    // Objdump disassembles executable files.
    
    //
    // Usage:
    
    Russ Cox's avatar
    Russ Cox committed
    //
    //	go tool objdump [-s symregexp] binary
    //
    // Objdump prints a disassembly of all text symbols (code) in the binary.
    // If the -s option is present, objdump only disassembles
    // symbols with names matching the regular expression.
    //
    // Alternate usage:
    //
    
    //	go tool objdump binary start end
    //
    
    Russ Cox's avatar
    Russ Cox committed
    // In this mode, objdump disassembles the binary starting at the start address and
    
    // stopping at the end address. The start and end addresses are program
    
    // counters written in hexadecimal with optional leading 0x prefix.
    
    Russ Cox's avatar
    Russ Cox committed
    // In this mode, objdump prints a sequence of stanzas of the form:
    
    //
    //	file:line
    //	 address: assembly
    //	 address: assembly
    //	 ...
    //
    // Each stanza gives the disassembly for a contiguous range of addresses
    // all mapped to the same original source file and line number.
    
    Russ Cox's avatar
    Russ Cox committed
    // This mode is intended for use by pprof.
    
    Russ Cox's avatar
    Russ Cox committed
    package main
    
    import (
    	"flag"
    	"fmt"
    	"log"
    	"os"
    
    Russ Cox's avatar
    Russ Cox committed
    	"regexp"
    
    Russ Cox's avatar
    Russ Cox committed
    	"strconv"
    
    	"cmd/internal/objfile"
    
    var printCode = flag.Bool("S", false, "print go code alongside assembly")
    
    Russ Cox's avatar
    Russ Cox committed
    var symregexp = flag.String("s", "", "only dump symbols matching this regexp")
    var symRE *regexp.Regexp
    
    Russ Cox's avatar
    Russ Cox committed
    
    func usage() {
    
    	fmt.Fprintf(os.Stderr, "usage: go tool objdump [-S] [-s symregexp] binary [start end]\n\n")
    
    Russ Cox's avatar
    Russ Cox committed
    	flag.PrintDefaults()
    
    Russ Cox's avatar
    Russ Cox committed
    	os.Exit(2)
    }
    
    
    Russ Cox's avatar
    Russ Cox committed
    type lookupFunc func(addr uint64) (sym string, base uint64)
    type disasmFunc func(code []byte, pc uint64, lookup lookupFunc) (text string, size int)
    
    
    Russ Cox's avatar
    Russ Cox committed
    func main() {
    	log.SetFlags(0)
    	log.SetPrefix("objdump: ")
    
    	flag.Usage = usage
    	flag.Parse()
    
    Russ Cox's avatar
    Russ Cox committed
    	if flag.NArg() != 1 && flag.NArg() != 3 {
    
    Russ Cox's avatar
    Russ Cox committed
    		usage()
    	}
    
    
    Russ Cox's avatar
    Russ Cox committed
    	if *symregexp != "" {
    		re, err := regexp.Compile(*symregexp)
    		if err != nil {
    			log.Fatalf("invalid -s regexp: %v", err)
    		}
    		symRE = re
    	}
    
    
    	f, err := objfile.Open(flag.Arg(0))
    
    Russ Cox's avatar
    Russ Cox committed
    	if err != nil {
    		log.Fatal(err)
    	}
    
    
    Russ Cox's avatar
    Russ Cox committed
    	if err != nil {
    
    		log.Fatalf("disassemble %s: %v", flag.Arg(0), err)
    
    	switch flag.NArg() {
    	default:
    		usage()
    	case 1:
    		// disassembly of entire object
    
    		dis.Print(os.Stdout, symRE, 0, ^uint64(0), *printCode)
    
    		os.Exit(0)
    
    	case 3:
    		// disassembly of PC range
    		start, err := strconv.ParseUint(strings.TrimPrefix(flag.Arg(1), "0x"), 16, 64)
    		if err != nil {
    			log.Fatalf("invalid start PC: %v", err)
    
    Russ Cox's avatar
    Russ Cox committed
    		}
    
    		end, err := strconv.ParseUint(strings.TrimPrefix(flag.Arg(2), "0x"), 16, 64)
    		if err != nil {
    			log.Fatalf("invalid end PC: %v", err)
    
    Russ Cox's avatar
    Russ Cox committed
    		}
    
    		dis.Print(os.Stdout, symRE, start, end, *printCode)