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