Bug 14156

Summary: --sort-section=alignment trashes init/fini sections, and anything similar
Product: binutils Reporter: Rich Felker <bugdal>
Component: ldAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: gabravier, hjl.tools
Priority: P2    
Version: 2.24   
Target Milestone: ---   
URL: http://sourceware.org/ml/binutils/2012-06/msg00225.html
Host: Target:
Build: Last reconfirmed:

Description Rich Felker 2012-05-24 00:21:30 UTC
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.
Comment 1 H.J. Lu 2012-06-24 16:42:55 UTC
Please provide a testcase.
Comment 2 H.J. Lu 2012-06-27 17:33:05 UTC
A patch is posted at

http://sourceware.org/ml/binutils/2012-06/msg00225.html
Comment 3 H.J. Lu 2012-06-27 17:35:31 UTC
(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.
Comment 4 Sourceware Commits 2012-07-10 06:51:02 UTC
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
Comment 5 H.J. Lu 2012-07-10 06:55:56 UTC
Fixed.
Comment 6 Sourceware Commits 2012-07-31 20:45:14 UTC
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
Comment 7 Sourceware Commits 2012-08-01 01:03:51 UTC
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
Comment 8 Sourceware Commits 2012-08-02 13:59:31 UTC
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