Reverse Engineering for Beginners

(avery) #1

CHAPTER 13. SWITCH()/CASE/DEFAULT CHAPTER 13. SWITCH()/CASE/DEFAULT


_f ENDP


The code mostly resembles what is in the source. There are no jumps between labels$LN4@fand$LN3@f: so when code
flow is at$LN4@f,readis first set to 1, thenwrite. This is why it’s called fall-through: code flow falls through one piece of
code (settingread) to another (settingwrite). Iftype=W, we land at$LN3@f, so no code settingreadto 1 is executed.


13.4.2 ARM64


Listing 13.14: GCC (Linaro) 4.9

.LC0:
.string "read=%d, write=%d\n"
f:
stp x29, x30, [sp, -48]!
add x29, sp, 0
str w0, [x29,28]
str wzr, [x29,44] ; set "read" and "write" local variables to zero
str wzr, [x29,40]
ldr w0, [x29,28] ; load "type" argument
cmp w0, 2 ; type=W?
beq .L3
cmp w0, 3 ; type=RW?
beq .L4
cmp w0, 1 ; type=R?
beq .L5
b .L6 ; otherwise...
.L4: ; case RW
mov w0, 1
str w0, [x29,44] ; read=1
.L3: ; case W
mov w0, 1
str w0, [x29,40] ; write=1
b .L6
.L5: ; case R
mov w0, 1
str w0, [x29,44] ; read=1
nop
.L6: ; default
adrp x0, .LC0 ; "read=%d, write=%d\n"
add x0, x0, :lo12:.LC0
ldr w1, [x29,44] ; load "read"
ldr w2, [x29,40] ; load "write"
bl printf
ldp x29, x30, [sp], 48
ret


Merely the same thing. There are no jumps between labels.L4and.L3.


13.5 Exercises.


13.5.1 Exercise #1


It’s possible to rework the C example in13.2 on page 155in such way that the compiler can produce even smaller code, but
will work just the same. Try to achieve it.

Free download pdf