Linux Audio

Check our new training course

Loading...
v4.6
 1#include <linux/moduleloader.h>
 2#include <linux/elf.h>
 3#include <linux/vmalloc.h>
 4#include <linux/fs.h>
 5#include <linux/string.h>
 6#include <linux/kernel.h>
 7
 
 
 
 
 
 
 8int apply_relocate_add(Elf32_Shdr *sechdrs,
 9		       const char *strtab,
10		       unsigned int symindex,
11		       unsigned int relsec,
12		       struct module *me)
13{
14	unsigned int i;
15	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
16
17	pr_debug("Applying relocate section %u to %u\n", relsec,
18	       sechdrs[relsec].sh_info);
19	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
20		/* This is where to make the change */
21		uint32_t *loc =
22			(uint32_t *)(sechdrs[sechdrs[relsec].sh_info].sh_addr
23					     + rela[i].r_offset);
24		/* This is the symbol it is referring to.  Note that all
25		   undefined symbols have been resolved.  */
26		Elf32_Sym *sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
27			+ ELF32_R_SYM(rela[i].r_info);
28		uint32_t v = sym->st_value + rela[i].r_addend;
29
30		switch (ELF32_R_TYPE(rela[i].r_info)) {
31		case R_H8_DIR24R8:
32			loc = (uint32_t *)((uint32_t)loc - 1);
33			*loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
34			break;
35		case R_H8_DIR24A8:
36			if (ELF32_R_SYM(rela[i].r_info))
37				*loc += v;
38			break;
39		case R_H8_DIR32:
40		case R_H8_DIR32A16:
41			*loc += v;
42			break;
43		case R_H8_PCREL16:
44			v -= (unsigned long)loc + 2;
45			if ((Elf32_Sword)v > 0x7fff ||
46			    (Elf32_Sword)v < -(Elf32_Sword)0x8000)
47				goto overflow;
48			else
49				*(unsigned short *)loc = v;
50			break;
51		case R_H8_PCREL8:
52			v -= (unsigned long)loc + 1;
53			if ((Elf32_Sword)v > 0x7f ||
54			    (Elf32_Sword)v < -(Elf32_Sword)0x80)
55				goto overflow;
56			else
57				*(unsigned char *)loc = v;
58			break;
59		default:
60			pr_err("module %s: Unknown relocation: %u\n",
61			       me->name, ELF32_R_TYPE(rela[i].r_info));
62			return -ENOEXEC;
63		}
64	}
65	return 0;
66 overflow:
67	pr_err("module %s: relocation offset overflow: %08x\n",
68	       me->name, rela[i].r_offset);
69	return -ENOEXEC;
70}
v3.5.6
 1#include <linux/moduleloader.h>
 2#include <linux/elf.h>
 3#include <linux/vmalloc.h>
 4#include <linux/fs.h>
 5#include <linux/string.h>
 6#include <linux/kernel.h>
 7
 8#if 0
 9#define DEBUGP printk
10#else
11#define DEBUGP(fmt...)
12#endif
13
14int apply_relocate_add(Elf32_Shdr *sechdrs,
15		       const char *strtab,
16		       unsigned int symindex,
17		       unsigned int relsec,
18		       struct module *me)
19{
20	unsigned int i;
21	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
22
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
 
28					     + rela[i].r_offset);
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;
34
35		switch (ELF32_R_TYPE(rela[i].r_info)) {
36		case R_H8_DIR24R8:
37			loc = (uint32_t *)((uint32_t)loc - 1);
38			*loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
39			break;
40		case R_H8_DIR24A8:
41			if (ELF32_R_SYM(rela[i].r_info))
42				*loc += v;
43			break;
44		case R_H8_DIR32:
45		case R_H8_DIR32A16:
46			*loc += v;
47			break;
48		case R_H8_PCREL16:
49			v -= (unsigned long)loc + 2;
50			if ((Elf32_Sword)v > 0x7fff || 
51			    (Elf32_Sword)v < -(Elf32_Sword)0x8000)
52				goto overflow;
53			else 
54				*(unsigned short *)loc = v;
55			break;
56		case R_H8_PCREL8:
57			v -= (unsigned long)loc + 1;
58			if ((Elf32_Sword)v > 0x7f || 
59			    (Elf32_Sword)v < -(Elf32_Sword)0x80)
60				goto overflow;
61			else 
62				*(unsigned char *)loc = v;
63			break;
64		default:
65			printk(KERN_ERR "module %s: Unknown relocation: %u\n",
66			       me->name, ELF32_R_TYPE(rela[i].r_info));
67			return -ENOEXEC;
68		}
69	}
70	return 0;
71 overflow:
72	printk(KERN_ERR "module %s: relocation offset overflow: %08x\n",
73	       me->name, rela[i].r_offset);
74	return -ENOEXEC;
75}