| 1 | + | // Copyright 2021 The GoRE Authors. All rights reserved. |
| 2 | + | // Use of this source code is governed by the license that |
| 3 | + | // can be found in the LICENSE file. |
| 4 | + | |
| 5 | + | package main |
| 6 | + | |
| 7 | + | import ( |
| 8 | + | "bytes" |
| 9 | + | "fmt" |
| 10 | + | "io" |
| 11 | + | "os" |
| 12 | + | |
| 13 | + | "github.com/cheynewallace/tabby" |
| 14 | + | "github.com/goretk/gore" |
| 15 | + | "github.com/spf13/cobra" |
| 16 | + | ) |
| 17 | + | |
| 18 | + | func init() { |
| 19 | + | modCMD := &cobra.Command{ |
| 20 | + | Use: "moduledata path/to/file", |
| 21 | + | Args: cobra.ExactArgs(1), |
| 22 | + | Aliases: []string{"md"}, |
| 23 | + | Short: "Display sections extracted from the moduledata structure.", |
| 24 | + | Run: func(cmd *cobra.Command, args []string) { |
| 25 | + | moduledata(args[0]) |
| 26 | + | }, |
| 27 | + | } |
| 28 | + | |
| 29 | + | dumpCMD := &cobra.Command{ |
| 30 | + | Use: "dump section path/to/file", |
| 31 | + | Args: cobra.ExactValidArgs(2), |
| 32 | + | Short: "Dump the content of a section to standard out.", |
| 33 | + | Run: func(cmd *cobra.Command, args []string) { |
| 34 | + | dumpModuleSection(args[1], args[0]) |
| 35 | + | }, |
| 36 | + | } |
| 37 | + | modCMD.AddCommand(dumpCMD) |
| 38 | + | |
| 39 | + | expCmd.AddCommand(modCMD) |
| 40 | + | } |
| 41 | + | |
| 42 | + | func dumpModuleSection(fp, section string) { |
| 43 | + | f, err := gore.Open(fp) |
| 44 | + | if err != nil { |
| 45 | + | fmt.Fprintf(os.Stderr, "Could not open the file: %s.\n", err) |
| 46 | + | return |
| 47 | + | } |
| 48 | + | defer f.Close() |
| 49 | + | |
| 50 | + | md, err := f.Moduledata() |
| 51 | + | if err != nil { |
| 52 | + | fmt.Fprintf(os.Stderr, "Could not retrieve the file's moduledata: %s.\n", err) |
| 53 | + | return |
| 54 | + | } |
| 55 | + | |
| 56 | + | var sec gore.ModuleDataSection |
| 57 | + | |
| 58 | + | switch section { |
| 59 | + | case "text": |
| 60 | + | sec = md.Text() |
| 61 | + | case "types": |
| 62 | + | sec = md.Types() |
| 63 | + | case "itablinks": |
| 64 | + | sec = md.ITabLinks() |
| 65 | + | case "pclntab": |
| 66 | + | sec = md.PCLNTab() |
| 67 | + | case "functab": |
| 68 | + | sec = md.FuncTab() |
| 69 | + | case "noptrdata": |
| 70 | + | sec = md.NoPtrData() |
| 71 | + | case "data": |
| 72 | + | sec = md.Data() |
| 73 | + | case "bss": |
| 74 | + | sec = md.Bss() |
| 75 | + | case "noptrbss": |
| 76 | + | sec = md.NoPtrBss() |
| 77 | + | default: |
| 78 | + | fmt.Fprintf(os.Stderr, "No known section with the name %s.\n", section) |
| 79 | + | return |
| 80 | + | } |
| 81 | + | |
| 82 | + | data, err := sec.Data() |
| 83 | + | if err != nil { |
| 84 | + | fmt.Fprintf(os.Stderr, "Error when getting the secton data: %s.\n", err) |
| 85 | + | return |
| 86 | + | } |
| 87 | + | if len(data) == 0 { |
| 88 | + | fmt.Fprintf(os.Stderr, "Section %s is empty.\n", section) |
| 89 | + | return |
| 90 | + | } |
| 91 | + | |
| 92 | + | r := bytes.NewReader(data) |
| 93 | + | io.Copy(os.Stdout, r) |
| 94 | + | } |
| 95 | + | |
| 96 | + | func moduledata(fp string) { |
| 97 | + | f, err := gore.Open(fp) |
| 98 | + | if err != nil { |
| 99 | + | fmt.Fprintf(os.Stderr, "Could not open the file: %s.\n", err) |
| 100 | + | return |
| 101 | + | } |
| 102 | + | defer f.Close() |
| 103 | + | |
| 104 | + | md, err := f.Moduledata() |
| 105 | + | if err != nil { |
| 106 | + | fmt.Fprintf(os.Stderr, "Could not retrieve the file's moduledata: %s.\n", err) |
| 107 | + | return |
| 108 | + | } |
| 109 | + | |
| 110 | + | tab := tabby.New() |
| 111 | + | |
| 112 | + | tab.AddHeader("Section", "Address", "Size") |
| 113 | + | |
| 114 | + | sections := []struct { |
| 115 | + | name string |
| 116 | + | info gore.ModuleDataSection |
| 117 | + | }{ |
| 118 | + | {"text", md.Text()}, |
| 119 | + | {"types", md.Types()}, |
| 120 | + | {"itablinks", md.ITabLinks()}, |
| 121 | + | {"pclntab", md.PCLNTab()}, |
| 122 | + | {"functab", md.FuncTab()}, |
| 123 | + | {"noptrdata", md.NoPtrData()}, |
| 124 | + | {"data", md.Data()}, |
| 125 | + | {"bss", md.Bss()}, |
| 126 | + | {"noptrbss", md.NoPtrBss()}, |
| 127 | + | } |
| 128 | + | |
| 129 | + | for _, v := range sections { |
| 130 | + | tab.AddLine(v.name, fmt.Sprintf("0x%x", v.info.Address), fmt.Sprintf("0x%x", v.info.Length)) |
| 131 | + | } |
| 132 | + | |
| 133 | + | tab.Print() |
| 134 | + | } |
| 135 | + | |