blob: 67a138aa67d7ed1623d3026928fa2760463064e8 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
Scott Wood8137af12013-12-14 11:47:32 +08002/*
3 * Copyright 2013 Freescale Semiconductor, Inc.
4 *
Scott Wood8137af12013-12-14 11:47:32 +08005 * 64-bit and little-endian target only until we need to support a different
6 * arch that needs this.
7 */
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>
Jonathan Gray43db3e32016-12-11 14:51:13 +110017#include "compiler.h"
Scott Wood8137af12013-12-14 11:47:32 +080018
19#ifndef R_AARCH64_RELATIVE
20#define R_AARCH64_RELATIVE 1027
21#endif
22
Michal Simekd8b04442022-06-24 14:14:59 +020023static uint64_t rela_start, rela_end, text_base;
24
Scott Wood8137af12013-12-14 11:47:32 +080025static const bool debug_en;
26
27static void debug(const char *fmt, ...)
28{
29 va_list args;
30
xypron.glpk@gmx.ded27e35f2017-05-03 22:40:11 +020031 if (debug_en) {
32 va_start(args, fmt);
Scott Wood8137af12013-12-14 11:47:32 +080033 vprintf(fmt, args);
xypron.glpk@gmx.ded27e35f2017-05-03 22:40:11 +020034 va_end(args);
35 }
Scott Wood8137af12013-12-14 11:47:32 +080036}
37
38static bool supported_rela(Elf64_Rela *rela)
39{
40 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
41 uint32_t type = rela->r_info & mask;
42
43 switch (type) {
44#ifdef R_AARCH64_RELATIVE
45 case R_AARCH64_RELATIVE:
46 return true;
47#endif
48 default:
49 fprintf(stderr, "warning: unsupported relocation type %"
50 PRIu32 " at %" PRIx64 "\n",
51 type, rela->r_offset);
52
53 return false;
54 }
55}
56
Scott Wood8137af12013-12-14 11:47:32 +080057static bool read_num(const char *str, uint64_t *num)
58{
59 char *endptr;
60 *num = strtoull(str, &endptr, 16);
61 return str[0] && !endptr[0];
62}
63
64int main(int argc, char **argv)
65{
66 FILE *f;
67 int i, num;
Michal Simekd8b04442022-06-24 14:14:59 +020068 uint64_t file_size;
Scott Wood8137af12013-12-14 11:47:32 +080069
70 if (argc != 5) {
71 fprintf(stderr, "Statically apply ELF rela relocations\n");
72 fprintf(stderr, "Usage: %s <bin file> <text base> " \
73 "<rela start> <rela end>\n", argv[0]);
74 fprintf(stderr, "All numbers in hex.\n");
75 return 1;
76 }
77
Scott Wood8137af12013-12-14 11:47:32 +080078 if (!read_num(argv[2], &text_base) ||
79 !read_num(argv[3], &rela_start) ||
80 !read_num(argv[4], &rela_end)) {
81 fprintf(stderr, "%s: bad number\n", argv[0]);
82 return 3;
83 }
84
Alistair Delva9d3d9812021-10-20 21:31:32 +000085 if (rela_start > rela_end || rela_start < text_base) {
Scott Wood8137af12013-12-14 11:47:32 +080086 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
87 return 3;
88 }
89
90 rela_start -= text_base;
91 rela_end -= text_base;
92
Michal Simekfe9d0492022-06-24 14:14:59 +020093 f = fopen(argv[1], "r+b");
94 if (!f) {
95 fprintf(stderr, "%s: Cannot open %s: %s\n",
96 argv[0], argv[1], strerror(errno));
97 return 2;
98 }
99
Alistair Delva9d3d9812021-10-20 21:31:32 +0000100 fseek(f, 0, SEEK_END);
101 file_size = ftell(f);
102 rewind(f);
103
104 if (rela_end > file_size) {
105 // Most likely compiler inserted some section that didn't get
106 // objcopy-ed into the final binary
107 rela_end = file_size;
108 }
109
110 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
111 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
112 return 3;
113 }
114
Scott Wood8137af12013-12-14 11:47:32 +0800115 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
116
117 for (i = 0; i < num; i++) {
118 Elf64_Rela rela, swrela;
119 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
120 uint64_t addr;
121
122 if (fseek(f, pos, SEEK_SET) < 0) {
123 fprintf(stderr, "%s: %s: seek to %" PRIx64
124 " failed: %s\n",
125 argv[0], argv[1], pos, strerror(errno));
126 }
127
128 if (fread(&rela, sizeof(rela), 1, f) != 1) {
129 fprintf(stderr, "%s: %s: read rela failed at %"
130 PRIx64 "\n",
131 argv[0], argv[1], pos);
132 return 4;
133 }
134
Jonathan Gray43db3e32016-12-11 14:51:13 +1100135 swrela.r_offset = cpu_to_le64(rela.r_offset);
136 swrela.r_info = cpu_to_le64(rela.r_info);
137 swrela.r_addend = cpu_to_le64(rela.r_addend);
Scott Wood8137af12013-12-14 11:47:32 +0800138
139 if (!supported_rela(&swrela))
140 continue;
141
142 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
143 swrela.r_offset, swrela.r_info, swrela.r_addend);
144
145 if (swrela.r_offset < text_base) {
146 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
147 argv[0], argv[1], pos);
148 return 4;
149 }
150
151 addr = swrela.r_offset - text_base;
152
153 if (fseek(f, addr, SEEK_SET) < 0) {
154 fprintf(stderr, "%s: %s: seek to %"
155 PRIx64 " failed: %s\n",
156 argv[0], argv[1], addr, strerror(errno));
157 }
158
159 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
160 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
161 argv[0], argv[1], addr);
162 return 4;
163 }
164 }
165
166 if (fclose(f) < 0) {
167 fprintf(stderr, "%s: %s: close failed: %s\n",
168 argv[0], argv[1], strerror(errno));
169 return 4;
170 }
171
172 return 0;
173}