Package ghidra.program.model.lang
Class BasicCompilerSpec
java.lang.Object
ghidra.program.model.lang.BasicCompilerSpec
- All Implemented Interfaces:
CompilerSpec
- Direct Known Subclasses:
ProgramCompilerSpec
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
Modifier and TypeFieldDescriptionprotected PrototypeModel[]
protected PrototypeModel
protected PrototypeModel
protected PrototypeModel
protected PrototypeModel[]
protected PcodeInjectLibrary
protected Varnode
Fields inherited from interface ghidra.program.model.lang.CompilerSpec
CALLING_CONVENTION_cdecl, CALLING_CONVENTION_default, CALLING_CONVENTION_fastcall, CALLING_CONVENTION_pascal, CALLING_CONVENTION_rustcall, CALLING_CONVENTION_stdcall, CALLING_CONVENTION_thiscall, CALLING_CONVENTION_unknown, CALLING_CONVENTION_vectorcall
-
Constructor Summary
ConstructorDescriptionClone 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, InputStream stream) Construct the specification from an XML stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
applyContextSettings
(DefaultProgramContext programContext) Apply context settings to the ProgramContext as specified by the configurationboolean
Return true if function prototypes respect the C-language data-type conversion conventions.void
Encode this entire specification to a stream.findBestCallingConvention
(Parameter[] params) Find the best guess at a calling convention model from this compiler spec given an ordered list of (potential) parameters with storage assignments.getAddressSpace
(String spaceName) Get an address space by name.getCallingConvention
(String name) Returns the Calling Convention Model with the given name.Get the language that the decompiler producesReturns the prototype model that is the default calling convention or else null.protected static ErrorHandler
getErrorHandler
(String docTitle) Generate an XML error handler suitable for parsing a specification document.Get the Language this compiler spec is based on.getProperty
(String key) Gets a property defined for this language, or null if that property isn't defined.getProperty
(String key, String defaultString) Gets the value of a property as a String, returning defaultString if undefined.boolean
getPropertyAsBoolean
(String key, boolean defaultBoolean) Gets the value of a property as a boolean, returning defaultBoolean if undefined.int
getPropertyAsInt
(String key, int defaultInt) Gets the value of a property as an int, returning defaultInt if undefined.Returns a read-only set view of the property keys defined on this language.Get the evaluation model matching the given type.Get the physical space used for stack data storageGet the default Stack Pointer register for this language if there is one.Get the stack address space defined by this specificationboolean
hasProperty
(String key) Returns whether this language has a property defined.boolean
isEquivalent
(CompilerSpec obj) Determine if this CompilerSpec is equivalent to another specified instanceboolean
boolean
Indicates whether variables are right-justified within the stack alignment.protected static void
Mark a given PrototypeModel as a Program specific extensionmatchConvention
(String conventionName) Get the PrototypeModel which corresponds to the given calling convention name.protected String
modelXrefs
(List<PrototypeModel> modelList, String defaultName, String evalCurrent, String evalCalled) Establish cross referencing to prototype models.protected void
registerProgramInject
(List<InjectPayloadSleigh> injectExtensions) Register Program based InjectPayloads with the p-code library.protected void
removeProgramMechanismPayloads
(Collection<PrototypeModel> modelList) Remove any call mechanism injections associated with the given list of PrototypeModelsprotected void
Sets thereturnaddress
ofmodel
to thereturnAddress
ofthis
if the model does not have a return address set.boolean
-
Field Details
-
defaultModel
-
evalCurrentModel
-
evalCalledModel
-
allmodels
-
models
-
pcodeInject
-
returnAddress
-
-
Constructor Details
-
BasicCompilerSpec
public BasicCompilerSpec(CompilerSpecDescription description, SleighLanguage language, InputStream stream) throws XmlParseException, SAXException, IOException, DuplicateNameException 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 XMLSAXException
- for syntax errors in the XMLIOException
- for errors accessing the streamDuplicateNameException
- if there exists more than one PrototypeModel with the same name
-
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
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 Details
-
getErrorHandler
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
-
applyContextSettings
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
- 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
- Specified by:
getCallingConventions
in interfaceCompilerSpec
- Returns:
- an array of the prototype models. Each prototype model specifies a calling convention.
-
getCallingConvention
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
- Specified by:
getAllModels
in interfaceCompilerSpec
- Returns:
- all possible PrototypeModels, including calling conventions and merge models
-
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
Description copied from interface:CompilerSpec
Get the language that the decompiler produces- Specified by:
getDecompilerOutputLanguage
in interfaceCompilerSpec
- Returns:
- an enum specifying the language
-
getPrototypeEvaluationModel
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
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
Description copied from interface:CompilerSpec
Get the stack address space defined by this specification- Specified by:
getStackSpace
in interfaceCompilerSpec
- Returns:
- stack address space
-
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
- 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
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
- Specified by:
getCompilerSpecDescription
in interfaceCompilerSpec
- Returns:
- a brief description of the compiler spec
-
getAddressSpace
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 String modelXrefs(List<PrototypeModel> modelList, String defaultName, String evalCurrent, String evalCalled) throws XmlParseException Establish cross referencing to prototype models. All xrefs are regenerated from a single complete list of PrototypeModels. If there are PrototypeModels with duplicate names, return an example name. Return null otherwise The modelList must provide a model with name matching defaultName or an exception is thrown. (In theory the schema guarantees this model always exists)- Parameters:
modelList
- is the complete list of modelsdefaultName
- is the name to use for the default modelevalCurrent
- is the name to use for evaluating the current function (or null)evalCalled
- is the name to use for evaluating called functions (or null)- Returns:
- a PrototypeModel name that was duplicated or null
- Throws:
XmlParseException
- if there is no model matching defaultName
-
encode
Description copied from interface:CompilerSpec
Encode this entire specification to a stream. A document is written with root element<compiler_spec>
.- Specified by:
encode
in interfaceCompilerSpec
- Parameters:
encoder
- is the stream encoder- Throws:
IOException
- for errors writing to the underlying stream
-
getDataOrganization
- Specified by:
getDataOrganization
in interfaceCompilerSpec
-
matchConvention
Description copied from interface:CompilerSpec
Get the PrototypeModel which corresponds to the given calling convention name. If no match is found the default prototype model is returned.- Specified by:
matchConvention
in interfaceCompilerSpec
- Parameters:
conventionName
- calling convention name.- Returns:
- the matching model or the defaultModel if nothing matches
-
findBestCallingConvention
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 with storage assignments.- Specified by:
findBestCallingConvention
in interfaceCompilerSpec
- Parameters:
params
- is the ordered list of parameters- Returns:
- prototype model corresponding to the specified function signature
-
getProperty
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
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
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
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
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
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
- Specified by:
getPcodeInjectLibrary
in interfaceCompilerSpec
-
removeProgramMechanismPayloads
Remove any call mechanism injections associated with the given list of PrototypeModels- Parameters:
modelList
- is the given list
-
registerProgramInject
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
Mark a given PrototypeModel as a Program specific extension- Parameters:
model
- is the given PrototypeModel
-
setDefaultReturnAddressIfNeeded
Sets thereturnaddress
ofmodel
to thereturnAddress
ofthis
if the model does not have a return address set.- Parameters:
model
- prototype
-
isEquivalent
Description copied from interface:CompilerSpec
Determine if this CompilerSpec is equivalent to another specified instance- Specified by:
isEquivalent
in interfaceCompilerSpec
- Parameters:
obj
- is the other instance- Returns:
- true if they are equivalent
-