From patchwork Wed Jul 4 15:55:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Christophe Lyon X-Patchwork-Id: 939449 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=uclibc-ng.org (client-ip=2a00:1828:2000:679::23; helo=helium.openadk.org; envelope-from=devel-bounces@uclibc-ng.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=st.com Received: from helium.openadk.org (helium.openadk.org [IPv6:2a00:1828:2000:679::23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41LQgY6HS9z9s3q for ; Thu, 5 Jul 2018 02:00:25 +1000 (AEST) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id 5046A10002; Wed, 4 Jul 2018 18:00:22 +0200 (CEST) X-Original-To: devel@uclibc-ng.org Delivered-To: devel@helium.openadk.org Received: from mx07-00178001.pphosted.com (mx07-00178001.pphosted.com [62.209.51.94]) by helium.openadk.org (Postfix) with ESMTPS id CE44B10002 for ; Wed, 4 Jul 2018 18:00:20 +0200 (CEST) Received: from pps.filterd (m0046037.ppops.net [127.0.0.1]) by mx07-.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id w64FsUwf023947; Wed, 4 Jul 2018 18:00:20 +0200 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com with ESMTP id 2k0dr4cssg-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Wed, 04 Jul 2018 18:00:07 +0200 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 3A66F56; Wed, 4 Jul 2018 16:00:06 +0000 (GMT) Received: from Webmail-eu.st.com (sfhdag5node1.st.com [10.75.127.13]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 283CE2C384; Wed, 4 Jul 2018 16:00:06 +0000 (GMT) Received: from gnb.st.com (10.75.127.44) by SFHDAG5NODE1.st.com (10.75.127.13) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Wed, 4 Jul 2018 18:00:05 +0200 From: Christophe Lyon To: , Date: Wed, 4 Jul 2018 17:55:23 +0200 Message-ID: <20180704155605.1892-11-christophe.lyon@st.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20180704155605.1892-1-christophe.lyon@st.com> References: <20180704155605.1892-1-christophe.lyon@st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.44] X-ClientProxiedBy: SFHDAG6NODE2.st.com (10.75.127.17) To SFHDAG5NODE1.st.com (10.75.127.13) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-07-04_05:, , signatures=0 Subject: [uclibc-ng-devel] [PATCH 10/32] [ARM][FDPIC] Allow to generate PIE 'static' binary X-BeenThere: devel@uclibc-ng.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: uClibc-ng Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: devel-bounces@uclibc-ng.org Sender: "devel" * libc/misc/internals/__uClibc_main.c (funcdesc_value): New. (fdpic_init_array_jump): New. (__uClibc_fini): Support __FDPIC__. (__uClibc_main): Likewise. Signed-off-by: Mickaël Guêné Signed-off-by: Christophe Lyon diff --git a/libc/misc/internals/__uClibc_main.c b/libc/misc/internals/__uClibc_main.c index d80565e..d8286f2 100644 --- a/libc/misc/internals/__uClibc_main.c +++ b/libc/misc/internals/__uClibc_main.c @@ -48,6 +48,39 @@ int _pe_secure = 0; libc_hidden_data_def(_pe_secure) +#if !defined(SHARED) && defined(__FDPIC__) +struct funcdesc_value +{ + void *entry_point; + void *got_value; +} __attribute__((__aligned__(8))); + + +/* Prevent compiler optimization that removes GOT assignment. + + Due to optimization passes (block split and move), in the rare case + where use r9 is the single instruction in a block we can have the + following behaviour: + - this block is marked as a forward block since use is not + considered as an active instruction after reload pass. + + - In this case a jump in this block can be moved to the start of the + next one and so remove use in this flow of instructions which can + lead to a removal of r9 restoration after a call. */ +#define _dl_stabilize_funcdesc(val) \ + ({ __asm__ ("" : "+m" (*(val))); (val); }) + +static void fdpic_init_array_jump(void *addr) +{ + struct funcdesc_value *fm = (struct funcdesc_value *) fdpic_init_array_jump; + struct funcdesc_value fd = {addr, fm->got_value}; + + void (*pf)(void) = (void*) _dl_stabilize_funcdesc(&fd); + + (*pf)(); +} +#endif + #ifndef SHARED void *__libc_stack_end = NULL; @@ -307,7 +340,11 @@ void __uClibc_fini(void) # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__) size_t i = __fini_array_end - __fini_array_start; while (i-- > 0) +#if !defined(SHARED) && defined(__FDPIC__) + fdpic_init_array_jump(__fini_array_start[i]); +#else (*__fini_array_start [i]) (); +#endif # endif if (__app_fini != NULL) (__app_fini)(); @@ -436,7 +473,11 @@ void __uClibc_main(int (*main)(int, char **, char **), int argc, const size_t size = __preinit_array_end - __preinit_array_start; size_t i; for (i = 0; i < size; i++) +#if !defined(SHARED) && defined(__FDPIC__) + fdpic_init_array_jump(__preinit_array_start[i]); +#else (*__preinit_array_start [i]) (); +#endif } # endif /* Run all the application's ctors now. */ @@ -452,7 +493,11 @@ void __uClibc_main(int (*main)(int, char **, char **), int argc, const size_t size = __init_array_end - __init_array_start; size_t i; for (i = 0; i < size; i++) +#if !defined(SHARED) && defined(__FDPIC__) + fdpic_init_array_jump(__init_array_start[i]); +#else (*__init_array_start [i]) (); +#endif } # endif #endif