1 // SPDX-License-Identifier: GPL-2.0-only !! 1 /* Kernel module help for Alpha. 2 /* !! 2 Copyright (C) 2002 Richard Henderson. 3 * Copyright (C) 2007-2009 Michal Simek <monst << 4 * Copyright (C) 2007-2009 PetaLogix << 5 */ << 6 3 7 #include <linux/export.h> !! 4 This program is free software; you can redistribute it and/or modify >> 5 it under the terms of the GNU General Public License as published by >> 6 the Free Software Foundation; either version 2 of the License, or >> 7 (at your option) any later version. >> 8 >> 9 This program is distributed in the hope that it will be useful, >> 10 but WITHOUT ANY WARRANTY; without even the implied warranty of >> 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> 12 GNU General Public License for more details. >> 13 >> 14 You should have received a copy of the GNU General Public License >> 15 along with this program; if not, write to the Free Software >> 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA >> 17 */ 8 #include <linux/moduleloader.h> 18 #include <linux/moduleloader.h> 9 #include <linux/kernel.h> << 10 #include <linux/elf.h> 19 #include <linux/elf.h> 11 #include <linux/vmalloc.h> 20 #include <linux/vmalloc.h> 12 #include <linux/fs.h> 21 #include <linux/fs.h> 13 #include <linux/string.h> 22 #include <linux/string.h> 14 #include <linux/pgtable.h> !! 23 #include <linux/kernel.h> >> 24 #include <linux/slab.h> 15 25 16 #include <asm/cacheflush.h> !! 26 #if 0 >> 27 #define DEBUGP printk >> 28 #else >> 29 #define DEBUGP(fmt...) >> 30 #endif 17 31 18 int apply_relocate_add(Elf32_Shdr *sechdrs, co !! 32 void * 19 unsigned int symindex, unsigned int re !! 33 module_alloc(unsigned long size) 20 { 34 { >> 35 if (size == 0) >> 36 return NULL; >> 37 return vmalloc(size); >> 38 } 21 39 22 unsigned int i; !! 40 void 23 Elf32_Rela *rela = (void *)sechdrs[rel !! 41 module_free(struct module *mod, void *module_region) 24 Elf32_Sym *sym; !! 42 { 25 unsigned long int *location; !! 43 vfree(module_region); 26 unsigned long int value; !! 44 } 27 45 28 pr_debug("Applying add relocation sect !! 46 /* Allocate the GOT at the end of the core sections. */ 29 relsec, sechdrs[relsec].sh_inf << 30 47 31 for (i = 0; i < sechdrs[relsec].sh_siz !! 48 struct got_entry { >> 49 struct got_entry *next; >> 50 Elf64_Sxword r_addend; >> 51 int got_offset; >> 52 }; >> 53 >> 54 static inline void >> 55 process_reloc_for_got(Elf64_Rela *rela, >> 56 struct got_entry *chains, Elf64_Xword *poffset) >> 57 { >> 58 unsigned long r_sym = ELF64_R_SYM (rela->r_info); >> 59 unsigned long r_type = ELF64_R_TYPE (rela->r_info); >> 60 Elf64_Sxword r_addend = rela->r_addend; >> 61 struct got_entry *g; >> 62 >> 63 if (r_type != R_ALPHA_LITERAL) >> 64 return; >> 65 >> 66 for (g = chains + r_sym; g ; g = g->next) >> 67 if (g->r_addend == r_addend) { >> 68 if (g->got_offset == 0) { >> 69 g->got_offset = *poffset; >> 70 *poffset += 8; >> 71 } >> 72 goto found_entry; >> 73 } 32 74 33 location = (void *)sechdrs[sec !! 75 g = kmalloc (sizeof (*g), GFP_KERNEL); 34 rela[i].r_offs !! 76 g->next = chains[r_sym].next; 35 sym = (Elf32_Sym *)sechdrs[sym !! 77 g->r_addend = r_addend; 36 ELF32_R_SYM(rela[i].r_ !! 78 g->got_offset = *poffset; 37 value = sym->st_value + rela[i !! 79 *poffset += 8; >> 80 chains[r_sym].next = g; >> 81 >> 82 found_entry: >> 83 /* Trick: most of the ELF64_R_TYPE field is unused. There are >> 84 42 valid relocation types, and a 32-bit field. Co-opt the >> 85 bits above 256 to store the got offset for this reloc. */ >> 86 rela->r_info |= g->got_offset << 8; >> 87 } 38 88 39 switch (ELF32_R_TYPE(rela[i].r !! 89 int >> 90 module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs, >> 91 char *secstrings, struct module *me) >> 92 { >> 93 struct got_entry *chains; >> 94 Elf64_Rela *rela; >> 95 Elf64_Shdr *esechdrs, *symtab, *s, *got; >> 96 unsigned long nsyms, nrela, i; >> 97 >> 98 esechdrs = sechdrs + hdr->e_shnum; >> 99 symtab = got = NULL; >> 100 >> 101 /* Find out how large the symbol table is. Allocate one got_entry >> 102 head per symbol. Normally this will be enough, but not always. >> 103 We'll chain different offsets for the symbol down each head. */ >> 104 for (s = sechdrs; s < esechdrs; ++s) >> 105 if (s->sh_type == SHT_SYMTAB) >> 106 symtab = s; >> 107 else if (!strcmp(".got", secstrings + s->sh_name)) { >> 108 got = s; >> 109 me->arch.gotsecindex = s - sechdrs; >> 110 } 40 111 41 /* !! 112 if (!symtab) { 42 * Be careful! mb-gcc / mb-ld !! 113 printk(KERN_ERR "module %s: no symbol table\n", me->name); 43 * text and the reloc table. I !! 114 return -ENOEXEC; 44 * read the current contents o !! 115 } 45 * then store the result back !! 116 if (!got) { 46 */ !! 117 printk(KERN_ERR "module %s: no got section\n", me->name); >> 118 return -ENOEXEC; >> 119 } 47 120 48 case R_MICROBLAZE_32: !! 121 nsyms = symtab->sh_size / sizeof(Elf64_Sym); 49 *location = value; !! 122 chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL); 50 break; !! 123 if (!chains) { >> 124 printk(KERN_ERR >> 125 "module %s: no memory for symbol chain buffer\n", >> 126 me->name); >> 127 return -ENOMEM; >> 128 } 51 129 52 case R_MICROBLAZE_64: !! 130 got->sh_size = 0; 53 location[0] = (locatio !! 131 got->sh_addralign = 8; 54 (value !! 132 got->sh_type = SHT_NOBITS; 55 location[1] = (locatio !! 133 56 (value !! 134 /* Examine all LITERAL relocations to find out what GOT entries 57 break; !! 135 are required. This sizes the GOT section as well. */ >> 136 for (s = sechdrs; s < esechdrs; ++s) >> 137 if (s->sh_type == SHT_RELA) { >> 138 nrela = s->sh_size / sizeof(Elf64_Rela); >> 139 rela = (void *)hdr + s->sh_offset; >> 140 for (i = 0; i < nrela; ++i) >> 141 process_reloc_for_got(rela+i, chains, >> 142 &got->sh_size); >> 143 } 58 144 59 case R_MICROBLAZE_64_PCREL: !! 145 /* Free the memory we allocated. */ 60 value -= (unsigned lon !! 146 for (i = 0; i < nsyms; ++i) { 61 location[0] = (locatio !! 147 struct got_entry *g, *n; 62 (value !! 148 for (g = chains[i].next; g ; g = n) { 63 location[1] = (locatio !! 149 n = g->next; 64 (value !! 150 kfree(g); 65 pr_debug("R_MICROBLAZE !! 151 } 66 value); !! 152 } 67 break; !! 153 kfree(chains); 68 154 69 case R_MICROBLAZE_32_PCREL_LO: !! 155 return 0; 70 pr_debug("R_MICROBLAZE !! 156 } 71 break; << 72 157 73 case R_MICROBLAZE_64_NONE: !! 158 int 74 pr_debug("R_MICROBLAZE !! 159 apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, 75 break; !! 160 unsigned int relsec, struct module *me) >> 161 { >> 162 printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name); >> 163 return -ENOEXEC; >> 164 } 76 165 77 case R_MICROBLAZE_NONE: !! 166 int 78 pr_debug("R_MICROBLAZE !! 167 apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, 79 break; !! 168 unsigned int symindex, unsigned int relsec, >> 169 struct module *me) >> 170 { >> 171 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; >> 172 unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela); >> 173 Elf64_Sym *symtab, *sym; >> 174 void *base, *location; >> 175 unsigned long got, gp; >> 176 >> 177 DEBUGP("Applying relocate section %u to %u\n", relsec, >> 178 sechdrs[relsec].sh_info); >> 179 >> 180 base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr; >> 181 symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr; >> 182 >> 183 /* The small sections were sorted to the end of the segment. >> 184 The following should definitely cover them. */ >> 185 gp = (u64)me->module_core + me->core_size - 0x8000; >> 186 got = sechdrs[me->arch.gotsecindex].sh_addr; >> 187 >> 188 for (i = 0; i < n; i++) { >> 189 unsigned long r_sym = ELF64_R_SYM (rela[i].r_info); >> 190 unsigned long r_type = ELF64_R_TYPE (rela[i].r_info); >> 191 unsigned long r_got_offset = r_type >> 8; >> 192 unsigned long value, hi, lo; >> 193 r_type &= 0xff; >> 194 >> 195 /* This is where to make the change. */ >> 196 location = base + rela[i].r_offset; >> 197 >> 198 /* This is the symbol it is referring to. Note that all >> 199 unresolved symbols have been resolved. */ >> 200 sym = symtab + r_sym; >> 201 value = sym->st_value + rela[i].r_addend; 80 202 >> 203 switch (r_type) { >> 204 case R_ALPHA_NONE: >> 205 break; >> 206 case R_ALPHA_REFQUAD: >> 207 /* BUG() can produce misaligned relocations. */ >> 208 ((u32 *)location)[0] = value; >> 209 ((u32 *)location)[1] = value >> 32; >> 210 break; >> 211 case R_ALPHA_GPREL32: >> 212 value -= gp; >> 213 if ((int)value != value) >> 214 goto reloc_overflow; >> 215 *(u32 *)location = value; >> 216 break; >> 217 case R_ALPHA_LITERAL: >> 218 hi = got + r_got_offset; >> 219 lo = hi - gp; >> 220 if ((short)lo != lo) >> 221 goto reloc_overflow; >> 222 *(u16 *)location = lo; >> 223 *(u64 *)hi = value; >> 224 break; >> 225 case R_ALPHA_LITUSE: >> 226 break; >> 227 case R_ALPHA_GPDISP: >> 228 value = gp - (u64)location; >> 229 lo = (short)value; >> 230 hi = (int)(value - lo); >> 231 if (hi + lo != value) >> 232 goto reloc_overflow; >> 233 *(u16 *)location = hi >> 16; >> 234 *(u16 *)(location + rela[i].r_addend) = lo; >> 235 break; >> 236 case R_ALPHA_BRSGP: >> 237 /* BRSGP is only allowed to bind to local symbols. >> 238 If the section is undef, this means that the >> 239 value was resolved from somewhere else. */ >> 240 if (sym->st_shndx == SHN_UNDEF) >> 241 goto reloc_overflow; >> 242 if ((sym->st_other & STO_ALPHA_STD_GPLOAD) == >> 243 STO_ALPHA_STD_GPLOAD) >> 244 /* Omit the prologue. */ >> 245 value += 8; >> 246 /* FALLTHRU */ >> 247 case R_ALPHA_BRADDR: >> 248 value -= (u64)location + 4; >> 249 if (value & 3) >> 250 goto reloc_overflow; >> 251 value = (long)value >> 2; >> 252 if (value + (1<<21) >= 1<<22) >> 253 goto reloc_overflow; >> 254 value &= 0x1fffff; >> 255 value |= *(u32 *)location & ~0x1fffff; >> 256 *(u32 *)location = value; >> 257 break; >> 258 case R_ALPHA_HINT: >> 259 break; >> 260 case R_ALPHA_SREL32: >> 261 value -= (u64)location; >> 262 if ((int)value != value) >> 263 goto reloc_overflow; >> 264 *(u32 *)location = value; >> 265 break; >> 266 case R_ALPHA_SREL64: >> 267 value -= (u64)location; >> 268 *(u64 *)location = value; >> 269 break; >> 270 case R_ALPHA_GPRELHIGH: >> 271 value = (long)(value - gp + 0x8000) >> 16; >> 272 if ((short) value != value) >> 273 goto reloc_overflow; >> 274 *(u16 *)location = value; >> 275 break; >> 276 case R_ALPHA_GPRELLOW: >> 277 value -= gp; >> 278 *(u16 *)location = value; >> 279 break; >> 280 case R_ALPHA_GPREL16: >> 281 value -= gp; >> 282 if ((short) value != value) >> 283 goto reloc_overflow; >> 284 *(u16 *)location = value; >> 285 break; 81 default: 286 default: 82 pr_err("module %s: Unk !! 287 printk(KERN_ERR "module %s: Unknown relocation: %lu\n", 83 module->name, !! 288 me->name, r_type); 84 ELF32_R_TYPE(r !! 289 return -ENOEXEC; >> 290 reloc_overflow: >> 291 if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION) >> 292 printk(KERN_ERR >> 293 "module %s: Relocation (type %lu) overflow vs section %d\n", >> 294 me->name, r_type, sym->st_shndx); >> 295 else >> 296 printk(KERN_ERR >> 297 "module %s: Relocation (type %lu) overflow vs %s\n", >> 298 me->name, r_type, strtab + sym->st_name); 85 return -ENOEXEC; 299 return -ENOEXEC; 86 } 300 } 87 } 301 } >> 302 88 return 0; 303 return 0; 89 } 304 } 90 305 91 int module_finalize(const Elf32_Ehdr *hdr, con !! 306 int 92 struct module *module) !! 307 module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, >> 308 struct module *me) 93 { 309 { 94 flush_dcache(); << 95 return 0; 310 return 0; >> 311 } >> 312 >> 313 void >> 314 module_arch_cleanup(struct module *mod) >> 315 { 96 } 316 } 97 317
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.