Skip to content

Commit

Permalink
Merge remote-tracking branch
Browse files Browse the repository at this point in the history
'origin/GP-3435_ghidra1_PR-5292_dukesilverrr_R_AARCH64_MOVW_UABS_G_Relocs'
(Closes #3545, Closes #3546, Closes #5292)
  • Loading branch information
ryanmkurtz committed May 15, 2023
2 parents 4b99900 + 90d4864 commit ce5f6b4
Showing 1 changed file with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,

boolean is64bit = true;

boolean overflowCheck = true; // *_NC type relocations specify "no overflow check"

Address symbolAddr = elfRelocationContext.getSymbolAddress(sym);
long symbolValue = elfRelocationContext.getSymbolValue(sym);
long newValue = 0;
Expand Down Expand Up @@ -137,6 +139,87 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
break;
}

// MOV[ZK]: ((S+A) >> 0) & 0xffff
case AARCH64_ElfRelocationConstants.R_AARCH64_MOVW_UABS_G0_NC: {
overflowCheck = false;
// fall-through
}
case AARCH64_ElfRelocationConstants.R_AARCH64_MOVW_UABS_G0: {
int oldValue = memory.getInt(relocationAddress, isBigEndianInstructions);
long imm = (symbolValue + addend) >> 0;

oldValue &= ~(0xffff << 5);
newValue = oldValue | ((imm & 0xffff) << 5);

memory.setInt(relocationAddress, (int) newValue, isBigEndianInstructions);

if (overflowCheck && imm > 0xffffL) {
// relocation already applied; report overflow condition
markAsError(program, relocationAddress, "R_AARCH64_MOVW_UABS_G0", symbolName,
"Failed overflow check for R_AARCH64_MOVW_UABS_G0 immediate value",
elfRelocationContext.getLog());
}
break;
}

// MOV[ZK]: ((S+A) >> 16) & 0xffff
case AARCH64_ElfRelocationConstants.R_AARCH64_MOVW_UABS_G1_NC: {
overflowCheck = false;
// fall-through
}
case AARCH64_ElfRelocationConstants.R_AARCH64_MOVW_UABS_G1: {
int oldValue = memory.getInt(relocationAddress, isBigEndianInstructions);
long imm = (symbolValue + addend) >> 16;

oldValue &= ~(0xffff << 5);
newValue = oldValue | ((imm & 0xffff) << 5);

memory.setInt(relocationAddress, (int) newValue, isBigEndianInstructions);

if (overflowCheck && imm > 0xffffL) {
// relocation already applied; report overflow condition
markAsError(program, relocationAddress, "R_AARCH64_MOVW_UABS_G0", symbolName,
"Failed overflow check for R_AARCH64_MOVW_UABS_G0 immediate value",
elfRelocationContext.getLog());
}
break;
}

// MOV[ZK]: ((S+A) >> 32) & 0xffff
case AARCH64_ElfRelocationConstants.R_AARCH64_MOVW_UABS_G2_NC: {
overflowCheck = false;
// fall-through
}
case AARCH64_ElfRelocationConstants.R_AARCH64_MOVW_UABS_G2: {
int oldValue = memory.getInt(relocationAddress, isBigEndianInstructions);
long imm = (symbolValue + addend) >> 32;

oldValue &= ~(0xffff << 5);
newValue = oldValue | ((imm & 0xffff) << 5);

memory.setInt(relocationAddress, (int) newValue, isBigEndianInstructions);

if (overflowCheck && imm > 0xffffL) {
// relocation already applied; report overflow condition
markAsError(program, relocationAddress, "R_AARCH64_MOVW_UABS_G0", symbolName,
"Failed overflow check for R_AARCH64_MOVW_UABS_G0 immediate value",
elfRelocationContext.getLog());
}
break;
}

// MOV[ZK]: ((S+A) >> 48) & 0xffff
case AARCH64_ElfRelocationConstants.R_AARCH64_MOVW_UABS_G3: {
int oldValue = memory.getInt(relocationAddress, isBigEndianInstructions);
long imm = (symbolValue + addend) >> 48;

oldValue &= ~(0xffff << 5);
newValue = oldValue | ((imm & 0xffff) << 5);

memory.setInt(relocationAddress, (int) newValue, isBigEndianInstructions);
break;
}

// ADRH: ((PG(S+A)-PG(P)) >> 12) & 0x1fffff
case AARCH64_ElfRelocationConstants.R_AARCH64_ADR_PREL_PG_HI21:
case AARCH64_ElfRelocationConstants.R_AARCH64_P32_ADR_PREL_PG_HI21: {
Expand Down

0 comments on commit ce5f6b4

Please sign in to comment.