mips-relocs.c 9.5 KB

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