2006-09-07 Paul Brook <paul@codesourcery.com>
[deliverable/binutils-gdb.git] / elfcpp / elfcpp.h
CommitLineData
bae7f79e
ILT
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
12namespace elfcpp
13{
14
15// Basic ELF types.
16
17// These types are always the same size.
18
19typedef uint16_t Elf_Half;
20typedef uint32_t Elf_Word;
21typedef int32_t Elf_Sword;
22typedef uint64_t Elf_Xword;
23typedef 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
28template<int size>
29struct 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
38template<>
39struct Elf_types<32>
40{
41 typedef uint32_t Elf_Addr;
42 typedef uint32_t Elf_Off;
43 typedef uint32_t Elf_WXword;
44};
45
46template<>
47struct 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
59const int EI_MAG0 = 0;
60const int EI_MAG1 = 1;
61const int EI_MAG2 = 2;
62const int EI_MAG3 = 3;
63const int EI_CLASS = 4;
64const int EI_DATA = 5;
65const int EI_VERSION = 6;
66const int EI_OSABI = 7;
67const int EI_ABIVERSION = 8;
68const int EI_PAD = 9;
69const int EI_NIDENT = 16;
70
71// The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
72
73const int ELFMAG0 = 0x7f;
74const int ELFMAG1 = 'E';
75const int ELFMAG2 = 'L';
76const int ELFMAG3 = 'F';
77
78// The valid values found in Ehdr e_ident[EI_CLASS].
79
80enum
81{
82 ELFCLASSNONE = 0,
83 ELFCLASS32 = 1,
84 ELFCLASS64 = 2
85};
86
87// The valid values found in Ehdr e_ident[EI_DATA].
88
89enum
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
98enum
99{
100 EV_NONE = 0,
101 EV_CURRENT = 1
102};
103
104// The valid values found in Ehdr e_ident[EI_OSABI].
105
106enum 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
133enum 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
148enum 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
286enum
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
302enum
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
343enum
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
362enum
363{
364 GRP_COMDAT = 0x1,
365 GRP_MASKOS = 0x0ff00000,
366 GRP_MASKPROC = 0xf0000000
367};
368
369// Symbol binding from Sym st_info field.
370
371enum 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
384enum 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
14bfc3f5
ILT
399inline STB
400elf_st_bind(unsigned char info)
401{
402 return static_cast<STB>(info >> 4);
403}
404
405inline STT
406elf_st_type(unsigned char info)
407{
408 return static_cast<STT>(info & 0xf);
409}
410
411inline unsigned char
412elf_st_info(STB bind, STT type)
413{
414 return ((static_cast<unsigned char>(bind) << 4)
415 + (static_cast<unsigned char>(type) & 0xf));
416}
417
bae7f79e
ILT
418// Symbol visibility from Sym st_other field.
419
420enum STV
421{
422 STV_DEFAULT = 0,
423 STV_INTERNAL = 1,
424 STV_HIDDEN = 2,
425 STV_PROTECTED = 3
426};
427
14bfc3f5
ILT
428inline STV
429elf_st_visibility(unsigned char other)
430{
431 return static_cast<STV>(other & 0x3);
432}
433
434inline unsigned char
435elf_st_nonvis(unsigned char other)
436{
437 return static_cast<STV>(other >> 2);
438}
439
bae7f79e
ILT
440} // End namespace elfcpp.
441
442// Include internal details after defining the types.
443#include "elfcpp_internal.h"
444
445namespace elfcpp
446{
447
448// The offset of the ELF file header in the ELF file.
449
450const int file_header_offset = 0;
451
452// ELF structure sizes.
453
454template<int size>
455struct Elf_sizes
456{
457 // Size of ELF file header.
458 static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
459 // Size of ELF section header.
460 static const int shdr_size = sizeof(internal::Shdr_data<size>);
461 // Size of ELF symbol table entry.
462 static const int sym_size = sizeof(internal::Sym_data<size>);
463};
464
465// Accessor class for the ELF file header.
466
467template<int size, bool big_endian>
468class Ehdr
469{
470 public:
471 Ehdr(const unsigned char* p)
472 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
473 { }
474
475 const unsigned char*
476 get_e_ident() const
477 { return this->p_->e_ident; }
478
479 Elf_Half
480 get_e_type() const
481 { return internal::convert_half<big_endian>(this->p_->e_type); }
482
483 Elf_Half
484 get_e_machine() const
485 { return internal::convert_half<big_endian>(this->p_->e_machine); }
486
487 Elf_Word
488 get_e_version() const
489 { return internal::convert_word<big_endian>(this->p_->e_version); }
490
491 typename Elf_types<size>::Elf_Addr
492 get_e_entry() const
493 { return internal::convert_addr<size, big_endian>(this->p_->e_entry); }
494
495 typename Elf_types<size>::Elf_Off
496 get_e_phoff() const
497 { return internal::convert_off<size, big_endian>(this->p_->e_phoff); }
498
499 typename Elf_types<size>::Elf_Off
500 get_e_shoff() const
501 { return internal::convert_off<size, big_endian>(this->p_->e_shoff); }
502
503 Elf_Word
504 get_e_flags() const
505 { return internal::convert_word<big_endian>(this->p_->e_flags); }
506
507 Elf_Half
508 get_e_ehsize() const
509 { return internal::convert_half<big_endian>(this->p_->e_ehsize); }
510
511 Elf_Half
512 get_e_phentsize() const
513 { return internal::convert_half<big_endian>(this->p_->e_phentsize); }
514
515 Elf_Half
516 get_e_phnum() const
517 { return internal::convert_half<big_endian>(this->p_->e_phnum); }
518
519 Elf_Half
520 get_e_shentsize() const
521 { return internal::convert_half<big_endian>(this->p_->e_shentsize); }
522
523 Elf_Half
524 get_e_shnum() const
525 { return internal::convert_half<big_endian>(this->p_->e_shnum); }
526
527 Elf_Half
528 get_e_shstrndx() const
529 { return internal::convert_half<big_endian>(this->p_->e_shstrndx); }
530
531 private:
532 const internal::Ehdr_data<size>* p_;
533};
534
535// Accessor class for an ELF section header.
536
537template<int size, bool big_endian>
538class Shdr
539{
540 public:
541 Shdr(const unsigned char* p)
542 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
543 { }
544
545 Elf_Word
546 get_sh_name() const
547 { return internal::convert_word<big_endian>(this->p_->sh_name); }
548
549 Elf_Word
550 get_sh_type() const
551 { return internal::convert_word<big_endian>(this->p_->sh_type); }
552
553 typename Elf_types<size>::Elf_WXword
554 get_sh_flags() const
555 { return internal::convert_wxword<size, big_endian>(this->p_->sh_flags); }
556
557 typename Elf_types<size>::Elf_Addr
558 get_sh_addr() const
559 { return internal::convert_addr<size, big_endian>(this->p_->sh_addr); }
560
561 typename Elf_types<size>::Elf_Off
562 get_sh_offset() const
563 { return internal::convert_off<size, big_endian>(this->p_->sh_offset); }
564
565 typename Elf_types<size>::Elf_WXword
566 get_sh_size() const
567 { return internal::convert_wxword<size, big_endian>(this->p_->sh_size); }
568
569 Elf_Word
570 get_sh_link() const
571 { return internal::convert_word<big_endian>(this->p_->sh_link); }
572
573 Elf_Word
574 get_sh_info() const
575 { return internal::convert_word<big_endian>(this->p_->sh_info); }
576
577 typename Elf_types<size>::Elf_WXword
578 get_sh_addralign() const
579 { return
580 internal::convert_wxword<size, big_endian>(this->p_->sh_addralign); }
581
582 typename Elf_types<size>::Elf_WXword
583 get_sh_entsize() const
584 { return internal::convert_wxword<size, big_endian>(this->p_->sh_entsize); }
585
586 private:
587 const internal::Shdr_data<size>* p_;
588};
589
590// Accessor class for an ELF symbol table entry.
591
592template<int size, bool big_endian>
593class Sym
594{
595 public:
596 Sym(const unsigned char* p)
597 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
598 { }
599
600 Elf_Word
601 get_st_name() const
602 { return internal::convert_word<big_endian>(this->p_->st_name); }
603
604 typename Elf_types<size>::Elf_Addr
605 get_st_value() const
606 { return internal::convert_addr<size, big_endian>(this->p_->st_value); }
607
608 typename Elf_types<size>::Elf_WXword
609 get_st_size() const
14bfc3f5 610 { return internal::convert_wxword<size, big_endian>(this->p_->st_size); }
bae7f79e
ILT
611
612 unsigned char
613 get_st_info() const
614 { return this->p_->st_info; }
615
14bfc3f5
ILT
616 STB
617 get_st_bind() const
618 { return elf_st_bind(this->get_st_info()); }
619
620 STT
621 get_st_type() const
622 { return elf_st_type(this->get_st_info()); }
623
bae7f79e
ILT
624 unsigned char
625 get_st_other() const
626 { return this->p_->st_other; }
627
14bfc3f5
ILT
628 STV
629 get_st_visibility() const
630 { return elf_st_visibility(this->get_st_other()); }
631
632 unsigned char
633 get_st_nonvis() const
634 { return elf_st_nonvis(this->get_st_other()); }
635
bae7f79e
ILT
636 Elf_Half
637 get_st_shndx() const
638 { return internal::convert_half<big_endian>(this->p_->st_shndx); }
639
640 private:
641 const internal::Sym_data<size>* p_;
642};
643
644} // End namespace elfcpp.
645
646#endif // !defined(ELFPCP_H)
This page took 0.052007 seconds and 4 git commands to generate.