Merge branch 'master' into next
[platform/kernel/u-boot.git] / tools / mips-relocs.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * MIPS Relocation Data Generator
4  *
5  * Copyright (c) 2017 Imagination Technologies Ltd.
6  */
7
8 #include <assert.h>
9 #include <elf.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <limits.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/mman.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19
20 #include <asm/relocs.h>
21
22 #define hdr_field(pfx, idx, field) ({                           \
23         uint64_t _val;                                          \
24         unsigned int _size;                                     \
25                                                                 \
26         if (is_64) {                                            \
27                 _val = pfx##hdr64[idx].field;                   \
28                 _size = sizeof(pfx##hdr64[0].field);            \
29         } else {                                                \
30                 _val = pfx##hdr32[idx].field;                   \
31                 _size = sizeof(pfx##hdr32[0].field);            \
32         }                                                       \
33                                                                 \
34         switch (_size) {                                        \
35         case 1:                                                 \
36                 break;                                          \
37         case 2:                                                 \
38                 _val = is_be ? be16toh(_val) : le16toh(_val);   \
39                 break;                                          \
40         case 4:                                                 \
41                 _val = is_be ? be32toh(_val) : le32toh(_val);   \
42                 break;                                          \
43         case 8:                                                 \
44                 _val = is_be ? be64toh(_val) : le64toh(_val);   \
45                 break;                                          \
46         }                                                       \
47                                                                 \
48         _val;                                                   \
49 })
50
51 #define set_hdr_field(pfx, idx, field, val) ({                  \
52         uint64_t _val;                                          \
53         unsigned int _size;                                     \
54                                                                 \
55         if (is_64)                                              \
56                 _size = sizeof(pfx##hdr64[0].field);            \
57         else                                                    \
58                 _size = sizeof(pfx##hdr32[0].field);            \
59                                                                 \
60         switch (_size) {                                        \
61         case 1:                                                 \
62                 _val = val;                                     \
63                 break;                                          \
64         case 2:                                                 \
65                 _val = is_be ? htobe16(val) : htole16(val);     \
66                 break;                                          \
67         case 4:                                                 \
68                 _val = is_be ? htobe32(val) : htole32(val);     \
69                 break;                                          \
70         case 8:                                                 \
71                 _val = is_be ? htobe64(val) : htole64(val);     \
72                 break;                                          \
73         default:                                                \
74                 /* We should never reach here */                \
75                 _val = 0;                                       \
76                 assert(0);                                      \
77                 break;                                          \
78         }                                                       \
79                                                                 \
80         if (is_64)                                              \
81                 pfx##hdr64[idx].field = _val;                   \
82         else                                                    \
83                 pfx##hdr32[idx].field = _val;                   \
84 })
85
86 #define ehdr_field(field) \
87         hdr_field(e, 0, field)
88 #define phdr_field(idx, field) \
89         hdr_field(p, idx, field)
90 #define shdr_field(idx, field) \
91         hdr_field(s, idx, field)
92
93 #define set_phdr_field(idx, field, val) \
94         set_hdr_field(p, idx, field, val)
95 #define set_shdr_field(idx, field, val) \
96         set_hdr_field(s, idx, field, val)
97
98 #define shstr(idx) (&shstrtab[idx])
99
100 bool is_64, is_be;
101 uint64_t text_base;
102
103 struct mips_reloc {
104         uint8_t type;
105         uint64_t offset;
106 } *relocs;
107 size_t relocs_sz, relocs_idx;
108
109 static int add_reloc(unsigned int type, uint64_t off)
110 {
111         struct mips_reloc *new;
112         size_t new_sz;
113
114         switch (type) {
115         case R_MIPS_NONE:
116         case R_MIPS_LO16:
117         case R_MIPS_PC16:
118         case R_MIPS_HIGHER:
119         case R_MIPS_HIGHEST:
120         case R_MIPS_PC21_S2:
121         case R_MIPS_PC26_S2:
122                 /* Skip these relocs */
123                 return 0;
124
125         default:
126                 break;
127         }
128
129         if (relocs_idx == relocs_sz) {
130                 new_sz = relocs_sz ? relocs_sz * 2 : 128;
131                 new = realloc(relocs, new_sz * sizeof(*relocs));
132                 if (!new) {
133                         fprintf(stderr, "Out of memory\n");
134                         return -ENOMEM;
135                 }
136
137                 relocs = new;
138                 relocs_sz = new_sz;
139         }
140
141         relocs[relocs_idx++] = (struct mips_reloc){
142                 .type = type,
143                 .offset = off,
144         };
145
146         return 0;
147 }
148
149 static int parse_mips32_rel(const void *_rel)
150 {
151         const Elf32_Rel *rel = _rel;
152         uint32_t off, type;
153
154         off = is_be ? be32toh(rel->r_offset) : le32toh(rel->r_offset);
155         off -= text_base;
156
157         type = is_be ? be32toh(rel->r_info) : le32toh(rel->r_info);
158         type = ELF32_R_TYPE(type);
159
160         return add_reloc(type, off);
161 }
162
163 static int parse_mips64_rela(const void *_rel)
164 {
165         const Elf64_Rela *rel = _rel;
166         uint64_t off, type;
167
168         off = is_be ? be64toh(rel->r_offset) : le64toh(rel->r_offset);
169         off -= text_base;
170
171         type = rel->r_info >> (64 - 8);
172
173         return add_reloc(type, off);
174 }
175
176 static void output_uint(uint8_t **buf, uint64_t val)
177 {
178         uint64_t tmp;
179
180         do {
181                 tmp = val & 0x7f;
182                 val >>= 7;
183                 tmp |= !!val << 7;
184                 *(*buf)++ = tmp;
185         } while (val);
186 }
187
188 static int compare_relocs(const void *a, const void *b)
189 {
190         const struct mips_reloc *ra = a, *rb = b;
191
192         return ra->offset - rb->offset;
193 }
194
195 int main(int argc, char *argv[])
196 {
197         unsigned int i, j, i_rel_shdr, sh_type, sh_entsize, sh_entries;
198         size_t rel_size, rel_actual_size;
199         const char *shstrtab, *sh_name, *rel_pfx;
200         int (*parse_fn)(const void *rel);
201         uint8_t *buf_start, *buf;
202         const Elf32_Ehdr *ehdr32;
203         const Elf64_Ehdr *ehdr64;
204         uintptr_t sh_offset;
205         Elf32_Shdr *shdr32;
206         Elf64_Shdr *shdr64;
207         struct stat st;
208         int err, fd;
209         void *elf;
210         bool skip;
211
212         fd = open(argv[1], O_RDWR);
213         if (fd == -1) {
214                 fprintf(stderr, "Unable to open input file %s\n", argv[1]);
215                 err = errno;
216                 goto out_ret;
217         }
218
219         err = fstat(fd, &st);
220         if (err) {
221                 fprintf(stderr, "Unable to fstat() input file\n");
222                 goto out_close_fd;
223         }
224
225         elf = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
226         if (elf == MAP_FAILED) {
227                 fprintf(stderr, "Unable to mmap() input file\n");
228                 err = errno;
229                 goto out_close_fd;
230         }
231
232         ehdr32 = elf;
233         ehdr64 = elf;
234
235         if (memcmp(&ehdr32->e_ident[EI_MAG0], ELFMAG, SELFMAG)) {
236                 fprintf(stderr, "Input file is not an ELF\n");
237                 err = -EINVAL;
238                 goto out_free_relocs;
239         }
240
241         if (ehdr32->e_ident[EI_VERSION] != EV_CURRENT) {
242                 fprintf(stderr, "Unrecognised ELF version\n");
243                 err = -EINVAL;
244                 goto out_free_relocs;
245         }
246
247         switch (ehdr32->e_ident[EI_CLASS]) {
248         case ELFCLASS32:
249                 is_64 = false;
250                 break;
251         case ELFCLASS64:
252                 is_64 = true;
253                 break;
254         default:
255                 fprintf(stderr, "Unrecognised ELF class\n");
256                 err = -EINVAL;
257                 goto out_free_relocs;
258         }
259
260         switch (ehdr32->e_ident[EI_DATA]) {
261         case ELFDATA2LSB:
262                 is_be = false;
263                 break;
264         case ELFDATA2MSB:
265                 is_be = true;
266                 break;
267         default:
268                 fprintf(stderr, "Unrecognised ELF data encoding\n");
269                 err = -EINVAL;
270                 goto out_free_relocs;
271         }
272
273         if (ehdr_field(e_type) != ET_EXEC) {
274                 fprintf(stderr, "Input ELF is not an executable\n");
275                 printf("type 0x%lx\n", ehdr_field(e_type));
276                 err = -EINVAL;
277                 goto out_free_relocs;
278         }
279
280         if (ehdr_field(e_machine) != EM_MIPS) {
281                 fprintf(stderr, "Input ELF does not target MIPS\n");
282                 err = -EINVAL;
283                 goto out_free_relocs;
284         }
285
286         shdr32 = elf + ehdr_field(e_shoff);
287         shdr64 = elf + ehdr_field(e_shoff);
288         shstrtab = elf + shdr_field(ehdr_field(e_shstrndx), sh_offset);
289
290         i_rel_shdr = UINT_MAX;
291         for (i = 0; i < ehdr_field(e_shnum); i++) {
292                 sh_name = shstr(shdr_field(i, sh_name));
293
294                 if (!strcmp(sh_name, ".data.reloc")) {
295                         i_rel_shdr = i;
296                         continue;
297                 }
298
299                 if (!strcmp(sh_name, ".text")) {
300                         text_base = shdr_field(i, sh_addr);
301                         continue;
302                 }
303         }
304         if (i_rel_shdr == UINT_MAX) {
305                 fprintf(stderr, "Unable to find .rel section\n");
306                 err = -EINVAL;
307                 goto out_free_relocs;
308         }
309         if (!text_base) {
310                 fprintf(stderr, "Unable to find .text base address\n");
311                 err = -EINVAL;
312                 goto out_free_relocs;
313         }
314
315         rel_pfx = is_64 ? ".rela." : ".rel.";
316
317         for (i = 0; i < ehdr_field(e_shnum); i++) {
318                 sh_type = shdr_field(i, sh_type);
319                 if ((sh_type != SHT_REL) && (sh_type != SHT_RELA))
320                         continue;
321
322                 sh_name = shstr(shdr_field(i, sh_name));
323                 if (strncmp(sh_name, rel_pfx, strlen(rel_pfx))) {
324                         if (strcmp(sh_name, ".rel") && strcmp(sh_name, ".rel.dyn"))
325                                 fprintf(stderr, "WARNING: Unexpected reloc section name '%s'\n", sh_name);
326                         continue;
327                 }
328
329                 /*
330                  * Skip reloc sections which either don't correspond to another
331                  * section in the ELF, or whose corresponding section isn't
332                  * loaded as part of the U-Boot binary (ie. doesn't have the
333                  * alloc flags set).
334                  */
335                 skip = true;
336                 for (j = 0; j < ehdr_field(e_shnum); j++) {
337                         if (strcmp(&sh_name[strlen(rel_pfx) - 1], shstr(shdr_field(j, sh_name))))
338                                 continue;
339
340                         skip = !(shdr_field(j, sh_flags) & SHF_ALLOC);
341                         break;
342                 }
343                 if (skip)
344                         continue;
345
346                 sh_offset = shdr_field(i, sh_offset);
347                 sh_entsize = shdr_field(i, sh_entsize);
348                 sh_entries = shdr_field(i, sh_size) / sh_entsize;
349
350                 if (sh_type == SHT_REL) {
351                         if (is_64) {
352                                 fprintf(stderr, "REL-style reloc in MIPS64 ELF?\n");
353                                 err = -EINVAL;
354                                 goto out_free_relocs;
355                         } else {
356                                 parse_fn = parse_mips32_rel;
357                         }
358                 } else {
359                         if (is_64) {
360                                 parse_fn = parse_mips64_rela;
361                         } else {
362                                 fprintf(stderr, "RELA-style reloc in MIPS32 ELF?\n");
363                                 err = -EINVAL;
364                                 goto out_free_relocs;
365                         }
366                 }
367
368                 for (j = 0; j < sh_entries; j++) {
369                         err = parse_fn(elf + sh_offset + (j * sh_entsize));
370                         if (err)
371                                 goto out_free_relocs;
372                 }
373         }
374
375         /* Sort relocs in ascending order of offset */
376         qsort(relocs, relocs_idx, sizeof(*relocs), compare_relocs);
377
378         /* Make reloc offsets relative to their predecessor */
379         for (i = relocs_idx - 1; i > 0; i--)
380                 relocs[i].offset -= relocs[i - 1].offset;
381
382         /* Write the relocations to the .rel section */
383         buf = buf_start = elf + shdr_field(i_rel_shdr, sh_offset);
384         for (i = 0; i < relocs_idx; i++) {
385                 output_uint(&buf, relocs[i].type);
386                 output_uint(&buf, relocs[i].offset >> 2);
387         }
388
389         /* Write a terminating R_MIPS_NONE (0) */
390         output_uint(&buf, R_MIPS_NONE);
391
392         /* Ensure the relocs didn't overflow the .rel section */
393         rel_size = shdr_field(i_rel_shdr, sh_size);
394         rel_actual_size = buf - buf_start;
395         if (rel_actual_size > rel_size) {
396                 fprintf(stderr, "Relocations overflow available space of 0x%lx (required 0x%lx)!\n",
397                         rel_size, rel_actual_size);
398                 fprintf(stderr, "Please adjust CONFIG_MIPS_RELOCATION_TABLE_SIZE to at least 0x%lx\n",
399                         (rel_actual_size + 0x100) & ~0xFF);
400                 err = -ENOMEM;
401                 goto out_free_relocs;
402         }
403
404         /* Make sure data is written back to the file */
405         err = msync(elf, st.st_size, MS_SYNC);
406         if (err) {
407                 fprintf(stderr, "Failed to msync: %d\n", errno);
408                 goto out_free_relocs;
409         }
410
411 out_free_relocs:
412         free(relocs);
413         munmap(elf, st.st_size);
414 out_close_fd:
415         close(fd);
416 out_ret:
417         return err;
418 }