[PATCH v2] Add -fgnu-retain to place used symbols in SHF_GNU_RETAIN section

Jakub Jelinek jakub@redhat.com
Wed Feb 17 14:26:38 GMT 2021


On Tue, Feb 16, 2021 at 11:59:21AM -0800, H.J. Lu wrote:
> 	PR target/99113
> 	* common.opt: Add -fgnu-retain.

I'm not sure -fgnu-retain as the option name.
Wouldn't say -fretain-used-vars be better?

> @@ -1666,6 +1666,10 @@ floop-unroll-and-jam
>  Common Var(flag_unroll_jam) Optimization
>  Perform unroll-and-jam on loops.
>  
> +fgnu-retain
> +Common Var(flag_gnu_retain)
> +Use SHF_GNU_RETAIN on used symbols if supported by the assembler and the linker.

on variables with the used attribute?

> diff --git a/gcc/toplev.c b/gcc/toplev.c
> index d8cc254adef..119cd7c0432 100644
> --- a/gcc/toplev.c
> +++ b/gcc/toplev.c
> @@ -1761,6 +1761,13 @@ process_options (void)
>    if (flag_large_source_files)
>      line_table->default_range_bits = 0;
>  
> +  if (flag_gnu_retain && !SUPPORTS_SHF_GNU_RETAIN)
> +    {
> +      warning_at (UNKNOWN_LOCATION, 0, "%qs is not supported for this target",
> +		  "-fgnu-retain");
> +      flag_gnu_retain = 0;
> +    }
> +
>    /* Please don't change global_options after this point, those changes won't
>       be reflected in optimization_{default,current}_node.  */
>  }
> diff --git a/gcc/varasm.c b/gcc/varasm.c
> index 29478ab0d8d..4e0e30abee5 100644
> --- a/gcc/varasm.c
> +++ b/gcc/varasm.c
> @@ -297,7 +297,7 @@ get_section (const char *name, unsigned int flags, tree decl,
>    slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
>  					    INSERT);
>    flags |= SECTION_NAMED;
> -  if (SUPPORTS_SHF_GNU_RETAIN
> +  if (flag_gnu_retain
>        && decl != nullptr
>        && DECL_P (decl)
>        && DECL_PRESERVE_P (decl))
> @@ -487,7 +487,7 @@ resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
>    if (DECL_SECTION_NAME (decl) == NULL
>        && targetm_common.have_named_sections
>        && (flag_function_or_data_sections
> -	  || (SUPPORTS_SHF_GNU_RETAIN && DECL_PRESERVE_P (decl))
> +	  || (flag_gnu_retain && DECL_PRESERVE_P (decl))
>  	  || DECL_COMDAT_GROUP (decl)))
>      {
>        targetm.asm_out.unique_section (decl, reloc);

I'm not convinced this will work properly with LTO, the option from
-flto -c compilation would be ignored.
For functions, we usually mark the option Optimization and make it
saved/restored on function switches, but this is for variables, so it will
not work for those.
I'd think better would be to add when seeing "used" attribute some
artificial attribute (unless we have "retain" attribute to mean that, say
"retain ") when the flag is on in handle_used_attribute and use that
attribute in varasm instead of the option?

> @@ -1227,7 +1227,7 @@ get_variable_section (tree decl, bool prefer_noswitch_p)
>      vnode->get_constructor ();
>  
>    if (DECL_COMMON (decl)
> -      && !(SUPPORTS_SHF_GNU_RETAIN && DECL_PRESERVE_P (decl)))
> +      && !(flag_gnu_retain && DECL_PRESERVE_P (decl)))
>      {
>        /* If the decl has been given an explicit section name, or it resides
>  	 in a non-generic address space, then it isn't common, and shouldn't
> @@ -7761,7 +7761,7 @@ switch_to_section (section *new_section, tree decl)
>  {
>    if (in_section == new_section)
>      {
> -      if (SUPPORTS_SHF_GNU_RETAIN
> +      if (flag_gnu_retain
>  	  && (new_section->common.flags & SECTION_NAMED)
>  	  && decl != nullptr
>  	  && DECL_P (decl)

> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -16168,6 +16168,11 @@ DSOs; if your program relies on reinitialization of a DSO via
> @code{dlclose} and @code{dlopen}, you can use
> @option{-fno-gnu-unique}.
>
> +@item -fgnu-retain
> +@opindex fgnu-retain
> +On systems with recent GNU assembler and linker, the compiler places
> +used symbols in separate SHF_GNU_RETAIN sections.

Again, variables with the used attribute.

	Jakub



More information about the Gcc-patches mailing list