ghidra icon indicating copy to clipboard operation
ghidra copied to clipboard

Use different names for a variable

Open jrmuizel opened this issue 6 years ago • 11 comments

Is there some way to separate out uses of a variable and give them separate names.

i.e. sometimes a particular stack location or register is reused for a different purpose. It would be valuable to give this information to the decompiler so that it can treat it as a separate variable.

jrmuizel avatar Sep 04 '19 19:09 jrmuizel

It is surely something to be wanted.

jackson2k2 avatar Oct 26 '19 23:10 jackson2k2

@emteere is there a recommended workflow to deal with this problem?

jrmuizel avatar Dec 05 '19 16:12 jrmuizel

I believe this should be labelled as a feature request.

Jaguar83 avatar Feb 27 '20 01:02 Jaguar83

For registers you can do it:

  1. Right click in the Decompiler → Commit Locals.
  2. Right click on the variable → Split out as New Variable.

saruman9 avatar Mar 02 '20 14:03 saruman9

4,883.64

For registers you can do it:

1. Right click in the Decompiler → _Commit Locals_.

2. Right click on the variable → _Split out as New Variable_.

I don't see this option in either the decompiler or the listing.

  • OS: Windows 10
  • Java Version: 11.0.6
  • Ghidra Version: 9.1.2

Jaguar83 avatar Mar 20 '20 02:03 Jaguar83

The code is here, but I also have not been able to find this item in the right-click menu.

hostar avatar Mar 26 '20 14:03 hostar

Looking over the code, it seems that this only applies to locals that are declared within the function in the assembly listing. Even then, the varnode associated with the variable must have a "HighVariable" whatever that is. There is no support for reusing parameters, renamed registers or other parameters that don't have whatever "HighVariable" is.

Jaguar83 avatar Mar 27 '20 06:03 Jaguar83

It is currently possible to split out a local variable as a new variable. Parameters of a functions do not have this functionality (yet).

madebr avatar Jan 24 '21 21:01 madebr

@madebr how?

jrmuizel avatar Jan 25 '21 00:01 jrmuizel

@jrmuizel Search for Split Out As New Variable in the documentation (F1). It's currently the last section in the Decompiler Window page.

madebr avatar Jan 25 '21 03:01 madebr

I've encountered the described issue many many times, but I would like to add another case where this feature would be necessary, and doesn't conceptually fall under "different purpose": static casts.

E.g.

derived_class* foo = (derived_class*) pBaseClass;
...
if (foo->derivedClassIntMember == 42) {
    ...
}

ends up many times looking like

// foo's type has been changed to derived_class* via the UI. 
// The register it is assigned to is never used for anything but foo of type derived_class* in the function.
derived_class* foo = (derived_class*) pBaseClass;
...
if ((base_class*)foo[16].pBaseClassPointerToSomething == (something_else*)42) {
    ...
}

The generated code is much harder to follow and the quality of the Find users of... results is diminished by the resulting false positives and negatives.

ghost avatar Aug 27 '22 23:08 ghost