Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I did some further debugging, and found that the "culprit" was the <code>-freorder-functions</code> optimization. It is documented in the manpage as follows:</p> <pre><code>-freorder-functions Reorder functions in the object file in order to improve code locality. This is implemented by using special subsections ".text.hot" for most frequently executed functions and ".text.unlikely" for unlikely executed functions. Reordering is done by the linker so object file format must support named sections and linker must place them in a reasonable way. </code></pre> <p>The last line in the documentation explains the problem I was having/causing. If we look at the compile commands from the original question again:</p> <pre><code>$ avr-gcc -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct \ -fshort-enums -mmcu=attiny13 -DF_CPU=1200000 -Wall -Wstrict-prototypes \ -Os -c test.c -o test.o $ avr-gcc test.o -o test.elf </code></pre> <p>...we see that I was passing the optimization flags to the compiler, but not to the linker. I assumed that the <code>CFLAGS</code> only affect compilation and not linking, so I did not pass them to the linker, but in that case I was wrong.</p> <p>The result: The assembly code was reordered by the compiler (including appropriate labels), but the linker did not consider these labels. And because the <code>test</code> function was placed before the <code>main</code> function by the compiler and not re-arranged by the linker, that was the code that was actually executed on the microcontroller.</p> <p>So the solution turned out to be: <strong>Compiler flags should also be passed to the linker!</strong></p>
 

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