Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/moduleloader.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/elf.h> |
| 10 | #include <linux/vmalloc.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/string.h> |
Vineet Gupta | 854a0d9 | 2013-01-22 17:03:19 +0530 | [diff] [blame] | 14 | #include <asm/unwind.h> |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 15 | |
| 16 | static inline void arc_write_me(unsigned short *addr, unsigned long value) |
| 17 | { |
| 18 | *addr = (value & 0xffff0000) >> 16; |
| 19 | *(addr + 1) = (value & 0xffff); |
| 20 | } |
| 21 | |
Vineet Gupta | 6716dbb | 2013-06-24 21:22:06 +0530 | [diff] [blame] | 22 | /* |
| 23 | * This gets called before relocation loop in generic loader |
| 24 | * Make a note of the section index of unwinding section |
Vineet Gupta | 854a0d9 | 2013-01-22 17:03:19 +0530 | [diff] [blame] | 25 | */ |
| 26 | int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, |
| 27 | char *secstr, struct module *mod) |
| 28 | { |
| 29 | #ifdef CONFIG_ARC_DW2_UNWIND |
Vineet Gupta | 854a0d9 | 2013-01-22 17:03:19 +0530 | [diff] [blame] | 30 | mod->arch.unw_sec_idx = 0; |
| 31 | mod->arch.unw_info = NULL; |
Vineet Gupta | 854a0d9 | 2013-01-22 17:03:19 +0530 | [diff] [blame] | 32 | #endif |
Vineet Gupta | eb1357d | 2017-01-16 10:48:09 -0800 | [diff] [blame] | 33 | mod->arch.secstr = secstr; |
Sachin Kamat | 955ad59 | 2013-03-06 16:53:45 +0530 | [diff] [blame] | 34 | return 0; |
Vineet Gupta | 854a0d9 | 2013-01-22 17:03:19 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void module_arch_cleanup(struct module *mod) |
| 38 | { |
| 39 | #ifdef CONFIG_ARC_DW2_UNWIND |
| 40 | if (mod->arch.unw_info) |
| 41 | unwind_remove_table(mod->arch.unw_info, 0); |
| 42 | #endif |
| 43 | } |
| 44 | |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 45 | int apply_relocate_add(Elf32_Shdr *sechdrs, |
| 46 | const char *strtab, |
| 47 | unsigned int symindex, /* sec index for sym tbl */ |
| 48 | unsigned int relsec, /* sec index for relo sec */ |
| 49 | struct module *module) |
| 50 | { |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 51 | int i, n, relo_type; |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 52 | Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr; |
| 53 | Elf32_Sym *sym_entry, *sym_sec; |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 54 | Elf32_Addr relocation, location, tgt_addr; |
Vineet Gupta | d65283f | 2016-10-25 10:43:20 -0700 | [diff] [blame] | 55 | unsigned int tgtsec; |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 56 | |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 57 | /* |
| 58 | * @relsec has relocations e.g. .rela.init.text |
| 59 | * @tgtsec is section to patch e.g. .init.text |
| 60 | */ |
Vineet Gupta | d65283f | 2016-10-25 10:43:20 -0700 | [diff] [blame] | 61 | tgtsec = sechdrs[relsec].sh_info; |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 62 | tgt_addr = sechdrs[tgtsec].sh_addr; |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 63 | sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr; |
| 64 | n = sechdrs[relsec].sh_size / sizeof(*rel_entry); |
| 65 | |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 66 | pr_debug("\nSection to fixup %s @%x\n", |
| 67 | module->arch.secstr + sechdrs[tgtsec].sh_name, tgt_addr); |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 68 | pr_debug("=========================================================\n"); |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 69 | pr_debug("r_off\tr_add\tst_value ADDRESS VALUE\n"); |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 70 | pr_debug("=========================================================\n"); |
| 71 | |
| 72 | /* Loop thru entries in relocation section */ |
| 73 | for (i = 0; i < n; i++) { |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 74 | const char *s; |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 75 | |
| 76 | /* This is where to make the change */ |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 77 | location = tgt_addr + rel_entry[i].r_offset; |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 78 | |
| 79 | /* This is the symbol it is referring to. Note that all |
| 80 | undefined symbols have been resolved. */ |
| 81 | sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info); |
| 82 | |
| 83 | relocation = sym_entry->st_value + rel_entry[i].r_addend; |
| 84 | |
Vineet Gupta | b75dcd9 | 2016-10-25 11:23:19 -0700 | [diff] [blame] | 85 | if (sym_entry->st_name == 0 && ELF_ST_TYPE (sym_entry->st_info) == STT_SECTION) { |
| 86 | s = module->arch.secstr + sechdrs[sym_entry->st_shndx].sh_name; |
| 87 | } else { |
| 88 | s = strtab + sym_entry->st_name; |
| 89 | } |
| 90 | |
| 91 | pr_debug(" %x\t%x\t%x %x %x [%s]\n", |
| 92 | rel_entry[i].r_offset, rel_entry[i].r_addend, |
| 93 | sym_entry->st_value, location, relocation, s); |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 94 | |
| 95 | /* This assumes modules are built with -mlong-calls |
| 96 | * so any branches/jumps are absolute 32 bit jmps |
| 97 | * global data access again is abs 32 bit. |
| 98 | * Both of these are handled by same relocation type |
| 99 | */ |
| 100 | relo_type = ELF32_R_TYPE(rel_entry[i].r_info); |
| 101 | |
Vineet Gupta | 94f4fb0 | 2016-09-12 16:50:50 -0700 | [diff] [blame] | 102 | if (likely(R_ARC_32_ME == relo_type)) /* ME ( S + A ) */ |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 103 | arc_write_me((unsigned short *)location, relocation); |
Vineet Gupta | 94f4fb0 | 2016-09-12 16:50:50 -0700 | [diff] [blame] | 104 | else if (R_ARC_32 == relo_type) /* ( S + A ) */ |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 105 | *((Elf32_Addr *) location) = relocation; |
Vineet Gupta | 94f4fb0 | 2016-09-12 16:50:50 -0700 | [diff] [blame] | 106 | else if (R_ARC_32_PCREL == relo_type) /* ( S + A ) - PDATA ) */ |
| 107 | *((Elf32_Addr *) location) = relocation - location; |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 108 | else |
| 109 | goto relo_err; |
| 110 | |
| 111 | } |
Vineet Gupta | d65283f | 2016-10-25 10:43:20 -0700 | [diff] [blame] | 112 | |
Vineet Gupta | eb1357d | 2017-01-16 10:48:09 -0800 | [diff] [blame] | 113 | #ifdef CONFIG_ARC_DW2_UNWIND |
Vineet Gupta | d65283f | 2016-10-25 10:43:20 -0700 | [diff] [blame] | 114 | if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0) |
| 115 | module->arch.unw_sec_idx = tgtsec; |
Vineet Gupta | eb1357d | 2017-01-16 10:48:09 -0800 | [diff] [blame] | 116 | #endif |
Vineet Gupta | d65283f | 2016-10-25 10:43:20 -0700 | [diff] [blame] | 117 | |
Vineet Gupta | fa1c3ff | 2013-01-29 19:28:05 +0530 | [diff] [blame] | 118 | return 0; |
| 119 | |
| 120 | relo_err: |
| 121 | pr_err("%s: unknown relocation: %u\n", |
| 122 | module->name, ELF32_R_TYPE(rel_entry[i].r_info)); |
| 123 | return -ENOEXEC; |
| 124 | |
| 125 | } |
Vineet Gupta | 854a0d9 | 2013-01-22 17:03:19 +0530 | [diff] [blame] | 126 | |
| 127 | /* Just before lift off: After sections have been relocated, we add the |
| 128 | * dwarf section to unwinder table pool |
| 129 | * This couldn't be done in module_frob_arch_sections() because |
| 130 | * relocations had not been applied by then |
| 131 | */ |
| 132 | int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, |
| 133 | struct module *mod) |
| 134 | { |
| 135 | #ifdef CONFIG_ARC_DW2_UNWIND |
| 136 | void *unw; |
| 137 | int unwsec = mod->arch.unw_sec_idx; |
| 138 | |
| 139 | if (unwsec) { |
| 140 | unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr, |
| 141 | sechdrs[unwsec].sh_size); |
| 142 | mod->arch.unw_info = unw; |
| 143 | } |
| 144 | #endif |
Sachin Kamat | 955ad59 | 2013-03-06 16:53:45 +0530 | [diff] [blame] | 145 | return 0; |
Vineet Gupta | 854a0d9 | 2013-01-22 17:03:19 +0530 | [diff] [blame] | 146 | } |