1*333d2b36SAndroid Build Coastguard Worker// Copyright 2018 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage symbol_inject 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Workerimport ( 18*333d2b36SAndroid Build Coastguard Worker "debug/pe" 19*333d2b36SAndroid Build Coastguard Worker "strconv" 20*333d2b36SAndroid Build Coastguard Worker "testing" 21*333d2b36SAndroid Build Coastguard Worker) 22*333d2b36SAndroid Build Coastguard Worker 23*333d2b36SAndroid Build Coastguard Workerfunc TestPESymbolTable(t *testing.T) { 24*333d2b36SAndroid Build Coastguard Worker testCases := []struct { 25*333d2b36SAndroid Build Coastguard Worker file *pe.File 26*333d2b36SAndroid Build Coastguard Worker symbol string 27*333d2b36SAndroid Build Coastguard Worker offset, size uint64 28*333d2b36SAndroid Build Coastguard Worker }{ 29*333d2b36SAndroid Build Coastguard Worker { 30*333d2b36SAndroid Build Coastguard Worker file: peSymbolTable1, 31*333d2b36SAndroid Build Coastguard Worker symbol: "soong_build_number", 32*333d2b36SAndroid Build Coastguard Worker offset: 0x2420, 33*333d2b36SAndroid Build Coastguard Worker size: 128, 34*333d2b36SAndroid Build Coastguard Worker }, 35*333d2b36SAndroid Build Coastguard Worker { 36*333d2b36SAndroid Build Coastguard Worker file: peSymbolTable2, 37*333d2b36SAndroid Build Coastguard Worker symbol: "symbol1", 38*333d2b36SAndroid Build Coastguard Worker offset: 0x2420, 39*333d2b36SAndroid Build Coastguard Worker size: 128, 40*333d2b36SAndroid Build Coastguard Worker }, 41*333d2b36SAndroid Build Coastguard Worker { 42*333d2b36SAndroid Build Coastguard Worker file: peSymbolTable2, 43*333d2b36SAndroid Build Coastguard Worker symbol: "symbol2", 44*333d2b36SAndroid Build Coastguard Worker offset: 0x24a0, 45*333d2b36SAndroid Build Coastguard Worker size: 128, 46*333d2b36SAndroid Build Coastguard Worker }, 47*333d2b36SAndroid Build Coastguard Worker { 48*333d2b36SAndroid Build Coastguard Worker // Test when symbol has the same value as the target symbol but is located afterwards in the list 49*333d2b36SAndroid Build Coastguard Worker file: &pe.File{ 50*333d2b36SAndroid Build Coastguard Worker FileHeader: pe.FileHeader{ 51*333d2b36SAndroid Build Coastguard Worker Machine: pe.IMAGE_FILE_MACHINE_I386, 52*333d2b36SAndroid Build Coastguard Worker }, 53*333d2b36SAndroid Build Coastguard Worker Sections: []*pe.Section{ 54*333d2b36SAndroid Build Coastguard Worker &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}}, 55*333d2b36SAndroid Build Coastguard Worker &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}}, 56*333d2b36SAndroid Build Coastguard Worker }, 57*333d2b36SAndroid Build Coastguard Worker Symbols: []*pe.Symbol{ 58*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: "_soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2}, 59*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: ".data", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x3}, 60*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: "_adb_device_banner", Value: 0xa0, SectionNumber: 2, Type: 0x0, StorageClass: 0x2}, 61*333d2b36SAndroid Build Coastguard Worker }, 62*333d2b36SAndroid Build Coastguard Worker }, 63*333d2b36SAndroid Build Coastguard Worker symbol: "soong_build_number", 64*333d2b36SAndroid Build Coastguard Worker offset: 0x15f020, 65*333d2b36SAndroid Build Coastguard Worker size: 128, 66*333d2b36SAndroid Build Coastguard Worker }, 67*333d2b36SAndroid Build Coastguard Worker { 68*333d2b36SAndroid Build Coastguard Worker // Test when symbol has nothing after it 69*333d2b36SAndroid Build Coastguard Worker file: &pe.File{ 70*333d2b36SAndroid Build Coastguard Worker FileHeader: pe.FileHeader{ 71*333d2b36SAndroid Build Coastguard Worker Machine: pe.IMAGE_FILE_MACHINE_AMD64, 72*333d2b36SAndroid Build Coastguard Worker }, 73*333d2b36SAndroid Build Coastguard Worker Sections: []*pe.Section{ 74*333d2b36SAndroid Build Coastguard Worker &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}}, 75*333d2b36SAndroid Build Coastguard Worker &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}}, 76*333d2b36SAndroid Build Coastguard Worker }, 77*333d2b36SAndroid Build Coastguard Worker Symbols: []*pe.Symbol{ 78*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: "soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2}, 79*333d2b36SAndroid Build Coastguard Worker }, 80*333d2b36SAndroid Build Coastguard Worker }, 81*333d2b36SAndroid Build Coastguard Worker symbol: "soong_build_number", 82*333d2b36SAndroid Build Coastguard Worker offset: 0x2420, 83*333d2b36SAndroid Build Coastguard Worker size: 128, 84*333d2b36SAndroid Build Coastguard Worker }, 85*333d2b36SAndroid Build Coastguard Worker { 86*333d2b36SAndroid Build Coastguard Worker // Test when symbol has a symbol in a different section after it 87*333d2b36SAndroid Build Coastguard Worker file: &pe.File{ 88*333d2b36SAndroid Build Coastguard Worker FileHeader: pe.FileHeader{ 89*333d2b36SAndroid Build Coastguard Worker Machine: pe.IMAGE_FILE_MACHINE_AMD64, 90*333d2b36SAndroid Build Coastguard Worker }, 91*333d2b36SAndroid Build Coastguard Worker Sections: []*pe.Section{ 92*333d2b36SAndroid Build Coastguard Worker &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}}, 93*333d2b36SAndroid Build Coastguard Worker &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}}, 94*333d2b36SAndroid Build Coastguard Worker &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}}, 95*333d2b36SAndroid Build Coastguard Worker }, 96*333d2b36SAndroid Build Coastguard Worker Symbols: []*pe.Symbol{ 97*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: "soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2}, 98*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: "_adb_device_banner", Value: 0x30, SectionNumber: 3, Type: 0x0, StorageClass: 0x2}, 99*333d2b36SAndroid Build Coastguard Worker }, 100*333d2b36SAndroid Build Coastguard Worker }, 101*333d2b36SAndroid Build Coastguard Worker symbol: "soong_build_number", 102*333d2b36SAndroid Build Coastguard Worker offset: 0x2420, 103*333d2b36SAndroid Build Coastguard Worker size: 128, 104*333d2b36SAndroid Build Coastguard Worker }, 105*333d2b36SAndroid Build Coastguard Worker { 106*333d2b36SAndroid Build Coastguard Worker // Test when symbols are out of order 107*333d2b36SAndroid Build Coastguard Worker file: &pe.File{ 108*333d2b36SAndroid Build Coastguard Worker FileHeader: pe.FileHeader{ 109*333d2b36SAndroid Build Coastguard Worker Machine: pe.IMAGE_FILE_MACHINE_AMD64, 110*333d2b36SAndroid Build Coastguard Worker }, 111*333d2b36SAndroid Build Coastguard Worker Sections: []*pe.Section{ 112*333d2b36SAndroid Build Coastguard Worker &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}}, 113*333d2b36SAndroid Build Coastguard Worker &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}}, 114*333d2b36SAndroid Build Coastguard Worker }, 115*333d2b36SAndroid Build Coastguard Worker Symbols: []*pe.Symbol{ 116*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: "_adb_device_banner", Value: 0xa0, SectionNumber: 2, Type: 0x0, StorageClass: 0x2}, 117*333d2b36SAndroid Build Coastguard Worker &pe.Symbol{Name: "soong_build_number", Value: 0x20, SectionNumber: 2, Type: 0x0, StorageClass: 0x2}, 118*333d2b36SAndroid Build Coastguard Worker }, 119*333d2b36SAndroid Build Coastguard Worker }, 120*333d2b36SAndroid Build Coastguard Worker symbol: "soong_build_number", 121*333d2b36SAndroid Build Coastguard Worker offset: 0x2420, 122*333d2b36SAndroid Build Coastguard Worker size: 128, 123*333d2b36SAndroid Build Coastguard Worker }, 124*333d2b36SAndroid Build Coastguard Worker } 125*333d2b36SAndroid Build Coastguard Worker 126*333d2b36SAndroid Build Coastguard Worker for i, testCase := range testCases { 127*333d2b36SAndroid Build Coastguard Worker t.Run(strconv.Itoa(i), func(t *testing.T) { 128*333d2b36SAndroid Build Coastguard Worker file, err := extractPESymbols(testCase.file) 129*333d2b36SAndroid Build Coastguard Worker if err != nil { 130*333d2b36SAndroid Build Coastguard Worker t.Error(err.Error()) 131*333d2b36SAndroid Build Coastguard Worker } 132*333d2b36SAndroid Build Coastguard Worker offset, size, err := findSymbol(file, testCase.symbol) 133*333d2b36SAndroid Build Coastguard Worker if err != nil { 134*333d2b36SAndroid Build Coastguard Worker t.Error(err.Error()) 135*333d2b36SAndroid Build Coastguard Worker } 136*333d2b36SAndroid Build Coastguard Worker if offset != testCase.offset || size != testCase.size { 137*333d2b36SAndroid Build Coastguard Worker t.Errorf("expected %x:%x, got %x:%x", testCase.offset, testCase.size, offset, size) 138*333d2b36SAndroid Build Coastguard Worker } 139*333d2b36SAndroid Build Coastguard Worker }) 140*333d2b36SAndroid Build Coastguard Worker } 141*333d2b36SAndroid Build Coastguard Worker} 142