ld: Add -static-pie tests

Message ID 20190410191132.9138-1-hjl.tools@gmail.com
State New
Headers show
Series
  • ld: Add -static-pie tests
Related show

Commit Message

H.J. Lu April 10, 2019, 7:11 p.m.
Add -static-pie tests for DT_INIT_ARRAY, DT_FINI_ARRAY, DT_PREINIT_ARRAY
and IFUNC.

	* testsuite/config/default.exp (STATIC_PIE_LDFLAGS): New.  Set
	to "-static-pie" if target compiler supports it.
	* testsuite/ld-elf/elf.exp: Run -static-pie tests if
	$STATIC_PIE_LDFLAGS isn't empty.
	* testsuite/ld-ifunc/ifunc.exp: Likewise.
---
 ld/testsuite/config/default.exp | 37 ++++++++++++++++++++++++
 ld/testsuite/ld-elf/elf.exp     | 50 +++++++++++++++++++++++++++++++++
 ld/testsuite/ld-ifunc/ifunc.exp | 16 ++++++++++-
 3 files changed, 102 insertions(+), 1 deletion(-)

-- 
2.20.1

Comments

H.J. Lu April 11, 2019, 1:47 p.m. | #1
On Wed, Apr 10, 2019 at 12:11 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>

> Add -static-pie tests for DT_INIT_ARRAY, DT_FINI_ARRAY, DT_PREINIT_ARRAY

> and IFUNC.

>

>         * testsuite/config/default.exp (STATIC_PIE_LDFLAGS): New.  Set

>         to "-static-pie" if target compiler supports it.

>         * testsuite/ld-elf/elf.exp: Run -static-pie tests if

>         $STATIC_PIE_LDFLAGS isn't empty.

>         * testsuite/ld-ifunc/ifunc.exp: Likewise.

> ---

>  ld/testsuite/config/default.exp | 37 ++++++++++++++++++++++++

>  ld/testsuite/ld-elf/elf.exp     | 50 +++++++++++++++++++++++++++++++++

>  ld/testsuite/ld-ifunc/ifunc.exp | 16 ++++++++++-

>  3 files changed, 102 insertions(+), 1 deletion(-)

>

> diff --git a/ld/testsuite/config/default.exp b/ld/testsuite/config/default.exp

> index 7bc869271e..5acab06329 100644

> --- a/ld/testsuite/config/default.exp

> +++ b/ld/testsuite/config/default.exp

> @@ -454,3 +454,40 @@ if { ![info exists STATIC_LDFLAGS] } then {

>         set STATIC_LDFLAGS ""

>      }

>  }

> +

> +# Set STATIC_PIE_LDFLAGS to "-static-pie" if target compiler supports it.

> +

> +if { ![info exists STATIC_PIE_LDFLAGS] } then {

> +    if { [which $CC] != 0 } {

> +       # Check if gcc supports -static-pie.

> +       set flags ""

> +       if [board_info [target_info name] exists cflags] {

> +           append flags " [board_info [target_info name] cflags]"

> +       }

> +       if [board_info [target_info name] exists ldflags] {

> +           append flags " [board_info [target_info name] ldflags]"

> +       }

> +

> +       set basename "tmpdir/static[pid]"

> +       set src ${basename}.c

> +       set output ${basename}

> +       set f [open $src "w"]

> +       puts $f "int main (void) { return 0; }"

> +       close $f

> +       if [is_remote host] {

> +           set src [remote_download host $src]

> +       }

> +       set static_available [run_host_cmd_yesno "$CC" "-static-pie $flags $src -o $output"]

> +       remote_file host delete $src

> +       remote_file host delete $output

> +       file delete $src

> +

> +       if { $static_available == 1 } then {

> +           set STATIC_PIE_LDFLAGS "-static-pie"

> +       } else {

> +           set STATIC_PIE_LDFLAGS ""

> +       }

> +    } else {

> +       set STATIC_PIE_LDFLAGS ""

> +    }

> +}

> diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp

> index 4a5ef8a219..4686870916 100644

> --- a/ld/testsuite/ld-elf/elf.exp

> +++ b/ld/testsuite/ld-elf/elf.exp

> @@ -235,6 +235,56 @@ if { [istarget *-*-linux*]

>       || [istarget *-*-gnu*] } {

>      run_ld_link_exec_tests $array_tests_pie $xfails

>

> +    if { $STATIC_PIE_LDFLAGS != "" } then {

> +       run_ld_link_exec_tests [list \

> +           [list \

> +               "Static PIE preinit array" \

> +               "$STATIC_PIE_LDFLAGS" \

> +               "" \

> +               {preinit.c} \

> +               "preinit-static-pie" \

> +               "preinit.out" \

> +               "-fPIE" \

> +           ] \

> +           [list \

> +               "Static PIE init array" \

> +               "$STATIC_PIE_LDFLAGS" \

> +               "" \

> +               {init.c} \

> +               "init-static-pie" \

> +               "init.out" \

> +               "-fPIE" \

> +           ] \

> +           [list \

> +               "Static PIE fini array" \

> +               "$STATIC_PIE_LDFLAGS" \

> +               "" \

> +               {fini.c} \

> +               "fini-static-pie" \

> +               "fini.out" \

> +               "-fPIE" \

> +           ] \

> +           [list \

> +               "Static PIE init array mixed" \

> +               "$STATIC_PIE_LDFLAGS" \

> +               "" \

> +               {init-mixed.c} \

> +               "init-mixed-static-pie" \

> +               "init-mixed.out" \

> +               "-I.  -fPIE" \

> +           ] \

> +           [list \

> +               "Static PIE PR ld/14525" \

> +               "$STATIC_PIE_LDFLAGS" \

> +               "" \

> +               {pr14525.c} \

> +               "pr14525-static-pie" \

> +               "pr14525.out" \

> +               "-fPIE" \

> +           ] \

> +       ]

> +    }

> +

>      run_ld_link_exec_tests [list \

>         [list \

>             "Run mbind2a" \

> diff --git a/ld/testsuite/ld-ifunc/ifunc.exp b/ld/testsuite/ld-ifunc/ifunc.exp

> index 6725d493e2..3671fe65e1 100644

> --- a/ld/testsuite/ld-ifunc/ifunc.exp

> +++ b/ld/testsuite/ld-ifunc/ifunc.exp

> @@ -777,6 +777,20 @@ run_ld_link_exec_tests [list \

>         "pass.out" \

>         "-fPIE -O2 -g" \

>      ] \

> -]}

> +]

> +if { $STATIC_PIE_LDFLAGS != "" } then {

> +    run_ld_link_exec_tests [list \

> +       [list \

> +           "Run pr23169g" \

> +           "$STATIC_PIE_LDFLAGS" \

> +           "" \

> +           { pr23169a.c pr23169b.c pr23169c.c } \

> +           "pr23169g" \

> +           "pass.out" \

> +           "-fPIE -O2 -g" \

> +       ] \

> +]

> +}

> +}

>

>  set ASFLAGS "$saved_ASFLAGS"

> --

> 2.20.1

>


I am checking it in.

-- 
H.J.

Patch

diff --git a/ld/testsuite/config/default.exp b/ld/testsuite/config/default.exp
index 7bc869271e..5acab06329 100644
--- a/ld/testsuite/config/default.exp
+++ b/ld/testsuite/config/default.exp
@@ -454,3 +454,40 @@  if { ![info exists STATIC_LDFLAGS] } then {
 	set STATIC_LDFLAGS ""
     }
 }
