From 24edc24dbe07ea0c08288b37682103a4751e47d0 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 14 Jul 2005 17:02:11 +0000
Subject: [PATCH] 2005-07-14  H.J. Lu  <hongjiu.lu@intel.com>

	* ld-elf/elf.exp (array_tests): New.
	Call run_ld_link_exec_tests with array_tests.

	* ld-elf/fini.c: New file.
	* ld-elf/fini.out: Likewise.
	* ld-elf/init.c: Likewise.
	* ld-elf/init.out: Likewise.
	* ld-elf/preinit.c: Likewise.
	* ld-elf/preinit.out: Likewise.

	* lib/ld-lib.exp (run_ld_link_exec_tests): New.
---
 ld/testsuite/ChangeLog          | 14 +++++
 ld/testsuite/ld-elf/elf.exp     | 16 ++++++
 ld/testsuite/ld-elf/fini.c      | 34 ++++++++++++
 ld/testsuite/ld-elf/fini.out    |  3 ++
 ld/testsuite/ld-elf/init.c      | 34 ++++++++++++
 ld/testsuite/ld-elf/init.out    |  3 ++
 ld/testsuite/ld-elf/preinit.c   | 34 ++++++++++++
 ld/testsuite/ld-elf/preinit.out |  3 ++
 ld/testsuite/lib/ld-lib.exp     | 95 +++++++++++++++++++++++++++++++++
 9 files changed, 236 insertions(+)
 create mode 100644 ld/testsuite/ld-elf/fini.c
 create mode 100644 ld/testsuite/ld-elf/fini.out
 create mode 100644 ld/testsuite/ld-elf/init.c
 create mode 100644 ld/testsuite/ld-elf/init.out
 create mode 100644 ld/testsuite/ld-elf/preinit.c
 create mode 100644 ld/testsuite/ld-elf/preinit.out

diff --git a/ld/testsuite/ChangeLog b/ld/testsuite/ChangeLog
index fabee28c648..135534a8af7 100644
--- a/ld/testsuite/ChangeLog
+++ b/ld/testsuite/ChangeLog
@@ -1,3 +1,17 @@
+2005-07-14  H.J. Lu  <hongjiu.lu@intel.com>
+
+	* ld-elf/elf.exp (array_tests): New.
+	Call run_ld_link_exec_tests with array_tests.
+
+	* ld-elf/fini.c: New file.
+	* ld-elf/fini.out: Likewise.
+	* ld-elf/init.c: Likewise.
+	* ld-elf/init.out: Likewise.
+	* ld-elf/preinit.c: Likewise.
+	* ld-elf/preinit.out: Likewise.
+
+	* lib/ld-lib.exp (run_ld_link_exec_tests): New.
+
 2005-07-12  Alan Modra  <amodra@bigpond.net.au>
 
 	* ld-powerpc/sdalib.s (lib_var): Size it.
diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp
index 0c82197e877..8dbb2c30753 100644
--- a/ld/testsuite/ld-elf/elf.exp
+++ b/ld/testsuite/ld-elf/elf.exp
@@ -28,3 +28,19 @@ foreach t $test_list {
     verbose [file rootname $t]
     run_dump_test [file rootname $t]
 }
