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