[PATCH] ld: Add -z start-stop-gc to let __start_/__stop_ not retain C identifier name sections

Fangrui Song i@maskray.me
Sun Feb 28 01:50:22 GMT 2021


On 2021-02-27, H.J. Lu via Binutils wrote:
>On Sat, Feb 27, 2021 at 12:52 PM Fangrui Song via Binutils
><binutils@sourceware.org> wrote:
>>
>> This rule was initially added in PR11133 to work around glibc.  However,
>> it makes GC on C identifier name sections ineffective.  In particular,
>> many modern metadata sections (e.g. clang -fsanitize-coverage and
>> -fprofile-generate) cannot be GCed.
>>
>> Add -z start-stop-gc to drop the rule.
>>
>>         PR 27451
>> include/
>>         * bfdlink.h (struct bfd_link_info): Add start_stop_gc.
>> ld/
>>         * emultempl/elf.em: Handle -z start-stop-gc and -z nostart-stop-gc.
>>         * ldmain.c: Initialize start_stop_gc.
>>         * lang.c (lang_process): Handle start_stop_gc.
>>         * testsuite/ld-gc/gc.exp: New test.
>>         * testsuite/ld-gc/start.s: Add .weak directive.
>>         * testsuite/ld-gc/start2.d: New test.
>>         * ld.texi: Mention -z start-stop-gc and -z nostart-stop-gc.
>>         * NEWS: Mention -z start-stop-gc.
>
>Does this option work with static glibc executable?
>
>-- 
>H.J.

Yes for a simple example.

// https://sourceware.org/bugzilla/show_bug.cgi?id=11133
#include <stdio.h>
int main() {
   printf("Hello World\n");
}


# --reproduce feature request: https://sourceware.org/bugzilla/show_bug.cgi?id=26119
gcc -fuse-ld=lld hello.cc -static -Wl,--gc-sections -Wl,--reproduce=/tmp/rep.tar
cd /tmp; tar xf rep.tar; cd rep
# Delete --chroot from response.txt
% ~/Dev/binutils-gdb/Debug/ld/ld-new @response.txt --gc-sections --print-gc-sections 2> 0
% ~/Dev/binutils-gdb/Debug/ld/ld-new @response.txt --gc-sections --print-gc-sections -z start-stop-gc 2> 1
% ./a.out
Hello World

There is one section which is now GCed:


% diff 0 1
16a17
> /home/ray/Dev/binutils-gdb/Debug/ld/ld-new: removing unused section '__libc_atexit' in file 'usr/lib/x86_64-linux-gnu/libc.a(genops.o)'

I don't know what the expectation is, but if the section is required to be retained, we should use SHF_GNU_RETAIN or other GC root approach
(see
https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order "What if all metadata sections are discarded?")


More information about the Binutils mailing list