LoongArch: Improve the reliability of test cases

Fix PR ld/31101

In some distributions, GCC enables --as-needed by default, which
may prevent linking to necessary dynamic libraries and cause test
failures. When tests require the host GCC, use the --no-as-needed
option and place the necessary dynamic libraries after the object
files. This prevents test failures and improves the test case
reliability.
diff --git a/ld/testsuite/ld-loongarch-elf/pic.exp b/ld/testsuite/ld-loongarch-elf/pic.exp
index e449a33..510c3fd 100644
--- a/ld/testsuite/ld-loongarch-elf/pic.exp
+++ b/ld/testsuite/ld-loongarch-elf/pic.exp
@@ -115,31 +115,33 @@
 set link_tests_libc [list \
   [list \
     "$testname readelf -s/-r nopic-global-so" \
-    "-L./tmpdir -lnopic-global -L/usr/lib -lc" "" \
+    "-Wl,--no-as-needed" "" \
     {nopic-global.s} \
     {{readelf {-s} nopic-global-so.sd} \
      {readelf {-r} nopic-global-so.rd}} \
     "nopic-global-so" \
+    "" "-L./tmpdir -lnopic-global" \
   ] \
   [list \
     "$testname readelf -s/-x nopic-weak-global-so" \
-    "-L./tmpdir -lnopic-global -L/usr/lib -lc" "" \
+    "-Wl,--no-as-needed" "" \
     {nopic-weak-global.s} \
     {{readelf {-s} nopic-weak-global-so.sd} \
      {readelf {-r} nopic-weak-global-so.rd}} \
     "nopic-weak-global-so" \
+    "" "-L./tmpdir -lnopic-global" \
   ] \
 ]
 
 
 #  0:name
-#  1:ld/ar leading options, placed before object files
-#  2:ld/ar trailing options, placed after object files
-#  3:assembler options
-#  4:filenames of assembler files
-#  5:list of actions, options and expected outputs.
-#  6:name of output file
-#  7:compiler flags (optional)
+#  1:leading ld or ar options
+#  2:compile options
+#  3:filenames of source files
+#  4:action and options.
+#  5:name of output file
+#  6:language (optional)
+#  7:trailing ld options (optional), placed after object files
 run_cc_link_tests $link_tests_libc
 
 
@@ -170,10 +172,11 @@
   ] \
   [list \
     "$testname" \
-    "-L./tmpdir -lnopic-global -lc -Wl,-rpath=./tmpdir -no-pie" "" \
+    "-Wl,--no-as-needed" "" \
     { nopic-global.s }   \
     "nopic-global-so" \
-    "nopic-global.out" \
+    "nopic-global.out" "" "" "" \
+    "-L./tmpdir -lnopic-global -lc -Wl,-rpath=./tmpdir -no-pie" \
   ] \
   [list \
     "$testname" \
@@ -184,10 +187,11 @@
   ] \
   [list \
     "$testname" \
-    "-L./tmpdir -lnopic-global -lc -Wl,-rpath=./tmpdir -no-pie" "" \
+    "-Wl,--no-as-needed" "" \
     { nopic-weak-global.s }   \
     "nopic-weak-global-so" \
-    "nopic-weak-global.out" \
+    "nopic-weak-global.out" "" "" "" \
+    "-L./tmpdir -lnopic-global -lc -Wl,-rpath=./tmpdir -no-pie" \
   ] \
 ]