alpha_reloc.c revision 1.27
1/*	$NetBSD: alpha_reloc.c,v 1.27 2005/08/15 10:52:42 skrll Exp $	*/
2
3/*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe 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, 1997, 1998, 1999 John D. Polstra.
40 * All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 *    notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 *    notice, this list of conditions and the following disclaimer in the
49 *    documentation and/or other materials provided with the distribution.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62
63#include <sys/types.h>
64#include <sys/stat.h>
65#include <string.h>
66
67#include "rtld.h"
68#include "debug.h"
69
70#ifdef RTLD_DEBUG_ALPHA
71#define	adbg(x)		xprintf x
72#else
73#define	adbg(x)		/* nothing */
74#endif
75
76void _rtld_bind_start(void);
77void _rtld_bind_start_old(void);
78void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
79caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
80static inline int _rtld_relocate_plt_object(const Obj_Entry *,
81    const Elf_Rela *, Elf_Addr *);
82
83void
84_rtld_setup_pltgot(const Obj_Entry *obj)
85{
86	uint32_t word0;
87
88	/*
89	 * The PLTGOT on the Alpha looks like this:
90	 *
91	 *	PLT HEADER
92	 *	.
93	 *	. 32 bytes
94	 *	.
95	 *	PLT ENTRY #0
96	 *	.
97	 *	. 12 bytes
98	 *	.
99	 *	PLT ENTRY #1
100	 *	.
101	 *	. 12 bytes
102	 *	.
103	 *	etc.
104	 *
105	 * The old-format entries look like (displacements filled in
106	 * by the linker):
107	 *
108	 *	ldah	$28, 0($31)		# 0x279f0000
109	 *	lda	$28, 0($28)		# 0x239c0000
110	 *	br	$31, plt0		# 0xc3e00000
111	 *
112	 * The new-format entries look like:
113	 *
114	 *	br	$28, plt0		# 0xc3800000
115	 *					# 0x00000000
116	 *					# 0x00000000
117	 *
118	 * What we do is fetch the first PLT entry and check to
119	 * see the first word of it matches the first word of the
120	 * old format.  If so, we use a binding routine that can
121	 * handle the old format, otherwise we use a binding routine
122	 * that handles the new format.
123	 *
124	 * Note that this is done on a per-object basis, we can mix
125	 * and match shared objects build with both the old and new
126	 * linker.
127	 */
128	word0 = *(uint32_t *)(((char *) obj->pltgot) + 32);
129	if ((word0 & 0xffff0000) == 0x279f0000) {
130		/* Old PLT entry format. */
131		adbg(("ALPHA: object %p has old PLT format\n", obj));
132		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start_old;
133		obj->pltgot[3] = (Elf_Addr) obj;
134	} else {
135		/* New PLT entry format. */
136		adbg(("ALPHA: object %p has new PLT format\n", obj));
137		obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
138		obj->pltgot[3] = (Elf_Addr) obj;
139	}
140
141	__asm __volatile("imb");
142}
143
144/*
145 * It is possible for the compiler to emit relocations for unaligned data.
146 * We handle this situation with these inlines.
147 */
148#define	RELOC_ALIGNED_P(x) \
149	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
150
151static __inline Elf_Addr
152load_ptr(void *where)
153{
154	Elf_Addr res;
155
156	memcpy(&res, where, sizeof(res));
157
158	return (res);
159}
160
161static __inline void
162store_ptr(void *where, Elf_Addr val)
163{
164
165	memcpy(where, &val, sizeof(val));
166}
167
168void
169_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
170{
171	const Elf_Rela *rela = 0, *relalim;
172	Elf_Addr relasz = 0;
173	Elf_Addr *where;
174
175	for (; dynp->d_tag != DT_NULL; dynp++) {
176		switch (dynp->d_tag) {
177		case DT_RELA:
178			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
179			break;
180		case DT_RELASZ:
181			relasz = dynp->d_un.d_val;
182			break;
183		}
184	}
185	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
186	for (; rela < relalim; rela++) {
187		where = (Elf_Addr *)(relocbase + rela->r_offset);
188		/* XXX For some reason I see a few GLOB_DAT relocs here. */
189		*where += (Elf_Addr)relocbase;
190	}
191}
192
193int
194_rtld_relocate_nonplt_objects(const Obj_Entry *obj)
195{
196	const Elf_Rela *rela;
197#define COMBRELOC
198#ifdef COMBRELOC
199	unsigned long lastsym = -1;
200#endif
201	Elf_Addr target = -1;
202
203	for (rela = obj->rela; rela < obj->relalim; rela++) {
204		Elf_Addr        *where;
205		const Elf_Sym   *def;
206		const Obj_Entry *defobj;
207		Elf_Addr         tmp;
208		unsigned long	 symnum;
209
210		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
211		symnum = ELF_R_SYM(rela->r_info);
212
213		switch (ELF_R_TYPE(rela->r_info)) {
214		case R_TYPE(NONE):
215			break;
216
217		case R_TYPE(REFQUAD):
218		case R_TYPE(GLOB_DAT):
219#ifdef COMBRELOC
220			if (symnum != lastsym) {
221#endif
222				def = _rtld_find_symdef(symnum, obj, &defobj,
223				    false);
224				if (def == NULL)
225					return -1;
226				target = (Elf_Addr)(defobj->relocbase +
227				    def->st_value);
228#ifdef COMBRELOC
229				lastsym = symnum;
230			}
231#endif
232
233			tmp = target + rela->r_addend;
234			if (__predict_true(RELOC_ALIGNED_P(where))) {
235				if (*where != tmp)
236					*where = tmp;
237			} else {
238				if (load_ptr(where) != tmp)
239					store_ptr(where, tmp);
240			}
241			rdbg(("REFQUAD/GLOB_DAT %s in %s --> %p in %s",
242			    obj->strtab + obj->symtab[symnum].st_name,
243			    obj->path, (void *)tmp, defobj->path));
244			break;
245
246		case R_TYPE(RELATIVE):
247			if (__predict_true(RELOC_ALIGNED_P(where)))
248				*where += (Elf_Addr)obj->relocbase;
249			else
250				store_ptr(where,
251				    load_ptr(where) + (Elf_Addr)obj->relocbase);
252			rdbg(("RELATIVE in %s --> %p", obj->path,
253			    (void *)*where));
254			break;
255
256		case R_TYPE(COPY):
257			/*
258			 * These are deferred until all other relocations have
259			 * been done.  All we do here is make sure that the
260			 * COPY relocation is not in a shared library.  They
261			 * are allowed only in executable files.
262			 */
263			if (obj->isdynamic) {
264				_rtld_error(
265			"%s: Unexpected R_COPY relocation in shared library",
266				    obj->path);
267				return -1;
268			}
269			rdbg(("COPY (avoid in main)"));
270			break;
271
272		default:
273			rdbg(("sym = %lu, type = %lu, offset = %p, "
274			    "addend = %p, contents = %p, symbol = %s",
275			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
276			    (void *)rela->r_offset, (void *)rela->r_addend,
277			    (void *)load_ptr(where),
278			    obj->strtab + obj->symtab[symnum].st_name));
279			_rtld_error("%s: Unsupported relocation type %ld "
280			    "in non-PLT relocations\n",
281			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
282			return -1;
283		}
284	}
285	return 0;
286}
287
288int
289_rtld_relocate_plt_lazy(const Obj_Entry *obj)
290{
291	const Elf_Rela *rela;
292
293	if (!obj->relocbase)
294		return 0;
295
296	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
297		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
298
299		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
300
301		/* Just relocate the GOT slots pointing into the PLT */
302		*where += (Elf_Addr)obj->relocbase;
303		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
304	}
305
306	return 0;
307}
308
309static inline int
310_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
311{
312	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
313	Elf_Addr new_value;
314	const Elf_Sym  *def;
315	const Obj_Entry *defobj;
316	Elf_Addr stubaddr;
317
318	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
319
320	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
321	if (def == NULL)
322		return -1;
323
324	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
325	rdbg(("bind now/fixup in %s --> old=%p new=%p",
326	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
327
328	if ((stubaddr = *where) != new_value) {
329		int64_t delta, idisp;
330		uint32_t insn[3], *stubptr;
331		int insncnt;
332		Elf_Addr pc;
333
334		/* Point this GOT entry at the target. */
335		*where = new_value;
336
337		/*
338		 * Alpha shared objects may have multiple GOTs, each
339		 * of which may point to this entry in the PLT.  But,
340		 * we only have a reference to the first GOT entry which
341		 * points to this PLT entry.  In order to avoid having to
342		 * re-bind this call every time a non-first GOT entry is
343		 * used, we will attempt to patch up the PLT entry to
344		 * reference the target, rather than the binder.
345		 *
346		 * When the PLT stub gets control, PV contains the address
347		 * of the PLT entry.  Each PLT entry has room for 3 insns.
348		 * If the displacement of the target from PV fits in a signed
349		 * 32-bit integer, we can simply add it to PV.  Otherwise,
350		 * we must load the GOT entry itself into PV.
351		 *
352		 * Note if the shared object uses the old PLT format, then
353		 * we cannot patch up the PLT safely, and so we skip it
354		 * in that case[*].
355		 *
356		 * [*] Actually, if we're not doing lazy-binding, then
357		 * we *can* (and do) patch up this PLT entry; the PLTGOT
358		 * thunk won't yet point to any binder entry point, and
359		 * so this test will fail as it would for the new PLT
360		 * entry format.
361		 */
362		if (obj->pltgot[2] == (Elf_Addr) &_rtld_bind_start_old) {
363			rdbg(("  old PLT format"));
364			goto out;
365		}
366
367		delta = new_value - stubaddr;
368		rdbg(("  stubaddr=%p, where-stubaddr=%ld, delta=%ld",
369		    (void *)stubaddr, (long)where - (long)stubaddr,
370		    (long)delta));
371		insncnt = 0;
372		if ((int32_t)delta == delta) {
373			/*
374			 * We can adjust PV with an LDA, LDAH sequence.
375			 *
376			 * First, build an LDA insn to adjust the low 16
377			 * bits.
378			 */
379			insn[insncnt++] = 0x08 << 26 | 27 << 21 | 27 << 16 |
380			    (delta & 0xffff);
381			rdbg(("  LDA  $27,%d($27)", (int16_t)delta));
382			/*
383			 * Adjust the delta to account for the effects of
384			 * the LDA, including sign-extension.
385			 */
386			delta -= (int16_t)delta;
387			if (delta != 0) {
388				/*
389				 * Build an LDAH instruction to adjust the
390				 * high 16 bits.
391				 */
392				insn[insncnt++] = 0x09 << 26 | 27 << 21 |
393				    27 << 16 | ((delta >> 16) & 0xffff);
394				rdbg(("  LDAH $27,%d($27)",
395				    (int16_t)(delta >> 16)));
396			}
397		} else {
398			int64_t dhigh;
399
400			/* We must load the GOT entry. */
401			delta = (Elf_Addr)where - stubaddr;
402
403			/*
404			 * If the GOT entry is too far away from the PLT
405			 * entry, then we can't patch up the PLT entry.
406			 * This PLT entry will have to be bound for each
407			 * GOT entry except for the first one.  This program
408			 * will still run, albeit very slowly.  It is very
409			 * unlikely that this case will ever happen in
410			 * practice.
411			 */
412			if ((int32_t)delta != delta) {
413				rdbg(("  PLT stub too far from GOT to relocate"));
414				goto out;
415			}
416			dhigh = delta - (int16_t)delta;
417			if (dhigh != 0) {
418				/*
419				 * Build an LDAH instruction to adjust the
420				 * high 16 bits.
421				 */
422				insn[insncnt++] = 0x09 << 26 | 27 << 21 |
423				    27 << 16 | ((dhigh >> 16) & 0xffff);
424				rdbg(("  LDAH $27,%d($27)",
425				    (int16_t)(dhigh >> 16)));
426			}
427			/* Build an LDQ to load the GOT entry. */
428			insn[insncnt++] = 0x29 << 26 | 27 << 21 |
429			    27 << 16 | (delta & 0xffff);
430			rdbg(("  LDQ  $27,%d($27)",
431			    (int16_t)delta));
432		}
433
434		/*
435		 * Now, build a JMP or BR insn to jump to the target.  If
436		 * the displacement fits in a sign-extended 21-bit field,
437		 * we can use the more efficient BR insn.  Otherwise, we
438		 * have to jump indirect through PV.
439		 */
440		pc = stubaddr + (4 * (insncnt + 1));
441		idisp = (int64_t)(new_value - pc) >> 2;
442		if (-0x100000 <= idisp && idisp < 0x100000) {
443			insn[insncnt++] = 0x30 << 26 | 31 << 21 |
444			    (idisp & 0x1fffff);
445			rdbg(("  BR   $31,%p", (void *)new_value));
446		} else {
447			insn[insncnt++] = 0x1a << 26 | 31 << 21 |
448			    27 << 16 | (idisp & 0x3fff);
449			rdbg(("  JMP  $31,($27),%d",
450			    (int)(idisp & 0x3fff)));
451		}
452
453		/*
454		 * Fill in the tail of the PLT entry first, for reentrancy.
455		 * Until we have overwritten the first insn (an unconditional
456		 * branch), the remaining insns have no effect.
457		 */
458		stubptr = (uint32_t *)stubaddr;
459		while (insncnt > 1) {
460			insncnt--;
461			stubptr[insncnt] = insn[insncnt];
462		}
463		/*
464		 * Commit the tail of the insn sequence to memory
465		 * before overwriting the first insn.
466		 */
467		__asm __volatile("wmb" ::: "memory");
468		stubptr[0] = insn[0];
469		/*
470		 * I-stream will be sync'd when we either return from
471		 * the binder (lazy bind case) or when the PLTGOT thunk
472		 * is patched up (bind-now case).
473		 */
474	}
475out:
476	if (tp)
477		*tp = new_value;
478
479	return 0;
480}
481
482caddr_t
483_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
484{
485	const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
486	Elf_Addr result;
487	int err;
488
489	err = _rtld_relocate_plt_object(obj, rela, &result);
490	if (err)
491		_rtld_die();
492
493	return (caddr_t)result;
494}
495
496int
497_rtld_relocate_plt_objects(const Obj_Entry *obj)
498{
499	const Elf_Rela *rela;
500
501	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
502		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
503			return -1;
504
505	return 0;
506}
507