Commit | Line | Data |
---|---|---|
8d9455b4 ILT |
1 | // elfcpp_swap.h -- Handle swapping for elfcpp -*- C++ -*- |
2 | ||
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. | |
6 | ||
7 | #ifndef ELFCPP_SWAP_H | |
8 | #define ELFCPP_SWAP_H | |
9 | ||
10 | #include <stdint.h> | |
11 | #include <endian.h> | |
12 | #include <byteswap.h> | |
13 | ||
14 | namespace elfcpp | |
15 | { | |
16 | ||
17 | // Endian simply indicates whether the host is big endian or not. | |
18 | ||
19 | struct Endian | |
20 | { | |
21 | public: | |
22 | // Used for template specializations. | |
23 | static const bool host_big_endian = __BYTE_ORDER == __BIG_ENDIAN; | |
24 | }; | |
25 | ||
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 | |
28 | // size. | |
29 | ||
30 | template<int size> | |
31 | struct Valtype_base; | |
32 | ||
33 | template<> | |
34 | struct Valtype_base<8> | |
35 | { | |
36 | typedef unsigned char Valtype; | |
37 | }; | |
38 | ||
39 | template<> | |
40 | struct Valtype_base<16> | |
41 | { | |
42 | typedef uint16_t Valtype; | |
43 | }; | |
44 | ||
45 | template<> | |
46 | struct Valtype_base<32> | |
47 | { | |
48 | typedef uint32_t Valtype; | |
49 | }; | |
50 | ||
51 | template<> | |
52 | struct Valtype_base<64> | |
53 | { | |
54 | typedef uint64_t Valtype; | |
55 | }; | |
56 | ||
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. | |
62 | ||
63 | template<int size, bool same_endian> | |
64 | struct Convert_endian; | |
65 | ||
66 | template<int size> | |
67 | struct Convert_endian<size, true> | |
68 | { | |
69 | typedef typename Valtype_base<size>::Valtype Valtype; | |
70 | ||
71 | static inline Valtype | |
72 | convert_host(Valtype v) | |
73 | { return v; } | |
74 | }; | |
75 | ||
76 | template<> | |
77 | struct Convert_endian<8, false> | |
78 | { | |
79 | typedef Valtype_base<8>::Valtype Valtype; | |
80 | ||
81 | static inline Valtype | |
82 | convert_host(Valtype v) | |
83 | { return v; } | |
84 | }; | |
85 | ||
86 | template<> | |
87 | struct Convert_endian<16, false> | |
88 | { | |
89 | typedef Valtype_base<16>::Valtype Valtype; | |
90 | ||
91 | static inline Valtype | |
92 | convert_host(Valtype v) | |
93 | { return bswap_16(v); } | |
94 | }; | |
95 | ||
96 | template<> | |
97 | struct Convert_endian<32, false> | |
98 | { | |
99 | typedef Valtype_base<32>::Valtype Valtype; | |
100 | ||
101 | static inline Valtype | |
102 | convert_host(Valtype v) | |
103 | { return bswap_32(v); } | |
104 | }; | |
105 | ||
106 | template<> | |
107 | struct Convert_endian<64, false> | |
108 | { | |
109 | typedef Valtype_base<64>::Valtype Valtype; | |
110 | ||
111 | static inline Valtype | |
112 | convert_host(Valtype v) | |
113 | { return bswap_64(v); } | |
114 | }; | |
115 | ||
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. | |
120 | ||
121 | template<int size, bool big_endian> | |
122 | struct Convert | |
123 | { | |
124 | typedef typename Valtype_base<size>::Valtype Valtype; | |
125 | ||
126 | static inline Valtype | |
127 | convert_host(Valtype v) | |
128 | { | |
129 | return Convert_endian<size, big_endian == Endian::host_big_endian> | |
130 | ::convert_host(v); | |
131 | } | |
132 | }; | |
133 | ||
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. | |
140 | ||
141 | template<int size, bool big_endian> | |
142 | struct Swap | |
143 | { | |
144 | typedef typename Valtype_base<size>::Valtype Valtype; | |
145 | ||
146 | static inline Valtype | |
147 | readval(const Valtype* wv) | |
148 | { return Convert<size, big_endian>::convert_host(*wv); } | |
149 | ||
150 | static inline void | |
151 | writeval(Valtype* wv, Valtype v) | |
152 | { *wv = Convert<size, big_endian>::convert_host(v); } | |
153 | ||
154 | static inline Valtype | |
155 | readval(const unsigned char* wv) | |
156 | { return readval(reinterpret_cast<const Valtype*>(wv)); } | |
157 | ||
158 | static inline void | |
159 | writeval(unsigned char* wv, Valtype v) | |
160 | { writeval(reinterpret_cast<Valtype*>(wv), v); } | |
161 | }; | |
162 | ||
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. | |
166 | ||
167 | template<bool big_endian> | |
168 | struct Swap<8, big_endian> | |
169 | { | |
170 | typedef typename Valtype_base<8>::Valtype Valtype; | |
171 | ||
172 | static inline Valtype | |
173 | readval(const Valtype* wv) | |
174 | { return *wv; } | |
175 | ||
176 | static inline void | |
177 | writeval(Valtype* wv, Valtype v) | |
178 | { *wv = v; } | |
179 | }; | |
180 | ||
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 | |
a3ad94ed ILT |
183 | // functions readval and writeval. The functions read and write |
184 | // values of the appropriate size out of buffers which may be | |
185 | // misaligned. | |
8d9455b4 ILT |
186 | |
187 | template<int size, bool big_endian> | |
188 | struct Swap_unaligned; | |
189 | ||
190 | template<bool big_endian> | |
191 | struct Swap_unaligned<8, big_endian> | |
192 | { | |
193 | typedef typename Valtype_base<8>::Valtype Valtype; | |
194 | ||
195 | static inline Valtype | |
a3ad94ed | 196 | readval(const unsigned char* wv) |
8d9455b4 ILT |
197 | { return *wv; } |
198 | ||
199 | static inline void | |
a3ad94ed | 200 | writeval(unsigned char* wv, Valtype v) |
8d9455b4 ILT |
201 | { *wv = v; } |
202 | }; | |
203 | ||
204 | template<> | |
205 | struct Swap_unaligned<16, false> | |
206 | { | |
207 | typedef Valtype_base<16>::Valtype Valtype; | |
208 | ||
209 | static inline Valtype | |
a3ad94ed | 210 | readval(const unsigned char* wv) |
8d9455b4 ILT |
211 | { |
212 | return (wv[1] << 8) | wv[0]; | |
213 | } | |
214 | ||
215 | static inline void | |
a3ad94ed | 216 | writeval(unsigned char* wv, Valtype v) |
8d9455b4 ILT |
217 | { |
218 | wv[1] = v >> 8; | |
219 | wv[0] = v; | |
220 | } | |
221 | }; | |
222 | ||
223 | template<> | |
224 | struct Swap_unaligned<16, true> | |
225 | { | |
226 | typedef Valtype_base<16>::Valtype Valtype; | |
227 | ||
228 | static inline Valtype | |
a3ad94ed | 229 | readval(const unsigned char* wv) |
8d9455b4 ILT |
230 | { |
231 | return (wv[0] << 8) | wv[1]; | |
232 | } | |
233 | ||
234 | static inline void | |
a3ad94ed | 235 | writeval(unsigned char* wv, Valtype v) |
8d9455b4 ILT |
236 | { |
237 | wv[0] = v >> 8; | |
238 | wv[1] = v; | |
239 | } | |
240 | }; | |
241 | ||
242 | template<> | |
243 | struct Swap_unaligned<32, false> | |
244 | { | |
245 | typedef Valtype_base<32>::Valtype Valtype; | |
246 | ||
247 | static inline Valtype | |
a3ad94ed | 248 | readval(const unsigned char* wv) |
8d9455b4 ILT |
249 | { |
250 | return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0]; | |
251 | } | |
252 | ||
253 | static inline void | |
a3ad94ed | 254 | writeval(unsigned char* wv, Valtype v) |
8d9455b4 ILT |
255 | { |
256 | wv[3] = v >> 24; | |
257 | wv[2] = v >> 16; | |
258 | wv[1] = v >> 8; | |
259 | wv[0] = v; | |
260 | } | |
261 | }; | |
262 | ||
263 | template<> | |
264 | struct Swap_unaligned<32, true> | |
265 | { | |
266 | typedef Valtype_base<32>::Valtype Valtype; | |
267 | ||
268 | static inline Valtype | |
a3ad94ed | 269 | readval(const unsigned char* wv) |
8d9455b4 ILT |
270 | { |
271 | return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3]; | |
272 | } | |
273 | ||
274 | static inline void | |
a3ad94ed | 275 | writeval(unsigned char* wv, Valtype v) |
8d9455b4 ILT |
276 | { |
277 | wv[0] = v >> 24; | |
278 | wv[1] = v >> 16; | |
279 | wv[2] = v >> 8; | |
280 | wv[3] = v; | |
281 | } | |
282 | }; | |
283 | ||
284 | template<> | |
285 | struct Swap_unaligned<64, false> | |
286 | { | |
287 | typedef Valtype_base<64>::Valtype Valtype; | |
288 | ||
289 | static inline Valtype | |
a3ad94ed | 290 | readval(const unsigned char* wv) |
8d9455b4 ILT |
291 | { |
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])); | |
300 | } | |
301 | ||
302 | static inline void | |
a3ad94ed | 303 | writeval(unsigned char* wv, Valtype v) |
8d9455b4 ILT |
304 | { |
305 | wv[7] = v >> 56; | |
306 | wv[6] = v >> 48; | |
307 | wv[5] = v >> 40; | |
308 | wv[4] = v >> 32; | |
309 | wv[3] = v >> 24; | |
310 | wv[2] = v >> 16; | |
311 | wv[1] = v >> 8; | |
312 | wv[0] = v; | |
313 | } | |
314 | }; | |
315 | ||
316 | template<> | |
317 | struct Swap_unaligned<64, true> | |
318 | { | |
319 | typedef Valtype_base<64>::Valtype Valtype; | |
320 | ||
321 | static inline Valtype | |
a3ad94ed | 322 | readval(const unsigned char* wv) |
8d9455b4 ILT |
323 | { |
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])); | |
332 | } | |
333 | ||
334 | static inline void | |
a3ad94ed | 335 | writeval(unsigned char* wv, Valtype v) |
8d9455b4 ILT |
336 | { |
337 | wv[7] = v >> 56; | |
338 | wv[6] = v >> 48; | |
339 | wv[5] = v >> 40; | |
340 | wv[4] = v >> 32; | |
341 | wv[3] = v >> 24; | |
342 | wv[2] = v >> 16; | |
343 | wv[1] = v >> 8; | |
344 | wv[0] = v; | |
345 | } | |
346 | }; | |
347 | ||
348 | } // End namespace elfcpp. | |
349 | ||
350 | #endif // !defined(ELFCPP_SWAP_H) |