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