1 // elfcpp_swap.h -- Handle swapping for elfcpp -*- C++ -*-
3 // This header file defines basic template classes to efficiently swap
4 // numbers between host form and target form. When the host and
5 // target have the same endianness, these turn into no-ops.
17 // Endian simply indicates whether the host is big endian or not.
22 // Used for template specializations.
23 static const bool host_big_endian
= __BYTE_ORDER
== __BIG_ENDIAN
;
26 // Valtype_base is a template based on size (8, 16, 32, 64) which
27 // defines the type Valtype as the unsigned integer of the specified
34 struct Valtype_base
<8>
36 typedef unsigned char Valtype
;
40 struct Valtype_base
<16>
42 typedef uint16_t Valtype
;
46 struct Valtype_base
<32>
48 typedef uint32_t Valtype
;
52 struct Valtype_base
<64>
54 typedef uint64_t Valtype
;
57 // Convert_endian is a template based on size and on whether the host
58 // and target have the same endianness. It defines the type Valtype
59 // as Valtype_base does, and also defines a function convert_host
60 // which takes an argument of type Valtype and returns the same value,
61 // but swapped if the host and target have different endianness.
63 template<int size
, bool same_endian
>
64 struct Convert_endian
;
67 struct Convert_endian
<size
, true>
69 typedef typename Valtype_base
<size
>::Valtype Valtype
;
72 convert_host(Valtype v
)
77 struct Convert_endian
<8, false>
79 typedef Valtype_base
<8>::Valtype Valtype
;
82 convert_host(Valtype v
)
87 struct Convert_endian
<16, false>
89 typedef Valtype_base
<16>::Valtype Valtype
;
92 convert_host(Valtype v
)
93 { return bswap_16(v
); }
97 struct Convert_endian
<32, false>
99 typedef Valtype_base
<32>::Valtype Valtype
;
101 static inline Valtype
102 convert_host(Valtype v
)
103 { return bswap_32(v
); }
107 struct Convert_endian
<64, false>
109 typedef Valtype_base
<64>::Valtype Valtype
;
111 static inline Valtype
112 convert_host(Valtype v
)
113 { return bswap_64(v
); }
116 // Convert is a template based on size and on whether the target is
117 // big endian. It defines Valtype and convert_host like
118 // Convert_endian. That is, it is just like Convert_endian except in
119 // the meaning of the second template parameter.
121 template<int size
, bool big_endian
>
124 typedef typename Valtype_base
<size
>::Valtype Valtype
;
126 static inline Valtype
127 convert_host(Valtype v
)
129 return Convert_endian
<size
, big_endian
== Endian::host_big_endian
>
134 // Swap is a template based on size and on whether the target is big
135 // endian. It defines the type Valtype and the functions readval and
136 // writeval. The functions read and write values of the appropriate
137 // size out of buffers, swapping them if necessary. readval and
138 // writeval are overloaded to take pointers to the appropriate type or
139 // pointers to unsigned char.
141 template<int size
, bool big_endian
>
144 typedef typename Valtype_base
<size
>::Valtype Valtype
;
146 static inline Valtype
147 readval(const Valtype
* wv
)
148 { return Convert
<size
, big_endian
>::convert_host(*wv
); }
151 writeval(Valtype
* wv
, Valtype v
)
152 { *wv
= Convert
<size
, big_endian
>::convert_host(v
); }
154 static inline Valtype
155 readval(const unsigned char* wv
)
156 { return readval(reinterpret_cast<const Valtype
*>(wv
)); }
159 writeval(unsigned char* wv
, Valtype v
)
160 { writeval(reinterpret_cast<Valtype
*>(wv
), v
); }
163 // We need to specialize the 8-bit version of Swap to avoid
164 // conflicting overloads, since both versions of readval and writeval
165 // will have the same type parameters.
167 template<bool big_endian
>
168 struct Swap
<8, big_endian
>
170 typedef typename Valtype_base
<8>::Valtype Valtype
;
172 static inline Valtype
173 readval(const Valtype
* wv
)
177 writeval(Valtype
* wv
, Valtype v
)
181 // Swap_unaligned is a template based on size and on whether the
182 // target is big endian. It defines the type Valtype and the
183 // functions readval and writeval. The functions read and write
184 // values of the appropriate size out of buffers which may be
187 template<int size
, bool big_endian
>
188 struct Swap_unaligned
;
190 template<bool big_endian
>
191 struct Swap_unaligned
<8, big_endian
>
193 typedef typename Valtype_base
<8>::Valtype Valtype
;
195 static inline Valtype
196 readval(const unsigned char* wv
)
200 writeval(unsigned char* wv
, Valtype v
)
205 struct Swap_unaligned
<16, false>
207 typedef Valtype_base
<16>::Valtype Valtype
;
209 static inline Valtype
210 readval(const unsigned char* wv
)
212 return (wv
[1] << 8) | wv
[0];
216 writeval(unsigned char* wv
, Valtype v
)
224 struct Swap_unaligned
<16, true>
226 typedef Valtype_base
<16>::Valtype Valtype
;
228 static inline Valtype
229 readval(const unsigned char* wv
)
231 return (wv
[0] << 8) | wv
[1];
235 writeval(unsigned char* wv
, Valtype v
)
243 struct Swap_unaligned
<32, false>
245 typedef Valtype_base
<32>::Valtype Valtype
;
247 static inline Valtype
248 readval(const unsigned char* wv
)
250 return (wv
[3] << 24) | (wv
[2] << 16) | (wv
[1] << 8) | wv
[0];
254 writeval(unsigned char* wv
, Valtype v
)
264 struct Swap_unaligned
<32, true>
266 typedef Valtype_base
<32>::Valtype Valtype
;
268 static inline Valtype
269 readval(const unsigned char* wv
)
271 return (wv
[0] << 24) | (wv
[1] << 16) | (wv
[2] << 8) | wv
[3];
275 writeval(unsigned char* wv
, Valtype v
)
285 struct Swap_unaligned
<64, false>
287 typedef Valtype_base
<64>::Valtype Valtype
;
289 static inline Valtype
290 readval(const unsigned char* wv
)
292 return ((static_cast<Valtype
>(wv
[7]) << 56)
293 | (static_cast<Valtype
>(wv
[6]) << 48)
294 | (static_cast<Valtype
>(wv
[5]) << 40)
295 | (static_cast<Valtype
>(wv
[4]) << 32)
296 | (static_cast<Valtype
>(wv
[3]) << 24)
297 | (static_cast<Valtype
>(wv
[2]) << 16)
298 | (static_cast<Valtype
>(wv
[1]) << 8)
299 | static_cast<Valtype
>(wv
[0]));
303 writeval(unsigned char* wv
, Valtype v
)
317 struct Swap_unaligned
<64, true>
319 typedef Valtype_base
<64>::Valtype Valtype
;
321 static inline Valtype
322 readval(const unsigned char* wv
)
324 return ((static_cast<Valtype
>(wv
[0]) << 56)
325 | (static_cast<Valtype
>(wv
[1]) << 48)
326 | (static_cast<Valtype
>(wv
[2]) << 40)
327 | (static_cast<Valtype
>(wv
[3]) << 32)
328 | (static_cast<Valtype
>(wv
[4]) << 24)
329 | (static_cast<Valtype
>(wv
[5]) << 16)
330 | (static_cast<Valtype
>(wv
[6]) << 8)
331 | static_cast<Valtype
>(wv
[7]));
335 writeval(unsigned char* wv
, Valtype v
)
348 } // End namespace elfcpp.
350 #endif // !defined(ELFCPP_SWAP_H)