ghidra/Ghidra/Features/Python/ghidra_scripts/ImportSymbolsScript.py
2021-03-17 18:22:50 -04:00

53 lines
1.8 KiB
Python

## ###
# IP: GHIDRA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
# Imports a file with lines in the form "symbolName 0xADDRESS function_or_label" where "f" indicates a function and "l" a label
# @author unkown; edited by matedealer <git@matedealer.de>
# @category Data
#
from ghidra.program.model.symbol.SourceType import *
import string
functionManager = currentProgram.getFunctionManager()
f = askFile("Give me a file to open", "Go baby go!")
for line in file(f.absolutePath): # note, cannot use open(), since that is in GhidraScript
pieces = line.split()
name = pieces[0]
address = toAddr(long(pieces[1], 16))
try:
function_or_label = pieces[2]
except IndexError:
function_or_label = "l"
if function_or_label == "f":
func = functionManager.getFunctionAt(address)
if func is not None:
old_name = func.getName()
func.setName(name, USER_DEFINED)
print("Renamed function {} to {} at address {}".format(old_name, name, address))
else:
func = createFunction(address, name)
print("Created function {} at address {}".format(name, address))
else:
print("Created label {} at address {}".format(name, address))
createLabel(address, name, False)