module.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/string.h>
  17. #include <asm/pgtable.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 locoffs;
  53. unsigned long int value;
  54. #if __GNUC__ < 4
  55. unsigned long int old_value;
  56. #endif
  57. pr_debug("Applying add relocation section %u to %u\n",
  58. relsec, sechdrs[relsec].sh_info);
  59. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  60. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr +
  61. rela[i].r_offset;
  62. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr +
  63. ELF32_R_SYM(rela[i].r_info);
  64. value = sym->st_value + rela[i].r_addend;
  65. switch (ELF32_R_TYPE(rela[i].r_info)) {
  66. /*
  67. * Be careful! mb-gcc / mb-ld splits the relocs between the
  68. * text and the reloc table. In general this means we must
  69. * read the current contents of (*location), add any offset
  70. * then store the result back in
  71. */
  72. case R_MICROBLAZE_32:
  73. #if __GNUC__ < 4
  74. old_value = *location;
  75. *location = value + old_value;
  76. pr_debug("R_MICROBLAZE_32 (%08lx->%08lx)\n",
  77. old_value, value);
  78. #else
  79. *location = value;
  80. #endif
  81. break;
  82. case R_MICROBLAZE_64:
  83. #if __GNUC__ < 4
  84. /* Split relocs only required/used pre gcc4.1.1 */
  85. old_value = ((location[0] & 0x0000FFFF) << 16) |
  86. (location[1] & 0x0000FFFF);
  87. value += old_value;
  88. #endif
  89. location[0] = (location[0] & 0xFFFF0000) |
  90. (value >> 16);
  91. location[1] = (location[1] & 0xFFFF0000) |
  92. (value & 0xFFFF);
  93. #if __GNUC__ < 4
  94. pr_debug("R_MICROBLAZE_64 (%08lx->%08lx)\n",
  95. old_value, value);
  96. #endif
  97. break;
  98. case R_MICROBLAZE_64_PCREL:
  99. locoffs = (location[0] & 0xFFFF) << 16 |
  100. (location[1] & 0xFFFF);
  101. value -= (unsigned long int)(location) + 4 +
  102. locoffs;
  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_NONE:
  111. pr_debug("R_MICROBLAZE_NONE\n");
  112. break;
  113. default:
  114. printk(KERN_ERR "module %s: "
  115. "Unknown relocation: %u\n",
  116. module->name,
  117. ELF32_R_TYPE(rela->r_info));
  118. return -ENOEXEC;
  119. }
  120. }
  121. return 0;
  122. }
  123. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  124. struct module *module)
  125. {
  126. return 0;
  127. }
  128. void module_arch_cleanup(struct module *mod)
  129. {
  130. }