Bug 17094 - bfd fails to parse ELF objects made with gas
Summary: bfd fails to parse ELF objects made with gas
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.24
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2014-06-26 12:51 UTC by Andre Landwehr
Modified: 2014-07-02 15:11 UTC (History)
1 user (show)

See Also:
Last reconfirmed:

Patch that makes _bfd_elf_make_section_from_shdr() not fail when sh_addralign is 0 or 1 (317 bytes, text/plain)
2014-06-26 12:51 UTC, Andre Landwehr

Note You need to log in before you can comment on or make changes to this bug.
Description Andre Landwehr 2014-06-26 12:51:52 UTC
Created attachment 7659 [details]
Patch that makes _bfd_elf_make_section_from_shdr() not fail when sh_addralign is 0 or 1

Preamble: I'm being a bit verbose here because it's the first time I dig deep into a toolchain and maybe I'm just too stupid to see the obvious...

My goal is to build a toolchain for a little-endian ARM 926ej-s CPU. Buildsystem is a x86_64-pc-linux-gnu. I built the binutils 2.24.0 and gcc 4.9.0. Building glibc then failed in configure script with

checking for .preinit_array/.init_array/.fini_array support... no
configure: error: Need linker with .init_array/.fini_array support.

It turned out that my gcc could not even compile the most simple file because a temporary file created by gas could later not be read by ld ("file not recognized: Bad value"). Digging deeper, I noticed that when doing "make check-gas" in binutils, almost half of the tests failed.

Debugging the stuff, I found that _bfd_elf_make_section_from_shdr() fails when bfd_set_section_alignment() fails. That function however only returns the alignment found, and according to the description of sh_addralign in "man 5 elf", 0 is a valid value for that.

With attached patch, "make check-gas" succeeds in all test cases and I can build a glibc. Due to other problems I could not build a full toolchain yet, so please be careful with the patch..


PS: here's the configure line I used for binutils:
./configure --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=arm-926ejs-linux-gnueabi --prefix=/usr/local/arm/cross-gcc-4.9.0-armv5te-soft/x86_64-pc-linux-gnu --enable-serial-configure --with-sysroot=/usr/local/arm/cross-gcc-4.9.0-armv5te-soft/x86_64-pc-linux-gnu/arm-926ejs-linux-gnueabi/sys-root -v
Comment 1 Alan Modra 2014-06-26 14:59:36 UTC
bfd_set_section_alignment never fails, so either your analysis is wrong or you have applied some patches to 2.24
Comment 2 Andre Landwehr 2014-07-02 12:55:22 UTC
It does not fail, but it (correctly) returns 0. And when it does, _bfd_make_section_from_shdr() returns FALSE, which is the problem. 
Sorry for the unclear wording.
Comment 3 Alan Modra 2014-07-02 14:42:39 UTC
From binutils-2.24 bfd/bfd-in2.h:

#define bfd_set_section_alignment(bfd, ptr, val) (((ptr)->alignment_power = (val)),TRUE)

How can that possibly be zero?  Ditto for the current binutils inline function.
Comment 4 Andre Landwehr 2014-07-02 15:01:01 UTC
ahhh ok I see, I removed the ",TRUE" part of the statement in an earlier step because of this compilation error:

gcc -DHAVE_CONFIG_H -I.  -I. -I. -I../bfd -I./config -I./../include -I./.. -I./../bfd -DLOCALEDIR="\"/usr/local/arm/cross-gcc-4.9.0-armv5te-soft/x86_64-pc-linux-gnu/share/locale\""  -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wshadow -Werror -g -O2 -MT write.o -MD -MP -MF .deps/write.Tpo -c -o write.o write.c
In file included from as.h:95:0,
                 from write.c:25:
write.c: In function ‘record_alignment’:
../bfd/bfd.h:303:83: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
 #define bfd_set_section_alignment(bfd, ptr, val) (((ptr)->alignment_power = (val)),TRUE)
write.c:366:5: note: in expansion of macro ‘bfd_set_section_alignment’
     bfd_set_section_alignment (stdoutput, seg, align);

Sorry, my bad. Should I close this bug and open a new one for the compilation problem?
Comment 5 Andre Landwehr 2014-07-02 15:10:02 UTC
It seems that explicitly casting the result of bfd_set_section_alignment() and the other macros that have that ",TRUE" section to void calms down the compiler
Comment 6 Alan Modra 2014-07-02 15:11:51 UTC
The compilation problem has already been fixed on mainline, so opening a bug about that is not necessary.