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 case R_AARCH64_RELATIVE:
49 fprintf(stderr, "warning: unsupported relocation type %"
50 PRIu32 " at %" PRIx64 "\n",
51 type, rela->r_offset);
57 static int decode_elf64(FILE *felf, char **argv)
61 uint64_t section_header_base, section_header_size;
62 uint64_t sh_addr, sh_offset, sh_size;
63 Elf64_Half sh_index, sh_num;
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 = le16_to_cpu(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 = le64_to_cpu(header.e_entry);
88 section_header_base = le64_to_cpu(header.e_shoff);
89 section_header_size = le16_to_cpu(header.e_shentsize) *
90 le16_to_cpu(header.e_shnum);
92 sh_table = malloc(section_header_size);
94 fprintf(stderr, "%s: Cannot allocate space for section header\n",
100 ret = fseek(felf, section_header_base, SEEK_SET);
102 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
103 argv[0], ret, section_header_base);
109 size = fread(sh_table, 1, section_header_size, felf);
110 if (size != section_header_size) {
111 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
112 argv[0], size, section_header_size);
118 sh_index = le16_to_cpu(header.e_shstrndx);
119 sh_size = le64_to_cpu(sh_table[sh_index].sh_size);
120 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
122 sh_str = malloc(sh_size);
124 fprintf(stderr, "malloc failed\n");
131 * Specifies the byte offset from the beginning of the file
132 * to the first byte in the section.
134 sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
135 sh_num = le16_to_cpu(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 < sh_num; i++) {
157 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
159 debug("%s\n", sh_name);
161 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
162 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
163 sh_size = le64_to_cpu(sh_table[i].sh_size);
165 if (!strcmp(".rela.dyn", sh_name)) {
166 debug("Found section\t\".rela_dyn\"\n");
167 debug(" at addr\t0x%08x\n", sh_addr);
168 debug(" at offset\t0x%08x\n", sh_offset);
169 debug(" of size\t0x%08x\n", sh_size);
170 rela_start = sh_addr;
171 rela_end = rela_start + sh_size;
181 debug("text_base\t0x%08lx\n", text_base);
182 debug("rela_start\t0x%08lx\n", rela_start);
183 debug("rela_end\t0x%08lx\n", rela_end);
191 static int decode_elf32(FILE *felf, char **argv)
195 uint64_t section_header_base, section_header_size;
196 uint32_t sh_addr, sh_offset, sh_size;
197 Elf32_Half sh_index, sh_num;
198 Elf32_Shdr *sh_table; /* Elf symbol table */
202 debug("32bit version\n");
204 /* Make sure we are at start */
207 size = fread(&header, 1, sizeof(header), felf);
208 if (size != sizeof(header)) {
213 machine = le16_to_cpu(header.e_machine);
214 debug("Machine %d\n", machine);
216 if (machine != EM_MICROBLAZE) {
217 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
221 text_base = le32_to_cpu(header.e_entry);
222 section_header_base = le32_to_cpu(header.e_shoff);
223 section_header_size = le16_to_cpu(header.e_shentsize) *
224 le16_to_cpu(header.e_shnum);
226 sh_table = malloc(section_header_size);
228 fprintf(stderr, "%s: Cannot allocate space for section header\n",
234 ret = fseek(felf, section_header_base, SEEK_SET);
236 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
237 argv[0], ret, section_header_base);
243 size = fread(sh_table, 1, section_header_size, felf);
244 if (size != section_header_size) {
245 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
246 argv[0], size, section_header_size);
252 sh_index = le16_to_cpu(header.e_shstrndx);
253 sh_size = le32_to_cpu(sh_table[sh_index].sh_size);
254 debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
256 sh_str = malloc(sh_size);
258 fprintf(stderr, "malloc failed\n");
265 * Specifies the byte offset from the beginning of the file
266 * to the first byte in the section.
268 sh_offset = le32_to_cpu(sh_table[sh_index].sh_offset);
269 sh_num = le16_to_cpu(header.e_shnum);
271 ret = fseek(felf, sh_offset, SEEK_SET);
273 fprintf(stderr, "Setting up sh_offset failed\n");
280 size = fread(sh_str, 1, sh_size, felf);
281 if (size != sh_size) {
282 fprintf(stderr, "%s: Can't read section: %lx/%x\n",
283 argv[0], size, sh_size);
290 for (i = 0; i < sh_num; i++) {
291 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
293 debug("%s\n", sh_name);
295 sh_addr = le64_to_cpu(sh_table[i].sh_addr);
296 sh_offset = le64_to_cpu(sh_table[i].sh_offset);
297 sh_size = le64_to_cpu(sh_table[i].sh_size);
299 if (!strcmp(".rela.dyn", sh_name)) {
300 debug("Found section\t\".rela_dyn\"\n");
301 debug(" at addr\t0x%08x\n", sh_addr);
302 debug(" at offset\t0x%08x\n", sh_offset);
303 debug(" of size\t0x%08x\n", sh_size);
304 rela_start = sh_addr;
305 rela_end = rela_start + sh_size;
307 if (!strcmp(".dynsym", sh_name)) {
308 debug("Found section\t\".dynsym\"\n");
309 debug(" at addr\t0x%08x\n", sh_addr);
310 debug(" at offset\t0x%08x\n", sh_offset);
311 debug(" of size\t0x%08x\n", sh_size);
321 debug("text_base\t0x%08lx\n", text_base);
322 debug("rela_start\t0x%08lx\n", rela_start);
323 debug("rela_end\t0x%08lx\n", rela_end);
324 debug("dyn_start\t0x%08lx\n", dyn_start);
332 static int decode_elf(char **argv)
336 unsigned char e_ident[EI_NIDENT];
338 felf = fopen(argv[2], "r+b");
340 fprintf(stderr, "%s: Cannot open %s: %s\n",
341 argv[0], argv[5], strerror(errno));
345 size = fread(e_ident, 1, EI_NIDENT, felf);
346 if (size != EI_NIDENT) {
351 /* Check if this is really ELF file */
352 if (e_ident[0] != 0x7f &&
360 ei_class = e_ident[4];
361 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
364 return decode_elf64(felf, argv);
366 return decode_elf32(felf, argv);
369 static int rela_elf64(char **argv, FILE *f)
373 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
374 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
378 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
380 for (i = 0; i < num; i++) {
381 Elf64_Rela rela, swrela;
382 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
385 if (fseek(f, pos, SEEK_SET) < 0) {
386 fprintf(stderr, "%s: %s: seek to %" PRIx64
388 argv[0], argv[1], pos, strerror(errno));
391 if (fread(&rela, sizeof(rela), 1, f) != 1) {
392 fprintf(stderr, "%s: %s: read rela failed at %"
394 argv[0], argv[1], pos);
398 swrela.r_offset = le64_to_cpu(rela.r_offset);
399 swrela.r_info = le64_to_cpu(rela.r_info);
400 swrela.r_addend = le64_to_cpu(rela.r_addend);
402 if (!supported_rela(&swrela))
405 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
406 swrela.r_offset, swrela.r_info, swrela.r_addend);
408 if (swrela.r_offset < text_base) {
409 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
410 argv[0], argv[1], pos);
414 addr = swrela.r_offset - text_base;
416 if (fseek(f, addr, SEEK_SET) < 0) {
417 fprintf(stderr, "%s: %s: seek to %"
418 PRIx64 " failed: %s\n",
419 argv[0], argv[1], addr, strerror(errno));
422 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
423 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
424 argv[0], argv[1], addr);
432 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
434 uint32_t mask = 0xffULL; /* would be different on 32-bit */
435 *type = rela->r_info & mask;
440 case R_MICROBLAZE_32:
441 debug("R_MICROBLAZE_32\n");
443 case R_MICROBLAZE_GLOB_DAT:
444 debug("R_MICROBLAZE_GLOB_DAT\n");
446 case R_MICROBLAZE_NONE:
447 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
449 case R_MICROBLAZE_REL:
450 debug("R_MICROBLAZE_REL\n");
453 fprintf(stderr, "warning: unsupported relocation type %"
454 PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
460 static int rela_elf32(char **argv, FILE *f)
463 uint32_t value, type;
465 if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
466 fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
470 num = (rela_end - rela_start) / sizeof(Elf32_Rela);
472 debug("Number of entries: %u\n", num);
474 for (i = 0; i < num; i++) {
475 Elf32_Rela rela, swrela;
477 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
478 uint32_t addr, pos_dyn;
480 debug("\nPossition:\t%d/0x%x\n", i, pos);
482 if (fseek(f, pos, SEEK_SET) < 0) {
483 fprintf(stderr, "%s: %s: seek to %" PRIx32
485 argv[0], argv[1], pos, strerror(errno));
488 if (fread(&rela, sizeof(rela), 1, f) != 1) {
489 fprintf(stderr, "%s: %s: read rela failed at %"
491 argv[0], argv[1], pos);
495 debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
496 PRIu32 " r_addend:\t%" PRIx32 "\n",
497 rela.r_offset, rela.r_info, rela.r_addend);
499 swrela.r_offset = le32_to_cpu(rela.r_offset);
500 swrela.r_info = le32_to_cpu(rela.r_info);
501 swrela.r_addend = le32_to_cpu(rela.r_addend);
503 debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
504 PRIu32 " r_addend:\t%" PRIx32 "\n",
505 swrela.r_offset, swrela.r_info, swrela.r_addend);
507 if (!supported_rela32(&swrela, &type))
510 if (swrela.r_offset < text_base) {
511 fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
512 argv[0], argv[1], pos);
516 addr = swrela.r_offset - text_base;
518 debug("Addr:\t0x%" PRIx32 "\n", addr);
521 case R_MICROBLAZE_REL:
522 if (fseek(f, addr, SEEK_SET) < 0) {
523 fprintf(stderr, "%s: %s: seek to %"
524 PRIx32 " failed: %s\n",
525 argv[0], argv[1], addr, strerror(errno));
529 debug("Write addend\n");
531 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
532 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
533 argv[0], argv[1], addr);
537 case R_MICROBLAZE_32:
538 case R_MICROBLAZE_GLOB_DAT:
539 /* global symbols read it and add reloc offset */
540 index = swrela.r_info >> 8;
541 pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
543 debug("Index:\t%d\n", index);
544 debug("Pos_dyn:\t0x%x\n", pos_dyn);
546 if (fseek(f, pos_dyn, SEEK_SET) < 0) {
547 fprintf(stderr, "%s: %s: seek to %"
548 PRIx32 " failed: %s\n",
549 argv[0], argv[1], pos_dyn, strerror(errno));
553 if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
554 fprintf(stderr, "%s: %s: read symbols failed at %"
556 argv[0], argv[1], pos_dyn);
560 debug("Symbol description:\n");
561 debug(" st_name:\t0x%x\n", symbols.st_name);
562 debug(" st_value:\t0x%x\n", symbols.st_value);
563 debug(" st_size:\t0x%x\n", symbols.st_size);
565 value = swrela.r_addend + symbols.st_value;
567 debug("Value:\t0x%x\n", value);
569 if (fseek(f, addr, SEEK_SET) < 0) {
570 fprintf(stderr, "%s: %s: seek to %"
571 PRIx32 " failed: %s\n",
572 argv[0], argv[1], addr, strerror(errno));
576 if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
577 fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
578 argv[0], argv[1], addr);
583 case R_MICROBLAZE_NONE:
584 debug("R_MICROBLAZE_NONE - skip\n");
587 fprintf(stderr, "warning: unsupported relocation type %"
588 PRIu32 " at %" PRIx32 "\n",
589 type, rela.r_offset);
596 int main(int argc, char **argv)
603 fprintf(stderr, "Statically apply ELF rela relocations\n");
604 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
609 ret = decode_elf(argv);
611 fprintf(stderr, "ELF decoding failed\n");
615 if (rela_start > rela_end || rela_start < text_base) {
616 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
620 rela_start -= text_base;
621 rela_end -= text_base;
622 dyn_start -= text_base;
624 f = fopen(argv[1], "r+b");
626 fprintf(stderr, "%s: Cannot open %s: %s\n",
627 argv[0], argv[1], strerror(errno));
631 fseek(f, 0, SEEK_END);
632 file_size = ftell(f);
635 if (rela_end > file_size) {
636 // Most likely compiler inserted some section that didn't get
637 // objcopy-ed into the final binary
638 rela_end = file_size;
642 ret = rela_elf64(argv, f);
644 ret = rela_elf32(argv, f);
647 fprintf(stderr, "%s: %s: close failed: %s\n",
648 argv[0], argv[1], strerror(errno));