Upx_Doxygen
https://github.com/upx/upx
p_elf.h
1 /* p_elf.h --
2 
3  This file is part of the UPX executable compressor.
4 
5  Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer
6  Copyright (C) 1996-2016 Laszlo Molnar
7  All Rights Reserved.
8 
9  UPX and the UCL library are free software; you can redistribute them
10  and/or modify them under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of
12  the License, or (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING.
21  If not, write to the Free Software Foundation, Inc.,
22  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24  Markus F.X.J. Oberhumer Laszlo Molnar
25  <markus@oberhumer.com> <ezerotven+github@gmail.com>
26  */
27 
28 
29 #ifndef __UPX_P_ELF_H
30 #define __UPX_P_ELF_H 1
31 
32 
33 /*************************************************************************
34 // N_Elf
35 **************************************************************************/
36 
37 namespace N_Elf {
38 
39 // integral types
40 template <class THalf, class TWord, class TXword, class TAddr, class TOff>
41 struct ElfITypes
42 {
43  typedef THalf Half;
44  typedef TWord Word;
45  typedef TXword Xword;
46  typedef TAddr Addr;
47  typedef TOff Off;
48  typedef THalf Section;
49  typedef THalf Versym;
50 };
51 
52 
53 // The ELF file header. This appears at the start of every ELF file.
54 template <class TElfITypes>
55 __packed_struct(Ehdr)
56  typedef typename TElfITypes::Half Half;
57  typedef typename TElfITypes::Word Word;
58  typedef typename TElfITypes::Addr Addr;
59  typedef typename TElfITypes::Off Off;
60 
61  unsigned char e_ident[16]; /* Magic number and other info */
62  Half e_type; /* Object file type */
63  Half e_machine; /* Architecture */
64  Word e_version; /* Object file version */
65  Addr e_entry; /* Entry point virtual address */
66  Off e_phoff; /* Program header table file offset */
67  Off e_shoff; /* Section header table file offset */
68  Word e_flags; /* Processor-specific flags */
69  Half e_ehsize; /* ELF header size in bytes */
70  Half e_phentsize; /* Program header table entry size */
71  Half e_phnum; /* Program header table entry count */
72  Half e_shentsize; /* Section header table entry size */
73  Half e_shnum; /* Section header table entry count */
74  Half e_shstrndx; /* Section header string table index */
75 
76 # define WANT_EHDR_ENUM 1
77 # include "p_elf_enum.h"
78 __packed_struct_end()
79 
80 
81 template <class TElfITypes>
82 __packed_struct(Dyn)
83  typedef typename TElfITypes::Xword Xword;
84  typedef typename TElfITypes::Addr Addr;
85 
86  Xword d_tag;
87  Addr d_val;
88 
89 # define WANT_DYN_ENUM 1
90 # include "p_elf_enum.h"
91 __packed_struct_end()
92 
93 
94 template <class TElfITypes>
95 __packed_struct(Rel)
96  typedef typename TElfITypes::Xword Xword;
97  typedef typename TElfITypes::Addr Addr;
98 
99  Addr r_offset;
100  Xword r_info;
101 __packed_struct_end()
102 
103 
104 template <class TElfITypes>
105 __packed_struct(Rela)
106  typedef typename TElfITypes::Xword Xword;
107  typedef typename TElfITypes::Addr Addr;
108 
109  Addr r_offset;
110  Xword r_info;
111  Xword r_addend;
112 __packed_struct_end()
113 
114 
115 template <class TElfITypes>
116 __packed_struct(External_Note)
117  typedef typename TElfITypes::Word Word;
118 
119  Word xn_namesz; // includes terminating '\0'
120  Word xn_datasz;
121  Word xn_type;
122  //char xn_name[N]; // terminate with '\0'
123  //char xn_data[M]; // aligned to 0 mod 4
124 __packed_struct_end()
125 
126 
127 } // namespace N_Elf
128 
129 
130 /*************************************************************************
131 // N_Elf32
132 **************************************************************************/
133 
134 namespace N_Elf32 {
135 
136 template <class TElfITypes>
137 __packed_struct(Phdr)
138  typedef typename TElfITypes::Word Word;
139  typedef typename TElfITypes::Addr Addr;
140  typedef typename TElfITypes::Off Off;
141 
142  Word p_type; /* Segment type */
143  Off p_offset; /* Segment file offset */
144  Addr p_vaddr; /* Segment virtual address */
145  Addr p_paddr; /* Segment physical address */
146  Word p_filesz; /* Segment size in file */
147  Word p_memsz; /* Segment size in memory */
148  Word p_flags; /* Segment flags */
149  Word p_align; /* Segment alignment */
150 
151 # define WANT_PHDR_ENUM 1
152 # include "p_elf_enum.h"
153 __packed_struct_end()
154 
155 
156 template <class TElfITypes>
157 __packed_struct(Shdr)
158  typedef typename TElfITypes::Word Word;
159  typedef typename TElfITypes::Addr Addr;
160  typedef typename TElfITypes::Off Off;
161 
162  Word sh_name; /* Section name (string tbl index) */
163  Word sh_type; /* Section type */
164  Word sh_flags; /* Section flags */
165  Addr sh_addr; /* Section virtual addr at execution */
166  Off sh_offset; /* Section file offset */
167  Word sh_size; /* Section size in bytes */
168  Word sh_link; /* Link to another section */
169  Word sh_info; /* Additional section information */
170  Word sh_addralign; /* Section alignment */
171  Word sh_entsize; /* Entry size if section holds table */
172 
173 # define WANT_SHDR_ENUM 1
174 # include "p_elf_enum.h"
175 __packed_struct_end()
176 
177 
178 template <class TElfITypes>
179 __packed_struct(Sym)
180  typedef typename TElfITypes::Word Word;
181  typedef typename TElfITypes::Addr Addr;
182  typedef typename TElfITypes::Section Section;
183 
184  Word st_name; /* symbol name (index into string table) */
185  Addr st_value; /* symbol value */
186  Word st_size; /* symbol size */
187  unsigned char st_info; /* symbol type and binding */
188  unsigned char st_other; /* symbol visibility */
189  Section st_shndx; /* section index */
190 
191 # define WANT_SYM_ENUM 1
192 # include "p_elf_enum.h"
193 
194  static unsigned int get_st_bind(unsigned x) { return 0xf & (x>>4); }
195  static unsigned int get_st_type(unsigned x) { return 0xf & x ; }
196  static unsigned char make_st_info(unsigned bind, unsigned type)
197  { return (unsigned char) (((bind<<4) + (0xf & type)) & 0xff); }
198 __packed_struct_end()
199 
200 
201 } // namespace N_Elf32
202 
203 
204 /*************************************************************************
205 // N_Elf64
206 **************************************************************************/
207 
208 namespace N_Elf64 {
209 
210 template <class TElfITypes>
211 __packed_struct(Phdr)
212  typedef typename TElfITypes::Word Word;
213  typedef typename TElfITypes::Xword Xword;
214  typedef typename TElfITypes::Addr Addr;
215  typedef typename TElfITypes::Off Off;
216 
217  Word p_type; /* Segment type */
218  Word p_flags; /* Segment flags */
219  Off p_offset; /* Segment file offset */
220  Addr p_vaddr; /* Segment virtual address */
221  Addr p_paddr; /* Segment physical address */
222  Xword p_filesz; /* Segment size in file */
223  Xword p_memsz; /* Segment size in memory */
224  Xword p_align; /* Segment alignment */
225 
226 # define WANT_PHDR_ENUM 1
227 # include "p_elf_enum.h"
228 __packed_struct_end()
229 
230 
231 template <class TElfITypes>
232 __packed_struct(Shdr)
233  typedef typename TElfITypes::Word Word;
234  typedef typename TElfITypes::Xword Xword;
235  typedef typename TElfITypes::Addr Addr;
236  typedef typename TElfITypes::Off Off;
237 
238  Word sh_name; /* Section name (string tbl index) */
239  Word sh_type; /* Section type */
240  Xword sh_flags; /* Section flags */
241  Addr sh_addr; /* Section virtual addr at execution */
242  Off sh_offset; /* Section file offset */
243  Xword sh_size; /* Section size in bytes */
244  Word sh_link; /* Link to another section */
245  Word sh_info; /* Additional section information */
246  Xword sh_addralign; /* Section alignment */
247  Xword sh_entsize; /* Entry size if section holds table */
248 
249 # define WANT_SHDR_ENUM 1
250 # include "p_elf_enum.h"
251 __packed_struct_end()
252 
253 
254 template <class TElfITypes>
255 __packed_struct(Sym)
256  typedef typename TElfITypes::Word Word;
257  typedef typename TElfITypes::Xword Xword;
258  typedef typename TElfITypes::Addr Addr;
259  typedef typename TElfITypes::Section Section;
260 
261  Word st_name; /* symbol name (index into string table) */
262  unsigned char st_info; /* symbol type and binding */
263  unsigned char st_other; /* symbol visibility */
264  Section st_shndx; /* section index */
265  Addr st_value; /* symbol value */
266  Xword st_size; /* symbol size */
267 
268 # define WANT_SYM_ENUM 1
269 # include "p_elf_enum.h"
270 
271  static unsigned int get_st_bind(unsigned x) { return 0xf & (x>>4); }
272  static unsigned int get_st_type(unsigned x) { return 0xf & x ; }
273  static unsigned char make_st_info(unsigned bind, unsigned type)
274  { return (unsigned char) (((bind<<4) + (0xf & type)) & 0xff); }
275 __packed_struct_end()
276 
277 
278 } // namespace N_Elf64
279 
280 
281 /*************************************************************************
282 // aggregate types into an ElfClass
283 **************************************************************************/
284 
285 namespace N_Elf {
286 
287 template <class TP>
289 {
290  typedef TP BeLePolicy;
291 
292  // integral types (target endianness)
293  typedef typename TP::U16 TE16;
294  typedef typename TP::U32 TE32;
295  typedef typename TP::U64 TE64;
297 
298  // ELF types
299  typedef N_Elf ::Ehdr<ElfITypes> Ehdr;
300  typedef N_Elf32::Phdr<ElfITypes> Phdr;
301  typedef N_Elf32::Shdr<ElfITypes> Shdr;
302  typedef N_Elf ::Dyn <ElfITypes> Dyn;
303  typedef N_Elf ::Rel <ElfITypes> Rel;
304  typedef N_Elf ::Rela<ElfITypes> Rela;
305  typedef N_Elf32::Sym <ElfITypes> Sym;
306  typedef N_Elf ::External_Note<ElfITypes> External_Note;
307 
308  static void compileTimeAssertions() {
309  BeLePolicy::compileTimeAssertions();
310  COMPILE_TIME_ASSERT(sizeof(Ehdr) == 52)
311  COMPILE_TIME_ASSERT(sizeof(Phdr) == 32)
312  COMPILE_TIME_ASSERT(sizeof(Shdr) == 40)
313  COMPILE_TIME_ASSERT(sizeof(Dyn) == 8)
314  COMPILE_TIME_ASSERT(sizeof(Rel) == 8)
315  COMPILE_TIME_ASSERT(sizeof(Rela) == 12)
316  COMPILE_TIME_ASSERT(sizeof(Sym) == 16)
317  COMPILE_TIME_ASSERT_ALIGNED1(Ehdr)
318  COMPILE_TIME_ASSERT_ALIGNED1(Phdr)
319  COMPILE_TIME_ASSERT_ALIGNED1(Shdr)
320  COMPILE_TIME_ASSERT_ALIGNED1(Dyn)
321  COMPILE_TIME_ASSERT_ALIGNED1(Rel)
322  COMPILE_TIME_ASSERT_ALIGNED1(Rela)
323  COMPILE_TIME_ASSERT_ALIGNED1(Sym)
324  }
325 };
326 
327 
328 template <class TP>
330 {
331  typedef TP BeLePolicy;
332 
333  // integral types (target endianness)
334  typedef typename TP::U16 TE16;
335  typedef typename TP::U32 TE32;
336  typedef typename TP::U64 TE64;
338 
339  // ELF types
340  typedef N_Elf ::Ehdr<ElfITypes> Ehdr;
341  typedef N_Elf64::Phdr<ElfITypes> Phdr;
342  typedef N_Elf64::Shdr<ElfITypes> Shdr;
343  typedef N_Elf ::Dyn <ElfITypes> Dyn;
344  typedef N_Elf ::Rel <ElfITypes> Rel;
345  typedef N_Elf ::Rela<ElfITypes> Rela;
346  typedef N_Elf64::Sym <ElfITypes> Sym;
347  typedef N_Elf ::External_Note<ElfITypes> External_Note;
348 
349  static void compileTimeAssertions() {
350  BeLePolicy::compileTimeAssertions();
351  COMPILE_TIME_ASSERT(sizeof(Ehdr) == 64)
352  COMPILE_TIME_ASSERT(sizeof(Phdr) == 56)
353  COMPILE_TIME_ASSERT(sizeof(Shdr) == 64)
354  COMPILE_TIME_ASSERT(sizeof(Dyn) == 16)
355  COMPILE_TIME_ASSERT(sizeof(Rel) == 16)
356  COMPILE_TIME_ASSERT(sizeof(Rela) == 24)
357  COMPILE_TIME_ASSERT(sizeof(Sym) == 24)
358  COMPILE_TIME_ASSERT_ALIGNED1(Ehdr)
359  COMPILE_TIME_ASSERT_ALIGNED1(Phdr)
360  COMPILE_TIME_ASSERT_ALIGNED1(Shdr)
361  COMPILE_TIME_ASSERT_ALIGNED1(Dyn)
362  COMPILE_TIME_ASSERT_ALIGNED1(Rel)
363  COMPILE_TIME_ASSERT_ALIGNED1(Rela)
364  COMPILE_TIME_ASSERT_ALIGNED1(Sym)
365  }
366 };
367 
368 
369 } // namespace N_Elf
370 
371 
378 
379 
380 /*************************************************************************
381 // shortcuts
382 **************************************************************************/
383 
384 typedef ElfClass_Host32::Ehdr Elf32_Ehdr;
385 typedef ElfClass_Host32::Phdr Elf32_Phdr;
386 typedef ElfClass_Host32::Shdr Elf32_Shdr;
387 typedef ElfClass_Host32::Dyn Elf32_Dyn;
388 typedef ElfClass_Host32::Rel Elf32_Rel;
389 typedef ElfClass_Host32::Rela Elf32_Rela;
390 typedef ElfClass_Host32::Sym Elf32_Sym;
391 typedef ElfClass_Host32::External_Note Elf32_External_Note;
392 
393 typedef ElfClass_Host64::Ehdr Elf64_Ehdr;
394 typedef ElfClass_Host64::Phdr Elf64_Phdr;
395 typedef ElfClass_Host64::Shdr Elf64_Shdr;
396 typedef ElfClass_Host64::Dyn Elf64_Dyn;
397 typedef ElfClass_Host64::Rel Elf64_Rel;
398 typedef ElfClass_Host64::Rela Elf64_Rela;
399 typedef ElfClass_Host64::Sym Elf64_Sym;
400 typedef ElfClass_Host64::External_Note Elf64_External_Note;
401 
402 typedef ElfClass_BE32::Ehdr Elf_BE32_Ehdr;
403 typedef ElfClass_BE32::Phdr Elf_BE32_Phdr;
404 typedef ElfClass_BE32::Shdr Elf_BE32_Shdr;
405 typedef ElfClass_BE32::Dyn Elf_BE32_Dyn;
406 typedef ElfClass_BE32::Rel Elf_BE32_Rel;
407 typedef ElfClass_BE32::Rela Elf_BE32_Rela;
408 typedef ElfClass_BE32::Sym Elf_BE32_Sym;
409 typedef ElfClass_BE32::External_Note Elf_BE32_External_Note;
410 
411 typedef ElfClass_BE64::Ehdr Elf_BE64_Ehdr;
412 typedef ElfClass_BE64::Phdr Elf_BE64_Phdr;
413 typedef ElfClass_BE64::Shdr Elf_BE64_Shdr;
414 typedef ElfClass_BE64::Dyn Elf_BE64_Dyn;
415 typedef ElfClass_BE64::Rel Elf_BE64_Rel;
416 typedef ElfClass_BE64::Rela Elf_BE64_Rela;
417 typedef ElfClass_BE64::Sym Elf_BE64_Sym;
418 typedef ElfClass_BE64::External_Note Elf_BE64_External_Note;
419 
420 typedef ElfClass_LE32::Ehdr Elf_LE32_Ehdr;
421 typedef ElfClass_LE32::Phdr Elf_LE32_Phdr;
422 typedef ElfClass_LE32::Shdr Elf_LE32_Shdr;
423 typedef ElfClass_LE32::Dyn Elf_LE32_Dyn;
424 typedef ElfClass_LE32::Rel Elf_LE32_Rel;
425 typedef ElfClass_LE32::Rela Elf_LE32_Rela;
426 typedef ElfClass_LE32::Sym Elf_LE32_Sym;
427 typedef ElfClass_LE32::External_Note Elf_LE32_External_Note;
428 
429 typedef ElfClass_LE64::Ehdr Elf_LE64_Ehdr;
430 typedef ElfClass_LE64::Phdr Elf_LE64_Phdr;
431 typedef ElfClass_LE64::Shdr Elf_LE64_Shdr;
432 typedef ElfClass_LE64::Dyn Elf_LE64_Dyn;
433 typedef ElfClass_LE64::Rel Elf_LE64_Rel;
434 typedef ElfClass_LE64::Rela Elf_LE64_Rela;
435 typedef ElfClass_LE64::Sym Elf_LE64_Sym;
436 typedef ElfClass_LE64::External_Note Elf_LE64_External_Note;
437 
438 
439 #endif /* already included */
440 
441 /* vim:set ts=4 sw=4 et: */
Definition: p_elf.h:329
Definition: p_elf.h:134
Definition: p_elf.h:208
Definition: p_elf.h:41
Definition: p_elf.h:37
Definition: p_elf.h:288