module.c 2.7 KB

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