https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92606

            Bug ID: 92606
           Summary: [avr] invalid merge of symbols in progmem and data
                    sections
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lists.oss at hamme dot info
  Target Milestone: ---
            Target: avr

AVR GCC 9.2.0 has a critical bug with merging identical constant progmem and
data section symbols to a single progmem symbol (.text). I discovered it while
debugging my 3d printer firmware (print head kept crashing inexplicably).

Due to this invalid optimization, if data is read from the symbol originally
defined in data space, only arbitrary data is read (from data space at the
address of the symbol in program space). Below is a short example for Arduino.

The example was compiled with GCC options `-Os -g -ffunction-sections
-fdata-sections -flto -Wl,--gc-section`. I'm not entirely sure, but the bug
appears to be triggered by link time optimization (-flto) in combination with
optimization levels -Os or higher.

-------------------
#include "Arduino.h"

static const PROGMEM float xyz_prog[] = { 123, 123, 123 };
float xyz[] = { 123, 123, 123 };

volatile int x = 0;

void setup() {
    Serial.begin(57600);
    Serial.print("X_prog: ");
    Serial.println(pgm_read_float_near(&xyz_prog[0]));
}

void loop() {
    Serial.print("X: ");
    Serial.println(xyz[x]);
}
-------------------

Expected output:

X_prog: 123
X: 123
X: 123
X: 123
...

Actual output (example):

X_prog: 123
X: -0.00
X: 0.00
X: 553676288.00
...

I've uploaded the example to https://github.com/xblax/avr_gcc_bug together with
the Arduino core library and a cmake project for easy compilation.

Reply via email to