설명 없음

elfexec.go 9.6KB

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