2006-10-18 Roy Marples <uberlord@gentoo.org>
[binutils.git] / elfcpp / elfcpp_internal.h
blob0d69baebfabb79dc6d5a245468daae30cf7ba62c
1 // elfcpp_internal.h -- internals for elfcpp -*- C++ -*-
3 // This is included by elfcpp.h, the external interface, but holds
4 // information which we want to keep private.
6 #include "elfcpp_config.h"
8 #include <byteswap.h>
10 #ifndef ELFCPP_INTERNAL_H
11 #define ELFCPP_INTERNAL_H
13 namespace elfcpp
16 namespace internal
19 #ifdef WORDS_BIG_ENDIAN
20 const bool host_big_endian = true;
21 #else
22 const bool host_big_endian = false;
23 #endif
25 // Conversion routines between target and host.
27 // Convert Elf_Half.
29 template<bool same_endian>
30 Elf_Half
31 convert_half_host(Elf_Half v);
33 template<>
34 inline Elf_Half
35 convert_half_host<true>(Elf_Half v)
37 return v;
40 template<>
41 inline Elf_Half
42 convert_half_host<false>(Elf_Half v)
44 return bswap_16(v);
47 template<bool big_endian>
48 inline Elf_Half
49 convert_half(Elf_Half v)
51 return convert_half_host<big_endian == host_big_endian>(v);
54 // Convert Elf_Word.
56 template<bool same_endian>
57 Elf_Word
58 convert_word_host(Elf_Word v);
60 template<>
61 inline Elf_Word
62 convert_word_host<true>(Elf_Word v)
64 return v;
67 template<>
68 inline Elf_Word
69 convert_word_host<false>(Elf_Word v)
71 return bswap_32(v);
74 template<bool big_endian>
75 inline Elf_Word
76 convert_word(Elf_Word v)
78 return convert_word_host<big_endian == host_big_endian>(v);
81 // Convert Elf_Xword.
83 template<bool same_endian>
84 Elf_Xword
85 convert_xword_host(Elf_Xword v);
87 template<>
88 inline Elf_Xword
89 convert_xword_host<true>(Elf_Xword v)
91 return v;
94 template<>
95 inline Elf_Xword
96 convert_xword_host<false>(Elf_Xword v)
98 return bswap_64(v);
101 template<bool big_endian>
102 inline Elf_Xword
103 convert_xword(Elf_Xword v)
105 return convert_xword_host<big_endian == host_big_endian>(v);
108 // Convert Elf_addr.
110 template<int size, bool same_endian>
111 typename Elf_types<size>::Elf_Addr
112 convert_addr_size(typename Elf_types<size>::Elf_Addr);
114 template<>
115 inline Elf_types<32>::Elf_Addr
116 convert_addr_size<32, true>(Elf_types<32>::Elf_Addr v)
118 return v;
121 template<>
122 inline Elf_types<64>::Elf_Addr
123 convert_addr_size<64, true>(Elf_types<64>::Elf_Addr v)
125 return v;
128 template<>
129 inline Elf_types<32>::Elf_Addr
130 convert_addr_size<32, false>(Elf_types<32>::Elf_Addr v)
132 return bswap_32(v);
135 template<>
136 inline Elf_types<64>::Elf_Addr
137 convert_addr_size<64, false>(Elf_types<64>::Elf_Addr v)
139 return bswap_64(v);
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)
146 return convert_addr_size<size, big_endian == host_big_endian>(v);
149 // Convert Elf_Off.
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)
155 return convert_addr_size<size, big_endian == host_big_endian>(v);
158 // Convert Elf_WXword.
160 template<int size, bool big_endian>
161 inline typename Elf_types<size>::Elf_WXword
162 convert_wxword(typename Elf_types<size>::Elf_WXword v)
164 return convert_addr_size<size, big_endian == host_big_endian>(v);
167 // Convert ELF_Swxword.
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)
173 return convert_addr_size<size, big_endian == host_big_endian>(v);
176 // The ELF file header.
178 template<int size>
179 struct Ehdr_data
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;
197 // An Elf section header.
199 template<int size>
200 struct Shdr_data
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;
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.
218 template<int size>
219 struct Phdr_data;
221 template<>
222 struct Phdr_data<32>
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;
234 template<>
235 struct Phdr_data<64>
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;
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.
251 template<int size>
252 struct Sym_data;
254 template<>
255 struct Sym_data<32>
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;
265 template<>
266 struct Sym_data<64>
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;
276 // Elf relocation table entries.
278 template<int size>
279 struct Rel_data
281 typename Elf_types<size>::Elf_Addr r_offset;
282 typename Elf_types<size>::Elf_WXword r_info;
285 template<int size>
286 struct Rela_data
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;
293 } // End namespace internal.
295 } // End namespace elfcpp.
297 #endif // !defined(ELFCPP_INTERNAL_H)