blob: 9a2849756022c01c2b3c6da9b7b5a0e371ed2e20 [file] [log] [blame]
Vineet Guptafa1c3ff2013-01-29 19:28:05 +05301/*
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>
Vineet Gupta854a0d92013-01-22 17:03:19 +053017#include <asm/unwind.h>
Vineet Guptafa1c3ff2013-01-29 19:28:05 +053018
19static inline void arc_write_me(unsigned short *addr, unsigned long value)
20{
21 *addr = (value & 0xffff0000) >> 16;
22 *(addr + 1) = (value & 0xffff);
23}
24
Vineet Gupta6716dbb2013-06-24 21:22:06 +053025/*
26 * This gets called before relocation loop in generic loader
27 * Make a note of the section index of unwinding section
Vineet Gupta854a0d92013-01-22 17:03:19 +053028 */
29int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
30 char *secstr, struct module *mod)
31{
32#ifdef CONFIG_ARC_DW2_UNWIND
33 int i;
34
35 mod->arch.unw_sec_idx = 0;
36 mod->arch.unw_info = NULL;
37
38 for (i = 1; i < hdr->e_shnum; i++) {
Vineet Gupta6716dbb2013-06-24 21:22:06 +053039 if (strcmp(secstr+sechdrs[i].sh_name, ".eh_frame") == 0) {
Vineet Gupta854a0d92013-01-22 17:03:19 +053040 mod->arch.unw_sec_idx = i;
41 break;
42 }
43 }
44#endif
Sachin Kamat955ad592013-03-06 16:53:45 +053045 return 0;
Vineet Gupta854a0d92013-01-22 17:03:19 +053046}
47
48void module_arch_cleanup(struct module *mod)
49{
50#ifdef CONFIG_ARC_DW2_UNWIND
51 if (mod->arch.unw_info)
52 unwind_remove_table(mod->arch.unw_info, 0);
53#endif
54}
55
Vineet Guptafa1c3ff2013-01-29 19:28:05 +053056int apply_relocate_add(Elf32_Shdr *sechdrs,
57 const char *strtab,
58 unsigned int symindex, /* sec index for sym tbl */
59 unsigned int relsec, /* sec index for relo sec */
60 struct module *module)
61{
62 int i, n;
63 Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
64 Elf32_Sym *sym_entry, *sym_sec;
65 Elf32_Addr relocation;
66 Elf32_Addr location;
67 Elf32_Addr sec_to_patch;
68 int relo_type;
69
70 sec_to_patch = sechdrs[sechdrs[relsec].sh_info].sh_addr;
71 sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
72 n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
73
74 pr_debug("\n========== Module Sym reloc ===========================\n");
75 pr_debug("Section to fixup %x\n", sec_to_patch);
76 pr_debug("=========================================================\n");
77 pr_debug("rela->r_off | rela->addend | sym->st_value | ADDR | VALUE\n");
78 pr_debug("=========================================================\n");
79
80 /* Loop thru entries in relocation section */
81 for (i = 0; i < n; i++) {
82
83 /* This is where to make the change */
84 location = sec_to_patch + rel_entry[i].r_offset;
85
86 /* This is the symbol it is referring to. Note that all
87 undefined symbols have been resolved. */
88 sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
89
90 relocation = sym_entry->st_value + rel_entry[i].r_addend;
91
92 pr_debug("\t%x\t\t%x\t\t%x %x %x [%s]\n",
93 rel_entry[i].r_offset, rel_entry[i].r_addend,
94 sym_entry->st_value, location, relocation,
95 strtab + sym_entry->st_name);
96
97 /* This assumes modules are built with -mlong-calls
98 * so any branches/jumps are absolute 32 bit jmps
99 * global data access again is abs 32 bit.
100 * Both of these are handled by same relocation type
101 */
102 relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
103
Vineet Gupta94f4fb02016-09-12 16:50:50 -0700104 if (likely(R_ARC_32_ME == relo_type)) /* ME ( S + A ) */
Vineet Guptafa1c3ff2013-01-29 19:28:05 +0530105 arc_write_me((unsigned short *)location, relocation);
Vineet Gupta94f4fb02016-09-12 16:50:50 -0700106 else if (R_ARC_32 == relo_type) /* ( S + A ) */
Vineet Guptafa1c3ff2013-01-29 19:28:05 +0530107 *((Elf32_Addr *) location) = relocation;
Vineet Gupta94f4fb02016-09-12 16:50:50 -0700108 else if (R_ARC_32_PCREL == relo_type) /* ( S + A ) - PDATA ) */
109 *((Elf32_Addr *) location) = relocation - location;
Vineet Guptafa1c3ff2013-01-29 19:28:05 +0530110 else
111 goto relo_err;
112
113 }
114 return 0;
115
116relo_err:
117 pr_err("%s: unknown relocation: %u\n",
118 module->name, ELF32_R_TYPE(rel_entry[i].r_info));
119 return -ENOEXEC;
120
121}
Vineet Gupta854a0d92013-01-22 17:03:19 +0530122
123/* Just before lift off: After sections have been relocated, we add the
124 * dwarf section to unwinder table pool
125 * This couldn't be done in module_frob_arch_sections() because
126 * relocations had not been applied by then
127 */
128int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
129 struct module *mod)
130{
131#ifdef CONFIG_ARC_DW2_UNWIND
132 void *unw;
133 int unwsec = mod->arch.unw_sec_idx;
134
135 if (unwsec) {
136 unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
137 sechdrs[unwsec].sh_size);
138 mod->arch.unw_info = unw;
139 }
140#endif
Sachin Kamat955ad592013-03-06 16:53:45 +0530141 return 0;
Vineet Gupta854a0d92013-01-22 17:03:19 +0530142}