Ghidra: createLabel doesn't set a new label as the primary label on a function

Created on 31 Mar 2019  路  5Comments  路  Source: NationalSecurityAgency/ghidra

Describe the bug
When using the default createLabel method exposed to the python api, it will not correctly set the new label as the primary label even if True is passed for makePrimary on a function. It does seem to work at any other point in the listing.

To Reproduce
Steps to reproduce the issue:

  1. use the cursor to select a the beginning of a function at a "FUN_*"
  2. open the python console
  3. type createLabel(currentAddress, "test", True)
  4. In listing, see the new label for the function name and that it is primary
  5. go back to console and type createLabel(currentAddress, "next", True)
  6. In listing, see the old label is still the primary label
  1. go to any other point in the function, repeat 3-5
  2. see that "next" is now the primary label (expected behavior I think)

Expected behavior
Make the new label the primary label

Environment (please complete the following information):

  • OS: Windows 10 x64
  • Java Version: openjdk 11.0.2
  • Ghidra Version: 9.0 and 9.0.1

Additional context
I can edit the new label I've created and manually change it to be the primary label by clicking the box. Maybe I'm just doing something wrong. Thanks for any help.

Bug

Most helpful comment

There's several ways to accomplish it via the Ghidra API. Try invoking the SetLabelPrimaryCmd. This command will handle setting a label to primary, and it handles the case where you want to change the function name.

Command command= new SetLabelPrimaryCmd( address, name, namespace )
command.applyTo( currentProgram );

The "name" parameter needs to already exist as a label/symbol.

All 5 comments

Sorry for the confusion. This behavior is as-designed; functions are special case labels(symbols). In order to change the name of a function, you must get the function object at the current address and call setName(). Will try to update the javadoc to reflect this case.

Function fun = getFunctionAt(currentAddress)
if ( fun == null )
createLabel(...)
else
fun.setName(...)

Thanks!

Thanks for the fast response.

Actually, I have another question, how do I make a label that already exists the new primary label, or function name. I get

ghidra.util.exception.DuplicateNameException: ... A symbol named "test" already exists at this address!

when I try to use setName. There must be a way since I can just click the box when editing the label manually. Do I have do delete the label first? @GhidraKnight

There's several ways to accomplish it via the Ghidra API. Try invoking the SetLabelPrimaryCmd. This command will handle setting a label to primary, and it handles the case where you want to change the function name.

Command command= new SetLabelPrimaryCmd( address, name, namespace )
command.applyTo( currentProgram );

The "name" parameter needs to already exist as a label/symbol.

Thanks again, works great! In javadocs, I noticed "that" should be "than" below:

public class SetLabelPrimaryCmd
extends java.lang.Object
implements Command
Command to make a label the primary label at an address. Only really makes sense if there is more that one label at the address - otherwise the label will already be primary.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

astrelsky picture astrelsky  路  3Comments

rrivera1849 picture rrivera1849  路  3Comments

loudinthecloud picture loudinthecloud  路  3Comments

chibicitiberiu picture chibicitiberiu  路  3Comments

pd0wm picture pd0wm  路  3Comments