fab8f6
commit 5315d05c7295fbb9345d85d6bf7cbe7c975a19c8
fab8f6
Author: hjl <hjl@138bc75d-0d04-0410-961f-82ee72b054a4>
fab8f6
Date:   Tue Jan 16 11:22:01 2018 +0000
fab8f6
fab8f6
    HJ patch #5
fab8f6
fab8f6
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
fab8f6
index e73389b..15cfe83 100644
fab8f6
--- a/gcc/config/i386/i386.c
fab8f6
+++ b/gcc/config/i386/i386.c
fab8f6
@@ -4634,6 +4634,19 @@ ix86_set_indirect_branch_type (tree fndecl)
fab8f6
 	}
fab8f6
       else
fab8f6
 	cfun->machine->indirect_branch_type = ix86_indirect_branch;
fab8f6
+
fab8f6
+      /* -mcmodel=large is not compatible with -mindirect-branch=thunk
fab8f6
+	 nor -mindirect-branch=thunk-extern.  */
fab8f6
+      if ((ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
fab8f6
+	  && ((cfun->machine->indirect_branch_type
fab8f6
+	       == indirect_branch_thunk_extern)
fab8f6
+	      || (cfun->machine->indirect_branch_type
fab8f6
+		  == indirect_branch_thunk)))
fab8f6
+	error ("%<-mindirect-branch=%s%> and %<-mcmodel=large%> are not "
fab8f6
+	       "compatible",
fab8f6
+	       ((cfun->machine->indirect_branch_type
fab8f6
+		 == indirect_branch_thunk_extern)
fab8f6
+		? "thunk-extern" : "thunk"));
fab8f6
     }
fab8f6
 
fab8f6
   if (cfun->machine->function_return_type == indirect_branch_unset)
fab8f6
@@ -4659,6 +4672,19 @@ ix86_set_indirect_branch_type (tree fndecl)
fab8f6
 	}
fab8f6
       else
fab8f6
 	cfun->machine->function_return_type = ix86_function_return;
fab8f6
+
fab8f6
+      /* -mcmodel=large is not compatible with -mfunction-return=thunk
fab8f6
+	 nor -mfunction-return=thunk-extern.  */
fab8f6
+      if ((ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
fab8f6
+	  && ((cfun->machine->function_return_type
fab8f6
+	       == indirect_branch_thunk_extern)
fab8f6
+	      || (cfun->machine->function_return_type
fab8f6
+		  == indirect_branch_thunk)))
fab8f6
+	error ("%<-mfunction-return=%s%> and %<-mcmodel=large%> are not "
fab8f6
+	       "compatible",
fab8f6
+	       ((cfun->machine->function_return_type
fab8f6
+		 == indirect_branch_thunk_extern)
fab8f6
+		? "thunk-extern" : "thunk"));
fab8f6
     }
fab8f6
 }
fab8f6
 
fab8f6
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
fab8f6
index 4a365c7..7b33803 100644
fab8f6
--- a/gcc/doc/invoke.texi
fab8f6
+++ b/gcc/doc/invoke.texi
fab8f6
@@ -14659,6 +14659,11 @@ to external call and return thunk provided in a separate object file.
fab8f6
 You can control this behavior for a specific function by using the
fab8f6
 function attribute @code{indirect_branch}.  @xref{Function Attributes}.
fab8f6
 
fab8f6
+Note that @option{-mcmodel=large} is incompatible with
fab8f6
+@option{-mindirect-branch=thunk} nor
fab8f6
+@option{-mindirect-branch=thunk-extern} since the thunk function may
fab8f6
+not be reachable in large code model.
fab8f6
+
fab8f6
 @item -mfunction-return=@var{choice}
fab8f6
 @opindex -mfunction-return
fab8f6
 Convert function return with @var{choice}.  The default is @samp{keep},
fab8f6
@@ -14670,6 +14675,11 @@ object file.  You can control this behavior for a specific function by
fab8f6
 using the function attribute @code{function_return}.
fab8f6
 @xref{Function Attributes}.
fab8f6
 
fab8f6
+Note that @option{-mcmodel=large} is incompatible with
fab8f6
+@option{-mfunction-return=thunk} nor
fab8f6
+@option{-mfunction-return=thunk-extern} since the thunk function may
fab8f6
+not be reachable in large code model.
fab8f6
+
fab8f6
 @item -mindirect-branch-register
fab8f6
 @opindex -mindirect-branch-register
