tools: relocate-rela: Remove guard around R_AARCH64_RELATIVE
[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         case R_AARCH64_RELATIVE:
47                 return true;
48         default:
49                 fprintf(stderr, "warning: unsupported relocation type %"
50                                 PRIu32 " at %" PRIx64 "\n",
51                         type, rela->r_offset);
52
53                 return false;
54         }
55 }
56
57 static int decode_elf64(FILE *felf, char **argv)
58 {
59         size_t size;
60         Elf64_Ehdr header;
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 */
65         int ret, i, machine;
66         char *sh_str;
67
68         debug("64bit version\n");
69
70         /* Make sure we are at start */
71         rewind(felf);
72
73         size = fread(&header, 1, sizeof(header), felf);
74         if (size != sizeof(header)) {
75                 fclose(felf);
76                 return 25;
77         }
78
79         machine = le16_to_cpu(header.e_machine);
80         debug("Machine\t%d\n", machine);
81
82         if (machine != EM_AARCH64) {
83                 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
84                 return 30;
85         }
86
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);
91
92         sh_table = malloc(section_header_size);
93         if (!sh_table) {
94                 fprintf(stderr, "%s: Cannot allocate space for section header\n",
95                         argv[0]);
96                 fclose(felf);
97                 return 26;
98         }
99
100         ret = fseek(felf, section_header_base, SEEK_SET);
101         if (ret) {
102                 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
103                         argv[0], ret, section_header_base);
104                 free(sh_table);
105                 fclose(felf);
106                 return 26;
107         }
108
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);
113                 free(sh_table);
114                 fclose(felf);
115                 return 27;
116         }
117
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);
121
122         sh_str = malloc(sh_size);
123         if (!sh_str) {
124                 fprintf(stderr, "malloc failed\n");
125                 free(sh_table);
126                 fclose(felf);
127                 return 28;
128         }
129
130         /*
131          * Specifies the byte offset from the beginning of the file
132          * to the first byte in the section.
133          */
134         sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
135         sh_num = le16_to_cpu(header.e_shnum);
136
137         ret = fseek(felf, sh_offset, SEEK_SET);
138         if (ret) {
139                 fprintf(stderr, "Setting up sh_offset failed\n");
140                 free(sh_str);
141                 free(sh_table);
142                 fclose(felf);
143                 return 29;
144         }
145
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);
150                 free(sh_str);
151                 free(sh_table);
152                 fclose(felf);
153                 return 30;
154         }
155
156         for (i = 0; i < sh_num; i++) {
157                 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
158
159                 debug("%s\n", sh_name);
160
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);
164
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;
172                         break;
173                 }
174         }
175
176         /* Clean up */
177         free(sh_str);
178         free(sh_table);
179         fclose(felf);
180
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);
184
185         if (!rela_start)
186                 return 1;
187
188         return 0;
189 }
190
191 static int decode_elf32(FILE *felf, char **argv)
192 {
193         size_t size;
194         Elf32_Ehdr header;
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 */
199         int ret, i, machine;
200         char *sh_str;
201
202         debug("32bit version\n");
203
204         /* Make sure we are at start */
205         rewind(felf);
206
207         size = fread(&header, 1, sizeof(header), felf);
208         if (size != sizeof(header)) {
209                 fclose(felf);
210                 return 25;
211         }
212
213         machine = le16_to_cpu(header.e_machine);
214         debug("Machine %d\n", machine);
215
216         if (machine != EM_MICROBLAZE) {
217                 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
218                 return 30;
219         }
220
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);
225
226         sh_table = malloc(section_header_size);
227         if (!sh_table) {
228                 fprintf(stderr, "%s: Cannot allocate space for section header\n",
229                         argv[0]);
230                 fclose(felf);
231                 return 26;
232         }
233
234         ret = fseek(felf, section_header_base, SEEK_SET);
235         if (ret) {
236                 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
237                         argv[0], ret, section_header_base);
238                 free(sh_table);
239                 fclose(felf);
240                 return 26;
241         }
242
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);
247                 free(sh_table);
248                 fclose(felf);
249                 return 27;
250         }
251
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);
255
256         sh_str = malloc(sh_size);
257         if (!sh_str) {
258                 fprintf(stderr, "malloc failed\n");
259                 free(sh_table);
260                 fclose(felf);
261                 return 28;
262         }
263
264         /*
265          * Specifies the byte offset from the beginning of the file
266          * to the first byte in the section.
267          */
268         sh_offset = le32_to_cpu(sh_table[sh_index].sh_offset);
269         sh_num = le16_to_cpu(header.e_shnum);
270
271         ret = fseek(felf, sh_offset, SEEK_SET);
272         if (ret) {
273                 fprintf(stderr, "Setting up sh_offset failed\n");
274                 free(sh_str);
275                 free(sh_table);
276                 fclose(felf);
277                 return 29;
278         }
279
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);
284                 free(sh_str);
285                 free(sh_table);
286                 fclose(felf);
287                 return 30;
288         }
289
290         for (i = 0; i < sh_num; i++) {
291                 char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
292
293                 debug("%s\n", sh_name);
294
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);
298
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;
306                 }
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);
312                         dyn_start = sh_addr;
313                 }
314         }
315
316         /* Clean up */
317         free(sh_str);
318         free(sh_table);
319         fclose(felf);
320
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);
325
326         if (!rela_start)
327                 return 1;
328
329         return 0;
330 }
331
332 static int decode_elf(char **argv)
333 {
334         FILE *felf;
335         size_t size;
336         unsigned char e_ident[EI_NIDENT];
337
338         felf = fopen(argv[2], "r+b");
339         if (!felf) {
340                 fprintf(stderr, "%s: Cannot open %s: %s\n",
341                         argv[0], argv[5], strerror(errno));
342                 return 2;
343         }
344
345         size = fread(e_ident, 1, EI_NIDENT, felf);
346         if (size != EI_NIDENT) {
347                 fclose(felf);
348                 return 25;
349         }
350
351         /* Check if this is really ELF file */
352         if (e_ident[0] != 0x7f &&
353             e_ident[1] != 'E' &&
354             e_ident[2] != 'L' &&
355             e_ident[3] != 'F') {
356                 fclose(felf);
357                 return 1;
358         }
359
360         ei_class = e_ident[4];
361         debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
362
363         if (ei_class == 2)
364                 return decode_elf64(felf, argv);
365
366         return decode_elf32(felf, argv);
367 }
368
369 static int rela_elf64(char **argv, FILE *f)
370 {
371         int i, num;
372
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]);
375                 return 3;
376         }
377
378         num = (rela_end - rela_start) / sizeof(Elf64_Rela);
379
380         for (i = 0; i < num; i++) {
381                 Elf64_Rela rela, swrela;
382                 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
383                 uint64_t addr;
384
385                 if (fseek(f, pos, SEEK_SET) < 0) {
386                         fprintf(stderr, "%s: %s: seek to %" PRIx64
387                                         " failed: %s\n",
388                                 argv[0], argv[1], pos, strerror(errno));
389                 }
390
391                 if (fread(&rela, sizeof(rela), 1, f) != 1) {
392                         fprintf(stderr, "%s: %s: read rela failed at %"
393                                         PRIx64 "\n",
394                                 argv[0], argv[1], pos);
395                         return 4;
396                 }
397
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);
401
402                 if (!supported_rela(&swrela))
403                         continue;
404
405                 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
406                       swrela.r_offset, swrela.r_info, swrela.r_addend);
407
408                 if (swrela.r_offset < text_base) {
409                         fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
410                                 argv[0], argv[1], pos);
411                         return 4;
412                 }
413
414                 addr = swrela.r_offset - text_base;
415
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));
420                 }
421
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);
425                         return 4;
426                 }
427         }
428
429         return 0;
430 }
431
432 static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
433 {
434         uint32_t mask = 0xffULL; /* would be different on 32-bit */
435         *type = rela->r_info & mask;
436
437         debug("Type:\t");
438
439         switch (*type) {
440         case R_MICROBLAZE_32:
441                 debug("R_MICROBLAZE_32\n");
442                 return true;
443         case R_MICROBLAZE_GLOB_DAT:
444                 debug("R_MICROBLAZE_GLOB_DAT\n");
445                 return true;
446         case R_MICROBLAZE_NONE:
447                 debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
448                 return false;
449         case R_MICROBLAZE_REL:
450                 debug("R_MICROBLAZE_REL\n");
451                 return true;
452         default:
453                 fprintf(stderr, "warning: unsupported relocation type %"
454                         PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
455
456                 return false;
457         }
458 }
459
460 static int rela_elf32(char **argv, FILE *f)
461 {
462         int i, num, index;
463         uint32_t value, type;
464
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]);
467                 return 3;
468         }
469
470         num = (rela_end - rela_start) / sizeof(Elf32_Rela);
471
472         debug("Number of entries: %u\n", num);
473
474         for (i = 0; i < num; i++) {
475                 Elf32_Rela rela, swrela;
476                 Elf32_Sym symbols;
477                 uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
478                 uint32_t addr, pos_dyn;
479
480                 debug("\nPossition:\t%d/0x%x\n", i, pos);
481
482                 if (fseek(f, pos, SEEK_SET) < 0) {
483                         fprintf(stderr, "%s: %s: seek to %" PRIx32
484                                         " failed: %s\n",
485                                 argv[0], argv[1], pos, strerror(errno));
486                 }
487
488                 if (fread(&rela, sizeof(rela), 1, f) != 1) {
489                         fprintf(stderr, "%s: %s: read rela failed at %"
490                                         PRIx32 "\n",
491                                 argv[0], argv[1], pos);
492                         return 4;
493                 }
494
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);
498
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);
502
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);
506
507                 if (!supported_rela32(&swrela, &type))
508                         continue;
509
510                 if (swrela.r_offset < text_base) {
511                         fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
512                                 argv[0], argv[1], pos);
513                         return 4;
514                 }
515
516                 addr = swrela.r_offset - text_base;
517
518                 debug("Addr:\t0x%" PRIx32 "\n", addr);
519
520                 switch (type) {
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));
526                                 return 5;
527                         }
528
529                         debug("Write addend\n");
530
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);
534                                 return 4;
535                         }
536                         break;
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;
542
543                         debug("Index:\t%d\n", index);
544                         debug("Pos_dyn:\t0x%x\n", pos_dyn);
545
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));
550                                 return 5;
551                         }
552
553                         if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
554                                 fprintf(stderr, "%s: %s: read symbols failed at %"
555                                                 PRIx32 "\n",
556                                         argv[0], argv[1], pos_dyn);
557                                 return 4;
558                         }
559
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);
564
565                         value = swrela.r_addend + symbols.st_value;
566
567                         debug("Value:\t0x%x\n", value);
568
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));
573                                 return 5;
574                         }
575
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);
579                                 return 4;
580                         }
581
582                         break;
583                 case R_MICROBLAZE_NONE:
584                         debug("R_MICROBLAZE_NONE - skip\n");
585                         break;
586                 default:
587                         fprintf(stderr, "warning: unsupported relocation type %"
588                                 PRIu32 " at %" PRIx32 "\n",
589                                 type, rela.r_offset);
590                 }
591         }
592
593         return 0;
594 }
595
596 int main(int argc, char **argv)
597 {
598         FILE *f;
599         int ret;
600         uint64_t file_size;
601
602         if (argc != 3) {
603                 fprintf(stderr, "Statically apply ELF rela relocations\n");
604                 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
605                         argv[0]);
606                 return 1;
607         }
608
609         ret = decode_elf(argv);
610         if (ret) {
611                 fprintf(stderr, "ELF decoding failed\n");
612                 return ret;
613         }
614
615         if (rela_start > rela_end || rela_start < text_base) {
616                 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
617                 return 3;
618         }
619
620         rela_start -= text_base;
621         rela_end -= text_base;
622         dyn_start -= text_base;
623
624         f = fopen(argv[1], "r+b");
625         if (!f) {
626                 fprintf(stderr, "%s: Cannot open %s: %s\n",
627                         argv[0], argv[1], strerror(errno));
628                 return 2;
629         }
630
631         fseek(f, 0, SEEK_END);
632         file_size = ftell(f);
633         rewind(f);
634
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;
639         }
640
641         if (ei_class == 2)
642                 ret = rela_elf64(argv, f);
643         else
644                 ret = rela_elf32(argv, f);
645
646         if (fclose(f) < 0) {
647                 fprintf(stderr, "%s: %s: close failed: %s\n",
648                         argv[0], argv[1], strerror(errno));
649                 return 4;
650         }
651
652         return ret;
653 }