├── .gitignore ├── CMakeLists.txt ├── Ghidra ├── .gitignore ├── Doxyfile ├── Ghidra.vcxproj ├── Ghidra.vcxproj.filters ├── Makefile ├── action.cc ├── action.hh ├── address.cc ├── address.hh ├── architecture.cc ├── architecture.hh ├── bfd_arch.cc ├── bfd_arch.hh ├── block.cc ├── block.hh ├── blockaction.cc ├── blockaction.hh ├── callgraph.cc ├── callgraph.hh ├── capability.cc ├── capability.hh ├── cast.cc ├── cast.hh ├── codedata.cc ├── codedata.hh ├── comment.cc ├── comment.hh ├── comment_ghidra.cc ├── comment_ghidra.hh ├── condexe.cc ├── condexe.hh ├── consolemain.cc ├── context.cc ├── context.hh ├── coreaction.cc ├── coreaction.hh ├── cover.cc ├── cover.hh ├── cpool.cc ├── cpool.hh ├── cpool_ghidra.cc ├── cpool_ghidra.hh ├── crc32.cc ├── crc32.hh ├── database.cc ├── database.hh ├── database_ghidra.cc ├── database_ghidra.hh ├── doccore.hh ├── docmain.hh ├── double.cc ├── double.hh ├── dynamic.cc ├── dynamic.hh ├── emulate.cc ├── emulate.hh ├── emulateutil.cc ├── emulateutil.hh ├── error.hh ├── filemanage.cc ├── filemanage.hh ├── float.cc ├── float.hh ├── flow.cc ├── flow.hh ├── fspec.cc ├── fspec.hh ├── funcdata.cc ├── funcdata.hh ├── funcdata_block.cc ├── funcdata_op.cc ├── funcdata_varnode.cc ├── ghidra_arch.cc ├── ghidra_arch.hh ├── ghidra_context.cc ├── ghidra_context.hh ├── ghidra_process.cc ├── ghidra_process.hh ├── ghidra_translate.cc ├── ghidra_translate.hh ├── globalcontext.cc ├── globalcontext.hh ├── grammar.cc ├── grammar.hh ├── grammar.tab.cpp ├── grammar.tab.h ├── grammar.y ├── graph.cc ├── graph.hh ├── heritage.cc ├── heritage.hh ├── ifacedecomp.cc ├── ifacedecomp.hh ├── ifaceterm.cc ├── ifaceterm.hh ├── inject_ghidra.cc ├── inject_ghidra.hh ├── inject_sleigh.cc ├── inject_sleigh.hh ├── interface.cc ├── interface.hh ├── jumptable.cc ├── jumptable.hh ├── libdecomp.cc ├── libdecomp.hh ├── loadimage.cc ├── loadimage.hh ├── loadimage_bfd.cc ├── loadimage_bfd.hh ├── loadimage_ghidra.cc ├── loadimage_ghidra.hh ├── loadimage_xml.cc ├── loadimage_xml.hh ├── memstate.cc ├── memstate.hh ├── merge.cc ├── merge.hh ├── op.cc ├── op.hh ├── opbehavior.cc ├── opbehavior.hh ├── opcodes.cc ├── opcodes.hh ├── options.cc ├── options.hh ├── override.cc ├── override.hh ├── paramid.cc ├── paramid.hh ├── partmap.hh ├── pcodecompile.cc ├── pcodecompile.hh ├── pcodeinject.cc ├── pcodeinject.hh ├── pcodeparse.cc ├── pcodeparse.hh ├── pcodeparse.tab.cpp ├── pcodeparse.tab.h ├── pcodeparse.y ├── pcoderaw.cc ├── pcoderaw.hh ├── prefersplit.cc ├── prefersplit.hh ├── prettyprint.cc ├── prettyprint.hh ├── printc.cc ├── printc.hh ├── printjava.cc ├── printjava.hh ├── printlanguage.cc ├── printlanguage.hh ├── rangemap.hh ├── rangeutil.cc ├── rangeutil.hh ├── raw_arch.cc ├── raw_arch.hh ├── ruleaction.cc ├── ruleaction.hh ├── rulecompile.cc ├── rulecompile.hh ├── ruleparse.tab.cpp ├── ruleparse.tab.h ├── ruleparse.y ├── semantics.cc ├── semantics.hh ├── sleigh.cc ├── sleigh.hh ├── sleigh_arch.cc ├── sleigh_arch.hh ├── sleighbase.cc ├── sleighbase.hh ├── sleighexample.cc ├── slgh_compile.cc ├── slgh_compile.hh ├── slghparse.cc ├── slghparse.hh ├── slghparse.y ├── slghpatexpress.cc ├── slghpatexpress.hh ├── slghpattern.cc ├── slghpattern.hh ├── slghscan.cc ├── slghscan.l ├── slghsymbol.cc ├── slghsymbol.hh ├── space.cc ├── space.hh ├── string_ghidra.cc ├── string_ghidra.hh ├── stringmanage.cc ├── stringmanage.hh ├── subflow.cc ├── subflow.hh ├── test.cc ├── test.hh ├── testfunction.cc ├── testfunction.hh ├── transform.cc ├── transform.hh ├── translate.cc ├── translate.hh ├── type.cc ├── type.hh ├── typegrp_ghidra.cc ├── typegrp_ghidra.hh ├── typeop.cc ├── typeop.hh ├── types.h ├── unify.cc ├── unify.hh ├── userop.cc ├── userop.hh ├── variable.cc ├── variable.hh ├── varmap.cc ├── varmap.hh ├── varnode.cc ├── varnode.hh ├── xml.cc ├── xml.hh ├── xml.tab.cpp ├── xml.tab.h ├── xml.y ├── xml_arch.cc └── xml_arch.hh ├── Ghidra9 ├── Ghidra.vcxproj ├── Ghidra.vcxproj.filters ├── action.cc ├── action.hh ├── address.cc ├── address.hh ├── architecture.cc ├── architecture.hh ├── bfd_arch.cc ├── bfd_arch.hh ├── block.cc ├── block.hh ├── blockaction.cc ├── blockaction.hh ├── callgraph.cc ├── callgraph.hh ├── capability.cc ├── capability.hh ├── cast.cc ├── cast.hh ├── codedata.cc ├── codedata.hh ├── comment.cc ├── comment.hh ├── comment_ghidra.cc ├── comment_ghidra.hh ├── condexe.cc ├── condexe.hh ├── consolemain.cc ├── context.cc ├── context.hh ├── coreaction.cc ├── coreaction.hh ├── cover.cc ├── cover.hh ├── cpool.cc ├── cpool.hh ├── cpool_ghidra.cc ├── cpool_ghidra.hh ├── crc32.cc ├── crc32.hh ├── database.cc ├── database.hh ├── database_ghidra.cc ├── database_ghidra.hh ├── doccore.hh ├── docmain.hh ├── double.cc ├── double.hh ├── dynamic.cc ├── dynamic.hh ├── emulate.cc ├── emulate.hh ├── emulateutil.cc ├── emulateutil.hh ├── error.hh ├── filemanage.cc ├── filemanage.hh ├── float.cc ├── float.hh ├── flow.cc ├── flow.hh ├── fspec.cc ├── fspec.hh ├── funcdata.cc ├── funcdata.hh ├── funcdata_block.cc ├── funcdata_op.cc ├── funcdata_varnode.cc ├── ghidra_arch.cc ├── ghidra_arch.hh ├── ghidra_context.cc ├── ghidra_context.hh ├── ghidra_process.cc ├── ghidra_process.hh ├── ghidra_translate.cc ├── ghidra_translate.hh ├── globalcontext.cc ├── globalcontext.hh ├── grammar.hh ├── grammar.tab.cpp ├── grammar.tab.h ├── grammar.y ├── graph.cc ├── graph.hh ├── heritage.cc ├── heritage.hh ├── ifacedecomp.cc ├── ifacedecomp.hh ├── ifaceterm.cc ├── ifaceterm.hh ├── inject_ghidra.cc ├── inject_ghidra.hh ├── inject_sleigh.cc ├── inject_sleigh.hh ├── interface.cc ├── interface.hh ├── jumptable.cc ├── jumptable.hh ├── libdecomp.cc ├── libdecomp.hh ├── loadimage.cc ├── loadimage.hh ├── loadimage_bfd.cc ├── loadimage_bfd.hh ├── loadimage_ghidra.cc ├── loadimage_ghidra.hh ├── loadimage_xml.cc ├── loadimage_xml.hh ├── memstate.cc ├── memstate.hh ├── merge.cc ├── merge.hh ├── op.cc ├── op.hh ├── opbehavior.cc ├── opbehavior.hh ├── opcodes.cc ├── opcodes.hh ├── options.cc ├── options.hh ├── override.cc ├── override.hh ├── paramid.cc ├── paramid.hh ├── partmap.hh ├── pcodecompile.cc ├── pcodecompile.hh ├── pcodeinject.cc ├── pcodeinject.hh ├── pcodeparse.hh ├── pcodeparse.tab.cpp ├── pcodeparse.tab.h ├── pcodeparse.y ├── pcoderaw.cc ├── pcoderaw.hh ├── prefersplit.cc ├── prefersplit.hh ├── prettyprint.cc ├── prettyprint.hh ├── printc.cc ├── printc.hh ├── printjava.cc ├── printjava.hh ├── printlanguage.cc ├── printlanguage.hh ├── rangemap.hh ├── rangeutil.cc ├── rangeutil.hh ├── raw_arch.cc ├── raw_arch.hh ├── ruleaction.cc ├── ruleaction.hh ├── rulecompile.cc ├── rulecompile.hh ├── ruleparse.tab.cpp ├── ruleparse.tab.h ├── ruleparse.y ├── semantics.cc ├── semantics.hh ├── sleigh.cc ├── sleigh.hh ├── sleigh_arch.cc ├── sleigh_arch.hh ├── sleighbase.cc ├── sleighbase.hh ├── sleighexample.cc ├── slgh_compile.cc ├── slgh_compile.hh ├── slghparse.tab.cpp ├── slghparse.tab.hh ├── slghparse.y ├── slghpatexpress.cc ├── slghpatexpress.hh ├── slghpattern.cc ├── slghpattern.hh ├── slghscan.flex.cpp ├── slghscan.flex.hh ├── slghscan.l ├── slghsymbol.cc ├── slghsymbol.hh ├── space.cc ├── space.hh ├── subflow.cc ├── subflow.hh ├── translate.cc ├── translate.hh ├── type.cc ├── type.hh ├── typegrp_ghidra.cc ├── typegrp_ghidra.hh ├── typeop.cc ├── typeop.hh ├── types.h ├── unify.cc ├── unify.hh ├── userop.cc ├── userop.hh ├── variable.cc ├── variable.hh ├── varmap.cc ├── varmap.hh ├── varnode.cc ├── varnode.hh ├── xml.hh ├── xml.tab.cpp ├── xml.tab.h ├── xml.y ├── xml_arch.cc └── xml_arch.hh ├── GhidraDec-x64dbg ├── Branding.cpp ├── Branding.h ├── CppSyntaxHighlighter.cpp ├── CppSyntaxHighlighter.h ├── CxxView.cpp ├── CxxView.h ├── GhidraDec-x64dbg.vcxproj ├── GhidraDec-x64dbg.vcxproj.filters ├── GhidraDecView.cpp ├── GhidraDecView.h ├── MainWindow.cpp ├── MainWindow.h ├── PluginMain.cpp ├── PluginMain.h ├── QtPlugin.cpp ├── QtPlugin.h ├── TextView.cpp ├── TextView.h ├── Version.h ├── config.h ├── dllmain.cpp ├── framework.h ├── images │ ├── GhidraIcon16.png │ ├── decompile_function.png │ └── decompile_selection.png ├── pch.cpp ├── pch.h └── x64dbg-module.qrc ├── GhidraDec.sln ├── GhidraDec.vcxproj ├── GhidraDec.vcxproj.filters ├── GhidraDecIface.cpp ├── GhidraDecIface.vcxproj ├── GhidraDecIface.vcxproj.filters ├── LICENSE ├── MakeGhidraDec.bat ├── PropertySheet.props ├── README.md ├── code_viewer.cpp ├── code_viewer.h ├── config_generator.cpp ├── config_generator.h ├── cpp.hint ├── decompile9 ├── address.cc ├── address.hh ├── context.cc ├── context.hh ├── emulate.cc ├── emulate.hh ├── error.hh ├── filemanage.cc ├── filemanage.hh ├── float.cc ├── float.hh ├── globalcontext.cc ├── globalcontext.hh ├── loadimage.cc ├── loadimage.hh ├── memstate.cc ├── memstate.hh ├── opbehavior.cc ├── opbehavior.hh ├── opcodes.cc ├── opcodes.hh ├── partmap.hh ├── pcodecompile.cc ├── pcodecompile.hh ├── pcodeparse.hh ├── pcodeparse.tab.cpp ├── pcodeparse.tab.h ├── pcodeparse.tab.hpp ├── pcodeparse.y ├── pcoderaw.cc ├── pcoderaw.hh ├── semantics.cc ├── semantics.hh ├── sleigh.cc ├── sleigh.hh ├── sleighbase.cc ├── sleighbase.hh ├── slghpatexpress.cc ├── slghpatexpress.hh ├── slghpattern.cc ├── slghpattern.hh ├── slghsymbol.cc ├── slghsymbol.hh ├── space.cc ├── space.hh ├── translate.cc ├── translate.hh ├── types.h ├── xml.hh ├── xml.tab.cpp ├── xml.tab.h ├── xml.tab.hpp └── xml.y ├── decompiler.cpp ├── decompiler.h ├── defs.cpp ├── defs.h ├── deps ├── CMakeLists.txt ├── jsoncpp │ └── CMakeLists.txt └── retdec │ └── CMakeLists.txt ├── extractghidra.bat ├── framework.h ├── idaplugin.cpp ├── idaplugin.h ├── plugin_config.cpp ├── plugin_config.h ├── sleighinterface.cpp └── sleighinterface.h /Ghidra/.gitignore: -------------------------------------------------------------------------------- 1 | coreext_* 2 | ghidraext_* 3 | consoleext_* 4 | libdecomp.a 5 | libdecomp_dbg.a 6 | decomp_dbg 7 | decomp_opt 8 | ghidra_dbg 9 | ghidra_opt 10 | ghidra_test_dbg 11 | sleigh_dbg 12 | com_dbg 13 | com_opt 14 | test_dbg 15 | ghi_dbg 16 | ghi_opt 17 | sla_dbg 18 | sla_opt 19 | 20 | -------------------------------------------------------------------------------- /Ghidra/bfd_arch.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * NOTE: Interface to GNU BFD library which is GPL 3 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | /// \file bfd_arch.hh 18 | /// \brief Specific implementation of Architecture using GNU BFD libraries 19 | 20 | #include "sleigh_arch.hh" 21 | #include "loadimage_bfd.hh" 22 | 23 | /// \brief Extension point for building a GNU BFD capable Architecture 24 | class BfdArchitectureCapability : public ArchitectureCapability { 25 | static BfdArchitectureCapability bfdArchitectureCapability; ///< The singleton instance 26 | BfdArchitectureCapability(void); ///< Singleton constructor 27 | BfdArchitectureCapability(const BfdArchitectureCapability &op2); ///< Not implemented 28 | BfdArchitectureCapability &operator=(const BfdArchitectureCapability &op2); ///< Not implemented 29 | public: 30 | virtual ~BfdArchitectureCapability(void); 31 | virtual Architecture *buildArchitecture(const string &filename,const string &target,ostream *estream); 32 | virtual bool isFileMatch(const string &filename) const; 33 | virtual bool isXmlMatch(Document *doc) const; 34 | }; 35 | 36 | /// \brief Architecture that reads executable files using GNU BFD libraries 37 | class BfdArchitecture : public SleighArchitecture { 38 | long adjustvma; ///< How much to adjust the virtual memory address 39 | virtual void buildLoader(DocumentStorage &store); 40 | virtual void resolveArchitecture(void); 41 | virtual void postSpecFile(void); 42 | public: 43 | virtual void saveXml(ostream &s) const; 44 | virtual void restoreXml(DocumentStorage &store); 45 | BfdArchitecture(const string &fname,const string &targ,ostream *estream); ///< Constructor 46 | virtual ~BfdArchitecture(void) {} 47 | }; 48 | -------------------------------------------------------------------------------- /Ghidra/capability.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "capability.hh" 17 | 18 | /// Access static vector of CapabilityPoint objects that are registered during static initialization 19 | /// The list itself is created once on the first call to this method 20 | /// \e after all the static initializers have run 21 | /// \return the list of registered extensions 22 | vector &CapabilityPoint::getList(void) 23 | 24 | { 25 | static vector thelist; // This gets allocated exactly once on first call 26 | return thelist; 27 | } 28 | 29 | /// Constructing the object automatically registers it. 30 | /// For global instances, this happens during static initialization 31 | CapabilityPoint::CapabilityPoint(void) 32 | 33 | { 34 | getList().push_back(this); 35 | } 36 | 37 | /// Give all registered capabilities a chance to initialize (\e after all static initialization has happened) 38 | void CapabilityPoint::initializeAll(void) 39 | 40 | { 41 | vector &list( getList() ); 42 | for(int4 i=0;iinitialize(); 45 | } 46 | list.clear(); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Ghidra/capability.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file capability.hh 17 | /// \brief Infrastructure for discovering code extensions to the decompiler 18 | #ifndef __CAPABILITY__ 19 | #define __CAPABILITY__ 20 | 21 | #include "types.h" 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | /// \brief Class for automatically registering extension points to the decompiler 28 | /// 29 | /// This uses the C++ static initializer feature to automatically \e discover 30 | /// and register extension point. Code for an extension should provide 31 | /// a class that derives from CapabilityPoint and overrides the initialize() method. 32 | /// Additionally there should be a singleton static instantiation of this extension class. 33 | /// The extensions are accumulated in a list automatically, then the decompiler engine 34 | /// will ensure that the initialize() method is called on each extension, allowing it 35 | /// to complete its integration. 36 | class CapabilityPoint { 37 | static vector &getList(void); ///< Retrieve the list of extension point singletons 38 | protected: 39 | CapabilityPoint(void); ///< Construct extension capability exactly once 40 | public: 41 | virtual ~CapabilityPoint(void) {} ///< Destructor 42 | 43 | /// \brief Complete initialization of an extension point 44 | /// 45 | /// This method is implemented by each extension so it can do specialized integration 46 | virtual void initialize(void)=0; 47 | 48 | static void initializeAll(void); ///< Finish initialization for all extension points 49 | }; 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Ghidra/comment_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "comment_ghidra.hh" 17 | 18 | CommentDatabaseGhidra::CommentDatabaseGhidra(ArchitectureGhidra *g) 19 | : CommentDatabase() 20 | { 21 | ghidra = g; 22 | cachefilled = false; 23 | } 24 | 25 | /// Fetch all comments for the function in one chunk. Deserialize them and 26 | /// store the Comment objects in the cache 27 | /// \param fad is the address of the given function 28 | void CommentDatabaseGhidra::fillCache(const Address &fad) const 29 | 30 | { 31 | Document *doc; 32 | uint4 commentfilter; 33 | 34 | if (cachefilled) return; // Already queried ghidra 35 | cachefilled = true; 36 | // Gather which types of comments are being printed currently 37 | commentfilter = ghidra->print->getHeaderComment(); 38 | commentfilter |= ghidra->print->getInstructionComment(); 39 | if (commentfilter==0) return; 40 | CommentSet::const_iterator iter,iterend; 41 | iter = cache.beginComment(fad); 42 | iterend = cache.endComment(fad); 43 | 44 | doc = ghidra->getComments(fad,commentfilter); 45 | if (doc != (Document *)0) { 46 | cache.restoreXml(doc->getRoot(),ghidra); 47 | delete doc; 48 | } 49 | } 50 | 51 | /// For the Ghidra implementation of CommentDatabase, addComment() is currently only 52 | /// called by the warning routines which generates the 53 | /// \e warning and \e warningheader comment types. Neither of 54 | /// these types is intended to be a permanent comment in the 55 | /// database, so we only add the comment to the cache 56 | void CommentDatabaseGhidra::addComment(uint4 tp, 57 | const Address &fad, 58 | const Address &ad, 59 | const string &txt) 60 | { 61 | cache.addComment(tp,fad,ad,txt); 62 | } 63 | 64 | bool CommentDatabaseGhidra::addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad, 65 | const string &txt) 66 | { 67 | return cache.addCommentNoDuplicate(tp,fad,ad,txt); 68 | } 69 | 70 | CommentSet::const_iterator CommentDatabaseGhidra::beginComment(const Address &fad) const 71 | 72 | { 73 | fillCache(fad); 74 | return cache.beginComment(fad); 75 | } 76 | 77 | CommentSet::const_iterator CommentDatabaseGhidra::endComment(const Address &fad) const 78 | 79 | { 80 | return cache.endComment(fad); 81 | } 82 | 83 | -------------------------------------------------------------------------------- /Ghidra/comment_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file comment_ghidra.hh 17 | /// \brief Obtain comments by talking to a Ghidra client 18 | #ifndef __COMMENT_GHIDRA__ 19 | #define __COMMENT_GHIDRA__ 20 | 21 | #include "comment.hh" 22 | #include "ghidra_arch.hh" 23 | 24 | /// \brief An implementation of CommentDatabase backed by a Ghidra client 25 | /// 26 | /// Comment information about particular functions is obtained by querying 27 | /// a Ghidra client. All comments for a single function are queried at once, and 28 | /// results are cached in this object. The cache needs to be cleared between 29 | /// functions using the clear() method. 30 | class CommentDatabaseGhidra : public CommentDatabase { 31 | ArchitectureGhidra *ghidra; ///< The Architecture and connection to the Ghidra client 32 | mutable CommentDatabaseInternal cache; ///< A cache of Comment objects received from the Ghidra client 33 | mutable bool cachefilled; ///< Set to \b true if comments for the current function have been fetched 34 | void fillCache(const Address &fad) const; ///< Fetch comments for the given function 35 | public: 36 | CommentDatabaseGhidra(ArchitectureGhidra *g); ///< Constructor 37 | virtual void clear(void) { cache.clear(); cachefilled=false; } 38 | virtual void clearType(const Address &fad,uint4 tp) { 39 | cache.clearType(fad,tp); 40 | } 41 | virtual void addComment(uint4 tp,const Address &fad, 42 | const Address &ad,const string &txt); 43 | virtual bool addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad,const string &txt); 44 | virtual void deleteComment(Comment *com) { 45 | throw LowlevelError("deleteComment unimplemented"); } 46 | virtual CommentSet::const_iterator beginComment(const Address &fad) const; 47 | virtual CommentSet::const_iterator endComment(const Address &fad) const; 48 | virtual void saveXml(ostream &s) const { 49 | throw LowlevelError("commentdb::saveXml unimplemented"); } 50 | virtual void restoreXml(const Element *el,const AddrSpaceManager *trans) { 51 | throw LowlevelError("commentdb::restoreXml unimplemented"); } 52 | }; 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /Ghidra/cpool_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "cpool_ghidra.hh" 17 | 18 | ConstantPoolGhidra::ConstantPoolGhidra(ArchitectureGhidra *g) 19 | 20 | { 21 | ghidra = g; 22 | } 23 | 24 | CPoolRecord *ConstantPoolGhidra::createRecord(const vector &refs) 25 | 26 | { 27 | throw LowlevelError("Cannot access constant pool with this method"); 28 | } 29 | 30 | const CPoolRecord *ConstantPoolGhidra::getRecord(const vector &refs) const 31 | 32 | { 33 | const CPoolRecord *rec = cache.getRecord(refs); 34 | if (rec == (const CPoolRecord *)0) { 35 | Document *doc; 36 | try { 37 | doc = ghidra->getCPoolRef(refs); 38 | } 39 | catch(JavaError &err) { 40 | throw LowlevelError("Error fetching constant pool record: " + err.explain); 41 | } 42 | catch(XmlError &err) { 43 | throw LowlevelError("Error in constant pool record xml: "+err.explain); 44 | } 45 | if (doc == (Document *)0) { 46 | ostringstream s; 47 | s << "Could not retrieve constant pool record for reference: 0x" << refs[0]; 48 | throw LowlevelError(s.str()); 49 | } 50 | rec = cache.restoreXmlRecord(refs,doc->getRoot(),*ghidra->types); 51 | delete doc; 52 | } 53 | return rec; 54 | } 55 | 56 | void ConstantPoolGhidra::saveXml(ostream &s) const 57 | 58 | { 59 | throw LowlevelError("Cannot access constant pool with this method"); 60 | } 61 | 62 | void ConstantPoolGhidra::restoreXml(const Element *el,TypeFactory &typegrp) 63 | 64 | { 65 | throw LowlevelError("Cannot access constant pool with this method"); 66 | } 67 | -------------------------------------------------------------------------------- /Ghidra/cpool_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file cpool_ghidra.hh 17 | /// \brief Utility for implementing a \e constant \e pool backed by a Ghidra client 18 | 19 | #ifndef __CPOOL_GHIDRA__ 20 | #define __CPOOL_GHIDRA__ 21 | 22 | #include "ghidra_arch.hh" 23 | 24 | /// \brief An implementation of ConstantPool using a Ghidra client as the backing storage 25 | /// 26 | /// The actual CPoolRecord objects are cached locally, but new queries are placed 27 | /// with the Ghidra client hosting the program currently being decompiled. The 28 | /// queries and response records are sent via XML. The saveXml() and restoreXml() 29 | /// methods are disabled. The clear() method only releases the local cache, 30 | /// no records on the Ghidra client are affected. 31 | class ConstantPoolGhidra : public ConstantPool { 32 | ArchitectureGhidra *ghidra; ///< The connection with the Ghidra client 33 | mutable ConstantPoolInternal cache; ///< The local cache of previouly queried CPoolRecord objects 34 | virtual CPoolRecord *createRecord(const vector &refs); 35 | public: 36 | ConstantPoolGhidra(ArchitectureGhidra *g); ///< Constructor 37 | virtual const CPoolRecord *getRecord(const vector &refs) const; 38 | virtual bool empty(void) const { return false; } 39 | virtual void clear(void) { cache.clear(); } 40 | virtual void saveXml(ostream &s) const; 41 | virtual void restoreXml(const Element *el,TypeFactory &typegrp); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Ghidra/crc32.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * REVIEWED: YES 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #include "crc32.hh" 18 | 19 | // Table for bytewise calculation of a 32-bit Cyclic Redundancy Check 20 | uint4 crc32tab[] = { 21 | 0x0,0x77073096,0xee0e612c,0x990951ba,0x76dc419,0x706af48f, 22 | 0xe963a535,0x9e6495a3,0xedb8832,0x79dcb8a4,0xe0d5e91e, 23 | 0x97d2d988,0x9b64c2b,0x7eb17cbd,0xe7b82d07,0x90bf1d91, 24 | 0x1db71064,0x6ab020f2,0xf3b97148,0x84be41de,0x1adad47d, 25 | 0x6ddde4eb,0xf4d4b551,0x83d385c7,0x136c9856,0x646ba8c0, 26 | 0xfd62f97a,0x8a65c9ec,0x14015c4f,0x63066cd9,0xfa0f3d63, 27 | 0x8d080df5,0x3b6e20c8,0x4c69105e,0xd56041e4,0xa2677172, 28 | 0x3c03e4d1,0x4b04d447,0xd20d85fd,0xa50ab56b,0x35b5a8fa, 29 | 0x42b2986c,0xdbbbc9d6,0xacbcf940,0x32d86ce3,0x45df5c75, 30 | 0xdcd60dcf,0xabd13d59,0x26d930ac,0x51de003a,0xc8d75180, 31 | 0xbfd06116,0x21b4f4b5,0x56b3c423,0xcfba9599,0xb8bda50f, 32 | 0x2802b89e,0x5f058808,0xc60cd9b2,0xb10be924,0x2f6f7c87, 33 | 0x58684c11,0xc1611dab,0xb6662d3d,0x76dc4190,0x1db7106, 34 | 0x98d220bc,0xefd5102a,0x71b18589,0x6b6b51f,0x9fbfe4a5, 35 | 0xe8b8d433,0x7807c9a2,0xf00f934,0x9609a88e,0xe10e9818, 36 | 0x7f6a0dbb,0x86d3d2d,0x91646c97,0xe6635c01,0x6b6b51f4, 37 | 0x1c6c6162,0x856530d8,0xf262004e,0x6c0695ed,0x1b01a57b, 38 | 0x8208f4c1,0xf50fc457,0x65b0d9c6,0x12b7e950,0x8bbeb8ea, 39 | 0xfcb9887c,0x62dd1ddf,0x15da2d49,0x8cd37cf3,0xfbd44c65, 40 | 0x4db26158,0x3ab551ce,0xa3bc0074,0xd4bb30e2,0x4adfa541, 41 | 0x3dd895d7,0xa4d1c46d,0xd3d6f4fb,0x4369e96a,0x346ed9fc, 42 | 0xad678846,0xda60b8d0,0x44042d73,0x33031de5,0xaa0a4c5f, 43 | 0xdd0d7cc9,0x5005713c,0x270241aa,0xbe0b1010,0xc90c2086, 44 | 0x5768b525,0x206f85b3,0xb966d409,0xce61e49f,0x5edef90e, 45 | 0x29d9c998,0xb0d09822,0xc7d7a8b4,0x59b33d17,0x2eb40d81, 46 | 0xb7bd5c3b,0xc0ba6cad,0xedb88320,0x9abfb3b6,0x3b6e20c, 47 | 0x74b1d29a,0xead54739,0x9dd277af,0x4db2615,0x73dc1683, 48 | 0xe3630b12,0x94643b84,0xd6d6a3e,0x7a6a5aa8,0xe40ecf0b, 49 | 0x9309ff9d,0xa00ae27,0x7d079eb1,0xf00f9344,0x8708a3d2, 50 | 0x1e01f268,0x6906c2fe,0xf762575d,0x806567cb,0x196c3671, 51 | 0x6e6b06e7,0xfed41b76,0x89d32be0,0x10da7a5a,0x67dd4acc, 52 | 0xf9b9df6f,0x8ebeeff9,0x17b7be43,0x60b08ed5,0xd6d6a3e8, 53 | 0xa1d1937e,0x38d8c2c4,0x4fdff252,0xd1bb67f1,0xa6bc5767, 54 | 0x3fb506dd,0x48b2364b,0xd80d2bda,0xaf0a1b4c,0x36034af6, 55 | 0x41047a60,0xdf60efc3,0xa867df55,0x316e8eef,0x4669be79, 56 | 0xcb61b38c,0xbc66831a,0x256fd2a0,0x5268e236,0xcc0c7795, 57 | 0xbb0b4703,0x220216b9,0x5505262f,0xc5ba3bbe,0xb2bd0b28, 58 | 0x2bb45a92,0x5cb36a04,0xc2d7ffa7,0xb5d0cf31,0x2cd99e8b, 59 | 0x5bdeae1d,0x9b64c2b0,0xec63f226,0x756aa39c,0x26d930a, 60 | 0x9c0906a9,0xeb0e363f,0x72076785,0x5005713,0x95bf4a82, 61 | 0xe2b87a14,0x7bb12bae,0xcb61b38,0x92d28e9b,0xe5d5be0d, 62 | 0x7cdcefb7,0xbdbdf21,0x86d3d2d4,0xf1d4e242,0x68ddb3f8, 63 | 0x1fda836e,0x81be16cd,0xf6b9265b,0x6fb077e1,0x18b74777, 64 | 0x88085ae6,0xff0f6a70,0x66063bca,0x11010b5c,0x8f659eff, 65 | 0xf862ae69,0x616bffd3,0x166ccf45,0xa00ae278,0xd70dd2ee, 66 | 0x4e048354,0x3903b3c2,0xa7672661,0xd06016f7,0x4969474d, 67 | 0x3e6e77db,0xaed16a4a,0xd9d65adc,0x40df0b66,0x37d83bf0, 68 | 0xa9bcae53,0xdebb9ec5,0x47b2cf7f,0x30b5ffe9,0xbdbdf21c, 69 | 0xcabac28a,0x53b39330,0x24b4a3a6,0xbad03605,0xcdd70693, 70 | 0x54de5729,0x23d967bf,0xb3667a2e,0xc4614ab8,0x5d681b02, 71 | 0x2a6f2b94,0xb40bbe37,0xc30c8ea1,0x5a05df1b,0x2d02ef8d }; 72 | 73 | -------------------------------------------------------------------------------- /Ghidra/crc32.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file crc32.hh 17 | /// \brief Table and function for computing a CRC32 18 | 19 | #ifndef __CRC32__ 20 | #define __CRC32__ 21 | 22 | #include "types.h" 23 | 24 | extern uint4 crc32tab[]; ///< Table for quickly computing a 32-bit Cyclic Redundacy Check (CRC) 25 | 26 | /// \brief Feed 8 bits into a CRC register 27 | /// 28 | /// \param reg is the current state of the CRC register 29 | /// \param val holds 8 bits (least significant) to feed in 30 | /// \return the new value of the register 31 | inline uint4 crc_update(uint4 reg,uint4 val) { 32 | return crc32tab[(reg ^ val)&0xff] ^ (reg>>8); } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Ghidra/error.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file error.hh 17 | /// \brief Base class for error handling facilities 18 | /// 19 | /// This is also doubles as a place to list the common include files 20 | 21 | #ifndef __CPUI_ERROR__ 22 | #define __CPUI_ERROR__ 23 | 24 | #include "types.h" 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | using namespace std; 37 | 38 | /// \brief The lowest level error generated by the decompiler 39 | /// 40 | /// This is the base error for all exceptions thrown by the 41 | /// decompiler. This underived form is thrown for very low 42 | /// level errors that immediately abort decompilation (usually 43 | /// for just a single function). 44 | struct LowlevelError { 45 | string explain; ///< Explanatory string 46 | /// Initialize the error with an explanatory string 47 | LowlevelError(const string &s) { explain = s; } 48 | }; 49 | 50 | /// \brief A generic recoverable error 51 | /// 52 | /// This error is the most basic form of recoverable error, 53 | /// meaning there is some problem that the user did not take 54 | /// into account. 55 | struct RecovError : public LowlevelError { 56 | /// Initialize the error with an explanatory string 57 | RecovError(const string &s) : LowlevelError(s) {} 58 | }; 59 | 60 | /// \brief An error generated while parsing a command or language 61 | /// 62 | /// This error is generated when parsing character data of some 63 | /// form, as in a user command from the console or when parsing 64 | /// C syntax. 65 | struct ParseError : public LowlevelError { // Parsing error 66 | /// Initialize the error with an explanatory string 67 | ParseError(const string &s) : LowlevelError(s) {} 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /Ghidra/filemanage.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // Generic (POSIX) class for searching files and managing paths 17 | 18 | #ifndef __FILEMANAGE__ 19 | #define __FILEMANAGE__ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | class FileManage { 30 | vector pathlist; // List of paths to search for files 31 | static char separator; 32 | static string buildPath(const vector &pathels,int level); 33 | static bool testDevelopmentPath(const vector &pathels,int level,string &root); 34 | static bool testInstallPath(const vector &pathels,int level,string &root); 35 | public: 36 | void addDir2Path(const string &path); 37 | void addCurrentDir(void); 38 | void findFile(string &res,const string &name) const; // Resolve full pathname 39 | void matchList(vector &res,const string &match,bool isSuffix) const; // List of files with suffix 40 | static bool isDirectory(const string &path); 41 | static void matchListDir(vector &res,const string &match,bool isSuffix,const string &dir,bool allowdot); 42 | static void directoryList(vector &res,const string &dirname,bool allowdot=false); 43 | static void scanDirectoryRecursive(vector &res,const string &matchname,const string &rootpath,int maxdepth); 44 | static void splitPath(const string &full,string &path,string &base); 45 | static bool isAbsolutePath(const string &full) { if (full.empty()) return false; return (full[0] == separator); } 46 | static string discoverGhidraRoot(const char *argv0); 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /Ghidra/ghidra_context.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "ghidra_context.hh" 17 | 18 | const TrackedSet &ContextGhidra::getTrackedSet(const Address &addr) const 19 | 20 | { 21 | cache.clear(); 22 | 23 | Document *doc = ((ArchitectureGhidra *)glb)->getTrackedRegisters(addr); 24 | Element *root = doc->getRoot(); 25 | 26 | restoreTracked(root,glb,cache); 27 | delete doc; 28 | return cache; 29 | } 30 | -------------------------------------------------------------------------------- /Ghidra/ghidra_translate.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file ghidra_translate.hh 17 | /// \brief Class for fetching p-code from a Ghidra client 18 | 19 | #ifndef __TRANSLATE_GHIDRA__ 20 | #define __TRANSLATE_GHIDRA__ 21 | 22 | #include "translate.hh" 23 | #include "ghidra_arch.hh" 24 | 25 | /// \brief An implementation of Translate that queries a Ghidra client for p-code information 26 | /// 27 | /// This class provides: 28 | /// - P-code for instructions and 29 | /// - Register names 30 | /// 31 | /// by sending a request to a Ghidra client and decoding the response. 32 | /// Messages are generally based on an XML format, but p-code responses in particular 33 | /// have a tight internal encoding. 34 | class GhidraTranslate : public Translate { 35 | ArchitectureGhidra *glb; ///< The Ghidra Architecture and connection to the client 36 | mutable map nm2addr; ///< Mapping from register name to Varnode 37 | mutable map addr2nm; ///< Mapping rom Varnode to register name 38 | const VarnodeData &cacheRegister(const string &nm,const VarnodeData &data) const; 39 | void restoreXml(const Element *el); ///< Initialize \b this Translate from XML 40 | public: 41 | GhidraTranslate(ArchitectureGhidra *g) { glb = g; } ///< Constructor 42 | 43 | virtual void initialize(DocumentStorage &store); 44 | virtual const VarnodeData &getRegister(const string &nm) const; 45 | virtual string getRegisterName(AddrSpace *base,uintb off,int4 size) const; 46 | virtual void getAllRegisters(map ®list) const { 47 | throw LowlevelError("Cannot currently get all registers through this interface"); } 48 | virtual void getUserOpNames(vector &res) const; 49 | virtual int4 oneInstruction(PcodeEmit &emit,const Address &baseaddr) const; 50 | virtual int4 instructionLength(const Address &baseaddr) const { 51 | throw LowlevelError("Cannot currently get instruction length through this interface"); } 52 | virtual int4 printAssembly(AssemblyEmit &emit,const Address &baseaddr) const { 53 | throw LowlevelError("Cannot dump assembly through this interface"); } 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /Ghidra/grammar.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_CPARSE_GRAMMAR_TAB_H_INCLUDED 34 | # define YY_CPARSE_GRAMMAR_TAB_H_INCLUDED 35 | /* Enabling traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int cparsedebug; 41 | #endif 42 | 43 | /* Tokens. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | /* Put the tokens into the symbol table, so that GDB and other debuggers 47 | know about them. */ 48 | enum yytokentype { 49 | DOTDOTDOT = 258, 50 | BADTOKEN = 259, 51 | STRUCT = 260, 52 | UNION = 261, 53 | ENUM = 262, 54 | DECLARATION_RESULT = 263, 55 | PARAM_RESULT = 264, 56 | NUMBER = 265, 57 | IDENTIFIER = 266, 58 | STORAGE_CLASS_SPECIFIER = 267, 59 | TYPE_QUALIFIER = 268, 60 | FUNCTION_SPECIFIER = 269, 61 | TYPE_NAME = 270 62 | }; 63 | #endif 64 | 65 | 66 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 67 | typedef union YYSTYPE 68 | { 69 | /* Line 2058 of yacc.c */ 70 | #line 25 "grammar.y" 71 | 72 | uint4 flags; 73 | TypeDeclarator *dec; 74 | vector *declist; 75 | TypeSpecifiers *spec; 76 | vector *ptrspec; 77 | Datatype *type; 78 | Enumerator *enumer; 79 | vector *vecenum; 80 | string *str; 81 | uintb *i; 82 | 83 | 84 | /* Line 2058 of yacc.c */ 85 | #line 86 "grammar.tab.h" 86 | } YYSTYPE; 87 | # define YYSTYPE_IS_TRIVIAL 1 88 | # define yystype YYSTYPE /* obsolescent; will be withdrawn */ 89 | # define YYSTYPE_IS_DECLARED 1 90 | #endif 91 | 92 | extern YYSTYPE cparselval; 93 | 94 | #ifdef YYPARSE_PARAM 95 | #if defined __STDC__ || defined __cplusplus 96 | int cparseparse (void *YYPARSE_PARAM); 97 | #else 98 | int cparseparse (); 99 | #endif 100 | #else /* ! YYPARSE_PARAM */ 101 | #if defined __STDC__ || defined __cplusplus 102 | int cparseparse (void); 103 | #else 104 | int cparseparse (); 105 | #endif 106 | #endif /* ! YYPARSE_PARAM */ 107 | 108 | #endif /* !YY_CPARSE_GRAMMAR_TAB_H_INCLUDED */ 109 | -------------------------------------------------------------------------------- /Ghidra/graph.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * REVIEWED: YES 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #include "funcdata.hh" 18 | 19 | extern void dump_dataflow_graph(Funcdata &data,ostream &s); 20 | extern void dump_controlflow_graph(const string &name,const BlockGraph &graph,ostream &s); 21 | extern void dump_dom_graph(const string &name,const BlockGraph &graph,ostream &s); 22 | -------------------------------------------------------------------------------- /Ghidra/ifaceterm.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file ifaceterm.hh 17 | /// \brief Add some terminal capabilities to the command-line interface (IfaceStatus) 18 | 19 | #include "interface.hh" 20 | 21 | #ifdef __TERMINAL__ 22 | extern "C" { 23 | #include 24 | #include 25 | } 26 | #endif 27 | 28 | /// \brief Implement the command-line interface on top of a specific input stream 29 | /// 30 | /// An initial input stream is provided as the base stream to parse for commands. 31 | /// Additional input streams can be stacked by invoking scripts. 32 | /// If the stream supports it, the stream parser recognizes special command-line editing 33 | /// and completion keys. 34 | class IfaceTerm : public IfaceStatus { 35 | #ifdef __TERMINAL__ 36 | bool is_terminal; ///< True if the input stream is a terminal 37 | int4 ifd; ///< Underlying file descriptor 38 | struct termios itty; ///< Original terminal settings 39 | #endif 40 | istream *sptr; ///< The base input stream for the interface 41 | vector inputstack; ///< Stack of nested input streams 42 | int4 doCompletion(string &line,int4 cursor); ///< 'Complete' the current command line 43 | virtual void readLine(string &line); 44 | public: 45 | IfaceTerm(const string &prmpt,istream &is,ostream &os); ///< Constructor 46 | virtual ~IfaceTerm(void); 47 | virtual void pushScript(istream *iptr,const string &newprompt); 48 | virtual void popScript(void); 49 | virtual bool isStreamFinished(void) const; 50 | }; 51 | -------------------------------------------------------------------------------- /Ghidra/libdecomp.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "libdecomp.hh" 17 | 18 | void startDecompilerLibrary(const char *sleighhome) 19 | 20 | { 21 | CapabilityPoint::initializeAll(); 22 | ArchitectureCapability::sortCapabilities(); 23 | 24 | if (sleighhome != (const char *)0) 25 | SleighArchitecture::scanForSleighDirectories(sleighhome); 26 | } 27 | 28 | void startDecompilerLibrary(const vector &extrapaths) 29 | 30 | { 31 | CapabilityPoint::initializeAll(); 32 | ArchitectureCapability::sortCapabilities(); 33 | 34 | for(uint4 i=0;i &extrapaths) 39 | 40 | { 41 | CapabilityPoint::initializeAll(); 42 | ArchitectureCapability::sortCapabilities(); 43 | 44 | if (sleighhome != (const char *)0) 45 | SleighArchitecture::scanForSleighDirectories(sleighhome); 46 | 47 | for(uint4 i=0;i &extrapaths); 32 | extern void startDecompilerLibrary(const char *sleighhome,const vector &extrapaths); 33 | 34 | extern void shutdownDecompilerLibrary(void); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /Ghidra/loadimage.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "loadimage.hh" 17 | 18 | /// This is a convenience method wrapped around the core 19 | /// loadFill() routine. It automatically allocates an array 20 | /// of the desired size, and then fills it with load image data. 21 | /// If the array cannot be allocated, an exception is thrown. 22 | /// The caller assumes the responsibility of freeing the 23 | /// array after it has been used. 24 | /// \param size is the number of bytes to read from the image 25 | /// \param addr is the address of the first byte being read 26 | /// \return a pointer to the desired bytes 27 | uint1 *LoadImage::load(int4 size,const Address &addr) 28 | 29 | { 30 | uint1 *buf = new uint1[ size ]; 31 | if (buf == (uint1 *)0) 32 | throw LowlevelError("Out of memory"); 33 | loadFill(buf,size,addr); 34 | return buf; 35 | } 36 | 37 | RawLoadImage::RawLoadImage(const string &f) : LoadImage(f) 38 | 39 | { 40 | vma = 0; 41 | thefile = (ifstream *)0; 42 | spaceid = (AddrSpace *)0; 43 | filesize = 0; 44 | } 45 | 46 | RawLoadImage::~RawLoadImage(void) 47 | 48 | { 49 | if (thefile != (ifstream *)0) { 50 | thefile->close(); 51 | delete thefile; 52 | } 53 | } 54 | 55 | /// The file is opened and its size immediately recovered. 56 | void RawLoadImage::open(void) 57 | 58 | { 59 | if (thefile != (ifstream *)0) throw LowlevelError("loadimage is already open"); 60 | thefile = new ifstream(filename.c_str()); 61 | if (!(*thefile)) { 62 | string errmsg = "Unable to open raw image file: "+filename; 63 | throw LowlevelError(errmsg); 64 | } 65 | thefile->seekg(0,ios::end); 66 | filesize = thefile->tellg(); 67 | } 68 | 69 | string RawLoadImage::getArchType(void) const 70 | 71 | { 72 | return "unknown"; 73 | } 74 | 75 | void RawLoadImage::adjustVma(long adjust) 76 | 77 | { 78 | adjust = AddrSpace::addressToByte(adjust,spaceid->getWordSize()); 79 | vma += adjust; 80 | } 81 | 82 | void RawLoadImage::loadFill(uint1 *ptr,int4 size,const Address &addr) 83 | 84 | { 85 | uintb curaddr = addr.getOffset(); 86 | uintb offset = 0; 87 | uintb readsize; 88 | 89 | curaddr -= vma; // Get relative offset of first byte 90 | while(size>0) { 91 | if (curaddr >= filesize) { 92 | if (offset == 0) // Initial address not within file 93 | break; 94 | memset(ptr+offset,0,size); // Fill out the rest of the buffer with 0 95 | return; 96 | } 97 | readsize = size; 98 | if (curaddr + readsize > filesize) // Adjust to biggest possible read 99 | readsize = filesize - curaddr; 100 | thefile->seekg(curaddr); 101 | thefile->read((char *)(ptr+offset),readsize); 102 | offset += readsize; 103 | size -= readsize; 104 | curaddr += readsize; 105 | } 106 | if (size > 0) { 107 | ostringstream errmsg; 108 | errmsg << "Unable to load " << dec << size << " bytes at " << addr.getShortcut(); 109 | addr.printRaw(errmsg); 110 | throw DataUnavailError(errmsg.str()); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /Ghidra/loadimage_bfd.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * NOTE: Interface to GNU BFD library which is GPL 3 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | // Use the GNU bfd library to manipulate a load image 18 | 19 | #ifndef __LOADIMAGE_BFD__ 20 | #define __LOADIMAGE_BFD__ 21 | 22 | #include "loadimage.hh" 23 | #include 24 | 25 | struct ImportRecord { 26 | string dllname; 27 | string funcname; 28 | int ordinal; 29 | Address address; 30 | Address thunkaddress; 31 | }; 32 | 33 | class LoadImageBfd : public LoadImage { 34 | static int4 bfdinit; // Is the library (globally) initialized 35 | string target; // File format (supported by BFD) 36 | bfd *thebfd; 37 | AddrSpace *spaceid; // We need to map space id to segments but since 38 | // we are currently ignoring segments anyway... 39 | uintb bufoffset; // Starting offset of byte buffer 40 | uint4 bufsize; // Number of bytes in the buffer 41 | uint1 *buffer; // The actual buffer 42 | mutable asymbol **symbol_table; 43 | mutable long number_of_symbols; 44 | mutable long cursymbol; 45 | mutable asection *secinfoptr; 46 | asection *findSection(uintb offset,uintb &ssize) const; // Find section containing given offset 47 | void advanceToNextSymbol(void) const; 48 | public: 49 | LoadImageBfd(const string &f,const string &t); 50 | void attachToSpace(AddrSpace *id) { spaceid = id; } 51 | void open(void); // Open any descriptors 52 | void close(void); // Close any descriptor 53 | void getImportTable(vector &irec) { throw LowlevelError("Not implemented"); } 54 | virtual ~LoadImageBfd(void); 55 | virtual void loadFill(uint1 *ptr,int4 size,const Address &addr); // Load a chunk of image 56 | virtual void openSymbols(void) const; 57 | virtual void closeSymbols(void) const; 58 | virtual bool getNextSymbol(LoadImageFunc &record) const; 59 | virtual void openSectionInfo(void) const; 60 | virtual void closeSectionInfo(void) const; 61 | virtual bool getNextSection(LoadImageSection &sec) const; 62 | virtual void getReadonly(RangeList &list) const; 63 | virtual string getArchType(void) const; 64 | virtual void adjustVma(long adjust); 65 | }; 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /Ghidra/loadimage_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "loadimage_ghidra.hh" 17 | 18 | LoadImageGhidra::LoadImageGhidra(ArchitectureGhidra *g) 19 | : LoadImage("ghidra_progam") 20 | 21 | { 22 | glb = g; 23 | } 24 | 25 | void LoadImageGhidra::open(void) 26 | 27 | { 28 | } 29 | 30 | void LoadImageGhidra::close(void) 31 | 32 | { 33 | } 34 | 35 | void LoadImageGhidra::loadFill(uint1 *ptr,int4 size,const Address &inaddr) 36 | 37 | { 38 | glb->getBytes(ptr,size,inaddr); 39 | } 40 | 41 | string LoadImageGhidra::getArchType(void) const 42 | 43 | { 44 | return "ghidra"; 45 | } 46 | 47 | void LoadImageGhidra::adjustVma(long adjust) 48 | 49 | { 50 | throw LowlevelError("Cannot adjust GHIDRA virtual memory"); 51 | } 52 | -------------------------------------------------------------------------------- /Ghidra/loadimage_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file loadimage_ghidra.hh 17 | /// \brief Use the Ghidra client as a load image 18 | #ifndef __LOADIMAGE_GHIDRA__ 19 | #define __LOADIMAGE_GHIDRA__ 20 | 21 | #include "loadimage.hh" 22 | #include "ghidra_arch.hh" 23 | 24 | class ArchitectureGhidra; 25 | 26 | /// \brief An implementation of the LoadImage interface using a Ghidra client as the back-end 27 | /// 28 | /// Requests for program bytes are marshaled to a Ghidra client which sends back the data 29 | class LoadImageGhidra : public LoadImage { 30 | ArchitectureGhidra *glb; ///< The owning Architecture and connection to the client 31 | public: 32 | LoadImageGhidra(ArchitectureGhidra *g); ///< Constructor 33 | // virtual ~LoadImage(void) {} 34 | void open(void); ///< Open any descriptors 35 | void close(void); ///< Close any descriptor 36 | virtual void loadFill(uint1 *ptr,int4 size,const Address &addr); 37 | // Read only flags are all controlled through the database interface 38 | virtual string getArchType(void) const; 39 | virtual void adjustVma(long adjust); 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /Ghidra/loadimage_xml.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file loadimage_xml.hh 17 | /// \brief Support for programs stored using an XML schema 18 | 19 | #ifndef __LOADIMAGE_XML__ 20 | #define __LOADIMAGE_XML__ 21 | 22 | #include "loadimage.hh" 23 | 24 | /// \brief Implementation of the LoadImage interface using underlying data stored in an XML format 25 | /// 26 | /// The image data is stored in an XML file in a \ file. 27 | /// The data is encoded in \ and potentially \ files. 28 | class LoadImageXml : public LoadImage { 29 | const Element *rootel; ///< The root XML element 30 | string archtype; ///< The architecture string 31 | const AddrSpaceManager *manage; ///< Manager of addresses 32 | set
readonlyset; ///< Starting address of read-only chunks 33 | map > chunk; ///< Chunks of image data, mapped by address 34 | map addrtosymbol; ///< Symbols sorted by address 35 | mutable map::const_iterator cursymbol; ///< Current symbol being reported 36 | void pad(void); ///< Make sure every chunk is followed by at least 512 bytes of pad 37 | public: 38 | LoadImageXml(const string &f,const Element *el); ///< Constructor 39 | void open(const AddrSpaceManager *m); ///< Read XML tags into the containers 40 | void clear(void); ///< Clear out all the caches 41 | void saveXml(ostream &s) const; ///< Save the image back out to an XML stream 42 | virtual ~LoadImageXml(void) { clear(); } 43 | virtual void loadFill(uint1 *ptr,int4 size,const Address &addr); 44 | virtual void openSymbols(void) const; 45 | virtual bool getNextSymbol(LoadImageFunc &record) const; 46 | virtual void getReadonly(RangeList &list) const; 47 | virtual string getArchType(void) const { return archtype; } 48 | virtual void adjustVma(long adjust); 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Ghidra/paramid.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #ifndef __CPUI_PARAMID__ 17 | #define __CPUI_PARAMID__ 18 | 19 | #include "funcdata.hh" 20 | 21 | class ParamMeasure { 22 | public: 23 | enum ParamIDIO { 24 | INPUT = 0, 25 | OUTPUT = 1 26 | }; 27 | enum ParamRank { 28 | BESTRANK = 1, 29 | DIRECTWRITEWITHOUTREAD = 1, //Output 30 | DIRECTREAD = 2, //Input. Must be same as DIRECTWRITEWITHREAD so that walkforward as part of walkbackward works 31 | // for detecting(not that DIRECTREAD is lower rank that DIRECTWRITEWITHOUTREAD) 32 | DIRECTWRITEWITHREAD = 2, //Output 33 | DIRECTWRITEUNKNOWNREAD = 3, //Output 34 | SUBFNPARAM = 4, //Input 35 | THISFNPARAM = 4, //Output 36 | SUBFNRETURN = 5, //Output 37 | THISFNRETURN = 5, //Input 38 | INDIRECT = 6, //Input or Output 39 | WORSTRANK = 7 40 | }; 41 | struct WalkState { 42 | bool best; 43 | int4 depth; 44 | ParamRank terminalrank; 45 | }; 46 | private: 47 | VarnodeData vndata; 48 | Datatype *vntype; 49 | ParamRank rank; 50 | ParamIDIO io; 51 | int4 numcalls; 52 | void walkforward( WalkState &state, PcodeOp *ignoreop, Varnode *vn ); 53 | void walkbackward( WalkState &state, PcodeOp *ignoreop,Varnode *vn ); 54 | void updaterank( ParamRank rank_in,bool best ) { rank = (best==true) ? min( rank, rank_in ) : max( rank, rank_in ); } 55 | public: 56 | ParamMeasure( const Address &addr, int4 sz, Datatype *dt, ParamIDIO io_in) { 57 | vndata.space=addr.getSpace(); vndata.offset=addr.getOffset(); vndata.size = sz; vntype=dt; io = io_in; rank=WORSTRANK; } 58 | void calculateRank(bool best,Varnode *basevn,PcodeOp *ignoreop); 59 | void saveXml( ostream &s,string tag,bool moredetail ) const; 60 | void savePretty( ostream &s,bool moredetail ) const; 61 | int4 getMeasure(void) const { return (int4) rank; } 62 | }; 63 | 64 | class ParamIDAnalysis 65 | { 66 | Funcdata *fd; 67 | list InputParamMeasures; 68 | list OutputParamMeasures; 69 | public: 70 | ParamIDAnalysis( Funcdata *fd_in, bool justproto ); 71 | void saveXml( ostream &s, bool moredetail ) const; 72 | void savePretty( ostream &s, bool moredetail ) const; 73 | }; 74 | 75 | #endif //ifndef __CPUI_PARAMID__ 76 | -------------------------------------------------------------------------------- /Ghidra/pcodeparse.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #ifndef __PCODE_SNIPPET__ 17 | #define __PCODE_SNIPPET__ 18 | 19 | #include "pcodecompile.hh" 20 | #include "sleighbase.hh" 21 | 22 | // Classes for compiling a standalone snippet of pcode, given an existing sleigh language 23 | 24 | struct IdentRec { 25 | const char *nm; 26 | int4 id; 27 | }; 28 | 29 | class PcodeLexer { 30 | public: 31 | enum { // Lexer states 32 | start, 33 | special2, // Middle of special 2 character operator 34 | special3, // First character of special 3 character operator 35 | special32, // Second character of special 3 character operator 36 | comment, // Middle of an endofline comment 37 | punctuation, // Punctuation character 38 | identifier, // Middle of an identifier 39 | hexstring, // Middle of a hexidecimal number 40 | decstring, // Middle of a decimal number 41 | endstream, // Reached end of stream 42 | illegal // Scanned an illegal character 43 | }; 44 | private: 45 | static const IdentRec idents[]; 46 | int4 curstate; 47 | char curchar,lookahead1,lookahead2; 48 | char curtoken[256]; 49 | int4 tokpos; 50 | bool endofstream; 51 | bool endofstreamsent; 52 | istream *s; 53 | string curidentifier; 54 | uintb curnum; 55 | void starttoken(void) { curtoken[0] = curchar; tokpos = 1; } 56 | void advancetoken(void) { curtoken[tokpos++] = curchar; } 57 | bool isIdent(char c) const { return (isalnum(c)||(c=='_')||(c=='.')); } 58 | bool isHex(char c) const { return isxdigit(c); } 59 | bool isDec(char c) const { return isdigit(c); } 60 | int4 findIdentifier(const string &str) const; 61 | int4 moveState(void); 62 | public: 63 | PcodeLexer(void) { s = (istream *)0; } 64 | void initialize(istream *t); 65 | int4 getNextToken(void); 66 | const string &getIdentifier(void) const { return curidentifier; } 67 | uintb getNumber(void) const { return curnum; } 68 | }; 69 | 70 | class PcodeSnippet : public PcodeCompile { 71 | PcodeLexer lexer; 72 | const SleighBase *sleigh; // Language from which we get symbols 73 | SymbolTree tree; // Symbols in the local scope of the snippet (temporaries) 74 | uintb tempbase; 75 | int4 errorcount; 76 | string firsterror; 77 | ConstructTpl *result; 78 | virtual uintb allocateTemp(void); 79 | virtual void addSymbol(SleighSymbol *sym); 80 | public: 81 | PcodeSnippet(const SleighBase *slgh); 82 | void setResult(ConstructTpl *res) { result = res; } 83 | ConstructTpl *releaseResult(void) { ConstructTpl *res = result; result = (ConstructTpl *)0; return res; } 84 | virtual ~PcodeSnippet(void); 85 | virtual const Location *getLocation(SleighSymbol *sym) const { return (const Location *)0; } 86 | virtual void reportError(const Location *loc, const string &msg); 87 | virtual void reportWarning(const Location *loc, const string &msg) {} 88 | bool hasErrors(void) const { return (errorcount != 0); } 89 | const string getErrorMessage(void) const { return firsterror; } 90 | void setUniqueBase(uintb val) { tempbase = val; } 91 | uintb getUniqueBase(void) const { return tempbase; } 92 | void clear(void); 93 | int lex(void); 94 | bool parseStream(istream& s); 95 | void addOperand(const string &name,int4 index); 96 | }; 97 | 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /Ghidra/pcoderaw.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "pcoderaw.hh" 17 | #include "translate.hh" 18 | 19 | /// Build this VarnodeData from an \b \ tag 20 | /// \param el is the parsed tag 21 | /// \param manage is the address space manager 22 | void VarnodeData::restoreXml(const Element *el,const AddrSpaceManager *manage) 23 | 24 | { 25 | space = (AddrSpace *)0; 26 | size = 0; 27 | int4 num = el->getNumAttributes(); 28 | for(int4 i=0;igetAttributeName(i)=="space") { 30 | space = manage->getSpaceByName(el->getAttributeValue(i)); 31 | if (space == (AddrSpace *)0) 32 | throw LowlevelError("Unknown space name: "+el->getAttributeValue(i)); 33 | offset = space->restoreXmlAttributes(el,size); 34 | return; 35 | } 36 | else if (el->getAttributeName(i)=="name") { 37 | const Translate *trans = manage->getDefaultCodeSpace()->getTrans(); 38 | const VarnodeData &point(trans->getRegister(el->getAttributeValue(i))); 39 | *this = point; 40 | return; 41 | } 42 | } 43 | } 44 | 45 | /// Return \b true, if \b this, as an address range, contains the other address range 46 | /// \param op2 is the other VarnodeData to test for containment 47 | /// \return \b true if \b this contains the other 48 | bool VarnodeData::contains(const VarnodeData &op2) const 49 | 50 | { 51 | if (space != op2.space) return false; 52 | if (op2.offset < offset) return false; 53 | if ((offset + (size-1)) < (op2.offset + (op2.size-1))) return false; 54 | return true; 55 | } 56 | -------------------------------------------------------------------------------- /Ghidra/prefersplit.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * REVIEWED: YES 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #ifndef __PREFERSPLIT__ 18 | #define __PREFERSPLIT__ 19 | 20 | #include "varnode.hh" 21 | class Funcdata; // Forward declaration 22 | 23 | struct PreferSplitRecord { 24 | VarnodeData storage; 25 | int4 splitoffset; // Number of initial bytes (in address order) to split into first piece 26 | bool operator<(const PreferSplitRecord &op2) const; 27 | }; 28 | 29 | class PreferSplitManager { 30 | class SplitInstance { 31 | friend class PreferSplitManager; 32 | int4 splitoffset; 33 | Varnode *vn; 34 | Varnode *hi; // Most significant piece 35 | Varnode *lo; // Least significant piece 36 | public: 37 | SplitInstance(Varnode *v,int4 off) { vn = v; splitoffset = off; hi = (Varnode *)0; lo = (Varnode *)0; } 38 | }; 39 | Funcdata *data; 40 | const vector *records; 41 | vector tempsplits; // Copies of temporaries that need additional splitting 42 | void fillinInstance(SplitInstance *inst,bool bigendian,bool sethi,bool setlo); 43 | void createCopyOps(SplitInstance *ininst,SplitInstance *outinst,PcodeOp *op,bool istemp); 44 | bool testDefiningCopy(SplitInstance *inst,PcodeOp *def,bool &istemp); 45 | void splitDefiningCopy(SplitInstance *inst,PcodeOp *def,bool istemp); 46 | bool testReadingCopy(SplitInstance *inst,PcodeOp *readop,bool &istemp); 47 | void splitReadingCopy(SplitInstance *inst,PcodeOp *readop,bool istemp); 48 | bool testZext(SplitInstance *inst,PcodeOp *op); 49 | void splitZext(SplitInstance *inst,PcodeOp *op); 50 | bool testPiece(SplitInstance *inst,PcodeOp *op); 51 | void splitPiece(SplitInstance *inst,PcodeOp *op); 52 | bool testSubpiece(SplitInstance *inst,PcodeOp *op); 53 | void splitSubpiece(SplitInstance *inst,PcodeOp *op); 54 | bool testLoad(SplitInstance *inst,PcodeOp *op); 55 | void splitLoad(SplitInstance *inst,PcodeOp *op); 56 | bool testStore(SplitInstance *inst,PcodeOp *op); 57 | void splitStore(SplitInstance *inst,PcodeOp *op); 58 | bool splitVarnode(SplitInstance *inst); 59 | void splitRecord(const PreferSplitRecord &rec); 60 | bool testTemporary(SplitInstance *inst); 61 | void splitTemporary(SplitInstance *inst); 62 | public: 63 | void init(Funcdata *fd,const vector *rec); 64 | const PreferSplitRecord *findRecord(Varnode *vn) const; 65 | static void initialize(vector &records); 66 | void split(void); 67 | void splitAdditional(void); 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /Ghidra/printjava.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file printjava.hh 17 | /// \brief Classes supporting the java-language back-end to the decompiler 18 | 19 | #ifndef __PRINTJAVA__ 20 | #define __PRINTJAVA__ 21 | 22 | #include "printc.hh" 23 | 24 | /// \brief Factory and static initializer for the "java-language" back-end to the decompiler 25 | /// 26 | /// The singleton adds itself to the list of possible back-end languages for the decompiler 27 | /// and it acts as a factory for producing the PrintJava object for emitting java-language tokens. 28 | class PrintJavaCapability : public PrintLanguageCapability { 29 | static PrintJavaCapability printJavaCapability; ///< The singleton instance 30 | PrintJavaCapability(void); ///< Singleton constructor 31 | PrintJavaCapability(const PrintJavaCapability &op2); ///< Not implemented 32 | PrintJavaCapability &operator=(const PrintJavaCapability &op); ///< Not implemented 33 | public: 34 | virtual PrintLanguage *buildLanguage(Architecture *glb); 35 | }; 36 | 37 | /// \brief The java-language token emitter 38 | /// 39 | /// This builds heavily on the c-language PrintC emitter. Most operator tokens, the format of 40 | /// function prototypes, and code structuring are shared. Specifics of the java constant pool are handled 41 | /// through the overloaded opCpoolRefOp(). 42 | /// 43 | /// Java data-types are mapped into the decompiler's data-type system in a specific way. The primitives 44 | /// \b int, \b long, \b short, \b byte, \b boolean, \b float, and \b double all map directly. The 45 | /// \b char primitive is treated as a 2 byte unsigned integer. A TypeStruct object holds the field 46 | /// layout for a java class, then java objects get mapped as follows: 47 | /// - Class reference = pointer to TYPE_UINT 48 | /// - Array of \b int, \b long, \b short, or \b byte = pointer to TYPE_INT 49 | /// - Array of \b float or \b double = pointer to TYPE_FLOAT 50 | /// - Array of \b boolean = pointer to TYPE_BOOL 51 | /// - Array of class objects = pointer to TYPE_PTR 52 | /// 53 | /// There are some adjustments to the printing of data-types and LOAD/STORE expressions 54 | /// to account for this mapping. 55 | class PrintJava : public PrintC { 56 | static OpToken instanceof; ///< The \b instanceof keyword 57 | static bool isArrayType(const Datatype *ct); ///< Does the given data-type reference a java array 58 | static bool needZeroArray(const Varnode *vn); ///< Do we need '[0]' syntax. 59 | void resetDefaultsPrintJava(void); ///< Set options that are specific to Java 60 | virtual void printUnicode(ostream &s,int4 onechar) const; 61 | public: 62 | PrintJava(Architecture *g,const string &nm="java-language"); ///< Constructor 63 | virtual void resetDefaults(void); 64 | virtual void docFunction(const Funcdata *fd); 65 | virtual void pushTypeStart(const Datatype *ct,bool noident); 66 | virtual void pushTypeEnd(const Datatype *ct); 67 | virtual bool doEmitWideCharPrefix(void) const { return false; } 68 | virtual void adjustTypeOperators(void); 69 | virtual void opLoad(const PcodeOp *op); 70 | virtual void opStore(const PcodeOp *op); 71 | virtual void opCallind(const PcodeOp *op); 72 | virtual void opCpoolRefOp(const PcodeOp *op); 73 | }; 74 | #endif 75 | -------------------------------------------------------------------------------- /Ghidra/raw_arch.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "raw_arch.hh" 17 | 18 | // Constructing this object registers the capability 19 | RawBinaryArchitectureCapability RawBinaryArchitectureCapability::rawBinaryArchitectureCapability; 20 | 21 | RawBinaryArchitectureCapability::RawBinaryArchitectureCapability(void) 22 | 23 | { 24 | name = "raw"; 25 | } 26 | 27 | RawBinaryArchitectureCapability::~RawBinaryArchitectureCapability(void) 28 | 29 | { 30 | SleighArchitecture::shutdown(); 31 | } 32 | 33 | Architecture *RawBinaryArchitectureCapability::buildArchitecture(const string &filename,const string &target,ostream *estream) 34 | 35 | { 36 | return new RawBinaryArchitecture(filename,target,estream); 37 | } 38 | 39 | bool RawBinaryArchitectureCapability::isFileMatch(const string &filename) const 40 | 41 | { 42 | return true; // File can always be opened as raw binary 43 | } 44 | 45 | bool RawBinaryArchitectureCapability::isXmlMatch(Document *doc) const 46 | 47 | { 48 | return (doc->getRoot()->getName() == "raw_savefile"); 49 | } 50 | 51 | void RawBinaryArchitecture::buildLoader(DocumentStorage &store) 52 | 53 | { 54 | RawLoadImage *ldr; 55 | 56 | collectSpecFiles(*errorstream); 57 | ldr = new RawLoadImage(getFilename()); 58 | ldr->open(); 59 | if (adjustvma != 0) 60 | ldr->adjustVma(adjustvma); 61 | loader = ldr; 62 | } 63 | 64 | void RawBinaryArchitecture::resolveArchitecture(void) 65 | 66 | { 67 | archid = getTarget(); // Nothing to derive from the image itself, we just copy in the passed in target 68 | SleighArchitecture::resolveArchitecture(); 69 | } 70 | 71 | void RawBinaryArchitecture::postSpecFile(void) 72 | 73 | { 74 | Architecture::postSpecFile(); 75 | ((RawLoadImage *)loader)->attachToSpace(getDefaultCodeSpace()); // Attach default space to loader 76 | } 77 | 78 | RawBinaryArchitecture::RawBinaryArchitecture(const string &fname,const string &targ,ostream *estream) 79 | : SleighArchitecture(fname,targ,estream) 80 | { 81 | adjustvma = 0; 82 | } 83 | 84 | void RawBinaryArchitecture::saveXml(ostream &s) const 85 | 86 | { 87 | s << "\n"; 91 | types->saveXmlCoreTypes(s); 92 | SleighArchitecture::saveXml(s); 93 | s << "\n"; 94 | } 95 | 96 | void RawBinaryArchitecture::restoreXml(DocumentStorage &store) 97 | 98 | { 99 | const Element *el = store.getTag("raw_savefile"); 100 | if (el == (const Element *)0) 101 | throw LowlevelError("Could not find raw_savefile tag"); 102 | 103 | restoreXmlHeader(el); 104 | { 105 | istringstream s( el->getAttributeValue("adjustvma")); 106 | s.unsetf(ios::dec | ios::hex | ios::oct); 107 | s >> adjustvma; 108 | } 109 | const List &list(el->getChildren()); 110 | List::const_iterator iter; 111 | 112 | iter = list.begin(); 113 | if (iter != list.end()) { 114 | if ((*iter)->getName() == "coretypes") { 115 | store.registerTag(*iter); 116 | ++iter; 117 | } 118 | } 119 | init(store); // Load the image and configure 120 | 121 | if (iter != list.end()) { 122 | store.registerTag(*iter); 123 | SleighArchitecture::restoreXml(store); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /Ghidra/raw_arch.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file raw_arch.hh 17 | /// \brief Bare bones capability for treating a file as a raw executable image 18 | #include "sleigh_arch.hh" 19 | #include "loadimage.hh" 20 | 21 | /// \brief Extension point for building an Architecture that reads in raw images 22 | class RawBinaryArchitectureCapability : public ArchitectureCapability { 23 | static RawBinaryArchitectureCapability rawBinaryArchitectureCapability; ///< The singleton instance 24 | RawBinaryArchitectureCapability(void); ///< Singleton 25 | RawBinaryArchitectureCapability(const RawBinaryArchitectureCapability &op2); ///< Not implemented 26 | RawBinaryArchitectureCapability &operator=(const RawBinaryArchitectureCapability &op2); ///< Not implemented 27 | public: 28 | virtual ~RawBinaryArchitectureCapability(void); 29 | virtual Architecture *buildArchitecture(const string &filename,const string &target,ostream *estream); 30 | virtual bool isFileMatch(const string &filename) const; 31 | virtual bool isXmlMatch(Document *doc) const; 32 | }; 33 | 34 | /// \brief Architecture that reads its binary as a raw file 35 | class RawBinaryArchitecture : public SleighArchitecture { 36 | long adjustvma; ///< What address byte 0 of the raw file gets treated as 37 | virtual void buildLoader(DocumentStorage &store); 38 | virtual void resolveArchitecture(void); 39 | virtual void postSpecFile(void); 40 | public: 41 | virtual void saveXml(ostream &s) const; 42 | virtual void restoreXml(DocumentStorage &store); 43 | RawBinaryArchitecture(const string &fname,const string &targ,ostream *estream); ///< Constructor 44 | virtual ~RawBinaryArchitecture(void) {} 45 | }; 46 | 47 | -------------------------------------------------------------------------------- /Ghidra/string_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "string_ghidra.hh" 17 | 18 | GhidraStringManager::GhidraStringManager(ArchitectureGhidra *g,int4 max) 19 | : StringManager(max) 20 | { 21 | glb = g; 22 | testBuffer = new uint1[max]; 23 | } 24 | 25 | GhidraStringManager::~GhidraStringManager(void) 26 | 27 | { 28 | delete [] testBuffer; 29 | } 30 | 31 | const vector &GhidraStringManager::getStringData(const Address &addr,Datatype *charType,bool &isTrunc) 32 | 33 | { 34 | map::iterator iter; 35 | iter = stringMap.find(addr); 36 | if (iter != stringMap.end()) { 37 | isTrunc = (*iter).second.isTruncated; 38 | return (*iter).second.byteData; 39 | } 40 | 41 | StringData &stringData(stringMap[addr]); 42 | stringData.isTruncated = false; 43 | glb->getStringData(stringData.byteData, addr, charType, maximumChars,stringData.isTruncated); 44 | isTrunc = stringData.isTruncated; 45 | return stringData.byteData; 46 | } 47 | -------------------------------------------------------------------------------- /Ghidra/string_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file string_ghidra.hh 17 | /// \brief Implementation of the StringManager through the ghidra client 18 | 19 | #ifndef __STRING_GHIDRA__ 20 | #define __STRING_GHIDRA__ 21 | 22 | #include "ghidra_arch.hh" 23 | 24 | /// \brief Implementation of the StringManager that queries through the ghidra client 25 | /// 26 | /// This acts as a front end to Ghidra's string formats and encodings. 27 | /// The client translates any type of string into a UTF8 representation, and this 28 | /// class stores it for final presentation. Escaping the UTF8 string is left up 29 | /// to the PrintLanguage. 30 | class GhidraStringManager : public StringManager { 31 | ArchitectureGhidra *glb; ///< The ghidra client interface 32 | uint1 *testBuffer; ///< Temporary storage for storing bytes from client 33 | public: 34 | GhidraStringManager(ArchitectureGhidra *g,int4 max); ///< Constructor 35 | virtual ~GhidraStringManager(void); 36 | virtual const vector &getStringData(const Address &addr,Datatype *charType,bool &isTrunc); 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Ghidra/typegrp_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "typegrp_ghidra.hh" 17 | 18 | Datatype *TypeFactoryGhidra::findById(const string &n,uint8 id,int4 sz) 19 | 20 | { 21 | Datatype *ct = TypeFactory::findById(n,id,sz); // Try internal find 22 | if (ct != (Datatype *)0) return ct; 23 | 24 | Document *doc; 25 | try { 26 | doc = ((ArchitectureGhidra *)glb)->getType(n,id); // See if ghidra knows about type 27 | } 28 | catch(XmlError &err) { 29 | throw LowlevelError("XML error: "+err.explain); 30 | } 31 | if (doc == (Document *)0) return (Datatype *)0; 32 | ct = restoreXmlType(doc->getRoot()); // Parse ghidra's type 33 | delete doc; 34 | return ct; 35 | } 36 | -------------------------------------------------------------------------------- /Ghidra/typegrp_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file typegrp_ghidra.hh 17 | /// \brief Interface for requesting data-type information from a Ghidra client 18 | 19 | #ifndef __TYPEGRP_GHIDRA__ 20 | #define __TYPEGRP_GHIDRA__ 21 | 22 | #include "type.hh" 23 | #include "ghidra_arch.hh" 24 | 25 | /// \brief An implementation of the TypeFactory interface, query a Ghidra client for data-type information 26 | /// 27 | /// Requests for a specific data-type name and id are marshaled to the Ghidra client, 28 | /// which sends back a description of the data-type. The description is parsed and 29 | /// converted into a Datatype object and cached in this object. 30 | class TypeFactoryGhidra : public TypeFactory { 31 | protected: 32 | virtual Datatype *findById(const string &n,uint8 id,int4 sz); 33 | public: 34 | TypeFactoryGhidra(ArchitectureGhidra *g) : TypeFactory(g) {} ///< Constructor 35 | virtual ~TypeFactoryGhidra(void) {} 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Ghidra/types.h: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * NOTE: Decompiler specific flags, refers to sparc,linux,windows,i386,apple,alpha,powerpc 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | /* typedefs for getting specific word sizes */ 18 | 19 | #ifndef __MYTYPES__ 20 | #define __MYTYPES__ 21 | 22 | #include 23 | 24 | // Use of uintm and intm is deprecated. They must currently be set to be 32-bit. 25 | typedef uint32_t uintm; 26 | typedef int32_t intm; 27 | 28 | typedef uint64_t uint8; 29 | typedef int64_t int8; 30 | typedef uint32_t uint4; 31 | typedef int32_t int4; 32 | typedef uint16_t uint2; 33 | typedef int16_t int2; 34 | typedef uint8_t uint1; 35 | typedef int8_t int1; 36 | 37 | /* uintp is intended to be an unsigned integer that is the same size as a pointer */ 38 | typedef uintptr_t uintp; 39 | 40 | #if defined (__x86_64__) || defined (__i386__) 41 | #define HOST_ENDIAN 0 42 | 43 | #else // other platforms (not compatible with g++ 4.8.5) 44 | class Endian { 45 | public: 46 | static constexpr const union { int4 whole; int1 part[4]; } host = { 1 }; 47 | }; 48 | #define HOST_ENDIAN Endian::host.part[3] 49 | #endif 50 | 51 | #if defined(_WINDOWS) 52 | #pragma warning (disable:4312) 53 | #pragma warning (disable:4311) 54 | #pragma warning (disable:4267) 55 | #pragma warning (disable:4018) 56 | #pragma warning (disable:4244) 57 | 58 | /* 59 | The windows standard template library list implementation seems to have a philosophical difference with 60 | the standard regarding the validity of iterators pointing to objects that are moved between containers 61 | (via the splice method) These defines turn off the validity checks 62 | (These have been moved to the VC project spec) 63 | */ 64 | //#define _SECURE_SCL 0 65 | //#define _HAS_ITERATOR_DEBUGGING 0 66 | #endif 67 | 68 | /* 69 | Big integers: These are intended to be arbitrary precison integers. However 70 | for efficiency, these are currently implemented as fixed precision. 71 | So for coding purposes, these should be interpreted as fixed 72 | precision integers that store as big a number as you would ever need. 73 | */ 74 | 75 | typedef int8 intb; /* This is a signed big integer */ 76 | typedef uint8 uintb; /* This is an unsigned big integer */ 77 | 78 | /* 79 | 80 | Other compilation flags 81 | 82 | CPUI_DEBUG -- This is the ONE debug switch that should be passed in 83 | from the compiler, all others are controlled below 84 | */ 85 | 86 | #ifdef CPUI_DEBUG 87 | # define OPACTION_DEBUG 88 | # define PRETTY_DEBUG 89 | //# define __REMOTE_SOCKET__ 90 | //# define TYPEPROP_DEBUG 91 | //# define DFSVERIFY_DEBUG 92 | //# define BLOCKCONSISTENT_DEBUG 93 | //# define MERGEMULTI_DEBUG 94 | //# define VARBANK_DEBUG 95 | #endif 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /Ghidra/xml.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_XML_XML_TAB_H_INCLUDED 34 | # define YY_XML_XML_TAB_H_INCLUDED 35 | /* Enabling traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int xmldebug; 41 | #endif 42 | 43 | /* Tokens. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | /* Put the tokens into the symbol table, so that GDB and other debuggers 47 | know about them. */ 48 | enum yytokentype { 49 | CHARDATA = 258, 50 | CDATA = 259, 51 | ATTVALUE = 260, 52 | COMMENT = 261, 53 | CHARREF = 262, 54 | NAME = 263, 55 | SNAME = 264, 56 | ELEMBRACE = 265, 57 | COMMBRACE = 266 58 | }; 59 | #endif 60 | 61 | 62 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 63 | typedef union YYSTYPE 64 | { 65 | /* Line 2058 of yacc.c */ 66 | #line 119 "xml.y" 67 | 68 | int4 i; 69 | string *str; 70 | Attributes *attr; 71 | NameValue *pair; 72 | 73 | 74 | /* Line 2058 of yacc.c */ 75 | #line 76 "xml.tab.h" 76 | } YYSTYPE; 77 | # define YYSTYPE_IS_TRIVIAL 1 78 | # define yystype YYSTYPE /* obsolescent; will be withdrawn */ 79 | # define YYSTYPE_IS_DECLARED 1 80 | #endif 81 | 82 | extern YYSTYPE xmllval; 83 | 84 | #ifdef YYPARSE_PARAM 85 | #if defined __STDC__ || defined __cplusplus 86 | int xmlparse (void *YYPARSE_PARAM); 87 | #else 88 | int xmlparse (); 89 | #endif 90 | #else /* ! YYPARSE_PARAM */ 91 | #if defined __STDC__ || defined __cplusplus 92 | int xmlparse (void); 93 | #else 94 | int xmlparse (); 95 | #endif 96 | #endif /* ! YYPARSE_PARAM */ 97 | 98 | #endif /* !YY_XML_XML_TAB_H_INCLUDED */ 99 | -------------------------------------------------------------------------------- /Ghidra/xml_arch.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file xml_arch.hh 17 | /// \brief Extension to read executables based on an XML format 18 | #include "sleigh_arch.hh" 19 | #include "loadimage_xml.hh" 20 | 21 | /// \brief Extension for building an XML format capable Architecture 22 | class XmlArchitectureCapability : public ArchitectureCapability { 23 | static XmlArchitectureCapability xmlArchitectureCapability; ///< The singleton instance 24 | XmlArchitectureCapability(void); ///< Singleton 25 | XmlArchitectureCapability(const XmlArchitectureCapability &op2); ///< Not implemented 26 | XmlArchitectureCapability &operator=(const XmlArchitectureCapability &op2); ///< Not implemented 27 | public: 28 | virtual ~XmlArchitectureCapability(void); 29 | virtual Architecture *buildArchitecture(const string &filename,const string &target,ostream *estream); 30 | virtual bool isFileMatch(const string &filename) const; 31 | virtual bool isXmlMatch(Document *doc) const; 32 | }; 33 | 34 | /// \brief An Architecture that loads executables using an XML format 35 | class XmlArchitecture : public SleighArchitecture { 36 | long adjustvma; ///< The amount to adjust the virtual memory address 37 | virtual void buildLoader(DocumentStorage &store); 38 | // virtual void resolveArchitecture(void); ///< Inherit SleighArchitecture's version 39 | virtual void postSpecFile(void); 40 | public: 41 | virtual void saveXml(ostream &s) const; 42 | virtual void restoreXml(DocumentStorage &store); 43 | XmlArchitecture(const string &fname,const string &targ,ostream *estream); ///< Constructor 44 | virtual ~XmlArchitecture(void) {} 45 | }; 46 | -------------------------------------------------------------------------------- /Ghidra9/bfd_arch.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * EXCLUDE: YES 4 | * NOTE: Interface to GNU BFD library which is GPL 3 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /// \file bfd_arch.hh 19 | /// \brief Specific implementation of Architecture using GNU BFD libraries 20 | 21 | #include "sleigh_arch.hh" 22 | #include "loadimage_bfd.hh" 23 | 24 | /// \brief Extension point for building a GNU BFD capable Architecture 25 | class BfdArchitectureCapability : public ArchitectureCapability { 26 | static BfdArchitectureCapability bfdArchitectureCapability; ///< The singleton instance 27 | BfdArchitectureCapability(void); ///< Singleton constructor 28 | BfdArchitectureCapability(const BfdArchitectureCapability &op2); ///< Not implemented 29 | BfdArchitectureCapability &operator=(const BfdArchitectureCapability &op2); ///< Not implemented 30 | public: 31 | virtual ~BfdArchitectureCapability(void); 32 | virtual Architecture *buildArchitecture(const string &filename,const string &target,ostream *estream); 33 | virtual bool isFileMatch(const string &filename) const; 34 | virtual bool isXmlMatch(Document *doc) const; 35 | }; 36 | 37 | /// \brief Architecture that reads executable files using GNU BFD libraries 38 | class BfdArchitecture : public SleighArchitecture { 39 | long adjustvma; ///< How much to adjust the virtual memory address 40 | virtual void buildLoader(DocumentStorage &store); 41 | virtual void resolveArchitecture(void); 42 | virtual void postSpecFile(void); 43 | public: 44 | virtual void saveXml(ostream &s) const; 45 | virtual void restoreXml(DocumentStorage &store); 46 | BfdArchitecture(const string &fname,const string &targ,ostream *estream); ///< Constructor 47 | virtual ~BfdArchitecture(void) {} 48 | }; 49 | -------------------------------------------------------------------------------- /Ghidra9/capability.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "capability.hh" 17 | 18 | /// Access static vector of CapabilityPoint objects that are registered during static initialization 19 | /// The list itself is created once on the first call to this method 20 | /// \e after all the static initializers have run 21 | /// \return the list of registered extensions 22 | vector &CapabilityPoint::getList(void) 23 | 24 | { 25 | static vector thelist; // This gets allocated exactly once on first call 26 | return thelist; 27 | } 28 | 29 | /// Constructing the object automatically registers it. 30 | /// For global instances, this happens during static initialization 31 | CapabilityPoint::CapabilityPoint(void) 32 | 33 | { 34 | getList().push_back(this); 35 | } 36 | 37 | /// Give all registered capabilities a chance to initialize (\e after all static initialization has happened) 38 | void CapabilityPoint::initializeAll(void) 39 | 40 | { 41 | vector &list( getList() ); 42 | for(int4 i=0;iinitialize(); 45 | } 46 | list.clear(); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Ghidra9/capability.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file capability.hh 17 | /// \brief Infrastructure for discovering code extensions to the decompiler 18 | #ifndef __CAPABILITY__ 19 | #define __CAPABILITY__ 20 | 21 | #include "types.h" 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | /// \brief Class for automatically registering extension points to the decompiler 28 | /// 29 | /// This uses the C++ static initializer feature to automatically \e discover 30 | /// and register extension point. Code for an extension should provide 31 | /// a class that derives from CapabilityPoint and overrides the initialize() method. 32 | /// Additionally there should be a singleton static instantiation of this extension class. 33 | /// The extensions are accumulated in a list automatically, then the decompiler engine 34 | /// will ensure that the initialize() method is called on each extension, allowing it 35 | /// to complete its integration. 36 | class CapabilityPoint { 37 | static vector &getList(void); ///< Retrieve the list of extension point singletons 38 | protected: 39 | CapabilityPoint(void); ///< Construct extension capability exactly once 40 | public: 41 | virtual ~CapabilityPoint(void) {} ///< Destructor 42 | 43 | /// \brief Complete initialization of an extension point 44 | /// 45 | /// This method is implemented by each extension so it can do specialized integration 46 | virtual void initialize(void)=0; 47 | 48 | static void initializeAll(void); ///< Finish initialization for all extension points 49 | }; 50 | 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Ghidra9/comment_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "comment_ghidra.hh" 17 | 18 | CommentDatabaseGhidra::CommentDatabaseGhidra(ArchitectureGhidra *g) 19 | : CommentDatabase() 20 | { 21 | ghidra = g; 22 | cachefilled = false; 23 | } 24 | 25 | /// Fetch all comments for the function in one chunk. Deserialize them and 26 | /// store the Comment objects in the cache 27 | /// \param fad is the address of the given function 28 | void CommentDatabaseGhidra::fillCache(const Address &fad) const 29 | 30 | { 31 | Document *doc; 32 | uint4 commentfilter; 33 | 34 | if (cachefilled) return; // Already queried ghidra 35 | cachefilled = true; 36 | // Gather which types of comments are being printed currently 37 | commentfilter = ghidra->print->getHeaderComment(); 38 | commentfilter |= ghidra->print->getInstructionComment(); 39 | if (commentfilter==0) return; 40 | CommentSet::const_iterator iter,iterend; 41 | iter = cache.beginComment(fad); 42 | iterend = cache.endComment(fad); 43 | 44 | doc = ghidra->getComments(fad,commentfilter); 45 | if (doc != (Document *)0) { 46 | cache.restoreXml(doc->getRoot(),ghidra); 47 | delete doc; 48 | } 49 | } 50 | 51 | /// For the Ghidra implementation of CommentDatabase, addComment() is currently only 52 | /// called by the warning routines which generates the 53 | /// \e warning and \e warningheader comment types. Neither of 54 | /// these types is intended to be a permanent comment in the 55 | /// database, so we only add the comment to the cache 56 | void CommentDatabaseGhidra::addComment(uint4 tp, 57 | const Address &fad, 58 | const Address &ad, 59 | const string &txt) 60 | { 61 | cache.addComment(tp,fad,ad,txt); 62 | } 63 | 64 | bool CommentDatabaseGhidra::addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad, 65 | const string &txt) 66 | { 67 | return cache.addCommentNoDuplicate(tp,fad,ad,txt); 68 | } 69 | 70 | CommentSet::const_iterator CommentDatabaseGhidra::beginComment(const Address &fad) const 71 | 72 | { 73 | fillCache(fad); 74 | return cache.beginComment(fad); 75 | } 76 | 77 | CommentSet::const_iterator CommentDatabaseGhidra::endComment(const Address &fad) const 78 | 79 | { 80 | return cache.endComment(fad); 81 | } 82 | 83 | -------------------------------------------------------------------------------- /Ghidra9/comment_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file comment_ghidra.hh 17 | /// \brief Obtain comments by talking to a Ghidra client 18 | #ifndef __COMMENT_GHIDRA__ 19 | #define __COMMENT_GHIDRA__ 20 | 21 | #include "comment.hh" 22 | #include "ghidra_arch.hh" 23 | 24 | /// \brief An implementation of CommentDatabase backed by a Ghidra client 25 | /// 26 | /// Comment information about particular functions is obtained by querying 27 | /// a Ghidra client. All comments for a single function are queried at once, and 28 | /// results are cached in this object. The cache needs to be cleared between 29 | /// functions using the clear() method. 30 | class CommentDatabaseGhidra : public CommentDatabase { 31 | ArchitectureGhidra *ghidra; ///< The Architecture and connection to the Ghidra client 32 | mutable CommentDatabaseInternal cache; ///< A cache of Comment objects received from the Ghidra client 33 | mutable bool cachefilled; ///< Set to \b true if comments for the current function have been fetched 34 | void fillCache(const Address &fad) const; ///< Fetch comments for the given function 35 | public: 36 | CommentDatabaseGhidra(ArchitectureGhidra *g); ///< Constructor 37 | virtual void clear(void) { cache.clear(); cachefilled=false; } 38 | virtual void clearType(const Address &fad,uint4 tp) { 39 | cache.clearType(fad,tp); 40 | } 41 | virtual void addComment(uint4 tp,const Address &fad, 42 | const Address &ad,const string &txt); 43 | virtual bool addCommentNoDuplicate(uint4 tp,const Address &fad,const Address &ad,const string &txt); 44 | virtual void deleteComment(Comment *com) { 45 | throw LowlevelError("deleteComment unimplemented"); } 46 | virtual CommentSet::const_iterator beginComment(const Address &fad) const; 47 | virtual CommentSet::const_iterator endComment(const Address &fad) const; 48 | virtual void saveXml(ostream &s) const { 49 | throw LowlevelError("commentdb::saveXml unimplemented"); } 50 | virtual void restoreXml(const Element *el,const AddrSpaceManager *trans) { 51 | throw LowlevelError("commentdb::restoreXml unimplemented"); } 52 | }; 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /Ghidra9/cpool_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "cpool_ghidra.hh" 17 | 18 | ConstantPoolGhidra::ConstantPoolGhidra(ArchitectureGhidra *g) 19 | 20 | { 21 | ghidra = g; 22 | } 23 | 24 | CPoolRecord *ConstantPoolGhidra::createRecord(const vector &refs) 25 | 26 | { 27 | throw LowlevelError("Cannot access constant pool with this method"); 28 | } 29 | 30 | const CPoolRecord *ConstantPoolGhidra::getRecord(const vector &refs) const 31 | 32 | { 33 | const CPoolRecord *rec = cache.getRecord(refs); 34 | if (rec == (const CPoolRecord *)0) { 35 | Document *doc; 36 | try { 37 | doc = ghidra->getCPoolRef(refs); 38 | } 39 | catch(JavaError &err) { 40 | throw LowlevelError("Error fetching constant pool record: " + err.explain); 41 | } 42 | catch(XmlError &err) { 43 | throw LowlevelError("Error in constant pool record xml: "+err.explain); 44 | } 45 | if (doc == (Document *)0) { 46 | ostringstream s; 47 | s << "Could not retrieve constant pool record for reference: 0x" << refs[0]; 48 | throw LowlevelError(s.str()); 49 | } 50 | rec = cache.restoreXmlRecord(refs,doc->getRoot(),*ghidra->types); 51 | delete doc; 52 | } 53 | return rec; 54 | } 55 | 56 | void ConstantPoolGhidra::saveXml(ostream &s) const 57 | 58 | { 59 | throw LowlevelError("Cannot access constant pool with this method"); 60 | } 61 | 62 | void ConstantPoolGhidra::restoreXml(const Element *el,TypeFactory &typegrp) 63 | 64 | { 65 | throw LowlevelError("Cannot access constant pool with this method"); 66 | } 67 | -------------------------------------------------------------------------------- /Ghidra9/cpool_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file cpool_ghidra.hh 17 | /// \brief Utility for implementing a \e constant \e pool backed by a Ghidra client 18 | 19 | #ifndef __CPOOL_GHIDRA__ 20 | #define __CPOOL_GHIDRA__ 21 | 22 | #include "ghidra_arch.hh" 23 | 24 | /// \brief An implementation of ConstantPool using a Ghidra client as the backing storage 25 | /// 26 | /// The actual CPoolRecord objects are cached locally, but new queries are placed 27 | /// with the Ghidra client hosting the program currently being decompiled. The 28 | /// queries and response records are sent via XML. The saveXml() and restoreXml() 29 | /// methods are disabled. The clear() method only releases the local cache, 30 | /// no records on the Ghidra client are affected. 31 | class ConstantPoolGhidra : public ConstantPool { 32 | ArchitectureGhidra *ghidra; ///< The connection with the Ghidra client 33 | mutable ConstantPoolInternal cache; ///< The local cache of previouly queried CPoolRecord objects 34 | virtual CPoolRecord *createRecord(const vector &refs); 35 | public: 36 | ConstantPoolGhidra(ArchitectureGhidra *g); ///< Constructor 37 | virtual const CPoolRecord *getRecord(const vector &refs) const; 38 | virtual bool empty(void) const { return false; } 39 | virtual void clear(void) { cache.clear(); } 40 | virtual void saveXml(ostream &s) const; 41 | virtual void restoreXml(const Element *el,TypeFactory &typegrp); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Ghidra9/crc32.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * REVIEWED: YES 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #include "crc32.hh" 18 | 19 | // Table for bytewise calculation of a 32-bit Cyclic Redundancy Check 20 | uint4 crc32tab[] = { 21 | 0x0,0x77073096,0xee0e612c,0x990951ba,0x76dc419,0x706af48f, 22 | 0xe963a535,0x9e6495a3,0xedb8832,0x79dcb8a4,0xe0d5e91e, 23 | 0x97d2d988,0x9b64c2b,0x7eb17cbd,0xe7b82d07,0x90bf1d91, 24 | 0x1db71064,0x6ab020f2,0xf3b97148,0x84be41de,0x1adad47d, 25 | 0x6ddde4eb,0xf4d4b551,0x83d385c7,0x136c9856,0x646ba8c0, 26 | 0xfd62f97a,0x8a65c9ec,0x14015c4f,0x63066cd9,0xfa0f3d63, 27 | 0x8d080df5,0x3b6e20c8,0x4c69105e,0xd56041e4,0xa2677172, 28 | 0x3c03e4d1,0x4b04d447,0xd20d85fd,0xa50ab56b,0x35b5a8fa, 29 | 0x42b2986c,0xdbbbc9d6,0xacbcf940,0x32d86ce3,0x45df5c75, 30 | 0xdcd60dcf,0xabd13d59,0x26d930ac,0x51de003a,0xc8d75180, 31 | 0xbfd06116,0x21b4f4b5,0x56b3c423,0xcfba9599,0xb8bda50f, 32 | 0x2802b89e,0x5f058808,0xc60cd9b2,0xb10be924,0x2f6f7c87, 33 | 0x58684c11,0xc1611dab,0xb6662d3d,0x76dc4190,0x1db7106, 34 | 0x98d220bc,0xefd5102a,0x71b18589,0x6b6b51f,0x9fbfe4a5, 35 | 0xe8b8d433,0x7807c9a2,0xf00f934,0x9609a88e,0xe10e9818, 36 | 0x7f6a0dbb,0x86d3d2d,0x91646c97,0xe6635c01,0x6b6b51f4, 37 | 0x1c6c6162,0x856530d8,0xf262004e,0x6c0695ed,0x1b01a57b, 38 | 0x8208f4c1,0xf50fc457,0x65b0d9c6,0x12b7e950,0x8bbeb8ea, 39 | 0xfcb9887c,0x62dd1ddf,0x15da2d49,0x8cd37cf3,0xfbd44c65, 40 | 0x4db26158,0x3ab551ce,0xa3bc0074,0xd4bb30e2,0x4adfa541, 41 | 0x3dd895d7,0xa4d1c46d,0xd3d6f4fb,0x4369e96a,0x346ed9fc, 42 | 0xad678846,0xda60b8d0,0x44042d73,0x33031de5,0xaa0a4c5f, 43 | 0xdd0d7cc9,0x5005713c,0x270241aa,0xbe0b1010,0xc90c2086, 44 | 0x5768b525,0x206f85b3,0xb966d409,0xce61e49f,0x5edef90e, 45 | 0x29d9c998,0xb0d09822,0xc7d7a8b4,0x59b33d17,0x2eb40d81, 46 | 0xb7bd5c3b,0xc0ba6cad,0xedb88320,0x9abfb3b6,0x3b6e20c, 47 | 0x74b1d29a,0xead54739,0x9dd277af,0x4db2615,0x73dc1683, 48 | 0xe3630b12,0x94643b84,0xd6d6a3e,0x7a6a5aa8,0xe40ecf0b, 49 | 0x9309ff9d,0xa00ae27,0x7d079eb1,0xf00f9344,0x8708a3d2, 50 | 0x1e01f268,0x6906c2fe,0xf762575d,0x806567cb,0x196c3671, 51 | 0x6e6b06e7,0xfed41b76,0x89d32be0,0x10da7a5a,0x67dd4acc, 52 | 0xf9b9df6f,0x8ebeeff9,0x17b7be43,0x60b08ed5,0xd6d6a3e8, 53 | 0xa1d1937e,0x38d8c2c4,0x4fdff252,0xd1bb67f1,0xa6bc5767, 54 | 0x3fb506dd,0x48b2364b,0xd80d2bda,0xaf0a1b4c,0x36034af6, 55 | 0x41047a60,0xdf60efc3,0xa867df55,0x316e8eef,0x4669be79, 56 | 0xcb61b38c,0xbc66831a,0x256fd2a0,0x5268e236,0xcc0c7795, 57 | 0xbb0b4703,0x220216b9,0x5505262f,0xc5ba3bbe,0xb2bd0b28, 58 | 0x2bb45a92,0x5cb36a04,0xc2d7ffa7,0xb5d0cf31,0x2cd99e8b, 59 | 0x5bdeae1d,0x9b64c2b0,0xec63f226,0x756aa39c,0x26d930a, 60 | 0x9c0906a9,0xeb0e363f,0x72076785,0x5005713,0x95bf4a82, 61 | 0xe2b87a14,0x7bb12bae,0xcb61b38,0x92d28e9b,0xe5d5be0d, 62 | 0x7cdcefb7,0xbdbdf21,0x86d3d2d4,0xf1d4e242,0x68ddb3f8, 63 | 0x1fda836e,0x81be16cd,0xf6b9265b,0x6fb077e1,0x18b74777, 64 | 0x88085ae6,0xff0f6a70,0x66063bca,0x11010b5c,0x8f659eff, 65 | 0xf862ae69,0x616bffd3,0x166ccf45,0xa00ae278,0xd70dd2ee, 66 | 0x4e048354,0x3903b3c2,0xa7672661,0xd06016f7,0x4969474d, 67 | 0x3e6e77db,0xaed16a4a,0xd9d65adc,0x40df0b66,0x37d83bf0, 68 | 0xa9bcae53,0xdebb9ec5,0x47b2cf7f,0x30b5ffe9,0xbdbdf21c, 69 | 0xcabac28a,0x53b39330,0x24b4a3a6,0xbad03605,0xcdd70693, 70 | 0x54de5729,0x23d967bf,0xb3667a2e,0xc4614ab8,0x5d681b02, 71 | 0x2a6f2b94,0xb40bbe37,0xc30c8ea1,0x5a05df1b,0x2d02ef8d }; 72 | 73 | -------------------------------------------------------------------------------- /Ghidra9/crc32.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file crc32.hh 17 | /// \brief Table and function for computing a CRC32 18 | 19 | #ifndef __CRC32__ 20 | #define __CRC32__ 21 | 22 | #include "types.h" 23 | 24 | extern uint4 crc32tab[]; ///< Table for quickly computing a 32-bit Cyclic Redundacy Check (CRC) 25 | 26 | /// \brief Feed 8 bits into a CRC register 27 | /// 28 | /// \param reg is the current state of the CRC register 29 | /// \param val holds 8 bits (least significant) to feed in 30 | /// \return the new value of the register 31 | inline uint4 crc_update(uint4 reg,uint4 val) { 32 | return crc32tab[(reg ^ val)&0xff] ^ (reg>>8); } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Ghidra9/error.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file error.hh 17 | /// \brief Base class for error handling facilities 18 | /// 19 | /// This is also doubles as a place to list the common include files 20 | 21 | #ifndef __CPUI_ERROR__ 22 | #define __CPUI_ERROR__ 23 | 24 | #include "types.h" 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | using namespace std; 37 | 38 | /// \brief The lowest level error generated by the decompiler 39 | /// 40 | /// This is the base error for all exceptions thrown by the 41 | /// decompiler. This underived form is thrown for very low 42 | /// level errors that immediately abort decompilation (usually 43 | /// for just a single function). 44 | struct LowlevelError { 45 | string explain; ///< Explanatory string 46 | /// Initialize the error with an explanatory string 47 | LowlevelError(const string &s) { explain = s; } 48 | }; 49 | 50 | /// \brief A generic recoverable error 51 | /// 52 | /// This error is the most basic form of recoverable error, 53 | /// meaning there is some problem that the user did not take 54 | /// into account. 55 | struct RecovError : public LowlevelError { 56 | /// Initialize the error with an explanatory string 57 | RecovError(const string &s) : LowlevelError(s) {} 58 | }; 59 | 60 | /// \brief An error generated while parsing a command or language 61 | /// 62 | /// This error is generated when parsing character data of some 63 | /// form, as in a user command from the console or when parsing 64 | /// C syntax. 65 | struct ParseError : public LowlevelError { // Parsing error 66 | /// Initialize the error with an explanatory string 67 | ParseError(const string &s) : LowlevelError(s) {} 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /Ghidra9/filemanage.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // Generic (POSIX) class for searching files and managing paths 17 | 18 | #ifndef __FILEMANAGE__ 19 | #define __FILEMANAGE__ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | class FileManage { 30 | vector pathlist; // List of paths to search for files 31 | static char separator; 32 | static string buildPath(const vector &pathels,int level); 33 | static bool testDevelopmentPath(const vector &pathels,int level,string &root); 34 | static bool testInstallPath(const vector &pathels,int level,string &root); 35 | public: 36 | void addDir2Path(const string &path); 37 | void addCurrentDir(void); 38 | void findFile(string &res,const string &name) const; // Resolve full pathname 39 | void matchList(vector &res,const string &match,bool isSuffix) const; // List of files with suffix 40 | static bool isDirectory(const string &path); 41 | static void matchListDir(vector &res,const string &match,bool isSuffix,const string &dir,bool allowdot); 42 | static void directoryList(vector &res,const string &dirname,bool allowdot=false); 43 | static void scanDirectoryRecursive(vector &res,const string &matchname,const string &rootpath,int maxdepth); 44 | static void splitPath(const string &full,string &path,string &base); 45 | static bool isAbsolutePath(const string &full) { if (full.empty()) return false; return (full[0] == separator); } 46 | static string discoverGhidraRoot(const char *argv0); 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /Ghidra9/ghidra_context.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "ghidra_context.hh" 17 | 18 | const TrackedSet &ContextGhidra::getTrackedSet(const Address &addr) const 19 | 20 | { 21 | cache.clear(); 22 | 23 | Document *doc = ((ArchitectureGhidra *)glb)->getTrackedRegisters(addr); 24 | Element *root = doc->getRoot(); 25 | 26 | restoreTracked(root,glb,cache); 27 | delete doc; 28 | return cache; 29 | } 30 | -------------------------------------------------------------------------------- /Ghidra9/ghidra_translate.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file ghidra_translate.hh 17 | /// \brief Class for fetching p-code from a Ghidra client 18 | 19 | #ifndef __TRANSLATE_GHIDRA__ 20 | #define __TRANSLATE_GHIDRA__ 21 | 22 | #include "translate.hh" 23 | #include "ghidra_arch.hh" 24 | 25 | /// \brief An implementation of Translate that queries a Ghidra client for p-code information 26 | /// 27 | /// This class provides: 28 | /// - P-code for instructions and 29 | /// - Register names 30 | /// 31 | /// by sending a request to a Ghidra client and decoding the response. 32 | /// Messages are generally based on an XML format, but p-code responses in particular 33 | /// have a tight internal encoding. 34 | class GhidraTranslate : public Translate { 35 | ArchitectureGhidra *glb; ///< The Ghidra Architecture and connection to the client 36 | mutable map nm2addr; ///< Mapping from register name to Varnode 37 | mutable map addr2nm; ///< Mapping rom Varnode to register name 38 | const VarnodeData &cacheRegister(const string &nm,const VarnodeData &data) const; 39 | void restoreXml(const Element *el); ///< Initialize \b this Translate from XML 40 | public: 41 | GhidraTranslate(ArchitectureGhidra *g) { glb = g; } ///< Constructor 42 | 43 | virtual void initialize(DocumentStorage &store); 44 | virtual void addRegister(const string &nm,AddrSpace *base,uintb offset,int4 size) { 45 | throw LowlevelError("Cannot add register name into GHIDRA through this interface"); } 46 | virtual const VarnodeData &getRegister(const string &nm) const; 47 | virtual string getRegisterName(AddrSpace *base,uintb off,int4 size) const; 48 | virtual void getAllRegisters(map ®list) const { 49 | throw LowlevelError("Cannot currently get all registers through this interface"); } 50 | virtual void getUserOpNames(vector &res) const; 51 | virtual int4 oneInstruction(PcodeEmit &emit,const Address &baseaddr) const; 52 | virtual int4 instructionLength(const Address &baseaddr) const { 53 | throw LowlevelError("Cannot currently get instruction length through this interface"); } 54 | virtual int4 printAssembly(AssemblyEmit &emit,const Address &baseaddr) const { 55 | throw LowlevelError("Cannot dump assembly through this interface"); } 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /Ghidra9/grammar.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_CPARSE_GRAMMAR_TAB_H_INCLUDED 34 | # define YY_CPARSE_GRAMMAR_TAB_H_INCLUDED 35 | /* Enabling traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int cparsedebug; 41 | #endif 42 | 43 | /* Tokens. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | /* Put the tokens into the symbol table, so that GDB and other debuggers 47 | know about them. */ 48 | enum yytokentype { 49 | DOTDOTDOT = 258, 50 | BADTOKEN = 259, 51 | STRUCT = 260, 52 | UNION = 261, 53 | ENUM = 262, 54 | DECLARATION_RESULT = 263, 55 | PARAM_RESULT = 264, 56 | NUMBER = 265, 57 | IDENTIFIER = 266, 58 | STORAGE_CLASS_SPECIFIER = 267, 59 | TYPE_QUALIFIER = 268, 60 | FUNCTION_SPECIFIER = 269, 61 | TYPE_NAME = 270 62 | }; 63 | #endif 64 | 65 | 66 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 67 | typedef union YYSTYPE 68 | { 69 | /* Line 2058 of yacc.c */ 70 | #line 25 "grammar.y" 71 | 72 | uint4 flags; 73 | TypeDeclarator *dec; 74 | vector *declist; 75 | TypeSpecifiers *spec; 76 | vector *ptrspec; 77 | Datatype *type; 78 | Enumerator *enumer; 79 | vector *vecenum; 80 | string *str; 81 | uintb *i; 82 | 83 | 84 | /* Line 2058 of yacc.c */ 85 | #line 86 "grammar.tab.h" 86 | } YYSTYPE; 87 | # define YYSTYPE_IS_TRIVIAL 1 88 | # define yystype YYSTYPE /* obsolescent; will be withdrawn */ 89 | # define YYSTYPE_IS_DECLARED 1 90 | #endif 91 | 92 | extern YYSTYPE cparselval; 93 | 94 | #ifdef YYPARSE_PARAM 95 | #if defined __STDC__ || defined __cplusplus 96 | int cparseparse (void *YYPARSE_PARAM); 97 | #else 98 | int cparseparse (); 99 | #endif 100 | #else /* ! YYPARSE_PARAM */ 101 | #if defined __STDC__ || defined __cplusplus 102 | int cparseparse (void); 103 | #else 104 | int cparseparse (); 105 | #endif 106 | #endif /* ! YYPARSE_PARAM */ 107 | 108 | #endif /* !YY_CPARSE_GRAMMAR_TAB_H_INCLUDED */ 109 | -------------------------------------------------------------------------------- /Ghidra9/graph.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * REVIEWED: YES 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #include "funcdata.hh" 18 | 19 | extern void dump_dataflow_graph(Funcdata &data,ostream &s); 20 | extern void dump_controlflow_graph(const string &name,const BlockGraph &graph,ostream &s); 21 | extern void dump_dom_graph(const string &name,const BlockGraph &graph,ostream &s); 22 | -------------------------------------------------------------------------------- /Ghidra9/ifaceterm.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // Add some terminal capabilities to the command-line interface 17 | #include "interface.hh" 18 | 19 | #ifdef __TERMINAL__ 20 | extern "C" { 21 | #include 22 | #include 23 | } 24 | #endif 25 | 26 | class IfaceTerm : public IfaceStatus { 27 | #ifdef __TERMINAL__ 28 | bool is_terminal; // True if the input stream is a terminal 29 | int4 ifd; // Underlying file descriptor 30 | struct termios itty; // Original terminal settings 31 | #endif 32 | int4 doCompletion(string &line,int4 cursor); 33 | virtual void readLine(string &line); 34 | public: 35 | IfaceTerm(const string &prmpt,istream &is,ostream &os); 36 | virtual ~IfaceTerm(void); 37 | }; 38 | -------------------------------------------------------------------------------- /Ghidra9/libdecomp.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "libdecomp.hh" 17 | 18 | void startDecompilerLibrary(const char *sleighhome) 19 | 20 | { 21 | CapabilityPoint::initializeAll(); 22 | ArchitectureCapability::sortCapabilities(); 23 | 24 | if (sleighhome != (const char *)0) 25 | SleighArchitecture::scanForSleighDirectories(sleighhome); 26 | } 27 | 28 | void startDecompilerLibrary(const vector &extrapaths) 29 | 30 | { 31 | CapabilityPoint::initializeAll(); 32 | ArchitectureCapability::sortCapabilities(); 33 | 34 | for(uint4 i=0;i &extrapaths) 39 | 40 | { 41 | CapabilityPoint::initializeAll(); 42 | ArchitectureCapability::sortCapabilities(); 43 | 44 | if (sleighhome != (const char *)0) 45 | SleighArchitecture::scanForSleighDirectories(sleighhome); 46 | 47 | for(uint4 i=0;i &extrapaths); 32 | extern void startDecompilerLibrary(const char *sleighhome,const vector &extrapaths); 33 | 34 | extern void shutdownDecompilerLibrary(void); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /Ghidra9/loadimage.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "loadimage.hh" 17 | 18 | /// This is a convenience method wrapped around the core 19 | /// loadFill() routine. It automatically allocates an array 20 | /// of the desired size, and then fills it with load image data. 21 | /// If the array cannot be allocated, an exception is thrown. 22 | /// The caller assumes the responsibility of freeing the 23 | /// array after it has been used. 24 | /// \param size is the number of bytes to read from the image 25 | /// \param addr is the address of the first byte being read 26 | /// \return a pointer to the desired bytes 27 | uint1 *LoadImage::load(int4 size,const Address &addr) 28 | 29 | { 30 | uint1 *buf = new uint1[ size ]; 31 | if (buf == (uint1 *)0) 32 | throw LowlevelError("Out of memory"); 33 | loadFill(buf,size,addr); 34 | return buf; 35 | } 36 | 37 | RawLoadImage::RawLoadImage(const string &f) : LoadImage(f) 38 | 39 | { 40 | vma = 0; 41 | thefile = (ifstream *)0; 42 | spaceid = (AddrSpace *)0; 43 | filesize = 0; 44 | } 45 | 46 | RawLoadImage::~RawLoadImage(void) 47 | 48 | { 49 | if (thefile != (ifstream *)0) { 50 | thefile->close(); 51 | delete thefile; 52 | } 53 | } 54 | 55 | /// The file is opened and its size immediately recovered. 56 | void RawLoadImage::open(void) 57 | 58 | { 59 | if (thefile != (ifstream *)0) throw LowlevelError("loadimage is already open"); 60 | thefile = new ifstream(filename.c_str()); 61 | if (!(*thefile)) { 62 | string errmsg = "Unable to open raw image file: "+filename; 63 | throw LowlevelError(errmsg); 64 | } 65 | thefile->seekg(0,ios::end); 66 | filesize = thefile->tellg(); 67 | } 68 | 69 | string RawLoadImage::getArchType(void) const 70 | 71 | { 72 | return "unknown"; 73 | } 74 | 75 | void RawLoadImage::adjustVma(long adjust) 76 | 77 | { 78 | adjust = AddrSpace::addressToByte(adjust,spaceid->getWordSize()); 79 | vma += adjust; 80 | } 81 | 82 | void RawLoadImage::loadFill(uint1 *ptr,int4 size,const Address &addr) 83 | 84 | { 85 | uintb curaddr = addr.getOffset(); 86 | uintb offset = 0; 87 | uintb readsize; 88 | 89 | curaddr -= vma; // Get relative offset of first byte 90 | while(size>0) { 91 | if (curaddr >= filesize) { 92 | if (offset == 0) // Initial address not within file 93 | break; 94 | memset(ptr+offset,0,size); // Fill out the rest of the buffer with 0 95 | return; 96 | } 97 | readsize = size; 98 | if (curaddr + readsize > filesize) // Adjust to biggest possible read 99 | readsize = filesize - curaddr; 100 | thefile->seekg(curaddr); 101 | thefile->read((char *)(ptr+offset),readsize); 102 | offset += readsize; 103 | size -= readsize; 104 | curaddr += readsize; 105 | } 106 | if (size > 0) { 107 | ostringstream errmsg; 108 | errmsg << "Unable to load " << dec << size << " bytes at " << addr.getShortcut(); 109 | addr.printRaw(errmsg); 110 | throw DataUnavailError(errmsg.str()); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /Ghidra9/loadimage_bfd.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * EXCLUDE: YES 4 | * NOTE: Links to GNU BFD library which is GPL 3 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | // Use the GNU bfd library to manipulate a load image 19 | 20 | #ifndef __LOADIMAGE_BFD__ 21 | #define __LOADIMAGE_BFD__ 22 | 23 | #include "loadimage.hh" 24 | #include 25 | 26 | struct ImportRecord { 27 | string dllname; 28 | string funcname; 29 | int ordinal; 30 | Address address; 31 | Address thunkaddress; 32 | }; 33 | 34 | class LoadImageBfd : public LoadImage { 35 | static int4 bfdinit; // Is the library (globally) initialized 36 | string target; // File format (supported by BFD) 37 | bfd *thebfd; 38 | AddrSpace *spaceid; // We need to map space id to segments but since 39 | // we are currently ignoring segments anyway... 40 | uintb bufoffset; // Starting offset of byte buffer 41 | uint4 bufsize; // Number of bytes in the buffer 42 | uint1 *buffer; // The actual buffer 43 | mutable asymbol **symbol_table; 44 | mutable long number_of_symbols; 45 | mutable long cursymbol; 46 | mutable asection *secinfoptr; 47 | asection *findSection(uintb offset,uintb &ssize) const; // Find section containing given offset 48 | void advanceToNextSymbol(void) const; 49 | public: 50 | LoadImageBfd(const string &f,const string &t); 51 | void attachToSpace(AddrSpace *id) { spaceid = id; } 52 | void open(void); // Open any descriptors 53 | void close(void); // Close any descriptor 54 | void getImportTable(vector &irec) { throw LowlevelError("Not implemented"); } 55 | virtual ~LoadImageBfd(void); 56 | virtual void loadFill(uint1 *ptr,int4 size,const Address &addr); // Load a chunk of image 57 | virtual void openSymbols(void) const; 58 | virtual void closeSymbols(void) const; 59 | virtual bool getNextSymbol(LoadImageFunc &record) const; 60 | virtual void openSectionInfo(void) const; 61 | virtual void closeSectionInfo(void) const; 62 | virtual bool getNextSection(LoadImageSection &sec) const; 63 | virtual void getReadonly(RangeList &list) const; 64 | virtual string getArchType(void) const; 65 | virtual void adjustVma(long adjust); 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /Ghidra9/loadimage_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "loadimage_ghidra.hh" 17 | 18 | LoadImageGhidra::LoadImageGhidra(ArchitectureGhidra *g) 19 | : LoadImage("ghidra_progam") 20 | 21 | { 22 | glb = g; 23 | } 24 | 25 | void LoadImageGhidra::open(void) 26 | 27 | { 28 | } 29 | 30 | void LoadImageGhidra::close(void) 31 | 32 | { 33 | } 34 | 35 | void LoadImageGhidra::loadFill(uint1 *ptr,int4 size,const Address &inaddr) 36 | 37 | { 38 | glb->getBytes(ptr,size,inaddr); 39 | } 40 | 41 | string LoadImageGhidra::getArchType(void) const 42 | 43 | { 44 | return "ghidra"; 45 | } 46 | 47 | void LoadImageGhidra::adjustVma(long adjust) 48 | 49 | { 50 | throw LowlevelError("Cannot adjust GHIDRA virtual memory"); 51 | } 52 | -------------------------------------------------------------------------------- /Ghidra9/loadimage_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file loadimage_ghidra.hh 17 | /// \brief Use the Ghidra client as a load image 18 | #ifndef __LOADIMAGE_GHIDRA__ 19 | #define __LOADIMAGE_GHIDRA__ 20 | 21 | #include "loadimage.hh" 22 | #include "ghidra_arch.hh" 23 | 24 | class ArchitectureGhidra; 25 | 26 | /// \brief An implementation of the LoadImage interface using a Ghidra client as the back-end 27 | /// 28 | /// Requests for program bytes are marshaled to a Ghidra client which sends back the data 29 | class LoadImageGhidra : public LoadImage { 30 | ArchitectureGhidra *glb; ///< The owning Architecture and connection to the client 31 | public: 32 | LoadImageGhidra(ArchitectureGhidra *g); ///< Constructor 33 | // virtual ~LoadImage(void) {} 34 | void open(void); ///< Open any descriptors 35 | void close(void); ///< Close any descriptor 36 | virtual void loadFill(uint1 *ptr,int4 size,const Address &addr); 37 | // Read only flags are all controlled through the database interface 38 | virtual string getArchType(void) const; 39 | virtual void adjustVma(long adjust); 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /Ghidra9/loadimage_xml.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file loadimage_xml.hh 17 | /// \brief Support for programs stored using an XML schema 18 | 19 | #ifndef __LOADIMAGE_XML__ 20 | #define __LOADIMAGE_XML__ 21 | 22 | #include "loadimage.hh" 23 | 24 | /// \brief Implementation of the LoadImage interface using underlying data stored in an XML format 25 | /// 26 | /// The image data is stored in an XML file in a \ file. 27 | /// The data is encoded in \ and potentially \ files. 28 | class LoadImageXml : public LoadImage { 29 | const Element *rootel; ///< The root XML element 30 | string archtype; ///< The architecture string 31 | const AddrSpaceManager *manage; ///< Manager of addresses 32 | set
readonlyset; ///< Starting address of read-only chunks 33 | map > chunk; ///< Chunks of image data, mapped by address 34 | map addrtosymbol; ///< Symbols sorted by address 35 | mutable map::const_iterator cursymbol; ///< Current symbol being reported 36 | void pad(void); ///< Make sure every chunk is followed by at least 512 bytes of pad 37 | public: 38 | LoadImageXml(const string &f,const Element *el); ///< Constructor 39 | void open(const AddrSpaceManager *m); ///< Read XML tags into the containers 40 | void clear(void); ///< Clear out all the caches 41 | void saveXml(ostream &s) const; ///< Save the image back out to an XML stream 42 | virtual ~LoadImageXml(void) { clear(); } 43 | virtual void loadFill(uint1 *ptr,int4 size,const Address &addr); 44 | virtual void openSymbols(void) const; 45 | virtual bool getNextSymbol(LoadImageFunc &record) const; 46 | virtual void getReadonly(RangeList &list) const; 47 | virtual string getArchType(void) const { return archtype; } 48 | virtual void adjustVma(long adjust); 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Ghidra9/paramid.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #ifndef __CPUI_PARAMID__ 17 | #define __CPUI_PARAMID__ 18 | 19 | #include "funcdata.hh" 20 | 21 | class ParamMeasure { 22 | public: 23 | enum ParamIDIO { 24 | INPUT = 0, 25 | OUTPUT = 1 26 | }; 27 | enum ParamRank { 28 | BESTRANK = 1, 29 | DIRECTWRITEWITHOUTREAD = 1, //Output 30 | DIRECTREAD = 2, //Input. Must be same as DIRECTWRITEWITHREAD so that walkforward as part of walkbackward works 31 | // for detecting(not that DIRECTREAD is lower rank that DIRECTWRITEWITHOUTREAD) 32 | DIRECTWRITEWITHREAD = 2, //Output 33 | DIRECTWRITEUNKNOWNREAD = 3, //Output 34 | SUBFNPARAM = 4, //Input 35 | THISFNPARAM = 4, //Output 36 | SUBFNRETURN = 5, //Output 37 | THISFNRETURN = 5, //Input 38 | INDIRECT = 6, //Input or Output 39 | WORSTRANK = 7 40 | }; 41 | struct WalkState { 42 | bool best; 43 | int4 depth; 44 | ParamRank terminalrank; 45 | }; 46 | private: 47 | VarnodeData vndata; 48 | Datatype *vntype; 49 | ParamRank rank; 50 | ParamIDIO io; 51 | int4 numcalls; 52 | void walkforward( WalkState &state, PcodeOp *ignoreop, Varnode *vn ); 53 | void walkbackward( WalkState &state, PcodeOp *ignoreop,Varnode *vn ); 54 | void updaterank( ParamRank rank_in,bool best ) { rank = (best==true) ? min( rank, rank_in ) : max( rank, rank_in ); } 55 | public: 56 | ParamMeasure( const Address &addr, int4 sz, Datatype *dt, ParamIDIO io_in) { 57 | vndata.space=addr.getSpace(); vndata.offset=addr.getOffset(); vndata.size = sz; vntype=dt; io = io_in; rank=WORSTRANK; } 58 | void calculateRank(bool best,Varnode *basevn,PcodeOp *ignoreop); 59 | void saveXml( ostream &s,string tag,bool moredetail ) const; 60 | void savePretty( ostream &s,bool moredetail ) const; 61 | int4 getMeasure(void) const { return (int4) rank; } 62 | }; 63 | 64 | class ParamIDAnalysis 65 | { 66 | Funcdata *fd; 67 | list InputParamMeasures; 68 | list OutputParamMeasures; 69 | public: 70 | ParamIDAnalysis( Funcdata *fd_in, bool justproto ); 71 | void saveXml( ostream &s, bool moredetail ) const; 72 | void savePretty( ostream &s, bool moredetail ) const; 73 | }; 74 | 75 | #endif //ifndef __CPUI_PARAMID__ 76 | -------------------------------------------------------------------------------- /Ghidra9/pcodeparse.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #ifndef __PCODE_SNIPPET__ 17 | #define __PCODE_SNIPPET__ 18 | 19 | #include "pcodecompile.hh" 20 | #include "sleighbase.hh" 21 | 22 | // Classes for compiling a standalone snippet of pcode, given an existing sleigh language 23 | 24 | struct IdentRec { 25 | const char *nm; 26 | int4 id; 27 | }; 28 | 29 | class PcodeLexer { 30 | public: 31 | enum { // Lexer states 32 | start, 33 | special2, // Middle of special 2 character operator 34 | special3, // First character of special 3 character operator 35 | special32, // Second character of special 3 character operator 36 | comment, // Middle of an endofline comment 37 | punctuation, // Punctuation character 38 | identifier, // Middle of an identifier 39 | hexstring, // Middle of a hexidecimal number 40 | decstring, // Middle of a decimal number 41 | endstream, // Reached end of stream 42 | illegal // Scanned an illegal character 43 | }; 44 | private: 45 | static const IdentRec idents[]; 46 | int4 curstate; 47 | char curchar,lookahead1,lookahead2; 48 | char curtoken[256]; 49 | int4 tokpos; 50 | bool endofstream; 51 | bool endofstreamsent; 52 | istream *s; 53 | string curidentifier; 54 | uintb curnum; 55 | void starttoken(void) { curtoken[0] = curchar; tokpos = 1; } 56 | void advancetoken(void) { curtoken[tokpos++] = curchar; } 57 | bool isIdent(char c) const { return (isalnum(c)||(c=='_')||(c=='.')); } 58 | bool isHex(char c) const { return isxdigit(c); } 59 | bool isDec(char c) const { return isdigit(c); } 60 | int4 findIdentifier(const string &str) const; 61 | int4 moveState(void); 62 | public: 63 | PcodeLexer(void) { s = (istream *)0; } 64 | void initialize(istream *t); 65 | int4 getNextToken(void); 66 | const string &getIdentifier(void) const { return curidentifier; } 67 | uintb getNumber(void) const { return curnum; } 68 | }; 69 | 70 | class PcodeSnippet : public PcodeCompile { 71 | PcodeLexer lexer; 72 | const SleighBase *sleigh; // Language from which we get symbols 73 | SymbolTree tree; // Symbols in the local scope of the snippet (temporaries) 74 | uintb tempbase; 75 | int4 errorcount; 76 | string firsterror; 77 | ConstructTpl *result; 78 | virtual uintb allocateTemp(void); 79 | virtual void addSymbol(SleighSymbol *sym); 80 | public: 81 | PcodeSnippet(const SleighBase *slgh); 82 | void setResult(ConstructTpl *res) { result = res; } 83 | ConstructTpl *releaseResult(void) { ConstructTpl *res = result; result = (ConstructTpl *)0; return res; } 84 | virtual ~PcodeSnippet(void); 85 | virtual void reportError(const string &msg); 86 | bool hasErrors(void) const { return (errorcount != 0); } 87 | const string getErrorMessage(void) const { return firsterror; } 88 | void setUniqueBase(uintb val) { tempbase = val; } 89 | uintb getUniqueBase(void) const { return tempbase; } 90 | void clear(void); 91 | int lex(void); 92 | bool parseStream(istream& s); 93 | void addOperand(const string &name,int4 index); 94 | }; 95 | 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /Ghidra9/pcoderaw.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "pcoderaw.hh" 17 | #include "translate.hh" 18 | 19 | /// Build this VarnodeData from an \b \ tag 20 | /// \param el is the parsed tag 21 | /// \param manage is the address space manager 22 | void VarnodeData::restoreXml(const Element *el,const AddrSpaceManager *manage) 23 | 24 | { 25 | space = (AddrSpace *)0; 26 | size = 0; 27 | int4 num = el->getNumAttributes(); 28 | for(int4 i=0;igetAttributeName(i)=="space") { 30 | space = manage->getSpaceByName(el->getAttributeValue(i)); 31 | if (space == (AddrSpace *)0) 32 | throw LowlevelError("Unknown space name: "+el->getAttributeValue(i)); 33 | offset = space->restoreXmlAttributes(el,size); 34 | return; 35 | } 36 | else if (el->getAttributeName(i)=="name") { 37 | const Translate *trans = manage->getDefaultSpace()->getTrans(); 38 | const VarnodeData &point(trans->getRegister(el->getAttributeValue(i))); 39 | *this = point; 40 | return; 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Ghidra9/prefersplit.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * REVIEWED: YES 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #ifndef __PREFERSPLIT__ 18 | #define __PREFERSPLIT__ 19 | 20 | #include "varnode.hh" 21 | class Funcdata; // Forward declaration 22 | 23 | struct PreferSplitRecord { 24 | VarnodeData storage; 25 | int4 splitoffset; // Number of initial bytes (in address order) to split into first piece 26 | bool operator<(const PreferSplitRecord &op2) const; 27 | }; 28 | 29 | class PreferSplitManager { 30 | class SplitInstance { 31 | friend class PreferSplitManager; 32 | int4 splitoffset; 33 | Varnode *vn; 34 | Varnode *hi; // Most significant piece 35 | Varnode *lo; // Least significant piece 36 | public: 37 | SplitInstance(Varnode *v,int4 off) { vn = v; splitoffset = off; hi = (Varnode *)0; lo = (Varnode *)0; } 38 | }; 39 | Funcdata *data; 40 | const vector *records; 41 | vector tempsplits; // Copies of temporaries that need additional splitting 42 | void fillinInstance(SplitInstance *inst,bool bigendian,bool sethi,bool setlo); 43 | void createCopyOps(SplitInstance *ininst,SplitInstance *outinst,PcodeOp *op,bool istemp); 44 | bool testDefiningCopy(SplitInstance *inst,PcodeOp *def,bool &istemp); 45 | void splitDefiningCopy(SplitInstance *inst,PcodeOp *def,bool istemp); 46 | bool testReadingCopy(SplitInstance *inst,PcodeOp *readop,bool &istemp); 47 | void splitReadingCopy(SplitInstance *inst,PcodeOp *readop,bool istemp); 48 | bool testZext(SplitInstance *inst,PcodeOp *op); 49 | void splitZext(SplitInstance *inst,PcodeOp *op); 50 | bool testPiece(SplitInstance *inst,PcodeOp *op); 51 | void splitPiece(SplitInstance *inst,PcodeOp *op); 52 | bool testSubpiece(SplitInstance *inst,PcodeOp *op); 53 | void splitSubpiece(SplitInstance *inst,PcodeOp *op); 54 | bool testLoad(SplitInstance *inst,PcodeOp *op); 55 | void splitLoad(SplitInstance *inst,PcodeOp *op); 56 | bool testStore(SplitInstance *inst,PcodeOp *op); 57 | void splitStore(SplitInstance *inst,PcodeOp *op); 58 | bool splitVarnode(SplitInstance *inst); 59 | void splitRecord(const PreferSplitRecord &rec); 60 | bool testTemporary(SplitInstance *inst); 61 | void splitTemporary(SplitInstance *inst); 62 | public: 63 | void init(Funcdata *fd,const vector *rec); 64 | const PreferSplitRecord *findRecord(Varnode *vn) const; 65 | static void initialize(vector &records); 66 | void split(void); 67 | void splitAdditional(void); 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /Ghidra9/printjava.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file printjava.hh 17 | /// \brief Classes supporting the java-language back-end to the decompiler 18 | 19 | #ifndef __PRINTJAVA__ 20 | #define __PRINTJAVA__ 21 | 22 | #include "printc.hh" 23 | 24 | /// \brief Factory and static initializer for the "java-language" back-end to the decompiler 25 | /// 26 | /// The singleton adds itself to the list of possible back-end languages for the decompiler 27 | /// and it acts as a factory for producing the PrintJava object for emitting java-language tokens. 28 | class PrintJavaCapability : public PrintLanguageCapability { 29 | static PrintJavaCapability printJavaCapability; ///< The singleton instance 30 | PrintJavaCapability(void); ///< Singleton constructor 31 | PrintJavaCapability(const PrintJavaCapability &op2); ///< Not implemented 32 | PrintJavaCapability &operator=(const PrintJavaCapability &op); ///< Not implemented 33 | public: 34 | virtual PrintLanguage *buildLanguage(Architecture *glb); 35 | }; 36 | 37 | /// \brief The java-language token emitter 38 | /// 39 | /// This builds heavily on the c-language PrintC emitter. Most operator tokens, the format of 40 | /// function prototypes, and code structuring are shared. Specifics of the java constant pool are handled 41 | /// through the overloaded opCpoolRefOp(). 42 | /// 43 | /// Java data-types are mapped into the decompiler's data-type system in a specific way. The primitives 44 | /// \b int, \b long, \b short, \b byte, \b boolean, \b float, and \b double all map directly. The 45 | /// \b char primitive is treated as a 2 byte unsigned integer. A TypeStruct object holds the field 46 | /// layout for a java class, then java objects get mapped as follows: 47 | /// - Class reference = pointer to TYPE_UINT 48 | /// - Array of \b int, \b long, \b short, or \b byte = pointer to TYPE_INT 49 | /// - Array of \b float or \b double = pointer to TYPE_FLOAT 50 | /// - Array of \b boolean = pointer to TYPE_BOOL 51 | /// - Array of class objects = pointer to TYPE_PTR 52 | /// 53 | /// There are some adjustments to the printing of data-types and LOAD/STORE expressions 54 | /// to account for this mapping. 55 | class PrintJava : public PrintC { 56 | static OpToken instanceof; ///< The \b instanceof keyword 57 | static bool isArrayType(const Datatype *ct); ///< Does the given data-type reference a java array 58 | static bool needZeroArray(const Varnode *vn); ///< Do we need '[0]' syntax. 59 | virtual void printUnicode(ostream &s,int4 onechar) const; 60 | public: 61 | PrintJava(Architecture *g,const string &nm="java-language"); ///< Constructor 62 | virtual void pushTypeStart(const Datatype *ct,bool noident); 63 | virtual void pushTypeEnd(const Datatype *ct); 64 | virtual bool doEmitWideCharPrefix(void) const { return false; } 65 | virtual void adjustTypeOperators(void); 66 | virtual void opLoad(const PcodeOp *op); 67 | virtual void opStore(const PcodeOp *op); 68 | virtual void opCallind(const PcodeOp *op); 69 | virtual void opCpoolRefOp(const PcodeOp *op); 70 | }; 71 | #endif 72 | -------------------------------------------------------------------------------- /Ghidra9/raw_arch.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "raw_arch.hh" 17 | 18 | // Constructing this object registers the capability 19 | RawBinaryArchitectureCapability RawBinaryArchitectureCapability::rawBinaryArchitectureCapability; 20 | 21 | RawBinaryArchitectureCapability::RawBinaryArchitectureCapability(void) 22 | 23 | { 24 | name = "raw"; 25 | } 26 | 27 | RawBinaryArchitectureCapability::~RawBinaryArchitectureCapability(void) 28 | 29 | { 30 | SleighArchitecture::shutdown(); 31 | } 32 | 33 | Architecture *RawBinaryArchitectureCapability::buildArchitecture(const string &filename,const string &target,ostream *estream) 34 | 35 | { 36 | return new RawBinaryArchitecture(filename,target,estream); 37 | } 38 | 39 | bool RawBinaryArchitectureCapability::isFileMatch(const string &filename) const 40 | 41 | { 42 | return true; // File can always be opened as raw binary 43 | } 44 | 45 | bool RawBinaryArchitectureCapability::isXmlMatch(Document *doc) const 46 | 47 | { 48 | return (doc->getRoot()->getName() == "raw_savefile"); 49 | } 50 | 51 | void RawBinaryArchitecture::buildLoader(DocumentStorage &store) 52 | 53 | { 54 | RawLoadImage *ldr; 55 | 56 | collectSpecFiles(*errorstream); 57 | ldr = new RawLoadImage(getFilename()); 58 | ldr->open(); 59 | if (adjustvma != 0) 60 | ldr->adjustVma(adjustvma); 61 | loader = ldr; 62 | } 63 | 64 | void RawBinaryArchitecture::resolveArchitecture(void) 65 | 66 | { 67 | archid = getTarget(); // Nothing to derive from the image itself, we just copy in the passed in target 68 | SleighArchitecture::resolveArchitecture(); 69 | } 70 | 71 | void RawBinaryArchitecture::postSpecFile(void) 72 | 73 | { 74 | ((RawLoadImage *)loader)->attachToSpace(getDefaultSpace()); // Attach default space to loader 75 | } 76 | 77 | RawBinaryArchitecture::RawBinaryArchitecture(const string &fname,const string &targ,ostream *estream) 78 | : SleighArchitecture(fname,targ,estream) 79 | { 80 | adjustvma = 0; 81 | } 82 | 83 | void RawBinaryArchitecture::saveXml(ostream &s) const 84 | 85 | { 86 | s << "\n"; 90 | types->saveXmlCoreTypes(s); 91 | SleighArchitecture::saveXml(s); 92 | s << "\n"; 93 | } 94 | 95 | void RawBinaryArchitecture::restoreXml(DocumentStorage &store) 96 | 97 | { 98 | const Element *el = store.getTag("raw_savefile"); 99 | if (el == (const Element *)0) 100 | throw LowlevelError("Could not find raw_savefile tag"); 101 | 102 | restoreXmlHeader(el); 103 | { 104 | istringstream s( el->getAttributeValue("adjustvma")); 105 | s.unsetf(ios::dec | ios::hex | ios::oct); 106 | s >> adjustvma; 107 | } 108 | const List &list(el->getChildren()); 109 | List::const_iterator iter; 110 | 111 | iter = list.begin(); 112 | if (iter != list.end()) { 113 | if ((*iter)->getName() == "coretypes") { 114 | store.registerTag(*iter); 115 | ++iter; 116 | } 117 | } 118 | init(store); // Load the image and configure 119 | 120 | if (iter != list.end()) { 121 | store.registerTag(*iter); 122 | SleighArchitecture::restoreXml(store); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /Ghidra9/raw_arch.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file raw_arch.hh 17 | /// \brief Bare bones capability for treating a file as a raw executable image 18 | #include "sleigh_arch.hh" 19 | #include "loadimage.hh" 20 | 21 | /// \brief Extension point for building an Architecture that reads in raw images 22 | class RawBinaryArchitectureCapability : public ArchitectureCapability { 23 | static RawBinaryArchitectureCapability rawBinaryArchitectureCapability; ///< The singleton instance 24 | RawBinaryArchitectureCapability(void); ///< Singleton 25 | RawBinaryArchitectureCapability(const RawBinaryArchitectureCapability &op2); ///< Not implemented 26 | RawBinaryArchitectureCapability &operator=(const RawBinaryArchitectureCapability &op2); ///< Not implemented 27 | public: 28 | virtual ~RawBinaryArchitectureCapability(void); 29 | virtual Architecture *buildArchitecture(const string &filename,const string &target,ostream *estream); 30 | virtual bool isFileMatch(const string &filename) const; 31 | virtual bool isXmlMatch(Document *doc) const; 32 | }; 33 | 34 | /// \brief Architecture that reads its binary as a raw file 35 | class RawBinaryArchitecture : public SleighArchitecture { 36 | long adjustvma; ///< What address byte 0 of the raw file gets treated as 37 | virtual void buildLoader(DocumentStorage &store); 38 | virtual void resolveArchitecture(void); 39 | virtual void postSpecFile(void); 40 | public: 41 | virtual void saveXml(ostream &s) const; 42 | virtual void restoreXml(DocumentStorage &store); 43 | RawBinaryArchitecture(const string &fname,const string &targ,ostream *estream); ///< Constructor 44 | virtual ~RawBinaryArchitecture(void) {} 45 | }; 46 | 47 | -------------------------------------------------------------------------------- /Ghidra9/sleighbase.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file sleighbase.hh 17 | /// \brief Base class for applications that process SLEIGH format specifications 18 | #ifndef __SLEIGHBASE__ 19 | #define __SLEIGHBASE__ 20 | 21 | #include "translate.hh" 22 | #include "slghsymbol.hh" 23 | 24 | /// \brief Common core of classes that read or write SLEIGH specification files natively. 25 | /// 26 | /// This class represents what's in common across the SLEIGH infrastructure between: 27 | /// - Reading the various SLEIGH specification files 28 | /// - Building and writing out SLEIGH specification files 29 | class SleighBase : public Translate { 30 | vector userop; ///< Names of user-define p-code ops for \b this Translate object 31 | map varnode_xref; ///< A map from Varnodes in the \e register space to register names 32 | protected: 33 | SubtableSymbol *root; ///< The root SLEIGH decoding symbol 34 | SymbolTable symtab; ///< The SLEIGH symbol table 35 | uint4 maxdelayslotbytes; ///< Maximum number of bytes in a delay-slot directive 36 | uint4 unique_allocatemask; ///< Bits that are guaranteed to be zero in the unique allocation scheme 37 | uint4 numSections; ///< Number of \e named sections 38 | void buildXrefs(void); ///< Build register map. Collect user-ops and context-fields. 39 | void reregisterContext(void); ///< Reregister context fields for a new executable 40 | void restoreXml(const Element *el); ///< Read a SLEIGH specification from XML 41 | public: 42 | SleighBase(void); ///< Construct an uninitialized translator 43 | bool isInitialized(void) const { return (root != (SubtableSymbol *)0); } ///< Return \b true if \b this is initialized 44 | virtual ~SleighBase(void) {} ///< Destructor 45 | virtual void addRegister(const string &nm,AddrSpace *base,uintb offset,int4 size); 46 | virtual const VarnodeData &getRegister(const string &nm) const; 47 | virtual string getRegisterName(AddrSpace *base,uintb off,int4 size) const; 48 | virtual void getAllRegisters(map ®list) const; 49 | virtual void getUserOpNames(vector &res) const; 50 | 51 | SleighSymbol *findSymbol(const string &nm) const { return symtab.findSymbol(nm); } ///< Find a specific SLEIGH symbol by name in the current scope 52 | SleighSymbol *findSymbol(uintm id) const { return symtab.findSymbol(id); } ///< Find a specific SLEIGH symbol by id 53 | SleighSymbol *findGlobalSymbol(const string &nm) const { return symtab.findGlobalSymbol(nm); } ///< Find a specific global SLEIGH symbol by name 54 | void saveXml(ostream &s) const; ///< Write out the SLEIGH specification as an XML \ tag. 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /Ghidra9/typegrp_ghidra.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * REVIEWED: YES 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #include "typegrp_ghidra.hh" 18 | 19 | Datatype *TypeFactoryGhidra::findById(const string &n,uint8 id) 20 | 21 | { 22 | Datatype *ct = TypeFactory::findById(n,id); // Try internal find 23 | if (ct != (Datatype *)0) return ct; 24 | 25 | Document *doc; 26 | try { 27 | doc = ((ArchitectureGhidra *)glb)->getType(n,id); // See if ghidra knows about type 28 | } 29 | catch(XmlError &err) { 30 | throw LowlevelError("XML error: "+err.explain); 31 | } 32 | if (doc == (Document *)0) return (Datatype *)0; 33 | ct = restoreXmlType(doc->getRoot()); // Parse ghidra's type 34 | delete doc; 35 | return ct; 36 | } 37 | -------------------------------------------------------------------------------- /Ghidra9/typegrp_ghidra.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file typegrp_ghidra.hh 17 | /// \brief Interface for requesting data-type information from a Ghidra client 18 | 19 | #ifndef __TYPEGRP_GHIDRA__ 20 | #define __TYPEGRP_GHIDRA__ 21 | 22 | #include "type.hh" 23 | #include "ghidra_arch.hh" 24 | 25 | /// \brief An implementation of the TypeFactory interface, query a Ghidra client for data-type information 26 | /// 27 | /// Requests for a specific data-type name and id are marshaled to the Ghidra client, 28 | /// which sends back a description of the data-type. The description is parsed and 29 | /// converted into a Datatype object and cached in this object. 30 | class TypeFactoryGhidra : public TypeFactory { 31 | protected: 32 | virtual Datatype *findById(const string &n,uint8 id); 33 | public: 34 | TypeFactoryGhidra(ArchitectureGhidra *g) : TypeFactory(g) {} ///< Constructor 35 | virtual ~TypeFactoryGhidra(void) {} 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Ghidra9/xml.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_XML_XML_TAB_H_INCLUDED 34 | # define YY_XML_XML_TAB_H_INCLUDED 35 | /* Enabling traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int xmldebug; 41 | #endif 42 | 43 | /* Tokens. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | /* Put the tokens into the symbol table, so that GDB and other debuggers 47 | know about them. */ 48 | enum yytokentype { 49 | CHARDATA = 258, 50 | CDATA = 259, 51 | ATTVALUE = 260, 52 | COMMENT = 261, 53 | CHARREF = 262, 54 | NAME = 263, 55 | SNAME = 264, 56 | ELEMBRACE = 265, 57 | COMMBRACE = 266 58 | }; 59 | #endif 60 | 61 | 62 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 63 | typedef union YYSTYPE 64 | { 65 | /* Line 2058 of yacc.c */ 66 | #line 103 "xml.y" 67 | 68 | int4 i; 69 | string *str; 70 | Attributes *attr; 71 | NameValue *pair; 72 | 73 | 74 | /* Line 2058 of yacc.c */ 75 | #line 76 "xml.tab.h" 76 | } YYSTYPE; 77 | # define YYSTYPE_IS_TRIVIAL 1 78 | # define yystype YYSTYPE /* obsolescent; will be withdrawn */ 79 | # define YYSTYPE_IS_DECLARED 1 80 | #endif 81 | 82 | extern YYSTYPE xmllval; 83 | 84 | #ifdef YYPARSE_PARAM 85 | #if defined __STDC__ || defined __cplusplus 86 | int xmlparse (void *YYPARSE_PARAM); 87 | #else 88 | int xmlparse (); 89 | #endif 90 | #else /* ! YYPARSE_PARAM */ 91 | #if defined __STDC__ || defined __cplusplus 92 | int xmlparse (void); 93 | #else 94 | int xmlparse (); 95 | #endif 96 | #endif /* ! YYPARSE_PARAM */ 97 | 98 | #endif /* !YY_XML_XML_TAB_H_INCLUDED */ 99 | -------------------------------------------------------------------------------- /Ghidra9/xml_arch.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file xml_arch.hh 17 | /// \brief Extension to read executables based on an XML format 18 | #include "sleigh_arch.hh" 19 | #include "loadimage_xml.hh" 20 | 21 | /// \brief Extension for building an XML format capable Architecture 22 | class XmlArchitectureCapability : public ArchitectureCapability { 23 | static XmlArchitectureCapability xmlArchitectureCapability; ///< The singleton instance 24 | XmlArchitectureCapability(void); ///< Singleton 25 | XmlArchitectureCapability(const XmlArchitectureCapability &op2); ///< Not implemented 26 | XmlArchitectureCapability &operator=(const XmlArchitectureCapability &op2); ///< Not implemented 27 | public: 28 | virtual Architecture *buildArchitecture(const string &filename,const string &target,ostream *estream); 29 | virtual bool isFileMatch(const string &filename) const; 30 | virtual bool isXmlMatch(Document *doc) const; 31 | }; 32 | 33 | /// \brief An Architecture that loads executables using an XML format 34 | class XmlArchitecture : public SleighArchitecture { 35 | long adjustvma; ///< The amount to adjust the virtual memory address 36 | virtual void buildLoader(DocumentStorage &store); 37 | // virtual void resolveArchitecture(void); ///< Inherit SleighArchitecture's version 38 | virtual void postSpecFile(void); 39 | public: 40 | virtual void saveXml(ostream &s) const; 41 | virtual void restoreXml(DocumentStorage &store); 42 | XmlArchitecture(const string &fname,const string &targ,ostream *estream); ///< Constructor 43 | virtual ~XmlArchitecture(void) {} 44 | }; 45 | -------------------------------------------------------------------------------- /GhidraDec-x64dbg/Branding.cpp: -------------------------------------------------------------------------------- 1 | /* The file is part of Snowman decompiler. */ 2 | /* See doc/licenses.asciidoc for the licensing information. */ 3 | 4 | #include "Branding.h" 5 | 6 | #include "Version.h" 7 | 8 | namespace nc { 9 | 10 | Branding branding() { 11 | Branding result; 12 | 13 | result.setApplicationName(QLatin1String("Nc")); 14 | result.setApplicationVersion(QLatin1String(version)); 15 | result.setOrganizationDomain(QLatin1String("derevenets.com")); 16 | result.setOrganizationName(result.organizationDomain()); 17 | result.setLicenseName(licenseName); 18 | result.setLicenseUrl(licenseUrl); 19 | result.setReportBugsTo(reportBugsTo); 20 | 21 | return result; 22 | } 23 | 24 | } // namespace nc 25 | 26 | /* vim:set et sts=4 sw=4: */ -------------------------------------------------------------------------------- /GhidraDec-x64dbg/Branding.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "config.h" 4 | 5 | #include 6 | 7 | namespace nc { 8 | 9 | class Branding { 10 | QString applicationName_; 11 | QString applicationVersion_; 12 | QString organizationDomain_; 13 | QString organizationName_; 14 | QString licenseName_; 15 | QString licenseUrl_; 16 | QString reportBugsTo_; 17 | 18 | public: 19 | const QString& applicationName() const { return applicationName_; } 20 | void setApplicationName(QString name) { applicationName_ = std::move(name); } 21 | 22 | const QString& applicationVersion() const { return applicationVersion_; } 23 | void setApplicationVersion(QString version) { applicationVersion_ = std::move(version); } 24 | 25 | const QString& organizationDomain() const { return organizationDomain_; } 26 | void setOrganizationDomain(QString domain) { organizationDomain_ = std::move(domain); } 27 | 28 | const QString& organizationName() const { return organizationName_; } 29 | void setOrganizationName(QString name) { organizationName_ = std::move(name); } 30 | 31 | const QString& licenseName() const { return licenseName_; } 32 | void setLicenseName(QString name) { licenseName_ = std::move(name); } 33 | 34 | const QString& licenseUrl() const { return licenseUrl_; } 35 | void setLicenseUrl(QString url) { licenseUrl_ = std::move(url); } 36 | 37 | const QString& reportBugsTo() const { return reportBugsTo_; } 38 | void setReportBugsTo(QString reportBugsTo) { reportBugsTo_ = reportBugsTo; } 39 | }; 40 | 41 | /** 42 | * \return Branding of the Nc library. 43 | */ 44 | Branding branding(); 45 | 46 | } // namespace nc 47 | 48 | /* vim:set et sts=4 sw=4: */ -------------------------------------------------------------------------------- /GhidraDec-x64dbg/GhidraDec-x64dbg.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | Source Files 44 | 45 | 46 | Source Files 47 | 48 | 49 | Source Files 50 | 51 | 52 | Source Files 53 | 54 | 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | Source Files 64 | 65 | 66 | Source Files 67 | 68 | 69 | Source Files 70 | 71 | 72 | Source Files 73 | 74 | 75 | Source Files 76 | 77 | 78 | Source Files 79 | 80 | 81 | Source Files 82 | 83 | 84 | Source Files 85 | 86 | 87 | 88 | 89 | Source Files 90 | 91 | 92 | -------------------------------------------------------------------------------- /GhidraDec-x64dbg/GhidraDecView.h: -------------------------------------------------------------------------------- 1 | #ifndef GHIDRADECVIEW_H 2 | #define GHIDRADECVIEW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | struct GhidraDecRange 11 | { 12 | duint start; 13 | duint end; 14 | }; 15 | 16 | class GhidraDecView : public QWidget 17 | { 18 | Q_OBJECT 19 | public: 20 | explicit GhidraDecView(QWidget* parent = nullptr); 21 | void decompileAt(const GhidraDecRange* ranges, duint count) const; 22 | 23 | protected: 24 | void closeEvent(QCloseEvent* event) override; 25 | 26 | private slots: 27 | void populateInstructionsContextMenu(QMenu* menu) const; 28 | void populateCxxContextMenu(QMenu* menu) const; 29 | void jumpFromInstructionsView() const; 30 | void jumpFromCxxView() const; 31 | 32 | private: 33 | QMainWindow* mGhidraDecMainWindow; 34 | QAction* mJumpFromInstructionsViewAction; 35 | QAction* mJumpFromCxxViewAction; 36 | }; 37 | 38 | #endif // GHIDRADECVIEW_H -------------------------------------------------------------------------------- /GhidraDec-x64dbg/PluginMain.cpp: -------------------------------------------------------------------------------- 1 | #include "pluginmain.h" 2 | #include "QtPlugin.h" 3 | 4 | #define plugin_name "GhidraDec" 5 | #define plugin_version 1 6 | 7 | int Plugin::handle; 8 | HWND Plugin::hwndDlg; 9 | int Plugin::hMenu; 10 | int Plugin::hMenuDisasm; 11 | int Plugin::hMenuDump; 12 | int Plugin::hMenuStack; 13 | int Plugin::hMenuGraph; 14 | int Plugin::hMenuMemmap; 15 | int Plugin::hMenuSymmod; 16 | 17 | extern "C" __declspec(dllexport) bool pluginit(PLUG_INITSTRUCT * initStruct) 18 | { 19 | initStruct->pluginVersion = plugin_version; 20 | initStruct->sdkVersion = PLUG_SDKVERSION; 21 | strcpy_s(initStruct->pluginName, plugin_name); 22 | 23 | Plugin::handle = initStruct->pluginHandle; 24 | QtPlugin::Init(); 25 | return true; 26 | } 27 | 28 | extern "C" __declspec(dllexport) void plugsetup(PLUG_SETUPSTRUCT * setupStruct) 29 | { 30 | Plugin::hwndDlg = setupStruct->hwndDlg; 31 | Plugin::hMenu = setupStruct->hMenu; 32 | Plugin::hMenuDisasm = setupStruct->hMenuDisasm; 33 | Plugin::hMenuDump = setupStruct->hMenuDump; 34 | Plugin::hMenuStack = setupStruct->hMenuStack; 35 | Plugin::hMenuGraph = setupStruct->hMenuGraph; 36 | Plugin::hMenuMemmap = setupStruct->hMenuMemmap; 37 | Plugin::hMenuSymmod = setupStruct->hMenuSymmod; 38 | GuiExecuteOnGuiThread(QtPlugin::Setup); 39 | QtPlugin::WaitForSetup(); 40 | } 41 | 42 | extern "C" __declspec(dllexport) bool plugstop() 43 | { 44 | GuiExecuteOnGuiThread(QtPlugin::Stop); 45 | QtPlugin::WaitForStop(); 46 | return true; 47 | } 48 | 49 | extern "C" __declspec(dllexport) void CBMENUPREPARE(CBTYPE, PLUG_CB_MENUPREPARE * info) 50 | { 51 | QtPlugin::MenuPrepare(info->hMenu); 52 | } 53 | 54 | extern "C" __declspec(dllexport) void CBMENUENTRY(CBTYPE, PLUG_CB_MENUENTRY * info) 55 | { 56 | QtPlugin::MenuEntry(info->hEntry); 57 | } -------------------------------------------------------------------------------- /GhidraDec-x64dbg/PluginMain.h: -------------------------------------------------------------------------------- 1 | #ifndef _PLUGINMAIN_H 2 | #define _PLUGINMAIN_H 3 | 4 | #include 5 | #include <_plugins.h> 6 | 7 | namespace Plugin 8 | { 9 | extern int handle; 10 | extern HWND hwndDlg; 11 | extern int hMenu; 12 | extern int hMenuDisasm; 13 | extern int hMenuDump; 14 | extern int hMenuStack; 15 | extern int hMenuGraph; 16 | extern int hMenuMemmap; 17 | extern int hMenuSymmod; 18 | } //Plugin 19 | 20 | #endif //_PLUGINMAIN_H -------------------------------------------------------------------------------- /GhidraDec-x64dbg/QtPlugin.h: -------------------------------------------------------------------------------- 1 | #ifndef QTPLUGIN_H 2 | #define QTPLUGIN_H 3 | 4 | #include "pluginmain.h" 5 | 6 | namespace QtPlugin 7 | { 8 | void Init(); 9 | void Setup(); 10 | void WaitForSetup(); 11 | void Stop(); 12 | void WaitForStop(); 13 | void ShowTab(); 14 | void MenuPrepare(int hMenu); 15 | void MenuEntry(int hMenu); 16 | } //QtPlugin 17 | 18 | #endif // QTPLUGIN_H -------------------------------------------------------------------------------- /GhidraDec-x64dbg/Version.h: -------------------------------------------------------------------------------- 1 | /* The file is part of Snowman decompiler. */ 2 | /* See doc/licenses.asciidoc for the licensing information. */ 3 | 4 | #pragma once 5 | 6 | namespace nc { 7 | 8 | /** Version of the product. */ 9 | extern const char version[]; 10 | 11 | /** Address for reporting bugs. */ 12 | extern const char reportBugsTo[]; 13 | 14 | /** Name of the license. */ 15 | extern const char licenseName[]; 16 | 17 | /** License URL. */ 18 | extern const char licenseUrl[]; 19 | 20 | } // namespace nc 21 | 22 | /* vim:set et sts=4 sw=4: */ -------------------------------------------------------------------------------- /GhidraDec-x64dbg/dllmain.cpp: -------------------------------------------------------------------------------- 1 | // dllmain.cpp : Defines the entry point for the DLL application. 2 | #include "pch.h" 3 | 4 | /*BOOL APIENTRY DllMain(HMODULE hModule, 5 | DWORD ul_reason_for_call, 6 | LPVOID lpReserved 7 | ) 8 | { 9 | switch (ul_reason_for_call) 10 | { 11 | case DLL_PROCESS_ATTACH: 12 | case DLL_THREAD_ATTACH: 13 | case DLL_THREAD_DETACH: 14 | case DLL_PROCESS_DETACH: 15 | break; 16 | } 17 | return TRUE; 18 | } 19 | 20 | */ -------------------------------------------------------------------------------- /GhidraDec-x64dbg/framework.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 4 | // Windows Header Files 5 | #include 6 | -------------------------------------------------------------------------------- /GhidraDec-x64dbg/images/GhidraIcon16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GregoryMorse/GhidraDec/9f8c72493ecb442b3eac41b69c01d26be5e674da/GhidraDec-x64dbg/images/GhidraIcon16.png -------------------------------------------------------------------------------- /GhidraDec-x64dbg/images/decompile_function.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GregoryMorse/GhidraDec/9f8c72493ecb442b3eac41b69c01d26be5e674da/GhidraDec-x64dbg/images/decompile_function.png -------------------------------------------------------------------------------- /GhidraDec-x64dbg/images/decompile_selection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GregoryMorse/GhidraDec/9f8c72493ecb442b3eac41b69c01d26be5e674da/GhidraDec-x64dbg/images/decompile_selection.png -------------------------------------------------------------------------------- /GhidraDec-x64dbg/pch.cpp: -------------------------------------------------------------------------------- 1 | // pch.cpp: source file corresponding to the pre-compiled header 2 | 3 | #include "pch.h" 4 | 5 | // When you are using pre-compiled headers, this source file is necessary for compilation to succeed. 6 | -------------------------------------------------------------------------------- /GhidraDec-x64dbg/pch.h: -------------------------------------------------------------------------------- 1 | // pch.h: This is a precompiled header file. 2 | // Files listed below are compiled only once, improving build performance for future builds. 3 | // This also affects IntelliSense performance, including code completion and many code browsing features. 4 | // However, files listed here are ALL re-compiled if any one of them is updated between builds. 5 | // Do not add files here that you will be updating frequently as this negates the performance advantage. 6 | 7 | #ifndef PCH_H 8 | #define PCH_H 9 | 10 | // add headers that you want to pre-compile here 11 | #include "framework.h" 12 | 13 | #endif //PCH_H 14 | -------------------------------------------------------------------------------- /GhidraDec-x64dbg/x64dbg-module.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/GhidraIcon16.png 4 | images/decompile_function.png 5 | images/decompile_selection.png 6 | 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Gregory Morse 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MakeGhidraDec.bat: -------------------------------------------------------------------------------- 1 | CD /D D:\source\repos\GhidraDec 2 | rmdir /s /q build 3 | mkdir build 4 | cd build 5 | SET SAVEPTH=%PATH% 6 | SET PATH=%PATH%;%ProgramFiles%\cmake\bin;%UserProfile%\Desktop\Apps\win_flex_bison 7 | 8 | del CMakeCache.txt 9 | cmake .. -DIDA_SDK_DIR=D:\libraries\idasdk77 10 | cmake --build . --config Release -- -m 11 | copy Release\*.dll "%UserProfile%\Desktop\Apps\IDA Pro 7.7\plugins" 12 | cd release 13 | mkdir ida77 14 | copy /Y *.dll ida77 15 | REM "%ProgramFiles%\7-zip\7z.exe" a -pexetools ghidradec.zip *.dll 16 | cd .. 17 | 18 | del CMakeCache.txt 19 | cmake .. -DIDA_SDK_DIR=D:\libraries\idasdk76 20 | cmake --build . --config Release -- -m 21 | copy Release\*.dll "%UserProfile%\Desktop\Apps\IDA Pro 7.6\plugins" 22 | cd release 23 | mkdir ida76 24 | copy /Y *.dll ida76 25 | REM "%ProgramFiles%\7-zip\7z.exe" a -pexetools ghidradec.zip *.dll 26 | cd .. 27 | 28 | del CMakeCache.txt 29 | cmake .. -DIDA_SDK_DIR=D:\libraries\idasdk75 30 | cmake --build . --config Release -- -m 31 | copy Release\*.dll "%UserProfile%\Desktop\Apps\IDA Pro 7.5 SP3\plugins" 32 | cd release 33 | mkdir ida75 34 | copy /Y *.dll ida75 35 | REM "%ProgramFiles%\7-zip\7z.exe" a -pexetools ghidradec.zip *.dll 36 | cd .. 37 | 38 | del CMakeCache.txt 39 | cmake .. -DIDA_SDK_DIR=D:\libraries\idasdk72 40 | cmake --build . --config Release -- -m 41 | copy Release\*.dll "%ProgramFiles%\Ida 7.2\plugins" 42 | cd release 43 | mkdir ida72 44 | copy /Y *.dll ida72 45 | REM "%ProgramFiles%\7-zip\7z.exe" a -pexetools ghidradec.zip *.dll 46 | cd .. 47 | 48 | del CMakeCache.txt 49 | cmake .. -DIDA_SDK_DIR=D:\libraries\idasdk70 50 | cmake --build . --config Release -- -m 51 | copy Release\*.dll "%ProgramFiles%\Ida 7.0\plugins" 52 | cd release 53 | mkdir ida70 54 | copy /Y *.dll ida70 55 | REM "%ProgramFiles%\7-zip\7z.exe" a -pexetools ghidradec-ida70.zip *.dll 56 | cd .. 57 | 58 | REM default already seems to be: -G "Visual Studio 16 2019 Win64" -A x64 59 | del CMakeCache.txt 60 | del deps\jsoncpp\jsoncpp-project-prefix\src\jsoncpp-project-build\CMakeCache.txt 61 | rmdir /s /q deps\jsoncpp\CMakeFiles 62 | cmake .. -G "Visual Studio 16 2019" -A Win32 -DIDA_SDK_DIR32=D:\libraries\idasdk68 63 | cmake --build . --config Release -- -m 64 | move /Y Release\ghidradec64.p64 Release\ghidradec.p64 65 | copy Release\*.plw "%ProgramFiles (x86)%\Ida 6.8\plugins" 66 | copy Release\*.p64 "%ProgramFiles (x86)%\Ida 6.8\plugins" 67 | cd release 68 | mkdir ida68 69 | copy /Y *.p64 ida68 70 | copy /Y *.plw ida68 71 | REM "%ProgramFiles%\7-zip\7z.exe" a -pexetools ghidradec-ida68.zip *.plw 72 | cd.. 73 | 74 | cd release 75 | del ghidradec.zip 76 | "%ProgramFiles%\7-zip\7z.exe" a -pexetools ghidradec.zip ida* 77 | cd .. 78 | 79 | cd .. 80 | SET PATH=%SAVEPTH% 81 | SET SAVEPTH= -------------------------------------------------------------------------------- /PropertySheet.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | $(UserProfile)\Desktop\Apps\win_flex_bison 6 | D:\Libraries\idasdk77 7 | 8 | 9 | 10 | 11 | D:\Libraries\idasdk68 12 | $(UserProfile)\Desktop\Apps\IDA Pro 7.7 13 | 14 | 15 | 16 | 17 | $(ProgramFiles)\Ida 6.8 18 | 19 | 20 | 21 | 22 | 23 | $(WIN_FLEX_BISON_PATH) 24 | 25 | 26 | $(IDA_SDK_DIR) 27 | 28 | 29 | $(IDA_SDK_DIR32) 30 | 31 | 32 | $(IDA_DEPLOY) 33 | 34 | 35 | $(IDA_DEPLOY32) 36 | 37 | 38 | -------------------------------------------------------------------------------- /config_generator.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file idaplugin/config_generator.h 3 | * @brief Module contains classes/methods dealing with information export 4 | * from IDA Pro to Retargetable Decompiler config database. 5 | * @copyright (c) 2017 Avast Software, licensed under the MIT license 6 | */ 7 | 8 | #ifndef IDAPLUGIN_CONFIG_GENERATOR_H 9 | #define IDAPLUGIN_CONFIG_GENERATOR_H 10 | 11 | #include 12 | 13 | #include "defs.h" 14 | 15 | namespace idaplugin { 16 | 17 | /** 18 | * Read information from IDA SDK structures and store it into 19 | * retargetable decompiler's configuration database. 20 | */ 21 | class ConfigGenerator 22 | { 23 | public: 24 | ConfigGenerator(RdGlobalInfo &gi); 25 | std::string generate(); 26 | 27 | private: 28 | void generateHeader(); 29 | void generateFunctions(); 30 | void generateFunctionType( 31 | const tinfo_t& fncType, 32 | retdec::config::Function& ccFnc); 33 | void generateSegmentsAndGlobals(); 34 | retdec::config::Storage generateObjectLocation( 35 | const argloc_t& loc, 36 | const tinfo_t& locType); 37 | void generateCallingConvention( 38 | const cm_t &idaCC, 39 | retdec::config::CallingConvention &configCC); 40 | 41 | std::string addrType2string(ea_t addr); 42 | std::string type2string(const tinfo_t &type); 43 | std::string defaultTypeString(); 44 | 45 | private: 46 | RdGlobalInfo& decompInfo; 47 | /// Configuration object. 48 | retdec::config::Config &config; 49 | /// Global variables. 50 | std::map structIdSet; 51 | }; 52 | 53 | } // namespace idaplugin 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /cpp.hint: -------------------------------------------------------------------------------- 1 | #define GHIDRADEC_API __declspec(dllexport) 2 | #define GHIDRADEC_API __declspec(dllimport) 3 | -------------------------------------------------------------------------------- /decompile9/error.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file error.hh 17 | /// \brief Base class for error handling facilities 18 | /// 19 | /// This is also doubles as a place to list the common include files 20 | 21 | #ifndef __CPUI_ERROR__ 22 | #define __CPUI_ERROR__ 23 | 24 | #include "types.h" 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | using namespace std; 37 | 38 | /// \brief The lowest level error generated by the decompiler 39 | /// 40 | /// This is the base error for all exceptions thrown by the 41 | /// decompiler. This underived form is thrown for very low 42 | /// level errors that immediately abort decompilation (usually 43 | /// for just a single function). 44 | struct LowlevelError { 45 | string explain; ///< Explanatory string 46 | /// Initialize the error with an explanatory string 47 | LowlevelError(const string &s) { explain = s; } 48 | }; 49 | 50 | /// \brief A generic recoverable error 51 | /// 52 | /// This error is the most basic form of recoverable error, 53 | /// meaning there is some problem that the user did not take 54 | /// into account. 55 | struct RecovError : public LowlevelError { 56 | /// Initialize the error with an explanatory string 57 | RecovError(const string &s) : LowlevelError(s) {} 58 | }; 59 | 60 | /// \brief An error generated while parsing a command or language 61 | /// 62 | /// This error is generated when parsing character data of some 63 | /// form, as in a user command from the console or when parsing 64 | /// C syntax. 65 | struct ParseError : public LowlevelError { // Parsing error 66 | /// Initialize the error with an explanatory string 67 | ParseError(const string &s) : LowlevelError(s) {} 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /decompile9/filemanage.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | // Generic (POSIX) class for searching files and managing paths 17 | 18 | #ifndef __FILEMANAGE__ 19 | #define __FILEMANAGE__ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | class FileManage { 30 | vector pathlist; // List of paths to search for files 31 | static char separator; 32 | static string buildPath(const vector &pathels,int level); 33 | static bool testDevelopmentPath(const vector &pathels,int level,string &root); 34 | static bool testInstallPath(const vector &pathels,int level,string &root); 35 | public: 36 | void addDir2Path(const string &path); 37 | void addCurrentDir(void); 38 | void findFile(string &res,const string &name) const; // Resolve full pathname 39 | void matchList(vector &res,const string &match,bool isSuffix) const; // List of files with suffix 40 | static bool isDirectory(const string &path); 41 | static void matchListDir(vector &res,const string &match,bool isSuffix,const string &dir,bool allowdot); 42 | static void directoryList(vector &res,const string &dirname,bool allowdot=false); 43 | static void scanDirectoryRecursive(vector &res,const string &matchname,const string &rootpath,int maxdepth); 44 | static void splitPath(const string &full,string &path,string &base); 45 | static bool isAbsolutePath(const string &full) { if (full.empty()) return false; return (full[0] == separator); } 46 | static string discoverGhidraRoot(const char *argv0); 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /decompile9/loadimage.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "loadimage.hh" 17 | 18 | /// This is a convenience method wrapped around the core 19 | /// loadFill() routine. It automatically allocates an array 20 | /// of the desired size, and then fills it with load image data. 21 | /// If the array cannot be allocated, an exception is thrown. 22 | /// The caller assumes the responsibility of freeing the 23 | /// array after it has been used. 24 | /// \param size is the number of bytes to read from the image 25 | /// \param addr is the address of the first byte being read 26 | /// \return a pointer to the desired bytes 27 | uint1 *LoadImage::load(int4 size,const Address &addr) 28 | 29 | { 30 | uint1 *buf = new uint1[ size ]; 31 | if (buf == (uint1 *)0) 32 | throw LowlevelError("Out of memory"); 33 | loadFill(buf,size,addr); 34 | return buf; 35 | } 36 | 37 | RawLoadImage::RawLoadImage(const string &f) : LoadImage(f) 38 | 39 | { 40 | vma = 0; 41 | thefile = (ifstream *)0; 42 | spaceid = (AddrSpace *)0; 43 | filesize = 0; 44 | } 45 | 46 | RawLoadImage::~RawLoadImage(void) 47 | 48 | { 49 | if (thefile != (ifstream *)0) { 50 | thefile->close(); 51 | delete thefile; 52 | } 53 | } 54 | 55 | /// The file is opened and its size immediately recovered. 56 | void RawLoadImage::open(void) 57 | 58 | { 59 | if (thefile != (ifstream *)0) throw LowlevelError("loadimage is already open"); 60 | thefile = new ifstream(filename.c_str()); 61 | if (!(*thefile)) { 62 | string errmsg = "Unable to open raw image file: "+filename; 63 | throw LowlevelError(errmsg); 64 | } 65 | thefile->seekg(0,ios::end); 66 | filesize = thefile->tellg(); 67 | } 68 | 69 | string RawLoadImage::getArchType(void) const 70 | 71 | { 72 | return "unknown"; 73 | } 74 | 75 | void RawLoadImage::adjustVma(long adjust) 76 | 77 | { 78 | adjust = AddrSpace::addressToByte(adjust,spaceid->getWordSize()); 79 | vma += adjust; 80 | } 81 | 82 | void RawLoadImage::loadFill(uint1 *ptr,int4 size,const Address &addr) 83 | 84 | { 85 | uintb curaddr = addr.getOffset(); 86 | uintb offset = 0; 87 | uintb readsize; 88 | 89 | curaddr -= vma; // Get relative offset of first byte 90 | while(size>0) { 91 | if (curaddr >= filesize) { 92 | if (offset == 0) // Initial address not within file 93 | break; 94 | memset(ptr+offset,0,size); // Fill out the rest of the buffer with 0 95 | return; 96 | } 97 | readsize = size; 98 | if (curaddr + readsize > filesize) // Adjust to biggest possible read 99 | readsize = filesize - curaddr; 100 | thefile->seekg(curaddr); 101 | thefile->read((char *)(ptr+offset),readsize); 102 | offset += readsize; 103 | size -= readsize; 104 | curaddr += readsize; 105 | } 106 | if (size > 0) { 107 | ostringstream errmsg; 108 | errmsg << "Unable to load " << dec << size << " bytes at " << addr.getShortcut(); 109 | addr.printRaw(errmsg); 110 | throw DataUnavailError(errmsg.str()); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /decompile9/pcodeparse.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #ifndef __PCODE_SNIPPET__ 17 | #define __PCODE_SNIPPET__ 18 | 19 | #include "pcodecompile.hh" 20 | #include "sleighbase.hh" 21 | 22 | // Classes for compiling a standalone snippet of pcode, given an existing sleigh language 23 | 24 | struct IdentRec { 25 | const char *nm; 26 | int4 id; 27 | }; 28 | 29 | class PcodeLexer { 30 | public: 31 | enum { // Lexer states 32 | start, 33 | special2, // Middle of special 2 character operator 34 | special3, // First character of special 3 character operator 35 | special32, // Second character of special 3 character operator 36 | comment, // Middle of an endofline comment 37 | punctuation, // Punctuation character 38 | identifier, // Middle of an identifier 39 | hexstring, // Middle of a hexidecimal number 40 | decstring, // Middle of a decimal number 41 | endstream, // Reached end of stream 42 | illegal // Scanned an illegal character 43 | }; 44 | private: 45 | static const IdentRec idents[]; 46 | int4 curstate; 47 | char curchar,lookahead1,lookahead2; 48 | char curtoken[256]; 49 | int4 tokpos; 50 | bool endofstream; 51 | bool endofstreamsent; 52 | istream *s; 53 | string curidentifier; 54 | uintb curnum; 55 | void starttoken(void) { curtoken[0] = curchar; tokpos = 1; } 56 | void advancetoken(void) { curtoken[tokpos++] = curchar; } 57 | bool isIdent(char c) const { return (isalnum(c)||(c=='_')||(c=='.')); } 58 | bool isHex(char c) const { return isxdigit(c); } 59 | bool isDec(char c) const { return isdigit(c); } 60 | int4 findIdentifier(const string &str) const; 61 | int4 moveState(void); 62 | public: 63 | PcodeLexer(void) { s = (istream *)0; } 64 | void initialize(istream *t); 65 | int4 getNextToken(void); 66 | const string &getIdentifier(void) const { return curidentifier; } 67 | uintb getNumber(void) const { return curnum; } 68 | }; 69 | 70 | class PcodeSnippet : public PcodeCompile { 71 | PcodeLexer lexer; 72 | const SleighBase *sleigh; // Language from which we get symbols 73 | SymbolTree tree; // Symbols in the local scope of the snippet (temporaries) 74 | uintb tempbase; 75 | int4 errorcount; 76 | string firsterror; 77 | ConstructTpl *result; 78 | virtual uintb allocateTemp(void); 79 | virtual void addSymbol(SleighSymbol *sym); 80 | public: 81 | PcodeSnippet(const SleighBase *slgh); 82 | void setResult(ConstructTpl *res) { result = res; } 83 | ConstructTpl *releaseResult(void) { ConstructTpl *res = result; result = (ConstructTpl *)0; return res; } 84 | virtual ~PcodeSnippet(void); 85 | virtual void reportError(const string &msg); 86 | bool hasErrors(void) const { return (errorcount != 0); } 87 | const string getErrorMessage(void) const { return firsterror; } 88 | void setUniqueBase(uintb val) { tempbase = val; } 89 | uintb getUniqueBase(void) const { return tempbase; } 90 | void clear(void); 91 | int lex(void); 92 | bool parseStream(istream& s); 93 | void addOperand(const string &name,int4 index); 94 | }; 95 | 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /decompile9/pcoderaw.cc: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include "pcoderaw.hh" 17 | #include "translate.hh" 18 | 19 | /// Build this VarnodeData from an \b \ tag 20 | /// \param el is the parsed tag 21 | /// \param manage is the address space manager 22 | void VarnodeData::restoreXml(const Element *el,const AddrSpaceManager *manage) 23 | 24 | { 25 | space = (AddrSpace *)0; 26 | size = 0; 27 | int4 num = el->getNumAttributes(); 28 | for(int4 i=0;igetAttributeName(i)=="space") { 30 | space = manage->getSpaceByName(el->getAttributeValue(i)); 31 | if (space == (AddrSpace *)0) 32 | throw LowlevelError("Unknown space name: "+el->getAttributeValue(i)); 33 | offset = space->restoreXmlAttributes(el,size); 34 | return; 35 | } 36 | else if (el->getAttributeName(i)=="name") { 37 | const Translate *trans = manage->getDefaultSpace()->getTrans(); 38 | const VarnodeData &point(trans->getRegister(el->getAttributeValue(i))); 39 | *this = point; 40 | return; 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /decompile9/sleighbase.hh: -------------------------------------------------------------------------------- 1 | /* ### 2 | * IP: GHIDRA 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /// \file sleighbase.hh 17 | /// \brief Base class for applications that process SLEIGH format specifications 18 | #ifndef __SLEIGHBASE__ 19 | #define __SLEIGHBASE__ 20 | 21 | #include "translate.hh" 22 | #include "slghsymbol.hh" 23 | 24 | /// \brief Common core of classes that read or write SLEIGH specification files natively. 25 | /// 26 | /// This class represents what's in common across the SLEIGH infrastructure between: 27 | /// - Reading the various SLEIGH specification files 28 | /// - Building and writing out SLEIGH specification files 29 | class SleighBase : public Translate { 30 | vector userop; ///< Names of user-define p-code ops for \b this Translate object 31 | map varnode_xref; ///< A map from Varnodes in the \e register space to register names 32 | protected: 33 | SubtableSymbol *root; ///< The root SLEIGH decoding symbol 34 | SymbolTable symtab; ///< The SLEIGH symbol table 35 | uint4 maxdelayslotbytes; ///< Maximum number of bytes in a delay-slot directive 36 | uint4 unique_allocatemask; ///< Bits that are guaranteed to be zero in the unique allocation scheme 37 | uint4 numSections; ///< Number of \e named sections 38 | void buildXrefs(void); ///< Build register map. Collect user-ops and context-fields. 39 | void reregisterContext(void); ///< Reregister context fields for a new executable 40 | void restoreXml(const Element *el); ///< Read a SLEIGH specification from XML 41 | public: 42 | SleighBase(void); ///< Construct an uninitialized translator 43 | bool isInitialized(void) const { return (root != (SubtableSymbol *)0); } ///< Return \b true if \b this is initialized 44 | virtual ~SleighBase(void) {} ///< Destructor 45 | virtual void addRegister(const string &nm,AddrSpace *base,uintb offset,int4 size); 46 | virtual const VarnodeData &getRegister(const string &nm) const; 47 | virtual string getRegisterName(AddrSpace *base,uintb off,int4 size) const; 48 | virtual void getAllRegisters(map ®list) const; 49 | virtual void getUserOpNames(vector &res) const; 50 | 51 | SleighSymbol *findSymbol(const string &nm) const { return symtab.findSymbol(nm); } ///< Find a specific SLEIGH symbol by name in the current scope 52 | SleighSymbol *findSymbol(uintm id) const { return symtab.findSymbol(id); } ///< Find a specific SLEIGH symbol by id 53 | SleighSymbol *findGlobalSymbol(const string &nm) const { return symtab.findGlobalSymbol(nm); } ///< Find a specific global SLEIGH symbol by name 54 | void saveXml(ostream &s) const; ///< Write out the SLEIGH specification as an XML \ tag. 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /decompile9/xml.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_XML_XML_TAB_H_INCLUDED 34 | # define YY_XML_XML_TAB_H_INCLUDED 35 | /* Enabling traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int xmldebug; 41 | #endif 42 | 43 | /* Tokens. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | /* Put the tokens into the symbol table, so that GDB and other debuggers 47 | know about them. */ 48 | enum yytokentype { 49 | CHARDATA = 258, 50 | CDATA = 259, 51 | ATTVALUE = 260, 52 | COMMENT = 261, 53 | CHARREF = 262, 54 | NAME = 263, 55 | SNAME = 264, 56 | ELEMBRACE = 265, 57 | COMMBRACE = 266 58 | }; 59 | #endif 60 | 61 | 62 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 63 | typedef union YYSTYPE 64 | { 65 | /* Line 2058 of yacc.c */ 66 | #line 103 "xml.y" 67 | 68 | int4 i; 69 | string *str; 70 | Attributes *attr; 71 | NameValue *pair; 72 | 73 | 74 | /* Line 2058 of yacc.c */ 75 | #line 76 "xml.tab.h" 76 | } YYSTYPE; 77 | # define YYSTYPE_IS_TRIVIAL 1 78 | # define yystype YYSTYPE /* obsolescent; will be withdrawn */ 79 | # define YYSTYPE_IS_DECLARED 1 80 | #endif 81 | 82 | extern YYSTYPE xmllval; 83 | 84 | #ifdef YYPARSE_PARAM 85 | #if defined __STDC__ || defined __cplusplus 86 | int xmlparse (void *YYPARSE_PARAM); 87 | #else 88 | int xmlparse (); 89 | #endif 90 | #else /* ! YYPARSE_PARAM */ 91 | #if defined __STDC__ || defined __cplusplus 92 | int xmlparse (void); 93 | #else 94 | int xmlparse (); 95 | #endif 96 | #endif /* ! YYPARSE_PARAM */ 97 | 98 | #endif /* !YY_XML_XML_TAB_H_INCLUDED */ 99 | -------------------------------------------------------------------------------- /decompile9/xml.tab.hpp: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_XML_D_SOURCE_REPOS_GHIDRADEC_DECOMPILE_XML_TAB_HPP_INCLUDED 34 | # define YY_XML_D_SOURCE_REPOS_GHIDRADEC_DECOMPILE_XML_TAB_HPP_INCLUDED 35 | /* Enabling traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int xmldebug; 41 | #endif 42 | 43 | /* Tokens. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | /* Put the tokens into the symbol table, so that GDB and other debuggers 47 | know about them. */ 48 | enum yytokentype { 49 | CHARDATA = 258, 50 | CDATA = 259, 51 | ATTVALUE = 260, 52 | COMMENT = 261, 53 | CHARREF = 262, 54 | NAME = 263, 55 | SNAME = 264, 56 | ELEMBRACE = 265, 57 | COMMBRACE = 266 58 | }; 59 | #endif 60 | 61 | 62 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 63 | typedef union YYSTYPE 64 | { 65 | /* Line 2058 of yacc.c */ 66 | #line 103 "D:/Source/Repos/GhidraDec/decompile/xml.y" 67 | 68 | int4 i; 69 | string *str; 70 | Attributes *attr; 71 | NameValue *pair; 72 | 73 | 74 | /* Line 2058 of yacc.c */ 75 | #line 76 "D:/Source/Repos/GhidraDec/decompile/xml.tab.hpp" 76 | } YYSTYPE; 77 | # define YYSTYPE_IS_TRIVIAL 1 78 | # define yystype YYSTYPE /* obsolescent; will be withdrawn */ 79 | # define YYSTYPE_IS_DECLARED 1 80 | #endif 81 | 82 | extern YYSTYPE xmllval; 83 | 84 | #ifdef YYPARSE_PARAM 85 | #if defined __STDC__ || defined __cplusplus 86 | int xmlparse (void *YYPARSE_PARAM); 87 | #else 88 | int xmlparse (); 89 | #endif 90 | #else /* ! YYPARSE_PARAM */ 91 | #if defined __STDC__ || defined __cplusplus 92 | int xmlparse (void); 93 | #else 94 | int xmlparse (); 95 | #endif 96 | #endif /* ! YYPARSE_PARAM */ 97 | 98 | #endif /* !YY_XML_D_SOURCE_REPOS_GHIDRADEC_DECOMPILE_XML_TAB_HPP_INCLUDED */ 99 | -------------------------------------------------------------------------------- /deps/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(jsoncpp) 2 | add_subdirectory(retdec) -------------------------------------------------------------------------------- /deps/jsoncpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(ExternalProject) 2 | 3 | if(CMAKE_C_COMPILER) 4 | set(CMAKE_C_COMPILER_OPTION "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}") 5 | endif() 6 | if(CMAKE_CXX_COMPILER) 7 | set(CMAKE_CXX_COMPILER_OPTION "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}") 8 | set(CMAKE_CXX_FLAGS_OPTION "-DCMAKE_CXX_FLAGS=-fPIC") 9 | endif() 10 | 11 | ExternalProject_Add(jsoncpp-project 12 | URL https://github.com/open-source-parsers/jsoncpp/archive/1.8.4.zip 13 | URL_HASH SHA256=2979436dbd4c48a3284dca9fa8f212298425ba3920ed6bacdda8905a94b111a8 14 | DOWNLOAD_NAME jsoncpp.zip 15 | CMAKE_ARGS 16 | # This does not work on MSVC, but may be useful on Linux. 17 | -DCMAKE_BUILD_TYPE=Release 18 | # Force the use of the same compiler as used to build the top-level 19 | # project. Otherwise, the external project may pick up a different 20 | # compiler, which may result in link errors. 21 | "${CMAKE_C_COMPILER_OPTION}" 22 | "${CMAKE_CXX_COMPILER_OPTION}" 23 | "${CMAKE_CXX_FLAGS_OPTION}" 24 | # Disable unused options. 25 | -DJSONCPP_WITH_TESTS=OFF 26 | -DJSONCPP_WITH_POST_BUILD_UNITTEST=OFF 27 | -DJSONCPP_WITH_PKGCONFIG_SUPPORT=OFF 28 | -DBUILD_SHARED_LIBS=OFF 29 | # Disable the update step. 30 | UPDATE_COMMAND "" 31 | # Disable the install step. 32 | INSTALL_COMMAND "" 33 | LOG_DOWNLOAD ON 34 | LOG_CONFIGURE ON 35 | LOG_BUILD ON 36 | ) 37 | 38 | # Set include directories. 39 | ExternalProject_Get_Property(jsoncpp-project source_dir) 40 | set(JSONCPP_INCLUDE_DIR ${source_dir}/include) 41 | 42 | # Add libraries. 43 | ExternalProject_Get_Property(jsoncpp-project binary_dir) 44 | 45 | if(MSVC) 46 | set(DEBUG_DIR "Debug/") 47 | set(RELEASE_DIR "Release/") 48 | endif() 49 | 50 | add_library(jsoncpp INTERFACE) 51 | target_link_libraries(jsoncpp INTERFACE debug ${binary_dir}/src/lib_json/${DEBUG_DIR}${CMAKE_STATIC_LIBRARY_PREFIX}jsoncpp${CMAKE_STATIC_LIBRARY_SUFFIX}) 52 | target_link_libraries(jsoncpp INTERFACE optimized ${binary_dir}/src/lib_json/${RELEASE_DIR}${CMAKE_STATIC_LIBRARY_PREFIX}jsoncpp${CMAKE_STATIC_LIBRARY_SUFFIX}) 53 | target_include_directories(jsoncpp SYSTEM INTERFACE ${JSONCPP_INCLUDE_DIR}) 54 | add_dependencies(jsoncpp jsoncpp-project) -------------------------------------------------------------------------------- /deps/retdec/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(ExternalProject) 2 | 3 | ExternalProject_Add(retdec-project 4 | URL https://github.com/avast/retdec/archive/10c0ddcf5e0fdb8ee4c5256787a0fa74bd069883.zip 5 | URL_HASH SHA256=2e2112ac8d5a31721c2cafe3d7657b13b5efaed9b22486e20fd2ac8de79a7311 6 | # Disable the configure step. 7 | CONFIGURE_COMMAND "" 8 | # Disable the update step. 9 | UPDATE_COMMAND "" 10 | # Disable the build step. 11 | BUILD_COMMAND "" 12 | # Disable the install step. 13 | INSTALL_COMMAND "" 14 | LOG_DOWNLOAD ON 15 | LOG_CONFIGURE ON 16 | LOG_BUILD ON 17 | ) 18 | 19 | # Set include directories. 20 | ExternalProject_Get_Property(retdec-project source_dir) 21 | ExternalProject_Get_Property(retdec-project binary_dir) 22 | 23 | set(RETDEC_UTILS_SOURCES 24 | ${source_dir}/src/utils/address.cpp 25 | ${source_dir}/src/utils/alignment.cpp 26 | ${source_dir}/src/utils/byte_value_storage.cpp 27 | ${source_dir}/src/utils/conversion.cpp 28 | ${source_dir}/src/utils/file_io.cpp 29 | ${source_dir}/src/utils/filesystem_path.cpp 30 | ${source_dir}/src/utils/math.cpp 31 | ${source_dir}/src/utils/string.cpp 32 | ${source_dir}/src/utils/system.cpp 33 | ${source_dir}/src/utils/time.cpp 34 | ) 35 | set(RETDEC_CONFIG_SOURCES 36 | ${source_dir}/src/config/architecture.cpp 37 | ${source_dir}/src/config/base.cpp 38 | ${source_dir}/src/config/calling_convention.cpp 39 | ${source_dir}/src/config/classes.cpp 40 | ${source_dir}/src/config/config.cpp 41 | ${source_dir}/src/config/file_format.cpp 42 | ${source_dir}/src/config/file_type.cpp 43 | ${source_dir}/src/config/functions.cpp 44 | ${source_dir}/src/config/language.cpp 45 | ${source_dir}/src/config/objects.cpp 46 | ${source_dir}/src/config/parameters.cpp 47 | ${source_dir}/src/config/patterns.cpp 48 | ${source_dir}/src/config/segments.cpp 49 | ${source_dir}/src/config/storage.cpp 50 | ${source_dir}/src/config/tool_info.cpp 51 | ${source_dir}/src/config/types.cpp 52 | ${source_dir}/src/config/vtables.cpp 53 | ) 54 | 55 | add_custom_command(OUTPUT ${RETDEC_UTILS_SOURCES} ${RETDEC_CONFIG_SOURCES} DEPENDS retdec-project) 56 | 57 | # Add libraries. 58 | add_library(retdec ${RETDEC_UTILS_SOURCES} ${RETDEC_CONFIG_SOURCES}) 59 | add_dependencies(retdec retdec-project) 60 | target_include_directories(retdec PUBLIC ${source_dir}/include) 61 | target_link_libraries(retdec jsoncpp) 62 | -------------------------------------------------------------------------------- /extractghidra.bat: -------------------------------------------------------------------------------- 1 | "%PROGRAMFILES%\7-zip\7z.exe" x -------------------------------------------------------------------------------- /framework.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 4 | // Windows Header Files 5 | #include 6 | -------------------------------------------------------------------------------- /plugin_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file idaplugin/plugin_config.h 3 | * @brief Module deals with RetDec plugin configuration. 4 | * @copyright (c) 2017 Avast Software, licensed under the MIT license 5 | */ 6 | 7 | #ifndef IDAPLUGIN_PLUGIN_CONFIG_H 8 | #define IDAPLUGIN_PLUGIN_CONFIG_H 9 | 10 | #include "defs.h" 11 | 12 | namespace idaplugin { 13 | 14 | bool pluginConfigurationMenu(RdGlobalInfo& rdgi); 15 | 16 | struct show_options_ah_t : public action_handler_t 17 | { 18 | show_options_ah_t(RdGlobalInfo* i) : rdgi(i) {} 19 | 20 | virtual int idaapi activate(action_activation_ctx_t *) 21 | { 22 | pluginConfigurationMenu(*rdgi); 23 | return false; 24 | } 25 | 26 | virtual action_state_t idaapi update(action_update_ctx_t *) 27 | { 28 | return AST_ENABLE_ALWAYS; 29 | } 30 | 31 | RdGlobalInfo* rdgi = nullptr; 32 | }; 33 | 34 | bool addConfigurationMenuOption(RdGlobalInfo& rdgi); 35 | 36 | bool readConfigFile(RdGlobalInfo& rdgi); 37 | void saveConfigTofile(RdGlobalInfo& rdgi); 38 | 39 | } // namespace idaplugin 40 | 41 | #endif 42 | --------------------------------------------------------------------------------