Home | History | Annotate | Download | only in elf
      1 /* ELF support for BFD.
      2    Copyright (C) 1991-2014 Free Software Foundation, Inc.
      3 
      4    Written by Fred Fish @ Cygnus Support, from information published
      5    in "UNIX System V Release 4, Programmers Guide: ANSI C and
      6    Programming Support Tools".
      7 
      8    This file is part of BFD, the Binary File Descriptor library.
      9 
     10    This program is free software; you can redistribute it and/or modify
     11    it under the terms of the GNU General Public License as published by
     12    the Free Software Foundation; either version 3 of the License, or
     13    (at your option) any later version.
     14 
     15    This program is distributed in the hope that it will be useful,
     16    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18    GNU General Public License for more details.
     19 
     20    You should have received a copy of the GNU General Public License
     21    along with this program; if not, write to the Free Software
     22    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     23    MA 02110-1301, USA.  */
     24 
     25 /* This file is part of ELF support for BFD, and contains the portions
     26    that describe how ELF is represented internally in the BFD library.
     27    I.E. it describes the in-memory representation of ELF.  It requires
     28    the elf-common.h file which contains the portions that are common to
     29    both the internal and external representations.  */
     30 
     31 /* NOTE that these structures are not kept in the same order as they appear
     32    in the object file.  In some cases they've been reordered for more optimal
     33    packing under various circumstances.  */
     34 
     35 #ifndef _ELF_INTERNAL_H
     36 #define _ELF_INTERNAL_H
     37 
     38 /* Special section indices, which may show up in st_shndx fields, among
     39    other places.  */
     40 
     41 #undef SHN_UNDEF
     42 #undef SHN_LORESERVE
     43 #undef SHN_LOPROC
     44 #undef SHN_HIPROC
     45 #undef SHN_LOOS
     46 #undef SHN_HIOS
     47 #undef SHN_ABS
     48 #undef SHN_COMMON
     49 #undef SHN_XINDEX
     50 #undef SHN_HIRESERVE
     51 #define SHN_UNDEF	0		/* Undefined section reference */
     52 #define SHN_LORESERVE	(-0x100u)	/* Begin range of reserved indices */
     53 #define SHN_LOPROC	(-0x100u)	/* Begin range of appl-specific */
     54 #define SHN_HIPROC	(-0xE1u)	/* End range of appl-specific */
     55 #define SHN_LOOS	(-0xE0u)	/* OS specific semantics, lo */
     56 #define SHN_HIOS	(-0xC1u)	/* OS specific semantics, hi */
     57 #define SHN_ABS		(-0xFu)		/* Associated symbol is absolute */
     58 #define SHN_COMMON	(-0xEu)		/* Associated symbol is in common */
     59 #define SHN_XINDEX	(-0x1u)		/* Section index is held elsewhere */
     60 #define SHN_HIRESERVE	(-0x1u)		/* End range of reserved indices */
     61 #define SHN_BAD		(-0x101u)	/* Used internally by bfd */
     62 
     63 /* ELF Header */
     64 
     65 #define EI_NIDENT	16		/* Size of e_ident[] */
     66 
     67 typedef struct elf_internal_ehdr {
     68   unsigned char		e_ident[EI_NIDENT]; /* ELF "magic number" */
     69   bfd_vma		e_entry;	/* Entry point virtual address */
     70   bfd_size_type		e_phoff;	/* Program header table file offset */
     71   bfd_size_type		e_shoff;	/* Section header table file offset */
     72   unsigned long		e_version;	/* Identifies object file version */
     73   unsigned long		e_flags;	/* Processor-specific flags */
     74   unsigned short	e_type;		/* Identifies object file type */
     75   unsigned short	e_machine;	/* Specifies required architecture */
     76   unsigned int		e_ehsize;	/* ELF header size in bytes */
     77   unsigned int		e_phentsize;	/* Program header table entry size */
     78   unsigned int		e_phnum;	/* Program header table entry count */
     79   unsigned int		e_shentsize;	/* Section header table entry size */
     80   unsigned int		e_shnum;	/* Section header table entry count */
     81   unsigned int		e_shstrndx;	/* Section header string table index */
     82 } Elf_Internal_Ehdr;
     83 
     84 /* Program header */
     85 
     86 struct elf_internal_phdr {
     87   unsigned long	p_type;			/* Identifies program segment type */
     88   unsigned long	p_flags;		/* Segment flags */
     89   bfd_vma	p_offset;		/* Segment file offset */
     90   bfd_vma	p_vaddr;		/* Segment virtual address */
     91   bfd_vma	p_paddr;		/* Segment physical address */
     92   bfd_vma	p_filesz;		/* Segment size in file */
     93   bfd_vma	p_memsz;		/* Segment size in memory */
     94   bfd_vma	p_align;		/* Segment alignment, file & memory */
     95 };
     96 
     97 typedef struct elf_internal_phdr Elf_Internal_Phdr;
     98 
     99 /* Section header */
    100 
    101 typedef struct elf_internal_shdr {
    102   unsigned int	sh_name;		/* Section name, index in string tbl */
    103   unsigned int	sh_type;		/* Type of section */
    104   bfd_vma	sh_flags;		/* Miscellaneous section attributes */
    105   bfd_vma	sh_addr;		/* Section virtual addr at execution */
    106   file_ptr	sh_offset;		/* Section file offset */
    107   bfd_size_type	sh_size;		/* Size of section in bytes */
    108   unsigned int	sh_link;		/* Index of another section */
    109   unsigned int	sh_info;		/* Additional section information */
    110   bfd_vma	sh_addralign;		/* Section alignment */
    111   bfd_size_type	sh_entsize;		/* Entry size if section holds table */
    112 
    113   /* The internal rep also has some cached info associated with it. */
    114   asection *	bfd_section;		/* Associated BFD section.  */
    115   unsigned char *contents;		/* Section contents.  */
    116 } Elf_Internal_Shdr;
    117 
    118 /* Symbol table entry */
    119 
    120 struct elf_internal_sym {
    121   bfd_vma	st_value;		/* Value of the symbol */
    122   bfd_vma	st_size;		/* Associated symbol size */
    123   unsigned long	st_name;		/* Symbol name, index in string tbl */
    124   unsigned char	st_info;		/* Type and binding attributes */
    125   unsigned char	st_other;		/* Visibilty, and target specific */
    126   unsigned char st_target_internal;	/* Internal-only information */
    127   unsigned int  st_shndx;		/* Associated section index */
    128 };
    129 
    130 typedef struct elf_internal_sym Elf_Internal_Sym;
    131 
    132 /* Note segments */
    133 
    134 typedef struct elf_internal_note {
    135   unsigned long	namesz;			/* Size of entry's owner string */
    136   unsigned long	descsz;			/* Size of the note descriptor */
    137   unsigned long	type;			/* Interpretation of the descriptor */
    138   char *	namedata;		/* Start of the name+desc data */
    139   char *	descdata;		/* Start of the desc data */
    140   bfd_vma	descpos;		/* File offset of the descdata */
    141 } Elf_Internal_Note;
    142 
    143 /* Relocation Entries */
    144 
    145 typedef struct elf_internal_rela {
    146   bfd_vma	r_offset;	/* Location at which to apply the action */
    147   bfd_vma	r_info;		/* Index and Type of relocation */
    148   bfd_vma	r_addend;	/* Constant addend used to compute value */
    149 } Elf_Internal_Rela;
    150 
    151 /* dynamic section structure */
    152 
    153 typedef struct elf_internal_dyn {
    154   /* This needs to support 64-bit values in elf64.  */
    155   bfd_vma d_tag;		/* entry tag value */
    156   union {
    157     /* This needs to support 64-bit values in elf64.  */
    158     bfd_vma	d_val;
    159     bfd_vma	d_ptr;
    160   } d_un;
    161 } Elf_Internal_Dyn;
    162 
    163 /* This structure appears in a SHT_GNU_verdef section.  */
    164 
    165 typedef struct elf_internal_verdef {
    166   unsigned short vd_version;	/* Version number of structure.  */
    167   unsigned short vd_flags;	/* Flags (VER_FLG_*).  */
    168   unsigned short vd_ndx;	/* Version index.  */
    169   unsigned short vd_cnt;	/* Number of verdaux entries.  */
    170   unsigned long	 vd_hash;	/* Hash of name.  */
    171   unsigned long	 vd_aux;	/* Offset to verdaux entries.  */
    172   unsigned long	 vd_next;	/* Offset to next verdef.  */
    173 
    174   /* These fields are set up when BFD reads in the structure.  FIXME:
    175      It would be cleaner to store these in a different structure.  */
    176   bfd			      *vd_bfd;		/* BFD.  */
    177   const char		      *vd_nodename;	/* Version name.  */
    178   struct elf_internal_verdef  *vd_nextdef;	/* vd_next as pointer.  */
    179   struct elf_internal_verdaux *vd_auxptr;	/* vd_aux as pointer.  */
    180   unsigned int		       vd_exp_refno;	/* Used by the linker.  */
    181 } Elf_Internal_Verdef;
    182 
    183 /* This structure appears in a SHT_GNU_verdef section.  */
    184 
    185 typedef struct elf_internal_verdaux {
    186   unsigned long vda_name;	/* String table offset of name.  */
    187   unsigned long vda_next;	/* Offset to next verdaux.  */
    188 
    189   /* These fields are set up when BFD reads in the structure.  FIXME:
    190      It would be cleaner to store these in a different structure.  */
    191   const char *vda_nodename;			/* vda_name as pointer.  */
    192   struct elf_internal_verdaux *vda_nextptr;	/* vda_next as pointer.  */
    193 } Elf_Internal_Verdaux;
    194 
    195 /* This structure appears in a SHT_GNU_verneed section.  */
    196 
    197 typedef struct elf_internal_verneed {
    198   unsigned short vn_version;	/* Version number of structure.  */
    199   unsigned short vn_cnt;	/* Number of vernaux entries.  */
    200   unsigned long	 vn_file;	/* String table offset of library name.  */
    201   unsigned long	 vn_aux;	/* Offset to vernaux entries.  */
    202   unsigned long	 vn_next;	/* Offset to next verneed.  */
    203 
    204   /* These fields are set up when BFD reads in the structure.  FIXME:
    205      It would be cleaner to store these in a different structure.  */
    206   bfd			      *vn_bfd;		/* BFD.  */
    207   const char                  *vn_filename;	/* vn_file as pointer.  */
    208   struct elf_internal_vernaux *vn_auxptr;	/* vn_aux as pointer.  */
    209   struct elf_internal_verneed *vn_nextref;	/* vn_nextref as pointer.  */
    210 } Elf_Internal_Verneed;
    211 
    212 /* This structure appears in a SHT_GNU_verneed section.  */
    213 
    214 typedef struct elf_internal_vernaux {
    215   unsigned long	 vna_hash;	/* Hash of dependency name.  */
    216   unsigned short vna_flags;	/* Flags (VER_FLG_*).  */
    217   unsigned short vna_other;	/* Unused.  */
    218   unsigned long	 vna_name;	/* String table offset to version name.  */
    219   unsigned long	 vna_next;	/* Offset to next vernaux.  */
    220 
    221   /* These fields are set up when BFD reads in the structure.  FIXME:
    222      It would be cleaner to store these in a different structure.  */
    223   const char                  *vna_nodename;	/* vna_name as pointer.  */
    224   struct elf_internal_vernaux *vna_nextptr;	/* vna_next as pointer.  */
    225 } Elf_Internal_Vernaux;
    226 
    227 /* This structure appears in a SHT_GNU_versym section.  This is not a
    228    standard ELF structure; ELF just uses Elf32_Half.  */
    229 
    230 typedef struct elf_internal_versym {
    231   unsigned short vs_vers;
    232 } Elf_Internal_Versym;
    233 
    234 /* Structure for syminfo section.  */
    235 typedef struct
    236 {
    237   unsigned short int 	si_boundto;
    238   unsigned short int	si_flags;
    239 } Elf_Internal_Syminfo;
    240 
    241 /* This structure appears on the stack and in NT_AUXV core file notes.  */
    242 typedef struct
    243 {
    244   bfd_vma a_type;
    245   bfd_vma a_val;
    246 } Elf_Internal_Auxv;
    247 
    248 
    249 /* This structure is used to describe how sections should be assigned
    250    to program segments.  */
    251 
    252 struct elf_segment_map
    253 {
    254   /* Next program segment.  */
    255   struct elf_segment_map *next;
    256   /* Program segment type.  */
    257   unsigned long p_type;
    258   /* Program segment flags.  */
    259   unsigned long p_flags;
    260   /* Program segment physical address.  */
    261   bfd_vma p_paddr;
    262   /* Program segment virtual address offset from section vma.  */
    263   bfd_vma p_vaddr_offset;
    264   /* Program segment alignment.  */
    265   bfd_vma p_align;
    266   /* Segment size in file and memory */
    267   bfd_vma p_size;
    268   /* Required size of filehdr + phdrs, if non-zero */
    269   bfd_vma header_size;
    270   /* Whether the p_flags field is valid; if not, the flags are based
    271      on the section flags.  */
    272   unsigned int p_flags_valid : 1;
    273   /* Whether the p_paddr field is valid; if not, the physical address
    274      is based on the section lma values.  */
    275   unsigned int p_paddr_valid : 1;
    276   /* Whether the p_align field is valid; if not, PT_LOAD segment
    277      alignment is based on the default maximum page size.  */
    278   unsigned int p_align_valid : 1;
    279   /* Whether the p_size field is valid; if not, the size are based
    280      on the section sizes.  */
    281   unsigned int p_size_valid : 1;
    282   /* Whether this segment includes the file header.  */
    283   unsigned int includes_filehdr : 1;
    284   /* Whether this segment includes the program headers.  */
    285   unsigned int includes_phdrs : 1;
    286   /* Number of sections (may be 0).  */
    287   unsigned int count;
    288   /* Sections.  Actual number of elements is in count field.  */
    289   asection *sections[1];
    290 };
    291 
    292 /* .tbss is special.  It doesn't contribute memory space to normal
    293    segments and it doesn't take file space in normal segments.  */
    294 #define ELF_TBSS_SPECIAL(sec_hdr, segment)			\
    295   (((sec_hdr)->sh_flags & SHF_TLS) != 0				\
    296    && (sec_hdr)->sh_type == SHT_NOBITS				\
    297    && (segment)->p_type != PT_TLS)
    298 
    299 #define ELF_SECTION_SIZE(sec_hdr, segment)			\
    300   (ELF_TBSS_SPECIAL(sec_hdr, segment) ? 0 : (sec_hdr)->sh_size)
    301 
    302 /* Decide if the section SEC_HDR is in SEGMENT.  If CHECK_VMA, then
    303    VMAs are checked for alloc sections.  If STRICT, then a zero size
    304    section won't match at the end of a segment, unless the segment
    305    is also zero size.  Regardless of STRICT and CHECK_VMA, zero size
    306    sections won't match at the start or end of PT_DYNAMIC, unless
    307    PT_DYNAMIC is itself zero sized.  */
    308 #define ELF_SECTION_IN_SEGMENT_1(sec_hdr, segment, check_vma, strict)	\
    309   ((/* Only PT_LOAD, PT_GNU_RELRO and PT_TLS segments can contain	\
    310        SHF_TLS sections.  */						\
    311     ((((sec_hdr)->sh_flags & SHF_TLS) != 0)				\
    312      && ((segment)->p_type == PT_TLS					\
    313 	 || (segment)->p_type == PT_GNU_RELRO				\
    314 	 || (segment)->p_type == PT_LOAD))				\
    315     /* PT_TLS segment contains only SHF_TLS sections, PT_PHDR no	\
    316        sections at all.  */						\
    317     || (((sec_hdr)->sh_flags & SHF_TLS) == 0				\
    318 	&& (segment)->p_type != PT_TLS					\
    319 	&& (segment)->p_type != PT_PHDR))				\
    320    /* Any section besides one of type SHT_NOBITS must have file		\
    321       offsets within the segment.  */					\
    322    && ((sec_hdr)->sh_type == SHT_NOBITS					\
    323        || ((bfd_vma) (sec_hdr)->sh_offset >= (segment)->p_offset	\
    324 	   && (!(strict)						\
    325 	       || ((sec_hdr)->sh_offset - (segment)->p_offset		\
    326 		   <= (segment)->p_filesz - 1))				\
    327 	   && (((sec_hdr)->sh_offset - (segment)->p_offset		\
    328 		+ ELF_SECTION_SIZE(sec_hdr, segment))			\
    329 	       <= (segment)->p_filesz)))				\
    330    /* SHF_ALLOC sections must have VMAs within the segment.  */		\
    331    && (!(check_vma)							\
    332        || ((sec_hdr)->sh_flags & SHF_ALLOC) == 0			\
    333        || ((sec_hdr)->sh_addr >= (segment)->p_vaddr			\
    334 	   && (!(strict)						\
    335 	       || ((sec_hdr)->sh_addr - (segment)->p_vaddr		\
    336 		   <= (segment)->p_memsz - 1))				\
    337 	   && (((sec_hdr)->sh_addr - (segment)->p_vaddr			\
    338 		+ ELF_SECTION_SIZE(sec_hdr, segment))			\
    339 	       <= (segment)->p_memsz)))					\
    340    /* No zero size sections at start or end of PT_DYNAMIC.  */		\
    341    && ((segment)->p_type != PT_DYNAMIC					\
    342        || (sec_hdr)->sh_size != 0					\
    343        || (segment)->p_memsz == 0					\
    344        || (((sec_hdr)->sh_type == SHT_NOBITS				\
    345 	    || ((bfd_vma) (sec_hdr)->sh_offset > (segment)->p_offset	\
    346 	        && ((sec_hdr)->sh_offset - (segment)->p_offset		\
    347 		    < (segment)->p_filesz)))				\
    348 	   && (((sec_hdr)->sh_flags & SHF_ALLOC) == 0			\
    349 	       || ((sec_hdr)->sh_addr > (segment)->p_vaddr		\
    350 		   && ((sec_hdr)->sh_addr - (segment)->p_vaddr		\
    351 		       < (segment)->p_memsz))))))
    352 
    353 #define ELF_SECTION_IN_SEGMENT(sec_hdr, segment)			\
    354   (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 0))
    355 
    356 #define ELF_SECTION_IN_SEGMENT_STRICT(sec_hdr, segment)			\
    357   (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 1))
    358 
    359 #endif /* _ELF_INTERNAL_H */
    360