Но я столкнулся с проблемами.
Это мой код:< /p>
Код: Выделить всё
#include
#include
#include
extern uint64_t __start_myfnsection[];
extern uint64_t __stop_myfnsection[];
// I create a simple function, which will be placed in a separate binary section
__attribute__((noinline, section("myfnsection"))) void myfn() {
static int i;
i++;
printf("I love lemons!^^.\n");
printf("i=%d\n", i);
return;
}
int main () {
// Test the function.
myfn();
// Find out and print the size of my function.
uint64_t myfn_length = (__stop_myfnsection - __start_myfnsection);
printf("Length of myfn() function is: %lu\n", myfn_length);
// Allocate on-stack memory, and copy my function to it
void* memory = __builtin_alloca(myfn_length);
memcpy(memory, &myfn, myfn_length);
// Create a pointer to the copied function.
void (*myfn_copy)() = memory;
// Attempt to execute it.
myfn_copy();
return 0;
}
Код: Выделить всё
$ gcc -pie -z execstack -fno-stack-protector -g test.c
Код: Выделить всё
$ ./a.out
I love lemons!^^.
i=1
Length of myfn() function is: 9
Illegal instruction
Код: Выделить всё
# gdb ./a.out
GNU gdb (GDB) 14.2
Reading symbols from ./a.out...
(gdb) run
Starting program: /home/mika/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so".
I love lemons!^^.
i=1
Length of myfn() function is: 9
Program received signal SIGILL, Illegal instruction.
0x0000007ffffffb10 in ?? ()
(gdb)
(gdb) bt
#0 0x0000007ffffffb10 in ?? ()
#1 0x0000005555556748 in _start_main ()
#2 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb)
Что мне не хватает или я пытаюсь сделать что-то невозможное? Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ction-in-c
Мобильная версия