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