2002-09-18 Roland McGrath <roland@redhat.com>
[platform/upstream/linaro-glibc.git] / sysdeps / mips / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.  MIPS version.
2    Copyright (C) 1996-2001, 2002 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 Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the 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    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /*  FIXME: Profiling of shared libraries is not implemented yet.  */
22 #ifndef dl_machine_h
23 #define dl_machine_h
24
25 #define ELF_MACHINE_NAME "MIPS"
26
27 #define ELF_MACHINE_NO_PLT
28
29 #include <entry.h>
30
31 #ifndef ENTRY_POINT
32 #error ENTRY_POINT needs to be defined for MIPS.
33 #endif
34
35 /* The offset of gp from GOT might be system-dependent.  It's set by
36    ld.  The same value is also */
37 #define OFFSET_GP_GOT 0x7ff0
38
39 #ifndef _RTLD_PROLOGUE
40 # define _RTLD_PROLOGUE(entry)                                          \
41         ".globl\t" __STRING(entry) "\n\t"                               \
42         ".ent\t" __STRING(entry) "\n\t"                                 \
43         ".type\t" __STRING(entry) ", @function\n"                       \
44         __STRING(entry) ":\n\t"
45 #endif
46
47 #ifndef _RTLD_EPILOGUE
48 # define _RTLD_EPILOGUE(entry)                                          \
49         ".end\t" __STRING(entry) "\n\t"                                 \
50         ".size\t" __STRING(entry) ", . - " __STRING(entry) "\n\t"
51 #endif
52
53 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.
54    This makes no sense on MIPS but we have to define this to R_MIPS_REL32
55    to avoid the asserts in dl-lookup.c from blowing.  */
56 #define ELF_MACHINE_JMP_SLOT                    R_MIPS_REL32
57 #define elf_machine_type_class(type)            ELF_RTYPE_CLASS_PLT
58
59 /* Translate a processor specific dynamic tag to the index
60    in l_info array.  */
61 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
62
63 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
64    with the run-time address of the r_debug structure  */
65 #define ELF_MACHINE_DEBUG_SETUP(l,r) \
66 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
67        *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
68        (ElfW(Addr)) (r); \
69    } while (0)
70
71 /* Return nonzero iff ELF header is compatible with the running host.  */
72 static inline int __attribute_used__
73 elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
74 {
75   switch (ehdr->e_machine)
76     {
77     case EM_MIPS:
78     case EM_MIPS_RS3_LE:
79       return 1;
80     default:
81       return 0;
82     }
83 }
84
85 static inline ElfW(Addr) *
86 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
87 {
88   /* FIXME: the offset of gp from GOT may be system-dependent. */
89   return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
90 }
91
92 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
93    first element of the GOT.  This must be inlined in a function which
94    uses global data.  */
95 static inline ElfW(Addr)
96 elf_machine_dynamic (void)
97 {
98   register ElfW(Addr) gp __asm__ ("$28");
99   return *elf_mips_got_from_gpreg (gp);
100 }
101
102
103 /* Return the run-time load address of the shared object.  */
104 static inline ElfW(Addr)
105 elf_machine_load_address (void)
106 {
107   ElfW(Addr) addr;
108   asm ("        .set noreorder\n"
109        "        la %0, here\n"
110        "        bltzal $0, here\n"
111        "        nop\n"
112        "here:   subu %0, $31, %0\n"
113        "        .set reorder\n"
114        :        "=r" (addr)
115        :        /* No inputs */
116        :        "$31");
117   return addr;
118 }
119
120 /* The MSB of got[1] of a gnu object is set to identify gnu objects.  */
121 #define ELF_MIPS_GNU_GOT1_MASK  0x80000000
122
123 /* We can't rely on elf_machine_got_rel because _dl_object_relocation_scope
124    fiddles with global data.  */
125 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info)                     \
126 do {                                                                    \
127   struct link_map *map = &bootstrap_map;                                \
128   ElfW(Sym) *sym;                                                       \
129   ElfW(Addr) *got;                                                      \
130   int i, n;                                                             \
131                                                                         \
132   got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);                  \
133                                                                         \
134   if (__builtin_expect (map->l_addr == 0, 1))                           \
135     break;                                                              \
136                                                                         \
137   /* got[0] is reserved. got[1] is also reserved for the dynamic object \
138      generated by gnu ld. Skip these reserved entries from              \
139      relocation.  */                                                    \
140   i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;                         \
141   n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;                   \
142                                                                         \
143   /* Add the run-time displacement to all local got entries. */         \
144   while (i < n)                                                         \
145     got[i++] += map->l_addr;                                            \
146                                                                         \
147   /* Handle global got entries. */                                      \
148   got += n;                                                             \
149   sym = (ElfW(Sym) *) D_PTR(map, l_info[DT_SYMTAB])                     \
150        + map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;                     \
151   i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val                      \
152        - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);                    \
153                                                                         \
154   while (i--)                                                           \
155     {                                                                   \
156       if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_COMMON)    \
157         *got = map->l_addr + sym->st_value;                             \
158       else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC                 \
159                && *got != sym->st_value)                                \
160         *got += map->l_addr;                                            \
161       else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)             \
162         {                                                               \
163           if (sym->st_other == 0)                                       \
164             *got += map->l_addr;                                        \
165         }                                                               \
166       else                                                              \
167         *got = map->l_addr + sym->st_value;                             \
168                                                                         \
169       got++;                                                            \
170       sym++;                                                            \
171     }                                                                   \
172 } while(0)
173
174
175 /* Get link map for callers object containing STUB_PC.  */
176 static inline struct link_map *
177 elf_machine_runtime_link_map (ElfW(Addr) gpreg, ElfW(Addr) stub_pc)
178 {
179   extern int _dl_mips_gnu_objects;
180
181   /* got[1] is reserved to keep its link map address for the shared
182      object generated by the gnu linker.  If all are such objects, we
183      can find the link map from current GPREG simply.  If not so, get
184      the link map for caller's object containing STUB_PC.  */
185
186   if (_dl_mips_gnu_objects)
187     {
188       ElfW(Addr) *got = elf_mips_got_from_gpreg (gpreg);
189       ElfW(Word) g1;
190
191       g1 = ((ElfW(Word) *) got)[1];
192
193       if ((g1 & ELF_MIPS_GNU_GOT1_MASK) != 0)
194         {
195           struct link_map *l =
196             (struct link_map *) (g1 & ~ELF_MIPS_GNU_GOT1_MASK);
197           ElfW(Addr) base, limit;
198           const ElfW(Phdr) *p = l->l_phdr;
199           ElfW(Half) this, nent = l->l_phnum;
200
201           /* For the common case of a stub being called from the containing
202              object, STUB_PC will point to somewhere within the object that
203              is described by the link map fetched via got[1].  Otherwise we
204              have to scan all maps.  */
205           for (this = 0; this < nent; this++)
206             {
207               if (p[this].p_type == PT_LOAD)
208                 {
209                   base = p[this].p_vaddr + l->l_addr;
210                   limit = base + p[this].p_memsz;
211                   if (stub_pc >= base && stub_pc < limit)
212                     return l;
213                 }
214             }
215         }
216     }
217
218     {
219       struct link_map *l = GL(dl_loaded);
220
221       while (l)
222         {
223           ElfW(Addr) base, limit;
224           const ElfW(Phdr) *p = l->l_phdr;
225           ElfW(Half) this, nent = l->l_phnum;
226
227           for (this = 0; this < nent; ++this)
228             {
229               if (p[this].p_type == PT_LOAD)
230                 {
231                   base = p[this].p_vaddr + l->l_addr;
232                   limit = base + p[this].p_memsz;
233                   if (stub_pc >= base && stub_pc < limit)
234                     return l;
235                 }
236             }
237           l = l->l_next;
238         }
239     }
240
241   _dl_signal_error (0, NULL, NULL, "cannot find runtime link map");
242   return NULL;
243 }
244
245 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
246    is called from assembler function _dl_runtime_resolve which converts
247    special argument registers t7 ($15) and t8 ($24):
248      t7  address to return to the caller of the function
249      t8  index for this function symbol in .dynsym
250    to usual c arguments.
251
252    Other architectures call fixup from dl-runtime.c in
253    _dl_runtime_resolve.  MIPS instead calls __dl_runtime_resolve.  We
254    have to use our own version because of the way the got section is
255    treated on MIPS (we've also got ELF_MACHINE_PLT defined).  */
256
257 #define ELF_MACHINE_RUNTIME_TRAMPOLINE                                        \
258 /* The flag _dl_mips_gnu_objects is set if all dynamic objects are            \
259    generated by the gnu linker. */                                            \
260 int _dl_mips_gnu_objects = 1;                                                 \
261                                                                               \
262 /* This is called from assembly stubs below which the compiler can't see.  */ \
263 static ElfW(Addr)                                                             \
264 __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr), ElfW(Addr))         \
265                   __attribute_used__;                                         \
266                                                                               \
267 static ElfW(Addr)                                                             \
268 __dl_runtime_resolve (ElfW(Word) sym_index,                                   \
269                       ElfW(Word) return_address,                              \
270                       ElfW(Addr) old_gpreg,                                   \
271                       ElfW(Addr) stub_pc)                                     \
272 {                                                                             \
273   struct link_map *l = elf_machine_runtime_link_map (old_gpreg, stub_pc);     \
274   const ElfW(Sym) *const symtab                                               \
275     = (const void *) D_PTR (l, l_info[DT_SYMTAB]);                            \
276   const char *strtab                                                          \
277     = (const void *) D_PTR (l, l_info[DT_STRTAB]);                            \
278   ElfW(Addr) *got                                                             \
279     = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);                            \
280   const ElfW(Word) local_gotno                                                \
281     = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;        \
282   const ElfW(Word) gotsym                                                     \
283     = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;             \
284   const ElfW(Sym) *sym = &symtab[sym_index];                                  \
285   ElfW(Addr) value;                                                           \
286                                                                               \
287   /* FIXME: The symbol versioning stuff is not tested yet.  */                \
288   if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)         \
289     {                                                                         \
290       switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)                       \
291         {                                                                     \
292         default:                                                              \
293           {                                                                   \
294             const ElfW(Half) *vernum =                                        \
295               (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);        \
296             ElfW(Half) ndx = vernum[sym_index & 0x7fff];                      \
297             const struct r_found_version *version = &l->l_versions[ndx];      \
298                                                                               \
299             if (version->hash != 0)                                           \
300               {                                                               \
301                 value = _dl_lookup_versioned_symbol(strtab + sym->st_name, l, \
302                                                     &sym, l->l_scope, version,\
303                                                     ELF_RTYPE_CLASS_PLT, 0);  \
304                 break;                                                        \
305               }                                                               \
306             /* Fall through.  */                                              \
307           }                                                                   \
308         case 0:                                                               \
309           value = _dl_lookup_symbol (strtab + sym->st_name, l, &sym,          \
310                                      l->l_scope, ELF_RTYPE_CLASS_PLT,         \
311                                      DL_LOOKUP_ADD_DEPENDENCY);               \
312         }                                                                     \
313                                                                               \
314       /* Currently value contains the base load address of the object         \
315          that defines sym.  Now add in the symbol offset.  */                 \
316       value = (sym ? value + sym->st_value : 0);                              \
317     }                                                                         \
318   else                                                                        \
319     /* We already found the symbol.  The module (and therefore its load       \
320        address) is also known.  */                                            \
321     value = l->l_addr + sym->st_value;                                        \
322                                                                               \
323   /* Apply the relocation with that value.  */                                \
324   *(got + local_gotno + sym_index - gotsym) = value;                          \
325                                                                               \
326   return value;                                                               \
327 }                                                                             \
328                                                                               \
329 asm ("\n                                                                      \
330         .text\n                                                               \
331         .align  2\n                                                           \
332         .globl  _dl_runtime_resolve\n                                         \
333         .type   _dl_runtime_resolve,@function\n                               \
334         .ent    _dl_runtime_resolve\n                                         \
335 _dl_runtime_resolve:\n                                                        \
336         .frame  $29, 40, $31\n                                                \
337         .set noreorder\n                                                      \
338         # Save GP.\n                                                          \
339         move    $3, $28\n                                                     \
340         # Modify t9 ($25) so as to point .cpload instruction.\n               \
341         addu    $25, 8\n                                                      \
342         # Compute GP.\n                                                       \
343         .cpload $25\n                                                         \
344         .set reorder\n                                                        \
345         # Save slot call pc.\n                                                \
346         move    $2, $31\n                                                     \
347         # Save arguments and sp value in stack.\n                             \
348         subu    $29, 40\n                                                     \
349         .cprestore 32\n                                                       \
350         sw      $15, 36($29)\n                                                \
351         sw      $4, 16($29)\n                                                 \
352         sw      $5, 20($29)\n                                                 \
353         sw      $6, 24($29)\n                                                 \
354         sw      $7, 28($29)\n                                                 \
355         move    $4, $24\n                                                     \
356         move    $5, $15\n                                                     \
357         move    $6, $3\n                                                      \
358         move    $7, $2\n                                                      \
359         jal     __dl_runtime_resolve\n                                        \
360         lw      $31, 36($29)\n                                                \
361         lw      $4, 16($29)\n                                                 \
362         lw      $5, 20($29)\n                                                 \
363         lw      $6, 24($29)\n                                                 \
364         lw      $7, 28($29)\n                                                 \
365         addu    $29, 40\n                                                     \
366         move    $25, $2\n                                                     \
367         jr      $25\n                                                         \
368         .end    _dl_runtime_resolve\n                                         \
369         .previous\n                                                           \
370 ");
371
372 /* Mask identifying addresses reserved for the user program,
373    where the dynamic linker should not map anything.  */
374 #define ELF_MACHINE_USER_ADDRESS_MASK   0x80000000UL
375
376
377
378 /* Initial entry point code for the dynamic linker.
379    The C function `_dl_start' is the real entry point;
380    its return value is the user program's entry point.
381    Note how we have to be careful about two things:
382
383    1) That we allocate a minimal stack of 24 bytes for
384       every function call, the MIPS ABI states that even
385       if all arguments are passed in registers the procedure
386       called can use the 16 byte area pointed to by $sp
387       when it is called to store away the arguments passed
388       to it.
389
390    2) That under Linux the entry is named __start
391       and not just plain _start.  */
392
393 #define RTLD_START asm (\
394         ".text\n"\
395         _RTLD_PROLOGUE(ENTRY_POINT)\
396         ".set noreorder\n\
397         bltzal $0, 0f\n\
398         nop\n\
399 0:      .cpload $31\n\
400         .set reorder\n\
401         # i386 ABI book says that the first entry of GOT holds\n\
402         # the address of the dynamic structure. Though MIPS ABI\n\
403         # doesn't say nothing about this, I emulate this here.\n\
404         la $4, _DYNAMIC\n\
405         # Subtract OFFSET_GP_GOT\n\
406         sw $4, -0x7ff0($28)\n\
407         move $4, $29\n\
408         subu $29, 16\n\
409         \n\
410         la $8, coff\n\
411         bltzal $8, coff\n\
412 coff:   subu $8, $31, $8\n\
413         \n\
414         la $25, _dl_start\n\
415         addu $25, $8\n\
416         jalr $25\n\
417         \n\
418         addiu $29, 16\n\
419         # Get the value of label '_dl_start_user' in t9 ($25).\n\
420         la $25, _dl_start_user\n\
421         .globl _dl_start_user\n\
422 _dl_start_user:\n\
423         .set noreorder\n\
424         .cpload $25\n\
425         .set reorder\n\
426         move $16, $28\n\
427         # Save the user entry point address in a saved register.\n\
428         move $17, $2\n\
429         # Store the highest stack address\n\
430         sw $29, __libc_stack_end\n\
431         # See if we were run as a command with the executable file\n\
432         # name as an extra leading argument.\n\
433         lw $2, _dl_skip_args\n\
434         beq $2, $0, 1f\n\
435         # Load the original argument count.\n\
436         lw $4, 0($29)\n\
437         # Subtract _dl_skip_args from it.\n\
438         subu $4, $2\n\
439         # Adjust the stack pointer to skip _dl_skip_args words.\n\
440         sll $2, 2\n\
441         addu $29, $2\n\
442         # Save back the modified argument count.\n\
443         sw $4, 0($29)\n\
444 1:      # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
445         lw $4, _rtld_local\n\
446         lw $5, 0($29)\n\
447         la $6, 4($29)\n\
448         sll $7, $5, 2\n\
449         addu $7, $7, $6\n\
450         addu $7, $7, 4\n\
451         subu $29, 16\n\
452         # Call the function to run the initializers.\n\
453         jal _dl_init_internal\n\
454         addiu $29, 16\n\
455         # Pass our finalizer function to the user in $2 as per ELF ABI.\n\
456         la $2, _dl_fini\n\
457         # Jump to the user entry point.\n\
458         move $25, $17\n\
459         jr $25\n\t"\
460         _RTLD_EPILOGUE(ENTRY_POINT)\
461         ".previous"\
462 );
463
464 /* The MIPS never uses Elfxx_Rela relocations.  */
465 #define ELF_MACHINE_NO_RELA 1
466
467 #endif /* !dl_machine_h */
468
469 #ifdef RESOLVE
470
471 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
472    MAP is the object containing the reloc.  */
473
474 static inline void
475 elf_machine_rel (struct link_map *map, const ElfW(Rel) *reloc,
476                  const ElfW(Sym) *sym, const struct r_found_version *version,
477                  ElfW(Addr) *const reloc_addr)
478 {
479   const unsigned long int r_type = ELFW(R_TYPE) (reloc->r_info);
480
481 #if !defined RTLD_BOOTSTRAP && !defined SHARED
482   /* This is defined in rtld.c, but nowhere in the static libc.a;
483      make the reference weak so static programs can still link.  This
484      declaration cannot be done when compiling rtld.c (i.e.  #ifdef
485      RTLD_BOOTSTRAP) because rtld.c contains the common defn for
486      _dl_rtld_map, which is incompatible with a weak decl in the same
487      file.  */
488   weak_extern (GL(dl_rtld_map));
489 #endif
490
491   switch (r_type)
492     {
493     case R_MIPS_REL32:
494       {
495         int symidx = ELFW(R_SYM) (reloc->r_info);
496
497         if (symidx)
498           {
499             const ElfW(Word) gotsym
500               = (const ElfW(Word)) map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
501
502             if (symidx < gotsym)
503               {
504 #ifndef RTLD_BOOTSTRAP
505                 if (map != &GL(dl_rtld_map))
506 #endif
507                   *reloc_addr += sym->st_value + map->l_addr;
508               }
509             else
510               {
511 #ifndef RTLD_BOOTSTRAP
512                 const ElfW(Addr) *got
513                   = (const ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
514                 const ElfW(Word) local_gotno
515                   = (const ElfW(Word))
516                     map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
517
518                 *reloc_addr += got[symidx + local_gotno - gotsym];
519 #endif
520               }
521           }
522         else
523 #ifndef RTLD_BOOTSTRAP
524           if (map != &GL(dl_rtld_map))
525 #endif
526             *reloc_addr += map->l_addr;
527       }
528       break;
529     case R_MIPS_NONE:           /* Alright, Wilbur.  */
530       break;
531     default:
532       _dl_reloc_bad_type (map, r_type, 0);
533       break;
534     }
535 }
536
537 static inline void
538 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc,
539                           ElfW(Addr) *const reloc_addr)
540 {
541   /* XXX Nothing to do.  There is no relative relocation, right?  */
542 }
543
544 static inline void
545 elf_machine_lazy_rel (struct link_map *map,
546                       ElfW(Addr) l_addr, const ElfW(Rel) *reloc)
547 {
548   /* Do nothing.  */
549 }
550
551 #ifndef RTLD_BOOTSTRAP
552 /* Relocate GOT. */
553 static inline void
554 elf_machine_got_rel (struct link_map *map, int lazy)
555 {
556   ElfW(Addr) *got;
557   ElfW(Sym) *sym;
558   const ElfW(Half) *vernum;
559   int i, n, symidx;
560
561 #define RESOLVE_GOTSYM(sym,vernum,sym_index)                              \
562     ({                                                                    \
563       const ElfW(Sym) *ref = sym;                                         \
564       const struct r_found_version *version                               \
565         = vernum ? &map->l_versions[vernum[sym_index] & 0x7fff] : NULL;   \
566       ElfW(Addr) value;                                                   \
567       value = RESOLVE (&ref, version, R_MIPS_REL32);                      \
568       (ref)? value + ref->st_value: 0;                                    \
569     })
570
571   if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
572     vernum = (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
573   else
574     vernum = NULL;
575
576   got = (ElfW(Addr) *) D_PTR (map, l_info[DT_PLTGOT]);
577
578   n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
579   /* The dynamic linker's local got entries have already been relocated.  */
580   if (map != &GL(dl_rtld_map))
581     {
582       /* got[0] is reserved. got[1] is also reserved for the dynamic object
583          generated by gnu ld. Skip these reserved entries from relocation.  */
584       i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2 : 1;
585
586       /* Add the run-time displacement to all local got entries if
587          needed.  */
588       if (__builtin_expect (map->l_addr != 0, 0))
589         {
590           while (i < n)
591             got[i++] += map->l_addr;
592         }
593     }
594
595   /* Handle global got entries. */
596   got += n;
597   /* Keep track of the symbol index.  */
598   symidx = map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
599   sym = (ElfW(Sym) *) D_PTR (map, l_info[DT_SYMTAB]) + symidx;
600   i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
601        - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
602
603   /* This loop doesn't handle Quickstart.  */
604   while (i--)
605     {
606       if (sym->st_shndx == SHN_UNDEF)
607         {
608           if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
609               && sym->st_value && lazy)
610             *got = sym->st_value + map->l_addr;
611           else
612             *got = RESOLVE_GOTSYM (sym, vernum, symidx);
613         }
614       else if (sym->st_shndx == SHN_COMMON)
615         *got = RESOLVE_GOTSYM (sym, vernum, symidx);
616       else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
617                && *got != sym->st_value
618                && lazy)
619         *got += map->l_addr;
620       else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION)
621         {
622           if (sym->st_other == 0)
623             *got += map->l_addr;
624         }
625       else
626         *got = RESOLVE_GOTSYM (sym, vernum, symidx);
627
628       ++got;
629       ++sym;
630       ++symidx;
631     }
632
633 #undef RESOLVE_GOTSYM
634 }
635 #endif
636
637 /* Set up the loaded object described by L so its stub function
638    will jump to the on-demand fixup code __dl_runtime_resolve.  */
639
640 static inline int
641 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
642 {
643 # ifndef RTLD_BOOTSTRAP
644   ElfW(Addr) *got;
645   extern void _dl_runtime_resolve (ElfW(Word));
646   extern int _dl_mips_gnu_objects;
647
648   if (lazy)
649     {
650       /* The GOT entries for functions have not yet been filled in.
651          Their initial contents will arrange when called to put an
652          offset into the .dynsym section in t8, the return address
653          in t7 and then jump to _GLOBAL_OFFSET_TABLE[0].  */
654       got = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
655
656       /* This function will get called to fix up the GOT entry indicated by
657          the register t8, and then jump to the resolved address.  */
658       got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
659
660       /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
661          of got[1] of a gnu object is set to identify gnu objects.
662          Where we can store l for non gnu objects? XXX  */
663       if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
664         got[1] = (ElfW(Addr)) ((unsigned) l | ELF_MIPS_GNU_GOT1_MASK);
665       else
666         _dl_mips_gnu_objects = 0;
667     }
668
669   /* Relocate global offset table.  */
670   elf_machine_got_rel (l, lazy);
671
672 # endif
673   return lazy;
674 }
675
676 #endif /* RESOLVE */