1/* Accumulation of various pieces of knowledge about ELF.
2   Copyright (C) 2000, 2001, 2002, 2003 Red Hat, Inc.
3   Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, version 2.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software Foundation,
16   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18#ifndef _ELF_KNOWLEDGE_H
19#define _ELF_KNOWLEDGE_H	1
20
21
22/* Test whether a section can be stripped or not.  */
23#define SECTION_STRIP_P(ebl, elf, ehdr, shdr, name, remove_comment, remove_debug) \
24  (/* Sections which are allocated are not removed.  */			      \
25   ((shdr)->sh_flags & SHF_ALLOC) == 0					      \
26   /* We never remove .note sections.  */				      \
27   && (shdr)->sh_type != SHT_NOTE					      \
28   /* If only debug information should be removed check the name.  There      \
29      is unfortunately no way.  */					      \
30   && (!remove_debug							      \
31       || ebl_debugscn_p (ebl, name)					      \
32       || (((shdr)->sh_type == SHT_RELA || (shdr)->sh_type == SHT_REL)	      \
33	   && ({ Elf_Scn *scn_l = elf_getscn (elf, (shdr)->sh_info);	      \
34		 GElf_Shdr shdr_mem_l;					      \
35		 GElf_Shdr *shdr_l = gelf_getshdr (scn_l, &shdr_mem_l);	      \
36		 const char *s_l;					      \
37		 shdr_l != NULL						      \
38		   && (s_l = elf_strptr (elf,				      \
39					 ((GElf_Ehdr *) (ehdr))->e_shstrndx,  \
40					 shdr_l->sh_name)) != NULL	      \
41		   && ebl_debugscn_p (ebl, s_l); })))			      \
42   && ((shdr)->sh_type != SHT_PROGBITS					      \
43   /* Never remove .gnu.warning.* sections.  */				      \
44       || (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) != 0   \
45   /* We remove .comment sections only if explicitly told to do so.  */	      \
46	   && ((remove_comment) || strcmp (name, ".comment") != 0)))	      \
47   /* So far we do not remove any of the non-standard sections.		      \
48      XXX Maybe in future.  */						      \
49   && (shdr)->sh_type < SHT_NUM)
50
51
52/* Test whether `sh_info' field in section header contains a section
53   index.  There are two kinds of sections doing this:
54
55   - the sections containing relocation information reference in this
56     field the section to which the relocations apply;
57
58   - section with the SHF_INFO_LINK flag set to signal that `sh_info'
59     references a section.  This allows correct handling of unknown
60     sections.  */
61#define SH_INFO_LINK_P(Shdr) \
62  ((Shdr)->sh_type == SHT_REL || (Shdr)->sh_type == SHT_RELA		      \
63   || ((Shdr)->sh_flags & SHF_INFO_LINK) != 0)
64
65
66/* When combining ELF section flags we must distinguish two kinds:
67
68   - flags which cause problem if not added to the result even if not
69     present in all input sections
70
71   - flags which cause problem if added to the result if not present
72     in all input sections
73
74   The following definition is for the general case.  There might be
75   machine specific extensions.  */
76#define SH_FLAGS_COMBINE(Flags1, Flags2) \
77  (((Flags1 | Flags2)							      \
78    & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR | SHF_LINK_ORDER		      \
79       | SHF_OS_NONCONFORMING | SHF_GROUP))				      \
80   | (Flags1 & Flags2 & (SHF_MERGE | SHF_STRINGS | SHF_INFO_LINK)))
81
82/* Similar macro: return the bits of the flags which necessarily must
83   match if two sections are automatically combined.  Sections still
84   can be forcefully combined in which case SH_FLAGS_COMBINE can be
85   used to determine the combined flags.  */
86#define SH_FLAGS_IMPORTANT(Flags) \
87  ((Flags) & ~((GElf_Xword) 0 | SHF_LINK_ORDER | SHF_OS_NONCONFORMING))
88
89
90/* Size of an entry in the hash table.  The ELF specification says all
91   entries are regardless of platform 32-bits in size.  Early 64-bit
92   ports (namely Alpha for Linux) got this wrong.  The wording was not
93   clear.
94
95   Several years later the ABI for the 64-bit S390s was developed.
96   Many things were copied from the IA-64 ABI (which uses the correct
97   32-bit entry size) but what do these people do?  They use 64-bit
98   entries.  It is really shocking to see what kind of morons are out
99   there.  And even worse: they are allowed to design ABIs.  */
100#define SH_ENTSIZE_HASH(Ehdr) \
101  ((Ehdr)->e_machine == EM_ALPHA					      \
102   || ((Ehdr)->e_machine == EM_S390					      \
103       && (Ehdr)->e_ident[EI_CLASS] == ELFCLASS64) ? 8 : 4)
104
105#endif	/* elf-knowledge.h */
106