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

ld.lld -z start-stop-gc (GC of C identifier name sections) #1307

Closed
MaskRay opened this issue Feb 20, 2021 · 1 comment
Closed

ld.lld -z start-stop-gc (GC of C identifier name sections) #1307

MaskRay opened this issue Feb 20, 2021 · 1 comment
Labels
[TOOL] lld The issue is relevant to LLD linker

Comments

@MaskRay
Copy link
Member

MaskRay commented Feb 20, 2021

https://lists.llvm.org/pipermail/llvm-dev/2021-February/148682.html I am thinking whether we can change the existing ld.lld --gc-sections rule about C identifier name sections.
https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order has more information. You may skip the paragraphs before "C identifier name sections".

A C identifier name section consists of isalnum characters and _. . is disallowed.

GNU ld's rule is

__start_/__stop_ references from a live input section retains all C identifier name sections.

(--gc-sections is based on input sections. A live input section indicates that the section is retained, due to a relocation chain from a GC root).

In LLD<=12, the rule is:

__start_/__stop_ references from a live input section retains all non-SHF_LINK_ORDER C identifier name sections.

(Example SHF_LINK_ORDER C identifier name sections: __patchable_function_entries (-fpatchable-function-entry), __sancov_guards (clang -fsanitize-coverage=trace-pc-guard, before clang 13))

In LLD>=13, the default rule is:

__start_/__stop_ references from a live input section retains all non-SHF_LINK_ORDER non-SHF_GROUP C identifier name sections.

(SHF_GROUP is to allow more __llvm_prf_cnts/__llvm_prf_data sections to be GCable.)

With ld.lld -z start-stop-gc (requires https://reviews.llvm.org/D96914), the (unhelpful in many cases) rule will be dropped entirely:

__start_/__stop_ references do not retain C identifier name sections.

i.e. All C identifier name sections will be GCable.

(Note, a KEEP(...) command in linker scripts makes all the matched input sections GC roots.)

HAVE_LD_DEAD_CODE_DATA_ELIMINATION (--gc-sections) is currently not very common, but the adoption is rising. The kernel has a large number of __start_/__stop_ sections, so understanding whether ld.lld -z start-stop-gc can cause breakage will be useful. We probably need a thorough test.

If a link has different sizes with ld.lld -z start-stop-gc, this means some C identifier name sections (and other sections reachable via relocation chain) can now be GCed.

  • If nothing breaks => cool, we are now able to GC some unnecessarily retained sections.
  • If something breaks => we should think whether we should use
    • KEEP(...) command in a linker script => no-brainer but it can unnecessarily retain sections which are otherwise unneeded.
    • Add a relocation from the user section to the metadata section (preferably R_*_NONE).
@nickdesaulniers nickdesaulniers added the [TOOL] lld The issue is relevant to LLD linker label Feb 23, 2021
@MaskRay MaskRay changed the title ld.lld "Don't let __start_/__stop_ retain non-SHF_LINK_ORDER C identifier name sections" ld.lld -z start-stop-gc (GC of C identifier name sections) Feb 26, 2021
@nickdesaulniers
Copy link
Member

@MaskRay is there anything more you'd like to do here? I'm going to close this out pre-emptively, but please do reopen with more info if you have more thoughts here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[TOOL] lld The issue is relevant to LLD linker
Projects
None yet
Development

No branches or pull requests

2 participants