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