1/*	$NetBSD: mdreloc.c,v 1.39 2011/03/12 22:54:36 joerg 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/cdefs.h>
70#ifndef lint
71__RCSID("$NetBSD: mdreloc.c,v 1.39 2011/03/12 22:54:36 joerg Exp $");
72#endif /* not lint */
73
74#include <sys/types.h>
75#include <sys/mman.h>
76#include <errno.h>
77#include <fcntl.h>
78#include <stdarg.h>
79#include <stdio.h>
80#include <stdlib.h>
81#include <string.h>
82#include <unistd.h>
83#include <elf.h>
84
85#include "debug.h"
86#include "rtld.h"
87
88void _rtld_bind_start(void);
89void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
90caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
91static inline int _rtld_relocate_plt_object(const Obj_Entry *,
92    const Elf_Rela *, Elf_Addr *);
93
94void
95_rtld_setup_pltgot(const Obj_Entry *obj)
96{
97	obj->pltgot[1] = (Elf_Addr) obj;
98	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
99}
100
101void
102_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
103{
104	const Elf_Rela *rela = 0, *relalim;
105	Elf_Addr relasz = 0;
106	Elf_Addr *where;
107
108	for (; dynp->d_tag != DT_NULL; dynp++) {
109		switch (dynp->d_tag) {
110		case DT_RELA:
111			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
112			break;
113		case DT_RELASZ:
114			relasz = dynp->d_un.d_val;
115			break;
116		}
117	}
118	/*
119	 * Assume only 64-bit relocations here, which should always
120	 * be true for the dynamic linker.
121	 */
122	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
123	for (; rela < relalim; rela++) {
124		where = (Elf_Addr *)(relocbase + rela->r_offset);
125		*where = (Elf_Addr)(relocbase + rela->r_addend);
126	}
127}
128
129int
130_rtld_relocate_nonplt_objects(Obj_Entry *obj)
131{
132	const Elf_Rela *rela;
133	const Elf_Sym *def = NULL;
134	const Obj_Entry *defobj =NULL;
135
136	for (rela = obj->rela; rela < obj->relalim; rela++) {
137		Elf64_Addr *where64;
138		Elf32_Addr *where32;
139		Elf64_Addr tmp64;
140		Elf32_Addr tmp32;
141		unsigned long	 symnum;
142
143		where64 = (Elf64_Addr *)(obj->relocbase + rela->r_offset);
144		where32 = (Elf32_Addr *)where64;
145		symnum = ELF_R_SYM(rela->r_info);
146
147		switch (ELF_R_TYPE(rela->r_info)) {
148		case R_TYPE(NONE):
149			break;
150
151		case R_TYPE(32):	/* word32 S + A, truncate */
152		case R_TYPE(32S):	/* word32 S + A, signed truncate */
153		case R_TYPE(GOT32):	/* word32 G + A (XXX can we see these?) */
154			def = _rtld_find_symdef(symnum, obj, &defobj, false);
155			if (def == NULL)
156				return -1;
157			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
158			    def->st_value + rela->r_addend);
159
160			if (*where32 != tmp32)
161				*where32 = tmp32;
162			rdbg(("32/32S %s in %s --> %p in %s",
163			    obj->strtab + obj->symtab[symnum].st_name,
164			    obj->path, (void *)(uintptr_t)*where32,
165			    defobj->path));
166			break;
167		case R_TYPE(64):	/* word64 S + A */
168			def = _rtld_find_symdef(symnum, obj, &defobj, false);
169			if (def == NULL)
170				return -1;
171			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value +
172			    rela->r_addend);
173
174			if (*where64 != tmp64)
175				*where64 = tmp64;
176			rdbg(("64 %s in %s --> %p in %s",
177			    obj->strtab + obj->symtab[symnum].st_name,
178			    obj->path, (void *)*where64, defobj->path));
179			break;
180		case R_TYPE(PC32):	/* word32 S + A - P */
181			def = _rtld_find_symdef(symnum, obj, &defobj, false);
182			if (def == NULL)
183				return -1;
184			tmp32 = (Elf32_Addr)(u_long)(defobj->relocbase +
185			    def->st_value + rela->r_addend -
186			    (Elf64_Addr)where64);
187			if (*where32 != tmp32)
188				*where32 = tmp32;
189			rdbg(("PC32 %s in %s --> %p in %s",
190			    obj->strtab + obj->symtab[symnum].st_name,
191			    obj->path, (void *)(unsigned long)*where32,
192			    defobj->path));
193			break;
194		case R_TYPE(GLOB_DAT):	/* word64 S */
195			def = _rtld_find_symdef(symnum, obj, &defobj, false);
196			if (def == NULL)
197				return -1;
198			tmp64 = (Elf64_Addr)(defobj->relocbase + def->st_value);
199
200			if (*where64 != tmp64)
201				*where64 = tmp64;
202			rdbg(("64 %s in %s --> %p in %s",
203			    obj->strtab + obj->symtab[symnum].st_name,
204			    obj->path, (void *)*where64, defobj->path));
205			break;
206		case R_TYPE(RELATIVE):  /* word64 B + A */
207			tmp64 = (Elf64_Addr)(obj->relocbase + rela->r_addend);
208			if (*where64 != tmp64)
209				*where64 = tmp64;
210			rdbg(("RELATIVE in %s --> %p", obj->path,
211			    (void *)*where64));
212       			break;
213
214		case R_TYPE(TPOFF64):
215			def = _rtld_find_symdef(symnum, obj, &defobj, false);
216			if (def == NULL)
217				return -1;
218
219			if (!defobj->tls_done &&
220			    _rtld_tls_offset_allocate(obj))
221				return -1;
222
223			*where64 = (Elf64_Addr)(def->st_value -
224			    defobj->tlsoffset + rela->r_addend);
225
226			rdbg(("TPOFF64 %s in %s --> %p",
227			    obj->strtab + obj->symtab[symnum].st_name,
228			    obj->path, (void *)*where64));
229
230			break;
231
232		case R_TYPE(DTPMOD64):
233			def = _rtld_find_symdef(symnum, obj, &defobj, false);
234			if (def == NULL)
235				return -1;
236
237			*where64 = (Elf64_Addr)defobj->tlsindex;
238
239			rdbg(("DTPMOD64 %s in %s --> %p",
240			    obj->strtab + obj->symtab[symnum].st_name,
241			    obj->path, (void *)*where64));
242
243			break;
244
245		case R_TYPE(DTPOFF64):
246			def = _rtld_find_symdef(symnum, obj, &defobj, false);
247			if (def == NULL)
248				return -1;
249
250			*where64 = (Elf64_Addr)(def->st_value + rela->r_addend);
251
252			rdbg(("DTPOFF64 %s in %s --> %p",
253			    obj->strtab + obj->symtab[symnum].st_name,
254			    obj->path, (void *)*where64));
255
256			break;
257
258		case R_TYPE(COPY):
259			rdbg(("COPY"));
260			break;
261
262		default:
263			rdbg(("sym = %lu, type = %lu, offset = %p, "
264			    "addend = %p, contents = %p, symbol = %s",
265			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
266			    (void *)rela->r_offset, (void *)rela->r_addend,
267			    (void *)*where64,
268			    obj->strtab + obj->symtab[symnum].st_name));
269			_rtld_error("%s: Unsupported relocation type %ld "
270			    "in non-PLT relocations",
271			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
272			return -1;
273		}
274	}
275	return 0;
276}
277
278int
279_rtld_relocate_plt_lazy(const Obj_Entry *obj)
280{
281	const Elf_Rela *rela;
282
283	if (!obj->relocbase)
284		return 0;
285
286	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
287		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
288
289		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
290
291		/* Just relocate the GOT slots pointing into the PLT */
292		*where += (Elf_Addr)obj->relocbase;
293		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
294	}
295
296	return 0;
297}
298
299static inline int
300_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
301{
302	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
303	Elf_Addr new_value;
304	const Elf_Sym  *def;
305	const Obj_Entry *defobj;
306	unsigned long info = rela->r_info;
307
308	assert(ELF_R_TYPE(info) == R_TYPE(JUMP_SLOT));
309
310	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
311	if (__predict_false(def == NULL))
312		return -1;
313	if (__predict_false(def == &_rtld_sym_zero))
314		return 0;
315
316	new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
317	    rela->r_addend);
318	rdbg(("bind now/fixup in %s --> old=%p new=%p",
319	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
320	if (*where != new_value)
321		*where = new_value;
322
323	if (tp)
324		*tp = new_value - rela->r_addend;
325
326	return 0;
327}
328
329caddr_t
330_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
331{
332	const Elf_Rela *rela = obj->pltrela + reloff;
333	Elf_Addr new_value;
334	int error;
335
336	new_value = 0; /* XXX GCC4 */
337
338	_rtld_shared_enter();
339	error = _rtld_relocate_plt_object(obj, rela, &new_value);
340	if (error)
341		_rtld_die();
342	_rtld_shared_exit();
343
344	return (caddr_t)new_value;
345}
346
347int
348_rtld_relocate_plt_objects(const Obj_Entry *obj)
349{
350	const Elf_Rela *rela;
351
352	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
353		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
354			return -1;
355
356	return 0;
357}
358