1 #include <linux/moduleloader.h>
3 #include <linux/vmalloc.h>
5 #include <linux/string.h>
6 #include <linux/kernel.h>
11 #define DEBUGP(fmt...)
14 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
16 unsigned int symindex
,
21 Elf32_Rela
*rela
= (void *)sechdrs
[relsec
].sh_addr
;
23 DEBUGP("Applying relocate section %u to %u\n", relsec
,
24 sechdrs
[relsec
].sh_info
);
25 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rela
); i
++) {
26 /* This is where to make the change */
27 uint32_t *loc
= (uint32_t *)(sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
29 /* This is the symbol it is referring to. Note that all
30 undefined symbols have been resolved. */
31 Elf32_Sym
*sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
32 + ELF32_R_SYM(rela
[i
].r_info
);
33 uint32_t v
= sym
->st_value
+ rela
[i
].r_addend
;
35 switch (ELF32_R_TYPE(rela
[i
].r_info
)) {
37 loc
= (uint32_t *)((uint32_t)loc
- 1);
38 *loc
= (*loc
& 0xff000000) | ((*loc
& 0xffffff) + v
);
41 if (ELF32_R_SYM(rela
[i
].r_info
))
49 v
-= (unsigned long)loc
+ 2;
50 if ((Elf32_Sword
)v
> 0x7fff ||
51 (Elf32_Sword
)v
< -(Elf32_Sword
)0x8000)
54 *(unsigned short *)loc
= v
;
57 v
-= (unsigned long)loc
+ 1;
58 if ((Elf32_Sword
)v
> 0x7f ||
59 (Elf32_Sword
)v
< -(Elf32_Sword
)0x80)
62 *(unsigned char *)loc
= v
;
65 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
66 me
->name
, ELF32_R_TYPE(rela
[i
].r_info
));
72 printk(KERN_ERR
"module %s: relocation offset overflow: %08x\n",
73 me
->name
, rela
[i
].r_offset
);