title: Where do I find the assembly that creates a static variable in the .data section of my C program?
body: <p>First time poster. 2nd year CS student.</p>
  
  <p>I am exploring the creation of static variables in the .data section of the Virtual Address Space in the context of a C source->GCC compilation->Linux execution environment.</p>
  
  <p>C program is test.c</p>
  
  <pre><code>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;
   }
  </code></pre>
  
  <p>Asking GDB to '<code>disass /m</code>' reveals that there is no code for the staticVar[] creation because inspecting the .s file reveals the variable resides in the read/write .data segment of the virtual address space having been placed there at the time of process creation(this process is what I'm interested in). </p>
  
  <p>Examining the output of (I though it was '<code>readelf -A test.o</code>') the object file contains assembly for what I assume is the creation of the array in the data segment. Here is the ELF output.    </p>
  
  <p>(Bonus if you can tell me what command generates this output. I can not duplicate it using readelf. I picked up the command off a website and saved output. I cant remember how generated) </p>
  
  <p>[snip]</p>
  
  <pre><code>    00000000 &lt;staticVar.1359&gt;:
     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
  </code></pre>
  
  <p>[snip]</p>
  
  <p><strong>Assumptions(please correct)</strong>: This assembly exists in the executable and is run by load_elf_binary(), or some part of the execve() initiated series of functions. I have no at&amp;t (basic intel) syntax knowledge but even intuitively I dont see how these instructions can build an array. Looks like they are just adding register values together.</p>
  
  <p><strong>Bottom Line:</strong> I would like to know as much as possible about the life cycle of this static array especially where is the "missing code" that builds it and how can I look at it? Or better yet how can I debug (step through) the loader process? I have tried setting a breakpoint before main at the __start_libc entry (or something like that) but could not identify anything promising in this area.</p>
  
  <p>Links to additional info are great!
  Thanks for your time!  </p>