No Description

elfexec.go 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 elfexec provides utility routines to examine ELF binaries.
  15. package elfexec
  16. import (
  17. "bufio"
  18. "debug/elf"
  19. "encoding/binary"
  20. "fmt"
  21. "io"
  22. )
  23. const (
  24. maxNoteSize = 1 << 20 // in bytes
  25. noteTypeGNUBuildID = 3
  26. )
  27. // elfNote is the payload of a Note Section in an ELF file.
  28. type elfNote struct {
  29. Name string // Contents of the "name" field, omitting the trailing zero byte.
  30. Desc []byte // Contents of the "desc" field.
  31. Type uint32 // Contents of the "type" field.
  32. }
  33. // parseNotes returns the notes from a SHT_NOTE section or PT_NOTE segment.
  34. func parseNotes(reader io.Reader, alignment int, order binary.ByteOrder) ([]elfNote, error) {
  35. r := bufio.NewReader(reader)
  36. // padding returns the number of bytes required to pad the given size to an
  37. // alignment boundary.
  38. padding := func(size int) int {
  39. return ((size + (alignment - 1)) &^ (alignment - 1)) - size
  40. }
  41. var notes []elfNote
  42. for {
  43. noteHeader := make([]byte, 12) // 3 4-byte words
  44. if _, err := io.ReadFull(r, noteHeader); err == io.EOF {
  45. break
  46. } else if err != nil {
  47. return nil, err
  48. }
  49. namesz := order.Uint32(noteHeader[0:4])
  50. descsz := order.Uint32(noteHeader[4:8])
  51. typ := order.Uint32(noteHeader[8:12])
  52. if uint64(namesz) > uint64(maxNoteSize) {
  53. return nil, fmt.Errorf("note name too long (%d bytes)", namesz)
  54. }
  55. var name string
  56. if namesz > 0 {
  57. // Documentation differs as to whether namesz is meant to include the
  58. // trailing zero, but everyone agrees that name is null-terminated.
  59. // So we'll just determine the actual length after the fact.
  60. var err error
  61. name, err = r.ReadString('\x00')
  62. if err == io.EOF {
  63. return nil, fmt.Errorf("missing note name (want %d bytes)", namesz)
  64. } else if err != nil {
  65. return nil, err
  66. }
  67. namesz = uint32(len(name))
  68. name = name[:len(name)-1]
  69. }
  70. // Drop padding bytes until the desc field.
  71. for n := padding(len(noteHeader) + int(namesz)); n > 0; n-- {
  72. if _, err := r.ReadByte(); err == io.EOF {
  73. return nil, fmt.Errorf(
  74. "missing %d bytes of padding after note name", n)
  75. } else if err != nil {
  76. return nil, err
  77. }
  78. }
  79. if uint64(descsz) > uint64(maxNoteSize) {
  80. return nil, fmt.Errorf("note desc too long (%d bytes)", descsz)
  81. }
  82. desc := make([]byte, int(descsz))
  83. if _, err := io.ReadFull(r, desc); err == io.EOF {
  84. return nil, fmt.Errorf("missing desc (want %d bytes)", len(desc))
  85. } else if err != nil {
  86. return nil, err
  87. }
  88. notes = append(notes, elfNote{Name: name, Desc: desc, Type: typ})
  89. // Drop padding bytes until the next note or the end of the section,
  90. // whichever comes first.
  91. for n := padding(len(desc)); n > 0; n-- {
  92. if _, err := r.ReadByte(); err == io.EOF {
  93. // We hit the end of the section before an alignment boundary.
  94. // This can happen if this section is at the end of the file or the next
  95. // section has a smaller alignment requirement.
  96. break
  97. } else if err != nil {
  98. return nil, err
  99. }
  100. }
  101. }
  102. return notes, nil
  103. }
  104. // GetBuildID returns the GNU build-ID for an ELF binary.
  105. //
  106. // If no build-ID was found but the binary was read without error, it returns
  107. // (nil, nil).
  108. func GetBuildID(binary io.ReaderAt) ([]byte, error) {
  109. f, err := elf.NewFile(binary)
  110. if err != nil {
  111. return nil, err
  112. }
  113. findBuildID := func(notes []elfNote) ([]byte, error) {
  114. var buildID []byte
  115. for _, note := range notes {
  116. if note.Name == "GNU" && note.Type == noteTypeGNUBuildID {
  117. if buildID == nil {
  118. buildID = note.Desc
  119. } else {
  120. return nil, fmt.Errorf("multiple build ids found, don't know which to use!")
  121. }
  122. }
  123. }
  124. return buildID, nil
  125. }
  126. for _, p := range f.Progs {
  127. if p.Type != elf.PT_NOTE {
  128. continue
  129. }
  130. notes, err := parseNotes(p.Open(), int(p.Align), f.ByteOrder)
  131. if err != nil {
  132. return nil, err
  133. }
  134. if b, err := findBuildID(notes); b != nil || err != nil {
  135. return b, err
  136. }
  137. }
  138. for _, s := range f.Sections {
  139. if s.Type != elf.SHT_NOTE {
  140. continue
  141. }
  142. notes, err := parseNotes(s.Open(), int(s.Addralign), f.ByteOrder)
  143. if err != nil {
  144. return nil, err
  145. }
  146. if b, err := findBuildID(notes); b != nil || err != nil {
  147. return b, err
  148. }
  149. }
  150. return nil, nil
  151. }
  152. // GetBase determines the base address to subtract from virtual
  153. // address to get symbol table address. For an executable, the base
  154. // is 0. Otherwise, it's a shared library, and the base is the
  155. // address where the mapping starts. The kernel is special, and may
  156. // use the address of the _stext symbol as the mmap start. _stext
  157. // offset can be obtained with `nm vmlinux | grep _stext`
  158. func GetBase(fh *elf.FileHeader, loadSegment *elf.ProgHeader, stextOffset *uint64, start, limit, offset uint64) (uint64, error) {
  159. const pageSize = 4096
  160. if start == 0 && offset == 0 &&
  161. (limit == ^uint64(0) || limit == 0) {
  162. // Some tools may introduce a fake mapping that spans the entire
  163. // address space. Assume that the address has already been
  164. // adjusted, so no additional base adjustment is necessary.
  165. return 0, nil
  166. }
  167. switch fh.Type {
  168. case elf.ET_EXEC:
  169. if loadSegment == nil {
  170. // Fixed-address executable, no adjustment.
  171. return 0, nil
  172. }
  173. if start == 0 && limit != 0 {
  174. // ChromeOS remaps its kernel to 0. Nothing else should come
  175. // down this path. Empirical values:
  176. // VADDR=0xffffffff80200000
  177. // stextOffset=0xffffffff80200198
  178. if stextOffset != nil {
  179. return -*stextOffset, nil
  180. }
  181. return -loadSegment.Vaddr, nil
  182. }
  183. if loadSegment.Vaddr-loadSegment.Off == start-offset {
  184. return offset, nil
  185. }
  186. if loadSegment.Vaddr == start-offset {
  187. return offset, nil
  188. }
  189. if start > loadSegment.Vaddr && limit > start && offset == 0 {
  190. // Some kernels look like:
  191. // VADDR=0xffffffff80200000
  192. // stextOffset=0xffffffff80200198
  193. // Start=0xffffffff83200000
  194. // Limit=0xffffffff84200000
  195. // Offset=0
  196. // So the base should be:
  197. if stextOffset != nil && (start%pageSize) == (*stextOffset%pageSize) {
  198. // perf uses the address of _stext as start. Some tools may
  199. // adjust for this before calling GetBase, in which case the the page
  200. // alignment should be different from that of stextOffset.
  201. return start - *stextOffset, nil
  202. }
  203. return start - loadSegment.Vaddr, nil
  204. } else if start < loadSegment.Vaddr && start%pageSize != 0 && stextOffset != nil && *stextOffset%pageSize == start%pageSize {
  205. // ChromeOS remaps its kernel to 0 + start%pageSize. Nothing
  206. // else should come down this path. Empirical values:
  207. // start=0x198 limit=0x2f9fffff offset=0
  208. // VADDR=0xffffffff81000000
  209. // stextOffset=0xffffffff81000198
  210. return -(*stextOffset - start), nil
  211. }
  212. return 0, fmt.Errorf("Don't know how to handle EXEC segment: %v start=0x%x limit=0x%x offset=0x%x", *loadSegment, start, limit, offset)
  213. case elf.ET_REL:
  214. if offset != 0 {
  215. return 0, fmt.Errorf("Don't know how to handle mapping.Offset")
  216. }
  217. return start, nil
  218. case elf.ET_DYN:
  219. if offset != 0 {
  220. if loadSegment == nil || loadSegment.Vaddr == 0 {
  221. return start - offset, nil
  222. }
  223. return 0, fmt.Errorf("Don't know how to handle mapping. Offset=%x, vaddr=%x",
  224. offset, loadSegment.Vaddr)
  225. }
  226. if loadSegment == nil {
  227. return start, nil
  228. }
  229. return start - loadSegment.Vaddr, nil
  230. }
  231. return 0, fmt.Errorf("Don't know how to handle FileHeader.Type %v", fh.Type)
  232. }