[PATCH] RISC-V: Avoid relocation for a function symbol self-referenced with -mno-relax

Jan Beulich jbeulich@suse.com
Tue Jan 7 08:07:06 GMT 2025


On 07.01.2025 04:03, Xiao Zeng wrote:
> 2025-01-07 10:46  Nelson Chu <nelson@rivosinc.com> wrote:
>>
>> On Tue, Jan 7, 2025 at 9:54 AM Xiao Zeng <zengxiao@eswincomputing.com>
>> wrote:
>>> --- a/gas/config/tc-riscv.c
>>> +++ b/gas/config/tc-riscv.c
>>> @@ -4800,7 +4800,7 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
>>>           Fill in a tentative value to improve objdump readability for
>>> -mrelax,
>>>           and set fx_done for -mno-relax.  */
>>>         if (fixP->fx_addsy
>>> -         && S_IS_LOCAL (fixP->fx_addsy)
>>> +         && (S_IS_LOCAL (fixP->fx_addsy) || !riscv_opts.relax)
>>>            && S_GET_SEGMENT (fixP->fx_addsy) == seg)
>>>          {
>>>            bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
>>> @@ -4835,7 +4835,7 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
>>>          riscv_pcrel_hi_fixup *entry = htab_find (riscv_pcrel_hi_fixup_hash,
>>>                                                   &search);
>>>          if (entry && entry->symbol
>>> -           && S_IS_LOCAL (entry->symbol)
>>> +           && (S_IS_LOCAL (entry->symbol) || !riscv_opts.relax)
>>>              && S_GET_SEGMENT (entry->symbol) == seg)
>>>            {
>>>              bfd_vma target = entry->target;
>>>
>>
>> The original idea was to only optimize local symbols, since global/weak
>> symbols may be preemptive in the link time, so we cannot resolve them in
>> the assembler time. 
> Your point is correct, but considering this special case: When relaxation is
> disabled, the assembler no longer needs to generate relocation information
> for references to this symbol within its body. Regardless of whether it's a
> global symbol, the relocation can be completed during the assembly phase,
> without needing to defer it to the linking (ld) phase.
> 
> It is optimized specifically for this special scenario.

Yet where in your change is the check that the reference is a _self_ reference?
How would you even reliably recognize such? Consider the case of one function-
like construct falling through to another one, with the latter having such a
self reference. If the latter is preempted, for it the omitted relocation
doesn't matter because it won't be invoked from elsewhere. Whereas if at the
same time the former isn't preempted, the references still need resolving
correctly to whatever overrode the latter symbol.

Jan


More information about the Binutils mailing list