<div dir="ltr">I've filed an internal issue tracker for us to investigate the impact of this proposal, although I don't know when we'll get a chance to schedule the work at this point. Also, it's worth noting that we can't test all downstream codebases that potentially could use this feature, so we'll likely want some potential source code or switch that will allow our users to keep their unused sections.<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 20 Feb 2021 at 00:01, Fangrui Song via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
tl;dr With --gc-sections, I think the rule "__start_foo/__stop_foo references from live sections retains all non-SHF_LINK_ORDER input sections foo" does not cary its weight, so I'd like to drop it entirely in <a href="https://reviews.llvm.org/D96914" rel="noreferrer" target="_blank">https://reviews.llvm.org/D96914</a><br>
<br>
I have done a large-scale internal test with huge amount of OSS usage and spotted two issues:<br>
<br>
(1) Linking systemd. <a href="https://github.com/systemd/systemd/blob/main/src/libsystemd/sd-bus/bus-error.h#L33" rel="noreferrer" target="_blank">https://github.com/systemd/systemd/blob/main/src/libsystemd/sd-bus/bus-error.h#L33</a> there will be an `undefined symbol: __start_SYSTEMD_BUS_ERROR_MAP` error. Supposedly it can be trivially fixed by using undefined weak symbols on __start_/__stop_.<br>
(2) Linking Swift. There will be errors like `undefined hidden symbol: __start_swift5_protocols`.<br>
    <a href="https://github.com/apple/swift/blob/main/stdlib/public/runtime/SwiftRT-ELF.cpp" rel="noreferrer" target="_blank">https://github.com/apple/swift/blob/main/stdlib/public/runtime/SwiftRT-ELF.cpp</a><br>
    It seems that trivially making `extern const char __start_##name` does not work.<br>
    The code relies on some `swift5_*` input sections being GC root.<br>
    (If someone can file an issue to Swift, I'd appreciate that.)<br>
    (If Swift folks can fix it, I'll give my big thanks:)<br>
<br>
This can still potentially break some propritary code so I am sending this heads-up.<br>
I'll place rationale below (it is complicated).<br>
<br>
<br>
<br>
The current rule is:<br>
<br>
   __start_/__stop_ references retains all non-SHF_LINK_ORDER C identifier name sections.<br>
<br>
After <a href="https://reviews.llvm.org/D96753" rel="noreferrer" target="_blank">https://reviews.llvm.org/D96753</a> , it will become<br>
<br>
   __start_/__stop_ references retains all non-SHF_LINK_ORDER non-SHF_GROUP C identifier name sections.<br>
<br>
(The section group special case is to allow garbage collecting __llvm_prf_* sections for -fprofile-generate/-fprofile-instr-generate. The saving is huge.)<br>
<br>
Personally I'd drop the rule entirely (D96914) (get support from jhenderson and phosek), i.e.<br>
<br>
   __start_/__stop_ references do not retain C identifier name sections.<br>
<br>
and hope folks can fix Swift/systemd to not rely on the original rule.<br>
<br>
---<br>
<br>
I have placed more details in <a href="https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order" rel="noreferrer" target="_blank">https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order</a><br>
discussing why the rule gets in the away and why SHF_LINK_ORDER is not a solution.<br>
(Section groups have size overhead for single metadata section.)<br>
<br>
I'll paste the relevant paragraph here for your convenience.<br>
(I may edit my article to make it clear)<br>
<br>
This is a common usage of metadata sections: each text section references a metadata section.<br>
The metadata sections have a C identifier name to allow the runtime to collect them via `__start_`/`__stop_` symbols.<br>
<br>
Since `__start_`/`__stop_` references are always present from live sections, the C identifier name sections appear like GC roots, which means they cannot be discarded by `ld --gc-sections`.<br>
<br>
For users who want to keep GC for these metadata sections, they can set the `SHF_LINK_ORDER` flag or make the metadata section a member of a section group.<br>
(GNU ld does not implement the `SHF_LINK_ORDER` rule yet. <<a href="https://sourceware.org/bugzilla/show_bug.cgi?id=27259" rel="noreferrer" target="_blank">https://sourceware.org/bugzilla/show_bug.cgi?id=27259</a>>)<br>
(In LLD, some folks have concluded that this rule does not cary its weight, so possibly it would be nice it we can drop it ([D96914](<a href="https://reviews.llvm.org/D96914))" rel="noreferrer" target="_blank">https://reviews.llvm.org/D96914))</a>.)<br>
<br>
Now, let's walk through an `SHF_LINK_ORDER` example when inlining can cause problems.<br>
<br>
```asm<br>
# Monolithic meta.<br>
.globl _start<br>
_start:<br>
   leaq __start_meta(%rip), %rdi<br>
   leaq __stop_meta(%rip), %rsi<br>
<br>
.section .text.foo,"ax",@progbits<br>
.globl foo<br>
foo:<br>
   leaq .Lmeta.foo(%rip), %rax<br>
   ret<br>
<br>
.section .text.bar,"ax",@progbits<br>
.globl bar<br>
bar:<br>
   call foo<br>
   leaq .Lmeta.bar(%rip), %rax<br>
   ret<br>
<br>
.section meta,"a",@progbits<br>
.Lmeta.foo:<br>
   .byte 0<br>
.Lmeta.bar:<br>
   .byte 1<br>
```<br>
<br>
The monolithic `meta` does not enable precise garbage collection.<br>
It may be attempting to make `meta` separate and add the `SHF_LINK_ORDER` flag (to defeat the "C identifier name sections appear like GC roots" rule):<br>
<br>
```asm<br>
.section meta,"ao",@progbits,foo<br>
.Lmeta.foo:<br>
   .byte 0<br>
<br>
.section meta,"ao",@progbits,bar<br>
.Lmeta.bar:<br>
   .byte 1<br>
```<br>
<br>
However, due to inlining (foo into bar), the `meta` for `.text.foo` may now get a reference from another text section `.text.bar`, breaking an implicit assumption of `SHF_LINK_ORDER`: such a section can only be referenced from its linked-to section.<br>
```asm<br>
# Both .text.foo and .text.bar reference meta.<br>
.section .text.foo,"ax",@progbits<br>
.globl foo<br>
foo:<br>
   leaq .Lmeta.foo(%rip), %rax<br>
   ret<br>
<br>
.section .text.bar,"ax",@progbits<br>
.globl bar<br>
bar:<br>
   leaq .Lmeta.foo(%rip), %rax<br>
   leaq .Lmeta.bar(%rip), %rax<br>
   ret<br>
```<br>
<br>
If `.text.bar` (caller) is retained while `.text.foo` (callee) is discarded, the `meta` for `foo` will link to a discarded section: an invalid state.<br>
LLD has an error: `{{.*}}:(meta): sh_link points to discarded section {{.*}}:(.text.foo)`.<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>