[PATCH] bfd/ELF: loongarch_{larch_reloc_name_lookup,adjust_reloc_bitsfield}() are exposed to gas

Lulu Cai cailulu@loongson.cn
Thu Nov 13 08:31:09 GMT 2025


Thanks for the patch!

This looks good to me. I've tested it on LoongArch and it works well 
with no regressions.

On 11/13/25 11:03 AM, Chenghua Xu wrote:
> Jan Beulich <jbeulich@suse.com> writes:
>
> @lulu,qinggang, Take a look at this patch..
>
>
>> As non-private functions, they should come with full disambiguating
>> prefixes - add bfd_elf_ to both. Hence commit 2903d813fcc2 ("bfd/ELF: mark
>> internal LoongArch functions hidden") also wrongly added ATTRIBUTE_HIDDEN
>> to them.
>>
>> While loongarch_get_uleb128_length() also falls in the same category,
>> having it live in libbfd, when outside of gas there's hardly any use to be
>> expected, isn't very useful. Drop the function altogether and simplify the
>> clearing of the ULEB128 in md_apply_fix().
>>
>> For loongarch_larch_reloc_name_lookup() drop gas'es custom declaration;
>> the libbfd one ought to be used, for producer and consumer to "see" the
>> same one. Also drop ATTRIBUTE_UNUSED there, as that makes sense only for
>> parameters in function definitions.
>>
>> --- a/bfd/elfnn-loongarch.c
>> +++ b/bfd/elfnn-loongarch.c
>> @@ -2758,7 +2758,7 @@ loongarch_reloc_rewrite_imm_insn (const
>>   {
>>     /* Adjust the immediate based on alignment and
>>        its position in the instruction.  */
>> -  if (!loongarch_adjust_reloc_bitsfield (input_bfd, howto, &reloc_val))
>> +  if (!bfd_elf_loongarch_adjust_reloc_bitsfield (input_bfd, howto, &reloc_val))
>>       return bfd_reloc_overflow;
>>   
>>     int bits = bfd_get_reloc_size (howto) * 8;
>> --- a/bfd/elfxx-loongarch.c
>> +++ b/bfd/elfxx-loongarch.c
>> @@ -1943,8 +1943,8 @@ loongarch_reloc_type_lookup (bfd *abfd A
>>   }
>>   
>>   bfd_reloc_code_real_type
>> -loongarch_larch_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
>> -				   const char *l_r_name)
>> +bfd_elf_loongarch_larch_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
>> +					   const char *l_r_name)
>>   {
>>     for (size_t i = 0; i < ARRAY_SIZE (loongarch_howto_table); i++)
>>       {
>> @@ -2081,8 +2081,8 @@ reloc_unsign_bits (bfd *abfd, reloc_howt
>>   }
>>   
>>   bool
>> -loongarch_adjust_reloc_bitsfield (bfd *abfd, reloc_howto_type *howto,
>> -				  bfd_vma *fix_val)
>> +bfd_elf_loongarch_adjust_reloc_bitsfield (bfd *abfd, reloc_howto_type *howto,
>> +					  bfd_vma *fix_val)
>>   {
>>     BFD_ASSERT (((loongarch_reloc_howto_type *)howto)->adjust_reloc_bits);
>>     return ((loongarch_reloc_howto_type *)
>> @@ -2223,10 +2223,3 @@ loongarch_write_unsigned_leb128 (bfd_byt
>>     while (len);
>>     return p;
>>   }
>> -
>> -int loongarch_get_uleb128_length (bfd_byte *buf)
>> -{
>> -  unsigned int len = 0;
>> -  _bfd_read_unsigned_leb128 (NULL, buf, &len);
>> -  return len;
>> -}
>> --- a/bfd/elfxx-loongarch.h
>> +++ b/bfd/elfxx-loongarch.h
>> @@ -33,12 +33,10 @@ loongarch_reloc_name_lookup (bfd *abfd A
>>     ATTRIBUTE_HIDDEN;
>>   
>>   extern bfd_reloc_code_real_type
>> -loongarch_larch_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
>> -				   const char *l_r_name) ATTRIBUTE_HIDDEN;
>> +bfd_elf_loongarch_larch_reloc_name_lookup (bfd *abfd, const char *l_r_name);
>>   
>>   bool
>> -loongarch_adjust_reloc_bitsfield (bfd *, reloc_howto_type *, bfd_vma *)
>> -  ATTRIBUTE_HIDDEN;
>> +bfd_elf_loongarch_adjust_reloc_bitsfield (bfd *, reloc_howto_type *, bfd_vma *);
>>   void
>>   bfd_elf32_loongarch_set_data_segment_info (struct bfd_link_info *, int *);
>>   void
>> @@ -48,8 +46,6 @@ bfd_byte *
>>   loongarch_write_unsigned_leb128 (bfd_byte *p, unsigned int len, bfd_vma value)
>>     ATTRIBUTE_HIDDEN;
>>   
>> -int loongarch_get_uleb128_length (bfd_byte *buf) ATTRIBUTE_HIDDEN;
>> -
>>   /* TRUE if this is a PLT reference to a local IFUNC.  */
>>   #define PLT_LOCAL_IFUNC_P(INFO, H) \
>>     ((H)->dynindx == -1 \
>> --- a/gas/config/loongarch-lex.h
>> +++ b/gas/config/loongarch-lex.h
>> @@ -32,6 +32,3 @@ loongarch_parse_expr (const char *expr,
>>   		      size_t max_reloc_num,
>>   		      size_t *reloc_num,
>>   		      offsetT *imm);
>> -bfd_reloc_code_real_type
>> -loongarch_larch_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
>> -				   const char *l_r_name);
>> --- a/gas/config/loongarch-parse.y
>> +++ b/gas/config/loongarch-parse.y
>> @@ -20,6 +20,7 @@
>>   #include "as.h"
>>   #include "loongarch-lex.h"
>>   #include "loongarch-parse.h"
>> +#include "bfd/elfxx-loongarch.h"
>>   static void yyerror (const char *s ATTRIBUTE_UNUSED)
>>   {
>>   };
>> @@ -133,7 +134,7 @@ reloc (const char *op_c_str, const char
>>       btype = BFD_RELOC_LARCH_B26;
>>     else
>>       {
>> -      btype = loongarch_larch_reloc_name_lookup (NULL, op_c_str);
>> +      btype = bfd_elf_loongarch_larch_reloc_name_lookup (NULL, op_c_str);
>>         if (btype == BFD_RELOC_NONE)
>>   	as_fatal (_("unsupported modifier %s"), op_c_str);
>>       }
>> --- a/gas/config/tc-loongarch.c
>> +++ b/gas/config/tc-loongarch.c
>> @@ -1530,7 +1530,7 @@ static void fix_reloc_insn (fixS *fixP,
>>   
>>     insn = bfd_getl32 (buf);
>>   
>> -  if (!loongarch_adjust_reloc_bitsfield (NULL, howto, &reloc_val))
>> +  if (!bfd_elf_loongarch_adjust_reloc_bitsfield (NULL, howto, &reloc_val))
>>       as_bad_where (fixP->fx_file, fixP->fx_line, "Reloc overflow");
>>   
>>     insn = (insn & (insn_t)howto->src_mask)
>> @@ -1832,12 +1832,9 @@ md_apply_fix (fixS *fixP, valueT *valP,
>>       case BFD_RELOC_LARCH_ADD_ULEB128:  */
>>       case BFD_RELOC_LARCH_SUB_ULEB128:
>>         {
>> -	unsigned int len = 0;
>> -	len = loongarch_get_uleb128_length ((bfd_byte *)buf);
>> -	bfd_byte *endp = (bfd_byte*) buf + len -1;
>>   	/* Clean the uleb128 value to 0. Do not reduce the length.  */
>> -	memset (buf, 0x80, len - 1);
>> -	*endp = 0;
>> +	for (bfd_byte *ptr = (bfd_byte *)buf; *ptr &= 0x80; ++ptr)
>> +	  /* Nothing.  */;
>>   	break;
>>         }
>>   




More information about the Binutils mailing list