Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The stack is used for the local variables but also for intermediate values that the function need. Here, your <code>foo()</code> function calls <code>bar()</code> by giving it two pointers, one to the string (<code>argv[1]</code>) and the other to the <code>buf</code> variable; those pointer values are pushed on the stack, namely in the <code>mov %eax,0x4(%esp)</code> and <code>mov %edx,(%esp)</code> opcodes. Thus, <code>foo()</code> needs a bit more than 256 bytes of stack space.</p> <p>In more details:</p> <pre><code>0x08048473 &lt;foo+0&gt;:push %ebp 0x08048474 &lt;foo+1&gt;:mov %esp,%ebp </code></pre> <p>That's the standard function prologue: the function will use <code>%ebp</code> to points to the stack elements <em>before</em> it was called (i.e. its arguments).</p> <pre><code>0x08048476 &lt;foo+3&gt;:sub $0x118,%esp </code></pre> <p>Some space is reserved on the stack, mainly (but not only) for <code>buf[]</code>.</p> <pre><code>0x0804847c &lt;foo+9&gt;:mov 0x8(%ebp),%eax 0x0804847f &lt;foo+12&gt;:add $0x4,%eax 0x08048482 &lt;foo+15&gt;:mov (%eax),%edx </code></pre> <p><code>0x8(%ebp)</code> is the <code>argv</code> function argument; these opcodes fetch the pointer from <code>argv[1]</code> and store the result in <code>%edx</code>. This will become the first argument to <code>bar()</code>.</p> <pre><code>0x08048484 &lt;foo+17&gt;:lea -0x100(%ebp),%eax </code></pre> <p>This stores in <code>%eax</code> the address of <code>buf[]</code> -- the compiler decided that <code>buf[]</code> was to be located in the upper 256 bytes of the stack space it reserved with <code>sub</code>.</p> <pre><code>0x0804848a &lt;foo+23&gt;:mov %eax,0x4(%esp) 0x0804848e &lt;foo+27&gt;:mov %edx,(%esp) </code></pre> <p>The two arguments for <code>bar()</code> are pushed on the stack (actually, written in the two top stack positions, <code>%esp</code> having been already adjusted).</p> <pre><code>0x08048491 &lt;foo+30&gt;:call 0x8048454 &lt;bar&gt; </code></pre> <p><code>bar()</code> is called.</p> <pre><code>0x08048496 &lt;foo+35&gt;:leave 0x08048497 &lt;foo+36&gt;:ret </code></pre> <p><code>leave</code> undoes the prologue (it is equivalent to <code>mov %ebp, %esp; pop %ebp</code>). <code>ret</code> exits the function.</p> <p>GCC is known to overallocate a bit on the stack; here, it could have reserved 264 bytes instead of 280. This seems to be an artefact of its internal register allocation optimizer (the extra stack slots <em>were</em> used to store intermediate values, but the optimizer finally found ways to keep the corresponding values in registers only).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload