i stored function in memory, and i want to get assembly code from my memory.
first i did
int Operation(int a)
{
__asm__(
".intel_syntax;"
"mov %%eax, %1;"
"add %%eax, 1;"
"add %%eax, 1;"
"mov %0, %%eax;"
".att_syntax;": "=r"(a): "r"(a) :"eax"
);
return a;
}
i wrote a function code
and then
uint8_t* func = (uint8_t*)Operation;
int i = 0;
int segment_id;
uint8_t* shared_memory;
segment_id = shmget(1234, PAGE_SIZE, IPC_CREAT | S_IRUSR | S_IWUSR);
shared_memory = (uint8_t*)shmat(segment_id, NULL, 0);
do
{
shared_memory[i++] = *func;
} while (*func++ != 0xC3);
shmdt(shared_memory);
i made shared memory and stored function in memory
and in another c,
int main(void)
{
uint8_t* shared_memory;
int segment_id;
int (*func)(int a);
int i=0;
int rtrn;
segment_id = shmget(1234, PAGE_SIZE, 0);
struct shmid_ds shmid_ds,*buf;
buf=&shmid_ds;
rtrn=shmctl(segment_id,IPC_STAT,buf);
buf->shm_perm.uid=getpid();
buf->shm_perm.mode=511;
rtrn=shmctl(segment_id,IPC_SET,buf);
rtrn=shmctl(segment_id,IPC_STAT,buf);
shared_memory = (uint8_t*)shmat(segment_id, NULL, SHM_EXEC);
func=shared_memory;
uint8_t* a=shared_memory;
do
{
printf("%X\n",*a);
a++;
i++;
//printf("%p %X\n",&shared_memory[i++],shared_memory[i++]);
} while (i != 500);
i=func(1);
printf("%d\n```",i);
i can execute function with " i= func(1);"
what i want to do is get assembly code from memory.
so i got hex code starting from "shared memory"
and i got result
55
48
89
E5
89
7D
FC
8B
55
so i thought if i disassemble those hex, i can get assembly code that i wrote in function, but i got different disassemble result that i expected
what i expected was function code that i wrote like this: "mov %%eax, %1;"
"add %%eax, 1;" "add %%eax, 1;" "mov %0, %%eax;"
what was wrong in my code or idea?