]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Inline functions for dynamic linking. |
c0fb8a56 | 2 | Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
afd4eb37 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
d66e34cd RM |
19 | |
20 | #include <elf.h> | |
d66e34cd | 21 | #include <dl-machine.h> |
d66e34cd RM |
22 | #include <assert.h> |
23 | ||
421f82e5 | 24 | |
cf29ffbe UD |
25 | /* Global read-only variable defined in rtld.c which is nonzero if we |
26 | shall give more warning messages. */ | |
27 | extern int _dl_verbose __attribute__ ((unused)); | |
28 | ||
29 | ||
d66e34cd RM |
30 | /* Read the dynamic section at DYN and fill in INFO with indices DT_*. */ |
31 | ||
e75154a6 | 32 | static inline void __attribute__ ((unused)) |
df4ef2ab | 33 | elf_get_dynamic_info (ElfW(Dyn) *dyn, |
1228ed5c UD |
34 | ElfW(Dyn) *info[DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM |
35 | + DT_EXTRANUM]) | |
d66e34cd RM |
36 | { |
37 | unsigned int i; | |
38 | ||
1228ed5c | 39 | for (i = 0; i < DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM; ++i) |
d66e34cd RM |
40 | info[i] = NULL; |
41 | ||
b122c703 RM |
42 | if (! dyn) |
43 | return; | |
44 | ||
d66e34cd RM |
45 | while (dyn->d_tag != DT_NULL) |
46 | { | |
a2e1b046 RM |
47 | if (dyn->d_tag < DT_NUM) |
48 | info[dyn->d_tag] = dyn; | |
49 | else if (dyn->d_tag >= DT_LOPROC && | |
50 | dyn->d_tag < DT_LOPROC + DT_PROCNUM) | |
51 | info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn; | |
1228ed5c UD |
52 | else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM) |
53 | info[DT_VERSIONTAGIDX (dyn->d_tag) + DT_NUM + DT_PROCNUM] = dyn; | |
df4ef2ab | 54 | else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM) |
1228ed5c UD |
55 | info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_PROCNUM |
56 | + DT_VERSIONTAGNUM] = dyn; | |
a2e1b046 | 57 | else |
38334018 | 58 | assert (! "bad dynamic tag"); |
bd355af0 | 59 | ++dyn; |
d66e34cd RM |
60 | } |
61 | ||
62 | if (info[DT_RELA]) | |
266180eb | 63 | assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela))); |
d66e34cd | 64 | if (info[DT_REL]) |
266180eb | 65 | assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel))); |
d66e34cd RM |
66 | if (info[DT_PLTREL]) |
67 | assert (info[DT_PLTREL]->d_un.d_val == DT_REL || | |
68 | info[DT_PLTREL]->d_un.d_val == DT_RELA); | |
69 | } | |
70 | ||
f51d1dfd RM |
71 | #ifdef RESOLVE |
72 | ||
421f82e5 RM |
73 | /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'. |
74 | These functions are almost identical, so we use cpp magic to avoid | |
75 | duplicating their code. It cannot be done in a more general function | |
76 | because we must be able to completely inline. */ | |
77 | ||
ca34d7a7 UD |
78 | /* On some machines, notably Sparc, DT_REL* includes DT_JMPREL in its |
79 | range. Note that according to the ELF spec, this is completely legal! | |
80 | But conditionally define things so that on machines we know this will | |
81 | not happen we do something more optimal. */ | |
82 | ||
83 | #ifdef ELF_MACHINE_PLTREL_OVERLAP | |
085320f5 | 84 | #define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy) \ |
ca34d7a7 | 85 | do { \ |
085320f5 | 86 | struct { ElfW(Addr) start, size; int lazy; } ranges[3]; \ |
052b6a6c UD |
87 | int ranges_index; \ |
88 | \ | |
89 | ranges[0].lazy = ranges[2].lazy = 0; \ | |
90 | ranges[1].lazy = 1; \ | |
91 | ranges[0].size = ranges[1].size = ranges[2].size = 0; \ | |
92 | \ | |
ca34d7a7 UD |
93 | if ((map)->l_info[DT_##RELOC]) \ |
94 | { \ | |
052b6a6c UD |
95 | ranges[0].start = (map)->l_info[DT_##RELOC]->d_un.d_ptr; \ |
96 | ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \ | |
ca34d7a7 | 97 | } \ |
ca34d7a7 | 98 | \ |
085320f5 | 99 | if ((do_lazy) \ |
052b6a6c UD |
100 | && (map)->l_info[DT_PLTREL] \ |
101 | && (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC) \ | |
102 | { \ | |
103 | ranges[1].start = (map)->l_info[DT_JMPREL]->d_un.d_ptr; \ | |
104 | ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \ | |
105 | ranges[2].start = ranges[1].start + ranges[1].size; \ | |
106 | ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start; \ | |
107 | ranges[0].size = ranges[1].start - ranges[0].start; \ | |
ca34d7a7 | 108 | } \ |
052b6a6c UD |
109 | \ |
110 | for (ranges_index = 0; ranges_index < 3; ++ranges_index) \ | |
111 | elf_dynamic_do_##reloc ((map), \ | |
112 | ranges[ranges_index].start, \ | |
113 | ranges[ranges_index].size, \ | |
114 | ranges[ranges_index].lazy); \ | |
ca34d7a7 UD |
115 | } while (0) |
116 | #else | |
085320f5 | 117 | #define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy) \ |
ca34d7a7 | 118 | do { \ |
085320f5 | 119 | struct { ElfW(Addr) start, size; int lazy; } ranges[2]; \ |
052b6a6c UD |
120 | int ranges_index; \ |
121 | ranges[0].lazy = 0; \ | |
052b6a6c UD |
122 | ranges[0].size = ranges[1].size = 0; \ |
123 | ranges[0].start = 0; \ | |
124 | \ | |
ca34d7a7 UD |
125 | if ((map)->l_info[DT_##RELOC]) \ |
126 | { \ | |
052b6a6c UD |
127 | ranges[0].start = (map)->l_info[DT_##RELOC]->d_un.d_ptr; \ |
128 | ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \ | |
ca34d7a7 UD |
129 | } \ |
130 | if ((map)->l_info[DT_PLTREL] && \ | |
131 | (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC) \ | |
132 | { \ | |
052b6a6c UD |
133 | ElfW(Addr) start = (map)->l_info[DT_JMPREL]->d_un.d_ptr; \ |
134 | \ | |
085320f5 | 135 | if ((do_lazy) \ |
052b6a6c UD |
136 | /* This test does not only detect whether the relocation \ |
137 | sections are in the right order, it also checks whether \ | |
138 | there is a DT_REL/DT_RELA section. */ \ | |
139 | || ranges[0].start + ranges[0].size != start) \ | |
140 | { \ | |
141 | ranges[1].start = start; \ | |
142 | ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \ | |
085320f5 | 143 | ranges[1].lazy = (do_lazy); \ |
052b6a6c UD |
144 | } \ |
145 | else \ | |
146 | /* Combine processing the sections. */ \ | |
147 | ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \ | |
ca34d7a7 | 148 | } \ |
052b6a6c UD |
149 | \ |
150 | for (ranges_index = 0; ranges_index < 2; ++ranges_index) \ | |
151 | elf_dynamic_do_##reloc ((map), \ | |
152 | ranges[ranges_index].start, \ | |
153 | ranges[ranges_index].size, \ | |
154 | ranges[ranges_index].lazy); \ | |
ca34d7a7 UD |
155 | } while (0) |
156 | #endif | |
157 | ||
421f82e5 RM |
158 | #if ! ELF_MACHINE_NO_REL |
159 | #include "do-rel.h" | |
ca34d7a7 UD |
160 | #define ELF_DYNAMIC_DO_REL(map, lazy) \ |
161 | _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy) | |
421f82e5 | 162 | #else |
f51d1dfd | 163 | #define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do. */ |
421f82e5 RM |
164 | #endif |
165 | ||
166 | #if ! ELF_MACHINE_NO_RELA | |
167 | #define DO_RELA | |
168 | #include "do-rel.h" | |
ca34d7a7 UD |
169 | #define ELF_DYNAMIC_DO_RELA(map, lazy) \ |
170 | _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy) | |
421f82e5 | 171 | #else |
f51d1dfd | 172 | #define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do. */ |
421f82e5 RM |
173 | #endif |
174 | ||
175 | /* This can't just be an inline function because GCC is too dumb | |
176 | to inline functions containing inlines themselves. */ | |
3996f34b UD |
177 | #define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \ |
178 | do { \ | |
c0fb8a56 UD |
179 | int edr_lazy = elf_machine_runtime_setup ((map), (lazy), \ |
180 | (consider_profile)); \ | |
3996f34b UD |
181 | ELF_DYNAMIC_DO_REL ((map), edr_lazy); \ |
182 | ELF_DYNAMIC_DO_RELA ((map), edr_lazy); \ | |
0501d603 | 183 | } while (0) |
f51d1dfd RM |
184 | |
185 | #endif |