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