module.c 8.1 KB

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