riscv.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021/04/23 chunyexixiaoyu first version
  9. */
  10. #include "../dlmodule.h"
  11. #include "../dlelf.h"
  12. #if (__riscv_xlen == 64)
  13. #define R_RISCV_NONE 0
  14. #define R_RISCV_32 1
  15. #define R_RISCV_64 2
  16. #define R_RISCV_RELATIVE 3
  17. #define R_RISCV_COPY 4
  18. #define R_RISCV_JUMP_SLOT 5
  19. #define R_RISCV_TLS_DTPMOD32 6
  20. #define R_RISCV_TLS_DTPMOD64 7
  21. #define R_RISCV_TLS_DTPREL32 8
  22. #define R_RISCV_TLS_DTPREL64 9
  23. #define R_RISCV_TLS_TPREL32 10
  24. #define R_RISCV_TLS_TPREL64 11
  25. #define DBG_TAG "kernel.module"
  26. #ifdef RT_DEBUG_MODULE
  27. #define DBG_LVL DBG_LOG
  28. #else
  29. #define DBG_LVL DBG_WARNING
  30. #endif /* defined (RT_DEBUG_MODULE) */
  31. #include <rtdbg.h>
  32. int dlmodule_relocate(struct rt_dlmodule *module, Elf_Rel *rel, Elf_Addr sym_val)
  33. {
  34. Elf64_Addr *where, tmp;
  35. Elf64_Sword addend, offset;
  36. rt_uint64_t upper, lower, sign, j1, j2;
  37. where = (Elf64_Addr *)((rt_uint8_t *)module->mem_space
  38. + rel->r_offset
  39. - module->vstart_addr);
  40. switch (ELF64_R_TYPE(rel->r_info))
  41. {
  42. case R_RISCV_NONE:
  43. break;
  44. case R_RISCV_64:
  45. *where = (Elf64_Addr)(sym_val + rel->r_addend);
  46. LOG_D("R_RISCV_64: %x -> %x",where, *where);
  47. break;
  48. case R_RISCV_RELATIVE:
  49. *where = (Elf64_Addr)((rt_uint8_t *)module->mem_space - module->vstart_addr + rel->r_addend);
  50. LOG_D("R_RISCV_RELATIVE: %x -> %x",where, *where);
  51. break;
  52. case R_RISCV_JUMP_SLOT:
  53. *where = (Elf64_Addr)sym_val;
  54. LOG_D("R_RISCV_JUMP_SLOT: %x -> %x",where, *where);
  55. break;
  56. default:
  57. LOG_D("__riscv__ELF: invalid relocate TYPE %d", ELF64_R_TYPE(rel->r_info));
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. #endif