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

LLD 18 leaves leaves behind undef symbols from discarded sections #85048

Closed
nikic opened this issue Mar 13, 2024 · 5 comments · Fixed by #85167
Closed

LLD 18 leaves leaves behind undef symbols from discarded sections #85048

nikic opened this issue Mar 13, 2024 · 5 comments · Fixed by #85167

Comments

@nikic
Copy link
Contributor

nikic commented Mar 13, 2024

After 1981b1b, LLD converts symbols in discarded sections into undef symbols. This happens even if there are no references to the symbol from non-discarded sections.

This change breaks building the Linux kernel with LLD in some configurations (CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y) and does not match the behavior of previous LLD versions or the behavior of the BFD linker.

To reproduce the issue:

test.c:

__attribute__((__section__(".discard"))) __attribute__((unused)) char __pcpu_unique_foo;

module.lds:

SECTIONS {
 /DISCARD/ : {
  *(.discard)
 }
}

Then run:

clang test.c -c
ld.lld -r -T module.lds test.o -o test
objdump -t test

This will produce:

0000000000000000       O *UND*	0000000000000000 __pcpu_unique_foo

While previously it did not.

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 13, 2024

@llvm/issue-subscribers-lld-elf

Author: Nikita Popov (nikic)

After https://github.com/llvm/llvm-project/commit/1981b1b6b92f7579a30c9ed32dbdf3bc749c1b40, LLD converts symbols in discarded sections into undef symbols. This happens even if there are no references to the symbol from non-discarded sections.

This change breaks building the Linux kernel with LLD in some configurations (CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y) and does not match the behavior of previous LLD versions or the behavior of the BFD linker.

To reproduce the issue:

test.c:

__attribute__((__section__(".discard"))) __attribute__((unused)) char __pcpu_unique_foo;

module.lds:

SECTIONS {
 /DISCARD/ : {
  *(.discard)
 }
}

Then run:

clang test.c -c
ld.lld -r -T module.lds test.o -o test
objdump -t test

This will produce:

0000000000000000       O *UND*	0000000000000000 __pcpu_unique_foo

While previously it did not.

@rnk
Copy link
Collaborator

rnk commented Mar 13, 2024

At Google, we noticed a related issue (internal 307561801) in our Linux kernel builds, but we believed it was specific to Google-internal build configurations, and that the LLD behavior was correct.

MaskRay added a commit to MaskRay/llvm-project that referenced this issue Mar 14, 2024
 llvm#69295 demoted Defined symbols relative to discarded sections.
If such a symbol is unreferenced, the desired behavior is to
eliminate it from .symtab just like --gc-sections discarded
definitions.
Linux kernel's CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y configuration expects
that the unreferenced `unused` is not emitted to .symtab
(ClangBuiltLinux/linux#2006).

For relocations referencing demoted symbols, the symbol index restores
to 0 like older lld (`R_X86_64_64 0` in `discard-section.s`).

Fix llvm#85048
@MaskRay
Copy link
Member

MaskRay commented Mar 14, 2024

Thanks for the detailed reproduce. A few changes have depended on this representation, so dropping demoteDefined would break many tests. #85167 should fix this forward.

MaskRay added a commit that referenced this issue Mar 14, 2024
…85167)

#69295 demoted Defined symbols relative to discarded sections.
If such a symbol is unreferenced, the desired behavior is to
eliminate it from .symtab just like --gc-sections discarded
definitions.
Linux kernel's CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y configuration expects
that the unreferenced `unused` is not emitted to .symtab
(ClangBuiltLinux/linux#2006).

For relocations referencing demoted symbols, the symbol index restores
to 0 like older lld (`R_X86_64_64 0` in `discard-section.s`).

Fix #85048
@nikic
Copy link
Contributor Author

nikic commented Mar 14, 2024

/cherry-pick 8fe3e70

llvmbot pushed a commit to llvmbot/llvm-project that referenced this issue Mar 14, 2024
…lvm#85167)

llvm#69295 demoted Defined symbols relative to discarded sections.
If such a symbol is unreferenced, the desired behavior is to
eliminate it from .symtab just like --gc-sections discarded
definitions.
Linux kernel's CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y configuration expects
that the unreferenced `unused` is not emitted to .symtab
(ClangBuiltLinux/linux#2006).

For relocations referencing demoted symbols, the symbol index restores
to 0 like older lld (`R_X86_64_64 0` in `discard-section.s`).

Fix llvm#85048

(cherry picked from commit 8fe3e70)
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 14, 2024

/pull-request #85266

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

Successfully merging a pull request may close this issue.

4 participants