Ghidra is a software reverse engineering (SRE) framework
Merge remote-tracking branch 'origin/Ghidra_11.4'
ghidra1f981f43fad48
diff --cc Ghidra/Features/Base/ghidra_scripts/AutoRenameSimpleLabels.javaindex 24970531e2,44bb72c352..376453b6a4--- a/Ghidra/Features/Base/ghidra_scripts/AutoRenameSimpleLabels.java+++ b/Ghidra/Features/Base/ghidra_scripts/AutoRenameSimpleLabels.java@@@ -49,7 -49,7 +49,6 @@@ public class AutoRenameSimpleLabels ext@Overridepublic void run() throws Exception {-- String tmpString = "\nScript: AutoRenameSimpleLabels() \n";//get listing of symbolsSymbolIterator iter = currentProgram.getSymbolTable().getAllSymbols(true);@@@ -147,15 -147,15 +146,13 @@@// now also propogate the repeatable comment up as well- String comment = currentProgram.getListing().getComment(CommentType.REPEATABLE,- operand_addr);- if (comment != null) {- if (currentProgram.getListing().getComment(CommentType.REPEATABLE,- startAddr) == null) {- //println("updating comment for " + operand +" is " + comment);- currentProgram.getListing()- .setComment(startAddr, CommentType.REPEATABLE, comment);- }+ String comment =+ currentProgram.getListing().getComment(CommentType.REPEATABLE, operand_addr);- if (comment != null) {- if (currentProgram.getListing()- .getComment(CommentType.REPEATABLE, startAddr) == null) {- //println("updating comment for " + operand +" is " + comment);- currentProgram.getListing()- .setComment(startAddr, CommentType.REPEATABLE, comment);- }++ if (comment != null && currentProgram.getListing()++ .getComment(CommentType.REPEATABLE, startAddr) == null) {++ //println("updating comment for " + operand +" is " + comment);++ currentProgram.getListing()++ .setComment(startAddr, CommentType.REPEATABLE, comment);}}}diff --cc Ghidra/Features/Base/ghidra_scripts/DeleteDeadDefaultPlatesScript.javaindex bf8a46eccc,563c271604..cc5fe05f97--- a/Ghidra/Features/Base/ghidra_scripts/DeleteDeadDefaultPlatesScript.java+++ b/Ghidra/Features/Base/ghidra_scripts/DeleteDeadDefaultPlatesScript.java@@@ -24,17 -24,18 +24,16 @@@ import ghidra.program.model.listing.*public class DeleteDeadDefaultPlatesScript extends GhidraScript {private static String DEAD_PLATE = " DEAD";- /* (non-Javadoc)- * @see ghidra.app.script.GhidraScript#run()- */+- /* (non-Javadoc)- * @see ghidra.app.script.GhidraScript#run()- */@Override- public void run() throws Exception {+ public void run() throws Exception {Listing listing = currentProgram.getListing();AddressSetView set = currentProgram.getMemory();if (currentSelection != null && !currentSelection.isEmpty()) {set = currentSelection;}- int updateCount=0;+++ int updateCount = 0;AddressIterator iter = listing.getCommentAddressIterator(CommentType.PLATE, set, true);while (iter.hasNext()) {Address addr = iter.next();diff --cc Ghidra/Features/Base/ghidra_scripts/DeleteEmptyPlateCommentsScript.javaindex 9906d3f23b,3818904a42..2767069f4c--- a/Ghidra/Features/Base/ghidra_scripts/DeleteEmptyPlateCommentsScript.java+++ b/Ghidra/Features/Base/ghidra_scripts/DeleteEmptyPlateCommentsScript.java@@@ -24,17 -24,18 +24,16 @@@ import ghidra.program.model.listing.*public class DeleteEmptyPlateCommentsScript extends GhidraScript {private static String EMPTY_PLATE = "";- /* (non-Javadoc)- * @see ghidra.app.script.GhidraScript#run()- */+- /* (non-Javadoc)- * @see ghidra.app.script.GhidraScript#run()- */@Override- public void run() throws Exception {+ public void run() throws Exception {Listing listing = currentProgram.getListing();AddressSetView set = currentProgram.getMemory();if (currentSelection != null && !currentSelection.isEmpty()) {set = currentSelection;}- int updateCount=0;+++ int updateCount = 0;AddressIterator iter = listing.getCommentAddressIterator(CommentType.PLATE, set, true);while (iter.hasNext()) {Address addr = iter.next();diff --cc Ghidra/Features/Base/ghidra_scripts/DeleteExitCommentsScript.javaindex 10d61e2a72,0e62b63d6e..c4ffd412ed--- a/Ghidra/Features/Base/ghidra_scripts/DeleteExitCommentsScript.java+++ b/Ghidra/Features/Base/ghidra_scripts/DeleteExitCommentsScript.java@@@ -24,17 -24,18 +24,16 @@@ import ghidra.program.model.listing.*public class DeleteExitCommentsScript extends GhidraScript {private static String EXIT_COMMENT = " Exit **********\r\n ";- /* (non-Javadoc)- * @see ghidra.app.script.GhidraScript#run()- */+- /* (non-Javadoc)- * @see ghidra.app.script.GhidraScript#run()- */@Override- public void run() throws Exception {+ public void run() throws Exception {Listing listing = currentProgram.getListing();AddressSetView set = currentProgram.getMemory();if (currentSelection != null && !currentSelection.isEmpty()) {set = currentSelection;}- int updateCount=0;+++ int updateCount = 0;AddressIterator iter = listing.getCommentAddressIterator(CommentType.POST, set, true);while (iter.hasNext()) {Address addr = iter.next();diff --cc Ghidra/Features/Base/ghidra_scripts/RegisterTouchesPerFunction.javaindex 7fa677657c,2f1939d8b1..3b6f57bebe--- a/Ghidra/Features/Base/ghidra_scripts/RegisterTouchesPerFunction.java+++ b/Ghidra/Features/Base/ghidra_scripts/RegisterTouchesPerFunction.java@@@ -25,161 -24,135 +24,134 @@@ import ghidra.app.script.GhidraScriptimport ghidra.program.model.lang.Register;import ghidra.program.model.listing.*;- public class RegisterTouchesPerFunction extends GhidraScript- {- private final static String DIVIDER = "*************************************************************\r\n";-- @Override- public void run() throws Exception- {- Listing l = this.currentProgram.getListing();-- if (this.askYesNo("Function Analysis - Register Touches",- "Analyze complete listing?"))- {- FunctionIterator fi = l.getFunctions(true);- while (fi.hasNext() && !monitor.isCancelled())- {- doAnalysis(l, fi.next());- }- }- else- {- doAnalysis(l, l.getFunctionContaining(this.currentAddress));- }- }-- private void doAnalysis(Listing list, Function func)- {- if (func == null) {- println("No function to analyze.");- return;- }- HashSet<String> affected, accessed;- Vector<String> restored;- Stack<String> pushPops;- boolean reviewRestored = false;- Instruction inst;- InstructionIterator iIter;-- monitor.setMessage("Analyzing registers in " + func.getName());-- String comment = list.getComment(CommentType.PLATE, func.getBody().getMinAddress());-- if (comment != null && comment.indexOf("TOUCHED REGISTER SUMMARY") > -1)- return;-- pushPops = new Stack<String>();- affected = new HashSet<String>();- accessed = new HashSet<String>();- restored = new Vector<String>();-- iIter = list.getInstructions(func.getBody(), true);-- while (iIter.hasNext() && !monitor.isCancelled())- {- inst = iIter.next();-- Object o[] = inst.getResultObjects();- for (int i = 0; i < o.length; i++)- {- if (o[i] instanceof Register)- {- String name = ((Register) o[i]).getName();-- if (inst.getMnemonicString().equalsIgnoreCase("pop"))- {- if (!name.equalsIgnoreCase("mult_addr")- && !name.equalsIgnoreCase("sp"))- {- if (pushPops.size() > 0)- {- restored.add(pushPops.pop() + "->" + name);- }- else- {- reviewRestored = true;- }- }- }- else- {- affected.add(name);- }- }- }- o = inst.getInputObjects();-- for (int i = 0; i < o.length; i++)- {- if (o[i] instanceof Register)- {- String name = ((Register) o[i]).getName();- if (inst.getMnemonicString().equalsIgnoreCase("push"))- {- if (!name.equalsIgnoreCase("mult_addr")- && !name.equalsIgnoreCase("sp"))- {- pushPops.push(name);- }- }- else- {- accessed.add(name);- }- }- }- }-- StringBuffer buffer = new StringBuffer();- if (comment != null) {- buffer.append(comment);- buffer.append("\r\n");- buffer.append(DIVIDER);- }- buffer.append("TOUCHED REGISTER SUMMARY:\r\n");- buffer.append(DIVIDER);- buffer.append("Register(s) Affected:\r\n");- buffer.append(getString(affected, 8));- buffer.append(DIVIDER);- buffer.append("Register(s) Accessed:\r\n");- buffer.append(getString(accessed, 8));- buffer.append(DIVIDER);- buffer.append("Register(s) Restored:\r\n");- buffer.append(getString(restored, 4));-- if(reviewRestored)- {- buffer.append("##Review - due to branches this list may not be accurate\r\n");- println(func.getName() + " - Review - due to branches this list may not be accurate");- }- buffer.append(DIVIDER);-- if (pushPops.size() > 0)- {-- buffer.append("Registers Remaining on Stack:\r\n");- buffer.append(" "+getString(pushPops, 8));- }+ public class RegisterTouchesPerFunction extends GhidraScript {+++ private final static String DIVIDER =+ "*************************************************************\r\n";++ @Override+ public void run() throws Exception {+ Listing l = this.currentProgram.getListing();++ if (this.askYesNo("Function Analysis - Register Touches", "Analyze complete listing?")) {+ FunctionIterator fi = l.getFunctions(true);+ while (fi.hasNext() && !monitor.isCancelled()) {+ doAnalysis(l, fi.next());+ }+ }+ else {+ doAnalysis(l, l.getFunctionContaining(this.currentAddress));+ }+ }++ private void doAnalysis(Listing list, Function func) {+ if (func == null) {+ println("No function to analyze.");+ return;+ }+ HashSet<String> affected, accessed;+ Vector<String> restored;+ Stack<String> pushPops;+ boolean reviewRestored = false;+ Instruction inst;+ InstructionIterator iIter;++ monitor.setMessage("Analyzing registers in " + func.getName());++ String comment = list.getComment(CommentType.PLATE, func.getBody().getMinAddress());-+ if (comment != null && comment.indexOf("TOUCHED REGISTER SUMMARY") > -1)+ return;++ pushPops = new Stack<String>();+ affected = new HashSet<String>();+ accessed = new HashSet<String>();+ restored = new Vector<String>();++ iIter = list.getInstructions(func.getBody(), true);++ while (iIter.hasNext() && !monitor.isCancelled()) {+ inst = iIter.next();++ Object o[] = inst.getResultObjects();+ for (Object element : o) {+ if (element instanceof Register) {+ String name = ((Register) element).getName();++ if (inst.getMnemonicString().equalsIgnoreCase("pop")) {+ if (!name.equalsIgnoreCase("mult_addr") && !name.equalsIgnoreCase("sp")) {+ if (pushPops.size() > 0) {+ restored.add(pushPops.pop() + "->" + name);+ }+ else {+ reviewRestored = true;+ }+ }+ }+ else {+ affected.add(name);+ }+ }+ }+ o = inst.getInputObjects();++ for (Object element : o) {+ if (element instanceof Register) {+ String name = ((Register) element).getName();+ if (inst.getMnemonicString().equalsIgnoreCase("push")) {+ if (!name.equalsIgnoreCase("mult_addr") && !name.equalsIgnoreCase("sp")) {+ pushPops.push(name);+ }+ }+ else {+ accessed.add(name);+ }+ }+ }+ }++ StringBuffer buffer = new StringBuffer();+ if (comment != null) {+ buffer.append(comment);+ buffer.append("\r\n");+ buffer.append(DIVIDER);+ }+ buffer.append("TOUCHED REGISTER SUMMARY:\r\n");+ buffer.append(DIVIDER);+ buffer.append("Register(s) Affected:\r\n");+ buffer.append(getString(affected, 8));+ buffer.append(DIVIDER);+ buffer.append("Register(s) Accessed:\r\n");+ buffer.append(getString(accessed, 8));+ buffer.append(DIVIDER);+ buffer.append("Register(s) Restored:\r\n");+ buffer.append(getString(restored, 4));++ if (reviewRestored) {+ buffer.append("##Review - due to branches this list may not be accurate\r\n");+ println(func.getName() + " - Review - due to branches this list may not be accurate");+ }+ buffer.append(DIVIDER);++ if (pushPops.size() > 0) {-+ buffer.append("Registers Remaining on Stack:\r\n");+ buffer.append(" " + getString(pushPops, 8));+ }list.setComment(func.getEntryPoint(), CommentType.PLATE, buffer.toString());- }-- private String getString(Collection<String> c, int itemsPerLine)- {- TreeSet<Object> ts = new TreeSet<Object>(c);- String temp = ts.toString();- temp = temp.substring(1, temp.length() - 1);- int i = 0;- int commaCount = 0;- while ((i = temp.indexOf(',', i + 1)) >= 0)- {- commaCount++;- if (commaCount % itemsPerLine == 0)- temp = temp.substring(0, i + 1) + "\r\n"- + temp.substring(i + 1).trim();- }-- return temp + "\r\n";- }+ }++ private String getString(Collection<String> c, int itemsPerLine) {+ TreeSet<Object> ts = new TreeSet<Object>(c);+ String temp = ts.toString();+ temp = temp.substring(1, temp.length() - 1);+ int i = 0;+ int commaCount = 0;+ while ((i = temp.indexOf(',', i + 1)) >= 0) {+ commaCount++;+ if (commaCount % itemsPerLine == 0)+ temp = temp.substring(0, i + 1) + "\r\n" + temp.substring(i + 1).trim();+ }++ return temp + "\r\n";+ }}diff --cc Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentCmd.javaindex cb9c2eeef7,77d83d32ab..ee87a17e44--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentCmd.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/comments/SetCommentCmd.java@@@ -36,14 -36,10 +36,11 @@@ public class SetCommentCmd implements C* @param addr address of code unit where comment will be placed* @param commentType valid comment type (see CodeUnit)* @param comment comment for code unit+ * @deprecated Use {@link #SetCommentCmd(Address, CommentType, String)} instead*/- @Deprecated+ @Deprecated(forRemoval = true, since = "11.4")public SetCommentCmd(Address addr, int commentType, String comment) {- this.address = addr;- this.commentType = CommentType.values()[commentType];- this.comment = comment;- cmdName = comment == null ? "Delete Comment" : "Set Comment";+ this(addr, CommentType.valueOf(commentType), comment);}/**@@@ -127,30 -123,9 +124,28 @@@* @param program the program being analyzed* @param addr the address where data is created* @param comment the comment about the data- * @param commentType the type of comment+ * @param commentType the type of comment ({@link CodeUnit#PLATE_COMMENT},+ * {@link CodeUnit#PRE_COMMENT}, {@link CodeUnit#EOL_COMMENT}, {@link CodeUnit#POST_COMMENT},+ * {@link CodeUnit#REPEATABLE_COMMENT})+ * @deprecated Use {@link #createComment(Program, Address, String, CommentType)} instead*/- @Deprecated++ @Deprecated(forRemoval = true, since = "11.4")+ public static void createComment(Program program, Address addr, String comment,+ int commentType) {+ SetCommentCmd commentCmd =- new SetCommentCmd(addr, CommentType.values()[commentType], comment);++ new SetCommentCmd(addr, CommentType.valueOf(commentType), comment);+ commentCmd.applyTo(program);+ }+ /**+ * Creates the specified comment of the specified type at address. The current comment of+ * this commentType will be cleared.+ *+ * @param program the program being analyzed+ * @param addr the address where data is created+ * @param comment the comment about the data- * @param commentType the type of comment ({@link CommentType#PLATE},- * {@link CodeUnit#PRE_COMMENT}, {@link CodeUnit#EOL_COMMENT}, {@link CodeUnit#POST_COMMENT},- * {@link CodeUnit#REPEATABLE_COMMENT})++ * @param commentType the type of comment+ */public static void createComment(Program program, Address addr, String comment,CommentType commentType) {SetCommentCmd commentCmd = new SetCommentCmd(addr, commentType, comment);diff --cc Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.javaindex 1e701b4432,981e27a77e..13fda9b7cd--- a/Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.java@@@ -933,15 -933,18 +933,15 @@@ public class ProgramBuilder});}+ @Deprecated(forRemoval = true, since = "11.4")public void createComment(String address, String comment, int commentType) {- CommentType type = CommentType.values()[commentType];- createComment(address, comment, type);- tx(() -> {- Listing listing = program.getListing();- listing.setComment(addr(address), commentType, comment);- });++ createComment(address, comment, CommentType.valueOf(commentType));}- public void createComment(String address, String comment, CommentType type) {+ public void createComment(String address, String comment, CommentType commentType) {tx(() -> {Listing listing = program.getListing();- listing.setComment(addr(address), type, comment);+ listing.setComment(addr(address), commentType, comment);});}diff --cc Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CommentsDBAdapterV0.javaindex e29f37dd5a,053d1c7b3a..f6d031e948--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CommentsDBAdapterV0.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CommentsDBAdapterV0.java@@@ -75,49 -66,30 +66,31 @@@ class CommentsDBAdapterV0 extends Comme}}- /**- * @see ghidra.program.database.code.CommentsDBAdapter#getRecord(long)- */@Overridepublic DBRecord getRecord(long addr) throws IOException {- return adaptRecord(commentTable.getRecord(addr));+ return v0ConvertRecord(commentTable.getRecord(addr));}- /**- * @see ghidra.program.database.code.CommentsDBAdapter#createRecord(long, int, java.lang.String)- */@Overridepublic DBRecord createRecord(long addr, int commentCol, String comment) throws IOException {-- return null;++ throw new UnsupportedOperationException();}- /**- * @see ghidra.program.database.code.CommentsDBAdapter#deleteRecord(long)- */@Overridepublic boolean deleteRecord(long addr) throws IOException {-- return false;++ throw new UnsupportedOperationException();}- /**- * @see ghidra.program.database.code.CommentsDBAdapter#deleteRecords(long, long)- */@Overridepublic boolean deleteRecords(Address start, Address end) throws IOException {-- return false;++ throw new UnsupportedOperationException();}- /**- * @see ghidra.program.database.code.CommentsDBAdapter#updateRecord(ghidra.framework.store.db.DBRecord)- */@Overridepublic void updateRecord(DBRecord commentRec) throws IOException {++ throw new UnsupportedOperationException();}- /**- *- * @see ghidra.program.database.code.CommentsDBAdapter#getKeys(long, long, boolean)- */@Overridepublic AddressKeyIterator getKeys(Address start, Address end, boolean atStart)throws IOException {@@@ -171,16 -131,10 +132,11 @@@return new RecordIteratorAdapter(new AddressKeyRecordIterator(commentTable, addrMap));}- /**- * @see ghidra.program.database.code.CommentsDBAdapter#putRecord(db.DBRecord)- */@Overridepublic void putRecord(DBRecord record) throws IOException {++ throw new UnsupportedOperationException();}- /**- * @see ghidra.program.database.code.CommentsDBAdapter#getRecordCount()- */@Overridepublic int getRecordCount() throws IOException {return commentTable.getRecordCount();@@@ -235,17 -186,11 +188,11 @@@this.it = it;}- /**- * @see ghidra.framework.store.db.RecordIterator#delete()- */@Overridepublic boolean delete() throws IOException {-- return false;++ throw new UnsupportedOperationException();}- /**- * @see ghidra.framework.store.db.RecordIterator#hasNext()- */@Overridepublic boolean hasNext() throws IOException {return it.hasNext();diff --cc Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/oldfunction/OldFunctionDataDB.javaindex 6104959ca7,6effee44de..347c985f48--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/oldfunction/OldFunctionDataDB.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/oldfunction/OldFunctionDataDB.java@@@ -16,7 -16,7 +16,8 @@@package ghidra.program.database.oldfunction;import java.io.IOException;--import java.util.*;++import java.util.ArrayList;++import java.util.List;import db.DBRecord;import db.Field;