https://gcc.gnu.org/g:c6ebf01257c2c1d8ceeec6273076544a336cccc9

commit r17-6109-gc6ebf01257c2c1d8ceeec6273076544a336cccc9
Author: Stefan Schulze Frielinghaus <[email protected]>
Date:   Tue Nov 10 13:11:35 2026 +0100

    s390: Fix return value ABI handling for nullptr_t
    
    Starting with r0-104773-g0d697034b7eecd in GCC 4.6 (which introduced
    initial C++11 and std::nullptr_t support), arguments of type nullptr_t,
    std::nullptr_t, and decltype(nullptr) are passed in registers.  However,
    return values are erroneously returned in memory, violating the psABI.
    
    Fixed by handling NULLPTR_TYPE in s390_return_in_memory, similarly to
    how it is already handled in s390_function_arg_integer, so that it is
    returned in a register.
    
    N.B., this also affects how arguments are passed.  Whenever a value is
    returned in memory, then a hidden argument is passed which holds the
    address where to store the value.  This in turn means that named
    arguments which were to be passed in GPR N are then passed in GPR N+1,
    or if no GPR is left, they are then passed in memory.  For example, if
    the TU of foo() is compiled with a GCC version prior to 17
    
    nullptr_t foo (int x)
    {
      return nullptr;
    }
    
    and the TU of main() with GCC 17
    
    extern nullptr_t foo (int);
    int main (void)
    {
      foo (0);
    }
    
    then running the program results in a segfault because foo() expects in
    r2 the hidden argument where to store the return value, whereas main()
    passes the first named argument in r2 (which is zero), leading to a
    segfault once written to.
    
    In order to warn users about the psABI change, a diagnostic is emitted.
    A note is used instead of a warning because all other psABI diagnostics
    are notes, too.
    
    As much as diagnostics are welcome, they are just as annoying to read
    multiple times.  Therefore, they should be emitted sparsely, especially
    in cases where the psABI change is not visible to users.  For example,
    do not warn for functions without linkage and where the address is not
    taken.  Likewise, do not warn for direct function calls to functions
    without linkage.  Linkage is determined via the cgraph flag
    externally_visible and not TREE_PUBLIC, since LTO partitioning makes
    static functions public in order to reference them across partitions.
    The flag is only set for functions defined in the current unit, i.e., it
    is unset for any callee which is only declared.  Such a callee may be
    defined in an object built by an older GCC and is therefore diagnosed,
    unless it only lives in another LTO partition.
    
    A natural place to emit a diagnostic would have been
    TARGET_RETURN_IN_MEMORY where the fix is also applied.  However, this
    hook is utilized in many places and at different stages even as early as
    by front ends.  Diagnostics should be postponed until it is clear
    whether code for a function is emitted, or whether calls materialize,
    i.e., for calls which are optimized out, an ABI break isn't user visible
    and no diagnostic is required.  Therefore, RTL expansion seems to be a
    good fit.  Target hooks TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE
    which both utilize s390_function_and_libcall_value() are used for this.
    Although aggregate_value_p() calls into the former hook early, e.g.,
    from front ends or the gimplifier, at that point neither
    currently_expanding_function_start nor currently_expanding_gimple_stmt
    is set, so no diagnostic is emitted.
    
    gcc/ChangeLog:
    
            * config/s390/s390.cc (s390_return_in_memory): Handle
            NULLPTR_TYPE.
            (s390_diagnose_nullptr_return): New function.
            (s390_function_and_libcall_value): Call new function.
    
    gcc/testsuite/ChangeLog:
    
            * g++.target/s390/nullptr-1.C: New test.
            * gcc.target/s390/lto/lto.exp: New file.
            * gcc.target/s390/lto/nullptr-lto-1_0.c: New test.
            * gcc.target/s390/lto/nullptr-lto-1_1.c: New test.
            * gcc.target/s390/lto/nullptr-lto-1_2.c: New test.
            * gcc.target/s390/nullptr-1.c: New test.
            * gcc.target/s390/nullptr-2.c: New test.
            * gcc.target/s390/nullptr-3.c: New test.
            * gcc.target/s390/nullptr-4.c: New test.
            * gcc.target/s390/nullptr-5.c: New test.

