module.c 7.9 KB

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