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