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, load_sz; 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_Phdr *phdr32; 206 Elf64_Phdr *phdr64; 207 Elf32_Shdr *shdr32; 208 Elf64_Shdr *shdr64; 209 struct stat st; 210 int err, fd; 211 void *elf; 212 bool skip; 213 214 fd = open(argv[1], O_RDWR); 215 if (fd == -1) { 216 fprintf(stderr, "Unable to open input file %s\n", argv[1]); 217 err = errno; 218 goto out_ret; 219 } 220 221 err = fstat(fd, &st); 222 if (err) { 223 fprintf(stderr, "Unable to fstat() input file\n"); 224 goto out_close_fd; 225 } 226 227 elf = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 228 if (elf == MAP_FAILED) { 229 fprintf(stderr, "Unable to mmap() input file\n"); 230 err = errno; 231 goto out_close_fd; 232 } 233 234 ehdr32 = elf; 235 ehdr64 = elf; 236 237 if (memcmp(&ehdr32->e_ident[EI_MAG0], ELFMAG, SELFMAG)) { 238 fprintf(stderr, "Input file is not an ELF\n"); 239 err = -EINVAL; 240 goto out_free_relocs; 241 } 242 243 if (ehdr32->e_ident[EI_VERSION] != EV_CURRENT) { 244 fprintf(stderr, "Unrecognised ELF version\n"); 245 err = -EINVAL; 246 goto out_free_relocs; 247 } 248 249 switch (ehdr32->e_ident[EI_CLASS]) { 250 case ELFCLASS32: 251 is_64 = false; 252 break; 253 case ELFCLASS64: 254 is_64 = true; 255 break; 256 default: 257 fprintf(stderr, "Unrecognised ELF class\n"); 258 err = -EINVAL; 259 goto out_free_relocs; 260 } 261 262 switch (ehdr32->e_ident[EI_DATA]) { 263 case ELFDATA2LSB: 264 is_be = false; 265 break; 266 case ELFDATA2MSB: 267 is_be = true; 268 break; 269 default: 270 fprintf(stderr, "Unrecognised ELF data encoding\n"); 271 err = -EINVAL; 272 goto out_free_relocs; 273 } 274 275 if (ehdr_field(e_type) != ET_EXEC) { 276 fprintf(stderr, "Input ELF is not an executable\n"); 277 printf("type 0x%lx\n", ehdr_field(e_type)); 278 err = -EINVAL; 279 goto out_free_relocs; 280 } 281 282 if (ehdr_field(e_machine) != EM_MIPS) { 283 fprintf(stderr, "Input ELF does not target MIPS\n"); 284 err = -EINVAL; 285 goto out_free_relocs; 286 } 287 288 phdr32 = elf + ehdr_field(e_phoff); 289 phdr64 = elf + ehdr_field(e_phoff); 290 shdr32 = elf + ehdr_field(e_shoff); 291 shdr64 = elf + ehdr_field(e_shoff); 292 shstrtab = elf + shdr_field(ehdr_field(e_shstrndx), sh_offset); 293 294 i_rel_shdr = UINT_MAX; 295 for (i = 0; i < ehdr_field(e_shnum); i++) { 296 sh_name = shstr(shdr_field(i, sh_name)); 297 298 if (!strcmp(sh_name, ".rel")) { 299 i_rel_shdr = i; 300 continue; 301 } 302 303 if (!strcmp(sh_name, ".text")) { 304 text_base = shdr_field(i, sh_addr); 305 continue; 306 } 307 } 308 if (i_rel_shdr == UINT_MAX) { 309 fprintf(stderr, "Unable to find .rel section\n"); 310 err = -EINVAL; 311 goto out_free_relocs; 312 } 313 if (!text_base) { 314 fprintf(stderr, "Unable to find .text base address\n"); 315 err = -EINVAL; 316 goto out_free_relocs; 317 } 318 319 rel_pfx = is_64 ? ".rela." : ".rel."; 320 321 for (i = 0; i < ehdr_field(e_shnum); i++) { 322 sh_type = shdr_field(i, sh_type); 323 if ((sh_type != SHT_REL) && (sh_type != SHT_RELA)) 324 continue; 325 326 sh_name = shstr(shdr_field(i, sh_name)); 327 if (strncmp(sh_name, rel_pfx, strlen(rel_pfx))) { 328 if (strcmp(sh_name, ".rel") && strcmp(sh_name, ".rel.dyn")) 329 fprintf(stderr, "WARNING: Unexpected reloc section name '%s'\n", sh_name); 330 continue; 331 } 332 333 /* 334 * Skip reloc sections which either don't correspond to another 335 * section in the ELF, or whose corresponding section isn't 336 * loaded as part of the U-Boot binary (ie. doesn't have the 337 * alloc flags set). 338 */ 339 skip = true; 340 for (j = 0; j < ehdr_field(e_shnum); j++) { 341 if (strcmp(&sh_name[strlen(rel_pfx) - 1], shstr(shdr_field(j, sh_name)))) 342 continue; 343 344 skip = !(shdr_field(j, sh_flags) & SHF_ALLOC); 345 break; 346 } 347 if (skip) 348 continue; 349 350 sh_offset = shdr_field(i, sh_offset); 351 sh_entsize = shdr_field(i, sh_entsize); 352 sh_entries = shdr_field(i, sh_size) / sh_entsize; 353 354 if (sh_type == SHT_REL) { 355 if (is_64) { 356 fprintf(stderr, "REL-style reloc in MIPS64 ELF?\n"); 357 err = -EINVAL; 358 goto out_free_relocs; 359 } else { 360 parse_fn = parse_mips32_rel; 361 } 362 } else { 363 if (is_64) { 364 parse_fn = parse_mips64_rela; 365 } else { 366 fprintf(stderr, "RELA-style reloc in MIPS32 ELF?\n"); 367 err = -EINVAL; 368 goto out_free_relocs; 369 } 370 } 371 372 for (j = 0; j < sh_entries; j++) { 373 err = parse_fn(elf + sh_offset + (j * sh_entsize)); 374 if (err) 375 goto out_free_relocs; 376 } 377 } 378 379 /* Sort relocs in ascending order of offset */ 380 qsort(relocs, relocs_idx, sizeof(*relocs), compare_relocs); 381 382 /* Make reloc offsets relative to their predecessor */ 383 for (i = relocs_idx - 1; i > 0; i--) 384 relocs[i].offset -= relocs[i - 1].offset; 385 386 /* Write the relocations to the .rel section */ 387 buf = buf_start = elf + shdr_field(i_rel_shdr, sh_offset); 388 for (i = 0; i < relocs_idx; i++) { 389 output_uint(&buf, relocs[i].type); 390 output_uint(&buf, relocs[i].offset >> 2); 391 } 392 393 /* Write a terminating R_MIPS_NONE (0) */ 394 output_uint(&buf, R_MIPS_NONE); 395 396 /* Ensure the relocs didn't overflow the .rel section */ 397 rel_size = shdr_field(i_rel_shdr, sh_size); 398 rel_actual_size = buf - buf_start; 399 if (rel_actual_size > rel_size) { 400 fprintf(stderr, "Relocs overflowed .rel section\n"); 401 return -ENOMEM; 402 } 403 404 /* Update the .rel section's size */ 405 set_shdr_field(i_rel_shdr, sh_size, rel_actual_size); 406 407 /* Shrink the PT_LOAD program header filesz (ie. shrink u-boot.bin) */ 408 for (i = 0; i < ehdr_field(e_phnum); i++) { 409 if (phdr_field(i, p_type) != PT_LOAD) 410 continue; 411 412 load_sz = phdr_field(i, p_filesz); 413 load_sz -= rel_size - rel_actual_size; 414 set_phdr_field(i, p_filesz, load_sz); 415 break; 416 } 417 418 /* Make sure data is written back to the file */ 419 err = msync(elf, st.st_size, MS_SYNC); 420 if (err) { 421 fprintf(stderr, "Failed to msync: %d\n", errno); 422 goto out_free_relocs; 423 } 424 425 out_free_relocs: 426 free(relocs); 427 munmap(elf, st.st_size); 428 out_close_fd: 429 close(fd); 430 out_ret: 431 return err; 432 } 433