module.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * linux/arch/arm/kernel/module.c
  3. *
  4. * Copyright (C) 2002 Russell King.
  5. * Modified for nommu by Hyok S. Choi
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Module allocation method suggested by Andi Kleen.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleloader.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/elf.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/slab.h>
  20. #include <linux/fs.h>
  21. #include <linux/string.h>
  22. #include <asm/pgtable.h>
  23. #ifdef CONFIG_XIP_KERNEL
  24. /*
  25. * The XIP kernel text is mapped in the module area for modules and
  26. * some other stuff to work without any indirect relocations.
  27. * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
  28. * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
  29. */
  30. extern void _etext;
  31. #undef MODULES_VADDR
  32. #define MODULES_VADDR (((unsigned long)&_etext + ~PGDIR_MASK) & PGDIR_MASK)
  33. #endif
  34. #ifdef CONFIG_MMU
  35. void *module_alloc(unsigned long size)
  36. {
  37. struct vm_struct *area;
  38. size = PAGE_ALIGN(size);
  39. if (!size)
  40. return NULL;
  41. area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
  42. if (!area)
  43. return NULL;
  44. return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
  45. }
  46. #else /* CONFIG_MMU */
  47. void *module_alloc(unsigned long size)
  48. {
  49. return size == 0 ? NULL : vmalloc(size);
  50. }
  51. #endif /* !CONFIG_MMU */
  52. void module_free(struct module *module, void *region)
  53. {
  54. vfree(region);
  55. }
  56. int module_frob_arch_sections(Elf_Ehdr *hdr,
  57. Elf_Shdr *sechdrs,
  58. char *secstrings,
  59. struct module *mod)
  60. {
  61. return 0;
  62. }
  63. int
  64. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  65. unsigned int relindex, struct module *module)
  66. {
  67. Elf32_Shdr *symsec = sechdrs + symindex;
  68. Elf32_Shdr *relsec = sechdrs + relindex;
  69. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  70. Elf32_Rel *rel = (void *)relsec->sh_addr;
  71. unsigned int i;
  72. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  73. unsigned long loc;
  74. Elf32_Sym *sym;
  75. s32 offset;
  76. offset = ELF32_R_SYM(rel->r_info);
  77. if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
  78. printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
  79. module->name, relindex, i);
  80. return -ENOEXEC;
  81. }
  82. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  83. if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
  84. printk(KERN_ERR "%s: out of bounds relocation, "
  85. "section %d reloc %d offset %d size %d\n",
  86. module->name, relindex, i, rel->r_offset,
  87. dstsec->sh_size);
  88. return -ENOEXEC;
  89. }
  90. loc = dstsec->sh_addr + rel->r_offset;
  91. switch (ELF32_R_TYPE(rel->r_info)) {
  92. case R_ARM_ABS32:
  93. *(u32 *)loc += sym->st_value;
  94. break;
  95. case R_ARM_PC24:
  96. case R_ARM_CALL:
  97. case R_ARM_JUMP24:
  98. offset = (*(u32 *)loc & 0x00ffffff) << 2;
  99. if (offset & 0x02000000)
  100. offset -= 0x04000000;
  101. offset += sym->st_value - loc;
  102. if (offset & 3 ||
  103. offset <= (s32)0xfe000000 ||
  104. offset >= (s32)0x02000000) {
  105. printk(KERN_ERR
  106. "%s: relocation out of range, section "
  107. "%d reloc %d sym '%s'\n", module->name,
  108. relindex, i, strtab + sym->st_name);
  109. return -ENOEXEC;
  110. }
  111. offset >>= 2;
  112. *(u32 *)loc &= 0xff000000;
  113. *(u32 *)loc |= offset & 0x00ffffff;
  114. break;
  115. default:
  116. printk(KERN_ERR "%s: unknown relocation: %u\n",
  117. module->name, ELF32_R_TYPE(rel->r_info));
  118. return -ENOEXEC;
  119. }
  120. }
  121. return 0;
  122. }
  123. int
  124. apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  125. unsigned int symindex, unsigned int relsec, struct module *module)
  126. {
  127. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  128. module->name);
  129. return -ENOEXEC;
  130. }
  131. int
  132. module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  133. struct module *module)
  134. {
  135. return 0;
  136. }
  137. void
  138. module_arch_cleanup(struct module *mod)
  139. {
  140. }