From patchwork Wed Aug 21 13:34:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jozef Lawrynowicz X-Patchwork-Id: 1150847 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-507450-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mittosystems.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="V6ZEc+PN"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mittosystems.com header.i=@mittosystems.com header.b="BGSCY20F"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46D7tk5BWnz9s3Z for ; Wed, 21 Aug 2019 23:34:36 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=LwTImhyD0spMvzbpIsj4lLespjWBzotneXvNvuB+5JRvkVvMz9Pga Lunb4MsOVGg0ee752gfDYjN7q5MccyKO+rueSfpZl8G9NmQQd+LHS2uQnvPm78VO T82BbSv3C/H69+xUuKbfoRp4gnQ1IQDF0l8+wcFbhCjI7BWmHViFrg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=q/DMZ1aoBEGcyqWwcO2zySiHRLc=; b=V6ZEc+PNXRURTyERBbSb ePLVThEg/C976az+d+RfLiahXxkdAGaWRNuoYzcreRNpAQM0FZk9XbmqKGoMcgL9 UQO1iODTP4M+AIKCfAltIfXhS55Um+r+eYZ24h3fEbnmUpblyd8/g+bipOtJxixx ZR+eq04rQlwwcOd3NCLjviM= Received: (qmail 29426 invoked by alias); 21 Aug 2019 13:34:29 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 29415 invoked by uid 89); 21 Aug 2019 13:34:29 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=UD:www.cygwin.com, www.cygwin.com, wwwcygwincom, 4748 X-HELO: mail-wr1-f68.google.com Received: from mail-wr1-f68.google.com (HELO mail-wr1-f68.google.com) (209.85.221.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 21 Aug 2019 13:34:27 +0000 Received: by mail-wr1-f68.google.com with SMTP id b16so2053642wrq.9 for ; Wed, 21 Aug 2019 06:34:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mittosystems.com; s=google; h=date:from:to:subject:message-id:mime-version; bh=shM5g4sLK16f4Rvh3I0Givd0rfQ2ZUM11xqXLb/+Gx4=; b=BGSCY20FCVND9J3u8FP6fVA0h/01dYBSxOFGEEPai8+70gqABuFizf8nBl2FDlJ75j 5I7BonY1v4uw1dYtTMWryf9oAd3/mnVdFDuEdZgSMN2TZ7+B4v7HhJPP4Axr0KJ90qwf Ex8pj9fQjMJmXUig9mr/ecj/kkQZ4sJZvdwbuCSoG3HWyQUrGkXXED0gBBSpcHzQbwQK RHzlpFLhh59y3t7j9gRAh51jDizh8rFP59afyZF5muSErJmSej1Dn3FN3ADX3bV0Fxde 1axahaxLYULaVl4+vgMPS7GVtIAWkWR+Mmudg4DCEYFqgMTbEwUaEI+T93R0j3TTvnGl YAFQ== Received: from jozef-kubuntu ([2a01:4b00:87fd:900:49e4:89d:ed5f:f40]) by smtp.gmail.com with ESMTPSA id w8sm3782077wmc.1.2019.08.21.06.34.23 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 21 Aug 2019 06:34:24 -0700 (PDT) Date: Wed, 21 Aug 2019 14:34:20 +0100 From: Jozef Lawrynowicz To: "gcc-patches@gcc.gnu.org" Subject: [PATCH] libgcc/crtstuff.c: Fix incorrect alignment of entries in CRT data structures (PR target/91306) Message-ID: <20190821143420.42e074d6@jozef-kubuntu> MIME-Version: 1.0 X-IsSubscribed: yes As described in PR target/91306, the alignment of entries in CRT data structures as set in libgcc/crtstuff.c can be too large for msp430-elf. crtstuff.c assumes the correct alignment of entries with a type of (void *) or int32 is the "sizeof" that type. However, for msp430-elf in the large memory model, pointer size is 4 bytes, but they only need to be 2-byte aligned. Likewise for int32. This is causing problems for __frame_dummy_init_array_entry, which gets inserted into .init_array. It is being forced into a 4 byte alignment, which can result in a gap between __frame_dummy_init_array_entry and the previous entry (or the start of the table). So when the startup code comes to run through the entries in .init_array, it interprets 2-bytes of padding 0s as part of a function address, resulting in a function call to the incorrect location. This issue has only recently appeared because msp430-elf was just transitioned to use init/fini_array by default, as mandated by the mspabi. The other CRT array entries in crtstuff.c are not used for msp430-elf, so aren't causing problems. As suggested in the PR, I tried changing the alignment of the array entry from sizeof(func_ptr) to __alignof__(func_ptr), and this fixed the issue. In the attached patch I also updated the other uses of sizeof in "aligned" attributes to use __alignof__ instead. I understand the alignment to sizeof(func_ptr) was originally added to fix an issue on mips64 (r182066 https://gcc.gnu.org/ml/gcc-patches/2011-12/msg00393.html), however I do not have access to a mips64 platform for which to bootstrap on. I tried building a cross-compiler with the "aligned" attributes removed to see if I could reproduce the failure, but the build completed successfully. I built the mips64 cross-compiler with my patch applied and it also completed successfully. I successfully regtested the patch for msp430-elf. It fixes many execution failures for msp430-elf/-mlarge. I also successfully bootstrapped and regtested x86_64-pc-linux-gnu (g++ gcc gfortran libatomic libgomp libitm libstdc++ objc). According to the mips eabi (is this the right one? http://www.cygwin.com/ml/binutils/2003-06/msg00436.html), pointers have 64-bit size and alignment in 64-bit mode, so I am assuming using __alignof__ instead of sizeof isn't going to cause problems. Ok for trunk? From 636ffa0c4f15047dc27c829d9a3c8fea9ad5d635 Mon Sep 17 00:00:00 2001 From: Jozef Lawrynowicz Date: Thu, 15 Aug 2019 14:17:25 +0100 Subject: [PATCH] libgcc/crtstuff.c: Fix incorrect alignment of entries in CRT data structures libgcc/ChangeLog: 2019-08-21 Jozef Lawrynowicz * crtstuff.c (__CTOR_LIST__): Align to the "__alignof__" the array element type, instead of "sizeof" the element type. (__DTOR_LIST__): Likewise. (__TMC_LIST__): Likewise. (__do_global_dtors_aux_fini_array_entry): Likewise. (__frame_dummy_init_array_entry): Likewise. (__CTOR_END__): Likewise. (__DTOR_END__): Likweise. (__FRAME_END__): Likewise. (__TMC_END__): Likewise. --- libgcc/crtstuff.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/libgcc/crtstuff.c b/libgcc/crtstuff.c index 4927a9f8977..c8a8e2c87a5 100644 --- a/libgcc/crtstuff.c +++ b/libgcc/crtstuff.c @@ -233,11 +233,11 @@ CTOR_LIST_BEGIN; static func_ptr force_to_data[1] __attribute__ ((__used__)) = { }; asm (__LIBGCC_CTORS_SECTION_ASM_OP__); STATIC func_ptr __CTOR_LIST__[1] - __attribute__ ((__used__, aligned(sizeof(func_ptr)))) + __attribute__ ((__used__, aligned(__alignof__(func_ptr)))) = { (func_ptr) (-1) }; #else STATIC func_ptr __CTOR_LIST__[1] - __attribute__ ((__used__, section(".ctors"), aligned(sizeof(func_ptr)))) + __attribute__ ((__used__, section(".ctors"), aligned(__alignof__(func_ptr)))) = { (func_ptr) (-1) }; #endif /* __CTOR_LIST__ alternatives */ @@ -246,11 +246,11 @@ DTOR_LIST_BEGIN; #elif defined(__LIBGCC_DTORS_SECTION_ASM_OP__) asm (__LIBGCC_DTORS_SECTION_ASM_OP__); STATIC func_ptr __DTOR_LIST__[1] - __attribute__ ((aligned(sizeof(func_ptr)))) + __attribute__ ((aligned(__alignof__(func_ptr)))) = { (func_ptr) (-1) }; #else STATIC func_ptr __DTOR_LIST__[1] - __attribute__((section(".dtors"), aligned(sizeof(func_ptr)))) + __attribute__((section(".dtors"), aligned(__alignof__(func_ptr)))) = { (func_ptr) (-1) }; #endif /* __DTOR_LIST__ alternatives */ #endif /* USE_INITFINI_ARRAY */ @@ -265,7 +265,7 @@ STATIC EH_FRAME_SECTION_CONST char __EH_FRAME_BEGIN__[] #if USE_TM_CLONE_REGISTRY STATIC func_ptr __TMC_LIST__[] - __attribute__((used, section(".tm_clone_table"), aligned(sizeof(void*)))) + __attribute__((used, section(".tm_clone_table"), aligned(__alignof__(void*)))) = { }; # ifdef HAVE_GAS_HIDDEN extern func_ptr __TMC_END__[] __attribute__((__visibility__ ("hidden"))); @@ -430,8 +430,8 @@ __do_global_dtors_aux (void) CRT_CALL_STATIC_FUNCTION (FINI_SECTION_ASM_OP, __do_global_dtors_aux) #elif defined (FINI_ARRAY_SECTION_ASM_OP) static func_ptr __do_global_dtors_aux_fini_array_entry[] - __attribute__ ((__used__, section(".fini_array"), aligned(sizeof(func_ptr)))) - = { __do_global_dtors_aux }; + __attribute__ ((__used__, section(".fini_array"), + aligned(__alignof__(func_ptr)))) = { __do_global_dtors_aux }; #else /* !FINI_SECTION_ASM_OP && !FINI_ARRAY_SECTION_ASM_OP */ static void __attribute__((used)) __do_global_dtors_aux_1 (void) @@ -474,8 +474,8 @@ frame_dummy (void) CRT_CALL_STATIC_FUNCTION (__LIBGCC_INIT_SECTION_ASM_OP__, frame_dummy) #else /* defined(__LIBGCC_INIT_SECTION_ASM_OP__) */ static func_ptr __frame_dummy_init_array_entry[] - __attribute__ ((__used__, section(".init_array"), aligned(sizeof(func_ptr)))) - = { frame_dummy }; + __attribute__ ((__used__, section(".init_array"), + aligned(__alignof__(func_ptr)))) = { frame_dummy }; #endif /* !defined(__LIBGCC_INIT_SECTION_ASM_OP__) */ #endif /* USE_EH_FRAME_REGISTRY || USE_TM_CLONE_REGISTRY */ @@ -588,11 +588,11 @@ CTOR_LIST_END; static func_ptr force_to_data[1] __attribute__ ((__used__)) = { }; asm (__LIBGCC_CTORS_SECTION_ASM_OP__); STATIC func_ptr __CTOR_END__[1] - __attribute__((aligned(sizeof(func_ptr)))) + __attribute__((aligned(__alignof__(func_ptr)))) = { (func_ptr) 0 }; #else STATIC func_ptr __CTOR_END__[1] - __attribute__((section(".ctors"), aligned(sizeof(func_ptr)))) + __attribute__((section(".ctors"), aligned(__alignof__(func_ptr)))) = { (func_ptr) 0 }; #endif @@ -607,16 +607,16 @@ func_ptr __DTOR_END__[1] #ifndef __LIBGCC_DTORS_SECTION_ASM_OP__ section(".dtors"), #endif - aligned(sizeof(func_ptr)), visibility ("hidden"))) + aligned(__alignof__(func_ptr)), visibility ("hidden"))) = { (func_ptr) 0 }; #elif defined(__LIBGCC_DTORS_SECTION_ASM_OP__) asm (__LIBGCC_DTORS_SECTION_ASM_OP__); STATIC func_ptr __DTOR_END__[1] - __attribute__ ((used, aligned(sizeof(func_ptr)))) + __attribute__ ((used, aligned(__alignof__(func_ptr)))) = { (func_ptr) 0 }; #else STATIC func_ptr __DTOR_END__[1] - __attribute__((used, section(".dtors"), aligned(sizeof(func_ptr)))) + __attribute__((used, section(".dtors"), aligned(__alignof__(func_ptr)))) = { (func_ptr) 0 }; #endif #endif /* USE_INITFINI_ARRAY */ @@ -635,7 +635,7 @@ typedef short int32; # endif STATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[] __attribute__ ((used, section(__LIBGCC_EH_FRAME_SECTION_NAME__), - aligned(sizeof(int32)))) + aligned(__alignof__(int32)))) = { 0 }; #endif /* __LIBGCC_EH_FRAME_SECTION_NAME__ */ @@ -644,7 +644,8 @@ STATIC EH_FRAME_SECTION_CONST int32 __FRAME_END__[] static # endif func_ptr __TMC_END__[] - __attribute__((used, section(".tm_clone_table"), aligned(sizeof(void *)))) + __attribute__((used, section(".tm_clone_table"), + aligned(__alignof__(void *)))) # ifdef HAVE_GAS_HIDDEN __attribute__((__visibility__ ("hidden"))) = { }; # else -- 2.17.1