module.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /*
  23. * This gets called before relocation loop in generic loader
  24. * Make a note of the section index of unwinding section
  25. */
  26. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  27. char *secstr, struct module *mod)
  28. {
  29. #ifdef CONFIG_ARC_DW2_UNWIND
  30. mod->arch.unw_sec_idx = 0;
  31. mod->arch.unw_info = NULL;
  32. #endif
  33. mod->arch.secstr = secstr;
  34. return 0;
  35. }
  36. void module_arch_cleanup(struct module *mod)
  37. {
  38. #ifdef CONFIG_ARC_DW2_UNWIND
  39. if (mod->arch.unw_info)
  40. unwind_remove_table(mod->arch.unw_info, 0);
  41. #endif
  42. }
  43. int apply_relocate_add(Elf32_Shdr *sechdrs,
  44. const char *strtab,
  45. unsigned int symindex, /* sec index for sym tbl */
  46. unsigned int relsec, /* sec index for relo sec */
  47. struct module *module)
  48. {
  49. int i, n, relo_type;
  50. Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
  51. Elf32_Sym *sym_entry, *sym_sec;
  52. Elf32_Addr relocation, location, tgt_addr;
  53. unsigned int tgtsec;
  54. /*
  55. * @relsec has relocations e.g. .rela.init.text
  56. * @tgtsec is section to patch e.g. .init.text
  57. */
  58. tgtsec = sechdrs[relsec].sh_info;
  59. tgt_addr = sechdrs[tgtsec].sh_addr;
  60. sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
  61. n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
  62. pr_debug("\nSection to fixup %s @%x\n",
  63. module->arch.secstr + sechdrs[tgtsec].sh_name, tgt_addr);
  64. pr_debug("=========================================================\n");
  65. pr_debug("r_off\tr_add\tst_value ADDRESS VALUE\n");
  66. pr_debug("=========================================================\n");
  67. /* Loop thru entries in relocation section */
  68. for (i = 0; i < n; i++) {
  69. const char *s;
  70. /* This is where to make the change */
  71. location = tgt_addr + rel_entry[i].r_offset;
  72. /* This is the symbol it is referring to. Note that all
  73. undefined symbols have been resolved. */
  74. sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
  75. relocation = sym_entry->st_value + rel_entry[i].r_addend;
  76. if (sym_entry->st_name == 0 && ELF_ST_TYPE (sym_entry->st_info) == STT_SECTION) {
  77. s = module->arch.secstr + sechdrs[sym_entry->st_shndx].sh_name;
  78. } else {
  79. s = strtab + sym_entry->st_name;
  80. }
  81. pr_debug(" %x\t%x\t%x %x %x [%s]\n",
  82. rel_entry[i].r_offset, rel_entry[i].r_addend,
  83. sym_entry->st_value, location, relocation, s);
  84. /* This assumes modules are built with -mlong-calls
  85. * so any branches/jumps are absolute 32 bit jmps
  86. * global data access again is abs 32 bit.
  87. * Both of these are handled by same relocation type
  88. */
  89. relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
  90. if (likely(R_ARC_32_ME == relo_type)) /* ME ( S + A ) */
  91. arc_write_me((unsigned short *)location, relocation);
  92. else if (R_ARC_32 == relo_type) /* ( S + A ) */
  93. *((Elf32_Addr *) location) = relocation;
  94. else if (R_ARC_32_PCREL == relo_type) /* ( S + A ) - PDATA ) */
  95. *((Elf32_Addr *) location) = relocation - location;
  96. else
  97. goto relo_err;
  98. }
  99. #ifdef CONFIG_ARC_DW2_UNWIND
  100. if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0)
  101. module->arch.unw_sec_idx = tgtsec;
  102. #endif
  103. return 0;
  104. relo_err:
  105. pr_err("%s: unknown relocation: %u\n",
  106. module->name, ELF32_R_TYPE(rel_entry[i].r_info));
  107. return -ENOEXEC;
  108. }
  109. /* Just before lift off: After sections have been relocated, we add the
  110. * dwarf section to unwinder table pool
  111. * This couldn't be done in module_frob_arch_sections() because
  112. * relocations had not been applied by then
  113. */
  114. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  115. struct module *mod)
  116. {
  117. #ifdef CONFIG_ARC_DW2_UNWIND
  118. void *unw;
  119. int unwsec = mod->arch.unw_sec_idx;
  120. if (unwsec) {
  121. unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
  122. sechdrs[unwsec].sh_size);
  123. mod->arch.unw_info = unw;
  124. }
  125. #endif
  126. return 0;
  127. }