module.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * AVR32-specific kernel module loader
  3. *
  4. * Copyright (C) 2005-2006 Atmel Corporation
  5. *
  6. * GOT initialization parts are based on the s390 version
  7. * Copyright (C) 2002, 2003 IBM Deutschland Entwicklung GmbH,
  8. * IBM Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/moduleloader.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/elf.h>
  18. #include <linux/vmalloc.h>
  19. void *module_alloc(unsigned long size)
  20. {
  21. if (size == 0)
  22. return NULL;
  23. return vmalloc(size);
  24. }
  25. void module_free(struct module *mod, void *module_region)
  26. {
  27. vfree(mod->arch.syminfo);
  28. mod->arch.syminfo = NULL;
  29. vfree(module_region);
  30. /* FIXME: if module_region == mod->init_region, trim exception
  31. * table entries. */
  32. }
  33. static inline int check_rela(Elf32_Rela *rela, struct module *module,
  34. char *strings, Elf32_Sym *symbols)
  35. {
  36. struct mod_arch_syminfo *info;
  37. info = module->arch.syminfo + ELF32_R_SYM(rela->r_info);
  38. switch (ELF32_R_TYPE(rela->r_info)) {
  39. case R_AVR32_GOT32:
  40. case R_AVR32_GOT16:
  41. case R_AVR32_GOT8:
  42. case R_AVR32_GOT21S:
  43. case R_AVR32_GOT18SW: /* mcall */
  44. case R_AVR32_GOT16S: /* ld.w */
  45. if (rela->r_addend != 0) {
  46. printk(KERN_ERR
  47. "GOT relocation against %s at offset %u with addend\n",
  48. strings + symbols[ELF32_R_SYM(rela->r_info)].st_name,
  49. rela->r_offset);
  50. return -ENOEXEC;
  51. }
  52. if (info->got_offset == -1UL) {
  53. info->got_offset = module->arch.got_size;
  54. module->arch.got_size += sizeof(void *);
  55. }
  56. pr_debug("GOT[%3lu] %s\n", info->got_offset,
  57. strings + symbols[ELF32_R_SYM(rela->r_info)].st_name);
  58. break;
  59. }
  60. return 0;
  61. }
  62. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  63. char *secstrings, struct module *module)
  64. {
  65. Elf32_Shdr *symtab;
  66. Elf32_Sym *symbols;
  67. Elf32_Rela *rela;
  68. char *strings;
  69. int nrela, i, j;
  70. int ret;
  71. /* Find the symbol table */
  72. symtab = NULL;
  73. for (i = 0; i < hdr->e_shnum; i++)
  74. switch (sechdrs[i].sh_type) {
  75. case SHT_SYMTAB:
  76. symtab = &sechdrs[i];
  77. break;
  78. }
  79. if (!symtab) {
  80. printk(KERN_ERR "module %s: no symbol table\n", module->name);
  81. return -ENOEXEC;
  82. }
  83. /* Allocate room for one syminfo structure per symbol. */
  84. module->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
  85. module->arch.syminfo = vmalloc(module->arch.nsyms
  86. * sizeof(struct mod_arch_syminfo));
  87. if (!module->arch.syminfo)
  88. return -ENOMEM;
  89. symbols = (void *)hdr + symtab->sh_offset;
  90. strings = (void *)hdr + sechdrs[symtab->sh_link].sh_offset;
  91. for (i = 0; i < module->arch.nsyms; i++) {
  92. if (symbols[i].st_shndx == SHN_UNDEF &&
  93. strcmp(strings + symbols[i].st_name,
  94. "_GLOBAL_OFFSET_TABLE_") == 0)
  95. /* "Define" it as absolute. */
  96. symbols[i].st_shndx = SHN_ABS;
  97. module->arch.syminfo[i].got_offset = -1UL;
  98. module->arch.syminfo[i].got_initialized = 0;
  99. }
  100. /* Allocate GOT entries for symbols that need it. */
  101. module->arch.got_size = 0;
  102. for (i = 0; i < hdr->e_shnum; i++) {
  103. if (sechdrs[i].sh_type != SHT_RELA)
  104. continue;
  105. nrela = sechdrs[i].sh_size / sizeof(Elf32_Rela);
  106. rela = (void *)hdr + sechdrs[i].sh_offset;
  107. for (j = 0; j < nrela; j++) {
  108. ret = check_rela(rela + j, module,
  109. strings, symbols);
  110. if (ret)
  111. goto out_free_syminfo;
  112. }
  113. }
  114. /*
  115. * Increase core size to make room for GOT and set start
  116. * offset for GOT.
  117. */
  118. module->core_size = ALIGN(module->core_size, 4);
  119. module->arch.got_offset = module->core_size;
  120. module->core_size += module->arch.got_size;
  121. return 0;
  122. out_free_syminfo:
  123. vfree(module->arch.syminfo);
  124. module->arch.syminfo = NULL;
  125. return ret;
  126. }
  127. static inline int reloc_overflow(struct module *module, const char *reloc_name,
  128. Elf32_Addr relocation)
  129. {
  130. printk(KERN_ERR "module %s: Value %lx does not fit relocation %s\n",
  131. module->name, (unsigned long)relocation, reloc_name);
  132. return -ENOEXEC;
  133. }
  134. #define get_u16(loc) (*((uint16_t *)loc))
  135. #define put_u16(loc, val) (*((uint16_t *)loc) = (val))
  136. int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  137. unsigned int symindex, unsigned int relindex,
  138. struct module *module)
  139. {
  140. Elf32_Shdr *symsec = sechdrs + symindex;
  141. Elf32_Shdr *relsec = sechdrs + relindex;
  142. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  143. Elf32_Rela *rel = (void *)relsec->sh_addr;
  144. unsigned int i;
  145. int ret = 0;
  146. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rela); i++, rel++) {
  147. struct mod_arch_syminfo *info;
  148. Elf32_Sym *sym;
  149. Elf32_Addr relocation;
  150. uint32_t *location;
  151. uint32_t value;
  152. location = (void *)dstsec->sh_addr + rel->r_offset;
  153. sym = (Elf32_Sym *)symsec->sh_addr + ELF32_R_SYM(rel->r_info);
  154. relocation = sym->st_value + rel->r_addend;
  155. info = module->arch.syminfo + ELF32_R_SYM(rel->r_info);
  156. /* Initialize GOT entry if necessary */
  157. switch (ELF32_R_TYPE(rel->r_info)) {
  158. case R_AVR32_GOT32:
  159. case R_AVR32_GOT16:
  160. case R_AVR32_GOT8:
  161. case R_AVR32_GOT21S:
  162. case R_AVR32_GOT18SW:
  163. case R_AVR32_GOT16S:
  164. if (!info->got_initialized) {
  165. Elf32_Addr *gotent;
  166. gotent = (module->module_core
  167. + module->arch.got_offset
  168. + info->got_offset);
  169. *gotent = relocation;
  170. info->got_initialized = 1;
  171. }
  172. relocation = info->got_offset;
  173. break;
  174. }
  175. switch (ELF32_R_TYPE(rel->r_info)) {
  176. case R_AVR32_32:
  177. case R_AVR32_32_CPENT:
  178. *location = relocation;
  179. break;
  180. case R_AVR32_22H_PCREL:
  181. relocation -= (Elf32_Addr)location;
  182. if ((relocation & 0xffe00001) != 0
  183. && (relocation & 0xffc00001) != 0xffc00000)
  184. return reloc_overflow(module,
  185. "R_AVR32_22H_PCREL",
  186. relocation);
  187. relocation >>= 1;
  188. value = *location;
  189. value = ((value & 0xe1ef0000)
  190. | (relocation & 0xffff)
  191. | ((relocation & 0x10000) << 4)
  192. | ((relocation & 0x1e0000) << 8));
  193. *location = value;
  194. break;
  195. case R_AVR32_11H_PCREL:
  196. relocation -= (Elf32_Addr)location;
  197. if ((relocation & 0xfffffc01) != 0
  198. && (relocation & 0xfffff801) != 0xfffff800)
  199. return reloc_overflow(module,
  200. "R_AVR32_11H_PCREL",
  201. relocation);
  202. value = get_u16(location);
  203. value = ((value & 0xf00c)
  204. | ((relocation & 0x1fe) << 3)
  205. | ((relocation & 0x600) >> 9));
  206. put_u16(location, value);
  207. break;
  208. case R_AVR32_9H_PCREL:
  209. relocation -= (Elf32_Addr)location;
  210. if ((relocation & 0xffffff01) != 0
  211. && (relocation & 0xfffffe01) != 0xfffffe00)
  212. return reloc_overflow(module,
  213. "R_AVR32_9H_PCREL",
  214. relocation);
  215. value = get_u16(location);
  216. value = ((value & 0xf00f)
  217. | ((relocation & 0x1fe) << 3));
  218. put_u16(location, value);
  219. break;
  220. case R_AVR32_9UW_PCREL:
  221. relocation -= ((Elf32_Addr)location) & 0xfffffffc;
  222. if ((relocation & 0xfffffc03) != 0)
  223. return reloc_overflow(module,
  224. "R_AVR32_9UW_PCREL",
  225. relocation);
  226. value = get_u16(location);
  227. value = ((value & 0xf80f)
  228. | ((relocation & 0x1fc) << 2));
  229. put_u16(location, value);
  230. break;
  231. case R_AVR32_GOTPC:
  232. /*
  233. * R6 = PC - (PC - GOT)
  234. *
  235. * At this point, relocation contains the
  236. * value of PC. Just subtract the value of
  237. * GOT, and we're done.
  238. */
  239. pr_debug("GOTPC: PC=0x%x, got_offset=0x%lx, core=0x%p\n",
  240. relocation, module->arch.got_offset,
  241. module->module_core);
  242. relocation -= ((unsigned long)module->module_core
  243. + module->arch.got_offset);
  244. *location = relocation;
  245. break;
  246. case R_AVR32_GOT18SW:
  247. if ((relocation & 0xfffe0003) != 0
  248. && (relocation & 0xfffc0003) != 0xffff0000)
  249. return reloc_overflow(module, "R_AVR32_GOT18SW",
  250. relocation);
  251. relocation >>= 2;
  252. /* fall through */
  253. case R_AVR32_GOT16S:
  254. if ((relocation & 0xffff8000) != 0
  255. && (relocation & 0xffff0000) != 0xffff0000)
  256. return reloc_overflow(module, "R_AVR32_GOT16S",
  257. relocation);
  258. pr_debug("GOT reloc @ 0x%x -> %u\n",
  259. rel->r_offset, relocation);
  260. value = *location;
  261. value = ((value & 0xffff0000)
  262. | (relocation & 0xffff));
  263. *location = value;
  264. break;
  265. default:
  266. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  267. module->name, ELF32_R_TYPE(rel->r_info));
  268. return -ENOEXEC;
  269. }
  270. }
  271. return ret;
  272. }
  273. int apply_relocate(Elf32_Shdr *sechdrs, const char *strtab,
  274. unsigned int symindex, unsigned int relindex,
  275. struct module *module)
  276. {
  277. printk(KERN_ERR "module %s: REL relocations are not supported\n",
  278. module->name);
  279. return -ENOEXEC;
  280. }
  281. int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  282. struct module *module)
  283. {
  284. vfree(module->arch.syminfo);
  285. module->arch.syminfo = NULL;
  286. return 0;
  287. }
  288. void module_arch_cleanup(struct module *module)
  289. {
  290. }