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
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 static inline uint64_t
91 __libdw_get_uleb128_unchecked (const unsigned char **addrp)
95 /* Unroll the first step to help the compiler optimize
96 for the common single-byte case. */
97 get_uleb128_step (acc, *addrp, 0);
99 const size_t max = len_leb128 (uint64_t);
100 for (size_t i = 1; i < max; ++i)
101 get_uleb128_step (acc, *addrp, i);
102 /* Other implementations set VALUE to UINT_MAX in this
103 case. So we better do this as well. */
107 /* Note, addr needs to me smaller than end. */
108 #define get_uleb128(var, addr, end) ((var) = __libdw_get_uleb128 (&(addr), end))
109 #define get_uleb128_unchecked(var, addr) ((var) = __libdw_get_uleb128_unchecked (&(addr)))
111 /* The signed case is similar, but we sign-extend the result. */
113 #define get_sleb128_step(var, addr, nth) \
115 unsigned char __b = *(addr)++; \
116 if (likely ((__b & 0x80) == 0)) \
118 struct { signed int i:7; } __s = { .i = __b }; \
119 (var) |= (typeof (var)) __s.i * ((typeof (var)) 1 << ((nth) * 7)); \
122 (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
125 static inline int64_t
126 __libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
130 /* Unroll the first step to help the compiler optimize
131 for the common single-byte case. */
132 get_sleb128_step (acc, *addrp, 0);
134 const size_t max = __libdw_max_len_sleb128 (*addrp - 1, end);
135 for (size_t i = 1; i < max; ++i)
136 get_sleb128_step (acc, *addrp, i);
137 /* Other implementations set VALUE to INT_MAX in this
138 case. So we better do this as well. */
142 static inline int64_t
143 __libdw_get_sleb128_unchecked (const unsigned char **addrp)
147 /* Unroll the first step to help the compiler optimize
148 for the common single-byte case. */
149 get_sleb128_step (acc, *addrp, 0);
151 /* Subtract one step, so we don't shift into sign bit. */
152 const size_t max = len_leb128 (int64_t) - 1;
153 for (size_t i = 1; i < max; ++i)
154 get_sleb128_step (acc, *addrp, i);
155 /* Other implementations set VALUE to INT_MAX in this
156 case. So we better do this as well. */
160 #define get_sleb128(var, addr, end) ((var) = __libdw_get_sleb128 (&(addr), end))
161 #define get_sleb128_unchecked(var, addr) ((var) = __libdw_get_sleb128_unchecked (&(addr)))
164 /* We use simple memory access functions in case the hardware allows it.
165 The caller has to make sure we don't have alias problems. */
168 # define read_2ubyte_unaligned(Dbg, Addr) \
169 (unlikely ((Dbg)->other_byte_order) \
170 ? bswap_16 (*((const uint16_t *) (Addr))) \
171 : *((const uint16_t *) (Addr)))
172 # define read_2sbyte_unaligned(Dbg, Addr) \
173 (unlikely ((Dbg)->other_byte_order) \
174 ? (int16_t) bswap_16 (*((const int16_t *) (Addr))) \
175 : *((const int16_t *) (Addr)))
177 # define read_4ubyte_unaligned_noncvt(Addr) \
178 *((const uint32_t *) (Addr))
179 # define read_4ubyte_unaligned(Dbg, Addr) \
180 (unlikely ((Dbg)->other_byte_order) \
181 ? bswap_32 (*((const uint32_t *) (Addr))) \
182 : *((const uint32_t *) (Addr)))
183 # define read_4sbyte_unaligned(Dbg, Addr) \
184 (unlikely ((Dbg)->other_byte_order) \
185 ? (int32_t) bswap_32 (*((const int32_t *) (Addr))) \
186 : *((const int32_t *) (Addr)))
188 # define read_8ubyte_unaligned_noncvt(Addr) \
189 *((const uint64_t *) (Addr))
190 # define read_8ubyte_unaligned(Dbg, Addr) \
191 (unlikely ((Dbg)->other_byte_order) \
192 ? bswap_64 (*((const uint64_t *) (Addr))) \
193 : *((const uint64_t *) (Addr)))
194 # define read_8sbyte_unaligned(Dbg, Addr) \
195 (unlikely ((Dbg)->other_byte_order) \
196 ? (int64_t) bswap_64 (*((const int64_t *) (Addr))) \
197 : *((const int64_t *) (Addr)))
212 # define read_2ubyte_unaligned(Dbg, Addr) \
213 read_2ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
214 # define read_2sbyte_unaligned(Dbg, Addr) \
215 read_2sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
216 # define read_4ubyte_unaligned(Dbg, Addr) \
217 read_4ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
218 # define read_4sbyte_unaligned(Dbg, Addr) \
219 read_4sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
220 # define read_8ubyte_unaligned(Dbg, Addr) \
221 read_8ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
222 # define read_8sbyte_unaligned(Dbg, Addr) \
223 read_8sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
225 static inline uint16_t
226 read_2ubyte_unaligned_1 (bool other_byte_order, const void *p)
228 const union unaligned *up = p;
229 if (unlikely (other_byte_order))
230 return bswap_16 (up->u2);
233 static inline int16_t
234 read_2sbyte_unaligned_1 (bool other_byte_order, const void *p)
236 const union unaligned *up = p;
237 if (unlikely (other_byte_order))
238 return (int16_t) bswap_16 (up->u2);
242 static inline uint32_t
243 read_4ubyte_unaligned_noncvt (const void *p)
245 const union unaligned *up = p;
248 static inline uint32_t
249 read_4ubyte_unaligned_1 (bool other_byte_order, const void *p)
251 const union unaligned *up = p;
252 if (unlikely (other_byte_order))
253 return bswap_32 (up->u4);
256 static inline int32_t
257 read_4sbyte_unaligned_1 (bool other_byte_order, const void *p)
259 const union unaligned *up = p;
260 if (unlikely (other_byte_order))
261 return (int32_t) bswap_32 (up->u4);
265 static inline uint64_t
266 read_8ubyte_unaligned_noncvt (const void *p)
268 const union unaligned *up = p;
271 static inline uint64_t
272 read_8ubyte_unaligned_1 (bool other_byte_order, const void *p)
274 const union unaligned *up = p;
275 if (unlikely (other_byte_order))
276 return bswap_64 (up->u8);
279 static inline int64_t
280 read_8sbyte_unaligned_1 (bool other_byte_order, const void *p)
282 const union unaligned *up = p;
283 if (unlikely (other_byte_order))
284 return (int64_t) bswap_64 (up->u8);
288 #endif /* allow unaligned */
291 #define read_2ubyte_unaligned_inc(Dbg, Addr) \
292 ({ uint16_t t_ = read_2ubyte_unaligned (Dbg, Addr); \
293 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
295 #define read_2sbyte_unaligned_inc(Dbg, Addr) \
296 ({ int16_t t_ = read_2sbyte_unaligned (Dbg, Addr); \
297 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \
300 #define read_4ubyte_unaligned_inc(Dbg, Addr) \
301 ({ uint32_t t_ = read_4ubyte_unaligned (Dbg, Addr); \
302 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
304 #define read_4sbyte_unaligned_inc(Dbg, Addr) \
305 ({ int32_t t_ = read_4sbyte_unaligned (Dbg, Addr); \
306 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \
309 #define read_8ubyte_unaligned_inc(Dbg, Addr) \
310 ({ uint64_t t_ = read_8ubyte_unaligned (Dbg, Addr); \
311 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
313 #define read_8sbyte_unaligned_inc(Dbg, Addr) \
314 ({ int64_t t_ = read_8sbyte_unaligned (Dbg, Addr); \
315 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \
318 /* 3ubyte reads are only used for DW_FORM_addrx3 and DW_FORM_strx3.
319 And are probably very rare. They are not optimized. They are
320 handled as if reading a 4byte value with the first (for big endian)
321 or last (for little endian) byte zero. */
324 file_byte_order (bool other_byte_order)
326 #if __BYTE_ORDER == __LITTLE_ENDIAN
327 return other_byte_order ? __BIG_ENDIAN : __LITTLE_ENDIAN;
329 return other_byte_order ? __LITTLE_ENDIAN : __BIG_ENDIAN;
333 static inline uint32_t
334 read_3ubyte_unaligned (Dwarf *dbg, const unsigned char *p)
341 bool other_byte_order = dbg->other_byte_order;
343 if (file_byte_order (other_byte_order) == __BIG_ENDIAN)
358 if (other_byte_order)
359 return bswap_32 (d.u4);
365 #define read_3ubyte_unaligned_inc(Dbg, Addr) \
366 ({ uint32_t t_ = read_2ubyte_unaligned (Dbg, Addr); \
367 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 3); \
370 #define read_addr_unaligned_inc(Nbytes, Dbg, Addr) \
371 (assert ((Nbytes) == 4 || (Nbytes) == 8), \
372 ((Nbytes) == 4 ? read_4ubyte_unaligned_inc (Dbg, Addr) \
373 : read_8ubyte_unaligned_inc (Dbg, Addr)))
375 #endif /* memory-access.h */