module.c 3.7 KB

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