module.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * linux/arch/unicore32/kernel/module.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleloader.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/elf.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/gfp.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/sections.h>
  23. void *module_alloc(unsigned long size)
  24. {
  25. struct vm_struct *area;
  26. size = PAGE_ALIGN(size);
  27. if (!size)
  28. return NULL;
  29. area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
  30. if (!area)
  31. return NULL;
  32. return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
  33. }
  34. void module_free(struct module *module, void *region)
  35. {
  36. vfree(region);
  37. }
  38. int module_frob_arch_sections(Elf_Ehdr *hdr,
  39. Elf_Shdr *sechdrs,
  40. char *secstrings,
  41. struct module *mod)
  42. {
  43. return 0;
  44. }
  45. int
  46. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  47. unsigned int relindex, struct module *module)
  48. {
  49. Elf32_Shdr *symsec = sechdrs + symindex;
  50. Elf32_Shdr *relsec = sechdrs + relindex;
  51. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  52. Elf32_Rel *rel = (void *)relsec->sh_addr;
  53. unsigned int i;
  54. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  55. unsigned long loc;
  56. Elf32_Sym *sym;
  57. s32 offset;
  58. offset = ELF32_R_SYM(rel->r_info);
  59. if (offset < 0 || offset >
  60. (symsec->sh_size / sizeof(Elf32_Sym))) {
  61. printk(KERN_ERR "%s: bad relocation, "
  62. "section %d reloc %d\n",
  63. module->name, relindex, i);
  64. return -ENOEXEC;
  65. }
  66. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  67. if (rel->r_offset < 0 || rel->r_offset >
  68. dstsec->sh_size - sizeof(u32)) {
  69. printk(KERN_ERR "%s: out of bounds relocation, "
  70. "section %d reloc %d offset %d size %d\n",
  71. module->name, relindex, i, rel->r_offset,
  72. dstsec->sh_size);
  73. return -ENOEXEC;
  74. }
  75. loc = dstsec->sh_addr + rel->r_offset;
  76. switch (ELF32_R_TYPE(rel->r_info)) {
  77. case R_UNICORE_NONE:
  78. /* ignore */
  79. break;
  80. case R_UNICORE_ABS32:
  81. *(u32 *)loc += sym->st_value;
  82. break;
  83. case R_UNICORE_PC24:
  84. case R_UNICORE_CALL:
  85. case R_UNICORE_JUMP24:
  86. offset = (*(u32 *)loc & 0x00ffffff) << 2;
  87. if (offset & 0x02000000)
  88. offset -= 0x04000000;
  89. offset += sym->st_value - loc;
  90. if (offset & 3 ||
  91. offset <= (s32)0xfe000000 ||
  92. offset >= (s32)0x02000000) {
  93. printk(KERN_ERR
  94. "%s: relocation out of range, section "
  95. "%d reloc %d sym '%s'\n", module->name,
  96. relindex, i, strtab + sym->st_name);
  97. return -ENOEXEC;
  98. }
  99. offset >>= 2;
  100. *(u32 *)loc &= 0xff000000;
  101. *(u32 *)loc |= offset & 0x00ffffff;
  102. break;
  103. default:
  104. printk(KERN_ERR "%s: unknown relocation: %u\n",
  105. module->name, ELF32_R_TYPE(rel->r_info));
  106. return -ENOEXEC;
  107. }
  108. }
  109. return 0;
  110. }
  111. int
  112. apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  113. unsigned int symindex, unsigned int relsec,
  114. struct module *module)
  115. {
  116. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  117. module->name);
  118. return -ENOEXEC;
  119. }
  120. int
  121. module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  122. struct module *module)
  123. {
  124. return 0;
  125. }
  126. void
  127. module_arch_cleanup(struct module *mod)
  128. {
  129. }