Skip to content

[LLD] Evaluation of DEFINED linker script directive produces wrong output #64600

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

Closed
edward-jones opened this issue Aug 10, 2023 · 3 comments · Fixed by #65866
Closed

[LLD] Evaluation of DEFINED linker script directive produces wrong output #64600

edward-jones opened this issue Aug 10, 2023 · 3 comments · Fixed by #65866
Assignees

Comments

@edward-jones
Copy link
Collaborator

The following linker script causes unexpected behaviour when APPLE is not defined by any input objects. The unexpected output is that APPLE will resolve to '1' instead of '2'.

APPLE = DEFINED (APPLE) ? 1 : 2;

It appears that when the symbol is first processed it is added to the symbol table with the expected value, however LLD reevaluates symbol assignments, and during these subsequent evaluations of the expression the DEFINED now returns 1 as APPLE has been inserted in the symbol table.

@llvmbot
Copy link
Member

llvmbot commented Aug 10, 2023

@llvm/issue-subscribers-lld-elf

@renesas-kyle-finch
Copy link

I have run into a similar issue. I think it has to do with when the symbol being set is the same symbol being checked by DEFINED.

Building off the original example when APPLE is not defined:

Scenario 1:
APPLE = DEFINED (APPLE) ? APPLE : 2;
This evaluates to 0 instead of 2

Scenario 2:
ORANGE = DEFINED (APPLE) ? APPLE : 2;
This correctly evaluates to 2

@MaskRay
Copy link
Member

MaskRay commented Sep 9, 2023

Created #65866

ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this issue Sep 19, 2023
Fix llvm#64600: the currently implementation is minimal (see
https://reviews.llvm.org/D83758), and an assignment like
`__TEXT_REGION_ORIGIN__ = DEFINED(__TEXT_REGION_ORIGIN__) ? __TEXT_REGION_ORIGIN__ : 0;`
(used by avr-ld[1]) leads to a value of zero (default value in `declareSymbol`),
which is unexpected.

Assign orders to symbol assignments and references so that
for a script-defined symbol, the `DEFINED` results match users'
expectation. I am unclear about GNU ld's exact behavior, but this hopefully
matches its behavior in the majority of cases.

[1]: https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=ld/scripttempl/avr.sc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants