[PATCH] RISC-V: Implement GOT load relaxation (--relax-got)

Jiawei jiawei@iscas.ac.cn
Thu Nov 26 11:16:06 GMT 2026


On 2026/11/26 17:05, Luke Zhuang wrote:
> Implement GOT load relaxation as described in the RISC-V psABI:
> https://riscv-non-isa.github.io/riscv-elf-psabi-doc/#_got_load_relaxation
>
> When --relax-got is enabled, the linker converts GOT indirect loads
> (auipc+lw/ld via GOT) into more efficient sequences:
>
>    - PC-relative (PCREL): auipc+lw/ld -> auipc+addi when the symbol is
>      relative, bound at link time to the output object, and the
>      PC-relative offset is in range.
>    - Absolute (ABS): auipc+lw/ld -> addi rd,x0,imm (with auipc deleted)
>      when the symbol is absolute and its address fits in a 12-bit
>      signed immediate.  Use c.li when the symbol's address fits in a
>      6-bit signed immediate and RVC instructions are permitted.
>
> The optimization is gated behind the --relax-got linker flag (off by
> default) and respects R_RISCV_RELAX markers.
> Preemptible symbols and IFUNC symbols are left unchanged.  Undefined
> weak symbols are relaxed only when known to be zero.
>
> This is especially useful when .o files compiled with -fPIC are linked
> into an executable and reference each other's variables, for example:
>
>    a.c:
>      int a = 3;
>      extern int b;
>
>      int foo_a (void)
>      {
>        return b;
>      }
>
>    b.c:
>      int b = 4;
>      extern int a;
>
>      int foo_b (void)
>      {
>        return a;
>      }
>
> The definitions of a and b bind to the executable, allowing both GOT
> loads to be relaxed.  This also applies to PIE executables.
>
> Assisted (mainly test coverage) by: Codex
>
> bfd/
> 	* elfnn-riscv.c (perform_relocation): Handle R_RISCV_GOT_TO_PCREL_HI
> 	and R_RISCV_GOT_TO_RVC_LI.
> 	(riscv_resolve_pcrel_lo_relocs): Check GOT LO addends after
> 	relaxation.
> 	(riscv_elf_relocate_section): Handle GOT relaxation relocations.
> 	Preserve GOT initialization and convert internal relocations to
> 	public types.
> 	(enum riscv_got_relax_type): New enum.
> 	(struct riscv_pcgp_hi_reloc): Add got_relaxed field.
> 	(riscv_record_pcgp_hi_reloc): Initialize got_relaxed.
> 	(riscv_got_address_near_zero_p): New function.
> 	(_bfd_riscv_relax_got_load): Likewise.
> 	(_bfd_riscv_relax_pc): Rewrite paired GOT loads to ADDI or C.LI.
> 	(riscv_relax_select_shorten): Dispatch GOT_HI20 to GOT relaxation
> 	and allow paired LO relaxation in PIC output.
> 	(_bfd_riscv_relax_section): Restrict GOT undefined weak relaxation
> 	to symbols known to be zero.  Defer GOT relaxation until JVT
> 	layout is complete.
> 	* elfxx-riscv.c (howto_table_internal): Add R_RISCV_GOT_TO_PCREL_HI,
> 	R_RISCV_GOT_TO_ABS_LO and R_RISCV_GOT_TO_RVC_LI.
> 	* elfxx-riscv.h (struct riscv_elf_params): Add relax_got field.
>
> include/
> 	* elf/riscv.h (R_RISCV_GOT_TO_PCREL_HI, R_RISCV_GOT_TO_ABS_LO)
> 	(R_RISCV_GOT_TO_RVC_LI): Define.
>
> ld/
> 	* NEWS: Mention GOT load relaxation.
> 	* emultempl/riscvelf.em: Add --relax-got/--no-relax-got options.
> 	* ld.texi: Document them.
> 	* ldlex.h (enum option_values): Add OPTION_RELAX_GOT and
> 	OPTION_NO_RELAX_GOT.
> 	* testsuite/ld-riscv-elf/got-relax-abs-range-rv32.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-abs-range.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-abs-range.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-c-li-rv32.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-c-li.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-c-li.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-dso-version.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-dso-version.ver: New version script.
> 	* testsuite/ld-riscv-elf/got-relax-dso.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-dynamic-lib.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-lo-addend-rvc.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-lo-addend.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-lo-addend.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-nopie.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-object.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-options-norelax.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-options.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-options.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-pcrel-range.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-pcrel-range.ld: New linker script.
> 	* testsuite/ld-riscv-elf/got-relax-pcrel-range.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-pie.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-relocs-rv32.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-relocs-rvc-rv32.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-relocs-rvc.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-relocs.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-relocs.s: New test input.
> 	* testsuite/ld-riscv-elf/got-relax-static.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-zero-page-rv32.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax-zero-page.d: New test.
> 	* testsuite/ld-riscv-elf/got-relax.s: New test input.
> 	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run GOT relaxation tests.
> ---
>   bfd/elfnn-riscv.c                             | 247 +++++++++++++++++-
>   bfd/elfxx-riscv.c                             |  43 +++
>   bfd/elfxx-riscv.h                             |   2 +
>   include/elf/riscv.h                           |   3 +
>   ld/NEWS                                       |   3 +
>   ld/emultempl/riscvelf.em                      |  13 +
>   ld/ld.texi                                    |  20 ++
>   ld/ldlex.h                                    |   2 +
>   .../ld-riscv-elf/got-relax-abs-range-rv32.d   |   6 +
>   .../ld-riscv-elf/got-relax-abs-range.d        |  28 ++
>   .../ld-riscv-elf/got-relax-abs-range.s        |  47 ++++
>   .../ld-riscv-elf/got-relax-c-li-rv32.d        |   6 +
>   ld/testsuite/ld-riscv-elf/got-relax-c-li.d    |  32 +++
>   ld/testsuite/ld-riscv-elf/got-relax-c-li.s    |  50 ++++
>   .../ld-riscv-elf/got-relax-dso-version.d      |  21 ++
>   .../ld-riscv-elf/got-relax-dso-version.ver    |   1 +
>   ld/testsuite/ld-riscv-elf/got-relax-dso.d     |  67 +++++
>   .../ld-riscv-elf/got-relax-dynamic-lib.s      |   6 +
>   .../ld-riscv-elf/got-relax-lo-addend-rvc.d    |   6 +
>   .../ld-riscv-elf/got-relax-lo-addend.d        |   5 +
>   .../ld-riscv-elf/got-relax-lo-addend.s        |  23 ++
>   ld/testsuite/ld-riscv-elf/got-relax-nopie.d   |  65 +++++
>   ld/testsuite/ld-riscv-elf/got-relax-object.s  |   6 +
>   .../ld-riscv-elf/got-relax-options-norelax.d  |  24 ++
>   ld/testsuite/ld-riscv-elf/got-relax-options.d |  24 ++
>   ld/testsuite/ld-riscv-elf/got-relax-options.s |  23 ++
>   .../ld-riscv-elf/got-relax-pcrel-range.d      |  28 ++
>   .../ld-riscv-elf/got-relax-pcrel-range.ld     |  24 ++
>   .../ld-riscv-elf/got-relax-pcrel-range.s      |  11 +
>   ld/testsuite/ld-riscv-elf/got-relax-pie.d     |  67 +++++
>   .../ld-riscv-elf/got-relax-relocs-rv32.d      |  40 +++
>   .../ld-riscv-elf/got-relax-relocs-rvc-rv32.d  |   6 +
>   .../ld-riscv-elf/got-relax-relocs-rvc.d       |  29 ++
>   ld/testsuite/ld-riscv-elf/got-relax-relocs.d  |  41 +++
>   ld/testsuite/ld-riscv-elf/got-relax-relocs.s  |  26 ++
>   ld/testsuite/ld-riscv-elf/got-relax-static.d  |  27 ++
>   .../ld-riscv-elf/got-relax-zero-page-rv32.d   |   6 +
>   .../ld-riscv-elf/got-relax-zero-page.d        |  27 ++
>   ld/testsuite/ld-riscv-elf/got-relax.s         | 150 +++++++++++
>   ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp    |  69 +++++
>   40 files changed, 1316 insertions(+), 8 deletions(-)
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-abs-range-rv32.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-abs-range.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-abs-range.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-c-li-rv32.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-c-li.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-c-li.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-dso-version.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-dso-version.ver
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-dso.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-dynamic-lib.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-lo-addend-rvc.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-lo-addend.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-lo-addend.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-nopie.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-object.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-options-norelax.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-options.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-options.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.ld
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-pie.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-relocs-rv32.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc-rv32.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-relocs.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-relocs.s
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-static.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-zero-page-rv32.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax-zero-page.d
>   create mode 100644 ld/testsuite/ld-riscv-elf/got-relax.s
>
> diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
> index 4c4ddbb78f7..f67f19b56ab 100644
> --- a/bfd/elfnn-riscv.c
> +++ b/bfd/elfnn-riscv.c
> @@ -2981,6 +2981,7 @@ perform_relocation (const reloc_howto_type *howto,
>       case R_RISCV_TLSDESC_HI20:
>       case R_RISCV_TLSDESC_LE_HI:
>       case R_RISCV_TLSDESC_IE_HI:
> +    case R_RISCV_GOT_TO_PCREL_HI:
>         if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
>   	return bfd_reloc_overflow;
>         value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
> @@ -3038,6 +3039,14 @@ perform_relocation (const reloc_howto_type *howto,
>         value = ENCODE_CJTYPE_IMM (value);
>         break;
>   
> +    case R_RISCV_GOT_TO_RVC_LI:
> +      if (ARCH_SIZE == 32)
> +	value = (bfd_signed_vma) (int32_t) value;
> +      if (!VALID_CITYPE_IMM (value))
> +	return bfd_reloc_overflow;
> +      value = ENCODE_CITYPE_IMM (value);
> +      break;
> +
>       case R_RISCV_RVC_LUI:
>         if (RISCV_CONST_HIGH_PART (value) == 0)
>   	{
> @@ -3457,7 +3466,8 @@ riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
>         char *string = NULL;
>         if (entry == NULL)
>   	string = _("%pcrel_lo missing matching %pcrel_hi");
> -      else if (entry->type == R_RISCV_GOT_HI20
> +      else if ((entry->type == R_RISCV_GOT_HI20
> +		|| entry->type == R_RISCV_GOT_TO_PCREL_HI)
>   	       && r->reloc->r_addend != 0)
>   	string = _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
>         else if (RISCV_CONST_HIGH_PART (entry->value)
> @@ -3981,6 +3991,9 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
>   	    }
>   	  break;
>   
> +	case R_RISCV_GOT_TO_PCREL_HI:
> +	case R_RISCV_GOT_TO_ABS_LO:
> +	case R_RISCV_GOT_TO_RVC_LI:
>   	case R_RISCV_GOT_HI20:
>   	  if (h != NULL)
>   	    {
> @@ -4057,6 +4070,36 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
>   	      riscv_elf_append_rela (info->output_bfd, s, &outrel);
>   	    }
>   
> +	  if (r_type == R_RISCV_GOT_TO_RVC_LI)
> +	    {
> +	      r = perform_relocation (howto, rel, relocation, input_section,
> +				      input_bfd, contents);
> +	      rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
> +	      rel->r_addend = 0;
> +	      if (r == bfd_reloc_ok)
> +		continue;
> +	      break;
> +	    }
> +
> +	  if (r_type == R_RISCV_GOT_TO_ABS_LO)
> +	    {
> +	      /* The temporary low-part relocation is needed because
> +		 otherwise we may lose the required GOT initialization step
> +		 if the related high parts are all deleted.  */
> +	      rel->r_info = ELFNN_R_INFO (r_symndx, R_RISCV_LO12_I);
> +	      howto = riscv_elf_rtype_to_howto (input_bfd, R_RISCV_LO12_I);
> +	      /* Keep the GOT LO addend check after deleting the HI.  */
> +	      if (rel->r_addend != 0)
> +		{
> +		  info->callbacks->reloc_dangerous
> +		    (info, _("%pcrel_lo with addend isn't allowed for "
> +			     "R_RISCV_GOT_HI20"),
> +		     input_bfd, input_section, rel->r_offset);
> +		  continue;
> +		}
> +	      break;
> +	    }
> +
>   	  if (rel->r_addend != 0)
>   	    {
>   	      msg = _("The addend isn't allowed for R_RISCV_GOT_HI20");
> @@ -4064,8 +4107,14 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
>   	    }
>   	  else
>   	    {
> -	      /* Address of got entry.  */
> -	      relocation = sec_addr (htab->elf.sgot) + off;
> +	      /* GOT_HI20 and GOT_TO_PCREL_HI reach here (GOT_TO_ABS_LO
> +		 exits above).  GOT_TO_PCREL_HI already has the final symbol
> +		 address, so there is no need to change the relocation
> +		 value.  */
> +	      if (r_type == R_RISCV_GOT_HI20)
> +		relocation = sec_addr (htab->elf.sgot) + off;
> +	      else
> +		rel->r_info = ELFNN_R_INFO (r_symndx, R_RISCV_PCREL_HI20);
>   	      absolute = riscv_zero_pcrel_hi_reloc (rel, info, pc,
>   						    &relocation, contents,
>   						    howto);
> @@ -5170,6 +5219,13 @@ riscv_elf_obj_attrs_handle_unknown (bfd *abfd, int tag)
>     return true;
>   }
>   
> +enum riscv_got_relax_type
> +{
> +  GOT_RELAXED_NONE = 0,
> +  GOT_RELAXED_PCREL,
> +  GOT_RELAXED_ABS
> +};
> +
>   /* A second format for recording PC-relative hi relocations.  This stores the
>      information required to relax them to GP-relative addresses.  */
>   
> @@ -5183,6 +5239,7 @@ struct riscv_pcgp_hi_reloc
>     asection *sym_sec;
>     struct elf_link_hash_entry *h;
>     bool undefined_weak;
> +  enum riscv_got_relax_type got_relaxed;
>     riscv_pcgp_hi_reloc *next;
>   };
>   
> @@ -5255,6 +5312,7 @@ riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off,
>     new->sym_sec = sym_sec;
>     new->h = h;
>     new->undefined_weak = undefined_weak;
> +  new->got_relaxed = GOT_RELAXED_NONE;
>     new->next = p->hi;
>     p->hi = new;
>     return true;
> @@ -6601,10 +6659,119 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec,
>   				   NULL, NULL, false);
>   }
>   
> +static bool
> +riscv_got_address_near_zero_p (bfd_vma address)
> +{
> +  /* Sign-extend RV32 addresses before checking the signed 12-bit range:
> +     0xffffffff represents -1 and can be relaxed to "addi rd, zero, -1".
> +     Otherwise it may be extended to 0x00000000ffffffff (4294967295).  */
> +  if (ARCH_SIZE == 32)
> +    address = (bfd_signed_vma) (int32_t) address;
> +  return VALID_ITYPE_IMM (address);
> +}
> +
> +/* Relax GOT load: auipc+lw/ld -> auipc+addi (or li/c.li).  Record the high
> +   part here and rewrite its low parts in _bfd_riscv_relax_pc.
> +
> +   TODO: Remove GOT entries that are no longer used after relaxation.  */
> +
> +static bool
> +_bfd_riscv_relax_got_load (bfd *abfd, asection *sec, asection *sym_sec,
> +			 struct bfd_link_info *info,
> +			 struct elf_link_hash_entry *h,
> +			 Elf_Internal_Rela *rel, bfd_vma symval,
> +			 bfd_vma max_alignment,
> +			 bfd_vma reserve_size ATTRIBUTE_UNUSED,
> +			 bool *again, riscv_pcgp_relocs *p,
> +			 bool undefined_weak)
> +{
> +  /* Do these checks that may stop relaxation:
> +     1. The symbol must be bound at link time to be within the object
> +	(including bounded known address at link time), unless it is an
> +	undefined weak symbol known to be zero.
> +     2. IFUNC addresses are chosen at run time.
> +     3. GOT_HI20 must have a zero addend.
> +     4. Check section boundaries.  */
> +  if ((!undefined_weak && !SYMBOL_REFERENCES_LOCAL (info, h))
> +      || (h != NULL && h->type == STT_GNU_IFUNC)
> +      || rel->r_addend != 0
> +      || rel->r_offset > sec->size || sec->size - rel->r_offset < 4)
> +    return true;
> +
> +  /* Check whether it's an absolute symbol (relaxed to li/c.li) or a relative
> +     symbol (pcrel).
> +     Here "absolute symbol" also includes undefined weak symbols known to
> +     be zero and normal section symbols near zero in non-PIC output.
> +     Undefined weak symbols that may be resolved at run time are excluded
> +     using UNDEFWEAK_NO_DYNAMIC_RELOC in _bfd_riscv_relax_section.  */
> +  bool absolute = (undefined_weak || bfd_is_abs_section (sym_sec)
> +		   || (!bfd_link_pic (info)
> +		       && riscv_got_address_near_zero_p (symval)));

The last condition uses the current address of a section-defined symbol, 
but deleting this 'auipc' can move that very symbol out of range.
Could we drop the '!bfd_link_pic (info)' near-zero case and limit this 
form to absolute symbols with stable values and undefined weak symbols 
known to be zero?
I think here also need some boundary tests.

> +
> +  if (absolute)
> +    {
> +      /* Absolute symbol.  */
> +      /* Need to make sure the address fits in a signed 12-bit immediate.  */
> +      /* TODO: Try pcrel instead then for non-PIC output.  */
> +      if (!riscv_got_address_near_zero_p (symval))
> +	return true;
> +    }
> +  else
> +    {
> +      /* Relative symbol.  */
> +      if (ARCH_SIZE > 32)
> +	{
> +	  /* On RV64, the target symbol may not be reached by auipc+addi,
> +	     so we need to estimate the range here.  */
> +	  bfd_vma offset = symval - (sec_addr (sec) + rel->r_offset);
> +	  /* Leave room for alignment changes in the future.  */
> +	  if (!VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset))
> +	      || !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART
> +				   (offset + max_alignment))
> +	      || !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART
> +				   (offset - max_alignment)))
> +	    return true;
> +	}
> +    }

