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.
11 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
12 #error "Only little-endian host is supported"
26 #include <sys/types.h>
41 #ifndef R_RISCV_RELATIVE
42 #define R_RISCV_RELATIVE 3
47 #define die(fmt, ...) \
49 fprintf(stderr, "%s: " fmt "\n", argv0, ## __VA_ARGS__); \
53 #define PRELINK_INC_BITS 32
54 #include "prelink-riscv.inc"
55 #undef PRELINK_INC_BITS
57 #define PRELINK_INC_BITS 64
58 #include "prelink-riscv.inc"
59 #undef PRELINK_INC_BITS
61 int main(int argc, const char *const *argv)
66 fprintf(stderr, "Usage: %s <u-boot>\n", argv0);
70 int fd = open(argv[1], O_RDWR, 0);
73 die("Cannot open %s: %s", argv[1], strerror(errno));
77 if (fstat(fd, &st) < 0)
78 die("Cannot stat %s: %s", argv[1], strerror(errno));
81 mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
83 if (data == MAP_FAILED)
84 die("Cannot mmap %s: %s", argv[1], strerror(errno));
88 unsigned char *e_ident = (unsigned char *)data;
90 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0)
91 die("Invalid ELF file %s", argv[1]);
93 bool is64 = e_ident[EI_CLASS] == ELFCLASS64;