1 /* Unaligned memory access functionality.
2 Copyright (C) 2000, 2001, 2002, 2003, 2008 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2001.
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 Red Hat elfutils is an included package of the Open Invention Network.
20 An included package of the Open Invention Network is a package for which
21 Open Invention Network licensees cross-license their patents. No patent
22 license is granted, either expressly or impliedly, by designation as an
23 included package. Should you wish to participate in the Open Invention
24 Network licensing program, please visit www.openinventionnetwork.com
25 <http://www.openinventionnetwork.com>. */
28 #define _UNALIGNED_H 1
34 #ifndef UNALIGNED_ACCESS_CLASS
35 # error "UNALIGNED_ACCESS_CLASS must be defined"
39 /* Macros to convert from the host byte order to that of the object file. */
40 #if UNALIGNED_ACCESS_CLASS == BYTE_ORDER
41 # define target_bswap_16(n) (n)
42 # define target_bswap_32(n) (n)
43 # define target_bswap_64(n) (n)
45 # define target_bswap_16(n) bswap_16 (n)
46 # define target_bswap_32(n) bswap_32 (n)
47 # define target_bswap_64(n) bswap_64 (n)
51 union u_2ubyte_unaligned
55 } __attribute__((packed));
57 union u_4ubyte_unaligned
61 } __attribute__((packed));
63 union u_8ubyte_unaligned
67 } __attribute__((packed));
70 /* Macros to store value at unaligned address. */
71 #define store_2ubyte_unaligned(ptr, value) \
72 (void) (((union u_2ubyte_unaligned *) (ptr))->u = target_bswap_16 (value))
73 #define store_4ubyte_unaligned(ptr, value) \
74 (void) (((union u_4ubyte_unaligned *) (ptr))->u = target_bswap_32 (value))
75 #define store_8ubyte_unaligned(ptr, value) \
76 (void) (((union u_8ubyte_unaligned *) (ptr))->u = target_bswap_64 (value))
79 /* Macros to add value to unaligned address. This is a bit more
80 complicated since the value must be read from memory and eventually
82 #if UNALIGNED_ACCESS_CLASS == BYTE_ORDER
83 # define add_2ubyte_unaligned(ptr, value) \
84 (void) (((union u_2ubyte_unaligned *) (ptr))->u += value)
85 # define add_4ubyte_unaligned(ptr, value) \
86 (void) (((union u_4ubyte_unaligned *) (ptr))->u += value)
87 # define add_8ubyte_unaligned(ptr, value) \
88 (void) (((union u_8ubyte_unaligned *) (ptr))->u += value)
90 # define add_2ubyte_unaligned(ptr, value) \
92 union u_2ubyte_unaligned *_ptr = (void *) (ptr); \
93 uint16_t _val = bswap_16 (_ptr->u) + (value); \
94 _ptr->u = bswap_16 (_val); \
96 # define add_4ubyte_unaligned(ptr, value) \
98 union u_4ubyte_unaligned *_ptr = (void *) (ptr); \
99 uint32_t _val = bswap_32 (_ptr->u) + (value); \
100 _ptr->u = bswap_32 (_val); \
102 # define add_8ubyte_unaligned(ptr, value) \
104 union u_8ubyte_unaligned *_ptr = (void *) (ptr); \
105 uint64_t _val = bswap_64 (_ptr->u) + (value); \
106 _ptr->u = bswap_64 (_val); \
110 #endif /* unaligned.h */