1/* Unaligned memory access functionality.
2   Copyright (C) 2000-2014, 2018 Red Hat, Inc.
3   This file is part of elfutils.
4
5   This file is free software; you can redistribute it and/or modify
6   it under the terms of either
7
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
11
12   or
13
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
17
18   or both in parallel, as here.
19
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.
24
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/>.  */
28
29#ifndef _MEMORY_ACCESS_H
30#define _MEMORY_ACCESS_H 1
31
32#include <limits.h>
33#include <stdint.h>
34
35#include <system.h>
36
37/* Number decoding macros.  See 7.6 Variable Length Data.  */
38
39#define len_leb128(var) ((8 * sizeof (var) + 6) / 7)
40
41static inline size_t
42__libdw_max_len_leb128 (const size_t type_len,
43			const unsigned char *addr, const unsigned char *end)
44{
45  const size_t pointer_len = likely (addr < end) ? end - addr : 0;
46  return likely (type_len <= pointer_len) ? type_len : pointer_len;
47}
48
49static inline size_t
50__libdw_max_len_uleb128 (const unsigned char *addr, const unsigned char *end)
51{
52  const size_t type_len = len_leb128 (uint64_t);
53  return __libdw_max_len_leb128 (type_len, addr, end);
54}
55
56static inline size_t
57__libdw_max_len_sleb128 (const unsigned char *addr, const unsigned char *end)
58{
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);
62}
63
64#define get_uleb128_step(var, addr, nth)				      \
65  do {									      \
66    unsigned char __b = *(addr)++;					      \
67    (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7);		      \
68    if (likely ((__b & 0x80) == 0))					      \
69      return (var);							      \
70  } while (0)
71
72static inline uint64_t
73__libdw_get_uleb128 (const unsigned char **addrp, const unsigned char *end)
74{
75  uint64_t acc = 0;
76
77  /* Unroll the first step to help the compiler optimize
78     for the common single-byte case.  */
79  get_uleb128_step (acc, *addrp, 0);
80
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.  */
86  return UINT64_MAX;
87}
88
89static inline uint64_t
90__libdw_get_uleb128_unchecked (const unsigned char **addrp)
91{
92  uint64_t acc = 0;
93
94  /* Unroll the first step to help the compiler optimize
95     for the common single-byte case.  */
96  get_uleb128_step (acc, *addrp, 0);
97
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.  */
103  return UINT64_MAX;
104}
105
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)))
109
110/* The signed case is similar, but we sign-extend the result.  */
111
112#define get_sleb128_step(var, addr, nth)				      \
113  do {									      \
114    unsigned char __b = *(addr)++;					      \
115    (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7);		      \
116    if (likely ((__b & 0x80) == 0))					      \
117      {									      \
118	if ((__b & 0x40) != 0)						      \
119	  (var) |= - ((typeof (var)) 1 << (((nth) + 1) * 7));		      \
120	return (var);							      \
121      }									      \
122  } while (0)
123
124static inline int64_t
125__libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
126{
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.  */
130  uint64_t acc = 0;
131
132  /* Unroll the first step to help the compiler optimize
133     for the common single-byte case.  */
134  get_sleb128_step (acc, *addrp, 0);
135
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);
139  if (*addrp == end)
140    return INT64_MAX;
141
142  /* There might be one extra byte.  */
143  unsigned char b = **addrp;
144  ++*addrp;
145  if (likely ((b & 0x80) == 0))
146    {
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;
150      return acc;
151    }
152
153  /* Other implementations set VALUE to INT_MAX in this
154     case.  So we better do this as well.  */
155  return INT64_MAX;
156}
157
158static inline int64_t
159__libdw_get_sleb128_unchecked (const unsigned char **addrp)
160{
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.  */
164  uint64_t acc = 0;
165
166  /* Unroll the first step to help the compiler optimize
167     for the common single-byte case.  */
168  get_sleb128_step (acc, *addrp, 0);
169
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);
174
175  /* There might be one extra byte.  */
176  unsigned char b = **addrp;
177  ++*addrp;
178  if (likely ((b & 0x80) == 0))
179    {
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;
183      return acc;
184    }
185
186  /* Other implementations set VALUE to INT_MAX in this
187     case.  So we better do this as well.  */
188  return INT64_MAX;
189}
190
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)))
193
194
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.  */
197#if ALLOW_UNALIGNED
198
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)))
207
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)))
218
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)))
229
230#else
231
232union unaligned
233  {
234    void *p;
235    uint16_t u2;
236    uint32_t u4;
237    uint64_t u8;
238    int16_t s2;
239    int32_t s4;
240    int64_t s8;
241  } attribute_packed;
242
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))
255
256static inline uint16_t
257read_2ubyte_unaligned_1 (bool other_byte_order, const void *p)
258{
259  const union unaligned *up = p;
260  if (unlikely (other_byte_order))
261    return bswap_16 (up->u2);
262  return up->u2;
263}
264static inline int16_t
265read_2sbyte_unaligned_1 (bool other_byte_order, const void *p)
266{
267  const union unaligned *up = p;
268  if (unlikely (other_byte_order))
269    return (int16_t) bswap_16 (up->u2);
270  return up->s2;
271}
272
273static inline uint32_t
274read_4ubyte_unaligned_noncvt (const void *p)
275{
276  const union unaligned *up = p;
277  return up->u4;
278}
279static inline uint32_t
280read_4ubyte_unaligned_1 (bool other_byte_order, const void *p)
281{
282  const union unaligned *up = p;
283  if (unlikely (other_byte_order))
284    return bswap_32 (up->u4);
285  return up->u4;
286}
287static inline int32_t
288read_4sbyte_unaligned_1 (bool other_byte_order, const void *p)
289{
290  const union unaligned *up = p;
291  if (unlikely (other_byte_order))
292    return (int32_t) bswap_32 (up->u4);
293  return up->s4;
294}
295
296static inline uint64_t
297read_8ubyte_unaligned_noncvt (const void *p)
298{
299  const union unaligned *up = p;
300  return up->u8;
301}
302static inline uint64_t
303read_8ubyte_unaligned_1 (bool other_byte_order, const void *p)
304{
305  const union unaligned *up = p;
306  if (unlikely (other_byte_order))
307    return bswap_64 (up->u8);
308  return up->u8;
309}
310static inline int64_t
311read_8sbyte_unaligned_1 (bool other_byte_order, const void *p)
312{
313  const union unaligned *up = p;
314  if (unlikely (other_byte_order))
315    return (int64_t) bswap_64 (up->u8);
316  return up->s8;
317}
318
319#endif	/* allow unaligned */
320
321
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);		      \
325     t_; })
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);		      \
329     t_; })
330
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);		      \
334     t_; })
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);		      \
338     t_; })
339
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);		      \
343     t_; })
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);		      \
347     t_; })
348
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.  */
353
354static inline int
355file_byte_order (bool other_byte_order)
356{
357#if BYTE_ORDER == LITTLE_ENDIAN
358  return other_byte_order ? BIG_ENDIAN : LITTLE_ENDIAN;
359#else
360  return other_byte_order ? LITTLE_ENDIAN : BIG_ENDIAN;
361#endif
362}
363
364static inline uint32_t
365read_3ubyte_unaligned (Dwarf *dbg, const unsigned char *p)
366{
367  union
368  {
369    uint32_t u4;
370    unsigned char c[4];
371  } d;
372  bool other_byte_order = dbg->other_byte_order;
373
374  if (file_byte_order (other_byte_order) == BIG_ENDIAN)
375    {
376      d.c[0] = 0x00;
377      d.c[1] = p[0];
378      d.c[2] = p[1];
379      d.c[3] = p[2];
380    }
381  else
382    {
383      d.c[0] = p[0];
384      d.c[1] = p[1];
385      d.c[2] = p[2];
386      d.c[3] = 0x00;
387    }
388
389  if (other_byte_order)
390    return bswap_32 (d.u4);
391  else
392    return d.u4;
393}
394
395
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);		      \
399     t_; })
400
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)))
405
406#endif	/* memory-access.h */
407