Skip to content

Instantly share code, notes, and snippets.

@ILyoan
Last active December 16, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ILyoan/5392936 to your computer and use it in GitHub Desktop.
Save ILyoan/5392936 to your computer and use it in GitHub Desktop.
This is a snippet of segmented stack manipulation for ARM. This snippet contains stack related parts so you can generate unwind table as the same as full version. (other parts are just commented for understand)
/*
This is a snippet of segmented stack manipulation for ARM.
This snippet contains stack related parts so you can generate unwind table as the same as full version.
(other parts are just commented for understand)
Compiled with gas, llvm-objdump shows extab as follow
Contents of section .ARM.extab:
0000 00000000 84419b01 b0b0b083 409b0281 .....A......@...
0010 84848496 b0b0a100 00000000 ............
84119b01 0b0b0b83 for foo
409b0281 84848496 b0b0a100 for __morestack
*/
.text
.align 2
.global foo
.type foo,%function
foo:
.fnstart
.LBB_FOO_STACKCHECK:
push {r4, r5}
// some code here for calcuating stack required and stack limit
// r4 <- stack required
// r5 <- stack limit
cmp r4, r5
blo .LBB_FOO_STACKCHECK_END
// need more stack
push {lr}
bl __morestack
pop {lr}
pop {r4, r5}
// return from this function
// function was already called from __morestack
mov pc, lr
.LBB_FOO_STACKCEHCK_END:
// don't need more stack
pop {r4, r5}
.LBB_FOO_PROLOGUE:
.save {r4, r5, r11, lr}
push {r4, r5, r11, lr}
.setfp r11, sp, #8
add r11, sp, #8
.pad #20
sub sp, sp, #20
.LBB_FOO_BODY:
// function body
pop {r4, r5, r11, lr}
mov pc, lr
.globl my_personality
.personality my_personality
.handlerdata
.fnend
.text
.align 2
.global __morestack
.hidden __morestack
.type __morestack, %function
__morestack:
.fnstart
// adjust unwind table
.save {r4, r5}
.save {lr}
.save {r6, fp, lr}
push {r6, fp, lr}
.movsp r6
mov r6, sp
.setfp fp, sp, #4
add fp, sp, #4
.LBB_MORESTACK_BODY:
// some code are here
// allocate new stack
// change to new stack and run original function
// return back to the original stack
pop {r6, fp, lr}
mov pc, lr
.fnend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment