[PATCH] LoongArch: Fix wrong relocation handling of symbols defined by PROVIDE

Lulu Cai cailulu@loongson.cn
Wed Aug 14 11:51:56 GMT 2024


On 8/14/24 5:41 PM, ywgrit wrote:
>
> 在 2024/8/14 下午5:20, Lulu Cai 写道:
>> On 8/13/24 10:38 AM, Jinyang He wrote:
>>> On 2024-08-09 17:40, Lulu Cai wrote:
>>>
>>>> If the symbol defined by PROVIDE in the link script is not in SECTION,
>>>> the symbol is placed in the ABS section. The linker considers that
>>>> symbols in the ABS section do not need to calculate PC relative 
>>>> offsets.
>>>>
>>>> Symbols in ABS sections should calculate PC relative offsets normally
>>>> based on relocations.
>>>> ---
>>>>   bfd/elfnn-loongarch.c                              |  2 +-
>>>>   ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp |  3 +++
>>>>   ld/testsuite/ld-loongarch-elf/provide_abs.d        | 12 ++++++++++++
>>>>   ld/testsuite/ld-loongarch-elf/provide_abs.ld       |  1 +
>>>>   ld/testsuite/ld-loongarch-elf/provide_noabs.d      | 13 
>>>> +++++++++++++
>>>>   ld/testsuite/ld-loongarch-elf/provide_noabs.ld     |  7 +++++++
>>>>   ld/testsuite/ld-loongarch-elf/provide_sym.s        |  7 +++++++
>>>>   7 files changed, 44 insertions(+), 1 deletion(-)
>>>>   create mode 100644 ld/testsuite/ld-loongarch-elf/provide_abs.d
>>>>   create mode 100644 ld/testsuite/ld-loongarch-elf/provide_abs.ld
>>>>   create mode 100644 ld/testsuite/ld-loongarch-elf/provide_noabs.d
>>>>   create mode 100644 ld/testsuite/ld-loongarch-elf/provide_noabs.ld
>>>>   create mode 100644 ld/testsuite/ld-loongarch-elf/provide_sym.s
>>>>
>>>> diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
>>>> index af8cb2575e7..811ab8dc2f5 100644
>>>> --- a/bfd/elfnn-loongarch.c
>>>> +++ b/bfd/elfnn-loongarch.c
>>>> @@ -3305,7 +3305,7 @@ loongarch_elf_relocate_section (bfd 
>>>> *output_bfd, struct bfd_link_info *info,
>>>>         /* The r_symndx will be STN_UNDEF (zero) only for relocs 
>>>> against symbols
>>>>        from removed linkonce sections, or sections discarded by a 
>>>> linker
>>>>        script.  Also for R_*_SOP_PUSH_ABSOLUTE and PCREL to specify 
>>>> const.  */
>>>> -      if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
>>>> +      if (r_symndx == STN_UNDEF)
>>>>       {
>>>>         defined_local = false;
>>>>         resolved_local = false;
>>>> diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp 
>>>> b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
>>>> index 16d89cd5a43..d8bc7dc1eb1 100644
>>>> --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
>>>> +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
>>>> @@ -183,5 +183,8 @@ if [istarget "loongarch64-*-*"] {
>>>>     run_dump_test "ie-le-relax"
>>>>     run_dump_test "tlsdesc_abs"
>>>>     run_dump_test "tlsdesc_extreme"
>>>> +  run_dump_test "provide_abs"
>>>> +  run_dump_test "provide_noabs"
>>>> +
>>>>   }
>>>>   diff --git a/ld/testsuite/ld-loongarch-elf/provide_abs.d 
>>>> b/ld/testsuite/ld-loongarch-elf/provide_abs.d
>>>> new file mode 100644
>>>> index 00000000000..1514fb18b01
>>>> --- /dev/null
>>>> +++ b/ld/testsuite/ld-loongarch-elf/provide_abs.d
>>>> @@ -0,0 +1,12 @@
>>>> +#source: provide_sym.s
>>>> +#as:
>>>> +#ld: -T provide_abs.ld
>>>> +#objdump: -d
>>>> +
>>>> +.*: +file format .*
>>>> +
>>>> +#...
>>>> +   0:    58001085     beq             \$a0, \$a1, 16    # 10 <fun1>
>>>> +   4:    40000c80     beqz            \$a0, 12    # 10 <fun1>
>>>> +   8:    54000800     bl              8    # 10 <fun1>
>>>> +#pass
>>>> diff --git a/ld/testsuite/ld-loongarch-elf/provide_abs.ld 
>>>> b/ld/testsuite/ld-loongarch-elf/provide_abs.ld
>>>> new file mode 100644
>>>> index 00000000000..473476cdc65
>>>> --- /dev/null
>>>> +++ b/ld/testsuite/ld-loongarch-elf/provide_abs.ld
>>>> @@ -0,0 +1 @@
>>>> +PROVIDE(fun1 = 0x10);
>>>
>>> For pie, should we reject pc-relative jump abs symbol?
>>>
>>
>> Well, I'm not sure if this is necessary. I haven't found any other 
>> ports that do this.
>>
>>>> diff --git a/ld/testsuite/ld-loongarch-elf/provide_noabs.d 
>>>> b/ld/testsuite/ld-loongarch-elf/provide_noabs.d
>>>> new file mode 100644
>>>> index 00000000000..7d6bc4d1baf
>>>> --- /dev/null
>>>> +++ b/ld/testsuite/ld-loongarch-elf/provide_noabs.d
>>>> @@ -0,0 +1,13 @@
>>>> +#source: provide_sym.s
>>>> +#as:
>>>> +#ld: -T provide_noabs.ld
>>>> +#objdump: -d
>>>> +
>>>> +.*: +file format .*
>>>> +
>>>> +
>>>> +#...
>>>> +   0:    58001085     beq             \$a0, \$a1, 16    # 10 <fun1>
>>>> +   4:    40000c80     beqz            \$a0, 12    # 10 <fun1>
>>>> +   8:    54000800     bl              8    # 10 <fun1>
>>>> +#pass
>>>> diff --git a/ld/testsuite/ld-loongarch-elf/provide_noabs.ld 
>>>> b/ld/testsuite/ld-loongarch-elf/provide_noabs.ld
>>>> new file mode 100644
>>>> index 00000000000..0154c6f3a29
>>>> --- /dev/null
>>>> +++ b/ld/testsuite/ld-loongarch-elf/provide_noabs.ld
>>>> @@ -0,0 +1,7 @@
>>>> +SECTIONS
>>>> +{
>>>> +  .text :
>>>> +  {
>>>> +    PROVIDE(fun1 = 0x10);
>>>> +  }
>>>> +}
>>>> diff --git a/ld/testsuite/ld-loongarch-elf/provide_sym.s 
>>>> b/ld/testsuite/ld-loongarch-elf/provide_sym.s
>>>> new file mode 100644
>>>> index 00000000000..6610894edbe
>>>> --- /dev/null
>>>> +++ b/ld/testsuite/ld-loongarch-elf/provide_sym.s
>>>> @@ -0,0 +1,7 @@
>>>> +    .text
>>>> +    .globl    main
>>>> +    .type    main, @function
>>>> +main:
>>>> +    beq    $a0,$a1,%b16(fun1)
>>>> +    beqz    $a0,%b21(fun1)
>>>> +    bl    %b26(fun1)
>> By the way, could abs symbol be got without relocate by dynamic 
>> linker? E.g, la.global a0, abs_sym. This is now disabled in the 
>> loongarch linker.

I'm not sure if you mean la.abs $a0, abs_sym?



More information about the Binutils mailing list