mdreloc.c revision 1.8 1 1.8 mycroft /* $NetBSD: mdreloc.c,v 1.8 2002/09/05 18:25:48 mycroft Exp $ */
2 1.4 thorpej
3 1.4 thorpej /*
4 1.4 thorpej * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.4 thorpej * All rights reserved.
6 1.4 thorpej *
7 1.4 thorpej * Written by Frank van der Linden for Wasabi Systems, Inc.
8 1.4 thorpej *
9 1.4 thorpej * Redistribution and use in source and binary forms, with or without
10 1.4 thorpej * modification, are permitted provided that the following conditions
11 1.4 thorpej * are met:
12 1.4 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.4 thorpej * notice, this list of conditions and the following disclaimer.
14 1.4 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.4 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.4 thorpej * documentation and/or other materials provided with the distribution.
17 1.4 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.4 thorpej * must display the following acknowledgement:
19 1.4 thorpej * This product includes software developed for the NetBSD Project by
20 1.4 thorpej * Wasabi Systems, Inc.
21 1.4 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.4 thorpej * or promote products derived from this software without specific prior
23 1.4 thorpej * written permission.
24 1.4 thorpej *
25 1.4 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.4 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.4 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.4 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.4 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.4 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.4 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.4 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.4 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.4 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.4 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.5 thorpej */
37 1.5 thorpej
38 1.5 thorpej /*
39 1.5 thorpej * Copyright 1996 John D. Polstra.
40 1.5 thorpej * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
41 1.5 thorpej * All rights reserved.
42 1.5 thorpej *
43 1.5 thorpej * Redistribution and use in source and binary forms, with or without
44 1.5 thorpej * modification, are permitted provided that the following conditions
45 1.5 thorpej * are met:
46 1.5 thorpej * 1. Redistributions of source code must retain the above copyright
47 1.5 thorpej * notice, this list of conditions and the following disclaimer.
48 1.5 thorpej * 2. Redistributions in binary form must reproduce the above copyright
49 1.5 thorpej * notice, this list of conditions and the following disclaimer in the
50 1.5 thorpej * documentation and/or other materials provided with the distribution.
51 1.5 thorpej * 3. All advertising materials mentioning features or use of this software
52 1.5 thorpej * must display the following acknowledgement:
53 1.5 thorpej * This product includes software developed by John Polstra.
54 1.5 thorpej * 4. The name of the author may not be used to endorse or promote products
55 1.5 thorpej * derived from this software without specific prior written permission.
56 1.5 thorpej *
57 1.5 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 1.5 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 1.5 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 1.5 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 1.5 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 1.5 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 1.5 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 1.5 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 1.5 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 1.5 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 1.4 thorpej */
68 1.4 thorpej
69 1.1 fvdl #include <sys/types.h>
70 1.1 fvdl #include <sys/mman.h>
71 1.1 fvdl #include <err.h>
72 1.1 fvdl #include <errno.h>
73 1.1 fvdl #include <fcntl.h>
74 1.1 fvdl #include <stdarg.h>
75 1.1 fvdl #include <stdio.h>
76 1.1 fvdl #include <stdlib.h>
77 1.1 fvdl #include <string.h>
78 1.1 fvdl #include <unistd.h>
79 1.1 fvdl #include <elf.h>
80 1.1 fvdl
81 1.1 fvdl #include "debug.h"
82 1.1 fvdl #include "rtld.h"
83 1.1 fvdl
84 1.1 fvdl extern Elf64_Addr _GLOBAL_OFFSET_TABLE_[];
85 1.1 fvdl
86 1.1 fvdl int
87 1.8 mycroft _rtld_relocate_plt_object(Obj_Entry *obj, const Elf_Rela *rela, caddr_t *addrp,
88 1.8 mycroft bool bind_now, bool dodebug)
89 1.8 mycroft {
90 1.8 mycroft Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
91 1.8 mycroft Elf_Addr new_value;
92 1.8 mycroft
93 1.8 mycroft /* Fully resolve procedure addresses now */
94 1.8 mycroft
95 1.8 mycroft if (bind_now || obj->pltgot == NULL) {
96 1.8 mycroft const Elf_Sym *def;
97 1.8 mycroft const Obj_Entry *defobj;
98 1.8 mycroft
99 1.8 mycroft assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
100 1.8 mycroft
101 1.8 mycroft def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
102 1.8 mycroft if (def == NULL)
103 1.8 mycroft return -1;
104 1.8 mycroft
105 1.8 mycroft new_value = (Elf_Addr)
106 1.8 mycroft (defobj->relocbase + def->st_value + rela->r_addend);
107 1.8 mycroft rdbg(dodebug, ("bind now %d/fixup in %s --> old=%p new=%p",
108 1.8 mycroft (int)bind_now,
109 1.8 mycroft defobj->strtab + def->st_name,
110 1.8 mycroft (void *)*where, (void *)new_value));
111 1.8 mycroft } else {
112 1.8 mycroft if (!obj->mainprog) {
113 1.8 mycroft /* Just relocate the GOT slots pointing into the PLT */
114 1.8 mycroft new_value = *where + (Elf_Addr) obj->relocbase;
115 1.8 mycroft rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
116 1.8 mycroft (void *)(unsigned long)*where));
117 1.8 mycroft } else {
118 1.8 mycroft return 0;
119 1.8 mycroft }
120 1.8 mycroft }
121 1.8 mycroft /*
122 1.8 mycroft * Since this page is probably copy-on-write, let's not write
123 1.8 mycroft * it unless we really really have to.
124 1.8 mycroft */
125 1.8 mycroft if (*where != new_value)
126 1.8 mycroft *where = new_value;
127 1.8 mycroft if (addrp != NULL)
128 1.8 mycroft *addrp = *(caddr_t *)(obj->relocbase + rela->r_offset) -
129 1.8 mycroft rela->r_addend;
130 1.8 mycroft return 0;
131 1.8 mycroft }
132 1.8 mycroft
133 1.8 mycroft void
134 1.8 mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
135 1.8 mycroft {
136 1.8 mycroft obj->pltgot[1] = (Elf_Addr) obj;
137 1.8 mycroft obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
138 1.8 mycroft }
139 1.8 mycroft
140 1.8 mycroft int
141 1.2 fvdl _rtld_relocate_nonplt_object(Obj_Entry *obj, const Elf_Rela *rela, bool dodebug)
142 1.1 fvdl {
143 1.1 fvdl Elf64_Addr *where64;
144 1.1 fvdl Elf32_Addr *where32;
145 1.1 fvdl const Elf_Sym *def;
146 1.1 fvdl const Obj_Entry *defobj;
147 1.1 fvdl Elf64_Addr tmp64;
148 1.1 fvdl Elf32_Addr tmp32;
149 1.1 fvdl
150 1.1 fvdl where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
151 1.1 fvdl where32 = (Elf32_Addr *)where64;
152 1.1 fvdl
153 1.1 fvdl switch (ELF_R_TYPE(rela->r_info)) {
154 1.1 fvdl
155 1.1 fvdl case R_TYPE(NONE):
156 1.1 fvdl break;
157 1.1 fvdl
158 1.1 fvdl case R_TYPE(32): /* word32 S + A, truncate */
159 1.1 fvdl case R_TYPE(32S): /* word32 S + A, signed truncate */
160 1.1 fvdl case R_TYPE(GOT32): /* word32 G + A (XXX can we see these?) */
161 1.7 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
162 1.1 fvdl if (def == NULL)
163 1.1 fvdl return -1;
164 1.1 fvdl tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase + def->st_value +
165 1.1 fvdl rela->r_addend);
166 1.1 fvdl
167 1.1 fvdl if (*where32 != tmp32)
168 1.1 fvdl *where32 = tmp32;
169 1.1 fvdl rdbg(dodebug, ("32/32S %s in %s --> %p in %s",
170 1.1 fvdl defobj->strtab + def->st_name, obj->path,
171 1.1 fvdl (void *)(unsigned long)*where32, defobj->path));
172 1.1 fvdl break;
173 1.1 fvdl case R_TYPE(64): /* word64 S + A */
174 1.7 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
175 1.1 fvdl if (def == NULL)
176 1.1 fvdl return -1;
177 1.1 fvdl
178 1.1 fvdl tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
179 1.1 fvdl rela->r_addend);
180 1.1 fvdl
181 1.1 fvdl if (*where64 != tmp64)
182 1.1 fvdl *where64 = tmp64;
183 1.1 fvdl rdbg(dodebug, ("64 %s in %s --> %p in %s",
184 1.1 fvdl defobj->strtab + def->st_name, obj->path,
185 1.1 fvdl (void *)*where64, defobj->path));
186 1.1 fvdl break;
187 1.1 fvdl case R_TYPE(PC32): /* word32 S + A - P */
188 1.7 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
189 1.1 fvdl if (def == NULL)
190 1.1 fvdl return -1;
191 1.1 fvdl tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase + def->st_value +
192 1.1 fvdl rela->r_addend - (Elf64_Addr)where64);
193 1.1 fvdl if (*where32 != tmp32)
194 1.1 fvdl *where32 = tmp32;
195 1.1 fvdl rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
196 1.1 fvdl defobj->strtab + def->st_name, obj->path,
197 1.1 fvdl (void *)(unsigned long)*where32, defobj->path));
198 1.1 fvdl break;
199 1.1 fvdl case R_TYPE(GLOB_DAT): /* word64 S */
200 1.7 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
201 1.1 fvdl if (def == NULL)
202 1.1 fvdl return -1;
203 1.1 fvdl
204 1.1 fvdl tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
205 1.1 fvdl
206 1.1 fvdl if (*where64 != tmp64)
207 1.1 fvdl *where64 = tmp64;
208 1.1 fvdl rdbg(dodebug, ("64 %s in %s --> %p in %s",
209 1.1 fvdl defobj->strtab + def->st_name, obj->path,
210 1.1 fvdl (void *)*where64, defobj->path));
211 1.1 fvdl break;
212 1.1 fvdl case R_TYPE(RELATIVE): /* word64 B + A */
213 1.1 fvdl tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
214 1.1 fvdl if (*where64 != tmp64)
215 1.1 fvdl *where64 = tmp64;
216 1.1 fvdl rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
217 1.1 fvdl (void *)*where64));
218 1.1 fvdl break;
219 1.1 fvdl
220 1.1 fvdl case R_TYPE(COPY):
221 1.1 fvdl rdbg(dodebug, ("COPY"));
222 1.1 fvdl break;
223 1.1 fvdl
224 1.1 fvdl default:
225 1.7 junyoung def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
226 1.1 fvdl rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
227 1.1 fvdl "addend = %p, contents = %p, symbol = %s",
228 1.1 fvdl (u_long)ELF_R_SYM(rela->r_info),
229 1.1 fvdl (u_long)ELF_R_TYPE(rela->r_info),
230 1.1 fvdl (void *)rela->r_offset, (void *)rela->r_addend,
231 1.1 fvdl (void *)*where64,
232 1.1 fvdl def ? defobj->strtab + def->st_name : "??"));
233 1.1 fvdl _rtld_error("%s: Unsupported relocation type %ld "
234 1.1 fvdl "in non-PLT relocations\n",
235 1.1 fvdl obj->path, (u_long) ELF_R_TYPE(rela->r_info));
236 1.1 fvdl return -1;
237 1.1 fvdl }
238 1.1 fvdl return 0;
239 1.1 fvdl }
240