public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: richard clark <richard.xnu.clark@gmail.com>
To: Xi Ruoyao <xry111@xry111.site>
Cc: Mark Rutland <mark.rutland@arm.com>,
	gcc-help@gcc.gnu.org,  linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org
Subject: Re: undefined reference to `__aarch64_cas4_sync' error on arm64 native build
Date: Tue, 9 Jan 2024 14:04:57 +0800	[thread overview]
Message-ID: <CAJNi4rOU_Mohtam0Zce=tymUy52W_ik4kezNsX-f8on76aX1Rw@mail.gmail.com> (raw)
In-Reply-To: <9aef98eed96ed32962ce90499291cb30ad5e3e14.camel@xry111.site>

On Tue, Jan 9, 2024 at 11:06 AM Xi Ruoyao <xry111@xry111.site> wrote:
>
> On Tue, 2024-01-09 at 10:55 +0800, richard clark wrote:
> > On Mon, Jan 8, 2024 at 6:56 PM Xi Ruoyao <xry111@xry111.site> wrote:
> > >
> > > On Mon, 2024-01-08 at 10:51 +0000, Mark Rutland via Gcc-help wrote:
> > > > > AFAIK, the native build for the kernel will not link to the libc.so
> > > > > but the userland application does, the builtin atomic primitives are
> > > > > implemented in the glibc:
> > > > > target-host $ objdump -t /lib/aarch64-linux-gnu/libc.so.6 | grep __aarch64_cas4
> > > > > 0000000000130950 l     F .text 0000000000000034 __aarch64_cas4_relax
> > > > > 0000000000130a10 l     F .text 0000000000000034 __aarch64_cas4_rel
> > > > > 0000000000130990 l     F .text 0000000000000034 __aarch64_cas4_acq
> > > > > seems the '__sync_val_compare_and_swap' used in the application will
> > > > > be renamed to _aarch64_cas4_{relax, rel, acq}. so the kernel will
> > > > > complain it will
> > > > > link to an 'undefined reference'. But interesting, why the
> > > > > cross-compile kernel will not generate the 'undefined reference', the
> > > > > cross-compile/build kernel will link to the glibc?
> > > >
> > > > This is due to a difference in default options between the two compilers; the
> > > > kernel isn't linked against libc in either case.
> > >
> > > And even if it's not the kernel but a normal application, it still
> > > cannot use these functions from Glibc as the objdump output contains
> > > "l", meaning these symbols are local symbols and they cannot referred
> > > somewhere out of the libc.so.6 itself.
> > Actually you can call those builtin atomic functions in you normal
> > application without link time error, even execute the output binary in
> > the target machine in case of cross-compile, only if the linked .so is
> > in your target environment.
>
> Because these functions are provided by libgcc.a:
>
> xry111@defiant:~$ objdump -t /usr/lib/gcc/aarch64-unknown-linux-gnu/13.2.0/libgcc.a | grep cas
> cas_1_1.o:     file format elf64-littleaarch64
[...]
> 0000000000000000 g     F .text  0000000000000038 .hidden __aarch64_cas1_sync
> cas_2_5.o:     file format elf64-littleaarch64
> 0000000000000000 g     F .text  0000000000000038 .hidden __aarch64_cas2_sync
> cas_4_5.o:     file format elf64-littleaarch64
[...]
>
> It seems libc.so.6 just get these functions from libgcc.a (a hidden
> global symbol becomes local when you link it into a shared library).

I think libgcc.a should be built with '-fvisibility=hidden' option and
hidden symbols, then as you said it will become a local sym when
linked with the libc.so.6.

> But the Linux kernel cannot use neither libc.so nor libgcc.a.  (I know
> some non-Linux kernel developers are overusing libgcc.a for kernels, but
> IMO this is just wrong and Linux developers also do not do this.  If the
> Linux kernel needs a symbol from libgcc the developers just provide
> their own implementation.)
Right, kernel should use its own implementation while not the one from
libc.so...
> --
> Xi Ruoyao <xry111@xry111.site>
> School of Aerospace Science and Technology, Xidian University

  reply	other threads:[~2024-01-09  6:05 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-02  3:29 richard clark
2024-01-02  8:53 ` richard clark
2024-01-04 18:18   ` Mark Rutland
2024-01-08  1:28     ` richard clark
2024-01-08 10:51       ` Mark Rutland
2024-01-08 10:56         ` Xi Ruoyao
2024-01-09  2:55           ` richard clark
2024-01-09  3:05             ` Xi Ruoyao
2024-01-09  6:04               ` richard clark [this message]
2024-01-09  7:48               ` Segher Boessenkool
2024-01-09  8:09                 ` Xi Ruoyao
2024-01-09  8:26                   ` Segher Boessenkool
2024-01-10  5:59                     ` richard clark
2024-01-10 14:10                       ` Segher Boessenkool
2024-01-11  1:42                         ` richard clark
2024-01-11 11:26                           ` Mark Rutland
2024-01-15  9:23                             ` richard clark
2024-01-09  2:52         ` richard clark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJNi4rOU_Mohtam0Zce=tymUy52W_ik4kezNsX-f8on76aX1Rw@mail.gmail.com' \
    --to=richard.xnu.clark@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=xry111@xry111.site \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).