1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/moduleloader.h>
3 #include <linux/elf.h>
4 #include <linux/vmalloc.h>
5 #include <linux/fs.h>
6 #include <linux/string.h>
7 #include <linux/kernel.h>
8 
apply_relocate_add(Elf32_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)9 int apply_relocate_add(Elf32_Shdr *sechdrs,
10 		       const char *strtab,
11 		       unsigned int symindex,
12 		       unsigned int relsec,
13 		       struct module *me)
14 {
15 	unsigned int i;
16 	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
17 
18 	pr_debug("Applying relocate section %u to %u\n", relsec,
19 	       sechdrs[relsec].sh_info);
20 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
21 		/* This is where to make the change */
22 		uint32_t *loc =
23 			(uint32_t *)(sechdrs[sechdrs[relsec].sh_info].sh_addr
24 					     + rela[i].r_offset);
25 		/* This is the symbol it is referring to.  Note that all
26 		   undefined symbols have been resolved.  */
27 		Elf32_Sym *sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
28 			+ ELF32_R_SYM(rela[i].r_info);
29 		uint32_t v = sym->st_value + rela[i].r_addend;
30 
31 		switch (ELF32_R_TYPE(rela[i].r_info)) {
32 		case R_H8_DIR24R8:
33 			loc = (uint32_t *)((uint32_t)loc - 1);
34 			*loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
35 			break;
36 		case R_H8_DIR24A8:
37 			if (ELF32_R_SYM(rela[i].r_info))
38 				*loc += v;
39 			break;
40 		case R_H8_DIR32:
41 		case R_H8_DIR32A16:
42 			*loc += v;
43 			break;
44 		case R_H8_PCREL16:
45 			v -= (unsigned long)loc + 2;
46 			if ((Elf32_Sword)v > 0x7fff ||
47 			    (Elf32_Sword)v < -(Elf32_Sword)0x8000)
48 				goto overflow;
49 			else
50 				*(unsigned short *)loc = v;
51 			break;
52 		case R_H8_PCREL8:
53 			v -= (unsigned long)loc + 1;
54 			if ((Elf32_Sword)v > 0x7f ||
55 			    (Elf32_Sword)v < -(Elf32_Sword)0x80)
56 				goto overflow;
57 			else
58 				*(unsigned char *)loc = v;
59 			break;
60 		default:
61 			pr_err("module %s: Unknown relocation: %u\n",
62 			       me->name, ELF32_R_TYPE(rela[i].r_info));
63 			return -ENOEXEC;
64 		}
65 	}
66 	return 0;
67  overflow:
68 	pr_err("module %s: relocation offset overflow: %08x\n",
69 	       me->name, rela[i].r_offset);
70 	return -ENOEXEC;
71 }
72