module.c 8.6 KB

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