Blame view

arch/sh/kernel/module.c 3.65 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*  Kernel module help for SH.
1cb80fcfe   Paul Mundt   sh: Merge sh and ...
2
3
4
5
6
7
8
9
10
      SHcompact version by Kaz Kojima and Paul Mundt.
  
      SHmedia bits:
  
  	Copyright 2004 SuperH (UK) Ltd
  	Author: Richard Curnow
  
  	Based on the sh version, and on code from the sh64-specific parts of
  	modutils, originally written by Richard Curnow and Ben Gaster.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
      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, write to the Free Software
      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
  #include <linux/moduleloader.h>
  #include <linux/elf.h>
  #include <linux/vmalloc.h>
3108cf061   Paul Mundt   sh: Fix up __bug_...
28
  #include <linux/bug.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
  #include <linux/fs.h>
  #include <linux/string.h>
  #include <linux/kernel.h>
1f9d29493   Harvey Harrison   sh: module.c use ...
32
  #include <asm/unaligned.h>
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
33
  #include <asm/dwarf.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
38
39
40
41
42
43
44
45
46
  int apply_relocate_add(Elf32_Shdr *sechdrs,
  		   const char *strtab,
  		   unsigned int symindex,
  		   unsigned int relsec,
  		   struct module *me)
  {
  	unsigned int i;
  	Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  	Elf32_Sym *sym;
  	Elf32_Addr relocation;
  	uint32_t *location;
  	uint32_t value;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47

1cb80fcfe   Paul Mundt   sh: Merge sh and ...
48
49
50
  	pr_debug("Applying relocate section %u to %u
  ", relsec,
  		 sechdrs[relsec].sh_info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
55
56
57
58
59
  	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  		/* This is where to make the change */
  		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  			+ rel[i].r_offset;
  		/* This is the symbol it is referring to.  Note that all
  		   undefined symbols have been resolved.  */
  		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  			+ ELF32_R_SYM(rel[i].r_info);
  		relocation = sym->st_value + rel[i].r_addend;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60

1cb80fcfe   Paul Mundt   sh: Merge sh and ...
61
62
63
64
65
66
  #ifdef CONFIG_SUPERH64
  		/* For text addresses, bit2 of the st_other field indicates
  		 * whether the symbol is SHmedia (1) or SHcompact (0).  If
  		 * SHmedia, the LSB of the symbol needs to be asserted
  		 * for the CPU to be in SHmedia mode when it starts executing
  		 * the branch target. */
7cd0378ef   Paul Mundt   sh: Fix up SHmedi...
67
  		relocation |= !!(sym->st_other & 4);
1cb80fcfe   Paul Mundt   sh: Merge sh and ...
68
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
  		switch (ELF32_R_TYPE(rel[i].r_info)) {
78207ffd0   Paul Mundt   sh: Ignore R_SH_N...
70
71
  		case R_SH_NONE:
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
  		case R_SH_DIR32:
1f9d29493   Harvey Harrison   sh: module.c use ...
73
  			value = get_unaligned(location);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
  			value += relocation;
1f9d29493   Harvey Harrison   sh: module.c use ...
75
  			put_unaligned(value, location);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
  			break;
  		case R_SH_REL32:
1cb80fcfe   Paul Mundt   sh: Merge sh and ...
78
  			relocation = (relocation - (Elf32_Addr) location);
1f9d29493   Harvey Harrison   sh: module.c use ...
79
  			value = get_unaligned(location);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
  			value += relocation;
1f9d29493   Harvey Harrison   sh: module.c use ...
81
  			put_unaligned(value, location);
1cb80fcfe   Paul Mundt   sh: Merge sh and ...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  			break;
  		case R_SH_IMM_LOW16:
  			*location = (*location & ~0x3fffc00) |
  				((relocation & 0xffff) << 10);
  			break;
  		case R_SH_IMM_MEDLOW16:
  			*location = (*location & ~0x3fffc00) |
  				(((relocation >> 16) & 0xffff) << 10);
  			break;
  		case R_SH_IMM_LOW16_PCREL:
  			relocation -= (Elf32_Addr) location;
  			*location = (*location & ~0x3fffc00) |
  				((relocation & 0xffff) << 10);
  			break;
  		case R_SH_IMM_MEDLOW16_PCREL:
  			relocation -= (Elf32_Addr) location;
  			*location = (*location & ~0x3fffc00) |
  				(((relocation >> 16) & 0xffff) << 10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
104
105
106
107
108
109
  			break;
  		default:
  			printk(KERN_ERR "module %s: Unknown relocation: %u
  ",
  			       me->name, ELF32_R_TYPE(rel[i].r_info));
  			return -ENOEXEC;
  		}
  	}
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
  int module_finalize(const Elf_Ehdr *hdr,
  		    const Elf_Shdr *sechdrs,
  		    struct module *me)
  {
5a3abba77   Paul Mundt   sh: Tidy up the d...
114
  	int ret = 0;
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
115

5a3abba77   Paul Mundt   sh: Tidy up the d...
116
  	ret |= module_dwarf_finalize(hdr, sechdrs, me);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
117

5a3abba77   Paul Mundt   sh: Tidy up the d...
118
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
  }
  
  void module_arch_cleanup(struct module *mod)
  {
5a3abba77   Paul Mundt   sh: Tidy up the d...
123
  	module_dwarf_cleanup(mod);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  }