https://sourceware.org/bugzilla/show_bug.cgi?id=18379
Bug ID: 18379 Summary: .fini_array support brings in unused codes Product: binutils Version: 2.26 (HEAD) Status: NEW Severity: normal Priority: P2 Component: ld Assignee: unassigned at sourceware dot org Reporter: hjl.tools at gmail dot com Target Milestone: --- To support .fini_array, linker always links in the .fini_array handler even if it isn't used. We can't mark the .fini_array handler weak since it won't be called when it is used: [hjl@gnu-6 fini]$ cat foo.c static void foo ( void ) __attribute__((destructor)); static int counter; static void foo (void) { counter++; } [hjl@gnu-6 fini]$ cat start.c extern void __libc_csu_fini (void) #ifdef WEAK __attribute__((weak)) #endif ; void _start () { #ifdef WEAK if (__libc_csu_fini) #endif __libc_csu_fini (); } [hjl@gnu-6 fini]$ cat fini.c #include <stdint.h> #include <stddef.h> extern void (*__fini_array_start[]) (void); extern void (*__fini_array_end[]) (void); static void run_fini_arry (void *p) { uintptr_t i = (uintptr_t) p; while (i-- > 0) (*__fini_array_start [i]) (); } extern void bar (void (*fn) (void *), void *arg, void *handle); void __libc_csu_fini (void) { ptrdiff_t count = __fini_array_end - __fini_array_start; if (count) bar ((void (*) (void *)) run_fini_arry, (void *) count, NULL); } [hjl@gnu-6 fini]$ cat bar.S .section .text.bar,"ax",@progbits .globl bar .type bar, @function bar: .space 0x400000 .size bar, .-bar [hjl@gnu-6 fini]$ make LD=ld gcc -O2 -g -c -o start.o start.c ld -o x start.o libbar.a gcc -O2 -g -DWEAK -c start.c -o startweak.o gcc -O2 -g -fpic -c -o foo.o foo.c ld -o xw startweak.o foo.o libbar.a ld -o yw startweak.o libbar.a ls -l x xw yw -rwxrwxr-x 1 hjl hjl 4197928 May 5 12:59 x -rwxrwxr-x 1 hjl hjl 2864 May 5 12:59 xw -rwxrwxr-x 1 hjl hjl 1968 May 5 12:59 yw [hjl@gnu-6 fini]$ -- You are receiving this mail because: You are on the CC list for the bug. _______________________________________________ bug-binutils mailing list bug-binutils@gnu.org https://lists.gnu.org/mailman/listinfo/bug-binutils