[PATCH] ld: testsuite: Associate C identifiers with the corresponding assembler or linker symbols in a target-independent manner.
Jan Dubiec <[email protected]> Sat, 18 Jul 2026 09:52:54 +0200
| Newsgroups | gmane.comp.gnu.binutils |
|---|---|
| Message-ID | <[email protected]> |
Some targets prepend an underscore to C symbol names, while others do
not (e.g. H8 vs. ARM). This can cause problems in two situations, as
shown in the log below:
1. when linking C and assembly code;
2. when a symbol name is specified on the linker's command line.
This patch resolves both issues by using the asm keyword to associate C
identifiers with the corresponding assembler/linker symbol names in a
target-independent manner.
[...]
h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-elf -g -O2 -fno-sanitize=all -c -fno-lto -c /mnt/Works/binutils/ld/testsuite/ld-elf/merge4a.c -o tmpdir/merge4a.o
Executing on host: sh -c {h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-elf -g -O2 -fno-sanitize=all -c -fno-lto -c /mnt/Works/binutils/ld/testsuite/ld-elf/merge4a.c -o tmpdir/merge4a.o 2>&1} /dev/null ld.tmp (timeout = 300)
spawn [open ...]
h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-elf -g -O2 -fno-sanitize=all -c -fno-lto -c /mnt/Works/binutils/ld/testsuite/ld-elf/merge4b.s -o tmpdir/merge4b.o
Executing on host: sh -c {h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-elf -g -O2 -fno-sanitize=all -c -fno-lto -c /mnt/Works/binutils/ld/testsuite/ld-elf/merge4b.s -o tmpdir/merge4b.o 2>&1} /dev/null ld.tmp (timeout = 300)
spawn [open ...]
h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -L/usr/local/h8300-elf/lib -L/mnt/Works/xcomp/build-binutils-h8300-linux/ld -o tmpdir/merge4 -L/mnt/Works/binutils/ld/testsuite/ld-elf tmpdir/merge4a.o tmpdir/merge4b.o
Executing on host: sh -c {h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -L/usr/local/h8300-elf/lib -L/mnt/Works/xcomp/build-binutils-h8300-linux/ld -o tmpdir/merge4 -L/mnt/Works/binutils/ld/testsuite/ld-elf tmpdir/merge4a.o tmpdir/merge4b.o 2>&1} /dev/null ld.tmp (timeout = 300)
spawn [open ...]
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: warning: /mnt/Works/xcomp/sysroot-linux/bin/../lib/gcc/h8300-elf/17.0.0/crtn.o: missing .note.GNU-stack section implies executable stack
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: tmpdir/merge4a.o: in function `main':
/mnt/Works/binutils/ld/testsuite/ld-elf/merge4a.c:19:(.text.startup+0x4): undefined reference to `addr_of_str'
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: /mnt/Works/binutils/ld/testsuite/ld-elf/merge4a.c:20:(.text.startup+0x1e): undefined reference to `addr_of_str2'
collect2: error: ld returned 1 exit status
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: warning: /mnt/Works/xcomp/sysroot-linux/bin/../lib/gcc/h8300-elf/17.0.0/crtn.o: missing .note.GNU-stack section implies executable stack
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: tmpdir/merge4a.o: in function `main':
/mnt/Works/binutils/ld/testsuite/ld-elf/merge4a.c:19:(.text.startup+0x4): undefined reference to `addr_of_str'
/mnt/Works/xcomp/sysroot-linux/h8300-elf/bin/ld: /mnt/Works/binutils/ld/testsuite/ld-elf/merge4a.c:20:(.text.startup+0x1e): undefined reference to `addr_of_str2'
collect2: error: ld returned 1 exit status
FAIL: merge4
[...]
[...]
h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-gc -g -O2 -fno-sanitize=all -ffunction-sections -fdata-sections -fno-sanitize=all -fno-lto -c /mnt/Works/binutils/ld/testsuite/ld-gc/pr13683.c -o tmpdir/pr13683.o
Executing on host: sh -c {h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-gc -g -O2 -fno-sanitize=all -ffunction-sections -fdata-sections -fno-sanitize=all -fno-lto -c /mnt/Works/binutils/ld/testsuite/ld-gc/pr13683.c -o tmpdir/pr13683.o 2>&1} /dev/null ld.tmp (timeout = 300)
spawn [open ...]
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/../gas/as-new --defsym UNDERSCORE=1 -o tmpdir/dummy.o /mnt/Works/binutils/ld/testsuite/ld-gc/dummy.s
Executing on host: sh -c {/mnt/Works/xcomp/build-binutils-h8300-linux/ld/../gas/as-new --defsym UNDERSCORE=1 -o tmpdir/dummy.o /mnt/Works/binutils/ld/testsuite/ld-gc/dummy.s 2>&1} /dev/null dump.tmp (timeout = 300)
spawn [open ...]
./ld-new -L/mnt/Works/binutils/ld/testsuite/ld-gc --gc-sections -e main --defsym foo=foo2 tmpdir/pr13683.o -o tmpdir/dump tmpdir/dummy.o
Executing on host: sh -c {./ld-new -L/mnt/Works/binutils/ld/testsuite/ld-gc --gc-sections -e main --defsym foo=foo2 tmpdir/pr13683.o -o tmpdir/dump tmpdir/dummy.o 2>&1} /dev/null dump.tmp (timeout = 300)
spawn [open ...]
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new: warning: tmpdir/pr13683.o: missing .note.GNU-stack section implies executable stack
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new:--defsym:1: undefined symbol `foo2' referenced in expression
failed with: </mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new:--defsym:1: undefined symbol `foo2' referenced in expression>, no expected output
FAIL: --gc-sections with --defsym
[...]
[...]
h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-gc -g -O2 -fno-sanitize=all -ffunction-sections -fdata-sections -fno-sanitize=all -fno-lto -O0 -c /mnt/Works/binutils/ld/testsuite/ld-gc/pr19161-1.c -o tmpdir/pr19161-1.o
Executing on host: sh -c {h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-gc -g -O2 -fno-sanitize=all -ffunction-sections -fdata-sections -fno-sanitize=all -fno-lto -O0 -c /mnt/Works/binutils/ld/testsuite/ld-gc/pr19161-1.c -o tmpdir/pr19161-1.o 2>&1} /dev/null ld.tmp (timeout = 300)
spawn [open ...]
h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-gc -g -O2 -fno-sanitize=all -ffunction-sections -fdata-sections -fno-sanitize=all -fno-lto -O0 -c /mnt/Works/binutils/ld/testsuite/ld-gc/pr19161-2.c -o tmpdir/pr19161-2.o
Executing on host: sh -c {h8300-elf-cc -B/mnt/Works/xcomp/build-binutils-h8300-linux/ld/tmpdir/ld/ -I/mnt/Works/binutils/ld/testsuite/ld-gc -g -O2 -fno-sanitize=all -ffunction-sections -fdata-sections -fno-sanitize=all -fno-lto -O0 -c /mnt/Works/binutils/ld/testsuite/ld-gc/pr19161-2.c -o tmpdir/pr19161-2.o 2>&1} /dev/null ld.tmp (timeout = 300)
spawn [open ...]
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/../gas/as-new --defsym UNDERSCORE=1 -o tmpdir/dummy.o /mnt/Works/binutils/ld/testsuite/ld-gc/dummy.s
Executing on host: sh -c {/mnt/Works/xcomp/build-binutils-h8300-linux/ld/../gas/as-new --defsym UNDERSCORE=1 -o tmpdir/dummy.o /mnt/Works/binutils/ld/testsuite/ld-gc/dummy.s 2>&1} /dev/null dump.tmp (timeout = 300)
spawn [open ...]
./ld-new -L/mnt/Works/binutils/ld/testsuite/ld-gc --gc-sections -e main tmpdir/pr19161-1.o tmpdir/pr19161-2.o -o tmpdir/dump tmpdir/dummy.o
Executing on host: sh -c {./ld-new -L/mnt/Works/binutils/ld/testsuite/ld-gc --gc-sections -e main tmpdir/pr19161-1.o tmpdir/pr19161-2.o -o tmpdir/dump tmpdir/dummy.o 2>&1} /dev/null dump.tmp (timeout = 300)
spawn [open ...]
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new: warning: tmpdir/pr19161-2.o: missing .note.GNU-stack section implies executable stack
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
/mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new: warning: cannot find entry symbol main; defaulting to 00000100
succeeded with: </mnt/Works/xcomp/build-binutils-h8300-linux/ld/ld-new: warning: cannot find entry symbol main; defaulting to 00000100>, no expected output
FAIL: --gc-sections with __start_SECTIONNAME
[...]
Signed-off-by: Jan Dubiec <[email protected]>
---
ld/testsuite/ld-elf/merge4a.c | 4 ++--
ld/testsuite/ld-gc/pr13683.c | 4 +++-
ld/testsuite/ld-gc/pr19161-1.c | 1 +
3 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/ld/testsuite/ld-elf/merge4a.c b/ld/testsuite/ld-elf/merge4a.c
index 133303e11ac..d050c30702a 100644
--- a/ld/testsuite/ld-elf/merge4a.c
+++ b/ld/testsuite/ld-elf/merge4a.c
@@ -1,8 +1,8 @@
extern const char * getstr3(int);
extern int printf (const char *, ...);
-extern const char *addr_of_str;
-extern const char *addr_of_str2;
+extern const char *addr_of_str asm("addr_of_str");
+extern const char *addr_of_str2 asm("addr_of_str2");
/* "foobar" needs to be a string literal, so that it's put into
a mergable string section, then merged with the "foobar" from merge4b.s
diff --git a/ld/testsuite/ld-gc/pr13683.c b/ld/testsuite/ld-gc/pr13683.c
index c585e06db51..252642d0c60 100644
--- a/ld/testsuite/ld-gc/pr13683.c
+++ b/ld/testsuite/ld-gc/pr13683.c
@@ -1,4 +1,6 @@
-void foo(void);
+void foo(void) asm("foo");
+void foo2(void) asm("foo2");
+int main(void) asm("main");
int main(void)
{
diff --git a/ld/testsuite/ld-gc/pr19161-1.c b/ld/testsuite/ld-gc/pr19161-1.c
index 519948e9b46..0667ff64cf7 100644
--- a/ld/testsuite/ld-gc/pr19161-1.c
+++ b/ld/testsuite/ld-gc/pr19161-1.c
@@ -1,4 +1,5 @@
int (*p)(void);
+int main(void) asm("main");
int
main ()
--
2.54.0