From 2766f292277dbc19cf13bee8dd7ffadcf2ccfcec Mon Sep 17 00:00:00 2001 From: Vladimir Kononovich Date: Sun, 14 Apr 2019 20:34:40 +0300 Subject: [PATCH] Fixed signatures loading and applying. --- .antProperties.xml | 2 +- .classpath | 330 +++++++++++++++-------------- .gitignore | 6 +- .project | 2 +- bin/README.txt | 1 - bin/help/TOC_Source.xml | 57 ----- bin/help/shared/Frontpage.css | 58 ----- bin/help/topics/psx/help.html | 23 -- bin/images/README.txt | 2 - extension.properties | 2 +- ghidra_scripts/README.txt | 1 - lib/README.txt | 3 - os/linux64/README.txt | 3 - os/osx64/README.txt | 3 - os/win64/README.txt | 3 - src/main/java/pat/PatParser.java | 42 ++-- src/main/java/psx/PsxAnalyzer.java | 19 +- src/main/java/psx/PsxLoader.java | 8 +- 18 files changed, 224 insertions(+), 341 deletions(-) delete mode 100644 bin/README.txt delete mode 100644 bin/help/TOC_Source.xml delete mode 100644 bin/help/shared/Frontpage.css delete mode 100644 bin/help/topics/psx/help.html delete mode 100644 bin/images/README.txt delete mode 100644 ghidra_scripts/README.txt delete mode 100644 lib/README.txt delete mode 100644 os/linux64/README.txt delete mode 100644 os/osx64/README.txt delete mode 100644 os/win64/README.txt diff --git a/.antProperties.xml b/.antProperties.xml index e4f63f8..3fc3499 100644 --- a/.antProperties.xml +++ b/.antProperties.xml @@ -1,5 +1,5 @@ - + diff --git a/.classpath b/.classpath index 824d14c..04e149d 100644 --- a/.classpath +++ b/.classpath @@ -5,404 +5,414 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index d1e319d..d2159a1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ /build/ /bin/ /dist/ -.antProperties.xml -.classpath -.project \ No newline at end of file +/\.antProperties.xml +/\.classpath +/\.project \ No newline at end of file diff --git a/.project b/.project index c9aa86a..d943df8 100644 --- a/.project +++ b/.project @@ -18,7 +18,7 @@ Ghidra 2 - D:/ghidra_9.0.1 + D:/ghidra_9.0.2 diff --git a/bin/README.txt b/bin/README.txt deleted file mode 100644 index 9e408f4..0000000 --- a/bin/README.txt +++ /dev/null @@ -1 +0,0 @@ -Java source directory to hold module-specific Ghidra scripts. diff --git a/bin/help/TOC_Source.xml b/bin/help/TOC_Source.xml deleted file mode 100644 index a34f62e..0000000 --- a/bin/help/TOC_Source.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - diff --git a/bin/help/shared/Frontpage.css b/bin/help/shared/Frontpage.css deleted file mode 100644 index 452bf6e..0000000 --- a/bin/help/shared/Frontpage.css +++ /dev/null @@ -1,58 +0,0 @@ -/* ### - * 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. - */ -/* - WARNING! - This file is copied to all help directories. If you change this file, you must copy it - to each src/main/help/help/shared directory. - - - Java Help Note: JavaHelp does not accept sizes (like in 'margin-top') in anything but - px (pixel) or with no type marking. - -*/ - -body { margin-bottom: 50px; margin-left: 10px; margin-right: 10px; margin-top: 10px; } /* some padding to improve readability */ -li { font-family:times new roman; font-size:14pt; } -h1 { color:#000080; font-family:times new roman; font-size:36pt; font-style:italic; font-weight:bold; text-align:center; } -h2 { margin: 10px; margin-top: 20px; color:#984c4c; font-family:times new roman; font-size:18pt; font-weight:bold; } -h3 { margin-left: 10px; margin-top: 20px; color:#0000ff; font-family:times new roman; font-size:14pt; font-weight:bold; } -h4 { margin-left: 10px; margin-top: 20px; font-family:times new roman; font-size:14pt; font-style:italic; } - -/* - P tag code. Most of the help files nest P tags inside of blockquote tags (the was the - way it had been done in the beginning). The net effect is that the text is indented. In - modern HTML we would use CSS to do this. We need to support the Ghidra P tags, nested in - blockquote tags, as well as naked P tags. The following two lines accomplish this. Note - that the 'blockquote p' definition will inherit from the first 'p' definition. -*/ -p { margin-left: 40px; font-family:times new roman; font-size:14pt; } -blockquote p { margin-left: 10px; } - -p.providedbyplugin { color:#7f7f7f; margin-left: 10px; font-size:14pt; margin-top:100px } -p.ProvidedByPlugin { color:#7f7f7f; margin-left: 10px; font-size:14pt; margin-top:100px } -p.relatedtopic { color:#800080; margin-left: 10px; font-size:14pt; } -p.RelatedTopic { color:#800080; margin-left: 10px; font-size:14pt; } - -/* - We wish for a tables to have space between it and the preceding element, so that text - is not too close to the top of the table. Also, nest the table a bit so that it is clear - the table relates to the preceding text. -*/ -table { margin-left: 20px; margin-top: 10px; width: 80%;} -td { font-family:times new roman; font-size:14pt; vertical-align: top; } -th { font-family:times new roman; font-size:14pt; font-weight:bold; background-color: #EDF3FE; } - -code { color: black; font-family: courier new; font-size: 14pt; } diff --git a/bin/help/topics/psx/help.html b/bin/help/topics/psx/help.html deleted file mode 100644 index 8f858d2..0000000 --- a/bin/help/topics/psx/help.html +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - Skeleton Help File for a Module - - - - -

Skeleton Help File for a Module

- -

This is a simple skeleton help topic. For a better description of what should and should not - go in here, see the "sample" Ghidra extension in the Extensions/Ghidra directory, or see your - favorite help topic. In general, language modules do not have their own help topics.

- - diff --git a/bin/images/README.txt b/bin/images/README.txt deleted file mode 100644 index f20ae77..0000000 --- a/bin/images/README.txt +++ /dev/null @@ -1,2 +0,0 @@ -The "src/resources/images" directory is intended to hold all image/icon files used by -this module. diff --git a/extension.properties b/extension.properties index 7b1ac42..34551f4 100644 --- a/extension.properties +++ b/extension.properties @@ -1,5 +1,5 @@ name=@extname@ description=Loader for PSX executables author=DrMefistO -createdOn=30.03.2019 +createdOn= version=@extversion@ diff --git a/ghidra_scripts/README.txt b/ghidra_scripts/README.txt deleted file mode 100644 index 9e408f4..0000000 --- a/ghidra_scripts/README.txt +++ /dev/null @@ -1 +0,0 @@ -Java source directory to hold module-specific Ghidra scripts. diff --git a/lib/README.txt b/lib/README.txt deleted file mode 100644 index 224d94f..0000000 --- a/lib/README.txt +++ /dev/null @@ -1,3 +0,0 @@ -The "lib" directory is intended to hold Jar files which this module -is dependent upon. This directory may be eliminated from a specific -module if no other Jar files are needed. diff --git a/os/linux64/README.txt b/os/linux64/README.txt deleted file mode 100644 index 965333c..0000000 --- a/os/linux64/README.txt +++ /dev/null @@ -1,3 +0,0 @@ -The "os/linux64" directory is intended to hold Linux native binaries -which this module is dependent upon. This directory may be eliminated for a specific -module if native binaries are not provided for the corresponding platform. diff --git a/os/osx64/README.txt b/os/osx64/README.txt deleted file mode 100644 index 824fe27..0000000 --- a/os/osx64/README.txt +++ /dev/null @@ -1,3 +0,0 @@ -The "os/osx64" directory is intended to hold macOS (OS X) native binaries -which this module is dependent upon. This directory may be eliminated for a specific -module if native binaries are not provided for the corresponding platform. diff --git a/os/win64/README.txt b/os/win64/README.txt deleted file mode 100644 index 924adf9..0000000 --- a/os/win64/README.txt +++ /dev/null @@ -1,3 +0,0 @@ -The "os/win64" directory is intended to hold MS Windows native binaries (.exe) -which this module is dependent upon. This directory may be eliminated for a specific -module if native binaries are not provided for the corresponding platform. diff --git a/src/main/java/pat/PatParser.java b/src/main/java/pat/PatParser.java index 80b3253..e97b1c6 100644 --- a/src/main/java/pat/PatParser.java +++ b/src/main/java/pat/PatParser.java @@ -26,8 +26,8 @@ import ghidra.util.task.TaskMonitor; public class PatParser { - private static final Pattern modulePat = Pattern.compile("([:\\^][0-9A-F]{4}@?) (\\w+) "); - private static final Pattern linePat = Pattern.compile("^((?:[0-9A-F\\.]{2})+) ([0-9A-F]{2}) ([0-9A-F]{4}) ([0-9A-F]{4}) ((?:[:\\^][0-9A-F]{4}@? \\w+ )+)((?:[0-9A-F\\.]{2})+)?$"); + private static final Pattern modulePat = Pattern.compile("([:\\^][0-9A-F]{4}@?) ([\\.\\w]+) "); + private static final Pattern linePat = Pattern.compile("^((?:[0-9A-F\\.]{2})+) ([0-9A-F]{2}) ([0-9A-F]{4}) ([0-9A-F]{4}) ((?:[:\\^][0-9A-F]{4}@? [\\.\\w]+ )+)((?:[0-9A-F\\.]{2})+)?$"); private List signatures = null; private final TaskMonitor monitor; @@ -72,9 +72,7 @@ public void applySignatures(ByteProvider provider, Program program, Address imag MaskedBytes fullBytes = sig.getFullBytes(); MaskedBytes tmpl = sig.getTemplateBytes(); - long progress = monitor.getProgress(); - Address addr = program.getMemory().findBytes(startAddr, endAddr, fullBytes.getBytes(), fullBytes.getMasks(), true, monitor); - monitor.setProgress(progress); + Address addr = program.getMemory().findBytes(startAddr, endAddr, fullBytes.getBytes(), fullBytes.getMasks(), true, TaskMonitor.DUMMY); if (addr == null) { monitor.incrementProgress(sig.getModules().size()); @@ -96,11 +94,11 @@ public void applySignatures(ByteProvider provider, Program program, Address imag for (ModuleData data : modules) { Address _addr = addr.add(data.getOffset()); - if (data.getType().isGlobal() || data.getType().isLocal()) { + if (data.getType().isGlobal()) { setFunction(program, fpa, _addr, data.getName(), data.getType().isGlobal(), false, log); } else if (!skipRefs && data.getType().isReference()) { - setInstrRefName(program, fpa, ps, _addr, data.getName(), log, monitor); + setInstrRefName(program, fpa, ps, _addr, data.getName(), log); } if (!(skipRefs && data.getType().isReference())) { @@ -180,13 +178,13 @@ public long getAllModulesCount() { return modulesCount; } - public static void setInstrRefName(Program program, FlatProgramAPI fpa, PseudoDisassembler ps, Address address, String name, MessageLog log, TaskMonitor monitor) { + public static void setInstrRefName(Program program, FlatProgramAPI fpa, PseudoDisassembler ps, Address address, String name, MessageLog log) { ReferenceManager refsMgr = program.getReferenceManager(); Reference[] refs = refsMgr.getReferencesFrom(address); if (refs.length == 0) { - disasmInstruction(program, address, monitor); + disasmInstruction(program, address); refs = refsMgr.getReferencesFrom(address); if (refs.length == 0) { @@ -196,6 +194,16 @@ public static void setInstrRefName(Program program, FlatProgramAPI fpa, PseudoDi refs = refsMgr.getFlowReferencesFrom(address.add(4)); Instruction instr = program.getListing().getInstructionAt(address.add(4)); + + if (instr == null) { + disasmInstruction(program, address.add(4)); + instr = program.getListing().getInstructionAt(address.add(4)); + + if (instr == null) { + return; + } + } + FlowType flowType = instr.getFlowType(); if (refs.length == 0 && !(flowType.isJump() || flowType.isCall() || flowType.isTerminal())) { @@ -218,21 +226,27 @@ public static void setInstrRefName(Program program, FlatProgramAPI fpa, PseudoDi } } - private static void disasmInstruction(Program program, Address address, TaskMonitor monitor) { - long progress = monitor.getProgress(); - DisassembleCommand cmd = new DisassembleCommand(address, null, false); - cmd.applyTo(program, monitor); - monitor.setProgress(progress); + private static void disasmInstruction(Program program, Address address) { + DisassembleCommand cmd = new DisassembleCommand(address, null, true); + cmd.applyTo(program, TaskMonitor.DUMMY); } public static void setFunction(Program program, FlatProgramAPI fpa, Address address, String name, boolean isFunction, boolean isEntryPoint, MessageLog log) { try { + if (fpa.getInstructionAt(address) == null) + disasmInstruction(program, address); + if (isFunction) { fpa.createFunction(address, name); } if (isEntryPoint) { fpa.addEntryPoint(address); } + + if (isFunction && program.getSymbolTable().hasSymbol(address)) { + return; + } + program.getSymbolTable().createLabel(address, name, SourceType.IMPORTED); } catch (InvalidInputException e) { log.appendException(e); diff --git a/src/main/java/psx/PsxAnalyzer.java b/src/main/java/psx/PsxAnalyzer.java index 1adea51..365ca83 100644 --- a/src/main/java/psx/PsxAnalyzer.java +++ b/src/main/java/psx/PsxAnalyzer.java @@ -21,17 +21,28 @@ import ghidra.util.task.TaskMonitor; import pat.PatParser; -/** - * TODO: Provide class-level documentation that describes what this analyzer does. - */ + public class PsxAnalyzer extends AbstractAnalyzer { private static final String OPTION_NAME = "PSYQ PAT-File Path"; private File file = null; + public static boolean isPsxLoader(Program program) { + return program.getExecutableFormat().equalsIgnoreCase(PsxLoader.PSX_LOADER); + } + public PsxAnalyzer() { super("PSYQ Signatures", "PSX signatures applier", AnalyzerType.INSTRUCTION_ANALYZER); - setDefaultEnablement(false); + } + + @Override + public boolean getDefaultEnablement(Program program) { + return isPsxLoader(program); + } + + @Override + public boolean canAnalyze(Program program) { + return isPsxLoader(program); } @Override diff --git a/src/main/java/psx/PsxLoader.java b/src/main/java/psx/PsxLoader.java index 16cacd2..c966b82 100644 --- a/src/main/java/psx/PsxLoader.java +++ b/src/main/java/psx/PsxLoader.java @@ -65,6 +65,8 @@ public class PsxLoader extends AbstractLibrarySupportLoader { 0x00, 0x00, 0x00, 0x00, 0x4D, 0x00, 0x00, 0x00 }; + public static final String PSX_LOADER = "PSX Executables Loader"; + private static final byte MAIN_SIGN_MASK[] = new byte[] { 0x00, 0x00, 0x00, (byte)0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, @@ -75,7 +77,7 @@ public class PsxLoader extends AbstractLibrarySupportLoader { @Override public String getName() { - return "PSX Executables Loader"; + return PSX_LOADER; } @Override @@ -131,10 +133,10 @@ protected void load(ByteProvider provider, LoadSpec loadSpec, List