Snapshot. Now able to produce a minimal executable which actually
[deliverable/binutils-gdb.git] / elfcpp / elfcpp.h
1 // elfcpp.h -- main header file for elfcpp -*- C++ -*-
2
3 // This is the external interface for elfcpp.
4
5 #ifndef ELFCPP_H
6 #define ELFCPP_H
7
8 #include "elfcpp_config.h"
9
10 #include <stdint.h>
11
12 namespace elfcpp
13 {
14
15 // Basic ELF types.
16
17 // These types are always the same size.
18
19 typedef uint16_t Elf_Half;
20 typedef uint32_t Elf_Word;
21 typedef int32_t Elf_Sword;
22 typedef uint64_t Elf_Xword;
23 typedef int64_t Elf_Sxword;
24
25 // These types vary in size depending on the ELF file class. The
26 // template parameter should be 32 or 64.
27
28 template<int size>
29 struct Elf_types;
30
31 template<>
32 struct Elf_types<32>
33 {
34 typedef uint32_t Elf_Addr;
35 typedef uint32_t Elf_Off;
36 typedef uint32_t Elf_WXword;
37 typedef int32_t Elf_Swxword;
38 };
39
40 template<>
41 struct Elf_types<64>
42 {
43 typedef uint64_t Elf_Addr;
44 typedef uint64_t Elf_Off;
45 typedef uint64_t Elf_WXword;
46 typedef int64_t Elf_Swxword;
47 };
48
49 // Offsets within the Ehdr e_ident field.
50
51 const int EI_MAG0 = 0;
52 const int EI_MAG1 = 1;
53 const int EI_MAG2 = 2;
54 const int EI_MAG3 = 3;
55 const int EI_CLASS = 4;
56 const int EI_DATA = 5;
57 const int EI_VERSION = 6;
58 const int EI_OSABI = 7;
59 const int EI_ABIVERSION = 8;
60 const int EI_PAD = 9;
61 const int EI_NIDENT = 16;
62
63 // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
64
65 const int ELFMAG0 = 0x7f;
66 const int ELFMAG1 = 'E';
67 const int ELFMAG2 = 'L';
68 const int ELFMAG3 = 'F';
69
70 // The valid values found in Ehdr e_ident[EI_CLASS].
71
72 enum
73 {
74 ELFCLASSNONE = 0,
75 ELFCLASS32 = 1,
76 ELFCLASS64 = 2
77 };
78
79 // The valid values found in Ehdr e_ident[EI_DATA].
80
81 enum
82 {
83 ELFDATANONE = 0,
84 ELFDATA2LSB = 1,
85 ELFDATA2MSB = 2
86 };
87
88 // The valid values found in Ehdr e_ident[EI_VERSION] and e_version.
89
90 enum
91 {
92 EV_NONE = 0,
93 EV_CURRENT = 1
94 };
95
96 // The valid values found in Ehdr e_ident[EI_OSABI].
97
98 enum ELFOSABI
99 {
100 ELFOSABI_NONE = 0,
101 ELFOSABI_HPUX = 1,
102 ELFOSABI_NETBSD = 2,
103 // ELFOSABI_LINUX is not listed in the ELF standard.
104 ELFOSABI_LINUX = 3,
105 // ELFOSABI_HURD is not listed in the ELF standard.
106 ELFOSABI_HURD = 4,
107 ELFOSABI_SOLARIS = 6,
108 ELFOSABI_AIX = 7,
109 ELFOSABI_IRIX = 8,
110 ELFOSABI_FREEBSD = 9,
111 ELFOSABI_TRU64 = 10,
112 ELFOSABI_MODESTO = 11,
113 ELFOSABI_OPENBSD = 12,
114 ELFOSABI_OPENVMS = 13,
115 ELFOSABI_NSK = 14,
116 ELFOSABI_AROS = 15,
117 // A GNU extension for the ARM.
118 ELFOSABI_ARM = 97,
119 // A GNU extension for the MSP.
120 ELFOSABI_STANDALONE = 255
121 };
122
123 // The valid values found in the Ehdr e_type field.
124
125 enum ET
126 {
127 ET_NONE = 0,
128 ET_REL = 1,
129 ET_EXEC = 2,
130 ET_DYN = 3,
131 ET_CORE = 4,
132 ET_LOOS = 0xfe00,
133 ET_HIOS = 0xfeff,
134 ET_LOPROC = 0xff00,
135 ET_HIPROC = 0xffff
136 };
137
138 // The valid values found in the Ehdr e_machine field.
139
140 enum EM
141 {
142 EM_NONE = 0,
143 EM_M32 = 1,
144 EM_SPARC = 2,
145 EM_386 = 3,
146 EM_68K = 4,
147 EM_88K = 5,
148 // 6 used to be EM_486
149 EM_860 = 7,
150 EM_MIPS = 8,
151 EM_S370 = 9,
152 EM_MIPS_RS3_LE = 10,
153 // 11 was the old Sparc V9 ABI.
154 // 12 through 14 are reserved.
155 EM_PARISC = 15,
156 // 16 is reserved.
157 // Some old PowerPC object files use 17.
158 EM_VPP500 = 17,
159 EM_SPARC32PLUS = 18,
160 EM_960 = 19,
161 EM_PPC = 20,
162 EM_PPC64 = 21,
163 EM_S390 = 22,
164 // 23 through 35 are served.
165 EM_V800 = 36,
166 EM_FR20 = 37,
167 EM_RH32 = 38,
168 EM_RCE = 39,
169 EM_ARM = 40,
170 EM_ALPHA = 41,
171 EM_SH = 42,
172 EM_SPARCV9 = 43,
173 EM_TRICORE = 44,
174 EM_ARC = 45,
175 EM_H8_300 = 46,
176 EM_H8_300H = 47,
177 EM_H8S = 48,
178 EM_H8_500 = 49,
179 EM_IA_64 = 50,
180 EM_MIPS_X = 51,
181 EM_COLDFIRE = 52,
182 EM_68HC12 = 53,
183 EM_MMA = 54,
184 EM_PCP = 55,
185 EM_NCPU = 56,
186 EM_NDR1 = 57,
187 EM_STARCORE = 58,
188 EM_ME16 = 59,
189 EM_ST100 = 60,
190 EM_TINYJ = 61,
191 EM_X86_64 = 62,
192 EM_PDSP = 63,
193 EM_PDP10 = 64,
194 EM_PDP11 = 65,
195 EM_FX66 = 66,
196 EM_ST9PLUS = 67,
197 EM_ST7 = 68,
198 EM_68HC16 = 69,
199 EM_68HC11 = 70,
200 EM_68HC08 = 71,
201 EM_68HC05 = 72,
202 EM_SVX = 73,
203 EM_ST19 = 74,
204 EM_VAX = 75,
205 EM_CRIS = 76,
206 EM_JAVELIN = 77,
207 EM_FIREPATH = 78,
208 EM_ZSP = 79,
209 EM_MMIX = 80,
210 EM_HUANY = 81,
211 EM_PRISM = 82,
212 EM_AVR = 83,
213 EM_FR30 = 84,
214 EM_D10V = 85,
215 EM_D30V = 86,
216 EM_V850 = 87,
217 EM_M32R = 88,
218 EM_MN10300 = 89,
219 EM_MN10200 = 90,
220 EM_PJ = 91,
221 EM_OPENRISC = 92,
222 EM_ARC_A5 = 93,
223 EM_XTENSA = 94,
224 EM_VIDEOCORE = 95,
225 EM_TMM_GPP = 96,
226 EM_NS32K = 97,
227 EM_TPC = 98,
228 // Some old picoJava object files use 99 (EM_PJ is correct).
229 EM_SNP1K = 99,
230 EM_ST200 = 100,
231 EM_IP2K = 101,
232 EM_MAX = 102,
233 EM_CR = 103,
234 EM_F2MC16 = 104,
235 EM_MSP430 = 105,
236 EM_BLACKFIN = 106,
237 EM_SE_C33 = 107,
238 EM_SEP = 108,
239 EM_ARCA = 109,
240 EM_UNICORE = 110,
241 EM_ALTERA_NIOS2 = 113,
242 EM_CRX = 114,
243 // The Morph MT.
244 EM_MT = 0x2530,
245 // DLX.
246 EM_DLX = 0x5aa5,
247 // FRV.
248 EM_FRV = 0x5441,
249 // Infineon Technologies 16-bit microcontroller with C166-V2 core.
250 EM_X16X = 0x4688,
251 // Xstorym16
252 EM_XSTORMY16 = 0xad45,
253 // Renesas M32C
254 EM_M32C = 0xfeb0,
255 // Vitesse IQ2000
256 EM_IQ2000 = 0xfeba,
257 // NIOS
258 EM_NIOS32 = 0xfebb
259 // Old AVR objects used 0x1057 (EM_AVR is correct).
260 // Old MSP430 objects used 0x1059 (EM_MSP430 is correct).
261 // Old FR30 objects used 0x3330 (EM_FR30 is correct).
262 // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OPENRISC is correct).
263 // Old D10V objects used 0x7650 (EM_D10V is correct).
264 // Old D30V objects used 0x7676 (EM_D30V is correct).
265 // Old IP2X objects used 0x8217 (EM_IP2K is correct).
266 // Old PowerPC objects used 0x9025 (EM_PPC is correct).
267 // Old Alpha objects used 0x9026 (EM_ALPHA is correct).
268 // Old M32R objects used 0x9041 (EM_M32R is correct).
269 // Old V850 objects used 0x9080 (EM_V850 is correct).
270 // Old S/390 objects used 0xa390 (EM_S390 is correct).
271 // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct).
272 // Old MN10300 objects used 0xbeef (EM_MN10300 is correct).
273 // Old MN10200 objects used 0xdead (EM_MN10200 is correct).
274 };
275
276 // Special section indices.
277
278 enum
279 {
280 SHN_UNDEF = 0,
281 SHN_LORESERVE = 0xff00,
282 SHN_LOPROC = 0xff00,
283 SHN_HIPROC = 0xff1f,
284 SHN_LOOS = 0xff20,
285 SHN_HIOS = 0xff3f,
286 SHN_ABS = 0xfff1,
287 SHN_COMMON = 0xfff2,
288 SHN_XINDEX = 0xffff,
289 SHN_HIRESERVE = 0xffff
290 };
291
292 // The valid values found in the Shdr sh_type field.
293
294 enum SHT
295 {
296 SHT_NULL = 0,
297 SHT_PROGBITS = 1,
298 SHT_SYMTAB = 2,
299 SHT_STRTAB = 3,
300 SHT_RELA = 4,
301 SHT_HASH = 5,
302 SHT_DYNAMIC = 6,
303 SHT_NOTE = 7,
304 SHT_NOBITS = 8,
305 SHT_REL = 9,
306 SHT_SHLIB = 10,
307 SHT_DYNSYM = 11,
308 SHT_INIT_ARRAY = 14,
309 SHT_FINI_ARRAY = 15,
310 SHT_PREINIT_ARRAY = 16,
311 SHT_GROUP = 17,
312 SHT_SYMTAB_SHNDX = 18,
313 SHT_LOOS = 0x60000000,
314 SHT_HIOS = 0x6fffffff,
315 SHT_LOPROC = 0x70000000,
316 SHT_HIPROC = 0x7fffffff,
317 SHT_LOUSER = 0x80000000,
318 SHT_HIUSER = 0xffffffff,
319 // The remaining values are not in the standard.
320 // List of prelink dependencies.
321 SHT_GNU_LIBLIST = 0x6ffffff7,
322 // Versions defined by file.
323 SHT_SUNW_verdef = 0x6ffffffd,
324 SHT_GNU_verdef = 0x6ffffffd,
325 // Versions needed by file.
326 SHT_SUNW_verneed = 0x6ffffffe,
327 SHT_GNU_verneed = 0x6ffffffe,
328 // Symbol versions,
329 SHT_SUNW_versym = 0x6fffffff,
330 SHT_GNU_versym = 0x6fffffff,
331 };
332
333 // The valid bit flags found in the Shdr sh_flags field.
334
335 enum SHF
336 {
337 SHF_WRITE = 0x1,
338 SHF_ALLOC = 0x2,
339 SHF_EXECINSTR = 0x4,
340 SHF_MERGE = 0x10,
341 SHF_STRINGS = 0x20,
342 SHF_INFO_LINK = 0x40,
343 SHF_LINK_ORDER = 0x80,
344 SHF_OS_NONCONFORMING = 0x100,
345 SHF_GROUP = 0x200,
346 SHF_TLS = 0x400,
347 SHF_MASKOS = 0x0ff00000,
348 SHF_MASKPROC = 0xf0000000
349 };
350
351 // Bit flags which appear in the first 32-bit word of the section data
352 // of a SHT_GROUP section.
353
354 enum
355 {
356 GRP_COMDAT = 0x1,
357 GRP_MASKOS = 0x0ff00000,
358 GRP_MASKPROC = 0xf0000000
359 };
360
361 // The valid values found in the Phdr p_type field.
362
363 enum PT
364 {
365 PT_NULL = 0,
366 PT_LOAD = 1,
367 PT_DYNAMIC = 2,
368 PT_INTERP = 3,
369 PT_NOTE = 4,
370 PT_SHLIB = 5,
371 PT_PHDR = 6,
372 PT_TLS = 7,
373 PT_LOOS = 0x60000000,
374 PT_HIOS = 0x6fffffff,
375 PT_LOPROC = 0x70000000,
376 PT_HIPROC = 0x7fffffff,
377 // The remaining values are not in the standard.
378 // Frame unwind information.
379 PT_GNU_EH_FRAME = 0x6474e550,
380 PT_SUNW_EH_FRAME = 0x6474e550,
381 // Stack flags.
382 PT_GNU_STACK = 0x6474e551,
383 // Read only after relocation.
384 PT_GNU_RELRO = 0x6474e552
385 };
386
387 // The valid bit flags found in the Phdr p_flags field.
388
389 enum PF
390 {
391 PF_X = 0x1,
392 PF_W = 0x2,
393 PF_R = 0x4,
394 PF_MASKOS = 0x0ff00000,
395 PF_MASKPROC = 0xf0000000
396 };
397
398 // Symbol binding from Sym st_info field.
399
400 enum STB
401 {
402 STB_LOCAL = 0,
403 STB_GLOBAL = 1,
404 STB_WEAK = 2,
405 STB_LOOS = 10,
406 STB_HIOS = 12,
407 STB_LOPROC = 13,
408 STB_HIPROC = 15
409 };
410
411 // Symbol types from Sym st_info field.
412
413 enum STT
414 {
415 STT_NOTYPE = 0,
416 STT_OBJECT = 1,
417 STT_FUNC = 2,
418 STT_SECTION = 3,
419 STT_FILE = 4,
420 STT_COMMON = 5,
421 STT_TLS = 6,
422 STT_LOOS = 10,
423 STT_HIOS = 12,
424 STT_LOPROC = 13,
425 STT_HIPROC = 15
426 };
427
428 inline STB
429 elf_st_bind(unsigned char info)
430 {
431 return static_cast<STB>(info >> 4);
432 }
433
434 inline STT
435 elf_st_type(unsigned char info)
436 {
437 return static_cast<STT>(info & 0xf);
438 }
439
440 inline unsigned char
441 elf_st_info(STB bind, STT type)
442 {
443 return ((static_cast<unsigned char>(bind) << 4)
444 + (static_cast<unsigned char>(type) & 0xf));
445 }
446
447 // Symbol visibility from Sym st_other field.
448
449 enum STV
450 {
451 STV_DEFAULT = 0,
452 STV_INTERNAL = 1,
453 STV_HIDDEN = 2,
454 STV_PROTECTED = 3
455 };
456
457 inline STV
458 elf_st_visibility(unsigned char other)
459 {
460 return static_cast<STV>(other & 0x3);
461 }
462
463 inline unsigned char
464 elf_st_nonvis(unsigned char other)
465 {
466 return static_cast<STV>(other >> 2);
467 }
468
469 inline unsigned char
470 elf_st_other(STV vis, unsigned char nonvis)
471 {
472 return ((nonvis << 2)
473 + (static_cast<unsigned char>(vis) & 3));
474 }
475
476 // Reloc information from Rel/Rela r_info field.
477
478 template<int size>
479 unsigned int
480 elf_r_sym(typename Elf_types<size>::Elf_WXword);
481
482 template<>
483 inline unsigned int
484 elf_r_sym<32>(Elf_Word v)
485 {
486 return v >> 8;
487 }
488
489 template<>
490 inline unsigned int
491 elf_r_sym<64>(Elf_Xword v)
492 {
493 return v >> 32;
494 }
495
496 template<int size>
497 unsigned int
498 elf_r_type(typename Elf_types<size>::Elf_WXword);
499
500 template<>
501 inline unsigned int
502 elf_r_type<32>(Elf_Word v)
503 {
504 return v & 0xff;
505 }
506
507 template<>
508 inline unsigned int
509 elf_r_type<64>(Elf_Xword v)
510 {
511 return v & 0xffffffff;
512 }
513
514 template<int size>
515 typename Elf_types<size>::Elf_WXword
516 elf_r_info(unsigned int s, unsigned int t);
517
518 template<>
519 inline Elf_Word
520 elf_r_info<32>(unsigned int s, unsigned int t)
521 {
522 return (s << 8) + (t & 0xff);
523 }
524
525 template<>
526 inline Elf_Xword
527 elf_r_info<64>(unsigned int s, unsigned int t)
528 {
529 return (static_cast<Elf_Xword>(s) << 32) + (t & 0xffffffff);
530 }
531
532 } // End namespace elfcpp.
533
534 // Include internal details after defining the types.
535 #include "elfcpp_internal.h"
536
537 namespace elfcpp
538 {
539
540 // The offset of the ELF file header in the ELF file.
541
542 const int file_header_offset = 0;
543
544 // ELF structure sizes.
545
546 template<int size>
547 struct Elf_sizes
548 {
549 // Size of ELF file header.
550 static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
551 // Size of ELF segment header.
552 static const int phdr_size = sizeof(internal::Phdr_data<size>);
553 // Size of ELF section header.
554 static const int shdr_size = sizeof(internal::Shdr_data<size>);
555 // Size of ELF symbol table entry.
556 static const int sym_size = sizeof(internal::Sym_data<size>);
557 // Sizes of ELF reloc entries.
558 static const int rel_size = sizeof(internal::Rel_data<size>);
559 static const int rela_size = sizeof(internal::Rela_data<size>);
560 };
561
562 // Given the address of an Elf_Word, return the value.
563
564 template<bool big_endian>
565 inline Elf_Word
566 read_elf_word(const Elf_Word* p)
567 {
568 return internal::convert_word<big_endian>(*p);
569 }
570
571 // Store an Elf_Word into an address.
572
573 template<bool big_endian>
574 inline void
575 write_elf_word(Elf_Word* p, Elf_Word v)
576 {
577 *p = internal::convert_word<big_endian>(v);
578 }
579
580 // Accessor class for the ELF file header.
581
582 template<int size, bool big_endian>
583 class Ehdr
584 {
585 public:
586 Ehdr(const unsigned char* p)
587 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
588 { }
589
590 const unsigned char*
591 get_e_ident() const
592 { return this->p_->e_ident; }
593
594 Elf_Half
595 get_e_type() const
596 { return internal::convert_half<big_endian>(this->p_->e_type); }
597
598 Elf_Half
599 get_e_machine() const
600 { return internal::convert_half<big_endian>(this->p_->e_machine); }
601
602 Elf_Word
603 get_e_version() const
604 { return internal::convert_word<big_endian>(this->p_->e_version); }
605
606 typename Elf_types<size>::Elf_Addr
607 get_e_entry() const
608 { return internal::convert_addr<size, big_endian>(this->p_->e_entry); }
609
610 typename Elf_types<size>::Elf_Off
611 get_e_phoff() const
612 { return internal::convert_off<size, big_endian>(this->p_->e_phoff); }
613
614 typename Elf_types<size>::Elf_Off
615 get_e_shoff() const
616 { return internal::convert_off<size, big_endian>(this->p_->e_shoff); }
617
618 Elf_Word
619 get_e_flags() const
620 { return internal::convert_word<big_endian>(this->p_->e_flags); }
621
622 Elf_Half
623 get_e_ehsize() const
624 { return internal::convert_half<big_endian>(this->p_->e_ehsize); }
625
626 Elf_Half
627 get_e_phentsize() const
628 { return internal::convert_half<big_endian>(this->p_->e_phentsize); }
629
630 Elf_Half
631 get_e_phnum() const
632 { return internal::convert_half<big_endian>(this->p_->e_phnum); }
633
634 Elf_Half
635 get_e_shentsize() const
636 { return internal::convert_half<big_endian>(this->p_->e_shentsize); }
637
638 Elf_Half
639 get_e_shnum() const
640 { return internal::convert_half<big_endian>(this->p_->e_shnum); }
641
642 Elf_Half
643 get_e_shstrndx() const
644 { return internal::convert_half<big_endian>(this->p_->e_shstrndx); }
645
646 private:
647 const internal::Ehdr_data<size>* p_;
648 };
649
650 // Write class for the ELF file header.
651
652 template<int size, bool big_endian>
653 class Ehdr_write
654 {
655 public:
656 Ehdr_write(unsigned char* p)
657 : p_(reinterpret_cast<internal::Ehdr_data<size>*>(p))
658 { }
659
660 void
661 put_e_ident(const unsigned char v[EI_NIDENT]) const
662 { memcpy(this->p_->e_ident, v, EI_NIDENT); }
663
664 void
665 put_e_type(Elf_Half v)
666 { this->p_->e_type = internal::convert_half<big_endian>(v); }
667
668 void
669 put_e_machine(Elf_Half v)
670 { this->p_->e_machine = internal::convert_half<big_endian>(v); }
671
672 void
673 put_e_version(Elf_Word v)
674 { this->p_->e_version = internal::convert_word<big_endian>(v); }
675
676 void
677 put_e_entry(typename Elf_types<size>::Elf_Addr v)
678 { this->p_->e_entry = internal::convert_addr<size, big_endian>(v); }
679
680 void
681 put_e_phoff(typename Elf_types<size>::Elf_Off v)
682 { this->p_->e_phoff = internal::convert_off<size, big_endian>(v); }
683
684 void
685 put_e_shoff(typename Elf_types<size>::Elf_Off v)
686 { this->p_->e_shoff = internal::convert_off<size, big_endian>(v); }
687
688 void
689 put_e_flags(Elf_Word v)
690 { this->p_->e_flags = internal::convert_word<big_endian>(v); }
691
692 void
693 put_e_ehsize(Elf_Half v)
694 { this->p_->e_ehsize = internal::convert_half<big_endian>(v); }
695
696 void
697 put_e_phentsize(Elf_Half v)
698 { this->p_->e_phentsize = internal::convert_half<big_endian>(v); }
699
700 void
701 put_e_phnum(Elf_Half v)
702 { this->p_->e_phnum = internal::convert_half<big_endian>(v); }
703
704 void
705 put_e_shentsize(Elf_Half v)
706 { this->p_->e_shentsize = internal::convert_half<big_endian>(v); }
707
708 void
709 put_e_shnum(Elf_Half v)
710 { this->p_->e_shnum = internal::convert_half<big_endian>(v); }
711
712 void
713 put_e_shstrndx(Elf_Half v)
714 { this->p_->e_shstrndx = internal::convert_half<big_endian>(v); }
715
716 private:
717 internal::Ehdr_data<size>* p_;
718 };
719
720 // Accessor class for an ELF section header.
721
722 template<int size, bool big_endian>
723 class Shdr
724 {
725 public:
726 Shdr(const unsigned char* p)
727 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
728 { }
729
730 Elf_Word
731 get_sh_name() const
732 { return internal::convert_word<big_endian>(this->p_->sh_name); }
733
734 Elf_Word
735 get_sh_type() const
736 { return internal::convert_word<big_endian>(this->p_->sh_type); }
737
738 typename Elf_types<size>::Elf_WXword
739 get_sh_flags() const
740 { return internal::convert_wxword<size, big_endian>(this->p_->sh_flags); }
741
742 typename Elf_types<size>::Elf_Addr
743 get_sh_addr() const
744 { return internal::convert_addr<size, big_endian>(this->p_->sh_addr); }
745
746 typename Elf_types<size>::Elf_Off
747 get_sh_offset() const
748 { return internal::convert_off<size, big_endian>(this->p_->sh_offset); }
749
750 typename Elf_types<size>::Elf_WXword
751 get_sh_size() const
752 { return internal::convert_wxword<size, big_endian>(this->p_->sh_size); }
753
754 Elf_Word
755 get_sh_link() const
756 { return internal::convert_word<big_endian>(this->p_->sh_link); }
757
758 Elf_Word
759 get_sh_info() const
760 { return internal::convert_word<big_endian>(this->p_->sh_info); }
761
762 typename Elf_types<size>::Elf_WXword
763 get_sh_addralign() const
764 { return
765 internal::convert_wxword<size, big_endian>(this->p_->sh_addralign); }
766
767 typename Elf_types<size>::Elf_WXword
768 get_sh_entsize() const
769 { return internal::convert_wxword<size, big_endian>(this->p_->sh_entsize); }
770
771 private:
772 const internal::Shdr_data<size>* p_;
773 };
774
775 // Write class for an ELF section header.
776
777 template<int size, bool big_endian>
778 class Shdr_write
779 {
780 public:
781 Shdr_write(unsigned char* p)
782 : p_(reinterpret_cast<internal::Shdr_data<size>*>(p))
783 { }
784
785 void
786 put_sh_name(Elf_Word v)
787 { this->p_->sh_name = internal::convert_word<big_endian>(v); }
788
789 void
790 put_sh_type(Elf_Word v)
791 { this->p_->sh_type = internal::convert_word<big_endian>(v); }
792
793 void
794 put_sh_flags(typename Elf_types<size>::Elf_WXword v)
795 { this->p_->sh_flags = internal::convert_wxword<size, big_endian>(v); }
796
797 void
798 put_sh_addr(typename Elf_types<size>::Elf_Addr v)
799 { this->p_->sh_addr = internal::convert_addr<size, big_endian>(v); }
800
801 void
802 put_sh_offset(typename Elf_types<size>::Elf_Off v)
803 { this->p_->sh_offset = internal::convert_off<size, big_endian>(v); }
804
805 void
806 put_sh_size(typename Elf_types<size>::Elf_WXword v)
807 { this->p_->sh_size = internal::convert_wxword<size, big_endian>(v); }
808
809 void
810 put_sh_link(Elf_Word v)
811 { this->p_->sh_link = internal::convert_word<big_endian>(v); }
812
813 void
814 put_sh_info(Elf_Word v)
815 { this->p_->sh_info = internal::convert_word<big_endian>(v); }
816
817 void
818 put_sh_addralign(typename Elf_types<size>::Elf_WXword v)
819 { this->p_->sh_addralign = internal::convert_wxword<size, big_endian>(v); }
820
821 void
822 put_sh_entsize(typename Elf_types<size>::Elf_WXword v)
823 { this->p_->sh_entsize = internal::convert_wxword<size, big_endian>(v); }
824
825 private:
826 internal::Shdr_data<size>* p_;
827 };
828
829 // Accessor class for an ELF segment header.
830
831 template<int size, bool big_endian>
832 class Phdr
833 {
834 public:
835 Phdr(const unsigned char* p)
836 : p_(reinterpret_cast<const internal::Phdr_data<size>*>(p))
837 { }
838
839 Elf_Word
840 get_p_type() const
841 { return internal::convert_word<big_endian>(this->p_->p_type); }
842
843 typename Elf_types<size>::Elf_Off
844 get_p_offset() const
845 { return internal::convert_off<size, big_endian>(this->p_->p_offset); }
846
847 typename Elf_types<size>::Elf_Addr
848 get_p_vaddr() const
849 { return internal::convert_addr<size, big_endian>(this->p_->p_vaddr); }
850
851 typename Elf_types<size>::Elf_Addr
852 get_p_paddr() const
853 { return internal::convert_addr<size, big_endian>(this->p_->p_paddr); }
854
855 typename Elf_types<size>::Elf_WXword
856 get_p_filesz() const
857 { return internal::convert_wxword<size, big_endian>(this->p_->p_filesz); }
858
859 typename Elf_types<size>::Elf_WXword
860 get_p_memsz() const
861 { return internal::convert_wxword<size, big_endian>(this->p_->p_memsz); }
862
863 Elf_Word
864 get_p_flags() const
865 { return internal::convert_word<big_endian>(this->p_->p_flags); }
866
867 typename Elf_types<size>::Elf_WXword
868 get_p_align() const
869 { return internal::convert_wxword<size, big_endian>(this->p_->p_align); }
870
871 private:
872 const internal::Phdr_data<size>* p_;
873 };
874
875 // Write class for an ELF segment header.
876
877 template<int size, bool big_endian>
878 class Phdr_write
879 {
880 public:
881 Phdr_write(unsigned char* p)
882 : p_(reinterpret_cast<internal::Phdr_data<size>*>(p))
883 { }
884
885 void
886 put_p_type(Elf_Word v)
887 { this->p_->p_type = internal::convert_word<big_endian>(v); }
888
889 void
890 put_p_offset(typename Elf_types<size>::Elf_Off v)
891 { this->p_->p_offset = internal::convert_off<size, big_endian>(v); }
892
893 void
894 put_p_vaddr(typename Elf_types<size>::Elf_Addr v)
895 { this->p_->p_vaddr = internal::convert_addr<size, big_endian>(v); }
896
897 void
898 put_p_paddr(typename Elf_types<size>::Elf_Addr v)
899 { this->p_->p_paddr = internal::convert_addr<size, big_endian>(v); }
900
901 void
902 put_p_filesz(typename Elf_types<size>::Elf_WXword v)
903 { this->p_->p_filesz = internal::convert_wxword<size, big_endian>(v); }
904
905 void
906 put_p_memsz(typename Elf_types<size>::Elf_WXword v)
907 { this->p_->p_memsz = internal::convert_wxword<size, big_endian>(v); }
908
909 void
910 put_p_flags(Elf_Word v)
911 { this->p_->p_flags = internal::convert_word<big_endian>(v); }
912
913 void
914 put_p_align(typename Elf_types<size>::Elf_WXword v)
915 { this->p_->p_align = internal::convert_wxword<size, big_endian>(v); }
916
917 private:
918 internal::Phdr_data<size>* p_;
919 };
920
921 // Accessor class for an ELF symbol table entry.
922
923 template<int size, bool big_endian>
924 class Sym
925 {
926 public:
927 Sym(const unsigned char* p)
928 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
929 { }
930
931 Elf_Word
932 get_st_name() const
933 { return internal::convert_word<big_endian>(this->p_->st_name); }
934
935 typename Elf_types<size>::Elf_Addr
936 get_st_value() const
937 { return internal::convert_addr<size, big_endian>(this->p_->st_value); }
938
939 typename Elf_types<size>::Elf_WXword
940 get_st_size() const
941 { return internal::convert_wxword<size, big_endian>(this->p_->st_size); }
942
943 unsigned char
944 get_st_info() const
945 { return this->p_->st_info; }
946
947 STB
948 get_st_bind() const
949 { return elf_st_bind(this->get_st_info()); }
950
951 STT
952 get_st_type() const
953 { return elf_st_type(this->get_st_info()); }
954
955 unsigned char
956 get_st_other() const
957 { return this->p_->st_other; }
958
959 STV
960 get_st_visibility() const
961 { return elf_st_visibility(this->get_st_other()); }
962
963 unsigned char
964 get_st_nonvis() const
965 { return elf_st_nonvis(this->get_st_other()); }
966
967 Elf_Half
968 get_st_shndx() const
969 { return internal::convert_half<big_endian>(this->p_->st_shndx); }
970
971 private:
972 const internal::Sym_data<size>* p_;
973 };
974
975 // Writer class for an ELF symbol table entry.
976
977 template<int size, bool big_endian>
978 class Sym_write
979 {
980 public:
981 Sym_write(unsigned char* p)
982 : p_(reinterpret_cast<internal::Sym_data<size>*>(p))
983 { }
984
985 void
986 put_st_name(Elf_Word v)
987 { this->p_->st_name = internal::convert_word<big_endian>(v); }
988
989 void
990 put_st_value(typename Elf_types<size>::Elf_Addr v)
991 { this->p_->st_value = internal::convert_addr<size, big_endian>(v); }
992
993 void
994 put_st_size(typename Elf_types<size>::Elf_WXword v)
995 { this->p_->st_size = internal::convert_wxword<size, big_endian>(v); }
996
997 void
998 put_st_info(unsigned char v)
999 { this->p_->st_info = v; }
1000
1001 void
1002 put_st_info(STB bind, STT type)
1003 { this->p_->st_info = elf_st_info(bind, type); }
1004
1005 void
1006 put_st_other(unsigned char v)
1007 { this->p_->st_other = v; }
1008
1009 void
1010 put_st_other(STV vis, unsigned char nonvis)
1011 { this->p_->st_other = elf_st_other(vis, nonvis); }
1012
1013 void
1014 put_st_shndx(Elf_Half v)
1015 { this->p_->st_shndx = internal::convert_half<big_endian>(v); }
1016
1017 Sym<size, big_endian>
1018 sym()
1019 { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); }
1020
1021 private:
1022 internal::Sym_data<size>* p_;
1023 };
1024
1025 // Accessor classes for Elf relocation table entries.
1026
1027 template<int size, bool big_endian>
1028 class Rel
1029 {
1030 public:
1031 Rel(const unsigned char* p)
1032 : p_(reinterpret_cast<const internal::Rel_data<size>*>(p))
1033 { }
1034
1035 typename Elf_types<size>::Elf_Addr
1036 get_r_offset() const
1037 { return internal::convert_addr<size, big_endian>(this->p_->r_offset); }
1038
1039 typename Elf_types<size>::Elf_WXword
1040 get_r_info() const
1041 { return internal::convert_wxword<size, big_endian>(this->p_->r_info); }
1042
1043 private:
1044 const internal::Rel_data<size>* p_;
1045 };
1046
1047 template<int size, bool big_endian>
1048 class Rela
1049 {
1050 public:
1051 Rela(const unsigned char* p)
1052 : p_(reinterpret_cast<const internal::Rela_data<size>*>(p))
1053 { }
1054
1055 typename Elf_types<size>::Elf_Addr
1056 get_r_offset() const
1057 { return internal::convert_addr<size, big_endian>(this->p_->r_offset); }
1058
1059 typename Elf_types<size>::Elf_WXword
1060 get_r_info() const
1061 { return internal::convert_wxword<size, big_endian>(this->p_->r_info); }
1062
1063 typename Elf_types<size>::Elf_Swxword
1064 get_r_addend() const
1065 { return internal::convert_swxword<size, big_endian>(this->p_->r_addend); }
1066
1067 private:
1068 const internal::Rela_data<size>* p_;
1069 };
1070
1071 } // End namespace elfcpp.
1072
1073 #endif // !defined(ELFPCP_H)
This page took 0.050324 seconds and 5 git commands to generate.