94084c
commit 0dcbf4c8705309af8c8c1620491c60539901a3b0
94084c
Author: Joseph Myers <joseph@codesourcery.com>
94084c
Date:   Fri Sep 17 13:12:10 2021 +0000
94084c
94084c
    Run conform/ tests using newly built libc
94084c
    
94084c
    Although the conform/ header tests are built using the headers of the
94084c
    glibc under test, the execution tests from conformtest (a few tests of
94084c
    the values of macros evaluating to string constants) are linked and
94084c
    run with system libc, not the newly built libc.
94084c
    
94084c
    Apart from preventing testing in cross environments, this can be a
94084c
    problem even for native testing.  Specifically, it can be useful to do
94084c
    native testing when building with a cross compiler that links with a
94084c
    libc that is not the system libc; for example, on x86_64, you can test
94084c
    all three ABIs that way if the kernel support is present, even if the
94084c
    host OS lacks 32-bit or x32 libraries or they are older than the
94084c
    libraries in the sysroot used by the compiler used to build glibc.
94084c
    This works for almost all tests, but not for these conformtest tests.
94084c
    
94084c
    Arrange for conformtest to link and run test programs similarly to
94084c
    other tests, with consequent refactoring of various variables in
94084c
    Makeconfig to allow passing relevant parts of the link-time command
94084c
    lines down to conformtest.  In general, the parts of the link command
94084c
    involving $@ or $^ are separated out from the parts that should be
94084c
    passed to conformtest (the variables passed to conformtest still
94084c
    involve various variables whose names involve $(@F), but those
94084c
    variables simply won't be defined for the conformtest makefile rules
94084c
    and I think their presence there is harmless).
94084c
    
94084c
    This is also most of the support that would be needed to allow running
94084c
    those tests of string constants for cross testing when test-wrapper is
94084c
    defined.  That will also need changes to where conformtest.py puts the
94084c
    test executables, so it puts them in the main object directory
94084c
    (expected to be shared with a test system in cross testing) rather
94084c
    than /tmp (not expected to be shared) as at present.
94084c
    
94084c
    Tested for x86_64.
94084c
    
94084c
    (cherry picked from commit f3eef963902d0f54f68cffc74f79b97f4d6154b7)
94084c
94084c
diff --git a/Makeconfig b/Makeconfig
94084c
index 68663d984e6a1264..2fa0884b4eee5e53 100644
94084c
--- a/Makeconfig
94084c
+++ b/Makeconfig
94084c
@@ -412,12 +412,13 @@ link-extra-libs-tests = $(libsupport)
94084c
 
94084c
 # Command for linking PIE programs with the C library.
94084c
 ifndef +link-pie
94084c
-+link-pie-before-libc = $(if $($(@F)-no-pie),$(no-pie-ldflag),-pie) \
94084c
-	     -Wl,-O1 -nostdlib -nostartfiles -o $@ \
94084c
++link-pie-before-inputs = $(if $($(@F)-no-pie),$(no-pie-ldflag),-pie) \
94084c
+	     -Wl,-O1 -nostdlib -nostartfiles \
94084c
 	     $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \
94084c
 	     $(combreloc-LDFLAGS) $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \
94084c
 	     $(firstword $(CRT-$(@F)) $(csu-objpfx)S$(start-installed-name)) \
94084c
-	     $(+preinit) $(+prectorS) \
94084c
+	     $(+preinit) $(+prectorS)
94084c
++link-pie-before-libc = -o $@ $(+link-pie-before-inputs) \
94084c
 	     $(filter-out $(addprefix $(csu-objpfx),start.o \
94084c
 						    S$(start-installed-name))\
94084c
 			  $(+preinit) $(link-extra-libs) \
94084c
@@ -442,11 +443,12 @@ endef
94084c
 endif
94084c
 # Command for statically linking programs with the C library.
94084c
 ifndef +link-static
94084c
-+link-static-before-libc = -nostdlib -nostartfiles -static -o $@ \
94084c
++link-static-before-inputs = -nostdlib -nostartfiles -static \
94084c
 	      $(if $($(@F)-no-pie),$(no-pie-ldflag),$(default-pie-ldflag)) \
94084c
 	      $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F))  \
