Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/moduleloader.h>
0008 #include <linux/kernel.h>
0009 #include <linux/elf.h>
0010 #include <linux/vmalloc.h>
0011 #include <linux/slab.h>
0012 #include <linux/fs.h>
0013 #include <linux/string.h>
0014 #include <asm/unwind.h>
0015 
0016 static inline void arc_write_me(unsigned short *addr, unsigned long value)
0017 {
0018     *addr = (value & 0xffff0000) >> 16;
0019     *(addr + 1) = (value & 0xffff);
0020 }
0021 
0022 /*
0023  * This gets called before relocation loop in generic loader
0024  * Make a note of the section index of unwinding section
0025  */
0026 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
0027                   char *secstr, struct module *mod)
0028 {
0029 #ifdef CONFIG_ARC_DW2_UNWIND
0030     mod->arch.unw_sec_idx = 0;
0031     mod->arch.unw_info = NULL;
0032 #endif
0033     mod->arch.secstr = secstr;
0034     return 0;
0035 }
0036 
0037 void module_arch_cleanup(struct module *mod)
0038 {
0039 #ifdef CONFIG_ARC_DW2_UNWIND
0040     if (mod->arch.unw_info)
0041         unwind_remove_table(mod->arch.unw_info, 0);
0042 #endif
0043 }
0044 
0045 int apply_relocate_add(Elf32_Shdr *sechdrs,
0046                const char *strtab,
0047                unsigned int symindex,   /* sec index for sym tbl */
0048                unsigned int relsec, /* sec index for relo sec */
0049                struct module *module)
0050 {
0051     int i, n, relo_type;
0052     Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
0053     Elf32_Sym *sym_entry, *sym_sec;
0054     Elf32_Addr relocation, location, tgt_addr;
0055     unsigned int tgtsec;
0056 
0057     /*
0058      * @relsec has relocations e.g. .rela.init.text
0059      * @tgtsec is section to patch e.g. .init.text
0060      */
0061     tgtsec = sechdrs[relsec].sh_info;
0062     tgt_addr = sechdrs[tgtsec].sh_addr;
0063     sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
0064     n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
0065 
0066     pr_debug("\nSection to fixup %s @%x\n",
0067          module->arch.secstr + sechdrs[tgtsec].sh_name, tgt_addr);
0068     pr_debug("=========================================================\n");
0069     pr_debug("r_off\tr_add\tst_value ADDRESS  VALUE\n");
0070     pr_debug("=========================================================\n");
0071 
0072     /* Loop thru entries in relocation section */
0073     for (i = 0; i < n; i++) {
0074         const char *s;
0075 
0076         /* This is where to make the change */
0077         location = tgt_addr + rel_entry[i].r_offset;
0078 
0079         /* This is the symbol it is referring to.  Note that all
0080            undefined symbols have been resolved.  */
0081         sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
0082 
0083         relocation = sym_entry->st_value + rel_entry[i].r_addend;
0084 
0085         if (sym_entry->st_name == 0 && ELF_ST_TYPE (sym_entry->st_info) == STT_SECTION) {
0086             s = module->arch.secstr + sechdrs[sym_entry->st_shndx].sh_name;
0087         } else {
0088             s = strtab + sym_entry->st_name;
0089         }
0090 
0091         pr_debug("   %x\t%x\t%x %x %x [%s]\n",
0092              rel_entry[i].r_offset, rel_entry[i].r_addend,
0093              sym_entry->st_value, location, relocation, s);
0094 
0095         /* This assumes modules are built with -mlong-calls
0096          * so any branches/jumps are absolute 32 bit jmps
0097          * global data access again is abs 32 bit.
0098          * Both of these are handled by same relocation type
0099          */
0100         relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
0101 
0102         if (likely(R_ARC_32_ME == relo_type))   /* ME ( S + A ) */
0103             arc_write_me((unsigned short *)location, relocation);
0104         else if (R_ARC_32 == relo_type)     /* ( S + A ) */
0105             *((Elf32_Addr *) location) = relocation;
0106         else if (R_ARC_32_PCREL == relo_type)   /* ( S + A ) - PDATA ) */
0107             *((Elf32_Addr *) location) = relocation - location;
0108         else
0109             goto relo_err;
0110 
0111     }
0112 
0113 #ifdef CONFIG_ARC_DW2_UNWIND
0114     if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0)
0115         module->arch.unw_sec_idx = tgtsec;
0116 #endif
0117 
0118     return 0;
0119 
0120 relo_err:
0121     pr_err("%s: unknown relocation: %u\n",
0122         module->name, ELF32_R_TYPE(rel_entry[i].r_info));
0123     return -ENOEXEC;
0124 
0125 }
0126 
0127 /* Just before lift off: After sections have been relocated, we add the
0128  * dwarf section to unwinder table pool
0129  * This couldn't be done in module_frob_arch_sections() because
0130  * relocations had not been applied by then
0131  */
0132 int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
0133             struct module *mod)
0134 {
0135 #ifdef CONFIG_ARC_DW2_UNWIND
0136     void *unw;
0137     int unwsec = mod->arch.unw_sec_idx;
0138 
0139     if (unwsec) {
0140         unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
0141                        sechdrs[unwsec].sh_size);
0142         mod->arch.unw_info = unw;
0143     }
0144 #endif
0145     return 0;
0146 }