module.c 3.7 KB

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