0001-elf2flt-handle-binutils-2.34.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. From fa0e77afba7d8d4107af5f8ddc8d38d23c3dd19d Mon Sep 17 00:00:00 2001
  2. From: Romain Naour <romain.naour@smile.fr>
  3. Date: Wed, 5 Feb 2020 10:31:32 +0100
  4. Subject: [PATCH] elf2flt: handle binutils >= 2.34
  5. The latest Binutils release (2.34) is not compatible with elf2flt due
  6. to a change in bfd_section_* macros. The issue has been reported to
  7. the Binutils mailing list but Alan Modra recommend to bundle libbfd
  8. library sources into each projects using it [1]. That's because the
  9. API is not stable over the time without any backward compatibility
  10. guaranties.
  11. On the other hand, the elf2flt tools needs to support modified
  12. version of binutils for specific arch/target [2].
  13. Add two tests in the configure script to detect this API change
  14. in order to support binutils < 2.34 and binutils >= 2.34.
  15. [1] https://sourceware.org/ml/binutils/2020-02/msg00044.html
  16. [2] https://github.com/uclinux-dev/elf2flt/issues/14
  17. Signed-off-by: Romain Naour <romain.naour@smile.fr>
  18. Signed-off-by: Waldemar Brodkorb <wbrodkorb@conet.de>
  19. ---
  20. configure.ac | 25 +++++++++++++++++++
  21. elf2flt.c | 81 ++++++++++++++++++++++++++++++++++--------------------------
  22. 2 files changed, 71 insertions(+), 35 deletions(-)
  23. diff --git a/configure.ac b/configure.ac
  24. index d6b4119..caae869 100644
  25. --- a/configure.ac
  26. +++ b/configure.ac
  27. @@ -212,6 +212,31 @@ AC_CHECK_FUNCS([ \
  28. strsignal \
  29. ])
  30. +dnl Various bfd section macros and functions like bfd_section_size() has been
  31. +dnl modified starting binutils >= 2.34.
  32. +dnl Check if the prototype is "bfd_section_size (sec)" or "bfd_section_size(bfd, ptr)"
  33. +if test "$binutils_build_dir" != "NONE"; then
  34. + CFLAGS="-I$binutils_include_dir -I$bfd_include_dir $CFLAGS"
  35. +fi
  36. +
  37. +AC_TRY_COMPILE([#include <bfd.h>],
  38. + [const asection *sec; bfd_section_size(sec);],
  39. + bfd_section_size_macro_has_one_arg=yes,
  40. + bfd_section_size_macro_has_one_arg=no)
  41. +if test "$bfd_section_size_macro_has_one_arg" = "yes" ; then
  42. + AC_DEFINE(HAVE_BFD_SECTION_SIZE_MACRO_HAS_ONE_ARG, 1,
  43. + [define to 1 for binutils >= 2.34])
  44. +fi
  45. +
  46. +AC_TRY_COMPILE([#include <bfd.h>],
  47. + [const asection *sec; bfd_section_vma(sec);],
  48. + bfd_section_vma_macro_has_one_arg=yes,
  49. + bfd_section_vma_macro_has_one_arg=no)
  50. +if test "$bfd_section_vma_macro_has_one_arg" = "yes" ; then
  51. + AC_DEFINE(HAVE_BFD_SECTION_VMA_MACRO_HAS_ONE_ARG, 1,
  52. + [define to 1 for binutils >= 2.34])
  53. +fi
  54. +
  55. if test "$GCC" = yes ; then
  56. CFLAGS="-Wall $CFLAGS"
  57. if test "$werror" = 1 ; then
  58. diff --git a/elf2flt.c b/elf2flt.c
  59. index b7c4a49..8dbd9b2 100644
  60. --- a/elf2flt.c
  61. +++ b/elf2flt.c
  62. @@ -149,6 +149,17 @@ const char *elf2flt_progname;
  63. #define O_BINARY 0
  64. #endif
  65. +#if defined(HAVE_BFD_SECTION_SIZE_MACRO_HAS_ONE_ARG)
  66. +#define elf2flt_bfd_section_size(abs_bfd, s) bfd_section_size(s)
  67. +#else
  68. +#define elf2flt_bfd_section_size(abs_bfd, s) bfd_section_size(abs_bfd, s)
  69. +#endif
  70. +
  71. +#if defined(HAVE_BFD_SECTION_VMA_MACRO_HAS_ONE_ARG)
  72. +#define elf2flt_bfd_section_vma(abs_bfd, s) bfd_section_vma(s)
  73. +#else
  74. +#define elf2flt_bfd_section_vma(abs_bfd, s) bfd_section_vma(abs_bfd, s)
  75. +#endif
  76. /* Extra output when running. */
  77. static int verbose = 0;
  78. @@ -323,9 +334,9 @@ compare_relocs (const void *pa, const void *pb)
  79. else if (!rb->sym_ptr_ptr || !*rb->sym_ptr_ptr)
  80. return 1;
  81. - a_vma = bfd_section_vma(compare_relocs_bfd,
  82. + a_vma = elf2flt_bfd_section_vma(compare_relocs_bfd,
  83. (*(ra->sym_ptr_ptr))->section);
  84. - b_vma = bfd_section_vma(compare_relocs_bfd,
  85. + b_vma = elf2flt_bfd_section_vma(compare_relocs_bfd,
  86. (*(rb->sym_ptr_ptr))->section);
  87. va = (*(ra->sym_ptr_ptr))->value + a_vma + ra->addend;
  88. vb = (*(rb->sym_ptr_ptr))->value + b_vma + rb->addend;
  89. @@ -403,7 +414,7 @@ output_relocs (
  90. }
  91. for (a = abs_bfd->sections; (a != (asection *) NULL); a = a->next) {
  92. - section_vma = bfd_section_vma(abs_bfd, a);
  93. + section_vma = elf2flt_bfd_section_vma(abs_bfd, a);
  94. if (verbose)
  95. printf("SECTION: %s [%p]: flags=0x%x vma=0x%"PRIx32"\n",
  96. @@ -442,7 +453,7 @@ output_relocs (
  97. continue;
  98. if (verbose)
  99. printf(" RELOCS: %s [%p]: flags=0x%x vma=0x%"BFD_VMA_FMT"x\n",
  100. - r->name, r, r->flags, bfd_section_vma(abs_bfd, r));
  101. + r->name, r, r->flags, elf2flt_bfd_section_vma(abs_bfd, r));
  102. if ((r->flags & SEC_RELOC) == 0)
  103. continue;
  104. relsize = bfd_get_reloc_upper_bound(rel_bfd, r);
  105. @@ -674,7 +685,7 @@ output_relocs (
  106. case R_BFIN_RIMM16:
  107. case R_BFIN_LUIMM16:
  108. case R_BFIN_HUIMM16:
  109. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  110. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  111. sym_addr += sym_vma + q->addend;
  112. if (weak_und_symbol(sym_section->name, (*(q->sym_ptr_ptr))))
  113. @@ -707,7 +718,7 @@ output_relocs (
  114. break;
  115. case R_BFIN_BYTE4_DATA:
  116. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  117. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  118. sym_addr += sym_vma + q->addend;
  119. if (weak_und_symbol (sym_section->name, (*(q->sym_ptr_ptr))))
  120. @@ -851,7 +862,7 @@ output_relocs (
  121. #if defined(TARGET_m68k)
  122. case R_68K_32:
  123. relocation_needed = 1;
  124. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  125. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  126. sym_addr += sym_vma + q->addend;
  127. break;
  128. case R_68K_PC16:
  129. @@ -876,7 +887,7 @@ output_relocs (
  130. q->address, sym_addr,
  131. (*p)->howto->rightshift,
  132. *(uint32_t *)r_mem);
  133. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  134. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  135. sym_addr += sym_vma + q->addend;
  136. break;
  137. case R_ARM_GOT32:
  138. @@ -904,7 +915,7 @@ output_relocs (
  139. #ifdef TARGET_v850
  140. case R_V850_ABS32:
  141. relocation_needed = 1;
  142. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  143. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  144. sym_addr += sym_vma + q->addend;
  145. break;
  146. case R_V850_ZDA_16_16_OFFSET:
  147. @@ -926,7 +937,7 @@ output_relocs (
  148. sym_addr = (*(q->sym_ptr_ptr))->value;
  149. q->address -= 1;
  150. r_mem -= 1; /* tracks q->address */
  151. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  152. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  153. sym_addr += sym_vma + q->addend;
  154. sym_addr |= (*(unsigned char *)r_mem<<24);
  155. break;
  156. @@ -939,7 +950,7 @@ output_relocs (
  157. /* Absolute symbol done not relocation */
  158. relocation_needed = !bfd_is_abs_section(sym_section);
  159. sym_addr = (*(q->sym_ptr_ptr))->value;
  160. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  161. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  162. sym_addr += sym_vma + q->addend;
  163. break;
  164. case R_H8_DIR32:
  165. @@ -952,7 +963,7 @@ output_relocs (
  166. }
  167. relocation_needed = 1;
  168. sym_addr = (*(q->sym_ptr_ptr))->value;
  169. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  170. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  171. sym_addr += sym_vma + q->addend;
  172. break;
  173. case R_H8_PCREL16:
  174. @@ -985,7 +996,7 @@ output_relocs (
  175. pflags=0x80000000;
  176. /* work out the relocation */
  177. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  178. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  179. sym_addr += sym_vma + q->addend;
  180. /* Write relocated pointer back */
  181. p[2] = (sym_addr >> 24) & 0xff;
  182. @@ -1001,7 +1012,7 @@ output_relocs (
  183. relocation_needed = 0;
  184. pflags = 0;
  185. sprintf(&addstr[0], "+0x%ld", sym_addr - (*(q->sym_ptr_ptr))->value -
  186. - bfd_section_vma(abs_bfd, sym_section));
  187. + elf2flt_bfd_section_vma(abs_bfd, sym_section));
  188. if (verbose)
  189. printf(" RELOC[%d]: offset=0x%"BFD_VMA_FMT"x symbol=%s%s "
  190. "section=%s size=%d "
  191. @@ -1017,7 +1028,7 @@ output_relocs (
  192. continue;
  193. }
  194. case R_MICROBLAZE_32:
  195. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  196. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  197. sym_addr += sym_vma + q->addend;
  198. relocation_needed = 1;
  199. break;
  200. @@ -1042,7 +1053,7 @@ output_relocs (
  201. case R_NIOS2_BFD_RELOC_32:
  202. relocation_needed = 1;
  203. pflags = (FLAT_NIOS2_R_32 << 28);
  204. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  205. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  206. sym_addr += sym_vma + q->addend;
  207. /* modify target, in target order */
  208. *(unsigned long *)r_mem = htoniosl(sym_addr);
  209. @@ -1052,7 +1063,7 @@ output_relocs (
  210. unsigned long exist_val;
  211. relocation_needed = 1;
  212. pflags = (FLAT_NIOS2_R_CALL26 << 28);
  213. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  214. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  215. sym_addr += sym_vma + q->addend;
  216. /* modify target, in target order */
  217. @@ -1083,7 +1094,7 @@ output_relocs (
  218. ? FLAT_NIOS2_R_HIADJ_LO : FLAT_NIOS2_R_HI_LO;
  219. pflags <<= 28;
  220. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  221. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  222. sym_addr += sym_vma + q->addend;
  223. /* modify high 16 bits, in target order */
  224. @@ -1116,7 +1127,7 @@ output_relocs (
  225. goto NIOS2_RELOC_ERR;
  226. }
  227. /* _gp holds a absolute value, otherwise the ld cannot generate correct code */
  228. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  229. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  230. //printf("sym=%x, %d, _gp=%x, %d\n", sym_addr+sym_vma, sym_addr+sym_vma, gp, gp);
  231. sym_addr += sym_vma + q->addend;
  232. sym_addr -= gp;
  233. @@ -1197,7 +1208,7 @@ NIOS2_RELOC_ERR:
  234. case R_SPARC_32:
  235. case R_SPARC_UA32:
  236. relocation_needed = 1;
  237. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  238. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  239. sym_addr += sym_vma + q->addend;
  240. break;
  241. case R_SPARC_PC22:
  242. @@ -1216,7 +1227,7 @@ NIOS2_RELOC_ERR:
  243. case R_SPARC_HI22:
  244. relocation_needed = 1;
  245. pflags = 0x80000000;
  246. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  247. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  248. sym_addr += sym_vma + q->addend;
  249. sym_addr |= (
  250. htonl(*(uint32_t *)r_mem)
  251. @@ -1226,7 +1237,7 @@ NIOS2_RELOC_ERR:
  252. case R_SPARC_LO10:
  253. relocation_needed = 1;
  254. pflags = 0x40000000;
  255. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  256. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  257. sym_addr += sym_vma + q->addend;
  258. sym_addr &= 0x000003ff;
  259. sym_addr |= (
  260. @@ -1240,7 +1251,7 @@ NIOS2_RELOC_ERR:
  261. #ifdef TARGET_sh
  262. case R_SH_DIR32:
  263. relocation_needed = 1;
  264. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  265. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  266. sym_addr += sym_vma + q->addend;
  267. break;
  268. case R_SH_REL32:
  269. @@ -1272,7 +1283,7 @@ NIOS2_RELOC_ERR:
  270. case R_E1_CONST31:
  271. relocation_needed = 1;
  272. DBG_E1("Handling Reloc <CONST31>\n");
  273. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  274. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  275. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  276. sec_vma, sym_addr, q->address);
  277. sym_addr = sec_vma + sym_addr;
  278. @@ -1287,7 +1298,7 @@ NIOS2_RELOC_ERR:
  279. relocation_needed = 0;
  280. DBG_E1("Handling Reloc <CONST31_PCREL>\n");
  281. DBG_E1("DONT RELOCATE AT LOADING\n");
  282. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  283. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  284. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  285. sec_vma, sym_addr, q->address);
  286. sym_addr = sec_vma + sym_addr;
  287. @@ -1314,7 +1325,7 @@ NIOS2_RELOC_ERR:
  288. relocation_needed = 0;
  289. DBG_E1("Handling Reloc <DIS29W_PCREL>\n");
  290. DBG_E1("DONT RELOCATE AT LOADING\n");
  291. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  292. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  293. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  294. sec_vma, sym_addr, q->address);
  295. sym_addr = sec_vma + sym_addr;
  296. @@ -1347,7 +1358,7 @@ NIOS2_RELOC_ERR:
  297. DBG_E1("Handling Reloc <DIS29B>\n");
  298. DIS29_RELOCATION:
  299. relocation_needed = 1;
  300. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  301. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  302. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%08x]\n",
  303. sec_vma, sym_addr);
  304. sym_addr = sec_vma + sym_addr;
  305. @@ -1364,7 +1375,7 @@ DIS29_RELOCATION:
  306. relocation_needed = 0;
  307. DBG_E1("Handling Reloc <IMM32_PCREL>\n");
  308. DBG_E1("DONT RELOCATE AT LOADING\n");
  309. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  310. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  311. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  312. sec_vma, sym_addr);
  313. sym_addr = sec_vma + sym_addr;
  314. @@ -1390,7 +1401,7 @@ DIS29_RELOCATION:
  315. case R_E1_IMM32:
  316. relocation_needed = 1;
  317. DBG_E1("Handling Reloc <IMM32>\n");
  318. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  319. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  320. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  321. sec_vma, sym_addr);
  322. sym_addr = sec_vma + sym_addr;
  323. @@ -1406,7 +1417,7 @@ DIS29_RELOCATION:
  324. case R_E1_WORD:
  325. relocation_needed = 1;
  326. DBG_E1("Handling Reloc <WORD>\n");
  327. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  328. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  329. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  330. sec_vma, sym_addr);
  331. sym_addr = sec_vma + sym_addr;
  332. @@ -1433,7 +1444,7 @@ DIS29_RELOCATION:
  333. }
  334. sprintf(&addstr[0], "+0x%lx", sym_addr - (*(q->sym_ptr_ptr))->value -
  335. - bfd_section_vma(abs_bfd, sym_section));
  336. + elf2flt_bfd_section_vma(abs_bfd, sym_section));
  337. /*
  338. @@ -1873,8 +1884,8 @@ int main(int argc, char *argv[])
  339. } else
  340. continue;
  341. - sec_size = bfd_section_size(abs_bfd, s);
  342. - sec_vma = bfd_section_vma(abs_bfd, s);
  343. + sec_size = elf2flt_bfd_section_size(abs_bfd, s);
  344. + sec_vma = elf2flt_bfd_section_vma(abs_bfd, s);
  345. if (sec_vma < *vma) {
  346. if (*len > 0)
  347. @@ -1899,7 +1910,7 @@ int main(int argc, char *argv[])
  348. if (s->flags & SEC_CODE)
  349. if (!bfd_get_section_contents(abs_bfd, s,
  350. text + (s->vma - text_vma), 0,
  351. - bfd_section_size(abs_bfd, s)))
  352. + elf2flt_bfd_section_size(abs_bfd, s)))
  353. {
  354. fatal("read error section %s", s->name);
  355. }
  356. @@ -1925,7 +1936,7 @@ int main(int argc, char *argv[])
  357. if (s->flags & SEC_DATA)
  358. if (!bfd_get_section_contents(abs_bfd, s,
  359. data + (s->vma - data_vma), 0,
  360. - bfd_section_size(abs_bfd, s)))
  361. + elf2flt_bfd_section_size(abs_bfd, s)))
  362. {
  363. fatal("read error section %s", s->name);
  364. }
  365. --
  366. 2.11.0