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