module-rela.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Copyright (C) 2001 Rusty Russell.
  17. * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
  18. * Copyright (C) 2005 Thiemo Seufer
  19. */
  20. #include <linux/elf.h>
  21. #include <linux/err.h>
  22. #include <linux/errno.h>
  23. #include <linux/moduleloader.h>
  24. extern int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v);
  25. static int apply_r_mips_32_rela(struct module *me, u32 *location, Elf_Addr v)
  26. {
  27. *location = v;
  28. return 0;
  29. }
  30. static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
  31. {
  32. if (v % 4) {
  33. pr_err("module %s: dangerous R_MIPS_26 RELArelocation\n",
  34. me->name);
  35. return -ENOEXEC;
  36. }
  37. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  38. printk(KERN_ERR
  39. "module %s: relocation overflow\n",
  40. me->name);
  41. return -ENOEXEC;
  42. }
  43. *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
  44. return 0;
  45. }
  46. static int apply_r_mips_hi16_rela(struct module *me, u32 *location, Elf_Addr v)
  47. {
  48. *location = (*location & 0xffff0000) |
  49. ((((long long) v + 0x8000LL) >> 16) & 0xffff);
  50. return 0;
  51. }
  52. static int apply_r_mips_lo16_rela(struct module *me, u32 *location, Elf_Addr v)
  53. {
  54. *location = (*location & 0xffff0000) | (v & 0xffff);
  55. return 0;
  56. }
  57. static int apply_r_mips_64_rela(struct module *me, u32 *location, Elf_Addr v)
  58. {
  59. *(Elf_Addr *)location = v;
  60. return 0;
  61. }
  62. static int apply_r_mips_higher_rela(struct module *me, u32 *location,
  63. Elf_Addr v)
  64. {
  65. *location = (*location & 0xffff0000) |
  66. ((((long long) v + 0x80008000LL) >> 32) & 0xffff);
  67. return 0;
  68. }
  69. static int apply_r_mips_highest_rela(struct module *me, u32 *location,
  70. Elf_Addr v)
  71. {
  72. *location = (*location & 0xffff0000) |
  73. ((((long long) v + 0x800080008000LL) >> 48) & 0xffff);
  74. return 0;
  75. }
  76. static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
  77. Elf_Addr v) = {
  78. [R_MIPS_NONE] = apply_r_mips_none,
  79. [R_MIPS_32] = apply_r_mips_32_rela,
  80. [R_MIPS_26] = apply_r_mips_26_rela,
  81. [R_MIPS_HI16] = apply_r_mips_hi16_rela,
  82. [R_MIPS_LO16] = apply_r_mips_lo16_rela,
  83. [R_MIPS_64] = apply_r_mips_64_rela,
  84. [R_MIPS_HIGHER] = apply_r_mips_higher_rela,
  85. [R_MIPS_HIGHEST] = apply_r_mips_highest_rela
  86. };
  87. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  88. unsigned int symindex, unsigned int relsec,
  89. struct module *me)
  90. {
  91. Elf_Mips_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  92. Elf_Sym *sym;
  93. u32 *location;
  94. unsigned int i;
  95. Elf_Addr v;
  96. int res;
  97. pr_debug("Applying relocate section %u to %u\n", relsec,
  98. sechdrs[relsec].sh_info);
  99. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  100. /* This is where to make the change */
  101. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  102. + rel[i].r_offset;
  103. /* This is the symbol it is referring to */
  104. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  105. + ELF_MIPS_R_SYM(rel[i]);
  106. if (IS_ERR_VALUE(sym->st_value)) {
  107. /* Ignore unresolved weak symbol */
  108. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  109. continue;
  110. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  111. me->name, strtab + sym->st_name);
  112. return -ENOENT;
  113. }
  114. v = sym->st_value + rel[i].r_addend;
  115. res = reloc_handlers_rela[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
  116. if (res)
  117. return res;
  118. }
  119. return 0;
  120. }