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