tools: relocate-rela: Fix ELF decoding on big-endian hosts
[platform/kernel/u-boot.git] / tools / relocate-rela.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
2 /*
3  * Copyright 2013 Freescale Semiconductor, Inc.
4  *
5  * 64-bit and little-endian target only until we need to support a different
6  * arch that needs this.
7  */
8
9 #include <elf.h>
10 #include <errno.h>
11 #include <inttypes.h>
12 #include <stdarg.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "compiler.h"
18
19 #ifndef R_AARCH64_RELATIVE
20 #define R_AARCH64_RELATIVE      1027
21 #endif
22
23 static int ei_class;
24
25 static uint64_t rela_start, rela_end, text_base, dyn_start;
26
27 static const bool debug_en;
28
29 static void debug(const char *fmt, ...)
30 {
31         va_list args;
32
33         if (debug_en) {
34                 va_start(args, fmt);
35                 vprintf(fmt, args);
36                 va_end(args);
37         }
38 }
39
40 static bool supported_rela(Elf64_Rela *rela)
41 {
42         uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
43         uint32_t type = rela->r_info & mask;
44
45         switch (type) {
46 #ifdef R_AARCH64_RELATIVE
47         case R_AARCH64_RELATIVE:
48                 return true;
49 #endif
50         default:
51                 fprintf(stderr, "warning: unsupported relocation type %"
52                                 PRIu32 " at %" PRIx64 "\n",
53                         type, rela->r_offset);
54
55                 return false;
56         }
57 }
58
59 static int decode_elf64(FILE *felf, char **argv)
60 {
61         size_t size;
62         Elf64_Ehdr header;
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 */
67         int ret, i, machine;
68         char *sh_str;
69
70         debug("64bit version\n");
71
72         /* Make sure we are at start */
73         rewind(felf);
74
75         size = fread(&header, 1, sizeof(header), felf);
76         if (size != sizeof(header)) {
77                 fclose(felf);
78                 return 25;
79         }
80
81         machine = le16_to_cpu(header.e_machine);
82         debug("Machine\t%d\n", machine);
83
84         if (machine != EM_AARCH64) {
85                 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
86                 return 30;
87         }
88
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);
93
94         sh_table = malloc(section_header_size);
95         if (!sh_table) {
96                 fprintf(stderr, "%s: Cannot allocate space for section header\n",
97                         argv[0]);
98                 fclose(felf);
99                 return 26;
100         }
101
102         ret = fseek(felf, section_header_base, SEEK_SET);
103         if (ret) {
104                 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
105                         argv[0], ret, section_header_base);
106                 free(sh_table);
107                 fclose(felf);
108                 return 26;
109         }
110
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);
115                 free(sh_table);
116                 fclose(felf);
117                 return 27;
118         }
119
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);
123
124         sh_str = malloc(sh_size);
125         if (!sh_str) {
126                 fprintf(stderr, "malloc failed\n");
127                 free(sh_table);
128                 fclose(felf);
129                 return 28;
130         }
131
132         /*
133          * Specifies the byte offset from the beginning of the file
134          * to the first byte in the section.
135          */
136         sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
137         sh_num = le16_to_cpu(header.e_shnum);
138
139         ret = fseek(felf, sh_offset, SEEK_SET);
140         if (ret) {
141                 fprintf(stderr, "Setting up sh_offset failed\n");
142                 free(sh_str);
143                 free(sh_table);
144                 fclose(felf);
145                 return 29;
146         }
147
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);
152                 free(sh_str);
153                 free(sh_table);
154                 fclose(felf);
155                 return 30;
156         }
157
158         for (i = 0; i < sh_num; i++) {
159                 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
160
161                 debug("%s\n", sh_name);
162
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);
166
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;
174                         break;
175                 }
176         }
177
178         /* Clean up */
179         free(sh_str);
180         free(sh_table);
181         fclose(felf);
182
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);
186
187         if (!rela_start)
188                 return 1;
189
190         return 0;
191 }
192
193 static int decode_elf32(FILE *felf, char **argv)
194 {
195         size_t size;
196         Elf32_Ehdr header;
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 */
201         int ret, i, machine;
202         char *sh_str;
203
204         debug("32bit version\n");
205
206         /* Make sure we are at start */
207         rewind(felf);
208
209         size = fread(&header, 1, sizeof(header), felf);
210         if (size != sizeof(header)) {
211                 fclose(felf);
212                 return 25;
213         }
214
215         machine = le16_to_cpu(header.e_machine);
216         debug("Machine %d\n", machine);
217
218         if (machine != EM_MICROBLAZE) {
219                 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
220                 return 30;
221         }
222
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);
227
228         sh_table = malloc(section_header_size);
229         if (!sh_table) {
230                 fprintf(stderr, "%s: Cannot allocate space for section header\n",
231                         argv[0]);
232                 fclose(felf);
233                 return 26;
234         }
235
236         ret = fseek(felf, section_header_base, SEEK_SET);
237         if (ret) {
238                 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
239                         argv[0], ret, section_header_base);
240                 free(sh_table);
241                 fclose(felf);
242                 return 26;
243         }
244
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);
249                 free(sh_table);
250                 fclose(felf);
251                 return 27;
252         }
253
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);
257
258         sh_str = malloc(sh_size);
259         if (!sh_str) {
260                 fprintf(stderr, "malloc failed\n");
261                 free(sh_table);
262                 fclose(felf);
263                 return 28;
264         }
265
266         /*
267          * Specifies the byte offset from the beginning of the file
268          * to the first byte in the section.
269          */
270         sh_offset = le32_to_cpu(sh_table[sh_index].sh_offset);
271         sh_num = le16_to_cpu(header.e_shnum);
272
273         ret = fseek(felf, sh_offset, SEEK_SET);
274         if (ret) {
275                 fprintf(stderr, "Setting up sh_offset failed\n");
276                 free(sh_str);
277                 free(sh_table);
278                 fclose(felf);
279                 return 29;
280         }
281
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);
286                 free(sh_str);
287                 free(sh_table);
288                 fclose(felf);
289                 return 30;
290         }
291
292         for (i = 0; i < sh_num; i++) {
293                 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
294
295                 debug("%s\n", sh_name);
296
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);
300
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;
308                 }
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);
314                         dyn_start = sh_addr;
315                 }
316         }
317
318         /* Clean up */
319         free(sh_str);
320         free(sh_table);
321         fclose(felf);
322
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);
327
328         if (!rela_start)
329                 return 1;
330
331         return 0;
332 }
333
334 static int decode_elf(char **argv)
335 {
336         FILE *felf;
337         size_t size;
338         unsigned char e_ident[EI_NIDENT];
339
340         felf = fopen(argv[2], "r+b");
341         if (!felf) {
342                 fprintf(stderr, "%s: Cannot open %s: %s\n",
343                         argv[0], argv[5], strerror(errno));
344                 return 2;
345         }
346
347         size = fread(e_ident, 1, EI_NIDENT, felf);
348         if (size != EI_NIDENT) {
349                 fclose(felf);
350                 return 25;
351         }
352
353         /* Check if this is really ELF file */
354         if (e_ident[0] != 0x7f &&
355             e_ident[1] != 'E' &&
356             e_ident[2] != 'L' &&
357             e_ident[3] != 'F') {
358                 fclose(felf);
359                 return 1;
360         }
361
362         ei_class = e_ident[4];
363         debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
364
365         if (ei_class == 2)
366                 return decode_elf64(felf, argv);
367
368         return decode_elf32(felf, argv);
369 }
370
371 static int rela_elf64(char **argv, FILE *f)
372 {
373         int i, num;
374
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]);
377                 return 3;
378         }
379
380         num = (rela_end - rela_start) / sizeof(Elf64_Rela);
381
382         for (i = 0; i < num; i++) {
383                 Elf64_Rela rela, swrela;
384                 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
385                 uint64_t addr;
386
387                 if (fseek(f, pos, SEEK_SET) < 0) {
388                         fprintf(stderr, "%s: %s: seek to %" PRIx64
389                                         " failed: %s\n",
390                                 argv[0], argv[1], pos, strerror(errno));
391                 }
392
393                 if (fread(&rela, sizeof(rela), 1, f) != 1) {
394                         fprintf(stderr, "%s: %s: read rela failed at %"
395                                         PRIx64 "\n",
396                                 argv[0], argv[1], pos);
397                         return 4;
398                 }
399
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);
403
404                 if (!supported_rela(&swrela))
405                         continue;
406
407                 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
408                       swrela.r_offset, swrela.r_info, swrela.r_addend);
409
410                 if (swrela.r_offset < text_base) {
411                         fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
412                                 argv[0], argv[1], pos);
413                         return 4;
414                 }
415
416                 addr = swrela.r_offset - text_base;
417
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));
422                 }
423
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);
427                         return 4;
428                 }
429         }
430
431         return 0;
432 }
433
434 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
435 {
436         uint32_t mask = 0xffULL; /* would be different on 32-bit */
437         *type = rela->r_info & mask;
438
439         debug("Type:\t");
440
441         switch (*type) {
442         case R_MICROBLAZE_32:
443                 debug("R_MICROBLAZE_32\n");
444                 return true;
445         case R_MICROBLAZE_GLOB_DAT:
446                 debug("R_MICROBLAZE_GLOB_DAT\n");
447                 return true;
448         case R_MICROBLAZE_NONE:
449                 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
450                 return false;
451         case R_MICROBLAZE_REL:
452                 debug("R_MICROBLAZE_REL\n");
453                 return true;
454         default:
455                 fprintf(stderr, "warning: unsupported relocation type %"
456                         PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
457
458                 return false;
459         }
460 }
461
462 static int rela_elf32(char **argv, FILE *f)
463 {
464         int i, num, index;
465         uint32_t value, type;
466
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]);
469                 return 3;
470         }
471
472         num = (rela_end - rela_start) / sizeof(Elf32_Rela);
473
474         debug("Number of entries: %u\n", num);
475
476         for (i = 0; i < num; i++) {
477                 Elf32_Rela rela, swrela;
478                 Elf32_Sym symbols;
479                 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
480                 uint32_t addr, pos_dyn;
481
482                 debug("\nPossition:\t%d/0x%x\n", i, pos);
483
484                 if (fseek(f, pos, SEEK_SET) < 0) {
485                         fprintf(stderr, "%s: %s: seek to %" PRIx32
486                                         " failed: %s\n",
487                                 argv[0], argv[1], pos, strerror(errno));
488                 }
489
490                 if (fread(&rela, sizeof(rela), 1, f) != 1) {
491                         fprintf(stderr, "%s: %s: read rela failed at %"
492                                         PRIx32 "\n",
493                                 argv[0], argv[1], pos);
494                         return 4;
495                 }
496
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);
500
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);
504
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);
508
509                 if (!supported_rela32(&swrela, &type))
510                         continue;
511
512                 if (swrela.r_offset < text_base) {
513                         fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
514                                 argv[0], argv[1], pos);
515                         return 4;
516                 }
517
518                 addr = swrela.r_offset - text_base;
519
520                 debug("Addr:\t0x%" PRIx32 "\n", addr);
521
522                 switch (type) {
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));
528                                 return 5;
529                         }
530
531                         debug("Write addend\n");
532
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);
536                                 return 4;
537                         }
538                         break;
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;
544
545                         debug("Index:\t%d\n", index);
546                         debug("Pos_dyn:\t0x%x\n", pos_dyn);
547
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));
552                                 return 5;
553                         }
554
555                         if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
556                                 fprintf(stderr, "%s: %s: read symbols failed at %"
557                                                 PRIx32 "\n",
558                                         argv[0], argv[1], pos_dyn);
559                                 return 4;
560                         }
561
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);
566
567                         value = swrela.r_addend + symbols.st_value;
568
569                         debug("Value:\t0x%x\n", value);
570
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));
575                                 return 5;
576                         }
577
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);
581                                 return 4;
582                         }
583
584                         break;
585                 case R_MICROBLAZE_NONE:
586                         debug("R_MICROBLAZE_NONE - skip\n");
587                         break;
588                 default:
589                         fprintf(stderr, "warning: unsupported relocation type %"
590                                 PRIu32 " at %" PRIx32 "\n",
591                                 type, rela.r_offset);
592                 }
593         }
594
595         return 0;
596 }
597
598 int main(int argc, char **argv)
599 {
600         FILE *f;
601         int ret;
602         uint64_t file_size;
603
604         if (argc != 3) {
605                 fprintf(stderr, "Statically apply ELF rela relocations\n");
606                 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
607                         argv[0]);
608                 return 1;
609         }
610
611         ret = decode_elf(argv);
612         if (ret) {
613                 fprintf(stderr, "ELF decoding failed\n");
614                 return ret;
615         }
616
617         if (rela_start > rela_end || rela_start < text_base) {
618                 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
619                 return 3;
620         }
621
622         rela_start -= text_base;
623         rela_end -= text_base;
624         dyn_start -= text_base;
625
626         f = fopen(argv[1], "r+b");
627         if (!f) {
628                 fprintf(stderr, "%s: Cannot open %s: %s\n",
629                         argv[0], argv[1], strerror(errno));
630                 return 2;
631         }
632
633         fseek(f, 0, SEEK_END);
634         file_size = ftell(f);
635         rewind(f);
636
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;
641         }
642
643         if (ei_class == 2)
644                 ret = rela_elf64(argv, f);
645         else
646                 ret = rela_elf32(argv, f);
647
648         if (fclose(f) < 0) {
649                 fprintf(stderr, "%s: %s: close failed: %s\n",
650                         argv[0], argv[1], strerror(errno));
651                 return 4;
652         }
653
654         return ret;
655 }