This bug came up from a user of musl libc (my project) building busybox (which has --sort-section=alignment in its makefile) on arm. He was experiencing crashes in busybox and nothing else, and upon examining the binary, found that the _init label (from crti.o) was getting reordered *after* the return code (from crtn.o), causing the call to _init to run off into other unrelated code. The problem has not been observed on any other systems (non-arm) with musl, nor with other libc implementations, but as far as I can tell, --sort-section=alignment is only working BY CHANCE on these systems due to the section sizes/alignments happening to keep the parts in the right order. I'm not sure what the right fix for this is, but I think the linker scripts should do something to ensure that sections which are pieced-together in a particular link order (init/fini and probably also ctors/dtors, and perhaps others) never get reordered. If that's not possible, then this option should be documented as dangerous/broken so that projects don't put it in their makefiles.
Please provide a testcase.
A patch is posted at http://sourceware.org/ml/binutils/2012-06/msg00225.html
(In reply to comment #0) > > I'm not sure what the right fix for this is, but I think the linker scripts > should do something to ensure that sections which are pieced-together in a > particular link order (init/fini and probably also ctors/dtors, and perhaps > others) never get reordered. If that's not possible, then this option should be > documented as dangerous/broken so that projects don't put it in their > makefiles. ctors/dtors sections contain pointers. The behavor of their order is undefined.
CVSROOT: /cvs/src Module name: src Changes by: hjl@sourceware.org 2012-07-10 06:50:57 Modified files: ld : ChangeLog NEWS ld.h ld.texinfo ldgram.y ldlang.c ldlex.l ld/testsuite : ChangeLog Added files: ld/testsuite/ld-elf: fini0.s fini1.s fini2.s fini3.s finin.s foo0.s foo1.s foo2.s foo3.s foon.s init0.s init1.s init2.s init3.s initn.s pr14156a.d pr14156b.d pr14156c.d pr14156c.t Log message: Add SORT_NONE and don't sort sort .init/.fini sections ld/ PR ld/14156 * NEWS: Mention SORT_NONE. * ld.h (sort_type): Add by_none. * ld.texinfo: Document SORT_NONE. * ldgram.y: Handle SORT_NONE. * ldlang.c (update_wild_statements): Handle by_none. Don't sort .init/.fini sections. * ldlex.l: Add SORT_NONE. ld/testsuite/ PR ld/14156 * ld-elf/fini0.s: New file. * ld-elf/fini1.s: Likewise. * ld-elf/fini2.s: Likewise. * ld-elf/fini3.s: Likewise. * ld-elf/finin.s: Likewise. * ld-elf/foo0.s: Likewise. * ld-elf/foo1.s: Likewise. * ld-elf/foo2.s: Likewise. * ld-elf/foo3.s: Likewise. * ld-elf/foon.s: Likewise. * ld-elf/init0.s: Likewise. * ld-elf/init1.s: Likewise. * ld-elf/init2.s: Likewise. * ld-elf/init3.s: Likewise. * ld-elf/initn.s: Likewise. * ld-elf/pr14156a.d: Likewise. * ld-elf/pr14156b.d: Likewise. * ld-elf/pr14156c.d: Likewise. * ld-elf/pr14156c.t: Likewise. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&r1=1.2465&r2=1.2466 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/NEWS.diff?cvsroot=src&r1=1.129&r2=1.130 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ld.h.diff?cvsroot=src&r1=1.54&r2=1.55 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ld.texinfo.diff?cvsroot=src&r1=1.282&r2=1.283 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldgram.y.diff?cvsroot=src&r1=1.69&r2=1.70 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldlang.c.diff?cvsroot=src&r1=1.395&r2=1.396 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldlex.l.diff?cvsroot=src&r1=1.55&r2=1.56 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1576&r2=1.1577 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/fini0.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/fini1.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/fini2.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/fini3.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/finin.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/foo0.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/foo1.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/foo2.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/foo3.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/foon.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/init0.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/init1.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/init2.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/init3.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/initn.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr14156a.d.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr14156b.d.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr14156c.d.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr14156c.t.diff?cvsroot=src&r1=NONE&r2=1.1
Fixed.
CVSROOT: /cvs/src Module name: src Changes by: hjl@sourceware.org 2012-07-31 20:45:02 Modified files: ld/testsuite : ChangeLog ld/testsuite/ld-elf: fini0.s foo0.s init0.s pr14156c.t Log message: Update PR ld/14156 tests * ld-elf/fini0.s: Indent global directive. * ld-elf/foo0.s: Likewise. * ld-elf/init0.s: Likewise. * ld-elf/pr14156c.t: Keep text section. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1579&r2=1.1580 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/fini0.s.diff?cvsroot=src&r1=1.1&r2=1.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/foo0.s.diff?cvsroot=src&r1=1.2&r2=1.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/init0.s.diff?cvsroot=src&r1=1.1&r2=1.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/pr14156c.t.diff?cvsroot=src&r1=1.1&r2=1.2
CVSROOT: /cvs/src Module name: src Changes by: hjl@sourceware.org 2012-08-01 01:03:47 Modified files: ld : ChangeLog ldlang.c ld/scripttempl : elf.sc elf32msp430.sc elf32msp430_3.sc elfd10v.sc elfd30v.sc elfxtensa.sc Log message: Don't call compare_section in case of by_none PR ld/14156 * ldlang.c (wild_sort): Don't call compare_section in case of by_none. * scripttempl/elf.sc: Add SORT_NONE to .init and .fini sections. * scripttempl/elf32msp430.sc: Likewise. * scripttempl/elf32msp430_3.sc: Likewise. * scripttempl/elfd10v.sc: Likewise. * scripttempl/elfd30v.sc: Likewise. * scripttempl/elfxtensa.sc: Likewise. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&r1=1.2472&r2=1.2473 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldlang.c.diff?cvsroot=src&r1=1.397&r2=1.398 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elf.sc.diff?cvsroot=src&r1=1.113&r2=1.114 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elf32msp430.sc.diff?cvsroot=src&r1=1.10&r2=1.11 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elf32msp430_3.sc.diff?cvsroot=src&r1=1.8&r2=1.9 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elfd10v.sc.diff?cvsroot=src&r1=1.16&r2=1.17 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elfd30v.sc.diff?cvsroot=src&r1=1.14&r2=1.15 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elfxtensa.sc.diff?cvsroot=src&r1=1.26&r2=1.27
CVSROOT: /cvs/src Module name: src Branch: binutils-2_23-branch Changes by: hjl@sourceware.org 2012-08-02 13:59:25 Modified files: ld : ChangeLog ldlang.c ld/scripttempl : elf.sc elf32msp430.sc elf32msp430_3.sc elfd10v.sc elfd30v.sc elfxtensa.sc Log message: Properly handle by_none Backport from mainline PR ld/14156 * ldlang.c (wild_sort): Properly handle by_none. * scripttempl/elf.sc: Add SORT_NONE to .init and .fini sections. * scripttempl/elf32msp430.sc: Likewise. * scripttempl/elf32msp430_3.sc: Likewise. * scripttempl/elfd10v.sc: Likewise. * scripttempl/elfd30v.sc: Likewise. * scripttempl/elfxtensa.sc: Likewise. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.2470.2.1&r2=1.2470.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldlang.c.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.397&r2=1.397.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elf.sc.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.113&r2=1.113.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elf32msp430.sc.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.10&r2=1.10.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elf32msp430_3.sc.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.8&r2=1.8.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elfd10v.sc.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.16&r2=1.16.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elfd30v.sc.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.14&r2=1.14.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/elfxtensa.sc.diff?cvsroot=src&only_with_tag=binutils-2_23-branch&r1=1.26&r2=1.26.2.1