module.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Kernel module loader for Hexagon
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <asm/module.h>
  21. #include <linux/elf.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleloader.h>
  24. #include <linux/vmalloc.h>
  25. #if 0
  26. #define DEBUGP printk
  27. #else
  28. #define DEBUGP(fmt , ...)
  29. #endif
  30. /*
  31. * module_frob_arch_sections - tweak got/plt sections.
  32. * @hdr - pointer to elf header
  33. * @sechdrs - pointer to elf load section headers
  34. * @secstrings - symbol names
  35. * @mod - pointer to module
  36. */
  37. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  38. char *secstrings,
  39. struct module *mod)
  40. {
  41. unsigned int i;
  42. int found = 0;
  43. /* Look for .plt and/or .got.plt and/or .init.plt sections */
  44. for (i = 0; i < hdr->e_shnum; i++) {
  45. DEBUGP("Section %d is %s\n", i,
  46. secstrings + sechdrs[i].sh_name);
  47. if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
  48. found = i+1;
  49. if (strcmp(secstrings + sechdrs[i].sh_name, ".got.plt") == 0)
  50. found = i+1;
  51. if (strcmp(secstrings + sechdrs[i].sh_name, ".rela.plt") == 0)
  52. found = i+1;
  53. }
  54. /* At this time, we don't support modules comiled with -shared */
  55. if (found) {
  56. printk(KERN_WARNING
  57. "Module '%s' contains unexpected .plt/.got sections.\n",
  58. mod->name);
  59. /* return -ENOEXEC; */
  60. }
  61. return 0;
  62. }
  63. /*
  64. * apply_relocate_add - perform rela relocations.
  65. * @sechdrs - pointer to section headers
  66. * @strtab - some sort of start address?
  67. * @symindex - symbol index offset or something?
  68. * @relsec - address to relocate to?
  69. * @module - pointer to module
  70. *
  71. * Perform rela relocations.
  72. */
  73. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  74. unsigned int symindex, unsigned int relsec,
  75. struct module *module)
  76. {
  77. unsigned int i;
  78. Elf32_Sym *sym;
  79. uint32_t *location;
  80. uint32_t value;
  81. unsigned int nrelocs = sechdrs[relsec].sh_size / sizeof(Elf32_Rela);
  82. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  83. Elf32_Word sym_info = sechdrs[relsec].sh_info;
  84. Elf32_Sym *sym_base = (Elf32_Sym *) sechdrs[symindex].sh_addr;
  85. void *loc_base = (void *) sechdrs[sym_info].sh_addr;
  86. DEBUGP("Applying relocations in section %u to section %u base=%p\n",
  87. relsec, sym_info, loc_base);
  88. for (i = 0; i < nrelocs; i++) {
  89. /* Symbol to relocate */
  90. sym = sym_base + ELF32_R_SYM(rela[i].r_info);
  91. /* Where to make the change */
  92. location = loc_base + rela[i].r_offset;
  93. /* `Everything is relative'. */
  94. value = sym->st_value + rela[i].r_addend;
  95. DEBUGP("%d: value=%08x loc=%p reloc=%d symbol=%s\n",
  96. i, value, location, ELF32_R_TYPE(rela[i].r_info),
  97. sym->st_name ?
  98. &strtab[sym->st_name] : "(anonymous)");
  99. switch (ELF32_R_TYPE(rela[i].r_info)) {
  100. case R_HEXAGON_B22_PCREL: {
  101. int dist = (int)(value - (uint32_t)location);
  102. if ((dist < -0x00800000) ||
  103. (dist >= 0x00800000)) {
  104. printk(KERN_ERR
  105. "%s: %s: %08x=%08x-%08x %s\n",
  106. module->name,
  107. "R_HEXAGON_B22_PCREL reloc out of range",
  108. dist, value, (uint32_t)location,
  109. sym->st_name ?
  110. &strtab[sym->st_name] : "(anonymous)");
  111. return -ENOEXEC;
  112. }
  113. DEBUGP("B22_PCREL contents: %08X.\n", *location);
  114. *location &= ~0x01ff3fff;
  115. *location |= 0x00003fff & dist;
  116. *location |= 0x01ff0000 & (dist<<2);
  117. DEBUGP("Contents after reloc: %08x\n", *location);
  118. break;
  119. }
  120. case R_HEXAGON_HI16:
  121. value = (value>>16) & 0xffff;
  122. /* fallthrough */
  123. case R_HEXAGON_LO16:
  124. *location &= ~0x00c03fff;
  125. *location |= value & 0x3fff;
  126. *location |= (value & 0xc000) << 8;
  127. break;
  128. case R_HEXAGON_32:
  129. *location = value;
  130. break;
  131. case R_HEXAGON_32_PCREL:
  132. *location = value - (uint32_t)location;
  133. break;
  134. case R_HEXAGON_PLT_B22_PCREL:
  135. case R_HEXAGON_GOTOFF_LO16:
  136. case R_HEXAGON_GOTOFF_HI16:
  137. printk(KERN_ERR "%s: GOT/PLT relocations unsupported\n",
  138. module->name);
  139. return -ENOEXEC;
  140. default:
  141. printk(KERN_ERR "%s: unknown relocation: %u\n",
  142. module->name,
  143. ELF32_R_TYPE(rela[i].r_info));
  144. return -ENOEXEC;
  145. }
  146. }
  147. return 0;
  148. }