xref: /linux-6.15/arch/arc/kernel/module.c (revision d65283f7)
1 /*
2  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/module.h>
10 #include <linux/moduleloader.h>
11 #include <linux/kernel.h>
12 #include <linux/elf.h>
13 #include <linux/vmalloc.h>
14 #include <linux/slab.h>
15 #include <linux/fs.h>
16 #include <linux/string.h>
17 #include <asm/unwind.h>
18 
19 static inline void arc_write_me(unsigned short *addr, unsigned long value)
20 {
21 	*addr = (value & 0xffff0000) >> 16;
22 	*(addr + 1) = (value & 0xffff);
23 }
24 
25 /*
26  * This gets called before relocation loop in generic loader
27  * Make a note of the section index of unwinding section
28  */
29 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
30 			      char *secstr, struct module *mod)
31 {
32 #ifdef CONFIG_ARC_DW2_UNWIND
33 	mod->arch.unw_sec_idx = 0;
34 	mod->arch.unw_info = NULL;
35 	mod->arch.secstr = secstr;
36 #endif
37 	return 0;
38 }
39 
40 void module_arch_cleanup(struct module *mod)
41 {
42 #ifdef CONFIG_ARC_DW2_UNWIND
43 	if (mod->arch.unw_info)
44 		unwind_remove_table(mod->arch.unw_info, 0);
45 #endif
46 }
47 
48 int apply_relocate_add(Elf32_Shdr *sechdrs,
49 		       const char *strtab,
50 		       unsigned int symindex,	/* sec index for sym tbl */
51 		       unsigned int relsec,	/* sec index for relo sec */
52 		       struct module *module)
53 {
54 	int i, n;
55 	Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
56 	Elf32_Sym *sym_entry, *sym_sec;
57 	Elf32_Addr relocation;
58 	Elf32_Addr location;
59 	Elf32_Addr sec_to_patch;
60 	int relo_type;
61 	unsigned int tgtsec;
62 
63 	tgtsec = sechdrs[relsec].sh_info;
64 	sec_to_patch = sechdrs[tgtsec].sh_addr;
65 	sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
66 	n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
67 
68 	pr_debug("\n========== Module Sym reloc ===========================\n");
69 	pr_debug("Section to fixup %x\n", sec_to_patch);
70 	pr_debug("=========================================================\n");
71 	pr_debug("rela->r_off | rela->addend | sym->st_value | ADDR | VALUE\n");
72 	pr_debug("=========================================================\n");
73 
74 	/* Loop thru entries in relocation section */
75 	for (i = 0; i < n; i++) {
76 
77 		/* This is where to make the change */
78 		location = sec_to_patch + rel_entry[i].r_offset;
79 
80 		/* This is the symbol it is referring to.  Note that all
81 		   undefined symbols have been resolved.  */
82 		sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
83 
84 		relocation = sym_entry->st_value + rel_entry[i].r_addend;
85 
86 		pr_debug("\t%x\t\t%x\t\t%x  %x %x [%s]\n",
87 			rel_entry[i].r_offset, rel_entry[i].r_addend,
88 			sym_entry->st_value, location, relocation,
89 			strtab + sym_entry->st_name);
90 
91 		/* This assumes modules are built with -mlong-calls
92 		 * so any branches/jumps are absolute 32 bit jmps
93 		 * global data access again is abs 32 bit.
94 		 * Both of these are handled by same relocation type
95 		 */
96 		relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
97 
98 		if (likely(R_ARC_32_ME == relo_type))	/* ME ( S + A ) */
99 			arc_write_me((unsigned short *)location, relocation);
100 		else if (R_ARC_32 == relo_type)		/* ( S + A ) */
101 			*((Elf32_Addr *) location) = relocation;
102 		else if (R_ARC_32_PCREL == relo_type)	/* ( S + A ) - PDATA ) */
103 			*((Elf32_Addr *) location) = relocation - location;
104 		else
105 			goto relo_err;
106 
107 	}
108 
109 	if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0)
110 		module->arch.unw_sec_idx = tgtsec;
111 
112 	return 0;
113 
114 relo_err:
115 	pr_err("%s: unknown relocation: %u\n",
116 		module->name, ELF32_R_TYPE(rel_entry[i].r_info));
117 	return -ENOEXEC;
118 
119 }
120 
121 /* Just before lift off: After sections have been relocated, we add the
122  * dwarf section to unwinder table pool
123  * This couldn't be done in module_frob_arch_sections() because
124  * relocations had not been applied by then
125  */
126 int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
127 		    struct module *mod)
128 {
129 #ifdef CONFIG_ARC_DW2_UNWIND
130 	void *unw;
131 	int unwsec = mod->arch.unw_sec_idx;
132 
133 	if (unwsec) {
134 		unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
135 				       sechdrs[unwsec].sh_size);
136 		mod->arch.unw_info = unw;
137 	}
138 #endif
139 	return 0;
140 }
141