Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // elfcpp_internal.h -- internals for elfcpp -*- C++ -*- |
2 | ||
3 | // This is included by elfcpp.h, the external interface, but holds | |
4 | // information which we want to keep private. | |
5 | ||
6 | #include "elfcpp_config.h" | |
7 | ||
8 | #include <byteswap.h> | |
9 | ||
10 | #ifndef ELFCPP_INTERNAL_H | |
11 | #define ELFCPP_INTERNAL_H | |
12 | ||
13 | namespace elfcpp | |
14 | { | |
15 | ||
16 | namespace internal | |
17 | { | |
18 | ||
19 | #ifdef WORDS_BIG_ENDIAN | |
20 | const bool host_big_endian = true; | |
21 | #else | |
22 | const bool host_big_endian = false; | |
23 | #endif | |
24 | ||
25 | // Conversion routines between target and host. | |
26 | ||
27 | // Convert Elf_Half. | |
28 | ||
29 | template<bool same_endian> | |
30 | Elf_Half | |
31 | convert_half_host(Elf_Half v); | |
32 | ||
33 | template<> | |
34 | inline Elf_Half | |
35 | convert_half_host<true>(Elf_Half v) | |
36 | { | |
37 | return v; | |
38 | } | |
39 | ||
40 | template<> | |
41 | inline Elf_Half | |
42 | convert_half_host<false>(Elf_Half v) | |
43 | { | |
44 | return bswap_16(v); | |
45 | } | |
46 | ||
47 | template<bool big_endian> | |
48 | inline Elf_Half | |
49 | convert_half(Elf_Half v) | |
50 | { | |
51 | return convert_half_host<big_endian == host_big_endian>(v); | |
52 | } | |
53 | ||
54 | // Convert Elf_Word. | |
55 | ||
56 | template<bool same_endian> | |
57 | Elf_Word | |
58 | convert_word_host(Elf_Word v); | |
59 | ||
60 | template<> | |
61 | inline Elf_Word | |
62 | convert_word_host<true>(Elf_Word v) | |
63 | { | |
64 | return v; | |
65 | } | |
66 | ||
67 | template<> | |
68 | inline Elf_Word | |
69 | convert_word_host<false>(Elf_Word v) | |
70 | { | |
71 | return bswap_32(v); | |
72 | } | |
73 | ||
74 | template<bool big_endian> | |
75 | inline Elf_Word | |
76 | convert_word(Elf_Word v) | |
77 | { | |
78 | return convert_word_host<big_endian == host_big_endian>(v); | |
79 | } | |
80 | ||
81 | // Convert Elf_Xword. | |
82 | ||
83 | template<bool same_endian> | |
84 | Elf_Xword | |
85 | convert_xword_host(Elf_Xword v); | |
86 | ||
87 | template<> | |
88 | inline Elf_Xword | |
89 | convert_xword_host<true>(Elf_Xword v) | |
90 | { | |
91 | return v; | |
92 | } | |
93 | ||
94 | template<> | |
95 | inline Elf_Xword | |
96 | convert_xword_host<false>(Elf_Xword v) | |
97 | { | |
98 | return bswap_64(v); | |
99 | } | |
100 | ||
101 | template<bool big_endian> | |
102 | inline Elf_Xword | |
103 | convert_xword(Elf_Xword v) | |
104 | { | |
105 | return convert_xword_host<big_endian == host_big_endian>(v); | |
106 | } | |
107 | ||
108 | // Convert Elf_addr. | |
109 | ||
110 | template<int size, bool same_endian> | |
111 | typename Elf_types<size>::Elf_Addr | |
112 | convert_addr_size(typename Elf_types<size>::Elf_Addr); | |
113 | ||
114 | template<> | |
115 | inline Elf_types<32>::Elf_Addr | |
116 | convert_addr_size<32, true>(Elf_types<32>::Elf_Addr v) | |
117 | { | |
118 | return v; | |
119 | } | |
120 | ||
121 | template<> | |
122 | inline Elf_types<64>::Elf_Addr | |
123 | convert_addr_size<64, true>(Elf_types<64>::Elf_Addr v) | |
124 | { | |
125 | return v; | |
126 | } | |
127 | ||
128 | template<> | |
129 | inline Elf_types<32>::Elf_Addr | |
130 | convert_addr_size<32, false>(Elf_types<32>::Elf_Addr v) | |
131 | { | |
132 | return bswap_32(v); | |
133 | } | |
134 | ||
135 | template<> | |
136 | inline Elf_types<64>::Elf_Addr | |
137 | convert_addr_size<64, false>(Elf_types<64>::Elf_Addr v) | |
138 | { | |
139 | return bswap_64(v); | |
140 | } | |
141 | ||
142 | template<int size, bool big_endian> | |
143 | inline typename Elf_types<size>::Elf_Addr | |
144 | convert_addr(typename Elf_types<size>::Elf_Addr v) | |
145 | { | |
146 | return convert_addr_size<size, big_endian == host_big_endian>(v); | |
147 | } | |
148 | ||
149 | // Convert Elf_Off. | |
150 | ||
151 | template<int size, bool big_endian> | |
152 | inline typename Elf_types<size>::Elf_Off | |
153 | convert_off(typename Elf_types<size>::Elf_Off v) | |
154 | { | |
155 | return convert_addr_size<size, big_endian == host_big_endian>(v); | |
156 | } | |
157 | ||
158 | // Convert Elf_WXword. | |
159 | ||
160 | template<int size, bool big_endian> | |
61ba1cf9 ILT |
161 | inline typename Elf_types<size>::Elf_WXword |
162 | convert_wxword(typename Elf_types<size>::Elf_WXword v) | |
163 | { | |
164 | return convert_addr_size<size, big_endian == host_big_endian>(v); | |
165 | } | |
166 | ||
167 | // Convert ELF_Swxword. | |
168 | ||
169 | template<int size, bool big_endian> | |
170 | inline typename Elf_types<size>::Elf_Swxword | |
171 | convert_swxword(typename Elf_types<size>::Elf_Swxword v) | |
bae7f79e ILT |
172 | { |
173 | return convert_addr_size<size, big_endian == host_big_endian>(v); | |
174 | } | |
175 | ||
176 | // The ELF file header. | |
177 | ||
178 | template<int size> | |
179 | struct Ehdr_data | |
180 | { | |
181 | unsigned char e_ident[EI_NIDENT]; | |
182 | Elf_Half e_type; | |
183 | Elf_Half e_machine; | |
184 | Elf_Word e_version; | |
185 | typename Elf_types<size>::Elf_Addr e_entry; | |
186 | typename Elf_types<size>::Elf_Off e_phoff; | |
187 | typename Elf_types<size>::Elf_Off e_shoff; | |
188 | Elf_Word e_flags; | |
189 | Elf_Half e_ehsize; | |
190 | Elf_Half e_phentsize; | |
191 | Elf_Half e_phnum; | |
192 | Elf_Half e_shentsize; | |
193 | Elf_Half e_shnum; | |
194 | Elf_Half e_shstrndx; | |
195 | }; | |
196 | ||
197 | // An Elf section header. | |
198 | ||
199 | template<int size> | |
200 | struct Shdr_data | |
201 | { | |
202 | Elf_Word sh_name; | |
203 | Elf_Word sh_type; | |
204 | typename Elf_types<size>::Elf_WXword sh_flags; | |
205 | typename Elf_types<size>::Elf_Addr sh_addr; | |
206 | typename Elf_types<size>::Elf_Off sh_offset; | |
207 | typename Elf_types<size>::Elf_WXword sh_size; | |
208 | Elf_Word sh_link; | |
209 | Elf_Word sh_info; | |
210 | typename Elf_types<size>::Elf_WXword sh_addralign; | |
211 | typename Elf_types<size>::Elf_WXword sh_entsize; | |
212 | }; | |
213 | ||
39081c14 ILT |
214 | // An ELF segment header. We use template specialization for the |
215 | // 32-bit and 64-bit versions because the fields are in a different | |
216 | // order. | |
217 | ||
218 | template<int size> | |
219 | struct Phdr_data; | |
220 | ||
221 | template<> | |
222 | struct Phdr_data<32> | |
223 | { | |
224 | Elf_Word p_type; | |
225 | Elf_types<32>::Elf_Off p_offset; | |
226 | Elf_types<32>::Elf_Addr p_vaddr; | |
227 | Elf_types<32>::Elf_Addr p_paddr; | |
228 | Elf_Word p_filesz; | |
229 | Elf_Word p_memsz; | |
230 | Elf_Word p_flags; | |
231 | Elf_Word p_align; | |
232 | }; | |
233 | ||
234 | template<> | |
235 | struct Phdr_data<64> | |
236 | { | |
237 | Elf_Word p_type; | |
238 | Elf_Word p_flags; | |
239 | Elf_types<64>::Elf_Off p_offset; | |
240 | Elf_types<64>::Elf_Addr p_vaddr; | |
241 | Elf_types<64>::Elf_Addr p_paddr; | |
242 | Elf_Xword p_filesz; | |
243 | Elf_Xword p_memsz; | |
244 | Elf_Xword p_align; | |
245 | }; | |
246 | ||
bae7f79e ILT |
247 | // An ELF symbol table entry. We use template specialization for the |
248 | // 32-bit and 64-bit versions because the fields are in a different | |
249 | // order. | |
250 | ||
251 | template<int size> | |
252 | struct Sym_data; | |
253 | ||
254 | template<> | |
255 | struct Sym_data<32> | |
256 | { | |
257 | Elf_Word st_name; | |
258 | Elf_types<32>::Elf_Addr st_value; | |
259 | Elf_Word st_size; | |
260 | unsigned char st_info; | |
261 | unsigned char st_other; | |
262 | Elf_Half st_shndx; | |
263 | }; | |
264 | ||
265 | template<> | |
266 | struct Sym_data<64> | |
267 | { | |
268 | Elf_Word st_name; | |
269 | unsigned char st_info; | |
270 | unsigned char st_other; | |
271 | Elf_Half st_shndx; | |
272 | Elf_types<64>::Elf_Addr st_value; | |
273 | Elf_Xword st_size; | |
274 | }; | |
275 | ||
61ba1cf9 ILT |
276 | // Elf relocation table entries. |
277 | ||
278 | template<int size> | |
279 | struct Rel_data | |
280 | { | |
281 | typename Elf_types<size>::Elf_Addr r_offset; | |
282 | typename Elf_types<size>::Elf_WXword r_info; | |
283 | }; | |
284 | ||
285 | template<int size> | |
286 | struct Rela_data | |
287 | { | |
288 | typename Elf_types<size>::Elf_Addr r_offset; | |
289 | typename Elf_types<size>::Elf_WXword r_info; | |
290 | typename Elf_types<size>::Elf_Swxword r_addend; | |
291 | }; | |
292 | ||
bae7f79e ILT |
293 | } // End namespace internal. |
294 | ||
295 | } // End namespace elfcpp. | |
296 | ||
297 | #endif // !defined(ELFCPP_INTERNAL_H) |