1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Andes Technology
4 * Chih-Mao Chen <cmchen@andestech.com>
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.
22 #include <sys/types.h>
38 #ifndef R_RISCV_RELATIVE
39 #define R_RISCV_RELATIVE 3
44 #define die(fmt, ...) \
46 fprintf(stderr, "%s: " fmt "\n", argv0, ## __VA_ARGS__); \
50 #define PRELINK_BYTEORDER le
51 #define PRELINK_INC_BITS 32
52 #include "prelink-riscv.inc"
53 #undef PRELINK_BYTEORDER
54 #undef PRELINK_INC_BITS
56 #define PRELINK_BYTEORDER le
57 #define PRELINK_INC_BITS 64
58 #include "prelink-riscv.inc"
59 #undef PRELINK_BYTEORDER
60 #undef PRELINK_INC_BITS
62 #define PRELINK_BYTEORDER be
63 #define PRELINK_INC_BITS 32
64 #include "prelink-riscv.inc"
65 #undef PRELINK_BYTEORDER
66 #undef PRELINK_INC_BITS
68 #define PRELINK_BYTEORDER be
69 #define PRELINK_INC_BITS 64
70 #include "prelink-riscv.inc"
71 #undef PRELINK_BYTEORDER
72 #undef PRELINK_INC_BITS
74 int main(int argc, const char *const *argv)
79 fprintf(stderr, "Usage: %s <u-boot>\n", argv0);
83 int fd = open(argv[1], O_RDWR, 0);
86 die("Cannot open %s: %s", argv[1], strerror(errno));
90 if (fstat(fd, &st) < 0)
91 die("Cannot stat %s: %s", argv[1], strerror(errno));
94 mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
96 if (data == MAP_FAILED)
97 die("Cannot mmap %s: %s", argv[1], strerror(errno));
101 unsigned char *e_ident = (unsigned char *)data;
103 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0)
104 die("Invalid ELF file %s", argv[1]);
106 bool is64 = e_ident[EI_CLASS] == ELFCLASS64;
107 bool isbe = e_ident[EI_DATA] == ELFDATA2MSB;