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 /* Allocate the GOT at the end of the core sections. */ 21 * module_frob_arch_sections - tweak got/plt s !! 33 22 * @hdr - pointer to elf header !! 34 struct got_entry { 23 * @sechdrs - pointer to elf load section head !! 35 struct got_entry *next; 24 * @secstrings - symbol names !! 36 Elf64_Sxword r_addend; 25 * @mod - pointer to module !! 37 int got_offset; 26 */ !! 38 }; 27 int module_frob_arch_sections(Elf_Ehdr *hdr, E !! 39 28 char *secstrin !! 40 static inline void 29 struct module !! 41 process_reloc_for_got(Elf64_Rela *rela, >> 42 struct got_entry *chains, Elf64_Xword *poffset) >> 43 { >> 44 unsigned long r_sym = ELF64_R_SYM (rela->r_info); >> 45 unsigned long r_type = ELF64_R_TYPE (rela->r_info); >> 46 Elf64_Sxword r_addend = rela->r_addend; >> 47 struct got_entry *g; >> 48 >> 49 if (r_type != R_ALPHA_LITERAL) >> 50 return; >> 51 >> 52 for (g = chains + r_sym; g ; g = g->next) >> 53 if (g->r_addend == r_addend) { >> 54 if (g->got_offset == 0) { >> 55 g->got_offset = *poffset; >> 56 *poffset += 8; >> 57 } >> 58 goto found_entry; >> 59 } >> 60 >> 61 g = kmalloc (sizeof (*g), GFP_KERNEL); >> 62 g->next = chains[r_sym].next; >> 63 g->r_addend = r_addend; >> 64 g->got_offset = *poffset; >> 65 *poffset += 8; >> 66 chains[r_sym].next = g; >> 67 >> 68 found_entry: >> 69 /* Trick: most of the ELF64_R_TYPE field is unused. There are >> 70 42 valid relocation types, and a 32-bit field. Co-opt the >> 71 bits above 256 to store the got offset for this reloc. */ >> 72 rela->r_info |= g->got_offset << 8; >> 73 } >> 74 >> 75 int >> 76 module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs, >> 77 char *secstrings, struct module *me) 30 { 78 { 31 unsigned int i; !! 79 struct got_entry *chains; 32 int found = 0; !! 80 Elf64_Rela *rela; >> 81 Elf64_Shdr *esechdrs, *symtab, *s, *got; >> 82 unsigned long nsyms, nrela, i; >> 83 >> 84 esechdrs = sechdrs + hdr->e_shnum; >> 85 symtab = got = NULL; >> 86 >> 87 /* Find out how large the symbol table is. Allocate one got_entry >> 88 head per symbol. Normally this will be enough, but not always. >> 89 We'll chain different offsets for the symbol down each head. */ >> 90 for (s = sechdrs; s < esechdrs; ++s) >> 91 if (s->sh_type == SHT_SYMTAB) >> 92 symtab = s; >> 93 else if (!strcmp(".got", secstrings + s->sh_name)) { >> 94 got = s; >> 95 me->arch.gotsecindex = s - sechdrs; >> 96 } 33 97 34 /* Look for .plt and/or .got.plt and/o !! 98 if (!symtab) { 35 for (i = 0; i < hdr->e_shnum; i++) { !! 99 printk(KERN_ERR "module %s: no symbol table\n", me->name); 36 DEBUGP("Section %d is %s\n", i !! 100 return -ENOEXEC; 37 secstrings + sechdrs[i] !! 101 } 38 if (strcmp(secstrings + sechdr !! 102 if (!got) { 39 found = i+1; !! 103 printk(KERN_ERR "module %s: no got section\n", me->name); 40 if (strcmp(secstrings + sechdr !! 104 return -ENOEXEC; 41 found = i+1; << 42 if (strcmp(secstrings + sechdr << 43 found = i+1; << 44 } 105 } 45 106 46 /* At this time, we don't support modu !! 107 nsyms = symtab->sh_size / sizeof(Elf64_Sym); 47 if (found) { !! 108 chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL); 48 printk(KERN_WARNING !! 109 if (!chains) { 49 "Module '%s' contains !! 110 printk(KERN_ERR 50 mod->name); !! 111 "module %s: no memory for symbol chain buffer\n", 51 /* return -ENOEXEC; */ !! 112 me->name); >> 113 return -ENOMEM; 52 } 114 } 53 115 >> 116 got->sh_size = 0; >> 117 got->sh_addralign = 8; >> 118 got->sh_type = SHT_NOBITS; >> 119 >> 120 /* Examine all LITERAL relocations to find out what GOT entries >> 121 are required. This sizes the GOT section as well. */ >> 122 for (s = sechdrs; s < esechdrs; ++s) >> 123 if (s->sh_type == SHT_RELA) { >> 124 nrela = s->sh_size / sizeof(Elf64_Rela); >> 125 rela = (void *)hdr + s->sh_offset; >> 126 for (i = 0; i < nrela; ++i) >> 127 process_reloc_for_got(rela+i, chains, >> 128 &got->sh_size); >> 129 } >> 130 >> 131 /* Free the memory we allocated. */ >> 132 for (i = 0; i < nsyms; ++i) { >> 133 struct got_entry *g, *n; >> 134 for (g = chains[i].next; g ; g = n) { >> 135 n = g->next; >> 136 kfree(g); >> 137 } >> 138 } >> 139 kfree(chains); >> 140 54 return 0; 141 return 0; 55 } 142 } 56 143 57 /* !! 144 int 58 * apply_relocate_add - perform rela relocatio !! 145 apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, 59 * @sechdrs - pointer to section headers !! 146 unsigned int symindex, unsigned int relsec, 60 * @strtab - some sort of start address? !! 147 struct module *me) 61 * @symindex - symbol index offset or somethin << 62 * @relsec - address to relocate to? << 63 * @module - pointer to module << 64 * << 65 * Perform rela relocations. << 66 */ << 67 int apply_relocate_add(Elf_Shdr *sechdrs, cons << 68 unsigned int symindex, << 69 struct module *module) << 70 { 148 { 71 unsigned int i; !! 149 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 72 Elf32_Sym *sym; !! 150 unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela); 73 uint32_t *location; !! 151 Elf64_Sym *symtab, *sym; 74 uint32_t value; !! 152 void *base, *location; 75 unsigned int nrelocs = sechdrs[relsec] !! 153 unsigned long got, gp; 76 Elf32_Rela *rela = (void *)sechdrs[rel !! 154 77 Elf32_Word sym_info = sechdrs[relsec]. !! 155 DEBUGP("Applying relocate section %u to %u\n", relsec, 78 Elf32_Sym *sym_base = (Elf32_Sym *) se !! 156 sechdrs[relsec].sh_info); 79 void *loc_base = (void *) sechdrs[sym_ !! 157 80 !! 158 base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr; 81 DEBUGP("Applying relocations in sectio !! 159 symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr; 82 relsec, sym_info, loc_base); !! 160 83 !! 161 got = sechdrs[me->arch.gotsecindex].sh_addr; 84 for (i = 0; i < nrelocs; i++) { !! 162 gp = got + 0x8000; 85 !! 163 86 /* Symbol to relocate */ !! 164 for (i = 0; i < n; i++) { 87 sym = sym_base + ELF32_R_SYM(r !! 165 unsigned long r_sym = ELF64_R_SYM (rela[i].r_info); 88 !! 166 unsigned long r_type = ELF64_R_TYPE (rela[i].r_info); 89 /* Where to make the change */ !! 167 unsigned long r_got_offset = r_type >> 8; 90 location = loc_base + rela[i]. !! 168 unsigned long value, hi, lo; 91 !! 169 r_type &= 0xff; 92 /* `Everything is relative'. * !! 170 >> 171 /* This is where to make the change. */ >> 172 location = base + rela[i].r_offset; >> 173 >> 174 /* This is the symbol it is referring to. Note that all >> 175 unresolved symbols have been resolved. */ >> 176 sym = symtab + r_sym; 93 value = sym->st_value + rela[i 177 value = sym->st_value + rela[i].r_addend; 94 178 95 DEBUGP("%d: value=%08x loc=%p !! 179 switch (r_type) { 96 i, value, location, ELF !! 180 case R_ALPHA_NONE: 97 sym->st_name ? !! 181 break; 98 &strtab[sym->st_name] : !! 182 case R_ALPHA_REFLONG: 99 !! 183 *(u32 *)location = value; 100 switch (ELF32_R_TYPE(rela[i].r !! 184 break; 101 case R_HEXAGON_B22_PCREL: { !! 185 case R_ALPHA_REFQUAD: 102 int dist = (int)(value !! 186 /* BUG() can produce misaligned relocations. */ 103 if ((dist < -0x0080000 !! 187 ((u32 *)location)[0] = value; 104 (dist >= 0x0080000 !! 188 ((u32 *)location)[1] = value >> 32; 105 printk(KERN_ER !! 189 break; 106 "%s: %s !! 190 case R_ALPHA_GPREL32: 107 module- !! 191 value -= gp; 108 "R_HEXA !! 192 if ((int)value != value) 109 dist, v !! 193 goto reloc_overflow; 110 sym->st !! 194 *(u32 *)location = value; 111 &strtab !! 195 break; 112 return -ENOEXE !! 196 case R_ALPHA_LITERAL: 113 } !! 197 hi = got + r_got_offset; 114 DEBUGP("B22_PCREL cont !! 198 lo = hi - gp; 115 *location &= ~0x01ff3f !! 199 if ((short)lo != lo) 116 *location |= 0x00003ff !! 200 goto reloc_overflow; 117 *location |= 0x01ff000 !! 201 *(u16 *)location = lo; 118 DEBUGP("Contents after !! 202 *(u64 *)hi = value; >> 203 break; >> 204 case R_ALPHA_LITUSE: >> 205 break; >> 206 case R_ALPHA_GPDISP: >> 207 value = gp - (u64)location; >> 208 lo = (short)value; >> 209 hi = (int)(value - lo); >> 210 if (hi + lo != value) >> 211 goto reloc_overflow; >> 212 *(u16 *)location = hi >> 16; >> 213 *(u16 *)(location + rela[i].r_addend) = lo; >> 214 break; >> 215 case R_ALPHA_BRSGP: >> 216 /* BRSGP is only allowed to bind to local symbols. >> 217 If the section is undef, this means that the >> 218 value was resolved from somewhere else. */ >> 219 if (sym->st_shndx == SHN_UNDEF) >> 220 goto reloc_overflow; >> 221 if ((sym->st_other & STO_ALPHA_STD_GPLOAD) == >> 222 STO_ALPHA_STD_GPLOAD) >> 223 /* Omit the prologue. */ >> 224 value += 8; >> 225 /* FALLTHRU */ >> 226 case R_ALPHA_BRADDR: >> 227 value -= (u64)location + 4; >> 228 if (value & 3) >> 229 goto reloc_overflow; >> 230 value = (long)value >> 2; >> 231 if (value + (1<<21) >= 1<<22) >> 232 goto reloc_overflow; >> 233 value &= 0x1fffff; >> 234 value |= *(u32 *)location & ~0x1fffff; >> 235 *(u32 *)location = value; >> 236 break; >> 237 case R_ALPHA_HINT: >> 238 break; >> 239 case R_ALPHA_SREL32: >> 240 value -= (u64)location; >> 241 if ((int)value != value) >> 242 goto reloc_overflow; >> 243 *(u32 *)location = value; >> 244 break; >> 245 case R_ALPHA_SREL64: >> 246 value -= (u64)location; >> 247 *(u64 *)location = value; >> 248 break; >> 249 case R_ALPHA_GPRELHIGH: >> 250 value = (long)(value - gp + 0x8000) >> 16; >> 251 if ((short) value != value) >> 252 goto reloc_overflow; >> 253 *(u16 *)location = value; >> 254 break; >> 255 case R_ALPHA_GPRELLOW: >> 256 value -= gp; >> 257 *(u16 *)location = value; >> 258 break; >> 259 case R_ALPHA_GPREL16: >> 260 value -= gp; >> 261 if ((short) value != value) >> 262 goto reloc_overflow; >> 263 *(u16 *)location = value; 119 break; 264 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: 265 default: 142 printk(KERN_ERR "%s: u !! 266 printk(KERN_ERR "module %s: Unknown relocation: %lu\n", 143 module->name, !! 267 me->name, r_type); 144 ELF32_R_TYPE(re !! 268 return -ENOEXEC; >> 269 reloc_overflow: >> 270 if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION) >> 271 printk(KERN_ERR >> 272 "module %s: Relocation (type %lu) overflow vs section %d\n", >> 273 me->name, r_type, sym->st_shndx); >> 274 else >> 275 printk(KERN_ERR >> 276 "module %s: Relocation (type %lu) overflow vs %s\n", >> 277 me->name, r_type, strtab + sym->st_name); 145 return -ENOEXEC; 278 return -ENOEXEC; 146 } 279 } 147 } 280 } >> 281 148 return 0; 282 return 0; 149 } 283 } 150 284
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.