2 * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2007-2009 PetaLogix
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/moduleloader.h>
12 #include <linux/kernel.h>
13 #include <linux/elf.h>
14 #include <linux/vmalloc.h>
15 #include <linux/slab.h>
17 #include <linux/string.h>
19 #include <asm/pgtable.h>
21 void *module_alloc(unsigned long size
)
24 ret
= (size
== 0) ? NULL
: vmalloc(size
);
25 pr_debug("module_alloc (%08lx@%08lx)\n", size
, (unsigned long int)ret
);
29 void module_free(struct module
*module
, void *region
)
31 pr_debug("module_free(%s,%08lx)\n", module
->name
,
32 (unsigned long)region
);
36 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
44 int apply_relocate(Elf32_Shdr
*sechdrs
, const char *strtab
,
45 unsigned int symindex
, unsigned int relsec
, struct module
*module
)
47 printk(KERN_ERR
"module %s: ADD RELOCATION unsupported\n",
52 int apply_relocate_add(Elf32_Shdr
*sechdrs
, const char *strtab
,
53 unsigned int symindex
, unsigned int relsec
, struct module
*module
)
57 Elf32_Rela
*rela
= (void *)sechdrs
[relsec
].sh_addr
;
59 unsigned long int *location
;
60 unsigned long int locoffs
;
61 unsigned long int value
;
63 unsigned long int old_value
;
66 pr_debug("Applying add relocation section %u to %u\n",
67 relsec
, sechdrs
[relsec
].sh_info
);
69 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rela
); i
++) {
71 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
+
73 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
+
74 ELF32_R_SYM(rela
[i
].r_info
);
75 value
= sym
->st_value
+ rela
[i
].r_addend
;
77 switch (ELF32_R_TYPE(rela
[i
].r_info
)) {
80 * Be careful! mb-gcc / mb-ld splits the relocs between the
81 * text and the reloc table. In general this means we must
82 * read the current contents of (*location), add any offset
83 * then store the result back in
88 old_value
= *location
;
89 *location
= value
+ old_value
;
91 pr_debug("R_MICROBLAZE_32 (%08lx->%08lx)\n",
100 /* Split relocs only required/used pre gcc4.1.1 */
101 old_value
= ((location
[0] & 0x0000FFFF) << 16) |
102 (location
[1] & 0x0000FFFF);
105 location
[0] = (location
[0] & 0xFFFF0000) |
107 location
[1] = (location
[1] & 0xFFFF0000) |
110 pr_debug("R_MICROBLAZE_64 (%08lx->%08lx)\n",
115 case R_MICROBLAZE_64_PCREL
:
116 locoffs
= (location
[0] & 0xFFFF) << 16 |
117 (location
[1] & 0xFFFF);
118 value
-= (unsigned long int)(location
) + 4 +
120 location
[0] = (location
[0] & 0xFFFF0000) |
122 location
[1] = (location
[1] & 0xFFFF0000) |
124 pr_debug("R_MICROBLAZE_64_PCREL (%08lx)\n",
128 case R_MICROBLAZE_NONE
:
129 pr_debug("R_MICROBLAZE_NONE\n");
133 printk(KERN_ERR
"module %s: "
134 "Unknown relocation: %u\n",
136 ELF32_R_TYPE(rela
->r_info
));
143 int module_finalize(const Elf32_Ehdr
*hdr
, const Elf_Shdr
*sechdrs
,
144 struct module
*module
)
149 void module_arch_cleanup(struct module
*mod
)