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;
64 uint64_t sh_addr, sh_offset, sh_size;
65 Elf64_Half sh_index, sh_num;
66 Elf64_Shdr *sh_table; /* Elf symbol table */
70 debug("64bit version\n");
72 /* Make sure we are at start */
75 size = fread(&header, 1, sizeof(header), felf);
76 if (size != sizeof(header)) {
81 machine = le16_to_cpu(header.e_machine);
82 debug("Machine\t%d\n", machine);
84 if (machine != EM_AARCH64) {
85 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
89 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);
94 sh_table = malloc(section_header_size);
96 fprintf(stderr, "%s: Cannot allocate space for section header\n",
102 ret = fseek(felf, section_header_base, SEEK_SET);
104 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
105 argv[0], ret, section_header_base);
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);
120 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);
124 sh_str = malloc(sh_size);
126 fprintf(stderr, "malloc failed\n");
133 * Specifies the byte offset from the beginning of the file
134 * to the first byte in the section.
136 sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
137 sh_num = le16_to_cpu(header.e_shnum);
139 ret = fseek(felf, sh_offset, SEEK_SET);
141 fprintf(stderr, "Setting up sh_offset failed\n");
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);
158 for (i = 0; i < sh_num; i++) {
159 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
161 debug("%s\n", sh_name);
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);
167 if (!strcmp(".rela.dyn", sh_name)) {
168 debug("Found section\t\".rela_dyn\"\n");
169 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;
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);
193 static int decode_elf32(FILE *felf, char **argv)
197 uint64_t section_header_base, section_header_size;
198 uint32_t sh_addr, sh_offset, sh_size;
199 Elf32_Half sh_index, sh_num;
200 Elf32_Shdr *sh_table; /* Elf symbol table */
204 debug("32bit version\n");
206 /* Make sure we are at start */
209 size = fread(&header, 1, sizeof(header), felf);
210 if (size != sizeof(header)) {
215 machine = le16_to_cpu(header.e_machine);
216 debug("Machine %d\n", machine);
218 if (machine != EM_MICROBLAZE) {
219 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
223 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);
228 sh_table = malloc(section_header_size);
230 fprintf(stderr, "%s: Cannot allocate space for section header\n",
236 ret = fseek(felf, section_header_base, SEEK_SET);
238 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
239 argv[0], ret, section_header_base);
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);
254 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);
258 sh_str = malloc(sh_size);
260 fprintf(stderr, "malloc failed\n");
267 * Specifies the byte offset from the beginning of the file
268 * to the first byte in the section.
270 sh_offset = le32_to_cpu(sh_table[sh_index].sh_offset);
271 sh_num = le16_to_cpu(header.e_shnum);
273 ret = fseek(felf, sh_offset, SEEK_SET);
275 fprintf(stderr, "Setting up sh_offset failed\n");
282 size = fread(sh_str, 1, sh_size, felf);
283 if (size != sh_size) {
284 fprintf(stderr, "%s: Can't read section: %lx/%x\n",
285 argv[0], size, sh_size);
292 for (i = 0; i < sh_num; i++) {
293 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
295 debug("%s\n", sh_name);
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);
301 if (!strcmp(".rela.dyn", sh_name)) {
302 debug("Found section\t\".rela_dyn\"\n");
303 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;
309 if (!strcmp(".dynsym", sh_name)) {
310 debug("Found section\t\".dynsym\"\n");
311 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);
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);
334 static int decode_elf(char **argv)
338 unsigned char e_ident[EI_NIDENT];
340 felf = fopen(argv[2], "r+b");
342 fprintf(stderr, "%s: Cannot open %s: %s\n",
343 argv[0], argv[5], strerror(errno));
347 size = fread(e_ident, 1, EI_NIDENT, felf);
348 if (size != EI_NIDENT) {
353 /* Check if this is really ELF file */
354 if (e_ident[0] != 0x7f &&
362 ei_class = e_ident[4];
363 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
366 return decode_elf64(felf, argv);
368 return decode_elf32(felf, argv);
371 static int rela_elf64(char **argv, FILE *f)
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]);
380 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
382 for (i = 0; i < num; i++) {
383 Elf64_Rela rela, swrela;
384 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
387 if (fseek(f, pos, SEEK_SET) < 0) {
388 fprintf(stderr, "%s: %s: seek to %" PRIx64
390 argv[0], argv[1], pos, strerror(errno));
393 if (fread(&rela, sizeof(rela), 1, f) != 1) {
394 fprintf(stderr, "%s: %s: read rela failed at %"
396 argv[0], argv[1], pos);
400 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);
404 if (!supported_rela(&swrela))
407 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
408 swrela.r_offset, swrela.r_info, swrela.r_addend);
410 if (swrela.r_offset < text_base) {
411 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
412 argv[0], argv[1], pos);
416 addr = swrela.r_offset - text_base;
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));
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);
434 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
436 uint32_t mask = 0xffULL; /* would be different on 32-bit */
437 *type = rela->r_info & mask;
442 case R_MICROBLAZE_32:
443 debug("R_MICROBLAZE_32\n");
445 case R_MICROBLAZE_GLOB_DAT:
446 debug("R_MICROBLAZE_GLOB_DAT\n");
448 case R_MICROBLAZE_NONE:
449 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
451 case R_MICROBLAZE_REL:
452 debug("R_MICROBLAZE_REL\n");
455 fprintf(stderr, "warning: unsupported relocation type %"
456 PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
462 static int rela_elf32(char **argv, FILE *f)
465 uint32_t value, type;
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]);
472 num = (rela_end - rela_start) / sizeof(Elf32_Rela);
474 debug("Number of entries: %u\n", num);
476 for (i = 0; i < num; i++) {
477 Elf32_Rela rela, swrela;
479 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
480 uint32_t addr, pos_dyn;
482 debug("\nPossition:\t%d/0x%x\n", i, pos);
484 if (fseek(f, pos, SEEK_SET) < 0) {
485 fprintf(stderr, "%s: %s: seek to %" PRIx32
487 argv[0], argv[1], pos, strerror(errno));
490 if (fread(&rela, sizeof(rela), 1, f) != 1) {
491 fprintf(stderr, "%s: %s: read rela failed at %"
493 argv[0], argv[1], pos);
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);
501 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);
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);
509 if (!supported_rela32(&swrela, &type))
512 if (swrela.r_offset < text_base) {
513 fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
514 argv[0], argv[1], pos);
518 addr = swrela.r_offset - text_base;
520 debug("Addr:\t0x%" PRIx32 "\n", addr);
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));
531 debug("Write addend\n");
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);
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;
545 debug("Index:\t%d\n", index);
546 debug("Pos_dyn:\t0x%x\n", pos_dyn);
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));
555 if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
556 fprintf(stderr, "%s: %s: read symbols failed at %"
558 argv[0], argv[1], pos_dyn);
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);
567 value = swrela.r_addend + symbols.st_value;
569 debug("Value:\t0x%x\n", value);
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));
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);
585 case R_MICROBLAZE_NONE:
586 debug("R_MICROBLAZE_NONE - skip\n");
589 fprintf(stderr, "warning: unsupported relocation type %"
590 PRIu32 " at %" PRIx32 "\n",
591 type, rela.r_offset);
598 int main(int argc, char **argv)
605 fprintf(stderr, "Statically apply ELF rela relocations\n");
606 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
611 ret = decode_elf(argv);
613 fprintf(stderr, "ELF decoding failed\n");
617 if (rela_start > rela_end || rela_start < text_base) {
618 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
622 rela_start -= text_base;
623 rela_end -= text_base;
624 dyn_start -= text_base;
626 f = fopen(argv[1], "r+b");
628 fprintf(stderr, "%s: Cannot open %s: %s\n",
629 argv[0], argv[1], strerror(errno));
633 fseek(f, 0, SEEK_END);
634 file_size = ftell(f);
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;
644 ret = rela_elf64(argv, f);
646 ret = rela_elf32(argv, f);
649 fprintf(stderr, "%s: %s: close failed: %s\n",
650 argv[0], argv[1], strerror(errno));