Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tstreiff opened this issue Jun 29, 2020 · 0 comments

Comments

@tstreiff
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant