Ghidra is a software reverse engineering (SRE) framework
GP-901 added regiater alias support and defined WREG aliases for PIC24 variants
ghidra18f9b06738443
diff --git a/Ghidra/Features/Base/ghidra_scripts/CreateRelocationBasedOperandReferences.java b/Ghidra/Features/Base/ghidra_scripts/CreateRelocationBasedOperandReferences.javanew file mode 100644index 0000000000..3038a52e16--- /dev/null+++ b/Ghidra/Features/Base/ghidra_scripts/CreateRelocationBasedOperandReferences.java@@ -0,0 +1,160 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+// This script creates references on scalar operands which can be directly+// correlated to a symbol or equate via a relocation table entry. Only symbols within+// byte-oriented address spaces will be considered.+//@category Analysis++import java.util.Iterator;+import java.util.List;++import ghidra.app.script.GhidraScript;+import ghidra.program.model.address.Address;+import ghidra.program.model.listing.Instruction;+import ghidra.program.model.listing.Listing;+import ghidra.program.model.reloc.Relocation;+import ghidra.program.model.reloc.RelocationTable;+import ghidra.program.model.scalar.Scalar;+import ghidra.program.model.symbol.*;++public class CreateRelocationBasedOperandReferences extends GhidraScript {++ @Override+ public void run() throws Exception {+ if (currentProgram == null) {+ popup("No active Program to analyze");+ return;+ }++ Listing listing = currentProgram.getListing();+ ReferenceManager refMgr = currentProgram.getReferenceManager();+ EquateTable equateTable = currentProgram.getEquateTable();+ SymbolTable symbolTable = currentProgram.getSymbolTable();++ RelocationTable relocationTable = currentProgram.getRelocationTable();+ if (relocationTable.getSize() == 0) {+ popup("Program does not have relocations");+ return;+ }++ Iterator<Relocation> relocations = relocationTable.getRelocations();++ monitor.initialize(relocationTable.getSize());+ int progress = 0;+ int refCount = 0;++ while (relocations.hasNext()) {+ monitor.checkCanceled();+ Relocation r = relocations.next();+ monitor.setProgress(progress++);++ Instruction instr = listing.getInstructionAt(r.getAddress());+ if (instr == null) {+ continue;+ }++ Equate equate = null;+ Symbol symbol = null;+ long value;++ List<Symbol> symbols = symbolTable.getLabelOrFunctionSymbols(r.getSymbolName(), null);+ if (symbols.size() == 0) {+ // check for possible equate definition+ equate = equateTable.getEquate(r.getSymbolName());+ if (equate == null) {+ continue;+ }+ value = equate.getValue();+ }+ else if (symbols.size() == 1) {+ symbol = symbols.get(0);+ Address a = symbol.getAddress();+ if (a.getAddressSpace().getAddressableUnitSize() != 1) {+ continue;+ }+ value = a.getOffset();+ }+ else {+ continue;+ }++ Reference[] referencesFrom = null;++ int opCnt = instr.getNumOperands();+ for (int opIndex = 0; opIndex < opCnt; opIndex++) {+ Scalar scalar =+ getScalarOperand(instr.getDefaultOperandRepresentationList(opIndex));+ if (scalar == null || scalar.getUnsignedValue() != value) {+ continue;+ }+ if (referencesFrom == null) {+ referencesFrom = refMgr.getReferencesFrom(instr.getAddress());+ }+ if (hasReference(referencesFrom, opIndex)) {+ continue; // reference exists on operand+ }+ if (equateTable.getEquates(instr.getAddress(), opIndex).size() != 0) {+ continue; // equate reference exists on operand+ }+ if (symbol != null) {+ Reference ref = refMgr.addMemoryReference(instr.getAddress(),+ symbol.getAddress(), RefType.DATA, SourceType.ANALYSIS, opIndex);+ refMgr.setAssociation(symbol, ref);+ }+ else {+ equate.addReference(instr.getAddress(), opIndex);+ }+ ++refCount;+ break;+ }++ }++ popup("Added " + refCount + " relocation-based references");+ }++ private Scalar getScalarOperand(List<Object> defaultOperandRepresentationList) {+ Scalar s = null;+ for (Object obj : defaultOperandRepresentationList) {+ if (obj instanceof String) {+ continue;+ }+ if (obj instanceof Character) {+ continue;+ }+ if (obj instanceof Scalar) {+ if (s != null) {+ return null;+ }+ s = (Scalar) obj;+ }+ else {+ return null;+ }+ }+ return s;+ }++ private boolean hasReference(Reference[] referencesFrom, int opIndex) {+ for (Reference r : referencesFrom) {+ if (r.getOperandIndex() == opIndex) {+ return true;+ }+ }+ return false;+ }++}diff --git a/Ghidra/Framework/SoftwareModeling/data/languages/processor_spec.rxg b/Ghidra/Framework/SoftwareModeling/data/languages/processor_spec.rxgindex d0c45fb169..beac886305 100644--- a/Ghidra/Framework/SoftwareModeling/data/languages/processor_spec.rxg+++ b/Ghidra/Framework/SoftwareModeling/data/languages/processor_spec.rxg@@ -99,6 +99,9 @@<optional><attribute name="rename"/></optional>+ <optional>+ <attribute name="alias"/>+ </optional><optional><attribute name="group"/></optional>diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/processors/sleigh/SleighLanguage.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/processors/sleigh/SleighLanguage.javaindex e86985a056..a049e5a600 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/processors/sleigh/SleighLanguage.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/plugin/processors/sleigh/SleighLanguage.java@@ -768,6 +768,7 @@ public class SleighLanguage implements Language {XmlElement reg = parser.start();String registerName = reg.getAttribute("name");String registerRename = reg.getAttribute("rename");+ String registerAlias = reg.getAttribute("alias");String groupName = reg.getAttribute("group");boolean isHidden = SpecXmlUtils.decodeBoolean(reg.getAttribute("hidden"));if (registerRename != null) {@@ -784,6 +785,9 @@ public class SleighLanguage implements Language {Msg.error(this, "duplicate register " + registerName + ": " +description.getSpecFile());}+ if (registerAlias != null) {+ registerBuilder.addAlias(registerName, registerAlias);+ }if (groupName != null) {registerBuilder.setGroup(registerName, groupName);}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/RegisterBuilder.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/RegisterBuilder.javaindex 84ea905d69..eca3c69911 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/RegisterBuilder.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/RegisterBuilder.java@@ -187,6 +187,25 @@ public class RegisterBuilder {return true;}+ /**+ * Add an alias to a previously defined register.+ * @param registerName defined register+ * @param alias alias to be added to defined register+ * @return true if alias addition was successful, else false+ */+ public boolean addAlias(String registerName, String alias) {+ Register register = registerMap.get(registerName);+ if (register == null) {+ return false;+ }+ if (registerMap.containsKey(alias)) {+ return false;+ }+ register.addAlias(alias);+ addRegisterToNameMap(alias, register);+ return true;+ }+/*** Set the group name for the specified register* @param registerName register namediff --git a/Ghidra/Processors/PIC/data/languages/PIC24.pspec b/Ghidra/Processors/PIC/data/languages/PIC24.pspecindex e08697bfc1..0357e6bb6a 100644--- a/Ghidra/Processors/PIC/data/languages/PIC24.pspec+++ b/Ghidra/Processors/PIC/data/languages/PIC24.pspec@@ -8,4 +8,22 @@</properties><programcounter register="PC"/><data_space space="ram"/>+ <register_data>+ <register name="W0" alias="WREG0"/>+ <register name="W1" alias="WREG1"/>+ <register name="W2" alias="WREG2"/>+ <register name="W3" alias="WREG3"/>+ <register name="W4" alias="WREG4"/>+ <register name="W5" alias="WREG5"/>+ <register name="W6" alias="WREG6"/>+ <register name="W7" alias="WREG7"/>+ <register name="W8" alias="WREG8"/>+ <register name="W9" alias="WREG9"/>+ <register name="W10" alias="WREG10"/>+ <register name="W11" alias="WREG11"/>+ <register name="W12" alias="WREG12"/>+ <register name="W13" alias="WREG13"/>+ <register name="W14" alias="WREG14"/>+ <register name="W15" alias="WREG15"/>+ </register_data></processor_spec>