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