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