cc7198b60e827272b2ffd2aa478a26ab7334d315
[kopensolaris-gnu/glibc.git] / sysdeps / mips / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.  MIPS version.
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Kazumoto Kojima <kkojima@info.kanagawa-u.ac.jp>.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #ifndef dl_machine_h
22 #define dl_machine_h
23
24 #define ELF_MACHINE_NAME "MIPS"
25
26 #include <assert.h>
27 #include <entry.h>
28
29 #ifndef ENTRY_POINT
30 #error ENTRY_POINT needs to be defined for MIPS.
31 #endif
32
33 #ifndef _RTLD_PROLOGUE
34 #ifdef __STDC__
35 #define _RTLD_PROLOGUE(entry) "\n\t.globl " #entry \
36                               "\n\t.ent " #entry \
37                               "\n\t" #entry ":\n\t"
38 #else
39 #define _RTLD_PROLOGUE(entry) "\n\t.globl entry\n\t.ent entry\n\t entry:\n\t"
40 #endif
41 #endif
42
43 #ifndef _RTLD_EPILOGUE
44 #ifdef __STDC__
45 #define _RTLD_EPILOGUE(entry) "\t.end " #entry "\n"
46 #else
47 #define _RTLD_EPILOGUE(entry) "\t.end entry\n"
48 #endif
49 #endif
50
51 /* I have no idea what I am doing. */
52 #define ELF_MACHINE_RELOC_NOPLT                 -1
53 #define elf_machine_lookup_noplt_p(type)        (1)
54 #define elf_machine_lookup_noexec_p(type)       (0)
55
56 /* Translate a processor specific dynamic tag to the index
57    in l_info array.  */
58 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
59
60 #if 0
61 /* We may need 64k alignment. */
62 #define ELF_MACHINE_ALIGN_MASK 0xffff
63 #endif
64
65 /*
66  * MIPS libraries are usually linked to a non-zero base address.  We
67  * subtrace the base address from the address where we map the object
68  * to.  This results in more efficient address space usage.
69  */
70 #if 0
71 #define MAP_BASE_ADDR(l) ((l)->l_info[DT_MIPS(BASE_ADDRESS)] ? \
72                           (l)->l_info[DT_MIPS(BASE_ADDRESS)]->d_un.d_ptr : 0)
73 #else
74 #define MAP_BASE_ADDR(l) 0x5ffe0000
75 #endif
76
77 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
78    with the run-time address of the r_debug structure  */
79 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
80 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
81        *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
82        (ElfW(Addr)) (r); \
83    } while (0)
84
85 /* Return nonzero iff E_MACHINE is compatible with the running host.  */
86 static inline int __attribute__ ((unused))
87 elf_machine_matches_host (ElfW(Half) e_machine)
88 {
89   switch (e_machine)
90     {
91     case EM_MIPS:
92     case EM_MIPS_RS4_BE:
93       return 1;
94     default:
95       return 0;
96     }
97 }
98
99 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
100 +   first element of the GOT.  This must be inlined in a function which
101 +   uses global data.  */
102 +static inline ElfW(Addr)
103 +elf_machine_dynamic (void)
104 +{
105 +  register ElfW(Addr) gp asm ("$28");
106 +  return * (ElfW(Addr) *) (gp - 0x7ff0);
107 +}
108 +
109 static inline ElfW(Addr) *
110 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
111 {
112   /* FIXME: the offset of gp from GOT may be system-dependent. */
113   return (ElfW(Addr) *) (gpreg - 0x7ff0);
114 }
115
116 /* Return the run-time address of the _GLOBAL_OFFSET_TABLE_.
117    Must be inlined in a function which uses global data.  */
118 static inline ElfW(Addr) *
119 elf_machine_got (void)
120 {
121   ElfW(Addr) gp;
122
123   __asm__ __volatile__("move %0, $28\n\t" : "=r" (gp));
124   return elf_mips_got_from_gpreg (gp);
125 }
126
127
128 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
129    first element of the GOT.  This must be inlined in a function which
130    uses global data.  */
131 static inline ElfW(Addr)
132 elf_machine_dynamic (void)
133 {
134   register ElfW(Addr) gp asm ("$28");
135   return * (ElfW(Addr) *) (gp - 0x7ff0);
136 }
137
138 /* Return the run-time load address of the shared object.  */
139 static inline ElfW(Addr)
140 elf_machine_load_address (void)
141 {
142   ElfW(Addr) addr;
143 #ifdef __mips64
144   asm ("        .set noreorder\n"
145        "        dla %0, here\n"
146        "        bltzal $0, here\n"
147        "        nop\n"
148        "here:   dsubu %0, $31, %0\n"
149        "        .set reorder\n"
150        :        "=r" (addr)
151        :        /* No inputs */
152        :        "$31");
153 #else
154   asm ("        .set noreorder\n"
155        "        la %0, here\n"
156        "        bltzal $0, here\n"
157        "        nop\n"
158        "here:   subu %0, $31, %0\n"
159        "        .set reorder\n"
160        :        "=r" (addr)
161        :        /* No inputs */
162        :        "$31");
163 #endif
164   return addr;
165 }
166
167 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
168 #define ELF_MIPS_GNU_GOT1_MASK 0x80000000
169
170 /* Relocate GOT. */
171 static inline void
172 elf_machine_got_rel (struct link_map *map, int lazy)
173 {
174   ElfW(Addr) *got;
175   ElfW(Sym) *sym;
176   int i, n;
177   struct link_map **scope;
178   const char *strtab
179     = ((void *) map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
180
181 #define RESOLVE_GOTSYM(sym) \
182     ({ \
183       const ElfW(Sym) *ref = sym; \
184       ElfW(Addr) sym_loadaddr; \
185       sym_loadaddr = _dl_lookup_symbol (strtab + sym->st_name, &ref, scope, \
186                                         map->l_name, ELF_MACHINE_RELOC_NOPLT);\
187       (ref)? sym_loadaddr + ref->st_value: 0; \
188     })
189
190   got = (ElfW(Addr) *) ((void *) map->l_addr
191                         + map->l_info[DT_PLTGOT]->d_un.d_ptr);
192
193   /* got[0] is reserved. got[1] is also reserved for the dynamic object
194      generated by gnu ld. Skip these reserved entries from relocation.  */
195   i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2: 1;
196   n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
197   /* Add the run-time display to all local got entries. */
198   while (i < n)
199     got[i++] += map->l_addr;
200
201   /* Set scope.  */
202   scope = _dl_object_relocation_scope (map);
203
204   /* Handle global got entries. */
205   got += n;
206   sym = (ElfW(Sym) *) ((void *) map->l_addr
207                        + map->l_info[DT_SYMTAB]->d_un.d_ptr);
208   sym += map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
209   i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
210        - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
211
212   while (i--)
213     {
214       if (sym->st_shndx == SHN_UNDEF)
215         {
216           if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC)
217             {
218               if (sym->st_value && lazy)
219                 *got = sym->st_value + map->l_addr;
220               else
221                 *got = RESOLVE_GOTSYM (sym);
222             }
223           else /* if (*got == 0 || *got == QS) */
224             *got = RESOLVE_GOTSYM (sym);
225         }
226       else if (sym->st_shndx == SHN_COMMON)
227         *got = RESOLVE_GOTSYM (sym);
228       else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
229                && *got != sym->st_value
230                && lazy)
231         *got += map->l_addr;
232       else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
233         {
234           if (sym->st_other == 0)
235             *got += map->l_addr;
236         }
237       else
238         *got = RESOLVE_GOTSYM (sym);
239
240       got++;
241       sym++;
242     }
243
244 #undef RESOLVE_GOTSYM
245   *_dl_global_scope_end = NULL;
246
247   return;
248 }
249
250 /* Set up the loaded object described by L so its stub function
251    will jump to the on-demand fixup code in dl-runtime.c.  */
252
253 static inline int
254 elf_machine_runtime_setup (struct link_map *l, int lazy)
255 {
256   ElfW(Addr) *got;
257   extern void _dl_runtime_resolve (ElfW(Word));
258   extern int _dl_mips_gnu_objects;
259
260 #ifdef RTLD_BOOTSTRAP
261     {
262       return lazy;
263     }
264 #endif
265   if (lazy)
266     {
267       /* The GOT entries for functions have not yet been filled in.
268          Their initial contents will arrange when called to put an
269          offset into the .dynsym section in t8, the return address
270          in t7 and then jump to _GLOBAL_OFFSET_TABLE[0].  */
271       got = (ElfW(Addr) *) ((void *) l->l_addr
272                             + l->l_info[DT_PLTGOT]->d_un.d_ptr);
273
274       /* This function will get called to fix up the GOT entry indicated by
275          the register t8, and then jump to the resolved address.  */
276       got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
277
278       /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
279          of got[1] of a gnu object is set to identify gnu objects.
280          Where we can store l for non gnu objects? XXX  */
281       if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
282         got[1] = (ElfW(Addr)) ((unsigned) l | ELF_MIPS_GNU_GOT1_MASK);
283       else
284         _dl_mips_gnu_objects = 0;
285     }
286
287   /* Relocate global offset table.  */
288   elf_machine_got_rel (l, lazy);
289
290   return lazy;
291 }
292
293 /* Get link_map for this object.  */
294 static inline struct link_map *
295 elf_machine_runtime_link_map (ElfW(Addr) gpreg, ElfW(Addr) stub_pc)
296 {
297   extern int _dl_mips_gnu_objects;
298
299   /* got[1] is reserved to keep its link map address for the shared
300      object generated by gnu linker. If all are such object, we can
301      find link map from current GPREG simply. If not so, get link map
302      for callers object containing STUB_PC.  */
303
304   if (_dl_mips_gnu_objects)
305     {
306       ElfW(Addr) *got = elf_mips_got_from_gpreg (gpreg);
307       ElfW(Word) g1;
308
309       g1 = ((ElfW(Word) *) got)[1];
310
311       if ((g1 & ELF_MIPS_GNU_GOT1_MASK) != 0)
312         return (struct link_map *) (g1 & ~ELF_MIPS_GNU_GOT1_MASK);
313     }
314
315     {
316       struct link_map *l = _dl_loaded;
317       struct link_map *ret = 0;
318       ElfW(Addr) candidate = 0;
319
320       while (l)
321         {
322           ElfW(Addr) base = 0;
323           const ElfW(Phdr) *p = l->l_phdr;
324           ElfW(Half) this, nent = l->l_phnum;
325
326           /* Get the base. */
327           for (this = 0; this < nent; this++)
328             if (p[this].p_type == PT_LOAD)
329               {
330                 base = p[this].p_vaddr + l->l_addr;
331                 break;
332               }
333           if (! base)
334             {
335               l = l->l_next;
336               continue;
337             }
338
339           /* Find closest link base addr. */
340           if ((base < stub_pc) && (candidate < base))
341             {
342               candidate = base;
343               ret = l;
344             }
345           l = l->l_next;
346         }
347       if (candidate && ret && (candidate < stub_pc))
348         return ret;
349       else if (!candidate)
350         return _dl_loaded;
351     }
352
353   _dl_signal_error (0, NULL, "cannot find runtime link map");
354   return NULL;
355 }
356
357 /* Mips has no PLT but define elf_machine_relplt to be elf_machine_rel. */
358 #define elf_machine_relplt elf_machine_rel
359
360 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
361    is called from assembler function _dl_runtime_resolve which converts
362    special argument registers t7 ($15) and t8 ($24):
363      t7  address to return to the caller of the function
364      t8  index for this function symbol in .dynsym
365    to usual c arguments.  */
366
367 #ifdef __mips64
368 #define ELF_MACHINE_RUNTIME_TRAMPOLINE                                        \
369 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are            \
370    generated by the gnu linker. */                                            \
371 int _dl_mips_gnu_objects = 1;                                                 \
372                                                                               \
373 /* This is called from assembly stubs below which the compiler can't see.  */ \
374 static ElfW(Addr)                                                             \
375 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr))         \
376                   __attribute__ ((unused));                                   \
377                                                                               \
378 static ElfW(Addr)                                                             \
379 __dl_runtime_resolve (ElfW(Word) sym_index,                                   \
380                       ElfW(Word) return_address,                              \
381                       ElfW(Addr) old_gpreg,                                   \
382                       ElfW(Addr) stub_pc)                                     \
383 {                                                                             \
384   struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc);     \
385   const ElfW(Sym) *const symtab                                               \
386     = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);     \
387   const char *strtab                                                          \
388     = (void *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);                \
389   const ElfW(Addr) *got                                                       \
390     = (const ElfW(Addr) *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);    \
391   const ElfW(Word) local_gotno                                                \
392     = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;        \
393   const ElfW(Word) gotsym                                                     \
394     = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;             \
395   const ElfW(Sym) *definer;                                                   \
396   ElfW(Addr) loadbase;                                                        \
397   ElfW(Addr) funcaddr;                                                        \
398   struct link_map **scope;                                                    \
399                                                                               \
400   /* Look up the symbol's run-time value.  */                                 \
401   scope = _dl_object_relocation_scope (l);                                    \
402   definer = &symtab[sym_index];                                               \
403                                                                               \
404   loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer,          \
405                                 scope, l->l_name, ELF_MACHINE_RELOC_NOPLT);   \
406                                                                               \
407   *_dl_global_scope_end = NULL;                                               \
408                                                                               \
409   /* Apply the relocation with that value.  */                                \
410   funcaddr = loadbase + definer->st_value;                                    \
411   *(got + local_gotno + sym_index - gotsym) = funcaddr;                       \
412                                                                               \
413   return funcaddr;                                                            \
414 }                                                                             \
415                                                                               \
416 asm ("\n                                                                      \
417         .text\n                                                               \
418         .align  3\n                                                           \
419         .globl  _dl_runtime_resolve\n                                         \
420         .type   _dl_runtime_resolve,@function\n                               \
421         .ent    _dl_runtime_resolve\n                                         \
422 _dl_runtime_resolve:\n                                                        \
423         .set noreorder\n                                                      \
424         # Save old GP to $3.\n                                                \
425         move    $3,$28\n                                                      \
426         # Modify t9 ($25) so as to point .cpload instruction.\n               \
427         daddu   $25,2*8\n                                                     \
428         # Compute GP.\n                                                       \
429         .cpload $25\n                                                         \
430         .set reorder\n                                                        \
431         # Save slot call pc.\n                                                \
432         move    $2, $31\n                                                     \
433         # Save arguments and sp value in stack.\n                             \
434         dsubu   $29, 10*8\n                                                   \
435         .cprestore 8*8\n                                                      \
436         sd      $15, 9*8($29)\n                                               \
437         sd      $4, 3*8($29)\n                                                \
438         sd      $5, 4*8($29)\n                                                \
439         sd      $6, 5*8($29)\n                                                \
440         sd      $7, 6*8($29)\n                                                \
441         sd      $16, 7*8($29)\n                                               \
442         move    $16, $29\n                                                    \
443         move    $4, $24\n                                                     \
444         move    $5, $15\n                                                     \
445         move    $6, $3\n                                                      \
446         move    $7, $2\n                                                      \
447         jal     __dl_runtime_resolve\n                                        \
448         move    $29, $16\n                                                    \
449         ld      $31, 9*8($29)\n                                               \
450         ld      $4, 3*8($29)\n                                                \
451         ld      $5, 4*8($29)\n                                                \
452         ld      $6, 5*8($29)\n                                                \
453         ld      $7, 6*8($29)\n                                                \
454         ld      $16, 7*8($29)\n                                               \
455         daddu   $29, 10*8\n                                                   \
456         move    $25, $2\n                                                     \
457         jr      $25\n                                                         \
458         .end    _dl_runtime_resolve\n                                         \
459 ");
460 #else
461 #define ELF_MACHINE_RUNTIME_TRAMPOLINE                                        \
462 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are            \
463    generated by the gnu linker. */                                            \
464 int _dl_mips_gnu_objects = 1;                                                 \
465                                                                               \
466 /* This is called from assembly stubs below which the compiler can't see.  */ \
467 static ElfW(Addr)                                                             \
468 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr))         \
469                   __attribute__ ((unused));                                   \
470                                                                               \
471 static ElfW(Addr)                                                             \
472 __dl_runtime_resolve (ElfW(Word) sym_index,                                   \
473                       ElfW(Word) return_address,                              \
474                       ElfW(Addr) old_gpreg,                                   \
475                       ElfW(Addr) stub_pc)                                     \
476 {                                                                             \
477   struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc);     \
478   const ElfW(Sym) *const symtab                                               \
479     = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);     \
480   const char *strtab                                                          \
481     = (void *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);                \
482   const ElfW(Addr) *got                                                       \
483     = (const ElfW(Addr) *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);    \
484   const ElfW(Word) local_gotno                                                \
485     = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;        \
486   const ElfW(Word) gotsym                                                     \
487     = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;             \
488   const ElfW(Sym) *definer;                                                   \
489   ElfW(Addr) loadbase;                                                        \
490   ElfW(Addr) funcaddr;                                                        \
491   struct link_map **scope;                                                    \
492                                                                               \
493   /* Look up the symbol's run-time value.  */                                 \
494   scope = _dl_object_relocation_scope (l);                                    \
495   definer = &symtab[sym_index];                                               \
496                                                                               \
497   loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer,          \
498                                 scope, l->l_name, ELF_MACHINE_RELOC_NOPLT);   \
499                                                                               \
500   *_dl_global_scope_end = NULL;                                               \
501                                                                               \
502   /* Apply the relocation with that value.  */                                \
503   funcaddr = loadbase + definer->st_value;                                    \
504   *(got + local_gotno + sym_index - gotsym) = funcaddr;                       \
505                                                                               \
506   return funcaddr;                                                            \
507 }                                                                             \
508                                                                               \
509 asm ("\n                                                                      \
510         .text\n                                                               \
511         .align  2\n                                                           \
512         .globl  _dl_runtime_resolve\n                                         \
513         .type   _dl_runtime_resolve,@function\n                               \
514         .ent    _dl_runtime_resolve\n                                         \
515 _dl_runtime_resolve:\n                                                        \
516         .set noreorder\n                                                      \
517         # Save slot call pc.\n                                                \
518         move    $3, $31\n                                                     \
519         # Modify t9 ($25) so as to point .cpload instruction.\n               \
520         addu    $25,8\n                                                       \
521         # Compute GP.\n                                                       \
522         .cpload $25\n                                                         \
523         .set reorder\n                                                        \
524         # Save slot call pc.\n                                                \
525         move    $2, $31\n                                                     \
526         # Save arguments and sp value in stack.\n                             \
527         subu    $29, 40\n                                                     \
528         .cprestore 32\n                                                       \
529         sw      $15, 36($29)\n                                                \
530         sw      $4, 12($29)\n                                                 \
531         sw      $5, 16($29)\n                                                 \
532         sw      $6, 20($29)\n                                                 \
533         sw      $7, 24($29)\n                                                 \
534         sw      $16, 28($29)\n                                                \
535         move    $16, $29\n                                                    \
536         move    $4, $24\n                                                     \
537         move    $5, $15\n                                                     \
538         move    $6, $3\n                                                      \
539         move    $7, $2\n                                                      \
540         jal     __dl_runtime_resolve\n                                        \
541         move    $29, $16\n                                                    \
542         lw      $31, 36($29)\n                                                \
543         lw      $4, 12($29)\n                                                 \
544         lw      $5, 16($29)\n                                                 \
545         lw      $6, 20($29)\n                                                 \
546         lw      $7, 24($29)\n                                                 \
547         lw      $16, 28($29)\n                                                \
548         addu    $29, 40\n                                                     \
549         move    $25, $2\n                                                     \
550         jr      $25\n                                                         \
551         .end    _dl_runtime_resolve\n                                         \
552 ");
553 #endif
554
555 /* Mask identifying addresses reserved for the user program,
556    where the dynamic linker should not map anything.  */
557 #define ELF_MACHINE_USER_ADDRESS_MASK   0x80000000UL
558
559
560
561 /* Initial entry point code for the dynamic linker.
562    The C function `_dl_start' is the real entry point;
563    its return value is the user program's entry point.
564    Note how we have to be careful about two things:
565
566    1) That we allocate a minimal stack of 24 bytes for
567       every function call, the MIPS ABI states that even
568       if all arguments are passed in registers the procedure
569       called can use the 16 byte area pointed to by $sp
570       when it is called to store away the arguments passed
571       to it.
572
573    2) That under Linux the entry is named __start
574       and not just plain _start.  */
575
576 #ifdef __mips64
577 #define RTLD_START asm ("\
578         .text\n\
579         .align  3\n"\
580 _RTLD_PROLOGUE (ENTRY_POINT)\
581 "       .globl _dl_start_user\n\
582         .set noreorder\n\
583         bltzal $0, 0f\n\
584         nop\n\
585 0:      .cpload $31\n\
586         .set reorder\n\
587         # i386 ABI book says that the first entry of GOT holds\n\
588         # the address of the dynamic structure. Though MIPS ABI\n\
589         # doesn't say nothing about this, I emulate this here.\n\
590         dla $4, _DYNAMIC\n\
591         sd $4, -0x7ff0($28)\n\
592         move $4, $29\n\
593         jal _dl_start\n\
594         # Get the value of label '_dl_start_user' in t9 ($25).\n\
595         dla $25, _dl_start_user\n\
596 _dl_start_user:\n\
597         .set noreorder\n\
598         .cpload $25\n\
599         .set reorder\n\
600         move $16, $28\n\
601         # Save the user entry point address in saved register.\n\
602         move $17, $2\n\
603         # See if we were run as a command with the executable file\n\
604         # name as an extra leading argument.\n\
605         ld $2, _dl_skip_args\n\
606         beq $2, $0, 1f\n\
607         # Load the original argument count.\n\
608         ld $4, 0($29)\n\
609         # Subtract _dl_skip_args from it.\n\
610         dsubu $4, $2\n\
611         # Adjust the stack pointer to skip _dl_skip_args words.\n\
612         dsll $2,2\n\
613         daddu $29, $2\n\
614         # Save back the modified argument count.\n\
615         sd $4, 0($29)\n\
616         # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
617 1:      dla $2, _dl_default_scope\n\
618         ld $4, 2*8($2)\n\
619         # Call _dl_init_next to return the address of an initializer\n\
620         # function to run.\n\
621         jal _dl_init_next\n\
622         move $28, $16\n\
623         # Check for zero return,  when out of initializers.\n\
624         beq $2, $0, 2f\n\
625         # Call the shared object initializer function.\n\
626         move $25, $2\n\
627         ld $4, 0($29)\n\
628         ld $5, 1*8($29)\n\
629         ld $6, 2*8($29)\n\
630         ld $7, 3*8($29)\n\
631         jalr $25\n\
632         move $28, $16\n\
633         # Loop to call _dl_init_next for the next initializer.\n\
634         b 1b\n\
635         # Pass our finalizer function to the user in ra.\n\
636 2:      dla $31, _dl_fini\n\
637         # Jump to the user entry point.\n\
638         move $25, $17\n\
639         ld $4, 0($29)\n\
640         ld $5, 1*8($29)\n\
641         ld $6, 2*8$29)\n\
642         ld $7, 3*8($29)\n\
643         jr $25\n"\
644 _RTLD_EPILOGUE(ENTRY_POINT) \
645 );
646
647 #else
648 #define RTLD_START asm ("\
649         .text\n"\
650 _RTLD_PROLOGUE(ENTRY_POINT)\
651 "       .globl _dl_start_user\n\
652         .set noreorder\n\
653         bltzal $0, 0f\n\
654         nop\n\
655 0:      .cpload $31\n\
656         .set reorder\n\
657         # i386 ABI book says that the first entry of GOT holds\n\
658         # the address of the dynamic structure. Though MIPS ABI\n\
659         # doesn't say nothing about this, I emulate this here.\n\
660         la $4, _DYNAMIC\n\
661         sw $4, -0x7ff0($28)\n\
662         move $4, $29\n\
663         subu $29, 16\n\
664         jal _dl_start\n\
665         addiu $29, 16\n\
666         # Get the value of label '_dl_start_user' in t9 ($25).\n\
667         la $25, _dl_start_user\n\
668 _dl_start_user:\n\
669         .set noreorder\n\
670         .cpload $25\n\
671         .set reorder\n\
672         move $16, $28\n\
673         # Save the user entry point address in saved register.\n\
674         move $17, $2\n\
675         # See if we were run as a command with the executable file\n\
676         # name as an extra leading argument.\n\
677         lw $2, _dl_skip_args\n\
678         beq $2, $0, 1f\n\
679         # Load the original argument count.\n\
680         lw $4, 0($29)\n\
681         # Subtract _dl_skip_args from it.\n\
682         subu $4, $2\n\
683         # Adjust the stack pointer to skip _dl_skip_args words.\n\
684         sll $2,2\n\
685         addu $29, $2\n\
686         # Save back the modified argument count.\n\
687         sw $4, 0($29)\n\
688         # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
689 1:      la $2, _dl_default_scope\n\
690         lw $4, 8($2)\n\
691         # Call _dl_init_next to return the address of an initializer\n\
692         # function to run.\n\
693         subu $29, 16\n\
694         jal _dl_init_next\n\
695         addiu $29, 16\n\
696         move $28, $16\n\
697         # Check for zero return,  when out of initializers.\n\
698         beq $2, $0, 2f\n\
699         # Call the shared object initializer function.\n\
700         move $25, $2\n\
701         lw $4, 0($29)\n\
702         lw $5, 4($29)\n\
703         lw $6, 8($29)\n\
704         lw $7, 12($29)\n\
705         jalr $25\n\
706         move $28, $16\n\
707         # Loop to call _dl_init_next for the next initializer.\n\
708         b 1b\n\
709 2:      # Clear the startup flag.  Assumes 32 bit ints.\n\
710         sw $0, _dl_starting_up\n\
711         # Pass our finalizer function to the user in ra.\n\
712         la $31, _dl_fini\n\
713         # Jump to the user entry point.\n\
714         move $25, $17\n\
715         lw $4, 0($29)\n\
716         lw $5, 4($29)\n\
717         lw $6, 8($29)\n\
718         lw $7, 12($29)\n\
719         jr $25\n"\
720 _RTLD_EPILOGUE(ENTRY_POINT)\
721 ");
722 #endif
723
724 /* The MIPS never uses Elfxx_Rela relocations.  */
725 #define ELF_MACHINE_NO_RELA 1
726
727 #endif /* !dl_machine_h */
728
729 #ifdef RESOLVE
730
731 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
732    MAP is the object containing the reloc.  */
733
734 static inline void
735 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
736                  const ElfW(Sym) *sym, const struct r_found_version *version)
737 {
738   ElfW(Addr) *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
739   ElfW(Addr) loadbase;
740   ElfW(Addr) undo __attribute__ ((unused));
741
742   switch (ELFW(R_TYPE) (reloc->r_info))
743     {
744     case R_MIPS_REL32:
745       {
746         ElfW(Addr) undo = 0;
747
748         if (ELFW(ST_BIND) (sym->st_info) == STB_LOCAL
749             && (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION
750                 || ELFW(ST_TYPE) (sym->st_info) == STT_NOTYPE))
751           {
752             *reloc_addr += map->l_addr;
753             break;
754           }
755 #ifndef RTLD_BOOTSTRAP
756         /* This is defined in rtld.c, but nowhere in the static libc.a;
757            make the reference weak so static programs can still link.  This
758            declaration cannot be done when compiling rtld.c (i.e.  #ifdef
759            RTLD_BOOTSTRAP) because rtld.c contains the common defn for
760            _dl_rtld_map, which is incompatible with a weak decl in the same
761            file.  */
762         weak_extern (_dl_rtld_map);
763         if (map == &_dl_rtld_map)
764           /* Undo the relocation done here during bootstrapping.  Now we will
765              relocate it anew, possibly using a binding found in the user
766              program or a loaded library rather than the dynamic linker's
767              built-in definitions used while loading those libraries.  */
768           undo = map->l_addr + sym->st_value;
769 #endif
770           loadbase = RESOLVE (&sym, version, 0);
771           *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
772         }
773       break;
774     case R_MIPS_NONE:           /* Alright, Wilbur.  */
775       break;
776     default:
777       assert (! "unexpected dynamic reloc type");
778       break;
779     }
780 }
781
782 static inline void
783 elf_machine_lazy_rel (struct link_map *map, const ElfW(Rel) *reloc)
784 {
785   /* Do nothing.  */
786 }
787
788 #endif /* RESOLVE */