RISC-V: Fix weak symbols with medany and explicit relocs.
authorJim Wilson <jimw@sifive.com>
Tue, 25 Sep 2018 22:52:38 +0000 (22:52 +0000)
committerJim Wilson <wilson@gcc.gnu.org>
Tue, 25 Sep 2018 22:52:38 +0000 (15:52 -0700)
gcc/
* config/riscv/riscv.c (riscv_split_symbol): Mark auipc label as weak
when target symbol is weak.

gcc/testsuite/
* gcc.target/riscv/weak-1.c: New.

From-SVN: r264586

gcc/ChangeLog
gcc/config/riscv/riscv.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/riscv/weak-1.c [new file with mode: 0644]

index 43857153e9f368d218016c59ac0cd5a5396a3382..d43e6d682ccef6e30ece8a83388791230882a771 100644 (file)
@@ -1,3 +1,8 @@
+2018-09-25  Jim Wilson  <jimw@sifive.com>
+
+       * config/riscv/riscv.c (riscv_split_symbol): Mark auipc label as weak
+       when target symbol is weak.
+
 2018-09-25  Bernd Edlinger  <bernd.edlinger@hotmail.de>
 
        PR c/87387
index 9d6d981a42aab86912dbfde787cf95404a2da39e..381a203da9be390d25ef8738f556903d05251caa 100644 (file)
@@ -1103,6 +1103,11 @@ riscv_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
 
          label = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
          SYMBOL_REF_FLAGS (label) |= SYMBOL_FLAG_LOCAL;
+         /* ??? Ugly hack to make weak symbols work.  May need to change the
+            RTL for the auipc and/or low patterns to get a better fix for
+            this.  */
+         if (! nonzero_address_p (addr))
+           SYMBOL_REF_WEAK (label) = 1;
 
          if (temp == NULL)
            temp = gen_reg_rtx (Pmode);
index 7b26b894b1551b77724147ac4ef3f6d701a58165..f690e62a4456f9fe8577e78b6f2e04a95aa684c6 100644 (file)
@@ -1,3 +1,7 @@
+2018-09-25  Jim Wilson  <jimw@sifive.com>
+
+       * gcc.target/riscv/weak-1.c: New.
+
 2018-09-25  Jeff Law  <law@redhat.com>
 
        * gcc.dg/warn-stpcpy-no-nul.c: Drop unnecessary xfails.
diff --git a/gcc/testsuite/gcc.target/riscv/weak-1.c b/gcc/testsuite/gcc.target/riscv/weak-1.c
new file mode 100644 (file)
index 0000000..0f20501
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-mcmodel=medany -mexplicit-relocs -O" } */
+
+/* Verify that the branch doesn't get optimized away.  */
+extern int weak_func(void) __attribute__ ((weak));
+
+int
+sub (void)
+{
+  if (weak_func)
+    return weak_func ();
+  return 0;
+}
+/* { dg-final { scan-assembler "b\(ne|eq\)" } } */