From dc4d44aa45d54acf52900ed32b71c13f4f3d7f2c Mon Sep 17 00:00:00 2001 From: Nelson Chu Date: Wed, 9 Oct 2019 16:18:40 +0800 Subject: [PATCH 2/2] RISC-V: Improve the error message when linking non-pic code into shared library. R_RISCV_CALL, R_RISCV_JAL and R_RISCV_RVC_JUMP are pc-relative relocation. For now, we do not allow the object with these relocation links into a shared library since the referenced symbols may be loaded to the places that too far from the pc. We can improve the error message for these unsupported relocation to notice user that they should recompile their code with `fPIC`. bfd/ * elfnn-riscv.c (riscv_elf_relocate_section): Report the error message that user should recompile their code with `fPIC` when linking non-pic code into shared library. ld/ * testsuite/ld-riscv-elf/lib-nopic-01b.d: Update the error message. --- bfd/elfnn-riscv.c | 30 ++++++++++++++++++++++++------ ld/testsuite/ld-riscv-elf/lib-nopic-01b.d | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 1c2c871..d0d2dcc 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -2305,12 +2305,30 @@ riscv_elf_relocate_section (bfd *output_bfd, && _bfd_elf_section_offset (output_bfd, info, input_section, rel->r_offset) != (bfd_vma) -1) { - if (asprintf (&msg_buf, - _("%%X%%P: unresolvable %s relocation against " - "symbol `%s'\n"), - howto->name, - h->root.root.string) == -1) - msg_buf = NULL; + switch (r_type) + { + case R_RISCV_CALL: + 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; + } + msg = msg_buf; r = bfd_reloc_notsupported; } diff --git a/ld/testsuite/ld-riscv-elf/lib-nopic-01b.d b/ld/testsuite/ld-riscv-elf/lib-nopic-01b.d index 08c2890..0d758a4 100644 --- a/ld/testsuite/ld-riscv-elf/lib-nopic-01b.d +++ b/ld/testsuite/ld-riscv-elf/lib-nopic-01b.d @@ -2,4 +2,4 @@ #source: lib-nopic-01b.s #as: #ld: -shared tmpdir/lib-nopic-01a.so -#error: .*unresolvable R_RISCV_CALL relocation against symbol `func1' +#error: .*relocation R_RISCV_CALL against `func1' can not be used when making a shared object; recompile with -fPIC -- 2.7.4