public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] riscv: Use memcpy to handle unaligned access when fixing R_RISCV_RELATIVE
@ 2022-06-28 13:52 Kito Cheng
  2022-06-30 15:06 ` Palmer Dabbelt
  0 siblings, 1 reply; 2+ messages in thread
From: Kito Cheng @ 2022-06-28 13:52 UTC (permalink / raw)
  To: libc-alpha, kito.cheng, palmer, darius, adhemerval.zanella
  Cc: Kito Cheng, Palmer Dabbelt

Although RISC-V Linux will enable the unaligned memory access handler by
default, that is quite expensive in general, using memcpy will be much cheaper
- just break down that into several load/store byte instructions.

ARM and MIPS has similar issue:

ARM: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51456
MIPS: https://gcc.gnu.org/legacy-ml/gcc-help/2005-07/msg00325.html

Acked-by: Palmer Dabbelt <palmer@rivosinc.com>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

---

V2 Changes:

- Fix minor typo
---
 sysdeps/riscv/dl-machine.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
index 4bb858adaa..dfedc9801d 100644
--- a/sysdeps/riscv/dl-machine.h
+++ b/sysdeps/riscv/dl-machine.h
@@ -157,7 +157,10 @@ __attribute__ ((always_inline))
 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
 			  void *const reloc_addr)
 {
-  *(ElfW(Addr) *) reloc_addr = l_addr + reloc->r_addend;
+  /* R_RISCV_RELATIVE might located in debug info section which might not
+     aligned to XLEN bytes.  Also support relocations on unaligned offsets.  */
+  ElfW(Addr) value = l_addr + reloc->r_addend;
+  memcpy (reloc_addr, &value, sizeof value);
 }
 
 /* Perform a relocation described by R_INFO at the location pointed to
-- 
2.34.0


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

* Re: [PATCH v2] riscv: Use memcpy to handle unaligned access when fixing R_RISCV_RELATIVE
  2022-06-28 13:52 [PATCH v2] riscv: Use memcpy to handle unaligned access when fixing R_RISCV_RELATIVE Kito Cheng
@ 2022-06-30 15:06 ` Palmer Dabbelt
  0 siblings, 0 replies; 2+ messages in thread
From: Palmer Dabbelt @ 2022-06-30 15:06 UTC (permalink / raw)
  To: kito.cheng
  Cc: libc-alpha, Kito Cheng, Darius Rad, adhemerval.zanella, kito.cheng

On Tue, 28 Jun 2022 06:52:19 PDT (-0700), kito.cheng@sifive.com wrote:
> Although RISC-V Linux will enable the unaligned memory access handler by
> default, that is quite expensive in general, using memcpy will be much cheaper
> - just break down that into several load/store byte instructions.
>
> ARM and MIPS has similar issue:
>
> ARM: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51456
> MIPS: https://gcc.gnu.org/legacy-ml/gcc-help/2005-07/msg00325.html
>
> Acked-by: Palmer Dabbelt <palmer@rivosinc.com>
> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
>
> ---
>
> V2 Changes:
>
> - Fix minor typo
> ---
>  sysdeps/riscv/dl-machine.h | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
> index 4bb858adaa..dfedc9801d 100644
> --- a/sysdeps/riscv/dl-machine.h
> +++ b/sysdeps/riscv/dl-machine.h
> @@ -157,7 +157,10 @@ __attribute__ ((always_inline))
>  elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
>  			  void *const reloc_addr)
>  {
> -  *(ElfW(Addr) *) reloc_addr = l_addr + reloc->r_addend;
> +  /* R_RISCV_RELATIVE might located in debug info section which might not
> +     aligned to XLEN bytes.  Also support relocations on unaligned offsets.  */
> +  ElfW(Addr) value = l_addr + reloc->r_addend;
> +  memcpy (reloc_addr, &value, sizeof value);
>  }
>
>  /* Perform a relocation described by R_INFO at the location pointed to

Committed.  Thanks!

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

end of thread, other threads:[~2022-06-30 15:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-28 13:52 [PATCH v2] riscv: Use memcpy to handle unaligned access when fixing R_RISCV_RELATIVE Kito Cheng
2022-06-30 15:06 ` Palmer Dabbelt

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).