relocate-rela.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
  5. *
  6. * 64-bit and little-endian target only until we need to support a different
  7. * arch that needs this.
  8. */
  9. #include <elf.h>
  10. #include <errno.h>
  11. #include <inttypes.h>
  12. #include <stdarg.h>
  13. #include <stdbool.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #ifndef R_AARCH64_RELATIVE
  18. #define R_AARCH64_RELATIVE 1027
  19. #endif
  20. static const bool debug_en;
  21. static void debug(const char *fmt, ...)
  22. {
  23. va_list args;
  24. va_start(args, fmt);
  25. if (debug_en)
  26. vprintf(fmt, args);
  27. }
  28. static bool supported_rela(Elf64_Rela *rela)
  29. {
  30. uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
  31. uint32_t type = rela->r_info & mask;
  32. switch (type) {
  33. #ifdef R_AARCH64_RELATIVE
  34. case R_AARCH64_RELATIVE:
  35. return true;
  36. #endif
  37. default:
  38. fprintf(stderr, "warning: unsupported relocation type %"
  39. PRIu32 " at %" PRIx64 "\n",
  40. type, rela->r_offset);
  41. return false;
  42. }
  43. }
  44. static inline uint64_t swap64(uint64_t val)
  45. {
  46. return ((val >> 56) & 0x00000000000000ffULL) |
  47. ((val >> 40) & 0x000000000000ff00ULL) |
  48. ((val >> 24) & 0x0000000000ff0000ULL) |
  49. ((val >> 8) & 0x00000000ff000000ULL) |
  50. ((val << 8) & 0x000000ff00000000ULL) |
  51. ((val << 24) & 0x0000ff0000000000ULL) |
  52. ((val << 40) & 0x00ff000000000000ULL) |
  53. ((val << 56) & 0xff00000000000000ULL);
  54. }
  55. #if __BYTE_ORDER == __LITTLE_ENDIAN
  56. static inline uint64_t be64(uint64_t val)
  57. {
  58. return swap64(val);
  59. }
  60. static inline uint64_t le64(uint64_t val)
  61. {
  62. return val;
  63. }
  64. #else
  65. static inline uint64_t le64(uint64_t val)
  66. {
  67. return swap64(val);
  68. }
  69. static inline uint64_t be64(uint64_t val)
  70. {
  71. return val;
  72. }
  73. #endif
  74. static bool read_num(const char *str, uint64_t *num)
  75. {
  76. char *endptr;
  77. *num = strtoull(str, &endptr, 16);
  78. return str[0] && !endptr[0];
  79. }
  80. int main(int argc, char **argv)
  81. {
  82. FILE *f;
  83. int i, num;
  84. uint64_t rela_start, rela_end, text_base;
  85. if (argc != 5) {
  86. fprintf(stderr, "Statically apply ELF rela relocations\n");
  87. fprintf(stderr, "Usage: %s <bin file> <text base> " \
  88. "<rela start> <rela end>\n", argv[0]);
  89. fprintf(stderr, "All numbers in hex.\n");
  90. return 1;
  91. }
  92. f = fopen(argv[1], "r+b");
  93. if (!f) {
  94. fprintf(stderr, "%s: Cannot open %s: %s\n",
  95. argv[0], argv[1], strerror(errno));
  96. return 2;
  97. }
  98. if (!read_num(argv[2], &text_base) ||
  99. !read_num(argv[3], &rela_start) ||
  100. !read_num(argv[4], &rela_end)) {
  101. fprintf(stderr, "%s: bad number\n", argv[0]);
  102. return 3;
  103. }
  104. if (rela_start > rela_end || rela_start < text_base ||
  105. (rela_end - rela_start) % sizeof(Elf64_Rela)) {
  106. fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
  107. return 3;
  108. }
  109. rela_start -= text_base;
  110. rela_end -= text_base;
  111. num = (rela_end - rela_start) / sizeof(Elf64_Rela);
  112. for (i = 0; i < num; i++) {
  113. Elf64_Rela rela, swrela;
  114. uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
  115. uint64_t addr;
  116. if (fseek(f, pos, SEEK_SET) < 0) {
  117. fprintf(stderr, "%s: %s: seek to %" PRIx64
  118. " failed: %s\n",
  119. argv[0], argv[1], pos, strerror(errno));
  120. }
  121. if (fread(&rela, sizeof(rela), 1, f) != 1) {
  122. fprintf(stderr, "%s: %s: read rela failed at %"
  123. PRIx64 "\n",
  124. argv[0], argv[1], pos);
  125. return 4;
  126. }
  127. swrela.r_offset = le64(rela.r_offset);
  128. swrela.r_info = le64(rela.r_info);
  129. swrela.r_addend = le64(rela.r_addend);
  130. if (!supported_rela(&swrela))
  131. continue;
  132. debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
  133. swrela.r_offset, swrela.r_info, swrela.r_addend);
  134. if (swrela.r_offset < text_base) {
  135. fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
  136. argv[0], argv[1], pos);
  137. return 4;
  138. }
  139. addr = swrela.r_offset - text_base;
  140. if (fseek(f, addr, SEEK_SET) < 0) {
  141. fprintf(stderr, "%s: %s: seek to %"
  142. PRIx64 " failed: %s\n",
  143. argv[0], argv[1], addr, strerror(errno));
  144. }
  145. if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
  146. fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
  147. argv[0], argv[1], addr);
  148. return 4;
  149. }
  150. }
  151. if (fclose(f) < 0) {
  152. fprintf(stderr, "%s: %s: close failed: %s\n",
  153. argv[0], argv[1], strerror(errno));
  154. return 4;
  155. }
  156. return 0;
  157. }