Update from main archive 961219
[glibc.git] / sysdeps / mips / dl-machine.h
blob9e80426ee372bccdcad1b003b59789f3bb1a5059
1 /* Machine-dependent ELF dynamic relocation inline functions. MIPS version.
2 Copyright (C) 1996 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>.
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.
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.
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. */
21 #define ELF_MACHINE_NAME "MIPS"
23 #include <assert.h>
24 #include <string.h>
25 #include <link.h>
26 #include <sys/types.h>
27 #include <sys/mman.h>
28 #include <unistd.h>
30 /* Translate a processor specific dynamic tag to the index
31 in l_info array. */
32 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
34 #if 1
35 /* XXX If FLAGS has the MAP_ALIGN bit, we need 64k alignment. */
36 #ifndef MAP_ALIGN
37 #define MAP_ALIGN 0x1000
38 #endif
39 #define ELF_MACHINE_ALIGN_MASK(flags) ((flags & MAP_ALIGN) ? 0xffff : 0)
40 #endif
42 /* If there is a DT_MIPS_RLD_MAP entry in the dynamic section, fill it in
43 with the run-time address of the r_debug structure */
44 #define ELF_MACHINE_SET_DEBUG(l,r) \
45 do { if ((l)->l_info[DT_MIPS (RLD_MAP)]) \
46 *(ElfW(Addr) *)((l)->l_info[DT_MIPS (RLD_MAP)]->d_un.d_ptr) = \
47 (ElfW(Addr)) (r); \
48 } while (0)
50 /* Return nonzero iff E_MACHINE is compatible with the running host. */
51 static inline int
52 elf_machine_matches_host (ElfW(Half) e_machine)
54 switch (e_machine)
56 case EM_MIPS:
57 case EM_MIPS_RS4_BE:
58 return 1;
59 default:
60 return 0;
64 static inline ElfW(Addr) *
65 elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
67 /* FIXME: the offset of gp from GOT may be system-dependent. */
68 return (ElfW(Addr) *) (gpreg - 0x7ff0);
70 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
71 first element of the GOT. This must be inlined in a function which
72 uses global data. */
73 static inline ElfW(Addr)
74 elf_machine_dynamic (void)
76 register ElfW(Addr) gp asm ("$28");
77 return * (ElfW(Addr) *) (gp - 0x7ff0);
81 /* Return the run-time load address of the shared object. */
82 static inline ElfW(Addr)
83 elf_machine_load_address (void)
85 ElfW(Addr) addr;
86 asm (" .set noreorder\n"
87 " la %0, here\n"
88 " bltzal $0, here\n"
89 " nop\n"
90 "here: subu %0, $31, %0\n"
91 " .set reorder\n"
92 : "=r" (addr));
93 return addr;
96 /* The MSB of got[1] of a gnu object is set to identify gnu objects. */
97 #define ELF_MIPS_GNU_GOT1_MASK 0x80000000
99 /* Relocate GOT. */
100 static void
101 elf_machine_got_rel (struct link_map *map)
103 ElfW(Addr) *got;
104 ElfW(Sym) *sym;
105 int i, n;
106 struct link_map **scope;
107 const char *strtab
108 = ((void *) map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
110 ElfW(Addr) resolve (const ElfW(Sym) *sym)
112 const ElfW(Sym) *ref = sym;
113 ElfW(Addr) sym_loadaddr;
114 sym_loadaddr = _dl_lookup_symbol (strtab + sym->st_name, &ref, scope,
115 map->l_name, 0, 1);
116 return (ref)? sym_loadaddr + ref->st_value: 0;
119 got = (ElfW(Addr) *) ((void *) map->l_addr
120 + map->l_info[DT_PLTGOT]->d_un.d_ptr);
122 /* got[0] is reserved. got[1] is also reserved for the dynamic object
123 generated by gnu ld. Skip these reserved entries from relocation. */
124 i = (got[1] & ELF_MIPS_GNU_GOT1_MASK)? 2: 1;
125 n = map->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;
126 /* Add the run-time display to all local got entries. */
127 while (i < n)
128 got[i++] += map->l_addr;
130 /* Set scope. */
131 scope = _dl_object_relocation_scope (map);
133 /* Handle global got entries. */
134 got += n;
135 sym = (ElfW(Sym) *) ((void *) map->l_addr
136 + map->l_info[DT_SYMTAB]->d_un.d_ptr);
137 sym += map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;
138 i = (map->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val
139 - map->l_info[DT_MIPS (GOTSYM)]->d_un.d_val);
141 while (i--)
143 if (sym->st_shndx == SHN_UNDEF)
145 if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC)
147 if (sym->st_value /* && maybe_stub (sym->st_value) */)
148 *got = sym->st_value + map->l_addr;
149 else
150 *got = resolve (sym);
152 else /* if (*got == 0 || *got == QS) */
153 *got = resolve (sym);
155 else if (sym->st_shndx == SHN_COMMON)
156 *got = resolve (sym);
157 else if (ELFW(ST_TYPE) (sym->st_info) == STT_FUNC
158 && *got != sym->st_value
159 /* && maybe_stub (*got) */)
160 *got += map->l_addr;
161 else if (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION
162 && ELFW(ST_BIND) (sym->st_info) == STB_GLOBAL)
164 if (sym->st_other == 0 && sym->st_shndx == SHN_ABS)
165 *got = sym->st_value + map->l_addr; /* only for _gp_disp */
166 /* else SGI stuff ignored */
168 else
169 *got = resolve (sym);
171 got++;
172 sym++;
175 *_dl_global_scope_end = NULL;
177 return;
180 /* The MIPS never uses Elfxx_Rela relocations. */
181 #define ELF_MACHINE_NO_RELA 1
183 /* Set up the loaded object described by L so its stub function
184 will jump to the on-demand fixup code in dl-runtime.c. */
186 static inline void
187 elf_machine_runtime_setup (struct link_map *l, int lazy)
189 ElfW(Addr) *got;
190 extern void _dl_runtime_resolve (ElfW(Word));
192 if (lazy)
194 /* The GOT entries for functions have not yet been filled in.
195 Their initial contents will arrange when called to put an
196 offset into the .dynsym section in t8, the return address
197 in t7 and then jump to _GLOBAL_OFFSET_TABLE[0]. */
198 got = (ElfW(Addr) *) ((void *) l->l_addr
199 + l->l_info[DT_PLTGOT]->d_un.d_ptr);
201 /* This function will get called to fix up the GOT entry indicated by
202 the register t8, and then jump to the resolved address. */
203 got[0] = (ElfW(Addr)) &_dl_runtime_resolve;
205 /* Store l to _GLOBAL_OFFSET_TABLE[1] for gnu object. The MSB
206 of got[1] of a gnu object is set to identify gnu objects.
207 Where we can store l for non gnu objects? XXX */
208 if ((got[1] & ELF_MIPS_GNU_GOT1_MASK) != 0)
209 got[1] = (ElfW(Addr)) ((unsigned) l | ELF_MIPS_GNU_GOT1_MASK);
210 else
211 ; /* Do nothing. */
214 /* Relocate global offset table. */
215 elf_machine_got_rel (l);
218 /* Get link_map for this object. */
219 static inline struct link_map *
220 elf_machine_runtime_link_map (ElfW(Addr) gpreg)
222 ElfW(Addr) *got = elf_mips_got_from_gpreg (gpreg);
223 ElfW(Word) g1;
225 g1 = ((ElfW(Word) *) got)[1];
227 /* got[1] is reserved to keep its link map address for the shared
228 object generated by gnu linker. If not so, we must search GOT
229 in object list slowly. XXX */
230 if ((g1 & ELF_MIPS_GNU_GOT1_MASK) != 0)
231 return (struct link_map *) (g1 & ~ELF_MIPS_GNU_GOT1_MASK);
232 else
234 struct link_map *l = _dl_loaded;
235 while (l)
237 if (got == (ElfW(Addr) *) ((void *) l->l_addr
238 + l->l_info[DT_PLTGOT]->d_un.d_ptr))
239 return l;
240 l = l->l_next;
243 _dl_signal_error (0, NULL, "cannot find runtime link map");
246 /* Mips has no PLT but define elf_machine_relplt to be elf_machine_rel. */
247 #define elf_machine_relplt elf_machine_rel
249 /* Define mips specific runtime resolver. The function __dl_runtime_resolve
250 is called from assembler function _dl_runtime_resolve which converts
251 special argument registers t7 ($15) and t8 ($24):
252 t7 address to return to the caller of the function
253 t8 index for this function symbol in .dynsym
254 to usual c arguments. */
256 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
257 /* This is called from assembly stubs below which the compiler can't see. */ \
258 static ElfW(Addr) __dl_runtime_resolve (ElfW(Word), ElfW(Word), ElfW(Addr)) \
259 __attribute__ ((unused)); \
261 static ElfW(Addr) \
262 __dl_runtime_resolve (ElfW(Word) sym_index,\
263 ElfW(Word) return_address,\
264 ElfW(Addr) old_gpreg)\
266 struct link_map *l = elf_machine_runtime_link_map (old_gpreg);\
267 const ElfW(Sym) *const symtab\
268 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);\
269 const char *strtab\
270 = (void *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);\
271 const ElfW(Addr) *got\
272 = (const ElfW(Addr) *) (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);\
273 const ElfW(Word) local_gotno\
274 = (const ElfW(Word)) l->l_info[DT_MIPS (LOCAL_GOTNO)]->d_un.d_val;\
275 const ElfW(Word) gotsym\
276 = (const ElfW(Word)) l->l_info[DT_MIPS (GOTSYM)]->d_un.d_val;\
277 const ElfW(Sym) *definer;\
278 ElfW(Addr) loadbase;\
279 ElfW(Addr) funcaddr;\
280 struct link_map **scope;\
282 /* Look up the symbol's run-time value. */\
283 scope = _dl_object_relocation_scope (l);\
284 definer = &symtab[sym_index];\
286 loadbase = _dl_lookup_symbol (strtab + definer->st_name, &definer,\
287 scope, l->l_name, 0, 1);\
289 *_dl_global_scope_end = NULL;\
291 /* Apply the relocation with that value. */\
292 funcaddr = loadbase + definer->st_value;\
293 *(got + local_gotno + sym_index - gotsym) = funcaddr;\
295 return funcaddr;\
298 asm ("\n\
299 .text\n\
300 .align 2\n\
301 .globl _dl_runtime_resolve\n\
302 .type _dl_runtime_resolve,@function\n\
303 .ent _dl_runtime_resolve\n\
304 _dl_runtime_resolve:\n\
305 .set noreorder\n\
306 # Save old GP to $3.\n\
307 move $3,$28\n\
308 # Modify t9 ($25) so as to point .cpload instruction.\n\
309 addu $25,8\n\
310 # Compute GP.\n\
311 .cpload $25\n\
312 .set reorder\n\
313 # Save arguments and sp value in stack.\n\
314 subu $29, 40\n\
315 .cprestore 32\n\
316 sw $15, 36($29)\n\
317 sw $4, 12($29)\n\
318 sw $5, 16($29)\n\
319 sw $6, 20($29)\n\
320 sw $7, 24($29)\n\
321 sw $16, 28($29)\n\
322 move $16, $29\n\
323 move $4, $24\n\
324 move $5, $15\n\
325 move $6, $3\n\
326 jal __dl_runtime_resolve\n\
327 move $29, $16\n\
328 lw $31, 36($29)\n\
329 lw $4, 12($29)\n\
330 lw $5, 16($29)\n\
331 lw $6, 20($29)\n\
332 lw $7, 24($29)\n\
333 lw $16, 28($29)\n\
334 addu $29, 40\n\
335 move $25, $2\n\
336 jr $25\n\
337 .end _dl_runtime_resolve\n\
340 /* Mask identifying addresses reserved for the user program,
341 where the dynamic linker should not map anything. */
342 #define ELF_MACHINE_USER_ADDRESS_MASK 0x00000000UL
344 /* Initial entry point code for the dynamic linker.
345 The C function `_dl_start' is the real entry point;
346 its return value is the user program's entry point. */
348 #define RTLD_START asm ("\
349 .text\n\
350 .globl _start\n\
351 .globl _dl_start_user\n\
352 .ent _start\n\
353 _start:\n\
354 .set noreorder\n\
355 bltzal $0, 0f\n\
356 nop\n\
357 0: .cpload $31\n\
358 .set reorder\n\
359 # i386 ABI book says that the first entry of GOT holds\n\
360 # the address of the dynamic structure. Though MIPS ABI\n\
361 # doesn't say nothing about this, I emulate this here.\n\
362 la $4, _DYNAMIC\n\
363 sw $4, -0x7ff0($28)\n\
364 move $4, $29\n\
365 jal _dl_start\n\
366 # Get the value of label '_dl_start_user' in t9 ($25).\n\
367 la $25, _dl_start_user\n\
368 _dl_start_user:\n\
369 .set noreorder\n\
370 .cpload $25\n\
371 .set reorder\n\
372 move $16, $28\n\
373 # Save the user entry point address in saved register.\n\
374 move $17, $2\n\
375 # See if we were run as a command with the executable file\n\
376 # name as an extra leading argument.\n\
377 lw $2, _dl_skip_args\n\
378 beq $2, $0, 1f\n\
379 # Load the original argument count.\n\
380 lw $4, 0($29)\n\
381 # Subtract _dl_skip_args from it.\n\
382 subu $4, $2\n\
383 # Adjust the stack pointer to skip _dl_skip_args words.\n\
384 sll $2,2\n\
385 addu $29, $2\n\
386 # Save back the modified argument count.\n\
387 sw $4, 0($29)\n\
388 # Get _dl_default_scope[2] as argument in _dl_init_next call below.\n\
389 1: la $2, _dl_default_scope\n\
390 lw $4, 8($2)\n\
391 # Call _dl_init_next to return the address of an initializer\n\
392 # function to run.\n\
393 jal _dl_init_next\n\
394 move $28, $16\n\
395 # Check for zero return, when out of initializers.\n\
396 beq $2, $0, 2f\n\
397 # Call the shared object initializer function.\n\
398 move $25, $2\n\
399 lw $4, 0($29)\n\
400 lw $5, 4($29)\n\
401 lw $6, 8($29)\n\
402 lw $7, 12($29)\n\
403 jalr $25\n\
404 move $28, $16\n\
405 # Loop to call _dl_init_next for the next initializer.\n\
406 b 1b\n\
407 # Pass our finalizer function to the user in ra.\n\
408 2: la $31, _dl_fini\n\
409 # Jump to the user entry point.\n\
410 move $25, $17\n\
411 lw $4, 0($29)\n\
412 lw $5, 4($29)\n\
413 lw $6, 8($29)\n\
414 lw $7, 12($29)\n\
415 jr $25\n\
416 .end _start\n\
419 #ifdef RESOLVE
421 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
422 MAP is the object containing the reloc. */
424 static inline void
425 elf_machine_rel (struct link_map *map,
426 const ElfW(Rel) *reloc, const ElfW(Sym) *sym)
428 ElfW(Addr) *const reloc_addr = (void *) (map->l_addr + reloc->r_offset);
429 ElfW(Addr) loadbase, undo;
431 switch (ELFW(R_TYPE) (reloc->r_info))
433 case R_MIPS_REL32:
434 if (ELFW(ST_BIND) (sym->st_info) == STB_LOCAL
435 && (ELFW(ST_TYPE) (sym->st_info) == STT_SECTION
436 || ELFW(ST_TYPE) (sym->st_info) == STT_NOTYPE))
437 *reloc_addr += map->l_addr;
438 else
440 #ifndef RTLD_BOOTSTRAP
441 /* This is defined in rtld.c, but nowhere in the static libc.a;
442 make the reference weak so static programs can still link. This
443 declaration cannot be done when compiling rtld.c (i.e. #ifdef
444 RTLD_BOOTSTRAP) because rtld.c contains the common defn for
445 _dl_rtld_map, which is incompatible with a weak decl in the same
446 file. */
447 weak_extern (_dl_rtld_map);
448 if (map == &_dl_rtld_map)
449 /* Undo the relocation done here during bootstrapping. Now we will
450 relocate it anew, possibly using a binding found in the user
451 program or a loaded library rather than the dynamic linker's
452 built-in definitions used while loading those libraries. */
453 undo = map->l_addr + sym->st_value;
454 else
455 #endif
456 undo = 0;
457 loadbase = RESOLVE (&sym, 0);
458 *reloc_addr += (sym ? (loadbase + sym->st_value) : 0) - undo;
460 break;
461 case R_MIPS_NONE: /* Alright, Wilbur. */
462 break;
463 default:
464 assert (! "unexpected dynamic reloc type");
465 break;
469 static inline void
470 elf_machine_lazy_rel (struct link_map *map, const ElfW(Rel) *reloc)
472 /* Do nothing. */
475 #endif /* RESOLVE */