1 // SPDX-License-Identifier: GPL-2.0-only !! 1 /* Kernel module help for Alpha. 2 /* !! 2 Copyright (C) 2002 Richard Henderson. 3 * Kernel module loader for Hexagon << 4 * << 5 * Copyright (c) 2010-2011, The Linux Foundati << 6 */ << 7 3 8 #include <asm/module.h> !! 4 This program is free software; you can redistribute it and/or modify 9 #include <linux/elf.h> !! 5 it under the terms of the GNU General Public License as published by 10 #include <linux/module.h> !! 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 */ 11 #include <linux/moduleloader.h> 18 #include <linux/moduleloader.h> >> 19 #include <linux/elf.h> 12 #include <linux/vmalloc.h> 20 #include <linux/vmalloc.h> >> 21 #include <linux/fs.h> >> 22 #include <linux/string.h> >> 23 #include <linux/kernel.h> >> 24 #include <linux/slab.h> 13 25 14 #if 0 26 #if 0 15 #define DEBUGP printk 27 #define DEBUGP printk 16 #else 28 #else 17 #define DEBUGP(fmt , ...) !! 29 #define DEBUGP(fmt...) 18 #endif 30 #endif 19 31 20 /* !! 32 void * 21 * module_frob_arch_sections - tweak got/plt s !! 33 module_alloc(unsigned long size) 22 * @hdr - pointer to elf header !! 34 { 23 * @sechdrs - pointer to elf load section head !! 35 if (size == 0) 24 * @secstrings - symbol names !! 36 return NULL; 25 * @mod - pointer to module !! 37 return vmalloc(size); 26 */ !! 38 } 27 int module_frob_arch_sections(Elf_Ehdr *hdr, E !! 39 28 char *secstrin !! 40 void 29 struct module !! 41 module_free(struct module *mod, void *module_region) 30 { !! 42 { 31 unsigned int i; !! 43 vfree(module_region); 32 int found = 0; !! 44 } 33 !! 45 34 /* Look for .plt and/or .got.plt and/o !! 46 /* Allocate the GOT at the end of the core sections. */ 35 for (i = 0; i < hdr->e_shnum; i++) { !! 47 36 DEBUGP("Section %d is %s\n", i !! 48 struct got_entry { 37 secstrings + sechdrs[i] !! 49 struct got_entry *next; 38 if (strcmp(secstrings + sechdr !! 50 Elf64_Addr r_offset; 39 found = i+1; !! 51 int got_offset; 40 if (strcmp(secstrings + sechdr !! 52 }; 41 found = i+1; !! 53 42 if (strcmp(secstrings + sechdr !! 54 static inline void 43 found = i+1; !! 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_Addr r_offset = rela->r_offset; >> 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_offset == r_offset) { >> 68 if (g->got_offset == 0) { >> 69 g->got_offset = *poffset; >> 70 *poffset += 8; >> 71 } >> 72 goto found_entry; >> 73 } >> 74 >> 75 g = kmalloc (sizeof (*g), GFP_KERNEL); >> 76 g->next = chains[r_sym].next; >> 77 g->r_offset = r_offset; >> 78 g->got_offset = *poffset; >> 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 } >> 88 >> 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 } >> 111 >> 112 if (!symtab) { >> 113 printk(KERN_ERR "module %s: no symbol table\n", me->name); >> 114 return -ENOEXEC; >> 115 } >> 116 if (!got) { >> 117 printk(KERN_ERR "module %s: no got section\n", me->name); >> 118 return -ENOEXEC; 44 } 119 } 45 120 46 /* At this time, we don't support modu !! 121 nsyms = symtab->sh_size / sizeof(Elf64_Sym); 47 if (found) { !! 122 chains = kmalloc(nsyms * sizeof(struct got_entry), GFP_KERNEL); 48 printk(KERN_WARNING !! 123 memset(chains, 0, nsyms * sizeof(struct got_entry)); 49 "Module '%s' contains !! 124 50 mod->name); !! 125 got->sh_size = 0; 51 /* return -ENOEXEC; */ !! 126 got->sh_addralign = 8; >> 127 got->sh_type = SHT_NOBITS; >> 128 >> 129 /* Examine all LITERAL relocations to find out what GOT entries >> 130 are required. This sizes the GOT section as well. */ >> 131 for (s = sechdrs; s < esechdrs; ++s) >> 132 if (s->sh_type == SHT_RELA) { >> 133 nrela = s->sh_size / sizeof(Elf64_Rela); >> 134 rela = (void *)hdr + s->sh_offset; >> 135 for (i = 0; i < nrela; ++i) >> 136 process_reloc_for_got(rela+i, chains, >> 137 &got->sh_size); >> 138 } >> 139 >> 140 /* Free the memory we allocated. */ >> 141 for (i = 0; i < nsyms; ++i) { >> 142 struct got_entry *g, *n; >> 143 for (g = chains[i].next; g ; g = n) { >> 144 n = g->next; >> 145 kfree(g); >> 146 } 52 } 147 } >> 148 kfree(chains); 53 149 54 return 0; 150 return 0; 55 } 151 } 56 152 57 /* !! 153 int 58 * apply_relocate_add - perform rela relocatio !! 154 apply_relocate(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, 59 * @sechdrs - pointer to section headers !! 155 unsigned int relsec, struct module *me) 60 * @strtab - some sort of start address? !! 156 { 61 * @symindex - symbol index offset or somethin !! 157 printk(KERN_ERR "module %s: REL relocation unsupported\n", me->name); 62 * @relsec - address to relocate to? !! 158 return -ENOEXEC; 63 * @module - pointer to module !! 159 } 64 * << 65 * Perform rela relocations. << 66 */ << 67 int apply_relocate_add(Elf_Shdr *sechdrs, cons << 68 unsigned int symindex, << 69 struct module *module) << 70 { << 71 unsigned int i; << 72 Elf32_Sym *sym; << 73 uint32_t *location; << 74 uint32_t value; << 75 unsigned int nrelocs = sechdrs[relsec] << 76 Elf32_Rela *rela = (void *)sechdrs[rel << 77 Elf32_Word sym_info = sechdrs[relsec]. << 78 Elf32_Sym *sym_base = (Elf32_Sym *) se << 79 void *loc_base = (void *) sechdrs[sym_ << 80 << 81 DEBUGP("Applying relocations in sectio << 82 relsec, sym_info, loc_base); << 83 << 84 for (i = 0; i < nrelocs; i++) { << 85 << 86 /* Symbol to relocate */ << 87 sym = sym_base + ELF32_R_SYM(r << 88 << 89 /* Where to make the change */ << 90 location = loc_base + rela[i]. << 91 160 92 /* `Everything is relative'. * !! 161 int >> 162 apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, >> 163 unsigned int symindex, unsigned int relsec, >> 164 struct module *me) >> 165 { >> 166 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; >> 167 unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela); >> 168 Elf64_Sym *symtab, *sym; >> 169 void *base, *location; >> 170 unsigned long got, gp; >> 171 >> 172 DEBUGP("Applying relocate section %u to %u\n", relsec, >> 173 sechdrs[relsec].sh_info); >> 174 >> 175 base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr; >> 176 symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr; >> 177 >> 178 /* The small sections were sorted to the end of the segment. >> 179 The following should definitely cover them. */ >> 180 gp = (u64)me->module_core + me->core_size - 0x8000; >> 181 got = sechdrs[me->arch.gotsecindex].sh_addr; >> 182 >> 183 for (i = 0; i < n; i++) { >> 184 unsigned long r_sym = ELF64_R_SYM (rela[i].r_info); >> 185 unsigned long r_type = ELF64_R_TYPE (rela[i].r_info); >> 186 unsigned long r_got_offset = r_type >> 8; >> 187 unsigned long value, hi, lo; >> 188 r_type &= 0xff; >> 189 >> 190 /* This is where to make the change. */ >> 191 location = base + rela[i].r_offset; >> 192 >> 193 /* This is the symbol it is referring to. Note that all >> 194 unresolved symbols have been resolved. */ >> 195 sym = symtab + r_sym; 93 value = sym->st_value + rela[i 196 value = sym->st_value + rela[i].r_addend; 94 197 95 DEBUGP("%d: value=%08x loc=%p !! 198 switch (r_type) { 96 i, value, location, ELF !! 199 case R_ALPHA_NONE: 97 sym->st_name ? !! 200 break; 98 &strtab[sym->st_name] : !! 201 case R_ALPHA_REFQUAD: 99 !! 202 /* BUG() can produce misaligned relocations. */ 100 switch (ELF32_R_TYPE(rela[i].r !! 203 ((u32 *)location)[0] = value; 101 case R_HEXAGON_B22_PCREL: { !! 204 ((u32 *)location)[1] = value >> 32; 102 int dist = (int)(value !! 205 break; 103 if ((dist < -0x0080000 !! 206 case R_ALPHA_GPREL32: 104 (dist >= 0x0080000 !! 207 value -= gp; 105 printk(KERN_ER !! 208 if ((int)value != value) 106 "%s: %s !! 209 goto reloc_overflow; 107 module- !! 210 *(u32 *)location = value; 108 "R_HEXA !! 211 break; 109 dist, v !! 212 case R_ALPHA_LITERAL: 110 sym->st !! 213 hi = got + r_got_offset; 111 &strtab !! 214 lo = hi - gp; 112 return -ENOEXE !! 215 if ((short)lo != lo) 113 } !! 216 goto reloc_overflow; 114 DEBUGP("B22_PCREL cont !! 217 *(u16 *)location = lo; 115 *location &= ~0x01ff3f !! 218 *(u64 *)hi = value; 116 *location |= 0x00003ff !! 219 break; 117 *location |= 0x01ff000 !! 220 case R_ALPHA_LITUSE: 118 DEBUGP("Contents after !! 221 break; >> 222 case R_ALPHA_GPDISP: >> 223 value = gp - (u64)location; >> 224 lo = (short)value; >> 225 hi = (int)(value - lo); >> 226 if (hi + lo != value) >> 227 goto reloc_overflow; >> 228 *(u16 *)location = hi >> 16; >> 229 *(u16 *)(location + rela[i].r_addend) = lo; >> 230 break; >> 231 case R_ALPHA_BRSGP: >> 232 /* BRSGP is only allowed to bind to local symbols. >> 233 If the section is undef, this means that the >> 234 value was resolved from somewhere else. */ >> 235 if (sym->st_shndx == SHN_UNDEF) >> 236 goto reloc_overflow; >> 237 /* FALLTHRU */ >> 238 case R_ALPHA_BRADDR: >> 239 value -= (u64)location + 4; >> 240 if (value & 3) >> 241 goto reloc_overflow; >> 242 value = (long)value >> 2; >> 243 if (value + (1<<21) >= 1<<22) >> 244 goto reloc_overflow; >> 245 value &= 0x1fffff; >> 246 value |= *(u32 *)location & ~0x1fffff; >> 247 *(u32 *)location = value; >> 248 break; >> 249 case R_ALPHA_HINT: >> 250 break; >> 251 case R_ALPHA_SREL32: >> 252 value -= (u64)location; >> 253 if ((int)value != value) >> 254 goto reloc_overflow; >> 255 *(u32 *)location = value; >> 256 break; >> 257 case R_ALPHA_SREL64: >> 258 value -= (u64)location; >> 259 *(u64 *)location = value; >> 260 break; >> 261 case R_ALPHA_GPRELHIGH: >> 262 value = (value - gp + 0x8000) >> 16; >> 263 if ((short) value != value) >> 264 goto reloc_overflow; >> 265 *(u16 *)location = value; >> 266 break; >> 267 case R_ALPHA_GPRELLOW: >> 268 value -= gp; >> 269 *(u16 *)location = value; >> 270 break; >> 271 case R_ALPHA_GPREL16: >> 272 value -= gp; >> 273 if ((short) value != value) >> 274 goto reloc_overflow; >> 275 *(u16 *)location = value; 119 break; 276 break; 120 } << 121 case R_HEXAGON_HI16: << 122 value = (value>>16) & << 123 fallthrough; << 124 case R_HEXAGON_LO16: << 125 *location &= ~0x00c03f << 126 *location |= value & 0 << 127 *location |= (value & << 128 break; << 129 case R_HEXAGON_32: << 130 *location = value; << 131 break; << 132 case R_HEXAGON_32_PCREL: << 133 *location = value - (u << 134 break; << 135 case R_HEXAGON_PLT_B22_PCREL: << 136 case R_HEXAGON_GOTOFF_LO16: << 137 case R_HEXAGON_GOTOFF_HI16: << 138 printk(KERN_ERR "%s: G << 139 module->name); << 140 return -ENOEXEC; << 141 default: 277 default: 142 printk(KERN_ERR "%s: u !! 278 printk(KERN_ERR "module %s: Unknown relocation: %lu\n", 143 module->name, !! 279 me->name, r_type); 144 ELF32_R_TYPE(re !! 280 return -ENOEXEC; >> 281 reloc_overflow: >> 282 if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION) >> 283 printk(KERN_ERR >> 284 "module %s: Relocation overflow vs section %d\n", >> 285 me->name, sym->st_shndx); >> 286 else >> 287 printk(KERN_ERR >> 288 "module %s: Relocation overflow vs %s\n", >> 289 me->name, strtab + sym->st_name); 145 return -ENOEXEC; 290 return -ENOEXEC; 146 } 291 } 147 } 292 } >> 293 >> 294 return 0; >> 295 } >> 296 >> 297 int >> 298 module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, >> 299 struct module *me) >> 300 { 148 return 0; 301 return 0; >> 302 } >> 303 >> 304 void >> 305 module_arch_cleanup(struct module *mod) >> 306 { 149 } 307 } 150 308
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.