windelbouwman / ppci

A compiler for ARM, X86, MSP430, xtensa and more implemented in pure Python
https://ppci.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
337 stars 36 forks source link

ppci-cc: cannot take the address of a function static variable for initializing a function static variable #101

Open tstreiff opened 4 years ago

tstreiff commented 4 years ago

int main() { static int li1; static int li2; static int *a[] = { li1, li2 }; return 0; }

Compilation of this (correct) program goes well, but the linker crashes

File "/home/tsf/sandbox/ppci/ppci/binutils/objectfile.py", line 286, in get_symbol_id_value raise ValueError("Undefined reference {}".format(symbol.name)) ValueError: Undefined reference li1

When checking the compiler-generated .oj file, I noticed that there are:

=> The same rule should be applied for function static variable definitions and references, otherwise the references to "li1" and "li2" cannot be resolved by the linker.

Note that all this works well if the variables are static at file level.

A side effect: having seen how function static variable names are postfixed to create unique identifiers, I suspected this is not bullet-proof,: what happens if I define a variable li1_0 ?

static int i_0;

int main() { static int i; return 0; }

The compiler did not like it:

File "/home/tsf/sandbox/ppci/ppci/binutils/outstream.py", line 119, in do_emit assert symbol.undefined AssertionError