Back to index

glibc  2.9
dl-machine.h
Go to the documentation of this file.
00001 /* Machine-dependent ELF dynamic relocation inline functions.  IA-64 version.
00002    Copyright (C) 1995-1997, 2000-2004, 2005, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef dl_machine_h
00021 #define dl_machine_h 1
00022 
00023 #define ELF_MACHINE_NAME "ia64"
00024 
00025 #include <assert.h>
00026 #include <string.h>
00027 #include <link.h>
00028 #include <errno.h>
00029 #include <dl-fptr.h>
00030 #include <tls.h>
00031 
00032 /* Translate a processor specific dynamic tag to the index
00033    in l_info array.  */
00034 #define DT_IA_64(x) (DT_IA_64_##x - DT_LOPROC + DT_NUM)
00035 
00036 static inline void __attribute__ ((always_inline))
00037 __ia64_init_bootstrap_fdesc_table (struct link_map *map)
00038 {
00039   Elf64_Addr *boot_table;
00040 
00041   /* careful: this will be called before got has been relocated... */
00042   asm (";; addl %0 = @gprel (_dl_boot_fptr_table), gp" : "=r"(boot_table));
00043 
00044   map->l_mach.fptr_table_len = ELF_MACHINE_BOOT_FPTR_TABLE_LEN;
00045   map->l_mach.fptr_table = boot_table;
00046 }
00047 
00048 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info)            \
00049        __ia64_init_bootstrap_fdesc_table (&bootstrap_map);
00050 
00051 /* Return nonzero iff ELF header is compatible with the running host.  */
00052 static inline int __attribute__ ((unused))
00053 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
00054 {
00055   return ehdr->e_machine == EM_IA_64;
00056 }
00057 
00058 
00059 /* Return the link-time address of _DYNAMIC.  */
00060 static inline Elf64_Addr __attribute__ ((unused, const))
00061 elf_machine_dynamic (void)
00062 {
00063   Elf64_Addr *p;
00064 
00065   __asm__ (
00066        ".section .sdata\n"
00067        "      .type __dynamic_ltv#, @object\n"
00068        "      .size __dynamic_ltv#, 8\n"
00069        "__dynamic_ltv:\n"
00070        "      data8  @ltv(_DYNAMIC#)\n"
00071        ".previous\n"
00072        "      addl   %0 = @gprel(__dynamic_ltv#), gp ;;"
00073        : "=r" (p));
00074 
00075   return *p;
00076 }
00077 
00078 
00079 /* Return the run-time load address of the shared object.  */
00080 static inline Elf64_Addr __attribute__ ((unused))
00081 elf_machine_load_address (void)
00082 {
00083   Elf64_Addr ip;
00084   int *p;
00085 
00086   __asm__ (
00087        "1:    mov %0 = ip\n"
00088        ".section .sdata\n"
00089        "2:    data4  @ltv(1b)\n"
00090        "       .align 8\n"
00091        ".previous\n"
00092        "      addl   %1 = @gprel(2b), gp ;;"
00093        : "=r" (ip), "=r" (p));
00094 
00095   return ip - (Elf64_Addr) *p;
00096 }
00097 
00098 /* Set up the loaded object described by L so its unrelocated PLT
00099    entries will jump to the on-demand fixup code in dl-runtime.c.  */
00100 
00101 static inline int __attribute__ ((unused, always_inline))
00102 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
00103 {
00104   extern void _dl_runtime_resolve (void);
00105   extern void _dl_runtime_profile (void);
00106 
00107   if (lazy)
00108     {
00109       register Elf64_Addr gp __asm__ ("gp");
00110       Elf64_Addr *reserve, doit;
00111 
00112       /*
00113        * Careful with the typecast here or it will try to add l-l_addr
00114        * pointer elements
00115        */
00116       reserve = ((Elf64_Addr *)
00117                (l->l_info[DT_IA_64 (PLT_RESERVE)]->d_un.d_ptr + l->l_addr));
00118       /* Identify this shared object.  */
00119       reserve[0] = (Elf64_Addr) l;
00120 
00121       /* This function will be called to perform the relocation.  */
00122       if (!profile)
00123        doit = (Elf64_Addr) ((struct fdesc *) &_dl_runtime_resolve)->ip;
00124       else
00125        {
00126          if (GLRO(dl_profile) != NULL
00127              && _dl_name_match_p (GLRO(dl_profile), l))
00128            {
00129              /* This is the object we are looking for.  Say that we really
00130                want profiling and the timers are started.  */
00131              GL(dl_profile_map) = l;
00132            }
00133          doit = (Elf64_Addr) ((struct fdesc *) &_dl_runtime_profile)->ip;
00134        }
00135 
00136       reserve[1] = doit;
00137       reserve[2] = gp;
00138     }
00139 
00140   return lazy;
00141 }
00142 
00143 /* Names of the architecture-specific auditing callback functions.  */
00144 #define ARCH_LA_PLTENTER ia64_gnu_pltenter
00145 #define ARCH_LA_PLTEXIT ia64_gnu_pltexit
00146 
00147 /* Undo the adds out0 = 16, sp below to get at the value we want in
00148    __libc_stack_end.  */
00149 #define DL_STACK_END(cookie) \
00150   ((void *) (((long) (cookie)) - 16))
00151 
00152 /* Initial entry point code for the dynamic linker.
00153    The C function `_dl_start' is the real entry point;
00154    its return value is the user program's entry point.  */
00155 
00156 #define RTLD_START asm (                                             \
00157 ".text\n"                                                            \
00158 "      .global _start#\n"                                            \
00159 "      .proc _start#\n"                                              \
00160 "_start:\n"                                                          \
00161 "0:    { .mii\n"                                                     \
00162 "        .prologue\n"                                                       \
00163 "        .save rp, r0\n"                                             \
00164 "        .body\n"                                                    \
00165 "        .prologue\n"                                                       \
00166 "        .save ar.pfs, r32\n"                                               \
00167 "        alloc loc0 = ar.pfs, 0, 3, 4, 0\n"                                 \
00168 "        .body\n"                                                    \
00169 "        mov r2 = ip\n"                                              \
00170 "        addl r3 = @gprel(0b), r0\n"                                        \
00171 "        ;;\n"                                                              \
00172 "      }\n"                                                          \
00173 "      { .mlx\n"                                                     \
00174 "        /* Calculate the GP, and save a copy in loc1.  */\n"               \
00175 "        sub gp = r2, r3\n"                                          \
00176 "        movl r8 = 0x9804c0270033f\n"                                       \
00177 "        ;;\n"                                                              \
00178 "      }\n"                                                          \
00179 "      { .mii\n"                                                     \
00180 "        mov ar.fpsr = r8\n"                                                \
00181 "        sub loc1 = r2, r3\n"                                               \
00182 "        /* _dl_start wants a pointer to the pointer to the arg block and\n" \
00183 "           the arg block starts with an integer, thus the magic 16. */\n"   \
00184 "        adds out0 = 16, sp\n"                                              \
00185 "      }\n"                                                          \
00186 "      { .bbb\n"                                                     \
00187 "        br.call.sptk.many b0 = _dl_start#\n"                               \
00188 "        ;;\n"                                                              \
00189 "      }\n"                                                          \
00190 "      .endp _start#\n"                                              \
00191 "      /* FALLTHRU */\n"                                             \
00192 "      .global _dl_start_user#\n"                                    \
00193 "      .proc _dl_start_user#\n"                                      \
00194 "_dl_start_user:\n"                                                  \
00195 "       .prologue\n"                                                 \
00196 "       .save rp, r0\n"                                              \
00197 "        .body\n"                                                    \
00198 "       .prologue\n"                                                 \
00199 "       .save ar.pfs, r32\n"                                                \
00200 "       .body\n"                                                     \
00201 "      { .mii\n"                                                     \
00202 "        addl r3 = @gprel(_dl_skip_args), gp\n"                      \
00203 "        adds r11 = 24, sp  /* Load the address of argv. */\n"       \
00204 "        /* Save the pointer to the user entry point fptr in loc2.  */\n"    \
00205 "        mov loc2 = ret0\n"                                          \
00206 "        ;;\n"                                                              \
00207 "      }\n"                                                          \
00208 "      { .mii\n"                                                     \
00209 "        ld4 r3 = [r3]\n"                                            \
00210 "        adds r10 = 16, sp  /* Load the address of argc. */\n"       \
00211 "        mov out2 = r11\n"                                           \
00212 "        ;;\n"                                                              \
00213 "        /* See if we were run as a command with the executable file\n"      \
00214 "           name as an extra leading argument.  If so, adjust the argv\n"    \
00215 "           pointer to skip _dl_skip_args words.\n"                         \
00216 "           Note that _dl_skip_args is an integer, not a long - Jes\n"             \
00217 "\n"                                                                 \
00218 "           The stack pointer has to be 16 byte aligned. We cannot simply\n" \
00219 "           addjust the stack pointer. We have to move the whole argv and\n" \
00220 "           envp and adjust _dl_argv by _dl_skip_args.  H.J.  */\n"         \
00221 "      }\n"                                                          \
00222 "      { .mib\n"                                                     \
00223 "        ld8 out1 = [r10]   /* is argc actually stored as a long\n"         \
00224 "                              or as an int? */\n"                          \
00225 "        addl r2 = @ltoff(_dl_argv), gp\n"                                  \
00226 "        ;;\n"                                                              \
00227 "      }\n"                                                          \
00228 "      { .mmi\n"                                                     \
00229 "        ld8 r2 = [r2]             /* Get the address of _dl_argv. */\n"           \
00230 "        sub out1 = out1, r3       /* Get the new argc. */\n"               \
00231 "        shladd r3 = r3, 3, r0\n"                                    \
00232 "        ;;\n"                                                              \
00233 "      }\n"                                                          \
00234 "      {\n"                                                          \
00235 "        .mib\n"                                                     \
00236 "        ld8 r17 = [r2]     /* Get _dl_argv. */\n"                          \
00237 "        add r15 = r11, r3  /* The address of the argv we move */\n"      \
00238 "        ;;\n"                                                              \
00239 "      }\n"                                                          \
00240 "      /* ??? Could probably merge these two loops into 3 bundles.\n"       \
00241 "         using predication to control which set of copies we're on.  */\n"  \
00242 "1:    /* Copy argv. */\n"                                           \
00243 "      { .mfi\n"                                                     \
00244 "        ld8 r16 = [r15], 8 /* Load the value in the old argv. */\n"      \
00245 "        ;;\n"                                                              \
00246 "      }\n"                                                          \
00247 "      { .mib\n"                                                     \
00248 "        st8 [r11] = r16, 8 /* Store it in the new argv. */\n"       \
00249 "        cmp.ne p6, p7 = 0, r16\n"                                   \
00250 "(p6)    br.cond.dptk.few 1b\n"                                      \
00251 "        ;;\n"                                                              \
00252 "      }\n"                                                          \
00253 "      { .mmi\n"                                                     \
00254 "        mov out3 = r11\n"                                           \
00255 "        sub r17 = r17, r3  /* Substract _dl_skip_args. */\n"        \
00256 "        addl out0 = @gprel(_rtld_local), gp\n"                      \
00257 "      }\n"                                                          \
00258 "1:    /* Copy env. */\n"                                            \
00259 "      { .mfi\n"                                                     \
00260 "        ld8 r16 = [r15], 8 /* Load the value in the old env. */\n"         \
00261 "        ;;\n"                                                              \
00262 "      }\n"                                                          \
00263 "      { .mib\n"                                                     \
00264 "        st8 [r11] = r16, 8 /* Store it in the new env. */\n"        \
00265 "        cmp.ne p6, p7 = 0, r16\n"                                   \
00266 "(p6)    br.cond.dptk.few 1b\n"                                      \
00267 "        ;;\n"                                                              \
00268 "      }\n"                                                          \
00269 "      { .mmb\n"                                                     \
00270 "        st8 [r10] = out1          /* Record the new argc. */\n"            \
00271 "        ld8 out0 = [out0]         /* get the linkmap */\n"          \
00272 "      }\n"                                                          \
00273 "      { .mmb\n"                                                     \
00274 "        st8 [r2] = r17            /* Load the new _dl_argv. */\n"          \
00275 "        br.call.sptk.many b0 = _dl_init_internal#\n"                       \
00276 "        ;;\n"                                                              \
00277 "      }\n"                                                          \
00278 "      /* Pass our finalizer function to the user,\n"                       \
00279 "         and jump to the user's entry point.  */\n"                        \
00280 "      { .mmi\n"                                                     \
00281 "        ld8 r3 = [loc2], 8\n"                                              \
00282 "        mov b0 = r0\n"                                              \
00283 "      }\n"                                                          \
00284 "      { .mmi\n"                                                     \
00285 "        addl ret0 = @ltoff(@fptr(_dl_fini#)), gp\n"                        \
00286 "        ;;\n"                                                              \
00287 "        mov b6 = r3\n"                                              \
00288 "      }\n"                                                          \
00289 "      { .mmi\n"                                                     \
00290 "        ld8 ret0 = [ret0]\n"                                               \
00291 "        ld8 gp = [loc2]\n"                                          \
00292 "        mov ar.pfs = loc0\n"                                               \
00293 "        ;;\n"                                                              \
00294 "      }\n"                                                          \
00295 "      { .mfb\n"                                                     \
00296 "        br.sptk.many b6\n"                                          \
00297 "        ;;\n"                                                              \
00298 "      }\n"                                                          \
00299 "      .endp _dl_start_user#\n"                                      \
00300 ".previous\n");
00301 
00302 
00303 #ifndef RTLD_START_SPECIAL_INIT
00304 #define RTLD_START_SPECIAL_INIT /* nothing */
00305 #endif
00306 
00307 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or TLS
00308    variable, so undefined references should not be allowed to define the
00309    value.
00310    ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
00311    of the main executable's symbols, as for a COPY reloc, which we don't
00312    use.  */
00313 /* ??? Ignore *MSB for now.  */
00314 #if !defined RTLD_BOOTSTRAP || USE___THREAD
00315 #define elf_machine_type_class(type) \
00316   (((type) == R_IA64_IPLTLSB || (type) == R_IA64_DTPMOD64LSB                \
00317     || (type) == R_IA64_DTPREL64LSB || (type) == R_IA64_TPREL64LSB)         \
00318    * ELF_RTYPE_CLASS_PLT)
00319 #else
00320 #define elf_machine_type_class(type) \
00321   (((type) == R_IA64_IPLTLSB) * ELF_RTYPE_CLASS_PLT)
00322 #endif
00323 
00324 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.  */
00325 #define ELF_MACHINE_JMP_SLOT        R_IA64_IPLTLSB
00326 
00327 /* According to the IA-64 specific documentation, Rela is always used.  */
00328 #define ELF_MACHINE_NO_REL 1
00329 
00330 /* Return the address of the entry point. */
00331 #define ELF_MACHINE_START_ADDRESS(map, start)    \
00332   DL_STATIC_FUNCTION_ADDRESS (map, start)
00333 
00334 /* Fixup a PLT entry to bounce directly to the function at VALUE.  */
00335 static inline struct fdesc __attribute__ ((always_inline))
00336 elf_machine_fixup_plt (struct link_map *l, lookup_t t,
00337                      const Elf64_Rela *reloc,
00338                      Elf64_Addr *reloc_addr, struct fdesc value)
00339 {
00340   /* l is the link_map for the caller, t is the link_map for the object
00341    * being called */
00342   /* got has already been relocated in elf_get_dynamic_info() */
00343   reloc_addr[1] = value.gp;
00344   /* we need a "release" here to ensure that the gp is visible before
00345      the code entry point is updated: */
00346   ((volatile Elf64_Addr *) reloc_addr)[0] = value.ip;
00347   return value;
00348 }
00349 
00350 /* Return the final value of a plt relocation.  */
00351 static inline struct fdesc
00352 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
00353                      struct fdesc value)
00354 {
00355   /* No need to handle rel vs rela since IA64 is rela only */
00356   return (struct fdesc) { value.ip + reloc->r_addend, value.gp };
00357 }
00358 
00359 #endif /* !dl_machine_h */
00360 
00361 #ifdef RESOLVE_MAP
00362 
00363 #define R_IA64_TYPE(R)       ((R) & -8)
00364 #define R_IA64_FORMAT(R) ((R) & 7)
00365 
00366 #define R_IA64_FORMAT_32MSB 4
00367 #define R_IA64_FORMAT_32LSB 5
00368 #define R_IA64_FORMAT_64MSB 6
00369 #define R_IA64_FORMAT_64LSB 7
00370 
00371 
00372 /* Perform the relocation specified by RELOC and SYM (which is fully
00373    resolved).  MAP is the object containing the reloc.  */
00374 auto inline void
00375 __attribute ((always_inline))
00376 elf_machine_rela (struct link_map *map,
00377                 const Elf64_Rela *reloc,
00378                 const Elf64_Sym *sym,
00379                 const struct r_found_version *version,
00380                 void *const reloc_addr_arg)
00381 {
00382   Elf64_Addr *const reloc_addr = reloc_addr_arg;
00383   const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
00384   Elf64_Addr value;
00385 
00386 #if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC && !defined SHARED
00387   /* This is defined in rtld.c, but nowhere in the static libc.a; make the
00388      reference weak so static programs can still link.  This declaration
00389      cannot be done when compiling rtld.c (i.e.  #ifdef RTLD_BOOTSTRAP)
00390      because rtld.c contains the common defn for _dl_rtld_map, which is
00391      incompatible with a weak decl in the same file.  */
00392   weak_extern (_dl_rtld_map);
00393 #endif
00394 
00395   /* We cannot use a switch here because we cannot locate the switch
00396      jump table until we've self-relocated.  */
00397 
00398 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
00399   if (__builtin_expect (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_REL64LSB),
00400                      0))
00401     {
00402       assert (ELF64_R_TYPE (reloc->r_info) == R_IA64_REL64LSB);
00403       value = *reloc_addr;
00404 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
00405       /* Already done in dynamic linker.  */
00406       if (map != &GL(dl_rtld_map))
00407 # endif
00408         value += map->l_addr;
00409     }
00410   else
00411 #endif
00412     if (__builtin_expect (r_type == R_IA64_NONE, 0))
00413       return;
00414   else
00415     {
00416       struct link_map *sym_map;
00417 
00418       /* RESOLVE_MAP() will return NULL if it fail to locate the symbol.  */
00419       if ((sym_map = RESOLVE_MAP (&sym, version, r_type)))
00420         {
00421          value = sym_map->l_addr + sym->st_value + reloc->r_addend;
00422 
00423          if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DIR64LSB))
00424            ;/* No adjustment.  */
00425          else if (r_type == R_IA64_IPLTLSB)
00426            {
00427              elf_machine_fixup_plt (NULL, NULL, reloc, reloc_addr,
00428                                  DL_FIXUP_MAKE_VALUE (sym_map, value));
00429              return;
00430            }
00431          else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_FPTR64LSB))
00432            value = _dl_make_fptr (sym_map, sym, value);
00433          else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_PCREL64LSB))
00434            value -= (Elf64_Addr) reloc_addr & -16;
00435 #if !defined RTLD_BOOTSTRAP || defined USE___THREAD
00436          else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DTPMOD64LSB))
00437 # ifdef RTLD_BOOTSTRAP
00438            /* During startup the dynamic linker is always index 1.  */
00439            value = 1;
00440 # else
00441            /* Get the information from the link map returned by the
00442               resolv function.  */
00443            value = sym_map->l_tls_modid;
00444          else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_DTPREL64LSB))
00445            value -= sym_map->l_addr;
00446 # endif
00447          else if (R_IA64_TYPE (r_type) == R_IA64_TYPE (R_IA64_TPREL64LSB))
00448            {
00449 # ifndef RTLD_BOOTSTRAP
00450              CHECK_STATIC_TLS (map, sym_map);
00451 # endif
00452              value += sym_map->l_tls_offset - sym_map->l_addr;
00453            }
00454 #endif
00455          else
00456            _dl_reloc_bad_type (map, r_type, 0);
00457        }
00458       else
00459        value = 0;
00460     }
00461 
00462   /* ??? Ignore MSB and Instruction format for now.  */
00463   if (R_IA64_FORMAT (r_type) == R_IA64_FORMAT_64LSB)
00464     *reloc_addr = value;
00465   else if (R_IA64_FORMAT (r_type) == R_IA64_FORMAT_32LSB)
00466     *(int *) reloc_addr = value;
00467   else if (r_type == R_IA64_IPLTLSB)
00468     {
00469       reloc_addr[0] = 0;
00470       reloc_addr[1] = 0;
00471     }
00472   else
00473     _dl_reloc_bad_type (map, r_type, 0);
00474 }
00475 
00476 /* Let do-rel.h know that on IA-64 if l_addr is 0, all RELATIVE relocs
00477    can be skipped.  */
00478 #define ELF_MACHINE_REL_RELATIVE 1
00479 
00480 auto inline void
00481 __attribute ((always_inline))
00482 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
00483                         void *const reloc_addr_arg)
00484 {
00485   Elf64_Addr *const reloc_addr = reloc_addr_arg;
00486   /* ??? Ignore MSB and Instruction format for now.  */
00487   assert (ELF64_R_TYPE (reloc->r_info) == R_IA64_REL64LSB);
00488 
00489   *reloc_addr += l_addr;
00490 }
00491 
00492 /* Perform a RELATIVE reloc on the .got entry that transfers to the .plt.  */
00493 auto inline void
00494 __attribute ((always_inline))
00495 elf_machine_lazy_rel (struct link_map *map,
00496                     Elf64_Addr l_addr, const Elf64_Rela *reloc)
00497 {
00498   Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
00499   const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
00500 
00501   if (r_type == R_IA64_IPLTLSB)
00502     {
00503       reloc_addr[0] += l_addr;
00504       reloc_addr[1] += l_addr;
00505     }
00506   else if (r_type == R_IA64_NONE)
00507     return;
00508   else
00509     _dl_reloc_bad_type (map, r_type, 1);
00510 }
00511 
00512 #endif /* RESOLVE_MAP */