The gelf_xlate FETCH/STORE macros are used to read/write values of a
specific size to a, possibly unaligned, pointer. If ALLOW_UNALIGNED is
defined they read/write the values directly, otherwise a packet union
is used. Replace both variants with a macro that uses memcpy the copy
the value in or out. This generates the same code.

        * libelf/gelf_xlate.c: Rewrite FETCH and STORE macros to one
        variant that uses memcpy.

Signed-off-by: Mark Wielaard <[email protected]>
---

V2 there was no V1, new patch in this series.

 libelf/gelf_xlate.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)

diff --git a/libelf/gelf_xlate.c b/libelf/gelf_xlate.c
index 749da1a17acf..2195b18555ce 100644
--- a/libelf/gelf_xlate.c
+++ b/libelf/gelf_xlate.c
@@ -62,24 +62,14 @@ static void
    functions referenced in the table will be aliases for one of these
    functions.  Which one is decided by the ELFxx_FSZ_type.  */
 
-#if ALLOW_UNALIGNED
+/* Use memcpy for the accesses so that the compiler takes care of
+   alignment.  */
 
-#define FETCH(Bits, ptr)       (*(const uint##Bits##_t *) ptr)
-#define STORE(Bits, ptr, val)  (*(uint##Bits##_t *) ptr = val)
+#define FETCH(Bits, ptr)                                                     \
+  ({ uint##Bits##_t vf_; memcpy (&vf_, (ptr), sizeof (vf_)); vf_; })
+#define STORE(Bits, ptr, val)                                                \
+  ({ uint##Bits##_t vs_ = (val); memcpy ((ptr), &vs_, sizeof (vs_)); vs_; })
 
-#else
-
-union unaligned
-  {
-    uint16_t u16;
-    uint32_t u32;
-    uint64_t u64;
-  } attribute_packed;
-
-#define FETCH(Bits, ptr)       (((const union unaligned *) ptr)->u##Bits)
-#define STORE(Bits, ptr, val)  (((union unaligned *) ptr)->u##Bits = val)
-
-#endif
 
 /* Now define the conversion functions for the basic types.  We use here
    the fact that file and memory types are the same and that we have the
-- 
2.56.1

Reply via email to