94084c
 	      $(firstword $(CRT-$(@F)) $(csu-objpfx)$(real-static-start-installed-name)) \
94084c
-	      $(+preinit) $(+prectorT) \
94084c
+	      $(+preinit) $(+prectorT)
94084c
++link-static-before-libc = -o $@ $(+link-static-before-inputs) \
94084c
 	      $(filter-out $(addprefix $(csu-objpfx),start.o \
94084c
 						     $(start-installed-name))\
94084c
 			   $(+preinit) $(link-extra-libs-static) \
94084c
@@ -473,13 +475,16 @@ endif
94084c
 ifeq (yes,$(build-pie-default))
94084c
 +link = $(+link-pie)
94084c
 +link-tests = $(+link-pie-tests)
94084c
++link-tests-before-inputs = $(+link-pie-before-inputs) $(rtld-tests-LDFLAGS)
94084c
++link-tests-after-inputs = $(link-libc-tests) $(+link-pie-after-libc)
94084c
 +link-printers-tests = $(+link-pie-printers-tests)
94084c
 else  # not build-pie-default
94084c
-+link-before-libc = -nostdlib -nostartfiles -o $@ \
94084c
++link-before-inputs = -nostdlib -nostartfiles \
94084c
 	      $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \
94084c
 	      $(combreloc-LDFLAGS) $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \
94084c
 	      $(firstword $(CRT-$(@F)) $(csu-objpfx)$(start-installed-name)) \
94084c
-	      $(+preinit) $(+prector) \
94084c
+	      $(+preinit) $(+prector)
94084c
++link-before-libc = -o $@ $(+link-before-inputs) \
94084c
 	      $(filter-out $(addprefix $(csu-objpfx),start.o \
94084c
 						     $(start-installed-name))\
94084c
 			   $(+preinit) $(link-extra-libs) \
94084c
@@ -491,6 +496,8 @@ $(CC) $(link-libc-rpath-link) $(+link-before-libc) $(rtld-LDFLAGS) \
94084c
   $(link-extra-flags) $(link-libc) $(+link-after-libc)
94084c
 $(call after-link,$@)
94084c
 endef
94084c
++link-tests-before-inputs = $(+link-before-inputs) $(rtld-tests-LDFLAGS)
94084c
++link-tests-after-inputs = $(link-libc-tests) $(+link-after-libc)
94084c
 define +link-tests
94084c
 $(CC) $(+link-before-libc) $(rtld-tests-LDFLAGS) $(link-libc-tests) \
94084c
 		     $(+link-after-libc)
94084c
@@ -505,6 +512,8 @@ endif  # build-pie-default
94084c
 else  # build-static
94084c
 +link = $(+link-static)
94084c
 +link-tests = $(+link-static-tests)
94084c
++link-tests-before-inputs = $(+link-static-before-inputs)
94084c
++link-tests-after-inputs = $(link-libc-static-tests) $(+link-static-after-libc)
94084c
 +link-printers-tests = $(+link-static-tests)
94084c
 endif  # build-shared
94084c
 endif  # +link
94084c
diff --git a/conform/Makefile b/conform/Makefile
94084c
index c64a83a905b991cc..296db818f0a72253 100644
94084c
--- a/conform/Makefile
94084c
+++ b/conform/Makefile
94084c
@@ -176,6 +176,9 @@ $(conformtest-header-tests): $(objpfx)%/conform.out: \
94084c
 	(set -e; std_hdr=$*; std=$${std_hdr%%/*}; hdr=$${std_hdr#*/}; \
94084c
 	 mkdir -p $(@D); \
94084c
 	 $(PYTHON) $< --cc='$(CC)' --flags='$(conformtest-cc-flags)' \
94084c
+		   --ldflags='$(+link-tests-before-inputs)' \
94084c
+		   --libs='$(+link-tests-after-inputs)' \
94084c
+		   --run-program-prefix='$(run-program-prefix)' \
94084c
 		   --standard=$$std --header=$$hdr $(conformtest-xfail) \
94084c
 		   $(conformtest-cross) \
94084c
 		   > $@ 2>&1;; \
94084c
diff --git a/conform/conformtest.py b/conform/conformtest.py
94084c
index f0405b71869831f9..4898e16c9fb96503 100644
94084c
--- a/conform/conformtest.py
94084c
+++ b/conform/conformtest.py
94084c
@@ -381,12 +381,16 @@ class MacroStrTest(object):
94084c
 class HeaderTests(object):
94084c
     """The set of tests run for a header."""
94084c
 
94084c
-    def __init__(self, header, standard, cc, flags, cross, xfail):
94084c
+    def __init__(self, header, standard, cc, flags, ldflags, libs,
94084c
+                 run_program_prefix, cross, xfail):
94084c
         """Initialize a HeaderTests object."""
94084c
         self.header = header
94084c
         self.standard = standard
94084c
         self.cc = cc
94084c
         self.flags = flags
94084c
+        self.ldflags = ldflags
94084c
+        self.libs = libs
94084c
+        self.run_program_prefix = run_program_prefix
94084c
         self.cross = cross
94084c
         self.xfail_str = xfail
94084c
         self.cflags_namespace = ('%s -fno-builtin %s -D_ISOMAC'
94084c
@@ -590,7 +594,8 @@ class HeaderTests(object):
94084c
         exe_file = os.path.join(self.temp_dir, 'test')
94084c
         with open(c_file, 'w') as c_file_out:
94084c
             c_file_out.write('#include <%s>\n%s' % (self.header, text))
94084c
-        cmd = ('%s %s %s -o %s' % (self.cc, self.cflags, c_file, exe_file))
94084c
+        cmd = ('%s %s %s %s %s -o %s' % (self.cc, self.cflags, self.ldflags,
94084c
+                                         c_file, self.libs, exe_file))
94084c
         try:
94084c
             subprocess.check_call(cmd, shell=True)
94084c
         except subprocess.CalledProcessError:
94084c
@@ -600,7 +605,9 @@ class HeaderTests(object):
94084c
             self.note_skip(name)
94084c
             return
94084c
         try:
94084c
-            subprocess.check_call(exe_file, shell=True)
94084c
+            subprocess.check_call('%s %s' % (self.run_program_prefix,
94084c
+                                             exe_file),
94084c
+                                  shell=True)
94084c
         except subprocess.CalledProcessError:
94084c
             self.note_error(name, self.group_xfail)
94084c
             return
94084c
@@ -719,12 +726,19 @@ def main():
94084c
                         help='C compiler to use')
94084c
     parser.add_argument('--flags', metavar='CFLAGS',
94084c
                         help='Compiler flags to use with CC')
94084c
+    parser.add_argument('--ldflags', metavar='LDFLAGS',
94084c
+                        help='Compiler arguments for linking before inputs')
94084c
+    parser.add_argument('--libs', metavar='LIBS',
94084c
+                        help='Compiler arguments for linking after inputs')
94084c
+    parser.add_argument('--run-program-prefix', metavar='RUN-PROGRAM-PREFIX',
94084c
+                        help='Wrapper for running newly built program')
94084c
     parser.add_argument('--cross', action='store_true',
94084c
                         help='Do not run compiled test programs')
94084c
     parser.add_argument('--xfail', metavar='COND',
94084c
                         help='Name of condition for XFAILs')
94084c
     args = parser.parse_args()
94084c
     tests = HeaderTests(args.header, args.standard, args.cc, args.flags,
94084c
+                        args.ldflags, args.libs, args.run_program_prefix,
94084c
                         args.cross, args.xfail)
94084c
     tests.run()
94084c