Earlier instructions in this section can still shrink while the target 
stays at a fixed address.


> +
> +  /* Safety checks for instruction encoding.  */
> +  unsigned int auipc
> +    = bfd_getl32 (elf_section_data (sec)->this_hdr.contents + rel->r_offset);
> +  unsigned int rd = (auipc >> OP_SH_RD) & OP_MASK_RD;
> +  if ((auipc & MASK_AUIPC) != MATCH_AUIPC || rd == 0)
> +    return true;
> +
> +  /* If the corresponding lo relocation has already been seen then it's not
> +     safe to relax this relocation.  */
> +  if (riscv_find_pcgp_lo_reloc (p, rel->r_offset))
> +    return true;
> +
> +  /* Only relaxable GOT_HI20s are recorded; unrecorded ones leave the
> +     paired LO untouched (no regular GP relaxation either).  */
> +  if (!riscv_record_pcgp_hi_reloc (p, rel->r_offset, rel->r_addend,
> +				 symval, ELFNN_R_SYM (rel->r_info),
> +				 sym_sec, h, undefined_weak))
> +    return false;
> +  p->hi->got_relaxed = absolute ? GOT_RELAXED_ABS : GOT_RELAXED_PCREL;
> +
> +  *again = true;
> +
> +  /* ABS: Delete auipc entirely; rewrite the LO to
> +     `addi rd, x0, imm` later.  */
> +  if (absolute)
> +    return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, info,
> +				    p, rel, false);
> +
> +  /* PCREL: rewrite GOT_HI20 -> GOT_TO_PCREL_HI.  */
> +  rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info),
> +			    R_RISCV_GOT_TO_PCREL_HI);

Could we defer both changes until all LO relocations referring to this 
HI have been checked? I tested a marked GOT HI shared by two 'ld' 
instructions, with the second 'ld' under .option norelax. With 
'--no-relax-got', both loads returned '&v'.
With '--relax-got', the second load returned the contents of 'v', its 
instruction was preserved, but the HI it uses had already changed.

Please collect the LOs by input section and HI offset, check the 
'R_RISCV_RELAX' marker on every member, and only then rewrite the group. 
A shared HI with one marked and one unmarked LO would make a good 
regression test.

See RISC-V psABI §11.1 and §11.1.5: 
https://riscv-non-isa.github.io/riscv-elf-psabi-doc/#_linker_relaxation_types

