Merge remote-tracking branch

'origin/GP-3985_ghidra1_RespectNamespaceOption' (Closes #5886)
This commit is contained in:
Ryan Kurtz
2023-10-31 07:11:25 -04:00
3 changed files with 42 additions and 27 deletions
@@ -1,6 +1,5 @@
/* ### /* ###
* IP: GHIDRA * IP: GHIDRA
* REVIEWED: YES
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,6 +15,8 @@
*/ */
package ghidra.app.util.viewer.field; package ghidra.app.util.viewer.field;
import javax.help.UnsupportedOperationException;
import ghidra.framework.options.ToolOptions; import ghidra.framework.options.ToolOptions;
import ghidra.program.model.address.Address; import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType; import ghidra.program.model.data.DataType;
@@ -35,7 +36,10 @@ public class LabelCodeUnitFormat extends BrowserCodeUnitFormat {
@Override @Override
protected String getOffcutLabelStringForInstruction(Address offcutAddress, protected String getOffcutLabelStringForInstruction(Address offcutAddress,
Instruction instruction) { Instruction instruction, Address markupAddress) {
if (markupAddress != null) {
throw new UnsupportedOperationException();
}
Program program = instruction.getProgram(); Program program = instruction.getProgram();
Symbol offsym = program.getSymbolTable().getPrimarySymbol(offcutAddress); Symbol offsym = program.getSymbolTable().getPrimarySymbol(offcutAddress);
Address instructionAddress = instruction.getMinAddress(); Address instructionAddress = instruction.getMinAddress();
@@ -230,7 +230,7 @@ public class LabelFieldFactory extends FieldFactory {
String offcutSymbolText = null; String offcutSymbolText = null;
if (!offcutSymbol.isDynamic()) { if (!offcutSymbol.isDynamic()) {
// the formatter doesn't change dynamic labels // the formatter doesn't change dynamic labels
offcutSymbolText = codeUnitFormat.getOffcutLabelString(offcutAddress, cu); offcutSymbolText = codeUnitFormat.getOffcutLabelString(offcutAddress, cu, null);
} }
else { else {
offcutSymbolText = offcutSymbol.getName(); offcutSymbolText = offcutSymbol.getName();
@@ -323,8 +323,8 @@ public class CodeUnitFormat {
else if (options.includeInferredVariableMarkup) { else if (options.includeInferredVariableMarkup) {
boolean isRead = isRead(reg, instr); boolean isRead = isRead(reg, instr);
Variable regVar = program.getFunctionManager() Variable regVar = program.getFunctionManager()
.getReferencedVariable( .getReferencedVariable(instr.getMinAddress(), reg.getAddress(),
instr.getMinAddress(), reg.getAddress(), reg.getMinimumByteSize(), isRead); reg.getMinimumByteSize(), isRead);
if (regVar != null) { if (regVar != null) {
// TODO: If register appears more than once, how can we distinguish read vs. write occurrence in operands // TODO: If register appears more than once, how can we distinguish read vs. write occurrence in operands
if (isRead && isWritten(reg, instr) && !hasRegisterWriteReference(instr, reg) && if (isRead && isWritten(reg, instr) && !hasRegisterWriteReference(instr, reg) &&
@@ -332,9 +332,8 @@ public class CodeUnitFormat {
// If register both read and written and there are no write references for this instruction // If register both read and written and there are no write references for this instruction
// see if there is only one reference to choose from - if not we can't determine how to markup // see if there is only one reference to choose from - if not we can't determine how to markup
Variable regWriteVar = program.getFunctionManager() Variable regWriteVar = program.getFunctionManager()
.getReferencedVariable( .getReferencedVariable(instr.getMinAddress(), reg.getAddress(),
instr.getMinAddress(), reg.getAddress(), reg.getMinimumByteSize(), reg.getMinimumByteSize(), false);
false);
if (regWriteVar != regVar) { if (regWriteVar != regVar) {
continue; // TODO: tough case - not which operand is read vs. write! continue; // TODO: tough case - not which operand is read vs. write!
} }
@@ -631,11 +630,10 @@ public class CodeUnitFormat {
return false; return false;
} }
Variable regVar = Variable regVar = instr.getProgram()
instr.getProgram()
.getFunctionManager() .getFunctionManager()
.getReferencedVariable(instr.getMinAddress(), .getReferencedVariable(instr.getMinAddress(), associatedRegister.getAddress(),
associatedRegister.getAddress(), associatedRegister.getMinimumByteSize(), true); associatedRegister.getMinimumByteSize(), true);
if (regVar == null) { if (regVar == null) {
return false; return false;
} }
@@ -1006,9 +1004,7 @@ public class CodeUnitFormat {
* @return equate which matches scalar value or null if not found. * @return equate which matches scalar value or null if not found.
*/ */
private Equate findEquate(Scalar scalar, List<Equate> equates) { private Equate findEquate(Scalar scalar, List<Equate> equates) {
Iterator<Equate> equateItr = equates.iterator(); for (Equate equate : equates) {
while (equateItr.hasNext()) {
Equate equate = equateItr.next();
if (equate.getValue() == scalar.getSignedValue() || if (equate.getValue() == scalar.getSignedValue() ||
equate.getValue() == scalar.getValue()) { equate.getValue() == scalar.getValue()) {
return equate; return equate;
@@ -1156,9 +1152,8 @@ public class CodeUnitFormat {
// NOTE: The isRead param is false since it really only pertains to register references which should // NOTE: The isRead param is false since it really only pertains to register references which should
// generally only correspond to writes // generally only correspond to writes
Variable refVar = fromCodeUnit.getProgram() Variable refVar = fromCodeUnit.getProgram()
.getFunctionManager() .getFunctionManager()
.getReferencedVariable( .getReferencedVariable(fromCodeUnit.getMinAddress(), ref.getToAddress(), 0, false);
fromCodeUnit.getMinAddress(), ref.getToAddress(), 0, false);
Object repObj = getReferenceRepresentation(fromCodeUnit, ref, refVar); Object repObj = getReferenceRepresentation(fromCodeUnit, ref, refVar);
return repObj != null ? repObj.toString() : null; return repObj != null ? repObj.toString() : null;
} }
@@ -1200,10 +1195,9 @@ public class CodeUnitFormat {
} }
private Object getOffsetReferenceRepresentation(CodeUnit cu, OffsetReference offsetRef) { private Object getOffsetReferenceRepresentation(CodeUnit cu, OffsetReference offsetRef) {
Reference baseRef = Reference baseRef = new MemReferenceImpl(offsetRef.getFromAddress(),
new MemReferenceImpl(offsetRef.getFromAddress(), offsetRef.getBaseAddress(), offsetRef.getBaseAddress(), RefType.DATA, offsetRef.getSource(),
RefType.DATA, offsetRef.getOperandIndex(), offsetRef.isPrimary());
offsetRef.getSource(), offsetRef.getOperandIndex(), offsetRef.isPrimary());
Object baseRefObj = getMemoryReferenceLabel(cu, baseRef); Object baseRefObj = getMemoryReferenceLabel(cu, baseRef);
long offset = offsetRef.getOffset(); long offset = offsetRef.getOffset();
String sign = "+"; String sign = "+";
@@ -1365,7 +1359,7 @@ public class CodeUnitFormat {
if (symbolAddress.isMemoryAddress()) { if (symbolAddress.isMemoryAddress()) {
CodeUnit cu = program.getListing().getCodeUnitContaining(symbolAddress); CodeUnit cu = program.getListing().getCodeUnitContaining(symbolAddress);
if (isOffcut(symbolAddress, cu)) { if (isOffcut(symbolAddress, cu)) {
return getOffcutLabelString(symbolAddress, cu); return getOffcutLabelString(symbolAddress, cu, markupAddress);
} }
else if (isStringData(cu)) { else if (isStringData(cu)) {
return getLabelStringForStringData((Data) cu, symbol); return getLabelStringForStringData((Data) cu, symbol);
@@ -1406,9 +1400,10 @@ public class CodeUnitFormat {
return prefix + UNDERSCORE + SymbolUtilities.getAddressString(symbol.getAddress()); return prefix + UNDERSCORE + SymbolUtilities.getAddressString(symbol.getAddress());
} }
public String getOffcutLabelString(Address offcutAddress, CodeUnit cu) { public String getOffcutLabelString(Address offcutAddress, CodeUnit cu, Address markupAddress) {
if (cu instanceof Instruction) { if (cu instanceof Instruction) {
return getOffcutLabelStringForInstruction(offcutAddress, (Instruction) cu); return getOffcutLabelStringForInstruction(offcutAddress, (Instruction) cu,
markupAddress);
} }
return getOffcutDataString(offcutAddress, (Data) cu); return getOffcutDataString(offcutAddress, (Data) cu);
} }
@@ -1439,8 +1434,19 @@ public class CodeUnitFormat {
return getDefaultOffcutString(offcutSymbol, data, diff, false); return getDefaultOffcutString(offcutSymbol, data, diff, false);
} }
/**
* Generate label string. This may serve two use cases:
* <ul>
* <li>Generating operand label at markupAddress for referenced instruction and offcutAddress</li>
* <li>Generating offcut label for an offcutAddress with instruction (markupAddress=null)</li>
* </ul>
* @param offcutAddress address for which generated label represents
* @param instruction instruction containing offcut address
* @param markupAddress address where a label will be referenced from (may be null)
* @return generated offcut label
*/
protected String getOffcutLabelStringForInstruction(Address offcutAddress, protected String getOffcutLabelStringForInstruction(Address offcutAddress,
Instruction instruction) { Instruction instruction, Address markupAddress) {
Program program = instruction.getProgram(); Program program = instruction.getProgram();
Symbol offsym = program.getSymbolTable().getPrimarySymbol(offcutAddress); Symbol offsym = program.getSymbolTable().getPrimarySymbol(offcutAddress);
Address instructionAddress = instruction.getMinAddress(); Address instructionAddress = instruction.getMinAddress();
@@ -1451,7 +1457,12 @@ public class CodeUnitFormat {
Symbol containingSymbol = program.getSymbolTable().getPrimarySymbol(instructionAddress); Symbol containingSymbol = program.getSymbolTable().getPrimarySymbol(instructionAddress);
if (containingSymbol != null) { if (containingSymbol != null) {
return options.simplifyTemplate(containingSymbol.getName()) + PLUS + diff; String displayName = containingSymbol.getName();
if (markupAddress != null) {
displayName = addNamespace(program, containingSymbol.getParentNamespace(),
displayName, markupAddress);
}
return simplifyTemplate(displayName) + PLUS + diff;
} }
return getDefaultOffcutString(offsym, instruction, diff, false); return getDefaultOffcutString(offsym, instruction, diff, false);
} }