module.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/moduleloader.h>
  7. #include <linux/kernel.h>
  8. #include <linux/elf.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/slab.h>
  11. #include <linux/fs.h>
  12. #include <linux/string.h>
  13. #include <asm/unwind.h>
  14. static inline void arc_write_me(unsigned short *addr, unsigned long value)
  15. {
  16. *addr = (value & 0xffff0000) >> 16;
  17. *(addr + 1) = (value & 0xffff);
  18. }
  19. /*
  20. * This gets called before relocation loop in generic loader
  21. * Make a note of the section index of unwinding section
  22. */
  23. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  24. char *secstr, struct module *mod)
  25. {
  26. #ifdef CONFIG_ARC_DW2_UNWIND
  27. mod->arch.unw_sec_idx = 0;
  28. mod->arch.unw_info = NULL;
  29. #endif
  30. mod->arch.secstr = secstr;
  31. return 0;
  32. }
  33. void module_arch_cleanup(struct module *mod)
  34. {
  35. #ifdef CONFIG_ARC_DW2_UNWIND
  36. if (mod->arch.unw_info)
  37. unwind_remove_table(mod->arch.unw_info, 0);
  38. #endif
  39. }
  40. int apply_relocate_add(Elf32_Shdr *sechdrs,
  41. const char *strtab,
  42. unsigned int symindex, /* sec index for sym tbl */
  43. unsigned int relsec, /* sec index for relo sec */
  44. struct module *module)
  45. {
  46. int i, n, relo_type;
  47. Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
  48. Elf32_Sym *sym_entry, *sym_sec;
  49. Elf32_Addr relocation, location, tgt_addr;
  50. unsigned int tgtsec;
  51. /*
  52. * @relsec has relocations e.g. .rela.init.text
  53. * @tgtsec is section to patch e.g. .init.text
  54. */
  55. tgtsec = sechdrs[relsec].sh_info;
  56. tgt_addr = sechdrs[tgtsec].sh_addr;
  57. sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
  58. n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
  59. pr_debug("\nSection to fixup %s @%x\n",
  60. module->arch.secstr + sechdrs[tgtsec].sh_name, tgt_addr);
  61. pr_debug("=========================================================\n");
  62. pr_debug("r_off\tr_add\tst_value ADDRESS VALUE\n");
  63. pr_debug("=========================================================\n");
  64. /* Loop thru entries in relocation section */
  65. for (i = 0; i < n; i++) {
  66. const char *s;
  67. /* This is where to make the change */
  68. location = tgt_addr + rel_entry[i].r_offset;
  69. /* This is the symbol it is referring to. Note that all
  70. undefined symbols have been resolved. */
  71. sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
  72. relocation = sym_entry->st_value + rel_entry[i].r_addend;
  73. if (sym_entry->st_name == 0 && ELF_ST_TYPE (sym_entry->st_info) == STT_SECTION) {
  74. s = module->arch.secstr + sechdrs[sym_entry->st_shndx].sh_name;
  75. } else {
  76. s = strtab + sym_entry->st_name;
  77. }
  78. pr_debug(" %x\t%x\t%x %x %x [%s]\n",
  79. rel_entry[i].r_offset, rel_entry[i].r_addend,
  80. sym_entry->st_value, location, relocation, s);
  81. /* This assumes modules are built with -mlong-calls
  82. * so any branches/jumps are absolute 32 bit jmps
  83. * global data access again is abs 32 bit.
  84. * Both of these are handled by same relocation type
  85. */
  86. relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
  87. if (likely(R_ARC_32_ME == relo_type)) /* ME ( S + A ) */
  88. arc_write_me((unsigned short *)location, relocation);
  89. else if (R_ARC_32 == relo_type) /* ( S + A ) */
  90. *((Elf32_Addr *) location) = relocation;
  91. else if (R_ARC_32_PCREL == relo_type) /* ( S + A ) - PDATA ) */
  92. *((Elf32_Addr *) location) = relocation - location;
  93. else
  94. goto relo_err;
  95. }
  96. #ifdef CONFIG_ARC_DW2_UNWIND
  97. if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0)
  98. module->arch.unw_sec_idx = tgtsec;
  99. #endif
  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. }