回复: [PATCH] LoongArch: Use medium cmodel build libc_nonshared.a.

Message ID 6386FE6D.025D62.50083@loongson.cn
State Changes Requested, archived
Headers
Series 回复: [PATCH] LoongArch: Use medium cmodel build libc_nonshared.a. |

Commit Message

caiyinyu Nov. 30, 2022, 6:55 a.m. UTC
  Sorry for the delay.

+# All the objects in lib*_nonshared.a need to be compiled with medium code
+# model or large applications may fail to link.
+CFLAGS-.oS += -mcmodel=medium

We just need add -mcmodel=medium to four files not all.

Here are my patches: 



从 Windows 版邮件发送

发件人: Xi Ruoyao
发送时间: 2022年11月23日 19:18
收件人: Florian Weimer; caiyinyu
抄送: adhemerval.zanella@linaro.org; libc-alpha@sourceware.org; i.swmail@xen0n.name; xuchenghua@loongson.cn
主题: Re: [PATCH] LoongArch: Use medium cmodel build libc_nonshared.a.

On Wed, 2022-11-23 at 11:31 +0100, Florian Weimer wrote:
> > From: Chenghua Xu <xuchenghua@loongson.cn>
> > 
> > This patch is used to fix address out-of-bounds error when building
> > firefox.
> 
> The .c files shouldn't be necessary.  The CFLAGS additions should
> perhaps be made subdirectory-specific.  And I think these days,
> libc_nonshared.a is much, much small (and we could size it down even
> further).

In my build libc_nonshared.a only contains:

- at_quick_exit.oS
- atexit.oS
- pthread_atfork.oS
- stack_chk_fail_local.oS

> But shouldn't the medium model be the GCC default if it is required for
> building applications?

Small code model can support a 128MiB .text section, and most
applications have a .text section < 128MiB.  But Firefox libxul.so can
be extremely large.

It's not worthy to pay the cost everywhere just for satisfying a monster
application.
  

Comments

caiyinyu Nov. 30, 2022, 8:40 a.m. UTC | #1
OK, I will use CFLAGS-.oS += -mcmodel=medium and have-cmodel-medium.

从 Windows 版邮件发送

发件人: Xi Ruoyao
发送时间: 2022年11月30日 15:34
收件人: caiyinyu@loongson.cn; Florian Weimer
抄送: adhemerval.zanella@linaro.org; libc-alpha@sourceware.org; i.swmail@xen0n.name; xuchenghua@loongson.cn
主题: Re: 回复: [PATCH] LoongArch: Use mediumcmodel build libc_nonshared.a.

On Wed, 2022-11-30 at 14:55 +0800, caiyinyu@loongson.cn wrote:
> +# All the objects in lib*_nonshared.a need to be compiled with medium
> code
> +# model or large applications may fail to link.
> +CFLAGS-.oS += -mcmodel=medium
>  
> We just need add -mcmodel=medium to four files not all.

I still prefer "all", to prevent subtle breakages if someone adds a file
into or removes a file from libc_nonshared.a.  Currently other .oS files
are in libsupport_nonshared.a which is used by the test suite and not
installed.

/* snip */

> +LIBC_CONFIG_VAR([enable_cmodel_medium],

It looks like we are using "-" instead of "_" in the first parameter of
LIBC_CONFIG_VAR calls everywhere.  Note that it's a variable name in a
Makefile, not a shell script.

And I'd remove the "enable" prefix: this prefix makes the variable looks
like "something can be turned on/off by `--{enable,disable}-cmodel-
medium'" but it's not true.

So I think "cmodel-medium" or "have-cmodel-medium" would be a better
name.

> [$libc_cv_loongarch_cmodel_medium])
  

Patch

diff --git a/sysdeps/loongarch/Makefile b/sysdeps/loongarch/Makefile
index 746d4c2c8f..be7fb38714 100644
--- a/sysdeps/loongarch/Makefile
+++ b/sysdeps/loongarch/Makefile
@@ -5,3 +5,10 @@  endif
 # LoongArch's assembler also needs to know about PIC as it changes the
 # definition of some assembler macros.
 ASFLAGS-.os += $(pic-ccflag)
+
+ifeq (yes,$(enable_cmodel_medium))
+CFLAGS-atexit.oS += -mcmodel=medium
+CFLAGS-at_quick_exit.oS += -mcmodel=medium
+CFLAGS-pthread_atfork.oS += -mcmodel=medium
+CFLAGS-stack_chk_fail_local.oS += -mcmodel=medium
+endif
diff --git a/sysdeps/loongarch/configure b/sysdeps/loongarch/configure
index 3046915ce2..6372449ea2 100644
--- a/sysdeps/loongarch/configure
+++ b/sysdeps/loongarch/configure
@@ -72,3 +72,25 @@  if test "$libc_cv_static_pie_on_loongarch" = yes; then
   $as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h

 fi
+
+  # Check if gcc supports option -mcmodel=medium.
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC supports option -mcmodel=medium" >&5
+$as_echo_n "checking whether $CC supports option -mcmodel=medium... " >&6; }
+if ${libc_cv_loongarch_cmodel_medium+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+    if { ac_try='${CC-cc} -c $CFLAGS -mcmodel=medium /dev/null 1>&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }; then
+    libc_cv_loongarch_cmodel_medium=yes
+  else
+    libc_cv_loongarch_cmodel_medium=no
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_loongarch_cmodel_medium" >&5
+$as_echo "$libc_cv_loongarch_cmodel_medium" >&6; }
+config_vars="$config_vars
+enable_cmodel_medium = $libc_cv_loongarch_cmodel_medium"
diff --git a/sysdeps/loongarch/configure.ac b/sysdeps/loongarch/configure.ac
index 06dd408ad9..7170dc7d59 100644
--- a/sysdeps/loongarch/configure.ac
+++ b/sysdeps/loongarch/configure.ac
@@ -51,3 +51,13 @@  EOF
 if test "$libc_cv_static_pie_on_loongarch" = yes; then
   AC_DEFINE(SUPPORT_STATIC_PIE)
 fi
+
+  # Check if gcc supports option -mcmodel=medium.
+AC_CACHE_CHECK(whether $CC supports option -mcmodel=medium,
+              libc_cv_loongarch_cmodel_medium, [dnl
+  if AC_TRY_COMMAND(${CC-cc} -c $CFLAGS -mcmodel=medium /dev/null 1>&AS_MESSAGE_LOG_FD); then
+    libc_cv_loongarch_cmodel_medium=yes
+  else
+    libc_cv_loongarch_cmodel_medium=no
+  fi])
+LIBC_CONFIG_VAR([enable_cmodel_medium], [$libc_cv_loongarch_cmodel_medium])