+
+# Set STATIC_PIE_LDFLAGS to "-static-pie" if target compiler supports it.
+
+if { ![info exists STATIC_PIE_LDFLAGS] } then {
+    if { [which $CC] != 0 } {
+	# Check if gcc supports -static-pie.
+	set flags ""
+	if [board_info [target_info name] exists cflags] {
+	    append flags " [board_info [target_info name] cflags]"
+	}
+	if [board_info [target_info name] exists ldflags] {
+	    append flags " [board_info [target_info name] ldflags]"
+	}
+
+	set basename "tmpdir/static[pid]"
+	set src ${basename}.c
+	set output ${basename}
+	set f [open $src "w"]
+	puts $f "int main (void) { return 0; }"
+	close $f
+	if [is_remote host] {
+	    set src [remote_download host $src]
+	}
+	set static_available [run_host_cmd_yesno "$CC" "-static-pie $flags $src -o $output"]
+	remote_file host delete $src
+	remote_file host delete $output
+	file delete $src
+
+	if { $static_available == 1 } then {
+	    set STATIC_PIE_LDFLAGS "-static-pie"
+	} else {
+	    set STATIC_PIE_LDFLAGS ""
+	}
+    } else {
+	set STATIC_PIE_LDFLAGS ""
+    }
+}
diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp
index 4a5ef8a219..4686870916 100644
--- a/ld/testsuite/ld-elf/elf.exp
+++ b/ld/testsuite/ld-elf/elf.exp
@@ -235,6 +235,56 @@  if { [istarget *-*-linux*]
      || [istarget *-*-gnu*] } {
     run_ld_link_exec_tests $array_tests_pie $xfails
 
+    if { $STATIC_PIE_LDFLAGS != "" } then {
+	run_ld_link_exec_tests [list \
+	    [list \
+		"Static PIE preinit array" \
+		"$STATIC_PIE_LDFLAGS" \
+		"" \
+		{preinit.c} \
+		"preinit-static-pie" \
+		"preinit.out" \
+		"-fPIE" \
+	    ] \
+	    [list \
+		"Static PIE init array" \
+		"$STATIC_PIE_LDFLAGS" \
+		"" \
+		{init.c} \
+		"init-static-pie" \
+		"init.out" \
+		"-fPIE" \
+	    ] \
+	    [list \
+		"Static PIE fini array" \
+		"$STATIC_PIE_LDFLAGS" \
+		"" \
+		{fini.c} \
+		"fini-static-pie" \
+		"fini.out" \
+		"-fPIE" \
+	    ] \
+	    [list \
+		"Static PIE init array mixed" \
+		"$STATIC_PIE_LDFLAGS" \
+		"" \
+		{init-mixed.c} \
+		"init-mixed-static-pie" \
+		"init-mixed.out" \
+		"-I.  -fPIE" \
+	    ] \
+	    [list \
+		"Static PIE PR ld/14525" \
+		"$STATIC_PIE_LDFLAGS" \
+		"" \
+		{pr14525.c} \
+		"pr14525-static-pie" \
+		"pr14525.out" \
+		"-fPIE" \
+	    ] \
+	]
+    }
+
     run_ld_link_exec_tests [list \
 	[list \
 	    "Run mbind2a" \
diff --git a/ld/testsuite/ld-ifunc/ifunc.exp b/ld/testsuite/ld-ifunc/ifunc.exp
index 6725d493e2..3671fe65e1 100644
--- a/ld/testsuite/ld-ifunc/ifunc.exp
+++ b/ld/testsuite/ld-ifunc/ifunc.exp
@@ -777,6 +777,20 @@  run_ld_link_exec_tests [list \
 	"pass.out" \
 	"-fPIE -O2 -g" \
     ] \
-]}
+] 
+if { $STATIC_PIE_LDFLAGS != "" } then {
+    run_ld_link_exec_tests [list \
+	[list \
+	    "Run pr23169g" \
+	    "$STATIC_PIE_LDFLAGS" \
+	    "" \
+	    { pr23169a.c pr23169b.c pr23169c.c } \
+	    "pr23169g" \
+	    "pass.out" \
+	    "-fPIE -O2 -g" \
+	] \
+]
+}
+}
 
 set ASFLAGS "$saved_ASFLAGS"