Skip to contentSkip to conversation

arm64: fix clang integrated assembler build

PATCH In review Arnd Bergmann 3 participants 3 messages May 27, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: fix clang integrated assembler build

clang and gas seem to interpret the symbols in memmove.S and memset.S differently, such that clang does not make them 'weak' as expected, which leads to a linker error, with both ld.bfd and ld.lld:

ld.lld: error: duplicate symbol: memmove

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

defined at common.c
kasan/common.o:(memmove) in archive mm/built-in.a
defined at memmove.o:(__memmove) in archive arch/arm64/lib/lib.a

ld.lld: error: duplicate symbol: memset

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

defined at common.c
kasan/common.o:(memset) in archive mm/built-in.a
defined at memset.o:(__memset) in archive arch/arm64/lib/lib.a

Copy the exact way these are written in memcpy_64.S, which does not have the same problem.

I don't know why this makes a difference, and it would be good to have someone with a better understanding of assembler internals review it.

It might be either a bug in the kernel or a bug in the assembler, no idea which one. My patch makes it work with all versions of clang and gcc, which is probably helpful even if it's a workaround for a clang bug.

---

---
 arch/arm64/lib/memcpy.S  | 3 +--
 arch/arm64/lib/memmove.S | 3 +--
 arch/arm64/lib/memset.S  | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)
arch/arm64/lib/memcpy.S +1 -2
index e0bf83d556f2..dc8d2a216a6e 100644
@@ -56,9 +56,8 @@
 	stp \reg1, \reg2, [\ptr], \val
 	.endm
 
-	.weak memcpy
 SYM_FUNC_START_ALIAS(__memcpy)
-SYM_FUNC_START_PI(memcpy)
+SYM_FUNC_START_WEAK_PI(memcpy)
 #include "copy_template.S"
 	ret
 SYM_FUNC_END_PI(memcpy)
arch/arm64/lib/memmove.S +1 -2
index 02cda2e33bde..1035dce4bdaf 100644
@@ -45,9 +45,8 @@ C_h	.req	x12
 D_l	.req	x13
 D_h	.req	x14
 
-	.weak memmove
 SYM_FUNC_START_ALIAS(__memmove)
-SYM_FUNC_START_PI(memmove)
+SYM_FUNC_START_WEAK_PI(memmove)
 	cmp	dstin, src
 	b.lo	__memcpy
 	add	tmp1, src, count
arch/arm64/lib/memset.S +1 -2
index 77c3c7ba0084..a9c1c9a01ea9 100644
@@ -42,9 +42,8 @@ dst		.req	x8
 tmp3w		.req	w9
 tmp3		.req	x9
 
-	.weak memset
 SYM_FUNC_START_ALIAS(__memset)
-SYM_FUNC_START_PI(memset)
+SYM_FUNC_START_WEAK_PI(memset)
 	mov	dst, dstin	/* Preserve return value.  */
 	and	A_lw, val, #255
 	orr	A_lw, A_lw, A_lw, lsl #8
Signature · 1 line
2.26.2
#1 lore raw
Quoted 19 lines · Arnd Bergmann

clang and gas seem to interpret the symbols in memmove.S and memset.S differently, such that clang does not make them 'weak' as expected, which leads to a linker error, with both ld.bfd and ld.lld:

ld.lld: error: duplicate symbol: memmove

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

defined at common.c
kasan/common.o:(memmove) in archive mm/built-in.a
defined at memmove.o:(__memmove) in archive arch/arm64/lib/lib.a

ld.lld: error: duplicate symbol: memset

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

defined at common.c
kasan/common.o:(memset) in archive mm/built-in.a
defined at memset.o:(__memset) in archive arch/arm64/lib/lib.a

Copy the exact way these are written in memcpy_64.S, which does not have the same problem.

Quoted 8 lines

I don't know why this makes a difference, and it would be good to have someone with a better understanding of assembler internals review it.

It might be either a bug in the kernel or a bug in the assembler, no idea which one. My patch makes it work with all versions of clang and gcc, which is probably helpful even if it's a workaround for a clang bug.

+ Bill, Fangrui, Jian
I think we saw this bug or a very similar bug internally around the ordering of .weak to .global.

Quoted 57 lines

---

---
 arch/arm64/lib/memcpy.S  | 3 +--
 arch/arm64/lib/memmove.S | 3 +--
 arch/arm64/lib/memset.S  | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)
arch/arm64/lib/memcpy.S +1 -2
index e0bf83d556f2..dc8d2a216a6e 100644
@@ -56,9 +56,8 @@
        stp \reg1, \reg2, [\ptr], \val
        .endm
 
-       .weak memcpy
 SYM_FUNC_START_ALIAS(__memcpy)
-SYM_FUNC_START_PI(memcpy)
+SYM_FUNC_START_WEAK_PI(memcpy)
 #include "copy_template.S"
        ret
 SYM_FUNC_END_PI(memcpy)
