* ld.texinfo (Options): Update description of local symbols for -X.
[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
1564db8d
ILT
440inline unsigned char
441elf_st_other(STV vis, unsigned char nonvis)
442{
443 return ((nonvis << 2)
444 + (static_cast<unsigned char>(vis) & 3));
445}
446
bae7f79e
ILT
447} // End namespace elfcpp.
448
449// Include internal details after defining the types.
450#include "elfcpp_internal.h"
451
452namespace elfcpp
453{
454
455// The offset of the ELF file header in the ELF file.
456
457const int file_header_offset = 0;
458
459// ELF structure sizes.
460
461template<int size>
462struct Elf_sizes
463{
464 // Size of ELF file header.
465 static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
466 // Size of ELF section header.
467 static const int shdr_size = sizeof(internal::Shdr_data<size>);
468 // Size of ELF symbol table entry.
469 static const int sym_size = sizeof(internal::Sym_data<size>);
470};
471
472// Accessor class for the ELF file header.
473
474template<int size, bool big_endian>
475class Ehdr
476{
477 public:
478 Ehdr(const unsigned char* p)
479 : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
480 { }
481
482 const unsigned char*
483 get_e_ident() const
484 { return this->p_->e_ident; }
485
486 Elf_Half
487 get_e_type() const
488 { return internal::convert_half<big_endian>(this->p_->e_type); }
489
490 Elf_Half
491 get_e_machine() const
492 { return internal::convert_half<big_endian>(this->p_->e_machine); }
493
494 Elf_Word
495 get_e_version() const
496 { return internal::convert_word<big_endian>(this->p_->e_version); }
497
498 typename Elf_types<size>::Elf_Addr
499 get_e_entry() const
500 { return internal::convert_addr<size, big_endian>(this->p_->e_entry); }
501
502 typename Elf_types<size>::Elf_Off
503 get_e_phoff() const
504 { return internal::convert_off<size, big_endian>(this->p_->e_phoff); }
505
506 typename Elf_types<size>::Elf_Off
507 get_e_shoff() const
508 { return internal::convert_off<size, big_endian>(this->p_->e_shoff); }
509
510 Elf_Word
511 get_e_flags() const
512 { return internal::convert_word<big_endian>(this->p_->e_flags); }
513
514 Elf_Half
515 get_e_ehsize() const
516 { return internal::convert_half<big_endian>(this->p_->e_ehsize); }
517
518 Elf_Half
519 get_e_phentsize() const
520 { return internal::convert_half<big_endian>(this->p_->e_phentsize); }
521
522 Elf_Half
523 get_e_phnum() const
524 { return internal::convert_half<big_endian>(this->p_->e_phnum); }
525
526 Elf_Half
527 get_e_shentsize() const
528 { return internal::convert_half<big_endian>(this->p_->e_shentsize); }
529
530 Elf_Half
531 get_e_shnum() const
532 { return internal::convert_half<big_endian>(this->p_->e_shnum); }
533
534 Elf_Half
535 get_e_shstrndx() const
536 { return internal::convert_half<big_endian>(this->p_->e_shstrndx); }
537
538 private:
539 const internal::Ehdr_data<size>* p_;
540};
541
542// Accessor class for an ELF section header.
543
544template<int size, bool big_endian>
545class Shdr
546{
547 public:
548 Shdr(const unsigned char* p)
549 : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
550 { }
551
552 Elf_Word
553 get_sh_name() const
554 { return internal::convert_word<big_endian>(this->p_->sh_name); }
555
556 Elf_Word
557 get_sh_type() const
558 { return internal::convert_word<big_endian>(this->p_->sh_type); }
559
560 typename Elf_types<size>::Elf_WXword
561 get_sh_flags() const
562 { return internal::convert_wxword<size, big_endian>(this->p_->sh_flags); }
563
564 typename Elf_types<size>::Elf_Addr
565 get_sh_addr() const
566 { return internal::convert_addr<size, big_endian>(this->p_->sh_addr); }
567
568 typename Elf_types<size>::Elf_Off
569 get_sh_offset() const
570 { return internal::convert_off<size, big_endian>(this->p_->sh_offset); }
571
572 typename Elf_types<size>::Elf_WXword
573 get_sh_size() const
574 { return internal::convert_wxword<size, big_endian>(this->p_->sh_size); }
575
576 Elf_Word
577 get_sh_link() const
578 { return internal::convert_word<big_endian>(this->p_->sh_link); }
579
580 Elf_Word
581 get_sh_info() const
582 { return internal::convert_word<big_endian>(this->p_->sh_info); }
583
584 typename Elf_types<size>::Elf_WXword
585 get_sh_addralign() const
586 { return
587 internal::convert_wxword<size, big_endian>(this->p_->sh_addralign); }
588
589 typename Elf_types<size>::Elf_WXword
590 get_sh_entsize() const
591 { return internal::convert_wxword<size, big_endian>(this->p_->sh_entsize); }
592
593 private:
594 const internal::Shdr_data<size>* p_;
595};
596
597// Accessor class for an ELF symbol table entry.
598
599template<int size, bool big_endian>
600class Sym
601{
602 public:
603 Sym(const unsigned char* p)
604 : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
605 { }
606
607 Elf_Word
608 get_st_name() const
609 { return internal::convert_word<big_endian>(this->p_->st_name); }
610
611 typename Elf_types<size>::Elf_Addr
612 get_st_value() const
613 { return internal::convert_addr<size, big_endian>(this->p_->st_value); }
614
615 typename Elf_types<size>::Elf_WXword
616 get_st_size() const
14bfc3f5 617 { return internal::convert_wxword<size, big_endian>(this->p_->st_size); }
bae7f79e
ILT
618
619 unsigned char
620 get_st_info() const
621 { return this->p_->st_info; }
622
14bfc3f5
ILT
623 STB
624 get_st_bind() const
625 { return elf_st_bind(this->get_st_info()); }
626
627 STT
628 get_st_type() const
629 { return elf_st_type(this->get_st_info()); }
630
bae7f79e
ILT
631 unsigned char
632 get_st_other() const
633 { return this->p_->st_other; }
634
14bfc3f5
ILT
635 STV
636 get_st_visibility() const
637 { return elf_st_visibility(this->get_st_other()); }
638
639 unsigned char
640 get_st_nonvis() const
641 { return elf_st_nonvis(this->get_st_other()); }
642
bae7f79e
ILT
643 Elf_Half
644 get_st_shndx() const
645 { return internal::convert_half<big_endian>(this->p_->st_shndx); }
646
647 private:
648 const internal::Sym_data<size>* p_;
649};
650
1564db8d
ILT
651// Writer class for an ELF symbol table entry.
652
653template<int size, bool big_endian>
654class Sym_write
655{
656 public:
657 Sym_write(unsigned char* p)
658 : p_(reinterpret_cast<internal::Sym_data<size>*>(p))
659 { }
660
661 void
662 put_st_name(Elf_Word v)
663 { this->p_->st_name = internal::convert_word<big_endian>(v); }
664
665 void
666 put_st_value(typename Elf_types<size>::Elf_Addr v)
667 { this->p_->st_value = internal::convert_addr<size, big_endian>(v); }
668
669 void
670 put_st_size(typename Elf_types<size>::Elf_WXword v)
671 { this->p_->st_size = internal::convert_wxword<size, big_endian>(v); }
672
673 void
674 put_st_info(unsigned char v)
675 { this->p_->st_info = v; }
676
677 void
678 put_st_info(STB bind, STT type)
679 { this->p_->st_info = elf_st_info(bind, type); }
680
681 void
682 put_st_other(unsigned char v)
683 { this->p_->st_other = v; }
684
685 void
686 put_st_other(STV vis, unsigned char nonvis)
687 { this->p_->st_other = elf_st_other(vis, nonvis); }
688
689 void
690 put_st_shndx(Elf_Half v)
691 { this->p_->st_shndx = internal::convert_half<big_endian>(v); }
692
693 Sym<size, big_endian>
694 sym()
695 { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); }
696
697 private:
698 internal::Sym_data<size>* p_;
699};
700
bae7f79e
ILT
701} // End namespace elfcpp.
702
703#endif // !defined(ELFPCP_H)
This page took 0.059718 seconds and 4 git commands to generate.