Back to index

glibc  2.9
dl-machine.h
Go to the documentation of this file.
00001 /* Machine-dependent ELF dynamic relocation inline functions.
00002    64 bit S/390 Version.
00003    Copyright (C) 2001-2005, 2006 Free Software Foundation, Inc.
00004    Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
00005    This file is part of the GNU C Library.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #ifndef dl_machine_h
00023 #define dl_machine_h
00024 
00025 #define ELF_MACHINE_NAME "s390x"
00026 
00027 #include <sys/param.h>
00028 #include <string.h>
00029 #include <link.h>
00030 
00031 /* This is an older, now obsolete value.  */
00032 #define EM_S390_OLD  0xA390
00033 
00034 /* Return nonzero iff E_MACHINE is compatible with the running host.  */
00035 static inline int
00036 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
00037 {
00038   return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD)
00039         && ehdr->e_ident[EI_CLASS] == ELFCLASS64;
00040 }
00041 
00042 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
00043    first element of the GOT.  This must be inlined in a function which
00044    uses global data.  */
00045 
00046 static inline Elf64_Addr
00047 elf_machine_dynamic (void)
00048 {
00049   register Elf64_Addr *got;
00050 
00051   asm( "      larl   %0,_GLOBAL_OFFSET_TABLE_\n"
00052        : "=&a" (got) : : "0" );
00053 
00054   return *got;
00055 }
00056 
00057 /* Return the run-time load address of the shared object.  */
00058 static inline Elf64_Addr
00059 elf_machine_load_address (void)
00060 {
00061   Elf64_Addr addr;
00062 
00063   asm( "   larl       %0,_dl_start\n"
00064        "   larl       1,_GLOBAL_OFFSET_TABLE_\n"
00065        "   lghi       2,_dl_start@GOT\n"
00066        "   slg        %0,0(2,1)"
00067        : "=&d" (addr) : : "1", "2" );
00068   return addr;
00069 }
00070 
00071 /* Set up the loaded object described by L so its unrelocated PLT
00072    entries will jump to the on-demand fixup code in dl-runtime.c.  */
00073 
00074 static inline int __attribute__ ((unused))
00075 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
00076 {
00077   extern void _dl_runtime_resolve (Elf64_Word);
00078   extern void _dl_runtime_profile (Elf64_Word);
00079 
00080   if (l->l_info[DT_JMPREL] && lazy)
00081     {
00082       /* The GOT entries for functions in the PLT have not yet been filled
00083         in.  Their initial contents will arrange when called to push an
00084         offset into the .rela.plt section, push _GLOBAL_OFFSET_TABLE_[1],
00085         and then jump to _GLOBAL_OFFSET_TABLE[2].  */
00086       Elf64_Addr *got;
00087       got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
00088       /* If a library is prelinked but we have to relocate anyway,
00089         we have to be able to undo the prelinking of .got.plt.
00090         The prelinker saved us here address of .plt + 0x2e.  */
00091       if (got[1])
00092        {
00093          l->l_mach.plt = got[1] + l->l_addr;
00094          l->l_mach.gotplt = (Elf64_Addr) &got[3];
00095        }
00096       got[1] = (Elf64_Addr) l;     /* Identify this shared object.     */
00097 
00098       /* The got[2] entry contains the address of a function which gets
00099         called to get the address of a so far unresolved function and
00100         jump to it.  The profiling extension of the dynamic linker allows
00101         to intercept the calls to collect information.   In this case we
00102         don't store the address in the GOT so that all future calls also
00103         end in this function.      */
00104       if (__builtin_expect (profile, 0))
00105        {
00106          got[2] = (Elf64_Addr) &_dl_runtime_profile;
00107 
00108          if (GLRO(dl_profile) != NULL
00109              && _dl_name_match_p (GLRO(dl_profile), l))
00110            /* This is the object we are looking for.  Say that we really
00111               want profiling and the timers are started.  */
00112            GL(dl_profile_map) = l;
00113        }
00114       else
00115        /* This function will get called to fix up the GOT entry indicated by
00116           the offset on the stack, and then jump to the resolved address.  */
00117        got[2] = (Elf64_Addr) &_dl_runtime_resolve;
00118     }
00119 
00120   return lazy;
00121 }
00122 
00123 /* Initial entry point code for the dynamic linker.
00124    The C function `_dl_start' is the real entry point;
00125    its return value is the user program's entry point.  */
00126 
00127 #define RTLD_START asm ("\n\
00128 .text\n\
00129 .align 4\n\
00130 .globl _start\n\
00131 .globl _dl_start_user\n\
00132 _start:\n\
00133        lgr   %r2,%r15\n\
00134        # Alloc stack frame\n\
00135        aghi  %r15,-160\n\
00136        # Set the back chain to zero\n\
00137        xc    0(8,%r15),0(%r15)\n\
00138        # Call _dl_start with %r2 pointing to arg on stack\n\
00139        brasl %r14,_dl_start      # call _dl_start\n\
00140 _dl_start_user:\n\
00141        # Save the user entry point address in %r8.\n\
00142        lgr   %r8,%r2\n\
00143        # Point %r12 at the GOT.\n\
00144        larl  %r12,_GLOBAL_OFFSET_TABLE_\n\
00145        # See if we were run as a command with the executable file\n\
00146        # name as an extra leading argument.\n\
00147        lghi  %r1,_dl_skip_args@GOT\n\
00148        lg    %r1,0(%r1,%r12)\n\
00149        lgf   %r1,0(%r1)       # load _dl_skip_args\n\
00150        # Get the original argument count.\n\
00151        lg    %r0,160(%r15)\n\
00152        # Subtract _dl_skip_args from it.\n\
00153        sgr   %r0,%r1\n\
00154        # Adjust the stack pointer to skip _dl_skip_args words.\n\
00155        sllg  %r1,%r1,3\n\
00156        agr   %r15,%r1\n\
00157        # Set the back chain to zero again\n\
00158        xc    0(8,%r15),0(%r15)\n\
00159        # Store back the modified argument count.\n\
00160        stg   %r0,160(%r15)\n\
00161        # The special initializer gets called with the stack just\n\
00162        # as the application's entry point will see it; it can\n\
00163        # switch stacks if it moves these contents over.\n\
00164 " RTLD_START_SPECIAL_INIT "\n\
00165        # Call the function to run the initializers.\n\
00166        # Load the parameters:\n\
00167        # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
00168        lghi  %r2,_rtld_local@GOT\n\
00169        lg    %r2,0(%r2,%r12)\n\
00170        lg    %r2,0(%r2)\n\
00171        lg    %r3,160(%r15)\n\
00172        la    %r4,168(%r15)\n\
00173        lgr   %r5,%r3\n\
00174        sllg  %r5,%r5,3\n\
00175        la    %r5,176(%r5,%r15)\n\
00176        brasl %r14,_dl_init_internal@PLT\n\
00177        # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
00178        lghi  %r14,_dl_fini@GOT\n\
00179        lg    %r14,0(%r14,%r12)\n\
00180        # Free stack frame\n\
00181        aghi  %r15,160\n\
00182        # Jump to the user's entry point (saved in %r8).\n\
00183        br    %r8\n\
00184 ");
00185 
00186 #ifndef RTLD_START_SPECIAL_INIT
00187 #define RTLD_START_SPECIAL_INIT /* nothing */
00188 #endif
00189 
00190 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
00191    TLS variable, so undefined references should not be allowed to
00192    define the value.
00193    ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
00194    of the main executable's symbols, as for a COPY reloc.  */
00195 #define elf_machine_type_class(type) \
00196   ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD                 \
00197      || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF)            \
00198     * ELF_RTYPE_CLASS_PLT)                                           \
00199    | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
00200 
00201 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.  */
00202 #define ELF_MACHINE_JMP_SLOT       R_390_JMP_SLOT
00203 
00204 /* The 64 bit S/390 never uses Elf64_Rel relocations.  */
00205 #define ELF_MACHINE_NO_REL 1
00206 
00207 /* We define an initialization functions.  This is called very early in
00208    _dl_sysdep_start.  */
00209 #define DL_PLATFORM_INIT dl_platform_init ()
00210 
00211 static inline void __attribute__ ((unused))
00212 dl_platform_init (void)
00213 {
00214   if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
00215     /* Avoid an empty string which would disturb us.  */
00216     GLRO(dl_platform) = NULL;
00217 }
00218 
00219 static inline Elf64_Addr
00220 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
00221                      const Elf64_Rela *reloc,
00222                      Elf64_Addr *reloc_addr, Elf64_Addr value)
00223 {
00224   return *reloc_addr = value;
00225 }
00226 
00227 /* Return the final value of a plt relocation.   */
00228 static inline Elf64_Addr
00229 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
00230                      Elf64_Addr value)
00231 {
00232   return value;
00233 }
00234 
00235 /* Names of the architecture-specific auditing callback functions.  */
00236 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
00237 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
00238 
00239 #endif /* !dl_machine_h */
00240 
00241 #ifdef RESOLVE_MAP
00242 
00243 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
00244    MAP is the object containing the reloc.  */
00245 
00246 auto inline void
00247 __attribute__ ((always_inline))
00248 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
00249                 const Elf64_Sym *sym, const struct r_found_version *version,
00250                 void *const reloc_addr_arg)
00251 {
00252   Elf64_Addr *const reloc_addr = reloc_addr_arg;
00253   const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
00254 
00255 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
00256   if (__builtin_expect (r_type == R_390_RELATIVE, 0))
00257     {
00258 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
00259       /* This is defined in rtld.c, but nowhere in the static libc.a;
00260         make the reference weak so static programs can still link.
00261         This declaration cannot be done when compiling rtld.c
00262         (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
00263         common defn for _dl_rtld_map, which is incompatible with a
00264         weak decl in the same file.  */
00265 #  ifndef SHARED
00266       weak_extern (GL(dl_rtld_map));
00267 #  endif
00268       if (map != &GL(dl_rtld_map)) /* Already done in rtld itself.  */
00269 # endif
00270        *reloc_addr = map->l_addr + reloc->r_addend;
00271     }
00272   else
00273 #endif
00274   if (__builtin_expect (r_type == R_390_NONE, 0))
00275     return;
00276   else
00277     {
00278 #ifndef RESOLVE_CONFLICT_FIND_MAP
00279       const Elf64_Sym *const refsym = sym;
00280 #endif
00281       struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
00282       Elf64_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
00283 
00284       switch (r_type)
00285        {
00286        case R_390_GLOB_DAT:
00287        case R_390_JMP_SLOT:
00288          *reloc_addr = value + reloc->r_addend;
00289          break;
00290 
00291 #if (!defined RTLD_BOOTSTRAP || USE___THREAD) \
00292     && !defined RESOLVE_CONFLICT_FIND_MAP
00293        case R_390_TLS_DTPMOD:
00294 # ifdef RTLD_BOOTSTRAP
00295          /* During startup the dynamic linker is always the module
00296             with index 1.
00297             XXX If this relocation is necessary move before RESOLVE
00298             call.  */
00299          *reloc_addr = 1;
00300 # else
00301          /* Get the information from the link map returned by the
00302             resolv function.  */
00303          if (sym_map != NULL)
00304            *reloc_addr = sym_map->l_tls_modid;
00305 # endif
00306          break;
00307        case R_390_TLS_DTPOFF:
00308 # ifndef RTLD_BOOTSTRAP
00309          /* During relocation all TLS symbols are defined and used.
00310             Therefore the offset is already correct.  */
00311          if (sym != NULL)
00312            *reloc_addr = sym->st_value + reloc->r_addend;
00313 # endif
00314          break;
00315        case R_390_TLS_TPOFF:
00316          /* The offset is negative, forward from the thread pointer.  */
00317 # ifdef RTLD_BOOTSTRAP
00318          *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
00319 # else
00320          /* We know the offset of the object the symbol is contained in.
00321             It is a negative value which will be added to the
00322             thread pointer.  */
00323          if (sym != NULL)
00324            {
00325              CHECK_STATIC_TLS (map, sym_map);
00326              *reloc_addr = (sym->st_value + reloc->r_addend
00327                           - sym_map->l_tls_offset);
00328            }
00329 #endif
00330          break;
00331 #endif  /* use TLS */
00332 
00333 #ifndef RTLD_BOOTSTRAP
00334 # ifndef RESOLVE_CONFLICT_FIND_MAP
00335        /* Not needed for dl-conflict.c.  */
00336        case R_390_COPY:
00337          if (sym == NULL)
00338            /* This can happen in trace mode if an object could not be
00339               found.  */
00340            break;
00341          if (__builtin_expect (sym->st_size > refsym->st_size, 0)
00342              || (__builtin_expect (sym->st_size < refsym->st_size, 0)
00343                 && __builtin_expect (GLRO(dl_verbose), 0)))
00344            {
00345              const char *strtab;
00346 
00347              strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
00348              _dl_error_printf ("\
00349 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
00350                             rtld_progname ?: "<program name unknown>",
00351                             strtab + refsym->st_name);
00352            }
00353          memcpy (reloc_addr_arg, (void *) value,
00354                 MIN (sym->st_size, refsym->st_size));
00355          break;
00356 # endif
00357        case R_390_64:
00358          *reloc_addr = value + reloc->r_addend;
00359          break;
00360        case R_390_32:
00361          *(unsigned int *) reloc_addr = value + reloc->r_addend;
00362          break;
00363        case R_390_16:
00364          *(unsigned short *) reloc_addr = value + reloc->r_addend;
00365          break;
00366        case R_390_8:
00367          *(char *) reloc_addr = value + reloc->r_addend;
00368          break;
00369 # ifndef RESOLVE_CONFLICT_FIND_MAP
00370        case R_390_PC64:
00371          *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
00372          break;
00373        case R_390_PC32DBL:
00374        case R_390_PLT32DBL:
00375          *(unsigned int *) reloc_addr = (unsigned int)
00376            ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
00377          break;
00378        case R_390_PC32:
00379          *(unsigned int *) reloc_addr =
00380            value + reloc->r_addend - (Elf64_Addr) reloc_addr;
00381          break;
00382        case R_390_PC16DBL:
00383        case R_390_PLT16DBL:
00384          *(unsigned short *) reloc_addr = (unsigned short)
00385            ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
00386          break;
00387        case R_390_PC16:
00388          *(unsigned short *) reloc_addr =
00389            value + reloc->r_addend - (Elf64_Addr) reloc_addr;
00390          break;
00391        case R_390_NONE:
00392          break;
00393 # endif
00394 #endif
00395 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
00396        default:
00397          /* We add these checks in the version to relocate ld.so only
00398             if we are still debugging.     */
00399          _dl_reloc_bad_type (map, r_type, 0);
00400          break;
00401 #endif
00402        }
00403     }
00404 }
00405 
00406 auto inline void
00407 __attribute__ ((always_inline))
00408 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
00409                         void *const reloc_addr_arg)
00410 {
00411   Elf64_Addr *const reloc_addr = reloc_addr_arg;
00412   *reloc_addr = l_addr + reloc->r_addend;
00413 }
00414 
00415 auto inline void
00416 __attribute__ ((always_inline))
00417 elf_machine_lazy_rel (struct link_map *map,
00418                     Elf64_Addr l_addr, const Elf64_Rela *reloc)
00419 {
00420   Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
00421   const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
00422   /* Check for unexpected PLT reloc type.  */
00423   if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
00424     {
00425       if (__builtin_expect (map->l_mach.plt, 0) == 0)
00426        *reloc_addr += l_addr;
00427       else
00428        *reloc_addr =
00429          map->l_mach.plt
00430          + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
00431     }
00432   else
00433     _dl_reloc_bad_type (map, r_type, 1);
00434 }
00435 
00436 #endif /* RESOLVE_MAP */