1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
3 * Copyright 2013 Freescale Semiconductor, Inc.
5 * 64-bit and little-endian target only until we need to support a different
6 * arch that needs this.
20 #define EM_AARCH64 183
23 #ifndef R_AARCH64_RELATIVE
24 #define R_AARCH64_RELATIVE 1027
39 #ifndef R_68K_GLOB_DAT
40 #define R_68K_GLOB_DAT 20
43 #ifndef R_68K_JMP_SLOT
44 #define R_68K_JMP_SLOT 21
47 #ifndef R_68K_RELATIVE
48 #define R_68K_RELATIVE 22
52 #define EM_MICROBLAZE 189
55 #ifndef R_MICROBLAZE_NONE
56 #define R_MICROBLAZE_NONE 0
59 #ifndef R_MICROBLAZE_32
60 #define R_MICROBLAZE_32 1
63 #ifndef R_MICROBLAZE_REL
64 #define R_MICROBLAZE_REL 16
67 #ifndef R_MICROBLAZE_GLOB_DAT
68 #define R_MICROBLAZE_GLOB_DAT 18
75 static uint64_t rela_start, rela_end, text_base, dyn_start;
77 static const bool debug_en;
79 static void debug(const char *fmt, ...)
90 static uint16_t elf16_to_cpu(uint16_t data)
92 if (ei_data == ELFDATA2LSB)
93 return le16_to_cpu(data);
95 return be16_to_cpu(data);
98 static uint32_t elf32_to_cpu(uint32_t data)
100 if (ei_data == ELFDATA2LSB)
101 return le32_to_cpu(data);
103 return be32_to_cpu(data);
106 static uint32_t cpu_to_elf32(uint32_t data)
108 if (ei_data == ELFDATA2LSB)
109 return cpu_to_le32(data);
111 return cpu_to_be32(data);
114 static bool supported_rela(Elf64_Rela *rela)
116 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
117 uint32_t type = rela->r_info & mask;
120 case R_AARCH64_RELATIVE:
123 fprintf(stderr, "warning: unsupported relocation type %"
124 PRIu32 " at %" PRIx64 "\n",
125 type, rela->r_offset);
131 static int decode_elf64(FILE *felf, char **argv)
135 uint64_t section_header_base, section_header_size;
136 uint64_t sh_addr, sh_offset, sh_size;
137 Elf64_Half sh_index, sh_num;
138 Elf64_Shdr *sh_table; /* Elf symbol table */
142 debug("64bit version\n");
144 /* Make sure we are at start */
147 size = fread(&header, 1, sizeof(header), felf);
148 if (size != sizeof(header)) {
153 machine = le16_to_cpu(header.e_machine);
154 debug("Machine\t%d\n", machine);
156 if (machine != EM_AARCH64) {
157 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
161 text_base = le64_to_cpu(header.e_entry);
162 section_header_base = le64_to_cpu(header.e_shoff);
163 section_header_size = le16_to_cpu(header.e_shentsize) *
164 le16_to_cpu(header.e_shnum);
166 sh_table = malloc(section_header_size);
168 fprintf(stderr, "%s: Cannot allocate space for section header\n",
174 ret = fseek(felf, section_header_base, SEEK_SET);
176 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
177 argv[0], ret, section_header_base);
183 size = fread(sh_table, 1, section_header_size, felf);
184 if (size != section_header_size) {
185 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
186 argv[0], size, section_header_size);
192 sh_index = le16_to_cpu(header.e_shstrndx);
193 sh_size = le64_to_cpu(sh_table[sh_index].sh_size);
194 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
196 sh_str = malloc(sh_size);
198 fprintf(stderr, "malloc failed\n");
205 * Specifies the byte offset from the beginning of the file
206 * to the first byte in the section.
208 sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
209 sh_num = le16_to_cpu(header.e_shnum);
211 ret = fseek(felf, sh_offset, SEEK_SET);
213 fprintf(stderr, "Setting up sh_offset failed\n");
220 size = fread(sh_str, 1, sh_size, felf);
221 if (size != sh_size) {
222 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
223 argv[0], size, sh_size);
230 for (i = 0; i < sh_num; i++) {
231 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
233 debug("%s\n", sh_name);
235 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
236 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
237 sh_size = le64_to_cpu(sh_table[i].sh_size);
239 if (!strcmp(".rela.dyn", sh_name)) {
240 debug("Found section\t\".rela_dyn\"\n");
241 debug(" at addr\t0x%08x\n", sh_addr);
242 debug(" at offset\t0x%08x\n", sh_offset);
243 debug(" of size\t0x%08x\n", sh_size);
244 rela_start = sh_addr;
245 rela_end = rela_start + sh_size;
255 debug("text_base\t0x%08lx\n", text_base);
256 debug("rela_start\t0x%08lx\n", rela_start);
257 debug("rela_end\t0x%08lx\n", rela_end);
265 static int decode_elf32(FILE *felf, char **argv)
269 uint64_t section_header_base, section_header_size;
270 uint32_t sh_addr, sh_offset, sh_size;
271 Elf32_Half sh_index, sh_num;
272 Elf32_Shdr *sh_table; /* Elf symbol table */
276 debug("32bit version\n");
278 /* Make sure we are at start */
281 size = fread(&header, 1, sizeof(header), felf);
282 if (size != sizeof(header)) {
287 machine = elf16_to_cpu(header.e_machine);
288 debug("Machine %d\n", machine);
290 if (machine != EM_MICROBLAZE && machine != EM_M68K) {
291 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
295 text_base = elf32_to_cpu(header.e_entry);
297 * M68K ELF entry point is MONITOR_BASE, not TEXT_BASE.
298 * TEXT_BASE is always MONITOR_BASE &~ 0x7ff, so clear
301 if (machine == EM_M68K)
304 section_header_base = elf32_to_cpu(header.e_shoff);
305 section_header_size = elf16_to_cpu(header.e_shentsize) *
306 elf16_to_cpu(header.e_shnum);
308 sh_table = malloc(section_header_size);
310 fprintf(stderr, "%s: Cannot allocate space for section header\n",
316 ret = fseek(felf, section_header_base, SEEK_SET);
318 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
319 argv[0], ret, section_header_base);
325 size = fread(sh_table, 1, section_header_size, felf);
326 if (size != section_header_size) {
327 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
328 argv[0], size, section_header_size);
334 sh_index = elf16_to_cpu(header.e_shstrndx);
335 sh_size = elf32_to_cpu(sh_table[sh_index].sh_size);
336 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
338 sh_str = malloc(sh_size);
340 fprintf(stderr, "malloc failed\n");
347 * Specifies the byte offset from the beginning of the file
348 * to the first byte in the section.
350 sh_offset = elf32_to_cpu(sh_table[sh_index].sh_offset);
351 sh_num = elf16_to_cpu(header.e_shnum);
353 ret = fseek(felf, sh_offset, SEEK_SET);
355 fprintf(stderr, "Setting up sh_offset failed\n");
362 size = fread(sh_str, 1, sh_size, felf);
363 if (size != sh_size) {
364 fprintf(stderr, "%s: Can't read section: %lx/%x\n",
365 argv[0], size, sh_size);
372 for (i = 0; i < sh_num; i++) {
373 char *sh_name = sh_str + elf32_to_cpu(sh_table[i].sh_name);
375 debug("%s\n", sh_name);
377 sh_addr = elf32_to_cpu(sh_table[i].sh_addr);
378 sh_offset = elf32_to_cpu(sh_table[i].sh_offset);
379 sh_size = elf32_to_cpu(sh_table[i].sh_size);
381 if (!strcmp(".rela.dyn", sh_name)) {
382 debug("Found section\t\".rela_dyn\"\n");
383 debug(" at addr\t0x%08x\n", sh_addr);
384 debug(" at offset\t0x%08x\n", sh_offset);
385 debug(" of size\t0x%08x\n", sh_size);
386 rela_start = sh_addr;
387 rela_end = rela_start + sh_size;
389 if (!strcmp(".dynsym", sh_name)) {
390 debug("Found section\t\".dynsym\"\n");
391 debug(" at addr\t0x%08x\n", sh_addr);
392 debug(" at offset\t0x%08x\n", sh_offset);
393 debug(" of size\t0x%08x\n", sh_size);
403 debug("text_base\t0x%08lx\n", text_base);
404 debug("rela_start\t0x%08lx\n", rela_start);
405 debug("rela_end\t0x%08lx\n", rela_end);
406 debug("dyn_start\t0x%08lx\n", dyn_start);
414 static int decode_elf(char **argv)
418 unsigned char e_ident[EI_NIDENT];
420 felf = fopen(argv[2], "r+b");
422 fprintf(stderr, "%s: Cannot open %s: %s\n",
423 argv[0], argv[5], strerror(errno));
427 size = fread(e_ident, 1, EI_NIDENT, felf);
428 if (size != EI_NIDENT) {
433 /* Check if this is really ELF file */
434 if (e_ident[0] != 0x7f &&
442 ei_class = e_ident[4];
443 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
445 ei_data = e_ident[5];
446 debug("EI_DATA(1=little endian, 2=big endian) %d\n", ei_data);
449 return decode_elf64(felf, argv);
451 return decode_elf32(felf, argv);
454 static int rela_elf64(char **argv, FILE *f)
458 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
459 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
463 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
465 for (i = 0; i < num; i++) {
466 Elf64_Rela rela, swrela;
467 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
470 if (fseek(f, pos, SEEK_SET) < 0) {
471 fprintf(stderr, "%s: %s: seek to %" PRIx64
473 argv[0], argv[1], pos, strerror(errno));
476 if (fread(&rela, sizeof(rela), 1, f) != 1) {
477 fprintf(stderr, "%s: %s: read rela failed at %"
479 argv[0], argv[1], pos);
483 swrela.r_offset = le64_to_cpu(rela.r_offset);
484 swrela.r_info = le64_to_cpu(rela.r_info);
485 swrela.r_addend = le64_to_cpu(rela.r_addend);
487 if (!supported_rela(&swrela))
490 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
491 swrela.r_offset, swrela.r_info, swrela.r_addend);
493 if (swrela.r_offset < text_base) {
494 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
495 argv[0], argv[1], pos);
499 addr = swrela.r_offset - text_base;
501 if (fseek(f, addr, SEEK_SET) < 0) {
502 fprintf(stderr, "%s: %s: seek to %"
503 PRIx64 " failed: %s\n",
504 argv[0], argv[1], addr, strerror(errno));
507 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
508 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
509 argv[0], argv[1], addr);
517 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
519 uint32_t mask = 0xffULL; /* would be different on 32-bit */
520 *type = rela->r_info & mask;
524 if (machine == EM_M68K) {
530 debug("R_68K_GLOB_DAT\n");
533 debug("R_68K_JMP_SLOT\n");
536 debug("R_68K_NONE - ignoring - do nothing\n");
539 debug("R_68K_RELATIVE\n");
544 case R_MICROBLAZE_32:
545 debug("R_MICROBLAZE_32\n");
547 case R_MICROBLAZE_GLOB_DAT:
548 debug("R_MICROBLAZE_GLOB_DAT\n");
550 case R_MICROBLAZE_NONE:
551 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
553 case R_MICROBLAZE_REL:
554 debug("R_MICROBLAZE_REL\n");
558 fprintf(stderr, "warning: unsupported relocation type %"
559 PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
564 static int rela_elf32(char **argv, FILE *f)
567 uint32_t value, type;
569 if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
570 fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
574 num = (rela_end - rela_start) / sizeof(Elf32_Rela);
576 debug("Number of entries: %u\n", num);
578 for (i = 0; i < num; i++) {
579 Elf32_Rela rela, swrela;
581 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
582 uint32_t addr, pos_dyn;
584 debug("\nPosition:\t%d/0x%x\n", i, pos);
586 if (fseek(f, pos, SEEK_SET) < 0) {
587 fprintf(stderr, "%s: %s: seek to %" PRIx32
589 argv[0], argv[1], pos, strerror(errno));
592 if (fread(&rela, sizeof(rela), 1, f) != 1) {
593 fprintf(stderr, "%s: %s: read rela failed at %"
595 argv[0], argv[1], pos);
599 debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
600 PRIu32 " r_addend:\t%" PRIx32 "\n",
601 rela.r_offset, rela.r_info, rela.r_addend);
603 swrela.r_offset = elf32_to_cpu(rela.r_offset);
604 swrela.r_info = elf32_to_cpu(rela.r_info);
605 swrela.r_addend = elf32_to_cpu(rela.r_addend);
607 debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
608 PRIu32 " r_addend:\t%" PRIx32 "\n",
609 swrela.r_offset, swrela.r_info, swrela.r_addend);
611 if (!supported_rela32(&swrela, &type))
614 if (swrela.r_offset < text_base) {
615 fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
616 argv[0], argv[1], pos);
620 addr = swrela.r_offset - text_base;
622 debug("Addr:\t0x%" PRIx32 "\n", addr);
624 if ((machine == EM_M68K && type == R_68K_RELATIVE) ||
625 (machine == EM_MICROBLAZE && type == R_MICROBLAZE_REL)) {
626 if (fseek(f, addr, SEEK_SET) < 0) {
627 fprintf(stderr, "%s: %s: seek to %"
628 PRIx32 " failed: %s\n",
629 argv[0], argv[1], addr, strerror(errno));
633 debug("Write addend\n");
635 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
636 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
637 argv[0], argv[1], addr);
640 } else if ((machine == EM_M68K &&
641 (type == R_68K_32 || type == R_68K_GLOB_DAT ||
642 type == R_68K_JMP_SLOT)) ||
643 (machine == EM_MICROBLAZE &&
644 (type == R_MICROBLAZE_32 ||
645 type == R_MICROBLAZE_GLOB_DAT))) {
646 /* global symbols read it and add reloc offset */
647 index = swrela.r_info >> 8;
648 pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
650 debug("Index:\t%d\n", index);
651 debug("Pos_dyn:\t0x%x\n", pos_dyn);
653 if (fseek(f, pos_dyn, SEEK_SET) < 0) {
654 fprintf(stderr, "%s: %s: seek to %"
655 PRIx32 " failed: %s\n",
656 argv[0], argv[1], pos_dyn, strerror(errno));
660 if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
661 fprintf(stderr, "%s: %s: read symbols failed at %"
663 argv[0], argv[1], pos_dyn);
667 debug("Symbol description:\n");
668 debug(" st_name:\t0x%x\n", elf32_to_cpu(symbols.st_name));
669 debug(" st_value:\t0x%x\n", elf32_to_cpu(symbols.st_value));
670 debug(" st_size:\t0x%x\n", elf32_to_cpu(symbols.st_size));
672 value = swrela.r_addend + elf32_to_cpu(symbols.st_value);
674 debug("Value:\t0x%x\n", value);
676 value = cpu_to_elf32(value);
678 if (fseek(f, addr, SEEK_SET) < 0) {
679 fprintf(stderr, "%s: %s: seek to %"
680 PRIx32 " failed: %s\n",
681 argv[0], argv[1], addr, strerror(errno));
685 if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
686 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
687 argv[0], argv[1], addr);
690 } else if (machine == EM_M68K && type == R_68K_NONE) {
691 debug("R_68K_NONE - skip\n");
692 } else if (machine == EM_MICROBLAZE && type == R_MICROBLAZE_NONE) {
693 debug("R_MICROBLAZE_NONE - skip\n");
695 fprintf(stderr, "warning: unsupported relocation type %"
696 PRIu32 " at %" PRIx32 "\n",
697 type, rela.r_offset);
704 int main(int argc, char **argv)
711 fprintf(stderr, "Statically apply ELF rela relocations\n");
712 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
717 ret = decode_elf(argv);
719 fprintf(stderr, "ELF decoding failed\n");
723 if (rela_start > rela_end || rela_start < text_base) {
724 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
728 rela_start -= text_base;
729 rela_end -= text_base;
730 dyn_start -= text_base;
732 f = fopen(argv[1], "r+b");
734 fprintf(stderr, "%s: Cannot open %s: %s\n",
735 argv[0], argv[1], strerror(errno));
739 fseek(f, 0, SEEK_END);
740 file_size = ftell(f);
743 if (rela_end > file_size) {
744 // Most likely compiler inserted some section that didn't get
745 // objcopy-ed into the final binary
746 rela_end = file_size;
750 ret = rela_elf64(argv, f);
752 ret = rela_elf32(argv, f);
755 fprintf(stderr, "%s: %s: close failed: %s\n",
756 argv[0], argv[1], strerror(errno));