mdreloc.c revision 1.9 1 /* $NetBSD: mdreloc.c,v 1.9 2002/09/05 20:08:20 mycroft 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 extern Elf64_Addr _GLOBAL_OFFSET_TABLE_[];
85
86 int
87 _rtld_relocate_plt_object(Obj_Entry *obj, const Elf_Rela *rela, caddr_t *addrp,
88 bool bind_now, bool dodebug)
89 {
90 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
91 Elf_Addr new_value;
92
93 /* Fully resolve procedure addresses now */
94
95 if (bind_now || obj->pltgot == NULL) {
96 const Elf_Sym *def;
97 const Obj_Entry *defobj;
98
99 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
100
101 def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
102 if (def == NULL)
103 return -1;
104
105 new_value = (Elf_Addr)
106 (defobj->relocbase + def->st_value + rela->r_addend);
107 rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
108 (int)bind_now,
109 defobj->strtab + def->st_name,
110 (void *)*where, (void *)new_value));
111 } else {
112 if (!obj->mainprog) {
113 /* Just relocate the GOT slots pointing into the PLT */
114 new_value = *where + (Elf_Addr) obj->relocbase;
115 rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
116 (void *)(unsigned long)*where));
117 } else {
118 return 0;
119 }
120 }
121 /*
122 * Since this page is probably copy-on-write, let's not write
123 * it unless we really really have to.
124 */
125 if (*where != new_value)
126 *where = new_value;
127 if (addrp != NULL)
128 *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset) -
129 rela->r_addend;
130 return 0;
131 }
132
133 void
134 _rtld_setup_pltgot(const Obj_Entry *obj)
135 {
136 obj->pltgot[1] = (Elf_Addr) obj;
137 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
138 }
139
140 int
141 _rtld_relocate_nonplt_objects(Obj_Entry *obj, bool dodebug)
142 {
143 const Elf_Rela *rela;
144
145 for (rela = obj->rela; rela < obj->relalim; rela++) {
146 Elf64_Addr *where64;
147 Elf32_Addr *where32;
148 const Elf_Sym *def;
149 const Obj_Entry *defobj;
150 Elf64_Addr tmp64;
151 Elf32_Addr tmp32;
152
153 where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
154 where32 = (Elf32_Addr *)where64;
155
156 switch (ELF_R_TYPE(rela->r_info)) {
157 case R_TYPE(NONE):
158 break;
159
160 case R_TYPE(32): /* word32 S + A, truncate */
161 case R_TYPE(32S): /* word32 S + A, signed truncate */
162 case R_TYPE(GOT32): /* word32 G + A (XXX can we see these?) */
163 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
164 false);
165 if (def == NULL)
166 return -1;
167 tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
168 def->st_value + rela->r_addend);
169
170 if (*where32 != tmp32)
171 *where32 = tmp32;
172 rdbg(dodebug, ("32/32S %s in %s --> %p in %s",
173 defobj->strtab + def->st_name, obj->path,
174 (void *)(unsigned long)*where32, defobj->path));
175 break;
176 case R_TYPE(64): /* word64 S + A */
177 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
178 false);
179 if (def == NULL)
180 return -1;
181
182 tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
183 rela->r_addend);
184
185 if (*where64 != tmp64)
186 *where64 = tmp64;
187 rdbg(dodebug, ("64 %s in %s --> %p in %s",
188 defobj->strtab + def->st_name, obj->path,
189 (void *)*where64, defobj->path));
190 break;
191 case R_TYPE(PC32): /* word32 S + A - P */
192 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
193 false);
194 if (def == NULL)
195 return -1;
196 tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
197 def->st_value + rela->r_addend -
198 (Elf64_Addr)where64);
199 if (*where32 != tmp32)
200 *where32 = tmp32;
201 rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
202 defobj->strtab + def->st_name, obj->path,
203 (void *)(unsigned long)*where32, defobj->path));
204 break;
205 case R_TYPE(GLOB_DAT): /* word64 S */
206 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
207 false);
208 if (def == NULL)
209 return -1;
210
211 tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
212
213 if (*where64 != tmp64)
214 *where64 = tmp64;
215 rdbg(dodebug, ("64 %s in %s --> %p in %s",
216 defobj->strtab + def->st_name, obj->path,
217 (void *)*where64, defobj->path));
218 break;
219 case R_TYPE(RELATIVE): /* word64 B + A */
220 tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
221 if (*where64 != tmp64)
222 *where64 = tmp64;
223 rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
224 (void *)*where64));
225 break;
226
227 case R_TYPE(COPY):
228 rdbg(dodebug, ("COPY"));
229 break;
230
231 default:
232 def = _rtld_find_symdef(rela->r_info, obj, &defobj,
233 true);
234 rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
235 "addend = %p, contents = %p, symbol = %s",
236 (u_long)ELF_R_SYM(rela->r_info),
237 (u_long)ELF_R_TYPE(rela->r_info),
238 (void *)rela->r_offset, (void *)rela->r_addend,
239 (void *)*where64,
240 def ? defobj->strtab + def->st_name : "??"));
241 _rtld_error("%s: Unsupported relocation type %ld "
242 "in non-PLT relocations\n",
243 obj->path, (u_long) ELF_R_TYPE(rela->r_info));
244 return -1;
245 }
246 }
247 return 0;
248 }
249