Initial CVS checkin of gold
[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 // Dummy types which should not be used.
32 typedef unsigned char Elf_Addr;
33 typedef unsigned char Elf_Off;
34 // WXword is for fields which are Elf32_Word and Elf64_Xword.
35 typedef unsigned char Elf_WXword;
36 };
37
38 template<>
39 struct Elf_types<32>
40 {
41 typedef uint32_t Elf_Addr;
42 typedef uint32_t Elf_Off;
43 typedef uint32_t Elf_WXword;
44 };
45
46 template<>
47 struct Elf_types<64>
48 {
49 typedef uint64_t Elf_Addr;
50 typedef uint64_t Elf_Off;
51 typedef uint64_t Elf_WXword;
52 static const int ehdr_size;
53 static const int shdr_size;
54 static const int sym_size;
55 };
56
57 // Offsets within the Ehdr e_ident field.
58
59 const int EI_MAG0 = 0;
60 const int EI_MAG1 = 1;
61 const int EI_MAG2 = 2;
62 const int EI_MAG3 = 3;
63 const int EI_CLASS = 4;
64 const int EI_DATA = 5;
65 const int EI_VERSION = 6;
66 const int EI_OSABI = 7;
67 const int EI_ABIVERSION = 8;
68 const int EI_PAD = 9;
69 const int EI_NIDENT = 16;
70
71 // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
72
73 const int ELFMAG0 = 0x7f;
74 const int ELFMAG1 = 'E';
75 const int ELFMAG2 = 'L';
76 const int ELFMAG3 = 'F';
77
78 // The valid values found in Ehdr e_ident[EI_CLASS].
79
80 enum
81 {
82 ELFCLASSNONE = 0,
83 ELFCLASS32 = 1,
84 ELFCLASS64 = 2
85 };
86
87 // The valid values found in Ehdr e_ident[EI_DATA].
88
89 enum
90 {
91 ELFDATANONE = 0,
92 ELFDATA2LSB = 1,
93 ELFDATA2MSB = 2
94 };
95
96 // The valid values found in Ehdr e_ident[EI_VERSION] and e_version.
97
98 enum
99 {
100 EV_NONE = 0,
101 EV_CURRENT = 1
102 };
103
104 // The valid values found in Ehdr e_ident[EI_OSABI].
105
106 enum ELFOSABI
107 {
108 ELFOSABI_NONE = 0,
109 ELFOSABI_HPUX = 1,
110 ELFOSABI_NETBSD = 2,
111 // ELFOSABI_LINUX is not listed in the ELF standard.
112 ELFOSABI_LINUX = 3,
113 // ELFOSABI_HURD is not listed in the ELF standard.
114 ELFOSABI_HURD = 4,
115 ELFOSABI_SOLARIS = 6,
116 ELFOSABI_AIX = 7,
117 ELFOSABI_IRIX = 8,
118 ELFOSABI_FREEBSD = 9,
119 ELFOSABI_TRU64 = 10,
120 ELFOSABI_MODESTO = 11,
121 ELFOSABI_OPENBSD = 12,
122 ELFOSABI_OPENVMS = 13,
123 ELFOSABI_NSK = 14,
124 ELFOSABI_AROS = 15,
125 // A GNU extension for the ARM.
126 ELFOSABI_ARM = 97,
127 // A GNU extension for the MSP.
128 ELFOSABI_STANDALONE = 255
129 };
130
131 // The valid values found in the Ehdr e_type field.
132
133 enum ET
134 {
135 ET_NONE = 0,
136 ET_REL = 1,
137 ET_EXEC = 2,
138 ET_DYN = 3,
139 ET_CORE = 4,
140 ET_LOOS = 0xfe00,
141 ET_HIOS = 0xfeff,
142 ET_LOPROC = 0xff00,
143 ET_HIPROC = 0xffff
144 };
145
146 // The valid values found in the Ehdr e_machine field.
147
148 enum EM
149 {
150 EM_NONE = 0,
151 EM_M32 = 1,
152 EM_SPARC = 2,
153 EM_386 = 3,
154 EM_68K = 4,
155 EM_88K = 5,
156 // 6 used to be EM_486
157 EM_860 = 7,
158 EM_MIPS = 8,
159 EM_S370 = 9,
160 EM_MIPS_RS3_LE = 10,
161 // 11 was the old Sparc V9 ABI.
162 // 12 through 14 are reserved.
163 EM_PARISC = 15,
164 // 16 is reserved.
165 // Some old PowerPC object files use 17.
166 EM_VPP500 = 17,
167 EM_SPARC32PLUS = 18,
168 EM_960 = 19,
169 EM_PPC = 20,
170 EM_PPC64 = 21,
171 EM_S390 = 22,
172 // 23 through 35 are served.
173 EM_V800 = 36,
174 EM_FR20 = 37,
175 EM_RH32 = 38,
176 EM_RCE = 39,
177 EM_ARM = 40,
178 EM_ALPHA = 41,
179 EM_SH = 42,
180 EM_SPARCV9 = 43,
181 EM_TRICORE = 44,
182 EM_ARC = 45,
183 EM_H8_300 = 46,
184 EM_H8_300H = 47,
185 EM_H8S = 48,
186 EM_H8_500 = 49,
187 EM_IA_64 = 50,
188 EM_MIPS_X = 51,
189 EM_COLDFIRE = 52,
190 EM_68HC12 = 53,
191 EM_MMA = 54,
192 EM_PCP = 55,
193 EM_NCPU = 56,
194 EM_NDR1 = 57,
195 EM_STARCORE = 58,
196 EM_ME16 = 59,
197 EM_ST100 = 60,
198 EM_TINYJ = 61,
199 EM_X86_64 = 62,
200 EM_PDSP = 63,
201 EM_PDP10 = 64,
202 EM_PDP11 = 65,
203 EM_FX66 = 66,
204 EM_ST9PLUS = 67,
205 EM_ST7 = 68,
206 EM_68HC16 = 69,
207 EM_68HC11 = 70,
208 EM_68HC08 = 71,
209 EM_68HC05 = 72,
210 EM_SVX = 73,
211 EM_ST19 = 74,
212 EM_VAX = 75,
213 EM_CRIS = 76,
214 EM_JAVELIN = 77,
215 EM_FIREPATH = 78,
216 EM_ZSP = 79,
217 EM_MMIX = 80,
218 EM_HUANY = 81,
219 EM_PRISM = 82,
220 EM_AVR = 83,
221 EM_FR30 = 84,
222 EM_D10V = 85,
223 EM_D30V = 86,
224 EM_V850 = 87,
225 EM_M32R = 88,
226 EM_MN10300 = 89,
227 EM_MN10200 = 90,
228 EM_PJ = 91,
229 EM_OPENRISC = 92,
230 EM_ARC_A5 = 93,
231 EM_XTENSA = 94,
232 EM_VIDEOCORE = 95,
233 EM_TMM_GPP = 96,
234 EM_NS32K = 97,
235 EM_TPC = 98,
236 // Some old picoJava object files use 99 (EM_PJ is correct).
237 EM_SNP1K = 99,
238 EM_ST200 = 100,
239 EM_IP2K = 101,
240 EM_MAX = 102,
241 EM_CR = 103,
242 EM_F2MC16 = 104,
243 EM_MSP430 = 105,
244 EM_BLACKFIN = 106,
245 EM_SE_C33 = 107,
246 EM_SEP = 108,
247 EM_ARCA = 109,
248 EM_UNICORE = 110,
249 EM_ALTERA_NIOS2 = 113,
250 EM_CRX = 114,
251 // The Morph MT.
252 EM_MT = 0x2530,
253 // DLX.
254 EM_DLX = 0x5aa5,
255 // FRV.
256 EM_FRV = 0x5441,
257 // Infineon Technologies 16-bit microcontroller with C166-V2 core.
258 EM_X16X = 0x4688,
259 // Xstorym16
260 EM_XSTORMY16 = 0xad45,
261 // Renesas M32C
262 EM_M32C = 0xfeb0,
263 // Vitesse IQ2000
264 EM_IQ2000 = 0xfeba,
265 // NIOS
266 EM_NIOS32 = 0xfebb
267 // Old AVR objects used 0x1057 (EM_AVR is correct).
268 // Old MSP430 objects used 0x1059 (EM_MSP430 is correct).
269 // Old FR30 objects used 0x3330 (EM_FR30 is correct).
270 // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OPENRISC is correct).
271 // Old D10V objects used 0x7650 (EM_D10V is correct).
272 // Old D30V objects used 0x7676 (EM_D30V is correct).
273 // Old IP2X objects used 0x8217 (EM_IP2K is correct).
274 // Old PowerPC objects used 0x9025 (EM_PPC is correct).
275 // Old Alpha objects used 0x9026 (EM_ALPHA is correct).
276 // Old M32R objects used 0x9041 (EM_M32R is correct).
277 // Old V850 objects used 0x9080 (EM_V850 is correct).
278 // Old S/390 objects used 0xa390 (EM_S390 is correct).
279 // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct).
280 // Old MN10300 objects used 0xbeef (EM_MN10300 is correct).
281 // Old MN10200 objects used 0xdead (EM_MN10200 is correct).
282 };
283
284 // Special section indices.
285
286 enum
287 {
288 SHN_UNDEF = 0,
289 SHN_LORESERVE = 0xff00,
290 SHN_LOPROC = 0xff00,
291 SHN_HIPROC = 0xff1f,
292 SHN_LOOS = 0xff20,
293 SHN_HIOS = 0xff3f,
294 SHN_ABS = 0xfff1,
295 SHN_COMMON = 0xfff2,
296 SHN_XINDEX = 0xffff,
297 SHN_HIRESERVE = 0xffff
298 };
299
300 // The valid values found in the Shdr sh_type field.
301
302 enum
303 {
304 SHT_NULL = 0,
305 SHT_PROGBITS = 1,
306 SHT_SYMTAB = 2,
307 SHT_STRTAB = 3,
308 SHT_RELA = 4,
309 SHT_HASH = 5,
310 SHT_DYNAMIC = 6,
311 SHT_NOTE = 7,
312 SHT_NOBITS = 8,
313 SHT_REL = 9,
314 SHT_SHLIB = 10,
315 SHT_DYNSYM = 11,
316 SHT_INIT_ARRAY = 14,
317 SHT_FINI_ARRAY = 15,
318 SHT_PREINIT_ARRAY = 16,
319 SHT_GROUP = 17,
320 SHT_SYMTAB_SHNDX = 18,
321 SHT_LOOS = 0x60000000,
322 SHT_HIOS = 0x6fffffff,
323 SHT_LOPROC = 0x70000000,
324 SHT_HIPROC = 0x7fffffff,
325 SHT_LOUSER = 0x80000000,
326 SHT_HIUSER = 0xffffffff,
327 // The remaining values are not in the standard.
328 // List of prelink dependencies.
329 SHT_GNU_LIBLIST = 0x6ffffff7,
330 // Versions defined by file.
331 SHT_SUNW_verdef = 0x6ffffffd,
332 SHT_GNU_verdef = 0x6ffffffd,
333 // Versions needed by file.
334 SHT_SUNW_verneed = 0x6ffffffe,
335 SHT_GNU_verneed = 0x6ffffffe,
336 // Symbol versions,
337 SHT_SUNW_versym = 0x6fffffff,
338 SHT_GNU_versym = 0x6fffffff,
339 };
340
341 // The valid bit flags found in the Shdr sh_flags field.
342
343 enum
344 {
345 SHF_WRITE = 0x1,
346 SHF_ALLOC = 0x2,
347 SHF_EXECINSTR = 0x4,
348 SHF_MERGE = 0x10,
349 SHF_STRINGS = 0x20,
350 SHF_INFO_LINK = 0x40,
351 SHF_LINK_ORDER = 0x80,
352 SHF_OS_NONCONFORMING = 0x100,
353 SHF_GROUP = 0x200,
354 SHF_TLS = 0x400,
355 SHF_MASKOS = 0x0ff00000,
356 SHF_MASKPROC = 0xf0000000
357 };
358
359 // Bit flags which appear in the first 32-bit word of the section data
360 // of a SHT_GROUP section.
361
362 enum
363 {
364 GRP_COMDAT = 0x1,
365 GRP_MASKOS = 0x0ff00000,
366 GRP_MASKPROC = 0xf0000000
367 };
368
369 // Symbol binding from Sym st_info field.
370
371 enum STB
372 {
373 STB_LOCAL = 0,
374 STB_GLOBAL = 1,
375 STB_WEAK = 2,
376 STB_LOOS = 10,
377 STB_HIOS = 12,
378 STB_LOPROC = 13,
379 STB_HIPROC = 15
380 };
381
382 // Symbol types from Sym st_info field.
383
384 enum STT
385 {
386 STT_NOTYPE = 0,
387 STT_OBJECT = 1,
388 STT_FUNC = 2,
389 STT_SECTION = 3,
390 STT_FILE = 4,
391 STT_COMMON = 5,
392 STT_TLS = 6,
393 STT_LOOS = 10,
394 STT_HIOS = 12,
395 STT_LOPROC = 13,
396 STT_HIPROC = 15
397 };
398
399 // Symbol visibility from Sym st_other field.
400
401 enum STV
402 {
403 STV_DEFAULT = 0,
404 STV_INTERNAL = 1,
405 STV_HIDDEN = 2,
406 STV_PROTECTED = 3
407 };
408
409 } // End namespace elfcpp.
410
411 // Include internal details after defining the types.
412 #include "elfcpp_internal.h"
413
414 namespace elfcpp
415 {
416
417 // The offset of the ELF file header in the ELF file.
418
419 const int file_header_offset = 0;
420
421 // ELF structure sizes.
422
423 template<int size>
424 struct Elf_sizes
425 {
426 // Size of ELF file header.
427 static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
428 // Size of ELF section header.
429 static const int shdr_size = sizeof(internal::Shdr_data<size>);
430 // Size of ELF symbol table entry.
431 static const int sym_size = sizeof(internal::Sym_data<size>);
432 };
433
434 // Accessor class for the ELF file header.
435
436 template<int size, bool big_endian>
437 class Ehdr
438 {
439 public:
440 Ehdr(const unsigned char* p)
441 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
442 { }
443
444 const unsigned char*
445 get_e_ident() const
446 { return this->p_->e_ident; }
447
448 Elf_Half
449 get_e_type() const
450 { return internal::convert_half<big_endian>(this->p_->e_type); }
451
452 Elf_Half
453 get_e_machine() const
454 { return internal::convert_half<big_endian>(this->p_->e_machine); }
455
456 Elf_Word
457 get_e_version() const
458 { return internal::convert_word<big_endian>(this->p_->e_version); }
459
460 typename Elf_types<size>::Elf_Addr
461 get_e_entry() const
462 { return internal::convert_addr<size, big_endian>(this->p_->e_entry); }
463
464 typename Elf_types<size>::Elf_Off
465 get_e_phoff() const
466 { return internal::convert_off<size, big_endian>(this->p_->e_phoff); }
467
468 typename Elf_types<size>::Elf_Off
469 get_e_shoff() const
470 { return internal::convert_off<size, big_endian>(this->p_->e_shoff); }
471
472 Elf_Word
473 get_e_flags() const
474 { return internal::convert_word<big_endian>(this->p_->e_flags); }
475
476 Elf_Half
477 get_e_ehsize() const
478 { return internal::convert_half<big_endian>(this->p_->e_ehsize); }
479
480 Elf_Half
481 get_e_phentsize() const
482 { return internal::convert_half<big_endian>(this->p_->e_phentsize); }
483
484 Elf_Half
485 get_e_phnum() const
486 { return internal::convert_half<big_endian>(this->p_->e_phnum); }
487
488 Elf_Half
489 get_e_shentsize() const
490 { return internal::convert_half<big_endian>(this->p_->e_shentsize); }
491
492 Elf_Half
493 get_e_shnum() const
494 { return internal::convert_half<big_endian>(this->p_->e_shnum); }
495
496 Elf_Half
497 get_e_shstrndx() const
498 { return internal::convert_half<big_endian>(this->p_->e_shstrndx); }
499
500 private:
501 const internal::Ehdr_data<size>* p_;
502 };
503
504 // Accessor class for an ELF section header.
505
506 template<int size, bool big_endian>
507 class Shdr
508 {
509 public:
510 Shdr(const unsigned char* p)
511 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
512 { }
513
514 Elf_Word
515 get_sh_name() const
516 { return internal::convert_word<big_endian>(this->p_->sh_name); }
517
518 Elf_Word
519 get_sh_type() const
520 { return internal::convert_word<big_endian>(this->p_->sh_type); }
521
522 typename Elf_types<size>::Elf_WXword
523 get_sh_flags() const
524 { return internal::convert_wxword<size, big_endian>(this->p_->sh_flags); }
525
526 typename Elf_types<size>::Elf_Addr
527 get_sh_addr() const
528 { return internal::convert_addr<size, big_endian>(this->p_->sh_addr); }
529
530 typename Elf_types<size>::Elf_Off
531 get_sh_offset() const
532 { return internal::convert_off<size, big_endian>(this->p_->sh_offset); }
533
534 typename Elf_types<size>::Elf_WXword
535 get_sh_size() const
536 { return internal::convert_wxword<size, big_endian>(this->p_->sh_size); }
537
538 Elf_Word
539 get_sh_link() const
540 { return internal::convert_word<big_endian>(this->p_->sh_link); }
541
542 Elf_Word
543 get_sh_info() const
544 { return internal::convert_word<big_endian>(this->p_->sh_info); }
545
546 typename Elf_types<size>::Elf_WXword
547 get_sh_addralign() const
548 { return
549 internal::convert_wxword<size, big_endian>(this->p_->sh_addralign); }
550
551 typename Elf_types<size>::Elf_WXword
552 get_sh_entsize() const
553 { return internal::convert_wxword<size, big_endian>(this->p_->sh_entsize); }
554
555 private:
556 const internal::Shdr_data<size>* p_;
557 };
558
559 // Accessor class for an ELF symbol table entry.
560
561 template<int size, bool big_endian>
562 class Sym
563 {
564 public:
565 Sym(const unsigned char* p)
566 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
567 { }
568
569 Elf_Word
570 get_st_name() const
571 { return internal::convert_word<big_endian>(this->p_->st_name); }
572
573 typename Elf_types<size>::Elf_Addr
574 get_st_value() const
575 { return internal::convert_addr<size, big_endian>(this->p_->st_value); }
576
577 typename Elf_types<size>::Elf_WXword
578 get_st_size() const
579 { return internal::convert_wxword<big_endian>(this->p_->st_size); }
580
581 unsigned char
582 get_st_info() const
583 { return this->p_->st_info; }
584
585 unsigned char
586 get_st_other() const
587 { return this->p_->st_other; }
588
589 Elf_Half
590 get_st_shndx() const
591 { return internal::convert_half<big_endian>(this->p_->st_shndx); }
592
593 private:
594 const internal::Sym_data<size>* p_;
595 };
596
597 } // End namespace elfcpp.
598
599 #endif // !defined(ELFPCP_H)
This page took 0.043112 seconds and 5 git commands to generate.