Patchwork [MIPS] diagnose -fpic/-fpie incompatibility with -mno-abicalls

login
register
mail settings
Submitter Sandra Loosemore
Date Aug. 3, 2012, 5:41 p.m.
Message ID <501C0D4A.1070906@codesourcery.com>
Download mbox | patch
Permalink /patch/175031/
State New
Headers show

Comments

Sandra Loosemore - Aug. 3, 2012, 5:41 p.m.
For all supported MIPS ABIs other than EABI, compiling with 
-fpic/-fPIC/-fpie/-fPIE implicitly requires abicalls support.  On Linux 
targets, -mabicalls is the default, but bare-metal targets like 
mips-sde-elf default to -mno-abicalls and naively compiling with -fPIC 
alone doesn't result in working code.  (In fact, even "-fPIC -mabicalls" 
doesn't work, because -mabicalls then conflicts with the default for -G, 
and the linker rejects mixing -mabicalls code with -mno-abicalls 
libraries.)

This patch adds a diagnostic to catch the bad option combination, and 
fixes a couple test cases that triggered the new error.  OK for mainline?

-Sandra


2012-08-03  Sandra Loosemore  <sandra@codesourcery.com>

	gcc/
	* config/mips/mips.c (mips_option_override): Check -fpic/-fpie
	and -mabicalls options for compatibility with ABI.

	gcc/testsuite/
	* g++.dg/opt/enum2.C: Require fpic target.
	* g++.dg/lto/20090303_0.C: Likewise.
Richard Sandiford - Aug. 4, 2012, 2:09 p.m.
Sandra Loosemore <sandra@codesourcery.com> writes:
> For all supported MIPS ABIs other than EABI, compiling with 
> -fpic/-fPIC/-fpie/-fPIE implicitly requires abicalls support.  On Linux 
> targets, -mabicalls is the default, but bare-metal targets like 
> mips-sde-elf default to -mno-abicalls and naively compiling with -fPIC 
> alone doesn't result in working code.  (In fact, even "-fPIC -mabicalls" 
> doesn't work, because -mabicalls then conflicts with the default for -G, 
> and the linker rejects mixing -mabicalls code with -mno-abicalls 
> libraries.)

Yeah.  I was tempted to add a message for this a while back, but I think
there was resistance from someone who was managing to generate a form of
PIC with careful use of that combination.  If they did, it was by accident
rather than design.

However, EABI doesn't support PIC at all.  (There used to be an
-membedded-pic option, but that's long gone.)  So I think this:

> Index: gcc/config/mips/mips.c
> ===================================================================
> --- gcc/config/mips/mips.c	(revision 190052)
> +++ gcc/config/mips/mips.c	(working copy)
> @@ -15872,6 +15872,8 @@ static void
>  mips_option_override (void)
>  {
>    int i, start, regno, mode;
> +  static const char * const mips_abi_name[] =
> +    {"-mabi=32", "-mabi=n32", "-mabi=64", "-mabi=eabi", "-mabi=o64"};
>  
>    if (global_options_set.x_mips_isa_option)
>      mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
> @@ -16053,6 +16055,12 @@ mips_option_override (void)
>        target_flags &= ~MASK_ABICALLS;
>      }
>  
> +  /* On non-EABI targets, -fpic and -fpie require -mabicalls.  */
> +  if (mips_abi != ABI_EABI && (flag_pic || flag_pie) && !TARGET_ABICALLS)
> +    error ("the combination of %qs and %qs is incompatible with %qs",
> +	   (flag_pic ? "-fpic" : "-fpie"),
> +	   "-mno-abicalls", mips_abi_name[mips_abi]);

should be:

  if (flag_pic)
    {
      if (mips_abi == ABI_EABI)
        error ("cannot generate position-independent code for %qs",
               "-mabi=eabi");
      else if (!TARGET_ABICALLS)
        error ("position-independent code requires %qs", "-mabicalls");
    }

(where flag_pie implies flag_pic).  I've removed the -fpic/-fpie thing
to avoid having to decide between printing "-fpic" and "-fPIC"
(or "-fpie" and "-fPIE").

OK with that change, if you agree.

Richard

Patch

Index: gcc/config/mips/mips.c
===================================================================
--- gcc/config/mips/mips.c	(revision 190052)
+++ gcc/config/mips/mips.c	(working copy)
@@ -15872,6 +15872,8 @@  static void
 mips_option_override (void)
 {
   int i, start, regno, mode;
+  static const char * const mips_abi_name[] =
+    {"-mabi=32", "-mabi=n32", "-mabi=64", "-mabi=eabi", "-mabi=o64"};
 
   if (global_options_set.x_mips_isa_option)
     mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
@@ -16053,6 +16055,12 @@  mips_option_override (void)
       target_flags &= ~MASK_ABICALLS;
     }
 
+  /* On non-EABI targets, -fpic and -fpie require -mabicalls.  */
+  if (mips_abi != ABI_EABI && (flag_pic || flag_pie) && !TARGET_ABICALLS)
+    error ("the combination of %qs and %qs is incompatible with %qs",
+	   (flag_pic ? "-fpic" : "-fpie"),
+	   "-mno-abicalls", mips_abi_name[mips_abi]);
+
   if (TARGET_ABICALLS_PIC2)
     /* We need to set flag_pic for executables as well as DSOs
        because we may reference symbols that are not defined in
Index: gcc/testsuite/g++.dg/opt/enum2.C
===================================================================
--- gcc/testsuite/g++.dg/opt/enum2.C	(revision 190052)
+++ gcc/testsuite/g++.dg/opt/enum2.C	(working copy)
@@ -1,8 +1,8 @@ 
 // PR c++/43680
 // Test that we don't make excessively aggressive assumptions about what
 // values an enum variable can have.
+// { dg-do run { target fpic } }
 // { dg-options "-O2 -fPIC" }
-// { dg-do run }
 
 extern "C" void abort ();
 
Index: gcc/testsuite/g++.dg/lto/20090303_0.C
===================================================================
--- gcc/testsuite/g++.dg/lto/20090303_0.C	(revision 190052)
+++ gcc/testsuite/g++.dg/lto/20090303_0.C	(working copy)
@@ -1,4 +1,5 @@ 
 /* { dg-lto-do run } */
+/* { dg-require-effective-target fpic } */
 /* { dg-lto-options {{-flto -flto-partition=1to1 -fPIC}} } */
 /* { dg-lto-options {{-flto -flto-partition=1to1}} { target sparc*-*-* } } */
 /* { dg-suppress-ld-options {-fPIC} }  */