1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/moduleloader.h>
4 #include <linux/vmalloc.h>
6 #include <linux/string.h>
7 #include <linux/kernel.h>
9 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
11 unsigned int symindex
,
16 Elf32_Rela
*rela
= (void *)sechdrs
[relsec
].sh_addr
;
18 pr_debug("Applying relocate section %u to %u\n", relsec
,
19 sechdrs
[relsec
].sh_info
);
20 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rela
); i
++) {
21 /* This is where to make the change */
23 (uint32_t *)(sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
25 /* This is the symbol it is referring to. Note that all
26 undefined symbols have been resolved. */
27 Elf32_Sym
*sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
28 + ELF32_R_SYM(rela
[i
].r_info
);
29 uint32_t v
= sym
->st_value
+ rela
[i
].r_addend
;
31 switch (ELF32_R_TYPE(rela
[i
].r_info
)) {
33 loc
= (uint32_t *)((uint32_t)loc
- 1);
34 *loc
= (*loc
& 0xff000000) | ((*loc
& 0xffffff) + v
);
37 if (ELF32_R_SYM(rela
[i
].r_info
))
45 v
-= (unsigned long)loc
+ 2;
46 if ((Elf32_Sword
)v
> 0x7fff ||
47 (Elf32_Sword
)v
< -(Elf32_Sword
)0x8000)
50 *(unsigned short *)loc
= v
;
53 v
-= (unsigned long)loc
+ 1;
54 if ((Elf32_Sword
)v
> 0x7f ||
55 (Elf32_Sword
)v
< -(Elf32_Sword
)0x80)
58 *(unsigned char *)loc
= v
;
61 pr_err("module %s: Unknown relocation: %u\n",
62 me
->name
, ELF32_R_TYPE(rela
[i
].r_info
));
68 pr_err("module %s: relocation offset overflow: %08x\n",
69 me
->name
, rela
[i
].r_offset
);