xref: /linux/arch/arc/kernel/module.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fa1c3ff9SVineet Gupta /*
3fa1c3ff9SVineet Gupta  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4fa1c3ff9SVineet Gupta  */
5fa1c3ff9SVineet Gupta 
6fa1c3ff9SVineet Gupta #include <linux/module.h>
7fa1c3ff9SVineet Gupta #include <linux/moduleloader.h>
8fa1c3ff9SVineet Gupta #include <linux/kernel.h>
9fa1c3ff9SVineet Gupta #include <linux/elf.h>
10fa1c3ff9SVineet Gupta #include <linux/vmalloc.h>
11fa1c3ff9SVineet Gupta #include <linux/slab.h>
12fa1c3ff9SVineet Gupta #include <linux/fs.h>
13fa1c3ff9SVineet Gupta #include <linux/string.h>
14854a0d95SVineet Gupta #include <asm/unwind.h>
15fa1c3ff9SVineet Gupta 
arc_write_me(unsigned short * addr,unsigned long value)16fa1c3ff9SVineet Gupta static inline void arc_write_me(unsigned short *addr, unsigned long value)
17fa1c3ff9SVineet Gupta {
18fa1c3ff9SVineet Gupta 	*addr = (value & 0xffff0000) >> 16;
19fa1c3ff9SVineet Gupta 	*(addr + 1) = (value & 0xffff);
20fa1c3ff9SVineet Gupta }
21fa1c3ff9SVineet Gupta 
226716dbbdSVineet Gupta /*
236716dbbdSVineet Gupta  * This gets called before relocation loop in generic loader
246716dbbdSVineet Gupta  * Make a note of the section index of unwinding section
25854a0d95SVineet Gupta  */
module_frob_arch_sections(Elf_Ehdr * hdr,Elf_Shdr * sechdrs,char * secstr,struct module * mod)26854a0d95SVineet Gupta int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
27854a0d95SVineet Gupta 			      char *secstr, struct module *mod)
28854a0d95SVineet Gupta {
29854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND
30854a0d95SVineet Gupta 	mod->arch.unw_sec_idx = 0;
31854a0d95SVineet Gupta 	mod->arch.unw_info = NULL;
32854a0d95SVineet Gupta #endif
33eb1357d9SVineet Gupta 	mod->arch.secstr = secstr;
34854a0d95SVineet Gupta 	return 0;
35854a0d95SVineet Gupta }
36854a0d95SVineet Gupta 
module_arch_cleanup(struct module * mod)37854a0d95SVineet Gupta void module_arch_cleanup(struct module *mod)
38854a0d95SVineet Gupta {
39854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND
40854a0d95SVineet Gupta 	if (mod->arch.unw_info)
41854a0d95SVineet Gupta 		unwind_remove_table(mod->arch.unw_info, 0);
42854a0d95SVineet Gupta #endif
43854a0d95SVineet Gupta }
44854a0d95SVineet Gupta 
apply_relocate_add(Elf32_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * module)45fa1c3ff9SVineet Gupta int apply_relocate_add(Elf32_Shdr *sechdrs,
46fa1c3ff9SVineet Gupta 		       const char *strtab,
47fa1c3ff9SVineet Gupta 		       unsigned int symindex,	/* sec index for sym tbl */
48fa1c3ff9SVineet Gupta 		       unsigned int relsec,	/* sec index for relo sec */
49fa1c3ff9SVineet Gupta 		       struct module *module)
50fa1c3ff9SVineet Gupta {
51b75dcd9cSVineet Gupta 	int i, n, relo_type;
52fa1c3ff9SVineet Gupta 	Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
53fa1c3ff9SVineet Gupta 	Elf32_Sym *sym_entry, *sym_sec;
54b75dcd9cSVineet Gupta 	Elf32_Addr relocation, location, tgt_addr;
55d65283f7SVineet Gupta 	unsigned int tgtsec;
56fa1c3ff9SVineet Gupta 
57b75dcd9cSVineet Gupta 	/*
58b75dcd9cSVineet Gupta 	 * @relsec has relocations e.g. .rela.init.text
59b75dcd9cSVineet Gupta 	 * @tgtsec is section to patch e.g. .init.text
60b75dcd9cSVineet Gupta 	 */
61d65283f7SVineet Gupta 	tgtsec = sechdrs[relsec].sh_info;
62b75dcd9cSVineet Gupta 	tgt_addr = sechdrs[tgtsec].sh_addr;
63fa1c3ff9SVineet Gupta 	sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
64fa1c3ff9SVineet Gupta 	n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
65fa1c3ff9SVineet Gupta 
66b75dcd9cSVineet Gupta 	pr_debug("\nSection to fixup %s @%x\n",
67b75dcd9cSVineet Gupta 		 module->arch.secstr + sechdrs[tgtsec].sh_name, tgt_addr);
68fa1c3ff9SVineet Gupta 	pr_debug("=========================================================\n");
69b75dcd9cSVineet Gupta 	pr_debug("r_off\tr_add\tst_value ADDRESS  VALUE\n");
70fa1c3ff9SVineet Gupta 	pr_debug("=========================================================\n");
71fa1c3ff9SVineet Gupta 
72fa1c3ff9SVineet Gupta 	/* Loop thru entries in relocation section */
73fa1c3ff9SVineet Gupta 	for (i = 0; i < n; i++) {
74b75dcd9cSVineet Gupta 		const char *s;
75fa1c3ff9SVineet Gupta 
76fa1c3ff9SVineet Gupta 		/* This is where to make the change */
77b75dcd9cSVineet Gupta 		location = tgt_addr + rel_entry[i].r_offset;
78fa1c3ff9SVineet Gupta 
79fa1c3ff9SVineet Gupta 		/* This is the symbol it is referring to.  Note that all
80fa1c3ff9SVineet Gupta 		   undefined symbols have been resolved.  */
81fa1c3ff9SVineet Gupta 		sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
82fa1c3ff9SVineet Gupta 
83fa1c3ff9SVineet Gupta 		relocation = sym_entry->st_value + rel_entry[i].r_addend;
84fa1c3ff9SVineet Gupta 
85b75dcd9cSVineet Gupta 		if (sym_entry->st_name == 0 && ELF_ST_TYPE (sym_entry->st_info) == STT_SECTION) {
86b75dcd9cSVineet Gupta 			s = module->arch.secstr + sechdrs[sym_entry->st_shndx].sh_name;
87b75dcd9cSVineet Gupta 		} else {
88b75dcd9cSVineet Gupta 			s = strtab + sym_entry->st_name;
89b75dcd9cSVineet Gupta 		}
90b75dcd9cSVineet Gupta 
91b75dcd9cSVineet Gupta 		pr_debug("   %x\t%x\t%x %x %x [%s]\n",
92fa1c3ff9SVineet Gupta 			 rel_entry[i].r_offset, rel_entry[i].r_addend,
93b75dcd9cSVineet Gupta 			 sym_entry->st_value, location, relocation, s);
94fa1c3ff9SVineet Gupta 
95fa1c3ff9SVineet Gupta 		/* This assumes modules are built with -mlong-calls
96fa1c3ff9SVineet Gupta 		 * so any branches/jumps are absolute 32 bit jmps
97fa1c3ff9SVineet Gupta 		 * global data access again is abs 32 bit.
98fa1c3ff9SVineet Gupta 		 * Both of these are handled by same relocation type
99fa1c3ff9SVineet Gupta 		 */
100fa1c3ff9SVineet Gupta 		relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
101fa1c3ff9SVineet Gupta 
10294f4fb08SVineet Gupta 		if (likely(R_ARC_32_ME == relo_type))	/* ME ( S + A ) */
103fa1c3ff9SVineet Gupta 			arc_write_me((unsigned short *)location, relocation);
10494f4fb08SVineet Gupta 		else if (R_ARC_32 == relo_type)		/* ( S + A ) */
105fa1c3ff9SVineet Gupta 			*((Elf32_Addr *) location) = relocation;
10694f4fb08SVineet Gupta 		else if (R_ARC_32_PCREL == relo_type)	/* ( S + A ) - PDATA ) */
10794f4fb08SVineet Gupta 			*((Elf32_Addr *) location) = relocation - location;
108fa1c3ff9SVineet Gupta 		else
109fa1c3ff9SVineet Gupta 			goto relo_err;
110fa1c3ff9SVineet Gupta 
111fa1c3ff9SVineet Gupta 	}
112d65283f7SVineet Gupta 
113eb1357d9SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND
114d65283f7SVineet Gupta 	if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0)
115d65283f7SVineet Gupta 		module->arch.unw_sec_idx = tgtsec;
116eb1357d9SVineet Gupta #endif
117d65283f7SVineet Gupta 
118fa1c3ff9SVineet Gupta 	return 0;
119fa1c3ff9SVineet Gupta 
120fa1c3ff9SVineet Gupta relo_err:
121fa1c3ff9SVineet Gupta 	pr_err("%s: unknown relocation: %u\n",
122fa1c3ff9SVineet Gupta 		module->name, ELF32_R_TYPE(rel_entry[i].r_info));
123fa1c3ff9SVineet Gupta 	return -ENOEXEC;
124fa1c3ff9SVineet Gupta 
125fa1c3ff9SVineet Gupta }
126854a0d95SVineet Gupta 
127854a0d95SVineet Gupta /* Just before lift off: After sections have been relocated, we add the
128854a0d95SVineet Gupta  * dwarf section to unwinder table pool
129854a0d95SVineet Gupta  * This couldn't be done in module_frob_arch_sections() because
130854a0d95SVineet Gupta  * relocations had not been applied by then
131854a0d95SVineet Gupta  */
module_finalize(const Elf32_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * mod)132854a0d95SVineet Gupta int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
133854a0d95SVineet Gupta 		    struct module *mod)
134854a0d95SVineet Gupta {
135854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND
136854a0d95SVineet Gupta 	void *unw;
137854a0d95SVineet Gupta 	int unwsec = mod->arch.unw_sec_idx;
138854a0d95SVineet Gupta 
139854a0d95SVineet Gupta 	if (unwsec) {
140854a0d95SVineet Gupta 		unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
141854a0d95SVineet Gupta 				       sechdrs[unwsec].sh_size);
142854a0d95SVineet Gupta 		mod->arch.unw_info = unw;
143854a0d95SVineet Gupta 	}
144854a0d95SVineet Gupta #endif
145854a0d95SVineet Gupta 	return 0;
146854a0d95SVineet Gupta }
147