99fb910718ddd3adad6ba1de284f6efb752b51c2
[platform/upstream/elfutils.git] / libelf / elf-knowledge.h
1 /* Accumulation of various pieces of knowledge about ELF.
2    Copyright (C) 2000-2012 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        || (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) != 0   \
45            /* We remove .comment sections only if explicitly told to do so. */\
46            && (remove_comment                                                 \
47                || strcmp (name, ".comment") != 0))))
48
49
50 /* Test whether `sh_info' field in section header contains a section
51    index.  There are two kinds of sections doing this:
52
53    - the sections containing relocation information reference in this
54      field the section to which the relocations apply;
55
56    - section with the SHF_INFO_LINK flag set to signal that `sh_info'
57      references a section.  This allows correct handling of unknown
58      sections.  */
59 #define SH_INFO_LINK_P(Shdr) \
60   ((Shdr)->sh_type == SHT_REL || (Shdr)->sh_type == SHT_RELA                  \
61    || ((Shdr)->sh_flags & SHF_INFO_LINK) != 0)
62
63
64 /* When combining ELF section flags we must distinguish two kinds:
65
66    - flags which cause problem if not added to the result even if not
67      present in all input sections
68
69    - flags which cause problem if added to the result if not present
70      in all input sections
71
72    The following definition is for the general case.  There might be
73    machine specific extensions.  */
74 #define SH_FLAGS_COMBINE(Flags1, Flags2) \
75   (((Flags1 | Flags2)                                                         \
76     & (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR | SHF_LINK_ORDER                 \
77        | SHF_OS_NONCONFORMING | SHF_GROUP))                                   \
78    | (Flags1 & Flags2 & (SHF_MERGE | SHF_STRINGS | SHF_INFO_LINK)))
79
80 /* Similar macro: return the bits of the flags which necessarily must
81    match if two sections are automatically combined.  Sections still
82    can be forcefully combined in which case SH_FLAGS_COMBINE can be
83    used to determine the combined flags.  */
84 #define SH_FLAGS_IMPORTANT(Flags) \
85   ((Flags) & ~((GElf_Xword) 0 | SHF_LINK_ORDER | SHF_OS_NONCONFORMING))
86
87
88 /* Size of an entry in the hash table.  The ELF specification says all
89    entries are regardless of platform 32-bits in size.  Early 64-bit
90    ports (namely Alpha for Linux) got this wrong.  The wording was not
91    clear.
92
93    Several years later the ABI for the 64-bit S390s was developed.
94    Many things were copied from the IA-64 ABI (which uses the correct
95    32-bit entry size) but what do these people do?  They use 64-bit
96    entries.  It is really shocking to see what kind of morons are out
97    there.  And even worse: they are allowed to design ABIs.  */
98 #define SH_ENTSIZE_HASH(Ehdr) \
99   ((Ehdr)->e_machine == EM_ALPHA                                              \
100    || ((Ehdr)->e_machine == EM_S390                                           \
101        && (Ehdr)->e_ident[EI_CLASS] == ELFCLASS64) ? 8 : 4)
102
103 #endif  /* elf-knowledge.h */