aboutsummaryrefslogtreecommitdiff
path: root/bfd/elfnn-riscv.c
diff options
context:
space:
mode:
authorNelson Chu <nelson@rivosinc.com>2022-09-12 09:26:52 +0800
committerNelson Chu <nelson@rivosinc.com>2022-09-12 11:31:41 +0800
commitecb915b4de7569027ad78bd3e24873bb92cb8e32 (patch)
tree322bc83c90f3bf3b2be8c4b3d696dcaaf0d328d1 /bfd/elfnn-riscv.c
parent0967e2e4926f07de0ff0eabd16ea8965c63b9f2e (diff)
downloadgdb-ecb915b4de7569027ad78bd3e24873bb92cb8e32.zip
gdb-ecb915b4de7569027ad78bd3e24873bb92cb8e32.tar.gz
gdb-ecb915b4de7569027ad78bd3e24873bb92cb8e32.tar.bz2
RISC-V: PR28509, the default visibility symbol cannot be referenced by R_RISCV_JAL.
When generating the shared object, the default visibility symbols may bind externally, which means they will be exported to the dynamic symbol table, and are preemptible by default. These symbols cannot be referenced by the non-pic R_RISCV_JAL and R_RISCV_RVC_JUMP. However, consider that linker may relax the R_RISCV_CALL relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if these relocations are relocated to the plt entries, then we won't report error for them. Perhaps we also need the similar checks for the R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. After applying this patch, and revert the following glibc patch, riscv: Fix incorrect jal with HIDDEN_JUMPTARGET https://sourceware.org/git/?p=glibc.git;a=commit;h=68389203832ab39dd0dbaabbc4059e7fff51c29b I get the expected errors as follows, ld: relocation R_RISCV_RVC_JUMP against `__sigsetjmp' which may bind externally can not be used when making a shared object; recompile with -fPIC ld: relocation R_RISCV_JAL against `exit' which may bind externally can not be used when making a shared object; recompile with -fPIC Besides, we also have similar changes for libgcc, RISC-V: jal cannot refer to a default visibility symbol for shared object https://github.com/gcc-mirror/gcc/commit/45116f342057b7facecd3d05c2091ce3a77eda59 bfd/ pr 28509 * elfnn-riscv.c (riscv_elf_relocate_section): Report errors when makeing a shard object, and the referenced symbols of R_RISCV_JAL relocations are default visibility. Besides, we should handle most of the cases here, so don't need the unresolvable check later for R_RISCV_JAL and R_RISCV_RVC_JUMP. ld/ pr 28509 * testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated. * testsuite/ld-riscv-elf/lib-nopic-01a.s: Removed. * testsuite/ld-riscv-elf/lib-nopic-01b.d: Likewise. * testsuite/ld-riscv-elf/lib-nopic-01b.s: Likewise. * testsuite/ld-riscv-elf/shared-lib-nopic-01.d: New testcase. * testsuite/ld-riscv-elf/shared-lib-nopic-01.s: Likewise. * testsuite/ld-riscv-elf/shared-lib-nopic-02.d: Likewise. * testsuite/ld-riscv-elf/shared-lib-nopic-02.s: Likewise. * testsuite/ld-riscv-elf/shared-lib-nopic-03.d: Likewise. * testsuite/ld-riscv-elf/shared-lib-nopic-03.s: Likewise. * testsuite/ld-riscv-elf/shared-lib-nopic-04.d: Likewise. * testsuite/ld-riscv-elf/shared-lib-nopic-04.s: Likewise.
Diffstat (limited to 'bfd/elfnn-riscv.c')
-rw-r--r--bfd/elfnn-riscv.c71
1 files changed, 43 insertions, 28 deletions
diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 86cb207..0e0a0b0 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -2457,12 +2457,44 @@ riscv_elf_relocate_section (bfd *output_bfd,
case R_RISCV_JAL:
case R_RISCV_RVC_JUMP:
- /* This line has to match the check in _bfd_riscv_relax_section. */
- if (bfd_link_pic (info) && h != NULL && h->plt.offset != MINUS_ONE)
+ if (bfd_link_pic (info) && h != NULL)
{
- /* Refer to the PLT entry. */
- relocation = sec_addr (htab->elf.splt) + h->plt.offset;
- unresolved_reloc = false;
+ if (h->plt.offset != MINUS_ONE)
+ {
+ /* Refer to the PLT entry. This check has to match the
+ check in _bfd_riscv_relax_section. */
+ relocation = sec_addr (htab->elf.splt) + h->plt.offset;
+ unresolved_reloc = false;
+ }
+ else if (!SYMBOL_REFERENCES_LOCAL (info, h)
+ && (input_section->flags & SEC_ALLOC) != 0
+ && (input_section->flags & SEC_READONLY) != 0
+ && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
+ {
+ /* PR 28509, when generating the shared object, these
+ referenced symbols may bind externally, which means
+ they will be exported to the dynamic symbol table,
+ and are preemptible by default. These symbols cannot
+ be referenced by the non-pic relocations, like
+ R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
+
+ However, consider that linker may relax the R_RISCV_CALL
+ relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
+ these relocations are relocated to the plt entries,
+ then we won't report error for them.
+
+ Perhaps we also need the similar checks for the
+ R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
+ if (asprintf (&msg_buf,
+ _("%%X%%P: relocation %s against `%s' which "
+ "may bind externally can not be used when "
+ "making a shared object; recompile "
+ "with -fPIC\n"),
+ howto->name, h->root.root.string) == -1)
+ msg_buf = NULL;
+ msg = msg_buf;
+ r = bfd_reloc_notsupported;
+ }
}
break;
@@ -2755,29 +2787,12 @@ riscv_elf_relocate_section (bfd *output_bfd,
&& _bfd_elf_section_offset (output_bfd, info, input_section,
rel->r_offset) != (bfd_vma) -1)
{
- switch (r_type)
- {
- case R_RISCV_JAL:
- case R_RISCV_RVC_JUMP:
- if (asprintf (&msg_buf,
- _("%%X%%P: relocation %s against `%s' can "
- "not be used when making a shared object; "
- "recompile with -fPIC\n"),
- howto->name,
- h->root.root.string) == -1)
- msg_buf = NULL;
- break;
-
- default:
- if (asprintf (&msg_buf,
- _("%%X%%P: unresolvable %s relocation against "
- "symbol `%s'\n"),
- howto->name,
- h->root.root.string) == -1)
- msg_buf = NULL;
- break;
- }
-
+ if (asprintf (&msg_buf,
+ _("%%X%%P: unresolvable %s relocation against "
+ "symbol `%s'\n"),
+ howto->name,
+ h->root.root.string) == -1)
+ msg_buf = NULL;
msg = msg_buf;
r = bfd_reloc_notsupported;
}