module.c 1.9 KB

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