mdreloc.c revision 1.24 1 /* $NetBSD: mdreloc.c,v 1.24 2005/07/17 05:57:21 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Frank van der Linden for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright 1996 John D. Polstra.
40 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by John Polstra.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 #include <sys/types.h>
70 #include <sys/mman.h>
71 #include <err.h>
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <stdarg.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <unistd.h>
79 #include <elf.h>
80
81 #include "debug.h"
82 #include "rtld.h"
83
84 void _rtld_bind_start(void);
85 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
86 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
87 static inline int _rtld_relocate_plt_object(const Obj_Entry *obj,
88 const Elf_Rela *rela, Elf_Addr *tp);
89
90 void
91 _rtld_setup_pltgot(const Obj_Entry *obj)
92 {
93 obj->pltgot[1] = (Elf_Addr) obj;
94 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
95 }
96
97 void
98 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
99 {
100 const Elf_Rela *rela = 0, *relalim;
101 Elf_Addr relasz = 0;
102 Elf_Addr *where;
103
104 for (; dynp->d_tag != DT_NULL; dynp++) {
105 switch (dynp->d_tag) {
106 case DT_RELA:
107 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
108 break;
109 case DT_RELASZ:
110 relasz = dynp->d_un.d_val;
111 break;
112 }
113 }
114 /*
115 * Assume only 64-bit relocations here, which should always
116 * be true for the dynamic linker.
117 */
118 relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
119 for (; rela < relalim; rela++) {
120 where = (Elf_Addr *)(relocbase + rela->r_offset);
121 *where = (Elf_Addr)(relocbase + rela->r_addend);
122 }
123 }
124
125 int
126 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
127 {
128 const Elf_Rela *rela;
129
130 for (rela = obj->rela; rela < obj->relalim; rela++) {
131 Elf64_Addr *where64;
132 Elf32_Addr *where32;
133 const Elf_Sym *def;
134 const Obj_Entry *defobj;
135 Elf64_Addr tmp64;
136 Elf32_Addr tmp32;
137 unsigned long symnum;
138
139 where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
140 where32 = (Elf32_Addr *)where64;
141 symnum = ELF_R_SYM(rela->r_info);
142
143 switch (ELF_R_TYPE(rela->r_info)) {
144 case R_TYPE(NONE):
145 break;
146
147 case R_TYPE(32): /* word32 S + A, truncate */
148 case R_TYPE(32S): /* word32 S + A, signed truncate */
149 case R_TYPE(GOT32): /* word32 G + A (XXX can we see these?) */
150 def = _rtld_find_symdef(symnum, obj, &defobj, false);
151 if (def == NULL)
152 return -1;
153 tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
154 def->st_value + rela->r_addend);
155
156 if (*where32 != tmp32)
157 *where32 = tmp32;
158 rdbg(("32/32S %s in %s --> %p in %s",
159 obj->strtab + obj->symtab[symnum].st_name,
160 obj->path, (void *)(unsigned long)*where32,
161 defobj->path));
162 break;
163 case R_TYPE(64): /* word64 S + A */
164 def = _rtld_find_symdef(symnum, obj, &defobj, false);
165 if (def == NULL)
166 return -1;
167
168 tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
169 rela->r_addend);
170
171 if (*where64 != tmp64)
172 *where64 = tmp64;
173 rdbg(("64 %s in %s --> %p in %s",
174 obj->strtab + obj->symtab[symnum].st_name,
175 obj->path, (void *)*where64, defobj->path));
176 break;
177 case R_TYPE(PC32): /* word32 S + A - P */
178 def = _rtld_find_symdef(symnum, obj, &defobj, false);
179 if (def == NULL)
180 return -1;
181 tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
182 def->st_value + rela->r_addend -
183 (Elf64_Addr)where64);
184 if (*where32 != tmp32)
185 *where32 = tmp32;
186 rdbg(("PC32 %s in %s --> %p in %s",
187 obj->strtab + obj->symtab[symnum].st_name,
188 obj->path, (void *)(unsigned long)*where32,
189 defobj->path));
190 break;
191 case R_TYPE(GLOB_DAT): /* word64 S */
192 def = _rtld_find_symdef(symnum, obj, &defobj, false);
193 if (def == NULL)
194 return -1;
195
196 tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
197
198 if (*where64 != tmp64)
199 *where64 = tmp64;
200 rdbg(("64 %s in %s --> %p in %s",
201 obj->strtab + obj->symtab[symnum].st_name,
202 obj->path, (void *)*where64, defobj->path));
203 break;
204 case R_TYPE(RELATIVE): /* word64 B + A */
205 tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
206 if (*where64 != tmp64)
207 *where64 = tmp64;
208 rdbg(("RELATIVE in %s --> %p", obj->path,
209 (void *)*where64));
210 break;
211
212 case R_TYPE(COPY):
213 rdbg(("COPY"));
214 break;
215
216 default:
217 rdbg(("sym = %lu, type = %lu, offset = %p, "
218 "addend = %p, contents = %p, symbol = %s",
219 symnum, (u_long)ELF_R_TYPE(rela->r_info),
220 (void *)rela->r_offset, (void *)rela->r_addend,
221 (void *)*where64,
222 obj->strtab + obj->symtab[symnum].st_name));
223 _rtld_error("%s: Unsupported relocation type %ld "
224 "in non-PLT relocations\n",
225 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
226 return -1;
227 }
228 }
229 return 0;
230 }
231
232 int
233 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
234 {
235 const Elf_Rela *rela;
236
237 if (!obj->relocbase)
238 return 0;
239
240 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
241 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
242
243 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
244
245 /* Just relocate the GOT slots pointing into the PLT */
246 *where += (Elf_Addr)obj->relocbase;
247 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
248 }
249
250 return 0;
251 }
252
253 static inline int
254 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
255 {
256 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
257 Elf_Addr new_value;
258 const Elf_Sym *def;
259 const Obj_Entry *defobj;
260
261 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
262
263 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
264 if (def == NULL)
265 return -1;
266
267 new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
268 rela->r_addend);
269 rdbg(("bind now/fixup in %s --> old=%p new=%p",
270 defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
271 if (*where != new_value)
272 *where = new_value;
273
274 if (tp)
275 *tp = new_value - rela->r_addend;
276
277 return 0;
278 }
279
280 caddr_t
281 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
282 {
283 const Elf_Rela *rela = obj->pltrela + reloff;
284 Elf_Addr new_value;
285 int err;
286
287 err = _rtld_relocate_plt_object(obj, rela, &new_value);
288 if (err)
289 _rtld_die();
290
291 return (caddr_t)new_value;
292 }
293
294 int
295 _rtld_relocate_plt_objects(const Obj_Entry *obj)
296 {
297 const Elf_Rela *rela;
298
299 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
300 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
301 return -1;
302
303 return 0;
304 }
305