So I am working on trying to execute a function from main in c without explicitly calling it by manipulating the stack on a 32 bit ARM architecture machine. My c code is as follows:
#include <stdio.h>
#include <stdlib.h>
void function2(){
printf("You have hit the target! :)\n");
exit(0);
}
void main(int argc, char**argv){
int i;
for(i = 0; i < 20; i++){
printf("ADDR: 0x%x -> 0x%x\n", (int*)(&argc+i), *(int*)(&argc+i));
}
}
From my understanding and from what was printed when I disassembled my main function using gdb, the link register is pushed onto the stack along with the frame pointer. Understand that the sole purpose of the loop above was to iterate through the stack to obtain the address of the link register on the stack.Then, I was going to edit my c code to change the value on the stack at that address to the first address of function2(). The output on my machine is as follows:
ADDR: 0x7eb9424c -> 0x1
ADDR: 0x7eb94250 -> 0x0
ADDR: 0x7eb94254 -> 0x2
ADDR: 0x7eb94258 -> 0x0
ADDR: 0x7eb9425c -> 0x76d82294
ADDR: 0x7eb94260 -> 0x76ea7000
ADDR: 0x7eb94264 -> 0x7eb943b4
ADDR: 0x7eb94268 -> 0x1
ADDR: 0x7eb9426c -> 0x10498
ADDR: 0x7eb94270 -> 0x76eff318
ADDR: 0x7eb94274 -> 0x76eff000
ADDR: 0x7eb94278 -> 0x0
ADDR: 0x7eb9427c -> 0x0
ADDR: 0x7eb94280 -> 0x10354
ADDR: 0x7eb94284 -> 0x0
ADDR: 0x7eb94288 -> 0x0
ADDR: 0x7eb9428c -> 0x0
ADDR: 0x7eb94290 -> 0x76f03000
ADDR: 0x7eb94294 -> 0x0
ADDR: 0x7eb94298 -> 0x6a7a8fc7
But when I gdb my program and get the info of the registers:
0 0x1 1
r1 0x7efff384 2130703236
r2 0x7efff38c 2130703244
r3 0x10498 66712
r4 0x0 0
r5 0x0 0
r6 0x10354 66388
r7 0x0 0
r8 0x0 0
r9 0x0 0
r10 0x76fff000 1996484608
r11 0x7efff22c 2130702892
r12 0x76fa3000 1996107776
sp 0x7efff218 0x7efff218
lr 0x76e7e294 1994908308
pc 0x104ac 0x104ac <main+20>
cpsr 0x60000010 1610612752
So the LR (link register) has a value of 0x76e7e294, but none of the dereferenced memory addresses on the stack yielded this value. Am I doing something wrong? Or is there some edge case where I am unable to access the stack frame of main?
EDIT: For anyone reading this, if you type in the gdb command 'info frame', you will print the stack frame of the function you are currently "within".