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