1/* ELF support for BFD.
2   Copyright (C) 1991-2020 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
67typedef 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
86struct 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
97typedef struct elf_internal_phdr Elf_Internal_Phdr;
98
99/* Section header */
100
101typedef 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/* Compression header */
119
120typedef struct elf_internal_chdr {
121  unsigned int	ch_type;		/* Type of compression */
122  bfd_size_type	ch_size;		/* Size of uncompressed data in bytes */
123  bfd_vma	ch_addralign;		/* Alignment of uncompressed data */
124} Elf_Internal_Chdr;
125
126/* Symbol table entry */
127
128struct elf_internal_sym {
129  bfd_vma	st_value;		/* Value of the symbol */
130  bfd_vma	st_size;		/* Associated symbol size */
131  unsigned long	st_name;		/* Symbol name, index in string tbl */
132  unsigned char	st_info;		/* Type and binding attributes */
133  unsigned char	st_other;		/* Visibilty, and target specific */
134  unsigned char st_target_internal;	/* Internal-only information */
135  unsigned int  st_shndx;		/* Associated section index */
136};
137
138typedef struct elf_internal_sym Elf_Internal_Sym;
139
140/* Note segments */
141
142typedef struct elf_internal_note {
143  unsigned long	namesz;			/* Size of entry's owner string */
144  unsigned long	descsz;			/* Size of the note descriptor */
145  unsigned long	type;			/* Interpretation of the descriptor */
146  char *	namedata;		/* Start of the name+desc data */
147  char *	descdata;		/* Start of the desc data */
148  bfd_vma	descpos;		/* File offset of the descdata */
149} Elf_Internal_Note;
150
151/* Relocation Entries */
152
153typedef struct elf_internal_rela {
154  bfd_vma	r_offset;	/* Location at which to apply the action */
155  bfd_vma	r_info;		/* Index and Type of relocation */
156  bfd_vma	r_addend;	/* Constant addend used to compute value */
157} Elf_Internal_Rela;
158
159/* dynamic section structure */
160
161typedef struct elf_internal_dyn {
162  /* This needs to support 64-bit values in elf64.  */
163  bfd_vma d_tag;		/* entry tag value */
164  union {
165    /* This needs to support 64-bit values in elf64.  */
166    bfd_vma	d_val;
167    bfd_vma	d_ptr;
168  } d_un;
169} Elf_Internal_Dyn;
170
171/* This structure appears in a SHT_GNU_verdef section.  */
172
173typedef struct elf_internal_verdef {
174  unsigned short vd_version;	/* Version number of structure.  */
175  unsigned short vd_flags;	/* Flags (VER_FLG_*).  */
176  unsigned short vd_ndx;	/* Version index.  */
177  unsigned short vd_cnt;	/* Number of verdaux entries.  */
178  unsigned long	 vd_hash;	/* Hash of name.  */
179  unsigned long	 vd_aux;	/* Offset to verdaux entries.  */
180  unsigned long	 vd_next;	/* Offset to next verdef.  */
181
182  /* These fields are set up when BFD reads in the structure.  FIXME:
183     It would be cleaner to store these in a different structure.  */
184  bfd			      *vd_bfd;		/* BFD.  */
185  const char		      *vd_nodename;	/* Version name.  */
186  struct elf_internal_verdef  *vd_nextdef;	/* vd_next as pointer.  */
187  struct elf_internal_verdaux *vd_auxptr;	/* vd_aux as pointer.  */
188  unsigned int		       vd_exp_refno;	/* Used by the linker.  */
189} Elf_Internal_Verdef;
190
191/* This structure appears in a SHT_GNU_verdef section.  */
192
193typedef struct elf_internal_verdaux {
194  unsigned long vda_name;	/* String table offset of name.  */
195  unsigned long vda_next;	/* Offset to next verdaux.  */
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 *vda_nodename;			/* vda_name as pointer.  */
200  struct elf_internal_verdaux *vda_nextptr;	/* vda_next as pointer.  */
201} Elf_Internal_Verdaux;
202
203/* This structure appears in a SHT_GNU_verneed section.  */
204
205typedef struct elf_internal_verneed {
206  unsigned short vn_version;	/* Version number of structure.  */
207  unsigned short vn_cnt;	/* Number of vernaux entries.  */
208  unsigned long	 vn_file;	/* String table offset of library name.  */
209  unsigned long	 vn_aux;	/* Offset to vernaux entries.  */
210  unsigned long	 vn_next;	/* Offset to next verneed.  */
211
212  /* These fields are set up when BFD reads in the structure.  FIXME:
213     It would be cleaner to store these in a different structure.  */
214  bfd			      *vn_bfd;		/* BFD.  */
215  const char                  *vn_filename;	/* vn_file as pointer.  */
216  struct elf_internal_vernaux *vn_auxptr;	/* vn_aux as pointer.  */
217  struct elf_internal_verneed *vn_nextref;	/* vn_nextref as pointer.  */
218} Elf_Internal_Verneed;
219
220/* This structure appears in a SHT_GNU_verneed section.  */
221
222typedef struct elf_internal_vernaux {
223  unsigned long	 vna_hash;	/* Hash of dependency name.  */
224  unsigned short vna_flags;	/* Flags (VER_FLG_*).  */
225  unsigned short vna_other;	/* Unused.  */
226  unsigned long	 vna_name;	/* String table offset to version name.  */
227  unsigned long	 vna_next;	/* Offset to next vernaux.  */
228
229  /* These fields are set up when BFD reads in the structure.  FIXME:
230     It would be cleaner to store these in a different structure.  */
231  const char                  *vna_nodename;	/* vna_name as pointer.  */
232  struct elf_internal_vernaux *vna_nextptr;	/* vna_next as pointer.  */
233} Elf_Internal_Vernaux;
234
235/* This structure appears in a SHT_GNU_versym section.  This is not a
236   standard ELF structure; ELF just uses Elf32_Half.  */
237
238typedef struct elf_internal_versym {
239  unsigned short vs_vers;
240} Elf_Internal_Versym;
241
242/* Structure for syminfo section.  */
243typedef struct
244{
245  unsigned short int 	si_boundto;
246  unsigned short int	si_flags;
247} Elf_Internal_Syminfo;
248
249/* This structure appears on the stack and in NT_AUXV core file notes.  */
250typedef struct
251{
252  bfd_vma a_type;
253  bfd_vma a_val;
254} Elf_Internal_Auxv;
255
256
257/* This structure is used to describe how sections should be assigned
258   to program segments.  */
259
260struct elf_segment_map
261{
262  /* Next program segment.  */
263  struct elf_segment_map *next;
264  /* Program segment type.  */
265  unsigned long p_type;
266  /* Program segment flags.  */
267  unsigned long p_flags;
268  /* Program segment physical address.  */
269  bfd_vma p_paddr;
270  /* Program segment virtual address offset from section vma.  */
271  bfd_vma p_vaddr_offset;
272  /* Program segment alignment.  */
273  bfd_vma p_align;
274  /* Segment size in file and memory */
275  bfd_vma p_size;
276  /* Whether the p_flags field is valid; if not, the flags are based
277     on the section flags.  */
278  unsigned int p_flags_valid : 1;
279  /* Whether the p_paddr field is valid; if not, the physical address
280     is based on the section lma values.  */
281  unsigned int p_paddr_valid : 1;
282  /* Whether the p_align field is valid; if not, PT_LOAD segment
283     alignment is based on the default maximum page size.  */
284  unsigned int p_align_valid : 1;
285  /* Whether the p_size field is valid; if not, the size are based
286     on the section sizes.  */
287  unsigned int p_size_valid : 1;
288  /* Whether this segment includes the file header.  */
289  unsigned int includes_filehdr : 1;
290  /* Whether this segment includes the program headers.  */
291  unsigned int includes_phdrs : 1;
292  /* Assume this PT_LOAD header has an lma of zero when sorting
293     headers before assigning file offsets.  PT_LOAD headers with this
294     flag set are placed after one with includes_filehdr set, and
295     before PT_LOAD headers without this flag set.  */
296  unsigned int no_sort_lma : 1;
297  /* Index holding original order before sorting segments.  */
298  unsigned int idx;
299  /* Number of sections (may be 0).  */
300  unsigned int count;
301  /* Sections.  Actual number of elements is in count field.  */
302  asection *sections[1];
303};
304
305/* .tbss is special.  It doesn't contribute memory space to normal
306   segments and it doesn't take file space in normal segments.  */
307#define ELF_TBSS_SPECIAL(sec_hdr, segment)			\
308  (((sec_hdr)->sh_flags & SHF_TLS) != 0				\
309   && (sec_hdr)->sh_type == SHT_NOBITS				\
310   && (segment)->p_type != PT_TLS)
311
312#define ELF_SECTION_SIZE(sec_hdr, segment)			\
313  (ELF_TBSS_SPECIAL(sec_hdr, segment) ? 0 : (sec_hdr)->sh_size)
314
315/* Decide if the section SEC_HDR is in SEGMENT.  If CHECK_VMA, then
316   VMAs are checked for alloc sections.  If STRICT, then a zero size
317   section won't match at the end of a segment, unless the segment
318   is also zero size.  Regardless of STRICT and CHECK_VMA, zero size
319   sections won't match at the start or end of PT_DYNAMIC nor PT_NOTE,
320   unless PT_DYNAMIC and PT_NOTE are themselves zero sized.  */
321#define ELF_SECTION_IN_SEGMENT_1(sec_hdr, segment, check_vma, strict)	\
322  ((/* Only PT_LOAD, PT_GNU_RELRO and PT_TLS segments can contain	\
323       SHF_TLS sections.  */						\
324    ((((sec_hdr)->sh_flags & SHF_TLS) != 0)				\
325     && ((segment)->p_type == PT_TLS					\
326	 || (segment)->p_type == PT_GNU_RELRO				\
327	 || (segment)->p_type == PT_LOAD))				\
328    /* PT_TLS segment contains only SHF_TLS sections, PT_PHDR no	\
329       sections at all.  */						\
330    || (((sec_hdr)->sh_flags & SHF_TLS) == 0				\
331	&& (segment)->p_type != PT_TLS					\
332	&& (segment)->p_type != PT_PHDR))				\
333   /* PT_LOAD and similar segments only have SHF_ALLOC sections.  */	\
334   && !(((sec_hdr)->sh_flags & SHF_ALLOC) == 0				\
335	&& ((segment)->p_type == PT_LOAD				\
336	    || (segment)->p_type == PT_DYNAMIC				\
337	    || (segment)->p_type == PT_GNU_EH_FRAME			\
338	    || (segment)->p_type == PT_GNU_STACK			\
339	    || (segment)->p_type == PT_GNU_RELRO			\
340	    || ((segment)->p_type >= PT_GNU_MBIND_LO			\
341		&& (segment)->p_type <= PT_GNU_MBIND_HI)))		\
342   /* Any section besides one of type SHT_NOBITS must have file		\
343      offsets within the segment.  */					\
344   && ((sec_hdr)->sh_type == SHT_NOBITS					\
345       || ((bfd_vma) (sec_hdr)->sh_offset >= (segment)->p_offset	\
346	   && (!(strict)						\
347	       || ((sec_hdr)->sh_offset - (segment)->p_offset		\
348		   <= (segment)->p_filesz - 1))				\
349	   && (((sec_hdr)->sh_offset - (segment)->p_offset		\
350		+ ELF_SECTION_SIZE(sec_hdr, segment))			\
351	       <= (segment)->p_filesz)))				\
352   /* SHF_ALLOC sections must have VMAs within the segment.  */		\
353   && (!(check_vma)							\
354       || ((sec_hdr)->sh_flags & SHF_ALLOC) == 0			\
355       || ((sec_hdr)->sh_addr >= (segment)->p_vaddr			\
356	   && (!(strict)						\
357	       || ((sec_hdr)->sh_addr - (segment)->p_vaddr		\
358		   <= (segment)->p_memsz - 1))				\
359	   && (((sec_hdr)->sh_addr - (segment)->p_vaddr			\
360		+ ELF_SECTION_SIZE(sec_hdr, segment))			\
361	       <= (segment)->p_memsz)))					\
362   /* No zero size sections at start or end of PT_DYNAMIC nor		\
363      PT_NOTE.  */							\
364   && (((segment)->p_type != PT_DYNAMIC					\
365	&& (segment)->p_type != PT_NOTE)				\
366       || (sec_hdr)->sh_size != 0					\
367       || (segment)->p_memsz == 0					\
368       || (((sec_hdr)->sh_type == SHT_NOBITS				\
369	    || ((bfd_vma) (sec_hdr)->sh_offset > (segment)->p_offset	\
370	        && ((sec_hdr)->sh_offset - (segment)->p_offset		\
371		    < (segment)->p_filesz)))				\
372	   && (((sec_hdr)->sh_flags & SHF_ALLOC) == 0			\
373	       || ((sec_hdr)->sh_addr > (segment)->p_vaddr		\
374		   && ((sec_hdr)->sh_addr - (segment)->p_vaddr		\
375		       < (segment)->p_memsz))))))
376
377#define ELF_SECTION_IN_SEGMENT(sec_hdr, segment)			\
378  (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 0))
379
380#define ELF_SECTION_IN_SEGMENT_STRICT(sec_hdr, segment)			\
381  (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 1))
382
383#endif /* _ELF_INTERNAL_H */
384