Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 (
  160. pageSize = 4096
  161. // PAGE_OFFSET for PowerPC64, see arch/powerpc/Kconfig in the kernel sources.
  162. pageOffsetPpc64 = 0xc000000000000000
  163. )
  164. if start == 0 && offset == 0 &&
  165. (limit == ^uint64(0) || limit == 0) {
  166. // Some tools may introduce a fake mapping that spans the entire
  167. // address space. Assume that the address has already been
  168. // adjusted, so no additional base adjustment is necessary.
  169. return 0, nil
  170. }
  171. switch fh.Type {
  172. case elf.ET_EXEC:
  173. if loadSegment == nil {
  174. // Fixed-address executable, no adjustment.
  175. return 0, nil
  176. }
  177. if start == 0 && limit != 0 {
  178. // ChromeOS remaps its kernel to 0. Nothing else should come
  179. // down this path. Empirical values:
  180. // VADDR=0xffffffff80200000
  181. // stextOffset=0xffffffff80200198
  182. if stextOffset != nil {
  183. return -*stextOffset, nil
  184. }
  185. return -loadSegment.Vaddr, nil
  186. }
  187. if loadSegment.Vaddr-loadSegment.Off == start-offset {
  188. return offset, nil
  189. }
  190. if loadSegment.Vaddr == start-offset {
  191. return offset, nil
  192. }
  193. if start >= loadSegment.Vaddr && limit > start && (offset == 0 || offset == pageOffsetPpc64) {
  194. // Some kernels look like:
  195. // VADDR=0xffffffff80200000
  196. // stextOffset=0xffffffff80200198
  197. // Start=0xffffffff83200000
  198. // Limit=0xffffffff84200000
  199. // Offset=0 (0xc000000000000000 for PowerPC64)
  200. // So the base should be:
  201. if stextOffset != nil && (start%pageSize) == (*stextOffset%pageSize) {
  202. // perf uses the address of _stext as start. Some tools may
  203. // adjust for this before calling GetBase, in which case the page
  204. // alignment should be different from that of stextOffset.
  205. return start - *stextOffset, nil
  206. }
  207. return start - loadSegment.Vaddr, nil
  208. } else if start%pageSize != 0 && stextOffset != nil && *stextOffset%pageSize == start%pageSize {
  209. // ChromeOS remaps its kernel to 0 + start%pageSize. Nothing
  210. // else should come down this path. Empirical values:
  211. // start=0x198 limit=0x2f9fffff offset=0
  212. // VADDR=0xffffffff81000000
  213. // stextOffset=0xffffffff81000198
  214. return -(*stextOffset - start), nil
  215. }
  216. 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)
  217. case elf.ET_REL:
  218. if offset != 0 {
  219. return 0, fmt.Errorf("Don't know how to handle mapping.Offset")
  220. }
  221. return start, nil
  222. case elf.ET_DYN:
  223. // The process mapping information, start = start of virtual address range,
  224. // and offset = offset in the executable file of the start address, tells us
  225. // that a runtime virtual address x maps to a file offset
  226. // fx = x - start + offset.
  227. if loadSegment == nil {
  228. return start - offset, nil
  229. }
  230. // The program header, if not nil, indicates the offset in the file where
  231. // the executable segment is located (loadSegment.Off), and the base virtual
  232. // address where the first byte of the segment is loaded
  233. // (loadSegment.Vaddr). A file offset fx maps to a virtual (symbol) address
  234. // sx = fx - loadSegment.Off + loadSegment.Vaddr.
  235. //
  236. // Thus, a runtime virtual address x maps to a symbol address
  237. // sx = x - start + offset - loadSegment.Off + loadSegment.Vaddr.
  238. return start - offset + loadSegment.Off - loadSegment.Vaddr, nil
  239. }
  240. return 0, fmt.Errorf("Don't know how to handle FileHeader.Type %v", fh.Type)
  241. }
  242. // FindTextProgHeader finds the program segment header containing the .text
  243. // section or nil if the segment cannot be found.
  244. func FindTextProgHeader(f *elf.File) *elf.ProgHeader {
  245. for _, s := range f.Sections {
  246. if s.Name == ".text" {
  247. // Find the LOAD segment containing the .text section.
  248. for _, p := range f.Progs {
  249. if p.Type == elf.PT_LOAD && p.Flags&elf.PF_X != 0 && s.Addr >= p.Vaddr && s.Addr < p.Vaddr+p.Memsz {
  250. return &p.ProgHeader
  251. }
  252. }
  253. }
  254. }
  255. return nil
  256. }