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> | |
161 | inline typename Elf_types<size>::Elf_Off | |
162 | convert_wxword(typename Elf_types<size>::Elf_Off v) | |
163 | { | |
164 | return convert_addr_size<size, big_endian == host_big_endian>(v); | |
165 | } | |
166 | ||
167 | // The ELF file header. | |
168 | ||
169 | template<int size> | |
170 | struct Ehdr_data | |
171 | { | |
172 | unsigned char e_ident[EI_NIDENT]; | |
173 | Elf_Half e_type; | |
174 | Elf_Half e_machine; | |
175 | Elf_Word e_version; | |
176 | typename Elf_types<size>::Elf_Addr e_entry; | |
177 | typename Elf_types<size>::Elf_Off e_phoff; | |
178 | typename Elf_types<size>::Elf_Off e_shoff; | |
179 | Elf_Word e_flags; | |
180 | Elf_Half e_ehsize; | |
181 | Elf_Half e_phentsize; | |
182 | Elf_Half e_phnum; | |
183 | Elf_Half e_shentsize; | |
184 | Elf_Half e_shnum; | |
185 | Elf_Half e_shstrndx; | |
186 | }; | |
187 | ||
188 | // An Elf section header. | |
189 | ||
190 | template<int size> | |
191 | struct Shdr_data | |
192 | { | |
193 | Elf_Word sh_name; | |
194 | Elf_Word sh_type; | |
195 | typename Elf_types<size>::Elf_WXword sh_flags; | |
196 | typename Elf_types<size>::Elf_Addr sh_addr; | |
197 | typename Elf_types<size>::Elf_Off sh_offset; | |
198 | typename Elf_types<size>::Elf_WXword sh_size; | |
199 | Elf_Word sh_link; | |
200 | Elf_Word sh_info; | |
201 | typename Elf_types<size>::Elf_WXword sh_addralign; | |
202 | typename Elf_types<size>::Elf_WXword sh_entsize; | |
203 | }; | |
204 | ||
205 | // An ELF symbol table entry. We use template specialization for the | |
206 | // 32-bit and 64-bit versions because the fields are in a different | |
207 | // order. | |
208 | ||
209 | template<int size> | |
210 | struct Sym_data; | |
211 | ||
212 | template<> | |
213 | struct Sym_data<32> | |
214 | { | |
215 | Elf_Word st_name; | |
216 | Elf_types<32>::Elf_Addr st_value; | |
217 | Elf_Word st_size; | |
218 | unsigned char st_info; | |
219 | unsigned char st_other; | |
220 | Elf_Half st_shndx; | |
221 | }; | |
222 | ||
223 | template<> | |
224 | struct Sym_data<64> | |
225 | { | |
226 | Elf_Word st_name; | |
227 | unsigned char st_info; | |
228 | unsigned char st_other; | |
229 | Elf_Half st_shndx; | |
230 | Elf_types<64>::Elf_Addr st_value; | |
231 | Elf_Xword st_size; | |
232 | }; | |
233 | ||
234 | } // End namespace internal. | |
235 | ||
236 | } // End namespace elfcpp. | |
237 | ||
238 | #endif // !defined(ELFCPP_INTERNAL_H) |