Hi Mark,

On Thu, Nov 26, 2026 at 8:58 PM Mark Wielaard <[email protected]> wrote:
>
> Replace the ALLOW_UNALIGNED and regular read unaligned functions with
> a simple memcpy variant. It generates the same code but doesn't rely
> on undefined behavior or packaged unions, which are a GCC extension.
>
>         * libdw/memory-access.h (read_[248][us]byte_unaligned_1):
>         Rewrite using memcpy.
>
> Signed-off-by: Mark Wielaard <[email protected]>

LGTM.

Aaron

> ---
>
> V2 no changes from V1.
>
>  libdw/memory-access.h | 104 +++++++++++-------------------------------
>  1 file changed, 26 insertions(+), 78 deletions(-)
>
> diff --git a/libdw/memory-access.h b/libdw/memory-access.h
> index 6d79343c9acc..7ced6d19d0e8 100644
> --- a/libdw/memory-access.h
> +++ b/libdw/memory-access.h
> @@ -1,5 +1,6 @@
>  /* Unaligned memory access functionality.
>     Copyright (C) 2000-2014, 2018 Red Hat, Inc.
> +   Copyright (C) 2026 Mark J. Wielaard <[email protected]>
>     This file is part of elfutils.
>
>     This file is free software; you can redistribute it and/or modify
> @@ -198,53 +199,8 @@ __libdw_get_sleb128_unchecked (const unsigned char 
> **addrp)
>  #define get_sleb128_unchecked(var, addr) ((var) = 
> __libdw_get_sleb128_unchecked (&(addr)))
>
>
> -/* We use simple memory access functions in case the hardware allows it.
> -   The caller has to make sure we don't have alias problems.  */
> -#if ALLOW_UNALIGNED
> -
> -# define read_2ubyte_unaligned(Dbg, Addr) \
> -  (unlikely ((Dbg)->other_byte_order)                                        
> \
> -   ? bswap_16 (*((const uint16_t *) (Addr)))                                 
> \
> -   : *((const uint16_t *) (Addr)))
> -# define read_2sbyte_unaligned(Dbg, Addr) \
> -  (unlikely ((Dbg)->other_byte_order)                                        
> \
> -   ? (int16_t) bswap_16 (*((const int16_t *) (Addr)))                        
> \
> -   : *((const int16_t *) (Addr)))
> -
> -# define read_4ubyte_unaligned_noncvt(Addr) \
> -   *((const uint32_t *) (Addr))
> -# define read_4ubyte_unaligned(Dbg, Addr) \
> -  (unlikely ((Dbg)->other_byte_order)                                        
> \
> -   ? bswap_32 (*((const uint32_t *) (Addr)))                                 
> \
> -   : *((const uint32_t *) (Addr)))
> -# define read_4sbyte_unaligned(Dbg, Addr) \
> -  (unlikely ((Dbg)->other_byte_order)                                        
> \
> -   ? (int32_t) bswap_32 (*((const int32_t *) (Addr)))                        
> \
> -   : *((const int32_t *) (Addr)))
> -
> -# define read_8ubyte_unaligned_noncvt(Addr) \
> -   *((const uint64_t *) (Addr))
> -# define read_8ubyte_unaligned(Dbg, Addr) \
> -  (unlikely ((Dbg)->other_byte_order)                                        
> \
> -   ? bswap_64 (*((const uint64_t *) (Addr)))                                 
> \
> -   : *((const uint64_t *) (Addr)))
> -# define read_8sbyte_unaligned(Dbg, Addr) \
> -  (unlikely ((Dbg)->other_byte_order)                                        
> \
> -   ? (int64_t) bswap_64 (*((const int64_t *) (Addr)))                        
> \
> -   : *((const int64_t *) (Addr)))
> -
> -#else
> -
> -union unaligned
> -  {
> -    void *p;
> -    uint16_t u2;
> -    uint32_t u4;
> -    uint64_t u8;
> -    int16_t s2;
> -    int32_t s4;
> -    int64_t s8;
> -  } attribute_packed;
> +/* We use memcpy to read the values so that the compiler takes care of
> +   alignment.  The caller has to make sure we don't have alias problems.  */
>
>  # define read_2ubyte_unaligned(Dbg, Addr) \
>    read_2ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
> @@ -262,69 +218,61 @@ union unaligned
>  static inline uint16_t
>  read_2ubyte_unaligned_1 (bool other_byte_order, const void *p)
>  {
> -  const union unaligned *up = p;
> -  if (unlikely (other_byte_order))
> -    return bswap_16 (up->u2);
> -  return up->u2;
> +  uint16_t v;
> +  memcpy (&v, p, sizeof v);
> +  return unlikely (other_byte_order) ? bswap_16 (v) : v;
>  }
> +
>  static inline int16_t
>  read_2sbyte_unaligned_1 (bool other_byte_order, const void *p)
>  {
> -  const union unaligned *up = p;
> -  if (unlikely (other_byte_order))
> -    return (int16_t) bswap_16 (up->u2);
> -  return up->s2;
> +  return (int16_t) read_2ubyte_unaligned_1 (other_byte_order, p);
>  }
>
>  static inline uint32_t
>  read_4ubyte_unaligned_noncvt (const void *p)
>  {
> -  const union unaligned *up = p;
> -  return up->u4;
> +  uint32_t v;
> +  memcpy (&v, p, sizeof v);
> +  return v;
>  }
> +
>  static inline uint32_t
>  read_4ubyte_unaligned_1 (bool other_byte_order, const void *p)
>  {
> -  const union unaligned *up = p;
> -  if (unlikely (other_byte_order))
> -    return bswap_32 (up->u4);
> -  return up->u4;
> +  uint32_t v;
> +  memcpy (&v, p, sizeof v);
> +  return unlikely (other_byte_order) ? bswap_32 (v) : v;
>  }
> +
>  static inline int32_t
>  read_4sbyte_unaligned_1 (bool other_byte_order, const void *p)
>  {
> -  const union unaligned *up = p;
> -  if (unlikely (other_byte_order))
> -    return (int32_t) bswap_32 (up->u4);
> -  return up->s4;
> +  return (int32_t) read_4ubyte_unaligned_1 (other_byte_order, p);
>  }
>
>  static inline uint64_t
>  read_8ubyte_unaligned_noncvt (const void *p)
>  {
> -  const union unaligned *up = p;
> -  return up->u8;
> +  uint64_t v;
> +  memcpy (&v, p, sizeof v);
> +  return v;
>  }
> +
>  static inline uint64_t
>  read_8ubyte_unaligned_1 (bool other_byte_order, const void *p)
>  {
> -  const union unaligned *up = p;
> -  if (unlikely (other_byte_order))
> -    return bswap_64 (up->u8);
> -  return up->u8;
> +  uint64_t v;
> +  memcpy (&v, p, sizeof v);
> +  return unlikely (other_byte_order) ? bswap_64 (v) : v;
>  }
> +
>  static inline int64_t
>  read_8sbyte_unaligned_1 (bool other_byte_order, const void *p)
>  {
> -  const union unaligned *up = p;
> -  if (unlikely (other_byte_order))
> -    return (int64_t) bswap_64 (up->u8);
> -  return up->s8;
> +  return (int64_t) read_8ubyte_unaligned_1 (other_byte_order, p);
>  }
>
> -#endif /* allow unaligned */
> -
> -
>  #define read_2ubyte_unaligned_inc(Dbg, Addr) \
>    ({ uint16_t t_ = read_2ubyte_unaligned (Dbg, Addr);                        
> \
>       Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2);                    
> \
> --
> 2.56.1
>

Reply via email to