module.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. mod->arch.secstr = secstr;
  33. #endif
  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;
  50. Elf32_Rela *rel_entry = (void *)sechdrs[relsec].sh_addr;
  51. Elf32_Sym *sym_entry, *sym_sec;
  52. Elf32_Addr relocation;
  53. Elf32_Addr location;
  54. Elf32_Addr sec_to_patch;
  55. int relo_type;
  56. unsigned int tgtsec;
  57. tgtsec = sechdrs[relsec].sh_info;
  58. sec_to_patch = sechdrs[tgtsec].sh_addr;
  59. sym_sec = (Elf32_Sym *) sechdrs[symindex].sh_addr;
  60. n = sechdrs[relsec].sh_size / sizeof(*rel_entry);
  61. pr_debug("\n========== Module Sym reloc ===========================\n");
  62. pr_debug("Section to fixup %x\n", sec_to_patch);
  63. pr_debug("=========================================================\n");
  64. pr_debug("rela->r_off | rela->addend | sym->st_value | ADDR | VALUE\n");
  65. pr_debug("=========================================================\n");
  66. /* Loop thru entries in relocation section */
  67. for (i = 0; i < n; i++) {
  68. /* This is where to make the change */
  69. location = sec_to_patch + rel_entry[i].r_offset;
  70. /* This is the symbol it is referring to. Note that all
  71. undefined symbols have been resolved. */
  72. sym_entry = sym_sec + ELF32_R_SYM(rel_entry[i].r_info);
  73. relocation = sym_entry->st_value + rel_entry[i].r_addend;
  74. pr_debug("\t%x\t\t%x\t\t%x %x %x [%s]\n",
  75. rel_entry[i].r_offset, rel_entry[i].r_addend,
  76. sym_entry->st_value, location, relocation,
  77. strtab + sym_entry->st_name);
  78. /* This assumes modules are built with -mlong-calls
  79. * so any branches/jumps are absolute 32 bit jmps
  80. * global data access again is abs 32 bit.
  81. * Both of these are handled by same relocation type
  82. */
  83. relo_type = ELF32_R_TYPE(rel_entry[i].r_info);
  84. if (likely(R_ARC_32_ME == relo_type)) /* ME ( S + A ) */
  85. arc_write_me((unsigned short *)location, relocation);
  86. else if (R_ARC_32 == relo_type) /* ( S + A ) */
  87. *((Elf32_Addr *) location) = relocation;
  88. else if (R_ARC_32_PCREL == relo_type) /* ( S + A ) - PDATA ) */
  89. *((Elf32_Addr *) location) = relocation - location;
  90. else
  91. goto relo_err;
  92. }
  93. if (strcmp(module->arch.secstr+sechdrs[tgtsec].sh_name, ".eh_frame") == 0)
  94. module->arch.unw_sec_idx = tgtsec;
  95. return 0;
  96. relo_err:
  97. pr_err("%s: unknown relocation: %u\n",
  98. module->name, ELF32_R_TYPE(rel_entry[i].r_info));
  99. return -ENOEXEC;
  100. }
  101. /* Just before lift off: After sections have been relocated, we add the
  102. * dwarf section to unwinder table pool
  103. * This couldn't be done in module_frob_arch_sections() because
  104. * relocations had not been applied by then
  105. */
  106. int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs,
  107. struct module *mod)
  108. {
  109. #ifdef CONFIG_ARC_DW2_UNWIND
  110. void *unw;
  111. int unwsec = mod->arch.unw_sec_idx;
  112. if (unwsec) {
  113. unw = unwind_add_table(mod, (void *)sechdrs[unwsec].sh_addr,
  114. sechdrs[unwsec].sh_size);
  115. mod->arch.unw_info = unw;
  116. }
  117. #endif
  118. return 0;
  119. }