> +  return true;
> +}
> +
>   /* Relax PC-relative references to GP-relative references.  */
>   
>   static bool
> -_bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
> +_bfd_riscv_relax_pc (bfd *abfd,
>   		     asection *sec,
>   		     asection *sym_sec,
>   		     struct bfd_link_info *link_info,
> @@ -6651,6 +6818,57 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
>   	  }
>   
>   	hi_reloc = *hi;
> +
> +	/* If the paired HI was GOT-relaxed, rewrite lw/ld -> addi here.  */
> +	if (hi_reloc.got_relaxed != GOT_RELAXED_NONE)
> +	  {
> +	    bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
> +	    unsigned int insn = bfd_getl32 (contents + rel->r_offset);
> +	    unsigned int rd = insn & (OP_MASK_RD << OP_SH_RD);
> +	    unsigned int rs1 = insn & (OP_MASK_RS1 << OP_SH_RS1);

I think we missing checking the origin instruction here. For example, 
'addi a1,t0,%pcrel_lo(.Lhi)' followed by 'ld a0,0(a1)' computes the GOT 
slot address and then reads the slot. With this patch, the final ld 
reads the variable itself.

So please add the LO relocation type check here, 
'loongarch_relax_pcala_ld' is a close BFD example, see: 
https://github.com/RTEMS/sourceware-mirror-binutils-gdb/blob/1b92b50d1ca485b090c841b8bc755a6ba6f159ee/bfd/elfnn-loongarch.c#L5919-L5927

> +
> +	    /* Rewrite lw/ld -> addi.
> +	       ABS: addi rd, x0, imm.
> +	       PCREL: addi rd, rs1, imm (rs1 = auipc rd).  */
> +	    if (hi_reloc.got_relaxed == GOT_RELAXED_ABS)
> +	      {
> +		insn = MATCH_ADDI | rd;
> +		rel->r_info = ELFNN_R_INFO (hi_reloc.hi_sym,
> +					  R_RISCV_GOT_TO_ABS_LO);
> +		rel->r_addend += hi_reloc.hi_addend;
> +
> +		bfd_vma value = hi_reloc.hi_addr;
> +		if (ARCH_SIZE == 32)
> +		  value = (bfd_signed_vma) (int32_t) value;
> +		/* Compress absolute values and weak symbols known to be 0.  */
> +		int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
> +		if (use_rvc
> +		    && rd != 0 && rel->r_addend == 0
> +		    && (hi_reloc.undefined_weak
> +			|| bfd_is_abs_section (hi_reloc.sym_sec))
> +		    && VALID_CITYPE_IMM (value))
> +		  {
> +		    bfd_putl32 (MATCH_C_LI | rd, contents + rel->r_offset);
> +		    rel->r_info = ELFNN_R_INFO (hi_reloc.hi_sym,
> +					      R_RISCV_GOT_TO_RVC_LI);
> +		    *again = true;
> +		    return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2,
> +					    2, link_info, pcgp_relocs,
> +					    rel + 1, false);
> +		  }
> +	      }
> +	    else
> +	      insn = MATCH_ADDI | rd | rs1;
> +
> +	    bfd_putl32 (insn, contents + rel->r_offset);
> +	    *again = true;
> +	    return true;
> +	  }
> +
> +	/* Only GOT low parts are relaxed in PIC output.  */
> +	if (bfd_link_pic (link_info))
> +	  return true;
> +
>   	symval = hi_reloc.hi_addr;
>   	sym_sec = hi_reloc.sym_sec;
>   
> @@ -6843,13 +7061,18 @@ riscv_relax_select_shorten (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
>   	       || type == R_RISCV_TLSDESC_ADD_LO12
>   	       || type == R_RISCV_TLSDESC_CALL))
>       f = _bfd_riscv_relax_tlsdesc;
> -  else if (!bfd_link_pic (info)
> -	   && (type == R_RISCV_PCREL_HI20
> -	       || type == R_RISCV_PCREL_LO12_I
> +  else if (!bfd_link_pic (info) && type == R_RISCV_PCREL_HI20)
> +    f = _bfd_riscv_relax_pc;
> +  else if ((!bfd_link_pic (info)
> +	    || riscv_elf_hash_table (info)->params->relax_got)
> +	   && (type == R_RISCV_PCREL_LO12_I
>   	       || type == R_RISCV_PCREL_LO12_S))
>       f = _bfd_riscv_relax_pc;
>     else if (type == R_RISCV_JAL)
>       f = _bfd_riscv_relax_jal;
> +  else if (type == R_RISCV_GOT_HI20
> +	   && riscv_elf_hash_table (info)->params->relax_got)
> +    f = _bfd_riscv_relax_got_load;
>     else
>       return true;
>   
> @@ -7210,7 +7433,10 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec,
>   		 for now, but will be guaranteed to be defined later.  */
>   	      && !h->root.linker_def
>   	      && (relax_func == _bfd_riscv_relax_lui
> -		  || relax_func == _bfd_riscv_relax_pc))
> +		  || relax_func == _bfd_riscv_relax_pc
> +		  || (relax_func == _bfd_riscv_relax_got_load
> +		      && (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)
> +			  || !htab->elf.dynamic_sections_created))))
>   	    {
>   	      /* For the lui and auipc relaxations, since the symbol
>   		 value of an undefined weak symbol is always be zero,
> @@ -7225,6 +7451,10 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec,
>   		 libraries can not happen currently.  Once we support the
>   		 auipc relaxations when creating shared libraries, then we will
>   		 need the more rigorous checking for this optimization.  */
> +	      /* For got load relaxations, undefined weak symbols could be
> +		 resolved by an external DSO (if with default visibility) at
> +		 runtime, so we have to rule this out using
> +		 UNDEFWEAK_NO_DYNAMIC_RELOC.  */
>   	      undefined_weak = true;
>   	    }
>   
> @@ -7307,6 +7537,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec,
>   	  && (((relax_func == _bfd_riscv_relax_call
>   		|| relax_func == _bfd_riscv_relax_jal) && sec != sym_sec)
>   	      || relax_func == _bfd_riscv_relax_pc
> +	      || relax_func == _bfd_riscv_relax_got_load
>   	      || relax_func == _bfd_riscv_relax_lui
>   	      || relax_func == _bfd_riscv_relax_tls_le))
>   	{
> diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c
> index dfd25936ae3..943333582f8 100644
> --- a/bfd/elfxx-riscv.c
> +++ b/bfd/elfxx-riscv.c
> @@ -1019,6 +1019,49 @@ static const reloc_howto_type howto_table_internal[] =
>   	 0,				/* src_mask */
>   	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
>   	 false),			/* pcrel_offset */
> +
> +  /* GOT load relaxed to PC-relative addressing.  */
> +  HOWTO (R_RISCV_GOT_TO_PCREL_HI,		/* type */
> +	 0,				/* rightshift */
> +	 4,				/* size */
> +	 32,				/* bitsize */
> +	 true,				/* pc_relative */
> +	 0,				/* bitpos */
> +	 complain_overflow_signed,	/* complain_on_overflow */
> +	 bfd_elf_generic_reloc,		/* special_function */
> +	 "R_RISCV_GOT_TO_PCREL_HI",	/* name */
> +	 false,				/* partial_inplace */
> +	 0,				/* src_mask */
> +	 ENCODE_UTYPE_IMM (-1U),	/* dst_mask */
> +	 false),			/* pcrel_offset */
> +
> +  HOWTO (R_RISCV_GOT_TO_ABS_LO,		/* type */
> +	 0,				/* rightshift */
> +	 4,				/* size */
> +	 32,				/* bitsize */
> +	 false,				/* pc_relative */
> +	 0,				/* bitpos */
> +	 complain_overflow_signed,	/* complain_on_overflow */
> +	 bfd_elf_generic_reloc,		/* special_function */
> +	 "R_RISCV_GOT_TO_ABS_LO",	/* name */
> +	 false,				/* partial_inplace */
> +	 0,				/* src_mask */
> +	 ENCODE_ITYPE_IMM (-1U),	/* dst_mask */
> +	 false),			/* pcrel_offset */
> +
> +  HOWTO (R_RISCV_GOT_TO_RVC_LI,		/* type */
> +	 0,				/* rightshift */
> +	 2,				/* size */
> +	 16,				/* bitsize */
> +	 false,				/* pc_relative */
> +	 0,				/* bitpos */
> +	 complain_overflow_signed,	/* complain_on_overflow */
> +	 bfd_elf_generic_reloc,		/* special_function */
> +	 "R_RISCV_GOT_TO_RVC_LI",	/* name */
> +	 false,				/* partial_inplace */
> +	 0,				/* src_mask */
> +	 ENCODE_CITYPE_IMM (-1U),	/* dst_mask */
> +	 false),			/* pcrel_offset */
>   };
>   
>   /* A mapping from BFD reloc types to RISC-V ELF reloc types.  */
> diff --git a/bfd/elfxx-riscv.h b/bfd/elfxx-riscv.h
> index 56c2567083c..7d79e623cfa 100644
> --- a/bfd/elfxx-riscv.h
> +++ b/bfd/elfxx-riscv.h
> @@ -68,6 +68,8 @@ struct riscv_elf_params
>     bool relax_gp;
>     /* Whether to relax code sequences for Zcmt.  */
>     bool relax_zcmt;
> +  /* Whether to relax GOT loads to direct addressing.  */
> +  bool relax_got;
>     /* Whether to check if SUB_ULEB128 relocation has non-zero addend.  */
>     bool check_uleb128;
>     /* How to mark the output for Zicfilp and Zicfiss.  */
> diff --git a/include/elf/riscv.h b/include/elf/riscv.h
> index f6f1fcffc60..476e3c9d952 100644
> --- a/include/elf/riscv.h
> +++ b/include/elf/riscv.h
> @@ -110,6 +110,9 @@ END_RELOC_NUMBERS (R_RISCV_max)
>   #define R_RISCV_TLSDESC_IE_LO     (R_RISCV_max + 9)
>   #define R_RISCV_TLSDESC_LE_HI     (R_RISCV_max + 10)
>   #define R_RISCV_TLSDESC_LE_LO     (R_RISCV_max + 11)
> +#define R_RISCV_GOT_TO_PCREL_HI   (R_RISCV_max + 12)
> +#define R_RISCV_GOT_TO_ABS_LO     (R_RISCV_max + 13)
> +#define R_RISCV_GOT_TO_RVC_LI     (R_RISCV_max + 14)
>   
>   /* Processor specific flags for the ELF header e_flags field.  */
>   
> diff --git a/ld/NEWS b/ld/NEWS
> index 56c549b513d..2230c49d83b 100644
> --- a/ld/NEWS
> +++ b/ld/NEWS
> @@ -1,5 +1,8 @@
>   -*- text -*-
>   
> +* Add the RISC-V --relax-got and --no-relax-got options to control GOT load
> +  relaxation to direct addressing.  Disabled by default.
> +
>   * Add the RISC-V --relax-tbljal[=zcmt] and --no-relax-tbljal options for
>     opt-in Zcmt table-jump relaxation in non-position-independent executable
>     links.  An omitted scheme selects zcmt; unsupported schemes are errors.
> diff --git a/ld/emultempl/riscvelf.em b/ld/emultempl/riscvelf.em
> index 64ad34016f2..1562d381fdd 100644
> --- a/ld/emultempl/riscvelf.em
> +++ b/ld/emultempl/riscvelf.em
> @@ -26,6 +26,7 @@ fragment <<EOF
>   #include "elfxx-riscv.h"
>   
>   static struct riscv_elf_params params = { .relax_gp = 1,
> +					  .relax_got = false,
>   					  .relax_zcmt = false,
>   					  .check_uleb128 = 0,
>   					  .zicfilp = RISCV_ZICFILP_IMPLICIT,
> @@ -55,6 +56,8 @@ PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
>       { "no-relax-gp", no_argument, NULL, OPTION_NO_RELAX_GP },
>       { "relax-tbljal", optional_argument, NULL, OPTION_RELAX_TBLJAL },
>       { "no-relax-tbljal", no_argument, NULL, OPTION_NO_RELAX_TBLJAL },
> +    { "relax-got", no_argument, NULL, OPTION_RELAX_GOT },
> +    { "no-relax-got", no_argument, NULL, OPTION_NO_RELAX_GOT },
>       { "check-uleb128", no_argument, NULL, OPTION_CHECK_ULEB128 },
>       { "no-check-uleb128", no_argument, NULL, OPTION_NO_CHECK_ULEB128 },
>   '
> @@ -64,6 +67,8 @@ PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
>     fprintf (file, _("  --no-relax-gp               Don'\''t perform GP relaxation\n"));
>     fprintf (file, _("  --relax-tbljal[=zcmt]       Enable RISC-V table-jump relaxation (scheme: zcmt)\n"));
>     fprintf (file, _("  --no-relax-tbljal           Disable RISC-V table-jump relaxation (default)\n"));
> +  fprintf (file, _("  --relax-got                 Relax GOT loads to direct addressing\n"));
> +  fprintf (file, _("  --no-relax-got              Don'\''t relax GOT loads (default)\n"));
>     fprintf (file, _("  --check-uleb128             Check if SUB_ULEB128 has non-zero addend\n"));
>     fprintf (file, _("  --no-check-uleb128          Don'\''t check if SUB_ULEB128 has non-zero addend\n"));
>     fprintf (file, _("\
> @@ -144,6 +149,14 @@ PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
>         params.relax_zcmt = false;
>         break;
>   
> +    case OPTION_RELAX_GOT:
> +      params.relax_got = true;
> +      break;
> +
> +    case OPTION_NO_RELAX_GOT:
> +      params.relax_got = false;
> +      break;
> +
>       case OPTION_CHECK_ULEB128:
>         params.check_uleb128 = 1;
>         break;
> diff --git a/ld/ld.texi b/ld/ld.texi
> index 522aa403833..e38abd27552 100644
> --- a/ld/ld.texi
> +++ b/ld/ld.texi
> @@ -4214,6 +4214,26 @@ emitted.
>   @item --no-relax-tbljal
>   Disable RISC-V table-jump relaxation.  This is the default.
>   
> +@kindex --relax-got
> +@item --relax-got
> +Enable GOT load relaxation.  References to non-preemptible symbols can
> +load the address directly instead of loading it from the GOT.  The linker
> +replaces the load with @code{addi} and can remove the preceding
> +@code{auipc} for a link-time constant address that fits a signed 12-bit
> +immediate.
> +Both relocations must be marked with @code{R_RISCV_RELAX}.
> +
> +When RVC instructions are permitted, absolute addresses that fit a signed
> +6-bit immediate can use @code{c.li} instead of @code{addi}.
> +
> +This relaxation is disabled by default.  The last @samp{--relax-got} or
> +@samp{--no-relax-got} option takes effect; @samp{--no-relax} disables it
> +regardless of option order.
> +
> +@kindex --no-relax-got
> +@item --no-relax-got
> +Disable GOT load relaxation.  This is the default.
> +
>   @end table
>   
>   @c man end
> diff --git a/ld/ldlex.h b/ld/ldlex.h
> index 0a3d34acf9e..f9fbd9b5c7b 100644
> --- a/ld/ldlex.h
> +++ b/ld/ldlex.h
> @@ -433,6 +433,8 @@ enum option_values
>     OPTION_NO_CHECK_ULEB128,
>     OPTION_RELAX_TBLJAL,
>     OPTION_NO_RELAX_TBLJAL,
> +  OPTION_RELAX_GOT,
> +  OPTION_NO_RELAX_GOT,
>     /* Used by emultempl/rxelf.em.  */
>     OPTION_NO_FLAG_MISMATCH_WARNINGS,
>     OPTION_IGNORE_LMA,
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-abs-range-rv32.d b/ld/testsuite/ld-riscv-elf/got-relax-abs-range-rv32.d
> new file mode 100644
> index 00000000000..f96319e0599
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-abs-range-rv32.d
> @@ -0,0 +1,6 @@
> +#name: GOT relaxation boundaries RV32
> +#source: got-relax-abs-range.s
> +#as: -march=rv32i -mabi=ilp32 -mlittle-endian
> +#ld: -melf32lriscv --relax-got -Tdata=0x600 --defsym abs_min=-2048 --defsym abs_max=2047 --defsym abs_neg=-1 --defsym abs_far=2048 --defsym abs_below=-2049
> +#objdump: -d -j .text -Mno-aliases
> +#dump: got-relax-abs-range.d
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-abs-range.d b/ld/testsuite/ld-riscv-elf/got-relax-abs-range.d
> new file mode 100644
> index 00000000000..b38d77e25b2
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-abs-range.d
> @@ -0,0 +1,28 @@
> +#name: GOT relaxation boundaries RV64
> +#source: got-relax-abs-range.s
> +#as: -march=rv64i -mabi=lp64 -mlittle-endian --defsym __64_bit__=1
> +#ld: -melf64lriscv --relax-got -Tdata=0x600 --defsym abs_min=-2048 --defsym abs_max=2047 --defsym abs_neg=-1 --defsym abs_far=2048 --defsym abs_below=-2049
> +#objdump: -d -j .text -Mno-aliases
> +
> +.*: +file format .*
> +
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +.*:\s+[0-9a-f]+\s+auipc\s+a0,.*
> +.*:\s+[0-9a-f]+\s+addi\s+a0,a0,.*
> +.*:\s+[0-9a-f]+\s+addi\s+a1,zero,-2048
> +.*:\s+[0-9a-f]+\s+addi\s+a2,zero,2047
> +.*:\s+[0-9a-f]+\s+addi\s+a3,zero,-1
> +.*:\s+[0-9a-f]+\s+addi\s+a4,zero,0
> +.*:\s+[0-9a-f]+\s+auipc\s+a5,.*
> +.*:\s+[0-9a-f]+\s+l[wd]\s+a5,.*
> +.*:\s+[0-9a-f]+\s+auipc\s+a6,.*
> +.*:\s+[0-9a-f]+\s+l[wd]\s+a6,.*
> +.*:\s+[0-9a-f]+\s+auipc\s+a7,.*
> +.*:\s+[0-9a-f]+\s+addi\s+a7,a7,.*# e04 <local_far_var>
> +.*:\s+[0-9a-f]+\s+auipc\s+s0,.*
> +.*:\s+[0-9a-f]+\s+l[wd]\s+s0,.*
> +.*:\s+[0-9a-f]+\s+addi\s+s2,zero,-1
> +.*:\s+[0-9a-f]+\s+addi\s+s3,zero,-1
> +.*:\s+[0-9a-f]+\s+jalr\s+zero,0\(ra\)
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-abs-range.s b/ld/testsuite/ld-riscv-elf/got-relax-abs-range.s
> new file mode 100644
> index 00000000000..0bb32f493e2
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-abs-range.s
> @@ -0,0 +1,47 @@
> +	.macro load rd, addr
> +	.ifdef __64_bit__
> +	ld \rd, \addr
> +	.else
> +	lw \rd, \addr
> +	.endif
> +	.endm
> +
> +	.option norvc
> +	.text
> +	.globl _start
> +_start:
> +	/* Exercise the assembler's own GOT relaxation markers.  */
> +	lga a0, local_var
> +	lga a1, abs_min
> +	lga a2, abs_max
> +	lga a3, abs_neg
> +	lga a4, weak_hidden
> +	/* Neither value fits in a signed 12-bit immediate.  */
> +	lga a5, abs_far
> +	lga a6, abs_below
> +	/* This symbol needs PC-relative addressing even in non-PIC output.  */
> +	lga a7, local_far_var
> +
> +	.option push
> +	.option norelax
> +	lga s0, local_var
> +	.option pop
> +
> +	/* Both uses become immediate loads when the high part is deleted.  */
> +.Labs:
> +	.reloc .Labs, R_RISCV_RELAX
> +	auipc s1, %got_pcrel_hi(abs_neg)
> +	load s2, %pcrel_lo(.Labs)(s1)
> +	load s3, %pcrel_lo(.Labs)(s1)
> +	ret
> +
> +	.hidden abs_min, abs_max, abs_neg, abs_far, abs_below
> +	.weak weak_hidden
> +	.hidden weak_hidden
> +	.data
> +local_var:
> +	.word 0
> +	/* Keep the next symbol outside the zero-page range.  */
> +	.space 0x800
> +local_far_var:
> +	.word 0
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-c-li-rv32.d b/ld/testsuite/ld-riscv-elf/got-relax-c-li-rv32.d
> new file mode 100644
> index 00000000000..34033576101
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-c-li-rv32.d
> @@ -0,0 +1,6 @@
> +#name: GOT relaxation C.LI RV32
> +#source: got-relax-c-li.s
> +#as: -march=rv32ic -mabi=ilp32 -mlittle-endian
> +#ld: -melf32lriscv --relax-got -Ttext=0x10000 -Tdata=0x20000 --section-start=.got=0x30000 --defsym c_min=-32 --defsym c_max=31 --defsym c_zero=0 --defsym c_below=-33 --defsym c_above=32 --defsym c_neg=-1
> +#objdump: -d -j .text -Mno-aliases
> +#dump: got-relax-c-li.d
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-c-li.d b/ld/testsuite/ld-riscv-elf/got-relax-c-li.d
> new file mode 100644
> index 00000000000..bc4e9b535da
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-c-li.d
> @@ -0,0 +1,32 @@
> +#name: GOT relaxation C.LI RV64
> +#source: got-relax-c-li.s
> +#as: -march=rv64ic -mabi=lp64 -mlittle-endian --defsym __64_bit__=1
> +#ld: -melf64lriscv --relax-got -Ttext=0x10000 -Tdata=0x20000 --section-start=.got=0x30000 --defsym c_min=-32 --defsym c_max=31 --defsym c_zero=0 --defsym c_below=-33 --defsym c_above=32 --defsym c_neg=-1
> +#objdump: -d -j .text -Mno-aliases
> +
> +.*: +file format .*
> +
> +Disassembly of section \.text:
> +
> +0+10000 <_start>:
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+a0,-32
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+a1,31
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+a2,0
> +.*:\s+[0-9a-f]{8}\s+addi\s+a3,zero,-33
> +.*:\s+[0-9a-f]{8}\s+addi\s+a4,zero,32
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+a5,0
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+sp,-1
> +.*:\s+[0-9a-f]{8}\s+auipc\s+s0,.*
> +.*:\s+[0-9a-f]{8}\s+addi\s+s0,s0,.*# 20000 <.*>
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+t2,-32
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+t3,-32
> +.*:\s+[0-9a-f]{8}\s+addi\s+zero,zero,0
> +.*:\s+[0-9a-f]{8}\s+auipc\s+t4,.*
> +.*:\s+[0-9a-f]{8}\s+l[wd]\s+t4,.*\(t4\).*
> +.*:\s+[0-9a-f]{4}\s+c\.addi\s+zero,0
> +.*:\s+[0-9a-f]{8}\s+addi\s+zero,zero,0
> +
> +0+10030 <aligned>:
> +.*:\s+[0-9a-f]{4}\s+c\.li\s+t5,31
> +.*:\s+[0-9a-f]{4}\s+c\.addi\s+zero,0
> +.*:\s+[0-9a-f]{8}\s+addi\s+zero,zero,0
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-c-li.s b/ld/testsuite/ld-riscv-elf/got-relax-c-li.s
> new file mode 100644
> index 00000000000..cadad4b4a0c
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-c-li.s
> @@ -0,0 +1,50 @@
> +	.macro load rd, addr
> +	.ifdef __64_bit__
> +	ld \rd, \addr
> +	.else
> +	lw \rd, \addr
> +	.endif
> +	.endm
> +
> +	.text
> +	.globl _start
> +_start:
> +	/* Check both ends of the C.LI range and the values
> +	   just outside it.  */
> +	lga a0, c_min
> +	lga a1, c_max
> +	lga a2, c_zero
> +	lga a3, c_below
> +	lga a4, c_above
> +	lga a5, weak_hidden
> +	lga sp, c_neg
> +	lga s0, local_var
> +
> +	/* Check both LOs are compressed and their common HI is removed.  */
> +.Lshared:
> +	.reloc .Lshared, R_RISCV_RELAX
> +	auipc t1, %got_pcrel_hi(c_min)
> +	load t2, %pcrel_lo(.Lshared)(t1)
> +	load t3, %pcrel_lo(.Lshared)(t1)
> +
> +	/* Check rd = x0 is skipped.  */
> +.Lzero:
> +	.reloc .Lzero, R_RISCV_RELAX
> +	auipc t0, %got_pcrel_hi(c_zero)
> +	load zero, %pcrel_lo(.Lzero)(t0)
> +
> +	.option push
> +	.option norelax
> +	lga t4, c_zero
> +	.option pop
> +
> +	/* Shortening must preserve the alignment of the following label.  */
> +	.balign 8
> +aligned:
> +	lga t5, c_max
> +
> +	.weak weak_hidden
> +	.hidden weak_hidden, c_min, c_max, c_zero, c_below, c_above, c_neg
> +	.data
> +local_var:
> +	.word 1
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-dso-version.d b/ld/testsuite/ld-riscv-elf/got-relax-dso-version.d
> new file mode 100644
> index 00000000000..a61a48867f4
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-dso-version.d
> @@ -0,0 +1,21 @@
> +#source: got-relax.s
> +#as: -march=rv64gc -mabi=lp64d -mlittle-endian
> +#ld: -melf64lriscv -shared -z separate-code --relax-got --relax-gp --defsym __global_pointer$=gp_near_var+0x100 --defsym gp_far_var=0x80000000 --defsym extern_var_from_object=0 --defsym extern_var_from_dynamic_lib=0 --defsym internal_var2=0 --version-script got-relax-dso-version.ver
> +#objdump: -d -j .text -Mno-aliases
> +
> +# The version script makes internal_var1 local, allowing GOT relaxation.
> +# Other symbol cases are covered by got-relax-dso.d.
> +
> +.*:[ 	]+file format .*
> +
> +#...
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +#  internal_var1: GOT->PCREL (non-preemptible via version script)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a0,a0,.*# [0-9a-f]+ <internal_var1>
> +#  extern_var_from_object: NOT relaxed (preemptible)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a1,.*\(a1\).*
> +#pass
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-dso-version.ver b/ld/testsuite/ld-riscv-elf/got-relax-dso-version.ver
> new file mode 100644
> index 00000000000..c9282f2111f
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-dso-version.ver
> @@ -0,0 +1 @@
> +{ local: internal_var1; };
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-dso.d b/ld/testsuite/ld-riscv-elf/got-relax-dso.d
> new file mode 100644
> index 00000000000..c0ef7e6971b
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-dso.d
> @@ -0,0 +1,67 @@
> +#source: got-relax.s
> +#as: -march=rv64gc -mabi=lp64d -mlittle-endian
> +#ld: -melf64lriscv -shared -z separate-code --relax-got --relax-gp --defsym __global_pointer$=gp_near_var+0x100 --defsym gp_far_var=0x80000000 --defsym extern_var_from_object=0 --defsym extern_var_from_dynamic_lib=0 --defsym internal_var2=0
> +#objdump: -d -j .text -Mno-aliases
> +
> +# DSO with --relax-got: only non-preemptible symbols relaxed;
> +# preemptible keep GOT.
> +# Tests the expected behaviour of GOT relaxation, as well as it should
> +# not affect the existing PCREL gp relaxation
> +
> +.*:[ 	]+file format .*
> +
> +#...
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +#  internal_var1: NOT relaxed (preemptible in DSO)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a0,.*\(a0\).*
> +#  extern_var_from_object: NOT relaxed (preemptible)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a1,.*\(a1\).*
> +#  extern_var_from_dynamic_lib: NOT relaxed (preemptible)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a2,.*\(a2\).*
> +#  hidden_var: GOT->PCREL (non-preemptible)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a3,a3,.*# [0-9a-f]+ <hidden_var>
> +#  weak_var: NOT relaxed (undefined weak may be resolved at runtime in DSO)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a4,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a4,.*\(a4\).*
> +#  hidden_weak_var: ABS (undefined hidden weak -> 0)
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.li[ 	]+s4,0
> +#  shared HI: NOT relaxed (internal_var2 is preemptible in DSO)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a5,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a6,.*\(a5\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a7,.*\(a5\).*
> +#  separated HI/LO: NOT relaxed (preemptible in DSO)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t1,zero,0
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+t0,.*\(t0\).*
> +#  interleave1: GOT stays (preemptible), pcrel stays
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t2,t2,.*# [0-9a-f]+ <gp_near_var>
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+t3,.*\(t3\).*
> +#  interleave2: same as above
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t4,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t5,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+t4,.*\(t4\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t5,t5,.*# [0-9a-f]+ <gp_near_var>
> +#  interleave3: GOT stays (preemptible), gp_far_var pcrel stays
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t6,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+t6,.*\(t6\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+s0,s0,.*# [0-9a-f]+ <gp_far_var>
> +#  local_var: GOT->PCREL (local symbol, non-preemptible even in DSO)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+s1,s1,.*
> +#  no RELAX marker: NOT relaxed (no R_RISCV_RELAX)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+s2,.*\(s2\).*
> +#  IFUNC: NOT relaxed (needs runtime resolution)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+s3,.*\(s3\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.jr[ 	]+ra
> +#pass
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-dynamic-lib.s b/ld/testsuite/ld-riscv-elf/got-relax-dynamic-lib.s
> new file mode 100644
> index 00000000000..cc8dd51511e
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-dynamic-lib.s
> @@ -0,0 +1,6 @@
> +	.data
> +	.globl	extern_var_from_dynamic_lib
> +	.type	extern_var_from_dynamic_lib,@object
> +	.size	extern_var_from_dynamic_lib,4
> +extern_var_from_dynamic_lib:
> +	.word	456
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-lo-addend-rvc.d b/ld/testsuite/ld-riscv-elf/got-relax-lo-addend-rvc.d
> new file mode 100644
> index 00000000000..afc520d4105
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-lo-addend-rvc.d
> @@ -0,0 +1,6 @@
> +#name: GOT relaxation C.LI invalid LO addend RV64
> +#source: got-relax-lo-addend.s
> +#as: -march=rv64ic -mabi=lp64 -mlittle-endian
> +#ld: -melf64lriscv -pie --relax-got --defsym abs_sym=16 --defsym other_sym=32
> +#error: .*dangerous relocation: %pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20\n
> +#error: .*dangerous relocation: %pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20\n?$
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-lo-addend.d b/ld/testsuite/ld-riscv-elf/got-relax-lo-addend.d
> new file mode 100644
> index 00000000000..14eb9117700
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-lo-addend.d
> @@ -0,0 +1,5 @@
> +#source: got-relax-lo-addend.s
> +#as: -march=rv64i -mabi=lp64 -mlittle-endian
> +#ld: -melf64lriscv -pie --relax-got --defsym abs_sym=16 --defsym other_sym=32
> +#error: .*dangerous relocation: %pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20\n
> +#error: .*dangerous relocation: %pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20\n?$
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-lo-addend.s b/ld/testsuite/ld-riscv-elf/got-relax-lo-addend.s
> new file mode 100644
> index 00000000000..14e10e59606
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-lo-addend.s
> @@ -0,0 +1,23 @@
> +	.text
> +	.globl _start
> +_start:
> +	/* Removing the HI must not hide the invalid LO addend.  */
> +.Lgot:
> +	.reloc .Lgot, R_RISCV_RELAX
> +	auipc a0, %got_pcrel_hi(abs_sym)
> +	ld a0, %pcrel_lo(.Lgot + 4)(a0)
> +
> +	/* Initialize this GOT entry even after reporting the error above.  */
> +	lga a1, other_sym
> +
> +	/* Changing the HI to PCREL must not hide the invalid LO addend.  */
> +.Lpcrel:
> +	.reloc .Lpcrel, R_RISCV_RELAX
> +	auipc a2, %got_pcrel_hi(pcrel_sym)
> +	ld a2, %pcrel_lo(.Lpcrel + 4)(a2)
> +	ret
> +
> +	.hidden abs_sym, other_sym
> +	.data
> +pcrel_sym:
> +	.word 0
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-nopie.d b/ld/testsuite/ld-riscv-elf/got-relax-nopie.d
> new file mode 100644
> index 00000000000..dced18e4e72
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-nopie.d
> @@ -0,0 +1,65 @@
> +#source: got-relax.s
> +#source: got-relax-object.s
> +#as: -march=rv64gc -mabi=lp64d -mlittle-endian
> +#ld: -melf64lriscv -no-pie -z separate-code --relax-got --relax-gp --defsym __global_pointer$=gp_near_var+0x100 --defsym gp_far_var=0x80000000 tmpdir/got-relax-dynamic-lib.so
> +#objdump: -d -j .text -Mno-aliases
> +
> +# Non-PIE with --relax-got
> +# Tests the expected behaviour of GOT relaxation, as well as it should
> +# not affect the existing PCREL gp relaxation
> +
> +.*:[ 	]+file format .*
> +
> +#...
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +#  internal_var: GOT->PCREL
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a0,a0,.*# [0-9a-f]+ <internal_var1>
> +#  extern_var_from_object: GOT->PCREL (defined in another object)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a1,a1,.*# [0-9a-f]+ <extern_var_from_object>
> +#  extern_var_from_dynamic_lib: NOT relaxed (preemptible, from DSO)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a2,.*\(a2\).*
> +#  hidden_var: GOT->PCREL
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a3,a3,.*# [0-9a-f]+ <hidden_var>
> +#  weak_var: dynamically resolvable; retain the GOT load.
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a4,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a4,.*\(a4\).*
> +#  hidden_weak_var: ABS (undefined hidden weak -> 0)
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.li[ 	]+s4,0
> +#  shared HI: auipc preserved, both LOs relaxed to addi with rs1=a5
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a5,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a6,a5,.*# [0-9a-f]+ <internal_var2>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a7,a5,.*
> +#  separated HI/LO: PCREL (non-adjacent HI/LO still relaxed)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t1,zero,0
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t0,t0,.*# [0-9a-f]+ <internal_var2>
> +#  interleave1: GOT->PCREL, gp relaxation as usual
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t2,gp,.*# [0-9a-f]+ <gp_near_var>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t3,t3,.*# [0-9a-f]+ <internal_var2>
> +#  interleave2: same as above
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t4,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t4,t4,.*# [0-9a-f]+ <internal_var2>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t5,gp,.*# [0-9a-f]+ <gp_near_var>
> +#  interleave3: GOT->PCREL, gp_far_var NOT gp-relaxed (too far from gp)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t6,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t6,t6,.*# [0-9a-f]+ <internal_var2>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+s0,s0,.*# [0-9a-f]+ <gp_far_var>
> +#  local_var: GOT->PCREL
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+s1,s1,.*
> +#  no RELAX marker: NOT relaxed (no R_RISCV_RELAX)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+s2,.*\(s2\).*
> +#  IFUNC: NOT relaxed (needs runtime resolution)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+s3,.*\(s3\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.jr[ 	]+ra
> +#pass
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-object.s b/ld/testsuite/ld-riscv-elf/got-relax-object.s
> new file mode 100644
> index 00000000000..88ba9ae3492
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-object.s
> @@ -0,0 +1,6 @@
> +	.data
> +	.globl	extern_var_from_object
> +	.type	extern_var_from_object,@object
> +	.size	extern_var_from_object,4
> +extern_var_from_object:
> +	.word	123
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-options-norelax.d b/ld/testsuite/ld-riscv-elf/got-relax-options-norelax.d
> new file mode 100644
> index 00000000000..648264d91f8
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-options-norelax.d
> @@ -0,0 +1,24 @@
> +#name: GOT relaxation options RV64
> +#source: got-relax-options.s
> +#as: -march=rv64gc -mabi=lp64d -mlittle-endian
> +#ld: -melf64lriscv -no-pie --relax-gp --defsym __global_pointer$=gp_near_var+0x100 --defsym abs_addi=2047 --defsym abs_c_li=16
> +#objdump: -d -j .text -Mno-aliases
> +
> +# With --no-relax or -r, both GOT and ordinary PCREL accesses stay unchanged.
> +
> +.*:[ 	]+file format .*
> +
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a0,.*\(a0\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a1,.*\(a1\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a2,.*\(a2\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a3,.*\(a3\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a4,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a4,a4,.*
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.jr[ 	]+ra
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-options.d b/ld/testsuite/ld-riscv-elf/got-relax-options.d
> new file mode 100644
> index 00000000000..bdd8637a130
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-options.d
> @@ -0,0 +1,24 @@
> +#name: GOT relaxation options RV64
> +#source: got-relax-options.s
> +#as: -march=rv64gc -mabi=lp64d -mlittle-endian
> +#ld: -melf64lriscv -no-pie --relax-gp --defsym __global_pointer$=gp_near_var+0x100 --defsym abs_addi=2047 --defsym abs_c_li=16
> +#objdump: -d -j .text -Mno-aliases
> +
> +# With GOT relaxation disabled, all GOT accesses remain as auipc+ld.
> +# Ordinary PCREL to GP relaxation must still work.
> +
> +.*:[ 	]+file format .*
> +
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a0,.*\(a0\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a1,.*\(a1\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a2,.*\(a2\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a3,.*\(a3\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a4,gp,.*# [0-9a-f]+ <gp_near_var>
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.jr[ 	]+ra
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-options.s b/ld/testsuite/ld-riscv-elf/got-relax-options.s
> new file mode 100644
> index 00000000000..7ddf62a01a2
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-options.s
> @@ -0,0 +1,23 @@
> +	.text
> +	.globl _start
> +_start:
> +	/* GOT accesses that can become PCREL, ADDI, or C.LI.  */
> +	lga a0, local_var
> +	lga a1, abs_addi
> +	lga a2, abs_c_li
> +	/* An undefined hidden weak symbol is known to be zero.  */
> +	lga a3, weak_hidden
> +
> +	/* Ordinary GP relaxation must not depend on --relax-got.  */
> +	lla a4, gp_near_var
> +	ret
> +
> +	.weak weak_hidden
> +	.hidden weak_hidden
> +	.data
> +local_var:
> +	.word 0
> +	.globl gp_near_var
> +	.hidden gp_near_var
> +gp_near_var:
> +	.word 0
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.d b/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.d
> new file mode 100644
> index 00000000000..d038a3f3252
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.d
> @@ -0,0 +1,28 @@
> +#name: GOT relaxation PC-relative range RV64
> +#source: got-relax-pcrel-range.s
> +#as: -march=rv64i -mabi=lp64 -mlittle-endian
> +#ld: -melf64lriscv --relax-got -Tgot-relax-pcrel-range.ld
> +#objdump: -d -j .text -Mno-aliases
> +
> +.*: +file format .*
> +
> +Disassembly of section \.text:
> +
> +0+90010000 <_start>:
> +# Forward target safely in range.
> +.*:\s+[0-9a-f]+\s+auipc\s+a0,0x7fffe
> +.*:\s+[0-9a-f]+\s+addi\s+a0,a0,0 # 11000e000 <positive_in>
> +# Keep GOT loads at the forward alignment margin and beyond the range.
> +.*:\s+[0-9a-f]+\s+auipc\s+a1,0x10
> +.*:\s+[0-9a-f]+\s+ld\s+a1,.*\(a1\).*
> +.*:\s+[0-9a-f]+\s+auipc\s+a2,0x10
> +.*:\s+[0-9a-f]+\s+ld\s+a2,.*\(a2\).*
> +# Backward target safely in range.
> +.*:\s+[0-9a-f]+\s+auipc\s+a3,0x80001
> +.*:\s+[0-9a-f]+\s+addi\s+a3,a3,0 # 10011018 <negative_in>
> +# Keep GOT loads at the backward alignment margin and beyond the range.
> +.*:\s+[0-9a-f]+\s+auipc\s+a4,0x10
> +.*:\s+[0-9a-f]+\s+ld\s+a4,.*\(a4\).*
> +.*:\s+[0-9a-f]+\s+auipc\s+a5,0x10
> +.*:\s+[0-9a-f]+\s+ld\s+a5,.*\(a5\).*
> +.*:\s+[0-9a-f]+\s+jalr\s+zero,0\(ra\)
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.ld b/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.ld
> new file mode 100644
> index 00000000000..c4835a52aaf
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.ld
> @@ -0,0 +1,24 @@
> +OUTPUT_ARCH (riscv)
> +ENTRY (_start)
> +SECTIONS
> +{
> +  .text 0x90010000 : { *(.text) }
> +  /* Keep the GOT reachable even when the target is too far away.  */
> +  .got 0x90020000 : { *(.got) }
> +
> +  /* Each input pair is eight bytes.  NOLOAD avoids large output files.
> +     The edge offsets fit AUIPC+ADDI, but leave no alignment margin.  */
> +  .positive_in (ADDR (.text) + 0x7fffe000) (NOLOAD) :
> +  { positive_in = .; . += 1; }
> +  .positive_edge (ADDR (.text) + 8 + 0x7ffff7ff) (NOLOAD) :
> +  { positive_edge = .; . += 1; }
> +  .positive_out (ADDR (.text) + 16 + 0x80000000) (NOLOAD) :
> +  { positive_out = .; . += 1; }
> +  .negative_in (ADDR (.text) + 24 - 0x7ffff000) (NOLOAD) :
> +  { negative_in = .; . += 1; }
> +  .negative_edge (ADDR (.text) + 32 - 0x80000800) (NOLOAD) :
> +  { negative_edge = .; . += 1; }
> +  .negative_out (ADDR (.text) + 40 - 0x80001000) (NOLOAD) :
> +  { negative_out = .; . += 1; }
> +  .riscv.attributes 0 : { *(.riscv.attributes) }
> +}
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.s b/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.s
> new file mode 100644
> index 00000000000..5bf1e6dd571
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-pcrel-range.s
> @@ -0,0 +1,11 @@
> +	.text
> +	.globl _start
> +_start:
> +	/* The script places each target relative to its own AUIPC.  */
> +	lga a0, positive_in
> +	lga a1, positive_edge
> +	lga a2, positive_out
> +	lga a3, negative_in
> +	lga a4, negative_edge
> +	lga a5, negative_out
> +	ret
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-pie.d b/ld/testsuite/ld-riscv-elf/got-relax-pie.d
> new file mode 100644
> index 00000000000..6c93bda7cd9
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-pie.d
> @@ -0,0 +1,67 @@
> +#source: got-relax.s
> +#source: got-relax-object.s
> +#as: -march=rv64gc -mabi=lp64d -mlittle-endian
> +#ld: -melf64lriscv -pie -z separate-code --relax-got --relax-gp --defsym __global_pointer$=gp_near_var+0x100 --defsym gp_far_var=0x80000000 tmpdir/got-relax-dynamic-lib.so
> +#objdump: -d -Mno-aliases
> +
> +# PIE with --relax-got
> +# Tests the expected behaviour of GOT relaxation, as well as it should
> +# not affect the existing PCREL gp relaxation
> +
> +.*:[ 	]+file format .*
> +
> +#...
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +#  internal_var: GOT->PCREL
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a0,a0,.*# [0-9a-f]+ <internal_var1>
> +#  extern_var_from_object: GOT->PCREL (defined in another object)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a1,a1,.*# [0-9a-f]+ <extern_var_from_object>
> +#  extern_var_from_dynamic_lib: NOT relaxed (preemptible, from DSO)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a2,.*\(a2\).*
> +#  hidden_var: GOT->PCREL
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a3,a3,.*# [0-9a-f]+ <hidden_var>
> +#  weak_var: dynamically resolvable; retain the GOT load.
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a4,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+a4,.*\(a4\).*
> +#  hidden_weak_var: ABS (undefined hidden weak -> 0)
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.li[ 	]+s4,0
> +#  shared HI: auipc preserved, both LOs relaxed to addi with rs1=a5
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+a5,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a6,a5,.*# [0-9a-f]+ <internal_var2>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+a7,a5,.*
> +#  separated HI/LO: GOT->PCREL (non-adjacent HI/LO still relaxed)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t1,zero,0
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t0,t0,.*# [0-9a-f]+ <internal_var2>
> +#  interleave1: GOT->PCREL, no gp relaxation (PIE)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t2,t2,.*# [0-9a-f]+ <gp_near_var>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t3,t3,.*# [0-9a-f]+ <internal_var2>
> +#  interleave2: same as above
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t4,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t5,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t4,t4,.*# [0-9a-f]+ <internal_var2>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t5,t5,.*# [0-9a-f]+ <gp_near_var>
> +#  interleave3: GOT->PCREL, gp_far_var stays pcrel (PIE, no gp relax anyway)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+t6,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s0,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+t6,t6,.*# [0-9a-f]+ <internal_var2>
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+s0,s0,.*# [0-9a-f]+ <gp_far_var>
> +#  local_var: GOT->PCREL
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s1,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+addi[ 	]+s1,s1,.*
> +#  no RELAX marker: NOT relaxed (no R_RISCV_RELAX)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s2,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+s2,.*\(s2\).*
> +#  IFUNC: NOT relaxed (needs runtime resolution)
> +.*:[ 	]+[0-9a-f]+[ 	]+auipc[ 	]+s3,0x[0-9a-f]+
> +.*:[ 	]+[0-9a-f]+[ 	]+ld[ 	]+s3,.*\(s3\).*
> +.*:[ 	]+[0-9a-f]+[ 	]+c\.jr[ 	]+ra
> +#pass
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-relocs-rv32.d b/ld/testsuite/ld-riscv-elf/got-relax-relocs-rv32.d
> new file mode 100644
> index 00000000000..14fd7f4ddb7
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-relocs-rv32.d
> @@ -0,0 +1,40 @@
> +#name: GOT relaxation relocations and GOT contents RV32
> +#source: got-relax-relocs.s
> +#as: -march=rv32i -mabi=ilp32 -mlittle-endian
> +#ld: -melf32lriscv -pie --relax-got --emit-relocs --defsym abs_var=16 -Ttext=0x10000 -Tdata=0x20000 --section-start=.got=0x30000
> +#readelf: -Wr -x .got
> +
> +# All text relocations must use public relocation types.
> +Relocation section '\.rela\.text' .* contains 18 entries:
> +\s+Offset\s+Info\s+Type\s+.*
> +0+10000\s+[0-9a-f]+\s+R_RISCV_PCREL_HI20\s+0+20000\s+local_var \+ 0
> +0+10000\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10004\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10000\s+\.L0\s+\+ 0
> +0+10004\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10008\s+[0-9a-f]+\s+R_RISCV_PCREL_HI20\s+0+20004\s+hidden_var \+ 0
> +0+10008\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+1000c\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10008\s+\.L0\s+\+ 0
> +0+1000c\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10010\s+[0-9a-f]+\s+R_RISCV_NONE\s+4
> +0+10010\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10010\s+[0-9a-f]+\s+R_RISCV_LO12_I\s+0+10\s+abs_var \+ 0
> +0+10010\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10014\s+[0-9a-f]+\s+R_RISCV_GOT_HI20\s+0+\s+weak_var \+ 0
> +0+10014\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10018\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10014\s+\.L0\s+\+ 0
> +0+10018\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+1001c\s+[0-9a-f]+\s+R_RISCV_GOT_HI20\s+0+20000\s+local_var \+ 0
> +0+10020\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+1001c\s+\.L0\s+\+ 0
> +
> +# Local and hidden entries need RELATIVE; the weak entry stays dynamic.
> +Relocation section '\.rela\.dyn' .* contains 4 entries:
> +\s+Offset\s+Info\s+Type\s+.*
> +0+30004\s+[0-9a-f]+\s+R_RISCV_RELATIVE\s+20000
> +0+30010\s+[0-9a-f]+\s+R_RISCV_RELATIVE\s+20004
> +0+\s+[0-9a-f]+\s+R_RISCV_NONE\s+0
> +0+3000c\s+[0-9a-f]+\s+R_RISCV_32\s+0+\s+weak_var \+ 0
> +
> +# Ignore the header pointer, but check every symbol's GOT value.
> +Hex dump of section '\.got':
> +\s+0x00030000 [0-9a-f]{8} 00000200 10000000 00000000 .*
> +\s+0x00030010 04000200 .*
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc-rv32.d b/ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc-rv32.d
> new file mode 100644
> index 00000000000..bf19dff0f32
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc-rv32.d
> @@ -0,0 +1,6 @@
> +#name: GOT relaxation C.LI relocations RV32
> +#source: got-relax-relocs.s
> +#as: -march=rv32ic -mabi=ilp32 -mlittle-endian
> +#ld: -melf32lriscv -pie --relax-got --emit-relocs --defsym abs_var=16 -Ttext=0x10000 -Tdata=0x20000 --section-start=.got=0x30000
> +#readelf: -Wr
> +#dump: got-relax-relocs-rvc.d
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc.d b/ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc.d
> new file mode 100644
> index 00000000000..8d978e42596
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-relocs-rvc.d
> @@ -0,0 +1,29 @@
> +#name: GOT relaxation C.LI relocations RV64
> +#source: got-relax-relocs.s
> +#as: -march=rv64ic -mabi=lp64 -mlittle-endian
> +#ld: -melf64lriscv -pie --relax-got --emit-relocs --defsym abs_var=16 -Ttext=0x10000 -Tdata=0x20000 --section-start=.got=0x30000
> +#readelf: -Wr
> +
> +Relocation section '\.rela\.text' .* contains 18 entries:
> +\s+Offset\s+Info\s+Type\s+.*
> +0+10000\s+[0-9a-f]+\s+R_RISCV_PCREL_HI20\s+0+20000\s+local_var \+ 0
> +0+10000\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10004\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10000\s+\.L0\s+\+ 0
> +0+10004\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10008\s+[0-9a-f]+\s+R_RISCV_PCREL_HI20\s+0+20004\s+hidden_var \+ 0
> +0+10008\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+1000c\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10008\s+\.L0\s+\+ 0
> +0+1000c\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10010\s+[0-9a-f]+\s+R_RISCV_NONE\s+4
> +0+10010\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +# There is no standard relocation for a C.LI immediate.
> +# Its internal marker is consumed.
> +0+10010\s+[0-9a-f]+\s+R_RISCV_NONE\s+0
> +0+10012\s+[0-9a-f]+\s+R_RISCV_NONE\s+2
> +0+10012\s+[0-9a-f]+\s+R_RISCV_GOT_HI20\s+0+\s+weak_var \+ 0
> +0+10012\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10016\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10012\s+\.L0\s+\+ 0
> +0+10016\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+1001a\s+[0-9a-f]+\s+R_RISCV_GOT_HI20\s+0+20000\s+local_var \+ 0
> +0+1001e\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+1001a\s+\.L0\s+\+ 0
> +#...
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-relocs.d b/ld/testsuite/ld-riscv-elf/got-relax-relocs.d
> new file mode 100644
> index 00000000000..8cdbfb5c9cb
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-relocs.d
> @@ -0,0 +1,41 @@
> +#name: GOT relaxation relocations and GOT contents RV64
> +#source: got-relax-relocs.s
> +#as: -march=rv64i -mabi=lp64 -mlittle-endian
> +#ld: -melf64lriscv -pie --relax-got --emit-relocs --defsym abs_var=16 -Ttext=0x10000 -Tdata=0x20000 --section-start=.got=0x30000
> +#readelf: -Wr -x .got
> +
> +# All text relocations must use public relocation types.
> +Relocation section '\.rela\.text' .* contains 18 entries:
> +\s+Offset\s+Info\s+Type\s+.*
> +0+10000\s+[0-9a-f]+\s+R_RISCV_PCREL_HI20\s+0+20000\s+local_var \+ 0
> +0+10000\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10004\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10000\s+\.L0\s+\+ 0
> +0+10004\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10008\s+[0-9a-f]+\s+R_RISCV_PCREL_HI20\s+0+20004\s+hidden_var \+ 0
> +0+10008\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+1000c\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10008\s+\.L0\s+\+ 0
> +0+1000c\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10010\s+[0-9a-f]+\s+R_RISCV_NONE\s+4
> +0+10010\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10010\s+[0-9a-f]+\s+R_RISCV_LO12_I\s+0+10\s+abs_var \+ 0
> +0+10010\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10014\s+[0-9a-f]+\s+R_RISCV_GOT_HI20\s+0+\s+weak_var \+ 0
> +0+10014\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+10018\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+10014\s+\.L0\s+\+ 0
> +0+10018\s+[0-9a-f]+\s+R_RISCV_RELAX\s+0
> +0+1001c\s+[0-9a-f]+\s+R_RISCV_GOT_HI20\s+0+20000\s+local_var \+ 0
> +0+10020\s+[0-9a-f]+\s+R_RISCV_PCREL_LO12_I\s+0+1001c\s+\.L0\s+\+ 0
> +
> +# Local and hidden entries need RELATIVE; the weak entry stays dynamic.
> +Relocation section '\.rela\.dyn' .* contains 4 entries:
> +\s+Offset\s+Info\s+Type\s+.*
> +0+30008\s+[0-9a-f]+\s+R_RISCV_RELATIVE\s+20000
> +0+30020\s+[0-9a-f]+\s+R_RISCV_RELATIVE\s+20004
> +0+\s+[0-9a-f]+\s+R_RISCV_NONE\s+0
> +0+30018\s+[0-9a-f]+\s+R_RISCV_64\s+0+\s+weak_var \+ 0
> +
> +# Ignore the header pointer, but check every symbol's GOT value.
> +Hex dump of section '\.got':
> +\s+0x00030000 [0-9a-f]{8} [0-9a-f]{8} 00000200 00000000 .*
> +\s+0x00030010 10000000 00000000 00000000 00000000 .*
> +\s+0x00030020 04000200 00000000 .*
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-relocs.s b/ld/testsuite/ld-riscv-elf/got-relax-relocs.s
> new file mode 100644
> index 00000000000..aa6f81675ca
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-relocs.s
> @@ -0,0 +1,26 @@
> +	.text
> +	.globl _start
> +_start:
> +	/* Exercise both local and global GOT entries.  */
> +	lga a0, local_var
> +	lga a1, hidden_var
> +	/* Delete the only HI referring to this absolute symbol.  */
> +	lga a2, abs_var
> +	/* The static link resolves this undefined weak symbol to zero.  */
> +	lga a3, weak_var
> +
> +	/* Keep an unrelaxed use of the same local GOT entry.  */
> +	.option push
> +	.option norelax
> +	lga a4, local_var
> +	.option pop
> +	ret
> +
> +	.weak weak_var
> +	.globl hidden_var
> +	.hidden hidden_var, abs_var
> +	.data
> +local_var:
> +	.word 1
> +hidden_var:
> +	.word 2
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-static.d b/ld/testsuite/ld-riscv-elf/got-relax-static.d
> new file mode 100644
> index 00000000000..b5b33db8316
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-static.d
> @@ -0,0 +1,27 @@
> +#name: GOT relaxation static weak and GOT contents after partial link RV64
> +#source: ../ld-elf/dummy.s
> +#as: -march=rv64i -mabi=lp64 -mlittle-endian
> +#ld: -melf64lriscv -static --relax-got --defsym abs_var=16 -Ttext=0x10000 -Tdata=0x20000 --section-start=.got=0x30000 tmpdir/got-relax-partial.o
> +#objdump: -sd -j .text -j .got -Mno-aliases
> +
> +.*: +file format .*
> +#...
> +Contents of section \.got:
> +\s+30000 00000000 00000000 00000200 00000000 .*
> +\s+30010 10000000 00000000 00000000 00000000 .*
> +\s+30020 04000200 00000000 .*
> +
> +Disassembly of section \.text:
> +
> +0+10000 <_start>:
> +.*:\s+[0-9a-f]+\s+auipc\s+a0,0x10
> +.*:\s+[0-9a-f]+\s+addi\s+a0,a0,0 # 20000 <.*>
> +.*:\s+[0-9a-f]+\s+auipc\s+a1,0x10
> +.*:\s+[0-9a-f]+\s+addi\s+a1,a1,-4 # 20004 <hidden_var>
> +.*:\s+[0-9a-f]+\s+addi\s+a2,zero,16
> +# A default-visible undefined weak is known to be zero in this static link.
> +.*:\s+[0-9a-f]+\s+addi\s+a3,zero,0
> +.*:\s+[0-9a-f]+\s+auipc\s+a4,0x20
> +.*:\s+[0-9a-f]+\s+ld\s+a4,-16\(a4\) # 30008 <.*>
> +.*:\s+[0-9a-f]+\s+jalr\s+zero,0\(ra\)
> +#pass
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-zero-page-rv32.d b/ld/testsuite/ld-riscv-elf/got-relax-zero-page-rv32.d
> new file mode 100644
> index 00000000000..9d2de57121a
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-zero-page-rv32.d
> @@ -0,0 +1,6 @@
> +#name: GOT relaxation zero-page RV32
> +#source: got-relax-abs-range.s
> +#as: -march=rv32i -mabi=ilp32 -mlittle-endian
> +#ld: -melf32lriscv -no-pie --relax-got -Tdata=0x600 --defsym abs_min=-2048 --defsym abs_max=2047 --defsym abs_neg=-1 --defsym abs_far=2048 --defsym abs_below=-2049
> +#objdump: -d -j .text -Mno-aliases
> +#dump: got-relax-zero-page.d
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax-zero-page.d b/ld/testsuite/ld-riscv-elf/got-relax-zero-page.d
> new file mode 100644
> index 00000000000..f20e736caf7
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax-zero-page.d
> @@ -0,0 +1,27 @@
> +#name: GOT relaxation zero-page RV64
> +#source: got-relax-abs-range.s
> +#as: -march=rv64i -mabi=lp64 -mlittle-endian --defsym __64_bit__=1
> +#ld: -melf64lriscv -no-pie --relax-got -Tdata=0x600 --defsym abs_min=-2048 --defsym abs_max=2047 --defsym abs_neg=-1 --defsym abs_far=2048 --defsym abs_below=-2049
> +#objdump: -d -j .text -Mno-aliases
> +
> +.*: +file format .*
> +
> +Disassembly of section \.text:
> +
> +0+[0-9a-f]+ <_start>:
> +.*:\s+[0-9a-f]+\s+addi\s+a0,zero,1536.*
> +.*:\s+[0-9a-f]+\s+addi\s+a1,zero,-2048
> +.*:\s+[0-9a-f]+\s+addi\s+a2,zero,2047
> +.*:\s+[0-9a-f]+\s+addi\s+a3,zero,-1
> +.*:\s+[0-9a-f]+\s+addi\s+a4,zero,0
> +.*:\s+[0-9a-f]+\s+auipc\s+a5,.*
> +.*:\s+[0-9a-f]+\s+l[wd]\s+a5,.*
> +.*:\s+[0-9a-f]+\s+auipc\s+a6,.*
> +.*:\s+[0-9a-f]+\s+l[wd]\s+a6,.*
> +.*:\s+[0-9a-f]+\s+auipc\s+a7,.*
> +.*:\s+[0-9a-f]+\s+addi\s+a7,a7,.*# e04 <local_far_var>
> +.*:\s+[0-9a-f]+\s+auipc\s+s0,.*
> +.*:\s+[0-9a-f]+\s+l[wd]\s+s0,.*
> +.*:\s+[0-9a-f]+\s+addi\s+s2,zero,-1
> +.*:\s+[0-9a-f]+\s+addi\s+s3,zero,-1
> +.*:\s+[0-9a-f]+\s+jalr\s+zero,0\(ra\)
> diff --git a/ld/testsuite/ld-riscv-elf/got-relax.s b/ld/testsuite/ld-riscv-elf/got-relax.s
> new file mode 100644
> index 00000000000..8c899c2b4ed
> --- /dev/null
> +++ b/ld/testsuite/ld-riscv-elf/got-relax.s
> @@ -0,0 +1,150 @@
> +	.text
> +	.globl	_start
> +	.type	_start,@function
> +_start:
> +	/* Internal: relaxable in exe; in DSO only if non-preemptible.  */
> +.L_got_int:
> +	.reloc .L_got_int, R_RISCV_RELAX
> +	auipc	a0, %got_pcrel_hi(internal_var1)
> +	ld	a0, %pcrel_lo(.L_got_int)(a0)
> +
> +	/* Variable from another input object: relaxable in executables.  */
> +.L_got_obj:
> +	.reloc .L_got_obj, R_RISCV_RELAX
> +	auipc	a1, %got_pcrel_hi(extern_var_from_object)
> +	ld	a1, %pcrel_lo(.L_got_obj)(a1)
> +
> +	/* Dynamic lib var: from DSO, preemptible, must NOT be relaxed.  */
> +.L_got_dlib:
> +	.reloc .L_got_dlib, R_RISCV_RELAX
> +	auipc	a2, %got_pcrel_hi(extern_var_from_dynamic_lib)
> +	ld	a2, %pcrel_lo(.L_got_dlib)(a2)
> +
> +	/* Hidden: always relaxable (non-preemptible).  */
> +.L_got_hid:
> +	.reloc .L_got_hid, R_RISCV_RELAX
> +	auipc	a3, %got_pcrel_hi(hidden_var)
> +	ld	a3, %pcrel_lo(.L_got_hid)(a3)
> +
> +	/* Default-visibility weak symbol may be resolved at runtime.  */
> +.L_got_weak:
> +	.reloc .L_got_weak, R_RISCV_RELAX
> +	auipc	a4, %got_pcrel_hi(weak_var)
> +	ld	a4, %pcrel_lo(.L_got_weak)(a4)
> +
> +	/* Undefined hidden weak: known to be zero.  */
> +.L_got_weak_hidden:
> +	.reloc .L_got_weak_hidden, R_RISCV_RELAX
> +	auipc	s4, %got_pcrel_hi(hidden_weak_var)
> +	ld	s4, %pcrel_lo(.L_got_weak_hidden)(s4)
> +
> +	/* Shared HI: one auipc shared by two PCREL_LO with different rd.  */
> +.L_got_shared:
> +	.reloc .L_got_shared, R_RISCV_RELAX
> +	auipc	a5, %got_pcrel_hi(internal_var2)
> +	ld	a6, %pcrel_lo(.L_got_shared)(a5)
> +	ld	a7, %pcrel_lo(.L_got_shared)(a5)
> +
> +	/* Separated HI/LO: auipc and ld not adjacent
> +	   (one intervening insn).  */
> +.L_got_sep:
> +	.reloc .L_got_sep, R_RISCV_RELAX
> +	auipc	t0, %got_pcrel_hi(internal_var2)
> +	mv	t1, zero
> +	ld	t0, %pcrel_lo(.L_got_sep)(t0)
> +
> +	/* Interleave pcrel_hi/got_hi/pcrel_lo/got_lo (pcrel gp-relaxable).  */
> +	/* check that GOT relaxation does not affect normal PCREL
> +	   relaxation.  */
> +.L_pcrel_ilv1:
> +	.reloc .L_pcrel_ilv1, R_RISCV_RELAX
> +	auipc	t2, %pcrel_hi(gp_near_var)
> +.L_got_ilv1:
> +	.reloc .L_got_ilv1, R_RISCV_RELAX
> +	auipc	t3, %got_pcrel_hi(internal_var2)
> +	addi	t2, t2, %pcrel_lo(.L_pcrel_ilv1)
> +	ld	t3, %pcrel_lo(.L_got_ilv1)(t3)
> +
> +	/* Interleave got_hi/pcrel_hi/got_lo/pcrel_lo (pcrel gp-relaxable).  */
> +	/* same as above, just different interleaving order.  */
> +.L_got_ilv2:
> +	.reloc .L_got_ilv2, R_RISCV_RELAX
> +	auipc	t4, %got_pcrel_hi(internal_var2)
> +.L_pcrel_ilv2:
> +	.reloc .L_pcrel_ilv2, R_RISCV_RELAX
> +	auipc	t5, %pcrel_hi(gp_near_var)
> +	ld	t4, %pcrel_lo(.L_got_ilv2)(t4)
> +	addi	t5, t5, %pcrel_lo(.L_pcrel_ilv2)
> +
> +	/* Interleave got_hi/pcrel_hi/got_lo/pcrel_lo
> +	   (pcrel NOT gp-relaxable).  */
> +.L_got_ilv3:
> +	.reloc .L_got_ilv3, R_RISCV_RELAX
> +	auipc	t6, %got_pcrel_hi(internal_var2)
> +.L_pcrel_far:
> +	.reloc .L_pcrel_far, R_RISCV_RELAX
> +	auipc	s0, %pcrel_hi(gp_far_var)
> +	ld	t6, %pcrel_lo(.L_got_ilv3)(t6)
> +	addi	s0, s0, %pcrel_lo(.L_pcrel_far)
> +
> +	/* Local symbol: always relaxable.  */
> +.L_got_local:
> +	.reloc .L_got_local, R_RISCV_RELAX
> +	auipc	s1, %got_pcrel_hi(local_var)
> +	ld	s1, %pcrel_lo(.L_got_local)(s1)
> +
> +	/* No RELAX marker: must NOT be relaxed even if symbol is local.  */
> +.L_got_norelax:
> +	auipc	s2, %got_pcrel_hi(internal_var2)
> +	ld	s2, %pcrel_lo(.L_got_norelax)(s2)
> +
> +	/* IFUNC: must NOT be relaxed (needs runtime resolution).  */
> +.L_got_ifunc:
> +	.reloc .L_got_ifunc, R_RISCV_RELAX
> +	auipc	s3, %got_pcrel_hi(ifunc_sym)
> +	ld	s3, %pcrel_lo(.L_got_ifunc)(s3)
> +
> +	ret
> +
> +	.type	ifunc_sym,@gnu_indirect_function
> +ifunc_sym:
> +	/* Resolver: just returns its own address for testing.  */
> +	auipc	a0, 0
> +	ret
> +
> +	.weak	weak_var
> +	.weak	hidden_weak_var
> +	.hidden	hidden_weak_var
> +
> +	.data
> +	.globl	internal_var1
> +	.type	internal_var1,@object
> +	.size	internal_var1,4
> +internal_var1:
> +	.word	42
> +
> +	.globl	hidden_var
> +	.hidden	hidden_var
> +	.type	hidden_var,@object
> +	.size	hidden_var,4
> +hidden_var:
> +	.word	99
> +
> +	.globl	internal_var2
> +	.type	internal_var2,@object
> +	.size	internal_var2,4
> +internal_var2:
> +	.word	77
> +
> +	.type	local_var,@object
> +	.size	local_var,4
> +local_var:
> +	.word	55
> +
> +
> +	.section .sdata,"aw",@progbits
> +	.globl gp_near_var
> +	.hidden gp_near_var
> +	.word 0
> +gp_near_var:
> +	.word 0
> diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
> index df2cd2bf260..fa04708e6d7 100644
> --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
> +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
> @@ -501,6 +501,75 @@ if [istarget "riscv*-*-*"] {
>       run_dump_test_ifunc "ifunc-overwrite" rv64 pie
>       run_dump_test_ifunc "ifunc-overwrite" rv64 pic
>   
> +    # GOT load relaxation tests.
> +    # Symbol binding.
> +    run_ld_link_tests {
> +	{ "got-relax-dynamic-lib"
> +	  "-shared -melf64lriscv" "" "-march=rv64gc -mabi=lp64d -mlittle-endian"
> +	  {got-relax-dynamic-lib.s}
> +	  {} "got-relax-dynamic-lib.so" }
> +    }
> +    run_dump_test "got-relax-nopie"
> +    run_dump_test "got-relax-pie"
> +    run_dump_test "got-relax-dso"
> +    run_dump_test "got-relax-dso-version"
> +
> +    # Absolute addresses and zero-page relaxation.
> +    run_dump_test "got-relax-abs-range" {{ld -pie} {name -pie}}
> +    run_dump_test "got-relax-abs-range" {{ld -shared} {name -shared}}
> +    run_dump_test "got-relax-abs-range-rv32" {{ld -pie} {name -pie}}
> +    run_dump_test "got-relax-abs-range-rv32" {{ld -shared} {name -shared}}
> +    run_dump_test "got-relax-zero-page"
> +    run_dump_test "got-relax-zero-page-rv32"
> +
> +    # PC-relative range.
> +    run_dump_test "got-relax-pcrel-range"

The new got-relax-zero-page, got-relax-zero-page-rv32 and 
got-relax-pcrel-range tests are below the return "# The following tests 
require shared library support."

Please move the static cases above the shared-library check. All four 
regressions described above can also be tested with a static link, so 
bare-metal configurations can exercise them.

> +
> +    # C.LI relaxation.
> +    run_dump_test "got-relax-c-li" {{ld -no-pie} {name -no-pie}}
> +    run_dump_test "got-relax-c-li" {{ld -pie} {name -pie}}
> +    run_dump_test "got-relax-c-li" {{ld -shared} {name -shared}}
> +    run_dump_test "got-relax-c-li-rv32" {{ld -no-pie} {name -no-pie}}
> +    run_dump_test "got-relax-c-li-rv32" {{ld -pie} {name -pie}}
> +    run_dump_test "got-relax-c-li-rv32" {{ld -shared} {name -shared}}
> +
> +    # Relocations and GOT contents.
> +    run_dump_test "got-relax-relocs"
> +    run_dump_test "got-relax-relocs-rv32"
> +    run_dump_test "got-relax-relocs-rvc"
> +    run_dump_test "got-relax-relocs-rvc-rv32"
> +
> +    # Partial link followed by a static link.
> +    run_ld_link_tests {
> +	{ "got-relax-partial"
> +	  "-r -melf64lriscv --relax-got" "" "-march=rv64i -mabi=lp64 -mlittle-endian"
> +	  {got-relax-relocs.s}
> +	  {} "got-relax-partial.o" }
> +    }
> +    run_dump_test "got-relax-static"
> +
> +    # Relaxation options.
> +    run_dump_test "got-relax-options"
> +    run_dump_test "got-relax-options" \
> +	{{ld {--relax-got --no-relax-got}}
> +	 {name {--relax-got --no-relax-got}}}
> +    run_dump_test "got-relax-zero-page" \
> +	{{ld {--no-relax-got --relax-got}}
> +	 {name {--no-relax-got --relax-got}}}
> +    run_dump_test "got-relax-options-norelax" \
> +	{{ld {--relax-got --no-relax}}
> +	 {name {--relax-got --no-relax}}}
> +    run_dump_test "got-relax-options-norelax" \
> +	{{ld {--no-relax --relax-got}}
> +	 {name {--no-relax --relax-got}}}
> +    run_dump_test "got-relax-options-norelax" \
> +	{{ld {-r --relax-got}}
> +	 {name {-r --relax-got}}}
> +
> +    # Invalid GOT addends.
> +    run_dump_test "got-relax-lo-addend"
> +    run_dump_test "got-relax-lo-addend-rvc"
> +
>       # TODO: Make the following tests work under RV32.
>       if [istarget "riscv32-*-*"] {
>         return
BR,
Jiawei



More information about the Binutils mailing list