1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import ghidra.app.script.GhidraScript; 18 import ghidra.program.model.listing.Function; 19 import ghidra.program.model.listing.FunctionIterator; 20 21 import java.io.ObjectOutputStream; 22 import java.math.BigInteger; 23 import java.net.Socket; 24 import java.util.ArrayList; 25 import java.util.Arrays; 26 import java.util.List; 27 28 public class FunctionOffsetPostScript extends GhidraScript { 29 run()30 public void run() throws Exception { 31 String spaceSeparatedFunctionNames = propertiesFileParams.getValue("functionNames"); 32 List<String> listOfFunctions = Arrays.asList(spaceSeparatedFunctionNames.split("\\s+")); 33 List<BigInteger> output = new ArrayList<>(); 34 35 // Find the function offsets 36 for (String function : listOfFunctions) { 37 FunctionIterator functionIterator = currentProgram.getListing().getFunctions(true); 38 BigInteger offset = null; 39 while (functionIterator.hasNext()) { 40 Function nextFunction = functionIterator.next(); 41 if (!nextFunction.getName().equals(function)) { 42 continue; // Skip to the next iteration if the function name doesn't match 43 } 44 45 // If the function name matches, calculate the offset 46 offset = 47 nextFunction 48 .getEntryPoint() 49 .subtract(currentProgram.getImageBase().getOffset()) 50 .getOffsetAsBigInteger(); 51 break; 52 } 53 54 // 'output' is appended in the same order as 'listOfFunctions' contains the function 55 // names. If an offset is not found, null is appended. 56 output.add(offset); 57 } 58 try (Socket socket = 59 new Socket( 60 "localhost", 61 Integer.parseInt(propertiesFileParams.getValue("port"))); 62 ObjectOutputStream outputStream = 63 new ObjectOutputStream(socket.getOutputStream()); ) { 64 outputStream.writeObject(output); 65 } 66 } 67 } 68