arch/arm64/lib/memmove.S +1 -2
index 02cda2e33bde..1035dce4bdaf 100644
@@ -45,9 +45,8 @@ C_h   .req    x12
 D_l    .req    x13
 D_h    .req    x14
 
-       .weak memmove
 SYM_FUNC_START_ALIAS(__memmove)
-SYM_FUNC_START_PI(memmove)
+SYM_FUNC_START_WEAK_PI(memmove)
        cmp     dstin, src
        b.lo    __memcpy
        add     tmp1, src, count
arch/arm64/lib/memset.S +1 -3
index 77c3c7ba0084..a9c1c9a01ea9 100644
@@ -42,9 +42,8 @@ dst           .req    x8
 tmp3w          .req    w9
 tmp3           .req    x9
 
-       .weak memset
 SYM_FUNC_START_ALIAS(__memset)
-SYM_FUNC_START_PI(memset)
+SYM_FUNC_START_WEAK_PI(memset)
        mov     dst, dstin      /* Preserve return value.  */
        and     A_lw, val, #255
        orr     A_lw, A_lw, A_lw, lsl #8
--

2.26.2

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 24 lines · Nick Desaulniers' via Clang Built Linux
Quoted 23 lines · Arnd Bergmann

clang and gas seem to interpret the symbols in memmove.S and memset.S differently, such that clang does not make them 'weak' as expected, which leads to a linker error, with both ld.bfd and ld.lld:

ld.lld: error: duplicate symbol: memmove

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

defined at common.c
kasan/common.o:(memmove) in archive mm/built-in.a
defined at memmove.o:(__memmove) in archive arch/arm64/lib/lib.a

ld.lld: error: duplicate symbol: memset

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

defined at common.c
kasan/common.o:(memset) in archive mm/built-in.a
defined at memset.o:(__memset) in archive arch/arm64/lib/lib.a

Copy the exact way these are written in memcpy_64.S, which does not have the same problem.

I don't know why this makes a difference, and it would be good to have someone with a better understanding of assembler internals review it.

Quoted 8 lines
Quoted 4 lines

It might be either a bug in the kernel or a bug in the assembler, no idea which one. My patch makes it work with all versions of clang and gcc, which is probably helpful even if it's a workaround for a clang bug.

+ Bill, Fangrui, Jian
I think we saw this bug or a very similar bug internally around the ordering of .weak to .global.

This may be another instance of
https://sourceware.org/pipermail/binutils/2020-March/000299.html
https://lore.kernel.org/linuxppc-dev/[email protected]/

I haven't checked but there may be both a .globl directive and a .weak directive

Quoted 47 lines
Quoted 47 lines

---

---
 arch/arm64/lib/memcpy.S  | 3 +--
 arch/arm64/lib/memmove.S | 3 +--
 arch/arm64/lib/memset.S  | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)
arch/arm64/lib/memcpy.S +1 -2
index e0bf83d556f2..dc8d2a216a6e 100644
@@ -56,9 +56,8 @@
        stp \reg1, \reg2, [\ptr], \val
        .endm
 
-       .weak memcpy
 SYM_FUNC_START_ALIAS(__memcpy)
-SYM_FUNC_START_PI(memcpy)
+SYM_FUNC_START_WEAK_PI(memcpy)
 #include "copy_template.S"
        ret
 SYM_FUNC_END_PI(memcpy)
arch/arm64/lib/memmove.S +1 -2
index 02cda2e33bde..1035dce4bdaf 100644
@@ -45,9 +45,8 @@ C_h   .req    x12
 D_l    .req    x13
 D_h    .req    x14
 
-       .weak memmove
 SYM_FUNC_START_ALIAS(__memmove)
-SYM_FUNC_START_PI(memmove)
+SYM_FUNC_START_WEAK_PI(memmove)
        cmp     dstin, src
        b.lo    __memcpy
        add     tmp1, src, count
arch/arm64/lib/memset.S +0 -0
index 77c3c7ba0084..a9c1c9a01ea9 100644
@@ -42,9 +42,8 @@ dst           .req    x8
 tmp3w          .req    w9
 tmp3           .req    x9
 
Quoted 18 lines
Quoted 9 lines
arch/arm64/lib/memset.S +1 -3
-       .weak memset
 SYM_FUNC_START_ALIAS(__memset)
-SYM_FUNC_START_PI(memset)
+SYM_FUNC_START_WEAK_PI(memset)
        mov     dst, dstin      /* Preserve return value.  */
        and     A_lw, val, #255
        orr     A_lw, A_lw, A_lw, lsl #8
--

2.26.2

Signature · 7 lines
Thanks,
~Nick Desaulniers

-- 
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/CAKwvOdnNxj-MdKj3aWoefF2W9PPG-TSeNU4Ym-N8NODJB5Yw_w%40mail.gmail.com.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel