[BZ #2172]
[glibc.git] / sysdeps / ia64 / dl-machine.h
blob55349690e316d05bbbab9c8c06ceb7ab4c48f819
1 /* Machine-dependent ELF dynamic relocation inline functions. IA-64 version.
2 Copyright (C) 1995-1997, 2000-2004, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef dl_machine_h
21 #define dl_machine_h 1
23 #define ELF_MACHINE_NAME "ia64"
25 #include <assert.h>
26 #include <string.h>
27 #include <link.h>
28 #include <errno.h>
29 #include <dl-fptr.h>
30 #include <tls.h>
32 /* Translate a processor specific dynamic tag to the index
33 in l_info array. */
34 #define DT_IA_64(x) (DT_IA_64_##x - DT_LOPROC + DT_NUM)
36 static inline void __attribute__ ((always_inline))
37 __ia64_init_bootstrap_fdesc_table (struct link_map *map)
39 Elf64_Addr *boot_table;
41 /* careful: this will be called before got has been relocated... */
42 asm (";; addl %0 = @gprel (_dl_boot_fptr_table), gp" : "=r"(boot_table));
44 map->l_mach.fptr_table_len = ELF_MACHINE_BOOT_FPTR_TABLE_LEN;
45 map->l_mach.fptr_table = boot_table;
48 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) \
49 __ia64_init_bootstrap_fdesc_table (&bootstrap_map);
51 /* Return nonzero iff ELF header is compatible with the running host. */
52 static inline int __attribute__ ((unused))
53 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
55 return ehdr->e_machine == EM_IA_64;
59 /* Return the link-time address of _DYNAMIC. */
60 static inline Elf64_Addr __attribute__ ((unused, const))
61 elf_machine_dynamic (void)
63 Elf64_Addr *p;
65 __asm__ (
66 ".section .sdata\n"
67 " .type __dynamic_ltv#, @object\n"
68 " .size __dynamic_ltv#, 8\n"
69 "__dynamic_ltv:\n"
70 " data8 @ltv(_DYNAMIC#)\n"
71 ".previous\n"
72 " addl %0 = @gprel(__dynamic_ltv#), gp ;;"
73 : "=r" (p));
75 return *p;
79 /* Return the run-time load address of the shared object. */
80 static inline Elf64_Addr __attribute__ ((unused))
81 elf_machine_load_address (void)
83 Elf64_Addr ip;
84 int *p;
86 __asm__ (
87 "1: mov %0 = ip\n"
88 ".section .sdata\n"
89 "2: data4 @ltv(1b)\n"
90 " .align 8\n"
91 ".previous\n"
92 " addl %1 = @gprel(2b), gp ;;"
93 : "=r" (ip), "=r" (p));
95 return ip - (Elf64_Addr) *p;
98 /* Set up the loaded object described by L so its unrelocated PLT
99 entries will jump to the on-demand fixup code in dl-runtime.c. */
101 static inline int __attribute__ ((unused, always_inline))
102 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
104 extern void _dl_runtime_resolve (void);
105 extern void _dl_runtime_profile (void);
107 if (lazy)
109 register Elf64_Addr gp __asm__ ("gp");
110 Elf64_Addr *reserve, doit;
113 * Careful with the typecast here or it will try to add l-l_addr
114 * pointer elements
116 reserve = ((Elf64_Addr *)
117 (l->l_info[DT_IA_64 (PLT_RESERVE)]->d_un.d_ptr + l->l_addr));
118 /* Identify this shared object. */
119 reserve[0] = (Elf64_Addr) l;
121 /* This function will be called to perform the relocation. */
122 if (!profile)
123 doit = (Elf64_Addr) ((struct fdesc *) &_dl_runtime_resolve)->ip;
124 else
126 if (GLRO(dl_profile) != NULL
127 && _dl_name_match_p (GLRO(dl_profile), l))
129 /* This is the object we are looking for. Say that we really
130 want profiling and the timers are started. */
131 GL(dl_profile_map) = l;
133 doit = (Elf64_Addr) ((struct fdesc *) &_dl_runtime_profile)->ip;
136 reserve[1] = doit;
137 reserve[2] = gp;
140 return lazy;
143 /* Names of the architecture-specific auditing callback functions. */
144 #define ARCH_LA_PLTENTER ia64_gnu_pltenter
145 #define ARCH_LA_PLTEXIT ia64_gnu_pltexit
147 /* Undo the adds out0 = 16, sp below to get at the value we want in
148 __libc_stack_end. */
149 #define DL_STACK_END(cookie) \
150 ((void *) (((long) (cookie)) - 16))
152 /* Initial entry point code for the dynamic linker.
153 The C function `_dl_start' is the real entry point;
154 its return value is the user program's entry point. */
156 #define RTLD_START asm ( \
157 ".text\n" \
158 " .global _start#\n" \
159 " .proc _start#\n" \
160 "_start:\n" \
161 "0: { .mii\n" \
162 " .prologue\n" \
163 " .save rp, r0\n" \
164 " .body\n" \
165 " .prologue\n" \
166 " .save ar.pfs, r32\n" \
167 " alloc loc0 = ar.pfs, 0, 3, 4, 0\n" \
168 " .body\n" \
169 " mov r2 = ip\n" \
170 " addl r3 = @gprel(0b), r0\n" \
171 " ;;\n" \
172 " }\n" \
173 " { .mlx\n" \
174 " /* Calculate the GP, and save a copy in loc1. */\n" \
175 " sub gp = r2, r3\n" \
176 " movl r8 = 0x9804c0270033f\n" \
177 " ;;\n" \
178 " }\n" \
179 " { .mii\n" \
180 " mov ar.fpsr = r8\n" \
181 " sub loc1 = r2, r3\n" \
182 " /* _dl_start wants a pointer to the pointer to the arg block and\n" \
183 " the arg block starts with an integer, thus the magic 16. */\n" \
184 " adds out0 = 16, sp\n" \
185 " }\n" \
186 " { .bbb\n" \
187 " br.call.sptk.many b0 = _dl_start#\n" \
188 " ;;\n" \
189 " }\n" \
190 " .endp _start#\n" \
191 " /* FALLTHRU */\n" \
192 " .global _dl_start_user#\n" \
193 " .proc _dl_start_user#\n" \
194 "_dl_start_user:\n" \
195 " .prologue\n" \
196 " .save rp, r0\n" \
197 " .body\n" \
198 " .prologue\n" \
199 " .save ar.pfs, r32\n" \
200 " .body\n" \
201 " { .mii\n" \
202 " addl r3 = @gprel(_dl_skip_args), gp\n" \
203 " adds r11 = 24, sp /* Load the address of argv. */\n" \
204 " /* Save the pointer to the user entry point fptr in loc2. */\n" \
205 " mov loc2 = ret0\n" \
206 " ;;\n" \
207 " }\n" \
208 " { .mii\n" \
209 " ld4 r3 = [r3]\n" \
210 " adds r10 = 16, sp /* Load the address of argc. */\n" \
211 " mov out2 = r11\n" \
212 " ;;\n" \
213 " /* See if we were run as a command with the executable file\n" \
214 " name as an extra leading argument. If so, adjust the argv\n" \
215 " pointer to skip _dl_skip_args words.\n" \
216 " Note that _dl_skip_args is an integer, not a long - Jes\n" \
217 "\n" \
218 " The stack pointer has to be 16 byte aligned. We cannot simply\n" \
219 " addjust the stack pointer. We have to move the whole argv and\n" \
220 " envp and adjust _dl_argv by _dl_skip_args. H.J. */\n" \
221 " }\n" \
222 " { .mib\n" \
223 " ld8 out1 = [r10] /* is argc actually stored as a long\n" \
224 " or as an int? */\n" \
225 " addl r2 = @ltoff(_dl_argv), gp\n" \
226 " ;;\n" \
227 " }\n" \
228 " { .mmi\n" \
229 " ld8 r2 = [r2] /* Get the address of _dl_argv. */\n" \
230 " sub out1 = out1, r3 /* Get the new argc. */\n" \
231 " shladd r3 = r3, 3, r0\n" \
232 " ;;\n" \
233 " }\n" \
234 " {\n" \
235 " .mib\n" \
236 " ld8 r17 = [r2] /* Get _dl_argv. */\n" \
237 " add r15 = r11, r3 /* The address of the argv we move */\n" \
238 " ;;\n" \
239 " }\n" \
240 " /* ??? Could probably merge these two loops into 3 bundles.\n" \
241 " using predication to control which set of copies we're on. */\n" \
242 "1: /* Copy argv. */\n" \
243 " { .mfi\n" \
244 " ld8 r16 = [r15], 8 /* Load the value in the old argv. */\n" \
245 " ;;\n" \
246 " }\n" \
247 " { .mib\n" \
248 " st8 [r11] = r16, 8 /* Store it in the new argv. */\n" \
249 " cmp.ne p6, p7 = 0, r16\n" \
250 "(p6) br.cond.dptk.few 1b\n" \
251 " ;;\n" \
252 " }\n" \
253 " { .mmi\n" \
254 " mov out3 = r11\n" \
255 " sub r17 = r17, r3 /* Substract _dl_skip_args. */\n" \
256 " addl out0 = @gprel(_rtld_local), gp\n" \
257 " }\n" \
258 "1: /* Copy env. */\n" \
259 " { .mfi\n" \
260 " ld8 r16 = [r15], 8 /* Load the value in the old env. */\n" \
261 " ;;\n" \
262 " }\n" \
263 " { .mib\n" \
264 " st8 [r11] = r16, 8 /* Store it in the new env. */\n" \
265 " cmp.ne p6, p7 = 0, r16\n" \
266 "(p6) br.cond.dptk.few 1b\n" \
267 " ;;\n" \
268 " }\n" \
269 " { .mmb\n" \
270 " st8 [r10] = out1 /* Record the new argc. */\n" \
271 " ld8 out0 = [out0] /* get the linkmap */\n" \
272 " }\n" \
273 " { .mmb\n" \
274 " st8 [r2] = r17 /* Load the new _dl_argv. */\n" \
275 " br.call.sptk.many b0 = _dl_init_internal#\n" \
276 " ;;\n" \
277 " }\n" \
278 " /* Pass our finalizer function to the user,\n" \
279 " and jump to the user's entry point. */\n" \
280 " { .mmi\n" \
281 " ld8 r3 = [loc2], 8\n" \
282 " mov b0 = r0\n" \
283 " }\n" \
284 " { .mmi\n" \
285 " addl ret0 = @ltoff(@fptr(_dl_fini#)), gp\n" \
286 " ;;\n" \
287 " mov b6 = r3\n" \
288 " }\n" \
289 " { .mmi\n" \
290 " ld8 ret0 = [ret0]\n" \
291 " ld8 gp = [loc2]\n" \
292 " mov ar.pfs = loc0\n" \
293 " ;;\n" \
294 " }\n" \
295 " { .mfb\n" \
296 " br.sptk.many b6\n" \
297 " ;;\n" \
298 " }\n" \
299 " .endp _dl_start_user#\n" \
300 ".previous\n");
303 #ifndef RTLD_START_SPECIAL_INIT
304 #define RTLD_START_SPECIAL_INIT /* nothing */
305 #endif
307 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or TLS
308 variable, so undefined references should not be allowed to define the
309 value.
310 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
311 of the main executable's symbols, as for a COPY reloc, which we don't
312 use. */
313 /* ??? Ignore *MSB for now. */
314 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
315 #define elf_machine_type_class(type) \
316 (((type) == R_IA64_IPLTLSB || (type) == R_IA64_DTPMOD64LSB \
317 || (type) == R_IA64_DTPREL64LSB || (type) == R_IA64_TPREL64LSB) \
318 * ELF_RTYPE_CLASS_PLT)
319 #else
320 #define elf_machine_type_class(type) \
321 (((type) == R_IA64_IPLTLSB) * ELF_RTYPE_CLASS_PLT)
322 #endif
324 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
325 #define ELF_MACHINE_JMP_SLOT R_IA64_IPLTLSB
327 /* According to the IA-64 specific documentation, Rela is always used. */
328 #define ELF_MACHINE_NO_REL 1
330 /* Return the address of the entry point. */
331 #define ELF_MACHINE_START_ADDRESS(map, start) \
332 DL_STATIC_FUNCTION_ADDRESS (map, start)
334 /* Fixup a PLT entry to bounce directly to the function at VALUE. */
335 static inline struct fdesc __attribute__ ((always_inline))
336 elf_machine_fixup_plt (struct link_map *l, lookup_t t,
337 const Elf64_Rela *reloc,
338 Elf64_Addr *reloc_addr, struct fdesc value)
340 /* l is the link_map for the caller, t is the link_map for the object
341 * being called */
342 /* got has already been relocated in elf_get_dynamic_info() */
343 reloc_addr[1] = value.gp;
344 /* we need a "release" here to ensure that the gp is visible before
345 the code entry point is updated: */
346 ((volatile Elf64_Addr *) reloc_addr)[0] = value.ip;
347 return value;
350 /* Return the final value of a plt relocation. */
351 static inline struct fdesc
352 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
353 struct fdesc value)
355 /* No need to handle rel vs rela since IA64 is rela only */
356 return (struct fdesc) { value.ip + reloc->r_addend, value.gp };
359 #endif /* !dl_machine_h */
361 #ifdef RESOLVE_MAP
363 #define R_IA64_TYPE(R) ((R) & -8)
364 #define R_IA64_FORMAT(R) ((R) & 7)
366 #define R_IA64_FORMAT_32MSB 4
367 #define R_IA64_FORMAT_32LSB 5
368 #define R_IA64_FORMAT_64MSB 6
369 #define R_IA64_FORMAT_64LSB 7
372 /* Perform the relocation specified by RELOC and SYM (which is fully
373 resolved). MAP is the object containing the reloc. */
374 auto inline void
375 __attribute ((always_inline))
376 elf_machine_rela (struct link_map *map,
377 const Elf64_Rela *reloc,
378 const Elf64_Sym *sym,
379 const struct r_found_version *version,
380 void *const reloc_addr_arg)
382 Elf64_Addr *const reloc_addr = reloc_addr_arg;
383 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
384 Elf64_Addr value;
386 #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC && !defined SHARED
387 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
388 reference weak so static programs can still link. This declaration
389 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
390 because rtld.c contains the common defn for _dl_rtld_map, which is
391 incompatible with a weak decl in the same file. */
392 weak_extern (_dl_rtld_map);
393 #endif
395 /* We cannot use a switch here because we cannot locate the switch
396 jump table until we've self-relocated. */
398 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
399 if (__builtin_expect (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_REL64LSB),
402 assert (ELF64_R_TYPE (reloc->r_info) == R_IA64_REL64LSB);
403 value = *reloc_addr;
404 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
405 /* Already done in dynamic linker. */
406 if (map != &GL(dl_rtld_map))
407 # endif
408 value += map->l_addr;
410 else
411 #endif
412 if (__builtin_expect (r_type == R_IA64_NONE, 0))
413 return;
414 else
416 struct link_map *sym_map;
418 /* RESOLVE_MAP() will return NULL if it fail to locate the symbol. */
419 if ((sym_map = RESOLVE_MAP (&sym, version, r_type)))
421 value = sym_map->l_addr + sym->st_value + reloc->r_addend;
423 if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DIR64LSB))
424 ;/* No adjustment. */
425 else if (r_type == R_IA64_IPLTLSB)
427 elf_machine_fixup_plt (NULL, NULL, reloc, reloc_addr,
428 DL_FIXUP_MAKE_VALUE (sym_map, value));
429 return;
431 else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_FPTR64LSB))
432 value = _dl_make_fptr (sym_map, sym, value);
433 else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_PCREL64LSB))
434 value -= (Elf64_Addr) reloc_addr & -16;
435 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || defined USE___THREAD)
436 else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DTPMOD64LSB))
437 # ifdef RTLD_BOOTSTRAP
438 /* During startup the dynamic linker is always index 1. */
439 value = 1;
440 # else
441 /* Get the information from the link map returned by the
442 resolv function. */
443 value = sym_map->l_tls_modid;
444 else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DTPREL64LSB))
445 value -= sym_map->l_addr;
446 # endif
447 else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_TPREL64LSB))
449 # ifndef RTLD_BOOTSTRAP
450 CHECK_STATIC_TLS (map, sym_map);
451 # endif
452 value += sym_map->l_tls_offset - sym_map->l_addr;
454 #endif
455 else
456 _dl_reloc_bad_type (map, r_type, 0);
458 else
459 value = 0;
462 /* ??? Ignore MSB and Instruction format for now. */
463 if (R_IA64_FORMAT (r_type) == R_IA64_FORMAT_64LSB)
464 *reloc_addr = value;
465 else if (R_IA64_FORMAT (r_type) == R_IA64_FORMAT_32LSB)
466 *(int *) reloc_addr = value;
467 else if (r_type == R_IA64_IPLTLSB)
469 reloc_addr[0] = 0;
470 reloc_addr[1] = 0;
472 else
473 _dl_reloc_bad_type (map, r_type, 0);
476 /* Let do-rel.h know that on IA-64 if l_addr is 0, all RELATIVE relocs
477 can be skipped. */
478 #define ELF_MACHINE_REL_RELATIVE 1
480 auto inline void
481 __attribute ((always_inline))
482 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
483 void *const reloc_addr_arg)
485 Elf64_Addr *const reloc_addr = reloc_addr_arg;
486 /* ??? Ignore MSB and Instruction format for now. */
487 assert (ELF64_R_TYPE (reloc->r_info) == R_IA64_REL64LSB);
489 *reloc_addr += l_addr;
492 /* Perform a RELATIVE reloc on the .got entry that transfers to the .plt. */
493 auto inline void
494 __attribute ((always_inline))
495 elf_machine_lazy_rel (struct link_map *map,
496 Elf64_Addr l_addr, const Elf64_Rela *reloc)
498 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
499 const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
501 if (r_type == R_IA64_IPLTLSB)
503 reloc_addr[0] += l_addr;
504 reloc_addr[1] += l_addr;
506 else if (r_type == R_IA64_NONE)
507 return;
508 else
509 _dl_reloc_bad_type (map, r_type, 1);
512 #endif /* RESOLVE_MAP */