Diff:
---
 gcc/config/s390/s390.cc                            |  65 ++++++++++
 gcc/testsuite/g++.target/s390/nullptr-1.C          |  17 +++
 gcc/testsuite/gcc.target/s390/lto/lto.exp          |  61 +++++++++
 .../gcc.target/s390/lto/nullptr-lto-1_0.c          | 140 +++++++++++++++++++++
 .../gcc.target/s390/lto/nullptr-lto-1_1.c          |  27 ++++
 .../gcc.target/s390/lto/nullptr-lto-1_2.c          |  34 +++++
 gcc/testsuite/gcc.target/s390/nullptr-1.c          | 112 +++++++++++++++++
 gcc/testsuite/gcc.target/s390/nullptr-2.c          |  82 ++++++++++++
 gcc/testsuite/gcc.target/s390/nullptr-3.c          |  25 ++++
 gcc/testsuite/gcc.target/s390/nullptr-4.c          |  44 +++++++
 gcc/testsuite/gcc.target/s390/nullptr-5.c          |  39 ++++++
 11 files changed, 646 insertions(+)

diff --git a/gcc/config/s390/s390.cc b/gcc/config/s390/s390.cc
index 5289d5aa6f0f..5b615aaef00e 100644
--- a/gcc/config/s390/s390.cc
+++ b/gcc/config/s390/s390.cc
@@ -13431,6 +13431,7 @@ s390_return_in_memory (const_tree type, const_tree 
fundecl ATTRIBUTE_UNUSED)
   /* We accept small integral (and similar) types.  */
   if (INTEGRAL_TYPE_P (type)
       || POINTER_TYPE_P (type)
+      || TREE_CODE (type) == NULLPTR_TYPE
       || TREE_CODE (type) == OFFSET_TYPE
       || SCALAR_FLOAT_TYPE_P (type))
     return int_size_in_bytes (type) > 8;
@@ -13477,6 +13478,68 @@ s390_promote_function_mode (const_tree type 
ATTRIBUTE_UNUSED,
   return mode;
 }
 
+/* Emit a diagnostic for the GCC 17.1 psABI fix for return type
+   NULLPTR_TYPE.  */
+
+static void
+s390_diagnose_nullptr_return (const_tree ret_type)
+{
+  if (!ret_type || TREE_CODE (ret_type) != NULLPTR_TYPE || !warn_psabi)
+    return;
+
+  bool warn_p = false;
+  /* Diagnose function definitions.  */
+  if (currently_expanding_function_start)
+    {
+      /* Since LTO may have promoted a static function to a public one in
+        order to be referenced across partitions, we cannot make use of
+        TREE_PUBLIC.  */
+      cgraph_node *node = cgraph_node::get (current_function_decl);
+      if (!node || node->externally_visible || node->address_taken)
+       warn_p = true;
+    }
+  /* Diagnose function call sites.  */
+  else if (currently_expanding_gimple_stmt
+          && is_a<gcall *> (currently_expanding_gimple_stmt))
+    {
+      /* Diagnose calls whose callee may have been built by an older GCC:
+
+        - Indirect calls.
+        - Calls to functions which are only declared.  The definition resides
+          in a different TU.  For LTO, symbols which are only declared have
+          externally_visible unset, so rely on DECL_EXTERNAL and on whether
+          the definition is in another partition instead.
+        - Calls to functions which are defined in this TU but may be replaced
+          at link time, e.g., weak functions or, when compiling for a shared
+          library (-fPIC), public functions with default visibility.
+
+        Calls to functions which cannot be replaced are not diagnosed since
+        caller and callee are compiled by the same GCC.  */
+      tree callee = gimple_call_fndecl (currently_expanding_gimple_stmt);
+      cgraph_node *node = callee ? cgraph_node::get (callee) : NULL;
+      if (!node
+         || (DECL_EXTERNAL (callee) && !node->in_other_partition)
+         || decl_replaceable_p (callee, true))
+       warn_p = true;
+    }
+
+  /* Functions expand_function_start and expand_call invoke hooks
+     TARGET_FUNCTION_VALUE / TARGET_LIBCALL_VALUE multiple times for a single
+     function definition / call.  Make sure to emit at most one diagnostic.  */
+  static unsigned last_reported_fndecl_uid;
+  if (warn_p
+      && DECL_UID (current_function_decl) != last_reported_fndecl_uid)
+    {
+      last_reported_fndecl_uid = DECL_UID (current_function_decl);
+      const char *url
+       = CHANGES_ROOT_URL "gcc-17/changes.html#s390_return_nullptr_type";
+      inform (input_location,
+             "the ABI for returning %qT changed %{in GCC 17.1%}; it is "
+             "now returned in a register instead of in memory",
+             ret_type, url);
+    }
+}
+
 /* Define where to return a (scalar) value of type RET_TYPE.
    If RET_TYPE is null, define where to return a (scalar)
    value of mode MODE from a libcall.  */
