This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch] config/arm/arm.c: Fix PR target/27387.
- From: Kazu Hirata <kazu at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: paul at codesourcery dot com
- Date: Tue, 2 May 2006 08:21:00 -0700
- Subject: [patch] config/arm/arm.c: Fix PR target/27387.
Hi,
Attached is a patch to fix PR target/27387.
Without this patch, the target address that the Thumb's thunk loads is
absolute even with -fPIC. This patch makes it pc-relative if we are
generating PIC.
Tested on arm-none-eabi. Committed to mainline, gcc-4_1-branch, and
csl/sourcerygxx-4_1 as approved by Paul Brook in private
communication.
Kazu Hirata
2006-05-02 Kazu Hirata <kazu@codesourcery.com>
PR target/27387
* arm.c (arm_output_mi_thunk): Use pc-relative addressing when
-mthumb -fPIC are used.
2006-05-02 Kazu Hirata <kazu@codesourcery.com>
PR target/27387
* gcc.target/arm/arm.exp: New.
* gcc.target/arm/pr27387.C: Likewise.
Index: config/arm/arm.c
===================================================================
--- config/arm/arm.c (revision 113358)
+++ config/arm/arm.c (working copy)
@@ -14692,6 +14692,7 @@ arm_output_mi_thunk (FILE *file, tree th
{
static int thunk_label = 0;
char label[256];
+ char labelpc[256];
int mi_delta = delta;
const char *const mi_op = mi_delta < 0 ? "sub" : "add";
int shift = 0;
@@ -14706,6 +14707,23 @@ arm_output_mi_thunk (FILE *file, tree th
fputs ("\tldr\tr12, ", file);
assemble_name (file, label);
fputc ('\n', file);
+ if (flag_pic)
+ {
+ /* If we are generating PIC, the ldr instruction below loads
+ "(target - 7) - .LTHUNKPCn" into r12. The pc reads as
+ the address of the add + 8, so we have:
+
+ r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
+ = target + 1.
+
+ Note that we have "+ 1" because some versions of GNU ld
+ don't set the low bit of the result for R_ARM_REL32
+ relocations against thumb function symbols. */
+ ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
+ assemble_name (file, labelpc);
+ fputs (":\n", file);
+ fputs ("\tadd\tr12, pc, r12\n", file);
+ }
}
while (mi_delta != 0)
{
@@ -14726,8 +14744,21 @@ arm_output_mi_thunk (FILE *file, tree th
ASM_OUTPUT_ALIGN (file, 2);
assemble_name (file, label);
fputs (":\n", file);
- assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
+ if (flag_pic)
+ {
+ /* Output ".word .LTHUNKn-7-.LTHUNKPCn". */
+ rtx tem = XEXP (DECL_RTL (function), 0);
+ tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
+ tem = gen_rtx_MINUS (GET_MODE (tem),
+ tem,
+ gen_rtx_SYMBOL_REF (Pmode,
+ ggc_strdup (labelpc)));
+ assemble_integer (tem, 4, BITS_PER_WORD, 1);
+ }
+ else
+ /* Output ".word .LTHUNKn". */
+ assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
}
else
{
--- /dev/null 2006-03-11 08:41:44.866675760 -0800
+++ testsuite/gcc.target/arm/arm.exp 2006-04-22 12:48:39.000000000 -0700
@@ -0,1 +1,42 @@
+# Copyright (C) 1997, 2004, 2006 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# GCC testsuite that uses the `dg.exp' driver.
+
+# Exit immediately if this isn't an ARM target.
+if ![istarget arm*-*-*] then {
+ return
+}
+
+# Load support procs.
+load_lib gcc-dg.exp
+
+# If a testcase doesn't have special options, use these.
+global DEFAULT_CFLAGS
+if ![info exists DEFAULT_CFLAGS] then {
+ set DEFAULT_CFLAGS " -ansi -pedantic-errors"
+}
+
+# Initialize `dg'.
+dg-init
+
+# Main loop.
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cCS\]]] \
+ "" $DEFAULT_CFLAGS
+
+# All done.
+dg-finish
--- /dev/null 2006-03-11 08:41:44.866675760 -0800
+++ testsuite/gcc.target/arm/pr27387.c 2006-04-26 19:37:56.000000000 -0700
@@ -0,0 +1,26 @@
+/* PR target/27387
+ We used to generate a non-PIC thunk on thumb even with -fPIC.
+ Make sure that won't happen anymore. */
+
+/* { dg-do compile } */
+/* { dg-require-effective-target arm32 } */
+/* { dg-options "-mthumb -fPIC" } */
+
+struct A {
+ virtual void f ();
+};
+
+struct B {
+ virtual void g ();
+};
+
+struct C : public A, public B {
+ virtual void g();
+};
+
+void
+C::g()
+{
+}
+
+/* { dg-final { scan-assembler "LTHUNKPC" } } */