1/* Unaligned memory access functionality. 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008 Red Hat, Inc. 3 Written by Ulrich Drepper <drepper@redhat.com>, 2001. 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/* When loading this file we require the macro MACHINE_ENCODING to be 38 defined to signal the endianness of the architecture which is 39 defined. */ 40#ifndef MACHINE_ENCODING 41# error "MACHINE_ENCODING needs to be defined" 42#endif 43#if MACHINE_ENCODING != BIG_ENDIAN && MACHINE_ENCODING != LITTLE_ENDIAN 44# error "MACHINE_ENCODING must signal either big or little endian" 45#endif 46 47 48/* We use simple memory access functions in case the hardware allows it. 49 The caller has to make sure we don't have alias problems. */ 50#if ALLOW_UNALIGNED 51 52# define read_2ubyte_unaligned(Addr) \ 53 (unlikely (MACHINE_ENCODING != BYTE_ORDER) \ 54 ? bswap_16 (*((const uint16_t *) (Addr))) \ 55 : *((const uint16_t *) (Addr))) 56# define read_2sbyte_unaligned(Addr) \ 57 (unlikely (MACHINE_ENCODING != BYTE_ORDER) \ 58 ? (int16_t) bswap_16 (*((const int16_t *) (Addr))) \ 59 : *((const int16_t *) (Addr))) 60 61# define read_4ubyte_unaligned_noncvt(Addr) \ 62 *((const uint32_t *) (Addr)) 63# define read_4ubyte_unaligned(Addr) \ 64 (unlikely (MACHINE_ENCODING != BYTE_ORDER) \ 65 ? bswap_32 (*((const uint32_t *) (Addr))) \ 66 : *((const uint32_t *) (Addr))) 67# define read_4sbyte_unaligned(Addr) \ 68 (unlikely (MACHINE_ENCODING != BYTE_ORDER) \ 69 ? (int32_t) bswap_32 (*((const int32_t *) (Addr))) \ 70 : *((const int32_t *) (Addr))) 71 72# define read_8ubyte_unaligned(Addr) \ 73 (unlikely (MACHINE_ENCODING != BYTE_ORDER) \ 74 ? bswap_64 (*((const uint64_t *) (Addr))) \ 75 : *((const uint64_t *) (Addr))) 76# define read_8sbyte_unaligned(Addr) \ 77 (unlikely (MACHINE_ENCODING != BYTE_ORDER) \ 78 ? (int64_t) bswap_64 (*((const int64_t *) (Addr))) \ 79 : *((const int64_t *) (Addr))) 80 81#else 82 83union unaligned 84 { 85 void *p; 86 uint16_t u2; 87 uint32_t u4; 88 uint64_t u8; 89 int16_t s2; 90 int32_t s4; 91 int64_t s8; 92 } attribute_packed; 93 94static inline uint16_t 95read_2ubyte_unaligned (const void *p) 96{ 97 const union unaligned *up = p; 98 if (MACHINE_ENCODING != BYTE_ORDER) 99 return bswap_16 (up->u2); 100 return up->u2; 101} 102static inline int16_t 103read_2sbyte_unaligned (const void *p) 104{ 105 const union unaligned *up = p; 106 if (MACHINE_ENCODING != BYTE_ORDER) 107 return (int16_t) bswap_16 (up->u2); 108 return up->s2; 109} 110 111static inline uint32_t 112read_4ubyte_unaligned_noncvt (const void *p) 113{ 114 const union unaligned *up = p; 115 return up->u4; 116} 117static inline uint32_t 118read_4ubyte_unaligned (const void *p) 119{ 120 const union unaligned *up = p; 121 if (MACHINE_ENCODING != BYTE_ORDER) 122 return bswap_32 (up->u4); 123 return up->u4; 124} 125static inline int32_t 126read_4sbyte_unaligned (const void *p) 127{ 128 const union unaligned *up = p; 129 if (MACHINE_ENCODING != BYTE_ORDER) 130 return (int32_t) bswap_32 (up->u4); 131 return up->s4; 132} 133 134static inline uint64_t 135read_8ubyte_unaligned (const void *p) 136{ 137 const union unaligned *up = p; 138 if (MACHINE_ENCODING != BYTE_ORDER) 139 return bswap_64 (up->u8); 140 return up->u8; 141} 142static inline int64_t 143read_8sbyte_unaligned (const void *p) 144{ 145 const union unaligned *up = p; 146 if (MACHINE_ENCODING != BYTE_ORDER) 147 return (int64_t) bswap_64 (up->u8); 148 return up->s8; 149} 150 151#endif /* allow unaligned */ 152 153 154#define read_2ubyte_unaligned_inc(Addr) \ 155 ({ uint16_t t_ = read_2ubyte_unaligned (Addr); \ 156 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \ 157 t_; }) 158#define read_2sbyte_unaligned_inc(Addr) \ 159 ({ int16_t t_ = read_2sbyte_unaligned (Addr); \ 160 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); \ 161 t_; }) 162 163#define read_4ubyte_unaligned_inc(Addr) \ 164 ({ uint32_t t_ = read_4ubyte_unaligned (Addr); \ 165 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \ 166 t_; }) 167#define read_4sbyte_unaligned_inc(Addr) \ 168 ({ int32_t t_ = read_4sbyte_unaligned (Addr); \ 169 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4); \ 170 t_; }) 171 172#define read_8ubyte_unaligned_inc(Addr) \ 173 ({ uint64_t t_ = read_8ubyte_unaligned (Addr); \ 174 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \ 175 t_; }) 176#define read_8sbyte_unaligned_inc(Addr) \ 177 ({ int64_t t_ = read_8sbyte_unaligned (Addr); \ 178 Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8); \ 179 t_; }) 180 181#endif /* memory-access.h */ 182