blob: c68eb4a0c3c1926c1fadfe848311223d29f959ec [file] [log] [blame]
Ben Chengcc6695e2012-03-07 23:04:02 -08001/* Unaligned memory access functionality.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2001.
4
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
8
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18 Red Hat elfutils is an included package of the Open Invention Network.
19 An included package of the Open Invention Network is a package for which
20 Open Invention Network licensees cross-license their patents. No patent
21 license is granted, either expressly or impliedly, by designation as an
22 included package. Should you wish to participate in the Open Invention
23 Network licensing program, please visit www.openinventionnetwork.com
24 <http://www.openinventionnetwork.com>. */
25
26#ifndef _MEMORY_ACCESS_H
27#define _MEMORY_ACCESS_H 1
28
29#include <byteswap.h>
30#include <endian.h>
31#include <limits.h>
32#include <stdint.h>
33
34
35/* When loading this file we require the macro MACHINE_ENCODING to be
36 defined to signal the endianness of the architecture which is
37 defined. */
38#ifndef MACHINE_ENCODING
39# error "MACHINE_ENCODING needs to be defined"
40#endif
41#if MACHINE_ENCODING != __BIG_ENDIAN && MACHINE_ENCODING != __LITTLE_ENDIAN
42# error "MACHINE_ENCODING must signal either big or little endian"
43#endif
44
45
46/* We use simple memory access functions in case the hardware allows it.
47 The caller has to make sure we don't have alias problems. */
48#if ALLOW_UNALIGNED
49
50# define read_2ubyte_unaligned(Addr) \
51 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
52 ? bswap_16 (*((const uint16_t *) (Addr))) \
53 : *((const uint16_t *) (Addr)))
54# define read_2sbyte_unaligned(Addr) \
55 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
56 ? (int16_t) bswap_16 (*((const int16_t *) (Addr))) \
57 : *((const int16_t *) (Addr)))
58
59# define read_4ubyte_unaligned_noncvt(Addr) \
60 *((const uint32_t *) (Addr))
61# define read_4ubyte_unaligned(Addr) \
62 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
63 ? bswap_32 (*((const uint32_t *) (Addr))) \
64 : *((const uint32_t *) (Addr)))
65# define read_4sbyte_unaligned(Addr) \
66 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
67 ? (int32_t) bswap_32 (*((const int32_t *) (Addr))) \
68 : *((const int32_t *) (Addr)))
69
70# define read_8ubyte_unaligned(Addr) \
71 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
72 ? bswap_64 (*((const uint64_t *) (Addr))) \
73 : *((const uint64_t *) (Addr)))
74# define read_8sbyte_unaligned(Addr) \
75 (unlikely (MACHINE_ENCODING != __BYTE_ORDER) \
76 ? (int64_t) bswap_64 (*((const int64_t *) (Addr))) \
77 : *((const int64_t *) (Addr)))
78
79#else
80
81union unaligned
82 {
83 void *p;
84 uint16_t u2;
85 uint32_t u4;
86 uint64_t u8;
87 int16_t s2;
88 int32_t s4;
89 int64_t s8;
90 } __attribute__ ((packed));
91
92static inline uint16_t
93read_2ubyte_unaligned (const void *p)
94{
95 const union unaligned *up = p;
96 if (MACHINE_ENCODING != __BYTE_ORDER)
97 return bswap_16 (up->u2);
98 return up->u2;
99}
100static inline int16_t
101read_2sbyte_unaligned (const void *p)
102{
103 const union unaligned *up = p;
104 if (MACHINE_ENCODING != __BYTE_ORDER)
105 return (int16_t) bswap_16 (up->u2);
106 return up->s2;
107}
108
109static inline uint32_t
110read_4ubyte_unaligned_noncvt (const void *p)
111{
112 const union unaligned *up = p;
113 return up->u4;
114}
115static inline uint32_t
116read_4ubyte_unaligned (const void *p)
117{
118 const union unaligned *up = p;
119 if (MACHINE_ENCODING != __BYTE_ORDER)
120 return bswap_32 (up->u4);
121 return up->u4;
122}
123static inline int32_t
124read_4sbyte_unaligned (const void *p)
125{
126 const union unaligned *up = p;
127 if (MACHINE_ENCODING != __BYTE_ORDER)
128 return (int32_t) bswap_32 (up->u4);
129 return up->s4;
130}
131
132static inline uint64_t
133read_8ubyte_unaligned (const void *p)
134{
135 const union unaligned *up = p;
136 if (MACHINE_ENCODING != __BYTE_ORDER)
137 return bswap_64 (up->u8);
138 return up->u8;
139}
140static inline int64_t
141read_8sbyte_unaligned (const void *p)
142{
143 const union unaligned *up = p;
144 if (MACHINE_ENCODING != __BYTE_ORDER)
145 return (int64_t) bswap_64 (up->u8);
146 return up->s8;
147}
148
149#endif /* allow unaligned */
150
151
152#define read_2ubyte_unaligned_inc(Addr) \
153 ({ uint16_t t_ = read_2ubyte_unaligned (Addr); \
154 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
155 t_; })
156#define read_2sbyte_unaligned_inc(Addr) \
157 ({ int16_t t_ = read_2sbyte_unaligned (Addr); \
158 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
159 t_; })
160
161#define read_4ubyte_unaligned_inc(Addr) \
162 ({ uint32_t t_ = read_4ubyte_unaligned (Addr); \
163 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
164 t_; })
165#define read_4sbyte_unaligned_inc(Addr) \
166 ({ int32_t t_ = read_4sbyte_unaligned (Addr); \
167 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
168 t_; })
169
170#define read_8ubyte_unaligned_inc(Addr) \
171 ({ uint64_t t_ = read_8ubyte_unaligned (Addr); \
172 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
173 t_; })
174#define read_8sbyte_unaligned_inc(Addr) \
175 ({ int64_t t_ = read_8sbyte_unaligned (Addr); \
176 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
177 t_; })
178
179#endif /* memory-access.h */