133965Sjdp/* ELF support for BFD.
2218822Sdim   Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002,
3218822Sdim   2003, 2006, 2007 Free Software Foundation, Inc.
433965Sjdp
533965Sjdp   Written by Fred Fish @ Cygnus Support, from information published
633965Sjdp   in "UNIX System V Release 4, Programmers Guide: ANSI C and
733965Sjdp   Programming Support Tools".
833965Sjdp
933965SjdpThis file is part of BFD, the Binary File Descriptor library.
1033965Sjdp
1133965SjdpThis program is free software; you can redistribute it and/or modify
1233965Sjdpit under the terms of the GNU General Public License as published by
1333965Sjdpthe Free Software Foundation; either version 2 of the License, or
1433965Sjdp(at your option) any later version.
1533965Sjdp
1633965SjdpThis program is distributed in the hope that it will be useful,
1733965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1833965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1933965SjdpGNU General Public License for more details.
2033965Sjdp
2133965SjdpYou should have received a copy of the GNU General Public License
2233965Sjdpalong with this program; if not, write to the Free Software
23218822SdimFoundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2433965Sjdp
2533965Sjdp
2633965Sjdp/* This file is part of ELF support for BFD, and contains the portions
2733965Sjdp   that describe how ELF is represented internally in the BFD library.
2833965Sjdp   I.E. it describes the in-memory representation of ELF.  It requires
2933965Sjdp   the elf-common.h file which contains the portions that are common to
3033965Sjdp   both the internal and external representations. */
3133965Sjdp
3260484Sobrien
3333965Sjdp/* NOTE that these structures are not kept in the same order as they appear
3433965Sjdp   in the object file.  In some cases they've been reordered for more optimal
3533965Sjdp   packing under various circumstances.  */
3633965Sjdp
3733965Sjdp#ifndef _ELF_INTERNAL_H
3833965Sjdp#define _ELF_INTERNAL_H
3933965Sjdp
4033965Sjdp/* ELF Header */
4133965Sjdp
4233965Sjdp#define EI_NIDENT	16		/* Size of e_ident[] */
4333965Sjdp
4433965Sjdptypedef struct elf_internal_ehdr {
4589857Sobrien  unsigned char		e_ident[EI_NIDENT]; /* ELF "magic number" */
4689857Sobrien  bfd_vma		e_entry;	/* Entry point virtual address */
4789857Sobrien  bfd_size_type		e_phoff;	/* Program header table file offset */
4889857Sobrien  bfd_size_type		e_shoff;	/* Section header table file offset */
4989857Sobrien  unsigned long		e_version;	/* Identifies object file version */
5089857Sobrien  unsigned long		e_flags;	/* Processor-specific flags */
5189857Sobrien  unsigned short	e_type;		/* Identifies object file type */
5289857Sobrien  unsigned short	e_machine;	/* Specifies required architecture */
5389857Sobrien  unsigned int		e_ehsize;	/* ELF header size in bytes */
5489857Sobrien  unsigned int		e_phentsize;	/* Program header table entry size */
5589857Sobrien  unsigned int		e_phnum;	/* Program header table entry count */
5689857Sobrien  unsigned int		e_shentsize;	/* Section header table entry size */
5789857Sobrien  unsigned int		e_shnum;	/* Section header table entry count */
5889857Sobrien  unsigned int		e_shstrndx;	/* Section header string table index */
5933965Sjdp} Elf_Internal_Ehdr;
6033965Sjdp
6133965Sjdp/* Program header */
6233965Sjdp
6333965Sjdpstruct elf_internal_phdr {
6433965Sjdp  unsigned long	p_type;			/* Identifies program segment type */
6533965Sjdp  unsigned long	p_flags;		/* Segment flags */
6633965Sjdp  bfd_vma	p_offset;		/* Segment file offset */
6733965Sjdp  bfd_vma	p_vaddr;		/* Segment virtual address */
6833965Sjdp  bfd_vma	p_paddr;		/* Segment physical address */
6933965Sjdp  bfd_vma	p_filesz;		/* Segment size in file */
7033965Sjdp  bfd_vma	p_memsz;		/* Segment size in memory */
7133965Sjdp  bfd_vma	p_align;		/* Segment alignment, file & memory */
7233965Sjdp};
7333965Sjdp
7433965Sjdptypedef struct elf_internal_phdr Elf_Internal_Phdr;
7533965Sjdp
7633965Sjdp/* Section header */
7733965Sjdp
7833965Sjdptypedef struct elf_internal_shdr {
7933965Sjdp  unsigned int	sh_name;		/* Section name, index in string tbl */
8033965Sjdp  unsigned int	sh_type;		/* Type of section */
8133965Sjdp  bfd_vma	sh_flags;		/* Miscellaneous section attributes */
8233965Sjdp  bfd_vma	sh_addr;		/* Section virtual addr at execution */
8333965Sjdp  bfd_size_type	sh_size;		/* Size of section in bytes */
8433965Sjdp  bfd_size_type	sh_entsize;		/* Entry size if section holds table */
8533965Sjdp  unsigned long	sh_link;		/* Index of another section */
8633965Sjdp  unsigned long	sh_info;		/* Additional section information */
8733965Sjdp  file_ptr	sh_offset;		/* Section file offset */
8833965Sjdp  unsigned int	sh_addralign;		/* Section alignment */
8933965Sjdp
9033965Sjdp  /* The internal rep also has some cached info associated with it. */
9133965Sjdp  asection *	bfd_section;		/* Associated BFD section.  */
9289857Sobrien  unsigned char *contents;		/* Section contents.  */
9333965Sjdp} Elf_Internal_Shdr;
9433965Sjdp
9533965Sjdp/* Symbol table entry */
9633965Sjdp
9733965Sjdpstruct elf_internal_sym {
9833965Sjdp  bfd_vma	st_value;		/* Value of the symbol */
9933965Sjdp  bfd_vma	st_size;		/* Associated symbol size */
10033965Sjdp  unsigned long	st_name;		/* Symbol name, index in string tbl */
10133965Sjdp  unsigned char	st_info;		/* Type and binding attributes */
10277298Sobrien  unsigned char	st_other;		/* Visibilty, and target specific */
10389857Sobrien  unsigned int  st_shndx;		/* Associated section index */
10433965Sjdp};
10533965Sjdp
10633965Sjdptypedef struct elf_internal_sym Elf_Internal_Sym;
10733965Sjdp
10833965Sjdp/* Note segments */
10933965Sjdp
11033965Sjdptypedef struct elf_internal_note {
11133965Sjdp  unsigned long	namesz;			/* Size of entry's owner string */
11233965Sjdp  unsigned long	descsz;			/* Size of the note descriptor */
11333965Sjdp  unsigned long	type;			/* Interpretation of the descriptor */
11460484Sobrien  char *	namedata;		/* Start of the name+desc data */
11560484Sobrien  char *	descdata;		/* Start of the desc data */
11660484Sobrien  bfd_vma	descpos;		/* File offset of the descdata */
11733965Sjdp} Elf_Internal_Note;
11833965Sjdp
11933965Sjdp/* Relocation Entries */
12033965Sjdp
12133965Sjdptypedef struct elf_internal_rela {
12233965Sjdp  bfd_vma	r_offset;	/* Location at which to apply the action */
12333965Sjdp  bfd_vma	r_info;		/* Index and Type of relocation */
12489857Sobrien  bfd_vma	r_addend;	/* Constant addend used to compute value */
12533965Sjdp} Elf_Internal_Rela;
12633965Sjdp
12733965Sjdp/* dynamic section structure */
12833965Sjdp
12933965Sjdptypedef struct elf_internal_dyn {
13033965Sjdp  /* This needs to support 64-bit values in elf64.  */
13133965Sjdp  bfd_vma d_tag;		/* entry tag value */
13233965Sjdp  union {
13333965Sjdp    /* This needs to support 64-bit values in elf64.  */
13433965Sjdp    bfd_vma	d_val;
13533965Sjdp    bfd_vma	d_ptr;
13633965Sjdp  } d_un;
13733965Sjdp} Elf_Internal_Dyn;
13833965Sjdp
13933965Sjdp/* This structure appears in a SHT_GNU_verdef section.  */
14033965Sjdp
14133965Sjdptypedef struct elf_internal_verdef {
14233965Sjdp  unsigned short vd_version;	/* Version number of structure.  */
14333965Sjdp  unsigned short vd_flags;	/* Flags (VER_FLG_*).  */
14433965Sjdp  unsigned short vd_ndx;	/* Version index.  */
14533965Sjdp  unsigned short vd_cnt;	/* Number of verdaux entries.  */
14633965Sjdp  unsigned long	 vd_hash;	/* Hash of name.  */
14733965Sjdp  unsigned long	 vd_aux;	/* Offset to verdaux entries.  */
14833965Sjdp  unsigned long	 vd_next;	/* Offset to next verdef.  */
14933965Sjdp
15033965Sjdp  /* These fields are set up when BFD reads in the structure.  FIXME:
15133965Sjdp     It would be cleaner to store these in a different structure.  */
15233965Sjdp  bfd			      *vd_bfd;		/* BFD.  */
15333965Sjdp  const char		      *vd_nodename;	/* Version name.  */
15433965Sjdp  struct elf_internal_verdef  *vd_nextdef;	/* vd_next as pointer.  */
15533965Sjdp  struct elf_internal_verdaux *vd_auxptr;	/* vd_aux as pointer.  */
15633965Sjdp  unsigned int		       vd_exp_refno;	/* Used by the linker.  */
15733965Sjdp} Elf_Internal_Verdef;
15833965Sjdp
15933965Sjdp/* This structure appears in a SHT_GNU_verdef section.  */
16033965Sjdp
16133965Sjdptypedef struct elf_internal_verdaux {
16233965Sjdp  unsigned long vda_name;	/* String table offset of name.  */
16333965Sjdp  unsigned long vda_next;	/* Offset to next verdaux.  */
16433965Sjdp
16533965Sjdp  /* These fields are set up when BFD reads in the structure.  FIXME:
16633965Sjdp     It would be cleaner to store these in a different structure.  */
16733965Sjdp  const char *vda_nodename;			/* vda_name as pointer.  */
16833965Sjdp  struct elf_internal_verdaux *vda_nextptr;	/* vda_next as pointer.  */
16933965Sjdp} Elf_Internal_Verdaux;
17060484Sobrien
17133965Sjdp/* This structure appears in a SHT_GNU_verneed section.  */
17233965Sjdp
17333965Sjdptypedef struct elf_internal_verneed {
17433965Sjdp  unsigned short vn_version;	/* Version number of structure.  */
17533965Sjdp  unsigned short vn_cnt;	/* Number of vernaux entries.  */
17633965Sjdp  unsigned long	 vn_file;	/* String table offset of library name.  */
17733965Sjdp  unsigned long	 vn_aux;	/* Offset to vernaux entries.  */
17833965Sjdp  unsigned long	 vn_next;	/* Offset to next verneed.  */
17933965Sjdp
18033965Sjdp  /* These fields are set up when BFD reads in the structure.  FIXME:
18133965Sjdp     It would be cleaner to store these in a different structure.  */
18233965Sjdp  bfd			      *vn_bfd;		/* BFD.  */
18333965Sjdp  const char                  *vn_filename;	/* vn_file as pointer.  */
18433965Sjdp  struct elf_internal_vernaux *vn_auxptr;	/* vn_aux as pointer.  */
18533965Sjdp  struct elf_internal_verneed *vn_nextref;	/* vn_nextref as pointer.  */
18633965Sjdp} Elf_Internal_Verneed;
18733965Sjdp
18833965Sjdp/* This structure appears in a SHT_GNU_verneed section.  */
18933965Sjdp
19033965Sjdptypedef struct elf_internal_vernaux {
19133965Sjdp  unsigned long	 vna_hash;	/* Hash of dependency name.  */
19233965Sjdp  unsigned short vna_flags;	/* Flags (VER_FLG_*).  */
19333965Sjdp  unsigned short vna_other;	/* Unused.  */
19433965Sjdp  unsigned long	 vna_name;	/* String table offset to version name.  */
19533965Sjdp  unsigned long	 vna_next;	/* Offset to next vernaux.  */
19633965Sjdp
19733965Sjdp  /* These fields are set up when BFD reads in the structure.  FIXME:
19833965Sjdp     It would be cleaner to store these in a different structure.  */
19933965Sjdp  const char                  *vna_nodename;	/* vna_name as pointer.  */
20033965Sjdp  struct elf_internal_vernaux *vna_nextptr;	/* vna_next as pointer.  */
20133965Sjdp} Elf_Internal_Vernaux;
20233965Sjdp
20333965Sjdp/* This structure appears in a SHT_GNU_versym section.  This is not a
20433965Sjdp   standard ELF structure; ELF just uses Elf32_Half.  */
20533965Sjdp
20633965Sjdptypedef struct elf_internal_versym {
20733965Sjdp  unsigned short vs_vers;
20833965Sjdp} Elf_Internal_Versym;
20933965Sjdp
21060484Sobrien/* Structure for syminfo section.  */
21160484Sobrientypedef struct
21260484Sobrien{
21360484Sobrien  unsigned short int 	si_boundto;
21460484Sobrien  unsigned short int	si_flags;
21560484Sobrien} Elf_Internal_Syminfo;
21660484Sobrien
217130561Sobrien/* This structure appears on the stack and in NT_AUXV core file notes.  */
218130561Sobrientypedef struct
219130561Sobrien{
220130561Sobrien  bfd_vma a_type;
221130561Sobrien  bfd_vma a_val;
222130561Sobrien} Elf_Internal_Auxv;
22360484Sobrien
22433965Sjdp
22533965Sjdp/* This structure is used to describe how sections should be assigned
22633965Sjdp   to program segments.  */
22733965Sjdp
22833965Sjdpstruct elf_segment_map
22933965Sjdp{
23033965Sjdp  /* Next program segment.  */
23133965Sjdp  struct elf_segment_map *next;
23233965Sjdp  /* Program segment type.  */
23333965Sjdp  unsigned long p_type;
23433965Sjdp  /* Program segment flags.  */
23533965Sjdp  unsigned long p_flags;
23633965Sjdp  /* Program segment physical address.  */
23733965Sjdp  bfd_vma p_paddr;
238218822Sdim  /* Program segment virtual address offset from section vma.  */
239218822Sdim  bfd_vma p_vaddr_offset;
240218822Sdim  /* Program segment alignment.  */
241218822Sdim  bfd_vma p_align;
24233965Sjdp  /* Whether the p_flags field is valid; if not, the flags are based
24333965Sjdp     on the section flags.  */
24433965Sjdp  unsigned int p_flags_valid : 1;
24533965Sjdp  /* Whether the p_paddr field is valid; if not, the physical address
24633965Sjdp     is based on the section lma values.  */
24733965Sjdp  unsigned int p_paddr_valid : 1;
248218822Sdim  /* Whether the p_align field is valid; if not, PT_LOAD segment
249218822Sdim     alignment is based on the default maximum page size.  */
250218822Sdim  unsigned int p_align_valid : 1;
25133965Sjdp  /* Whether this segment includes the file header.  */
25233965Sjdp  unsigned int includes_filehdr : 1;
25333965Sjdp  /* Whether this segment includes the program headers.  */
25433965Sjdp  unsigned int includes_phdrs : 1;
25533965Sjdp  /* Number of sections (may be 0).  */
25633965Sjdp  unsigned int count;
25733965Sjdp  /* Sections.  Actual number of elements is in count field.  */
25833965Sjdp  asection *sections[1];
25933965Sjdp};
26033965Sjdp
261218822Sdim/* .tbss is special.  It doesn't contribute memory space to normal
262218822Sdim   segments and it doesn't take file space in normal segments.  */
263218822Sdim#define ELF_SECTION_SIZE(sec_hdr, segment)			\
264218822Sdim   (((sec_hdr->sh_flags & SHF_TLS) == 0				\
265218822Sdim     || sec_hdr->sh_type != SHT_NOBITS				\
266218822Sdim     || segment->p_type == PT_TLS) ? sec_hdr->sh_size : 0)
267218822Sdim
268218822Sdim/* Decide if the given sec_hdr is in the given segment.  PT_TLS segment
269218822Sdim   contains only SHF_TLS sections.  Only PT_LOAD and PT_TLS segments
270218822Sdim   can contain SHF_TLS sections.  */
271218822Sdim#define ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment)			\
272218822Sdim  (((((sec_hdr->sh_flags & SHF_TLS) != 0)				\
273218822Sdim     && (segment->p_type == PT_TLS					\
274218822Sdim	 || segment->p_type == PT_LOAD))				\
275218822Sdim    || ((sec_hdr->sh_flags & SHF_TLS) == 0				\
276218822Sdim	&& segment->p_type != PT_TLS))					\
277218822Sdim   /* Any section besides one of type SHT_NOBITS must have a file	\
278218822Sdim      offset within the segment.  */					\
279218822Sdim   && (sec_hdr->sh_type == SHT_NOBITS					\
280218822Sdim       || ((bfd_vma) sec_hdr->sh_offset >= segment->p_offset		\
281218822Sdim	   && (sec_hdr->sh_offset + ELF_SECTION_SIZE(sec_hdr, segment)	\
282218822Sdim	       <= segment->p_offset + segment->p_filesz)))		\
283218822Sdim   /* SHF_ALLOC sections must have VMAs within the segment.  */		\
284218822Sdim   && ((sec_hdr->sh_flags & SHF_ALLOC) == 0				\
285218822Sdim       || (sec_hdr->sh_addr >= segment->p_vaddr				\
286218822Sdim	   && (sec_hdr->sh_addr + ELF_SECTION_SIZE(sec_hdr, segment)	\
287218822Sdim	       <= segment->p_vaddr + segment->p_memsz))))
288218822Sdim
289218822Sdim/* Decide if the given sec_hdr is in the given segment in file.  */
290218822Sdim#define ELF_IS_SECTION_IN_SEGMENT_FILE(sec_hdr, segment)	\
291218822Sdim  (sec_hdr->sh_size > 0						\
292218822Sdim   && ELF_IS_SECTION_IN_SEGMENT (sec_hdr, segment))
293218822Sdim
294218822Sdim/* Decide if the given sec_hdr is in the given segment in memory.  */
295218822Sdim#define ELF_IS_SECTION_IN_SEGMENT_MEMORY(sec_hdr, segment)	\
296218822Sdim  (ELF_SECTION_SIZE(sec_hdr, segment) > 0			\
297218822Sdim   && ELF_IS_SECTION_IN_SEGMENT (sec_hdr, segment))
298218822Sdim
29933965Sjdp#endif /* _ELF_INTERNAL_H */
300