module.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OpenRISC module.c
  4. *
  5. * Linux architectural port borrowing liberally from similar works of
  6. * others. All original copyrights apply as per the original source
  7. * declaration.
  8. *
  9. * Modifications for the OpenRISC architecture:
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. */
  12. #include <linux/moduleloader.h>
  13. #include <linux/elf.h>
  14. int apply_relocate_add(Elf32_Shdr *sechdrs,
  15. const char *strtab,
  16. unsigned int symindex,
  17. unsigned int relsec,
  18. struct module *me)
  19. {
  20. unsigned int i;
  21. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  22. Elf32_Sym *sym;
  23. uint32_t *location;
  24. uint32_t value;
  25. pr_debug("Applying relocate section %u to %u\n", relsec,
  26. sechdrs[relsec].sh_info);
  27. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  28. /* This is where to make the change */
  29. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  30. + rel[i].r_offset;
  31. /* This is the symbol it is referring to. Note that all
  32. undefined symbols have been resolved. */
  33. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  34. + ELF32_R_SYM(rel[i].r_info);
  35. value = sym->st_value + rel[i].r_addend;
  36. switch (ELF32_R_TYPE(rel[i].r_info)) {
  37. case R_OR32_32:
  38. *location = value;
  39. break;
  40. case R_OR32_CONST:
  41. *((uint16_t *)location + 1) = value;
  42. break;
  43. case R_OR32_CONSTH:
  44. *((uint16_t *)location + 1) = value >> 16;
  45. break;
  46. case R_OR32_JUMPTARG:
  47. value -= (uint32_t)location;
  48. value >>= 2;
  49. value &= 0x03ffffff;
  50. value |= *location & 0xfc000000;
  51. *location = value;
  52. break;
  53. default:
  54. pr_err("module %s: Unknown relocation: %u\n",
  55. me->name, ELF32_R_TYPE(rel[i].r_info));
  56. break;
  57. }
  58. }
  59. return 0;
  60. }