在哪里可以找到在C程序的.data节中创建静态变量的程序集? [英] Where do I find the assembly that creates a static variable in the .data section of my C program?

查看:178
本文介绍了在哪里可以找到在C程序的.data节中创建静态变量的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次海报. CS 2年级学生.

我正在探索在C源代码-> GCC编译-> Linux执行环境的上下文中,在虚拟地址空间.data部分中创建静态变量的方法.

C程序是test.c

int main()
{
   register int i = 0;
   register int sum = 0;
   static int staticVar[10] = {1,2,3,4,5,6,7,8,9,-1};
   Loop: 
        sum = sum + staticVar[i]; //optimized away
    i = i+1;
    if(i != 10)
    goto Loop; 
   return 0;
 }

将GDB设置为'disass /m'会显示没有用于staticVar []创建的代码,因为检查.s文件会发现该变量位于虚拟地址空间的读/写.data段中,该位置已放置在该位置.在创建流程时(我对此流程很感兴趣).

检查目标文件的输出(尽管我是'readelf -A test.o',但我假设它是在数据段中创建数组的程序集).这是ELF输出.

((如果您能告诉我是什么命令生成此输出的.我无法使用readelf复制它.我从网站上拾取了该命令并保存了输出.我不记得如何生成)

[snip]

    00000000 <staticVar.1359>:
   0:01 00                  add    %eax,(%eax)
   2:00 00                  add    %al,(%eax) 
   4:02 00                  add    (%eax),%al
   6:00 00                  add    %al,(%eax)
   8:03 00                  add    (%eax),%eax
   a:00 00                  add    %al,(%eax)
   c:04 00                  add    $0x0,%al
   e:00 00                  add    %al,(%eax)
  10:05 00 00 00 06         add    $0x6000000,%eax
  15:00 00                  add    %al,(%eax)
  17:00 07                  add    %al,(%edi)
  19:00 00                  add    %al,(%eax)
  1b:00 08                  add    %cl,(%eax)
  1d:00 00                  add    %al,(%eax)
  1f:00 09                  add    %cl,(%ecx)
  21:00 00                  add    %al,(%eax)
  23:00 ff                  add    %bh,%bh
  25:ff                     (bad)  
  26:ff                     (bad)  
  27:ff                     .byte 0xff

[snip]

假设(请正确):该程序集存在于可执行文件中,并由load_elf_binary()或execve()启动的一系列功能的一部分运行.我没有at& t(基本intel)语法知识,但即使直觉上也看不到这些指令如何构建数组.看起来他们只是将寄存器值加在一起.

底线::我想尽可能多地了解此静态数组的生命周期,尤其是构建它的缺失代码"在哪里,我该如何看待它?还是更好,但是我该如何调试(逐步)加载程序呢?我曾尝试在__start_libc条目(或类似的条目)上的main之前设置一个断点,但是无法确定该区域中有希望的任何内容.

其他信息的链接非常棒! 谢谢你的时间!

解决方案

staticVar的初始化程序存储在可执行文件的.data部分中.使用objdump(例如

Links to additional info are great! Thanks for your time!

The initializers for staticVar is stored in the .data section of the executable. Using objdump (e.g. How can I examine contents of a data section of an ELF file on Linux?) should reveal something like this for your file:

./test:     file format elf64-x86-64

Contents of section .data:
 00d2c0 00000000 00000000 00000000 00000000  ................
 00d2d0 00000000 00000000 00000000 00000000  ................
 00d2e0 01000000 02000000 03000000 04000000  ................
 00d2f0 05000000 06000000 07000000 08000000  ................
 00d300 09000000 ffffffff 00000000 00000000  ................
 00d310 00000000 00000000 00000000 00000000  ................

That content from the executable is directly mapped into the address space of your process, so there is no need for any code to create the data. Codes that operate on staticVar will refer to the content directly using memory pointers; e.g. for the loop you posted, gcc -S gave me this:

  18                .L5:
  19 0013 90            nop
  20                .L2:
  21 0014 4863C3        movslq  %ebx, %rax
  22 0017 8B148500      movl    staticVar.1707(,%rax,4), %edx
  22      000000
  23 001e 8B45F4        movl    -12(%rbp), %eax
  24 0021 01D0          addl    %edx, %eax
  25 0023 8945F4        movl    %eax, -12(%rbp)
  26 0026 83C301        addl    $1, %ebx
  27 0029 83FB0A        cmpl    $10, %ebx
  28 002c 75E5          jne .L5

Lifetime of this static array would be the lifetime of your process, similar to a global variable. In any case, there is no code that builds it. It's just some data in memory.

P/S: You may need to add volatile to sum like such: volatile int sum = 0; Otherwise gcc would probably optimize it away since the resulting value of sum is never used.

这篇关于在哪里可以找到在C程序的.data节中创建静态变量的程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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