module.c 8.7 KB

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