xref: /openbmc/linux/arch/microblaze/kernel/module.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
4  * Copyright (C) 2007-2009 PetaLogix
5  */
6 
7 #include <linux/export.h>
8 #include <linux/moduleloader.h>
9 #include <linux/kernel.h>
10 #include <linux/elf.h>
11 #include <linux/vmalloc.h>
12 #include <linux/fs.h>
13 #include <linux/string.h>
14 
15 #include <asm/pgtable.h>
16 #include <asm/cacheflush.h>
17 
18 int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
19 	unsigned int symindex, unsigned int relsec, struct module *module)
20 {
21 
22 	unsigned int i;
23 	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
24 	Elf32_Sym *sym;
25 	unsigned long int *location;
26 	unsigned long int value;
27 #if __GNUC__ < 4
28 	unsigned long int old_value;
29 #endif
30 
31 	pr_debug("Applying add relocation section %u to %u\n",
32 		relsec, sechdrs[relsec].sh_info);
33 
34 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
35 
36 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr +
37 				rela[i].r_offset;
38 		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr +
39 			ELF32_R_SYM(rela[i].r_info);
40 		value = sym->st_value + rela[i].r_addend;
41 
42 		switch (ELF32_R_TYPE(rela[i].r_info)) {
43 
44 		/*
45 		 * Be careful! mb-gcc / mb-ld splits the relocs between the
46 		 * text and the reloc table. In general this means we must
47 		 * read the current contents of (*location), add any offset
48 		 * then store the result back in
49 		 */
50 
51 		case R_MICROBLAZE_32:
52 #if __GNUC__ < 4
53 			old_value = *location;
54 			*location = value + old_value;
55 
56 			pr_debug("R_MICROBLAZE_32 (%08lx->%08lx)\n",
57 				old_value, value);
58 #else
59 			*location = value;
60 #endif
61 			break;
62 
63 		case R_MICROBLAZE_64:
64 #if __GNUC__ < 4
65 			/* Split relocs only required/used pre gcc4.1.1 */
66 			old_value = ((location[0] & 0x0000FFFF) << 16) |
67 					(location[1] & 0x0000FFFF);
68 			value += old_value;
69 #endif
70 			location[0] = (location[0] & 0xFFFF0000) |
71 					(value >> 16);
72 			location[1] = (location[1] & 0xFFFF0000) |
73 					(value & 0xFFFF);
74 #if __GNUC__ < 4
75 			pr_debug("R_MICROBLAZE_64 (%08lx->%08lx)\n",
76 				old_value, value);
77 #endif
78 			break;
79 
80 		case R_MICROBLAZE_64_PCREL:
81 #if __GNUC__ < 4
82 			old_value = (location[0] & 0xFFFF) << 16 |
83 				(location[1] & 0xFFFF);
84 			value -= old_value;
85 #endif
86 			value -= (unsigned long int)(location) + 4;
87 			location[0] = (location[0] & 0xFFFF0000) |
88 					(value >> 16);
89 			location[1] = (location[1] & 0xFFFF0000) |
90 					(value & 0xFFFF);
91 			pr_debug("R_MICROBLAZE_64_PCREL (%08lx)\n",
92 				value);
93 			break;
94 
95 		case R_MICROBLAZE_32_PCREL_LO:
96 			pr_debug("R_MICROBLAZE_32_PCREL_LO\n");
97 			break;
98 
99 		case R_MICROBLAZE_64_NONE:
100 			pr_debug("R_MICROBLAZE_64_NONE\n");
101 			break;
102 
103 		case R_MICROBLAZE_NONE:
104 			pr_debug("R_MICROBLAZE_NONE\n");
105 			break;
106 
107 		default:
108 			pr_err("module %s: Unknown relocation: %u\n",
109 				module->name,
110 				ELF32_R_TYPE(rela[i].r_info));
111 			return -ENOEXEC;
112 		}
113 	}
114 	return 0;
115 }
116 
117 int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
118 		struct module *module)
119 {
120 	flush_dcache();
121 	return 0;
122 }
123