From feb16a5e5d35d4f632e1be10ce0ac4f4c3505d22 Mon Sep 17 00:00:00 2001
From: Duan bo <duanbo3@huawei.com>
Date: Wed, 15 Apr 2020 05:19:31 -0400
Subject: [PATCH] aarch64: Add an error message in large code model for ilp32
 [PR94577]

The option -mabi=ilp32 should not be used in large code model. An error
message is added for the option conflict.

2020-04-15  Duan bo  <duanbo3@huawei.com>

	PR target/94577
	* config/aarch64/aarch64.c: Add an error message for option conflict.
	* doc/invoke.texi (-mcmodel=large): Mention that -mcmodel=large is
	incompatible with -fpic, -fPIC and -mabi=ilp32.

2020-04-15  Duan bo  <duanbo3@huawei.com>

	PR target/94577
	* gcc.target/aarch64/pr94577.c: New test.
---
 gcc/ChangeLog                              |  7 ++++
 gcc/config/aarch64/aarch64.c               | 41 ++++++++++++----------
 gcc/doc/invoke.texi                        |  4 ++-
 gcc/testsuite/ChangeLog                    |  5 +++
 gcc/testsuite/gcc.target/aarch64/pr94577.c | 10 ++++++
 5 files changed, 47 insertions(+), 20 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/pr94577.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 3c6a45e8fe7..c2f1fcb7bff 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2020-04-15  Duan bo  <duanbo3@huawei.com>
+
+	PR target/94577
+	* config/aarch64/aarch64.c: Add an error message for option conflict.
+	* doc/invoke.texi (-mcmodel=large): Mention that -mcmodel=large is
+	incompatible with -fpic, -fPIC and -mabi=ilp32.
+
 2020-04-14  Max Filippov  <jcmvbkbc@gmail.com>
 
 	PR target/94584
diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c
index 4af562a81ea..f8a38bd899a 100644
--- a/gcc/config/aarch64/aarch64.c
+++ b/gcc/config/aarch64/aarch64.c
@@ -14707,32 +14707,35 @@ aarch64_init_expanders (void)
 static void
 initialize_aarch64_code_model (struct gcc_options *opts)
 {
-   if (opts->x_flag_pic)
+   aarch64_cmodel = opts->x_aarch64_cmodel_var;
+   switch (opts->x_aarch64_cmodel_var)
      {
-       switch (opts->x_aarch64_cmodel_var)
-	 {
-	 case AARCH64_CMODEL_TINY:
+       case AARCH64_CMODEL_TINY:
+	 if (opts->x_flag_pic)
 	   aarch64_cmodel = AARCH64_CMODEL_TINY_PIC;
-	   break;
-	 case AARCH64_CMODEL_SMALL:
+	 break;
+       case AARCH64_CMODEL_SMALL:
+	 if (opts->x_flag_pic)
+	   {
 #ifdef HAVE_AS_SMALL_PIC_RELOCS
-	   aarch64_cmodel = (flag_pic == 2
-			     ? AARCH64_CMODEL_SMALL_PIC
-			     : AARCH64_CMODEL_SMALL_SPIC);
+	     aarch64_cmodel = (flag_pic == 2
+			       ? AARCH64_CMODEL_SMALL_PIC
+			       : AARCH64_CMODEL_SMALL_SPIC);
 #else
-	   aarch64_cmodel = AARCH64_CMODEL_SMALL_PIC;
+	     aarch64_cmodel = AARCH64_CMODEL_SMALL_PIC;
 #endif
-	   break;
-	 case AARCH64_CMODEL_LARGE:
+	   }
+	 break;
+       case AARCH64_CMODEL_LARGE:
+	 if (opts->x_flag_pic)
 	   sorry ("code model %qs with %<-f%s%>", "large",
 		  opts->x_flag_pic > 1 ? "PIC" : "pic");
-	   break;
-	 default:
-	   gcc_unreachable ();
-	 }
-     }
-   else
-     aarch64_cmodel = opts->x_aarch64_cmodel_var;
+	 if (opts->x_aarch64_abi == AARCH64_ABI_ILP32)
+	   sorry ("code model large not supported in ilp32 mode");
+	 break;
+       default:
+	 gcc_unreachable ();
+    }
 }
 
 /* Implement TARGET_OPTION_SAVE.  */
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 3bb95ffd220..4108c9dee10 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -16744,7 +16744,9 @@ dynamically linked.  This is the default code model.
 @item -mcmodel=large
 @opindex mcmodel=large
 Generate code for the large code model.  This makes no assumptions about
-addresses and sizes of sections.  Programs can be statically linked only.
+addresses and sizes of sections.  Programs can be statically linked only.  The
+@option{-mcmodel=large} option is incompatible with @option{-mabi=ilp32},
+@option{-fpic} and @option{-fPIC}.
 
 @item -mstrict-align
 @itemx -mno-strict-align
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 69f9b93cd49..c80aefa1f73 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-04-15  Duan bo  <duanbo3@huawei.com>
+
+	PR target/94577
+	* gcc.target/aarch64/pr94577.c: New test.
+
 2020-04-13  Max Filippov  <jcmvbkbc@gmail.com>
 
 	PR target/94584
diff --git a/gcc/testsuite/gcc.target/aarch64/pr94577.c b/gcc/testsuite/gcc.target/aarch64/pr94577.c
new file mode 100644
index 00000000000..c7d3d7881fa
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr94577.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-mcmodel=large -mabi=ilp32" } */
+
+void
+foo ()
+{
+  // Do nothing
+}
+
+/* { dg-message "sorry, unimplemented: code model large not supported in ilp32 mode"  "" { target *-*-* } 0 } */
-- 
2.19.1

