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
49 static uint64_t rela_start, rela_end, text_base, dyn_start;
51 static const bool debug_en;
53 static void debug(const char *fmt, ...)
64 static bool supported_rela(Elf64_Rela *rela)
66 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
67 uint32_t type = rela->r_info & mask;
70 case R_AARCH64_RELATIVE:
73 fprintf(stderr, "warning: unsupported relocation type %"
74 PRIu32 " at %" PRIx64 "\n",
75 type, rela->r_offset);
81 static int decode_elf64(FILE *felf, char **argv)
85 uint64_t section_header_base, section_header_size;
86 uint64_t sh_addr, sh_offset, sh_size;
87 Elf64_Half sh_index, sh_num;
88 Elf64_Shdr *sh_table; /* Elf symbol table */
92 debug("64bit version\n");
94 /* Make sure we are at start */
97 size = fread(&header, 1, sizeof(header), felf);
98 if (size != sizeof(header)) {
103 machine = le16_to_cpu(header.e_machine);
104 debug("Machine\t%d\n", machine);
106 if (machine != EM_AARCH64) {
107 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
111 text_base = le64_to_cpu(header.e_entry);
112 section_header_base = le64_to_cpu(header.e_shoff);
113 section_header_size = le16_to_cpu(header.e_shentsize) *
114 le16_to_cpu(header.e_shnum);
116 sh_table = malloc(section_header_size);
118 fprintf(stderr, "%s: Cannot allocate space for section header\n",
124 ret = fseek(felf, section_header_base, SEEK_SET);
126 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
127 argv[0], ret, section_header_base);
133 size = fread(sh_table, 1, section_header_size, felf);
134 if (size != section_header_size) {
135 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
136 argv[0], size, section_header_size);
142 sh_index = le16_to_cpu(header.e_shstrndx);
143 sh_size = le64_to_cpu(sh_table[sh_index].sh_size);
144 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
146 sh_str = malloc(sh_size);
148 fprintf(stderr, "malloc failed\n");
155 * Specifies the byte offset from the beginning of the file
156 * to the first byte in the section.
158 sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
159 sh_num = le16_to_cpu(header.e_shnum);
161 ret = fseek(felf, sh_offset, SEEK_SET);
163 fprintf(stderr, "Setting up sh_offset failed\n");
170 size = fread(sh_str, 1, sh_size, felf);
171 if (size != sh_size) {
172 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
173 argv[0], size, sh_size);
180 for (i = 0; i < sh_num; i++) {
181 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
183 debug("%s\n", sh_name);
185 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
186 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
187 sh_size = le64_to_cpu(sh_table[i].sh_size);
189 if (!strcmp(".rela.dyn", sh_name)) {
190 debug("Found section\t\".rela_dyn\"\n");
191 debug(" at addr\t0x%08x\n", sh_addr);
192 debug(" at offset\t0x%08x\n", sh_offset);
193 debug(" of size\t0x%08x\n", sh_size);
194 rela_start = sh_addr;
195 rela_end = rela_start + sh_size;
205 debug("text_base\t0x%08lx\n", text_base);
206 debug("rela_start\t0x%08lx\n", rela_start);
207 debug("rela_end\t0x%08lx\n", rela_end);
215 static int decode_elf32(FILE *felf, char **argv)
219 uint64_t section_header_base, section_header_size;
220 uint32_t sh_addr, sh_offset, sh_size;
221 Elf32_Half sh_index, sh_num;
222 Elf32_Shdr *sh_table; /* Elf symbol table */
226 debug("32bit version\n");
228 /* Make sure we are at start */
231 size = fread(&header, 1, sizeof(header), felf);
232 if (size != sizeof(header)) {
237 machine = le16_to_cpu(header.e_machine);
238 debug("Machine %d\n", machine);
240 if (machine != EM_MICROBLAZE) {
241 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
245 text_base = le32_to_cpu(header.e_entry);
246 section_header_base = le32_to_cpu(header.e_shoff);
247 section_header_size = le16_to_cpu(header.e_shentsize) *
248 le16_to_cpu(header.e_shnum);
250 sh_table = malloc(section_header_size);
252 fprintf(stderr, "%s: Cannot allocate space for section header\n",
258 ret = fseek(felf, section_header_base, SEEK_SET);
260 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
261 argv[0], ret, section_header_base);
267 size = fread(sh_table, 1, section_header_size, felf);
268 if (size != section_header_size) {
269 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
270 argv[0], size, section_header_size);
276 sh_index = le16_to_cpu(header.e_shstrndx);
277 sh_size = le32_to_cpu(sh_table[sh_index].sh_size);
278 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
280 sh_str = malloc(sh_size);
282 fprintf(stderr, "malloc failed\n");
289 * Specifies the byte offset from the beginning of the file
290 * to the first byte in the section.
292 sh_offset = le32_to_cpu(sh_table[sh_index].sh_offset);
293 sh_num = le16_to_cpu(header.e_shnum);
295 ret = fseek(felf, sh_offset, SEEK_SET);
297 fprintf(stderr, "Setting up sh_offset failed\n");
304 size = fread(sh_str, 1, sh_size, felf);
305 if (size != sh_size) {
306 fprintf(stderr, "%s: Can't read section: %lx/%x\n",
307 argv[0], size, sh_size);
314 for (i = 0; i < sh_num; i++) {
315 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
317 debug("%s\n", sh_name);
319 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
320 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
321 sh_size = le64_to_cpu(sh_table[i].sh_size);
323 if (!strcmp(".rela.dyn", sh_name)) {
324 debug("Found section\t\".rela_dyn\"\n");
325 debug(" at addr\t0x%08x\n", sh_addr);
326 debug(" at offset\t0x%08x\n", sh_offset);
327 debug(" of size\t0x%08x\n", sh_size);
328 rela_start = sh_addr;
329 rela_end = rela_start + sh_size;
331 if (!strcmp(".dynsym", sh_name)) {
332 debug("Found section\t\".dynsym\"\n");
333 debug(" at addr\t0x%08x\n", sh_addr);
334 debug(" at offset\t0x%08x\n", sh_offset);
335 debug(" of size\t0x%08x\n", sh_size);
345 debug("text_base\t0x%08lx\n", text_base);
346 debug("rela_start\t0x%08lx\n", rela_start);
347 debug("rela_end\t0x%08lx\n", rela_end);
348 debug("dyn_start\t0x%08lx\n", dyn_start);
356 static int decode_elf(char **argv)
360 unsigned char e_ident[EI_NIDENT];
362 felf = fopen(argv[2], "r+b");
364 fprintf(stderr, "%s: Cannot open %s: %s\n",
365 argv[0], argv[5], strerror(errno));
369 size = fread(e_ident, 1, EI_NIDENT, felf);
370 if (size != EI_NIDENT) {
375 /* Check if this is really ELF file */
376 if (e_ident[0] != 0x7f &&
384 ei_class = e_ident[4];
385 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
388 return decode_elf64(felf, argv);
390 return decode_elf32(felf, argv);
393 static int rela_elf64(char **argv, FILE *f)
397 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
398 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
402 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
404 for (i = 0; i < num; i++) {
405 Elf64_Rela rela, swrela;
406 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
409 if (fseek(f, pos, SEEK_SET) < 0) {
410 fprintf(stderr, "%s: %s: seek to %" PRIx64
412 argv[0], argv[1], pos, strerror(errno));
415 if (fread(&rela, sizeof(rela), 1, f) != 1) {
416 fprintf(stderr, "%s: %s: read rela failed at %"
418 argv[0], argv[1], pos);
422 swrela.r_offset = le64_to_cpu(rela.r_offset);
423 swrela.r_info = le64_to_cpu(rela.r_info);
424 swrela.r_addend = le64_to_cpu(rela.r_addend);
426 if (!supported_rela(&swrela))
429 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
430 swrela.r_offset, swrela.r_info, swrela.r_addend);
432 if (swrela.r_offset < text_base) {
433 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
434 argv[0], argv[1], pos);
438 addr = swrela.r_offset - text_base;
440 if (fseek(f, addr, SEEK_SET) < 0) {
441 fprintf(stderr, "%s: %s: seek to %"
442 PRIx64 " failed: %s\n",
443 argv[0], argv[1], addr, strerror(errno));
446 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
447 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
448 argv[0], argv[1], addr);
456 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
458 uint32_t mask = 0xffULL; /* would be different on 32-bit */
459 *type = rela->r_info & mask;
464 case R_MICROBLAZE_32:
465 debug("R_MICROBLAZE_32\n");
467 case R_MICROBLAZE_GLOB_DAT:
468 debug("R_MICROBLAZE_GLOB_DAT\n");
470 case R_MICROBLAZE_NONE:
471 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
473 case R_MICROBLAZE_REL:
474 debug("R_MICROBLAZE_REL\n");
477 fprintf(stderr, "warning: unsupported relocation type %"
478 PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
484 static int rela_elf32(char **argv, FILE *f)
487 uint32_t value, type;
489 if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
490 fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
494 num = (rela_end - rela_start) / sizeof(Elf32_Rela);
496 debug("Number of entries: %u\n", num);
498 for (i = 0; i < num; i++) {
499 Elf32_Rela rela, swrela;
501 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
502 uint32_t addr, pos_dyn;
504 debug("\nPossition:\t%d/0x%x\n", i, pos);
506 if (fseek(f, pos, SEEK_SET) < 0) {
507 fprintf(stderr, "%s: %s: seek to %" PRIx32
509 argv[0], argv[1], pos, strerror(errno));
512 if (fread(&rela, sizeof(rela), 1, f) != 1) {
513 fprintf(stderr, "%s: %s: read rela failed at %"
515 argv[0], argv[1], pos);
519 debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
520 PRIu32 " r_addend:\t%" PRIx32 "\n",
521 rela.r_offset, rela.r_info, rela.r_addend);
523 swrela.r_offset = le32_to_cpu(rela.r_offset);
524 swrela.r_info = le32_to_cpu(rela.r_info);
525 swrela.r_addend = le32_to_cpu(rela.r_addend);
527 debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
528 PRIu32 " r_addend:\t%" PRIx32 "\n",
529 swrela.r_offset, swrela.r_info, swrela.r_addend);
531 if (!supported_rela32(&swrela, &type))
534 if (swrela.r_offset < text_base) {
535 fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
536 argv[0], argv[1], pos);
540 addr = swrela.r_offset - text_base;
542 debug("Addr:\t0x%" PRIx32 "\n", addr);
545 case R_MICROBLAZE_REL:
546 if (fseek(f, addr, SEEK_SET) < 0) {
547 fprintf(stderr, "%s: %s: seek to %"
548 PRIx32 " failed: %s\n",
549 argv[0], argv[1], addr, strerror(errno));
553 debug("Write addend\n");
555 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
556 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
557 argv[0], argv[1], addr);
561 case R_MICROBLAZE_32:
562 case R_MICROBLAZE_GLOB_DAT:
563 /* global symbols read it and add reloc offset */
564 index = swrela.r_info >> 8;
565 pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
567 debug("Index:\t%d\n", index);
568 debug("Pos_dyn:\t0x%x\n", pos_dyn);
570 if (fseek(f, pos_dyn, SEEK_SET) < 0) {
571 fprintf(stderr, "%s: %s: seek to %"
572 PRIx32 " failed: %s\n",
573 argv[0], argv[1], pos_dyn, strerror(errno));
577 if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
578 fprintf(stderr, "%s: %s: read symbols failed at %"
580 argv[0], argv[1], pos_dyn);
584 debug("Symbol description:\n");
585 debug(" st_name:\t0x%x\n", symbols.st_name);
586 debug(" st_value:\t0x%x\n", symbols.st_value);
587 debug(" st_size:\t0x%x\n", symbols.st_size);
589 value = swrela.r_addend + symbols.st_value;
591 debug("Value:\t0x%x\n", value);
593 if (fseek(f, addr, SEEK_SET) < 0) {
594 fprintf(stderr, "%s: %s: seek to %"
595 PRIx32 " failed: %s\n",
596 argv[0], argv[1], addr, strerror(errno));
600 if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
601 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
602 argv[0], argv[1], addr);
607 case R_MICROBLAZE_NONE:
608 debug("R_MICROBLAZE_NONE - skip\n");
611 fprintf(stderr, "warning: unsupported relocation type %"
612 PRIu32 " at %" PRIx32 "\n",
613 type, rela.r_offset);
620 int main(int argc, char **argv)
627 fprintf(stderr, "Statically apply ELF rela relocations\n");
628 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
633 ret = decode_elf(argv);
635 fprintf(stderr, "ELF decoding failed\n");
639 if (rela_start > rela_end || rela_start < text_base) {
640 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
644 rela_start -= text_base;
645 rela_end -= text_base;
646 dyn_start -= text_base;
648 f = fopen(argv[1], "r+b");
650 fprintf(stderr, "%s: Cannot open %s: %s\n",
651 argv[0], argv[1], strerror(errno));
655 fseek(f, 0, SEEK_END);
656 file_size = ftell(f);
659 if (rela_end > file_size) {
660 // Most likely compiler inserted some section that didn't get
661 // objcopy-ed into the final binary
662 rela_end = file_size;
666 ret = rela_elf64(argv, f);
668 ret = rela_elf32(argv, f);
671 fprintf(stderr, "%s: %s: close failed: %s\n",
672 argv[0], argv[1], strerror(errno));