module.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2007-2009 PetaLogix
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleloader.h>
  11. #include <linux/kernel.h>
  12. #include <linux/elf.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/fs.h>
  15. #include <linux/string.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/cacheflush.h>
  18. void *module_alloc(unsigned long size)
  19. {
  20. void *ret;
  21. ret = (size == 0) ? NULL : vmalloc(size);
  22. pr_debug("module_alloc (%08lx@%08lx)\n", size, (unsigned long int)ret);
  23. return ret;
  24. }
  25. void module_free(struct module *module, void *region)
  26. {
  27. pr_debug("module_free(%s,%08lx)\n", module->name,
  28. (unsigned long)region);
  29. vfree(region);
  30. }
  31. int module_frob_arch_sections(Elf_Ehdr *hdr,
  32. Elf_Shdr *sechdrs,
  33. char *secstrings,
  34. struct module *mod)
  35. {
  36. return 0;
  37. }
  38. int apply_relocate(Elf32_Shdr *sechdrs, const char *strtab,
  39. unsigned int symindex, unsigned int relsec, struct module *module)
  40. {
  41. printk(KERN_ERR "module %s: ADD RELOCATION unsupported\n",
  42. module->name);
  43. return -ENOEXEC;
  44. }
  45. int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,
  46. unsigned int symindex, unsigned int relsec, struct module *module)
  47. {
  48. unsigned int i;
  49. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  50. Elf32_Sym *sym;
  51. unsigned long int *location;
  52. unsigned long int value;
  53. #if __GNUC__ < 4
  54. unsigned long int old_value;
  55. #endif
  56. pr_debug("Applying add relocation section %u to %u\n",
  57. relsec, sechdrs[relsec].sh_info);
  58. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  59. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr +
  60. rela[i].r_offset;
  61. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr +
  62. ELF32_R_SYM(rela[i].r_info);
  63. value = sym->st_value + rela[i].r_addend;
  64. switch (ELF32_R_TYPE(rela[i].r_info)) {
  65. /*
  66. * Be careful! mb-gcc / mb-ld splits the relocs between the
  67. * text and the reloc table. In general this means we must
  68. * read the current contents of (*location), add any offset
  69. * then store the result back in
  70. */
  71. case R_MICROBLAZE_32:
  72. #if __GNUC__ < 4
  73. old_value = *location;
  74. *location = value + old_value;
  75. pr_debug("R_MICROBLAZE_32 (%08lx->%08lx)\n",
  76. old_value, value);
  77. #else
  78. *location = value;
  79. #endif
  80. break;
  81. case R_MICROBLAZE_64:
  82. #if __GNUC__ < 4
  83. /* Split relocs only required/used pre gcc4.1.1 */
  84. old_value = ((location[0] & 0x0000FFFF) << 16) |
  85. (location[1] & 0x0000FFFF);
  86. value += old_value;
  87. #endif
  88. location[0] = (location[0] & 0xFFFF0000) |
  89. (value >> 16);
  90. location[1] = (location[1] & 0xFFFF0000) |
  91. (value & 0xFFFF);
  92. #if __GNUC__ < 4
  93. pr_debug("R_MICROBLAZE_64 (%08lx->%08lx)\n",
  94. old_value, value);
  95. #endif
  96. break;
  97. case R_MICROBLAZE_64_PCREL:
  98. #if __GNUC__ < 4
  99. old_value = (location[0] & 0xFFFF) << 16 |
  100. (location[1] & 0xFFFF);
  101. value -= old_value;
  102. #endif
  103. value -= (unsigned long int)(location) + 4;
  104. location[0] = (location[0] & 0xFFFF0000) |
  105. (value >> 16);
  106. location[1] = (location[1] & 0xFFFF0000) |
  107. (value & 0xFFFF);
  108. pr_debug("R_MICROBLAZE_64_PCREL (%08lx)\n",
  109. value);
  110. break;
  111. case R_MICROBLAZE_32_PCREL_LO:
  112. pr_debug("R_MICROBLAZE_32_PCREL_LO\n");
  113. break;
  114. case R_MICROBLAZE_64_NONE:
  115. pr_debug("R_MICROBLAZE_NONE\n");
  116. break;
  117. case R_MICROBLAZE_NONE:
  118. pr_debug("R_MICROBLAZE_NONE\n");
  119. break;
  120. default:
  121. printk(KERN_ERR "module %s: "
  122. "Unknown relocation: %u\n",
  123. module->name,
  124. ELF32_R_TYPE(rela[i].r_info));
  125. return -ENOEXEC;
  126. }
  127. }
  128. return 0;
  129. }
  130. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  131. struct module *module)
  132. {
  133. flush_dcache();
  134. return 0;
  135. }
  136. void module_arch_cleanup(struct module *mod)
  137. {
  138. }