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
28 #define EM_MICROBLAZE 189
31 #ifndef R_MICROBLAZE_NONE
32 #define R_MICROBLAZE_NONE 0
35 #ifndef R_MICROBLAZE_32
36 #define R_MICROBLAZE_32 1
39 #ifndef R_MICROBLAZE_REL
40 #define R_MICROBLAZE_REL 16
43 #ifndef R_MICROBLAZE_GLOB_DAT
44 #define R_MICROBLAZE_GLOB_DAT 18
50 static uint64_t rela_start, rela_end, text_base, dyn_start;
52 static const bool debug_en;
54 static void debug(const char *fmt, ...)
65 static uint16_t elf16_to_cpu(uint16_t data)
67 if (ei_data == ELFDATA2LSB)
68 return le16_to_cpu(data);
70 return be16_to_cpu(data);
73 static uint32_t elf32_to_cpu(uint32_t data)
75 if (ei_data == ELFDATA2LSB)
76 return le32_to_cpu(data);
78 return be32_to_cpu(data);
81 static bool supported_rela(Elf64_Rela *rela)
83 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
84 uint32_t type = rela->r_info & mask;
87 case R_AARCH64_RELATIVE:
90 fprintf(stderr, "warning: unsupported relocation type %"
91 PRIu32 " at %" PRIx64 "\n",
92 type, rela->r_offset);
98 static int decode_elf64(FILE *felf, char **argv)
102 uint64_t section_header_base, section_header_size;
103 uint64_t sh_addr, sh_offset, sh_size;
104 Elf64_Half sh_index, sh_num;
105 Elf64_Shdr *sh_table; /* Elf symbol table */
109 debug("64bit version\n");
111 /* Make sure we are at start */
114 size = fread(&header, 1, sizeof(header), felf);
115 if (size != sizeof(header)) {
120 machine = le16_to_cpu(header.e_machine);
121 debug("Machine\t%d\n", machine);
123 if (machine != EM_AARCH64) {
124 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
128 text_base = le64_to_cpu(header.e_entry);
129 section_header_base = le64_to_cpu(header.e_shoff);
130 section_header_size = le16_to_cpu(header.e_shentsize) *
131 le16_to_cpu(header.e_shnum);
133 sh_table = malloc(section_header_size);
135 fprintf(stderr, "%s: Cannot allocate space for section header\n",
141 ret = fseek(felf, section_header_base, SEEK_SET);
143 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
144 argv[0], ret, section_header_base);
150 size = fread(sh_table, 1, section_header_size, felf);
151 if (size != section_header_size) {
152 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
153 argv[0], size, section_header_size);
159 sh_index = le16_to_cpu(header.e_shstrndx);
160 sh_size = le64_to_cpu(sh_table[sh_index].sh_size);
161 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
163 sh_str = malloc(sh_size);
165 fprintf(stderr, "malloc failed\n");
172 * Specifies the byte offset from the beginning of the file
173 * to the first byte in the section.
175 sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
176 sh_num = le16_to_cpu(header.e_shnum);
178 ret = fseek(felf, sh_offset, SEEK_SET);
180 fprintf(stderr, "Setting up sh_offset failed\n");
187 size = fread(sh_str, 1, sh_size, felf);
188 if (size != sh_size) {
189 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
190 argv[0], size, sh_size);
197 for (i = 0; i < sh_num; i++) {
198 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
200 debug("%s\n", sh_name);
202 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
203 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
204 sh_size = le64_to_cpu(sh_table[i].sh_size);
206 if (!strcmp(".rela.dyn", sh_name)) {
207 debug("Found section\t\".rela_dyn\"\n");
208 debug(" at addr\t0x%08x\n", sh_addr);
209 debug(" at offset\t0x%08x\n", sh_offset);
210 debug(" of size\t0x%08x\n", sh_size);
211 rela_start = sh_addr;
212 rela_end = rela_start + sh_size;
222 debug("text_base\t0x%08lx\n", text_base);
223 debug("rela_start\t0x%08lx\n", rela_start);
224 debug("rela_end\t0x%08lx\n", rela_end);
232 static int decode_elf32(FILE *felf, char **argv)
236 uint64_t section_header_base, section_header_size;
237 uint32_t sh_addr, sh_offset, sh_size;
238 Elf32_Half sh_index, sh_num;
239 Elf32_Shdr *sh_table; /* Elf symbol table */
243 debug("32bit version\n");
245 /* Make sure we are at start */
248 size = fread(&header, 1, sizeof(header), felf);
249 if (size != sizeof(header)) {
254 machine = elf16_to_cpu(header.e_machine);
255 debug("Machine %d\n", machine);
257 if (machine != EM_MICROBLAZE) {
258 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
262 text_base = elf32_to_cpu(header.e_entry);
263 section_header_base = elf32_to_cpu(header.e_shoff);
264 section_header_size = elf16_to_cpu(header.e_shentsize) *
265 elf16_to_cpu(header.e_shnum);
267 sh_table = malloc(section_header_size);
269 fprintf(stderr, "%s: Cannot allocate space for section header\n",
275 ret = fseek(felf, section_header_base, SEEK_SET);
277 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
278 argv[0], ret, section_header_base);
284 size = fread(sh_table, 1, section_header_size, felf);
285 if (size != section_header_size) {
286 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
287 argv[0], size, section_header_size);
293 sh_index = elf16_to_cpu(header.e_shstrndx);
294 sh_size = elf32_to_cpu(sh_table[sh_index].sh_size);
295 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
297 sh_str = malloc(sh_size);
299 fprintf(stderr, "malloc failed\n");
306 * Specifies the byte offset from the beginning of the file
307 * to the first byte in the section.
309 sh_offset = elf32_to_cpu(sh_table[sh_index].sh_offset);
310 sh_num = elf16_to_cpu(header.e_shnum);
312 ret = fseek(felf, sh_offset, SEEK_SET);
314 fprintf(stderr, "Setting up sh_offset failed\n");
321 size = fread(sh_str, 1, sh_size, felf);
322 if (size != sh_size) {
323 fprintf(stderr, "%s: Can't read section: %lx/%x\n",
324 argv[0], size, sh_size);
331 for (i = 0; i < sh_num; i++) {
332 char *sh_name = sh_str + elf32_to_cpu(sh_table[i].sh_name);
334 debug("%s\n", sh_name);
336 sh_addr = elf32_to_cpu(sh_table[i].sh_addr);
337 sh_offset = elf32_to_cpu(sh_table[i].sh_offset);
338 sh_size = elf32_to_cpu(sh_table[i].sh_size);
340 if (!strcmp(".rela.dyn", sh_name)) {
341 debug("Found section\t\".rela_dyn\"\n");
342 debug(" at addr\t0x%08x\n", sh_addr);
343 debug(" at offset\t0x%08x\n", sh_offset);
344 debug(" of size\t0x%08x\n", sh_size);
345 rela_start = sh_addr;
346 rela_end = rela_start + sh_size;
348 if (!strcmp(".dynsym", sh_name)) {
349 debug("Found section\t\".dynsym\"\n");
350 debug(" at addr\t0x%08x\n", sh_addr);
351 debug(" at offset\t0x%08x\n", sh_offset);
352 debug(" of size\t0x%08x\n", sh_size);
362 debug("text_base\t0x%08lx\n", text_base);
363 debug("rela_start\t0x%08lx\n", rela_start);
364 debug("rela_end\t0x%08lx\n", rela_end);
365 debug("dyn_start\t0x%08lx\n", dyn_start);
373 static int decode_elf(char **argv)
377 unsigned char e_ident[EI_NIDENT];
379 felf = fopen(argv[2], "r+b");
381 fprintf(stderr, "%s: Cannot open %s: %s\n",
382 argv[0], argv[5], strerror(errno));
386 size = fread(e_ident, 1, EI_NIDENT, felf);
387 if (size != EI_NIDENT) {
392 /* Check if this is really ELF file */
393 if (e_ident[0] != 0x7f &&
401 ei_class = e_ident[4];
402 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
404 ei_data = e_ident[5];
405 debug("EI_DATA(1=little endian, 2=big endian) %d\n", ei_data);
408 return decode_elf64(felf, argv);
410 return decode_elf32(felf, argv);
413 static int rela_elf64(char **argv, FILE *f)
417 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
418 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
422 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
424 for (i = 0; i < num; i++) {
425 Elf64_Rela rela, swrela;
426 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
429 if (fseek(f, pos, SEEK_SET) < 0) {
430 fprintf(stderr, "%s: %s: seek to %" PRIx64
432 argv[0], argv[1], pos, strerror(errno));
435 if (fread(&rela, sizeof(rela), 1, f) != 1) {
436 fprintf(stderr, "%s: %s: read rela failed at %"
438 argv[0], argv[1], pos);
442 swrela.r_offset = le64_to_cpu(rela.r_offset);
443 swrela.r_info = le64_to_cpu(rela.r_info);
444 swrela.r_addend = le64_to_cpu(rela.r_addend);
446 if (!supported_rela(&swrela))
449 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
450 swrela.r_offset, swrela.r_info, swrela.r_addend);
452 if (swrela.r_offset < text_base) {
453 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
454 argv[0], argv[1], pos);
458 addr = swrela.r_offset - text_base;
460 if (fseek(f, addr, SEEK_SET) < 0) {
461 fprintf(stderr, "%s: %s: seek to %"
462 PRIx64 " failed: %s\n",
463 argv[0], argv[1], addr, strerror(errno));
466 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
467 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
468 argv[0], argv[1], addr);
476 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
478 uint32_t mask = 0xffULL; /* would be different on 32-bit */
479 *type = rela->r_info & mask;
484 case R_MICROBLAZE_32:
485 debug("R_MICROBLAZE_32\n");
487 case R_MICROBLAZE_GLOB_DAT:
488 debug("R_MICROBLAZE_GLOB_DAT\n");
490 case R_MICROBLAZE_NONE:
491 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
493 case R_MICROBLAZE_REL:
494 debug("R_MICROBLAZE_REL\n");
497 fprintf(stderr, "warning: unsupported relocation type %"
498 PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
504 static int rela_elf32(char **argv, FILE *f)
507 uint32_t value, type;
509 if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
510 fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
514 num = (rela_end - rela_start) / sizeof(Elf32_Rela);
516 debug("Number of entries: %u\n", num);
518 for (i = 0; i < num; i++) {
519 Elf32_Rela rela, swrela;
521 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
522 uint32_t addr, pos_dyn;
524 debug("\nPosition:\t%d/0x%x\n", i, pos);
526 if (fseek(f, pos, SEEK_SET) < 0) {
527 fprintf(stderr, "%s: %s: seek to %" PRIx32
529 argv[0], argv[1], pos, strerror(errno));
532 if (fread(&rela, sizeof(rela), 1, f) != 1) {
533 fprintf(stderr, "%s: %s: read rela failed at %"
535 argv[0], argv[1], pos);
539 debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
540 PRIu32 " r_addend:\t%" PRIx32 "\n",
541 rela.r_offset, rela.r_info, rela.r_addend);
543 swrela.r_offset = elf32_to_cpu(rela.r_offset);
544 swrela.r_info = elf32_to_cpu(rela.r_info);
545 swrela.r_addend = elf32_to_cpu(rela.r_addend);
547 debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
548 PRIu32 " r_addend:\t%" PRIx32 "\n",
549 swrela.r_offset, swrela.r_info, swrela.r_addend);
551 if (!supported_rela32(&swrela, &type))
554 if (swrela.r_offset < text_base) {
555 fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
556 argv[0], argv[1], pos);
560 addr = swrela.r_offset - text_base;
562 debug("Addr:\t0x%" PRIx32 "\n", addr);
565 case R_MICROBLAZE_REL:
566 if (fseek(f, addr, SEEK_SET) < 0) {
567 fprintf(stderr, "%s: %s: seek to %"
568 PRIx32 " failed: %s\n",
569 argv[0], argv[1], addr, strerror(errno));
573 debug("Write addend\n");
575 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
576 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
577 argv[0], argv[1], addr);
581 case R_MICROBLAZE_32:
582 case R_MICROBLAZE_GLOB_DAT:
583 /* global symbols read it and add reloc offset */
584 index = swrela.r_info >> 8;
585 pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
587 debug("Index:\t%d\n", index);
588 debug("Pos_dyn:\t0x%x\n", pos_dyn);
590 if (fseek(f, pos_dyn, SEEK_SET) < 0) {
591 fprintf(stderr, "%s: %s: seek to %"
592 PRIx32 " failed: %s\n",
593 argv[0], argv[1], pos_dyn, strerror(errno));
597 if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
598 fprintf(stderr, "%s: %s: read symbols failed at %"
600 argv[0], argv[1], pos_dyn);
604 debug("Symbol description:\n");
605 debug(" st_name:\t0x%x\n", symbols.st_name);
606 debug(" st_value:\t0x%x\n", symbols.st_value);
607 debug(" st_size:\t0x%x\n", symbols.st_size);
609 value = swrela.r_addend + symbols.st_value;
611 debug("Value:\t0x%x\n", value);
613 if (fseek(f, addr, SEEK_SET) < 0) {
614 fprintf(stderr, "%s: %s: seek to %"
615 PRIx32 " failed: %s\n",
616 argv[0], argv[1], addr, strerror(errno));
620 if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
621 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
622 argv[0], argv[1], addr);
627 case R_MICROBLAZE_NONE:
628 debug("R_MICROBLAZE_NONE - skip\n");
631 fprintf(stderr, "warning: unsupported relocation type %"
632 PRIu32 " at %" PRIx32 "\n",
633 type, rela.r_offset);
640 int main(int argc, char **argv)
647 fprintf(stderr, "Statically apply ELF rela relocations\n");
648 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
653 ret = decode_elf(argv);
655 fprintf(stderr, "ELF decoding failed\n");
659 if (rela_start > rela_end || rela_start < text_base) {
660 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
664 rela_start -= text_base;
665 rela_end -= text_base;
666 dyn_start -= text_base;
668 f = fopen(argv[1], "r+b");
670 fprintf(stderr, "%s: Cannot open %s: %s\n",
671 argv[0], argv[1], strerror(errno));
675 fseek(f, 0, SEEK_END);
676 file_size = ftell(f);
679 if (rela_end > file_size) {
680 // Most likely compiler inserted some section that didn't get
681 // objcopy-ed into the final binary
682 rela_end = file_size;
686 ret = rela_elf64(argv, f);
688 ret = rela_elf32(argv, f);
691 fprintf(stderr, "%s: %s: close failed: %s\n",
692 argv[0], argv[1], strerror(errno));