summaryrefslogtreecommitdiff
path: root/function.cpp
diff options
context:
space:
mode:
authorRusty Wagner <rusty.wagner@gmail.com>2022-07-07 21:03:29 -0400
committerRusty Wagner <rusty.wagner@gmail.com>2022-08-10 15:17:53 -0400
commit390d5d27d4de6b7c9cb06b957a483bb2ab69f241 (patch)
tree556133db637166968a77d68daf7ff2160df79b40 /function.cpp
parent4037000b6cb8a184ad098d0eff397518d7fe55b9 (diff)
Merge variables API/UI, variable liveness API for determining soundness of variable merges
Diffstat (limited to 'function.cpp')
-rw-r--r--function.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/function.cpp b/function.cpp
index c93a8329..06815c39 100644
--- a/function.cpp
+++ b/function.cpp
@@ -1496,6 +1496,24 @@ string Function::GetVariableName(const Variable& var)
}
+string Function::GetVariableNameOrDefault(const Variable& var)
+{
+ char* name = BNGetVariableNameOrDefault(m_object, &var);
+ string result = name;
+ BNFreeString(name);
+ return result;
+}
+
+
+string Function::GetLastSeenVariableNameOrDefault(const Variable& var)
+{
+ char* name = BNGetLastSeenVariableNameOrDefault(m_object, &var);
+ string result = name;
+ BNFreeString(name);
+ return result;
+}
+
+
void Function::SetAutoIndirectBranches(
Architecture* sourceArch, uint64_t source, const std::vector<ArchAndAddr>& branches)
{
@@ -2518,6 +2536,81 @@ void Function::SetVariableDeadStoreElimination(const Variable& var, BNDeadStoreE
}
+std::map<Variable, std::set<Variable>> Function::GetMergedVariables()
+{
+ size_t count;
+ BNMergedVariable* mergedVars = BNGetMergedVariables(m_object, &count);
+
+ std::map<Variable, std::set<Variable>> result;
+ for (size_t i = 0; i < count; i++)
+ {
+ Variable target;
+ target.type = mergedVars[i].target.type;
+ target.index = mergedVars[i].target.index;
+ target.storage = mergedVars[i].target.storage;
+
+ set<Variable> sources;
+ for (size_t j = 0; j < mergedVars[i].sourceCount; j++)
+ {
+ Variable source;
+ source.type = mergedVars[i].sources[j].type;
+ source.index = mergedVars[i].sources[j].index;
+ source.storage = mergedVars[i].sources[j].storage;
+ sources.insert(source);
+ }
+
+ result[target] = sources;
+ }
+
+ BNFreeMergedVariableList(mergedVars, count);
+ return result;
+}
+
+
+void Function::MergeVariables(const Variable& target, const std::set<Variable>& sources)
+{
+ BNVariable targetData;
+ targetData.type = target.type;
+ targetData.index = target.index;
+ targetData.storage = target.storage;
+
+ BNVariable* sourceData = new BNVariable[sources.size()];
+ size_t i = 0;
+ for (auto& var : sources)
+ {
+ sourceData[i].type = var.type;
+ sourceData[i].index = var.index;
+ sourceData[i].storage = var.storage;
+ i++;
+ }
+
+ BNMergeVariables(m_object, &targetData, sourceData, sources.size());
+ delete[] sourceData;
+}
+
+
+void Function::UnmergeVariables(const Variable& target, const std::set<Variable>& sources)
+{
+ BNVariable targetData;
+ targetData.type = target.type;
+ targetData.index = target.index;
+ targetData.storage = target.storage;
+
+ BNVariable* sourceData = new BNVariable[sources.size()];
+ size_t i = 0;
+ for (auto& var : sources)
+ {
+ sourceData[i].type = var.type;
+ sourceData[i].index = var.index;
+ sourceData[i].storage = var.storage;
+ i++;
+ }
+
+ BNUnmergeVariables(m_object, &targetData, sourceData, sources.size());
+ delete[] sourceData;
+}
+
+
vector<ILReferenceSource> Function::GetMediumLevelILVariableReferences(const Variable& var)
{
size_t count;