Package ghidra.program.model.lang
Class BasicCompilerSpec
- java.lang.Object
-
- ghidra.program.model.lang.BasicCompilerSpec
-
- All Implemented Interfaces:
CompilerSpec
- Direct Known Subclasses:
ProgramCompilerSpec
public class BasicCompilerSpec extends java.lang.Object implements CompilerSpec
BasicCompilerSpec implements the CompilerSpec interface based on static information from a particular .cspec file. Typically the .cspec file is read in once by a Language object whenever a new or opened Program indicates a particular language and compiler. The BasicCompilerSpec is owned by the Language and (parts of it) may be reused by multiple Programs.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface ghidra.program.model.lang.CompilerSpec
CompilerSpec.EvaluationModelType
-
-
Field Summary
Fields Modifier and Type Field Description protected PrototypeModel[]
allmodels
static int
CONSTANT_SPACE_INDEX
protected PrototypeModel
defaultModel
protected PrototypeModel
evalCalledModel
protected PrototypeModel
evalCurrentModel
static java.lang.String
JOIN_SPACE_NAME
protected PrototypeModel[]
models
static int
OTHER_SPACE_INDEX
static java.lang.String
OTHER_SPACE_NAME
protected PcodeInjectLibrary
pcodeInject
static java.lang.String
STACK_SPACE_NAME
-
Fields inherited from interface ghidra.program.model.lang.CompilerSpec
CALLING_CONVENTION_cdecl, CALLING_CONVENTION_fastcall, CALLING_CONVENTION_pascal, CALLING_CONVENTION_stdcall, CALLING_CONVENTION_thiscall, CALLING_CONVENTION_vectorcall
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BasicCompilerSpec(BasicCompilerSpec op2)
Clone the spec so that program can safely extend it without affecting the base spec from Language.BasicCompilerSpec(CompilerSpecDescription description, SleighLanguage language, ResourceFile cspecFile)
Read in the specification from an XML file.BasicCompilerSpec(CompilerSpecDescription description, SleighLanguage language, java.io.InputStream stream)
Construct the specification from an XML stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
applyContextSettings(DefaultProgramContext programContext)
Apply context settings to the ProgramContext as specified by the configurationboolean
doesCDataTypeConversions()
Return true if function prototypes respect the C-language data-type conversion conventions.boolean
equals(java.lang.Object obj)
PrototypeModel
findBestCallingConvention(Parameter[] params)
Find the best guess at a calling convention model from this compiler spec given an ordered list of (potential) parameters.AddressSpace
getAddressSpace(java.lang.String spaceName)
Get an address space by name.PrototypeModel[]
getAllModels()
PrototypeModel
getCallingConvention(java.lang.String name)
Returns the Calling Convention Model with the given name.PrototypeModel[]
getCallingConventions()
CompilerSpecDescription
getCompilerSpecDescription()
CompilerSpecID
getCompilerSpecID()
DataOrganization
getDataOrganization()
DecompilerLanguage
getDecompilerOutputLanguage()
Get the language that the decompiler producesPrototypeModel
getDefaultCallingConvention()
Returns the prototype model that is the default calling convention or else null.protected static org.xml.sax.ErrorHandler
getErrorHandler(java.lang.String docTitle)
Generate an XML error handler suitable for parsing a specification document.Language
getLanguage()
Get the Language this compiler spec is based on.PcodeInjectLibrary
getPcodeInjectLibrary()
java.lang.String
getProperty(java.lang.String key)
Gets a property defined for this language, or null if that property isn't defined.java.lang.String
getProperty(java.lang.String key, java.lang.String defaultString)
Gets the value of a property as a String, returning defaultString if undefined.boolean
getPropertyAsBoolean(java.lang.String key, boolean defaultBoolean)
Gets the value of a property as a boolean, returning defaultBoolean if undefined.int
getPropertyAsInt(java.lang.String key, int defaultInt)
Gets the value of a property as an int, returning defaultInt if undefined.java.util.Set<java.lang.String>
getPropertyKeys()
Returns a read-only set view of the property keys defined on this language.PrototypeModel
getPrototypeEvaluationModel(CompilerSpec.EvaluationModelType modelType)
Get the evaluation model matching the given type.AddressSpace
getStackBaseSpace()
Get the physical space used for stack data storageRegister
getStackPointer()
Get the default Stack Pointer register for this language if there is one.AddressSpace
getStackSpace()
Get the stack address space defined by this specificationjava.lang.String
getXMLString()
Convenience method to marshal this entire object, via saveXml, into a String object.int
hashCode()
boolean
hasProperty(java.lang.String key)
Returns whether this language has a property defined.boolean
isGlobal(Address addr)
boolean
isStackRightJustified()
Indicates whether variables are right-justified within the stack alignment.protected static void
markPrototypeAsExtension(PrototypeModel model)
Mark a given PrototypeModel as a Program specific extensionPrototypeModel
matchConvention(GenericCallingConvention genericCallingConvention)
Get the PrototypeModel corresponding to the given generic calling conventionprotected void
modelXrefs(java.util.List<PrototypeModel> modelList, java.lang.String defaultName, java.lang.String evalCurrent, java.lang.String evalCalled)
Establish cross referencing to prototype models.protected void
registerProgramInject(java.util.List<InjectPayloadSleigh> injectExtensions)
Register Program based InjectPayloads with the p-code library.protected void
removeProgramMechanismPayloads(java.util.Collection<PrototypeModel> modelList)
Remove any call mechanism injections associated with the given list of PrototypeModelsvoid
saveXml(java.lang.StringBuilder buffer)
Marshal this entire specification to an XML stream.boolean
stackGrowsNegative()
-
-
-
Field Detail
-
STACK_SPACE_NAME
public static final java.lang.String STACK_SPACE_NAME
- See Also:
- Constant Field Values
-
JOIN_SPACE_NAME
public static final java.lang.String JOIN_SPACE_NAME
- See Also:
- Constant Field Values
-
OTHER_SPACE_NAME
public static final java.lang.String OTHER_SPACE_NAME
- See Also:
- Constant Field Values
-
CONSTANT_SPACE_INDEX
public static final int CONSTANT_SPACE_INDEX
- See Also:
- Constant Field Values
-
OTHER_SPACE_INDEX
public static final int OTHER_SPACE_INDEX
- See Also:
- Constant Field Values
-
defaultModel
protected PrototypeModel defaultModel
-
evalCurrentModel
protected PrototypeModel evalCurrentModel
-
evalCalledModel
protected PrototypeModel evalCalledModel
-
allmodels
protected PrototypeModel[] allmodels
-
models
protected PrototypeModel[] models
-
pcodeInject
protected PcodeInjectLibrary pcodeInject
-
-
Constructor Detail
-
BasicCompilerSpec
public BasicCompilerSpec(CompilerSpecDescription description, SleighLanguage language, java.io.InputStream stream) throws XmlParseException, org.xml.sax.SAXException, java.io.IOException
Construct the specification from an XML stream. This is currently only used for testing.- Parameters:
description
- is the .ldefs description matching this specificationlanguage
- is the language that owns the specificationstream
- is the XML stream- Throws:
XmlParseException
- for badly formed XMLorg.xml.sax.SAXException
- for syntax errors in the XMLjava.io.IOException
- for errors accessing the stream
-
BasicCompilerSpec
public BasicCompilerSpec(CompilerSpecDescription description, SleighLanguage language, ResourceFile cspecFile) throws CompilerSpecNotFoundException
Read in the specification from an XML file.- Parameters:
description
- is the .ldefs description associated with the specificationlanguage
- is the language owning the specificationcspecFile
- is the XML file- Throws:
CompilerSpecNotFoundException
- for any form of error preventing the specification from being loaded.
-
BasicCompilerSpec
protected BasicCompilerSpec(BasicCompilerSpec op2)
Clone the spec so that program can safely extend it without affecting the base spec from Language.- Parameters:
op2
- is the spec to clone
-
-
Method Detail
-
getErrorHandler
protected static org.xml.sax.ErrorHandler getErrorHandler(java.lang.String docTitle)
Generate an XML error handler suitable for parsing a specification document. - Warnings are logged. - Errors cause a SAXParseException- Parameters:
docTitle
- is the title of the document- Returns:
- the error handler object
-
getXMLString
public java.lang.String getXMLString()
Convenience method to marshal this entire object, via saveXml, into a String object.- Returns:
- a String containing this entire spec as an XML document.
-
applyContextSettings
public void applyContextSettings(DefaultProgramContext programContext)
Description copied from interface:CompilerSpec
Apply context settings to the ProgramContext as specified by the configuration- Specified by:
applyContextSettings
in interfaceCompilerSpec
- Parameters:
programContext
- is the ProgramContext
-
getCompilerSpecID
public CompilerSpecID getCompilerSpecID()
- Specified by:
getCompilerSpecID
in interfaceCompilerSpec
- Returns:
- the id string associated with this compiler spec;
-
doesCDataTypeConversions
public boolean doesCDataTypeConversions()
Description copied from interface:CompilerSpec
Return true if function prototypes respect the C-language data-type conversion conventions. This amounts to converting array data-types to pointer-to-element data-types. In C, arrays are passed by reference (structures are still passed by value)- Specified by:
doesCDataTypeConversions
in interfaceCompilerSpec
- Returns:
- if the prototype does C-language data-type conversions
-
getCallingConventions
public PrototypeModel[] getCallingConventions()
- Specified by:
getCallingConventions
in interfaceCompilerSpec
- Returns:
- an array of the prototype models. Each prototype model specifies a calling convention.
-
getCallingConvention
public PrototypeModel getCallingConvention(java.lang.String name)
Description copied from interface:CompilerSpec
Returns the Calling Convention Model with the given name.- Specified by:
getCallingConvention
in interfaceCompilerSpec
- Parameters:
name
- the name of the calling convention to retrieve- Returns:
- the calling convention with the given name or null if there is none with that name.
-
getAllModels
public PrototypeModel[] getAllModels()
- Specified by:
getAllModels
in interfaceCompilerSpec
- Returns:
- all possible PrototypeModels, including calling conventions and merge models
-
getDefaultCallingConvention
public PrototypeModel getDefaultCallingConvention()
Description copied from interface:CompilerSpec
Returns the prototype model that is the default calling convention or else null.- Specified by:
getDefaultCallingConvention
in interfaceCompilerSpec
- Returns:
- the default calling convention or null.
-
getDecompilerOutputLanguage
public DecompilerLanguage getDecompilerOutputLanguage()
Description copied from interface:CompilerSpec
Get the language that the decompiler produces- Specified by:
getDecompilerOutputLanguage
in interfaceCompilerSpec
- Returns:
- an enum specifying the language
-
getPrototypeEvaluationModel
public PrototypeModel getPrototypeEvaluationModel(CompilerSpec.EvaluationModelType modelType)
Description copied from interface:CompilerSpec
Get the evaluation model matching the given type. If analysis needs to apply a PrototypeModel to a function but a specific model is not known, then this method can be used to select a putative PrototypeModel based on the analysis use-case: - EVAL_CURRENT indicates the model to use for the "current function" being analyzed - EVAL_CALLED indicates the model to use for a function called by the current function- Specified by:
getPrototypeEvaluationModel
in interfaceCompilerSpec
- Parameters:
modelType
- is the type of evaluation model- Returns:
- prototype evaluation model
-
getStackPointer
public Register getStackPointer()
Description copied from interface:CompilerSpec
Get the default Stack Pointer register for this language if there is one.- Specified by:
getStackPointer
in interfaceCompilerSpec
- Returns:
- default stack pointer register.
-
isStackRightJustified
public boolean isStackRightJustified()
Description copied from interface:CompilerSpec
Indicates whether variables are right-justified within the stack alignment.- Specified by:
isStackRightJustified
in interfaceCompilerSpec
- Returns:
- true if right stack justification applies.
-
getStackSpace
public AddressSpace getStackSpace()
Description copied from interface:CompilerSpec
Get the stack address space defined by this specification- Specified by:
getStackSpace
in interfaceCompilerSpec
- Returns:
- stack address space
-
getStackBaseSpace
public AddressSpace getStackBaseSpace()
Description copied from interface:CompilerSpec
Get the physical space used for stack data storage- Specified by:
getStackBaseSpace
in interfaceCompilerSpec
- Returns:
- address space which contains the stack
-
stackGrowsNegative
public boolean stackGrowsNegative()
- Specified by:
stackGrowsNegative
in interfaceCompilerSpec
- Returns:
- true if the stack grows with negative offsets
-
isGlobal
public boolean isGlobal(Address addr)
- Specified by:
isGlobal
in interfaceCompilerSpec
- Parameters:
addr
- is the (start of the) storage location- Returns:
- true if the specified storage location has been designated "global" in scope
-
getLanguage
public Language getLanguage()
Description copied from interface:CompilerSpec
Get the Language this compiler spec is based on. Note that compiler specs may be reused across multiple languages in the cspec files on disk, but once loaded in memory are actually separate objects. (M:N on disk, 1:N in memory)- Specified by:
getLanguage
in interfaceCompilerSpec
- Returns:
- the language this compiler spec is based on
-
getCompilerSpecDescription
public CompilerSpecDescription getCompilerSpecDescription()
- Specified by:
getCompilerSpecDescription
in interfaceCompilerSpec
- Returns:
- a brief description of the compiler spec
-
getAddressSpace
public AddressSpace getAddressSpace(java.lang.String spaceName)
Description copied from interface:CompilerSpec
Get an address space by name. This can be value added over the normal AddressFactory.getAddressSpace routine because the compiler spec can refer to special internal spaces like the stack space- Specified by:
getAddressSpace
in interfaceCompilerSpec
- Parameters:
spaceName
- is the name of the address space- Returns:
- the corresponding AddressSpace object
-
modelXrefs
protected void modelXrefs(java.util.List<PrototypeModel> modelList, java.lang.String defaultName, java.lang.String evalCurrent, java.lang.String evalCalled)
Establish cross referencing to prototype models. All xrefs are regenerated from a single complete list of PrototypeModels- Parameters:
modelList
- is the complete list of modelsdefaultName
- is the name to use for the default model (or null)evalCurrent
- is the name to use for evaluating the current function (or null)evalCalled
- is the name to use for evaluating called functions (or null)
-
saveXml
public void saveXml(java.lang.StringBuilder buffer)
Marshal this entire specification to an XML stream. An XML document is written with root tag \. - Parameters:
buffer
- is the XML stream
-
getDataOrganization
public DataOrganization getDataOrganization()
- Specified by:
getDataOrganization
in interfaceCompilerSpec
-
matchConvention
public PrototypeModel matchConvention(GenericCallingConvention genericCallingConvention)
Description copied from interface:CompilerSpec
Get the PrototypeModel corresponding to the given generic calling convention- Specified by:
matchConvention
in interfaceCompilerSpec
- Parameters:
genericCallingConvention
- is the given generic calling convention- Returns:
- the matching model or the defaultModel if nothing matches
-
findBestCallingConvention
public PrototypeModel findBestCallingConvention(Parameter[] params)
Description copied from interface:CompilerSpec
Find the best guess at a calling convention model from this compiler spec given an ordered list of (potential) parameters.- Specified by:
findBestCallingConvention
in interfaceCompilerSpec
- Parameters:
params
- is the ordered list of parameters- Returns:
- prototype model corresponding to the specified function signature
-
getProperty
public java.lang.String getProperty(java.lang.String key)
Description copied from interface:CompilerSpec
Gets a property defined for this language, or null if that property isn't defined.- Specified by:
getProperty
in interfaceCompilerSpec
- Parameters:
key
- the property key- Returns:
- the property value, or null if not defined
-
getPropertyKeys
public java.util.Set<java.lang.String> getPropertyKeys()
Description copied from interface:CompilerSpec
Returns a read-only set view of the property keys defined on this language.- Specified by:
getPropertyKeys
in interfaceCompilerSpec
- Returns:
- read-only set of property keys
-
getProperty
public java.lang.String getProperty(java.lang.String key, java.lang.String defaultString)
Description copied from interface:CompilerSpec
Gets the value of a property as a String, returning defaultString if undefined.- Specified by:
getProperty
in interfaceCompilerSpec
- Parameters:
key
- the property keydefaultString
- the default value to return if property is undefined- Returns:
- the property value as a String, or the default value if undefined
-
getPropertyAsBoolean
public boolean getPropertyAsBoolean(java.lang.String key, boolean defaultBoolean)
Description copied from interface:CompilerSpec
Gets the value of a property as a boolean, returning defaultBoolean if undefined.- Specified by:
getPropertyAsBoolean
in interfaceCompilerSpec
- Parameters:
key
- the property keydefaultBoolean
- the default value to return if property is undefined- Returns:
- the property value as a boolean, or the default value if undefined
-
getPropertyAsInt
public int getPropertyAsInt(java.lang.String key, int defaultInt)
Description copied from interface:CompilerSpec
Gets the value of a property as an int, returning defaultInt if undefined.- Specified by:
getPropertyAsInt
in interfaceCompilerSpec
- Parameters:
key
- the property keydefaultInt
- the default value to return if property is undefined- Returns:
- the property value as an int, or the default value if undefined
-
hasProperty
public boolean hasProperty(java.lang.String key)
Description copied from interface:CompilerSpec
Returns whether this language has a property defined.- Specified by:
hasProperty
in interfaceCompilerSpec
- Parameters:
key
- the property key- Returns:
- if the property is defined
-
getPcodeInjectLibrary
public PcodeInjectLibrary getPcodeInjectLibrary()
- Specified by:
getPcodeInjectLibrary
in interfaceCompilerSpec
-
removeProgramMechanismPayloads
protected void removeProgramMechanismPayloads(java.util.Collection<PrototypeModel> modelList)
Remove any call mechanism injections associated with the given list of PrototypeModels- Parameters:
modelList
- is the given list
-
registerProgramInject
protected void registerProgramInject(java.util.List<InjectPayloadSleigh> injectExtensions)
Register Program based InjectPayloads with the p-code library. This allows derived classes to extend the library- Parameters:
injectExtensions
- is the list of payloads to register
-
markPrototypeAsExtension
protected static void markPrototypeAsExtension(PrototypeModel model)
Mark a given PrototypeModel as a Program specific extension- Parameters:
model
- is the given PrototypeModel
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-