1*a9fa9459Szrj // elfcpp_swap.h -- Handle swapping for elfcpp   -*- C++ -*-
2*a9fa9459Szrj 
3*a9fa9459Szrj // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj 
6*a9fa9459Szrj // This file is part of elfcpp.
7*a9fa9459Szrj 
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or
9*a9fa9459Szrj // modify it under the terms of the GNU Library General Public License
10*a9fa9459Szrj // as published by the Free Software Foundation; either version 2, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj 
13*a9fa9459Szrj // In addition to the permissions in the GNU Library General Public
14*a9fa9459Szrj // License, the Free Software Foundation gives you unlimited
15*a9fa9459Szrj // permission to link the compiled version of this file into
16*a9fa9459Szrj // combinations with other programs, and to distribute those
17*a9fa9459Szrj // combinations without any restriction coming from the use of this
18*a9fa9459Szrj // file.  (The Library Public License restrictions do apply in other
19*a9fa9459Szrj // respects; for example, they cover modification of the file, and
20*a9fa9459Szrj /// distribution when not linked into a combined executable.)
21*a9fa9459Szrj 
22*a9fa9459Szrj // This program is distributed in the hope that it will be useful, but
23*a9fa9459Szrj // WITHOUT ANY WARRANTY; without even the implied warranty of
24*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25*a9fa9459Szrj // Library General Public License for more details.
26*a9fa9459Szrj 
27*a9fa9459Szrj // You should have received a copy of the GNU Library General Public
28*a9fa9459Szrj // License along with this program; if not, write to the Free Software
29*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
30*a9fa9459Szrj // 02110-1301, USA.
31*a9fa9459Szrj 
32*a9fa9459Szrj // This header file defines basic template classes to efficiently swap
33*a9fa9459Szrj // numbers between host form and target form.  When the host and
34*a9fa9459Szrj // target have the same endianness, these turn into no-ops.
35*a9fa9459Szrj 
36*a9fa9459Szrj #ifndef ELFCPP_SWAP_H
37*a9fa9459Szrj #define ELFCPP_SWAP_H
38*a9fa9459Szrj 
39*a9fa9459Szrj #include <stdint.h>
40*a9fa9459Szrj 
41*a9fa9459Szrj // We need an autoconf-generated config.h file for endianness and
42*a9fa9459Szrj // swapping.  We check two macros: WORDS_BIGENDIAN and
43*a9fa9459Szrj // HAVE_BYTESWAP_H.
44*a9fa9459Szrj 
45*a9fa9459Szrj #include "config.h"
46*a9fa9459Szrj 
47*a9fa9459Szrj #ifdef HAVE_BYTESWAP_H
48*a9fa9459Szrj #include <byteswap.h>
49*a9fa9459Szrj #else
50*a9fa9459Szrj // Provide our own versions of the byteswap functions.
51*a9fa9459Szrj inline uint16_t
bswap_16(uint16_t v)52*a9fa9459Szrj bswap_16(uint16_t v)
53*a9fa9459Szrj {
54*a9fa9459Szrj   return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
55*a9fa9459Szrj }
56*a9fa9459Szrj 
57*a9fa9459Szrj inline uint32_t
bswap_32(uint32_t v)58*a9fa9459Szrj bswap_32(uint32_t v)
59*a9fa9459Szrj {
60*a9fa9459Szrj   return (  ((v & 0xff000000) >> 24)
61*a9fa9459Szrj 	  | ((v & 0x00ff0000) >>  8)
62*a9fa9459Szrj 	  | ((v & 0x0000ff00) <<  8)
63*a9fa9459Szrj 	  | ((v & 0x000000ff) << 24));
64*a9fa9459Szrj }
65*a9fa9459Szrj 
66*a9fa9459Szrj inline uint64_t
bswap_64(uint64_t v)67*a9fa9459Szrj bswap_64(uint64_t v)
68*a9fa9459Szrj {
69*a9fa9459Szrj   return (  ((v & 0xff00000000000000ULL) >> 56)
70*a9fa9459Szrj 	  | ((v & 0x00ff000000000000ULL) >> 40)
71*a9fa9459Szrj 	  | ((v & 0x0000ff0000000000ULL) >> 24)
72*a9fa9459Szrj 	  | ((v & 0x000000ff00000000ULL) >>  8)
73*a9fa9459Szrj 	  | ((v & 0x00000000ff000000ULL) <<  8)
74*a9fa9459Szrj 	  | ((v & 0x0000000000ff0000ULL) << 24)
75*a9fa9459Szrj 	  | ((v & 0x000000000000ff00ULL) << 40)
76*a9fa9459Szrj 	  | ((v & 0x00000000000000ffULL) << 56));
77*a9fa9459Szrj }
78*a9fa9459Szrj #endif // !defined(HAVE_BYTESWAP_H)
79*a9fa9459Szrj 
80*a9fa9459Szrj // gcc 4.3 and later provides __builtin_bswap32 and __builtin_bswap64.
81*a9fa9459Szrj 
82*a9fa9459Szrj #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
83*a9fa9459Szrj #undef bswap_32
84*a9fa9459Szrj #define bswap_32 __builtin_bswap32
85*a9fa9459Szrj #undef bswap_64
86*a9fa9459Szrj #define bswap_64 __builtin_bswap64
87*a9fa9459Szrj #endif
88*a9fa9459Szrj 
89*a9fa9459Szrj namespace elfcpp
90*a9fa9459Szrj {
91*a9fa9459Szrj 
92*a9fa9459Szrj // Endian simply indicates whether the host is big endian or not.
93*a9fa9459Szrj 
94*a9fa9459Szrj struct Endian
95*a9fa9459Szrj {
96*a9fa9459Szrj  public:
97*a9fa9459Szrj   // Used for template specializations.
98*a9fa9459Szrj   static const bool host_big_endian =
99*a9fa9459Szrj #ifdef WORDS_BIGENDIAN
100*a9fa9459Szrj     true
101*a9fa9459Szrj #else
102*a9fa9459Szrj     false
103*a9fa9459Szrj #endif
104*a9fa9459Szrj     ;
105*a9fa9459Szrj };
106*a9fa9459Szrj 
107*a9fa9459Szrj // Valtype_base is a template based on size (8, 16, 32, 64) which
108*a9fa9459Szrj // defines the type Valtype as the unsigned integer, and
109*a9fa9459Szrj // Signed_valtype as the signed integer, of the specified size.
110*a9fa9459Szrj 
111*a9fa9459Szrj template<int size>
112*a9fa9459Szrj struct Valtype_base;
113*a9fa9459Szrj 
114*a9fa9459Szrj template<>
115*a9fa9459Szrj struct Valtype_base<8>
116*a9fa9459Szrj {
117*a9fa9459Szrj   typedef uint8_t Valtype;
118*a9fa9459Szrj   typedef int8_t Signed_valtype;
119*a9fa9459Szrj };
120*a9fa9459Szrj 
121*a9fa9459Szrj template<>
122*a9fa9459Szrj struct Valtype_base<16>
123*a9fa9459Szrj {
124*a9fa9459Szrj   typedef uint16_t Valtype;
125*a9fa9459Szrj   typedef int16_t Signed_valtype;
126*a9fa9459Szrj };
127*a9fa9459Szrj 
128*a9fa9459Szrj template<>
129*a9fa9459Szrj struct Valtype_base<32>
130*a9fa9459Szrj {
131*a9fa9459Szrj   typedef uint32_t Valtype;
132*a9fa9459Szrj   typedef int32_t Signed_valtype;
133*a9fa9459Szrj };
134*a9fa9459Szrj 
135*a9fa9459Szrj template<>
136*a9fa9459Szrj struct Valtype_base<64>
137*a9fa9459Szrj {
138*a9fa9459Szrj   typedef uint64_t Valtype;
139*a9fa9459Szrj   typedef int64_t Signed_valtype;
140*a9fa9459Szrj };
141*a9fa9459Szrj 
142*a9fa9459Szrj // Convert_endian is a template based on size and on whether the host
143*a9fa9459Szrj // and target have the same endianness.  It defines the type Valtype
144*a9fa9459Szrj // as Valtype_base does, and also defines a function convert_host
145*a9fa9459Szrj // which takes an argument of type Valtype and returns the same value,
146*a9fa9459Szrj // but swapped if the host and target have different endianness.
147*a9fa9459Szrj 
148*a9fa9459Szrj template<int size, bool same_endian>
149*a9fa9459Szrj struct Convert_endian;
150*a9fa9459Szrj 
151*a9fa9459Szrj template<int size>
152*a9fa9459Szrj struct Convert_endian<size, true>
153*a9fa9459Szrj {
154*a9fa9459Szrj   typedef typename Valtype_base<size>::Valtype Valtype;
155*a9fa9459Szrj 
156*a9fa9459Szrj   static inline Valtype
157*a9fa9459Szrj   convert_host(Valtype v)
158*a9fa9459Szrj   { return v; }
159*a9fa9459Szrj };
160*a9fa9459Szrj 
161*a9fa9459Szrj template<>
162*a9fa9459Szrj struct Convert_endian<8, false>
163*a9fa9459Szrj {
164*a9fa9459Szrj   typedef Valtype_base<8>::Valtype Valtype;
165*a9fa9459Szrj 
166*a9fa9459Szrj   static inline Valtype
167*a9fa9459Szrj   convert_host(Valtype v)
168*a9fa9459Szrj   { return v; }
169*a9fa9459Szrj };
170*a9fa9459Szrj 
171*a9fa9459Szrj template<>
172*a9fa9459Szrj struct Convert_endian<16, false>
173*a9fa9459Szrj {
174*a9fa9459Szrj   typedef Valtype_base<16>::Valtype Valtype;
175*a9fa9459Szrj 
176*a9fa9459Szrj   static inline Valtype
177*a9fa9459Szrj   convert_host(Valtype v)
178*a9fa9459Szrj   { return bswap_16(v); }
179*a9fa9459Szrj };
180*a9fa9459Szrj 
181*a9fa9459Szrj template<>
182*a9fa9459Szrj struct Convert_endian<32, false>
183*a9fa9459Szrj {
184*a9fa9459Szrj   typedef Valtype_base<32>::Valtype Valtype;
185*a9fa9459Szrj 
186*a9fa9459Szrj   static inline Valtype
187*a9fa9459Szrj   convert_host(Valtype v)
188*a9fa9459Szrj   { return bswap_32(v); }
189*a9fa9459Szrj };
190*a9fa9459Szrj 
191*a9fa9459Szrj template<>
192*a9fa9459Szrj struct Convert_endian<64, false>
193*a9fa9459Szrj {
194*a9fa9459Szrj   typedef Valtype_base<64>::Valtype Valtype;
195*a9fa9459Szrj 
196*a9fa9459Szrj   static inline Valtype
197*a9fa9459Szrj   convert_host(Valtype v)
198*a9fa9459Szrj   { return bswap_64(v); }
199*a9fa9459Szrj };
200*a9fa9459Szrj 
201*a9fa9459Szrj // Convert is a template based on size and on whether the target is
202*a9fa9459Szrj // big endian.  It defines Valtype and convert_host like
203*a9fa9459Szrj // Convert_endian.  That is, it is just like Convert_endian except in
204*a9fa9459Szrj // the meaning of the second template parameter.
205*a9fa9459Szrj 
206*a9fa9459Szrj template<int size, bool big_endian>
207*a9fa9459Szrj struct Convert
208*a9fa9459Szrj {
209*a9fa9459Szrj   typedef typename Valtype_base<size>::Valtype Valtype;
210*a9fa9459Szrj 
211*a9fa9459Szrj   static inline Valtype
212*a9fa9459Szrj   convert_host(Valtype v)
213*a9fa9459Szrj   {
214*a9fa9459Szrj     return Convert_endian<size, big_endian == Endian::host_big_endian>
215*a9fa9459Szrj       ::convert_host(v);
216*a9fa9459Szrj   }
217*a9fa9459Szrj };
218*a9fa9459Szrj 
219*a9fa9459Szrj // Swap is a template based on size and on whether the target is big
220*a9fa9459Szrj // endian.  It defines the type Valtype and the functions readval and
221*a9fa9459Szrj // writeval.  The functions read and write values of the appropriate
222*a9fa9459Szrj // size out of buffers, swapping them if necessary.  readval and
223*a9fa9459Szrj // writeval are overloaded to take pointers to the appropriate type or
224*a9fa9459Szrj // pointers to unsigned char.
225*a9fa9459Szrj 
226*a9fa9459Szrj template<int size, bool big_endian>
227*a9fa9459Szrj struct Swap
228*a9fa9459Szrj {
229*a9fa9459Szrj   typedef typename Valtype_base<size>::Valtype Valtype;
230*a9fa9459Szrj 
231*a9fa9459Szrj   static inline Valtype
232*a9fa9459Szrj   readval(const Valtype* wv)
233*a9fa9459Szrj   { return Convert<size, big_endian>::convert_host(*wv); }
234*a9fa9459Szrj 
235*a9fa9459Szrj   static inline void
236*a9fa9459Szrj   writeval(Valtype* wv, Valtype v)
237*a9fa9459Szrj   { *wv = Convert<size, big_endian>::convert_host(v); }
238*a9fa9459Szrj 
239*a9fa9459Szrj   static inline Valtype
240*a9fa9459Szrj   readval(const unsigned char* wv)
241*a9fa9459Szrj   { return readval(reinterpret_cast<const Valtype*>(wv)); }
242*a9fa9459Szrj 
243*a9fa9459Szrj   static inline void
244*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
245*a9fa9459Szrj   { writeval(reinterpret_cast<Valtype*>(wv), v); }
246*a9fa9459Szrj };
247*a9fa9459Szrj 
248*a9fa9459Szrj // We need to specialize the 8-bit version of Swap to avoid
249*a9fa9459Szrj // conflicting overloads, since both versions of readval and writeval
250*a9fa9459Szrj // will have the same type parameters.
251*a9fa9459Szrj 
252*a9fa9459Szrj template<bool big_endian>
253*a9fa9459Szrj struct Swap<8, big_endian>
254*a9fa9459Szrj {
255*a9fa9459Szrj   typedef typename Valtype_base<8>::Valtype Valtype;
256*a9fa9459Szrj 
257*a9fa9459Szrj   static inline Valtype
258*a9fa9459Szrj   readval(const Valtype* wv)
259*a9fa9459Szrj   { return *wv; }
260*a9fa9459Szrj 
261*a9fa9459Szrj   static inline void
262*a9fa9459Szrj   writeval(Valtype* wv, Valtype v)
263*a9fa9459Szrj   { *wv = v; }
264*a9fa9459Szrj };
265*a9fa9459Szrj 
266*a9fa9459Szrj // Swap_unaligned is a template based on size and on whether the
267*a9fa9459Szrj // target is big endian.  It defines the type Valtype and the
268*a9fa9459Szrj // functions readval and writeval.  The functions read and write
269*a9fa9459Szrj // values of the appropriate size out of buffers which may be
270*a9fa9459Szrj // misaligned.
271*a9fa9459Szrj 
272*a9fa9459Szrj template<int size, bool big_endian>
273*a9fa9459Szrj struct Swap_unaligned;
274*a9fa9459Szrj 
275*a9fa9459Szrj template<bool big_endian>
276*a9fa9459Szrj struct Swap_unaligned<8, big_endian>
277*a9fa9459Szrj {
278*a9fa9459Szrj   typedef typename Valtype_base<8>::Valtype Valtype;
279*a9fa9459Szrj 
280*a9fa9459Szrj   static inline Valtype
281*a9fa9459Szrj   readval(const unsigned char* wv)
282*a9fa9459Szrj   { return *wv; }
283*a9fa9459Szrj 
284*a9fa9459Szrj   static inline void
285*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
286*a9fa9459Szrj   { *wv = v; }
287*a9fa9459Szrj };
288*a9fa9459Szrj 
289*a9fa9459Szrj template<>
290*a9fa9459Szrj struct Swap_unaligned<16, false>
291*a9fa9459Szrj {
292*a9fa9459Szrj   typedef Valtype_base<16>::Valtype Valtype;
293*a9fa9459Szrj 
294*a9fa9459Szrj   static inline Valtype
295*a9fa9459Szrj   readval(const unsigned char* wv)
296*a9fa9459Szrj   {
297*a9fa9459Szrj     return (wv[1] << 8) | wv[0];
298*a9fa9459Szrj   }
299*a9fa9459Szrj 
300*a9fa9459Szrj   static inline void
301*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
302*a9fa9459Szrj   {
303*a9fa9459Szrj     wv[1] = v >> 8;
304*a9fa9459Szrj     wv[0] = v;
305*a9fa9459Szrj   }
306*a9fa9459Szrj };
307*a9fa9459Szrj 
308*a9fa9459Szrj template<>
309*a9fa9459Szrj struct Swap_unaligned<16, true>
310*a9fa9459Szrj {
311*a9fa9459Szrj   typedef Valtype_base<16>::Valtype Valtype;
312*a9fa9459Szrj 
313*a9fa9459Szrj   static inline Valtype
314*a9fa9459Szrj   readval(const unsigned char* wv)
315*a9fa9459Szrj   {
316*a9fa9459Szrj     return (wv[0] << 8) | wv[1];
317*a9fa9459Szrj   }
318*a9fa9459Szrj 
319*a9fa9459Szrj   static inline void
320*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
321*a9fa9459Szrj   {
322*a9fa9459Szrj     wv[0] = v >> 8;
323*a9fa9459Szrj     wv[1] = v;
324*a9fa9459Szrj   }
325*a9fa9459Szrj };
326*a9fa9459Szrj 
327*a9fa9459Szrj template<>
328*a9fa9459Szrj struct Swap_unaligned<32, false>
329*a9fa9459Szrj {
330*a9fa9459Szrj   typedef Valtype_base<32>::Valtype Valtype;
331*a9fa9459Szrj 
332*a9fa9459Szrj   static inline Valtype
333*a9fa9459Szrj   readval(const unsigned char* wv)
334*a9fa9459Szrj   {
335*a9fa9459Szrj     return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0];
336*a9fa9459Szrj   }
337*a9fa9459Szrj 
338*a9fa9459Szrj   static inline void
339*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
340*a9fa9459Szrj   {
341*a9fa9459Szrj     wv[3] = v >> 24;
342*a9fa9459Szrj     wv[2] = v >> 16;
343*a9fa9459Szrj     wv[1] = v >> 8;
344*a9fa9459Szrj     wv[0] = v;
345*a9fa9459Szrj   }
346*a9fa9459Szrj };
347*a9fa9459Szrj 
348*a9fa9459Szrj template<>
349*a9fa9459Szrj struct Swap_unaligned<32, true>
350*a9fa9459Szrj {
351*a9fa9459Szrj   typedef Valtype_base<32>::Valtype Valtype;
352*a9fa9459Szrj 
353*a9fa9459Szrj   static inline Valtype
354*a9fa9459Szrj   readval(const unsigned char* wv)
355*a9fa9459Szrj   {
356*a9fa9459Szrj     return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3];
357*a9fa9459Szrj   }
358*a9fa9459Szrj 
359*a9fa9459Szrj   static inline void
360*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
361*a9fa9459Szrj   {
362*a9fa9459Szrj     wv[0] = v >> 24;
363*a9fa9459Szrj     wv[1] = v >> 16;
364*a9fa9459Szrj     wv[2] = v >> 8;
365*a9fa9459Szrj     wv[3] = v;
366*a9fa9459Szrj   }
367*a9fa9459Szrj };
368*a9fa9459Szrj 
369*a9fa9459Szrj template<>
370*a9fa9459Szrj struct Swap_unaligned<64, false>
371*a9fa9459Szrj {
372*a9fa9459Szrj   typedef Valtype_base<64>::Valtype Valtype;
373*a9fa9459Szrj 
374*a9fa9459Szrj   static inline Valtype
375*a9fa9459Szrj   readval(const unsigned char* wv)
376*a9fa9459Szrj   {
377*a9fa9459Szrj     return ((static_cast<Valtype>(wv[7]) << 56)
378*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[6]) << 48)
379*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[5]) << 40)
380*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[4]) << 32)
381*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[3]) << 24)
382*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[2]) << 16)
383*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[1]) << 8)
384*a9fa9459Szrj 	    | static_cast<Valtype>(wv[0]));
385*a9fa9459Szrj   }
386*a9fa9459Szrj 
387*a9fa9459Szrj   static inline void
388*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
389*a9fa9459Szrj   {
390*a9fa9459Szrj     wv[7] = v >> 56;
391*a9fa9459Szrj     wv[6] = v >> 48;
392*a9fa9459Szrj     wv[5] = v >> 40;
393*a9fa9459Szrj     wv[4] = v >> 32;
394*a9fa9459Szrj     wv[3] = v >> 24;
395*a9fa9459Szrj     wv[2] = v >> 16;
396*a9fa9459Szrj     wv[1] = v >> 8;
397*a9fa9459Szrj     wv[0] = v;
398*a9fa9459Szrj   }
399*a9fa9459Szrj };
400*a9fa9459Szrj 
401*a9fa9459Szrj template<>
402*a9fa9459Szrj struct Swap_unaligned<64, true>
403*a9fa9459Szrj {
404*a9fa9459Szrj   typedef Valtype_base<64>::Valtype Valtype;
405*a9fa9459Szrj 
406*a9fa9459Szrj   static inline Valtype
407*a9fa9459Szrj   readval(const unsigned char* wv)
408*a9fa9459Szrj   {
409*a9fa9459Szrj     return ((static_cast<Valtype>(wv[0]) << 56)
410*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[1]) << 48)
411*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[2]) << 40)
412*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[3]) << 32)
413*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[4]) << 24)
414*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[5]) << 16)
415*a9fa9459Szrj 	    | (static_cast<Valtype>(wv[6]) << 8)
416*a9fa9459Szrj 	    | static_cast<Valtype>(wv[7]));
417*a9fa9459Szrj   }
418*a9fa9459Szrj 
419*a9fa9459Szrj   static inline void
420*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
421*a9fa9459Szrj   {
422*a9fa9459Szrj     wv[0] = v >> 56;
423*a9fa9459Szrj     wv[1] = v >> 48;
424*a9fa9459Szrj     wv[2] = v >> 40;
425*a9fa9459Szrj     wv[3] = v >> 32;
426*a9fa9459Szrj     wv[4] = v >> 24;
427*a9fa9459Szrj     wv[5] = v >> 16;
428*a9fa9459Szrj     wv[6] = v >> 8;
429*a9fa9459Szrj     wv[7] = v;
430*a9fa9459Szrj   }
431*a9fa9459Szrj };
432*a9fa9459Szrj 
433*a9fa9459Szrj // Swap_aligned32 is a template based on size and on whether the
434*a9fa9459Szrj // target is big endian.  It defines the type Valtype and the
435*a9fa9459Szrj // functions readval and writeval.  The functions read and write
436*a9fa9459Szrj // values of the appropriate size out of buffers which may not be
437*a9fa9459Szrj // 64-bit aligned, but are 32-bit aligned.
438*a9fa9459Szrj 
439*a9fa9459Szrj template<int size, bool big_endian>
440*a9fa9459Szrj struct Swap_aligned32
441*a9fa9459Szrj {
442*a9fa9459Szrj   typedef typename Valtype_base<size>::Valtype Valtype;
443*a9fa9459Szrj 
444*a9fa9459Szrj   static inline Valtype
445*a9fa9459Szrj   readval(const unsigned char* wv)
446*a9fa9459Szrj   { return Swap<size, big_endian>::readval(
447*a9fa9459Szrj 	reinterpret_cast<const Valtype*>(wv)); }
448*a9fa9459Szrj 
449*a9fa9459Szrj   static inline void
450*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
451*a9fa9459Szrj   { Swap<size, big_endian>::writeval(reinterpret_cast<Valtype*>(wv), v); }
452*a9fa9459Szrj };
453*a9fa9459Szrj 
454*a9fa9459Szrj template<>
455*a9fa9459Szrj struct Swap_aligned32<64, true>
456*a9fa9459Szrj {
457*a9fa9459Szrj   typedef Valtype_base<64>::Valtype Valtype;
458*a9fa9459Szrj 
459*a9fa9459Szrj   static inline Valtype
460*a9fa9459Szrj   readval(const unsigned char* wv)
461*a9fa9459Szrj   {
462*a9fa9459Szrj     return ((static_cast<Valtype>(Swap<32, true>::readval(wv)) << 32)
463*a9fa9459Szrj 	    | static_cast<Valtype>(Swap<32, true>::readval(wv + 4)));
464*a9fa9459Szrj   }
465*a9fa9459Szrj 
466*a9fa9459Szrj   static inline void
467*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
468*a9fa9459Szrj   {
469*a9fa9459Szrj     typedef Valtype_base<32>::Valtype Valtype32;
470*a9fa9459Szrj 
471*a9fa9459Szrj     Swap<32, true>::writeval(wv, static_cast<Valtype32>(v >> 32));
472*a9fa9459Szrj     Swap<32, true>::writeval(wv + 4, static_cast<Valtype32>(v));
473*a9fa9459Szrj   }
474*a9fa9459Szrj };
475*a9fa9459Szrj 
476*a9fa9459Szrj template<>
477*a9fa9459Szrj struct Swap_aligned32<64, false>
478*a9fa9459Szrj {
479*a9fa9459Szrj   typedef Valtype_base<64>::Valtype Valtype;
480*a9fa9459Szrj 
481*a9fa9459Szrj   static inline Valtype
482*a9fa9459Szrj   readval(const unsigned char* wv)
483*a9fa9459Szrj   {
484*a9fa9459Szrj     return ((static_cast<Valtype>(Swap<32, false>::readval(wv + 4)) << 32)
485*a9fa9459Szrj 	    | static_cast<Valtype>(Swap<32, false>::readval(wv)));
486*a9fa9459Szrj   }
487*a9fa9459Szrj 
488*a9fa9459Szrj   static inline void
489*a9fa9459Szrj   writeval(unsigned char* wv, Valtype v)
490*a9fa9459Szrj   {
491*a9fa9459Szrj     typedef Valtype_base<32>::Valtype Valtype32;
492*a9fa9459Szrj 
493*a9fa9459Szrj     Swap<32, false>::writeval(wv + 4, static_cast<Valtype32>(v >> 32));
494*a9fa9459Szrj     Swap<32, false>::writeval(wv, static_cast<Valtype32>(v));
495*a9fa9459Szrj   }
496*a9fa9459Szrj };
497*a9fa9459Szrj 
498*a9fa9459Szrj } // End namespace elfcpp.
499*a9fa9459Szrj 
500*a9fa9459Szrj #endif // !defined(ELFCPP_SWAP_H)
501