1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
4 * Copyright (C) 2007-2009 PetaLogix
7 #include <linux/export.h>
8 #include <linux/moduleloader.h>
9 #include <linux/kernel.h>
10 #include <linux/elf.h>
11 #include <linux/vmalloc.h>
13 #include <linux/string.h>
14 #include <linux/pgtable.h>
16 #include <asm/cacheflush.h>
18 int apply_relocate_add(Elf32_Shdr
*sechdrs
, const char *strtab
,
19 unsigned int symindex
, unsigned int relsec
, struct module
*module
)
23 Elf32_Rela
*rela
= (void *)sechdrs
[relsec
].sh_addr
;
25 unsigned long int *location
;
26 unsigned long int value
;
28 unsigned long int old_value
;
31 pr_debug("Applying add relocation section %u to %u\n",
32 relsec
, sechdrs
[relsec
].sh_info
);
34 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rela
); i
++) {
36 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
+
38 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
+
39 ELF32_R_SYM(rela
[i
].r_info
);
40 value
= sym
->st_value
+ rela
[i
].r_addend
;
42 switch (ELF32_R_TYPE(rela
[i
].r_info
)) {
45 * Be careful! mb-gcc / mb-ld splits the relocs between the
46 * text and the reloc table. In general this means we must
47 * read the current contents of (*location), add any offset
48 * then store the result back in
53 old_value
= *location
;
54 *location
= value
+ old_value
;
56 pr_debug("R_MICROBLAZE_32 (%08lx->%08lx)\n",
65 /* Split relocs only required/used pre gcc4.1.1 */
66 old_value
= ((location
[0] & 0x0000FFFF) << 16) |
67 (location
[1] & 0x0000FFFF);
70 location
[0] = (location
[0] & 0xFFFF0000) |
72 location
[1] = (location
[1] & 0xFFFF0000) |
75 pr_debug("R_MICROBLAZE_64 (%08lx->%08lx)\n",
80 case R_MICROBLAZE_64_PCREL
:
82 old_value
= (location
[0] & 0xFFFF) << 16 |
83 (location
[1] & 0xFFFF);
86 value
-= (unsigned long int)(location
) + 4;
87 location
[0] = (location
[0] & 0xFFFF0000) |
89 location
[1] = (location
[1] & 0xFFFF0000) |
91 pr_debug("R_MICROBLAZE_64_PCREL (%08lx)\n",
95 case R_MICROBLAZE_32_PCREL_LO
:
96 pr_debug("R_MICROBLAZE_32_PCREL_LO\n");
99 case R_MICROBLAZE_64_NONE
:
100 pr_debug("R_MICROBLAZE_64_NONE\n");
103 case R_MICROBLAZE_NONE
:
104 pr_debug("R_MICROBLAZE_NONE\n");
108 pr_err("module %s: Unknown relocation: %u\n",
110 ELF32_R_TYPE(rela
[i
].r_info
));
117 int module_finalize(const Elf32_Ehdr
*hdr
, const Elf_Shdr
*sechdrs
,
118 struct module
*module
)