1fa1c3ff9SVineet Gupta /* 2fa1c3ff9SVineet Gupta * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 3fa1c3ff9SVineet Gupta * 4fa1c3ff9SVineet Gupta * This program is free software; you can redistribute it and/or modify 5fa1c3ff9SVineet Gupta * it under the terms of the GNU General Public License version 2 as 6fa1c3ff9SVineet Gupta * published by the Free Software Foundation. 7fa1c3ff9SVineet Gupta */ 8fa1c3ff9SVineet Gupta 9fa1c3ff9SVineet Gupta #include <linux/module.h> 10fa1c3ff9SVineet Gupta #include <linux/moduleloader.h> 11fa1c3ff9SVineet Gupta #include <linux/kernel.h> 12fa1c3ff9SVineet Gupta #include <linux/elf.h> 13fa1c3ff9SVineet Gupta #include <linux/vmalloc.h> 14fa1c3ff9SVineet Gupta #include <linux/slab.h> 15fa1c3ff9SVineet Gupta #include <linux/fs.h> 16fa1c3ff9SVineet Gupta #include <linux/string.h> 17854a0d95SVineet Gupta #include <asm/unwind.h> 18fa1c3ff9SVineet Gupta 19fa1c3ff9SVineet Gupta static inline void arc_write_me(unsigned short *addr, unsigned long value) 20fa1c3ff9SVineet Gupta { 21fa1c3ff9SVineet Gupta *addr = (value & 0xffff0000) >> 16; 22fa1c3ff9SVineet Gupta *(addr + 1) = (value & 0xffff); 23fa1c3ff9SVineet Gupta } 24fa1c3ff9SVineet Gupta 256716dbbdSVineet Gupta /* 266716dbbdSVineet Gupta * This gets called before relocation loop in generic loader 276716dbbdSVineet Gupta * Make a note of the section index of unwinding section 28854a0d95SVineet Gupta */ 29854a0d95SVineet Gupta int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 30854a0d95SVineet Gupta char *secstr, struct module *mod) 31854a0d95SVineet Gupta { 32854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND 33854a0d95SVineet Gupta mod->arch.unw_sec_idx = 0; 34854a0d95SVineet Gupta mod->arch.unw_info = NULL; 35d65283f7SVineet Gupta mod->arch.secstr = secstr; 36854a0d95SVineet Gupta #endif 37854a0d95SVineet Gupta return 0; 38854a0d95SVineet Gupta } 39854a0d95SVineet Gupta 40854a0d95SVineet Gupta void module_arch_cleanup(struct module *mod) 41854a0d95SVineet Gupta { 42854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND 43854a0d95SVineet Gupta if (mod->arch.unw_info) 44854a0d95SVineet Gupta unwind_remove_table(mod->arch.unw_info, 0); 45854a0d95SVineet Gupta #endif 46854a0d95SVineet Gupta } 47854a0d95SVineet Gupta 48fa1c3ff9SVineet Gupta int apply_relocate_add(Elf32_Shdr *sechdrs, 49fa1c3ff9SVineet Gupta const char *strtab, 50fa1c3ff9SVineet Gupta unsigned int symindex, /* sec index for sym tbl */ 51fa1c3ff9SVineet Gupta unsigned int relsec, /* sec index for relo sec */ 52fa1c3ff9SVineet Gupta struct module *module) 53fa1c3ff9SVineet Gupta { 54*b75dcd9cSVineet Gupta int i, n, relo_type; 55fa1c3ff9SVineet Gupta Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr; 56fa1c3ff9SVineet Gupta Elf32_Sym *sym_entry, *sym_sec; 57*b75dcd9cSVineet Gupta Elf32_Addr relocation, location, tgt_addr; 58d65283f7SVineet Gupta unsigned int tgtsec; 59fa1c3ff9SVineet Gupta 60*b75dcd9cSVineet Gupta /* 61*b75dcd9cSVineet Gupta * @relsec has relocations e.g. .rela.init.text 62*b75dcd9cSVineet Gupta * @tgtsec is section to patch e.g. .init.text 63*b75dcd9cSVineet Gupta */ 64d65283f7SVineet Gupta tgtsec = sechdrs[relsec].sh_info; 65*b75dcd9cSVineet Gupta tgt_addr = sechdrs[tgtsec].sh_addr; 66fa1c3ff9SVineet Gupta sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr; 67fa1c3ff9SVineet Gupta n = sechdrs[relsec].sh_size / sizeof(*rel_entry); 68fa1c3ff9SVineet Gupta 69*b75dcd9cSVineet Gupta pr_debug("\nSection to fixup %s @%x\n", 70*b75dcd9cSVineet Gupta module->arch.secstr + sechdrs[tgtsec].sh_name, tgt_addr); 71fa1c3ff9SVineet Gupta pr_debug("=========================================================\n"); 72*b75dcd9cSVineet Gupta pr_debug("r_off\tr_add\tst_value ADDRESS VALUE\n"); 73fa1c3ff9SVineet Gupta pr_debug("=========================================================\n"); 74fa1c3ff9SVineet Gupta 75fa1c3ff9SVineet Gupta /* Loop thru entries in relocation section */ 76fa1c3ff9SVineet Gupta for (i = 0; i < n; i++) { 77*b75dcd9cSVineet Gupta const char *s; 78fa1c3ff9SVineet Gupta 79fa1c3ff9SVineet Gupta /* This is where to make the change */ 80*b75dcd9cSVineet Gupta location = tgt_addr + rel_entry[i].r_offset; 81fa1c3ff9SVineet Gupta 82fa1c3ff9SVineet Gupta /* This is the symbol it is referring to. Note that all 83fa1c3ff9SVineet Gupta undefined symbols have been resolved. */ 84fa1c3ff9SVineet Gupta sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info); 85fa1c3ff9SVineet Gupta 86fa1c3ff9SVineet Gupta relocation = sym_entry->st_value + rel_entry[i].r_addend; 87fa1c3ff9SVineet Gupta 88*b75dcd9cSVineet Gupta if (sym_entry->st_name == 0 && ELF_ST_TYPE (sym_entry->st_info) == STT_SECTION) { 89*b75dcd9cSVineet Gupta s = module->arch.secstr + sechdrs[sym_entry->st_shndx].sh_name; 90*b75dcd9cSVineet Gupta } else { 91*b75dcd9cSVineet Gupta s = strtab + sym_entry->st_name; 92*b75dcd9cSVineet Gupta } 93*b75dcd9cSVineet Gupta 94*b75dcd9cSVineet Gupta pr_debug(" %x\t%x\t%x %x %x [%s]\n", 95fa1c3ff9SVineet Gupta rel_entry[i].r_offset, rel_entry[i].r_addend, 96*b75dcd9cSVineet Gupta sym_entry->st_value, location, relocation, s); 97fa1c3ff9SVineet Gupta 98fa1c3ff9SVineet Gupta /* This assumes modules are built with -mlong-calls 99fa1c3ff9SVineet Gupta * so any branches/jumps are absolute 32 bit jmps 100fa1c3ff9SVineet Gupta * global data access again is abs 32 bit. 101fa1c3ff9SVineet Gupta * Both of these are handled by same relocation type 102fa1c3ff9SVineet Gupta */ 103fa1c3ff9SVineet Gupta relo_type = ELF32_R_TYPE(rel_entry[i].r_info); 104fa1c3ff9SVineet Gupta 10594f4fb08SVineet Gupta if (likely(R_ARC_32_ME == relo_type)) /* ME ( S + A ) */ 106fa1c3ff9SVineet Gupta arc_write_me((unsigned short *)location, relocation); 10794f4fb08SVineet Gupta else if (R_ARC_32 == relo_type) /* ( S + A ) */ 108fa1c3ff9SVineet Gupta *((Elf32_Addr *) location) = relocation; 10994f4fb08SVineet Gupta else if (R_ARC_32_PCREL == relo_type) /* ( S + A ) - PDATA ) */ 11094f4fb08SVineet Gupta *((Elf32_Addr *) location) = relocation - location; 111fa1c3ff9SVineet Gupta else 112fa1c3ff9SVineet Gupta goto relo_err; 113fa1c3ff9SVineet Gupta 114fa1c3ff9SVineet Gupta } 115d65283f7SVineet Gupta 116d65283f7SVineet Gupta if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0) 117d65283f7SVineet Gupta module->arch.unw_sec_idx = tgtsec; 118d65283f7SVineet Gupta 119fa1c3ff9SVineet Gupta return 0; 120fa1c3ff9SVineet Gupta 121fa1c3ff9SVineet Gupta relo_err: 122fa1c3ff9SVineet Gupta pr_err("%s: unknown relocation: %u\n", 123fa1c3ff9SVineet Gupta module->name, ELF32_R_TYPE(rel_entry[i].r_info)); 124fa1c3ff9SVineet Gupta return -ENOEXEC; 125fa1c3ff9SVineet Gupta 126fa1c3ff9SVineet Gupta } 127854a0d95SVineet Gupta 128854a0d95SVineet Gupta /* Just before lift off: After sections have been relocated, we add the 129854a0d95SVineet Gupta * dwarf section to unwinder table pool 130854a0d95SVineet Gupta * This couldn't be done in module_frob_arch_sections() because 131854a0d95SVineet Gupta * relocations had not been applied by then 132854a0d95SVineet Gupta */ 133854a0d95SVineet Gupta int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, 134854a0d95SVineet Gupta struct module *mod) 135854a0d95SVineet Gupta { 136854a0d95SVineet Gupta #ifdef CONFIG_ARC_DW2_UNWIND 137854a0d95SVineet Gupta void *unw; 138854a0d95SVineet Gupta int unwsec = mod->arch.unw_sec_idx; 139854a0d95SVineet Gupta 140854a0d95SVineet Gupta if (unwsec) { 141854a0d95SVineet Gupta unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr, 142854a0d95SVineet Gupta sechdrs[unwsec].sh_size); 143854a0d95SVineet Gupta mod->arch.unw_info = unw; 144854a0d95SVineet Gupta } 145854a0d95SVineet Gupta #endif 146854a0d95SVineet Gupta return 0; 147854a0d95SVineet Gupta } 148