mips-relocs.c 9.5 KB

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