[PATCH 2/3] LoongArch: Allow R_LARCH_PCALA_HI20 or R_LARCH_PCREL20_S2 against undefined weak symbols for static PIE

Lulu Cai cailulu@loongson.cn
Wed Dec 18 08:02:45 GMT 2024


On 12/13/24 1:53 PM, Xi Ruoyao wrote:
> In a static PIE, undefined weak symbols should be just resolved to
> runtime address 0, like those symbols with non-default visibility.  This
> was silently broken in all prior Binutils releases with "-static-pie
> -mdirect-extern-access":
>
>      $ cat t.c
>      int x (void) __attribute__ ((weak));
>
>      int
>      main (void)
>      {
>        __builtin_printf("%p\n", x);
>      }
>      $ gcc t.c -static-pie -mdirect-extern-access
>      $ ./a.out
>      0x7ffff1d64000
>
> Since commit 599df6e2db17d1c4 ("ld, LoongArch: print error about
> linking without -fPIC or -fPIE flag in more detail"), the situation has
> been improved: the linker errors out instead of silently producing a
> wrong output file.

This commit was refined while the original changes were made in commit 
4cb77761d68791.

> But logically, using -mdirect-extern-access for a static PIE perfectly
> makes sense, and we should not prevent that even if the programmer uses
> weak symbols.  Linux kernel is such an example, and Linux < 6.10 now
> fails to build with Binutils trunk.  (The silent breakage with prior
> Binutils releases was "benign" due to some blind luck.)
>
> While since the 6.10 release Linux has removed those potentially
> undefined weak symbols (due to performance issue), we still should
> support weak symbols in -mdirect-extern-access -static-pie and unbreak
> building old kernels.
>
> Link: https://lore.kernel.org/loongarch/20241206085810.112341-1-chenhuacai@loongson.cn/
> Signed-off-by: Xi Ruoyao <xry111@xry111.site>
> ---
>   bfd/elfnn-loongarch.c                         | 27 ++++++++++++-------
>   .../ld-loongarch-elf/ld-loongarch-elf.exp     |  1 +
>   .../ld-loongarch-elf/weak-undef-static-pie.d  | 13 +++++++++
>   .../ld-loongarch-elf/weak-undef-static-pie.s  |  8 ++++++
>   4 files changed, 40 insertions(+), 9 deletions(-)
>   create mode 100644 ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/weak-undef-static-pie.s



More information about the Binutils mailing list