fab8f6
 Force indirect call and jump via register.
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-10.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-10.c
fab8f6
new file mode 100644
fab8f6
index 0000000..a0674bd
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-10.c
fab8f6
@@ -0,0 +1,7 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-inline -mfunction-return=keep -mcmodel=large" } */
fab8f6
+
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-8.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-8.c
fab8f6
new file mode 100644
fab8f6
index 0000000..7a80a89
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-8.c
fab8f6
@@ -0,0 +1,7 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk -mfunction-return=keep -mcmodel=large" } */
fab8f6
+
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mindirect-branch=thunk' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-9.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-9.c
fab8f6
new file mode 100644
fab8f6
index 0000000..d4d45c5
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-9.c
fab8f6
@@ -0,0 +1,7 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -mfunction-return=keep -mcmodel=large" } */
fab8f6
+
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mindirect-branch=thunk-extern' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-10.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-10.c
fab8f6
new file mode 100644
fab8f6
index 0000000..3a2aead
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-10.c
fab8f6
@@ -0,0 +1,9 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=keep -mfunction-return=keep -mcmodel=large" } */
fab8f6
+/* { dg-additional-options "-fPIC" { target fpic } } */
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("thunk-extern")))
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mindirect-branch=thunk-extern' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-11.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-11.c
fab8f6
new file mode 100644
fab8f6
index 0000000..8e52f03
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-11.c
fab8f6
@@ -0,0 +1,9 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=keep -mfunction-return=keep -mcmodel=large" } */
fab8f6
+/* { dg-additional-options "-fPIC" { target fpic } } */
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("thunk-inline")))
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-9.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-9.c
fab8f6
new file mode 100644
fab8f6
index 0000000..bdaa4f6
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-9.c
fab8f6
@@ -0,0 +1,9 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=keep -mfunction-return=keep -mcmodel=large" } */
fab8f6
+/* { dg-additional-options "-fPIC" { target fpic } } */
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("thunk")))
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mindirect-branch=thunk' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-17.c b/gcc/testsuite/gcc.target/i386/ret-thunk-17.c
fab8f6
new file mode 100644
fab8f6
index 0000000..0605e2c
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-17.c
fab8f6
@@ -0,0 +1,7 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mfunction-return=thunk -mindirect-branch=keep -mcmodel=large" } */
fab8f6
+
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mfunction-return=thunk' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-18.c b/gcc/testsuite/gcc.target/i386/ret-thunk-18.c
fab8f6
new file mode 100644
fab8f6
index 0000000..307019d
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-18.c
fab8f6
@@ -0,0 +1,8 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mfunction-return=thunk-extern -mindirect-branch=keep -mcmodel=large" } */
fab8f6
+/* { dg-additional-options "-fPIC" { target fpic } } */
fab8f6
+
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mfunction-return=thunk-extern' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-19.c b/gcc/testsuite/gcc.target/i386/ret-thunk-19.c
fab8f6
new file mode 100644
fab8f6
index 0000000..772617f
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-19.c
fab8f6
@@ -0,0 +1,8 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mfunction-return=keep -mindirect-branch=keep -mcmodel=large" } */
fab8f6
+
fab8f6
+__attribute__ ((function_return("thunk")))
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mfunction-return=thunk' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-20.c b/gcc/testsuite/gcc.target/i386/ret-thunk-20.c
fab8f6
new file mode 100644
fab8f6
index 0000000..1e9f9bd
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-20.c
fab8f6
@@ -0,0 +1,9 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mfunction-return=keep -mindirect-branch=keep -mcmodel=large" } */
fab8f6
+/* { dg-additional-options "-fPIC" { target fpic } } */
fab8f6
+
fab8f6
+__attribute__ ((function_return("thunk-extern")))
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{ /* { dg-error "'-mfunction-return=thunk-extern' and '-mcmodel=large' are not compatible" } */
fab8f6
+}
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-21.c b/gcc/testsuite/gcc.target/i386/ret-thunk-21.c
fab8f6
new file mode 100644
fab8f6
index 0000000..eea07f7
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-21.c
fab8f6
@@ -0,0 +1,9 @@
fab8f6
+/* { dg-do compile { target { lp64 } } } */
fab8f6
+/* { dg-options "-O2 -mfunction-return=keep -mindirect-branch=keep -mcmodel=large" } */
fab8f6
+/* { dg-additional-options "-fPIC" { target fpic } } */
fab8f6
+
fab8f6
+__attribute__ ((function_return("thunk-inline")))
fab8f6
+void
fab8f6
+bar (void)
fab8f6
+{
fab8f6
+}