| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 14 | void *module_alloc(unsigned long size) | 
|  | 15 | { | 
|  | 16 | if (size == 0) | 
|  | 17 | return NULL; | 
|  | 18 | return vmalloc(size); | 
|  | 19 | } | 
|  | 20 |  | 
|  | 21 |  | 
|  | 22 | /* Free memory returned from module_alloc */ | 
|  | 23 | void module_free(struct module *mod, void *module_region) | 
|  | 24 | { | 
|  | 25 | vfree(module_region); | 
|  | 26 | /* FIXME: If module_region == mod->init_region, trim exception | 
|  | 27 | table entries. */ | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | /* We don't need anything special. */ | 
|  | 31 | int module_frob_arch_sections(Elf_Ehdr *hdr, | 
|  | 32 | Elf_Shdr *sechdrs, | 
|  | 33 | char *secstrings, | 
|  | 34 | struct module *mod) | 
|  | 35 | { | 
|  | 36 | return 0; | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | int apply_relocate(Elf32_Shdr *sechdrs, | 
|  | 40 | const char *strtab, | 
|  | 41 | unsigned int symindex, | 
|  | 42 | unsigned int relsec, | 
|  | 43 | struct module *me) | 
|  | 44 | { | 
|  | 45 | printk(KERN_ERR "module %s: RELOCATION unsupported\n", | 
|  | 46 | me->name); | 
|  | 47 | return -ENOEXEC; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | int apply_relocate_add(Elf32_Shdr *sechdrs, | 
|  | 51 | const char *strtab, | 
|  | 52 | unsigned int symindex, | 
|  | 53 | unsigned int relsec, | 
|  | 54 | struct module *me) | 
|  | 55 | { | 
|  | 56 | unsigned int i; | 
|  | 57 | Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr; | 
|  | 58 |  | 
|  | 59 | DEBUGP("Applying relocate section %u to %u\n", relsec, | 
|  | 60 | sechdrs[relsec].sh_info); | 
|  | 61 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { | 
|  | 62 | /* This is where to make the change */ | 
|  | 63 | uint32_t *loc = (uint32_t *)(sechdrs[sechdrs[relsec].sh_info].sh_addr | 
|  | 64 | + rela[i].r_offset); | 
|  | 65 | /* This is the symbol it is referring to.  Note that all | 
|  | 66 | undefined symbols have been resolved.  */ | 
|  | 67 | Elf32_Sym *sym = (Elf32_Sym *)sechdrs[symindex].sh_addr | 
|  | 68 | + ELF32_R_SYM(rela[i].r_info); | 
|  | 69 | uint32_t v = sym->st_value + rela[i].r_addend; | 
|  | 70 |  | 
|  | 71 | switch (ELF32_R_TYPE(rela[i].r_info)) { | 
|  | 72 | case R_H8_DIR24R8: | 
|  | 73 | loc = (uint32_t *)((uint32_t)loc - 1); | 
|  | 74 | *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v); | 
|  | 75 | break; | 
|  | 76 | case R_H8_DIR24A8: | 
|  | 77 | if (ELF32_R_SYM(rela[i].r_info)) | 
|  | 78 | *loc += v; | 
|  | 79 | break; | 
|  | 80 | case R_H8_DIR32: | 
|  | 81 | case R_H8_DIR32A16: | 
|  | 82 | *loc += v; | 
|  | 83 | break; | 
|  | 84 | case R_H8_PCREL16: | 
|  | 85 | v -= (unsigned long)loc + 2; | 
|  | 86 | if ((Elf32_Sword)v > 0x7fff || | 
|  | 87 | (Elf32_Sword)v < -(Elf32_Sword)0x8000) | 
|  | 88 | goto overflow; | 
|  | 89 | else | 
|  | 90 | *(unsigned short *)loc = v; | 
|  | 91 | break; | 
|  | 92 | case R_H8_PCREL8: | 
|  | 93 | v -= (unsigned long)loc + 1; | 
|  | 94 | if ((Elf32_Sword)v > 0x7f || | 
|  | 95 | (Elf32_Sword)v < -(Elf32_Sword)0x80) | 
|  | 96 | goto overflow; | 
|  | 97 | else | 
|  | 98 | *(unsigned char *)loc = v; | 
|  | 99 | break; | 
|  | 100 | default: | 
|  | 101 | printk(KERN_ERR "module %s: Unknown relocation: %u\n", | 
|  | 102 | me->name, ELF32_R_TYPE(rela[i].r_info)); | 
|  | 103 | return -ENOEXEC; | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 | return 0; | 
|  | 107 | overflow: | 
|  | 108 | printk(KERN_ERR "module %s: relocation offset overflow: %08x\n", | 
|  | 109 | me->name, rela[i].r_offset); | 
|  | 110 | return -ENOEXEC; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | int module_finalize(const Elf_Ehdr *hdr, | 
|  | 114 | const Elf_Shdr *sechdrs, | 
|  | 115 | struct module *me) | 
|  | 116 | { | 
|  | 117 | return 0; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | void module_arch_cleanup(struct module *mod) | 
|  | 121 | { | 
|  | 122 | } |