relocate-rela.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
  2. /*
  3. * Copyright 2013 Freescale Semiconductor, Inc.
  4. *
  5. * 64-bit and little-endian target only until we need to support a different
  6. * arch that needs this.
  7. */
  8. #include <elf.h>
  9. #include <errno.h>
  10. #include <inttypes.h>
  11. #include <stdarg.h>
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "compiler.h"
  17. #ifndef EM_AARCH64
  18. #define EM_AARCH64 183
  19. #endif
  20. #ifndef R_AARCH64_RELATIVE
  21. #define R_AARCH64_RELATIVE 1027
  22. #endif
  23. #ifndef EM_MICROBLAZE
  24. #define EM_MICROBLAZE 189
  25. #endif
  26. #ifndef R_MICROBLAZE_NONE
  27. #define R_MICROBLAZE_NONE 0
  28. #endif
  29. #ifndef R_MICROBLAZE_32
  30. #define R_MICROBLAZE_32 1
  31. #endif
  32. #ifndef R_MICROBLAZE_REL
  33. #define R_MICROBLAZE_REL 16
  34. #endif
  35. #ifndef R_MICROBLAZE_GLOB_DAT
  36. #define R_MICROBLAZE_GLOB_DAT 18
  37. #endif
  38. static int ei_class;
  39. static int ei_data;
  40. static uint64_t rela_start, rela_end, text_base, dyn_start;
  41. static const bool debug_en;
  42. static void debug(const char *fmt, ...)
  43. {
  44. va_list args;
  45. if (debug_en) {
  46. va_start(args, fmt);
  47. vprintf(fmt, args);
  48. va_end(args);
  49. }
  50. }
  51. static uint16_t elf16_to_cpu(uint16_t data)
  52. {
  53. if (ei_data == ELFDATA2LSB)
  54. return le16_to_cpu(data);
  55. return be16_to_cpu(data);
  56. }
  57. static uint32_t elf32_to_cpu(uint32_t data)
  58. {
  59. if (ei_data == ELFDATA2LSB)
  60. return le32_to_cpu(data);
  61. return be32_to_cpu(data);
  62. }
  63. static bool supported_rela(Elf64_Rela *rela)
  64. {
  65. uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
  66. uint32_t type = rela->r_info & mask;
  67. switch (type) {
  68. case R_AARCH64_RELATIVE:
  69. return true;
  70. default:
  71. fprintf(stderr, "warning: unsupported relocation type %"
  72. PRIu32 " at %" PRIx64 "\n",
  73. type, rela->r_offset);
  74. return false;
  75. }
  76. }
  77. static int decode_elf64(FILE *felf, char **argv)
  78. {
  79. size_t size;
  80. Elf64_Ehdr header;
  81. uint64_t section_header_base, section_header_size;
  82. uint64_t sh_addr, sh_offset, sh_size;
  83. Elf64_Half sh_index, sh_num;
  84. Elf64_Shdr *sh_table; /* Elf symbol table */
  85. int ret, i, machine;
  86. char *sh_str;
  87. debug("64bit version\n");
  88. /* Make sure we are at start */
  89. rewind(felf);
  90. size = fread(&header, 1, sizeof(header), felf);
  91. if (size != sizeof(header)) {
  92. fclose(felf);
  93. return 25;
  94. }
  95. machine = le16_to_cpu(header.e_machine);
  96. debug("Machine\t%d\n", machine);
  97. if (machine != EM_AARCH64) {
  98. fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
  99. return 30;
  100. }
  101. text_base = le64_to_cpu(header.e_entry);
  102. section_header_base = le64_to_cpu(header.e_shoff);
  103. section_header_size = le16_to_cpu(header.e_shentsize) *
  104. le16_to_cpu(header.e_shnum);
  105. sh_table = malloc(section_header_size);
  106. if (!sh_table) {
  107. fprintf(stderr, "%s: Cannot allocate space for section header\n",
  108. argv[0]);
  109. fclose(felf);
  110. return 26;
  111. }
  112. ret = fseek(felf, section_header_base, SEEK_SET);
  113. if (ret) {
  114. fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
  115. argv[0], ret, section_header_base);
  116. free(sh_table);
  117. fclose(felf);
  118. return 26;
  119. }
  120. size = fread(sh_table, 1, section_header_size, felf);
  121. if (size != section_header_size) {
  122. fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
  123. argv[0], size, section_header_size);
  124. free(sh_table);
  125. fclose(felf);
  126. return 27;
  127. }
  128. sh_index = le16_to_cpu(header.e_shstrndx);
  129. sh_size = le64_to_cpu(sh_table[sh_index].sh_size);
  130. debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
  131. sh_str = malloc(sh_size);
  132. if (!sh_str) {
  133. fprintf(stderr, "malloc failed\n");
  134. free(sh_table);
  135. fclose(felf);
  136. return 28;
  137. }
  138. /*
  139. * Specifies the byte offset from the beginning of the file
  140. * to the first byte in the section.
  141. */
  142. sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset);
  143. sh_num = le16_to_cpu(header.e_shnum);
  144. ret = fseek(felf, sh_offset, SEEK_SET);
  145. if (ret) {
  146. fprintf(stderr, "Setting up sh_offset failed\n");
  147. free(sh_str);
  148. free(sh_table);
  149. fclose(felf);
  150. return 29;
  151. }
  152. size = fread(sh_str, 1, sh_size, felf);
  153. if (size != sh_size) {
  154. fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
  155. argv[0], size, sh_size);
  156. free(sh_str);
  157. free(sh_table);
  158. fclose(felf);
  159. return 30;
  160. }
  161. for (i = 0; i < sh_num; i++) {
  162. char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name);
  163. debug("%s\n", sh_name);
  164. sh_addr = le64_to_cpu(sh_table[i].sh_addr);
  165. sh_offset = le64_to_cpu(sh_table[i].sh_offset);
  166. sh_size = le64_to_cpu(sh_table[i].sh_size);
  167. if (!strcmp(".rela.dyn", sh_name)) {
  168. debug("Found section\t\".rela_dyn\"\n");
  169. debug(" at addr\t0x%08x\n", sh_addr);
  170. debug(" at offset\t0x%08x\n", sh_offset);
  171. debug(" of size\t0x%08x\n", sh_size);
  172. rela_start = sh_addr;
  173. rela_end = rela_start + sh_size;
  174. break;
  175. }
  176. }
  177. /* Clean up */
  178. free(sh_str);
  179. free(sh_table);
  180. fclose(felf);
  181. debug("text_base\t0x%08lx\n", text_base);
  182. debug("rela_start\t0x%08lx\n", rela_start);
  183. debug("rela_end\t0x%08lx\n", rela_end);
  184. if (!rela_start)
  185. return 1;
  186. return 0;
  187. }
  188. static int decode_elf32(FILE *felf, char **argv)
  189. {
  190. size_t size;
  191. Elf32_Ehdr header;
  192. uint64_t section_header_base, section_header_size;
  193. uint32_t sh_addr, sh_offset, sh_size;
  194. Elf32_Half sh_index, sh_num;
  195. Elf32_Shdr *sh_table; /* Elf symbol table */
  196. int ret, i, machine;
  197. char *sh_str;
  198. debug("32bit version\n");
  199. /* Make sure we are at start */
  200. rewind(felf);
  201. size = fread(&header, 1, sizeof(header), felf);
  202. if (size != sizeof(header)) {
  203. fclose(felf);
  204. return 25;
  205. }
  206. machine = elf16_to_cpu(header.e_machine);
  207. debug("Machine %d\n", machine);
  208. if (machine != EM_MICROBLAZE) {
  209. fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
  210. return 30;
  211. }
  212. text_base = elf32_to_cpu(header.e_entry);
  213. section_header_base = elf32_to_cpu(header.e_shoff);
  214. section_header_size = elf16_to_cpu(header.e_shentsize) *
  215. elf16_to_cpu(header.e_shnum);
  216. sh_table = malloc(section_header_size);
  217. if (!sh_table) {
  218. fprintf(stderr, "%s: Cannot allocate space for section header\n",
  219. argv[0]);
  220. fclose(felf);
  221. return 26;
  222. }
  223. ret = fseek(felf, section_header_base, SEEK_SET);
  224. if (ret) {
  225. fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
  226. argv[0], ret, section_header_base);
  227. free(sh_table);
  228. fclose(felf);
  229. return 26;
  230. }
  231. size = fread(sh_table, 1, section_header_size, felf);
  232. if (size != section_header_size) {
  233. fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
  234. argv[0], size, section_header_size);
  235. free(sh_table);
  236. fclose(felf);
  237. return 27;
  238. }
  239. sh_index = elf16_to_cpu(header.e_shstrndx);
  240. sh_size = elf32_to_cpu(sh_table[sh_index].sh_size);
  241. debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size);
  242. sh_str = malloc(sh_size);
  243. if (!sh_str) {
  244. fprintf(stderr, "malloc failed\n");
  245. free(sh_table);
  246. fclose(felf);
  247. return 28;
  248. }
  249. /*
  250. * Specifies the byte offset from the beginning of the file
  251. * to the first byte in the section.
  252. */
  253. sh_offset = elf32_to_cpu(sh_table[sh_index].sh_offset);
  254. sh_num = elf16_to_cpu(header.e_shnum);
  255. ret = fseek(felf, sh_offset, SEEK_SET);
  256. if (ret) {
  257. fprintf(stderr, "Setting up sh_offset failed\n");
  258. free(sh_str);
  259. free(sh_table);
  260. fclose(felf);
  261. return 29;
  262. }
  263. size = fread(sh_str, 1, sh_size, felf);
  264. if (size != sh_size) {
  265. fprintf(stderr, "%s: Can't read section: %lx/%x\n",
  266. argv[0], size, sh_size);
  267. free(sh_str);
  268. free(sh_table);
  269. fclose(felf);
  270. return 30;
  271. }
  272. for (i = 0; i < sh_num; i++) {
  273. char *sh_name = sh_str + elf32_to_cpu(sh_table[i].sh_name);
  274. debug("%s\n", sh_name);
  275. sh_addr = elf32_to_cpu(sh_table[i].sh_addr);
  276. sh_offset = elf32_to_cpu(sh_table[i].sh_offset);
  277. sh_size = elf32_to_cpu(sh_table[i].sh_size);
  278. if (!strcmp(".rela.dyn", sh_name)) {
  279. debug("Found section\t\".rela_dyn\"\n");
  280. debug(" at addr\t0x%08x\n", sh_addr);
  281. debug(" at offset\t0x%08x\n", sh_offset);
  282. debug(" of size\t0x%08x\n", sh_size);
  283. rela_start = sh_addr;
  284. rela_end = rela_start + sh_size;
  285. }
  286. if (!strcmp(".dynsym", sh_name)) {
  287. debug("Found section\t\".dynsym\"\n");
  288. debug(" at addr\t0x%08x\n", sh_addr);
  289. debug(" at offset\t0x%08x\n", sh_offset);
  290. debug(" of size\t0x%08x\n", sh_size);
  291. dyn_start = sh_addr;
  292. }
  293. }
  294. /* Clean up */
  295. free(sh_str);
  296. free(sh_table);
  297. fclose(felf);
  298. debug("text_base\t0x%08lx\n", text_base);
  299. debug("rela_start\t0x%08lx\n", rela_start);
  300. debug("rela_end\t0x%08lx\n", rela_end);
  301. debug("dyn_start\t0x%08lx\n", dyn_start);
  302. if (!rela_start)
  303. return 1;
  304. return 0;
  305. }
  306. static int decode_elf(char **argv)
  307. {
  308. FILE *felf;
  309. size_t size;
  310. unsigned char e_ident[EI_NIDENT];
  311. felf = fopen(argv[2], "r+b");
  312. if (!felf) {
  313. fprintf(stderr, "%s: Cannot open %s: %s\n",
  314. argv[0], argv[5], strerror(errno));
  315. return 2;
  316. }
  317. size = fread(e_ident, 1, EI_NIDENT, felf);
  318. if (size != EI_NIDENT) {
  319. fclose(felf);
  320. return 25;
  321. }
  322. /* Check if this is really ELF file */
  323. if (e_ident[0] != 0x7f &&
  324. e_ident[1] != 'E' &&
  325. e_ident[2] != 'L' &&
  326. e_ident[3] != 'F') {
  327. fclose(felf);
  328. return 1;
  329. }
  330. ei_class = e_ident[4];
  331. debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
  332. ei_data = e_ident[5];
  333. debug("EI_DATA(1=little endian, 2=big endian) %d\n", ei_data);
  334. if (ei_class == 2)
  335. return decode_elf64(felf, argv);
  336. return decode_elf32(felf, argv);
  337. }
  338. static int rela_elf64(char **argv, FILE *f)
  339. {
  340. int i, num;
  341. if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
  342. fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
  343. return 3;
  344. }
  345. num = (rela_end - rela_start) / sizeof(Elf64_Rela);
  346. for (i = 0; i < num; i++) {
  347. Elf64_Rela rela, swrela;
  348. uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
  349. uint64_t addr;
  350. if (fseek(f, pos, SEEK_SET) < 0) {
  351. fprintf(stderr, "%s: %s: seek to %" PRIx64
  352. " failed: %s\n",
  353. argv[0], argv[1], pos, strerror(errno));
  354. }
  355. if (fread(&rela, sizeof(rela), 1, f) != 1) {
  356. fprintf(stderr, "%s: %s: read rela failed at %"
  357. PRIx64 "\n",
  358. argv[0], argv[1], pos);
  359. return 4;
  360. }
  361. swrela.r_offset = le64_to_cpu(rela.r_offset);
  362. swrela.r_info = le64_to_cpu(rela.r_info);
  363. swrela.r_addend = le64_to_cpu(rela.r_addend);
  364. if (!supported_rela(&swrela))
  365. continue;
  366. debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
  367. swrela.r_offset, swrela.r_info, swrela.r_addend);
  368. if (swrela.r_offset < text_base) {
  369. fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
  370. argv[0], argv[1], pos);
  371. return 4;
  372. }
  373. addr = swrela.r_offset - text_base;
  374. if (fseek(f, addr, SEEK_SET) < 0) {
  375. fprintf(stderr, "%s: %s: seek to %"
  376. PRIx64 " failed: %s\n",
  377. argv[0], argv[1], addr, strerror(errno));
  378. }
  379. if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
  380. fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
  381. argv[0], argv[1], addr);
  382. return 4;
  383. }
  384. }
  385. return 0;
  386. }
  387. static bool supported_rela32(Elf32_Rela *rela, uint32_t *type)
  388. {
  389. uint32_t mask = 0xffULL; /* would be different on 32-bit */
  390. *type = rela->r_info & mask;
  391. debug("Type:\t");
  392. switch (*type) {
  393. case R_MICROBLAZE_32:
  394. debug("R_MICROBLAZE_32\n");
  395. return true;
  396. case R_MICROBLAZE_GLOB_DAT:
  397. debug("R_MICROBLAZE_GLOB_DAT\n");
  398. return true;
  399. case R_MICROBLAZE_NONE:
  400. debug("R_MICROBLAZE_NONE - ignoring - do nothing\n");
  401. return false;
  402. case R_MICROBLAZE_REL:
  403. debug("R_MICROBLAZE_REL\n");
  404. return true;
  405. default:
  406. fprintf(stderr, "warning: unsupported relocation type %"
  407. PRIu32 " at %" PRIx32 "\n", *type, rela->r_offset);
  408. return false;
  409. }
  410. }
  411. static int rela_elf32(char **argv, FILE *f)
  412. {
  413. int i, num, index;
  414. uint32_t value, type;
  415. if ((rela_end - rela_start) % sizeof(Elf32_Rela)) {
  416. fprintf(stderr, "%s: rela size isn't a multiple of Elf32_Rela\n", argv[0]);
  417. return 3;
  418. }
  419. num = (rela_end - rela_start) / sizeof(Elf32_Rela);
  420. debug("Number of entries: %u\n", num);
  421. for (i = 0; i < num; i++) {
  422. Elf32_Rela rela, swrela;
  423. Elf32_Sym symbols;
  424. uint32_t pos = rela_start + sizeof(Elf32_Rela) * i;
  425. uint32_t addr, pos_dyn;
  426. debug("\nPosition:\t%d/0x%x\n", i, pos);
  427. if (fseek(f, pos, SEEK_SET) < 0) {
  428. fprintf(stderr, "%s: %s: seek to %" PRIx32
  429. " failed: %s\n",
  430. argv[0], argv[1], pos, strerror(errno));
  431. }
  432. if (fread(&rela, sizeof(rela), 1, f) != 1) {
  433. fprintf(stderr, "%s: %s: read rela failed at %"
  434. PRIx32 "\n",
  435. argv[0], argv[1], pos);
  436. return 4;
  437. }
  438. debug("Rela:\toffset:\t%" PRIx32 " r_info:\t%"
  439. PRIu32 " r_addend:\t%" PRIx32 "\n",
  440. rela.r_offset, rela.r_info, rela.r_addend);
  441. swrela.r_offset = elf32_to_cpu(rela.r_offset);
  442. swrela.r_info = elf32_to_cpu(rela.r_info);
  443. swrela.r_addend = elf32_to_cpu(rela.r_addend);
  444. debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%"
  445. PRIu32 " r_addend:\t%" PRIx32 "\n",
  446. swrela.r_offset, swrela.r_info, swrela.r_addend);
  447. if (!supported_rela32(&swrela, &type))
  448. continue;
  449. if (swrela.r_offset < text_base) {
  450. fprintf(stderr, "%s: %s: bad rela at %" PRIx32 "\n",
  451. argv[0], argv[1], pos);
  452. return 4;
  453. }
  454. addr = swrela.r_offset - text_base;
  455. debug("Addr:\t0x%" PRIx32 "\n", addr);
  456. switch (type) {
  457. case R_MICROBLAZE_REL:
  458. if (fseek(f, addr, SEEK_SET) < 0) {
  459. fprintf(stderr, "%s: %s: seek to %"
  460. PRIx32 " failed: %s\n",
  461. argv[0], argv[1], addr, strerror(errno));
  462. return 5;
  463. }
  464. debug("Write addend\n");
  465. if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
  466. fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
  467. argv[0], argv[1], addr);
  468. return 4;
  469. }
  470. break;
  471. case R_MICROBLAZE_32:
  472. case R_MICROBLAZE_GLOB_DAT:
  473. /* global symbols read it and add reloc offset */
  474. index = swrela.r_info >> 8;
  475. pos_dyn = dyn_start + sizeof(Elf32_Sym) * index;
  476. debug("Index:\t%d\n", index);
  477. debug("Pos_dyn:\t0x%x\n", pos_dyn);
  478. if (fseek(f, pos_dyn, SEEK_SET) < 0) {
  479. fprintf(stderr, "%s: %s: seek to %"
  480. PRIx32 " failed: %s\n",
  481. argv[0], argv[1], pos_dyn, strerror(errno));
  482. return 5;
  483. }
  484. if (fread(&symbols, sizeof(symbols), 1, f) != 1) {
  485. fprintf(stderr, "%s: %s: read symbols failed at %"
  486. PRIx32 "\n",
  487. argv[0], argv[1], pos_dyn);
  488. return 4;
  489. }
  490. debug("Symbol description:\n");
  491. debug(" st_name:\t0x%x\n", symbols.st_name);
  492. debug(" st_value:\t0x%x\n", symbols.st_value);
  493. debug(" st_size:\t0x%x\n", symbols.st_size);
  494. value = swrela.r_addend + symbols.st_value;
  495. debug("Value:\t0x%x\n", value);
  496. if (fseek(f, addr, SEEK_SET) < 0) {
  497. fprintf(stderr, "%s: %s: seek to %"
  498. PRIx32 " failed: %s\n",
  499. argv[0], argv[1], addr, strerror(errno));
  500. return 5;
  501. }
  502. if (fwrite(&value, sizeof(rela.r_addend), 1, f) != 1) {
  503. fprintf(stderr, "%s: %s: write failed at %" PRIx32 "\n",
  504. argv[0], argv[1], addr);
  505. return 4;
  506. }
  507. break;
  508. case R_MICROBLAZE_NONE:
  509. debug("R_MICROBLAZE_NONE - skip\n");
  510. break;
  511. default:
  512. fprintf(stderr, "warning: unsupported relocation type %"
  513. PRIu32 " at %" PRIx32 "\n",
  514. type, rela.r_offset);
  515. }
  516. }
  517. return 0;
  518. }
  519. int main(int argc, char **argv)
  520. {
  521. FILE *f;
  522. int ret;
  523. uint64_t file_size;
  524. if (argc != 3) {
  525. fprintf(stderr, "Statically apply ELF rela relocations\n");
  526. fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
  527. argv[0]);
  528. return 1;
  529. }
  530. ret = decode_elf(argv);
  531. if (ret) {
  532. fprintf(stderr, "ELF decoding failed\n");
  533. return ret;
  534. }
  535. if (rela_start > rela_end || rela_start < text_base) {
  536. fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
  537. return 3;
  538. }
  539. rela_start -= text_base;
  540. rela_end -= text_base;
  541. dyn_start -= text_base;
  542. f = fopen(argv[1], "r+b");
  543. if (!f) {
  544. fprintf(stderr, "%s: Cannot open %s: %s\n",
  545. argv[0], argv[1], strerror(errno));
  546. return 2;
  547. }
  548. fseek(f, 0, SEEK_END);
  549. file_size = ftell(f);
  550. rewind(f);
  551. if (rela_end > file_size) {
  552. // Most likely compiler inserted some section that didn't get
  553. // objcopy-ed into the final binary
  554. rela_end = file_size;
  555. }
  556. if (ei_class == 2)
  557. ret = rela_elf64(argv, f);
  558. else
  559. ret = rela_elf32(argv, f);
  560. if (fclose(f) < 0) {
  561. fprintf(stderr, "%s: %s: close failed: %s\n",
  562. argv[0], argv[1], strerror(errno));
  563. return 4;
  564. }
  565. return ret;
  566. }