blob: e62247d51e2a903b1562984ba02d8dce09f4e8a1 [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 Simek4c9e2d62022-06-24 14:14:59 +020023static int ei_class;
24
Michal Simekd8b04442022-06-24 14:14:59 +020025static uint64_t rela_start, rela_end, text_base;
26
Scott Wood8137af12013-12-14 11:47:32 +080027static const bool debug_en;
28
29static void debug(const char *fmt, ...)
30{
31 va_list args;
32
xypron.glpk@gmx.ded27e35f2017-05-03 22:40:11 +020033 if (debug_en) {
34 va_start(args, fmt);
Scott Wood8137af12013-12-14 11:47:32 +080035 vprintf(fmt, args);
xypron.glpk@gmx.ded27e35f2017-05-03 22:40:11 +020036 va_end(args);
37 }
Scott Wood8137af12013-12-14 11:47:32 +080038}
39
40static bool supported_rela(Elf64_Rela *rela)
41{
42 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
43 uint32_t type = rela->r_info & mask;
44
45 switch (type) {
46#ifdef R_AARCH64_RELATIVE
47 case R_AARCH64_RELATIVE:
48 return true;
49#endif
50 default:
51 fprintf(stderr, "warning: unsupported relocation type %"
52 PRIu32 " at %" PRIx64 "\n",
53 type, rela->r_offset);
54
55 return false;
56 }
57}
58
Michal Simek4c9e2d62022-06-24 14:14:59 +020059static int decode_elf64(FILE *felf, char **argv)
Scott Wood8137af12013-12-14 11:47:32 +080060{
Michal Simek4c9e2d62022-06-24 14:14:59 +020061 size_t size;
62 Elf64_Ehdr header;
63 uint64_t section_header_base, section_header_size, sh_offset, sh_size;
64 Elf64_Shdr *sh_table; /* Elf symbol table */
65 int ret, i, machine;
66 char *sh_str;
67
68 debug("64bit version\n");
69
70 /* Make sure we are at start */
71 rewind(felf);
72
73 size = fread(&header, 1, sizeof(header), felf);
74 if (size != sizeof(header)) {
75 fclose(felf);
76 return 25;
77 }
78
79 machine = header.e_machine;
80 debug("Machine\t%d\n", machine);
81
82 text_base = header.e_entry;
83 section_header_base = header.e_shoff;
84 section_header_size = header.e_shentsize * header.e_shnum;
85
86 sh_table = malloc(section_header_size);
87 if (!sh_table) {
88 fprintf(stderr, "%s: Cannot allocate space for section header\n",
89 argv[0]);
90 fclose(felf);
91 return 26;
92 }
93
94 ret = fseek(felf, section_header_base, SEEK_SET);
95 if (ret) {
96 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
97 argv[0], ret, section_header_base);
98 free(sh_table);
99 fclose(felf);
100 return 26;
101 }
102
103 size = fread(sh_table, 1, section_header_size, felf);
104 if (size != section_header_size) {
105 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
106 argv[0], size, section_header_size);
107 free(sh_table);
108 fclose(felf);
109 return 27;
110 }
111
112 sh_size = sh_table[header.e_shstrndx].sh_size;
113 debug("e_shstrndx\t0x%08x\n", header.e_shstrndx);
114 debug("sh_size\t\t0x%08lx\n", sh_size);
115
116 sh_str = malloc(sh_size);
117 if (!sh_str) {
118 fprintf(stderr, "malloc failed\n");
119 free(sh_table);
120 fclose(felf);
121 return 28;
122 }
123
124 /*
125 * Specifies the byte offset from the beginning of the file
126 * to the first byte in the section.
127 */
128 sh_offset = sh_table[header.e_shstrndx].sh_offset;
129
130 debug("sh_offset\t0x%08x\n", header.e_shnum);
131
132 ret = fseek(felf, sh_offset, SEEK_SET);
133 if (ret) {
134 fprintf(stderr, "Setting up sh_offset failed\n");
135 free(sh_str);
136 free(sh_table);
137 fclose(felf);
138 return 29;
139 }
140
141 size = fread(sh_str, 1, sh_size, felf);
142 if (size != sh_size) {
143 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
144 argv[0], size, sh_size);
145 free(sh_str);
146 free(sh_table);
147 fclose(felf);
148 return 30;
149 }
150
151 for (i = 0; i < header.e_shnum; i++) {
152 /* fprintf(stderr, "%s\n", sh_str + sh_table[i].sh_name); Debug only */
153 if (!strcmp(".rela.dyn", (sh_str + sh_table[i].sh_name))) {
154 debug("Found section\t\".rela_dyn\"\n");
155 debug(" at addr\t0x%08x\n",
156 (unsigned int)sh_table[i].sh_addr);
157 debug(" at offset\t0x%08x\n",
158 (unsigned int)sh_table[i].sh_offset);
159 debug(" of size\t0x%08x\n",
160 (unsigned int)sh_table[i].sh_size);
161 rela_start = sh_table[i].sh_addr;
162 rela_end = rela_start + sh_table[i].sh_size;
163 break;
164 }
165 }
166
167 /* Clean up */
168 free(sh_str);
169 free(sh_table);
170 fclose(felf);
171
172 debug("text_base\t0x%08lx\n", text_base);
173 debug("rela_start\t0x%08lx\n", rela_start);
174 debug("rela_end\t0x%08lx\n", rela_end);
175
176 if (!rela_start)
177 return 1;
178
179 return 0;
180}
181
182static int decode_elf(char **argv)
183{
184 FILE *felf;
185 size_t size;
186 unsigned char e_ident[EI_NIDENT];
187
188 felf = fopen(argv[2], "r+b");
189 if (!felf) {
190 fprintf(stderr, "%s: Cannot open %s: %s\n",
191 argv[0], argv[5], strerror(errno));
192 return 2;
193 }
194
195 size = fread(e_ident, 1, EI_NIDENT, felf);
196 if (size != EI_NIDENT) {
197 fclose(felf);
198 return 25;
199 }
200
201 /* Check if this is really ELF file */
202 if (e_ident[0] != 0x7f &&
203 e_ident[1] != 'E' &&
204 e_ident[2] != 'L' &&
205 e_ident[3] != 'F') {
206 fclose(felf);
207 return 1;
208 }
209
210 ei_class = e_ident[4];
211 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
212
213 if (ei_class == 2)
214 return decode_elf64(felf, argv);
215
216 return 1;
Scott Wood8137af12013-12-14 11:47:32 +0800217}
218
Michal Simek582ffb52022-06-24 14:15:00 +0200219static int rela_elf64(char **argv, FILE *f)
Scott Wood8137af12013-12-14 11:47:32 +0800220{
Michal Simek582ffb52022-06-24 14:15:00 +0200221 int i, num;
Alistair Delva9d3d9812021-10-20 21:31:32 +0000222
223 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
224 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
225 return 3;
226 }
227
Scott Wood8137af12013-12-14 11:47:32 +0800228 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
229
230 for (i = 0; i < num; i++) {
231 Elf64_Rela rela, swrela;
232 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
233 uint64_t addr;
234
235 if (fseek(f, pos, SEEK_SET) < 0) {
236 fprintf(stderr, "%s: %s: seek to %" PRIx64
237 " failed: %s\n",
238 argv[0], argv[1], pos, strerror(errno));
239 }
240
241 if (fread(&rela, sizeof(rela), 1, f) != 1) {
242 fprintf(stderr, "%s: %s: read rela failed at %"
243 PRIx64 "\n",
244 argv[0], argv[1], pos);
245 return 4;
246 }
247
Jonathan Gray43db3e32016-12-11 14:51:13 +1100248 swrela.r_offset = cpu_to_le64(rela.r_offset);
249 swrela.r_info = cpu_to_le64(rela.r_info);
250 swrela.r_addend = cpu_to_le64(rela.r_addend);
Scott Wood8137af12013-12-14 11:47:32 +0800251
252 if (!supported_rela(&swrela))
253 continue;
254
255 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
256 swrela.r_offset, swrela.r_info, swrela.r_addend);
257
258 if (swrela.r_offset < text_base) {
259 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
260 argv[0], argv[1], pos);
261 return 4;
262 }
263
264 addr = swrela.r_offset - text_base;
265
266 if (fseek(f, addr, SEEK_SET) < 0) {
267 fprintf(stderr, "%s: %s: seek to %"
268 PRIx64 " failed: %s\n",
269 argv[0], argv[1], addr, strerror(errno));
270 }
271
272 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
273 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
274 argv[0], argv[1], addr);
275 return 4;
276 }
277 }
278
Michal Simek582ffb52022-06-24 14:15:00 +0200279 return 0;
280}
281
282int main(int argc, char **argv)
283{
284 FILE *f;
285 int ret;
286 uint64_t file_size;
287
288 if (argc != 3) {
289 fprintf(stderr, "Statically apply ELF rela relocations\n");
290 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
291 argv[0]);
292 return 1;
293 }
294
295 ret = decode_elf(argv);
296 if (ret) {
297 fprintf(stderr, "ELF decoding failed\n");
298 return ret;
299 }
300
301 if (rela_start > rela_end || rela_start < text_base) {
302 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
303 return 3;
304 }
305
306 rela_start -= text_base;
307 rela_end -= text_base;
308
309 f = fopen(argv[1], "r+b");
310 if (!f) {
311 fprintf(stderr, "%s: Cannot open %s: %s\n",
312 argv[0], argv[1], strerror(errno));
313 return 2;
314 }
315
316 fseek(f, 0, SEEK_END);
317 file_size = ftell(f);
318 rewind(f);
319
320 if (rela_end > file_size) {
321 // Most likely compiler inserted some section that didn't get
322 // objcopy-ed into the final binary
323 rela_end = file_size;
324 }
325
326 if (ei_class == 2)
327 ret = rela_elf64(argv, f);
328
Scott Wood8137af12013-12-14 11:47:32 +0800329 if (fclose(f) < 0) {
330 fprintf(stderr, "%s: %s: close failed: %s\n",
331 argv[0], argv[1], strerror(errno));
332 return 4;
333 }
334
Michal Simek582ffb52022-06-24 14:15:00 +0200335 return ret;
Scott Wood8137af12013-12-14 11:47:32 +0800336}