summaryrefslogtreecommitdiffstats
path: root/arch/score/kernel/module.c
blob: 469e3b64e2f26aa760a307d68827eacc0768df21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * arch/score/kernel/module.c
 *
 * Score Processor version.
 *
 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
 *  Chen Liqin <liqin.chen@sunplusct.com>
 *  Lennox Wu <lennox.wu@sunplusct.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see the file COPYING, or write
 * to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/moduleloader.h>
#include <linux/module.h>
#include <linux/vmalloc.h>

int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
		unsigned int symindex, unsigned int relindex,
		struct module *me)
{
	Elf32_Shdr *symsec = sechdrs + symindex;
	Elf32_Shdr *relsec = sechdrs + relindex;
	Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
	Elf32_Rel *rel = (void *)relsec->sh_addr;
	unsigned int i;

	for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
		unsigned long loc;
		Elf32_Sym *sym;
		s32 r_offset;

		r_offset = ELF32_R_SYM(rel->r_info);
		if ((r_offset < 0) ||
		    (r_offset > (symsec->sh_size / sizeof(Elf32_Sym)))) {
			printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
				me->name, relindex, i);
				return -ENOEXEC;
		}

		sym = ((Elf32_Sym *)symsec->sh_addr) + r_offset;

		if ((rel->r_offset < 0) ||
		    (rel->r_offset > dstsec->sh_size - sizeof(u32))) {
			printk(KERN_ERR "%s: out of bounds relocation, "
				"section %d reloc %d offset %d size %d\n",
				me->name, relindex, i, rel->r_offset,
				dstsec->sh_size);
			return -ENOEXEC;
		}

		loc = dstsec->sh_addr + rel->r_offset;
		switch (ELF32_R_TYPE(rel->r_info)) {
		case R_SCORE_NONE:
			break;
		case R_SCORE_ABS32:
			*(unsigned long *)loc += sym->st_value;
			break;
		case R_SCORE_HI16:
			break;
		case R_SCORE_LO16: {
			unsigned long hi16_offset, offset;
			unsigned long uvalue;
			unsigned long temp, temp_hi;
			temp_hi = *((unsigned long *)loc - 1);
			temp = *(unsigned long *)loc;

			hi16_offset = (((((temp_hi) >> 16) & 0x3) << 15) |
					((temp_hi) & 0x7fff)) >> 1;
			offset = ((temp >> 16 & 0x03) << 15) |
					((temp & 0x7fff) >> 1);
			offset = (hi16_offset << 16) | (offset & 0xffff);
			uvalue = sym->st_value + offset;
			hi16_offset = (uvalue >> 16) << 1;

			temp_hi = ((temp_hi) & (~(0x37fff))) |
					(hi16_offset & 0x7fff) |
					((hi16_offset << 1) & 0x30000);
			*((unsigned long *)loc - 1) = temp_hi;

			offset = (uvalue & 0xffff) << 1;
			temp = (temp & (~(0x37fff))) | (offset & 0x7fff) |
				((offset << 1) & 0x30000);
			*(unsigned long *)loc = temp;
			break;
		}
		case R_SCORE_24: {
			unsigned long hi16_offset, offset;
			unsigned long uvalue;
			unsigned long temp;

			temp = *(unsigned long *)loc;
			offset = (temp & 0x03FF7FFE);
			hi16_offset = (offset & 0xFFFF0000);
			offset = (hi16_offset | ((offset & 0xFFFF) << 1)) >> 2;

			uvalue = (sym->st_value + offset) >> 1;
			uvalue = uvalue & 0x00ffffff;

			temp = (temp & 0xfc008001) |
				((uvalue << 2) & 0x3ff0000) |
				((uvalue & 0x3fff) << 1);
			*(unsigned long *)loc = temp;
			break;
		}
		default:
			printk(KERN_ERR "%s: unknown relocation: %u\n",
				me->name, ELF32_R_TYPE(rel->r_info));
			return -ENOEXEC;
		}
	}

	return 0;
}

int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
		unsigned int symindex, unsigned int relsec,
		struct module *me)
{
	/* Non-standard return value... most other arch's return -ENOEXEC
	 * for an unsupported relocation variant
	 */
	return 0;
}

/* Given an address, look for it in the module exception tables. */
const struct exception_table_entry *search_module_dbetables(unsigned long addr)
{
	return NULL;
}