1 // elfcpp_swap.h -- Handle swapping for elfcpp -*- C++ -*-
3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of elfcpp.
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
32 // This header file defines basic template classes to efficiently swap
33 // numbers between host form and target form. When the host and
34 // target have the same endianness, these turn into no-ops.
41 // We need an autoconf-generated config.h file for endianness and
42 // swapping. We check two macros: WORDS_BIGENDIAN and
47 #ifdef HAVE_BYTESWAP_H
50 // Provide our own versions of the byteswap functions.
54 return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
60 return ( ((v & 0xff000000) >> 24)
61 | ((v & 0x00ff0000) >> 8)
62 | ((v & 0x0000ff00) << 8)
63 | ((v & 0x000000ff) << 24));
69 return ( ((v & 0xff00000000000000ULL) >> 56)
70 | ((v & 0x00ff000000000000ULL) >> 40)
71 | ((v & 0x0000ff0000000000ULL) >> 24)
72 | ((v & 0x000000ff00000000ULL) >> 8)
73 | ((v & 0x00000000ff000000ULL) << 8)
74 | ((v & 0x0000000000ff0000ULL) << 24)
75 | ((v & 0x000000000000ff00ULL) << 40)
76 | ((v & 0x00000000000000ffULL) << 56));
78 #endif // !defined(HAVE_BYTESWAP_H)
80 // gcc 4.3 and later provides __builtin_bswap32 and __builtin_bswap64.
82 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
84 #define bswap_32 __builtin_bswap32
86 #define bswap_64 __builtin_bswap64
92 // Endian simply indicates whether the host is big endian or not.
97 // Used for template specializations.
98 static const bool host_big_endian =
99 #ifdef WORDS_BIGENDIAN
107 // Valtype_base is a template based on size (8, 16, 32, 64) which
108 // defines the type Valtype as the unsigned integer, and
109 // Signed_valtype as the signed integer, of the specified size.
115 struct Valtype_base<8>
117 typedef uint8_t Valtype;
118 typedef int8_t Signed_valtype;
122 struct Valtype_base<16>
124 typedef uint16_t Valtype;
125 typedef int16_t Signed_valtype;
129 struct Valtype_base<32>
131 typedef uint32_t Valtype;
132 typedef int32_t Signed_valtype;
136 struct Valtype_base<64>
138 typedef uint64_t Valtype;
139 typedef int64_t Signed_valtype;
142 // Convert_endian is a template based on size and on whether the host
143 // and target have the same endianness. It defines the type Valtype
144 // as Valtype_base does, and also defines a function convert_host
145 // which takes an argument of type Valtype and returns the same value,
146 // but swapped if the host and target have different endianness.
148 template<int size, bool same_endian>
149 struct Convert_endian;
152 struct Convert_endian<size, true>
154 typedef typename Valtype_base<size>::Valtype Valtype;
156 static inline Valtype
157 convert_host(Valtype v)
162 struct Convert_endian<8, false>
164 typedef Valtype_base<8>::Valtype Valtype;
166 static inline Valtype
167 convert_host(Valtype v)
172 struct Convert_endian<16, false>
174 typedef Valtype_base<16>::Valtype Valtype;
176 static inline Valtype
177 convert_host(Valtype v)
178 { return bswap_16(v); }
182 struct Convert_endian<32, false>
184 typedef Valtype_base<32>::Valtype Valtype;
186 static inline Valtype
187 convert_host(Valtype v)
188 { return bswap_32(v); }
192 struct Convert_endian<64, false>
194 typedef Valtype_base<64>::Valtype Valtype;
196 static inline Valtype
197 convert_host(Valtype v)
198 { return bswap_64(v); }
201 // Convert is a template based on size and on whether the target is
202 // big endian. It defines Valtype and convert_host like
203 // Convert_endian. That is, it is just like Convert_endian except in
204 // the meaning of the second template parameter.
206 template<int size, bool big_endian>
209 typedef typename Valtype_base<size>::Valtype Valtype;
211 static inline Valtype
212 convert_host(Valtype v)
214 return Convert_endian<size, big_endian == Endian::host_big_endian>
219 // Swap is a template based on size and on whether the target is big
220 // endian. It defines the type Valtype and the functions readval and
221 // writeval. The functions read and write values of the appropriate
222 // size out of buffers, swapping them if necessary. readval and
223 // writeval are overloaded to take pointers to the appropriate type or
224 // pointers to unsigned char.
226 template<int size, bool big_endian>
229 typedef typename Valtype_base<size>::Valtype Valtype;
231 static inline Valtype
232 readval(const Valtype* wv)
233 { return Convert<size, big_endian>::convert_host(*wv); }
236 writeval(Valtype* wv, Valtype v)
237 { *wv = Convert<size, big_endian>::convert_host(v); }
239 static inline Valtype
240 readval(const unsigned char* wv)
241 { return readval(reinterpret_cast<const Valtype*>(wv)); }
244 writeval(unsigned char* wv, Valtype v)
245 { writeval(reinterpret_cast<Valtype*>(wv), v); }
248 // We need to specialize the 8-bit version of Swap to avoid
249 // conflicting overloads, since both versions of readval and writeval
250 // will have the same type parameters.
252 template<bool big_endian>
253 struct Swap<8, big_endian>
255 typedef typename Valtype_base<8>::Valtype Valtype;
257 static inline Valtype
258 readval(const Valtype* wv)
262 writeval(Valtype* wv, Valtype v)
266 // Swap_unaligned is a template based on size and on whether the
267 // target is big endian. It defines the type Valtype and the
268 // functions readval and writeval. The functions read and write
269 // values of the appropriate size out of buffers which may be
272 template<int size, bool big_endian>
273 struct Swap_unaligned;
275 template<bool big_endian>
276 struct Swap_unaligned<8, big_endian>
278 typedef typename Valtype_base<8>::Valtype Valtype;
280 static inline Valtype
281 readval(const unsigned char* wv)
285 writeval(unsigned char* wv, Valtype v)
290 struct Swap_unaligned<16, false>
292 typedef Valtype_base<16>::Valtype Valtype;
294 static inline Valtype
295 readval(const unsigned char* wv)
297 return (wv[1] << 8) | wv[0];
301 writeval(unsigned char* wv, Valtype v)
309 struct Swap_unaligned<16, true>
311 typedef Valtype_base<16>::Valtype Valtype;
313 static inline Valtype
314 readval(const unsigned char* wv)
316 return (wv[0] << 8) | wv[1];
320 writeval(unsigned char* wv, Valtype v)
328 struct Swap_unaligned<32, false>
330 typedef Valtype_base<32>::Valtype Valtype;
332 static inline Valtype
333 readval(const unsigned char* wv)
335 return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0];
339 writeval(unsigned char* wv, Valtype v)
349 struct Swap_unaligned<32, true>
351 typedef Valtype_base<32>::Valtype Valtype;
353 static inline Valtype
354 readval(const unsigned char* wv)
356 return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3];
360 writeval(unsigned char* wv, Valtype v)
370 struct Swap_unaligned<64, false>
372 typedef Valtype_base<64>::Valtype Valtype;
374 static inline Valtype
375 readval(const unsigned char* wv)
377 return ((static_cast<Valtype>(wv[7]) << 56)
378 | (static_cast<Valtype>(wv[6]) << 48)
379 | (static_cast<Valtype>(wv[5]) << 40)
380 | (static_cast<Valtype>(wv[4]) << 32)
381 | (static_cast<Valtype>(wv[3]) << 24)
382 | (static_cast<Valtype>(wv[2]) << 16)
383 | (static_cast<Valtype>(wv[1]) << 8)
384 | static_cast<Valtype>(wv[0]));
388 writeval(unsigned char* wv, Valtype v)
402 struct Swap_unaligned<64, true>
404 typedef Valtype_base<64>::Valtype Valtype;
406 static inline Valtype
407 readval(const unsigned char* wv)
409 return ((static_cast<Valtype>(wv[0]) << 56)
410 | (static_cast<Valtype>(wv[1]) << 48)
411 | (static_cast<Valtype>(wv[2]) << 40)
412 | (static_cast<Valtype>(wv[3]) << 32)
413 | (static_cast<Valtype>(wv[4]) << 24)
414 | (static_cast<Valtype>(wv[5]) << 16)
415 | (static_cast<Valtype>(wv[6]) << 8)
416 | static_cast<Valtype>(wv[7]));
420 writeval(unsigned char* wv, Valtype v)
433 // Swap_aligned32 is a template based on size and on whether the
434 // target is big endian. It defines the type Valtype and the
435 // functions readval and writeval. The functions read and write
436 // values of the appropriate size out of buffers which may not be
437 // 64-bit aligned, but are 32-bit aligned.
439 template<int size, bool big_endian>
440 struct Swap_aligned32
442 typedef typename Valtype_base<size>::Valtype Valtype;
444 static inline Valtype
445 readval(const unsigned char* wv)
446 { return Swap<size, big_endian>::readval(
447 reinterpret_cast<const Valtype*>(wv)); }
450 writeval(unsigned char* wv, Valtype v)
451 { Swap<size, big_endian>::writeval(reinterpret_cast<Valtype*>(wv), v); }
455 struct Swap_aligned32<64, true>
457 typedef Valtype_base<64>::Valtype Valtype;
459 static inline Valtype
460 readval(const unsigned char* wv)
462 return ((static_cast<Valtype>(Swap<32, true>::readval(wv)) << 32)
463 | static_cast<Valtype>(Swap<32, true>::readval(wv + 4)));
467 writeval(unsigned char* wv, Valtype v)
469 typedef Valtype_base<32>::Valtype Valtype32;
471 Swap<32, true>::writeval(wv, static_cast<Valtype32>(v >> 32));
472 Swap<32, true>::writeval(wv + 4, static_cast<Valtype32>(v));
477 struct Swap_aligned32<64, false>
479 typedef Valtype_base<64>::Valtype Valtype;
481 static inline Valtype
482 readval(const unsigned char* wv)
484 return ((static_cast<Valtype>(Swap<32, false>::readval(wv + 4)) << 32)
485 | static_cast<Valtype>(Swap<32, false>::readval(wv)));
489 writeval(unsigned char* wv, Valtype v)
491 typedef Valtype_base<32>::Valtype Valtype32;
493 Swap<32, false>::writeval(wv + 4, static_cast<Valtype32>(v >> 32));
494 Swap<32, false>::writeval(wv, static_cast<Valtype32>(v));
498 } // End namespace elfcpp.
500 #endif // !defined(ELFCPP_SWAP_H)