xref: /linux/arch/arc/kernel/module.c (revision 6716dbbdefa9867ba98dea91d89b14168043a48c)
1fa1c3ff9SVineet Gupta /*
2fa1c3ff9SVineet Gupta  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3fa1c3ff9SVineet Gupta  *
4fa1c3ff9SVineet Gupta  * This program is free software; you can redistribute it and/or modify
5fa1c3ff9SVineet Gupta  * it under the terms of the GNU General Public License version 2 as
6fa1c3ff9SVineet Gupta  * published by the Free Software Foundation.
7fa1c3ff9SVineet Gupta  */
8fa1c3ff9SVineet Gupta 
9fa1c3ff9SVineet Gupta #include <linux/module.h>
10fa1c3ff9SVineet Gupta #include <linux/moduleloader.h>
11fa1c3ff9SVineet Gupta #include <linux/kernel.h>
12fa1c3ff9SVineet Gupta #include <linux/elf.h>
13fa1c3ff9SVineet Gupta #include <linux/vmalloc.h>
14fa1c3ff9SVineet Gupta #include <linux/slab.h>
15fa1c3ff9SVineet Gupta #include <linux/fs.h>
16fa1c3ff9SVineet Gupta #include <linux/string.h>
17854a0d95SVineet Gupta #include <asm/unwind.h>
18fa1c3ff9SVineet Gupta 
19fa1c3ff9SVineet Gupta static inline void arc_write_me(unsigned short *addr, unsigned long value)
20fa1c3ff9SVineet Gupta {
21fa1c3ff9SVineet Gupta 	*addr = (value & 0xffff0000) >> 16;
22fa1c3ff9SVineet Gupta 	*(addr + 1) = (value & 0xffff);
23fa1c3ff9SVineet Gupta }
24fa1c3ff9SVineet Gupta 
25*6716dbbdSVineet Gupta /*
26*6716dbbdSVineet Gupta  * This gets called before relocation loop in generic loader
27*6716dbbdSVineet Gupta  * Make a note of the section index of unwinding section
28854a0d95SVineet Gupta  */
29854a0d95SVineet Gupta int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
30854a0d95SVineet Gupta 			      char *secstr, struct module *mod)
31854a0d95SVineet Gupta {
32854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND
33854a0d95SVineet Gupta 	int i;
34854a0d95SVineet Gupta 
35854a0d95SVineet Gupta 	mod->arch.unw_sec_idx = 0;
36854a0d95SVineet Gupta 	mod->arch.unw_info = NULL;
37854a0d95SVineet Gupta 
38854a0d95SVineet Gupta 	for (i = 1; i < hdr->e_shnum; i++) {
39*6716dbbdSVineet Gupta 		if (strcmp(secstr+sechdrs[i].sh_name, ".eh_frame") == 0) {
40854a0d95SVineet Gupta 			mod->arch.unw_sec_idx = i;
41854a0d95SVineet Gupta 			break;
42854a0d95SVineet Gupta 		}
43854a0d95SVineet Gupta 	}
44854a0d95SVineet Gupta #endif
45854a0d95SVineet Gupta 	return 0;
46854a0d95SVineet Gupta }
47854a0d95SVineet Gupta 
48854a0d95SVineet Gupta void module_arch_cleanup(struct module *mod)
49854a0d95SVineet Gupta {
50854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND
51854a0d95SVineet Gupta 	if (mod->arch.unw_info)
52854a0d95SVineet Gupta 		unwind_remove_table(mod->arch.unw_info, 0);
53854a0d95SVineet Gupta #endif
54854a0d95SVineet Gupta }
55854a0d95SVineet Gupta 
56fa1c3ff9SVineet Gupta int apply_relocate_add(Elf32_Shdr *sechdrs,
57fa1c3ff9SVineet Gupta 		       const char *strtab,
58fa1c3ff9SVineet Gupta 		       unsigned int symindex,	/* sec index for sym tbl */
59fa1c3ff9SVineet Gupta 		       unsigned int relsec,	/* sec index for relo sec */
60fa1c3ff9SVineet Gupta 		       struct module *module)
61fa1c3ff9SVineet Gupta {
62fa1c3ff9SVineet Gupta 	int i, n;
63fa1c3ff9SVineet Gupta 	Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
64fa1c3ff9SVineet Gupta 	Elf32_Sym *sym_entry, *sym_sec;
65fa1c3ff9SVineet Gupta 	Elf32_Addr relocation;
66fa1c3ff9SVineet Gupta 	Elf32_Addr location;
67fa1c3ff9SVineet Gupta 	Elf32_Addr sec_to_patch;
68fa1c3ff9SVineet Gupta 	int relo_type;
69fa1c3ff9SVineet Gupta 
70fa1c3ff9SVineet Gupta 	sec_to_patch = sechdrs[sechdrs[relsec].sh_info].sh_addr;
71fa1c3ff9SVineet Gupta 	sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
72fa1c3ff9SVineet Gupta 	n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
73fa1c3ff9SVineet Gupta 
74fa1c3ff9SVineet Gupta 	pr_debug("\n========== Module Sym reloc ===========================\n");
75fa1c3ff9SVineet Gupta 	pr_debug("Section to fixup %x\n", sec_to_patch);
76fa1c3ff9SVineet Gupta 	pr_debug("=========================================================\n");
77fa1c3ff9SVineet Gupta 	pr_debug("rela->r_off | rela->addend | sym->st_value | ADDR | VALUE\n");
78fa1c3ff9SVineet Gupta 	pr_debug("=========================================================\n");
79fa1c3ff9SVineet Gupta 
80fa1c3ff9SVineet Gupta 	/* Loop thru entries in relocation section */
81fa1c3ff9SVineet Gupta 	for (i = 0; i < n; i++) {
82fa1c3ff9SVineet Gupta 
83fa1c3ff9SVineet Gupta 		/* This is where to make the change */
84fa1c3ff9SVineet Gupta 		location = sec_to_patch + rel_entry[i].r_offset;
85fa1c3ff9SVineet Gupta 
86fa1c3ff9SVineet Gupta 		/* This is the symbol it is referring to.  Note that all
87fa1c3ff9SVineet Gupta 		   undefined symbols have been resolved.  */
88fa1c3ff9SVineet Gupta 		sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
89fa1c3ff9SVineet Gupta 
90fa1c3ff9SVineet Gupta 		relocation = sym_entry->st_value + rel_entry[i].r_addend;
91fa1c3ff9SVineet Gupta 
92fa1c3ff9SVineet Gupta 		pr_debug("\t%x\t\t%x\t\t%x  %x %x [%s]\n",
93fa1c3ff9SVineet Gupta 			rel_entry[i].r_offset, rel_entry[i].r_addend,
94fa1c3ff9SVineet Gupta 			sym_entry->st_value, location, relocation,
95fa1c3ff9SVineet Gupta 			strtab + sym_entry->st_name);
96fa1c3ff9SVineet Gupta 
97fa1c3ff9SVineet Gupta 		/* This assumes modules are built with -mlong-calls
98fa1c3ff9SVineet Gupta 		 * so any branches/jumps are absolute 32 bit jmps
99fa1c3ff9SVineet Gupta 		 * global data access again is abs 32 bit.
100fa1c3ff9SVineet Gupta 		 * Both of these are handled by same relocation type
101fa1c3ff9SVineet Gupta 		 */
102fa1c3ff9SVineet Gupta 		relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
103fa1c3ff9SVineet Gupta 
10494f4fb08SVineet Gupta 		if (likely(R_ARC_32_ME == relo_type))	/* ME ( S + A ) */
105fa1c3ff9SVineet Gupta 			arc_write_me((unsigned short *)location, relocation);
10694f4fb08SVineet Gupta 		else if (R_ARC_32 == relo_type)		/* ( S + A ) */
107fa1c3ff9SVineet Gupta 			*((Elf32_Addr *) location) = relocation;
10894f4fb08SVineet Gupta 		else if (R_ARC_32_PCREL == relo_type)	/* ( S + A ) - PDATA ) */
10994f4fb08SVineet Gupta 			*((Elf32_Addr *) location) = relocation - location;
110fa1c3ff9SVineet Gupta 		else
111fa1c3ff9SVineet Gupta 			goto relo_err;
112fa1c3ff9SVineet Gupta 
113fa1c3ff9SVineet Gupta 	}
114fa1c3ff9SVineet Gupta 	return 0;
115fa1c3ff9SVineet Gupta 
116fa1c3ff9SVineet Gupta relo_err:
117fa1c3ff9SVineet Gupta 	pr_err("%s: unknown relocation: %u\n",
118fa1c3ff9SVineet Gupta 		module->name, ELF32_R_TYPE(rel_entry[i].r_info));
119fa1c3ff9SVineet Gupta 	return -ENOEXEC;
120fa1c3ff9SVineet Gupta 
121fa1c3ff9SVineet Gupta }
122854a0d95SVineet Gupta 
123854a0d95SVineet Gupta /* Just before lift off: After sections have been relocated, we add the
124854a0d95SVineet Gupta  * dwarf section to unwinder table pool
125854a0d95SVineet Gupta  * This couldn't be done in module_frob_arch_sections() because
126854a0d95SVineet Gupta  * relocations had not been applied by then
127854a0d95SVineet Gupta  */
128854a0d95SVineet Gupta int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
129854a0d95SVineet Gupta 		    struct module *mod)
130854a0d95SVineet Gupta {
131854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND
132854a0d95SVineet Gupta 	void *unw;
133854a0d95SVineet Gupta 	int unwsec = mod->arch.unw_sec_idx;
134854a0d95SVineet Gupta 
135854a0d95SVineet Gupta 	if (unwsec) {
136854a0d95SVineet Gupta 		unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
137854a0d95SVineet Gupta 				       sechdrs[unwsec].sh_size);
138854a0d95SVineet Gupta 		mod->arch.unw_info = unw;
139854a0d95SVineet Gupta 	}
140854a0d95SVineet Gupta #endif
141854a0d95SVineet Gupta 	return 0;
142854a0d95SVineet Gupta }
143