[PATCH v3] x86: Add tls check in gas

Cui, Lili lili.cui@intel.com
Wed Sep 18 12:27:56 GMT 2024


> > +    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
> > +      /* Check GOTPC32 TLSDESC access model:
> > +
> > +        --- LP64 mode ---
> > +        leaq x@tlsdesc(%rip), %reg64 --> Memory reg must be %rip.
> > +
> > +        --- X32 mode ---
> > +        rex/rex2 leal x@tlsdesc(%rip), %reg32 --> Memory reg must be %rip.
> > +
> > +        In X32 mode, when dest is Egpr, gas will automatically add rex2 to it.
> > +        When dest is Gpr, we need to check whether there is a rex/rex2
> prefix.
> > +       */
> > +      if (i.tm.mnem_off != MN_lea)
> > +       return x86_tls_error_insn;
> > +      if (!i.base_reg)
> > +       return x86_tls_error_no_base_reg;
> > +      if (i.base_reg->reg_num != RegIP)
> > +       return x86_tls_error_rip;
> > +      if (i.op[1].regs->reg_type.bitfield.dword
> > +              && !(i.op[1].regs->reg_flags & RegRex2)
> 
> Please align &&
> 
> > +              && (pp.rex2_encoding == false
> 
> No need for (
> 
> > +                  && pp.rex_encoding == false
> > +                  && i.prefix[REX_PREFIX] != REX_OPCODE))
> 
> Limit it to x86_elf_abi == X86_64_ABI since output_insn will add a dummy
> REX_OPCODE prefix for lea with R_X86_64_GOTPC32_TLSDESC
> relocation when generating x32 object.  Otherwise, I got
> 
> /export/build/gnu/tools-build/binutils-gitlab/build-x86_64-linux/ld/../gas/as-
> new
>  -mx86-used-note=no --x32  -o tmpdir/pr25416-1.o
> /export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64/pr25416-
> 1.s
> Executing on host: sh -c
> {/export/build/gnu/tools-build/binutils-gitlab/build-x86_64-linux/ld/../gas/as-
> new
>  -mx86-used-note=no --x32  -o tmpdir/pr25416-1.o
> /export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64/pr25416-
> 1.s
> 2>&1}  /dev/null dump.tmp (timeout = 300)
> spawn [open ...]^M
> /export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64/pr25416-
> 1.s:
> Assembler messages:
> /export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64/pr25416-
> 1.s:5:
> Error: @TLSDESC operator requires REX prefix in x32 mode
> failed with: </export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-
> 64/pr25416-1.s:
> Assembler messages:
> /export/gnu/import/git/gitlab/x86-binutils/ld/testsuite/ld-x86-64/pr25416-
> 1.s:5:
> Error: @TLSDESC operator requires REX prefix in x32 mode>, no expected
> output
> FAIL: X32 GDesc 1
> 

Removed the rex/rex2 check here and added the comments.

> > +    case BFD_RELOC_NONE:
> > +      /* This isn't a relocation.  */
> > +      return x86_tls_error_continue;
> > +
> > +    default:
> > +      break;
> > +    }
> > +
> > +  /* This relocation is OK.  */
> > +  return x86_tls_error_none;
> > +}
> > +
> > +static void
> > +x86_report_tls_error (enum x86_tls_error_type tls_error, enum
> bfd_reloc_code_real r_type)
> 
> Line is too long.
> 

Done.

> >
> >      default:
> >        return 0;
> > @@ -17069,6 +17398,16 @@ md_show_usage (FILE *stream)
> >      fprintf (stream, _("(default: no)\n"));
> >    fprintf (stream, _("\
> >                            generate relax relocations\n"));
> > +
> > +  fprintf (stream, _("\
> > +  -mtls-check=[no|yes] "));
> > +  if (DEFAULT_X86_TLS_CHECK)
> > +    fprintf (stream, _("(default: yes)\n"));
> > +  else
> > +    fprintf (stream, _("(default: no)\n"));
> > +  fprintf (stream, _("\
> > +                    check tls relocation\n"));
> 
> Please use spaces not tab and align it with other help messages.
> Output TLS instead of tls.
> 

Done.

> > diff --git a/gas/configure.ac b/gas/configure.ac
> > index 6b978aae3f7..5fe57e52746 100644
> > --- a/gas/configure.ac
> > +++ b/gas/configure.ac
> > @@ -95,6 +95,17 @@ AC_ARG_ENABLE(x86_relax_relocations,
> >    no)  ac_default_x86_relax_relocations=0 ;;
> >  esac])dnl
> >
> > +# PR gas/32022
> > +# Decide if check tls relocation.
> 
> Decide if x86 assembler should check TLS relocation.
> 

Done.

> > +ac_default_x86_tls_check=unset
> > +# Provide a configure time option to override our default.
> > +AC_ARG_ENABLE(tls_check,
> 
> x86_tls_check
> 

Done.

> > +             AS_HELP_STRING([--enable-tls-check],
> > +             [check tls relocation by default]),
> 
> check x86 TLS relocation by default
>
 
Done.

> > +[case "${enableval}" in
> > +  no)  ac_default_x86_tls_check=0 ;;
> > +esac])dnl
> > @@ -737,6 +748,13 @@
> AC_DEFINE_UNQUOTED(DEFAULT_GENERATE_X86_RELAX_RELOCATIONS,
> >    $ac_default_x86_relax_relocations,
> >    [Define to 1 if you want to generate x86 relax relocations by default.])
> >
> > +if test ${ac_default_x86_tls_check} = unset; then
> > +  ac_default_x86_tls_check=1
> > +fi
> > +AC_DEFINE_UNQUOTED(DEFAULT_X86_TLS_CHECK,
> > +  $ac_default_x86_tls_check,
> > +  [Define to 1 if you want to check tls relocation by default.])
> 
> Define to 1 if you want to check x86 TLS relocation by default.
> 

Done.

Thanks,
Lili.


More information about the Binutils mailing list