1 /* Unaligned memory access functionality.
2 Copyright (C) 2000-2014, 2018 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
29 #ifndef _MEMORY_ACCESS_H
30 #define _MEMORY_ACCESS_H 1
37 /* Number decoding macros. See 7.6 Variable Length Data. */
39 #define len_leb128(var) ((8 * sizeof (var) + 6) / 7)
42 __libdw_max_len_leb128 (const size_t type_len,
43 const unsigned char *addr, const unsigned char *end)
45 const size_t pointer_len = likely (addr < end) ? end - addr : 0;
46 return likely (type_len <= pointer_len) ? type_len : pointer_len;
50 __libdw_max_len_uleb128 (const unsigned char *addr, const unsigned char *end)
52 const size_t type_len = len_leb128 (uint64_t);
53 return __libdw_max_len_leb128 (type_len, addr, end);
57 __libdw_max_len_sleb128 (const unsigned char *addr, const unsigned char *end)
59 /* Subtract one step, so we don't shift into sign bit. */
60 const size_t type_len = len_leb128 (int64_t) - 1;
61 return __libdw_max_len_leb128 (type_len, addr, end);
64 #define get_uleb128_step(var, addr, nth) \
66 unsigned char __b = *(addr)++; \
67 (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
68 if (likely ((__b & 0x80) == 0)) \
72 static inline uint64_t
73 __libdw_get_uleb128 (const unsigned char **addrp, const unsigned char *end)
77 /* Unroll the first step to help the compiler optimize
78 for the common single-byte case. */
79 get_uleb128_step (acc, *addrp, 0);
81 const size_t max = __libdw_max_len_uleb128 (*addrp - 1, end);
82 for (size_t i = 1; i < max; ++i)
83 get_uleb128_step (acc, *addrp, i);
84 /* Other implementations set VALUE to UINT_MAX in this
85 case. So we better do this as well. */
89 static inline uint64_t
90 __libdw_get_uleb128_unchecked (const unsigned char **addrp)
94 /* Unroll the first step to help the compiler optimize
95 for the common single-byte case. */
96 get_uleb128_step (acc, *addrp, 0);
98 const size_t max = len_leb128 (uint64_t);
99 for (size_t i = 1; i < max; ++i)
100 get_uleb128_step (acc, *addrp, i);
101 /* Other implementations set VALUE to UINT_MAX in this
102 case. So we better do this as well. */
106 /* Note, addr needs to me smaller than end. */
107 #define get_uleb128(var, addr, end) ((var) = __libdw_get_uleb128 (&(addr), end))
108 #define get_uleb128_unchecked(var, addr) ((var) = __libdw_get_uleb128_unchecked (&(addr)))
110 /* The signed case is similar, but we sign-extend the result. */
112 #define get_sleb128_step(var, addr, nth) \
114 unsigned char __b = *(addr)++; \
115 (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
116 if (likely ((__b & 0x80) == 0)) \
118 if ((__b & 0x40) != 0) \
119 (var) |= - ((typeof (var)) 1 << (((nth) + 1) * 7)); \
124 static inline int64_t
125 __libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
127 /* Do the work in an unsigned type, but use implementation-defined
128 behavior to cast to signed on return. This avoids some undefined
129 behavior when shifting. */
132 /* Unroll the first step to help the compiler optimize
133 for the common single-byte case. */
134 get_sleb128_step (acc, *addrp, 0);
136 const size_t max = __libdw_max_len_sleb128 (*addrp - 1, end);
137 for (size_t i = 1; i < max; ++i)
138 get_sleb128_step (acc, *addrp, i);
142 /* There might be one extra byte. */
143 unsigned char b = **addrp;
145 if (likely ((b & 0x80) == 0))
147 /* We only need the low bit of the final byte, and as it is the
148 sign bit, we don't need to do anything else here. */
149 acc |= ((typeof (acc)) b) << 7 * max;
153 /* Other implementations set VALUE to INT_MAX in this
154 case. So we better do this as well. */
158 static inline int64_t
159 __libdw_get_sleb128_unchecked (const unsigned char **addrp)
161 /* Do the work in an unsigned type, but use implementation-defined
162 behavior to cast to signed on return. This avoids some undefined
163 behavior when shifting. */
166 /* Unroll the first step to help the compiler optimize
167 for the common single-byte case. */
168 get_sleb128_step (acc, *addrp, 0);
170 /* Subtract one step, so we don't shift into sign bit. */
171 const size_t max = len_leb128 (int64_t) - 1;
172 for (size_t i = 1; i < max; ++i)
173 get_sleb128_step (acc, *addrp, i);
175 /* There might be one extra byte. */
176 unsigned char b = **addrp;
178 if (likely ((b & 0x80) == 0))
180 /* We only need the low bit of the final byte, and as it is the
181 sign bit, we don't need to do anything else here. */
182 acc |= ((typeof (acc)) b) << 7 * max;
186 /* Other implementations set VALUE to INT_MAX in this
187 case. So we better do this as well. */
191 #define get_sleb128(var, addr, end) ((var) = __libdw_get_sleb128 (&(addr), end))
192 #define get_sleb128_unchecked(var, addr) ((var) = __libdw_get_sleb128_unchecked (&(addr)))
195 /* We use simple memory access functions in case the hardware allows it.
196 The caller has to make sure we don't have alias problems. */
199 # define read_2ubyte_unaligned(Dbg, Addr) \
200 (unlikely ((Dbg)->other_byte_order) \
201 ? bswap_16 (*((const uint16_t *) (Addr))) \
202 : *((const uint16_t *) (Addr)))
203 # define read_2sbyte_unaligned(Dbg, Addr) \
204 (unlikely ((Dbg)->other_byte_order) \
205 ? (int16_t) bswap_16 (*((const int16_t *) (Addr))) \
206 : *((const int16_t *) (Addr)))
208 # define read_4ubyte_unaligned_noncvt(Addr) \
209 *((const uint32_t *) (Addr))
210 # define read_4ubyte_unaligned(Dbg, Addr) \
211 (unlikely ((Dbg)->other_byte_order) \
212 ? bswap_32 (*((const uint32_t *) (Addr))) \
213 : *((const uint32_t *) (Addr)))
214 # define read_4sbyte_unaligned(Dbg, Addr) \
215 (unlikely ((Dbg)->other_byte_order) \
216 ? (int32_t) bswap_32 (*((const int32_t *) (Addr))) \
217 : *((const int32_t *) (Addr)))
219 # define read_8ubyte_unaligned_noncvt(Addr) \
220 *((const uint64_t *) (Addr))
221 # define read_8ubyte_unaligned(Dbg, Addr) \
222 (unlikely ((Dbg)->other_byte_order) \
223 ? bswap_64 (*((const uint64_t *) (Addr))) \
224 : *((const uint64_t *) (Addr)))
225 # define read_8sbyte_unaligned(Dbg, Addr) \
226 (unlikely ((Dbg)->other_byte_order) \
227 ? (int64_t) bswap_64 (*((const int64_t *) (Addr))) \
228 : *((const int64_t *) (Addr)))
243 # define read_2ubyte_unaligned(Dbg, Addr) \
244 read_2ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
245 # define read_2sbyte_unaligned(Dbg, Addr) \
246 read_2sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
247 # define read_4ubyte_unaligned(Dbg, Addr) \
248 read_4ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
249 # define read_4sbyte_unaligned(Dbg, Addr) \
250 read_4sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
251 # define read_8ubyte_unaligned(Dbg, Addr) \
252 read_8ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
253 # define read_8sbyte_unaligned(Dbg, Addr) \
254 read_8sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
256 static inline uint16_t
257 read_2ubyte_unaligned_1 (bool other_byte_order, const void *p)
259 const union unaligned *up = p;
260 if (unlikely (other_byte_order))
261 return bswap_16 (up->u2);
264 static inline int16_t
265 read_2sbyte_unaligned_1 (bool other_byte_order, const void *p)
267 const union unaligned *up = p;
268 if (unlikely (other_byte_order))
269 return (int16_t) bswap_16 (up->u2);
273 static inline uint32_t
274 read_4ubyte_unaligned_noncvt (const void *p)
276 const union unaligned *up = p;
279 static inline uint32_t
280 read_4ubyte_unaligned_1 (bool other_byte_order, const void *p)
282 const union unaligned *up = p;
283 if (unlikely (other_byte_order))
284 return bswap_32 (up->u4);
287 static inline int32_t
288 read_4sbyte_unaligned_1 (bool other_byte_order, const void *p)
290 const union unaligned *up = p;
291 if (unlikely (other_byte_order))
292 return (int32_t) bswap_32 (up->u4);
296 static inline uint64_t
297 read_8ubyte_unaligned_noncvt (const void *p)
299 const union unaligned *up = p;
302 static inline uint64_t
303 read_8ubyte_unaligned_1 (bool other_byte_order, const void *p)
305 const union unaligned *up = p;
306 if (unlikely (other_byte_order))
307 return bswap_64 (up->u8);
310 static inline int64_t
311 read_8sbyte_unaligned_1 (bool other_byte_order, const void *p)
313 const union unaligned *up = p;
314 if (unlikely (other_byte_order))
315 return (int64_t) bswap_64 (up->u8);
319 #endif /* allow unaligned */
322 #define read_2ubyte_unaligned_inc(Dbg, Addr) \
323 ({ uint16_t t_ = read_2ubyte_unaligned (Dbg, Addr); \
324 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
326 #define read_2sbyte_unaligned_inc(Dbg, Addr) \
327 ({ int16_t t_ = read_2sbyte_unaligned (Dbg, Addr); \
328 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
331 #define read_4ubyte_unaligned_inc(Dbg, Addr) \
332 ({ uint32_t t_ = read_4ubyte_unaligned (Dbg, Addr); \
333 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
335 #define read_4sbyte_unaligned_inc(Dbg, Addr) \
336 ({ int32_t t_ = read_4sbyte_unaligned (Dbg, Addr); \
337 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
340 #define read_8ubyte_unaligned_inc(Dbg, Addr) \
341 ({ uint64_t t_ = read_8ubyte_unaligned (Dbg, Addr); \
342 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
344 #define read_8sbyte_unaligned_inc(Dbg, Addr) \
345 ({ int64_t t_ = read_8sbyte_unaligned (Dbg, Addr); \
346 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
349 /* 3ubyte reads are only used for DW_FORM_addrx3 and DW_FORM_strx3.
350 And are probably very rare. They are not optimized. They are
351 handled as if reading a 4byte value with the first (for big endian)
352 or last (for little endian) byte zero. */
355 file_byte_order (bool other_byte_order)
357 #if BYTE_ORDER == LITTLE_ENDIAN
358 return other_byte_order ? BIG_ENDIAN : LITTLE_ENDIAN;
360 return other_byte_order ? LITTLE_ENDIAN : BIG_ENDIAN;
364 static inline uint32_t
365 read_3ubyte_unaligned (Dwarf *dbg, const unsigned char *p)
372 bool other_byte_order = dbg->other_byte_order;
374 if (file_byte_order (other_byte_order) == BIG_ENDIAN)
389 if (other_byte_order)
390 return bswap_32 (d.u4);
396 #define read_3ubyte_unaligned_inc(Dbg, Addr) \
397 ({ uint32_t t_ = read_3ubyte_unaligned (Dbg, Addr); \
398 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 3); \
401 #define read_addr_unaligned_inc(Nbytes, Dbg, Addr) \
402 (assert ((Nbytes) == 4 || (Nbytes) == 8), \
403 ((Nbytes) == 4 ? read_4ubyte_unaligned_inc (Dbg, Addr) \
404 : read_8ubyte_unaligned_inc (Dbg, Addr)))
406 #endif /* memory-access.h */