gccのコンパイルの基礎を初めて学びました。私が知る限り、スタックはセキュリティ上の理由から実行できません。それでは、スタック上にどのようにコードを記述して実行するのですか?関数へのポインタを返す関数の場合、これを観察しました。コードへのポインタを返すのではなく、実行されるコードを持つスタック上の位置に戻ります。それはどのようにLinuxで許可されていますか?スタック上のコードの実行
すでにgccによって行われています。私はそれがどのように可能であるか知りたいですか?ここで
はCコードです:
#include<stdio.h>
typedef int (* funcptr)();
funcptr f()
{
int g()
{
}
return (&g);
}
main()
{
funcptr fp;
fp = f();
fp();
}
そしてここでは、スタック上のコードをgeeratingアセンブリコードの一部です:
#Starting trampoline code. The trampoline code is a small
#piece of code set up inside the stack!!!. This code, when
#executed, loads ecx with the static link and calls the
#function g
movb $-71, (%eax) # This is B9, the opcode for
"movl address_in_next_loc ecx"
this, when executed, will
load the static link in ecx
movl %edx, 1(%eax) # address_in_next_loc=ebp-16
the static link effectively
movb $-23, 5(%eax) # This is E9. the opcode for
jmp addr_nxt_ins + offset_
in_nxt_loc
Since the offset_in_nxt_loc
is &g - addr_nxt_ins, this
results in a jump to &g
movl $g.1831, %ecx # Stores &g - addr_nxt_ins
leal 10(%eax), %edx #
subl %edx, %ecx #
movl %ecx, %edx #
movl %edx, 6(%eax) #
#End of trampoline code
gccはこれをどうしていますか? –
gccで作成されたアセンブリを読み込んでいるためです。私はアセンブリを作成し、それを読んだ。 –
このアセンブリコードを表示できますか? –