Home | History | Annotate | Download | only in backends
      1 /* PPC specific symbolic name handling.
      2    Copyright (C) 2004, 2005, 2007, 2014, 2015 Red Hat, Inc.
      3    This file is part of elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2004.
      5 
      6    This file is free software; you can redistribute it and/or modify
      7    it under the terms of either
      8 
      9      * the GNU Lesser General Public License as published by the Free
     10        Software Foundation; either version 3 of the License, or (at
     11        your option) any later version
     12 
     13    or
     14 
     15      * the GNU General Public License as published by the Free
     16        Software Foundation; either version 2 of the License, or (at
     17        your option) any later version
     18 
     19    or both in parallel, as here.
     20 
     21    elfutils is distributed in the hope that it will be useful, but
     22    WITHOUT ANY WARRANTY; without even the implied warranty of
     23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     24    General Public License for more details.
     25 
     26    You should have received copies of the GNU General Public License and
     27    the GNU Lesser General Public License along with this program.  If
     28    not, see <http://www.gnu.org/licenses/>.  */
     29 
     30 #ifdef HAVE_CONFIG_H
     31 # include <config.h>
     32 #endif
     33 
     34 #include <assert.h>
     35 #include <elf.h>
     36 #include <stddef.h>
     37 #include <string.h>
     38 
     39 #define BACKEND		ppc_
     40 #include "libebl_CPU.h"
     41 
     42 
     43 /* Check for the simple reloc types.  */
     44 Elf_Type
     45 ppc_reloc_simple_type (Ebl *ebl __attribute__ ((unused)), int type)
     46 {
     47   switch (type)
     48     {
     49     case R_PPC_ADDR32:
     50     case R_PPC_UADDR32:
     51       return ELF_T_WORD;
     52     case R_PPC_UADDR16:
     53       return ELF_T_HALF;
     54     default:
     55       return ELF_T_NUM;
     56     }
     57 }
     58 
     59 
     60 const char *
     61 ppc_dynamic_tag_name (int64_t tag, char *buf __attribute__ ((unused)),
     62 		      size_t len __attribute__ ((unused)))
     63 {
     64   switch (tag)
     65     {
     66     case DT_PPC_GOT:
     67       return "PPC_GOT";
     68     default:
     69       break;
     70     }
     71   return NULL;
     72 }
     73 
     74 
     75 bool
     76 ppc_dynamic_tag_check (int64_t tag)
     77 {
     78   return tag == DT_PPC_GOT;
     79 }
     80 
     81 
     82 /* Look for DT_PPC_GOT.  */
     83 static bool
     84 find_dyn_got (Elf *elf, GElf_Addr *addr)
     85 {
     86   size_t phnum;
     87   if (elf_getphdrnum (elf, &phnum) != 0)
     88     return false;
     89 
     90   for (size_t i = 0; i < phnum; ++i)
     91     {
     92       GElf_Phdr phdr_mem;
     93       GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
     94       if (phdr == NULL || phdr->p_type != PT_DYNAMIC)
     95 	continue;
     96 
     97       Elf_Scn *scn = gelf_offscn (elf, phdr->p_offset);
     98       GElf_Shdr shdr_mem;
     99       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    100       Elf_Data *data = elf_getdata (scn, NULL);
    101       if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC && data != NULL
    102 	  && shdr->sh_entsize != 0)
    103 	for (unsigned int j = 0; j < shdr->sh_size / shdr->sh_entsize; ++j)
    104 	  {
    105 	    GElf_Dyn dyn_mem;
    106 	    GElf_Dyn *dyn = gelf_getdyn (data, j, &dyn_mem);
    107 	    if (dyn != NULL && dyn->d_tag == DT_PPC_GOT)
    108 	      {
    109 		*addr = dyn->d_un.d_ptr;
    110 		return true;
    111 	      }
    112 	  }
    113 
    114       /* There is only one PT_DYNAMIC entry.  */
    115       break;
    116     }
    117 
    118   return false;
    119 }
    120 
    121 
    122 /* Check whether given symbol's st_value and st_size are OK despite failing
    123    normal checks.  */
    124 bool
    125 ppc_check_special_symbol (Elf *elf, GElf_Ehdr *ehdr, const GElf_Sym *sym,
    126 			  const char *name, const GElf_Shdr *destshdr)
    127 {
    128   if (name == NULL)
    129     return false;
    130 
    131   if (strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
    132     {
    133       /* In -msecure-plt mode, DT_PPC_GOT is present and must match.  */
    134       GElf_Addr gotaddr;
    135       if (find_dyn_got (elf, &gotaddr))
    136 	return sym->st_value == gotaddr;
    137 
    138       /* In -mbss-plt mode, any place in the section is valid.  */
    139       return true;
    140     }
    141 
    142   const char *sname = elf_strptr (elf, ehdr->e_shstrndx, destshdr->sh_name);
    143   if (sname == NULL)
    144     return false;
    145 
    146   /* Small data area.  Normally points to .sdata, in which case we
    147      check it is at an offset of 0x8000.  It might however fall in the
    148      .data section, in which case we cannot check the offset.  The
    149      size always should be zero.  */
    150   if (strcmp (name, "_SDA_BASE_") == 0)
    151     return (((strcmp (sname, ".sdata") == 0
    152 	      && sym->st_value == destshdr->sh_addr + 0x8000)
    153 	     || strcmp (sname, ".data") == 0)
    154 	    && sym->st_size == 0);
    155 
    156   if (strcmp (name, "_SDA2_BASE_") == 0)
    157     return (strcmp (sname, ".sdata2") == 0
    158 	    && sym->st_value == destshdr->sh_addr + 0x8000
    159 	    && sym->st_size == 0);
    160 
    161   return false;
    162 }
    163 
    164 
    165 /* Check if backend uses a bss PLT in this file.  */
    166 bool
    167 ppc_bss_plt_p (Elf *elf)
    168 {
    169   GElf_Addr addr;
    170   return ! find_dyn_got (elf, &addr);
    171 }
    172