Mercurial > hg > CbC > CbC_gcc
comparison gcc/lto/lto-endian.h @ 63:b7f97abdc517 gcc-4.6-20100522
update gcc from gcc-4.5.0 to gcc-4.6
author | ryoma <e075725@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 24 May 2010 12:47:05 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
56:3c8a44c06a95 | 63:b7f97abdc517 |
---|---|
1 /* Very simple endian-ness layer for LTO object file handling | |
2 Copyright 2010 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GCC. | |
5 | |
6 GCC is free software; you can redistribute it and/or modify it under | |
7 the terms of the GNU General Public License as published by the Free | |
8 Software Foundation; either version 3, or (at your option) any later | |
9 version. | |
10 | |
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GCC; see the file COPYING3. If not see | |
18 <http://www.gnu.org/licenses/>. */ | |
19 | |
20 /* This header file provides a simple way to handle object files in | |
21 another endian-ness than the host machine. This is necesarry to | |
22 enable cross-compilation with LTO enabled. Targets that use the | |
23 ELF binary object format do not need this (libelf already handles | |
24 endian-ness) but for COFF and Mach-O the functions in this header | |
25 are used in the minimal binary object reader/writer. | |
26 | |
27 For all functions in this header, the user is responsible for | |
28 making sure that the memory accesses are valid. */ | |
29 | |
30 #ifndef GCC_LTO_ENDIAN_H | |
31 #define GCC_LTO_ENDIAN_H | |
32 | |
33 #include <stdint.h> | |
34 #include <inttypes.h> | |
35 | |
36 static inline uint16_t | |
37 get_uint16_le (const unsigned char *ptr) | |
38 { | |
39 return ptr[0] | (ptr[1] << 8); | |
40 } | |
41 | |
42 static inline uint32_t | |
43 get_uint32_le (const unsigned char *ptr) | |
44 { | |
45 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24); | |
46 } | |
47 | |
48 static inline uint64_t | |
49 get_uint64_le (const unsigned char *ptr_) | |
50 { | |
51 #define ptr (uint64_t) ptr_ | |
52 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24) | |
53 | (ptr[4] << 32) | (ptr[5] << 40) | (ptr[6] << 48) | (ptr[7] << 56); | |
54 #undef ptr | |
55 } | |
56 | |
57 static inline uint16_t | |
58 get_uint16_be (const unsigned char *ptr) | |
59 { | |
60 return ptr[1] | (ptr[2] << 8); | |
61 } | |
62 | |
63 static inline uint32_t | |
64 get_uint32_be (const unsigned char *ptr) | |
65 { | |
66 return ptr[3] | (ptr[2] << 8) | (ptr[1] << 16) | (ptr[0] << 24); | |
67 } | |
68 | |
69 static inline uint64_t | |
70 get_uint64_be (const unsigned char *ptr_) | |
71 { | |
72 #define ptr (uint64_t) ptr_ | |
73 return ptr[7] | (ptr[6] << 8) | (ptr[5] << 16) | (ptr[4] << 24) | |
74 | (ptr[3] << 32) | (ptr[2] << 40) | (ptr[1] << 48) | (ptr[0] << 56); | |
75 #undef ptr | |
76 } | |
77 | |
78 static inline void | |
79 put_uint16_le (unsigned char *ptr, uint16_t data) | |
80 { | |
81 ptr[0] = data & 0xff; | |
82 ptr[1] = (data >> 8) & 0xff; | |
83 } | |
84 | |
85 static inline void | |
86 put_uint32_le (unsigned char *ptr, uint32_t data) | |
87 { | |
88 ptr[0] = data & 0xff; | |
89 ptr[1] = (data >> 8) & 0xff; | |
90 ptr[2] = (data >> 16) & 0xff; | |
91 ptr[3] = (data >> 24) & 0xff; | |
92 } | |
93 | |
94 static inline void | |
95 put_uint64_le (unsigned char *ptr, uint64_t data) | |
96 { | |
97 ptr[0] = data & 0xff; | |
98 ptr[1] = (data >> 8) & 0xff; | |
99 ptr[2] = (data >> 16) & 0xff; | |
100 ptr[3] = (data >> 24) & 0xff; | |
101 ptr[4] = (data >> 32) & 0xff; | |
102 ptr[5] = (data >> 40) & 0xff; | |
103 ptr[6] = (data >> 48) & 0xff; | |
104 ptr[7] = (data >> 56) & 0xff; | |
105 } | |
106 | |
107 static inline void | |
108 put_uint16_be (unsigned char *ptr, uint16_t data) | |
109 { | |
110 ptr[1] = data & 0xff; | |
111 ptr[0] = (data >> 8) & 0xff; | |
112 } | |
113 | |
114 static inline void | |
115 put_uint32_be (unsigned char *ptr, uint32_t data) | |
116 { | |
117 ptr[3] = data & 0xff; | |
118 ptr[2] = (data >> 8) & 0xff; | |
119 ptr[1] = (data >> 16) & 0xff; | |
120 ptr[0] = (data >> 24) & 0xff; | |
121 } | |
122 | |
123 static inline void | |
124 put_uint64_be (unsigned char *ptr, uint64_t data) | |
125 { | |
126 ptr[7] = data & 0xff; | |
127 ptr[6] = (data >> 8) & 0xff; | |
128 ptr[5] = (data >> 16) & 0xff; | |
129 ptr[4] = (data >> 24) & 0xff; | |
130 ptr[3] = (data >> 32) & 0xff; | |
131 ptr[2] = (data >> 40) & 0xff; | |
132 ptr[1] = (data >> 48) & 0xff; | |
133 ptr[0] = (data >> 56) & 0xff; | |
134 } | |
135 | |
136 static inline void | |
137 get_string (unsigned char *ptr, char *dest, size_t len) | |
138 { | |
139 memcpy (dest, ptr, len); | |
140 } | |
141 | |
142 static inline void | |
143 put_string (unsigned char *ptr, char *src, size_t len) | |
144 { | |
145 memcpy (ptr, src, len); | |
146 } | |
147 | |
148 /* Use the target macro BYTES_BIG_ENDIAN to choose. */ | |
149 | |
150 static inline uint16_t | |
151 get_uint16 (const unsigned char *ptr) | |
152 { | |
153 if (BYTES_BIG_ENDIAN) | |
154 return get_uint16_be (ptr); | |
155 else | |
156 return get_uint16_le (ptr); | |
157 } | |
158 | |
159 static inline uint32_t | |
160 get_uint32 (const unsigned char *ptr) | |
161 { | |
162 if (BYTES_BIG_ENDIAN) | |
163 return get_uint32_be (ptr); | |
164 else | |
165 return get_uint32_le (ptr); | |
166 } | |
167 | |
168 static inline uint64_t | |
169 get_uint64 (const unsigned char *ptr) | |
170 { | |
171 if (BYTES_BIG_ENDIAN) | |
172 return get_uint64_be (ptr); | |
173 else | |
174 return get_uint64_le (ptr); | |
175 } | |
176 | |
177 static inline void | |
178 put_uint16 (unsigned char *ptr, uint16_t data) | |
179 { | |
180 if (BYTES_BIG_ENDIAN) | |
181 put_uint16_be (ptr, data); | |
182 else | |
183 put_uint16_le (ptr, data); | |
184 } | |
185 | |
186 static inline void | |
187 put_uint32 (unsigned char *ptr, uint32_t data) | |
188 { | |
189 if (BYTES_BIG_ENDIAN) | |
190 put_uint32_be (ptr, data); | |
191 else | |
192 put_uint32_le (ptr, data); | |
193 } | |
194 | |
195 static inline void | |
196 put_uint64 (unsigned char *ptr, uint64_t data) | |
197 { | |
198 if (BYTES_BIG_ENDIAN) | |
199 put_uint64_be (ptr, data); | |
200 else | |
201 put_uint64_le (ptr, data); | |
202 } | |
203 | |
204 #endif /* GCC_LTO_ENDIAN_H */ | |
205 |