rtld_machine.c revision 1.4
1#define _DYN_LOADER
2
3#include <sys/types.h>
4#include <sys/cdefs.h>
5#include <sys/mman.h>
6
7#include <nlist.h>
8#include <link.h>
9
10#include "syscall.h"
11#include "archdep.h"
12#include "resolve.h"
13
14void
15_dl_bcopy(const void *src, void *dest, int size)
16{
17	const unsigned char *psrc = src;
18	unsigned char *pdest = dest;
19	int i;
20
21	for (i = 0; i < size; i++)
22		pdest[i] = psrc[i];
23}
24
25/*
26 * The following table holds for each relocation type:
27 *	- the width in bits of the memory location the relocation
28 *	  applies to (not currently used)
29 *	- the number of bits the relocation value must be shifted to the
30 *	  right (i.e. discard least significant bits) to fit into
31 *	  the appropriate field in the instruction word.
32 *	- flags indicating whether
33 *		* the relocation involves a symbol
34 *		* the relocation is relative to the current position
35 *		* the relocation is for a GOT entry
36 *		* the relocation is relative to the load address
37 *
38 */
39#define _RF_S		0x80000000		/* Resolve symbol */
40#define _RF_A		0x40000000		/* Use addend */
41#define _RF_P		0x20000000		/* Location relative */
42#define _RF_G		0x10000000		/* GOT offset */
43#define _RF_B		0x08000000		/* Load address relative */
44#define _RF_U		0x04000000		/* Unaligned */
45#define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
46#define _RF_RS(s)	((s) & 0xff)		/* right shift */
47static int reloc_target_flags[] = {
48	0,							/* NONE */
49	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* RELOC_32*/
50	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC32 */
51	_RF_G|			_RF_SZ(32) | _RF_RS(00),	/* GOT32 */
52	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
53	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* COPY */
54	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
55	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* JUMP_SLOT */
56	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
57	0,							/* GOTOFF XXX */
58	0,							/* GOTPC XXX */
59	0,							/* DUMMY 11 */
60	0,							/* DUMMY 12 */
61	0,							/* DUMMY 13 */
62	0,							/* DUMMY 14 */
63	0,							/* DUMMY 15 */
64	0,							/* DUMMY 16 */
65	0,							/* DUMMY 17 */
66	0,							/* DUMMY 18 */
67	0,							/* DUMMY 19 */
68	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* RELOC_16 */
69	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* PC_16 */
70	_RF_S|_RF_A|		_RF_SZ(8) | _RF_RS(0),		/* RELOC_8 */
71	_RF_S|_RF_A|_RF_P|	_RF_SZ(8) | _RF_RS(0),		/* RELOC_PC8 */
72};
73
74#define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
75#define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
76#define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
77#define RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
78#define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
79#define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
80#define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
81
82static long reloc_target_bitmask[] = {
83#define _BM(x)	(~(-(1ULL << (x))))
84	0,		/* NONE */
85	_BM(32),	/* RELOC_32*/
86	_BM(32),	/* PC32 */
87	_BM(32),	/* GOT32 */
88	_BM(32),	/* PLT32 */
89	0,		/* COPY */
90	_BM(32),	/* GLOB_DAT */
91	_BM(32),	/* JUMP_SLOT */
92	_BM(32),	/* RELATIVE */
93	0,		/* GOTOFF XXX */
94	0,		/* GOTPC XXX */
95	0,		/* DUMMY 11 */
96	0,		/* DUMMY 12 */
97	0,		/* DUMMY 13 */
98	0,		/* DUMMY 14 */
99	0,		/* DUMMY 15 */
100	0,		/* DUMMY 16 */
101	0,		/* DUMMY 17 */
102	0,		/* DUMMY 18 */
103	0,		/* DUMMY 19 */
104	_BM(16),	/* RELOC_16 */
105	_BM(8),		/* PC_16 */
106	_BM(8),		/* RELOC_8 */
107	_BM(8),		/* RELOC_PC8 */
108#undef _BM
109};
110#define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
111
112void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value);
113
114int
115_dl_md_reloc(elf_object_t *object, int rel, int relsz)
116{
117	long	i;
118	long	numrel;
119	long	fails = 0;
120	Elf_Addr loff;
121	Elf_Rel *rels;
122	struct load_list *llist;
123
124	loff = object->load_offs;
125	numrel = object->Dyn.info[relsz] / sizeof(Elf32_Rel);
126	rels = (Elf32_Rel *)(object->Dyn.info[rel]);
127	if (rels == NULL)
128		return(0);
129
130	/*
131	 * unprotect some segments if we need it.
132	 */
133	if ((rel == DT_REL || rel == DT_RELA)) {
134		for (llist = object->load_list; llist != NULL; llist = llist->next) {
135			if (!(llist->prot & PROT_WRITE))
136				_dl_mprotect(llist->start, llist->size,
137				    llist->prot|PROT_WRITE);
138		}
139	}
140
141	for (i = 0; i < numrel; i++, rels++) {
142		Elf_Addr *where, value, ooff, mask;
143		Elf_Word type;
144		const Elf_Sym *sym, *this;
145		const char *symn;
146
147		type = ELF_R_TYPE(rels->r_info);
148
149		if (type == R_TYPE(NONE))
150			continue;
151
152		if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL)
153			continue;
154
155		where = (Elf_Addr *)(rels->r_offset + loff);
156
157		if (RELOC_USE_ADDEND(type))
158			value = *where & RELOC_VALUE_BITMASK(type);
159		else
160			value = 0;
161
162		sym = NULL;
163		symn = NULL;
164		if (RELOC_RESOLVE_SYMBOL(type)) {
165			sym = object->dyn.symtab;
166			sym += ELF_R_SYM(rels->r_info);
167			symn = object->dyn.strtab + sym->st_name;
168
169			if (sym->st_shndx != SHN_UNDEF &&
170			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
171				value += loff;
172			} else {
173				this = NULL;
174				ooff = _dl_find_symbol(symn, _dl_objects,
175				    &this, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
176				    ((type == R_TYPE(JUMP_SLOT))?
177					SYM_PLT:SYM_NOTPLT),
178				    sym->st_size, object->load_name);
179				if (this == NULL) {
180resolve_failed:
181					_dl_printf("%s: %s: can't resolve "
182					    "reference '%s'\n",
183					    _dl_progname, object->load_name,
184					    symn);
185					fails++;
186					continue;
187				}
188				value += (Elf_Addr)(ooff + this->st_value);
189			}
190		}
191
192		if (type == R_TYPE(JUMP_SLOT)) {
193			_dl_reloc_plt((Elf_Word *)where, value);
194			continue;
195		}
196
197		if (type == R_TYPE(COPY)) {
198			void *dstaddr = where;
199			const void *srcaddr;
200			const Elf_Sym *dstsym = sym, *srcsym = NULL;
201			size_t size = dstsym->st_size;
202			Elf_Addr soff;
203
204			soff = _dl_find_symbol(symn, object->next, &srcsym,
205			    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
206			    ((type == R_TYPE(JUMP_SLOT)) ? SYM_PLT:SYM_NOTPLT),
207			    size, object->load_name);
208			if (srcsym == NULL)
209				goto resolve_failed;
210
211			srcaddr = (void *)(soff + srcsym->st_value);
212			_dl_bcopy(srcaddr, dstaddr, size);
213			continue;
214		}
215
216		if (RELOC_PC_RELATIVE(type))
217			value -= (Elf_Addr)where;
218		if (RELOC_BASE_RELATIVE(type))
219			value += loff;
220
221		mask = RELOC_VALUE_BITMASK(type);
222		value >>= RELOC_VALUE_RIGHTSHIFT(type);
223		value &= mask;
224
225		if (RELOC_UNALIGNED(type)) {
226			/* Handle unaligned relocations. */
227			Elf_Addr tmp = 0;
228			char *ptr = (char *)where;
229			int i, size = RELOC_TARGET_SIZE(type)/8;
230
231			/* Read it in one byte at a time. */
232			for (i=0; i<size; i++)
233				tmp = (tmp << 8) | ptr[i];
234
235			tmp &= ~mask;
236			tmp |= value;
237
238			/* Write it back out. */
239			for (i=0; i<size; i++)
240				ptr[i] = ((tmp >> (8*i)) & 0xff);
241		} else if (RELOC_TARGET_SIZE(type) > 32) {
242			*where &= ~mask;
243			*where |= value;
244		} else {
245			Elf32_Addr *where32 = (Elf32_Addr *)where;
246
247			*where32 &= ~mask;
248			*where32 |= value;
249		}
250	}
251
252	/* reprotect the unprotected segments */
253	if ((rel == DT_REL || rel == DT_RELA)) {
254		for (llist = object->load_list; llist != NULL; llist = llist->next) {
255			if (!(llist->prot & PROT_WRITE))
256				_dl_mprotect(llist->start, llist->size,
257				    llist->prot);
258		}
259	}
260
261	return (fails);
262}
263
264struct jmpslot {
265	u_short opcode;
266	u_short addr[2];
267	u_short reloc_index;
268#define JMPSLOT_RELOC_MASK              0xffff
269};
270#define JUMP    0xe990          /* NOP + JMP opcode */
271
272void
273_dl_reloc_plt(Elf_Addr *where, Elf_Addr value)
274{
275	*where = value;
276}
277
278/*
279 * Resolve a symbol at run-time.
280 */
281Elf_Addr
282_dl_bind(elf_object_t *object, int index)
283{
284	Elf_Rel *rel;
285	Elf_Word *addr;
286	const Elf_Sym *sym, *this;
287	const char *symn;
288	Elf_Addr ooff;
289
290	rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
291
292	rel += index/sizeof(Elf_Rel);
293
294	sym = object->dyn.symtab;
295	sym += ELF_R_SYM(rel->r_info);
296	symn = object->dyn.strtab + sym->st_name;
297
298	addr = (Elf_Word *)(object->load_offs + rel->r_offset);
299	this = NULL;
300	ooff = _dl_find_symbol(symn, _dl_objects, &this,
301	    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT, 0, object->load_name);
302	if (this == NULL) {
303		_dl_printf("lazy binding failed!\n");
304		*((int *)0) = 0;        /* XXX */
305	}
306
307	_dl_reloc_plt(addr, ooff + this->st_value);
308
309	return((Elf_Addr)ooff + this->st_value);
310}
311
312void
313_dl_md_reloc_got(elf_object_t *object, int lazy)
314{
315	extern void _dl_bind_start(void);       /* XXX */
316	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
317	int i, num;
318	Elf_Rel *rel;
319	struct load_list *llist;
320
321	if (pltgot == NULL)
322		return; /* it is possible to have no PLT/GOT relocations */
323
324	pltgot[1] = (Elf_Addr)object;
325	pltgot[2] = (Elf_Addr)&_dl_bind_start;
326
327	if (object->Dyn.info[DT_PLTREL] != DT_REL)
328		return;
329
330	if (!lazy) {
331		_dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
332		return;
333	}
334
335	rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
336	num = (object->Dyn.info[DT_PLTRELSZ]);
337	for (llist = object->load_list; llist != NULL; llist = llist->next) {
338		if (!(llist->prot & PROT_WRITE))
339			_dl_mprotect(llist->start, llist->size,
340			    llist->prot|PROT_WRITE);
341	}
342	for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) {
343		Elf_Addr *where;
344		where = (Elf_Addr *)(rel->r_offset + object->load_offs);
345		*where += object->load_offs;
346	}
347	for (llist = object->load_list; llist != NULL; llist = llist->next) {
348		if (!(llist->prot & PROT_WRITE))
349			_dl_mprotect(llist->start, llist->size,
350			    llist->prot);
351	}
352}
353
354
355