+
+# The following tests require running the executable generated by ld.
+if ![isnative] {
+    return
+}
+
+set array_tests {
+    {"preinit array" "" "" {preinit.c} "preinit" "preinit.out"}
+    {"static preinit array" "-static" "" {preinit.c} "preinit" "preinit.out"}
+    {"init array" "" "" {init.c} "init" "init.out"}
+    {"static init array" "-static" "" {init.c} "init" "init.out"}
+    {"fini array" "" "" {fini.c} "fini" "fini.out"}
+    {"static fini array" "-static" "" {fini.c} "fini" "fini.out"}
+}
+
+run_ld_link_exec_tests $array_tests
diff --git a/ld/testsuite/ld-elf/fini.c b/ld/testsuite/ld-elf/fini.c
new file mode 100644
index 00000000000..cb6065538e6
--- /dev/null
+++ b/ld/testsuite/ld-elf/fini.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+
+static void
+fini_0 (void)
+{
+  printf ("fini array 0\n");
+}
+
+static void
+fini_1 (void)
+{
+  printf ("fini array 1\n");
+}
+
+static void
+fini_2 (void)
+{
+  printf ("fini array 2\n");
+}
+
+void (*const fini_array []) (void)
+     __attribute__ ((section (".fini_array"),
+		     aligned (sizeof (void *)))) =
+{
+  &fini_0,
+  &fini_1,
+  &fini_2
+};
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/ld/testsuite/ld-elf/fini.out b/ld/testsuite/ld-elf/fini.out
new file mode 100644
index 00000000000..2bab6e6744d
--- /dev/null
+++ b/ld/testsuite/ld-elf/fini.out
@@ -0,0 +1,3 @@
+fini array 2
+fini array 1
+fini array 0
diff --git a/ld/testsuite/ld-elf/init.c b/ld/testsuite/ld-elf/init.c
new file mode 100644
index 00000000000..3b1ffb1f22d
--- /dev/null
+++ b/ld/testsuite/ld-elf/init.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+
+static void
+init_0 (void)
+{
+  printf ("init array 0\n");
+}
+
+static void
+init_1 (void)
+{
+  printf ("init array 1\n");
+}
+
+static void
+init_2 (void)
+{
+  printf ("init array 2\n");
+}
+
+void (*const init_array []) (void)
+     __attribute__ ((section (".init_array"),
+		     aligned (sizeof (void *)))) =
+{
+  &init_0,
+  &init_1,
+  &init_2
+};
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/ld/testsuite/ld-elf/init.out b/ld/testsuite/ld-elf/init.out
new file mode 100644
index 00000000000..f988b84c877
--- /dev/null
+++ b/ld/testsuite/ld-elf/init.out
@@ -0,0 +1,3 @@
+init array 0
+init array 1
+init array 2
diff --git a/ld/testsuite/ld-elf/preinit.c b/ld/testsuite/ld-elf/preinit.c
new file mode 100644
index 00000000000..9ef2a89c6c3
--- /dev/null
+++ b/ld/testsuite/ld-elf/preinit.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+
+static void
+preinit_0 (void)
+{
+  printf ("preinit array 0\n");
+}
+
+static void
+preinit_1 (void)
+{
+  printf ("preinit array 1\n");
+}
+
+static void
+preinit_2 (void)
+{
+  printf ("preinit array 2\n");
+}
+
+void (*const preinit_array []) (void)
+     __attribute__ ((section (".preinit_array"),
+		     aligned (sizeof (void *)))) =
+{
+  &preinit_0,
+  &preinit_1,
+  &preinit_2
+};
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/ld/testsuite/ld-elf/preinit.out b/ld/testsuite/ld-elf/preinit.out
new file mode 100644
index 00000000000..6a30bc6dd2e
--- /dev/null
+++ b/ld/testsuite/ld-elf/preinit.out
@@ -0,0 +1,3 @@
+preinit array 0
+preinit array 1
+preinit array 2
diff --git a/ld/testsuite/lib/ld-lib.exp b/ld/testsuite/lib/ld-lib.exp
index cc0741bb414..54df99156af 100644
--- a/ld/testsuite/lib/ld-lib.exp
+++ b/ld/testsuite/lib/ld-lib.exp
@@ -1262,3 +1262,98 @@ if ![string length [info proc prune_warnings]] {
 	return $text
     }
 }
+
+# List contains test-items with 3 items followed by 2 lists, one item and
+# one optional item:
+# 0:name
+# 1:ld options
+# 2:assembler options
+# 3:filenames of assembler files
+# 4:name of output file
+# 5:expected output
+# 6:compiler flags (optional)
+
+proc run_ld_link_exec_tests { ldtests } {
+    global ld
+    global as
+    global srcdir
+    global subdir
+    global env
+    global CC
+    global CFLAGS
+
+    foreach testitem $ldtests {
+	set testname [lindex $testitem 0]
+	set ld_options [lindex $testitem 1]
+	set as_options [lindex $testitem 2]
+	set src_files  [lindex $testitem 3]
+	set binfile tmpdir/[lindex $testitem 4]
+	set expfile [lindex $testitem 5]
+	set cflags [lindex $testitem 6]
+	set objfiles {}
+	set is_unresolved 0
+	set failed 0
+
+#	verbose -log "Testname is $testname"
+#	verbose -log "ld_options is $ld_options"
+#	verbose -log "as_options is $as_options"
+#	verbose -log "src_files is $src_files"
+#	verbose -log "actions is $actions"
+#	verbose -log "binfile is $binfile"
+
+	# Assemble each file in the test.
+	foreach src_file $src_files {
+	    set objfile "tmpdir/[file rootname $src_file].o"
+	    lappend objfiles $objfile
+
+	    if ![ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/$src_file $objfile] {
+		set is_unresolved 1
+		break
+	    }
+	}
+
+	# Catch assembler errors.
+	if { $is_unresolved != 0 } {
+	    unresolved $testname
+	    continue
+	}
+
+	if ![ld_link $ld $binfile "-L$srcdir/$subdir $ld_options $objfiles"] {
+	    set failed 1
+	} else {
+	    set failed 0
+	    send_log "Running: $binfile > $binfile.out\n"
+	    verbose "Running: $binfile > $binfile.out"
+	    catch "exec $binfile > $binfile.out" exec_output
+	    
+	    if ![string match "" $exec_output] then {
+		send_log "$exec_output\n"
+		verbose "$exec_output" 1
+		set failed 1
+	    } else {
+		send_log "diff $binfile.out $srcdir/$subdir/$expfile\n"
+		verbose "diff $binfile.out $srcdir/$subdir/$expfile"
+		catch "exec diff $binfile.out $srcdir/$subdir/$expfile" exec_output
+		set exec_output [prune_warnings $exec_output]
+
+		if ![string match "" $exec_output] then {
+		    send_log "$exec_output\n"
+		    verbose "$exec_output" 1
+		    set failed 1
+		}
+	    }
+
+	    if { $failed != 0 } {
+		fail $testname
+	    } else { if { $is_unresolved == 0 } {
+		pass $testname
+	    } }
+	}
+
+	# Catch action errors.
+	if { $is_unresolved != 0 } {
+	    unresolved $testname
+	    continue
+	}
+    }
+}
-- 
GitLab