Skip to content

Fix ldp and stp encoding in the arm64 assembler ##arch - #27132

Merged
trufae merged 2 commits into
radareorg:masterfrom
HuzaifaChaudary:arm64-ldp-stp-encoding
Oct 23, 2026
Merged

trufae merged 2 commits into
radareorg:masterfrom
HuzaifaChaudary:arm64-ldp-stp-encoding

Conversation

@HuzaifaChaudary

@HuzaifaChaudary HuzaifaChaudary commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor
  • Mark this if you consider it ready to merge
  • I've added tests (optional)
  • I wrote some lines in the book (optional)

Description

stp and ldp went through encode2regs , which puts the second register in the Rn field , and the base register was never encoded , so every pair load and store came out as a different instruction . pre and post index and the 32-bit forms were not handled at all :

input before after
ldp x0, x1, [sp, 16] 200041a9 = ldp x0, x0, [x1, 16] e00741a9
stp x29, x30, [sp, -32]! dd02fea9 = ldp x29, x0, [x22, -32]! fd7bbea9
ldp x29, x30, [sp], 32 dd0342a9 = ldp x29, x0, [x30, 32] fd7bc2a8
stp w0, w1, [x2, 16] 200001a9 = stp x0, x0, [x1, 16] 40040229

( decoded with llvm-mc )

so wa stp x29, x30, [sp, -0x20]! , the usual function prologue , wrote an ldp . stp now builds the word like adrp does and swaps it at the end : signed offset , [rn, imm]! and [rn], imm , x and w pairs with the offset scaled by 8 or 4 and checked against the 7-bit range , and ldp with the same register twice is still refused . a second commit adds s , d and q register pairs , which the parser did not know about , stp d8, d9, [sp, -0x10]! gave ldp xzr, xzr, [sp, -8]! before .

found by assembling the same text with rasm2 -a arm.nz and llvm-mc and decoding both . all 68 ldp / stp forms in that list now give the same bytes as llvm , none did before , and the ones llvm refuses ( ldp w0, w1, [sp, 504] ) are refused here too . the 14 new ad lines in db/asm/arm_64 fail before the change and round trip through the disassembler after . db/asm , db/tools/rasm2 , db/cmd/cmd_pa and db/cmd/write have no failures .

Copilot AI balanced review requested due to automatic review settings October 22, 2026 19:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@mfulghum

mfulghum commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Did you test this for loading SIMD registers (i.e. (B|H|S|D|Q))? I think this would break them as it stands, since they're FPU registers instead of GPRs.

@HuzaifaChaudary

Copy link
Copy Markdown
Contributor Author

good catch , I had only tested the general purpose pairs . on master they were already wrong though : stp d8, d9, [sp, -0x10]! gave ffffffa9 , which is ldp xzr, xzr, [sp, -8]! , stp s0, s1, [x0, 8] gave stp x0, x0, [x0, 8] , and the ldp ones were refused , because the parser had no s , d or q registers at all and stp ignored the operand types . with the first commit they were refused instead .

ab0c734 adds them : the parser now reads s0-s31 , d0-d31 and q0-q31 as FP registers with their size , and stp / ldp use the SIMD&FP opcodes with the offset scaled by 4 , 8 or 16 . b and h have no pair load or store , so those are still refused , as are mixed pairs like stp d0, s1, [x0] .

checked against llvm-mc , all match : stp d8, d9, [sp, -16]! e827bf6d , ldp d8, d9, [sp], 16 e827c16c , stp s0, s1, [x0, 8] 0004012d , ldp q0, q1, [x0, 32] 000441ad , stp q2, q3, [sp, -64]! e20fbead , ldp s30, s31, [x29, -256] be7f602d , stp q0, q1, [sp, 1008] e0871fad . five of them are new ad lines in db/asm/arm_64 , which is 688 OK , and every other instruction in my 3915 line arm64 list assembles to the same bytes as before this commit .

@trufae
trufae merged commit 493348d into radareorg:master Oct 23, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants