From f4df877297f500e9a39a8d96275c2520079d6e40 Mon Sep 17 00:00:00 2001 From: John Marino Date: Mon, 23 Jan 2012 01:21:15 +0100 Subject: [PATCH] csu: Add .note.ABI-tag and .note.GNU-stack For all arches except sparc, gcc emits the section directive for the following struct with a PROGBITS type. However, newer versions of binutils (after 2.16.90) require the section to be of NOTE type, to guarantee that the .note.ABI-tag section correctly ends up in the first page of the final executable. Unfortunately, there is no clean way to tell gcc to use another section type, so this C file (or the C file that includes it) must be compiled in multiple steps: - Compile the .c file to a .s file. - Edit the .s file to change the 'progbits' type to 'note', for the section directive that defines the .note.ABI-tag section. - Compile the .s file to an object file. These steps are done in the invididual Makefiles for each applicable arch. Taken-from: FreeBSD SVN 217375 (13 JAN 2011) On i386 crtn.S: Keep the stack aligned to a 16 byte boundary when calling init functions so that we don't cause a bus error if they start storing SSE math stuff on the stack. Taken-from: FreeBSD SVN 146369 (19 MAY 2005) --- lib/csu/common/crtbrand.c | 20 ++++++++++++++++-- lib/csu/i386/Makefile.csu | 41 +++++++++++++++++++++++++++---------- lib/csu/i386/crt1_s.S | 4 +++- lib/csu/i386/crti.S | 7 +++++-- lib/csu/i386/crtn.S | 7 +++++-- lib/csu/x86_64/Makefile.csu | 35 +++++++++++++++++++++++-------- lib/csu/x86_64/crti.S | 6 +++--- lib/csu/x86_64/crtn.S | 6 +++--- 8 files changed, 94 insertions(+), 32 deletions(-) diff --git a/lib/csu/common/crtbrand.c b/lib/csu/common/crtbrand.c index c83a4832db..1d0ddf3eb2 100644 --- a/lib/csu/common/crtbrand.c +++ b/lib/csu/common/crtbrand.c @@ -22,8 +22,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $FreeBSD: src/lib/csu/common/crtbrand.c,v 1.1.2.1 2000/10/30 20:32:24 obrien Exp $ - * $DragonFly: src/lib/csu/common/crtbrand.c,v 1.5 2006/09/30 18:07:04 corecode Exp $ + * $FreeBSD: src/lib/csu/common/crtbrand.c SVN 217375 2011/01/13 dim $ */ #include @@ -36,6 +35,23 @@ * Special ".note" entry specifying the ABI version. See * http://www.netbsd.org/Documentation/kernel/elf-notes.html * for more information. + * + * For all arches except sparc, gcc emits the section directive for the + * following struct with a PROGBITS type. However, newer versions of binutils + * (after 2.16.90) require the section to be of NOTE type, to guarantee that the + * .note.ABI-tag section correctly ends up in the first page of the final + * executable. + * + * Unfortunately, there is no clean way to tell gcc to use another section type, + * so this C file (or the C file that includes it) must be compiled in multiple + * steps: + * + * - Compile the .c file to a .s file. + * - Edit the .s file to change the 'progbits' type to 'note', for the section + * directive that defines the .note.ABI-tag section. + * - Compile the .s file to an object file. + * + * These steps are done in the individual Makefiles for each applicable arch. */ static const struct { int32_t namesz; diff --git a/lib/csu/i386/Makefile.csu b/lib/csu/i386/Makefile.csu index 62f6e91e65..6342d1c979 100644 --- a/lib/csu/i386/Makefile.csu +++ b/lib/csu/i386/Makefile.csu @@ -1,10 +1,11 @@ -# $FreeBSD: src/lib/csu/i386-elf/Makefile,v 1.6.2.5 2002/11/23 17:44:29 ru Exp $ +# $FreeBSD: src/lib/csu/i386-elf/Makefile SVN 217375 2011/01/13 dim $ -SRCS+= crti.S crtn.S +SRCS= crti.S crtn.S OBJS+= gcrt1.o crt1.o Scrt1.o INSTALLOBJS+= crt1.o crti.o crtn.o gcrt1.o Scrt1.o -CLEANFILES+= crt1.o crti.o crtn.o gcrt1.o Scrt1.o \ - crt1_c.o crt1_s.o gcrtl_c.o Scrt1_c.o +CLEANFILES= crt1.o crti.o crtn.o gcrt1.o Scrt1.o +CLEANFILES+= crt1_c.o crt1_s.o gcrt1_c.o Scrt1_c.o +CLEANFILES+= crt1_c.s gcrt1_c.s Scrt1_c.s WARNS?= 2 CFLAGS+= -I${CSUDIR}/../common \ @@ -12,19 +13,37 @@ CFLAGS+= -I${CSUDIR}/../common \ .PATH: ${CSUDIR} ${CSUDIR}/../common -gcrt1_c.o: crt1_c.c - ${CC} ${CFLAGS} -DGCRT -c -o gcrt1_c.o ${CSUDIR}/crt1_c.c +# See the comment in lib/csu/common/crtbrand.c for the reason crt1_c.c is not +# directly compiled to .o files. -gcrt1.o: gcrt1_c.o crt1_s.o +gcrt1_c.s: crt1_c.c + ${CC} ${CFLAGS} -DGCRT -S -o ${.TARGET} ${CSUDIR}/crt1_c.c + sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET} + +gcrt1_c.o: gcrt1_c.s + ${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1_c.s + +gcrt1.o: gcrt1_c.o crt1_s.o ${LD} ${LDFLAGS} -o gcrt1.o -r crt1_s.o gcrt1_c.o -crt1.o: crt1_c.o crt1_s.o +crt1_c.s: crt1_c.c + ${CC} ${CFLAGS} -S -o ${.TARGET} ${CSUDIR}/crt1_c.c + sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET} + +crt1_c.o: crt1_c.s + ${CC} ${CFLAGS} -c -o ${.TARGET} crt1_c.s + +crt1.o: crt1_c.o crt1_s.o ${LD} ${LDFLAGS} -o crt1.o -r crt1_s.o crt1_c.o objcopy --localize-symbol _start1 crt1.o -Scrt1_c.o: crt1_c.c - ${CC} ${CFLAGS} -fPIC -DPIC -c -o Scrt1_c.o ${CSUDIR}/crt1_c.c +Scrt1_c.s: crt1_c.c + ${CC} ${CFLAGS} -fPIC -DPIC -S -o ${.TARGET} ${CSUDIR}/crt1_c.c + sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET} + +Scrt1_c.o: Scrt1_c.s + ${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1_c.s -Scrt1.o: Scrt1_c.o crt1_s.o +Scrt1.o: Scrt1_c.o crt1_s.o ${LD} ${LDFLAGS} -o Scrt1.o -r crt1_s.o Scrt1_c.o objcopy --localize-symbol _start1 Scrt1.o diff --git a/lib/csu/i386/crt1_s.S b/lib/csu/i386/crt1_s.S index 9b029ec94b..d64d037bf5 100644 --- a/lib/csu/i386/crt1_s.S +++ b/lib/csu/i386/crt1_s.S @@ -22,7 +22,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $FreeBSD: src/lib/csi/i386-elf/crt1_s.S, svn 217383 2011/01/19 kib $ + * $FreeBSD: src/lib/csi/i386-elf/crt1_s.S, svn 217383 2011/01/13 kib $ */ .text @@ -47,3 +47,5 @@ _start: int3 .cfi_endproc .size _start, . - _start + + .section .note.GNU-stack,"",%progbits diff --git a/lib/csu/i386/crti.S b/lib/csu/i386/crti.S index 56067e5129..628d6d3137 100644 --- a/lib/csu/i386/crti.S +++ b/lib/csu/i386/crti.S @@ -22,8 +22,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $FreeBSD: src/lib/csu/i386-elf/crti.S,v 1.2.2.2 2000/10/30 20:32:25 obrien Exp $ - * $DragonFly: src/lib/csu/i386/crti.S,v 1.1 2004/06/15 08:53:09 joerg Exp $ + * $FreeBSD: src/lib/csu/i386-elf/crti.S SVN 217105 2011/01/07 kib $ */ .section .init,"ax",@progbits @@ -31,9 +30,13 @@ .globl _init .type _init,@function _init: + sub $12,%esp /* re-align stack pointer */ .section .fini,"ax",@progbits .align 4 .globl _fini .type _fini,@function _fini: + sub $12,%esp /* re-align stack pointer */ + + .section .note.GNU-stack,"",%progbits diff --git a/lib/csu/i386/crtn.S b/lib/csu/i386/crtn.S index 04da6dc1b8..d0f9834e43 100644 --- a/lib/csu/i386/crtn.S +++ b/lib/csu/i386/crtn.S @@ -22,12 +22,15 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $FreeBSD: src/lib/csu/i386-elf/crtn.S,v 1.2.2.2 2000/10/30 20:32:25 obrien Exp $ - * $DragonFly: src/lib/csu/i386/crtn.S,v 1.1 2004/06/15 08:53:09 joerg Exp $ + * $FreeBSD: src/lib/csu/i386-elf/crtn.S SVN 217105 2011/01/07 kib $ */ .section .init,"ax",@progbits + add $12,%esp ret .section .fini,"ax",@progbits + add $12,%esp ret + + .section .note.GNU-stack,"",%progbits diff --git a/lib/csu/x86_64/Makefile.csu b/lib/csu/x86_64/Makefile.csu index c0ea976a0e..c5b2103214 100644 --- a/lib/csu/x86_64/Makefile.csu +++ b/lib/csu/x86_64/Makefile.csu @@ -1,9 +1,10 @@ -# $FreeBSD: src/lib/csu/amd64/Makefile,v 1.18 2003/06/30 12:53:39 ru Exp $ +# $FreeBSD: src/lib/csu/amd64/Makefile SVN 217375 2011/01/13 dim $ -SRCS+= crt1.c crti.S crtn.S +SRCS= crt1.c crti.S crtn.S OBJS+= Scrt1.o gcrt1.o -INSTALLOBJS+= crt1.o crti.o crtn.o gcrt1.o -CLEANFILES+= crt1.o crti.o crtn.o gcrt1.o +INSTALLOBJS+= crt1.o crti.o crtn.o Scrt1.o gcrt1.o +CLEANFILES= crt1.s gcrt1.s Scrt1.s +CLEANFILES+= crt1.o crti.o crtn.o Scrt1.o gcrt1.o WARNS?= 2 CFLAGS+= -I${CSUDIR}/../common \ @@ -12,8 +13,26 @@ CFLAGS+= -fno-omit-frame-pointer .PATH: ${CSUDIR} ${CSUDIR}/../common -gcrt1.o: crt1.c - ${CC} ${CFLAGS} -DGCRT -c -o gcrt1.o ${CSUDIR}/crt1.c +# See the comment in lib/csu/common/crtbrand.c for the reason crt1.c is not +# directly compiled to .o files. -Scrt1.o: crt1.c - ${CC} ${CFLAGS} -fPIC -DPIC -c -o Scrt1.o ${CSUDIR}/crt1.c +crt1.s: crt1.c + ${CC} ${CFLAGS} -S -o ${.TARGET} ${CSUDIR}/crt1.c + sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET} + +crt1.o: crt1.s + ${CC} ${CFLAGS} -c -o ${.TARGET} crt1.s + +gcrt1.s: crt1.c + ${CC} ${CFLAGS} -DGCRT -S -o ${.TARGET} ${CSUDIR}/crt1.c + sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET} + +gcrt1.o: gcrt1.s + ${CC} ${CFLAGS} -c -o ${.TARGET} gcrt1.s + +Scrt1.s: crt1.c + ${CC} ${CFLAGS} -fPIC -DPIC -S -o ${.TARGET} ${CSUDIR}/crt1.c + sed -i "" -e '/\.note\.ABI-tag/s/progbits/note/' ${.TARGET} + +Scrt1.o: Scrt1.s + ${CC} ${CFLAGS} -c -o ${.TARGET} Scrt1.s diff --git a/lib/csu/x86_64/crti.S b/lib/csu/x86_64/crti.S index 76c30576c8..d47fff5012 100644 --- a/lib/csu/x86_64/crti.S +++ b/lib/csu/x86_64/crti.S @@ -30,13 +30,13 @@ .globl _init .type _init,@function _init: - subq $8,%rsp + subq $8,%rsp .section .fini,"ax",@progbits .align 4 .globl _fini .type _fini,@function _fini: - subq $8,%rsp + subq $8,%rsp - .section .rodata + .section .note.GNU-stack,"",%progbits diff --git a/lib/csu/x86_64/crtn.S b/lib/csu/x86_64/crtn.S index 4f75eae2f5..15664e03d0 100644 --- a/lib/csu/x86_64/crtn.S +++ b/lib/csu/x86_64/crtn.S @@ -26,11 +26,11 @@ */ .section .init,"ax",@progbits - addq $8,%rsp + addq $8,%rsp ret .section .fini,"ax",@progbits - addq $8,%rsp + addq $8,%rsp ret - .section .rodata + .section .note.GNU-stack,"",%progbits -- 2.41.0