module.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * linux/arch/arm/kernel/module.c
  3. *
  4. * Copyright (C) 2002 Russell King.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Module allocation method suggested by Andi Kleen.
  11. */
  12. #include <linux/config.h>
  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. void *module_alloc(unsigned long size)
  34. {
  35. struct vm_struct *area;
  36. size = PAGE_ALIGN(size);
  37. if (!size)
  38. return NULL;
  39. area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
  40. if (!area)
  41. return NULL;
  42. return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
  43. }
  44. void module_free(struct module *module, void *region)
  45. {
  46. vfree(region);
  47. }
  48. int module_frob_arch_sections(Elf_Ehdr *hdr,
  49. Elf_Shdr *sechdrs,
  50. char *secstrings,
  51. struct module *mod)
  52. {
  53. return 0;
  54. }
  55. int
  56. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  57. unsigned int relindex, struct module *module)
  58. {
  59. Elf32_Shdr *symsec = sechdrs + symindex;
  60. Elf32_Shdr *relsec = sechdrs + relindex;
  61. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  62. Elf32_Rel *rel = (void *)relsec->sh_addr;
  63. unsigned int i;
  64. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  65. unsigned long loc;
  66. Elf32_Sym *sym;
  67. s32 offset;
  68. offset = ELF32_R_SYM(rel->r_info);
  69. if (offset < 0 || offset > (symsec->sh_size / sizeof(Elf32_Sym))) {
  70. printk(KERN_ERR "%s: bad relocation, section %d reloc %d\n",
  71. module->name, relindex, i);
  72. return -ENOEXEC;
  73. }
  74. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  75. if (rel->r_offset < 0 || rel->r_offset > dstsec->sh_size - sizeof(u32)) {
  76. printk(KERN_ERR "%s: out of bounds relocation, "
  77. "section %d reloc %d offset %d size %d\n",
  78. module->name, relindex, i, rel->r_offset,
  79. dstsec->sh_size);
  80. return -ENOEXEC;
  81. }
  82. loc = dstsec->sh_addr + rel->r_offset;
  83. switch (ELF32_R_TYPE(rel->r_info)) {
  84. case R_ARM_ABS32:
  85. *(u32 *)loc += sym->st_value;
  86. break;
  87. case R_ARM_PC24:
  88. case R_ARM_CALL:
  89. case R_ARM_JUMP24:
  90. offset = (*(u32 *)loc & 0x00ffffff) << 2;
  91. if (offset & 0x02000000)
  92. offset -= 0x04000000;
  93. offset += sym->st_value - loc;
  94. if (offset & 3 ||
  95. offset <= (s32)0xfc000000 ||
  96. offset >= (s32)0x04000000) {
  97. printk(KERN_ERR
  98. "%s: relocation out of range, section "
  99. "%d reloc %d sym '%s'\n", module->name,
  100. relindex, i, strtab + sym->st_name);
  101. return -ENOEXEC;
  102. }
  103. offset >>= 2;
  104. *(u32 *)loc &= 0xff000000;
  105. *(u32 *)loc |= offset & 0x00ffffff;
  106. break;
  107. default:
  108. printk(KERN_ERR "%s: unknown relocation: %u\n",
  109. module->name, ELF32_R_TYPE(rel->r_info));
  110. return -ENOEXEC;
  111. }
  112. }
  113. return 0;
  114. }
  115. int
  116. apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  117. unsigned int symindex, unsigned int relsec, struct module *module)
  118. {
  119. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  120. module->name);
  121. return -ENOEXEC;
  122. }
  123. int
  124. module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  125. struct module *module)
  126. {
  127. return 0;
  128. }
  129. void
  130. module_arch_cleanup(struct module *mod)
  131. {
  132. }