+_bfd_riscv_relax_pc (bfd *abfd,
+ asection *sec,
+ asection *sym_sec,
+ struct bfd_link_info *link_info,
+ Elf_Internal_Rela *rel,
+ bfd_vma symval,
+ bfd_vma max_alignment,
+ bfd_vma reserve_size,
+ bfd_boolean *again ATTRIBUTE_UNUSED,
+ riscv_pcgp_relocs *pcgp_relocs)
+{
+ bfd_vma gp = riscv_global_pointer_value (link_info);
+
+ BFD_ASSERT (rel->r_offset + 4 <= sec->size);
+
+ /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
+ * actual target address. */
+ riscv_pcgp_hi_reloc hi_reloc = {0};
+ switch (ELFNN_R_TYPE (rel->r_info))
+ {
+ case R_RISCV_PCREL_LO12_I:
+ case R_RISCV_PCREL_LO12_S:
+ {
+ riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs,
+ symval - sec_addr(sym_sec));
+ if (hi == NULL)
+ {
+ riscv_record_pcgp_lo_reloc (pcgp_relocs, symval - sec_addr(sym_sec));
+ return TRUE;
+ }
+
+ hi_reloc = *hi;
+ symval = hi_reloc.hi_addr;
+ sym_sec = hi_reloc.sym_sec;
+ if (!riscv_use_pcgp_hi_reloc(pcgp_relocs, hi->hi_sec_off))
+ (*_bfd_error_handler)
+ (_("%B(%A+0x%lx): Unable to clear RISCV_PCREL_HI20 reloc"
+ "for cooresponding RISCV_PCREL_LO12 reloc"),
+ abfd, sec, rel->r_offset);
+ }
+ break;
+
+ case R_RISCV_PCREL_HI20:
+ /* Mergeable symbols and code might later move out of range. */
+ if (sym_sec->flags & (SEC_MERGE | SEC_CODE))
+ return TRUE;
+
+ /* If the cooresponding lo relocation has already been seen then it's not
+ * safe to relax this relocation. */
+ if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset))
+ return TRUE;
+
+ break;
+
+ default:
+ abort ();
+ }
+
+ if (gp)
+ {
+ /* If gp and the symbol are in the same output section, then
+ consider only that section's alignment. */
+ struct bfd_link_hash_entry *h =
+ bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE, TRUE);
+ if (h->u.def.section->output_section == sym_sec->output_section)
+ max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
+ }
+
+ /* Is the reference in range of x0 or gp?
+ Valid gp range conservatively because of alignment issue. */
+ if (VALID_ITYPE_IMM (symval)
+ || (symval >= gp
+ && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
+ || (symval < gp
+ && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size)))
+ {
+ unsigned sym = hi_reloc.hi_sym;
+ switch (ELFNN_R_TYPE (rel->r_info))
+ {
+ case R_RISCV_PCREL_LO12_I:
+ rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
+ rel->r_addend += hi_reloc.hi_addend;
+ return riscv_delete_pcgp_lo_reloc (pcgp_relocs, rel->r_offset, 4);
+
+ case R_RISCV_PCREL_LO12_S:
+ rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
+ rel->r_addend += hi_reloc.hi_addend;
+ return riscv_delete_pcgp_lo_reloc (pcgp_relocs, rel->r_offset, 4);
+
+ case R_RISCV_PCREL_HI20:
+ riscv_record_pcgp_hi_reloc (pcgp_relocs,
+ rel->r_offset,
+ rel->r_addend,
+ symval,
+ ELFNN_R_SYM(rel->r_info),
+ sym_sec);
+ /* We can delete the unnecessary AUIPC and reloc. */
+ rel->r_info = ELFNN_R_INFO (0, R_RISCV_DELETE);
+ rel->r_addend = 4;
+ return riscv_delete_pcgp_hi_reloc (pcgp_relocs, rel->r_offset);
+
+ default:
+ abort ();
+ }
+ }
+
+ return TRUE;
+}
+
+/* Relax PC-relative references to GP-relative references. */
+
+static bfd_boolean