module.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. int
  35. apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  36. unsigned int relindex, struct module *module)
  37. {
  38. Elf32_Shdr *symsec = sechdrs + symindex;
  39. Elf32_Shdr *relsec = sechdrs + relindex;
  40. Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  41. Elf32_Rel *rel = (void *)relsec->sh_addr;
  42. unsigned int i;
  43. for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  44. unsigned long loc;
  45. Elf32_Sym *sym;
  46. s32 offset;
  47. offset = ELF32_R_SYM(rel->r_info);
  48. if (offset < 0 || offset >
  49. (symsec->sh_size / sizeof(Elf32_Sym))) {
  50. printk(KERN_ERR "%s: bad relocation, "
  51. "section %d reloc %d\n",
  52. module->name, relindex, i);
  53. return -ENOEXEC;
  54. }
  55. sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  56. if (rel->r_offset < 0 || rel->r_offset >
  57. dstsec->sh_size - sizeof(u32)) {
  58. printk(KERN_ERR "%s: out of bounds relocation, "
  59. "section %d reloc %d offset %d size %d\n",
  60. module->name, relindex, i, rel->r_offset,
  61. dstsec->sh_size);
  62. return -ENOEXEC;
  63. }
  64. loc = dstsec->sh_addr + rel->r_offset;
  65. switch (ELF32_R_TYPE(rel->r_info)) {
  66. case R_UNICORE_NONE:
  67. /* ignore */
  68. break;
  69. case R_UNICORE_ABS32:
  70. *(u32 *)loc += sym->st_value;
  71. break;
  72. case R_UNICORE_PC24:
  73. case R_UNICORE_CALL:
  74. case R_UNICORE_JUMP24:
  75. offset = (*(u32 *)loc & 0x00ffffff) << 2;
  76. if (offset & 0x02000000)
  77. offset -= 0x04000000;
  78. offset += sym->st_value - loc;
  79. if (offset & 3 ||
  80. offset <= (s32)0xfe000000 ||
  81. offset >= (s32)0x02000000) {
  82. printk(KERN_ERR
  83. "%s: relocation out of range, section "
  84. "%d reloc %d sym '%s'\n", module->name,
  85. relindex, i, strtab + sym->st_name);
  86. return -ENOEXEC;
  87. }
  88. offset >>= 2;
  89. *(u32 *)loc &= 0xff000000;
  90. *(u32 *)loc |= offset & 0x00ffffff;
  91. break;
  92. default:
  93. printk(KERN_ERR "%s: unknown relocation: %u\n",
  94. module->name, ELF32_R_TYPE(rel->r_info));
  95. return -ENOEXEC;
  96. }
  97. }
  98. return 0;
  99. }