Bug 104248 - armel: C11 atomics requires to be linked with libatomic.a explicitly
Summary: armel: C11 atomics requires to be linked with libatomic.a explicitly
Status: RESOLVED DUPLICATE of bug 81358
Alias: None
Product: gcc
Classification: Unclassified
Component: driver (show other bugs)
Version: 11.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-26 16:43 UTC by Mathieu Malaterre
Modified: 2022-01-26 17:28 UTC (History)
0 users

See Also:
Host:
Target: arm-linux-gnueabi
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mathieu Malaterre 2022-01-26 16:43:53 UTC
Consider the following c11 code (lib + executable):

```
::::::::::::::
foo.c
::::::::::::::
_Atomic(long long) ll;

int foo(void)
{
        ++ll;
        return 42;
}
::::::::::::::
prog.c
::::::::::::::
int foo(void);

int main(int argc, char* argv[])
{
  return foo();
}
```

On Debian armel arch, most built system will fail to compile the above. The error reported is:

```
/usr/bin/ld: libfoo.a(foo.c.o): in function `foo':
foo.c:(.text+0x40): undefined reference to `__atomic_fetch_add_8'
```

One need to carefully pass `atomic` library on the compilation line. So if you are lucky this is just:

LDFLAGS=-latomic ...

some other time this more complex as in this above case where a static library is build, thus one needs to:

```
cc -rdynamic prog.o -o prog libfoo.a -Wl,-Bstatic -latomic -Wl,-Bdynamic
```

---

I see that on some other arch (riscv), the spec file for gcc has been updated to pass automatically (?) the proper flag: `--as-needed -latomic` [1]

Could someone from gcc/arm team please describe what is the reason for not doing something equivalent for the spec file in armel case ? Thanks for your time.

[1] https://github.com/riscv-collab/riscv-gcc/issues/12#issuecomment-276587351
Comment 1 Mathieu Malaterre 2022-01-26 16:48:15 UTC
In case that help, cmake instructions for the test case is:

```
project(p C)
set(CMAKE_C_STANDARD 11)
add_library(foo STATIC foo.c)
add_executable(prog prog.c)
target_link_libraries(prog foo)
```
Comment 2 Andreas Schwab 2022-01-26 17:02:12 UTC
This is a general problem with architectures not implementing all atomic operations inline.
Comment 3 Mathieu Malaterre 2022-01-26 17:21:29 UTC
@Andreas the specific issue I am raising is the difference between the approach of riscv vs armel. The riscv team is taking the responsability for putting the missing `-latomic`, while armel expect the user to understand how c11 atomics are implemented on a particular architecture and fix the link line at the build system level (eg. cmake / meson ...).

In summary: what is the risk (if any) to update the gcc spec file on armel ?
Comment 4 Andrew Pinski 2022-01-26 17:28:05 UTC
Dup of bug 81358.

Note riscv atomics are messed up too(there is a bug report for that).

*** This bug has been marked as a duplicate of bug 81358 ***