Skip to content

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

@tstreiff

Description

@tstreiff

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:

  • 2 entries defining "li1_0" and "li2_1" (an integer postfix is added to make the local name unique across the object file) => OK
  • 2 "using" entries for "li1" and "li2" (no prefix for them) => wrong

=> 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions