1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f6b165c6SMichal Simek /*
3f6b165c6SMichal Simek * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
4f6b165c6SMichal Simek * Copyright (C) 2007-2009 PetaLogix
5f6b165c6SMichal Simek */
6f6b165c6SMichal Simek
7d64af918SMichal Simek #include <linux/export.h>
8f6b165c6SMichal Simek #include <linux/moduleloader.h>
9f6b165c6SMichal Simek #include <linux/kernel.h>
10f6b165c6SMichal Simek #include <linux/elf.h>
11f6b165c6SMichal Simek #include <linux/vmalloc.h>
12f6b165c6SMichal Simek #include <linux/fs.h>
13f6b165c6SMichal Simek #include <linux/string.h>
14ca5999fdSMike Rapoport #include <linux/pgtable.h>
15*65fddcfcSMike Rapoport
16a5e48b88SMichal Simek #include <asm/cacheflush.h>
17f6b165c6SMichal Simek
apply_relocate_add(Elf32_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * module)18f6b165c6SMichal Simek int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
19f6b165c6SMichal Simek unsigned int symindex, unsigned int relsec, struct module *module)
20f6b165c6SMichal Simek {
21f6b165c6SMichal Simek
22f6b165c6SMichal Simek unsigned int i;
23f6b165c6SMichal Simek Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
24f6b165c6SMichal Simek Elf32_Sym *sym;
25f6b165c6SMichal Simek unsigned long int *location;
26f6b165c6SMichal Simek unsigned long int value;
27f6b165c6SMichal Simek
28f6b165c6SMichal Simek pr_debug("Applying add relocation section %u to %u\n",
29f6b165c6SMichal Simek relsec, sechdrs[relsec].sh_info);
30f6b165c6SMichal Simek
31f6b165c6SMichal Simek for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
32f6b165c6SMichal Simek
33f6b165c6SMichal Simek location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr +
34f6b165c6SMichal Simek rela[i].r_offset;
35f6b165c6SMichal Simek sym = (Elf32_Sym *)sechdrs[symindex].sh_addr +
36f6b165c6SMichal Simek ELF32_R_SYM(rela[i].r_info);
37f6b165c6SMichal Simek value = sym->st_value + rela[i].r_addend;
38f6b165c6SMichal Simek
39f6b165c6SMichal Simek switch (ELF32_R_TYPE(rela[i].r_info)) {
40f6b165c6SMichal Simek
41f6b165c6SMichal Simek /*
42f6b165c6SMichal Simek * Be careful! mb-gcc / mb-ld splits the relocs between the
43f6b165c6SMichal Simek * text and the reloc table. In general this means we must
44f6b165c6SMichal Simek * read the current contents of (*location), add any offset
45f6b165c6SMichal Simek * then store the result back in
46f6b165c6SMichal Simek */
47f6b165c6SMichal Simek
48f6b165c6SMichal Simek case R_MICROBLAZE_32:
49f6b165c6SMichal Simek *location = value;
50f6b165c6SMichal Simek break;
51f6b165c6SMichal Simek
52f6b165c6SMichal Simek case R_MICROBLAZE_64:
53f6b165c6SMichal Simek location[0] = (location[0] & 0xFFFF0000) |
54f6b165c6SMichal Simek (value >> 16);
55f6b165c6SMichal Simek location[1] = (location[1] & 0xFFFF0000) |
56f6b165c6SMichal Simek (value & 0xFFFF);
57f6b165c6SMichal Simek break;
58f6b165c6SMichal Simek
59f6b165c6SMichal Simek case R_MICROBLAZE_64_PCREL:
60fadf2e60SJohn Williams value -= (unsigned long int)(location) + 4;
61f6b165c6SMichal Simek location[0] = (location[0] & 0xFFFF0000) |
62f6b165c6SMichal Simek (value >> 16);
63f6b165c6SMichal Simek location[1] = (location[1] & 0xFFFF0000) |
64f6b165c6SMichal Simek (value & 0xFFFF);
65f6b165c6SMichal Simek pr_debug("R_MICROBLAZE_64_PCREL (%08lx)\n",
66f6b165c6SMichal Simek value);
67f6b165c6SMichal Simek break;
68f6b165c6SMichal Simek
69fadf2e60SJohn Williams case R_MICROBLAZE_32_PCREL_LO:
70fadf2e60SJohn Williams pr_debug("R_MICROBLAZE_32_PCREL_LO\n");
71fadf2e60SJohn Williams break;
72fadf2e60SJohn Williams
73679711b8SMichal Simek case R_MICROBLAZE_64_NONE:
74f5b778beSMichal Simek pr_debug("R_MICROBLAZE_64_NONE\n");
75679711b8SMichal Simek break;
76679711b8SMichal Simek
77f6b165c6SMichal Simek case R_MICROBLAZE_NONE:
78f6b165c6SMichal Simek pr_debug("R_MICROBLAZE_NONE\n");
79f6b165c6SMichal Simek break;
80f6b165c6SMichal Simek
81f6b165c6SMichal Simek default:
826bd55f0bSMichal Simek pr_err("module %s: Unknown relocation: %u\n",
83f6b165c6SMichal Simek module->name,
84fadf2e60SJohn Williams ELF32_R_TYPE(rela[i].r_info));
85f6b165c6SMichal Simek return -ENOEXEC;
86f6b165c6SMichal Simek }
87f6b165c6SMichal Simek }
88f6b165c6SMichal Simek return 0;
89f6b165c6SMichal Simek }
90f6b165c6SMichal Simek
module_finalize(const Elf32_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * module)91f6b165c6SMichal Simek int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
92f6b165c6SMichal Simek struct module *module)
93f6b165c6SMichal Simek {
94a5e48b88SMichal Simek flush_dcache();
95f6b165c6SMichal Simek return 0;
96f6b165c6SMichal Simek }
97