mips-relocs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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;
  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_Shdr *shdr32;
  177. Elf64_Shdr *shdr64;
  178. struct stat st;
  179. int err, fd;
  180. void *elf;
  181. bool skip;
  182. fd = open(argv[1], O_RDWR);
  183. if (fd == -1) {
  184. fprintf(stderr, "Unable to open input file %s\n", argv[1]);
  185. err = errno;
  186. goto out_ret;
  187. }
  188. err = fstat(fd, &st);
  189. if (err) {
  190. fprintf(stderr, "Unable to fstat() input file\n");
  191. goto out_close_fd;
  192. }
  193. elf = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  194. if (elf == MAP_FAILED) {
  195. fprintf(stderr, "Unable to mmap() input file\n");
  196. err = errno;
  197. goto out_close_fd;
  198. }
  199. ehdr32 = elf;
  200. ehdr64 = elf;
  201. if (memcmp(&ehdr32->e_ident[EI_MAG0], ELFMAG, SELFMAG)) {
  202. fprintf(stderr, "Input file is not an ELF\n");
  203. err = -EINVAL;
  204. goto out_free_relocs;
  205. }
  206. if (ehdr32->e_ident[EI_VERSION] != EV_CURRENT) {
  207. fprintf(stderr, "Unrecognised ELF version\n");
  208. err = -EINVAL;
  209. goto out_free_relocs;
  210. }
  211. switch (ehdr32->e_ident[EI_CLASS]) {
  212. case ELFCLASS32:
  213. is_64 = false;
  214. break;
  215. case ELFCLASS64:
  216. is_64 = true;
  217. break;
  218. default:
  219. fprintf(stderr, "Unrecognised ELF class\n");
  220. err = -EINVAL;
  221. goto out_free_relocs;
  222. }
  223. switch (ehdr32->e_ident[EI_DATA]) {
  224. case ELFDATA2LSB:
  225. is_be = false;
  226. break;
  227. case ELFDATA2MSB:
  228. is_be = true;
  229. break;
  230. default:
  231. fprintf(stderr, "Unrecognised ELF data encoding\n");
  232. err = -EINVAL;
  233. goto out_free_relocs;
  234. }
  235. if (ehdr_field(e_type) != ET_EXEC) {
  236. fprintf(stderr, "Input ELF is not an executable\n");
  237. printf("type 0x%lx\n", ehdr_field(e_type));
  238. err = -EINVAL;
  239. goto out_free_relocs;
  240. }
  241. if (ehdr_field(e_machine) != EM_MIPS) {
  242. fprintf(stderr, "Input ELF does not target MIPS\n");
  243. err = -EINVAL;
  244. goto out_free_relocs;
  245. }
  246. shdr32 = elf + ehdr_field(e_shoff);
  247. shdr64 = elf + ehdr_field(e_shoff);
  248. shstrtab = elf + shdr_field(ehdr_field(e_shstrndx), sh_offset);
  249. i_rel_shdr = UINT_MAX;
  250. for (i = 0; i < ehdr_field(e_shnum); i++) {
  251. sh_name = shstr(shdr_field(i, sh_name));
  252. if (!strcmp(sh_name, ".data.reloc")) {
  253. i_rel_shdr = i;
  254. continue;
  255. }
  256. if (!strcmp(sh_name, ".text")) {
  257. text_base = shdr_field(i, sh_addr);
  258. continue;
  259. }
  260. }
  261. if (i_rel_shdr == UINT_MAX) {
  262. fprintf(stderr, "Unable to find .rel section\n");
  263. err = -EINVAL;
  264. goto out_free_relocs;
  265. }
  266. if (!text_base) {
  267. fprintf(stderr, "Unable to find .text base address\n");
  268. err = -EINVAL;
  269. goto out_free_relocs;
  270. }
  271. rel_pfx = is_64 ? ".rela." : ".rel.";
  272. for (i = 0; i < ehdr_field(e_shnum); i++) {
  273. sh_type = shdr_field(i, sh_type);
  274. if ((sh_type != SHT_REL) && (sh_type != SHT_RELA))
  275. continue;
  276. sh_name = shstr(shdr_field(i, sh_name));
  277. if (strncmp(sh_name, rel_pfx, strlen(rel_pfx))) {
  278. if (strcmp(sh_name, ".rel") && strcmp(sh_name, ".rel.dyn"))
  279. fprintf(stderr, "WARNING: Unexpected reloc section name '%s'\n", sh_name);
  280. continue;
  281. }
  282. /*
  283. * Skip reloc sections which either don't correspond to another
  284. * section in the ELF, or whose corresponding section isn't
  285. * loaded as part of the U-Boot binary (ie. doesn't have the
  286. * alloc flags set).
  287. */
  288. skip = true;
  289. for (j = 0; j < ehdr_field(e_shnum); j++) {
  290. if (strcmp(&sh_name[strlen(rel_pfx) - 1], shstr(shdr_field(j, sh_name))))
  291. continue;
  292. skip = !(shdr_field(j, sh_flags) & SHF_ALLOC);
  293. break;
  294. }
  295. if (skip)
  296. continue;
  297. sh_offset = shdr_field(i, sh_offset);
  298. sh_entsize = shdr_field(i, sh_entsize);
  299. sh_entries = shdr_field(i, sh_size) / sh_entsize;
  300. if (sh_type == SHT_REL) {
  301. if (is_64) {
  302. fprintf(stderr, "REL-style reloc in MIPS64 ELF?\n");
  303. err = -EINVAL;
  304. goto out_free_relocs;
  305. } else {
  306. parse_fn = parse_mips32_rel;
  307. }
  308. } else {
  309. if (is_64) {
  310. parse_fn = parse_mips64_rela;
  311. } else {
  312. fprintf(stderr, "RELA-style reloc in MIPS32 ELF?\n");
  313. err = -EINVAL;
  314. goto out_free_relocs;
  315. }
  316. }
  317. for (j = 0; j < sh_entries; j++) {
  318. err = parse_fn(elf + sh_offset + (j * sh_entsize));
  319. if (err)
  320. goto out_free_relocs;
  321. }
  322. }
  323. /* Sort relocs in ascending order of offset */
  324. qsort(relocs, relocs_idx, sizeof(*relocs), compare_relocs);
  325. /* Make reloc offsets relative to their predecessor */
  326. for (i = relocs_idx - 1; i > 0; i--)
  327. relocs[i].offset -= relocs[i - 1].offset;
  328. /* Write the relocations to the .rel section */
  329. buf = buf_start = elf + shdr_field(i_rel_shdr, sh_offset);
  330. for (i = 0; i < relocs_idx; i++) {
  331. output_uint(&buf, relocs[i].type);
  332. output_uint(&buf, relocs[i].offset >> 2);
  333. }
  334. /* Write a terminating R_MIPS_NONE (0) */
  335. output_uint(&buf, R_MIPS_NONE);
  336. /* Ensure the relocs didn't overflow the .rel section */
  337. rel_size = shdr_field(i_rel_shdr, sh_size);
  338. rel_actual_size = buf - buf_start;
  339. if (rel_actual_size > rel_size) {
  340. fprintf(stderr, "Relocations overflow available space of 0x%lx (required 0x%lx)!\n",
  341. rel_size, rel_actual_size);
  342. fprintf(stderr, "Please adjust CONFIG_MIPS_RELOCATION_TABLE_SIZE to at least 0x%lx\n",
  343. (rel_actual_size + 0x100) & ~0xFF);
  344. err = -ENOMEM;
  345. goto out_free_relocs;
  346. }
  347. /* Make sure data is written back to the file */
  348. err = msync(elf, st.st_size, MS_SYNC);
  349. if (err) {
  350. fprintf(stderr, "Failed to msync: %d\n", errno);
  351. goto out_free_relocs;
  352. }
  353. out_free_relocs:
  354. free(relocs);
  355. munmap(elf, st.st_size);
  356. out_close_fd:
  357. close(fd);
  358. out_ret:
  359. return err;
  360. }