|
| 1 | +/* |
| 2 | + * Utility functions to work with ELF files. |
| 3 | + * |
| 4 | + * Copyright (C) 2016, IBM Corporation |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2, or (at your option) |
| 9 | + * any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <asm/ppc-opcode.h> |
| 18 | +#include <asm/elf_util.h> |
| 19 | + |
| 20 | +/* |
| 21 | + * We just need to use the functions defined in <asm/module.h>, so just declare |
| 22 | + * struct module here and avoid having to import <linux/module.h>. |
| 23 | + */ |
| 24 | +struct module; |
| 25 | +#include <asm/module.h> |
| 26 | + |
| 27 | +#ifdef PPC64_ELF_ABI_v2 |
| 28 | +/* PowerPC64 specific values for the Elf64_Sym st_other field. */ |
| 29 | +#define STO_PPC64_LOCAL_BIT 5 |
| 30 | +#define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT) |
| 31 | +#define PPC64_LOCAL_ENTRY_OFFSET(other) \ |
| 32 | + (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2) |
| 33 | + |
| 34 | +static unsigned int local_entry_offset(const Elf64_Sym *sym) |
| 35 | +{ |
| 36 | + /* sym->st_other indicates offset to local entry point |
| 37 | + * (otherwise it will assume r12 is the address of the start |
| 38 | + * of function and try to derive r2 from it). */ |
| 39 | + return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other); |
| 40 | +} |
| 41 | +#else |
| 42 | +static unsigned int local_entry_offset(const Elf64_Sym *sym) |
| 43 | +{ |
| 44 | + return 0; |
| 45 | +} |
| 46 | +#endif |
| 47 | + |
| 48 | +#ifdef CC_USING_MPROFILE_KERNEL |
| 49 | +/* |
| 50 | + * In case of _mcount calls, do not save the current callee's TOC (in r2) into |
| 51 | + * the original caller's stack frame. If we did we would clobber the saved TOC |
| 52 | + * value of the original caller. |
| 53 | + */ |
| 54 | +static void squash_toc_save_inst(const char *name, unsigned long addr) |
| 55 | +{ |
| 56 | + struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr; |
| 57 | + |
| 58 | + /* Only for calls to _mcount */ |
| 59 | + if (strcmp("_mcount", name) != 0) |
| 60 | + return; |
| 61 | + |
| 62 | + stub->jump[2] = PPC_INST_NOP; |
| 63 | +} |
| 64 | +#else |
| 65 | +static void squash_toc_save_inst(const char *name, unsigned long addr) { } |
| 66 | +#endif |
| 67 | + |
| 68 | +/** |
| 69 | + * elf64_apply_relocate_add - apply 64 bit RELA relocations |
| 70 | + * @elf_info: Support information for the ELF binary being relocated. |
| 71 | + * @strtab: String table for the associated symbol table. |
| 72 | + * @symindex: Section header index for the associated symbol table. |
| 73 | + * @relsec: Section header index for the relocations to apply. |
| 74 | + * @obj_name: The name of the ELF binary, for information messages. |
| 75 | + */ |
| 76 | +int elf64_apply_relocate_add(const struct elf_info *elf_info, |
| 77 | + const char *strtab, unsigned int symindex, |
| 78 | + unsigned int relsec, const char *obj_name) |
| 79 | +{ |
| 80 | + unsigned int i; |
| 81 | + Elf64_Shdr *sechdrs = elf_info->sechdrs; |
| 82 | + Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; |
| 83 | + Elf64_Sym *sym; |
| 84 | + unsigned long *location; |
| 85 | + unsigned long value; |
| 86 | + |
| 87 | + |
| 88 | + for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { |
| 89 | + /* This is where to make the change */ |
| 90 | + location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 91 | + + rela[i].r_offset; |
| 92 | + /* This is the symbol it is referring to */ |
| 93 | + sym = (Elf64_Sym *)sechdrs[symindex].sh_addr |
| 94 | + + ELF64_R_SYM(rela[i].r_info); |
| 95 | + |
| 96 | + pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n", |
| 97 | + location, (long)ELF64_R_TYPE(rela[i].r_info), |
| 98 | + strtab + sym->st_name, (unsigned long)sym->st_value, |
| 99 | + (long)rela[i].r_addend); |
| 100 | + |
| 101 | + /* `Everything is relative'. */ |
| 102 | + value = sym->st_value + rela[i].r_addend; |
| 103 | + |
| 104 | + switch (ELF64_R_TYPE(rela[i].r_info)) { |
| 105 | + case R_PPC64_ADDR32: |
| 106 | + /* Simply set it */ |
| 107 | + *(u32 *)location = value; |
| 108 | + break; |
| 109 | + |
| 110 | + case R_PPC64_ADDR64: |
| 111 | + /* Simply set it */ |
| 112 | + *(unsigned long *)location = value; |
| 113 | + break; |
| 114 | + |
| 115 | + case R_PPC64_TOC: |
| 116 | + *(unsigned long *)location = my_r2(elf_info); |
| 117 | + break; |
| 118 | + |
| 119 | + case R_PPC64_TOC16: |
| 120 | + /* Subtract TOC pointer */ |
| 121 | + value -= my_r2(elf_info); |
| 122 | + if (value + 0x8000 > 0xffff) { |
| 123 | + pr_err("%s: bad TOC16 relocation (0x%lx)\n", |
| 124 | + obj_name, value); |
| 125 | + return -ENOEXEC; |
| 126 | + } |
| 127 | + *((uint16_t *) location) |
| 128 | + = (*((uint16_t *) location) & ~0xffff) |
| 129 | + | (value & 0xffff); |
| 130 | + break; |
| 131 | + |
| 132 | + case R_PPC64_TOC16_LO: |
| 133 | + /* Subtract TOC pointer */ |
| 134 | + value -= my_r2(elf_info); |
| 135 | + *((uint16_t *) location) |
| 136 | + = (*((uint16_t *) location) & ~0xffff) |
| 137 | + | (value & 0xffff); |
| 138 | + break; |
| 139 | + |
| 140 | + case R_PPC64_TOC16_DS: |
| 141 | + /* Subtract TOC pointer */ |
| 142 | + value -= my_r2(elf_info); |
| 143 | + if ((value & 3) != 0 || value + 0x8000 > 0xffff) { |
| 144 | + pr_err("%s: bad TOC16_DS relocation (0x%lx)\n", |
| 145 | + obj_name, value); |
| 146 | + return -ENOEXEC; |
| 147 | + } |
| 148 | + *((uint16_t *) location) |
| 149 | + = (*((uint16_t *) location) & ~0xfffc) |
| 150 | + | (value & 0xfffc); |
| 151 | + break; |
| 152 | + |
| 153 | + case R_PPC64_TOC16_LO_DS: |
| 154 | + /* Subtract TOC pointer */ |
| 155 | + value -= my_r2(elf_info); |
| 156 | + if ((value & 3) != 0) { |
| 157 | + pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n", |
| 158 | + obj_name, value); |
| 159 | + return -ENOEXEC; |
| 160 | + } |
| 161 | + *((uint16_t *) location) |
| 162 | + = (*((uint16_t *) location) & ~0xfffc) |
| 163 | + | (value & 0xfffc); |
| 164 | + break; |
| 165 | + |
| 166 | + case R_PPC64_TOC16_HA: |
| 167 | + /* Subtract TOC pointer */ |
| 168 | + value -= my_r2(elf_info); |
| 169 | + value = ((value + 0x8000) >> 16); |
| 170 | + *((uint16_t *) location) |
| 171 | + = (*((uint16_t *) location) & ~0xffff) |
| 172 | + | (value & 0xffff); |
| 173 | + break; |
| 174 | + |
| 175 | + case R_PPC_REL24: |
| 176 | + /* FIXME: Handle weak symbols here --RR */ |
| 177 | + if (sym->st_shndx == SHN_UNDEF) { |
| 178 | + /* External: go via stub */ |
| 179 | + value = stub_for_addr(elf_info, value, obj_name); |
| 180 | + if (!value) |
| 181 | + return -ENOENT; |
| 182 | + if (!restore_r2((u32 *)location + 1, obj_name)) |
| 183 | + return -ENOEXEC; |
| 184 | + |
| 185 | + squash_toc_save_inst(strtab + sym->st_name, value); |
| 186 | + } else |
| 187 | + value += local_entry_offset(sym); |
| 188 | + |
| 189 | + /* Convert value to relative */ |
| 190 | + value -= (unsigned long)location; |
| 191 | + if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ |
| 192 | + pr_err("%s: REL24 %li out of range!\n", |
| 193 | + obj_name, (long int)value); |
| 194 | + return -ENOEXEC; |
| 195 | + } |
| 196 | + |
| 197 | + /* Only replace bits 2 through 26 */ |
| 198 | + *(uint32_t *)location |
| 199 | + = (*(uint32_t *)location & ~0x03fffffc) |
| 200 | + | (value & 0x03fffffc); |
| 201 | + break; |
| 202 | + |
| 203 | + case R_PPC64_REL64: |
| 204 | + /* 64 bits relative (used by features fixups) */ |
| 205 | + *location = value - (unsigned long)location; |
| 206 | + break; |
| 207 | + |
| 208 | + case R_PPC64_TOCSAVE: |
| 209 | + /* |
| 210 | + * Marker reloc indicates we don't have to save r2. |
| 211 | + * That would only save us one instruction, so ignore |
| 212 | + * it. |
| 213 | + */ |
| 214 | + break; |
| 215 | + |
| 216 | + case R_PPC64_ENTRY: |
| 217 | + /* |
| 218 | + * Optimize ELFv2 large code model entry point if |
| 219 | + * the TOC is within 2GB range of current location. |
| 220 | + */ |
| 221 | + value = my_r2(elf_info) - (unsigned long)location; |
| 222 | + if (value + 0x80008000 > 0xffffffff) |
| 223 | + break; |
| 224 | + /* |
| 225 | + * Check for the large code model prolog sequence: |
| 226 | + * ld r2, ...(r12) |
| 227 | + * add r2, r2, r12 |
| 228 | + */ |
| 229 | + if ((((uint32_t *)location)[0] & ~0xfffc) |
| 230 | + != 0xe84c0000) |
| 231 | + break; |
| 232 | + if (((uint32_t *)location)[1] != 0x7c426214) |
| 233 | + break; |
| 234 | + /* |
| 235 | + * If found, replace it with: |
| 236 | + * addis r2, r12, (.TOC.-func)@ha |
| 237 | + * addi r2, r12, (.TOC.-func)@l |
| 238 | + */ |
| 239 | + ((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value); |
| 240 | + ((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value); |
| 241 | + break; |
| 242 | + |
| 243 | + case R_PPC64_REL16_HA: |
| 244 | + /* Subtract location pointer */ |
| 245 | + value -= (unsigned long)location; |
| 246 | + value = ((value + 0x8000) >> 16); |
| 247 | + *((uint16_t *) location) |
| 248 | + = (*((uint16_t *) location) & ~0xffff) |
| 249 | + | (value & 0xffff); |
| 250 | + break; |
| 251 | + |
| 252 | + case R_PPC64_REL16_LO: |
| 253 | + /* Subtract location pointer */ |
| 254 | + value -= (unsigned long)location; |
| 255 | + *((uint16_t *) location) |
| 256 | + = (*((uint16_t *) location) & ~0xffff) |
| 257 | + | (value & 0xffff); |
| 258 | + break; |
| 259 | + |
| 260 | + default: |
| 261 | + pr_err("%s: Unknown ADD relocation: %lu\n", |
| 262 | + obj_name, |
| 263 | + (unsigned long)ELF64_R_TYPE(rela[i].r_info)); |
| 264 | + return -ENOEXEC; |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + return 0; |
| 269 | +} |
0 commit comments