mdreloc.c revision 1.28
1/*	$NetBSD: mdreloc.c,v 1.28 2011/03/25 18:07:05 joerg Exp $	*/
2
3#include <sys/cdefs.h>
4#ifndef lint
5__RCSID("$NetBSD: mdreloc.c,v 1.28 2011/03/25 18:07:05 joerg Exp $");
6#endif /* not lint */
7
8#include <sys/cdefs.h>
9#ifndef lint
10__RCSID("$NetBSD: mdreloc.c,v 1.28 2011/03/25 18:07:05 joerg Exp $");
11#endif /* not lint */
12
13#include <sys/types.h>
14
15#include "debug.h"
16#include "rtld.h"
17
18void _rtld_bind_start(void);
19void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
20caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
21static inline int _rtld_relocate_plt_object(const Obj_Entry *,
22    const Elf_Rela *, Elf_Addr *);
23
24
25void
26_rtld_setup_pltgot(const Obj_Entry *obj)
27{
28	obj->pltgot[1] = (Elf_Addr) obj;
29	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
30}
31
32void
33_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
34{
35	const Elf_Rela *rela = 0, *relalim;
36	Elf_Addr relasz = 0;
37	Elf_Addr *where;
38
39	for (; dynp->d_tag != DT_NULL; dynp++) {
40		switch (dynp->d_tag) {
41		case DT_RELA:
42			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
43			break;
44		case DT_RELASZ:
45			relasz = dynp->d_un.d_val;
46			break;
47		}
48	}
49	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
50	for (; rela < relalim; rela++) {
51		where = (Elf_Addr *)(relocbase + rela->r_offset);
52		*where += (Elf_Addr)relocbase;
53	}
54}
55
56int
57_rtld_relocate_nonplt_objects(Obj_Entry *obj)
58{
59	const Elf_Rela *rela;
60
61	for (rela = obj->rela; rela < obj->relalim; rela++) {
62		Elf_Addr        *where;
63		const Elf_Sym   *def;
64		const Obj_Entry *defobj;
65		Elf_Addr         tmp;
66		unsigned long	 symnum;
67
68		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
69		symnum = ELF_R_SYM(rela->r_info);
70
71		switch (ELF_R_TYPE(rela->r_info)) {
72		case R_TYPE(NONE):
73			break;
74
75#if 1 /* XXX should not occur */
76		case R_TYPE(PC32):
77			def = _rtld_find_symdef(symnum, obj, &defobj, false);
78			if (def == NULL)
79				return -1;
80
81			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
82			    rela->r_addend) - (Elf_Addr)where;
83			if (*where != tmp)
84				*where = tmp;
85			rdbg(("PC32 %s in %s --> %p in %s",
86			    obj->strtab + obj->symtab[symnum].st_name,
87			    obj->path, (void *)*where, defobj->path));
88			break;
89
90		case R_TYPE(GOT32):
91#endif
92		case R_TYPE(32):
93		case R_TYPE(GLOB_DAT):
94			def = _rtld_find_symdef(symnum, obj, &defobj, false);
95			if (def == NULL)
96				return -1;
97
98			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
99			    rela->r_addend);
100			if (*where != tmp)
101				*where = tmp;
102			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
103			    obj->strtab + obj->symtab[symnum].st_name,
104			    obj->path, (void *)*where, defobj->path));
105			break;
106
107		case R_TYPE(RELATIVE):
108			*where += (Elf_Addr)obj->relocbase;
109			rdbg(("RELATIVE in %s --> %p", obj->path,
110			    (void *)*where));
111			break;
112
113		case R_TYPE(COPY):
114			/*
115			 * These are deferred until all other relocations have
116			 * been done.  All we do here is make sure that the
117			 * COPY relocation is not in a shared library.  They
118			 * are allowed only in executable files.
119			 */
120			if (obj->isdynamic) {
121				_rtld_error(
122			"%s: Unexpected R_COPY relocation in shared library",
123				    obj->path);
124				return -1;
125			}
126			rdbg(("COPY (avoid in main)"));
127			break;
128
129		default:
130			rdbg(("sym = %lu, type = %lu, offset = %p, "
131			    "addend = %p, contents = %p, symbol = %s",
132			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
133			    (void *)rela->r_offset, (void *)rela->r_addend,
134			    (void *)*where,
135			    obj->strtab + obj->symtab[symnum].st_name));
136			_rtld_error("%s: Unsupported relocation type %ld "
137			    "in non-PLT relocations",
138			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
139			return -1;
140		}
141	}
142	return 0;
143}
144
145int
146_rtld_relocate_plt_lazy(const Obj_Entry *obj)
147{
148	const Elf_Rela *rela;
149
150	if (!obj->relocbase)
151		return 0;
152
153	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
154		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
155
156		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
157
158		/* Just relocate the GOT slots pointing into the PLT */
159		*where += (Elf_Addr)obj->relocbase;
160		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
161	}
162
163	return 0;
164}
165
166static inline int
167_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
168    Elf_Addr *tp)
169{
170	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
171	Elf_Addr new_value;
172	const Elf_Sym  *def;
173	const Obj_Entry *defobj;
174	unsigned long info = rela->r_info;
175
176	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
177
178	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
179	if (__predict_false(def == NULL))
180		return -1;
181	if (__predict_false(def == &_rtld_sym_zero))
182		return 0;
183
184	assert(rela->r_addend == 0);
185	new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
186	    rela->r_addend);
187	rdbg(("bind now/fixup in %s --> old=%p new=%p",
188	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
189	if (*where != new_value)
190		*where = new_value;
191
192	if (tp)
193		*tp = new_value - rela->r_addend;
194
195	return 0;
196}
197
198caddr_t
199_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
200{
201	const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
202	Elf_Addr result;
203	int err;
204
205	result = 0;	/* XXX gcc */
206
207	_rtld_shared_enter();
208	err = _rtld_relocate_plt_object(obj, rela, &result);
209	if (err)
210		_rtld_die();
211	_rtld_shared_exit();
212
213	return (caddr_t)result;
214}
215
216int
217_rtld_relocate_plt_objects(const Obj_Entry *obj)
218{
219	const Elf_Rela *rela;
220
221	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
222		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
223			return -1;
224
225	return 0;
226}
227