module.c 3.0 KB

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