Skip to content

Commit

Permalink
Allow relating variables with negative offset
Browse files Browse the repository at this point in the history
Also lax requirement of variable's address being 'meaningful' to be >= the process's main mach binary header address.
  • Loading branch information
zorgiepoo committed May 12, 2024
1 parent 8480593 commit af8031a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Bit Slicer/ZGDocumentTableController.m
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ - (NSDragOperation)tableView:(NSTableView *)__unused tableView validateDrop:(id
for (NSNumber *draggingRow in draggingRows)
{
ZGVariable *draggingVariable = documentVariables[draggingRow.unsignedIntegerValue];
if (draggingVariable.type == ZGScript || draggingVariable.usesDynamicLabelAddress)
// Even if the label already uses a label in its address,
// we should still allow the user to override the address if they want
// to change the relation of the variable from one label to another one
if (draggingVariable.type == ZGScript)
{
return NSDragOperationNone;
}
Expand Down
22 changes: 17 additions & 5 deletions Bit Slicer/ZGVariableController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1029,27 +1029,30 @@ - (void)relateVariables:(NSArray<ZGVariable *> *)variables toLabeledVariable:(ZG
ZGDocumentWindowController *windowController = _windowController;
ZGProcess *process = windowController.currentProcess;

// If there are any duplicate addresses or any address is zero or the addresses are <= than the labeled variable
ZGMemoryAddress mainBinaryHeaderAddress = process.mainMachBinary.headerAddress;

// If there are any duplicate addresses or any address < mainBinaryHeaderAddress
// we will assume the variable's addresses are not meaningful and can be overwritten based on stride
// Otherwise we will assume the current addresses are meaningful and are relative to the labeled variable
ZGMemoryAddress labeledVariableAddress = labeledVariable.address;
NSMutableSet<NSNumber *> *visitedAddresses = [NSMutableSet set];
BOOL currentAddressesRelatable = YES;
for (ZGVariable *variable in variables)
{
if (variable.address == 0x0 || variable.address <= labeledVariableAddress)
if (variable.address < mainBinaryHeaderAddress)
{
currentAddressesRelatable = NO;
break;
}

if ([visitedAddresses containsObject:@(variable.address)])
NSNumber *variableAddress = @(variable.address);
if ([visitedAddresses containsObject:variableAddress])
{
currentAddressesRelatable = NO;
break;
}

[visitedAddresses addObject:@(variable.address)];
[visitedAddresses addObject:variableAddress];
}

NSMutableArray<NSString *> *newAddressFormulas = [NSMutableArray array];
Expand Down Expand Up @@ -1082,7 +1085,16 @@ - (void)relateVariables:(NSArray<ZGVariable *> *)variables toLabeledVariable:(ZG
{
for (ZGVariable *variable in variables)
{
NSString *newAddressFormula = [NSString stringWithFormat:@"label(\"%@\") + 0x%llX", label, variable.address - labeledVariableAddress];
ZGMemoryAddress variableAddress = variable.address;
NSString *newAddressFormula;
if (variableAddress >= labeledVariableAddress)
{
newAddressFormula = [NSString stringWithFormat:@"label(\"%@\") + 0x%llX", label, variableAddress - labeledVariableAddress];
}
else
{
newAddressFormula = [NSString stringWithFormat:@"label(\"%@\") - 0x%llX", label, labeledVariableAddress - variableAddress];
}

[newAddressFormulas addObject:newAddressFormula];
}
Expand Down

0 comments on commit af8031a

Please sign in to comment.