暂无描述

disasm.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package binutils
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "os/exec"
  20. "regexp"
  21. "strconv"
  22. "internal/plugin"
  23. "golang/demangle"
  24. )
  25. var (
  26. nmOutputRE = regexp.MustCompile(`^\s*([[:xdigit:]]+)\s+(.)\s+(.*)`)
  27. objdumpAsmOutputRE = regexp.MustCompile(`^\s*([[:xdigit:]]+):\s+(.*)`)
  28. objdumpOutputFileLine = regexp.MustCompile(`^(.*):([0-9]+)`)
  29. )
  30. func findSymbols(nm, file string, r *regexp.Regexp, address uint64) ([]*plugin.Sym, error) {
  31. // Get from nm a list of symbols sorted by address.
  32. cmd := exec.Command(nm, "-n", file)
  33. out, err := cmd.Output()
  34. if err != nil {
  35. return nil, fmt.Errorf("%v: %v", cmd.Args, err)
  36. }
  37. // Collect all symbols from the nm output, grouping names mapped to
  38. // the same address into a single symbol.
  39. var symbols []*plugin.Sym
  40. names, start := []string{}, uint64(0)
  41. buf := bytes.NewBuffer(out)
  42. for symAddr, name, err := nextSymbol(buf); err == nil; symAddr, name, err = nextSymbol(buf) {
  43. if err != nil {
  44. return nil, err
  45. }
  46. if start == symAddr {
  47. names = append(names, name)
  48. continue
  49. }
  50. if match := matchSymbol(names, start, symAddr-1, r, address); match != nil {
  51. symbols = append(symbols, &plugin.Sym{match, file, start, symAddr - 1})
  52. }
  53. names, start = []string{name}, symAddr
  54. }
  55. return symbols, nil
  56. }
  57. // matchSymbol checks if a symbol is to be selected by checking its
  58. // name to the regexp and optionally its address. It returns the name(s)
  59. // to be used for the matched symbol, or nil if no match
  60. func matchSymbol(names []string, start, end uint64, r *regexp.Regexp, address uint64) []string {
  61. if address != 0 && address >= start && address <= end {
  62. return names
  63. }
  64. for _, name := range names {
  65. if r.MatchString(name) {
  66. return []string{name}
  67. }
  68. // Match all possible demangled versions of the name.
  69. for _, o := range [][]demangle.Option{
  70. {demangle.NoClones},
  71. {demangle.NoParams},
  72. {demangle.NoParams, demangle.NoTemplateParams},
  73. } {
  74. if demangled, err := demangle.ToString(name, o...); err == nil && r.MatchString(demangled) {
  75. return []string{demangled}
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. // disassemble returns the assembly instructions in a function from a
  82. // binary file. It uses objdump to obtain the assembly listing.
  83. func disassemble(objdump string, file string, start, stop uint64) ([]plugin.Inst, error) {
  84. cmd := exec.Command(objdump, "-d", "-C", "--no-show-raw-insn", "-l",
  85. fmt.Sprintf("--start-address=%#x", start),
  86. fmt.Sprintf("--stop-address=%#x", stop),
  87. file)
  88. out, err := cmd.Output()
  89. if err != nil {
  90. return nil, fmt.Errorf("%v: %v", cmd.Args, err)
  91. }
  92. buf := bytes.NewBuffer(out)
  93. file, line := "", 0
  94. var assembly []plugin.Inst
  95. for {
  96. input, err := buf.ReadString('\n')
  97. if err != nil {
  98. if err != io.EOF {
  99. return nil, err
  100. }
  101. if input == "" {
  102. break
  103. }
  104. }
  105. if fields := objdumpAsmOutputRE.FindStringSubmatch(input); len(fields) == 3 {
  106. if address, err := strconv.ParseUint(fields[1], 16, 64); err == nil {
  107. assembly = append(assembly,
  108. plugin.Inst{
  109. Addr: address,
  110. Text: fields[2],
  111. File: file,
  112. Line: line,
  113. })
  114. continue
  115. }
  116. }
  117. if fields := objdumpOutputFileLine.FindStringSubmatch(input); len(fields) == 3 {
  118. if l, err := strconv.ParseUint(fields[2], 10, 32); err == nil {
  119. file, line = fields[1], int(l)
  120. }
  121. }
  122. }
  123. return assembly, nil
  124. }
  125. // nextSymbol parses the nm output to find the next symbol listed.
  126. // Skips over any output it cannot recognize.
  127. func nextSymbol(buf *bytes.Buffer) (uint64, string, error) {
  128. for {
  129. line, err := buf.ReadString('\n')
  130. if err != nil {
  131. if err != io.EOF || line == "" {
  132. return 0, "", err
  133. }
  134. }
  135. if fields := nmOutputRE.FindStringSubmatch(line); len(fields) == 4 {
  136. if address, err := strconv.ParseUint(fields[1], 16, 64); err == nil {
  137. return address, fields[3], nil
  138. }
  139. }
  140. }
  141. }