module.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleloader.h>
  10. #include <linux/kernel.h>
  11. #include <linux/elf.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/slab.h>
  14. #include <linux/fs.h>
  15. #include <linux/string.h>
  16. #include <asm/unwind.h>
  17. static inline void arc_write_me(unsigned short *addr, unsigned long value)
  18. {
  19. *addr = (value & 0xffff0000) >> 16;
  20. *(addr + 1) = (value & 0xffff);
  21. }
  22. /* ARC specific section quirks - before relocation loop in generic loader
  23. *
  24. * For dwarf unwinding out of modules, this needs to
  25. * 1. Ensure the .debug_frame is allocatable (ARC Linker bug: despite
  26. * -fasynchronous-unwind-tables it doesn't).
  27. * 2. Since we are iterating thru sec hdr tbl anyways, make a note of
  28. * the exact section index, for later use.
  29. */
  30. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  31. char *secstr, struct module *mod)
  32. {
  33. #ifdef CONFIG_ARC_DW2_UNWIND
  34. int i;
  35. mod->arch.unw_sec_idx = 0;
  36. mod->arch.unw_info = NULL;
  37. for (i = 1; i < hdr->e_shnum; i++) {
  38. if (strcmp(secstr+sechdrs[i].sh_name, ".debug_frame") == 0) {
  39. sechdrs[i].sh_flags |= SHF_ALLOC;
  40. mod->arch.unw_sec_idx = i;
  41. break;
  42. }
  43. }
  44. #endif
  45. return 0;
  46. }
  47. void module_arch_cleanup(struct module *mod)
  48. {
  49. #ifdef CONFIG_ARC_DW2_UNWIND
  50. if (mod->arch.unw_info)
  51. unwind_remove_table(mod->arch.unw_info, 0);
  52. #endif
  53. }
  54. int apply_relocate_add(Elf32_Shdr *sechdrs,
  55. const char *strtab,
  56. unsigned int symindex, /* sec index for sym tbl */
  57. unsigned int relsec, /* sec index for relo sec */
  58. struct module *module)
  59. {
  60. int i, n;
  61. Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
  62. Elf32_Sym *sym_entry, *sym_sec;
  63. Elf32_Addr relocation;
  64. Elf32_Addr location;
  65. Elf32_Addr sec_to_patch;
  66. int relo_type;
  67. sec_to_patch = sechdrs[sechdrs[relsec].sh_info].sh_addr;
  68. sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
  69. n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
  70. pr_debug("\n========== Module Sym reloc ===========================\n");
  71. pr_debug("Section to fixup %x\n", sec_to_patch);
  72. pr_debug("=========================================================\n");
  73. pr_debug("rela->r_off | rela->addend | sym->st_value | ADDR | VALUE\n");
  74. pr_debug("=========================================================\n");
  75. /* Loop thru entries in relocation section */
  76. for (i = 0; i < n; i++) {
  77. /* This is where to make the change */
  78. location = sec_to_patch + rel_entry[i].r_offset;
  79. /* This is the symbol it is referring to. Note that all
  80. undefined symbols have been resolved. */
  81. sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
  82. relocation = sym_entry->st_value + rel_entry[i].r_addend;
  83. pr_debug("\t%x\t\t%x\t\t%x %x %x [%s]\n",
  84. rel_entry[i].r_offset, rel_entry[i].r_addend,
  85. sym_entry->st_value, location, relocation,
  86. strtab + sym_entry->st_name);
  87. /* This assumes modules are built with -mlong-calls
  88. * so any branches/jumps are absolute 32 bit jmps
  89. * global data access again is abs 32 bit.
  90. * Both of these are handled by same relocation type
  91. */
  92. relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
  93. if (likely(R_ARC_32_ME == relo_type))
  94. arc_write_me((unsigned short *)location, relocation);
  95. else if (R_ARC_32 == relo_type)
  96. *((Elf32_Addr *) location) = relocation;
  97. else
  98. goto relo_err;
  99. }
  100. return 0;
  101. relo_err:
  102. pr_err("%s: unknown relocation: %u\n",
  103. module->name, ELF32_R_TYPE(rel_entry[i].r_info));
  104. return -ENOEXEC;
  105. }
  106. /* Just before lift off: After sections have been relocated, we add the
  107. * dwarf section to unwinder table pool
  108. * This couldn't be done in module_frob_arch_sections() because
  109. * relocations had not been applied by then
  110. */
  111. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  112. struct module *mod)
  113. {
  114. #ifdef CONFIG_ARC_DW2_UNWIND
  115. void *unw;
  116. int unwsec = mod->arch.unw_sec_idx;
  117. if (unwsec) {
  118. unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
  119. sechdrs[unwsec].sh_size);
  120. mod->arch.unw_info = unw;
  121. }
  122. #endif
  123. return 0;
  124. }