1 // elfcpp_swap.h -- Handle swapping for elfcpp -*- C++ -*-
3 // Copyright (C) 2006-2022 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
49 #endif // defined(HAVE_BYTESWAP_H)
51 // Provide our own versions of the byteswap functions.
52 #if !HAVE_DECL_BSWAP_16
53 static inline uint16_t
56 return ((v
>> 8) & 0xff) | ((v
& 0xff) << 8);
58 #endif // !HAVE_DECL_BSWAP16
60 #if !HAVE_DECL_BSWAP_32
61 static inline uint32_t
64 return ( ((v
& 0xff000000) >> 24)
65 | ((v
& 0x00ff0000) >> 8)
66 | ((v
& 0x0000ff00) << 8)
67 | ((v
& 0x000000ff) << 24));
69 #endif // !HAVE_DECL_BSWAP32
71 #if !HAVE_DECL_BSWAP_64
72 static inline uint64_t
75 return ( ((v
& 0xff00000000000000ULL
) >> 56)
76 | ((v
& 0x00ff000000000000ULL
) >> 40)
77 | ((v
& 0x0000ff0000000000ULL
) >> 24)
78 | ((v
& 0x000000ff00000000ULL
) >> 8)
79 | ((v
& 0x00000000ff000000ULL
) << 8)
80 | ((v
& 0x0000000000ff0000ULL
) << 24)
81 | ((v
& 0x000000000000ff00ULL
) << 40)
82 | ((v
& 0x00000000000000ffULL
) << 56));
84 #endif // !HAVE_DECL_BSWAP64
86 // gcc 4.3 and later provides __builtin_bswap32 and __builtin_bswap64.
88 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
90 #define bswap_32 __builtin_bswap32
92 #define bswap_64 __builtin_bswap64
98 // Endian simply indicates whether the host is big endian or not.
103 // Used for template specializations.
104 static const bool host_big_endian
=
105 #ifdef WORDS_BIGENDIAN
113 // Valtype_base is a template based on size (8, 16, 32, 64) which
114 // defines the type Valtype as the unsigned integer, and
115 // Signed_valtype as the signed integer, of the specified size.
121 struct Valtype_base
<8>
123 typedef uint8_t Valtype
;
124 typedef int8_t Signed_valtype
;
128 struct Valtype_base
<16>
130 typedef uint16_t Valtype
;
131 typedef int16_t Signed_valtype
;
135 struct Valtype_base
<32>
137 typedef uint32_t Valtype
;
138 typedef int32_t Signed_valtype
;
142 struct Valtype_base
<64>
144 typedef uint64_t Valtype
;
145 typedef int64_t Signed_valtype
;
148 // Convert_endian is a template based on size and on whether the host
149 // and target have the same endianness. It defines the type Valtype
150 // as Valtype_base does, and also defines a function convert_host
151 // which takes an argument of type Valtype and returns the same value,
152 // but swapped if the host and target have different endianness.
154 template<int size
, bool same_endian
>
155 struct Convert_endian
;
158 struct Convert_endian
<size
, true>
160 typedef typename Valtype_base
<size
>::Valtype Valtype
;
162 static inline Valtype
163 convert_host(Valtype v
)
168 struct Convert_endian
<8, false>
170 typedef Valtype_base
<8>::Valtype Valtype
;
172 static inline Valtype
173 convert_host(Valtype v
)
178 struct Convert_endian
<16, false>
180 typedef Valtype_base
<16>::Valtype Valtype
;
182 static inline Valtype
183 convert_host(Valtype v
)
184 { return bswap_16(v
); }
188 struct Convert_endian
<32, false>
190 typedef Valtype_base
<32>::Valtype Valtype
;
192 static inline Valtype
193 convert_host(Valtype v
)
194 { return bswap_32(v
); }
198 struct Convert_endian
<64, false>
200 typedef Valtype_base
<64>::Valtype Valtype
;
202 static inline Valtype
203 convert_host(Valtype v
)
204 { return bswap_64(v
); }
207 // Convert is a template based on size and on whether the target is
208 // big endian. It defines Valtype and convert_host like
209 // Convert_endian. That is, it is just like Convert_endian except in
210 // the meaning of the second template parameter.
212 template<int size
, bool big_endian
>
215 typedef typename Valtype_base
<size
>::Valtype Valtype
;
217 static inline Valtype
218 convert_host(Valtype v
)
220 return Convert_endian
<size
, big_endian
== Endian::host_big_endian
>
225 // Swap is a template based on size and on whether the target is big
226 // endian. It defines the type Valtype and the functions readval and
227 // writeval. The functions read and write values of the appropriate
228 // size out of buffers, swapping them if necessary. readval and
229 // writeval are overloaded to take pointers to the appropriate type or
230 // pointers to unsigned char.
232 template<int size
, bool big_endian
>
235 typedef typename Valtype_base
<size
>::Valtype Valtype
;
237 static inline Valtype
238 readval(const Valtype
* wv
)
239 { return Convert
<size
, big_endian
>::convert_host(*wv
); }
242 writeval(Valtype
* wv
, Valtype v
)
243 { *wv
= Convert
<size
, big_endian
>::convert_host(v
); }
245 static inline Valtype
246 readval(const unsigned char* wv
)
247 { return readval(reinterpret_cast<const Valtype
*>(wv
)); }
250 writeval(unsigned char* wv
, Valtype v
)
251 { writeval(reinterpret_cast<Valtype
*>(wv
), v
); }
254 // We need to specialize the 8-bit version of Swap to avoid
255 // conflicting overloads, since both versions of readval and writeval
256 // will have the same type parameters.
258 template<bool big_endian
>
259 struct Swap
<8, big_endian
>
261 typedef typename Valtype_base
<8>::Valtype Valtype
;
263 static inline Valtype
264 readval(const Valtype
* wv
)
268 writeval(Valtype
* wv
, Valtype v
)
272 // Swap_unaligned is a template based on size and on whether the
273 // target is big endian. It defines the type Valtype and the
274 // functions readval and writeval. The functions read and write
275 // values of the appropriate size out of buffers which may be
278 template<int size
, bool big_endian
>
279 struct Swap_unaligned
;
281 template<bool big_endian
>
282 struct Swap_unaligned
<8, big_endian
>
284 typedef typename Valtype_base
<8>::Valtype Valtype
;
286 static inline Valtype
287 readval(const unsigned char* wv
)
291 writeval(unsigned char* wv
, Valtype v
)
296 struct Swap_unaligned
<16, false>
298 typedef Valtype_base
<16>::Valtype Valtype
;
300 static inline Valtype
301 readval(const unsigned char* wv
)
303 return (wv
[1] << 8) | wv
[0];
307 writeval(unsigned char* wv
, Valtype v
)
315 struct Swap_unaligned
<16, true>
317 typedef Valtype_base
<16>::Valtype Valtype
;
319 static inline Valtype
320 readval(const unsigned char* wv
)
322 return (wv
[0] << 8) | wv
[1];
326 writeval(unsigned char* wv
, Valtype v
)
334 struct Swap_unaligned
<32, false>
336 typedef Valtype_base
<32>::Valtype Valtype
;
338 static inline Valtype
339 readval(const unsigned char* wv
)
341 return (wv
[3] << 24) | (wv
[2] << 16) | (wv
[1] << 8) | wv
[0];
345 writeval(unsigned char* wv
, Valtype v
)
355 struct Swap_unaligned
<32, true>
357 typedef Valtype_base
<32>::Valtype Valtype
;
359 static inline Valtype
360 readval(const unsigned char* wv
)
362 return (wv
[0] << 24) | (wv
[1] << 16) | (wv
[2] << 8) | wv
[3];
366 writeval(unsigned char* wv
, Valtype v
)
376 struct Swap_unaligned
<64, false>
378 typedef Valtype_base
<64>::Valtype Valtype
;
380 static inline Valtype
381 readval(const unsigned char* wv
)
383 return ((static_cast<Valtype
>(wv
[7]) << 56)
384 | (static_cast<Valtype
>(wv
[6]) << 48)
385 | (static_cast<Valtype
>(wv
[5]) << 40)
386 | (static_cast<Valtype
>(wv
[4]) << 32)
387 | (static_cast<Valtype
>(wv
[3]) << 24)
388 | (static_cast<Valtype
>(wv
[2]) << 16)
389 | (static_cast<Valtype
>(wv
[1]) << 8)
390 | static_cast<Valtype
>(wv
[0]));
394 writeval(unsigned char* wv
, Valtype v
)
408 struct Swap_unaligned
<64, true>
410 typedef Valtype_base
<64>::Valtype Valtype
;
412 static inline Valtype
413 readval(const unsigned char* wv
)
415 return ((static_cast<Valtype
>(wv
[0]) << 56)
416 | (static_cast<Valtype
>(wv
[1]) << 48)
417 | (static_cast<Valtype
>(wv
[2]) << 40)
418 | (static_cast<Valtype
>(wv
[3]) << 32)
419 | (static_cast<Valtype
>(wv
[4]) << 24)
420 | (static_cast<Valtype
>(wv
[5]) << 16)
421 | (static_cast<Valtype
>(wv
[6]) << 8)
422 | static_cast<Valtype
>(wv
[7]));
426 writeval(unsigned char* wv
, Valtype v
)
439 // Swap_aligned32 is a template based on size and on whether the
440 // target is big endian. It defines the type Valtype and the
441 // functions readval and writeval. The functions read and write
442 // values of the appropriate size out of buffers which may not be
443 // 64-bit aligned, but are 32-bit aligned.
445 template<int size
, bool big_endian
>
446 struct Swap_aligned32
448 typedef typename Valtype_base
<size
>::Valtype Valtype
;
450 static inline Valtype
451 readval(const unsigned char* wv
)
452 { return Swap
<size
, big_endian
>::readval(
453 reinterpret_cast<const Valtype
*>(wv
)); }
456 writeval(unsigned char* wv
, Valtype v
)
457 { Swap
<size
, big_endian
>::writeval(reinterpret_cast<Valtype
*>(wv
), v
); }
461 struct Swap_aligned32
<64, true>
463 typedef Valtype_base
<64>::Valtype Valtype
;
465 static inline Valtype
466 readval(const unsigned char* wv
)
468 return ((static_cast<Valtype
>(Swap
<32, true>::readval(wv
)) << 32)
469 | static_cast<Valtype
>(Swap
<32, true>::readval(wv
+ 4)));
473 writeval(unsigned char* wv
, Valtype v
)
475 typedef Valtype_base
<32>::Valtype Valtype32
;
477 Swap
<32, true>::writeval(wv
, static_cast<Valtype32
>(v
>> 32));
478 Swap
<32, true>::writeval(wv
+ 4, static_cast<Valtype32
>(v
));
483 struct Swap_aligned32
<64, false>
485 typedef Valtype_base
<64>::Valtype Valtype
;
487 static inline Valtype
488 readval(const unsigned char* wv
)
490 return ((static_cast<Valtype
>(Swap
<32, false>::readval(wv
+ 4)) << 32)
491 | static_cast<Valtype
>(Swap
<32, false>::readval(wv
)));
495 writeval(unsigned char* wv
, Valtype v
)
497 typedef Valtype_base
<32>::Valtype Valtype32
;
499 Swap
<32, false>::writeval(wv
+ 4, static_cast<Valtype32
>(v
>> 32));
500 Swap
<32, false>::writeval(wv
, static_cast<Valtype32
>(v
));
504 } // End namespace elfcpp.
506 #endif // !defined(ELFCPP_SWAP_H)