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.
19 #ifndef R_AARCH64_RELATIVE
20 #define R_AARCH64_RELATIVE 1027
25 static uint64_t rela_start, rela_end, text_base, dyn_start;
27 static const bool debug_en;
29 static void debug(const char *fmt, ...)
40 static bool supported_rela(Elf64_Rela *rela)
42 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
43 uint32_t type = rela->r_info & mask;
46 #ifdef R_AARCH64_RELATIVE
47 case R_AARCH64_RELATIVE:
51 fprintf(stderr, "warning: unsupported relocation type %"
52 PRIu32 " at %" PRIx64 "\n",
53 type, rela->r_offset);
59 static int decode_elf64(FILE *felf, char **argv)
63 uint64_t section_header_base, section_header_size, sh_offset, sh_size;
64 Elf64_Shdr *sh_table; /* Elf symbol table */
68 debug("64bit version\n");
70 /* Make sure we are at start */
73 size = fread(&header, 1, sizeof(header), felf);
74 if (size != sizeof(header)) {
79 machine = header.e_machine;
80 debug("Machine\t%d\n", machine);
82 if (machine != EM_AARCH64) {
83 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
87 text_base = header.e_entry;
88 section_header_base = header.e_shoff;
89 section_header_size = header.e_shentsize * header.e_shnum;
91 sh_table = malloc(section_header_size);
93 fprintf(stderr, "%s: Cannot allocate space for section header\n",
99 ret = fseek(felf, section_header_base, SEEK_SET);
101 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
102 argv[0], ret, section_header_base);
108 size = fread(sh_table, 1, section_header_size, felf);
109 if (size != section_header_size) {
110 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
111 argv[0], size, section_header_size);
117 sh_size = sh_table[header.e_shstrndx].sh_size;
118 debug("e_shstrndx\t0x%08x\n", header.e_shstrndx);
119 debug("sh_size\t\t0x%08lx\n", sh_size);
121 sh_str = malloc(sh_size);
123 fprintf(stderr, "malloc failed\n");
130 * Specifies the byte offset from the beginning of the file
131 * to the first byte in the section.
133 sh_offset = sh_table[header.e_shstrndx].sh_offset;
135 debug("sh_offset\t0x%08x\n", header.e_shnum);
137 ret = fseek(felf, sh_offset, SEEK_SET);
139 fprintf(stderr, "Setting up sh_offset failed\n");
146 size = fread(sh_str, 1, sh_size, felf);
147 if (size != sh_size) {
148 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
149 argv[0], size, sh_size);
156 for (i = 0; i < header.e_shnum; i++) {
157 /* fprintf(stderr, "%s\n", sh_str + sh_table[i].sh_name); Debug only */
158 if (!strcmp(".rela.dyn", (sh_str + sh_table[i].sh_name))) {
159 debug("Found section\t\".rela_dyn\"\n");
160 debug(" at addr\t0x%08x\n",
161 (unsigned int)sh_table[i].sh_addr);
162 debug(" at offset\t0x%08x\n",
163 (unsigned int)sh_table[i].sh_offset);
164 debug(" of size\t0x%08x\n",
165 (unsigned int)sh_table[i].sh_size);
166 rela_start = sh_table[i].sh_addr;
167 rela_end = rela_start + sh_table[i].sh_size;
177 debug("text_base\t0x%08lx\n", text_base);
178 debug("rela_start\t0x%08lx\n", rela_start);
179 debug("rela_end\t0x%08lx\n", rela_end);
187 static int decode_elf32(FILE *felf, char **argv)
191 uint64_t section_header_base, section_header_size, sh_offset, sh_size;
192 Elf32_Shdr *sh_table; /* Elf symbol table */
196 debug("32bit version\n");
198 /* Make sure we are at start */
201 size = fread(&header, 1, sizeof(header), felf);
202 if (size != sizeof(header)) {
207 machine = header.e_machine;
208 debug("Machine %d\n", machine);
210 if (machine != EM_MICROBLAZE) {
211 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
215 text_base = header.e_entry;
216 section_header_base = header.e_shoff;
218 debug("Section header base %x\n", section_header_base);
220 section_header_size = header.e_shentsize * header.e_shnum;
222 debug("Section header size %d\n", section_header_size);
224 sh_table = malloc(section_header_size);
226 fprintf(stderr, "%s: Cannot allocate space for section header\n",
232 ret = fseek(felf, section_header_base, SEEK_SET);
234 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
235 argv[0], ret, section_header_base);
241 size = fread(sh_table, 1, section_header_size, felf);
242 if (size != section_header_size) {
243 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
244 argv[0], size, section_header_size);
250 sh_size = sh_table[header.e_shstrndx].sh_size;
251 debug("e_shstrndx %x, sh_size %lx\n", header.e_shstrndx, sh_size);
253 sh_str = malloc(sh_size);
255 fprintf(stderr, "malloc failed\n");
262 * Specifies the byte offset from the beginning of the file
263 * to the first byte in the section.
265 sh_offset = sh_table[header.e_shstrndx].sh_offset;
267 debug("sh_offset %x\n", header.e_shnum);
269 ret = fseek(felf, sh_offset, SEEK_SET);
271 fprintf(stderr, "Setting up sh_offset failed\n");
278 size = fread(sh_str, 1, sh_size, felf);
279 if (size != sh_size) {
280 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
281 argv[0], size, sh_size);
288 for (i = 0; i < header.e_shnum; i++) {
289 debug("%s\n", sh_str + sh_table[i].sh_name);
290 if (!strcmp(".rela.dyn", (sh_str + sh_table[i].sh_name))) {
291 debug("Found section\t\".rela_dyn\"\n");
292 debug(" at addr\t0x%08x\n", (unsigned int)sh_table[i].sh_addr);
293 debug(" at offset\t0x%08x\n", (unsigned int)sh_table[i].sh_offset);
294 debug(" of size\t0x%08x\n", (unsigned int)sh_table[i].sh_size);
295 rela_start = sh_table[i].sh_addr;
296 rela_end = rela_start + sh_table[i].sh_size;
298 if (!strcmp(".dynsym", (sh_str + sh_table[i].sh_name))) {
299 debug("Found section\t\".dynsym\"\n");
300 debug(" at addr\t0x%08x\n", (unsigned int)sh_table[i].sh_addr);
301 debug(" at offset\t0x%08x\n", (unsigned int)sh_table[i].sh_offset);
302 debug(" of size\t0x%08x\n", (unsigned int)sh_table[i].sh_size);
303 dyn_start = sh_table[i].sh_addr;
312 debug("text_base\t0x%08lx\n", text_base);
313 debug("rela_start\t0x%08lx\n", rela_start);
314 debug("rela_end\t0x%08lx\n", rela_end);
315 debug("dyn_start\t0x%08lx\n", dyn_start);
323 static int decode_elf(char **argv)
327 unsigned char e_ident[EI_NIDENT];
329 felf = fopen(argv[2], "r+b");
331 fprintf(stderr, "%s: Cannot open %s: %s\n",
332 argv[0], argv[5], strerror(errno));
336 size = fread(e_ident, 1, EI_NIDENT, felf);
337 if (size != EI_NIDENT) {
342 /* Check if this is really ELF file */
343 if (e_ident[0] != 0x7f &&
351 ei_class = e_ident[4];
352 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
355 return decode_elf64(felf, argv);
357 return decode_elf32(felf, argv);
360 static int rela_elf64(char **argv, FILE *f)
364 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
365 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
369 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
371 for (i = 0; i < num; i++) {
372 Elf64_Rela rela, swrela;
373 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
376 if (fseek(f, pos, SEEK_SET) < 0) {
377 fprintf(stderr, "%s: %s: seek to %" PRIx64
379 argv[0], argv[1], pos, strerror(errno));
382 if (fread(&rela, sizeof(rela), 1, f) != 1) {
383 fprintf(stderr, "%s: %s: read rela failed at %"
385 argv[0], argv[1], pos);
389 swrela.r_offset = cpu_to_le64(rela.r_offset);
390 swrela.r_info = cpu_to_le64(rela.r_info);
391 swrela.r_addend = cpu_to_le64(rela.r_addend);
393 if (!supported_rela(&swrela))
396 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
397 swrela.r_offset, swrela.r_info, swrela.r_addend);
399 if (swrela.r_offset < text_base) {
400 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
401 argv[0], argv[1], pos);
405 addr = swrela.r_offset - text_base;
407 if (fseek(f, addr, SEEK_SET) < 0) {
408 fprintf(stderr, "%s: %s: seek to %"
409 PRIx64 " failed: %s\n",
410 argv[0], argv[1], addr, strerror(errno));
413 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
414 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
415 argv[0], argv[1], addr);
423 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
425 uint32_t mask = 0xffULL; /* would be different on 32-bit */
426 *type = rela->r_info & mask;
431 case R_MICROBLAZE_32:
432 debug("R_MICROBLAZE_32\n");
434 case R_MICROBLAZE_GLOB_DAT:
435 debug("R_MICROBLAZE_GLOB_DAT\n");
437 case R_MICROBLAZE_NONE:
438 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
440 case R_MICROBLAZE_REL:
441 debug("R_MICROBLAZE_REL\n");
444 fprintf(stderr, "warning: unsupported relocation type %"
445 PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
451 static int rela_elf32(char **argv, FILE *f)
454 uint32_t value, type;
456 if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
457 fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
461 num = (rela_end - rela_start) / sizeof(Elf32_Rela);
463 debug("Number of entries: %u\n", num);
465 for (i = 0; i < num; i++) {
466 Elf32_Rela rela, swrela;
468 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
469 uint32_t addr, pos_dyn;
471 debug("\nPossition:\t%d/0x%x\n", i, pos);
473 if (fseek(f, pos, SEEK_SET) < 0) {
474 fprintf(stderr, "%s: %s: seek to %" PRIx32
476 argv[0], argv[1], pos, strerror(errno));
479 if (fread(&rela, sizeof(rela), 1, f) != 1) {
480 fprintf(stderr, "%s: %s: read rela failed at %"
482 argv[0], argv[1], pos);
486 debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
487 PRIu32 " r_addend:\t%" PRIx32 "\n",
488 rela.r_offset, rela.r_info, rela.r_addend);
490 swrela.r_offset = cpu_to_le32(rela.r_offset);
491 swrela.r_info = cpu_to_le32(rela.r_info);
492 swrela.r_addend = cpu_to_le32(rela.r_addend);
494 debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
495 PRIu32 " r_addend:\t%" PRIx32 "\n",
496 swrela.r_offset, swrela.r_info, swrela.r_addend);
498 if (!supported_rela32(&swrela, &type))
501 if (swrela.r_offset < text_base) {
502 fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
503 argv[0], argv[1], pos);
507 addr = swrela.r_offset - text_base;
509 debug("Addr:\t0x%" PRIx32 "\n", addr);
512 case R_MICROBLAZE_REL:
513 if (fseek(f, addr, SEEK_SET) < 0) {
514 fprintf(stderr, "%s: %s: seek to %"
515 PRIx32 " failed: %s\n",
516 argv[0], argv[1], addr, strerror(errno));
520 debug("Write addend\n");
522 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
523 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
524 argv[0], argv[1], addr);
528 case R_MICROBLAZE_32:
529 case R_MICROBLAZE_GLOB_DAT:
530 /* global symbols read it and add reloc offset */
531 index = swrela.r_info >> 8;
532 pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
534 debug("Index:\t%d\n", index);
535 debug("Pos_dyn:\t0x%x\n", pos_dyn);
537 if (fseek(f, pos_dyn, SEEK_SET) < 0) {
538 fprintf(stderr, "%s: %s: seek to %"
539 PRIx32 " failed: %s\n",
540 argv[0], argv[1], pos_dyn, strerror(errno));
544 if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
545 fprintf(stderr, "%s: %s: read symbols failed at %"
547 argv[0], argv[1], pos_dyn);
551 debug("Symbol description:\n");
552 debug(" st_name:\t0x%x\n", symbols.st_name);
553 debug(" st_value:\t0x%x\n", symbols.st_value);
554 debug(" st_size:\t0x%x\n", symbols.st_size);
556 value = swrela.r_addend + symbols.st_value;
558 debug("Value:\t0x%x\n", value);
560 if (fseek(f, addr, SEEK_SET) < 0) {
561 fprintf(stderr, "%s: %s: seek to %"
562 PRIx32 " failed: %s\n",
563 argv[0], argv[1], addr, strerror(errno));
567 if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
568 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
569 argv[0], argv[1], addr);
574 case R_MICROBLAZE_NONE:
575 debug("R_MICROBLAZE_NONE - skip\n");
578 fprintf(stderr, "warning: unsupported relocation type %"
579 PRIu32 " at %" PRIx32 "\n",
580 type, rela.r_offset);
587 int main(int argc, char **argv)
594 fprintf(stderr, "Statically apply ELF rela relocations\n");
595 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
600 ret = decode_elf(argv);
602 fprintf(stderr, "ELF decoding failed\n");
606 if (rela_start > rela_end || rela_start < text_base) {
607 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
611 rela_start -= text_base;
612 rela_end -= text_base;
613 dyn_start -= text_base;
615 f = fopen(argv[1], "r+b");
617 fprintf(stderr, "%s: Cannot open %s: %s\n",
618 argv[0], argv[1], strerror(errno));
622 fseek(f, 0, SEEK_END);
623 file_size = ftell(f);
626 if (rela_end > file_size) {
627 // Most likely compiler inserted some section that didn't get
628 // objcopy-ed into the final binary
629 rela_end = file_size;
633 ret = rela_elf64(argv, f);
635 ret = rela_elf32(argv, f);
638 fprintf(stderr, "%s: %s: close failed: %s\n",
639 argv[0], argv[1], strerror(errno));