1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel module loader for Hexagon
5 * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
8 #include <asm/module.h>
10 #include <linux/module.h>
11 #include <linux/moduleloader.h>
12 #include <linux/vmalloc.h>
17 #define DEBUGP(fmt , ...)
21 * module_frob_arch_sections - tweak got/plt sections.
22 * @hdr - pointer to elf header
23 * @sechdrs - pointer to elf load section headers
24 * @secstrings - symbol names
25 * @mod - pointer to module
27 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
34 /* Look for .plt and/or .got.plt and/or .init.plt sections */
35 for (i = 0; i < hdr->e_shnum; i++) {
36 DEBUGP("Section %d is %s\n", i,
37 secstrings + sechdrs[i].sh_name);
38 if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
40 if (strcmp(secstrings + sechdrs[i].sh_name, ".got.plt") == 0)
42 if (strcmp(secstrings + sechdrs[i].sh_name, ".rela.plt") == 0)
46 /* At this time, we don't support modules comiled with -shared */
49 "Module '%s' contains unexpected .plt/.got sections.\n",
51 /* return -ENOEXEC; */
58 * apply_relocate_add - perform rela relocations.
59 * @sechdrs - pointer to section headers
60 * @strtab - some sort of start address?
61 * @symindex - symbol index offset or something?
62 * @relsec - address to relocate to?
63 * @module - pointer to module
65 * Perform rela relocations.
67 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
68 unsigned int symindex, unsigned int relsec,
69 struct module *module)
75 unsigned int nrelocs = sechdrs[relsec].sh_size / sizeof(Elf32_Rela);
76 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
77 Elf32_Word sym_info = sechdrs[relsec].sh_info;
78 Elf32_Sym *sym_base = (Elf32_Sym *) sechdrs[symindex].sh_addr;
79 void *loc_base = (void *) sechdrs[sym_info].sh_addr;
81 DEBUGP("Applying relocations in section %u to section %u base=%p\n",
82 relsec, sym_info, loc_base);
84 for (i = 0; i < nrelocs; i++) {
86 /* Symbol to relocate */
87 sym = sym_base + ELF32_R_SYM(rela[i].r_info);
89 /* Where to make the change */
90 location = loc_base + rela[i].r_offset;
92 /* `Everything is relative'. */
93 value = sym->st_value + rela[i].r_addend;
95 DEBUGP("%d: value=%08x loc=%p reloc=%d symbol=%s\n",
96 i, value, location, ELF32_R_TYPE(rela[i].r_info),
98 &strtab[sym->st_name] : "(anonymous)");
100 switch (ELF32_R_TYPE(rela[i].r_info)) {
101 case R_HEXAGON_B22_PCREL: {
102 int dist = (int)(value - (uint32_t)location);
103 if ((dist < -0x00800000) ||
104 (dist >= 0x00800000)) {
106 "%s: %s: %08x=%08x-%08x %s\n",
108 "R_HEXAGON_B22_PCREL reloc out of range",
109 dist, value, (uint32_t)location,
111 &strtab[sym->st_name] : "(anonymous)");
114 DEBUGP("B22_PCREL contents: %08X.\n", *location);
115 *location &= ~0x01ff3fff;
116 *location |= 0x00003fff & dist;
117 *location |= 0x01ff0000 & (dist<<2);
118 DEBUGP("Contents after reloc: %08x\n", *location);
122 value = (value>>16) & 0xffff;
125 *location &= ~0x00c03fff;
126 *location |= value & 0x3fff;
127 *location |= (value & 0xc000) << 8;
132 case R_HEXAGON_32_PCREL:
133 *location = value - (uint32_t)location;
135 case R_HEXAGON_PLT_B22_PCREL:
136 case R_HEXAGON_GOTOFF_LO16:
137 case R_HEXAGON_GOTOFF_HI16:
138 printk(KERN_ERR "%s: GOT/PLT relocations unsupported\n",
142 printk(KERN_ERR "%s: unknown relocation: %u\n",
144 ELF32_R_TYPE(rela[i].r_info));