[PATCH] "used" attribute saves decl from linker garbage collection
Thu Nov 5 11:37:39 GMT 2020
On Thu, Nov 05, 2020 at 06:21:21AM -0500, Hans-Peter Nilsson wrote:
> On Wed, 4 Nov 2020, H.J. Lu wrote:
> > .retain is ill-defined. For example,
> > [hjl@gnu-cfl-2 gcc]$ cat /tmp/x.c
> > static int xyzzy __attribute__((__used__));
> > [hjl@gnu-cfl-2 gcc]$ ./xgcc -B./ -S /tmp/x.c -fcommon
> > [hjl@gnu-cfl-2 gcc]$ cat x.s
> > .file "x.c"
> > .text
> > .retain xyzzy <<<<<<<<< What does it do?
> > .local xyzzy
> > .comm xyzzy,4,4
> > .ident "GCC: (GNU) 11.0.0 20201103 (experimental)"
> > .section .note.GNU-stack,"",@progbits
> > [hjl@gnu-cfl-2 gcc]$
> To answer that question: it's up to the assembler, but for ELF
> and SHF_GNU_RETAIN, it seems obvious it'd tell the assembler to
> set SHF_GNU_RETAIN for the section where the symbol ends up.
> We both know this isn't rocket science with binutils.
Indeed, and my patch handles it trivially:
+obj_elf_retain (int arg ATTRIBUTE_UNUSED)
.... snip ....
+ sym = get_sym_from_input_line_and_check ();
+ symbol_get_obj (sym)->retain = 1;
@@ -2624,6 +2704,9 @@ elf_frob_symbol (symbolS *symp, int *puntp)
+ if (symbol_get_obj (symp)->retain)
+ elf_section_flags (S_GET_SEGMENT (symp)) |= SHF_GNU_RETAIN;
/* Double check weak symbols. */
if (S_IS_WEAK (symp))
We could check that the symbol named in the .retain directive has
already been defined, however this isn't compatible with GCC
mark_decl_preserved handling, since mark_decl_preserved is called
emitted before the local symbols are defined in the assembly output
GAS should at least validate that the symbol named in the .retain
directive does end up as a symbol though.
> brgds, H-P
More information about the Gcc-patches