prelink-riscv.inc 3.0 KB

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