prelink-riscv.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Andes Technology
  4. * Chih-Mao Chen <cmchen@andestech.com>
  5. *
  6. * Statically process runtime relocations on RISC-V ELF images
  7. * so that it can be directly executed when loaded at LMA
  8. * without fixup. Both RV32 and RV64 are supported.
  9. */
  10. #define CONCAT_IMPL(x, y) x##y
  11. #define CONCAT(x, y) CONCAT_IMPL(x, y)
  12. #define CONCAT3(x, y, z) CONCAT(CONCAT(x, y), z)
  13. #define prelink_nn CONCAT(prelink, PRELINK_INC_BITS)
  14. #define uintnn_t CONCAT3(uint, PRELINK_INC_BITS, _t)
  15. #define get_offset_nn CONCAT(get_offset_, PRELINK_INC_BITS)
  16. #define Elf_Ehdr CONCAT3(Elf, PRELINK_INC_BITS, _Ehdr)
  17. #define Elf_Phdr CONCAT3(Elf, PRELINK_INC_BITS, _Phdr)
  18. #define Elf_Rela CONCAT3(Elf, PRELINK_INC_BITS, _Rela)
  19. #define Elf_Sym CONCAT3(Elf, PRELINK_INC_BITS, _Sym)
  20. #define Elf_Dyn CONCAT3(Elf, PRELINK_INC_BITS, _Dyn)
  21. #define Elf_Addr CONCAT3(Elf, PRELINK_INC_BITS, _Addr)
  22. #define ELF_R_TYPE CONCAT3(ELF, PRELINK_INC_BITS, _R_TYPE)
  23. #define ELF_R_SYM CONCAT3(ELF, PRELINK_INC_BITS, _R_SYM)
  24. static void* get_offset_nn (void* data, Elf_Phdr* phdrs, size_t phnum, Elf_Addr addr)
  25. {
  26. Elf_Phdr *p;
  27. for (p = phdrs; p < phdrs + phnum; ++p)
  28. if (p->p_vaddr <= addr && p->p_vaddr + p->p_memsz > addr)
  29. return data + p->p_offset + (addr - p->p_vaddr);
  30. return NULL;
  31. }
  32. static void prelink_nn(void *data)
  33. {
  34. Elf_Ehdr *ehdr = data;
  35. Elf_Phdr *p;
  36. Elf_Dyn *dyn;
  37. Elf_Rela *r;
  38. if (ehdr->e_machine != EM_RISCV)
  39. die("Machine type is not RISC-V");
  40. Elf_Phdr *phdrs = data + ehdr->e_phoff;
  41. Elf_Dyn *dyns = NULL;
  42. for (p = phdrs; p < phdrs + ehdr->e_phnum; ++p) {
  43. if (p->p_type == PT_DYNAMIC) {
  44. dyns = data + p->p_offset;
  45. break;
  46. }
  47. }
  48. if (dyns == NULL)
  49. die("No dynamic section found");
  50. Elf_Rela *rela_dyn = NULL;
  51. size_t rela_count = 0;
  52. Elf_Sym *dynsym = NULL;
  53. for (dyn = dyns;; ++dyn) {
  54. if (dyn->d_tag == DT_NULL)
  55. break;
  56. else if (dyn->d_tag == DT_RELA)
  57. rela_dyn = get_offset_nn(data, phdrs, ehdr->e_phnum, + dyn->d_un.d_ptr);
  58. else if (dyn->d_tag == DT_RELASZ)
  59. rela_count = dyn->d_un.d_val / sizeof(Elf_Rela);
  60. else if (dyn->d_tag == DT_SYMTAB)
  61. dynsym = get_offset_nn(data, phdrs, ehdr->e_phnum, + dyn->d_un.d_ptr);
  62. }
  63. if (rela_dyn == NULL)
  64. die("No .rela.dyn found");
  65. if (dynsym == NULL)
  66. die("No .dynsym found");
  67. for (r = rela_dyn; r < rela_dyn + rela_count; ++r) {
  68. void* buf = get_offset_nn(data, phdrs, ehdr->e_phnum, r->r_offset);
  69. if (buf == NULL)
  70. continue;
  71. if (ELF_R_TYPE(r->r_info) == R_RISCV_RELATIVE)
  72. *((uintnn_t*) buf) = r->r_addend;
  73. else if (ELF_R_TYPE(r->r_info) == R_RISCV_32)
  74. *((uint32_t*) buf) = dynsym[ELF_R_SYM(r->r_info)].st_value;
  75. else if (ELF_R_TYPE(r->r_info) == R_RISCV_64)
  76. *((uint64_t*) buf) = dynsym[ELF_R_SYM(r->r_info)].st_value;
  77. }
  78. }
  79. #undef prelink_nn
  80. #undef uintnn_t
  81. #undef get_offset_nn
  82. #undef Elf_Ehdr
  83. #undef Elf_Phdr
  84. #undef Elf_Rela
  85. #undef Elf_Sym
  86. #undef Elf_Dyn
  87. #undef Elf_Addr
  88. #undef ELF_R_TYPE
  89. #undef ELF_R_SYM
  90. #undef CONCAT_IMPL
  91. #undef CONCAT
  92. #undef CONCAT3