mips: Add appropriate linker flags when compiling with -static-pie

Message ID CANP1oa0QMfUCRwGpP46Hz3xz9CsHEkHdMJXJ5sv+92-boR3u5Q@mail.gmail.com
State Accepted, archived
Headers
Series mips: Add appropriate linker flags when compiling with -static-pie |

Checks

Context Check Description
snail/gcc-patches-check success Github commit url

Commit Message

linted Sept. 14, 2022, 6:09 p.m. UTC
  Hello,

This patch fixes missing flags when compiling with -static-pie on mips. I
made these modifications based on the previously submitted static pie patch
for arm as well as the working code for aarch64.

I tested with a host of mips-elf and checked with mips-sim. This patch was
also tested and used with uclibc-ng to generate static pie elfs.

This is my first patch for gcc, so please let me know if there is anything
I missed.



Signed-off-by: linted <linted@users.noreply.github.com>
---
 gcc/config/mips/gnu-user.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

linted Sept. 25, 2022, 3:49 p.m. UTC | #1
Hello,
I'm just checking to see if anyone has had a chance to look at this.

Thank you

On Wed, Sep 14, 2022 at 2:09 PM linted <linted90@gmail.com> wrote:

> Hello,
>
> This patch fixes missing flags when compiling with -static-pie on mips. I
> made these modifications based on the previously submitted static pie patch
> for arm as well as the working code for aarch64.
>
> I tested with a host of mips-elf and checked with mips-sim. This patch was
> also tested and used with uclibc-ng to generate static pie elfs.
>
> This is my first patch for gcc, so please let me know if there is anything
> I missed.
>
>
>
> Signed-off-by: linted <linted@users.noreply.github.com>
> ---
>  gcc/config/mips/gnu-user.h | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h
> index 6aad7192e69..b1c665b7f37 100644
> --- a/gcc/config/mips/gnu-user.h
> +++ b/gcc/config/mips/gnu-user.h
> @@ -56,11 +56,12 @@ along with GCC; see the file COPYING3.  If not see
>  #define GNU_USER_TARGET_LINK_SPEC "\
>    %{G*} %{EB} %{EL} %{mips*} %{shared} \
>    %{!shared: \
> -    %{!static: \
> +    %{!static:%{!static-pie: \
>        %{rdynamic:-export-dynamic} \
>        %{mabi=n32: -dynamic-linker " GNU_USER_DYNAMIC_LINKERN32 "} \
>        %{mabi=64: -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
> -      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}} \
> +      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \
> +    %{static-pie:-Bstatic -pie --no-dynamic-linker -z text} \
>      %{static}} \
>    %{mabi=n32:-m" GNU_USER_LINK_EMULATIONN32 "} \
>    %{mabi=64:-m" GNU_USER_LINK_EMULATION64 "} \
> --
> 2.34.1
>
>
>
  
Jeff Law Oct. 12, 2022, 6:17 p.m. UTC | #2
On 9/25/22 09:49, linted via Gcc-patches wrote:
> Hello,
> I'm just checking to see if anyone has had a chance to look at this.
>
> Thank you
>
> On Wed, Sep 14, 2022 at 2:09 PM linted <linted90@gmail.com> wrote:
>
>> Hello,
>>
>> This patch fixes missing flags when compiling with -static-pie on mips. I
>> made these modifications based on the previously submitted static pie patch
>> for arm as well as the working code for aarch64.
>>
>> I tested with a host of mips-elf and checked with mips-sim. This patch was
>> also tested and used with uclibc-ng to generate static pie elfs.
>>
>> This is my first patch for gcc, so please let me know if there is anything
>> I missed.
>>
>>
>>
>> Signed-off-by: linted <linted@users.noreply.github.com>
>> ---
>>   gcc/config/mips/gnu-user.h | 5 +++--
>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h
>> index 6aad7192e69..b1c665b7f37 100644
>> --- a/gcc/config/mips/gnu-user.h
>> +++ b/gcc/config/mips/gnu-user.h
>> @@ -56,11 +56,12 @@ along with GCC; see the file COPYING3.  If not see
>>   #define GNU_USER_TARGET_LINK_SPEC "\
>>     %{G*} %{EB} %{EL} %{mips*} %{shared} \
>>     %{!shared: \
>> -    %{!static: \
>> +    %{!static:%{!static-pie: \
>>         %{rdynamic:-export-dynamic} \
>>         %{mabi=n32: -dynamic-linker " GNU_USER_DYNAMIC_LINKERN32 "} \
>>         %{mabi=64: -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
>> -      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}} \
>> +      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \
>> +    %{static-pie:-Bstatic -pie --no-dynamic-linker -z text} \
>>       %{static}} \

This is a bit out of my usual areas of expertise.  But what I find odd 
here is that for -static we essentially do nothing, but for -static-pie 
we need "-Bstatic -pie --no-dynamic-linker -z text".    Is the -Bstatic 
really needed for static-pie  And if it is, then wouldn't it be needed 
for -static as well?    If you look carefully at aarch64, you'll see it 
includes -Bstatic for -static.


Jeff
  
linted Oct. 20, 2022, 5:29 p.m. UTC | #3
On Wed, Oct 12, 2022 at 2:18 PM Jeff Law via Gcc-patches <
gcc-patches@gcc.gnu.org> wrote:

>
> On 9/25/22 09:49, linted via Gcc-patches wrote:
> > Hello,
> > I'm just checking to see if anyone has had a chance to look at this.
> >
> > Thank you
> >
> > On Wed, Sep 14, 2022 at 2:09 PM linted <linted90@gmail.com> wrote:
> >
> >> Hello,
> >>
> >> This patch fixes missing flags when compiling with -static-pie on mips.
> I
> >> made these modifications based on the previously submitted static pie
> patch
> >> for arm as well as the working code for aarch64.
> >>
> >> I tested with a host of mips-elf and checked with mips-sim. This patch
> was
> >> also tested and used with uclibc-ng to generate static pie elfs.
> >>
> >> This is my first patch for gcc, so please let me know if there is
> anything
> >> I missed.
> >>
> >>
> >>
> >> Signed-off-by: linted <linted@users.noreply.github.com>
> >> ---
> >>   gcc/config/mips/gnu-user.h | 5 +++--
> >>   1 file changed, 3 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h
> >> index 6aad7192e69..b1c665b7f37 100644
> >> --- a/gcc/config/mips/gnu-user.h
> >> +++ b/gcc/config/mips/gnu-user.h
> >> @@ -56,11 +56,12 @@ along with GCC; see the file COPYING3.  If not see
> >>   #define GNU_USER_TARGET_LINK_SPEC "\
> >>     %{G*} %{EB} %{EL} %{mips*} %{shared} \
> >>     %{!shared: \
> >> -    %{!static: \
> >> +    %{!static:%{!static-pie: \
> >>         %{rdynamic:-export-dynamic} \
> >>         %{mabi=n32: -dynamic-linker " GNU_USER_DYNAMIC_LINKERN32 "} \
> >>         %{mabi=64: -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
> >> -      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}} \
> >> +      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \
> >> +    %{static-pie:-Bstatic -pie --no-dynamic-linker -z text} \
> >>       %{static}} \
>
> This is a bit out of my usual areas of expertise.  But what I find odd
> here is that for -static we essentially do nothing, but for -static-pie
> we need "-Bstatic -pie --no-dynamic-linker -z text".    Is the -Bstatic
> really needed for static-pie  And if it is, then wouldn't it be needed
> for -static as well?    If you look carefully at aarch64, you'll see it
> includes -Bstatic for -static.
>
>
> Jeff
>

This is a really good question. From what I can tell, the linker will treat
-Bstatic and -static identically. So when compiling with -static, adding a
-Bstatic is redundant since the -static will be passed on to the linker.

In the case of -static-pie, we need to explicitly specify -Bstatic so that
the linker will know that we want the binary to be statically linked since
there is no -static being passed on.

I am of the opinion that we should usually be explicit, but in this case, I
think it is more prudent to change less code.
  

Patch

diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h
index 6aad7192e69..b1c665b7f37 100644
--- a/gcc/config/mips/gnu-user.h
+++ b/gcc/config/mips/gnu-user.h
@@ -56,11 +56,12 @@  along with GCC; see the file COPYING3.  If not see
 #define GNU_USER_TARGET_LINK_SPEC "\
   %{G*} %{EB} %{EL} %{mips*} %{shared} \
   %{!shared: \
-    %{!static: \
+    %{!static:%{!static-pie: \
       %{rdynamic:-export-dynamic} \
       %{mabi=n32: -dynamic-linker " GNU_USER_DYNAMIC_LINKERN32 "} \
       %{mabi=64: -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
-      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}} \
+      %{mabi=32: -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \
+    %{static-pie:-Bstatic -pie --no-dynamic-linker -z text} \
     %{static}} \
   %{mabi=n32:-m" GNU_USER_LINK_EMULATIONN32 "} \
   %{mabi=64:-m" GNU_USER_LINK_EMULATION64 "} \