Back to index

glibc  2.9
dl-machine.h
Go to the documentation of this file.
00001 /* Machine-dependent ELF dynamic relocation inline functions.  PowerPC version.
00002    Copyright (C) 1995-2002, 2003, 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
00022 
00023 #define ELF_MACHINE_NAME "powerpc"
00024 
00025 #include <assert.h>
00026 #include <dl-tls.h>
00027 
00028 /* Translate a processor specific dynamic tag to the index
00029    in l_info array.  */
00030 #define DT_PPC(x) (DT_PPC_##x - DT_LOPROC + DT_NUM)
00031 
00032 /* Return nonzero iff ELF header is compatible with the running host.  */
00033 static inline int
00034 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
00035 {
00036   return ehdr->e_machine == EM_PPC;
00037 }
00038 
00039 /* Return the value of the GOT pointer.  */
00040 static inline Elf32_Addr * __attribute__ ((const))
00041 ppc_got (void)
00042 {
00043   Elf32_Addr *got;
00044 #ifdef HAVE_ASM_PPC_REL16
00045   asm ("bcl 20,31,1f\n"
00046        "1:    mflr %0\n"
00047        "      addis %0,%0,_GLOBAL_OFFSET_TABLE_-1b@ha\n"
00048        "      addi %0,%0,_GLOBAL_OFFSET_TABLE_-1b@l\n"
00049        : "=b" (got) : : "lr");
00050 #else
00051   asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
00052        : "=l" (got));
00053 #endif
00054   return got;
00055 }
00056 
00057 /* Return the link-time address of _DYNAMIC, stored as
00058    the first value in the GOT. */
00059 static inline Elf32_Addr __attribute__ ((const))
00060 elf_machine_dynamic (void)
00061 {
00062   return *ppc_got ();
00063 }
00064 
00065 /* Return the run-time load address of the shared object.  */
00066 static inline Elf32_Addr __attribute__ ((const))
00067 elf_machine_load_address (void)
00068 {
00069   Elf32_Addr *branchaddr;
00070   Elf32_Addr runtime_dynamic;
00071 
00072   /* This is much harder than you'd expect.  Possibly I'm missing something.
00073      The 'obvious' way:
00074 
00075        Apparently, "bcl 20,31,$+4" is what should be used to load LR
00076        with the address of the next instruction.
00077        I think this is so that machines that do bl/blr pairing don't
00078        get confused.
00079 
00080      asm ("bcl 20,31,0f ;"
00081          "0: mflr 0 ;"
00082          "lis %0,0b@ha;"
00083          "addi %0,%0,0b@l;"
00084          "subf %0,%0,0"
00085          : "=b" (addr) : : "r0", "lr");
00086 
00087      doesn't work, because the linker doesn't have to (and in fact doesn't)
00088      update the @ha and @l references; the loader (which runs after this
00089      code) will do that.
00090 
00091      Instead, we use the following trick:
00092 
00093      The linker puts the _link-time_ address of _DYNAMIC at the first
00094      word in the GOT. We could branch to that address, if we wanted,
00095      by using an @local reloc; the linker works this out, so it's safe
00096      to use now. We can't, of course, actually branch there, because
00097      we'd cause an illegal instruction exception; so we need to compute
00098      the address ourselves. That gives us the following code: */
00099 
00100   /* Get address of the 'b _DYNAMIC@local'...  */
00101   asm ("bcl 20,31,0f;"
00102        "b _DYNAMIC@local;"
00103        "0:"
00104        : "=l" (branchaddr));
00105 
00106   /* So now work out the difference between where the branch actually points,
00107      and the offset of that location in memory from the start of the file.  */
00108   runtime_dynamic = ((Elf32_Addr) branchaddr
00109                    + ((Elf32_Sword) (*branchaddr << 6 & 0xffffff00) >> 6));
00110 
00111   return runtime_dynamic - elf_machine_dynamic ();
00112 }
00113 
00114 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */
00115 
00116 /* The PLT uses Elf32_Rela relocs.  */
00117 #define elf_machine_relplt elf_machine_rela
00118 
00119 /* Mask identifying addresses reserved for the user program,
00120    where the dynamic linker should not map anything.  */
00121 #define ELF_MACHINE_USER_ADDRESS_MASK     0xf0000000UL
00122 
00123 /* The actual _start code is in dl-start.S.  Use a really
00124    ugly bit of assembler to let dl-start.o see _dl_start.  */
00125 #define RTLD_START asm (".globl _dl_start");
00126 
00127 /* Decide where a relocatable object should be loaded.  */
00128 extern ElfW(Addr)
00129 __elf_preferred_address(struct link_map *loader, size_t maplength,
00130                      ElfW(Addr) mapstartpref);
00131 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
00132   __elf_preferred_address (loader, maplength, mapstartpref)
00133 
00134 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
00135    PLT entries should not be allowed to define the value.
00136    ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
00137    of the main executable's symbols, as for a COPY reloc.  */
00138 /* We never want to use a PLT entry as the destination of a
00139    reloc, when what is being relocated is a branch. This is
00140    partly for efficiency, but mostly so we avoid loops.  */
00141 #if !defined RTLD_BOOTSTRAP || USE___THREAD
00142 #define elf_machine_type_class(type)                    \
00143   ((((type) == R_PPC_JMP_SLOT                           \
00144     || (type) == R_PPC_REL24                            \
00145     || ((type) >= R_PPC_DTPMOD32 /* contiguous TLS */   \
00146        && (type) <= R_PPC_DTPREL32)                     \
00147     || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT)   \
00148    | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
00149 #else
00150 #define elf_machine_type_class(type) \
00151   ((((type) == R_PPC_JMP_SLOT                           \
00152     || (type) == R_PPC_REL24                            \
00153     || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT)   \
00154    | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
00155 #endif
00156 
00157 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries.  */
00158 #define ELF_MACHINE_JMP_SLOT       R_PPC_JMP_SLOT
00159 
00160 /* The PowerPC never uses REL relocations.  */
00161 #define ELF_MACHINE_NO_REL 1
00162 
00163 /* Set up the loaded object described by MAP so its unrelocated PLT
00164    entries will jump to the on-demand fixup code in dl-runtime.c.
00165    Also install a small trampoline to be used by entries that have
00166    been relocated to an address too far away for a single branch.  */
00167 extern int __elf_machine_runtime_setup (struct link_map *map,
00168                                    int lazy, int profile);
00169 
00170 static inline int
00171 elf_machine_runtime_setup (struct link_map *map,
00172                         int lazy, int profile)
00173 {
00174   if (map->l_info[DT_JMPREL] == 0)
00175     return lazy;
00176 
00177   if (map->l_info[DT_PPC(GOT)] == 0)
00178     /* Handle old style PLT.  */
00179     return __elf_machine_runtime_setup (map, lazy, profile);
00180 
00181   /* New style non-exec PLT consisting of an array of addresses.  */
00182   map->l_info[DT_PPC(GOT)]->d_un.d_ptr += map->l_addr;
00183   if (lazy)
00184     {
00185       Elf32_Addr *plt, *got, glink;
00186       Elf32_Word num_plt_entries;
00187       void (*dlrr) (void);
00188       extern void _dl_runtime_resolve (void);
00189       extern void _dl_prof_resolve (void);
00190 
00191       if (__builtin_expect (!profile, 1))
00192        dlrr = _dl_runtime_resolve;
00193       else
00194        {
00195          if (GLRO(dl_profile) != NULL
00196              &&_dl_name_match_p (GLRO(dl_profile), map))
00197            GL(dl_profile_map) = map;
00198          dlrr = _dl_prof_resolve;
00199        }
00200       got = (Elf32_Addr *) map->l_info[DT_PPC(GOT)]->d_un.d_ptr;
00201       glink = got[1];
00202       got[1] = (Elf32_Addr) dlrr;
00203       got[2] = (Elf32_Addr) map;
00204 
00205       /* Relocate everything in .plt by the load address offset.  */
00206       plt = (Elf32_Addr *) D_PTR (map, l_info[DT_PLTGOT]);
00207       num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
00208                       / sizeof (Elf32_Rela));
00209 
00210       /* If a library is prelinked but we have to relocate anyway,
00211         we have to be able to undo the prelinking of .plt section.
00212         The prelinker saved us at got[1] address of .glink
00213         section's start.  */
00214       if (glink)
00215        {
00216          glink += map->l_addr;
00217          while (num_plt_entries-- != 0)
00218            *plt++ = glink, glink += 4;
00219        }
00220       else
00221        while (num_plt_entries-- != 0)
00222          *plt++ += map->l_addr;
00223     }
00224   return lazy;
00225 }
00226 
00227 /* Change the PLT entry whose reloc is 'reloc' to call the actual routine.  */
00228 extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map,
00229                                       const Elf32_Rela *reloc,
00230                                       Elf32_Addr *reloc_addr,
00231                                       Elf32_Addr finaladdr);
00232 
00233 static inline Elf32_Addr
00234 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
00235                      const Elf32_Rela *reloc,
00236                      Elf32_Addr *reloc_addr, Elf64_Addr finaladdr)
00237 {
00238   if (map->l_info[DT_PPC(GOT)] == 0)
00239     /* Handle old style PLT.  */
00240     return __elf_machine_fixup_plt (map, reloc, reloc_addr, finaladdr);
00241 
00242   *reloc_addr = finaladdr;
00243   return finaladdr;
00244 }
00245 
00246 /* Return the final value of a plt relocation.  */
00247 static inline Elf32_Addr
00248 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
00249                      Elf32_Addr value)
00250 {
00251   return value + reloc->r_addend;
00252 }
00253 
00254 
00255 /* Names of the architecture-specific auditing callback functions.  */
00256 #define ARCH_LA_PLTENTER ppc32_gnu_pltenter
00257 #define ARCH_LA_PLTEXIT ppc32_gnu_pltexit
00258 
00259 #endif /* dl_machine_h */
00260 
00261 #ifdef RESOLVE_MAP
00262 
00263 /* Do the actual processing of a reloc, once its target address
00264    has been determined.  */
00265 extern void __process_machine_rela (struct link_map *map,
00266                                 const Elf32_Rela *reloc,
00267                                 struct link_map *sym_map,
00268                                 const Elf32_Sym *sym,
00269                                 const Elf32_Sym *refsym,
00270                                 Elf32_Addr *const reloc_addr,
00271                                 Elf32_Addr finaladdr,
00272                                 int rinfo) attribute_hidden;
00273 
00274 /* Call _dl_signal_error when a resolved value overflows a relocated area.  */
00275 extern void _dl_reloc_overflow (struct link_map *map,
00276                             const char *name,
00277                             Elf32_Addr *const reloc_addr,
00278                             const Elf32_Sym *refsym) attribute_hidden;
00279 
00280 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
00281    LOADADDR is the load address of the object; INFO is an array indexed
00282    by DT_* of the .dynamic section info.  */
00283 
00284 auto inline void __attribute__ ((always_inline))
00285 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
00286                 const Elf32_Sym *sym, const struct r_found_version *version,
00287                 void *const reloc_addr_arg)
00288 {
00289   Elf32_Addr *const reloc_addr = reloc_addr_arg;
00290   const Elf32_Sym *const refsym = sym;
00291   Elf32_Addr value;
00292   const int r_type = ELF32_R_TYPE (reloc->r_info);
00293   struct link_map *sym_map = NULL;
00294 
00295 #ifndef RESOLVE_CONFLICT_FIND_MAP
00296   if (r_type == R_PPC_RELATIVE)
00297     {
00298       *reloc_addr = map->l_addr + reloc->r_addend;
00299       return;
00300     }
00301 
00302   if (__builtin_expect (r_type == R_PPC_NONE, 0))
00303     return;
00304 
00305   /* binutils on ppc32 includes st_value in r_addend for relocations
00306      against local symbols.  */
00307   if (__builtin_expect (ELF32_ST_BIND (sym->st_info) == STB_LOCAL, 0)
00308       && sym->st_shndx != SHN_UNDEF)
00309     value = map->l_addr;
00310   else
00311     {
00312       sym_map = RESOLVE_MAP (&sym, version, r_type);
00313       value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
00314     }
00315   value += reloc->r_addend;
00316 #else
00317   value = reloc->r_addend;
00318 #endif
00319 
00320   /* A small amount of code is duplicated here for speed.  In libc,
00321      more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared
00322      libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or
00323      R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine
00324      wouldn't usually handle).  As an bonus, doing this here allows
00325      the switch statement in __process_machine_rela to work.  */
00326   switch (r_type)
00327     {
00328     case R_PPC_GLOB_DAT:
00329     case R_PPC_ADDR32:
00330       *reloc_addr = value;
00331       break;
00332 
00333 #if (!defined RTLD_BOOTSTRAP || USE___THREAD) \
00334     && !defined RESOLVE_CONFLICT_FIND_MAP
00335 # ifdef RTLD_BOOTSTRAP
00336 #  define NOT_BOOTSTRAP 0
00337 # else
00338 #  define NOT_BOOTSTRAP 1
00339 # endif
00340 
00341     case R_PPC_DTPMOD32:
00342       if (!NOT_BOOTSTRAP)
00343        /* During startup the dynamic linker is always index 1.  */
00344        *reloc_addr = 1;
00345       else if (sym_map != NULL)
00346        /* Get the information from the link map returned by the
00347           RESOLVE_MAP function.  */
00348        *reloc_addr = sym_map->l_tls_modid;
00349       break;
00350     case R_PPC_DTPREL32:
00351       /* During relocation all TLS symbols are defined and used.
00352         Therefore the offset is already correct.  */
00353       if (NOT_BOOTSTRAP && sym_map != NULL)
00354        *reloc_addr = TLS_DTPREL_VALUE (sym, reloc);
00355       break;
00356     case R_PPC_TPREL32:
00357       if (!NOT_BOOTSTRAP || sym_map != NULL)
00358        {
00359          if (NOT_BOOTSTRAP)
00360            CHECK_STATIC_TLS (map, sym_map);
00361          *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
00362        }
00363       break;
00364 #endif
00365 
00366     case R_PPC_JMP_SLOT:
00367 #ifdef RESOLVE_CONFLICT_FIND_MAP
00368       RESOLVE_CONFLICT_FIND_MAP (map, reloc_addr);
00369 #endif
00370       if (map->l_info[DT_PPC(GOT)] != 0)
00371        {
00372          *reloc_addr = value;
00373          break;
00374        }
00375       /* FALLTHROUGH */
00376 
00377     default:
00378       __process_machine_rela (map, reloc, sym_map, sym, refsym,
00379                            reloc_addr, value, r_type);
00380     }
00381 }
00382 
00383 auto inline void __attribute__ ((always_inline))
00384 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
00385                         void *const reloc_addr_arg)
00386 {
00387   Elf32_Addr *const reloc_addr = reloc_addr_arg;
00388   *reloc_addr = l_addr + reloc->r_addend;
00389 }
00390 
00391 auto inline void __attribute__ ((always_inline))
00392 elf_machine_lazy_rel (struct link_map *map,
00393                     Elf32_Addr l_addr, const Elf32_Rela *reloc)
00394 {
00395   /* elf_machine_runtime_setup handles this. */
00396 }
00397 
00398 /* The SVR4 ABI specifies that the JMPREL relocs must be inside the
00399    DT_RELA table.  */
00400 #define ELF_MACHINE_PLTREL_OVERLAP 1
00401 
00402 #endif /* RESOLVE_MAP */