* gas/ppc/altivec_xcoff.s: Do not use .machine pseudo-ops as it overrides gas options.
[deliverable/binutils-gdb.git] / elfcpp / elfcpp_swap.h
CommitLineData
8d9455b4
ILT
1// elfcpp_swap.h -- Handle swapping for elfcpp -*- C++ -*-
2
6cb15b7f
ILT
3// Copyright 2006, 2007, Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of elfcpp.
7
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.
12
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.)
21
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.
26
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
30// 02110-1301, USA.
31
8d9455b4
ILT
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.
35
36#ifndef ELFCPP_SWAP_H
37#define ELFCPP_SWAP_H
38
39#include <stdint.h>
40#include <endian.h>
41#include <byteswap.h>
42
43namespace elfcpp
44{
45
46// Endian simply indicates whether the host is big endian or not.
47
48struct Endian
49{
50 public:
51 // Used for template specializations.
52 static const bool host_big_endian = __BYTE_ORDER == __BIG_ENDIAN;
53};
54
55// Valtype_base is a template based on size (8, 16, 32, 64) which
56// defines the type Valtype as the unsigned integer of the specified
57// size.
58
59template<int size>
60struct Valtype_base;
61
62template<>
63struct Valtype_base<8>
64{
65 typedef unsigned char Valtype;
66};
67
68template<>
69struct Valtype_base<16>
70{
71 typedef uint16_t Valtype;
72};
73
74template<>
75struct Valtype_base<32>
76{
77 typedef uint32_t Valtype;
78};
79
80template<>
81struct Valtype_base<64>
82{
83 typedef uint64_t Valtype;
84};
85
86// Convert_endian is a template based on size and on whether the host
87// and target have the same endianness. It defines the type Valtype
88// as Valtype_base does, and also defines a function convert_host
89// which takes an argument of type Valtype and returns the same value,
90// but swapped if the host and target have different endianness.
91
92template<int size, bool same_endian>
93struct Convert_endian;
94
95template<int size>
96struct Convert_endian<size, true>
97{
98 typedef typename Valtype_base<size>::Valtype Valtype;
99
100 static inline Valtype
101 convert_host(Valtype v)
102 { return v; }
103};
104
105template<>
106struct Convert_endian<8, false>
107{
108 typedef Valtype_base<8>::Valtype Valtype;
109
110 static inline Valtype
111 convert_host(Valtype v)
112 { return v; }
113};
114
115template<>
116struct Convert_endian<16, false>
117{
118 typedef Valtype_base<16>::Valtype Valtype;
119
120 static inline Valtype
121 convert_host(Valtype v)
122 { return bswap_16(v); }
123};
124
125template<>
126struct Convert_endian<32, false>
127{
128 typedef Valtype_base<32>::Valtype Valtype;
129
130 static inline Valtype
131 convert_host(Valtype v)
132 { return bswap_32(v); }
133};
134
135template<>
136struct Convert_endian<64, false>
137{
138 typedef Valtype_base<64>::Valtype Valtype;
139
140 static inline Valtype
141 convert_host(Valtype v)
142 { return bswap_64(v); }
143};
144
145// Convert is a template based on size and on whether the target is
146// big endian. It defines Valtype and convert_host like
147// Convert_endian. That is, it is just like Convert_endian except in
148// the meaning of the second template parameter.
149
150template<int size, bool big_endian>
151struct Convert
152{
153 typedef typename Valtype_base<size>::Valtype Valtype;
154
155 static inline Valtype
156 convert_host(Valtype v)
157 {
158 return Convert_endian<size, big_endian == Endian::host_big_endian>
159 ::convert_host(v);
160 }
161};
162
163// Swap is a template based on size and on whether the target is big
164// endian. It defines the type Valtype and the functions readval and
165// writeval. The functions read and write values of the appropriate
166// size out of buffers, swapping them if necessary. readval and
167// writeval are overloaded to take pointers to the appropriate type or
168// pointers to unsigned char.
169
170template<int size, bool big_endian>
171struct Swap
172{
173 typedef typename Valtype_base<size>::Valtype Valtype;
174
175 static inline Valtype
176 readval(const Valtype* wv)
177 { return Convert<size, big_endian>::convert_host(*wv); }
178
179 static inline void
180 writeval(Valtype* wv, Valtype v)
181 { *wv = Convert<size, big_endian>::convert_host(v); }
182
183 static inline Valtype
184 readval(const unsigned char* wv)
185 { return readval(reinterpret_cast<const Valtype*>(wv)); }
186
187 static inline void
188 writeval(unsigned char* wv, Valtype v)
189 { writeval(reinterpret_cast<Valtype*>(wv), v); }
190};
191
192// We need to specialize the 8-bit version of Swap to avoid
193// conflicting overloads, since both versions of readval and writeval
194// will have the same type parameters.
195
196template<bool big_endian>
197struct Swap<8, big_endian>
198{
199 typedef typename Valtype_base<8>::Valtype Valtype;
200
201 static inline Valtype
202 readval(const Valtype* wv)
203 { return *wv; }
204
205 static inline void
206 writeval(Valtype* wv, Valtype v)
207 { *wv = v; }
208};
209
210// Swap_unaligned is a template based on size and on whether the
211// target is big endian. It defines the type Valtype and the
a3ad94ed
ILT
212// functions readval and writeval. The functions read and write
213// values of the appropriate size out of buffers which may be
214// misaligned.
8d9455b4
ILT
215
216template<int size, bool big_endian>
217struct Swap_unaligned;
218
219template<bool big_endian>
220struct Swap_unaligned<8, big_endian>
221{
222 typedef typename Valtype_base<8>::Valtype Valtype;
223
224 static inline Valtype
a3ad94ed 225 readval(const unsigned char* wv)
8d9455b4
ILT
226 { return *wv; }
227
228 static inline void
a3ad94ed 229 writeval(unsigned char* wv, Valtype v)
8d9455b4
ILT
230 { *wv = v; }
231};
232
233template<>
234struct Swap_unaligned<16, false>
235{
236 typedef Valtype_base<16>::Valtype Valtype;
237
238 static inline Valtype
a3ad94ed 239 readval(const unsigned char* wv)
8d9455b4
ILT
240 {
241 return (wv[1] << 8) | wv[0];
242 }
243
244 static inline void
a3ad94ed 245 writeval(unsigned char* wv, Valtype v)
8d9455b4
ILT
246 {
247 wv[1] = v >> 8;
248 wv[0] = v;
249 }
250};
251
252template<>
253struct Swap_unaligned<16, true>
254{
255 typedef Valtype_base<16>::Valtype Valtype;
256
257 static inline Valtype
a3ad94ed 258 readval(const unsigned char* wv)
8d9455b4
ILT
259 {
260 return (wv[0] << 8) | wv[1];
261 }
262
263 static inline void
a3ad94ed 264 writeval(unsigned char* wv, Valtype v)
8d9455b4
ILT
265 {
266 wv[0] = v >> 8;
267 wv[1] = v;
268 }
269};
270
271template<>
272struct Swap_unaligned<32, false>
273{
274 typedef Valtype_base<32>::Valtype Valtype;
275
276 static inline Valtype
a3ad94ed 277 readval(const unsigned char* wv)
8d9455b4
ILT
278 {
279 return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0];
280 }
281
282 static inline void
a3ad94ed 283 writeval(unsigned char* wv, Valtype v)
8d9455b4
ILT
284 {
285 wv[3] = v >> 24;
286 wv[2] = v >> 16;
287 wv[1] = v >> 8;
288 wv[0] = v;
289 }
290};
291
292template<>
293struct Swap_unaligned<32, true>
294{
295 typedef Valtype_base<32>::Valtype Valtype;
296
297 static inline Valtype
a3ad94ed 298 readval(const unsigned char* wv)
8d9455b4
ILT
299 {
300 return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3];
301 }
302
303 static inline void
a3ad94ed 304 writeval(unsigned char* wv, Valtype v)
8d9455b4
ILT
305 {
306 wv[0] = v >> 24;
307 wv[1] = v >> 16;
308 wv[2] = v >> 8;
309 wv[3] = v;
310 }
311};
312
313template<>
314struct Swap_unaligned<64, false>
315{
316 typedef Valtype_base<64>::Valtype Valtype;
317
318 static inline Valtype
a3ad94ed 319 readval(const unsigned char* wv)
8d9455b4
ILT
320 {
321 return ((static_cast<Valtype>(wv[7]) << 56)
322 | (static_cast<Valtype>(wv[6]) << 48)
323 | (static_cast<Valtype>(wv[5]) << 40)
324 | (static_cast<Valtype>(wv[4]) << 32)
325 | (static_cast<Valtype>(wv[3]) << 24)
326 | (static_cast<Valtype>(wv[2]) << 16)
327 | (static_cast<Valtype>(wv[1]) << 8)
328 | static_cast<Valtype>(wv[0]));
329 }
330
331 static inline void
a3ad94ed 332 writeval(unsigned char* wv, Valtype v)
8d9455b4
ILT
333 {
334 wv[7] = v >> 56;
335 wv[6] = v >> 48;
336 wv[5] = v >> 40;
337 wv[4] = v >> 32;
338 wv[3] = v >> 24;
339 wv[2] = v >> 16;
340 wv[1] = v >> 8;
341 wv[0] = v;
342 }
343};
344
345template<>
346struct Swap_unaligned<64, true>
347{
348 typedef Valtype_base<64>::Valtype Valtype;
349
350 static inline Valtype
a3ad94ed 351 readval(const unsigned char* wv)
8d9455b4
ILT
352 {
353 return ((static_cast<Valtype>(wv[0]) << 56)
354 | (static_cast<Valtype>(wv[1]) << 48)
355 | (static_cast<Valtype>(wv[2]) << 40)
356 | (static_cast<Valtype>(wv[3]) << 32)
357 | (static_cast<Valtype>(wv[4]) << 24)
358 | (static_cast<Valtype>(wv[5]) << 16)
359 | (static_cast<Valtype>(wv[6]) << 8)
360 | static_cast<Valtype>(wv[7]));
361 }
362
363 static inline void
a3ad94ed 364 writeval(unsigned char* wv, Valtype v)
8d9455b4
ILT
365 {
366 wv[7] = v >> 56;
367 wv[6] = v >> 48;
368 wv[5] = v >> 40;
369 wv[4] = v >> 32;
370 wv[3] = v >> 24;
371 wv[2] = v >> 16;
372 wv[1] = v >> 8;
373 wv[0] = v;
374 }
375};
376
377} // End namespace elfcpp.
378
379#endif // !defined(ELFCPP_SWAP_H)
This page took 0.083617 seconds and 4 git commands to generate.