public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>,
	James Greenhalgh	<James.Greenhalgh@arm.com>
Cc: nd <nd@arm.com>, Richard Earnshaw <Richard.Earnshaw@arm.com>
Subject: Re: [PATCH v3][AArch64] Fix symbol offset limit
Date: Tue, 15 Aug 2017 17:36:00 -0000	[thread overview]
Message-ID: <DB6PR0801MB2053D2064FB84DE8459D3A72838D0@DB6PR0801MB2053.eurprd08.prod.outlook.com> (raw)
In-Reply-To: <HE1PR0801MB205836AD1159695F9AFC0D4D83B30@HE1PR0801MB2058.eurprd08.prod.outlook.com>


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  <wdijkstr@arm.com>

    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\\\+" } } */                            

      reply	other threads:[~2017-08-15 16:27 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-23 14:11 [PATCH][AArch64] " Wilco Dijkstra
2016-08-26 10:43 ` Richard Earnshaw (lists)
2016-08-26 19:07   ` Wilco Dijkstra
2016-09-12 15:30     ` [PATCH v2][AArch64] " Wilco Dijkstra
2016-09-21 14:48       ` Wilco Dijkstra
2016-10-17 12:42       ` Wilco Dijkstra
2016-10-25  9:47         ` Wilco Dijkstra
2016-11-02 16:48           ` Wilco Dijkstra
2016-11-14 13:07             ` Wilco Dijkstra
2016-12-06 15:07       ` Wilco Dijkstra
2017-01-17 15:14         ` [PATCH v3][AArch64] " Wilco Dijkstra
2017-02-02 14:44           ` Wilco Dijkstra
2017-02-23 16:58             ` Wilco Dijkstra
2017-04-20 16:03           ` Wilco Dijkstra
2017-06-13 14:00             ` Wilco Dijkstra
2017-06-14 14:07               ` James Greenhalgh
2017-06-14 16:03                 ` Wilco Dijkstra
2017-06-15 15:13                 ` Richard Earnshaw (lists)
2017-06-15 16:55                   ` Wilco Dijkstra
2017-06-15 17:39                     ` Richard Earnshaw (lists)
2017-06-15 17:51                       ` Wilco Dijkstra
2017-06-15 18:11                         ` Richard Earnshaw (lists)
2017-06-15 18:18                           ` Wilco Dijkstra
2017-06-15 18:34                             ` Richard Earnshaw (lists)
2017-06-15 18:55                               ` Wilco Dijkstra
2017-06-15 19:52                                 ` Joseph Myers
2017-06-16 15:14                                   ` Nathan Sidwell
2017-06-27 15:36               ` Wilco Dijkstra
2017-07-14 14:28                 ` Wilco Dijkstra
2017-07-21 11:23                   ` Wilco Dijkstra
2017-08-01 10:19                     ` Wilco Dijkstra
2017-08-15 17:36                       ` Wilco Dijkstra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DB6PR0801MB2053D2064FB84DE8459D3A72838D0@DB6PR0801MB2053.eurprd08.prod.outlook.com \
    --to=wilco.dijkstra@arm.com \
    --cc=James.Greenhalgh@arm.com \
    --cc=Richard.Earnshaw@arm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=nd@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).