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