prelink-riscv.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <errno.h>
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <elf.h>
  17. #include <fcntl.h>
  18. #include <sys/mman.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <compiler.h>
  23. #ifndef EM_RISCV
  24. #define EM_RISCV 243
  25. #endif
  26. #ifndef R_RISCV_32
  27. #define R_RISCV_32 1
  28. #endif
  29. #ifndef R_RISCV_64
  30. #define R_RISCV_64 2
  31. #endif
  32. #ifndef R_RISCV_RELATIVE
  33. #define R_RISCV_RELATIVE 3
  34. #endif
  35. const char *argv0;
  36. #define die(fmt, ...) \
  37. do { \
  38. fprintf(stderr, "%s: " fmt "\n", argv0, ## __VA_ARGS__); \
  39. exit(EXIT_FAILURE); \
  40. } while (0)
  41. #define PRELINK_BYTEORDER le
  42. #define PRELINK_INC_BITS 32
  43. #include "prelink-riscv.inc"
  44. #undef PRELINK_BYTEORDER
  45. #undef PRELINK_INC_BITS
  46. #define PRELINK_BYTEORDER le
  47. #define PRELINK_INC_BITS 64
  48. #include "prelink-riscv.inc"
  49. #undef PRELINK_BYTEORDER
  50. #undef PRELINK_INC_BITS
  51. #define PRELINK_BYTEORDER be
  52. #define PRELINK_INC_BITS 32
  53. #include "prelink-riscv.inc"
  54. #undef PRELINK_BYTEORDER
  55. #undef PRELINK_INC_BITS
  56. #define PRELINK_BYTEORDER be
  57. #define PRELINK_INC_BITS 64
  58. #include "prelink-riscv.inc"
  59. #undef PRELINK_BYTEORDER
  60. #undef PRELINK_INC_BITS
  61. int main(int argc, const char *const *argv)
  62. {
  63. argv0 = argv[0];
  64. if (argc < 2) {
  65. fprintf(stderr, "Usage: %s <u-boot>\n", argv0);
  66. exit(EXIT_FAILURE);
  67. }
  68. int fd = open(argv[1], O_RDWR, 0);
  69. if (fd < 0)
  70. die("Cannot open %s: %s", argv[1], strerror(errno));
  71. struct stat st;
  72. if (fstat(fd, &st) < 0)
  73. die("Cannot stat %s: %s", argv[1], strerror(errno));
  74. void *data =
  75. mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  76. if (data == MAP_FAILED)
  77. die("Cannot mmap %s: %s", argv[1], strerror(errno));
  78. close(fd);
  79. unsigned char *e_ident = (unsigned char *)data;
  80. if (memcmp(e_ident, ELFMAG, SELFMAG) != 0)
  81. die("Invalid ELF file %s", argv[1]);
  82. bool is64 = e_ident[EI_CLASS] == ELFCLASS64;
  83. bool isbe = e_ident[EI_DATA] == ELFDATA2MSB;
  84. if (is64) {
  85. if (isbe)
  86. prelink_be64(data);
  87. else
  88. prelink_le64(data);
  89. } else {
  90. if (isbe)
  91. prelink_be32(data);
  92. else
  93. prelink_le32(data);
  94. }
  95. return 0;
  96. }