xref: /freebsd-13.1/libexec/rtld-elf/mips/reloc.c (revision b040b03d)
1 /*	$NetBSD: mips_reloc.c,v 1.58 2010/01/14 11:57:06 skrll Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright 1997 Michael L. Hitch <[email protected]>
7  * Portions copyright 2002 Charles M. Hannum <[email protected]>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/endian.h>
39 
40 #include <stdlib.h>
41 #include <string.h>
42 #include <inttypes.h>
43 
44 #include <machine/sysarch.h>
45 #include <machine/tls.h>
46 
47 #include "debug.h"
48 #include "rtld.h"
49 
50 #ifdef __mips_n64
51 #define	GOT1_MASK	0x8000000000000000UL
52 #else
53 #define	GOT1_MASK	0x80000000UL
54 #endif
55 
56 /*
57  * Determine if the second GOT entry is reserved for rtld or if it is
58  * the first "real" GOT entry.
59  *
60  * This must be a macro rather than a function so that
61  * _rtld_relocate_nonplt_self doesn't trigger a GOT invocation trying
62  * to use it before the local GOT entries in rtld are adjusted.
63  */
64 #ifdef __mips_n64
65 /* Old binutils uses the 32-bit GOT1 mask value for N64. */
66 #define GOT1_RESERVED_FOR_RTLD(got)					\
67 	(((got)[1] == 0x80000000) || (got)[1] & GOT1_MASK)
68 #else
69 #define GOT1_RESERVED_FOR_RTLD(got)	((got)[1] & GOT1_MASK)
70 #endif
71 
72 #ifdef __mips_n64
73 /*
74  * ELF64 MIPS encodes the relocs uniquely.  The first 32-bits of info contain
75  * the symbol index.  The top 32-bits contain three relocation types encoded
76  * in big-endian integer with first relocation in LSB.  This means for little
77  * endian we have to byte swap that integer (r_type).
78  */
79 #define	Elf_Sxword			Elf64_Sxword
80 #define	ELF_R_NXTTYPE_64_P(r_type)	((((r_type) >> 8) & 0xff) == R_TYPE(64))
81 #if BYTE_ORDER == LITTLE_ENDIAN
82 #undef ELF_R_SYM
83 #undef ELF_R_TYPE
84 #define ELF_R_SYM(r_info)		((r_info) & 0xffffffff)
85 #define ELF_R_TYPE(r_info)		bswap32((r_info) >> 32)
86 #endif
87 #else
88 #define	ELF_R_NXTTYPE_64_P(r_type)	(0)
89 #define	Elf_Sxword			Elf32_Sword
90 #endif
91 
92 void _rtld_pltbind_start(void);
93 
94 void
95 init_pltgot(Obj_Entry *obj)
96 {
97 
98 	if (obj->pltgot != NULL) {
99 		obj->pltgot[0] = (Elf_Addr) &_rtld_bind_start;
100 		if (GOT1_RESERVED_FOR_RTLD(obj->pltgot))
101 			obj->pltgot[1] = (Elf_Addr) obj | GOT1_MASK;
102 	}
103 	if (obj->mips_pltgot != NULL) {
104 		obj->mips_pltgot[0] = (Elf_Addr) &_rtld_pltbind_start;
105 		obj->mips_pltgot[1] = (Elf_Addr) obj;
106 	}
107 }
108 
109 int
110 do_copy_relocations(Obj_Entry *dstobj)
111 {
112 	const Obj_Entry *srcobj, *defobj;
113 	const Elf_Rel *rellim;
114 	const Elf_Rel *rel;
115 	const Elf_Sym *srcsym;
116 	const Elf_Sym *dstsym;
117 	const void *srcaddr;
118 	const char *name;
119 	void *dstaddr;
120 	SymLook req;
121 	size_t size;
122 	int res;
123 
124 	/*
125 	 * COPY relocs are invalid outside of the main program
126 	 */
127 	assert(dstobj->mainprog);
128 
129 	rellim = (const Elf_Rel *)((const char *)dstobj->rel + dstobj->relsize);
130 	for (rel = dstobj->rel; rel < rellim; rel++) {
131 		if (ELF_R_TYPE(rel->r_info) != R_MIPS_COPY)
132 			continue;
133 
134 		dstaddr = (void *)(dstobj->relocbase + rel->r_offset);
135 		dstsym = dstobj->symtab + ELF_R_SYM(rel->r_info);
136 		name = dstobj->strtab + dstsym->st_name;
137 		size = dstsym->st_size;
138 
139 		symlook_init(&req, name);
140 		req.ventry = fetch_ventry(dstobj, ELF_R_SYM(rel->r_info));
141 		req.flags = SYMLOOK_EARLY;
142 
143 		for (srcobj = globallist_next(dstobj); srcobj != NULL;
144 		     srcobj = globallist_next(srcobj)) {
145 			res = symlook_obj(&req, srcobj);
146 			if (res == 0) {
147 				srcsym = req.sym_out;
148 				defobj = req.defobj_out;
149 				break;
150 			}
151 		}
152 		if (srcobj == NULL) {
153 			_rtld_error(
154 "Undefined symbol \"%s\" referenced from COPY relocation in %s",
155 			    name, dstobj->path);
156 			return (-1);
157 		}
158 
159 		srcaddr = (const void *)(defobj->relocbase + srcsym->st_value);
160 		memcpy(dstaddr, srcaddr, size);
161 	}
162 
163 	return (0);
164 }
165 
166 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
167 
168 /*
169  * It is possible for the compiler to emit relocations for unaligned data.
170  * We handle this situation with these inlines.
171  */
172 static __inline Elf_Sxword
173 load_ptr(void *where, size_t len)
174 {
175 	Elf_Sxword val;
176 
177 	if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
178 #ifdef __mips_n64
179 		if (len == sizeof(Elf_Sxword))
180 			return *(Elf_Sxword *)where;
181 #endif
182 		return *(Elf_Sword *)where;
183 	}
184 
185 	val = 0;
186 #if BYTE_ORDER == LITTLE_ENDIAN
187 	(void)memcpy(&val, where, len);
188 #endif
189 #if BYTE_ORDER == BIG_ENDIAN
190 	(void)memcpy((uint8_t *)((&val)+1) - len, where, len);
191 #endif
192 	return (len == sizeof(Elf_Sxword)) ? val : (Elf_Sword)val;
193 }
194 
195 static __inline void
196 store_ptr(void *where, Elf_Sxword val, size_t len)
197 {
198 	if (__predict_true(((uintptr_t)where & (len - 1)) == 0)) {
199 #ifdef __mips_n64
200 		if (len == sizeof(Elf_Sxword)) {
201 			*(Elf_Sxword *)where = val;
202 			return;
203 		}
204 #endif
205 		*(Elf_Sword *)where = val;
206 		return;
207 	}
208 #if BYTE_ORDER == LITTLE_ENDIAN
209 	(void)memcpy(where, &val, len);
210 #endif
211 #if BYTE_ORDER == BIG_ENDIAN
212 	(void)memcpy(where, (const uint8_t *)((&val)+1) - len, len);
213 #endif
214 }
215 
216 void
217 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
218 {
219 	const Elf_Rel *rel = NULL, *rellim;
220 	Elf_Addr relsz = 0;
221 	const Elf_Sym *symtab = NULL, *sym;
222 	Elf_Addr *where;
223 	Elf_Addr *got = NULL;
224 	Elf_Word local_gotno = 0, symtabno = 0, gotsym = 0;
225 	size_t i;
226 
227 	for (; dynp->d_tag != DT_NULL; dynp++) {
228 		switch (dynp->d_tag) {
229 		case DT_REL:
230 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
231 			break;
232 		case DT_RELSZ:
233 			relsz = dynp->d_un.d_val;
234 			break;
235 		case DT_SYMTAB:
236 			symtab = (const Elf_Sym *)(relocbase + dynp->d_un.d_ptr);
237 			break;
238 		case DT_PLTGOT:
239 			got = (Elf_Addr *)(relocbase + dynp->d_un.d_ptr);
240 			break;
241 		case DT_MIPS_LOCAL_GOTNO:
242 			local_gotno = dynp->d_un.d_val;
243 			break;
244 		case DT_MIPS_SYMTABNO:
245 			symtabno = dynp->d_un.d_val;
246 			break;
247 		case DT_MIPS_GOTSYM:
248 			gotsym = dynp->d_un.d_val;
249 			break;
250 		}
251 	}
252 
253 	i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1;
254 	/* Relocate the local GOT entries */
255 	got += i;
256 	for (; i < local_gotno; i++) {
257 		*got++ += relocbase;
258 	}
259 
260 	sym = symtab + gotsym;
261 	/* Now do the global GOT entries */
262 	for (i = gotsym; i < symtabno; i++) {
263 		*got = sym->st_value + relocbase;
264 		++sym;
265 		++got;
266 	}
267 
268 	rellim = (const Elf_Rel *)((const char *)rel + relsz);
269 	for (; rel < rellim; rel++) {
270 		Elf_Word r_symndx, r_type;
271 
272 		where = (void *)(relocbase + rel->r_offset);
273 
274 		r_symndx = ELF_R_SYM(rel->r_info);
275 		r_type = ELF_R_TYPE(rel->r_info);
276 
277 		switch (r_type & 0xff) {
278 		case R_TYPE(REL32): {
279 			const size_t rlen =
280 			    ELF_R_NXTTYPE_64_P(r_type)
281 				? sizeof(Elf_Sxword)
282 				: sizeof(Elf_Sword);
283 			Elf_Sxword old = load_ptr(where, rlen);
284 			Elf_Sxword val = old;
285 #ifdef __mips_n64
286 			assert(r_type == R_TYPE(REL32)
287 			    || r_type == (R_TYPE(REL32)|(R_TYPE(64) << 8)));
288 #endif
289 			assert(r_symndx < gotsym);
290 			sym = symtab + r_symndx;
291 			assert(ELF_ST_BIND(sym->st_info) == STB_LOCAL);
292 			val += relocbase;
293 			dbg("REL32/L(%p) %p -> %p in <self>",
294 			    where, (void *)old, (void *)val);
295 			store_ptr(where, val, rlen);
296 			break;
297 		}
298 
299 		case R_TYPE(GPREL32):
300 		case R_TYPE(NONE):
301 			break;
302 
303 
304 		default:
305 			abort();
306 			break;
307 		}
308 	}
309 }
310 
311 Elf_Addr
312 _mips_rtld_bind(Obj_Entry *obj, Elf_Size reloff)
313 {
314         Elf_Addr *got = obj->pltgot;
315         const Elf_Sym *def;
316         const Obj_Entry *defobj;
317         Elf_Addr *where;
318         Elf_Addr target;
319         RtldLockState lockstate;
320 
321 	rlock_acquire(rtld_bind_lock, &lockstate);
322 	if (sigsetjmp(lockstate.env, 0) != 0)
323 		lock_upgrade(rtld_bind_lock, &lockstate);
324 
325 	where = &got[obj->local_gotno + reloff - obj->gotsym];
326         def = find_symdef(reloff, obj, &defobj, SYMLOOK_IN_PLT, NULL,
327            &lockstate);
328         if (def == NULL)
329 		rtld_die();
330 
331         target = (Elf_Addr)(defobj->relocbase + def->st_value);
332         dbg("bind now/fixup at %s sym # %jd in %s --> was=%p new=%p",
333 	    obj->path,
334 	    (intmax_t)reloff, defobj->strtab + def->st_name,
335 	    (void *)*where, (void *)target);
336 	if (!ld_bind_not)
337 		*where = target;
338 	lock_release(rtld_bind_lock, &lockstate);
339 	return (Elf_Addr)target;
340 }
341 
342 int
343 reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,
344     RtldLockState *lockstate)
345 {
346 	const Elf_Rel *rel;
347 	const Elf_Rel *rellim;
348 	Elf_Addr *got = obj->pltgot;
349 	const Elf_Sym *sym, *def;
350 	const Obj_Entry *defobj;
351 	Elf_Word i;
352 #ifdef SUPPORT_OLD_BROKEN_LD
353 	int broken;
354 #endif
355 
356 	/* The relocation for the dynamic loader has already been done. */
357 	if (obj == obj_rtld)
358 		return (0);
359 
360 	if ((flags & SYMLOOK_IFUNC) != 0)
361 		/* XXX not implemented */
362 		return (0);
363 
364 #ifdef SUPPORT_OLD_BROKEN_LD
365 	broken = 0;
366 	sym = obj->symtab;
367 	for (i = 1; i < 12; i++)
368 		if (sym[i].st_info == ELF_ST_INFO(STB_LOCAL, STT_NOTYPE))
369 			broken = 1;
370 	dbg("%s: broken=%d", obj->path, broken);
371 #endif
372 
373 	i = GOT1_RESERVED_FOR_RTLD(got) ? 2 : 1;
374 
375 	/* Relocate the local GOT entries */
376 	got += i;
377 	dbg("got:%p for %d entries adding %p",
378 	    got, obj->local_gotno, obj->relocbase);
379 	for (; i < obj->local_gotno; i++) {
380 		*got += (Elf_Addr)obj->relocbase;
381 		got++;
382 	}
383 	sym = obj->symtab + obj->gotsym;
384 
385 	dbg("got:%p for %d entries",
386 	    got, obj->symtabno);
387 	/* Now do the global GOT entries */
388 	for (i = obj->gotsym; i < obj->symtabno; i++) {
389 		dbg(" doing got %d sym %p (%s, %lx)", i - obj->gotsym, sym,
390 		    sym->st_name + obj->strtab, (u_long) *got);
391 
392 #ifdef SUPPORT_OLD_BROKEN_LD
393 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
394 		    broken && sym->st_shndx == SHN_UNDEF) {
395 			/*
396 			 * XXX DANGER WILL ROBINSON!
397 			 * You might think this is stupid, as it intentionally
398 			 * defeats lazy binding -- and you'd be right.
399 			 * Unfortunately, for lazy binding to work right, we
400 			 * need to a way to force the GOT slots used for
401 			 * function pointers to be resolved immediately.  This
402 			 * is supposed to be done automatically by the linker,
403 			 * by not outputting a PLT slot and setting st_value
404 			 * to 0 if there are non-PLT references, but older
405 			 * versions of GNU ld do not do this.
406 			 */
407 			def = find_symdef(i, obj, &defobj, flags, NULL,
408 			    lockstate);
409 			if (def == NULL)
410 				return -1;
411 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
412 		} else
413 #endif
414 		if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
415 		    sym->st_value != 0 && sym->st_shndx == SHN_UNDEF) {
416 			/*
417 			 * If there are non-PLT references to the function,
418 			 * st_value should be 0, forcing us to resolve the
419 			 * address immediately.
420 			 *
421 			 * XXX DANGER WILL ROBINSON!
422 			 * The linker is not outputting PLT slots for calls to
423 			 * functions that are defined in the same shared
424 			 * library.  This is a bug, because it can screw up
425 			 * link ordering rules if the symbol is defined in
426 			 * more than one module.  For now, if there is a
427 			 * definition, we fail the test above and force a full
428 			 * symbol lookup.  This means that all intra-module
429 			 * calls are bound immediately.  - mycroft, 2003/09/24
430 			 */
431 			*got = sym->st_value + (Elf_Addr)obj->relocbase;
432 			if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
433 				dbg("Warning2, i:%d maps to relocbase address:%p",
434 				    i, obj->relocbase);
435 			}
436 
437 		} else if (sym->st_info == ELF_ST_INFO(STB_GLOBAL, STT_SECTION)) {
438 			/* Symbols with index SHN_ABS are not relocated. */
439 			if (sym->st_shndx != SHN_ABS) {
440 				*got = sym->st_value +
441 				    (Elf_Addr)obj->relocbase;
442 				if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
443 					dbg("Warning3, i:%d maps to relocbase address:%p",
444 					    i, obj->relocbase);
445 				}
446 			}
447 		} else {
448 			/* TODO: add cache here */
449 			def = find_symdef(i, obj, &defobj, flags, NULL,
450 			    lockstate);
451 			if (def == NULL) {
452 				dbg("Warning4, can't find symbole %d", i);
453 				return -1;
454 			}
455 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
456 			if ((Elf_Addr)(*got) == (Elf_Addr)obj->relocbase) {
457 				dbg("Warning4, i:%d maps to relocbase address:%p",
458 				    i, obj->relocbase);
459 				dbg("via first obj symbol %s",
460 				    obj->strtab + obj->symtab[i].st_name);
461 				dbg("found in obj %p:%s",
462 				    defobj, defobj->path);
463 			}
464 		}
465 
466 		dbg("  --> now %lx", (u_long) *got);
467 		++sym;
468 		++got;
469 	}
470 
471 	got = obj->pltgot;
472 	rellim = (const Elf_Rel *)((const char *)obj->rel + obj->relsize);
473 	for (rel = obj->rel; rel < rellim; rel++) {
474 		Elf_Word	r_symndx, r_type;
475 		void		*where;
476 
477 		where = obj->relocbase + rel->r_offset;
478 		r_symndx = ELF_R_SYM(rel->r_info);
479 		r_type = ELF_R_TYPE(rel->r_info);
480 
481 		switch (r_type & 0xff) {
482 		case R_TYPE(NONE):
483 			break;
484 
485 		case R_TYPE(REL32): {
486 			/* 32-bit PC-relative reference */
487 			const size_t rlen =
488 			    ELF_R_NXTTYPE_64_P(r_type)
489 				? sizeof(Elf_Sxword)
490 				: sizeof(Elf_Sword);
491 			Elf_Sxword old = load_ptr(where, rlen);
492 			Elf_Sxword val = old;
493 
494 			def = obj->symtab + r_symndx;
495 
496 			if (r_symndx >= obj->gotsym) {
497 				val += got[obj->local_gotno + r_symndx - obj->gotsym];
498 				dbg("REL32/G(%p) %p --> %p (%s) in %s",
499 				    where, (void *)old, (void *)val,
500 				    obj->strtab + def->st_name,
501 				    obj->path);
502 			} else {
503 				/*
504 				 * XXX: ABI DIFFERENCE!
505 				 *
506 				 * Old NetBSD binutils would generate shared
507 				 * libs with section-relative relocations being
508 				 * already adjusted for the start address of
509 				 * the section.
510 				 *
511 				 * New binutils, OTOH, generate shared libs
512 				 * with the same relocations being based at
513 				 * zero, so we need to add in the start address
514 				 * of the section.
515 				 *
516 				 * --rkb, Oct 6, 2001
517 				 */
518 
519 				if (def->st_info ==
520 				    ELF_ST_INFO(STB_LOCAL, STT_SECTION)
521 #ifdef SUPPORT_OLD_BROKEN_LD
522 				    && !broken
523 #endif
524 				    )
525 					val += (Elf_Addr)def->st_value;
526 
527 				val += (Elf_Addr)obj->relocbase;
528 
529 				dbg("REL32/L(%p) %p -> %p (%s) in %s",
530 				    where, (void *)old, (void *)val,
531 				    obj->strtab + def->st_name, obj->path);
532 			}
533 			store_ptr(where, val, rlen);
534 			break;
535 		}
536 
537 		case R_TYPE(COPY):
538 			/*
539 			 * These are deferred until all other relocations have
540 			 * been done. All we do here is make sure that the
541 			 * COPY relocation is not in a shared library. They
542 			 * are allowed only in executable files.
543 			 */
544 			if (!obj->mainprog) {
545 				_rtld_error("%s: Unexpected R_MIPS_COPY "
546 				    "relocation in shared library", obj->path);
547 				return (-1);
548 			}
549 			break;
550 
551 #ifdef __mips_n64
552 		case R_TYPE(TLS_DTPMOD64):
553 #else
554 		case R_TYPE(TLS_DTPMOD32):
555 #endif
556 		{
557 
558 			const size_t rlen = sizeof(Elf_Addr);
559 			Elf_Addr old = load_ptr(where, rlen);
560 			Elf_Addr val = old;
561 
562         		def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
563 	    			lockstate);
564 			if (def == NULL)
565 				return -1;
566 
567 			val += (Elf_Addr)defobj->tlsindex;
568 
569 			store_ptr(where, val, rlen);
570 			dbg("DTPMOD %s in %s %p --> %p in %s",
571 			    obj->strtab + obj->symtab[r_symndx].st_name,
572 			    obj->path, (void *)old, (void*)val, defobj->path);
573 			break;
574 		}
575 
576 #ifdef __mips_n64
577 		case R_TYPE(TLS_DTPREL64):
578 #else
579 		case R_TYPE(TLS_DTPREL32):
580 #endif
581 		{
582 			const size_t rlen = sizeof(Elf_Addr);
583 			Elf_Addr old = load_ptr(where, rlen);
584 			Elf_Addr val = old;
585 
586         		def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
587 	    			lockstate);
588 			if (def == NULL)
589 				return -1;
590 
591 			if (!defobj->tls_done && !allocate_tls_offset(obj))
592 				return -1;
593 
594 			val += (Elf_Addr)def->st_value - TLS_DTP_OFFSET;
595 			store_ptr(where, val, rlen);
596 
597 			dbg("DTPREL %s in %s %p --> %p in %s",
598 			    obj->strtab + obj->symtab[r_symndx].st_name,
599 			    obj->path, (void*)old, (void *)val, defobj->path);
600 			break;
601 		}
602 
603 #ifdef __mips_n64
604 		case R_TYPE(TLS_TPREL64):
605 #else
606 		case R_TYPE(TLS_TPREL32):
607 #endif
608 		{
609 			const size_t rlen = sizeof(Elf_Addr);
610 			Elf_Addr old = load_ptr(where, rlen);
611 			Elf_Addr val = old;
612 
613         		def = find_symdef(r_symndx, obj, &defobj, flags, NULL,
614 	    			lockstate);
615 
616 			if (def == NULL)
617 				return -1;
618 
619 			if (!defobj->tls_done && !allocate_tls_offset(obj))
620 				return -1;
621 
622 			val += (Elf_Addr)(def->st_value + defobj->tlsoffset
623 			    - TLS_TP_OFFSET - TLS_TCB_SIZE);
624 			store_ptr(where, val, rlen);
625 
626 			dbg("TPREL %s in %s %p --> %p in %s",
627 			    obj->strtab + obj->symtab[r_symndx].st_name,
628 			    obj->path, (void*)old, (void *)val, defobj->path);
629 			break;
630 		}
631 
632 
633 
634 		default:
635 			dbg("sym = %lu, type = %lu, offset = %p, "
636 			    "contents = %p, symbol = %s",
637 			    (u_long)r_symndx, (u_long)ELF_R_TYPE(rel->r_info),
638 			    (void *)rel->r_offset,
639 			    (void *)load_ptr(where, sizeof(Elf_Sword)),
640 			    obj->strtab + obj->symtab[r_symndx].st_name);
641 			_rtld_error("%s: Unsupported relocation type %ld "
642 			    "in non-PLT relocations",
643 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
644 			return -1;
645 		}
646 	}
647 
648 	return 0;
649 }
650 
651 /*
652  *  Process the PLT relocations.
653  */
654 int
655 reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
656 {
657 	const Elf_Rel *rellim;
658 	const Elf_Rel *rel;
659 
660 	rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
661 	for (rel = obj->pltrel; rel < rellim; rel++) {
662 		Elf_Addr *where;
663 
664 		switch (ELF_R_TYPE(rel->r_info)) {
665 		case R_MIPS_JUMP_SLOT:
666 			where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
667 			*where += (Elf_Addr )obj->relocbase;
668 			break;
669 		default:
670 			_rtld_error("Unknown relocation type %u in PLT",
671 			    (unsigned int)ELF_R_TYPE(rel->r_info));
672 			return (-1);
673 		}
674 	}
675 
676 	return (0);
677 }
678 
679 /*
680  * LD_BIND_NOW was set - force relocation for all jump slots
681  */
682 int
683 reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
684 {
685 	const Obj_Entry *defobj;
686 	const Elf_Rel *rellim;
687 	const Elf_Rel *rel;
688 	const Elf_Sym *def;
689 
690 	rellim = (const Elf_Rel *)((const char *)obj->pltrel + obj->pltrelsize);
691 	for (rel = obj->pltrel; rel < rellim; rel++) {
692 		Elf_Addr *where;
693 
694 		switch (ELF_R_TYPE(rel->r_info)) {
695 		case R_MIPS_JUMP_SLOT:
696 			def = find_symdef(ELF_R_SYM(rel->r_info), obj,
697 			    &defobj, SYMLOOK_IN_PLT | flags, NULL, lockstate);
698 			if (def == NULL) {
699 				dbg("reloc_jmpslots: sym not found");
700 				return (-1);
701 			}
702 
703 			where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
704 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
705 			break;
706 		default:
707 			_rtld_error("Unknown relocation type %u in PLT",
708 			    (unsigned int)ELF_R_TYPE(rel->r_info));
709 			return (-1);
710 		}
711 	}
712 
713 	return (0);
714 }
715 
716 int
717 reloc_iresolve(Obj_Entry *obj __unused,
718     struct Struct_RtldLockState *lockstate __unused)
719 {
720 
721 	/* XXX not implemented */
722 	return (0);
723 }
724 
725 int
726 reloc_iresolve_nonplt(Obj_Entry *obj __unused,
727     struct Struct_RtldLockState *lockstate __unused)
728 {
729 
730 	/* XXX not implemented */
731 	return (0);
732 }
733 
734 int
735 reloc_gnu_ifunc(Obj_Entry *obj __unused, int flags __unused,
736     struct Struct_RtldLockState *lockstate __unused)
737 {
738 
739 	/* XXX not implemented */
740 	return (0);
741 }
742 
743 Elf_Addr
744 reloc_jmpslot(Elf_Addr *where, Elf_Addr target,
745     const Obj_Entry *defobj __unused, const Obj_Entry *obj __unused,
746     const Elf_Rel *rel)
747 {
748 
749 	assert(ELF_R_TYPE(rel->r_info) == R_MIPS_JUMP_SLOT);
750 
751 	if (*where != target && !ld_bind_not)
752 		*where = target;
753 	return (target);
754 }
755 
756 void
757 ifunc_init(Elf_Auxinfo aux_info[__min_size(AT_COUNT)] __unused)
758 {
759 
760 }
761 
762 void
763 allocate_initial_tls(Obj_Entry *objs)
764 {
765 	char *tls;
766 
767 	/*
768 	 * Fix the size of the static TLS block by using the maximum
769 	 * offset allocated so far and adding a bit for dynamic modules to
770 	 * use.
771 	 */
772 	tls_static_space = tls_last_offset + tls_last_size + RTLD_STATIC_TLS_EXTRA;
773 
774 	tls = (char *) allocate_tls(objs, NULL, TLS_TCB_SIZE, 8);
775 
776 	sysarch(MIPS_SET_TLS, tls);
777 }
778 
779 void *
780 __tls_get_addr(tls_index* ti)
781 {
782 	Elf_Addr **tls;
783 	char *p;
784 
785 	tls = _get_tp();
786 	p = tls_get_addr_common(tls, ti->ti_module, ti->ti_offset +
787 	    TLS_DTP_OFFSET);
788 
789 	return (p);
790 }
791