0001
0002
0003
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
0024
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,
0048 unsigned int relsec,
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
0059
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
0073 for (i = 0; i < n; i++) {
0074 const char *s;
0075
0076
0077 location = tgt_addr + rel_entry[i].r_offset;
0078
0079
0080
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
0096
0097
0098
0099
0100 relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
0101
0102 if (likely(R_ARC_32_ME == relo_type))
0103 arc_write_me((unsigned short *)location, relocation);
0104 else if (R_ARC_32 == relo_type)
0105 *((Elf32_Addr *) location) = relocation;
0106 else if (R_ARC_32_PCREL == relo_type)
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
0128
0129
0130
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 }