Set NOPIE_CFLAGS and NOPIE_LDFLAGS

GCC 6 can be configured to generate PIE by default.  But some linker
size tests expect non-PIE.  This patch defines NOPIE_CFLAGS to
"-fno-PIE" and NOPIE_LDFLAGS to "-no-pie" if target compiler supports
them.  Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS to linker size tests if
needed.

	* config/default.exp (NOPIE_CFLAGS): New.
	(NOPIE_LDFLAGS): Likewise.
	* ld-size/size.exp (run_cc_link_tests): Add $NOPIE_CFLAGS and
	$NOPIE_LDFLAGS if needed.
	(run_ld_link_exec_tests): Add $NOPIE_CFLAGS if needed.
This commit is contained in:
H.J. Lu 2015-07-27 16:16:17 -07:00
parent 13e1dec896
commit 6f8ea6c424
3 changed files with 276 additions and 94 deletions

View File

@ -1,3 +1,11 @@
2015-07-27 H.J. Lu <hongjiu.lu@intel.com>
* config/default.exp (NOPIE_CFLAGS): New.
(NOPIE_LDFLAGS): Likewise.
* ld-size/size.exp (run_cc_link_tests): Add $NOPIE_CFLAGS and
$NOPIE_LDFLAGS if needed.
(run_ld_link_exec_tests): Add $NOPIE_CFLAGS if needed.
2015-07-27 H.J. Lu <hongjiu.lu@intel.com> 2015-07-27 H.J. Lu <hongjiu.lu@intel.com>
* ld-srec/srec.exp (CC): Save and restore. Append $PLT_CFLAGS. * ld-srec/srec.exp (CC): Save and restore. Append $PLT_CFLAGS.

View File

@ -313,3 +313,42 @@ if { ![info exists PLT_CFLAGS] } then {
set PLT_CFLAGS "" set PLT_CFLAGS ""
} }
} }
# Set NOPIE_CFLAGS to "-fno-PIE" and NOPIE_LDFLAGS to "-no-pie" if
# target compiler supports them.
if { ![info exists NOPIE_CFLAGS] || ![info exists NOPIE_LDFLAGS] } then {
if { [which $CC] != 0 } {
# Check if gcc supports -fno-PIE -no-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/nopie[pid]"
set src ${basename}.c
set output ${basename}
set f [open $src "w"]
puts $f "int main (void) { return 0; }"
close $f
remote_download host $src
set nopie_available [run_host_cmd_yesno "$CC" "$flags -fno-PIE -no-pie $src -o $output"]
remote_file host delete $src
remote_file host delete $output
file delete $src
if { $nopie_available == 1 } then {
set NOPIE_CFLAGS "-fno-PIE"
set NOPIE_LDFLAGS "-no-pie"
} else {
set NOPIE_CFLAGS ""
set NOPIE_LDFLAGS ""
}
} else {
set NOPIE_CFLAGS ""
set NOPIE_LDFLAGS ""
}
}

View File

