It may be possible this functionality is already present but after 30 minutes looking through menus and searching I can't find it.
When reverse engineering small databases on custom OSes (e.g. for retrocomputing) it may be possible that a selection of labels is already known (e.g. of the OS API).
It would be nice to be able to import a file of references as a simple NAME=ADDRESS list.
Here's an example that I've used on 6502dis:
OSCLI=$FFF7
OSBYTE=$FFF4
OSWORD=$FFF1
OSWRCH=$FFEE
OSNEWL=$FFE7
OSASCI=$FFE3
OSRDCH=$FFE0
OSFILE=$FFDD
OSARGS=$FFDA
OSBGET=$FFD7
OSBPUT=$FFD4
OSGBPB=$FFD1
OSFIND=$FFCE
Look at the ImportSymbolScript.py plugin
Took me a while to find, hidden in Data->ImportSymbolsScript.py rather than Import or Symbol (!); but yeah that does it for me...
Thanks!
Saying that, running it gets:
creating symbol OSCLI at address fff7
Traceback (most recent call last):
File "E:\ghidra_9.0\Ghidra\Features\Python\ghidra_scripts\ImportSymbolsScript.py", line 13, in <module>
createSymbol(address, pieces[0], False, 0)
TypeError: createSymbol(): expected 3 or 5 args; got 4
I amended the script to use createLabel instead; but I'm not a machine set up to push to git at the moment:
```#Imports a file with lines in the form "symbolName 0xADDRESS"
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()
address = toAddr(long(pieces[1], 16))
print "creating symbol", pieces[0], "at address", address
# silly Ghidra developers created two versions of createSymbol(); Python
# only can see one
createLabel(address, pieces[0], False)
```
Thanks for finding this and providing the fix. We'll include the fix in the next release.
I think its more useful to be able to import from a CSV file.
I've modified the original code, and reversed the order i.e Address then Symbol and changed the separator to a comma.
Please feel free to include it if its of any use ;-)
#Imports a CSV file with lines in the form "0xADDRESS,symbolName"
#@category Data
#@author Roger Clark. Based on the original ImportSymbolsScript.py
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(",")
address = toAddr(long(pieces[0], 16))
symbolName = pieces[1].rstrip() # some CSV's have a newline at the end which needs to be removed
print "creating symbol", symbolName, "at address", address
createSymbol(address, symbolName, False)
Does anyone know where I can find the API to the Python script interface
I need to import the symbol table again when a new symbol is found, but currently if I import the list again, it adds the same symbol again to the address for all the symbols that were imported last time
I found by trial and error there seems to be a function called removeSymbol( ) which takes 2 args, both appear to be strings, but it does not seem to remove symbols.
I suspect it doesn't appear to work, because I don't know what the 2 args are suppose to contain.
Since Ghidra uses Jython, the entire public Java API is accessible from Python. The GhidraScript and FlatProgramAPI classes get automatically injected into the Python scripting environment, so you don't have to import anything special to access the public methods in those classes.
Most helpful comment
Look at the ImportSymbolScript.py plugin