xref: /netbsd-src/libexec/ld.elf_so/arch/x86_64/mdreloc.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: mdreloc.c,v 1.23 2003/07/24 10:12:30 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@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 
88 void
89 _rtld_setup_pltgot(const Obj_Entry *obj)
90 {
91 	obj->pltgot[1] = (Elf_Addr) obj;
92 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
93 }
94 
95 void
96 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
97 {
98 	const Elf_Rela *rela = 0, *relalim;
99 	Elf_Addr relasz = 0;
100 	Elf_Addr *where;
101 
102 	for (; dynp->d_tag != DT_NULL; dynp++) {
103 		switch (dynp->d_tag) {
104 		case DT_RELA:
105 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
106 			break;
107 		case DT_RELASZ:
108 			relasz = dynp->d_un.d_val;
109 			break;
110 		}
111 	}
112 	/*
113 	 * Assume only 64-bit relocations here, which should always
114 	 * be true for the dynamic linker.
115 	 */
116 	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
117 	for (; rela < relalim; rela++) {
118 		where = (Elf_Addr *)(relocbase + rela->r_offset);
119 		*where = (Elf_Addr)(relocbase + rela->r_addend);
120 	}
121 }
122 
123 int
124 _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
125 {
126 	const Elf_Rela *rela;
127 
128 	for (rela = obj->rela; rela < obj->relalim; rela++) {
129 		Elf64_Addr *where64;
130 		Elf32_Addr *where32;
131 		const Elf_Sym *def;
132 		const Obj_Entry *defobj;
133 		Elf64_Addr tmp64;
134 		Elf32_Addr tmp32;
135 		unsigned long	 symnum;
136 
137 		where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
138 		where32 = (Elf32_Addr *)where64;
139 		symnum = ELF_R_SYM(rela->r_info);
140 
141 		switch (ELF_R_TYPE(rela->r_info)) {
142 		case R_TYPE(NONE):
143 			break;
144 
145 		case R_TYPE(32):	/* word32 S + A, truncate */
146 		case R_TYPE(32S):	/* word32 S + A, signed truncate */
147 		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
148 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
149 			if (def == NULL)
150 				return -1;
151 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
152 			    def->st_value + rela->r_addend);
153 
154 			if (*where32 != tmp32)
155 				*where32 = tmp32;
156 			rdbg(("32/32S %s in %s --> %p in %s",
157 			    obj->strtab + obj->symtab[symnum].st_name,
158 			    obj->path, (void *)(unsigned long)*where32,
159 			    defobj->path));
160 			break;
161 		case R_TYPE(64):	/* word64 S + A */
162 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
163 			if (def == NULL)
164 				return -1;
165 
166 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
167 			    rela->r_addend);
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(PC32):	/* word32 S + A - P */
176 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
177 			if (def == NULL)
178 				return -1;
179 			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
180 			    def->st_value + rela->r_addend -
181 			    (Elf64_Addr)where64);
182 			if (*where32 != tmp32)
183 				*where32 = tmp32;
184 			rdbg(("PC32 %s in %s --> %p in %s",
185 			    obj->strtab + obj->symtab[symnum].st_name,
186 			    obj->path, (void *)(unsigned long)*where32,
187 			    defobj->path));
188 			break;
189 		case R_TYPE(GLOB_DAT):	/* word64 S */
190 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
191 			if (def == NULL)
192 				return -1;
193 
194 			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
195 
196 			if (*where64 != tmp64)
197 				*where64 = tmp64;
198 			rdbg(("64 %s in %s --> %p in %s",
199 			    obj->strtab + obj->symtab[symnum].st_name,
200 			    obj->path, (void *)*where64, defobj->path));
201 			break;
202 		case R_TYPE(RELATIVE):  /* word64 B + A */
203 			tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
204 			if (*where64 != tmp64)
205 				*where64 = tmp64;
206 			rdbg(("RELATIVE in %s --> %p", obj->path,
207 			    (void *)*where64));
208        			break;
209 
210 		case R_TYPE(COPY):
211 			rdbg(("COPY"));
212 			break;
213 
214 		default:
215 			rdbg(("sym = %lu, type = %lu, offset = %p, "
216 			    "addend = %p, contents = %p, symbol = %s",
217 			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
218 			    (void *)rela->r_offset, (void *)rela->r_addend,
219 			    (void *)*where64,
220 			    obj->strtab + obj->symtab[symnum].st_name));
221 			_rtld_error("%s: Unsupported relocation type %ld "
222 			    "in non-PLT relocations\n",
223 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
224 			return -1;
225 		}
226 	}
227 	return 0;
228 }
229 
230 int
231 _rtld_relocate_plt_lazy(const Obj_Entry *obj)
232 {
233 	const Elf_Rela *rela;
234 
235 	if (!obj->relocbase)
236 		return 0;
237 
238 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
239 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
240 
241 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
242 
243 		/* Just relocate the GOT slots pointing into the PLT */
244 		*where += (Elf_Addr)obj->relocbase;
245 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
246 	}
247 
248 	return 0;
249 }
250 
251 caddr_t
252 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
253 {
254 	const Elf_Rela *rela = obj->pltrela + reloff;
255 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
256 	Elf_Addr new_value;
257 	const Elf_Sym  *def;
258 	const Obj_Entry *defobj;
259 
260 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
261 
262 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
263 	if (def == NULL)
264 		_rtld_die();
265 
266 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
267 	    rela->r_addend);
268 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
269 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
270 	if (*where != new_value)
271 		*where = new_value;
272 
273 	return (caddr_t)(new_value - rela->r_addend);
274 }
275