在Linux中可执行堆栈的示例(i386架构) [英] Example of executable stack in Linux (i386 architecture)

查看:290
本文介绍了在Linux中可执行堆栈的示例(i386架构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,当我们使用嵌套函数的,GCC需要的trampoline code 。然而,随着code,编译时使用gcc不显示可执行堆栈。 (我用 execstack 以验证是否堆栈是可执行的)

I found that when we use nested functions, GCC requires an executable stack for trampoline code. However, following code, when compiled using gcc doesn't show an executable stack. (I used execstack to verify if the stack is executable)

#include <stdio.h>
#include <unistd.h>

int main()
{
        int add( int a, int b)
        {
                return a + b;
        }
        return add(2, 3);
}

为什么这不会导致可执行栈?而如果它是不应该,那么可以有人给例如code结构,它的确实的给可执行堆栈?

推荐答案

如果嵌套函数并不在它的父的堆栈依赖的话,那么它只是一个普通的功能 - 嵌套是句法(和范围)糖。

If the nested function doesn't depend in its "parent"'s stack at all, then it's just a plain function - the nesting is syntactic (and scoping) sugar.

如果你不采取嵌套函数的地址,没有蹦床code是必要的要么。所以,你需要的东西多一点参与触发了这一切。

And if you don't take the address of the nested function, no trampoline code is necessary either. So you'll need something a bit more involved to trigger all that.

下面是一个虚拟的例子:

Here's a dummy example:

// file t.c
int doit(int (*fun)(int), int x)
{
    return fun(x);
}

int foo(int a)
{
        int add(int b)
        {
                return a + b;
        }
        return doit(&add, 2);
}

int main(void)
{
    return foo(1);
}

$ gcc t.c
t.c: In function 'foo':
t.c:8:13: warning: trampoline generated for nested function 'add'
$ ./a.out 
$ echo $?
3
$ execstack a.out 
X a.out

这篇关于在Linux中可执行堆栈的示例(i386架构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