Ich bin neu in den Grundlagen der GCC-Kompilierung. Soweit ich weiß, ist der Stack aus Sicherheitsgründen nicht ausführbar. Dann wie können wir Code auf Stapel haben und ausführen. Ich beobachtete dies im Falle einer Funktion, die den Zeiger auf eine Funktion zurückgibt. Es gibt keinen Zeiger auf den Code zurück, sondern auf einen Ort auf dem Stapel, auf dem der Code ausgeführt wird. Wie ist das in Linux erlaubt?Ausführung von Code auf dem Stapel
Es ist bereits von gcc getan. Ich möchte wissen, wie es überhaupt möglich ist? Hier
ist der C-Code:
#include<stdio.h>
typedef int (* funcptr)();
funcptr f()
{
int g()
{
}
return (&g);
}
main()
{
funcptr fp;
fp = f();
fp();
}
Und hier ist der Teil des Assembler-Code geerating Code auf den Stack:
#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
Was macht sagen Sie gcc dies tut? –
Weil ich die von gcc erstellte Assembly gelesen habe. Ich habe die Baugruppe erstellt und gelesen. –
Können Sie diesen Assemblercode anzeigen? –