module.c 3.9 KB

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