[v2] ld: Add an LTO test for common symbol override

Message ID 20240814152915.104648-1-hjl.tools@gmail.com
State Committed
Headers
Series [v2] ld: Add an LTO test for common symbol override |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm warning Patch is already merged
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 warning Patch is already merged

Commit Message

H.J. Lu Aug. 14, 2024, 3:29 p.m. UTC
  Linker checks if a symbol in an archive member is a real definition, not
common, before including the archive member in the link output so that
only a real definition in archive will override the common symbol in
object file.  Add an LTO test to verify that a real definition in archive
overrides the common symbol in object file.

	* testsuite/ld-plugin/common-1.c: New file.
	* testsuite/ld-plugin/definition-1.c: Likewise.
	* testsuite/ld-plugin/lto.exp: Run common tests.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 ld/testsuite/ld-plugin/common-1.c     | 11 +++++++++++
 ld/testsuite/ld-plugin/definition-1.c |  1 +
 ld/testsuite/ld-plugin/lto.exp        | 18 ++++++++++++++++++
 3 files changed, 30 insertions(+)
 create mode 100644 ld/testsuite/ld-plugin/common-1.c
 create mode 100644 ld/testsuite/ld-plugin/definition-1.c
  

Comments

H.J. Lu Aug. 14, 2024, 3:39 p.m. UTC | #1
On Wed, Aug 14, 2024 at 8:29 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Linker checks if a symbol in an archive member is a real definition, not
> common, before including the archive member in the link output so that
> only a real definition in archive will override the common symbol in
> object file.  Add an LTO test to verify that a real definition in archive
> overrides the common symbol in object file.
>
>         * testsuite/ld-plugin/common-1.c: New file.
>         * testsuite/ld-plugin/definition-1.c: Likewise.
>         * testsuite/ld-plugin/lto.exp: Run common tests.
>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  ld/testsuite/ld-plugin/common-1.c     | 11 +++++++++++
>  ld/testsuite/ld-plugin/definition-1.c |  1 +
>  ld/testsuite/ld-plugin/lto.exp        | 18 ++++++++++++++++++
>  3 files changed, 30 insertions(+)
>  create mode 100644 ld/testsuite/ld-plugin/common-1.c
>  create mode 100644 ld/testsuite/ld-plugin/definition-1.c
>
> diff --git a/ld/testsuite/ld-plugin/common-1.c b/ld/testsuite/ld-plugin/common-1.c
> new file mode 100644
> index 00000000000..b4f616aeea2
> --- /dev/null
> +++ b/ld/testsuite/ld-plugin/common-1.c
> @@ -0,0 +1,11 @@
> +#include <stdio.h>
> +
> +int foo;
> +
> +int main()
> +{
> +  if (foo == 1)
> +    printf ("PASS\n");
> +
> +  return 0;
> +}
> diff --git a/ld/testsuite/ld-plugin/definition-1.c b/ld/testsuite/ld-plugin/definition-1.c
> new file mode 100644
> index 00000000000..a244740a3a5
> --- /dev/null
> +++ b/ld/testsuite/ld-plugin/definition-1.c
> @@ -0,0 +1 @@
> +int foo = 1;
> diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
> index 1a8c3736307..bbd68407edc 100644
> --- a/ld/testsuite/ld-plugin/lto.exp
> +++ b/ld/testsuite/ld-plugin/lto.exp
> @@ -231,6 +231,13 @@ set lto_link_tests [list \
>     "$plug_opt" "-flto $lto_no_fat -fcommon" \
>     {pr26389.c} \
>     [list [list "nm" "$plug_opt" "pr26389.d"]]] \
> +  [list "Build libdefinition-1.a" \
> +   "$plug_opt" \
> +   "-O2 -fcommon -flto $lto_no_fat" \
> +   {definition-1.c} \
> +   {} \
> +   "libdefinition-1.a" \
> +  ] \
>  ]
>
>  if { [at_least_gcc_version 10 0] && [check_lto_shared_available] } {
> @@ -763,6 +770,17 @@ set lto_run_tests [list \
>     {pr26262a.c} "pr26262b" "pass.out" \
>     "-flto -O2" "c" "" \
>     ""] \
> +  [list "Run common-1" \
> +   "-O2 -flto" \
> +   "" \
> +   {common-1.c} \
> +   "common-1" \
> +   "pass.out" \
> +   "-O2 -fcommon -flto $lto_no_fat" \
> +   "c" \
> +   "" \
> +   "tmpdir/libdefinition-1.a" \
> +  ] \
>  ]
>
>  if { [at_least_gcc_version 4 7] } {
> --
> 2.46.0
>

I am checking it in.
  

Patch

diff --git a/ld/testsuite/ld-plugin/common-1.c b/ld/testsuite/ld-plugin/common-1.c
new file mode 100644
index 00000000000..b4f616aeea2
--- /dev/null
+++ b/ld/testsuite/ld-plugin/common-1.c
@@ -0,0 +1,11 @@ 
+#include <stdio.h>
+
+int foo;
+
+int main()
+{
+  if (foo == 1)
+    printf ("PASS\n");
+
+  return 0;
+}
diff --git a/ld/testsuite/ld-plugin/definition-1.c b/ld/testsuite/ld-plugin/definition-1.c
new file mode 100644
index 00000000000..a244740a3a5
--- /dev/null
+++ b/ld/testsuite/ld-plugin/definition-1.c
@@ -0,0 +1 @@ 
+int foo = 1;
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index 1a8c3736307..bbd68407edc 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -231,6 +231,13 @@  set lto_link_tests [list \
    "$plug_opt" "-flto $lto_no_fat -fcommon" \
    {pr26389.c} \
    [list [list "nm" "$plug_opt" "pr26389.d"]]] \
+  [list "Build libdefinition-1.a" \
+   "$plug_opt" \
+   "-O2 -fcommon -flto $lto_no_fat" \
+   {definition-1.c} \
+   {} \
+   "libdefinition-1.a" \
+  ] \
 ]
 
 if { [at_least_gcc_version 10 0] && [check_lto_shared_available] } {
@@ -763,6 +770,17 @@  set lto_run_tests [list \
    {pr26262a.c} "pr26262b" "pass.out" \
    "-flto -O2" "c" "" \
    ""] \
+  [list "Run common-1" \
+   "-O2 -flto" \
+   "" \
+   {common-1.c} \
+   "common-1" \
+   "pass.out" \
+   "-O2 -fcommon -flto $lto_no_fat" \
+   "c" \
+   "" \
+   "tmpdir/libdefinition-1.a" \
+  ] \
 ]
 
 if { [at_least_gcc_version 4 7] } {