From patchwork Thu Apr 20 16:01:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 752866 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3w83Wj5QgPz9s7F for ; Fri, 21 Apr 2017 02:01:21 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="pwg4H3FF"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:references:in-reply-to :content-type:content-transfer-encoding:mime-version; q=dns; s= default; b=wXCma4W70b3X2o26OqhyFzUO6ldksdAxzbFTcaMRgK1sHKP+pUt7q cme148s+3OOnCdYhxq1Nd+0GVMhIMomZC8CAd0dk1ljaSz45Q+22+5zzn9ucdkVI e+Qa1X6hM148LPHcMoUyItREpVtidsV5C0YzftecRnBBKEI6x6gj1s= 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:from :to:cc:subject:date:message-id:references:in-reply-to :content-type:content-transfer-encoding:mime-version; s=default; bh=Jygrib6wgeOjR9qTtUCh0zNo3o0=; b=pwg4H3FFb3Wm19QybKPsFcMhOWmW TCQDr9Tq6MYSgIo4xbzmI9Ito3JZLgOJKppemIwamOyRoRLdxixsw5Z4T+lv1Wj7 o5qTNLvGvcLXFQS1j608ZYVJzLIkJY2xbX9QuTdhAB5V9wRvPlUSNRVMgQQbVXd0 VD5oyzUyNxXEHYs= Received: (qmail 36562 invoked by alias); 20 Apr 2017 16:01:08 -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 36526 invoked by uid 89); 20 Apr 2017 16:01:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LOTSOFHASH, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=accurately X-HELO: EUR02-HE1-obe.outbound.protection.outlook.com Received: from mail-eopbgr10087.outbound.protection.outlook.com (HELO EUR02-HE1-obe.outbound.protection.outlook.com) (40.107.1.87) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 20 Apr 2017 16:01:06 +0000 Received: from AM5PR0802MB2610.eurprd08.prod.outlook.com (10.175.46.18) by AM5PR0802MB2388.eurprd08.prod.outlook.com (10.175.43.150) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1047.13; Thu, 20 Apr 2017 16:01:05 +0000 Received: from AM5PR0802MB2610.eurprd08.prod.outlook.com ([10.175.46.18]) by AM5PR0802MB2610.eurprd08.prod.outlook.com ([10.175.46.18]) with mapi id 15.01.1047.013; Thu, 20 Apr 2017 16:01:04 +0000 From: Wilco Dijkstra To: GCC Patches , James Greenhalgh CC: nd , Richard Earnshaw Subject: Re: [PATCH v3][AArch64] Fix symbol offset limit Date: Thu, 20 Apr 2017 16:01:04 +0000 Message-ID: References: , <896d038e-f19f-d95e-2213-f3360ba71b28@arm.com>, , , , In-Reply-To: authentication-results: arm.com; dkim=none (message not signed) header.d=none; arm.com; dmarc=none action=none header.from=arm.com; x-microsoft-exchange-diagnostics: 1; AM5PR0802MB2388; 7:jj3tSPFBKU8X6DJbKlTt+gX8aDhMYxyNiZrRyI3bAFILoMsSdqooRmXWNvNV3zy7QU+HsM3dVRketsBnFgM1+CNcbEBwx4592HG6yZAUDyNicKl8Ngx/pKUN4BMMIFJ/szdxjmz+ERgXGVx82Y3HitcmwwHBr7dL4k9tCxCH91TEooap/t1ZkVUYEfjOcrJXldI63LLlzJjDtFF1v5unpjaSn0wPbrlYZgTdgtL6gCDl+LDBT6vzLknUP1eq4Fy3nwrloI4amcleTrNTteoGBGywodhdtWvcUh837HKc622dSYth3W/9vtQzPOcniEf9qRhP8/SLH6WrtbwL6ZMfSw== x-ms-office365-filtering-correlation-id: 02e2e820-2155-46b1-5a58-08d488067329 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254075)(48565401081)(201703131423075)(201703031133081)(201702281549075); SRVR:AM5PR0802MB2388; nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(180628864354917); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(10201501046)(6055026)(6041248)(20161123562025)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(20161123564025)(20161123555025)(6072148); SRVR:AM5PR0802MB2388; BCL:0; PCL:0; RULEID:; SRVR:AM5PR0802MB2388; x-forefront-prvs: 02830F0362 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(39400400002)(39840400002)(39410400002)(39850400002)(39450400003)(39860400002)(377424004)(77096006)(2900100001)(8936002)(50986999)(8676002)(74316002)(81166006)(33656002)(76176999)(102836003)(4326008)(3846002)(6116002)(54356999)(7736002)(38730400002)(3280700002)(93886004)(122556002)(53546009)(305945005)(25786009)(3660700001)(2906002)(6506006)(66066001)(6246003)(575784001)(229853002)(54906002)(7696004)(6636002)(99286003)(55016002)(86362001)(9686003)(5660300001)(2950100002)(189998001)(53936002); DIR:OUT; SFP:1101; SCL:1; SRVR:AM5PR0802MB2388; H:AM5PR0802MB2610.eurprd08.prod.outlook.com; FPR:; SPF:None; MLV:sfv; LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Apr 2017 16:01:04.6971 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0802MB2388 ping From: Wilco Dijkstra Sent: 17 January 2017 15:14 To: Richard Earnshaw; GCC Patches; James Greenhalgh Cc: nd Subject: Re: [PATCH v3][AArch64] Fix symbol offset limit   Here is v3 of the patch - tree_fits_uhwi_p was necessary to ensure the size of a declaration is an integer. So the question is whether we should allow largish offsets outside of the bounds of symbols (v1), no offsets (this version), or small offsets (small negative and positive offsets just outside a symbol are common). The only thing we can't allow is any offset like we currently do... In aarch64_classify_symbol symbols are allowed full-range offsets on relocations. This means the offset can use all of the +/-4GB offset, leaving no offset available for the symbol itself.  This results in relocation overflow and link-time errors for simple expressions like &global_char + 0xffffff00. To avoid this, limit the offset to +/-1GB so that the symbol needs to be within a 3GB offset from its references.  For the tiny code model use a 64KB offset, allowing most of the 1MB range for code/data between the symbol and its references. For symbols with a defined size, limit the offset to be within the size of the symbol. ChangeLog: 2017-01-17  Wilco Dijkstra      gcc/         * config/aarch64/aarch64.c (aarch64_classify_symbol):         Apply reasonable limit to symbol offsets.     testsuite/         * gcc.target/aarch64/symbol-range.c (foo): Set new limit.         * gcc.target/aarch64/symbol-range-tiny.c (foo): Likewise. diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c index e8d65ead95a3c5730c2ffe64a9e057779819f7b4..f1d54e332dc1cf1ef0bc4b1e46b0ebebe1c4cea4 100644 --- a/gcc/config/aarch64/aarch64.c +++ b/gcc/config/aarch64/aarch64.c @@ -9809,6 +9809,8 @@ aarch64_classify_symbol (rtx x, rtx offset)        if (aarch64_tls_symbol_p (x))          return aarch64_classify_tls_symbol (x);   +      const_tree decl = SYMBOL_REF_DECL (x); +        switch (aarch64_cmodel)          {          case AARCH64_CMODEL_TINY: @@ -9817,25 +9819,45 @@ aarch64_classify_symbol (rtx x, rtx offset)               we have no way of knowing the address of symbol at compile time               so we can't accurately say if the distance between the PC and               symbol + offset is outside the addressible range of +/-1M in the -            TINY code model.  So we rely on images not being greater than -            1M and cap the offset at 1M and anything beyond 1M will have to -            be loaded using an alternative mechanism.  Furthermore if the -            symbol is a weak reference to something that isn't known to -            resolve to a symbol in this module, then force to memory.  */ +            TINY code model.  So we limit the maximum offset to +/-64KB and +            assume the offset to the symbol is not larger than +/-(1M - 64KB). +            Furthermore force to memory if the symbol is a weak reference to +            something that doesn't resolve to a symbol in this module.  */            if ((SYMBOL_REF_WEAK (x)                 && !aarch64_symbol_binds_local_p (x)) -             || INTVAL (offset) < -1048575 || INTVAL (offset) > 1048575) +             || !IN_RANGE (INTVAL (offset), -0x10000, 0x10000))              return SYMBOL_FORCE_TO_MEM; + +         /* Limit offset to within the size of a declaration if available.  */ +         if (decl && DECL_P (decl)) +           { +             const_tree decl_size = DECL_SIZE (decl); + +             if (tree_fits_uhwi_p (decl_size) +                 && !IN_RANGE (INTVAL (offset), 0, tree_to_uhwi (decl_size))) +               return SYMBOL_FORCE_TO_MEM; +           } +            return SYMBOL_TINY_ABSOLUTE;            case AARCH64_CMODEL_SMALL:            /* Same reasoning as the tiny code model, but the offset cap here is -            4G.  */ +            1G, allowing +/-3G for the offset to the symbol.  */            if ((SYMBOL_REF_WEAK (x)                 && !aarch64_symbol_binds_local_p (x)) -             || !IN_RANGE (INTVAL (offset), HOST_WIDE_INT_C (-4294967263), -                           HOST_WIDE_INT_C (4294967264))) +             || !IN_RANGE (INTVAL (offset), -0x40000000, 0x40000000))              return SYMBOL_FORCE_TO_MEM; + +         /* Limit offset to within the size of a declaration if available.  */ +         if (decl && DECL_P (decl)) +           { +             const_tree decl_size = DECL_SIZE (decl); + +             if (tree_fits_uhwi_p (decl_size) +                 && !IN_RANGE (INTVAL (offset), 0, tree_to_uhwi (decl_size))) +               return SYMBOL_FORCE_TO_MEM; +           } +            return SYMBOL_SMALL_ABSOLUTE;            case AARCH64_CMODEL_TINY_PIC: diff --git a/gcc/testsuite/gcc.target/aarch64/symbol-range-tiny.c b/gcc/testsuite/gcc.target/aarch64/symbol-range-tiny.c index d7e46b059e41f2672b3a1da5506fa8944e752e01..d49ff4dbe5786ef6d343d2b90052c09676dd7fe5 100644 --- a/gcc/testsuite/gcc.target/aarch64/symbol-range-tiny.c +++ b/gcc/testsuite/gcc.target/aarch64/symbol-range-tiny.c @@ -1,12 +1,12 @@ -/* { dg-do compile } */ +/* { dg-do link } */  /* { dg-options "-O3 -save-temps -mcmodel=tiny" } */   -int fixed_regs[0x00200000]; +char fixed_regs[0x00200000];    int -foo() +main ()  { -  return fixed_regs[0x00080000]; +  return fixed_regs[0x000ff000];  }    /* { dg-final { scan-assembler-not "adr\tx\[0-9\]+, fixed_regs\\\+" } } */ diff --git a/gcc/testsuite/gcc.target/aarch64/symbol-range.c b/gcc/testsuite/gcc.target/aarch64/symbol-range.c index 6574cf4310430b847e77ea56bf8f20ef312d53e4..75c87c12f08004c153efc5192e5cfab566c089db 100644 --- a/gcc/testsuite/gcc.target/aarch64/symbol-range.c +++ b/gcc/testsuite/gcc.target/aarch64/symbol-range.c @@ -1,12 +1,12 @@ -/* { dg-do compile } */ +/* { dg-do link } */  /* { dg-options "-O3 -save-temps -mcmodel=small" } */   -int fixed_regs[0x200000000ULL]; +char fixed_regs[0x200000000ULL];    int -foo() +main ()  { -  return fixed_regs[0x100000000ULL]; +  return fixed_regs[0xfffff000ULL];  }    /* { dg-final { scan-assembler-not "adrp\tx\[0-9\]+, fixed_regs\\\+" } } */