elf.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2018 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 symbol_inject
  15. import (
  16. "debug/elf"
  17. "fmt"
  18. "io"
  19. )
  20. type mockableElfFile interface {
  21. Symbols() ([]elf.Symbol, error)
  22. Sections() []elf.SectionHeader
  23. Type() elf.Type
  24. }
  25. var _ mockableElfFile = elfFileWrapper{}
  26. type elfFileWrapper struct {
  27. *elf.File
  28. }
  29. func (f elfFileWrapper) Sections() []elf.SectionHeader {
  30. ret := make([]elf.SectionHeader, len(f.File.Sections))
  31. for i, section := range f.File.Sections {
  32. ret[i] = section.SectionHeader
  33. }
  34. return ret
  35. }
  36. func (f elfFileWrapper) Type() elf.Type {
  37. return f.File.Type
  38. }
  39. type mockElfFile struct {
  40. symbols []elf.Symbol
  41. sections []elf.SectionHeader
  42. t elf.Type
  43. }
  44. func (f mockElfFile) Sections() []elf.SectionHeader { return f.sections }
  45. func (f mockElfFile) Symbols() ([]elf.Symbol, error) { return f.symbols, nil }
  46. func (f mockElfFile) Type() elf.Type { return f.t }
  47. func elfSymbolsFromFile(r io.ReaderAt) (*File, error) {
  48. elfFile, err := elf.NewFile(r)
  49. if err != nil {
  50. return nil, cantParseError{err}
  51. }
  52. return extractElfSymbols(elfFileWrapper{elfFile})
  53. }
  54. func extractElfSymbols(elfFile mockableElfFile) (*File, error) {
  55. symbols, err := elfFile.Symbols()
  56. if err != nil {
  57. return nil, err
  58. }
  59. file := &File{}
  60. for _, section := range elfFile.Sections() {
  61. file.Sections = append(file.Sections, &Section{
  62. Name: section.Name,
  63. Addr: section.Addr,
  64. Offset: section.Offset,
  65. Size: section.Size,
  66. })
  67. }
  68. _ = elf.Section{}
  69. for _, symbol := range symbols {
  70. if elf.ST_TYPE(symbol.Info) != elf.STT_OBJECT {
  71. continue
  72. }
  73. if symbol.Section == elf.SHN_UNDEF || symbol.Section >= elf.SHN_LORESERVE {
  74. continue
  75. }
  76. if int(symbol.Section) >= len(file.Sections) {
  77. return nil, fmt.Errorf("invalid section index %d", symbol.Section)
  78. }
  79. section := file.Sections[symbol.Section]
  80. var addr uint64
  81. switch elfFile.Type() {
  82. case elf.ET_REL:
  83. // "In relocatable files, st_value holds a section offset for a defined symbol.
  84. // That is, st_value is an offset from the beginning of the section that st_shndx identifies."
  85. addr = symbol.Value
  86. case elf.ET_EXEC, elf.ET_DYN:
  87. // "In executable and shared object files, st_value holds a virtual address. To make these
  88. // files’ symbols more useful for the dynamic linker, the section offset (file interpretation)
  89. // gives way to a virtual address (memory interpretation) for which the section number is
  90. // irrelevant."
  91. if symbol.Value < section.Addr {
  92. return nil, fmt.Errorf("symbol starts before the start of its section")
  93. }
  94. addr = symbol.Value - section.Addr
  95. if addr+symbol.Size > section.Size {
  96. return nil, fmt.Errorf("symbol extends past the end of its section")
  97. }
  98. default:
  99. return nil, fmt.Errorf("unsupported elf file type %d", elfFile.Type())
  100. }
  101. file.Symbols = append(file.Symbols, &Symbol{
  102. Name: symbol.Name,
  103. Addr: addr,
  104. Size: symbol.Size,
  105. Section: section,
  106. })
  107. }
  108. return file, nil
  109. }
  110. func dumpElfSymbols(r io.ReaderAt) error {
  111. elfFile, err := elf.NewFile(r)
  112. if err != nil {
  113. return cantParseError{err}
  114. }
  115. symbols, err := elfFile.Symbols()
  116. if err != nil {
  117. return err
  118. }
  119. fmt.Println("mockElfFile{")
  120. fmt.Printf("\tt: %#v,\n", elfFile.Type)
  121. fmt.Println("\tsections: []elf.SectionHeader{")
  122. for _, section := range elfFile.Sections {
  123. fmt.Printf("\t\t%#v,\n", section.SectionHeader)
  124. }
  125. fmt.Println("\t},")
  126. fmt.Println("\tsymbols: []elf.Symbol{")
  127. for _, symbol := range symbols {
  128. fmt.Printf("\t\t%#v,\n", symbol)
  129. }
  130. fmt.Println("\t},")
  131. fmt.Println("}")
  132. return nil
  133. }