1 | /* Do relocations for ELF dynamic linking. |
2 | Copyright (C) 1995-2022 Free Software Foundation, Inc. |
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 Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the 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 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <ldsodefs.h> |
20 | |
21 | /* This file may be included twice, to define both |
22 | `elf_dynamic_do_rel' and `elf_dynamic_do_rela'. */ |
23 | |
24 | #ifdef DO_RELA |
25 | # define elf_dynamic_do_Rel elf_dynamic_do_Rela |
26 | # define Rel Rela |
27 | # define elf_machine_rel elf_machine_rela |
28 | # define elf_machine_rel_relative elf_machine_rela_relative |
29 | #endif |
30 | |
31 | #ifndef DO_ELF_MACHINE_REL_RELATIVE |
32 | # define DO_ELF_MACHINE_REL_RELATIVE(map, l_addr, relative) \ |
33 | elf_machine_rel_relative (l_addr, relative, \ |
34 | (void *) (l_addr + relative->r_offset)) |
35 | #endif |
36 | |
37 | /* Perform the relocations in MAP on the running program image as specified |
38 | by RELTAG, SZTAG. If LAZY is nonzero, this is the first pass on PLT |
39 | relocations; they should be set up to call _dl_runtime_resolve, rather |
40 | than fully resolved now. */ |
41 | |
42 | static inline void __attribute__ ((always_inline)) |
43 | elf_dynamic_do_Rel (struct link_map *map, struct r_scope_elem *scope[], |
44 | ElfW(Addr) reladdr, ElfW(Addr) relsize, |
45 | __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative, |
46 | int lazy, int skip_ifunc) |
47 | { |
48 | const ElfW(Rel) *r = (const void *) reladdr; |
49 | const ElfW(Rel) *end = (const void *) (reladdr + relsize); |
50 | ElfW(Addr) l_addr = map->l_addr; |
51 | # if defined ELF_MACHINE_IRELATIVE && !defined RTLD_BOOTSTRAP |
52 | const ElfW(Rel) *r2 = NULL; |
53 | const ElfW(Rel) *end2 = NULL; |
54 | # endif |
55 | |
56 | #if (!defined DO_RELA || !defined ELF_MACHINE_PLT_REL) && !defined RTLD_BOOTSTRAP |
57 | /* We never bind lazily during ld.so bootstrap. Unfortunately gcc is |
58 | not clever enough to see through all the function calls to realize |
59 | that. */ |
60 | if (lazy) |
61 | { |
62 | /* Doing lazy PLT relocations; they need very little info. */ |
63 | for (; r < end; ++r) |
64 | # ifdef ELF_MACHINE_IRELATIVE |
65 | if (ELFW(R_TYPE) (r->r_info) == ELF_MACHINE_IRELATIVE) |
66 | { |
67 | if (r2 == NULL) |
68 | r2 = r; |
69 | end2 = r; |
70 | } |
71 | else |
72 | # endif |
73 | elf_machine_lazy_rel (map, scope, l_addr, r, skip_ifunc); |
74 | |
75 | # ifdef ELF_MACHINE_IRELATIVE |
76 | if (r2 != NULL) |
77 | for (; r2 <= end2; ++r2) |
78 | if (ELFW(R_TYPE) (r2->r_info) == ELF_MACHINE_IRELATIVE) |
79 | elf_machine_lazy_rel (map, scope, l_addr, r2, skip_ifunc); |
80 | # endif |
81 | } |
82 | else |
83 | #endif |
84 | { |
85 | const ElfW(Sym) *const symtab = |
86 | (const void *) D_PTR (map, l_info[DT_SYMTAB]); |
87 | const ElfW(Rel) *relative = r; |
88 | r += nrelative; |
89 | |
90 | #ifndef RTLD_BOOTSTRAP |
91 | /* This is defined in rtld.c, but nowhere in the static libc.a; make |
92 | the reference weak so static programs can still link. This |
93 | declaration cannot be done when compiling rtld.c (i.e. #ifdef |
94 | RTLD_BOOTSTRAP) because rtld.c contains the common defn for |
95 | _dl_rtld_map, which is incompatible with a weak decl in the same |
96 | file. */ |
97 | # ifndef SHARED |
98 | weak_extern (GL(dl_rtld_map)); |
99 | # endif |
100 | if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */ |
101 | # if !defined DO_RELA || defined ELF_MACHINE_REL_RELATIVE |
102 | /* Rela platforms get the offset from r_addend and this must |
103 | be copied in the relocation address. Therefore we can skip |
104 | the relative relocations only if this is for rel |
105 | relocations or rela relocations if they are computed as |
106 | memory_loc += l_addr... */ |
107 | if (l_addr != 0) |
108 | # else |
109 | /* ...or we know the object has been prelinked. */ |
110 | if (l_addr != 0 || ! map->l_info[VALIDX(DT_GNU_PRELINKED)]) |
111 | # endif |
112 | #endif |
113 | for (; relative < r; ++relative) |
114 | DO_ELF_MACHINE_REL_RELATIVE (map, l_addr, relative); |
115 | |
116 | #ifdef RTLD_BOOTSTRAP |
117 | /* The dynamic linker always uses versioning. */ |
118 | assert (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL); |
119 | #else |
120 | if (map->l_info[VERSYMIDX (DT_VERSYM)]) |
121 | #endif |
122 | { |
123 | const ElfW(Half) *const version = |
124 | (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]); |
125 | |
126 | for (; r < end; ++r) |
127 | { |
128 | ElfW(Half) ndx = version[ELFW(R_SYM) (r->r_info)] & 0x7fff; |
129 | const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (r->r_info)]; |
130 | void *const r_addr_arg = (void *) (l_addr + r->r_offset); |
131 | const struct r_found_version *rversion = &map->l_versions[ndx]; |
132 | #if defined ELF_MACHINE_IRELATIVE && !defined RTLD_BOOTSTRAP |
133 | if (ELFW(R_TYPE) (r->r_info) == ELF_MACHINE_IRELATIVE) |
134 | { |
135 | if (r2 == NULL) |
136 | r2 = r; |
137 | end2 = r; |
138 | continue; |
139 | } |
140 | #endif |
141 | |
142 | elf_machine_rel (map, scope, r, sym, rversion, r_addr_arg, |
143 | skip_ifunc); |
144 | #if defined SHARED && !defined RTLD_BOOTSTRAP |
145 | if (ELFW(R_TYPE) (r->r_info) == ELF_MACHINE_JMP_SLOT |
146 | && GLRO(dl_naudit) > 0) |
147 | { |
148 | struct link_map *sym_map |
149 | = RESOLVE_MAP (map, scope, &sym, rversion, |
150 | ELF_MACHINE_JMP_SLOT); |
151 | if (sym != NULL) |
152 | _dl_audit_symbind (map, NULL, sym, r_addr_arg, sym_map); |
153 | } |
154 | #endif |
155 | } |
156 | |
157 | #if defined ELF_MACHINE_IRELATIVE && !defined RTLD_BOOTSTRAP |
158 | if (r2 != NULL) |
159 | for (; r2 <= end2; ++r2) |
160 | if (ELFW(R_TYPE) (r2->r_info) == ELF_MACHINE_IRELATIVE) |
161 | { |
162 | ElfW(Half) ndx |
163 | = version[ELFW(R_SYM) (r2->r_info)] & 0x7fff; |
164 | elf_machine_rel (map, scope, r2, |
165 | &symtab[ELFW(R_SYM) (r2->r_info)], |
166 | &map->l_versions[ndx], |
167 | (void *) (l_addr + r2->r_offset), |
168 | skip_ifunc); |
169 | } |
170 | #endif |
171 | } |
172 | #ifndef RTLD_BOOTSTRAP |
173 | else |
174 | { |
175 | for (; r < end; ++r) |
176 | { |
177 | const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (r->r_info)]; |
178 | void *const r_addr_arg = (void *) (l_addr + r->r_offset); |
179 | # ifdef ELF_MACHINE_IRELATIVE |
180 | if (ELFW(R_TYPE) (r->r_info) == ELF_MACHINE_IRELATIVE) |
181 | { |
182 | if (r2 == NULL) |
183 | r2 = r; |
184 | end2 = r; |
185 | continue; |
186 | } |
187 | # endif |
188 | elf_machine_rel (map, scope, r, sym, NULL, r_addr_arg, |
189 | skip_ifunc); |
190 | # if defined SHARED && !defined RTLD_BOOTSTRAP |
191 | if (ELFW(R_TYPE) (r->r_info) == ELF_MACHINE_JMP_SLOT |
192 | && GLRO(dl_naudit) > 0) |
193 | { |
194 | struct link_map *sym_map |
195 | = RESOLVE_MAP (map, scope, &sym, |
196 | (struct r_found_version *) NULL, |
197 | ELF_MACHINE_JMP_SLOT); |
198 | if (sym != NULL) |
199 | _dl_audit_symbind (map, NULL , sym,r_addr_arg, sym_map); |
200 | } |
201 | # endif |
202 | } |
203 | |
204 | # ifdef ELF_MACHINE_IRELATIVE |
205 | if (r2 != NULL) |
206 | for (; r2 <= end2; ++r2) |
207 | if (ELFW(R_TYPE) (r2->r_info) == ELF_MACHINE_IRELATIVE) |
208 | elf_machine_rel (map, scope, r2, &symtab[ELFW(R_SYM) (r2->r_info)], |
209 | NULL, (void *) (l_addr + r2->r_offset), |
210 | skip_ifunc); |
211 | # endif |
212 | } |
213 | #endif |
214 | } |
215 | } |
216 | |
217 | #undef elf_dynamic_do_Rel |
218 | #undef Rel |
219 | #undef elf_machine_rel |
220 | #undef elf_machine_rel_relative |
221 | #undef DO_ELF_MACHINE_REL_RELATIVE |
222 | #undef DO_RELA |
223 | |