riscv.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2006-2018, 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. int dlmodule_relocate(struct rt_dlmodule *module, Elf_Rel *rel, Elf_Addr sym_val)
  26. {
  27. Elf64_Addr *where, tmp;
  28. Elf64_Sword addend, offset;
  29. rt_uint64_t upper, lower, sign, j1, j2;
  30. where = (Elf64_Addr *)((rt_uint8_t *)module->mem_space
  31. + rel->r_offset
  32. - module->vstart_addr);
  33. switch (ELF64_R_TYPE(rel->r_info))
  34. {
  35. case R_RISCV_NONE:
  36. break;
  37. case R_RISCV_64:
  38. *where = (Elf64_Addr)(sym_val + rel->r_addend);
  39. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_RISCV_64: %x -> %x\n",where, *where));
  40. break;
  41. case R_RISCV_RELATIVE:
  42. *where = (Elf64_Addr)((rt_uint8_t *)module->mem_space - module->vstart_addr + rel->r_addend);
  43. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_RISCV_RELATIVE: %x -> %x\n",where, *where));
  44. break;
  45. case R_RISCV_JUMP_SLOT:
  46. *where = (Elf64_Addr)sym_val;
  47. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_RISCV_JUMP_SLOT: %x -> %x\n",where, *where));
  48. break;
  49. default:
  50. RT_DEBUG_LOG(RT_DEBUG_MODULE, ("__riscv__ELF: invalid relocate TYPE %d\n", ELF64_R_TYPE(rel->r_info)));
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. #endif