1 /* Common code for ebl reloc functions.
2 Copyright (C) 2005, 2006 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18 Red Hat elfutils is an included package of the Open Invention Network.
19 An included package of the Open Invention Network is a package for which
20 Open Invention Network licensees cross-license their patents. No patent
21 license is granted, either expressly or impliedly, by designation as an
22 included package. Should you wish to participate in the Open Invention
23 Network licensing program, please visit www.openinventionnetwork.com
24 <http://www.openinventionnetwork.com>. */
26 #include "libebl_CPU.h"
29 #define R_TYPE(name) PASTE (RELOC_PREFIX, name)
30 #define PASTE(a, b) PASTE_1 (a, b)
31 #define PASTE_1(a, b) a##b
32 #define R_NAME(name) R_NAME_1 (RELOC_PREFIX, name)
33 #define R_NAME_1(prefix, type) R_NAME_2 (prefix, type)
34 #define R_NAME_2(prefix, type) #prefix #type
36 #define RELOC_TYPES STRINGIFIED_PASTE (BACKEND, reloc.def)
37 #define STRINGIFIED_PASTE(a, b) STRINGIFY (PASTE (a, b))
38 #define STRINGIFY(x) STRINGIFY_1 (x)
39 #define STRINGIFY_1(x) #x
41 /* Provide a table of reloc type names, in a PIC-friendly fashion. */
43 static const struct EBLHOOK(reloc_nametable)
46 #define RELOC_TYPE(type, uses) \
47 char name_##type[sizeof R_NAME (type)];
50 } EBLHOOK(reloc_nametable) =
53 #define RELOC_TYPE(type, uses) R_NAME (type),
57 #define reloc_namestr (&EBLHOOK(reloc_nametable).zero)
59 static const uint_fast16_t EBLHOOK(reloc_nameidx)[] =
61 #define RELOC_TYPE(type, uses) \
62 [R_TYPE (type)] = offsetof (struct EBLHOOK(reloc_nametable), name_##type),
67 ((int) (sizeof EBLHOOK(reloc_nameidx) / sizeof EBLHOOK(reloc_nameidx)[0]))
69 #define REL (1 << (ET_REL - 1))
70 #define EXEC (1 << (ET_EXEC - 1))
71 #define DYN (1 << (ET_DYN - 1))
72 static const uint8_t EBLHOOK(reloc_valid)[] =
74 #define RELOC_TYPE(type, uses) [R_TYPE (type)] = uses,
83 EBLHOOK(reloc_type_name) (int reloc,
84 char *buf __attribute__ ((unused)),
85 size_t len __attribute__ ((unused)))
87 if (reloc >= 0 && reloc < nreloc && EBLHOOK(reloc_nameidx)[reloc] != 0)
88 return &reloc_namestr[EBLHOOK(reloc_nameidx)[reloc]];
93 EBLHOOK(reloc_type_check) (int reloc)
95 return reloc >= 0 && reloc < nreloc && EBLHOOK(reloc_nameidx)[reloc] != 0;
99 EBLHOOK(reloc_valid_use) (Elf *elf, int reloc)
101 uint8_t uses = EBLHOOK(reloc_valid)[reloc];
104 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
105 assert (ehdr != NULL);
106 uint8_t type = ehdr->e_type;
108 return type > ET_NONE && type < ET_CORE && (uses & (1 << (type - 1)));
113 EBLHOOK(copy_reloc_p) (int reloc)
115 return reloc == R_TYPE (COPY);
119 EBLHOOK(none_reloc_p) (int reloc)
121 return reloc == R_TYPE (NONE);
124 #ifndef NO_RELATIVE_RELOC
126 EBLHOOK(relative_reloc_p) (int reloc)
128 return reloc == R_TYPE (RELATIVE);
133 EBLHOOK(init_reloc) (Ebl *ebl)
135 ebl->reloc_type_name = EBLHOOK(reloc_type_name);
136 ebl->reloc_type_check = EBLHOOK(reloc_type_check);
137 ebl->reloc_valid_use = EBLHOOK(reloc_valid_use);
138 ebl->copy_reloc_p = EBLHOOK(copy_reloc_p);
139 ebl->none_reloc_p = EBLHOOK(none_reloc_p);
140 #ifndef NO_RELATIVE_RELOC
141 ebl->relative_reloc_p = EBLHOOK(relative_reloc_p);