@ -61,90 +61,240 @@ if { [which $CC] == 0 } {
return return
} }
set build_tests { # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
{"Build libsize-1.so" global NOPIE_CFLAGS NOPIE_LDFLAGS
"-shared" "-fPIC"
{size-1a.c size-1b.c} {} "libsize-1.so"}
{"Build libsize-2.so"
"-shared" "-fPIC"
{size-2a.c size-2b.c} {} "libsize-2.so"}
{"Build libsize-3a.so"
"-shared" "-fPIC"
{size-3a.c} {} "libsize-3a.so"}
{"Build libsize-3b.so"
"-shared" "-fPIC"
{size-3b.c} {} "libsize-3b.so"}
{"Build libsize-3c.so"
"-shared" "-fPIC"
{size-3c.c} {} "libsize-3c.so"}
{"Build libsize-6b.so"
"-shared" "-fPIC"
{size-6b.c} {} "libsize-6b.so"}
{"Build libsize-7.so"
"-shared" "-fPIC"
{size-7b.c} {} "libsize-7.so"}
{"Build size-7"
"-Wl,--no-as-needed tmpdir/libsize-7.so" ""
{size-7a.c} {{readelf -rW size-7.rd}} "size-7.exe"}
{"Build libsize-8.so"
"-shared" "-fPIC"
{size-8b.c} {} "libsize-8.so"}
{"Build size-8"
"-Wl,--no-as-needed tmpdir/libsize-8.so" ""
{size-8a.c} {{readelf -rW size-8.rd}} "size-8.exe"}
{"Build libsize-9.so"
"-shared" "-fPIC"
{size-9b.c} {{readelf -rW size-9.rd}} "libsize-9.so"}
{"Build libsize-10.so"
"-shared" "-fPIC"
{size-10b.c} {{readelf -rW size-10.rd}} "libsize-10.so"}
}
run_cc_link_tests $build_tests run_cc_link_tests [list \
[list \
"Build libsize-1.so" \
"-shared" \
"-fPIC" \
{size-1a.c size-1b.c} \
{} \
"libsize-1.so" \
] \
[list \
"Build libsize-2.so" \
"-shared" \
"-fPIC" \
{size-2a.c size-2b.c} \
{} \
"libsize-2.so" \
] \
[list \
"Build libsize-3a.so" \
"-shared" "-fPIC" \
{size-3a.c} \
{} \
"libsize-3a.so" \
] \
[list \
"Build libsize-3b.so" \
"-shared" \
"-fPIC" \
{size-3b.c} \
{} \
"libsize-3b.so" \
] \
[list \
"Build libsize-3c.so" \
"-shared" \
"-fPIC" \
{size-3c.c} \
{} \
"libsize-3c.so" \
] \
[list \
"Build libsize-6b.so" \
"-shared" \
"-fPIC" \
{size-6b.c} \
{} \
"libsize-6b.so" \
] \
[list \
"Build libsize-7.so" \
"-shared" \
"-fPIC" \
{size-7b.c} \
{} \
"libsize-7.so" \
] \
[list \
"Build size-7" \
"$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libsize-7.so" \
"$NOPIE_CFLAGS" \
{size-7a.c} \
{{readelf -rW size-7.rd}} \
"size-7.exe" \
] \
[list \
"Build libsize-8.so" \
"-shared" "-fPIC" \
{size-8b.c} \
{} \
"libsize-8.so" \
] \
[list \
"Build size-8" \
"$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libsize-8.so" \
"$NOPIE_CFLAGS" \
{size-8a.c} \
{{readelf -rW size-8.rd}} \
"size-8.exe" \
] \
[list \
"Build libsize-9.so" \
"-shared" \
"-fPIC" \
{size-9b.c} \
{{readelf -rW size-9.rd}} \
"libsize-9.so" \
] \
[list \
"Build libsize-10.so" \
"-shared" \
"-fPIC" \
{size-10b.c} \
{{readelf -rW size-10.rd}} \
"libsize-10.so" \
] \
]
set run_tests { # Run-time size relocation tests.
{"Run size-1" run_ld_link_exec_tests [] [list \
"tmpdir/libsize-1.so" "" [list \
{size-1.c} "size-1" "size-1.out"} "Run size-1" \
{"Run size-2" "tmpdir/libsize-1.so" \
"tmpdir/libsize-2.so" "" "" \
{size-2.c} "size-2" "size-2.out"} {size-1.c} \
{"Run size-3a" "size-1" \
"" "" "size-1.out" \
{size-3.c size-3a.c} "size-3a" "size-3.out"} ] \
{"Run size-3b" [list \
"tmpdir/libsize-3a.so" "" "Run size-2" \
{size-3.c} "size-3b" "size-3.out"} "tmpdir/libsize-2.so" \
{"Run size-3c" "" \
"" "" {size-2.c} \
{size-3.c size-3b.c} "size-3c" "size-3.out"} "size-2" \
{"Run size-3d" "size-2.out" \
"tmpdir/libsize-3b.so" "" ] \
{size-3.c} "size-3d" "size-3.out"} [list \
{"Run size-3d" "Run size-3a" \
"" "" "" \
{size-3.c size-3c.c} "size-3d" "size-3.out"} "" \
{size-3.c size-3a.c} \
"size-3a" \
"size-3.out" \
] \
[list \
"Run size-3b" \
"tmpdir/libsize-3a.so" \
"" \
{size-3.c} \
"size-3b" \
"size-3.out" \
] \
[list \
"Run size-3c" \
"" \
"" \
{size-3.c size-3b.c} \
"size-3c" \
"size-3.out" \
] \
[list \
"Run size-3d (1)" \
"tmpdir/libsize-3b.so" \
"" \
{size-3.c} \
"size-3d1" \
"size-3.out" \
] \
[list \
"Run size-3d (2)" \
"" \
"" \
{size-3.c size-3c.c} \
"size-3d2" \
"size-3.out" \
] \
[list \
{"Run size-3e" {"Run size-3e"
"tmpdir/libsize-3c.so" "" "tmpdir/libsize-3c.so" ""
{size-3.c} "size-3e" "size-3.out"} {size-3.c} "size-3e" "size-3.out"}
{"Run size-4a" ] \
"" "" [list \
{size-4a.c size-4b.c} "size-4a" "size-4.out"} "Run size-4a" \
{"Run size-4b" "" \
"" "" "" \
{size-4b.c size-4a.c} "size-4b" "size-4.out"} {size-4a.c size-4b.c} \
{"Run size-5a" "size-4a" \
"" "" "size-4.out" \
{size-5a.c size-5b.c} "size-5a" "size-5.out"} "$NOPIE_CFLAGS" \
{"Run size-5b" ] \
"" "" [list \
{size-5b.c size-5a.c} "size-5b" "size-5.out"} "Run size-4b" \
{"Run size-6" "" \
"tmpdir/libsize-6b.so" "" "" \
{size-6a.c} "size-6" "size-6.out"} {size-4b.c size-4a.c} \
} "size-4b" \
"size-4.out" \
run_ld_link_exec_tests [] $run_tests "$NOPIE_CFLAGS" \
] \
[list \
"Run size-5a" \
"" \
"" \
{size-5a.c size-5b.c} \
"size-5a" \
"size-5.out" \
"$NOPIE_CFLAGS" \
] \
[list \
"Run size-5b" \
"" \
"" \
{size-5b.c size-5a.c} \
"size-5b" \
"size-5.out" \
"$NOPIE_CFLAGS" \
] \
[list \
"Run size-6" \
"tmpdir/libsize-6b.so" \
"" \
{size-6a.c} \
"size-6" \
"size-6.out" \
"$NOPIE_CFLAGS" \
] \
[list \
"Run size-8" \
"tmpdir/libsize-8.so --hash-styl=gnu" \
"" \
{size-8a.c} \
"size-8" \
"size-8.out" \
"$NOPIE_CFLAGS" \
] \
[list \
"Run size-9" \
"tmpdir/libsize-9.so" \
"" \
{size-9a.c} \
"size-9" \
"size-9.out" \
] \
[list \
"Run size-10" \
"tmpdir/libsize-10.so" \
"" \
{size-10a.c} \
"size-10" \
"size-10.out" \
] \
]
# Check if size relocation works at run-time. # Check if size relocation works at run-time.
catch "exec tmpdir/size-7.exe > tmpdir/dump.out" exec_output catch "exec tmpdir/size-7.exe > tmpdir/dump.out" exec_output
@ -159,18 +309,3 @@ if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/size-7.out" ] } then {
fail "Run-time size relocation" fail "Run-time size relocation"
return return
} }
# Run-time size relocation tests.
set run_time_tests {
{"Run size-8"
"tmpdir/libsize-8.so --hash-styl=gnu" ""
{size-8a.c} "size-8" "size-8.out"}
{"Run size-9"
"tmpdir/libsize-9.so" ""
{size-9a.c} "size-9" "size-9.out"}
{"Run size-10"
"tmpdir/libsize-10.so" ""
{size-10a.c} "size-10" "size-10.out"}
}
run_ld_link_exec_tests [] $run_time_tests