pe_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/pe"
  17. "strconv"
  18. "testing"
  19. )
  20. func TestPESymbolTable(t *testing.T) {
  21. testCases := []struct {
  22. file *pe.File
  23. symbol string
  24. offset, size uint64
  25. }{
  26. {
  27. file: peSymbolTable1,
  28. symbol: "soong_build_number",
  29. offset: 0x2420,
  30. size: 128,
  31. },
  32. {
  33. file: peSymbolTable2,
  34. symbol: "symbol1",
  35. offset: 0x2420,
  36. size: 128,
  37. },
  38. {
  39. file: peSymbolTable2,
  40. symbol: "symbol2",
  41. offset: 0x24a0,
  42. size: 128,
  43. },
  44. {
  45. // Test when symbol has the same value as the target symbol but is located afterwards in the list
  46. file: &pe.File{
  47. FileHeader: pe.FileHeader{
  48. Machine: pe.IMAGE_FILE_MACHINE_I386,
  49. },
  50. Sections: []*pe.Section{
  51. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".text", VirtualSize: 0x15e83c, VirtualAddress: 0x1000, Size: 0x15ea00, Offset: 0x600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x60500060}},
  52. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".data", VirtualSize: 0x6a58, VirtualAddress: 0x160000, Size: 0x6c00, Offset: 0x15f000, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0600040}},
  53. },
  54. Symbols: []*pe.Symbol{
  55. &pe.Symbol{Name: "_soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2},
  56. &pe.Symbol{Name: ".data", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x3},
  57. &pe.Symbol{Name: "_adb_device_banner", Value: 0xa0, SectionNumber: 2, Type: 0x0, StorageClass: 0x2},
  58. },
  59. },
  60. symbol: "soong_build_number",
  61. offset: 0x15f020,
  62. size: 128,
  63. },
  64. {
  65. // Test when symbol has nothing after it
  66. file: &pe.File{
  67. FileHeader: pe.FileHeader{
  68. Machine: pe.IMAGE_FILE_MACHINE_AMD64,
  69. },
  70. Sections: []*pe.Section{
  71. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".text", VirtualSize: 0x1cc0, VirtualAddress: 0x1000, Size: 0x1e00, Offset: 0x600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x60500020}},
  72. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".data", VirtualSize: 0xa0, VirtualAddress: 0x3000, Size: 0x200, Offset: 0x2400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0600040}},
  73. },
  74. Symbols: []*pe.Symbol{
  75. &pe.Symbol{Name: "soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2},
  76. },
  77. },
  78. symbol: "soong_build_number",
  79. offset: 0x2420,
  80. size: 128,
  81. },
  82. {
  83. // Test when symbol has a symbol in a different section after it
  84. file: &pe.File{
  85. FileHeader: pe.FileHeader{
  86. Machine: pe.IMAGE_FILE_MACHINE_AMD64,
  87. },
  88. Sections: []*pe.Section{
  89. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".text", VirtualSize: 0x1cc0, VirtualAddress: 0x1000, Size: 0x1e00, Offset: 0x600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x60500020}},
  90. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".data", VirtualSize: 0xa0, VirtualAddress: 0x3000, Size: 0x200, Offset: 0x2400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0600040}},
  91. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".rdata", VirtualSize: 0x5e0, VirtualAddress: 0x4000, Size: 0x600, Offset: 0x2600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x40500040}},
  92. },
  93. Symbols: []*pe.Symbol{
  94. &pe.Symbol{Name: "soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2},
  95. &pe.Symbol{Name: "_adb_device_banner", Value: 0x30, SectionNumber: 3, Type: 0x0, StorageClass: 0x2},
  96. },
  97. },
  98. symbol: "soong_build_number",
  99. offset: 0x2420,
  100. size: 128,
  101. },
  102. {
  103. // Test when symbols are out of order
  104. file: &pe.File{
  105. FileHeader: pe.FileHeader{
  106. Machine: pe.IMAGE_FILE_MACHINE_AMD64,
  107. },
  108. Sections: []*pe.Section{
  109. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".text", VirtualSize: 0x1cc0, VirtualAddress: 0x1000, Size: 0x1e00, Offset: 0x600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x60500020}},
  110. &pe.Section{SectionHeader: pe.SectionHeader{Name: ".data", VirtualSize: 0x120, VirtualAddress: 0x3000, Size: 0x200, Offset: 0x2400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0600040}},
  111. },
  112. Symbols: []*pe.Symbol{
  113. &pe.Symbol{Name: "_adb_device_banner", Value: 0xa0, SectionNumber: 2, Type: 0x0, StorageClass: 0x2},
  114. &pe.Symbol{Name: "soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2},
  115. },
  116. },
  117. symbol: "soong_build_number",
  118. offset: 0x2420,
  119. size: 128,
  120. },
  121. }
  122. for i, testCase := range testCases {
  123. t.Run(strconv.Itoa(i), func(t *testing.T) {
  124. file, err := extractPESymbols(testCase.file)
  125. if err != nil {
  126. t.Error(err.Error())
  127. }
  128. offset, size, err := findSymbol(file, testCase.symbol)
  129. if err != nil {
  130. t.Error(err.Error())
  131. }
  132. if offset != testCase.offset || size != testCase.size {
  133. t.Errorf("expected %x:%x, got %x:%x", testCase.offset, testCase.size, offset, size)
  134. }
  135. })
  136. }
  137. }