以前的堆栈变量

时间:2012-05-02 01:46:11

标签: c gcc assembly x86

我有这个问题,我递归调用C中的函数,C是词法范围的,所以我只能访问当前的堆栈帧。我想从前一个函数调用中创建的前一个堆栈帧中提取参数和局部变量,同时我在当前堆栈帧上

我知道前一次递归调用的值仍在堆栈中,但是我无法访问这些值,因为它们已经埋没了#34;在活动堆栈框架下?

我想从前一个堆栈中提取参数和局部变量,并将它们复制到copy_of_buried_arg和copy_of_buried_loc;

要求使用GAS提取内联汇编来提取变量,这是我到目前为止所做的,我整天都在尝试,我似乎无法弄清楚,我把纸叠在纸上并进行了计算但是没有什么工作,我也尝试删除对printf的调用,这样堆栈会更干净,但我无法弄清楚正确的算法。这是到目前为止的代码,我的函数在第二次迭代时停止

#include <stdio.h>

char glo = 97;   // just for fun 97 is ascii lowercase 'a'
int copy_of_buried_arg;
char copy_of_buried_loc;

void rec(int arg) {
  char loc;

  loc = glo + arg * 2; // just for fun, some char arithmetic
  printf("inside rec() arg=%d loc='%c'\n", arg, loc);

  if (arg != 0) {
    // after this assembly code runs, the copy_of_buried_arg and
    // copy_of_buried_loc variables will have arg, loc values from
    // the frame of the previous call to rec().
    __asm__("\n\
            movl 28(%esp), %eax #moving stack pointer to old ebp (pointing it to old ebp)\n\
            addl $8, %eax       #now eax points to the first argument for the old ebp \n\
            movl (%eax), %ecx   #copy the value inside eax to ecx\n\ 
            movl %ecx, copy_of_buried_arg   # copies the old argument\n\
    \n\

");

    printf("copy_of_buried_arg=%u copy_of_buried_loc='%c'\n",
       copy_of_buried_arg, copy_of_buried_loc);
  } else {
      printf("there is no buried stack frame\n");// runs if argument = 0 so only the first time
  }

  if (arg < 10) {
    rec(arg + 1);
  }
}

int main (int argc, char **argv) {
  rec(0);

  return 0;
}

2 个答案:

答案 0 :(得分:1)

我可以尝试提供帮助,但在GAS中没有Linux或程序集。但计算应该类似:

这是几次通话后的筹码。典型的堆栈帧设置会创建堆栈帧的链接列表,其中EBP是当前堆栈帧,并指向前一堆栈帧的旧值。

      +-------+
ESP-> |loc='c'|     <- ESP currently points here.
      +-------+
EBP-> |oldEBP |--+  <- rec(0)'s call frame
      +-------+  |
      |retaddr|  |  <- return value of rec(1)
      +-------+  |
      |arg=1  |  |  <- pushed argument of rec(1)
      +-------+  |
      |loc='a'|  |  <- local variable of rec(0)
      +-------+  |
   +--|oldEBP |<-+  <- main's call frame
   |  +-------+
   |  |retaddr|     <- return value of rec(0)
   |  +-------+ 
   |  |arg=0  |     <- pushed argument of rec(0)
   |  +-------+
  \|/ 
to main's call frame

这是由以下序列创建的:

  1. 首先推送参数arg。
  2. 调用该函数,按回车地址。
  3. 推出即将成为旧的EBP,保留以前的堆栈帧。
  4. 将ESP(堆栈顶部,包含oldEBP)移动到EBP中,创建新的堆栈帧。
  5. 减去局部变量的空间。
  6. 这对32位堆栈有影响,EBP+8将始终是调用的第一个参数,EBP+12第二个参数,等等。EBP-n始终是一个偏移量一个局部变量。

    然后(在MASM中)获取上一个locarg的代码:

    mov ecx,[ebp]              // get previous stack frame
    mov edx,[ecx]+8            // get first argument
    mov copy_of_buried_arg,edx // save it
    mov dl,[ecx]-1             // get first char-sized local variable.
    mov copy_of_buried_loc,dl  // save it
    

    或者我在GAS中的最佳猜测(我不知道但是知道它是向MASM倒退的):

    movl (%ebp),ecx
    movl 8(%ecx),edx
    movl edx,copy_of_buried_arg
    movb -1(%ecx),dl
    movb dl,copy_of_buried_loc
    

    在Windows上使用VS2010使用我的MASM输出代码:

    inside rec() arg=0 loc='a'
    there is no buried stack frame
    inside rec() arg=1 loc='c'
    copy_of_buried_arg=0 copy_of_buried_loc='a'
    inside rec() arg=2 loc='e'
    copy_of_buried_arg=1 copy_of_buried_loc='c'
    inside rec() arg=3 loc='g'
    copy_of_buried_arg=2 copy_of_buried_loc='e'
    inside rec() arg=4 loc='i'
    copy_of_buried_arg=3 copy_of_buried_loc='g'
    inside rec() arg=5 loc='k'
    copy_of_buried_arg=4 copy_of_buried_loc='i'
    inside rec() arg=6 loc='m'
    copy_of_buried_arg=5 copy_of_buried_loc='k'
    inside rec() arg=7 loc='o'
    copy_of_buried_arg=6 copy_of_buried_loc='m'
    inside rec() arg=8 loc='q'
    copy_of_buried_arg=7 copy_of_buried_loc='o'
    inside rec() arg=9 loc='s'
    copy_of_buried_arg=8 copy_of_buried_loc='q'
    inside rec() arg=10 loc='u'
    copy_of_buried_arg=9 copy_of_buried_loc='s'
    

答案 1 :(得分:0)

使用我的编译器(gcc 3.3.4)我最终得到了这个:

#include <stdio.h>

char glo = 97;   // just for fun 97 is ascii lowercase 'a'
int copy_of_buried_arg;
char copy_of_buried_loc;

void rec(int arg) {
  char loc;

  loc = glo + arg * 2; // just for fun, some char arithmetic
  printf("inside rec() arg=%d loc='%c'\n", arg, loc);

  if (arg != 0) {
    // after this assembly code runs, the copy_of_buried_arg and
    // copy_of_buried_loc variables will have arg, loc values from
    // the frame of the previous call to rec().
    __asm__ __volatile__ (
            "movl 40(%%ebp), %%eax #\n"
            "movl %%eax, %0 #\n"
            "movb 31(%%ebp), %%al #\n"
            "movb %%al, %1 #\n"
            : "=m" (copy_of_buried_arg), "=m" (copy_of_buried_loc)
            :
            : "eax"
    );

    printf("copy_of_buried_arg=%u copy_of_buried_loc='%c'\n",
       copy_of_buried_arg, copy_of_buried_loc);
  } else {
      printf("there is no buried stack frame\n");// runs if argument = 0 so only the first time
  }

  if (arg < 10) {
    rec(arg + 1);
  }
}

int main (int argc, char **argv) {
  rec(0);

  return 0;
}

以下是相关部分的反汇编(使用gcc file.c -S -o file.s获取):

_rec:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movl    8(%ebp), %eax
        addl    %eax, %eax
        addb    _glo, %al
        movb    %al, -1(%ebp)
        subl    $4, %esp
        movsbl  -1(%ebp),%eax
        pushl   %eax
        pushl   8(%ebp)
        pushl   $LC0
        call    _printf
        addl    $16, %esp
        cmpl    $0, 8(%ebp)
        je      L2
/APP
        movl 40(%ebp), %eax #
movl %eax, _copy_of_buried_arg #
movb 31(%ebp), %al #
movb %al, _copy_of_buried_loc #

/NO_APP
        subl    $4, %esp
        movsbl  _copy_of_buried_loc,%eax
        pushl   %eax
        pushl   _copy_of_buried_arg
        pushl   $LC1
        call    _printf
        addl    $16, %esp
        jmp     L3
L2:
        subl    $12, %esp
        pushl   $LC2
        call    _printf
        addl    $16, %esp
L3:
        cmpl    $9, 8(%ebp)
        jg      L1
        subl    $12, %esp
        movl    8(%ebp), %eax
        incl    %eax
        pushl   %eax
        call    _rec
        addl    $16, %esp
L1:
        leave
        ret

最初将ebp(40和31)的偏移设置为任意猜测值(例如0),然后通过观察反汇编和一些简单的计算进行细化。

注意,当函数递归调用时,函数使用额外的12 + 4 = 16字节堆栈进行对齐和参数:

        subl    $12, %esp
        movl    8(%ebp), %eax
        incl    %eax
        pushl   %eax
        call    _rec
        addl    $16, %esp

返回地址也有4个字节。

然后该函数对旧的ebp及其局部变量使用4 + 8 = 12个字节:

_rec:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp

因此,每次递归调用时,堆栈总数增加16 + 4 + 12 = 32个字节。

现在,我们知道如何通过arg获取本地locebp

        movl    8(%ebp), %eax ; <- arg
        addl    %eax, %eax
        addb    _glo, %al
        movb    %al, -1(%ebp) ; <- loc

因此,我们只需将偏移量8和-1加到32,然后到达40和31。

做同样的事情,你就会得到你的“隐藏”变量。