blob: fcf3fb201f07c5e6b18f76cb8ea7cf7b9d5cd9b3 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
Scott Wood49fa0592013-12-14 11:47:32 +08002/*
3 * Copyright 2013 Freescale Semiconductor, Inc.
4 *
Scott Wood49fa0592013-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 Gray9dd92042016-12-11 14:51:13 +110017#include "compiler.h"
Scott Wood49fa0592013-12-14 11:47:32 +080018
19#ifndef R_AARCH64_RELATIVE
20#define R_AARCH64_RELATIVE 1027
21#endif
22
Michal Simekef320202022-06-24 14:14:59 +020023static int ei_class;
24
Michal Simek4fad25f2022-06-24 14:15:00 +020025static uint64_t rela_start, rela_end, text_base, dyn_start;
Michal Simek6944cec2022-06-24 14:14:59 +020026
Scott Wood49fa0592013-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.de5cdd5392017-05-03 22:40:11 +020033 if (debug_en) {
34 va_start(args, fmt);
Scott Wood49fa0592013-12-14 11:47:32 +080035 vprintf(fmt, args);
xypron.glpk@gmx.de5cdd5392017-05-03 22:40:11 +020036 va_end(args);
37 }
Scott Wood49fa0592013-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 }
Michal Simekef320202022-06-24 14:14:59 +020057}
58
59static int decode_elf64(FILE *felf, char **argv)
60{
61 size_t size;
62 Elf64_Ehdr header;
Samuel Hollandd0517862022-07-15 01:40:25 -050063 uint64_t section_header_base, section_header_size;
64 uint64_t sh_addr, sh_offset, sh_size;
65 Elf64_Half sh_index, sh_num;
Michal Simekef320202022-06-24 14:14:59 +020066 Elf64_Shdr *sh_table; /* Elf symbol table */
67 int ret, i, machine;
68 char *sh_str;
69
70 debug("64bit version\n");
71
72 /* Make sure we are at start */
73 rewind(felf);
74
75 size = fread(&header, 1, sizeof(header), felf);
76 if (size != sizeof(header)) {
77 fclose(felf);
78 return 25;
79 }
80
Samuel Hollandd0517862022-07-15 01:40:25 -050081 machine = le16_to_cpu(header.e_machine);
Michal Simekef320202022-06-24 14:14:59 +020082 debug("Machine\t%d\n", machine);
83
Michal Simek97d50302022-06-24 14:15:00 +020084 if (machine != EM_AARCH64) {
85 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
86 return 30;
87 }
88
Samuel Hollandd0517862022-07-15 01:40:25 -050089 text_base = le64_to_cpu(header.e_entry);
90 section_header_base = le64_to_cpu(header.e_shoff);
91 section_header_size = le16_to_cpu(header.e_shentsize) *
92 le16_to_cpu(header.e_shnum);
Michal Simekef320202022-06-24 14:14:59 +020093
94 sh_table = malloc(section_header_size);
95 if (!sh_table) {
96 fprintf(stderr, "%s: Cannot allocate space for section header\n",
97 argv[0]);
98 fclose(felf);
99 return 26;
100 }
101
102 ret = fseek(felf, section_header_base, SEEK_SET);
103 if (ret) {
104 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
105 argv[0], ret, section_header_base);
106 free(sh_table);
107 fclose(felf);
108 return 26;
109 }
110
111 size = fread(sh_table, 1, section_header_size, felf);
112 if (size != section_header_size) {
113 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
114 argv[0], size, section_header_size);
115 free(sh_table);
116 fclose(felf);
117 return 27;
118 }
119
Samuel Hollandd0517862022-07-15 01:40:25 -0500120 sh_index = le16_to_cpu(header.e_shstrndx);
121 sh_size = le64_to_cpu(sh_table[sh_index].sh_size);
122 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
Michal Simekef320202022-06-24 14:14:59 +0200123
124 sh_str = malloc(sh_size);
125 if (!sh_str) {
126 fprintf(stderr, "malloc failed\n");
127 free(sh_table);
128 fclose(felf);
129 return 28;
130 }
131
132 /*
133 * Specifies the byte offset from the beginning of the file
134 * to the first byte in the section.
135 */
Samuel Hollandd0517862022-07-15 01:40:25 -0500136 sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
137 sh_num = le16_to_cpu(header.e_shnum);
Michal Simekef320202022-06-24 14:14:59 +0200138
139 ret = fseek(felf, sh_offset, SEEK_SET);
140 if (ret) {
141 fprintf(stderr, "Setting up sh_offset failed\n");
142 free(sh_str);
143 free(sh_table);
144 fclose(felf);
145 return 29;
146 }
147
148 size = fread(sh_str, 1, sh_size, felf);
149 if (size != sh_size) {
150 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
151 argv[0], size, sh_size);
152 free(sh_str);
153 free(sh_table);
154 fclose(felf);
155 return 30;
156 }
157
Samuel Hollandd0517862022-07-15 01:40:25 -0500158 for (i = 0; i < sh_num; i++) {
159 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
160
161 debug("%s\n", sh_name);
162
163 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
164 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
165 sh_size = le64_to_cpu(sh_table[i].sh_size);
166
167 if (!strcmp(".rela.dyn", sh_name)) {
Michal Simekef320202022-06-24 14:14:59 +0200168 debug("Found section\t\".rela_dyn\"\n");
Samuel Hollandd0517862022-07-15 01:40:25 -0500169 debug(" at addr\t0x%08x\n", sh_addr);
170 debug(" at offset\t0x%08x\n", sh_offset);
171 debug(" of size\t0x%08x\n", sh_size);
172 rela_start = sh_addr;
173 rela_end = rela_start + sh_size;
Michal Simekef320202022-06-24 14:14:59 +0200174 break;
175 }
176 }
177
178 /* Clean up */
179 free(sh_str);
180 free(sh_table);
181 fclose(felf);
182
183 debug("text_base\t0x%08lx\n", text_base);
184 debug("rela_start\t0x%08lx\n", rela_start);
185 debug("rela_end\t0x%08lx\n", rela_end);
186
187 if (!rela_start)
188 return 1;
189
190 return 0;
Scott Wood49fa0592013-12-14 11:47:32 +0800191}
192
Michal Simek4fad25f2022-06-24 14:15:00 +0200193static int decode_elf32(FILE *felf, char **argv)
194{
195 size_t size;
196 Elf32_Ehdr header;
Samuel Hollandd0517862022-07-15 01:40:25 -0500197 uint64_t section_header_base, section_header_size;
198 uint32_t sh_addr, sh_offset, sh_size;
199 Elf32_Half sh_index, sh_num;
Michal Simek4fad25f2022-06-24 14:15:00 +0200200 Elf32_Shdr *sh_table; /* Elf symbol table */
201 int ret, i, machine;
202 char *sh_str;
203
204 debug("32bit version\n");
205
206 /* Make sure we are at start */
207 rewind(felf);
208
209 size = fread(&header, 1, sizeof(header), felf);
210 if (size != sizeof(header)) {
211 fclose(felf);
212 return 25;
213 }
214
Samuel Hollandd0517862022-07-15 01:40:25 -0500215 machine = le16_to_cpu(header.e_machine);
Michal Simek4fad25f2022-06-24 14:15:00 +0200216 debug("Machine %d\n", machine);
217
218 if (machine != EM_MICROBLAZE) {
219 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
220 return 30;
221 }
222
Samuel Hollandd0517862022-07-15 01:40:25 -0500223 text_base = le32_to_cpu(header.e_entry);
224 section_header_base = le32_to_cpu(header.e_shoff);
225 section_header_size = le16_to_cpu(header.e_shentsize) *
226 le16_to_cpu(header.e_shnum);
Michal Simek4fad25f2022-06-24 14:15:00 +0200227
228 sh_table = malloc(section_header_size);
229 if (!sh_table) {
230 fprintf(stderr, "%s: Cannot allocate space for section header\n",
231 argv[0]);
232 fclose(felf);
233 return 26;
234 }
235
236 ret = fseek(felf, section_header_base, SEEK_SET);
237 if (ret) {
238 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
239 argv[0], ret, section_header_base);
240 free(sh_table);
241 fclose(felf);
242 return 26;
243 }
244
245 size = fread(sh_table, 1, section_header_size, felf);
246 if (size != section_header_size) {
247 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
248 argv[0], size, section_header_size);
249 free(sh_table);
250 fclose(felf);
251 return 27;
252 }
253
Samuel Hollandd0517862022-07-15 01:40:25 -0500254 sh_index = le16_to_cpu(header.e_shstrndx);
255 sh_size = le32_to_cpu(sh_table[sh_index].sh_size);
256 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
Michal Simek4fad25f2022-06-24 14:15:00 +0200257
258 sh_str = malloc(sh_size);
259 if (!sh_str) {
260 fprintf(stderr, "malloc failed\n");
261 free(sh_table);
262 fclose(felf);
263 return 28;
264 }
265
266 /*
267 * Specifies the byte offset from the beginning of the file
268 * to the first byte in the section.
269 */
Samuel Hollandd0517862022-07-15 01:40:25 -0500270 sh_offset = le32_to_cpu(sh_table[sh_index].sh_offset);
271 sh_num = le16_to_cpu(header.e_shnum);
Michal Simek4fad25f2022-06-24 14:15:00 +0200272
273 ret = fseek(felf, sh_offset, SEEK_SET);
274 if (ret) {
275 fprintf(stderr, "Setting up sh_offset failed\n");
276 free(sh_str);
277 free(sh_table);
278 fclose(felf);
279 return 29;
280 }
281
282 size = fread(sh_str, 1, sh_size, felf);
283 if (size != sh_size) {
Samuel Hollandd0517862022-07-15 01:40:25 -0500284 fprintf(stderr, "%s: Can't read section: %lx/%x\n",
Michal Simek4fad25f2022-06-24 14:15:00 +0200285 argv[0], size, sh_size);
286 free(sh_str);
287 free(sh_table);
288 fclose(felf);
289 return 30;
290 }
291
Samuel Hollandd0517862022-07-15 01:40:25 -0500292 for (i = 0; i < sh_num; i++) {
293 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
294
295 debug("%s\n", sh_name);
296
297 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
298 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
299 sh_size = le64_to_cpu(sh_table[i].sh_size);
300
301 if (!strcmp(".rela.dyn", sh_name)) {
Michal Simek4fad25f2022-06-24 14:15:00 +0200302 debug("Found section\t\".rela_dyn\"\n");
Samuel Hollandd0517862022-07-15 01:40:25 -0500303 debug(" at addr\t0x%08x\n", sh_addr);
304 debug(" at offset\t0x%08x\n", sh_offset);
305 debug(" of size\t0x%08x\n", sh_size);
306 rela_start = sh_addr;
307 rela_end = rela_start + sh_size;
Michal Simek4fad25f2022-06-24 14:15:00 +0200308 }
Samuel Hollandd0517862022-07-15 01:40:25 -0500309 if (!strcmp(".dynsym", sh_name)) {
Michal Simek4fad25f2022-06-24 14:15:00 +0200310 debug("Found section\t\".dynsym\"\n");
Samuel Hollandd0517862022-07-15 01:40:25 -0500311 debug(" at addr\t0x%08x\n", sh_addr);
312 debug(" at offset\t0x%08x\n", sh_offset);
313 debug(" of size\t0x%08x\n", sh_size);
314 dyn_start = sh_addr;
Michal Simek4fad25f2022-06-24 14:15:00 +0200315 }
316 }
317
318 /* Clean up */
319 free(sh_str);
320 free(sh_table);
321 fclose(felf);
322
323 debug("text_base\t0x%08lx\n", text_base);
324 debug("rela_start\t0x%08lx\n", rela_start);
325 debug("rela_end\t0x%08lx\n", rela_end);
326 debug("dyn_start\t0x%08lx\n", dyn_start);
327
328 if (!rela_start)
329 return 1;
330
331 return 0;
332}
333
Michal Simekef320202022-06-24 14:14:59 +0200334static int decode_elf(char **argv)
Scott Wood49fa0592013-12-14 11:47:32 +0800335{
Michal Simekef320202022-06-24 14:14:59 +0200336 FILE *felf;
337 size_t size;
338 unsigned char e_ident[EI_NIDENT];
339
340 felf = fopen(argv[2], "r+b");
341 if (!felf) {
342 fprintf(stderr, "%s: Cannot open %s: %s\n",
343 argv[0], argv[5], strerror(errno));
344 return 2;
345 }
346
347 size = fread(e_ident, 1, EI_NIDENT, felf);
348 if (size != EI_NIDENT) {
349 fclose(felf);
350 return 25;
351 }
352
353 /* Check if this is really ELF file */
354 if (e_ident[0] != 0x7f &&
355 e_ident[1] != 'E' &&
356 e_ident[2] != 'L' &&
357 e_ident[3] != 'F') {
358 fclose(felf);
359 return 1;
360 }
361
362 ei_class = e_ident[4];
363 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
364
365 if (ei_class == 2)
366 return decode_elf64(felf, argv);
367
Michal Simek4fad25f2022-06-24 14:15:00 +0200368 return decode_elf32(felf, argv);
Scott Wood49fa0592013-12-14 11:47:32 +0800369}
370
Michal Simekf9c28a12022-06-24 14:15:00 +0200371static int rela_elf64(char **argv, FILE *f)
Scott Wood49fa0592013-12-14 11:47:32 +0800372{
Michal Simekf9c28a12022-06-24 14:15:00 +0200373 int i, num;
Alistair Delva5321bfe2021-10-20 21:31:32 +0000374
375 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
376 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
377 return 3;
378 }
379
Scott Wood49fa0592013-12-14 11:47:32 +0800380 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
381
382 for (i = 0; i < num; i++) {
383 Elf64_Rela rela, swrela;
384 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
385 uint64_t addr;
386
387 if (fseek(f, pos, SEEK_SET) < 0) {
388 fprintf(stderr, "%s: %s: seek to %" PRIx64
389 " failed: %s\n",
390 argv[0], argv[1], pos, strerror(errno));
391 }
392
393 if (fread(&rela, sizeof(rela), 1, f) != 1) {
394 fprintf(stderr, "%s: %s: read rela failed at %"
395 PRIx64 "\n",
396 argv[0], argv[1], pos);
397 return 4;
398 }
399
Samuel Hollandd0517862022-07-15 01:40:25 -0500400 swrela.r_offset = le64_to_cpu(rela.r_offset);
401 swrela.r_info = le64_to_cpu(rela.r_info);
402 swrela.r_addend = le64_to_cpu(rela.r_addend);
Scott Wood49fa0592013-12-14 11:47:32 +0800403
404 if (!supported_rela(&swrela))
405 continue;
406
407 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
408 swrela.r_offset, swrela.r_info, swrela.r_addend);
409
410 if (swrela.r_offset < text_base) {
411 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
412 argv[0], argv[1], pos);
413 return 4;
414 }
415
416 addr = swrela.r_offset - text_base;
417
418 if (fseek(f, addr, SEEK_SET) < 0) {
419 fprintf(stderr, "%s: %s: seek to %"
420 PRIx64 " failed: %s\n",
421 argv[0], argv[1], addr, strerror(errno));
422 }
423
424 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
425 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
426 argv[0], argv[1], addr);
427 return 4;
428 }
429 }
430
Michal Simekf9c28a12022-06-24 14:15:00 +0200431 return 0;
432}
433
Michal Simek39b2b6c2022-06-24 14:15:00 +0200434static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
435{
436 uint32_t mask = 0xffULL; /* would be different on 32-bit */
437 *type = rela->r_info & mask;
438
439 debug("Type:\t");
440
441 switch (*type) {
442 case R_MICROBLAZE_32:
443 debug("R_MICROBLAZE_32\n");
444 return true;
445 case R_MICROBLAZE_GLOB_DAT:
446 debug("R_MICROBLAZE_GLOB_DAT\n");
447 return true;
448 case R_MICROBLAZE_NONE:
449 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
450 return false;
451 case R_MICROBLAZE_REL:
452 debug("R_MICROBLAZE_REL\n");
453 return true;
454 default:
455 fprintf(stderr, "warning: unsupported relocation type %"
456 PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
457
458 return false;
459 }
460}
461
462static int rela_elf32(char **argv, FILE *f)
463{
464 int i, num, index;
465 uint32_t value, type;
466
467 if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
468 fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
469 return 3;
470 }
471
472 num = (rela_end - rela_start) / sizeof(Elf32_Rela);
473
474 debug("Number of entries: %u\n", num);
475
476 for (i = 0; i < num; i++) {
477 Elf32_Rela rela, swrela;
478 Elf32_Sym symbols;
479 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
480 uint32_t addr, pos_dyn;
481
482 debug("\nPossition:\t%d/0x%x\n", i, pos);
483
484 if (fseek(f, pos, SEEK_SET) < 0) {
485 fprintf(stderr, "%s: %s: seek to %" PRIx32
486 " failed: %s\n",
487 argv[0], argv[1], pos, strerror(errno));
488 }
489
490 if (fread(&rela, sizeof(rela), 1, f) != 1) {
491 fprintf(stderr, "%s: %s: read rela failed at %"
492 PRIx32 "\n",
493 argv[0], argv[1], pos);
494 return 4;
495 }
496
497 debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
498 PRIu32 " r_addend:\t%" PRIx32 "\n",
499 rela.r_offset, rela.r_info, rela.r_addend);
500
Samuel Hollandd0517862022-07-15 01:40:25 -0500501 swrela.r_offset = le32_to_cpu(rela.r_offset);
502 swrela.r_info = le32_to_cpu(rela.r_info);
503 swrela.r_addend = le32_to_cpu(rela.r_addend);
Michal Simek39b2b6c2022-06-24 14:15:00 +0200504
505 debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
506 PRIu32 " r_addend:\t%" PRIx32 "\n",
507 swrela.r_offset, swrela.r_info, swrela.r_addend);
508
509 if (!supported_rela32(&swrela, &type))
510 continue;
511
512 if (swrela.r_offset < text_base) {
513 fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
514 argv[0], argv[1], pos);
515 return 4;
516 }
517
518 addr = swrela.r_offset - text_base;
519
520 debug("Addr:\t0x%" PRIx32 "\n", addr);
521
522 switch (type) {
523 case R_MICROBLAZE_REL:
524 if (fseek(f, addr, SEEK_SET) < 0) {
525 fprintf(stderr, "%s: %s: seek to %"
526 PRIx32 " failed: %s\n",
527 argv[0], argv[1], addr, strerror(errno));
528 return 5;
529 }
530
531 debug("Write addend\n");
532
533 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
534 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
535 argv[0], argv[1], addr);
536 return 4;
537 }
538 break;
539 case R_MICROBLAZE_32:
540 case R_MICROBLAZE_GLOB_DAT:
541 /* global symbols read it and add reloc offset */
542 index = swrela.r_info >> 8;
543 pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
544
545 debug("Index:\t%d\n", index);
546 debug("Pos_dyn:\t0x%x\n", pos_dyn);
547
548 if (fseek(f, pos_dyn, SEEK_SET) < 0) {
549 fprintf(stderr, "%s: %s: seek to %"
550 PRIx32 " failed: %s\n",
551 argv[0], argv[1], pos_dyn, strerror(errno));
552 return 5;
553 }
554
555 if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
556 fprintf(stderr, "%s: %s: read symbols failed at %"
557 PRIx32 "\n",
558 argv[0], argv[1], pos_dyn);
559 return 4;
560 }
561
562 debug("Symbol description:\n");
563 debug(" st_name:\t0x%x\n", symbols.st_name);
564 debug(" st_value:\t0x%x\n", symbols.st_value);
565 debug(" st_size:\t0x%x\n", symbols.st_size);
566
567 value = swrela.r_addend + symbols.st_value;
568
569 debug("Value:\t0x%x\n", value);
570
571 if (fseek(f, addr, SEEK_SET) < 0) {
572 fprintf(stderr, "%s: %s: seek to %"
573 PRIx32 " failed: %s\n",
574 argv[0], argv[1], addr, strerror(errno));
575 return 5;
576 }
577
578 if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
579 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
580 argv[0], argv[1], addr);
581 return 4;
582 }
583
584 break;
585 case R_MICROBLAZE_NONE:
586 debug("R_MICROBLAZE_NONE - skip\n");
587 break;
588 default:
589 fprintf(stderr, "warning: unsupported relocation type %"
590 PRIu32 " at %" PRIx32 "\n",
591 type, rela.r_offset);
592 }
593 }
594
595 return 0;
596}
597
Michal Simekf9c28a12022-06-24 14:15:00 +0200598int main(int argc, char **argv)
599{
600 FILE *f;
601 int ret;
602 uint64_t file_size;
603
604 if (argc != 3) {
605 fprintf(stderr, "Statically apply ELF rela relocations\n");
606 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
607 argv[0]);
608 return 1;
609 }
610
611 ret = decode_elf(argv);
612 if (ret) {
613 fprintf(stderr, "ELF decoding failed\n");
614 return ret;
615 }
616
617 if (rela_start > rela_end || rela_start < text_base) {
618 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
619 return 3;
620 }
621
622 rela_start -= text_base;
623 rela_end -= text_base;
Michal Simek4fad25f2022-06-24 14:15:00 +0200624 dyn_start -= text_base;
Michal Simekf9c28a12022-06-24 14:15:00 +0200625
626 f = fopen(argv[1], "r+b");
627 if (!f) {
628 fprintf(stderr, "%s: Cannot open %s: %s\n",
629 argv[0], argv[1], strerror(errno));
630 return 2;
631 }
632
633 fseek(f, 0, SEEK_END);
634 file_size = ftell(f);
635 rewind(f);
636
637 if (rela_end > file_size) {
638 // Most likely compiler inserted some section that didn't get
639 // objcopy-ed into the final binary
640 rela_end = file_size;
641 }
642
643 if (ei_class == 2)
644 ret = rela_elf64(argv, f);
Michal Simek39b2b6c2022-06-24 14:15:00 +0200645 else
646 ret = rela_elf32(argv, f);
Michal Simekf9c28a12022-06-24 14:15:00 +0200647
Scott Wood49fa0592013-12-14 11:47:32 +0800648 if (fclose(f) < 0) {
649 fprintf(stderr, "%s: %s: close failed: %s\n",
650 argv[0], argv[1], strerror(errno));
651 return 4;
652 }
653
Michal Simekf9c28a12022-06-24 14:15:00 +0200654 return ret;
Scott Wood49fa0592013-12-14 11:47:32 +0800655}