Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 49 additions & 18 deletions Ghidra/Features/Jython/ghidra_scripts/ImportSymbolsScript.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
## ###
# 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.
# 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 text file containing symbol definitions, with a maximum of one symbol defined per line. Each symbol definition is in the form of "symbol_name address function_or_label", where "symbol_name" is the name of the symbol, "address" is the address of the symbol in one of the forms listed below, and "function_or_label" is either "f" or "l", with "f" indicating that a function is to be created and "l" indicating that a label is to be created.
# Imports a text file containing symbol definitions, with a maximum of one symbol defined per line. Each symbol definition is in the form of "symbol_name address function_or_label [data_type]", where "symbol_name" is the name of the symbol, "address" is the address of the symbol in one of the forms listed below, "function_or_label" is either "f" or "l", with "f" indicating that a function is to be created and "l" indicating that a label is to be created, and "data_type" is an optional data type name to apply to labels.
# Address formats are the same as those that can be used with the "Go to address" function. For example:
# - 1234abcd
# - 0x1234abcd
Expand All @@ -22,15 +22,42 @@
# - MEMORY_REGION:1234abcd
# - MEMORY_REGION:0x1234abcd
# Omitting the address space or memory region specifier from the address will result in the function or label being created in the default address space.
# @author unkown; edited by matedealer <[email protected]>
# @category Import
# Data type formats (optional 4th field for labels only):
# - Simple types: "byte", "word", "dword", "pointer", etc.
# - Array types: "byte[256]", "dword[10]", etc.
# @author <[email protected]>; edited by matedealer <[email protected]>
# @category Data
# @runtime Jython
#

from ghidra.program.model.symbol.SourceType import *
from ghidra.program.model.data import ArrayDataType
import string

functionManager = currentProgram.getFunctionManager()
dataTypeManager = currentProgram.getDataTypeManager()
listing = currentProgram.getListing()

def parseDataType(type_spec):
# Check for array syntax: type[count]
if '[' in type_spec and type_spec.endswith(']'):
base_type = type_spec[:type_spec.index('[')]
array_count = int(type_spec[type_spec.index('[')+1:-1])
dt_list = []
dataTypeManager.findDataTypes(base_type, dt_list)
if len(dt_list) > 0:
return ArrayDataType(dt_list[0], array_count, dt_list[0].getLength())
else:
print("Warning: base data type '{}' not found".format(base_type))
return None
else:
dt_list = []
dataTypeManager.findDataTypes(type_spec, dt_list)
if len(dt_list) > 0:
return dt_list[0]
else:
print("Warning: data type '{}' not found".format(type_spec))
return None

f = askFile("Give me a file to open", "Go baby go!")

Expand All @@ -45,7 +72,7 @@
except IndexError:
function_or_label = "l"


if function_or_label == "f":
func = functionManager.getFunctionAt(address)

Expand All @@ -58,5 +85,9 @@
print("Created function {} at address {}".format(name, address))

else:
print("Created label {} at address {}".format(name, address))
createLabel(address, name, False)
if len(pieces) > 3:
dt = parseDataType(pieces[3])
if dt is not None:
listing.createData(address, dt)
print("Created label {} at address {}".format(name, address))