Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Unaligned memory access functionality. |
| 2 | Copyright (C) 2000, 2001, 2002, 2003 Red Hat, Inc. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 3 | This file is part of Red Hat elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2001. |
| 5 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 9 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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, |
Ulrich Drepper | 1e9ef50 | 2006-04-04 22:29:06 +0000 | [diff] [blame] | 17 | Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 18 | |
| 19 | Red Hat elfutils is an included package of the Open Invention Network. |
| 20 | An included package of the Open Invention Network is a package for which |
| 21 | Open Invention Network licensees cross-license their patents. No patent |
| 22 | license is granted, either expressly or impliedly, by designation as an |
| 23 | included package. Should you wish to participate in the Open Invention |
| 24 | Network licensing program, please visit www.openinventionnetwork.com |
| 25 | <http://www.openinventionnetwork.com>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 26 | |
| 27 | #ifndef _UNALIGNED_H |
| 28 | #define _UNALIGNED_H 1 |
| 29 | |
| 30 | #include <byteswap.h> |
| 31 | #include <endian.h> |
| 32 | |
| 33 | |
| 34 | #ifndef UNALIGNED_ACCESS_CLASS |
| 35 | # error "UNALIGNED_ACCESS_CLASS must be defined" |
| 36 | #endif |
| 37 | |
| 38 | |
| 39 | /* Macros to convert from the host byte order to that of the object file. */ |
| 40 | #if UNALIGNED_ACCESS_CLASS == BYTE_ORDER |
| 41 | # define target_bswap_16(n) (n) |
| 42 | # define target_bswap_32(n) (n) |
| 43 | # define target_bswap_64(n) (n) |
| 44 | #else |
| 45 | # define target_bswap_16(n) bswap_16 (n) |
| 46 | # define target_bswap_32(n) bswap_32 (n) |
| 47 | # define target_bswap_64(n) bswap_64 (n) |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | union u_2ubyte_unaligned |
| 52 | { |
| 53 | uint16_t u; |
| 54 | char c[2]; |
| 55 | } __attribute__((packed)); |
| 56 | |
| 57 | union u_4ubyte_unaligned |
| 58 | { |
| 59 | uint32_t u; |
| 60 | char c[4]; |
| 61 | } __attribute__((packed)); |
| 62 | |
| 63 | union u_8ubyte_unaligned |
| 64 | { |
| 65 | uint64_t u; |
| 66 | char c[8]; |
| 67 | } __attribute__((packed)); |
| 68 | |
| 69 | |
| 70 | /* Macros to store value at unaligned address. */ |
| 71 | #define store_2ubyte_unaligned(ptr, value) \ |
| 72 | (void) (((union u_2ubyte_unaligned *) (ptr))->u = target_bswap_16 (value)) |
| 73 | #define store_4ubyte_unaligned(ptr, value) \ |
| 74 | (void) (((union u_4ubyte_unaligned *) (ptr))->u = target_bswap_32 (value)) |
| 75 | #define store_8ubyte_unaligned(ptr, value) \ |
| 76 | (void) (((union u_8ubyte_unaligned *) (ptr))->u = target_bswap_64 (value)) |
| 77 | |
| 78 | |
| 79 | /* Macros to add value to unaligned address. This is a bit more |
| 80 | complicated since the value must be read from memory and eventually |
| 81 | converted twice. */ |
| 82 | #if UNALIGNED_ACCESS_CLASS == BYTE_ORDER |
| 83 | # define add_2ubyte_unaligned(ptr, value) \ |
| 84 | (void) (((union u_2ubyte_unaligned *) (ptr))->u += value) |
| 85 | # define add_4ubyte_unaligned(ptr, value) \ |
| 86 | (void) (((union u_4ubyte_unaligned *) (ptr))->u += value) |
| 87 | # define add_8ubyte_unaligned(ptr, value) \ |
| 88 | (void) (((union u_8ubyte_unaligned *) (ptr))->u += value) |
| 89 | #else |
| 90 | # define add_2ubyte_unaligned(ptr, value) \ |
| 91 | do { \ |
| 92 | union u_2ubyte_unaligned *_ptr = (ptr); \ |
| 93 | uint16_t _val = bswap_16 (_ptr->u) + (value); \ |
| 94 | _ptr->u = bswap_16 (_val); \ |
| 95 | } while (0) |
| 96 | # define add_4ubyte_unaligned(ptr, value) \ |
| 97 | do { \ |
| 98 | union u_4ubyte_unaligned *_ptr = (ptr); \ |
| 99 | uint32_t _val = bswap_32 (_ptr->u) + (value); \ |
| 100 | _ptr->u = bswap_32 (_val); \ |
| 101 | } while (0) |
| 102 | # define add_8ubyte_unaligned(ptr, value) \ |
| 103 | do { \ |
| 104 | union u_8ubyte_unaligned *_ptr = (ptr); \ |
| 105 | uint64_t _val = bswap_64 (_ptr->u) + (value); \ |
| 106 | _ptr->u = bswap_64 (_val); \ |
| 107 | } while (0) |
| 108 | #endif |
| 109 | |
| 110 | #endif /* unaligned.h */ |