Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 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@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 | /* Check whether machine flags are valid. */ |
| 61 | bool |
| 62 | ppc_machine_flag_check (GElf_Word flags) |
| 63 | { |
| 64 | return ((flags &~ (EF_PPC_EMB |
| 65 | | EF_PPC_RELOCATABLE |
| 66 | | EF_PPC_RELOCATABLE_LIB)) == 0); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | const char * |
| 71 | ppc_dynamic_tag_name (int64_t tag, char *buf __attribute__ ((unused)), |
| 72 | size_t len __attribute__ ((unused))) |
| 73 | { |
| 74 | switch (tag) |
| 75 | { |
| 76 | case DT_PPC_GOT: |
| 77 | return "PPC_GOT"; |
| 78 | case DT_PPC_OPT: |
| 79 | return "PPC_OPT"; |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | bool |
| 88 | ppc_dynamic_tag_check (int64_t tag) |
| 89 | { |
| 90 | return (tag == DT_PPC_GOT |
| 91 | || tag == DT_PPC_OPT); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* Look for DT_PPC_GOT. */ |
| 96 | static bool |
| 97 | find_dyn_got (Elf *elf, GElf_Addr *addr) |
| 98 | { |
| 99 | size_t phnum; |
| 100 | if (elf_getphdrnum (elf, &phnum) != 0) |
| 101 | return false; |
| 102 | |
| 103 | for (size_t i = 0; i < phnum; ++i) |
| 104 | { |
| 105 | GElf_Phdr phdr_mem; |
| 106 | GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem); |
| 107 | if (phdr == NULL || phdr->p_type != PT_DYNAMIC) |
| 108 | continue; |
| 109 | |
| 110 | Elf_Scn *scn = gelf_offscn (elf, phdr->p_offset); |
| 111 | GElf_Shdr shdr_mem; |
| 112 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 113 | Elf_Data *data = elf_getdata (scn, NULL); |
| 114 | if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC && data != NULL |
| 115 | && shdr->sh_entsize != 0) |
| 116 | for (unsigned int j = 0; j < shdr->sh_size / shdr->sh_entsize; ++j) |
| 117 | { |
| 118 | GElf_Dyn dyn_mem; |
| 119 | GElf_Dyn *dyn = gelf_getdyn (data, j, &dyn_mem); |
| 120 | if (dyn != NULL && dyn->d_tag == DT_PPC_GOT) |
| 121 | { |
| 122 | *addr = dyn->d_un.d_ptr; |
| 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* There is only one PT_DYNAMIC entry. */ |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* Check whether given symbol's st_value and st_size are OK despite failing |
| 136 | normal checks. */ |
| 137 | bool |
| 138 | ppc_check_special_symbol (Elf *elf, GElf_Ehdr *ehdr, const GElf_Sym *sym, |
| 139 | const char *name, const GElf_Shdr *destshdr) |
| 140 | { |
| 141 | if (name == NULL) |
| 142 | return false; |
| 143 | |
| 144 | if (strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 145 | { |
| 146 | /* In -msecure-plt mode, DT_PPC_GOT is present and must match. */ |
| 147 | GElf_Addr gotaddr; |
| 148 | if (find_dyn_got (elf, &gotaddr)) |
| 149 | return sym->st_value == gotaddr; |
| 150 | |
| 151 | /* In -mbss-plt mode, any place in the section is valid. */ |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | const char *sname = elf_strptr (elf, ehdr->e_shstrndx, destshdr->sh_name); |
| 156 | if (sname == NULL) |
| 157 | return false; |
| 158 | |
| 159 | /* Small data area. Normally points to .sdata, in which case we |
| 160 | check it is at an offset of 0x8000. It might however fall in the |
| 161 | .data section, in which case we cannot check the offset. The |
| 162 | size always should be zero. */ |
| 163 | if (strcmp (name, "_SDA_BASE_") == 0) |
| 164 | return (((strcmp (sname, ".sdata") == 0 |
| 165 | && sym->st_value == destshdr->sh_addr + 0x8000) |
| 166 | || strcmp (sname, ".data") == 0) |
| 167 | && sym->st_size == 0); |
| 168 | |
| 169 | if (strcmp (name, "_SDA2_BASE_") == 0) |
| 170 | return (strcmp (sname, ".sdata2") == 0 |
| 171 | && sym->st_value == destshdr->sh_addr + 0x8000 |
| 172 | && sym->st_size == 0); |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /* Check if backend uses a bss PLT in this file. */ |
| 179 | bool |
| 180 | ppc_bss_plt_p (Elf *elf) |
| 181 | { |
| 182 | GElf_Addr addr; |
| 183 | return ! find_dyn_got (elf, &addr); |
| 184 | } |