mdreloc.c revision 1.33.6.1 1 /* $NetBSD: mdreloc.c,v 1.33.6.1 2023/08/04 12:55:48 martin Exp $ */
2
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: mdreloc.c,v 1.33.6.1 2023/08/04 12:55:48 martin Exp $");
6 #endif /* not lint */
7
8 #include <sys/types.h>
9
10 #include "debug.h"
11 #include "rtld.h"
12
13 void _rtld_bind_start(void);
14 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
15 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
16 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
17 const Elf_Rela *, Elf_Addr *);
18
19
20 void
21 _rtld_setup_pltgot(const Obj_Entry *obj)
22 {
23 obj->pltgot[1] = (Elf_Addr) obj;
24 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
25 }
26
27 void
28 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
29 {
30 const Elf_Rela *rela = 0, *relalim;
31 Elf_Addr relasz = 0;
32 Elf_Addr *where;
33
34 for (; dynp->d_tag != DT_NULL; dynp++) {
35 switch (dynp->d_tag) {
36 case DT_RELA:
37 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
38 break;
39 case DT_RELASZ:
40 relasz = dynp->d_un.d_val;
41 break;
42 }
43 }
44 relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
45 for (; rela < relalim; rela++) {
46 where = (Elf_Addr *)(relocbase + rela->r_offset);
47 *where += (Elf_Addr)relocbase;
48 }
49 }
50
51 int
52 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
53 {
54 const Elf_Rela *rela;
55 const Elf_Sym *def = NULL;
56 const Obj_Entry *defobj = NULL;
57 unsigned long last_symnum = ULONG_MAX;
58
59 for (rela = obj->rela; rela < obj->relalim; rela++) {
60 Elf_Addr *where;
61 Elf_Addr tmp;
62 unsigned long symnum;
63
64 where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
65
66 switch (ELF_R_TYPE(rela->r_info)) {
67 case R_TYPE(PC32):
68 case R_TYPE(GOT32):
69 case R_TYPE(32):
70 case R_TYPE(GLOB_DAT):
71 case R_TYPE(TLS_DTPMOD32):
72 case R_TYPE(TLS_DTPREL32):
73 case R_TYPE(TLS_TPREL32):
74 symnum = ELF_R_SYM(rela->r_info);
75 if (last_symnum != symnum) {
76 last_symnum = symnum;
77 def = _rtld_find_symdef(symnum, obj, &defobj,
78 false);
79 if (def == NULL)
80 return -1;
81 }
82 break;
83 default:
84 break;
85 }
86
87 switch (ELF_R_TYPE(rela->r_info)) {
88 case R_TYPE(NONE):
89 break;
90
91 #if 1 /* XXX should not occur */
92 case R_TYPE(PC32):
93 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
94 rela->r_addend) - (Elf_Addr)where;
95 if (*where != tmp)
96 *where = tmp;
97 rdbg(("PC32 %s in %s --> %p in %s",
98 obj->strtab + obj->symtab[symnum].st_name,
99 obj->path, (void *)*where, defobj->path));
100 break;
101
102 case R_TYPE(GOT32):
103 #endif
104 case R_TYPE(32):
105 case R_TYPE(GLOB_DAT):
106 tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
107 rela->r_addend);
108 if (*where != tmp)
109 *where = tmp;
110 rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
111 obj->strtab + obj->symtab[symnum].st_name,
112 obj->path, (void *)*where, defobj->path));
113 break;
114
115 case R_TYPE(RELATIVE):
116 *where += (Elf_Addr)obj->relocbase;
117 rdbg(("RELATIVE in %s --> %p", obj->path,
118 (void *)*where));
119 break;
120
121 case R_TYPE(COPY):
122 /*
123 * These are deferred until all other relocations have
124 * been done. All we do here is make sure that the
125 * COPY relocation is not in a shared library. They
126 * are allowed only in executable files.
127 */
128 if (obj->isdynamic) {
129 _rtld_error(
130 "%s: Unexpected R_COPY relocation in shared library",
131 obj->path);
132 return -1;
133 }
134 rdbg(("COPY (avoid in main)"));
135 break;
136
137 case R_TYPE(TLS_DTPMOD32):
138 *where = (Elf_Addr)defobj->tlsindex;
139 rdbg(("DTPMOD32 %s in %s --> %p in %s",
140 obj->strtab + obj->symtab[symnum].st_name,
141 obj->path, (void *)*where, defobj->path));
142 break;
143
144 case R_TYPE(TLS_DTPREL32):
145 *where = (Elf_Addr)(def->st_value + rela->r_addend
146 - TLS_DTV_OFFSET);
147 rdbg(("DTPREL32 %s in %s --> %p in %s",
148 obj->strtab + obj->symtab[symnum].st_name,
149 obj->path, (void *)*where, defobj->path));
150 break;
151
152 case R_TYPE(TLS_TPREL32):
153 if (!defobj->tls_static &&
154 _rtld_tls_offset_allocate(__UNCONST(defobj)))
155 return -1;
156
157 *where = (Elf_Addr)(def->st_value + rela->r_addend
158 + defobj->tlsoffset - TLS_TP_OFFSET);
159 rdbg(("TPREL32 %s in %s --> %p in %s",
160 obj->strtab + obj->symtab[symnum].st_name,
161 obj->path, (void *)*where, defobj->path));
162 break;
163
164 default:
165 rdbg(("sym = %lu, type = %lu, offset = %p, "
166 "addend = %p, contents = %p, symbol = %s",
167 (u_long)ELF_R_SYM(rela->r_info),
168 (u_long)ELF_R_TYPE(rela->r_info),
169 (void *)rela->r_offset, (void *)rela->r_addend,
170 (void *)*where,
171 obj->strtab + obj->symtab[symnum].st_name));
172 _rtld_error("%s: Unsupported relocation type %ld "
173 "in non-PLT relocations",
174 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
175 return -1;
176 }
177 }
178 return 0;
179 }
180
181 int
182 _rtld_relocate_plt_lazy(Obj_Entry *obj)
183 {
184 const Elf_Rela *rela;
185
186 if (!obj->relocbase)
187 return 0;
188
189 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
190 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
191
192 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
193
194 /* Just relocate the GOT slots pointing into the PLT */
195 *where += (Elf_Addr)obj->relocbase;
196 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
197 }
198
199 return 0;
200 }
201
202 static inline int
203 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
204 Elf_Addr *tp)
205 {
206 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
207 Elf_Addr new_value;
208 const Elf_Sym *def;
209 const Obj_Entry *defobj;
210 unsigned long info = rela->r_info;
211
212 assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
213
214 def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
215 if (__predict_false(def == NULL))
216 return -1;
217 if (__predict_false(def == &_rtld_sym_zero))
218 return 0;
219
220 assert(rela->r_addend == 0);
221 if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
222 if (tp == NULL)
223 return 0;
224 new_value = _rtld_resolve_ifunc(defobj, def);
225 } else {
226 new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
227 rela->r_addend);
228 }
229 rdbg(("bind now/fixup in %s --> old=%p new=%p",
230 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
231 if (*where != new_value)
232 *where = new_value;
233
234 if (tp)
235 *tp = new_value - rela->r_addend;
236
237 return 0;
238 }
239
240 caddr_t
241 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
242 {
243 const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
244 Elf_Addr result;
245 int err;
246
247 result = 0; /* XXX gcc */
248
249 _rtld_shared_enter();
250 err = _rtld_relocate_plt_object(obj, rela, &result);
251 if (err)
252 _rtld_die();
253 _rtld_shared_exit();
254
255 return (caddr_t)result;
256 }
257
258 int
259 _rtld_relocate_plt_objects(const Obj_Entry *obj)
260 {
261 const Elf_Rela *rela;
262
263 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
264 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
265 return -1;
266
267 return 0;
268 }
269