[PATCH v2] LoongArch: Fix the infinite loop caused by calling undefweak symbol
Lulu Cai
cailulu@loongson.cn
Thu Nov 21 01:24:19 GMT 2024
On 11/20/24 10:22 AM, mengqinggang wrote:
>
> 在 2024/11/20 上午9:53, Xi Ruoyao 写道:
>> On Wed, 2024-11-20 at 09:28 +0800, Lulu Cai wrote:
>>> The undefweak symbol value of non-default visibility is 0 and does
>>> not use plt entry, and will not be relocated in the relocate_secion
>>> function. As a result, an infinite loop is generated because
>>> bl %plt(sym) => bl 0.
>>>
>>> Fix this by converting the bl/call36 instructions to nop or removing
>>> them.
>> IMO it's better to convert it to "jirl $ra, $zero, 0". Semantically an
>> undefined weak symbol should be resolved to address 0 so we should make
>> a call to address 0, instead of silently ignore the call.
>>
>> Also "b %plt(sym)" should be turned to "jirl $zero, $zero, 0" as well
>> when sym is non-default visibility and weak undefined.
>
> AArch64 optimizes calls to undefined weak symbols to a NOP. It maybe
> a better choice.
>
Each port handles calls to non-default visibility undefweak symbols
slightly differently.
x86-64 and riscv preserve the semantics of the symbol using "call 0" and
"jalr 0", while
aarc64 optimizes the call to "nop". It is unclear why aarch64 optimizes
instead of preserving
semantics.
> In bfd/elfnn-aarch64.c:
>
> 6046 /* A call to an undefined weak symbol is converted to a
> jump to
> 6047 the next instruction unless a PLT entry will be created.
> 6048 The jump to the next instruction is optimized as a NOP.
> 6049 Do the same for local undefined symbols. */
> 6050 if (weak_undef_p && ! via_plt_p)
> 6051 {
> 6052 bfd_putl32 (INSN_NOP, hit_data);
> 6053 return bfd_reloc_ok;
> 6054 }
More information about the Binutils
mailing list