1 /* Unaligned memory access functionality.
2 Copyright (C) 2000-2014 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2001.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
30 #ifndef _MEMORY_ACCESS_H
31 #define _MEMORY_ACCESS_H 1
38 /* Number decoding macros. See 7.6 Variable Length Data. */
40 #define len_leb128(var) ((8 * sizeof (var) + 6) / 7)
43 __libdw_max_len_leb128 (const size_t type_len,
44 const unsigned char *addr, const unsigned char *end)
46 const size_t pointer_len = likely (addr < end) ? end - addr : 0;
47 return likely (type_len <= pointer_len) ? type_len : pointer_len;
51 __libdw_max_len_uleb128 (const unsigned char *addr, const unsigned char *end)
53 const size_t type_len = len_leb128 (uint64_t);
54 return __libdw_max_len_leb128 (type_len, addr, end);
58 __libdw_max_len_sleb128 (const unsigned char *addr, const unsigned char *end)
60 /* Subtract one step, so we don't shift into sign bit. */
61 const size_t type_len = len_leb128 (int64_t) - 1;
62 return __libdw_max_len_leb128 (type_len, addr, end);
65 #define get_uleb128_step(var, addr, nth) \
67 unsigned char __b = *(addr)++; \
68 (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
69 if (likely ((__b & 0x80) == 0)) \
73 static inline uint64_t
74 __libdw_get_uleb128 (const unsigned char **addrp, const unsigned char *end)
78 /* Unroll the first step to help the compiler optimize
79 for the common single-byte case. */
80 get_uleb128_step (acc, *addrp, 0);
82 const size_t max = __libdw_max_len_uleb128 (*addrp - 1, end);
83 for (size_t i = 1; i < max; ++i)
84 get_uleb128_step (acc, *addrp, i);
85 /* Other implementations set VALUE to UINT_MAX in this
86 case. So we better do this as well. */
90 /* Note, addr needs to me smaller than end. */
91 #define get_uleb128(var, addr, end) ((var) = __libdw_get_uleb128 (&(addr), end))
93 /* The signed case is similar, but we sign-extend the result. */
95 #define get_sleb128_step(var, addr, nth) \
97 unsigned char __b = *(addr)++; \
98 if (likely ((__b & 0x80) == 0)) \
100 struct { signed int i:7; } __s = { .i = __b }; \
101 (var) |= (typeof (var)) __s.i * ((typeof (var)) 1 << ((nth) * 7)); \
104 (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
107 static inline int64_t
108 __libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
112 /* Unroll the first step to help the compiler optimize
113 for the common single-byte case. */
114 get_sleb128_step (acc, *addrp, 0);
116 const size_t max = __libdw_max_len_sleb128 (*addrp - 1, end);
117 for (size_t i = 1; i < max; ++i)
118 get_sleb128_step (acc, *addrp, i);
119 /* Other implementations set VALUE to INT_MAX in this
120 case. So we better do this as well. */
124 #define get_sleb128(var, addr, end) ((var) = __libdw_get_sleb128 (&(addr), end))
127 /* We use simple memory access functions in case the hardware allows it.
128 The caller has to make sure we don't have alias problems. */
131 # define read_2ubyte_unaligned(Dbg, Addr) \
132 (unlikely ((Dbg)->other_byte_order) \
133 ? bswap_16 (*((const uint16_t *) (Addr))) \
134 : *((const uint16_t *) (Addr)))
135 # define read_2sbyte_unaligned(Dbg, Addr) \
136 (unlikely ((Dbg)->other_byte_order) \
137 ? (int16_t) bswap_16 (*((const int16_t *) (Addr))) \
138 : *((const int16_t *) (Addr)))
140 # define read_4ubyte_unaligned_noncvt(Addr) \
141 *((const uint32_t *) (Addr))
142 # define read_4ubyte_unaligned(Dbg, Addr) \
143 (unlikely ((Dbg)->other_byte_order) \
144 ? bswap_32 (*((const uint32_t *) (Addr))) \
145 : *((const uint32_t *) (Addr)))
146 # define read_4sbyte_unaligned(Dbg, Addr) \
147 (unlikely ((Dbg)->other_byte_order) \
148 ? (int32_t) bswap_32 (*((const int32_t *) (Addr))) \
149 : *((const int32_t *) (Addr)))
151 # define read_8ubyte_unaligned_noncvt(Addr) \
152 *((const uint64_t *) (Addr))
153 # define read_8ubyte_unaligned(Dbg, Addr) \
154 (unlikely ((Dbg)->other_byte_order) \
155 ? bswap_64 (*((const uint64_t *) (Addr))) \
156 : *((const uint64_t *) (Addr)))
157 # define read_8sbyte_unaligned(Dbg, Addr) \
158 (unlikely ((Dbg)->other_byte_order) \
159 ? (int64_t) bswap_64 (*((const int64_t *) (Addr))) \
160 : *((const int64_t *) (Addr)))
173 } __attribute__ ((packed));
175 # define read_2ubyte_unaligned(Dbg, Addr) \
176 read_2ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
177 # define read_2sbyte_unaligned(Dbg, Addr) \
178 read_2sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
179 # define read_4ubyte_unaligned(Dbg, Addr) \
180 read_4ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
181 # define read_4sbyte_unaligned(Dbg, Addr) \
182 read_4sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
183 # define read_8ubyte_unaligned(Dbg, Addr) \
184 read_8ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
185 # define read_8sbyte_unaligned(Dbg, Addr) \
186 read_8sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
188 static inline uint16_t
189 read_2ubyte_unaligned_1 (bool other_byte_order, const void *p)
191 const union unaligned *up = p;
192 if (unlikely (other_byte_order))
193 return bswap_16 (up->u2);
196 static inline int16_t
197 read_2sbyte_unaligned_1 (bool other_byte_order, const void *p)
199 const union unaligned *up = p;
200 if (unlikely (other_byte_order))
201 return (int16_t) bswap_16 (up->u2);
205 static inline uint32_t
206 read_4ubyte_unaligned_noncvt (const void *p)
208 const union unaligned *up = p;
211 static inline uint32_t
212 read_4ubyte_unaligned_1 (bool other_byte_order, const void *p)
214 const union unaligned *up = p;
215 if (unlikely (other_byte_order))
216 return bswap_32 (up->u4);
219 static inline int32_t
220 read_4sbyte_unaligned_1 (bool other_byte_order, const void *p)
222 const union unaligned *up = p;
223 if (unlikely (other_byte_order))
224 return (int32_t) bswap_32 (up->u4);
228 static inline uint64_t
229 read_8ubyte_unaligned_noncvt (const void *p)
231 const union unaligned *up = p;
234 static inline uint64_t
235 read_8ubyte_unaligned_1 (bool other_byte_order, const void *p)
237 const union unaligned *up = p;
238 if (unlikely (other_byte_order))
239 return bswap_64 (up->u8);
242 static inline int64_t
243 read_8sbyte_unaligned_1 (bool other_byte_order, const void *p)
245 const union unaligned *up = p;
246 if (unlikely (other_byte_order))
247 return (int64_t) bswap_64 (up->u8);
251 #endif /* allow unaligned */
254 #define read_2ubyte_unaligned_inc(Dbg, Addr) \
255 ({ uint16_t t_ = read_2ubyte_unaligned (Dbg, Addr); \
256 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
258 #define read_2sbyte_unaligned_inc(Dbg, Addr) \
259 ({ int16_t t_ = read_2sbyte_unaligned (Dbg, Addr); \
260 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
263 #define read_4ubyte_unaligned_inc(Dbg, Addr) \
264 ({ uint32_t t_ = read_4ubyte_unaligned (Dbg, Addr); \
265 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
267 #define read_4sbyte_unaligned_inc(Dbg, Addr) \
268 ({ int32_t t_ = read_4sbyte_unaligned (Dbg, Addr); \
269 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
272 #define read_8ubyte_unaligned_inc(Dbg, Addr) \
273 ({ uint64_t t_ = read_8ubyte_unaligned (Dbg, Addr); \
274 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
276 #define read_8sbyte_unaligned_inc(Dbg, Addr) \
277 ({ int64_t t_ = read_8sbyte_unaligned (Dbg, Addr); \
278 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
282 #define read_addr_unaligned_inc(Nbytes, Dbg, Addr) \
283 (assert ((Nbytes) == 4 || (Nbytes) == 8), \
284 ((Nbytes) == 4 ? read_4ubyte_unaligned_inc (Dbg, Addr) \
285 : read_8ubyte_unaligned_inc (Dbg, Addr)))
287 #endif /* memory-access.h */