public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Segment fault in riscv_elf_append_rela.
@ 2023-03-09  8:04 Nelson Chu
  2023-03-09  8:33 ` Alan Modra
  0 siblings, 1 reply; 5+ messages in thread
From: Nelson Chu @ 2023-03-09  8:04 UTC (permalink / raw)
  To: binutils; +Cc: nelson

I seem to remember someone had sent a pacth for this before, but I cannot
find where it is for now.  So I just send it by myself.

======

% cat tmp.s
foo:
	lui	a0, %hi(_end)     # R_RISCV_HI20
	addi	a0, a0, %lo(_end) # R_RISCV_LO12
	.8byte foo                # R_RISCV_64
% riscv64-unknown-linux-gnu-as tmp.s -o tmp.o
% riscv64-unknown-linux-gnu-ld -shared tmp.o
.* tmp.o: relocation R_RISCV_HI20 against `_end' can not be used when making a shared object; recompile with -fPIC
zsh: segmentation fault

I accidently meet this segment fault from the above case.  Since we don't
allow the absolute access (R_RISCV_HI20) when building shared object, the
riscv_elf_check_relocs should return false directly when analyzing the lui,
so there won't have rel.dyn section.  But linker still try to emit the
dynamic relocation for R_RISCV_64 in the riscv_elf_relocate_section, which
cause the segmant fault in the riscv_elf_append_rela.  Refer to other targets,
Loongarch use BFD_ASSERT, and AARCH64 call abort(), to check if the dynamic
section exsits before emiting.  Since BFD_ASSERT still meet the segmant fault,
I think just call abort here is a better chosen.

bfd/
    * elfnn-riscv.c (riscv_elf_append_rela): Abort when the dynamic section
    doesn't exist.
---
 bfd/elfnn-riscv.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 1200e6b11b5..36cdd629aa2 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -182,6 +182,9 @@ riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
   const struct elf_backend_data *bed;
   bfd_byte *loc;
 
+  if (!s || !s->contents)
+    abort ();
+
   bed = get_elf_backend_data (abfd);
   loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
   bed->s->swap_reloca_out (abfd, rel, loc);
-- 
2.37.1 (Apple Git-137.1)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-10 10:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-09  8:04 [PATCH] RISC-V: Segment fault in riscv_elf_append_rela Nelson Chu
2023-03-09  8:33 ` Alan Modra
2023-03-09 11:41   ` Nelson Chu
2023-03-09 12:59     ` Alan Modra
2023-03-10 10:06       ` Nelson Chu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).