2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/moduleloader.h>
11 #include <linux/kernel.h>
12 #include <linux/elf.h>
13 #include <linux/vmalloc.h>
14 #include <linux/slab.h>
16 #include <linux/string.h>
18 static inline void arc_write_me(unsigned short *addr, unsigned long value)
20 *addr = (value & 0xffff0000) >> 16;
21 *(addr + 1) = (value & 0xffff);
24 int apply_relocate_add(Elf32_Shdr *sechdrs,
26 unsigned int symindex, /* sec index for sym tbl */
27 unsigned int relsec, /* sec index for relo sec */
28 struct module *module)
31 Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
32 Elf32_Sym *sym_entry, *sym_sec;
33 Elf32_Addr relocation;
35 Elf32_Addr sec_to_patch;
38 sec_to_patch = sechdrs[sechdrs[relsec].sh_info].sh_addr;
39 sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
40 n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
42 pr_debug("\n========== Module Sym reloc ===========================\n");
43 pr_debug("Section to fixup %x\n", sec_to_patch);
44 pr_debug("=========================================================\n");
45 pr_debug("rela->r_off | rela->addend | sym->st_value | ADDR | VALUE\n");
46 pr_debug("=========================================================\n");
48 /* Loop thru entries in relocation section */
49 for (i = 0; i < n; i++) {
51 /* This is where to make the change */
52 location = sec_to_patch + rel_entry[i].r_offset;
54 /* This is the symbol it is referring to. Note that all
55 undefined symbols have been resolved. */
56 sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
58 relocation = sym_entry->st_value + rel_entry[i].r_addend;
60 pr_debug("\t%x\t\t%x\t\t%x %x %x [%s]\n",
61 rel_entry[i].r_offset, rel_entry[i].r_addend,
62 sym_entry->st_value, location, relocation,
63 strtab + sym_entry->st_name);
65 /* This assumes modules are built with -mlong-calls
66 * so any branches/jumps are absolute 32 bit jmps
67 * global data access again is abs 32 bit.
68 * Both of these are handled by same relocation type
70 relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
72 if (likely(R_ARC_32_ME == relo_type))
73 arc_write_me((unsigned short *)location, relocation);
74 else if (R_ARC_32 == relo_type)
75 *((Elf32_Addr *) location) = relocation;
83 pr_err("%s: unknown relocation: %u\n",
84 module->name, ELF32_R_TYPE(rel_entry[i].r_info));