libbsd: Branch 'main'
GitLab Mirror
gitlab-mirror at kemper.freedesktop.org
Thu Mar 14 08:34:44 UTC 2024
test/nlist.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
New commits:
commit b62f36f3055262faf28e4ff12dd7ed03c44e09d8
Author: Sam James <sam at gentoo.org>
Date: Wed Mar 13 23:34:55 2024 +0000
test: nlist: make resilient against -Wl,--gc-sections
With linker sections GC enabled, we get a test failure in `nlist.c`:
```
nlist: nlist.c:72: main: Assertion `rc == 0' failed.
```
This turns out to be because several sections used by the test can be discarded:
```
ld: removing unused section '.text.func_pub' in file 'nlist.o'
ld: removing unused section '.bss.data_pub_uninit' in file 'nlist.o'
ld: removing unused section '.data.data_pub_init' in file 'nlist.o'
```
Reproduced with `CFLAGS="-Og -fdata-sections -ffunction-sections"` and
`LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,--gc-sections -Wl,-z,start-stop-gc"`.
Additionally, `LDFLAGS="... -Wl,--print-gc-sections"` can help with diagnosing
which sections get removed.
We already mark these symbols as `used`, but we need `retain` [0] for them
to survive linker GC too.
[0] https://releases.llvm.org/18.1.0/tools/lld/docs/ELF/start-stop-gc.html#annotate-c-identifier-name-sections
Closes: !29
Signed-off-by: Sam James <sam at gentoo.org>
Signed-off-by: Guillem Jover <guillem at hadrons.org>
diff --git a/test/nlist.c b/test/nlist.c
index 558524f..0031a4d 100644
--- a/test/nlist.c
+++ b/test/nlist.c
@@ -33,17 +33,29 @@
#include <fcntl.h>
#include <nlist.h>
+#if defined(__has_attribute)
+#if __has_attribute(__retain__)
+#define ATTRIBUTE_RETAIN __attribute__((__retain__))
+#else
+#define ATTRIBUTE_RETAIN
+#endif
+#endif
+
static int data_prv_init = 50;
extern int data_pub_init;
extern int data_pub_uninit[2048];
extern int *data_pub_ptr;
+/* GCC's __has_attribute has odd behavior for retain: https://gcc.gnu.org/PR99587 */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wattributes"
int *data_pub_ptr = &data_prv_init;
-int data_pub_init __attribute__((__used__)) = 10;
-int data_pub_uninit[2048] __attribute__((__used__));
+int data_pub_init ATTRIBUTE_RETAIN __attribute__((__used__)) = 10;
+int data_pub_uninit[2048] ATTRIBUTE_RETAIN __attribute__((__used__));
extern int
-func_pub(void) __attribute__((__used__)) ;
+func_pub(void) ATTRIBUTE_RETAIN __attribute__((__used__));
+#pragma GCC diagnostic pop
int
func_pub(void)
More information about the libbsd
mailing list