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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. From d78acba6510527aaa01a41eaf4c931f7a57e5f44 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 [1]. The issue has been reported
  7. to the Binutils mailing list but Alan Modra recommend to bundle
  8. libbfd library sources into each projects using it [2]. That's
  9. because the API is not stable over the time without any backward
  10. compatibility guaranties.
  11. On the other hand, the elf2flt tools needs to support modified
  12. version of binutils for specific arch/target [3].
  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. Upstream status: [4]
  16. [1] https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=fd3619828e94a24a92cddec42cbc0ab33352eeb4
  17. [2] https://sourceware.org/ml/binutils/2020-02/msg00044.html
  18. [3] https://github.com/uclinux-dev/elf2flt/issues/14
  19. [4] https://github.com/uclinux-dev/elf2flt/pull/15
  20. Signed-off-by: Romain Naour <romain.naour@smile.fr>
  21. ---
  22. configure.ac | 16 +++++++++++
  23. elf2flt.c | 81 +++++++++++++++++++++++++++++-----------------------
  24. 2 files changed, 61 insertions(+), 36 deletions(-)
  25. diff --git a/configure.ac b/configure.ac
  26. index b7db2cb..fdc0876 100644
  27. --- a/configure.ac
  28. +++ b/configure.ac
  29. @@ -229,6 +229,22 @@ AC_CHECK_FUNCS([ \
  30. strsignal \
  31. ])
  32. +dnl Various bfd section macros and functions like bfd_section_size() have been
  33. +dnl modified starting with binutils >= 2.34.
  34. +dnl Check if the prototypes take a bfd argument.
  35. +if test "$binutils_build_dir" != "NONE"; then
  36. + CFLAGS="-I$binutils_include_dir -I$bfd_include_dir $CFLAGS"
  37. +fi
  38. +
  39. +AC_TRY_COMPILE([#include <bfd.h>],
  40. + [const asection *sec; bfd_section_size(sec);],
  41. + bfd_section_api_takes_bfd=no,
  42. + bfd_section_api_takes_bfd=yes)
  43. +if test "$bfd_section_api_takes_bfd" = "yes" ; then
  44. + AC_DEFINE(HAVE_BFD_SECTION_API_TAKES_BFD, 1,
  45. + [define to 1 for binutils < 2.34])
  46. +fi
  47. +
  48. if test "$GCC" = yes ; then
  49. CFLAGS="-Wall $CFLAGS"
  50. if test "$werror" = 1 ; then
  51. diff --git a/elf2flt.c b/elf2flt.c
  52. index 7ac0617..ea6b5a1 100644
  53. --- a/elf2flt.c
  54. +++ b/elf2flt.c
  55. @@ -149,6 +149,17 @@ const char *elf2flt_progname;
  56. #define O_BINARY 0
  57. #endif
  58. +/*
  59. + * The bfd parameter isn't actually used by any of the bfd_section funcs and
  60. + * have been removed since binutils 2.34.
  61. + */
  62. +#ifdef HAVE_BFD_SECTION_API_TAKES_BFD
  63. +#define elf2flt_bfd_section_size(s) bfd_section_size(NULL, s)
  64. +#define elf2flt_bfd_section_vma(s) bfd_section_vma(NULL, s)
  65. +#else
  66. +#define elf2flt_bfd_section_size(s) bfd_section_size(s)
  67. +#define elf2flt_bfd_section_vma(s) bfd_section_vma(s)
  68. +#endif
  69. /* Extra output when running. */
  70. static int verbose = 0;
  71. @@ -323,10 +334,8 @@ compare_relocs (const void *pa, const void *pb)
  72. else if (!rb->sym_ptr_ptr || !*rb->sym_ptr_ptr)
  73. return 1;
  74. - a_vma = bfd_section_vma(compare_relocs_bfd,
  75. - (*(ra->sym_ptr_ptr))->section);
  76. - b_vma = bfd_section_vma(compare_relocs_bfd,
  77. - (*(rb->sym_ptr_ptr))->section);
  78. + a_vma = elf2flt_bfd_section_vma((*(ra->sym_ptr_ptr))->section);
  79. + b_vma = elf2flt_bfd_section_vma((*(rb->sym_ptr_ptr))->section);
  80. va = (*(ra->sym_ptr_ptr))->value + a_vma + ra->addend;
  81. vb = (*(rb->sym_ptr_ptr))->value + b_vma + rb->addend;
  82. return va - vb;
  83. @@ -403,7 +412,7 @@ output_relocs (
  84. }
  85. for (a = abs_bfd->sections; (a != (asection *) NULL); a = a->next) {
  86. - section_vma = bfd_section_vma(abs_bfd, a);
  87. + section_vma = elf2flt_bfd_section_vma(a);
  88. if (verbose)
  89. printf("SECTION: %s [%p]: flags=0x%x vma=0x%"PRIx32"\n",
  90. @@ -443,7 +452,7 @@ output_relocs (
  91. continue;
  92. if (verbose)
  93. printf(" RELOCS: %s [%p]: flags=0x%x vma=0x%"BFD_VMA_FMT"x\n",
  94. - r->name, r, r->flags, bfd_section_vma(abs_bfd, r));
  95. + r->name, r, r->flags, elf2flt_bfd_section_vma(r));
  96. if ((r->flags & SEC_RELOC) == 0)
  97. continue;
  98. relsize = bfd_get_reloc_upper_bound(rel_bfd, r);
  99. @@ -695,7 +704,7 @@ output_relocs (
  100. case R_BFIN_RIMM16:
  101. case R_BFIN_LUIMM16:
  102. case R_BFIN_HUIMM16:
  103. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  104. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  105. sym_addr += sym_vma + q->addend;
  106. if (weak_und_symbol(sym_section->name, (*(q->sym_ptr_ptr))))
  107. @@ -728,7 +737,7 @@ output_relocs (
  108. break;
  109. case R_BFIN_BYTE4_DATA:
  110. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  111. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  112. sym_addr += sym_vma + q->addend;
  113. if (weak_und_symbol (sym_section->name, (*(q->sym_ptr_ptr))))
  114. @@ -886,7 +895,7 @@ output_relocs (
  115. #if defined(TARGET_m68k)
  116. case R_68K_32:
  117. relocation_needed = 1;
  118. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  119. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  120. sym_addr += sym_vma + q->addend;
  121. break;
  122. case R_68K_PC16:
  123. @@ -911,7 +920,7 @@ output_relocs (
  124. q->address, sym_addr,
  125. (*p)->howto->rightshift,
  126. *(uint32_t *)r_mem);
  127. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  128. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  129. sym_addr += sym_vma + q->addend;
  130. break;
  131. case R_ARM_GOT32:
  132. @@ -939,7 +948,7 @@ output_relocs (
  133. #ifdef TARGET_v850
  134. case R_V850_ABS32:
  135. relocation_needed = 1;
  136. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  137. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  138. sym_addr += sym_vma + q->addend;
  139. break;
  140. case R_V850_ZDA_16_16_OFFSET:
  141. @@ -961,7 +970,7 @@ output_relocs (
  142. sym_addr = (*(q->sym_ptr_ptr))->value;
  143. q->address -= 1;
  144. r_mem -= 1; /* tracks q->address */
  145. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  146. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  147. sym_addr += sym_vma + q->addend;
  148. sym_addr |= (*(unsigned char *)r_mem<<24);
  149. break;
  150. @@ -974,7 +983,7 @@ output_relocs (
  151. /* Absolute symbol done not relocation */
  152. relocation_needed = !bfd_is_abs_section(sym_section);
  153. sym_addr = (*(q->sym_ptr_ptr))->value;
  154. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  155. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  156. sym_addr += sym_vma + q->addend;
  157. break;
  158. case R_H8_DIR32:
  159. @@ -987,7 +996,7 @@ output_relocs (
  160. }
  161. relocation_needed = 1;
  162. sym_addr = (*(q->sym_ptr_ptr))->value;
  163. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  164. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  165. sym_addr += sym_vma + q->addend;
  166. break;
  167. case R_H8_PCREL16:
  168. @@ -1013,7 +1022,7 @@ output_relocs (
  169. #ifdef TARGET_microblaze
  170. case R_MICROBLAZE_64:
  171. /* work out the relocation */
  172. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  173. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  174. sym_addr += sym_vma + q->addend;
  175. /* Write relocated pointer back */
  176. r_mem[2] = (sym_addr >> 24) & 0xff;
  177. @@ -1027,7 +1036,7 @@ output_relocs (
  178. pflags = 0x80000000;
  179. break;
  180. case R_MICROBLAZE_32:
  181. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  182. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  183. sym_addr += sym_vma + q->addend;
  184. relocation_needed = 1;
  185. break;
  186. @@ -1059,7 +1068,7 @@ output_relocs (
  187. case R_NIOS2_BFD_RELOC_32:
  188. relocation_needed = 1;
  189. pflags = (FLAT_NIOS2_R_32 << 28);
  190. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  191. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  192. sym_addr += sym_vma + q->addend;
  193. /* modify target, in target order */
  194. *(unsigned long *)r_mem = htoniosl(sym_addr);
  195. @@ -1069,7 +1078,7 @@ output_relocs (
  196. unsigned long exist_val;
  197. relocation_needed = 1;
  198. pflags = (FLAT_NIOS2_R_CALL26 << 28);
  199. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  200. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  201. sym_addr += sym_vma + q->addend;
  202. /* modify target, in target order */
  203. @@ -1100,7 +1109,7 @@ output_relocs (
  204. ? FLAT_NIOS2_R_HIADJ_LO : FLAT_NIOS2_R_HI_LO;
  205. pflags <<= 28;
  206. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  207. + sym_vma = elf2flt_bfd_section_vma(abs_bfd, sym_section);
  208. sym_addr += sym_vma + q->addend;
  209. /* modify high 16 bits, in target order */
  210. @@ -1133,7 +1142,7 @@ output_relocs (
  211. goto NIOS2_RELOC_ERR;
  212. }
  213. /* _gp holds a absolute value, otherwise the ld cannot generate correct code */
  214. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  215. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  216. //printf("sym=%x, %d, _gp=%x, %d\n", sym_addr+sym_vma, sym_addr+sym_vma, gp, gp);
  217. sym_addr += sym_vma + q->addend;
  218. sym_addr -= gp;
  219. @@ -1214,7 +1223,7 @@ NIOS2_RELOC_ERR:
  220. case R_SPARC_32:
  221. case R_SPARC_UA32:
  222. relocation_needed = 1;
  223. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  224. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  225. sym_addr += sym_vma + q->addend;
  226. break;
  227. case R_SPARC_PC22:
  228. @@ -1233,7 +1242,7 @@ NIOS2_RELOC_ERR:
  229. case R_SPARC_HI22:
  230. relocation_needed = 1;
  231. pflags = 0x80000000;
  232. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  233. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  234. sym_addr += sym_vma + q->addend;
  235. sym_addr |= (
  236. htonl(*(uint32_t *)r_mem)
  237. @@ -1243,7 +1252,7 @@ NIOS2_RELOC_ERR:
  238. case R_SPARC_LO10:
  239. relocation_needed = 1;
  240. pflags = 0x40000000;
  241. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  242. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  243. sym_addr += sym_vma + q->addend;
  244. sym_addr &= 0x000003ff;
  245. sym_addr |= (
  246. @@ -1257,7 +1266,7 @@ NIOS2_RELOC_ERR:
  247. #ifdef TARGET_sh
  248. case R_SH_DIR32:
  249. relocation_needed = 1;
  250. - sym_vma = bfd_section_vma(abs_bfd, sym_section);
  251. + sym_vma = elf2flt_bfd_section_vma(sym_section);
  252. sym_addr += sym_vma + q->addend;
  253. break;
  254. case R_SH_REL32:
  255. @@ -1289,7 +1298,7 @@ NIOS2_RELOC_ERR:
  256. case R_E1_CONST31:
  257. relocation_needed = 1;
  258. DBG_E1("Handling Reloc <CONST31>\n");
  259. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  260. + sec_vma = elf2flt_bfd_section_vma(sym_section);
  261. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  262. sec_vma, sym_addr, q->address);
  263. sym_addr = sec_vma + sym_addr;
  264. @@ -1304,7 +1313,7 @@ NIOS2_RELOC_ERR:
  265. relocation_needed = 0;
  266. DBG_E1("Handling Reloc <CONST31_PCREL>\n");
  267. DBG_E1("DONT RELOCATE AT LOADING\n");
  268. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  269. + sec_vma = elf2flt_bfd_section_vma(sym_section);
  270. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  271. sec_vma, sym_addr, q->address);
  272. sym_addr = sec_vma + sym_addr;
  273. @@ -1331,7 +1340,7 @@ NIOS2_RELOC_ERR:
  274. relocation_needed = 0;
  275. DBG_E1("Handling Reloc <DIS29W_PCREL>\n");
  276. DBG_E1("DONT RELOCATE AT LOADING\n");
  277. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  278. + sec_vma = elf2flt_bfd_section_vma(sym_section);
  279. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x], q->address : [0x%x]\n",
  280. sec_vma, sym_addr, q->address);
  281. sym_addr = sec_vma + sym_addr;
  282. @@ -1364,7 +1373,7 @@ NIOS2_RELOC_ERR:
  283. DBG_E1("Handling Reloc <DIS29B>\n");
  284. DIS29_RELOCATION:
  285. relocation_needed = 1;
  286. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  287. + sec_vma = elf2flt_bfd_section_vma(sym_section);
  288. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%08x]\n",
  289. sec_vma, sym_addr);
  290. sym_addr = sec_vma + sym_addr;
  291. @@ -1381,7 +1390,7 @@ DIS29_RELOCATION:
  292. relocation_needed = 0;
  293. DBG_E1("Handling Reloc <IMM32_PCREL>\n");
  294. DBG_E1("DONT RELOCATE AT LOADING\n");
  295. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  296. + sec_vma = elf2flt_bfd_section_vma(sym_section);
  297. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  298. sec_vma, sym_addr);
  299. sym_addr = sec_vma + sym_addr;
  300. @@ -1407,7 +1416,7 @@ DIS29_RELOCATION:
  301. case R_E1_IMM32:
  302. relocation_needed = 1;
  303. DBG_E1("Handling Reloc <IMM32>\n");
  304. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  305. + sec_vma = elf2flt_bfd_section_vma(sym_section);
  306. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  307. sec_vma, sym_addr);
  308. sym_addr = sec_vma + sym_addr;
  309. @@ -1423,7 +1432,7 @@ DIS29_RELOCATION:
  310. case R_E1_WORD:
  311. relocation_needed = 1;
  312. DBG_E1("Handling Reloc <WORD>\n");
  313. - sec_vma = bfd_section_vma(abs_bfd, sym_section);
  314. + sec_vma = elf2flt_bfd_section_vma(sym_section);
  315. DBG_E1("sec_vma : [0x%x], sym_addr : [0x%x]\n",
  316. sec_vma, sym_addr);
  317. sym_addr = sec_vma + sym_addr;
  318. @@ -1450,7 +1459,7 @@ DIS29_RELOCATION:
  319. }
  320. sprintf(&addstr[0], "+0x%lx", sym_addr - (*(q->sym_ptr_ptr))->value -
  321. - bfd_section_vma(abs_bfd, sym_section));
  322. + elf2flt_bfd_section_vma(sym_section));
  323. /*
  324. @@ -1890,8 +1899,8 @@ int main(int argc, char *argv[])
  325. } else
  326. continue;
  327. - sec_size = bfd_section_size(abs_bfd, s);
  328. - sec_vma = bfd_section_vma(abs_bfd, s);
  329. + sec_size = elf2flt_bfd_section_size(s);
  330. + sec_vma = elf2flt_bfd_section_vma(s);
  331. if (sec_vma < *vma) {
  332. if (*len > 0)
  333. @@ -1920,7 +1929,7 @@ int main(int argc, char *argv[])
  334. (SEC_DATA | SEC_READONLY | SEC_RELOC)))
  335. if (!bfd_get_section_contents(abs_bfd, s,
  336. text + (s->vma - text_vma), 0,
  337. - bfd_section_size(abs_bfd, s)))
  338. + elf2flt_bfd_section_size(s)))
  339. {
  340. fatal("read error section %s", s->name);
  341. }
  342. @@ -1950,7 +1959,7 @@ int main(int argc, char *argv[])
  343. (SEC_READONLY | SEC_RELOC)))
  344. if (!bfd_get_section_contents(abs_bfd, s,
  345. data + (s->vma - data_vma), 0,
  346. - bfd_section_size(abs_bfd, s)))
  347. + elf2flt_bfd_section_size(s)))
  348. {
  349. fatal("read error section %s", s->name);
  350. }
  351. --
  352. 2.35.1