Ghidra 11.3.2
Ghidra internal decompiler documentation.
Loading...
Searching...
No Matches
ghidra::PrimitiveExtractor Class Reference

Class for extracting primitive elements of a data-type. More...

#include <modelrules.hh>

Collaboration diagram for ghidra::PrimitiveExtractor:
[legend]

Classes

class  Primitive
 A primitive data-type and its offset within the containing data-type. More...
 

Public Member Functions

 PrimitiveExtractor (Datatype *dt, bool unionIllegal, int4 offset, int4 max)
 Constructor.
 
int4 size (void) const
 Return the number of primitives extracted.
 
const Primitiveget (int4 i) const
 Get a particular primitive.
 
bool isValid (void) const
 Return true if primitives were successfully extracted.
 
bool containsUnknown (void) const
 Are there unknown elements.
 
bool isAligned (void) const
 Are all elements aligned.
 
bool containsHoles (void) const
 Is there empty space that is not padding.
 

Private Types

enum  {
  unknown_element = 1 , unaligned = 2 , extra_space = 4 , invalid = 8 ,
  union_invalid = 16
}
 

Private Member Functions

int4 checkOverlap (vector< Primitive > &res, vector< Primitive > &small, int4 point, Primitive &big)
 Check that a big Primitive properly overlaps smaller Primitives.
 
bool commonRefinement (vector< Primitive > &first, vector< Primitive > &second)
 Overwrite first list with common refinement of first and second.
 
bool handleUnion (TypeUnion *dt, int4 max, int4 offset)
 Add primitives representing a union data-type.
 
bool extract (Datatype *dt, int4 max, int4 offset)
 Extract list of primitives from given data-type.
 

Private Attributes

vector< Primitiveprimitives
 List of extracted primitives.
 
uint4 flags
 Boolean properties of the data-type.
 

Detailed Description

Class for extracting primitive elements of a data-type.

This recursively collects the formal primitive data-types of a composite data-type, laying them out with their offsets in an array. Other boolean properties are collected.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
unknown_element 

Contains at least one TYPE_UNKNOWN primitive.

unaligned 

At least one primitive is not properly aligned.

extra_space 

Data-type contains empty space not attributable to alignment padding.

invalid 

Data-type exceeded maximum or contained illegal elements.

union_invalid 

Unions are treated as an illegal element.

Constructor & Destructor Documentation

◆ PrimitiveExtractor()

ghidra::PrimitiveExtractor::PrimitiveExtractor ( Datatype dt,
bool  unionIllegal,
int4  offset,
int4  max 
)

Constructor.

Parameters
dtis data-type extract from
unionIllegalis true if unions encountered during extraction are considered illegal
offsetis the starting offset to associate with the data-type
maxis the maximum number of primitives to extract before giving up

References extract(), flags, invalid, and union_invalid.

Member Function Documentation

◆ checkOverlap()

int4 ghidra::PrimitiveExtractor::checkOverlap ( vector< Primitive > &  res,
vector< Primitive > &  small,
int4  point,
Primitive big 
)
private

Check that a big Primitive properly overlaps smaller Primitives.

If the big Primitive does not properly overlap the smaller Primitives starting at the given point, return -1. Otherwise, if the big Primitive is floating-point, add the overlapped primitives to the common refinement list, or if not a floating-point, add the big Primitive to the list. (Integer primitives are preferred over floating-point primitives in this way) Return the index of the next primitive after the overlap.

Parameters
resholds the common refinement list
smallis the list of Primitives that are overlapped
pointis the index of the first overlap
bigis the big overlapping Primitive
Returns
the index of the next Primitive after the overlap or -1 if the overlap is invalid

References ghidra::PrimitiveExtractor::Primitive::dt, ghidra::Datatype::getAlignSize(), ghidra::Datatype::getMetatype(), ghidra::PrimitiveExtractor::Primitive::offset, and ghidra::TYPE_FLOAT.

Referenced by commonRefinement().

◆ commonRefinement()

bool ghidra::PrimitiveExtractor::commonRefinement ( vector< Primitive > &  first,
vector< Primitive > &  second 
)
private

Overwrite first list with common refinement of first and second.

Given two sets of overlapping Primitives, find a common refinement of the lists. If there is any partial overlap of two Primitives, false is returned. If the same primitive data-type occurs at the same offset, it is included in the refinement. Otherwise an integer data-type is preferred over a floating-point data-type, or a bigger primitive is preferred over smaller overlapping primitives. The final refinement replaces the first list.

Parameters
firstis the first list of Primitives
secondis the second list
Returns
true if a refinement was successfully constructed

References checkOverlap(), ghidra::PrimitiveExtractor::Primitive::dt, ghidra::Datatype::getAlignSize(), and ghidra::PrimitiveExtractor::Primitive::offset.

Referenced by handleUnion().

◆ extract()

bool ghidra::PrimitiveExtractor::extract ( Datatype dt,
int4  max,
int4  offset 
)
private

Extract list of primitives from given data-type.

An array of the primitive data-types, with their associated offsets, is constructed. If the given data-type is already primitive it is put in the array by itself. Otherwise if it is composite, its components are recursively added to the array. Boolean properties about the primitives encountered are recorded:

  • Are any of the primitives undefined
  • Are all the primitives properly aligned.

If a maximum number of extracted primitives is exceeded, or if an illegal data-type is encountered (void or other internal data-type) false is returned.

Parameters
dtis the given data-type to extract primitives from
maxis the maximum number of primitives to extract before giving up
offsetis the starting offset to associate with the first primitive
Returns
true if all primitives were extracted

< Mark that the data-type contains an unknown primitive

References ghidra::TypeStruct::beginField(), ghidra::TypeStruct::endField(), extra_space, extract(), flags, ghidra::Datatype::getAlignment(), ghidra::Datatype::getAlignSize(), ghidra::Datatype::getMetatype(), handleUnion(), primitives, ghidra::TYPE_ARRAY, ghidra::TYPE_BOOL, ghidra::TYPE_CODE, ghidra::TYPE_FLOAT, ghidra::TYPE_INT, ghidra::TYPE_PTR, ghidra::TYPE_PTRREL, ghidra::TYPE_STRUCT, ghidra::TYPE_UINT, ghidra::TYPE_UNION, ghidra::TYPE_UNKNOWN, unaligned, and unknown_element.

Referenced by extract(), and PrimitiveExtractor().

◆ handleUnion()

bool ghidra::PrimitiveExtractor::handleUnion ( TypeUnion dt,
int4  max,
int4  offset 
)
private

Add primitives representing a union data-type.

Form a primitive list for each field of the union. Then, if possible, form a common refinement of all the primitive lists and add to the end of this extractor's list.

Parameters
dtis the union data-type
maxis the maximum number primitives allowed for this extraction
offsetis the starting offset of the union within the parent
Returns
true if a common refinement was found and appended

References commonRefinement(), flags, ghidra::TypeUnion::getField(), isValid(), ghidra::TypeUnion::numDepend(), ghidra::TypeField::offset, primitives, ghidra::TypeField::type, and union_invalid.

Referenced by extract().


The documentation for this class was generated from the following files: