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
* REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +15,8 @@
*/
package ghidra.app.util.viewer.field;
import javax.help.UnsupportedOperationException;
import ghidra.framework.options.ToolOptions;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType;
@@ -35,7 +36,10 @@ public class LabelCodeUnitFormat extends BrowserCodeUnitFormat {
@Override
protected String getOffcutLabelStringForInstruction(Address offcutAddress,
Instruction instruction) {
Instruction instruction, Address markupAddress) {
if (markupAddress != null) {
throw new UnsupportedOperationException();
}
Program program = instruction.getProgram();
Symbol offsym = program.getSymbolTable().getPrimarySymbol(offcutAddress);
Address instructionAddress = instruction.getMinAddress();
@@ -230,7 +230,7 @@ public class LabelFieldFactory extends FieldFactory {
String offcutSymbolText = null;
if (!offcutSymbol.isDynamic()) {
// the formatter doesn't change dynamic labels
offcutSymbolText = codeUnitFormat.getOffcutLabelString(offcutAddress, cu);
offcutSymbolText = codeUnitFormat.getOffcutLabelString(offcutAddress, cu, null);
}
else {
offcutSymbolText = offcutSymbol.getName();
@@ -323,8 +323,8 @@ public class CodeUnitFormat {
else if (options.includeInferredVariableMarkup) {
boolean isRead = isRead(reg, instr);
Variable regVar = program.getFunctionManager()
.getReferencedVariable(
instr.getMinAddress(), reg.getAddress(), reg.getMinimumByteSize(), isRead);
.getReferencedVariable(instr.getMinAddress(), reg.getAddress(),
reg.getMinimumByteSize(), isRead);
if (regVar != null) {
// 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) &&
@@ -332,9 +332,8 @@ public class CodeUnitFormat {
// 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
Variable regWriteVar = program.getFunctionManager()
.getReferencedVariable(
instr.getMinAddress(), reg.getAddress(), reg.getMinimumByteSize(),
false);
.getReferencedVariable(instr.getMinAddress(), reg.getAddress(),
reg.getMinimumByteSize(), false);
if (regWriteVar != regVar) {
continue; // TODO: tough case - not which operand is read vs. write!
}
@@ -631,11 +630,10 @@ public class CodeUnitFormat {
return false;
}
Variable regVar =
instr.getProgram()
Variable regVar = instr.getProgram()
.getFunctionManager()
.getReferencedVariable(instr.getMinAddress(),
associatedRegister.getAddress(), associatedRegister.getMinimumByteSize(), true);
.getReferencedVariable(instr.getMinAddress(), associatedRegister.getAddress(),
associatedRegister.getMinimumByteSize(), true);
if (regVar == null) {
return false;
}
@@ -1006,9 +1004,7 @@ public class CodeUnitFormat {
* @return equate which matches scalar value or null if not found.
*/
private Equate findEquate(Scalar scalar, List<Equate> equates) {
Iterator<Equate> equateItr = equates.iterator();
while (equateItr.hasNext()) {
Equate equate = equateItr.next();
for (Equate equate : equates) {
if (equate.getValue() == scalar.getSignedValue() ||
equate.getValue() == scalar.getValue()) {
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
// generally only correspond to writes
Variable refVar = fromCodeUnit.getProgram()
.getFunctionManager()
.getReferencedVariable(
fromCodeUnit.getMinAddress(), ref.getToAddress(), 0, false);
.getFunctionManager()
.getReferencedVariable(fromCodeUnit.getMinAddress(), ref.getToAddress(), 0, false);
Object repObj = getReferenceRepresentation(fromCodeUnit, ref, refVar);
return repObj != null ? repObj.toString() : null;
}
@@ -1200,10 +1195,9 @@ public class CodeUnitFormat {
}
private Object getOffsetReferenceRepresentation(CodeUnit cu, OffsetReference offsetRef) {
Reference baseRef =
new MemReferenceImpl(offsetRef.getFromAddress(), offsetRef.getBaseAddress(),
RefType.DATA,
offsetRef.getSource(), offsetRef.getOperandIndex(), offsetRef.isPrimary());
Reference baseRef = new MemReferenceImpl(offsetRef.getFromAddress(),
offsetRef.getBaseAddress(), RefType.DATA, offsetRef.getSource(),
offsetRef.getOperandIndex(), offsetRef.isPrimary());
Object baseRefObj = getMemoryReferenceLabel(cu, baseRef);
long offset = offsetRef.getOffset();
String sign = "+";
@@ -1365,7 +1359,7 @@ public class CodeUnitFormat {
if (symbolAddress.isMemoryAddress()) {
CodeUnit cu = program.getListing().getCodeUnitContaining(symbolAddress);
if (isOffcut(symbolAddress, cu)) {
return getOffcutLabelString(symbolAddress, cu);
return getOffcutLabelString(symbolAddress, cu, markupAddress);
}
else if (isStringData(cu)) {
return getLabelStringForStringData((Data) cu, symbol);
@@ -1406,9 +1400,10 @@ public class CodeUnitFormat {
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) {
return getOffcutLabelStringForInstruction(offcutAddress, (Instruction) cu);
return getOffcutLabelStringForInstruction(offcutAddress, (Instruction) cu,
markupAddress);
}
return getOffcutDataString(offcutAddress, (Data) cu);
}
@@ -1439,8 +1434,19 @@ public class CodeUnitFormat {
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,
Instruction instruction) {
Instruction instruction, Address markupAddress) {
Program program = instruction.getProgram();
Symbol offsym = program.getSymbolTable().getPrimarySymbol(offcutAddress);
Address instructionAddress = instruction.getMinAddress();
@@ -1451,7 +1457,12 @@ public class CodeUnitFormat {
Symbol containingSymbol = program.getSymbolTable().getPrimarySymbol(instructionAddress);
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);
}