1/*
2 * arch/score/kernel/module.c
3 *
4 * Score Processor version.
5 *
6 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
7 *  Chen Liqin <liqin.chen@sunplusct.com>
8 *  Lennox Wu <lennox.wu@sunplusct.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see the file COPYING, or write
22 * to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 */
25
26#include <linux/moduleloader.h>
27#include <linux/module.h>
28#include <linux/vmalloc.h>
29
30int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
31		unsigned int symindex, unsigned int relindex,
32		struct module *me)
33{
34	Elf32_Shdr *symsec = sechdrs + symindex;
35	Elf32_Shdr *relsec = sechdrs + relindex;
36	Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
37	Elf32_Rel *rel = (void *)relsec->sh_addr;
38	unsigned int i;
39
40	for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
41		unsigned long loc;
42		Elf32_Sym *sym;
43		s32 r_offset;
44
45		r_offset = ELF32_R_SYM(rel->r_info);
46		if ((r_offset < 0) ||
47		    (r_offset > (symsec->sh_size / sizeof(Elf32_Sym)))) {
48			printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
49				me->name, relindex, i);
50				return -ENOEXEC;
51		}
52
53		sym = ((Elf32_Sym *)symsec->sh_addr) + r_offset;
54
55		if ((rel->r_offset < 0) ||
56		    (rel->r_offset > dstsec->sh_size - sizeof(u32))) {
57			printk(KERN_ERR "%s: out of bounds relocation, "
58				"section %d reloc %d offset %d size %d\n",
59				me->name, relindex, i, rel->r_offset,
60				dstsec->sh_size);
61			return -ENOEXEC;
62		}
63
64		loc = dstsec->sh_addr + rel->r_offset;
65		switch (ELF32_R_TYPE(rel->r_info)) {
66		case R_SCORE_NONE:
67			break;
68		case R_SCORE_ABS32:
69			*(unsigned long *)loc += sym->st_value;
70			break;
71		case R_SCORE_HI16:
72			break;
73		case R_SCORE_LO16: {
74			unsigned long hi16_offset, offset;
75			unsigned long uvalue;
76			unsigned long temp, temp_hi;
77			temp_hi = *((unsigned long *)loc - 1);
78			temp = *(unsigned long *)loc;
79
80			hi16_offset = (((((temp_hi) >> 16) & 0x3) << 15) |
81					((temp_hi) & 0x7fff)) >> 1;
82			offset = ((temp >> 16 & 0x03) << 15) |
83					((temp & 0x7fff) >> 1);
84			offset = (hi16_offset << 16) | (offset & 0xffff);
85			uvalue = sym->st_value + offset;
86			hi16_offset = (uvalue >> 16) << 1;
87
88			temp_hi = ((temp_hi) & (~(0x37fff))) |
89					(hi16_offset & 0x7fff) |
90					((hi16_offset << 1) & 0x30000);
91			*((unsigned long *)loc - 1) = temp_hi;
92
93			offset = (uvalue & 0xffff) << 1;
94			temp = (temp & (~(0x37fff))) | (offset & 0x7fff) |
95				((offset << 1) & 0x30000);
96			*(unsigned long *)loc = temp;
97			break;
98		}
99		case R_SCORE_24: {
100			unsigned long hi16_offset, offset;
101			unsigned long uvalue;
102			unsigned long temp;
103
104			temp = *(unsigned long *)loc;
105			offset = (temp & 0x03FF7FFE);
106			hi16_offset = (offset & 0xFFFF0000);
107			offset = (hi16_offset | ((offset & 0xFFFF) << 1)) >> 2;
108
109			uvalue = (sym->st_value + offset) >> 1;
110			uvalue = uvalue & 0x00ffffff;
111
112			temp = (temp & 0xfc008001) |
113				((uvalue << 2) & 0x3ff0000) |
114				((uvalue & 0x3fff) << 1);
115			*(unsigned long *)loc = temp;
116			break;
117		}
118		default:
119			printk(KERN_ERR "%s: unknown relocation: %u\n",
120				me->name, ELF32_R_TYPE(rel->r_info));
121			return -ENOEXEC;
122		}
123	}
124
125	return 0;
126}
127
128/* Given an address, look for it in the module exception tables. */
129const struct exception_table_entry *search_module_dbetables(unsigned long addr)
130{
131	return NULL;
132}
133