@@ -13487,6 +13550,8 @@ s390_function_and_libcall_value (machine_mode mode,
                                 const_tree fntype_or_decl,
                                 bool outgoing ATTRIBUTE_UNUSED)
 {
+  s390_diagnose_nullptr_return (ret_type);
+
   /* For vector return types it is important to use the RET_TYPE
      argument whenever available since the middle-end might have
      changed the mode to a scalar mode.  */
diff --git a/gcc/testsuite/g++.target/s390/nullptr-1.C 
b/gcc/testsuite/g++.target/s390/nullptr-1.C
new file mode 100644
index 000000000000..25b21b814c36
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/nullptr-1.C
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2 -Wpsabi" }
+
+// Ensure that after template instantiation no note is emitted.
+
+template <typename T, typename FuncT>
+static __attribute__ ((noinline)) T
+call (FuncT &&F)
+{
+  return F();
+}
+
+int *
+test ()
+{
+  return call<int *> ([] () { return nullptr; });
+}
diff --git a/gcc/testsuite/gcc.target/s390/lto/lto.exp 
b/gcc/testsuite/gcc.target/s390/lto/lto.exp
new file mode 100644
index 000000000000..47d210f248d6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/lto/lto.exp
@@ -0,0 +1,61 @@
+# Copyright (C) 2009-2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Exit immediately if this isn't an s390 target.
+if ![istarget s390*-*-*] then {
+  return
+}
+
+# Test link-time optimization across multiple files.
+#
+# Programs are broken into multiple files.  Each one is compiled
+# separately with LTO information.  The final executable is generated
+# by collecting all the generated object files using regular LTO or WHOPR.
+
+if $tracelevel then {
+    strace $tracelevel
+}
+
+# Load procedures from common libraries.
+load_lib standard.exp
+load_lib gcc.exp
+
+# Load the language-independent compatibility support procedures.
+load_lib lto.exp
+
+# If LTO has not been enabled, bail.
+if { ![check_effective_target_lto] } {
+    return
+}
+
+gcc_init
+lto_init no-mathlib
+
+# Define an identifier for use with this suite to avoid name conflicts
+# with other lto tests running at the same time.
+set sid "c_lto"
+
+# Main loop.
+foreach src [lsort [find $srcdir/$subdir *_0.c]] {
+    # If we're only testing specific files and this isn't one of them, skip it.
+    if ![runtest_file_p $runtests $src] then {
+       continue
+    }
+
+    lto-execute $src $sid
+}
+
+lto_finish
diff --git a/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_0.c 
b/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_0.c
new file mode 100644
index 000000000000..d3822d553847
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_0.c
@@ -0,0 +1,140 @@
+/* { dg-lto-do link } */
+/* { dg-require-linker-plugin "" } */
+/* { dg-lto-options { { -O2 -std=c23 -Wpsabi -flto -flto-partition=one 
-fuse-linker-plugin -fdump-tree-optimized } } } */
+
+/* Check -Wpsabi diagnostics for returning nullptr_t when linking an executable
+   with LTO and the linker plugin.  This file and nullptr-lto-1_1.c are part of
+   the LTO set whereas nullptr-lto-1_2.c is built without LTO.  For the latter
+   we have to assume that it was built with an older GCC and therefore expect
+   diagnostics.
+
+   Throughout the whole test
+   __asm__ volatile ("");
+   is used in order to prevent the removal of calls and whole functions.  We
+   cannot rely on attribute noipa because this has effects on function
+   visibility.  */
+
+#include <stddef.h>
+
+/* Defined in nullptr-lto-1_1.c, i.e., part of the LTO set.  */
+extern nullptr_t lto_extern (void);
+extern nullptr_t lto_weak (void);
+[[gnu::visibility ("hidden")]] extern nullptr_t lto_hidden (void);
+
+/* Defined in nullptr-lto-1_2.c, i.e., not part of the LTO set.  */
+extern nullptr_t obj_extern (void);
+extern nullptr_t obj_weak (void);
+[[gnu::visibility ("hidden")]] extern nullptr_t obj_hidden (void);
+extern nullptr_t (*volatile obj_fn_ptr) (void);
+
+/* Check that no diagnostics are emitted for calls to functions defined in the
+   LTO set.  The linker resolves them to the definition in the LTO set and
+   nothing outside references them, so neither the calls nor the definitions
+   are diagnosed.  */
+
+[[gnu::noinline]]
+void
+call_lto_extern (void)
+{
+  lto_extern ();
+}
+
+[[gnu::noinline]]
+void
+call_lto_weak (void)
+{
+  lto_weak ();
+}
+
+[[gnu::noinline]]
+void
+call_lto_hidden (void)
+{
+  lto_hidden ();
+}
+
+/* Check that diagnostics are emitted for calls to functions which are defined
+   in a non-LTO object.  */
+
+[[gnu::noinline]]
+void
+call_obj_extern (void)
+{
+  obj_extern (); /* { dg-lto-note "the ABI for returning 'nullptr_t' changed 
in GCC 17.1" } */
+}
+
+[[gnu::noinline]]
+void
+call_obj_weak (void)
+{
+  obj_weak (); /* { dg-lto-note "the ABI for returning 'nullptr_t' changed in 
GCC 17.1" } */
+}
+
+[[gnu::noinline]]
+void
+call_obj_hidden (void)
+{
+  obj_hidden (); /* { dg-lto-note "the ABI for returning 'nullptr_t' changed 
in GCC 17.1" } */
+}
+
+[[gnu::noinline]]
+void
+call_indirect (void)
+{
+  obj_fn_ptr (); /* { dg-lto-note "the ABI for returning 'nullptr_t' changed 
in GCC 17.1" } */
+}
+
+/* Diagnose a hidden function which is referenced from a non-LTO object from
+   nullptr-lto-1_2.c.  Here the linker plugin reports that the symbol is used
+   from outside of the LTO set.  */
+
+[[gnu::visibility ("hidden"), gnu::noinline]]
+nullptr_t
+hidden_used_by_obj (void) /* { dg-lto-note "the ABI for returning 'nullptr_t' 
changed in GCC 17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+/* Do not diagnose a hidden function which is only referenced from the LTO 
set.  */
+
+[[gnu::visibility ("hidden"), gnu::noinline]]
+nullptr_t
+hidden_unused_by_obj (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+int
+main (void)
+{
+  call_lto_extern ();
+  call_lto_weak ();
+  call_lto_hidden ();
+  call_obj_extern ();
+  call_obj_weak ();
+  call_obj_hidden ();
+  call_indirect ();
+  hidden_used_by_obj ();
+  hidden_unused_by_obj ();
+  return 0;
+}
+
+/* Make sure that all calls are still present before RTL expansion.  */
+/* { dg-final { scan-ltrans-tree-dump "lto_extern\\S* \\(\\);" "optimized" } } 
*/
+/* { dg-final { scan-ltrans-tree-dump "lto_weak\\S* \\(\\);" "optimized" } } */
+/* { dg-final { scan-ltrans-tree-dump "lto_hidden\\S* \\(\\);" "optimized" } } 
*/
+/* { dg-final { scan-ltrans-tree-dump "obj_extern\\S* \\(\\);" "optimized" } } 
*/
+/* { dg-final { scan-ltrans-tree-dump "obj_weak\\S* \\(\\);" "optimized" } } */
+/* { dg-final { scan-ltrans-tree-dump "obj_hidden\\S* \\(\\);" "optimized" } } 
*/
+/* { dg-final { scan-ltrans-tree-dump "_\\d+ \\(\\);" "optimized" } } */
+/* { dg-final { scan-ltrans-tree-dump "  call_lto_extern \\(\\);" "optimized" 
} } */
+/* { dg-final { scan-ltrans-tree-dump "  call_lto_weak \\(\\);" "optimized" } 
} */
+/* { dg-final { scan-ltrans-tree-dump "  call_lto_hidden \\(\\);" "optimized" 
} } */
+/* { dg-final { scan-ltrans-tree-dump "  call_obj_extern \\(\\);" "optimized" 
} } */
+/* { dg-final { scan-ltrans-tree-dump "  call_obj_weak \\(\\);" "optimized" } 
} */
+/* { dg-final { scan-ltrans-tree-dump "  call_obj_hidden \\(\\);" "optimized" 
} } */
+/* { dg-final { scan-ltrans-tree-dump "  call_indirect \\(\\);" "optimized" } 
} */
+/* { dg-final { scan-ltrans-tree-dump "  hidden_used_by_obj \\(\\);" 
"optimized" } } */
+/* { dg-final { scan-ltrans-tree-dump "  hidden_unused_by_obj \\(\\);" 
"optimized" } } */
diff --git a/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_1.c 
b/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_1.c
new file mode 100644
index 000000000000..32de705f3c66
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_1.c
@@ -0,0 +1,27 @@
+/* Part of the LTO set, see nullptr-lto-1_0.c.  */
+
+#include <stddef.h>
+
+[[gnu::noinline]]
+nullptr_t
+lto_extern (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline, gnu::weak]]
+nullptr_t
+lto_weak (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline, gnu::visibility ("hidden")]]
+nullptr_t
+lto_hidden (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
diff --git a/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_2.c 
b/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_2.c
new file mode 100644
index 000000000000..2e417fee9f8b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/lto/nullptr-lto-1_2.c
@@ -0,0 +1,34 @@
+/* { dg-options "-fno-lto -Wno-psabi" } */
+
+/* Built without LTO, see nullptr-lto-1_0.c.  Disable psABI diagnostics because
+   the test harness cannot treat them correctly.  */
+
+#include <stddef.h>
+
+[[gnu::noinline]]
+nullptr_t
+obj_extern (void)
+{
+  return nullptr;
+}
+
+[[gnu::noinline, gnu::weak]]
+nullptr_t
+obj_weak (void)
+{
+  return nullptr;
+}
+
+[[gnu::noinline, gnu::visibility ("hidden")]]
+nullptr_t
+obj_hidden (void)
+{
+  return nullptr;
+}
+
+nullptr_t (*volatile obj_fn_ptr) (void) = obj_extern;
+
+/* Reference a function from the LTO set which has hidden visibility.  */
+
+[[gnu::visibility ("hidden")]] extern nullptr_t hidden_used_by_obj (void);
+nullptr_t (*volatile ref_hidden_used_by_obj) (void) = hidden_used_by_obj;
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-1.c 
b/gcc/testsuite/gcc.target/s390/nullptr-1.c
new file mode 100644
index 000000000000..0654b11de483
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-1.c
@@ -0,0 +1,112 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23 -Wno-psabi" } */
+/* { dg-final { check-function-bodies "**" "" "" } } */
+
+/* ABI tests for passing/returning values of type nullptr_t.  */
+
+#include <stddef.h>
+
+/*
+** test_ret_callee:
+**     lghi    %r2,0
+**     br      %r14
+*/
+[[gnu::noinline]]
+nullptr_t
+test_ret_callee (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+/*
+** test_ret_caller:
+**     ...
+**     brasl   %r14,test_ret_callee@PLT
+**     ...
+**     stg     %r2,160\(%r15\)
+**     ...
+*/
+void
+test_ret_caller (void)
+{
+  volatile nullptr_t x = test_ret_callee ();
+}
+
+/*
+** test_arg_1_call:
+**     ...
+**     lghi    %r2,0
+**     ...
+*/
+extern void test_arg_1 (nullptr_t);
+void
+test_arg_1_call (void)
+{
+  test_arg_1 (nullptr);
+}
+
+/*
+** test_arg_2_call:
+**     ...
+**     lghi    %r3,0
+**     ...
+*/
+extern void test_arg_2 (long, nullptr_t);
+void
+test_arg_2_call (void)
+{
+  test_arg_2 (42, nullptr);
+}
+
+/*
+** test_arg_3_call:
+**     ...
+**     lghi    %r4,0
+**     ...
+*/
+extern void test_arg_3 (long, long, nullptr_t);
+void
+test_arg_3_call (void)
+{
+  test_arg_3 (42, 42, nullptr);
+}
+
+/*
+** test_arg_4_call:
+**     ...
+**     lghi    %r5,0
+**     ...
+*/
+extern void test_arg_4 (long, long, long, nullptr_t);
+void
+test_arg_4_call (void)
+{
+  test_arg_4 (42, 42, 42, nullptr);
+}
+
+/*
+** test_arg_5_call:
+**     ...
+**     lghi    %r6,0
+**     ...
+*/
+extern void test_arg_5 (long, long, long, long, nullptr_t);
+void
+test_arg_5_call (void)
+{
+  test_arg_5 (42, 42, 42, 42, nullptr);
+}
+
+/*
+** test_arg_6_call:
+**     ...
+**     (mvghi  160\(%r15\),0|stg       %r[0-9]+,160\(%r15\))
+**     ...
+*/
+extern void test_arg_6 (long, long, long, long, long, nullptr_t);
+void
+test_arg_6_call (void)
+{
+  test_arg_6 (42, 42, 42, 42, 42, nullptr);
+}
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-2.c 
b/gcc/testsuite/gcc.target/s390/nullptr-2.c
new file mode 100644
index 000000000000..8583ed54870b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-2.c
@@ -0,0 +1,82 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23 -Wpsabi" } */
+
+/* Check that -Wpsabi diagnoses the changed ABI for returning nullptr_t and
+   that the diagnostic is emitted only once per function.  Notes are emitted at
+   call sites or for function definitions with external linkage or internal
+   linkage where the address is taken.  */
+
+#include <stddef.h>
+
+[[gnu::noinline]]
+nullptr_t
+fun_extern_same_tu (void) /* { dg-note "the ABI for returning 'nullptr_t' 
changed in GCC 17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline, gnu::weak]]
+nullptr_t
+fun_extern_same_tu_weak (void) /* { dg-note "the ABI for returning 'nullptr_t' 
changed in GCC 17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+static nullptr_t
+fun_static_unused (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline]]
+static nullptr_t
+fun_static (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline]]
+static nullptr_t
+fun_static_addrtaken (void) /* { dg-note "the ABI for returning 'nullptr_t' 
changed in GCC 17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+nullptr_t (*fn_ptr) (void) = fun_static_addrtaken;
+
+void
+test_internal_call (void)
+{
+  fun_extern_same_tu ();
+  fun_static ();
+  fun_static_addrtaken ();
+}
+
+void
+test_extern_same_tu_weak_call (void)
+{
+  fun_extern_same_tu_weak (); /* { dg-note "the ABI for returning 'nullptr_t' 
changed in GCC 17.1" } */
+}
+
+void
+test_indirect_call (void)
+{
+  /* Variable fn_ptr may have changed and we might no longer call
+     fun_static_addrtaken.  */
+  fn_ptr ();  /* { dg-note "the ABI for returning 'nullptr_t' changed in GCC 
17.1" } */
+}
+
+extern nullptr_t foo (void);
+extern nullptr_t bar (void);
+
+void
+test_one_note_per_function (void)
+{
+  foo (); /* { dg-note "the ABI for returning 'nullptr_t' changed in GCC 17.1" 
} */
+  foo ();
+  bar ();
+}
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-3.c 
b/gcc/testsuite/gcc.target/s390/nullptr-3.c
new file mode 100644
index 000000000000..5e4282135783
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-3.c
@@ -0,0 +1,25 @@
+/* { dg-do link } */
+/* { dg-require-effective-target lto } */
+/* { dg-options "-O2 -std=c23 -Wpsabi -flto -flto-partition=max" } */
+/* { dg-bogus "the ABI for returning" "" { target *-*-* } 0 } */
+
+/* Check that no diagnostic about the changed ABI for returning nullptr_t is
+   emitted for functions with internal linkage when using LTO.  Partitioning
+   may promote functions to be public in order to reference them across
+   partitions, which must not be mistaken for external linkage.  */
+
+#include <stddef.h>
+
+[[gnu::noinline]]
+static nullptr_t
+fun_static (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+int
+main (void)
+{
+  fun_static ();
+}
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-4.c 
b/gcc/testsuite/gcc.target/s390/nullptr-4.c
new file mode 100644
index 000000000000..758fee62f237
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-4.c
@@ -0,0 +1,44 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23 -Wpsabi -fPIC" } */
+
+/* Check that -Wpsabi diagnoses calls to functions defined in the same TU if
+   the callee may be interposed at link time.  */
+
+#include <stddef.h>
+
+[[gnu::noinline]]
+nullptr_t
+fun_extern (void) /* { dg-note "the ABI for returning 'nullptr_t' changed in 
GCC 17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline, gnu::visibility ("hidden")]]
+nullptr_t
+fun_hidden (void) /* { dg-note "the ABI for returning 'nullptr_t' changed in 
GCC 17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline]]
+static nullptr_t
+fun_static (void)
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+void
+test_interposable_call (void)
+{
+  fun_extern (); /* { dg-note "the ABI for returning 'nullptr_t' changed in 
GCC 17.1" } */
+}
+
+void
+test_noninterposable_call (void)
+{
+  fun_hidden ();
+  fun_static ();
+}
diff --git a/gcc/testsuite/gcc.target/s390/nullptr-5.c 
b/gcc/testsuite/gcc.target/s390/nullptr-5.c
new file mode 100644
index 000000000000..a918a769f4e4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/nullptr-5.c
@@ -0,0 +1,39 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c23 -Wpsabi -fPIC -fno-semantic-interposition" } */
+
+/* Check -Wpsabi for calls to functions defined in the same TU when compiling
+   with -fno-semantic-interposition.  The user promises that functions are not
+   interposed at link time.  Therefore, do not diagnose calls to public
+   functions.  Weak functions may still be replaced at link time, e.g., by a
+   definition built by an older GCC, and therefore calls to them are diagnosed.
+   */
+
+#include <stddef.h>
+
+[[gnu::noinline]]
+nullptr_t
+fun_extern (void) /* { dg-note "the ABI for returning 'nullptr_t' changed in 
GCC 17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+[[gnu::noinline, gnu::weak]]
+nullptr_t
+fun_weak (void) /* { dg-note "the ABI for returning 'nullptr_t' changed in GCC 
17.1" } */
+{
+  __asm__ volatile ("");
+  return nullptr;
+}
+
+void
+test_call (void)
+{
+  fun_extern ();
+}
+
+void
+test_weak_call (void)
+{
+  fun_weak (); /* { dg-note "the ABI for returning 'nullptr_t' changed in GCC 
17.1" } */
+}

Reply via email to