Home | History | Annotate | Download | only in libdw
      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 (at) redhat.com>, 2001.
      5 
      6    This file is free software; you can redistribute it and/or modify
      7    it under the terms of either
      8 
      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
     12 
     13    or
     14 
     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
     18 
     19    or both in parallel, as here.
     20 
     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.
     25 
     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/>.  */
     29 
     30 #ifndef _MEMORY_ACCESS_H
     31 #define _MEMORY_ACCESS_H 1
     32 
     33 #include <byteswap.h>
     34 #include <limits.h>
     35 #include <stdint.h>
     36 
     37 
     38 /* Number decoding macros.  See 7.6 Variable Length Data.  */
     39 
     40 #define len_leb128(var) ((8 * sizeof (var) + 6) / 7)
     41 
     42 static inline size_t
     43 __libdw_max_len_leb128 (const unsigned char *addr, const unsigned char *end)
     44 {
     45   const size_t type_len = len_leb128 (uint64_t);
     46   const size_t pointer_len = likely (addr < end) ? end - addr : 0;
     47   return likely (type_len <= pointer_len) ? type_len : pointer_len;
     48 }
     49 
     50 #define get_uleb128_step(var, addr, nth)				      \
     51   do {									      \
     52     unsigned char __b = *(addr)++;					      \
     53     (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7);		      \
     54     if (likely ((__b & 0x80) == 0))					      \
     55       return (var);							      \
     56   } while (0)
     57 
     58 static inline uint64_t
     59 __libdw_get_uleb128 (const unsigned char **addrp, const unsigned char *end)
     60 {
     61   uint64_t acc = 0;
     62 
     63   /* Unroll the first step to help the compiler optimize
     64      for the common single-byte case.  */
     65   get_uleb128_step (acc, *addrp, 0);
     66 
     67   const size_t max = __libdw_max_len_leb128 (*addrp - 1, end);
     68   for (size_t i = 1; i < max; ++i)
     69     get_uleb128_step (acc, *addrp, i);
     70   /* Other implementations set VALUE to UINT_MAX in this
     71      case.  So we better do this as well.  */
     72   return UINT64_MAX;
     73 }
     74 
     75 /* Note, addr needs to me smaller than end. */
     76 #define get_uleb128(var, addr, end) ((var) = __libdw_get_uleb128 (&(addr), end))
     77 
     78 /* The signed case is similar, but we sign-extend the result.  */
     79 
     80 #define get_sleb128_step(var, addr, nth)				      \
     81   do {									      \
     82     unsigned char __b = *(addr)++;					      \
     83     if (likely ((__b & 0x80) == 0))					      \
     84       {									      \
     85 	struct { signed int i:7; } __s = { .i = __b };			      \
     86 	(var) |= (typeof (var)) __s.i * ((typeof (var)) 1 << ((nth) * 7));    \
     87 	return (var);							      \
     88       }									      \
     89     (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7);		      \
     90   } while (0)
     91 
     92 static inline int64_t
     93 __libdw_get_sleb128 (const unsigned char **addrp, const unsigned char *end)
     94 {
     95   int64_t acc = 0;
     96 
     97   /* Unroll the first step to help the compiler optimize
     98      for the common single-byte case.  */
     99   get_sleb128_step (acc, *addrp, 0);
    100 
    101   const size_t max = __libdw_max_len_leb128 (*addrp - 1, end);
    102   for (size_t i = 1; i < max; ++i)
    103     get_sleb128_step (acc, *addrp, i);
    104   /* Other implementations set VALUE to INT_MAX in this
    105      case.  So we better do this as well.  */
    106   return INT64_MAX;
    107 }
    108 
    109 #define get_sleb128(var, addr, end) ((var) = __libdw_get_sleb128 (&(addr), end))
    110 
    111 
    112 /* We use simple memory access functions in case the hardware allows it.
    113    The caller has to make sure we don't have alias problems.  */
    114 #if ALLOW_UNALIGNED
    115 
    116 # define read_2ubyte_unaligned(Dbg, Addr) \
    117   (unlikely ((Dbg)->other_byte_order)					      \
    118    ? bswap_16 (*((const uint16_t *) (Addr)))				      \
    119    : *((const uint16_t *) (Addr)))
    120 # define read_2sbyte_unaligned(Dbg, Addr) \
    121   (unlikely ((Dbg)->other_byte_order)					      \
    122    ? (int16_t) bswap_16 (*((const int16_t *) (Addr)))			      \
    123    : *((const int16_t *) (Addr)))
    124 
    125 # define read_4ubyte_unaligned_noncvt(Addr) \
    126    *((const uint32_t *) (Addr))
    127 # define read_4ubyte_unaligned(Dbg, Addr) \
    128   (unlikely ((Dbg)->other_byte_order)					      \
    129    ? bswap_32 (*((const uint32_t *) (Addr)))				      \
    130    : *((const uint32_t *) (Addr)))
    131 # define read_4sbyte_unaligned(Dbg, Addr) \
    132   (unlikely ((Dbg)->other_byte_order)					      \
    133    ? (int32_t) bswap_32 (*((const int32_t *) (Addr)))			      \
    134    : *((const int32_t *) (Addr)))
    135 
    136 # define read_8ubyte_unaligned_noncvt(Addr) \
    137    *((const uint64_t *) (Addr))
    138 # define read_8ubyte_unaligned(Dbg, Addr) \
    139   (unlikely ((Dbg)->other_byte_order)					      \
    140    ? bswap_64 (*((const uint64_t *) (Addr)))				      \
    141    : *((const uint64_t *) (Addr)))
    142 # define read_8sbyte_unaligned(Dbg, Addr) \
    143   (unlikely ((Dbg)->other_byte_order)					      \
    144    ? (int64_t) bswap_64 (*((const int64_t *) (Addr)))			      \
    145    : *((const int64_t *) (Addr)))
    146 
    147 #else
    148 
    149 union unaligned
    150   {
    151     void *p;
    152     uint16_t u2;
    153     uint32_t u4;
    154     uint64_t u8;
    155     int16_t s2;
    156     int32_t s4;
    157     int64_t s8;
    158   } __attribute__ ((packed));
    159 
    160 # define read_2ubyte_unaligned(Dbg, Addr) \
    161   read_2ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
    162 # define read_2sbyte_unaligned(Dbg, Addr) \
    163   read_2sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
    164 # define read_4ubyte_unaligned(Dbg, Addr) \
    165   read_4ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
    166 # define read_4sbyte_unaligned(Dbg, Addr) \
    167   read_4sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
    168 # define read_8ubyte_unaligned(Dbg, Addr) \
    169   read_8ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
    170 # define read_8sbyte_unaligned(Dbg, Addr) \
    171   read_8sbyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
    172 
    173 static inline uint16_t
    174 read_2ubyte_unaligned_1 (bool other_byte_order, const void *p)
    175 {
    176   const union unaligned *up = p;
    177   if (unlikely (other_byte_order))
    178     return bswap_16 (up->u2);
    179   return up->u2;
    180 }
    181 static inline int16_t
    182 read_2sbyte_unaligned_1 (bool other_byte_order, const void *p)
    183 {
    184   const union unaligned *up = p;
    185   if (unlikely (other_byte_order))
    186     return (int16_t) bswap_16 (up->u2);
    187   return up->s2;
    188 }
    189 
    190 static inline uint32_t
    191 read_4ubyte_unaligned_noncvt (const void *p)
    192 {
    193   const union unaligned *up = p;
    194   return up->u4;
    195 }
    196 static inline uint32_t
    197 read_4ubyte_unaligned_1 (bool other_byte_order, const void *p)
    198 {
    199   const union unaligned *up = p;
    200   if (unlikely (other_byte_order))
    201     return bswap_32 (up->u4);
    202   return up->u4;
    203 }
    204 static inline int32_t
    205 read_4sbyte_unaligned_1 (bool other_byte_order, const void *p)
    206 {
    207   const union unaligned *up = p;
    208   if (unlikely (other_byte_order))
    209     return (int32_t) bswap_32 (up->u4);
    210   return up->s4;
    211 }
    212 
    213 static inline uint64_t
    214 read_8ubyte_unaligned_noncvt (const void *p)
    215 {
    216   const union unaligned *up = p;
    217   return up->u8;
    218 }
    219 static inline uint64_t
    220 read_8ubyte_unaligned_1 (bool other_byte_order, const void *p)
    221 {
    222   const union unaligned *up = p;
    223   if (unlikely (other_byte_order))
    224     return bswap_64 (up->u8);
    225   return up->u8;
    226 }
    227 static inline int64_t
    228 read_8sbyte_unaligned_1 (bool other_byte_order, const void *p)
    229 {
    230   const union unaligned *up = p;
    231   if (unlikely (other_byte_order))
    232     return (int64_t) bswap_64 (up->u8);
    233   return up->s8;
    234 }
    235 
    236 #endif	/* allow unaligned */
    237 
    238 
    239 #define read_2ubyte_unaligned_inc(Dbg, Addr) \
    240   ({ uint16_t t_ = read_2ubyte_unaligned (Dbg, Addr);			      \
    241      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2);		      \
    242      t_; })
    243 #define read_2sbyte_unaligned_inc(Dbg, Addr) \
    244   ({ int16_t t_ = read_2sbyte_unaligned (Dbg, Addr);			      \
    245      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2);		      \
    246      t_; })
    247 
    248 #define read_4ubyte_unaligned_inc(Dbg, Addr) \
    249   ({ uint32_t t_ = read_4ubyte_unaligned (Dbg, Addr);			      \
    250      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4);		      \
    251      t_; })
    252 #define read_4sbyte_unaligned_inc(Dbg, Addr) \
    253   ({ int32_t t_ = read_4sbyte_unaligned (Dbg, Addr);			      \
    254      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4);		      \
    255      t_; })
    256 
    257 #define read_8ubyte_unaligned_inc(Dbg, Addr) \
    258   ({ uint64_t t_ = read_8ubyte_unaligned (Dbg, Addr);			      \
    259      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8);		      \
    260      t_; })
    261 #define read_8sbyte_unaligned_inc(Dbg, Addr) \
    262   ({ int64_t t_ = read_8sbyte_unaligned (Dbg, Addr);			      \
    263      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8);		      \
    264      t_; })
    265 
    266 
    267 #define read_addr_unaligned_inc(Nbytes, Dbg, Addr)			\
    268   (assert ((Nbytes) == 4 || (Nbytes) == 8),				\
    269     ((Nbytes) == 4 ? read_4ubyte_unaligned_inc (Dbg, Addr)		\
    270      : read_8ubyte_unaligned_inc (Dbg, Addr)))
    271 
    272 #endif	/* memory-access.h */
    273