Home | History | Annotate | Download | only in libdw
      1 /* Unaligned memory access functionality.
      2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2001.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    In addition, as a special exception, Red Hat, Inc. gives You the
     20    additional right to link the code of Red Hat elfutils with code licensed
     21    under any Open Source Initiative certified open source license
     22    (http://www.opensource.org/licenses/index.php) which requires the
     23    distribution of source code with any binary distribution and to
     24    distribute linked combinations of the two.  Non-GPL Code permitted under
     25    this exception must only link to the code of Red Hat elfutils through
     26    those well defined interfaces identified in the file named EXCEPTION
     27    found in the source code files (the "Approved Interfaces").  The files
     28    of Non-GPL Code may instantiate templates or use macros or inline
     29    functions from the Approved Interfaces without causing the resulting
     30    work to be covered by the GNU General Public License.  Only Red Hat,
     31    Inc. may make changes or additions to the list of Approved Interfaces.
     32    Red Hat's grant of this exception is conditioned upon your not adding
     33    any new exceptions.  If you wish to add a new Approved Interface or
     34    exception, please contact Red Hat.  You must obey the GNU General Public
     35    License in all respects for all of the Red Hat elfutils code and other
     36    code used in conjunction with Red Hat elfutils except the Non-GPL Code
     37    covered by this exception.  If you modify this file, you may extend this
     38    exception to your version of the file, but you are not obligated to do
     39    so.  If you do not wish to provide this exception without modification,
     40    you must delete this exception statement from your version and license
     41    this file solely under the GPL without exception.
     42 
     43    Red Hat elfutils is an included package of the Open Invention Network.
     44    An included package of the Open Invention Network is a package for which
     45    Open Invention Network licensees cross-license their patents.  No patent
     46    license is granted, either expressly or impliedly, by designation as an
     47    included package.  Should you wish to participate in the Open Invention
     48    Network licensing program, please visit www.openinventionnetwork.com
     49    <http://www.openinventionnetwork.com>.  */
     50 
     51 #ifndef _MEMORY_ACCESS_H
     52 #define _MEMORY_ACCESS_H 1
     53 
     54 #include <byteswap.h>
     55 #include <limits.h>
     56 #include <stdint.h>
     57 
     58 
     59 /* Number decoding macros.  See 7.6 Variable Length Data.  */
     60 
     61 #define get_uleb128_step(var, addr, nth, break)				      \
     62     __b = *(addr)++;							      \
     63     var |= (uintmax_t) (__b & 0x7f) << (nth * 7);			      \
     64     if (likely ((__b & 0x80) == 0))					      \
     65       break
     66 
     67 #define get_uleb128(var, addr)						      \
     68   do {									      \
     69     unsigned char __b;							      \
     70     var = 0;								      \
     71     get_uleb128_step (var, addr, 0, break);				      \
     72     var = __libdw_get_uleb128 (var, 1, &(addr));			      \
     73   } while (0)
     74 
     75 #define get_uleb128_rest_return(var, i, addrp)				      \
     76   do {									      \
     77     for (; i < 10; ++i)							      \
     78       {									      \
     79 	get_uleb128_step (var, *addrp, i, return var);			      \
     80       }									      \
     81     /* Other implementations set VALUE to UINT_MAX in this		      \
     82        case.  So we better do this as well.  */				      \
     83     return UINT64_MAX;							      \
     84   } while (0)
     85 
     86 /* The signed case is similar, but we sign-extend the result.  */
     87 
     88 #define get_sleb128_step(var, addr, nth, break)				      \
     89     __b = *(addr)++;							      \
     90     _v |= (uint64_t) (__b & 0x7f) << (nth * 7);				      \
     91     if (likely ((__b & 0x80) == 0))					      \
     92       {									      \
     93 	var = (_v << (64 - (nth * 7) - 7) >> (64 - (nth * 7) - 7));	      \
     94         break;					 			      \
     95       }									      \
     96     else do {} while (0)
     97 
     98 #define get_sleb128(var, addr)						      \
     99   do {									      \
    100     unsigned char __b;							      \
    101     int64_t _v = 0;							      \
    102     get_sleb128_step (var, addr, 0, break);				      \
    103     var = __libdw_get_sleb128 (_v, 1, &(addr));				      \
    104   } while (0)
    105 
    106 #define get_sleb128_rest_return(var, i, addrp)				      \
    107   do {									      \
    108     for (; i < 9; ++i)							      \
    109       {									      \
    110 	get_sleb128_step (var, *addrp, i, return var);			      \
    111       }									      \
    112     /* Other implementations set VALUE to INT_MAX in this		      \
    113        case.  So we better do this as well.  */				      \
    114     return INT64_MAX;							      \
    115   } while (0)
    116 
    117 #ifdef IS_LIBDW
    118 extern uint64_t __libdw_get_uleb128 (uint64_t acc, unsigned int i,
    119 				     const unsigned char **addrp)
    120      internal_function attribute_hidden;
    121 extern int64_t __libdw_get_sleb128 (int64_t acc, unsigned int i,
    122 				    const unsigned char **addrp)
    123      internal_function attribute_hidden;
    124 #else
    125 static uint64_t
    126 __attribute__ ((unused))
    127 __libdw_get_uleb128 (uint64_t acc, unsigned int i, const unsigned char **addrp)
    128 {
    129   unsigned char __b;
    130   get_uleb128_rest_return (acc, i, addrp);
    131 }
    132 static int64_t
    133 __attribute__ ((unused))
    134 __libdw_get_sleb128 (int64_t acc, unsigned int i, const unsigned char **addrp)
    135 {
    136   unsigned char __b;
    137   int64_t _v = acc;
    138   get_sleb128_rest_return (acc, i, addrp);
    139 }
    140 #endif
    141 
    142 
    143 /* We use simple memory access functions in case the hardware allows it.
    144    The caller has to make sure we don't have alias problems.  */
    145 #if ALLOW_UNALIGNED
    146 
    147 # define read_2ubyte_unaligned(Dbg, Addr) \
    148   (unlikely ((Dbg)->other_byte_order)					      \
    149    ? bswap_16 (*((const uint16_t *) (Addr)))				      \
    150    : *((const uint16_t *) (Addr)))
    151 # define read_2sbyte_unaligned(Dbg, Addr) \
    152   (unlikely ((Dbg)->other_byte_order)					      \
    153    ? (int16_t) bswap_16 (*((const int16_t *) (Addr)))			      \
    154    : *((const int16_t *) (Addr)))
    155 
    156 # define read_4ubyte_unaligned_noncvt(Addr) \
    157    *((const uint32_t *) (Addr))
    158 # define read_4ubyte_unaligned(Dbg, Addr) \
    159   (unlikely ((Dbg)->other_byte_order)					      \
    160    ? bswap_32 (*((const uint32_t *) (Addr)))				      \
    161    : *((const uint32_t *) (Addr)))
    162 # define read_4sbyte_unaligned(Dbg, Addr) \
    163   (unlikely ((Dbg)->other_byte_order)					      \
    164    ? (int32_t) bswap_32 (*((const int32_t *) (Addr)))			      \
    165    : *((const int32_t *) (Addr)))
    166 
    167 # define read_8ubyte_unaligned(Dbg, Addr) \
    168   (unlikely ((Dbg)->other_byte_order)					      \
    169    ? bswap_64 (*((const uint64_t *) (Addr)))				      \
    170    : *((const uint64_t *) (Addr)))
    171 # define read_8sbyte_unaligned(Dbg, Addr) \
    172   (unlikely ((Dbg)->other_byte_order)					      \
    173    ? (int64_t) bswap_64 (*((const int64_t *) (Addr)))			      \
    174    : *((const int64_t *) (Addr)))
    175 
    176 #else
    177 
    178 union unaligned
    179   {
    180     void *p;
    181     uint16_t u2;
    182     uint32_t u4;
    183     uint64_t u8;
    184     int16_t s2;
    185     int32_t s4;
    186     int64_t s8;
    187   } __attribute__ ((packed));
    188 
    189 static inline uint16_t
    190 read_2ubyte_unaligned (Dwarf *dbg, const void *p)
    191 {
    192   const union unaligned *up = p;
    193   if (dbg->other_byte_order)
    194     return bswap_16 (up->u2);
    195   return up->u2;
    196 }
    197 static inline int16_t
    198 read_2sbyte_unaligned (Dwarf *dbg, const void *p)
    199 {
    200   const union unaligned *up = p;
    201   if (dbg->other_byte_order)
    202     return (int16_t) bswap_16 (up->u2);
    203   return up->s2;
    204 }
    205 
    206 static inline uint32_t
    207 read_4ubyte_unaligned_noncvt (const void *p)
    208 {
    209   const union unaligned *up = p;
    210   return up->u4;
    211 }
    212 static inline uint32_t
    213 read_4ubyte_unaligned (Dwarf *dbg, const void *p)
    214 {
    215   const union unaligned *up = p;
    216   if (dbg->other_byte_order)
    217     return bswap_32 (up->u4);
    218   return up->u4;
    219 }
    220 static inline int32_t
    221 read_4sbyte_unaligned (Dwarf *dbg, const void *p)
    222 {
    223   const union unaligned *up = p;
    224   if (dbg->other_byte_order)
    225     return (int32_t) bswap_32 (up->u4);
    226   return up->s4;
    227 }
    228 
    229 static inline uint64_t
    230 read_8ubyte_unaligned (Dwarf *dbg, const void *p)
    231 {
    232   const union unaligned *up = p;
    233   if (dbg->other_byte_order)
    234     return bswap_64 (up->u8);
    235   return up->u8;
    236 }
    237 static inline int64_t
    238 read_8sbyte_unaligned (Dwarf *dbg, const void *p)
    239 {
    240   const union unaligned *up = p;
    241   if (dbg->other_byte_order)
    242     return (int64_t) bswap_64 (up->u8);
    243   return up->s8;
    244 }
    245 
    246 #endif	/* allow unaligned */
    247 
    248 
    249 #define read_2ubyte_unaligned_inc(Dbg, Addr) \
    250   ({ uint16_t t_ = read_2ubyte_unaligned (Dbg, Addr);			      \
    251      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2);		      \
    252      t_; })
    253 #define read_2sbyte_unaligned_inc(Dbg, Addr) \
    254   ({ int16_t t_ = read_2sbyte_unaligned (Dbg, Addr);			      \
    255      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2);		      \
    256      t_; })
    257 
    258 #define read_4ubyte_unaligned_inc(Dbg, Addr) \
    259   ({ uint32_t t_ = read_4ubyte_unaligned (Dbg, Addr);			      \
    260      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4);		      \
    261      t_; })
    262 #define read_4sbyte_unaligned_inc(Dbg, Addr) \
    263   ({ int32_t t_ = read_4sbyte_unaligned (Dbg, Addr);			      \
    264      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4);		      \
    265      t_; })
    266 
    267 #define read_8ubyte_unaligned_inc(Dbg, Addr) \
    268   ({ uint64_t t_ = read_8ubyte_unaligned (Dbg, Addr);			      \
    269      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8);		      \
    270      t_; })
    271 #define read_8sbyte_unaligned_inc(Dbg, Addr) \
    272   ({ int64_t t_ = read_8sbyte_unaligned (Dbg, Addr);			      \
    273      Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8);		      \
    274      t_; })
    275 
    276 #endif	/* memory-access.h */
    277