[PATCH v1] ld, LoongArch: print error about linking without -fPIC or -fPIE flag in more detail

Lulu Cai cailulu@loongson.cn
Fri Nov 8 01:11:03 GMT 2024


Thanks, I think the revision is OK, and I'll push it later if no one 
else comments.

On 11/1/24 7:41 PM, Xin Wang wrote:
> ---
>   bfd/elfnn-loongarch.c                         | 36 +++++++++++++------
>   .../bad_pcala_hi20_global_pie.d               |  4 +++
>   .../bad_pcala_hi20_global_pie.s               |  3 ++
>   .../bad_pcala_hi20_weak_pie.d                 |  4 +++
>   .../bad_pcala_hi20_weak_pie.s                 |  3 ++
>   .../ld-loongarch-elf/ld-loongarch-elf.exp     |  2 ++
>   6 files changed, 42 insertions(+), 10 deletions(-)
>   create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.s
>   create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.s
>
> diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
> index cc60eb6cacb..608d179c168 100644
> --- a/bfd/elfnn-loongarch.c
> +++ b/bfd/elfnn-loongarch.c
> @@ -856,13 +856,15 @@ loongarch_tls_transition (bfd *input_bfd,
>   }
>   
>   static bool
> -bad_static_reloc (bfd *abfd, const Elf_Internal_Rela *rel, asection *sec,
> -		  unsigned r_type, struct elf_link_hash_entry *h,
> +bad_static_reloc (struct bfd_link_info *info,
> +		  bfd *abfd, const Elf_Internal_Rela *rel,
> +		  asection *sec, unsigned r_type,
> +		  struct elf_link_hash_entry *h,
>   		  Elf_Internal_Sym *isym)
>   {
> -  /* We propably can improve the information to tell users that they should
> -     be recompile the code with -fPIC or -fPIE, just like what x86 does.  */
>     reloc_howto_type * r = loongarch_elf_rtype_to_howto (abfd, r_type);
> +  const char *object;
> +  const char *pic;
>     const char *name = NULL;
>   
>     if (h)
> @@ -874,10 +876,24 @@ bad_static_reloc (bfd *abfd, const Elf_Internal_Rela *rel, asection *sec,
>     if (name == NULL || *name == '\0')
>       name ="<nameless>";
>   
> +  if (bfd_link_dll (info))
> +    {
> +      object = _("a shared object");
> +      pic = _("; recompile with -fPIC");
> +    }
> +  else
> +    {
> +      if (bfd_link_pie (info))
> +	object = _("a PIE object");
> +      else
> +	object = _("a PDE object");
> +      pic = _("; recompile with -fPIE");
> +    }
> +
>     (*_bfd_error_handler)
>      (_("%pB:(%pA+%#lx): relocation %s against `%s` can not be used when making "
> -      "a shared object; recompile with -fPIC"),
> -    abfd, sec, (long) rel->r_offset, r ? r->name : _("<unknown>"), name);
> +      "%s%s"),
> +    abfd, sec, (long) rel->r_offset, r ? r->name : _("<unknown>"), name, object, pic);
>     bfd_set_error (bfd_error_bad_value);
>     return false;
>   }
> @@ -1047,7 +1063,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>   	case R_LARCH_TLS_LE_HI20_R:
>   	case R_LARCH_SOP_PUSH_TLS_TPREL:
>   	  if (!bfd_link_executable (info))
> -	    return bad_static_reloc (abfd, rel, sec, r_type, h, isym);
> +	    return bad_static_reloc (info, abfd, rel, sec, r_type, h, isym);
>   
>   	  if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
>   							   r_symndx,
> @@ -1065,7 +1081,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>   
>   	case R_LARCH_ABS_HI20:
>   	  if (bfd_link_pic (info))
> -	    return bad_static_reloc (abfd, rel, sec, r_type, h, isym);
> +	    return bad_static_reloc (info, abfd, rel, sec, r_type, h, isym);
>   
>   	  /* Fall through.  */
>   	case R_LARCH_SOP_PUSH_ABSOLUTE:
> @@ -1087,7 +1103,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>   	      && (sec->flags & SEC_ALLOC) != 0
>   	      && (sec->flags & SEC_READONLY) != 0
>   	      && ! LARCH_REF_LOCAL (info, h))
> -	    return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
> +	    return bad_static_reloc (info, abfd, rel, sec, r_type, h, NULL);
>   
>   	  break;
>   
> @@ -1115,7 +1131,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>   	      && (sec->flags & SEC_ALLOC) != 0
>   	      && (sec->flags & SEC_READONLY) != 0
>   	      && ! LARCH_REF_LOCAL (info, h))
> -	    return bad_static_reloc (abfd, rel, sec, r_type, h, NULL);
> +	    return bad_static_reloc (info, abfd, rel, sec, r_type, h, NULL);
>   
>   	  break;
>   
> diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d
> new file mode 100644
> index 00000000000..5dc2f7c4e4b
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.d
> @@ -0,0 +1,4 @@
> +#name: PC-relative relocation making executable
> +#source: bad_pcala_hi20_global_pie.s
> +#ld: -pie -z undefs --defsym _start=0
> +#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -fPIE
> diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.s
> new file mode 100644
> index 00000000000..f07bff98496
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_global_pie.s
> @@ -0,0 +1,3 @@
> +    .global sym
> +main:
> +    la.pcrel $a0, sym
> diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d
> new file mode 100644
> index 00000000000..5515f257191
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.d
> @@ -0,0 +1,4 @@
> +#name: PC-relative relocation making executable
> +#source: bad_pcala_hi20_weak_pie.s
> +#ld: -pie --defsym _start=0
> +#error: .*: relocation R_LARCH_PCALA_HI20 against `sym` can not be used when making a PIE object; recompile with -fPIE
> diff --git a/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.s b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.s
> new file mode 100644
> index 00000000000..43abe59ac35
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/bad_pcala_hi20_weak_pie.s
> @@ -0,0 +1,3 @@
> +    .weak sym
> +main:
> +    la.pcrel $a0, sym
> diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> index 4e0068d9f89..3313f72eee4 100644
> --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> @@ -168,6 +168,8 @@ if [istarget "loongarch64-*-*"] {
>       run_dump_test "abssym_shared"
>       run_dump_test "bad_pcala_hi20_global"
>       run_dump_test "bad_pcala_hi20_weak"
> +    run_dump_test "bad_pcala_hi20_global_pie"
> +    run_dump_test "bad_pcala_hi20_weak_pie"
>       run_dump_test "bad_pcrel20_s2_global"
>       run_dump_test "bad_pcrel20_s2_weak"
>     }




More information about the Binutils mailing list