1 // SPDX-License-Identifier: GPL-2.0 !! 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 // Copyright (C) 2018 Hangzhou C-SKY Microsyst !! 2 /* Kernel module help for Alpha. >> 3 Copyright (C) 2002 Richard Henderson. 3 4 >> 5 */ 4 #include <linux/moduleloader.h> 6 #include <linux/moduleloader.h> 5 #include <linux/elf.h> 7 #include <linux/elf.h> 6 #include <linux/mm.h> << 7 #include <linux/vmalloc.h> 8 #include <linux/vmalloc.h> 8 #include <linux/slab.h> << 9 #include <linux/fs.h> 9 #include <linux/fs.h> 10 #include <linux/string.h> 10 #include <linux/string.h> 11 #include <linux/kernel.h> 11 #include <linux/kernel.h> 12 #include <linux/spinlock.h> !! 12 #include <linux/slab.h> 13 13 14 #ifdef CONFIG_CPU_CK810 !! 14 #if 0 15 #define IS_BSR32(hi16, lo16) (((hi1 !! 15 #define DEBUGP printk 16 #define IS_JSRI32(hi16, lo16) ((hi16 !! 16 #else 17 !! 17 #define DEBUGP(fmt...) 18 #define CHANGE_JSRI_TO_LRW(addr) do { !! 18 #endif 19 *(uint16_t *)(addr) = (*(uint16_t *)(a << 20 *((uint16_t *)(addr) + 1) = *((uint16_ << 21 } while (0) << 22 << 23 #define SET_JSR32_R26(addr) do { << 24 *(uint16_t *)(addr) = 0xE8Fa; << 25 *((uint16_t *)(addr) + 1) = 0x0000; << 26 } while (0) << 27 19 28 static void jsri_2_lrw_jsr(uint32_t *location) !! 20 /* Allocate the GOT at the end of the core sections. */ >> 21 >> 22 struct got_entry { >> 23 struct got_entry *next; >> 24 Elf64_Sxword r_addend; >> 25 int got_offset; >> 26 }; >> 27 >> 28 static inline void >> 29 process_reloc_for_got(Elf64_Rela *rela, >> 30 struct got_entry *chains, Elf64_Xword *poffset) 29 { 31 { 30 uint16_t *location_tmp = (uint16_t *)l !! 32 unsigned long r_sym = ELF64_R_SYM (rela->r_info); >> 33 unsigned long r_type = ELF64_R_TYPE (rela->r_info); >> 34 Elf64_Sxword r_addend = rela->r_addend; >> 35 struct got_entry *g; 31 36 32 if (IS_BSR32(*location_tmp, *(location !! 37 if (r_type != R_ALPHA_LITERAL) 33 return; 38 return; 34 39 35 if (IS_JSRI32(*location_tmp, *(locatio !! 40 for (g = chains + r_sym; g ; g = g->next) 36 /* jsri 0x... --> lrw r26, 0x !! 41 if (g->r_addend == r_addend) { 37 CHANGE_JSRI_TO_LRW(location); !! 42 if (g->got_offset == 0) { 38 /* lsli r0, r0 --> jsr r26 */ !! 43 g->got_offset = *poffset; 39 SET_JSR32_R26(location + 1); !! 44 *poffset += 8; 40 } !! 45 } >> 46 goto found_entry; >> 47 } >> 48 >> 49 g = kmalloc (sizeof (*g), GFP_KERNEL); >> 50 g->next = chains[r_sym].next; >> 51 g->r_addend = r_addend; >> 52 g->got_offset = *poffset; >> 53 *poffset += 8; >> 54 chains[r_sym].next = g; >> 55 >> 56 found_entry: >> 57 /* Trick: most of the ELF64_R_TYPE field is unused. There are >> 58 42 valid relocation types, and a 32-bit field. Co-opt the >> 59 bits above 256 to store the got offset for this reloc. */ >> 60 rela->r_info |= g->got_offset << 8; 41 } 61 } 42 #else !! 62 43 static inline void jsri_2_lrw_jsr(uint32_t *lo !! 63 int >> 64 module_frob_arch_sections(Elf64_Ehdr *hdr, Elf64_Shdr *sechdrs, >> 65 char *secstrings, struct module *me) 44 { 66 { 45 return; !! 67 struct got_entry *chains; >> 68 Elf64_Rela *rela; >> 69 Elf64_Shdr *esechdrs, *symtab, *s, *got; >> 70 unsigned long nsyms, nrela, i; >> 71 >> 72 esechdrs = sechdrs + hdr->e_shnum; >> 73 symtab = got = NULL; >> 74 >> 75 /* Find out how large the symbol table is. Allocate one got_entry >> 76 head per symbol. Normally this will be enough, but not always. >> 77 We'll chain different offsets for the symbol down each head. */ >> 78 for (s = sechdrs; s < esechdrs; ++s) >> 79 if (s->sh_type == SHT_SYMTAB) >> 80 symtab = s; >> 81 else if (!strcmp(".got", secstrings + s->sh_name)) { >> 82 got = s; >> 83 me->arch.gotsecindex = s - sechdrs; >> 84 } >> 85 >> 86 if (!symtab) { >> 87 printk(KERN_ERR "module %s: no symbol table\n", me->name); >> 88 return -ENOEXEC; >> 89 } >> 90 if (!got) { >> 91 printk(KERN_ERR "module %s: no got section\n", me->name); >> 92 return -ENOEXEC; >> 93 } >> 94 >> 95 nsyms = symtab->sh_size / sizeof(Elf64_Sym); >> 96 chains = kcalloc(nsyms, sizeof(struct got_entry), GFP_KERNEL); >> 97 if (!chains) { >> 98 printk(KERN_ERR >> 99 "module %s: no memory for symbol chain buffer\n", >> 100 me->name); >> 101 return -ENOMEM; >> 102 } >> 103 >> 104 got->sh_size = 0; >> 105 got->sh_addralign = 8; >> 106 got->sh_type = SHT_NOBITS; >> 107 >> 108 /* Examine all LITERAL relocations to find out what GOT entries >> 109 are required. This sizes the GOT section as well. */ >> 110 for (s = sechdrs; s < esechdrs; ++s) >> 111 if (s->sh_type == SHT_RELA) { >> 112 nrela = s->sh_size / sizeof(Elf64_Rela); >> 113 rela = (void *)hdr + s->sh_offset; >> 114 for (i = 0; i < nrela; ++i) >> 115 process_reloc_for_got(rela+i, chains, >> 116 &got->sh_size); >> 117 } >> 118 >> 119 /* Free the memory we allocated. */ >> 120 for (i = 0; i < nsyms; ++i) { >> 121 struct got_entry *g, *n; >> 122 for (g = chains[i].next; g ; g = n) { >> 123 n = g->next; >> 124 kfree(g); >> 125 } >> 126 } >> 127 kfree(chains); >> 128 >> 129 return 0; 46 } 130 } 47 #endif << 48 131 49 int apply_relocate_add(Elf32_Shdr *sechdrs, co !! 132 int 50 unsigned int symindex, unsigne !! 133 apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, >> 134 unsigned int symindex, unsigned int relsec, >> 135 struct module *me) 51 { 136 { 52 unsigned int i; !! 137 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 53 Elf32_Rela *rel = (void *) sechdr !! 138 unsigned long i, n = sechdrs[relsec].sh_size / sizeof(*rela); 54 Elf32_Sym *sym; !! 139 Elf64_Sym *symtab, *sym; 55 uint32_t *location; !! 140 void *base, *location; 56 short *temp; !! 141 unsigned long got, gp; 57 !! 142 58 for (i = 0; i < sechdrs[relsec].sh_siz !! 143 DEBUGP("Applying relocate section %u to %u\n", relsec, 59 /* This is where to make the c !! 144 sechdrs[relsec].sh_info); 60 location = (void *)sechdrs[sec !! 145 61 !! 146 base = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr; 62 sym = (Elf32_Sym *)sechdrs[sym !! 147 symtab = (Elf64_Sym *)sechdrs[symindex].sh_addr; 63 !! 148 64 !! 149 /* The small sections were sorted to the end of the segment. 65 switch (ELF32_R_TYPE(rel[i].r_ !! 150 The following should definitely cover them. */ 66 case R_CSKY_32: !! 151 gp = (u64)me->core_layout.base + me->core_layout.size - 0x8000; 67 /* We add the value in !! 152 got = sechdrs[me->arch.gotsecindex].sh_addr; 68 *location = rel[i].r_a !! 153 69 break; !! 154 for (i = 0; i < n; i++) { 70 case R_CSKY_PC32: !! 155 unsigned long r_sym = ELF64_R_SYM (rela[i].r_info); 71 /* Add the value, subt !! 156 unsigned long r_type = ELF64_R_TYPE (rela[i].r_info); 72 *location = rel[i].r_a !! 157 unsigned long r_got_offset = r_type >> 8; 73 !! 158 unsigned long value, hi, lo; 74 break; !! 159 r_type &= 0xff; 75 case R_CSKY_PCRELJSR_IMM11BY2: !! 160 76 break; !! 161 /* This is where to make the change. */ 77 case R_CSKY_PCRELJSR_IMM26BY2: !! 162 location = base + rela[i].r_offset; 78 jsri_2_lrw_jsr(locatio !! 163 79 break; !! 164 /* This is the symbol it is referring to. Note that all 80 case R_CSKY_ADDR_HI16: !! 165 unresolved symbols have been resolved. */ 81 temp = ((short *)loca !! 166 sym = symtab + r_sym; 82 *temp = (short) !! 167 value = sym->st_value + rela[i].r_addend; 83 ((rel[i].r_add !! 168 84 break; !! 169 switch (r_type) { 85 case R_CSKY_ADDR_LO16: !! 170 case R_ALPHA_NONE: 86 temp = ((short *)loca !! 171 break; 87 *temp = (short) !! 172 case R_ALPHA_REFLONG: 88 ((rel[i].r_add !! 173 *(u32 *)location = value; >> 174 break; >> 175 case R_ALPHA_REFQUAD: >> 176 /* BUG() can produce misaligned relocations. */ >> 177 ((u32 *)location)[0] = value; >> 178 ((u32 *)location)[1] = value >> 32; >> 179 break; >> 180 case R_ALPHA_GPREL32: >> 181 value -= gp; >> 182 if ((int)value != value) >> 183 goto reloc_overflow; >> 184 *(u32 *)location = value; >> 185 break; >> 186 case R_ALPHA_LITERAL: >> 187 hi = got + r_got_offset; >> 188 lo = hi - gp; >> 189 if ((short)lo != lo) >> 190 goto reloc_overflow; >> 191 *(u16 *)location = lo; >> 192 *(u64 *)hi = value; >> 193 break; >> 194 case R_ALPHA_LITUSE: >> 195 break; >> 196 case R_ALPHA_GPDISP: >> 197 value = gp - (u64)location; >> 198 lo = (short)value; >> 199 hi = (int)(value - lo); >> 200 if (hi + lo != value) >> 201 goto reloc_overflow; >> 202 *(u16 *)location = hi >> 16; >> 203 *(u16 *)(location + rela[i].r_addend) = lo; >> 204 break; >> 205 case R_ALPHA_BRSGP: >> 206 /* BRSGP is only allowed to bind to local symbols. >> 207 If the section is undef, this means that the >> 208 value was resolved from somewhere else. */ >> 209 if (sym->st_shndx == SHN_UNDEF) >> 210 goto reloc_overflow; >> 211 if ((sym->st_other & STO_ALPHA_STD_GPLOAD) == >> 212 STO_ALPHA_STD_GPLOAD) >> 213 /* Omit the prologue. */ >> 214 value += 8; >> 215 fallthrough; >> 216 case R_ALPHA_BRADDR: >> 217 value -= (u64)location + 4; >> 218 if (value & 3) >> 219 goto reloc_overflow; >> 220 value = (long)value >> 2; >> 221 if (value + (1<<21) >= 1<<22) >> 222 goto reloc_overflow; >> 223 value &= 0x1fffff; >> 224 value |= *(u32 *)location & ~0x1fffff; >> 225 *(u32 *)location = value; >> 226 break; >> 227 case R_ALPHA_HINT: >> 228 break; >> 229 case R_ALPHA_SREL32: >> 230 value -= (u64)location; >> 231 if ((int)value != value) >> 232 goto reloc_overflow; >> 233 *(u32 *)location = value; >> 234 break; >> 235 case R_ALPHA_SREL64: >> 236 value -= (u64)location; >> 237 *(u64 *)location = value; >> 238 break; >> 239 case R_ALPHA_GPRELHIGH: >> 240 value = (long)(value - gp + 0x8000) >> 16; >> 241 if ((short) value != value) >> 242 goto reloc_overflow; >> 243 *(u16 *)location = value; >> 244 break; >> 245 case R_ALPHA_GPRELLOW: >> 246 value -= gp; >> 247 *(u16 *)location = value; >> 248 break; >> 249 case R_ALPHA_GPREL16: >> 250 value -= gp; >> 251 if ((short) value != value) >> 252 goto reloc_overflow; >> 253 *(u16 *)location = value; 89 break; 254 break; 90 default: 255 default: 91 pr_err("module %s: Unk !! 256 printk(KERN_ERR "module %s: Unknown relocation: %lu\n", 92 me->name, ELF3 !! 257 me->name, r_type); >> 258 return -ENOEXEC; >> 259 reloc_overflow: >> 260 if (ELF64_ST_TYPE (sym->st_info) == STT_SECTION) >> 261 printk(KERN_ERR >> 262 "module %s: Relocation (type %lu) overflow vs section %d\n", >> 263 me->name, r_type, sym->st_shndx); >> 264 else >> 265 printk(KERN_ERR >> 266 "module %s: Relocation (type %lu) overflow vs %s\n", >> 267 me->name, r_type, strtab + sym->st_name); 93 return -ENOEXEC; 268 return -ENOEXEC; 94 } 269 } 95 } 270 } >> 271 96 return 0; 272 return 0; 97 } 273 } 98 274
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.