diff options
author | Khem Raj <raj.khem@gmail.com> | 2022-01-12 10:57:55 -0800 |
---|---|---|
committer | Khem Raj <raj.khem@gmail.com> | 2022-01-12 11:49:58 -0800 |
commit | d24837beb4005f7a006a31aed64fec9bc3fffb23 (patch) | |
tree | 584a65078474022bb66f5bf34a7d4589f47e73c0 | |
parent | 02ed1fbaff0e4a101908adb710f0bff7d6ec189d (diff) | |
download | meta-clang-d24837beb4005f7a006a31aed64fec9bc3fffb23.tar.gz |
busybox: Fix 1.35 build with clang
Signed-off-by: Ross Burton <ross.burton@arm.com>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
-rw-r--r-- | recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch | 172 | ||||
-rw-r--r-- | recipes-core/busybox/busybox/0001-apply-const-trick-to-ptr_to_globals.patch | 48 | ||||
-rw-r--r-- | recipes-core/busybox/busybox/clang.cfg | 1 | ||||
-rw-r--r-- | recipes-core/busybox/busybox_1.35%.bbappend (renamed from recipes-core/busybox/busybox_1.34%.bbappend) | 5 |
4 files changed, 52 insertions, 174 deletions
diff --git a/recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch b/recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch deleted file mode 100644 index 468a32d..0000000 --- a/recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch +++ /dev/null | |||
@@ -1,172 +0,0 @@ | |||
1 | From d941b59087d34cb93053b638c066bf080122e7bb Mon Sep 17 00:00:00 2001 | ||
2 | From: Khem Raj <raj.khem@gmail.com> | ||
3 | Date: Wed, 16 Jan 2019 22:39:24 -0800 | ||
4 | Subject: [PATCH] Turn ptr_to_globals and bb_errno to be non const | ||
5 | |||
6 | writing to a const variable is undefined behavior | ||
7 | |||
8 | This is undefined as per (C99 6.7.3 paragraph 5) see [1] | ||
9 | |||
10 | errno and ptr_to_globals is written to in code, this fails with | ||
11 | segfaults when compiled with clang | ||
12 | |||
13 | unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base) | ||
14 | { | ||
15 | unsigned long v; | ||
16 | char *endptr; | ||
17 | |||
18 | if (!endp) endp = &endptr; | ||
19 | *endp = (char*) arg; | ||
20 | |||
21 | if (!isalnum(arg[0])) return ret_ERANGE(); | ||
22 | errno = 0; | ||
23 | v = strtoul(arg, endp, base); | ||
24 | if (v > UINT_MAX) return ret_ERANGE(); | ||
25 | return handle_errors(v, endp); | ||
26 | } | ||
27 | |||
28 | without 'const' ( working code ) | ||
29 | |||
30 | Dump of assembler code for function bb_strtou: | ||
31 | 0x0000555555568298 <+0>: push %rbx | ||
32 | 0x0000555555568299 <+1>: sub $0x10,%rsp | ||
33 | 0x000055555556829d <+5>: test %rsi,%rsi | ||
34 | 0x00005555555682a0 <+8>: lea 0x8(%rsp),%rbx | ||
35 | 0x00005555555682a5 <+13>: cmovne %rsi,%rbx | ||
36 | 0x00005555555682a9 <+17>: mov %rdi,(%rbx) | ||
37 | 0x00005555555682ac <+20>: mov (%rdi),%al | ||
38 | 0x00005555555682ae <+22>: lea -0x30(%rax),%ecx | ||
39 | 0x00005555555682b1 <+25>: cmp $0xa,%cl | ||
40 | 0x00005555555682b4 <+28>: jb 0x5555555682be <bb_strtou+38> | ||
41 | 0x00005555555682b6 <+30>: or $0x20,%al | ||
42 | 0x00005555555682b8 <+32>: add $0x9f,%al | ||
43 | 0x00005555555682ba <+34>: cmp $0x1a,%al | ||
44 | 0x00005555555682bc <+36>: jae 0x5555555682dc <bb_strtou+68> | ||
45 | 0x00005555555682be <+38>: mov 0x107da3(%rip),%rax # 0x555555670068 <bb_errno> | ||
46 | => 0x00005555555682c5 <+45>: movl $0x0,(%rax) | ||
47 | 0x00005555555682cb <+51>: mov %rbx,%rsi | ||
48 | 0x00005555555682ce <+54>: callq 0x555555564310 <strtoul@plt> | ||
49 | 0x00005555555682d3 <+59>: mov %rax,%rcx | ||
50 | 0x00005555555682d6 <+62>: shr $0x20,%rcx | ||
51 | 0x00005555555682da <+66>: je 0x5555555682f0 <bb_strtou+88> | ||
52 | 0x00005555555682dc <+68>: mov 0x107d85(%rip),%rax # 0x555555670068 <bb_errno> | ||
53 | 0x00005555555682e3 <+75>: movl $0x22,(%rax) | ||
54 | 0x00005555555682e9 <+81>: mov $0xffffffff,%eax | ||
55 | 0x00005555555682ee <+86>: jmp 0x5555555682fb <bb_strtou+99> | ||
56 | 0x00005555555682f0 <+88>: mov %rax,%rdi | ||
57 | 0x00005555555682f3 <+91>: mov %rbx,%rsi | ||
58 | 0x00005555555682f6 <+94>: callq 0x5555555681e8 <handle_errors> | ||
59 | 0x00005555555682fb <+99>: add $0x10,%rsp | ||
60 | 0x00005555555682ff <+103>: pop %rbx | ||
61 | 0x0000555555568300 <+104>: retq | ||
62 | |||
63 | here address of bb_errno is valid rax = 0x7ffff7cac6c0 | ||
64 | |||
65 | with 'const' ( non-working code ) | ||
66 | |||
67 | Dump of assembler code for function bb_strtou: | ||
68 | 0x00005555555682a4 <+0>: push %r14 | ||
69 | 0x00005555555682a6 <+2>: push %rbx | ||
70 | 0x00005555555682a7 <+3>: push %rax | ||
71 | 0x00005555555682a8 <+4>: test %rsi,%rsi | ||
72 | 0x00005555555682ab <+7>: mov %rsp,%rbx | ||
73 | 0x00005555555682ae <+10>: cmovne %rsi,%rbx | ||
74 | 0x00005555555682b2 <+14>: mov %rdi,(%rbx) | ||
75 | 0x00005555555682b5 <+17>: mov (%rdi),%al | ||
76 | 0x00005555555682b7 <+19>: lea -0x30(%rax),%ecx | ||
77 | 0x00005555555682ba <+22>: cmp $0xa,%cl | ||
78 | 0x00005555555682bd <+25>: jb 0x5555555682d6 <bb_strtou+50> | ||
79 | 0x00005555555682bf <+27>: or $0x20,%al | ||
80 | 0x00005555555682c1 <+29>: add $0x9f,%al | ||
81 | 0x00005555555682c3 <+31>: cmp $0x1a,%al | ||
82 | 0x00005555555682c5 <+33>: jb 0x5555555682d6 <bb_strtou+50> | ||
83 | 0x00005555555682c7 <+35>: mov 0x107d9a(%rip),%rax # 0x555555670068 <bb_errno> | ||
84 | 0x00005555555682ce <+42>: movl $0x22,(%rax) | ||
85 | 0x00005555555682d4 <+48>: jmp 0x5555555682fc <bb_strtou+88> | ||
86 | 0x00005555555682d6 <+50>: mov 0x107d8b(%rip),%r14 # 0x555555670068 <bb_errno> | ||
87 | => 0x00005555555682dd <+57>: movl $0x0,(%r14) | ||
88 | 0x00005555555682e4 <+64>: mov %rbx,%rsi | ||
89 | 0x00005555555682e7 <+67>: callq 0x555555564300 <strtoul@plt> | ||
90 | 0x00005555555682ec <+72>: mov %rax,%rcx | ||
91 | 0x00005555555682ef <+75>: shr $0x20,%rcx | ||
92 | 0x00005555555682f3 <+79>: je 0x555555568303 <bb_strtou+95> | ||
93 | 0x00005555555682f5 <+81>: movl $0x22,(%r14) | ||
94 | 0x00005555555682fc <+88>: mov $0xffffffff,%eax | ||
95 | 0x0000555555568301 <+93>: jmp 0x55555556830e <bb_strtou+106> | ||
96 | 0x0000555555568303 <+95>: mov %rax,%rdi | ||
97 | 0x0000555555568306 <+98>: mov %rbx,%rsi | ||
98 | 0x0000555555568309 <+101>: callq 0x5555555681f4 <handle_errors> | ||
99 | 0x000055555556830e <+106>: add $0x8,%rsp | ||
100 | 0x0000555555568312 <+110>: pop %rbx | ||
101 | 0x0000555555568313 <+111>: pop %r14 | ||
102 | 0x0000555555568315 <+113>: retq | ||
103 | |||
104 | r14 is 0x0 and writing to this ofcourse ends up in segfault | ||
105 | |||
106 | [1] https://bugs.llvm.org/show_bug.cgi?id=39919 | ||
107 | |||
108 | Signed-off-by: Khem Raj <raj.khem@gmail.com> | ||
109 | --- | ||
110 | coreutils/test.c | 2 +- | ||
111 | include/libbb.h | 4 ++-- | ||
112 | libbb/lineedit.c | 2 +- | ||
113 | shell/ash.c | 5 ++--- | ||
114 | 4 files changed, 6 insertions(+), 7 deletions(-) | ||
115 | |||
116 | --- a/coreutils/test.c | ||
117 | +++ b/coreutils/test.c | ||
118 | @@ -435,7 +435,7 @@ struct test_statics { | ||
119 | }; | ||
120 | |||
121 | /* See test_ptr_hack.c */ | ||
122 | -extern struct test_statics *const test_ptr_to_statics; | ||
123 | +extern struct test_statics *test_ptr_to_statics; | ||
124 | |||
125 | #define S (*test_ptr_to_statics) | ||
126 | #define args (S.args ) | ||
127 | --- a/include/libbb.h | ||
128 | +++ b/include/libbb.h | ||
129 | @@ -342,7 +342,7 @@ struct BUG_off_t_size_is_misdetected { | ||
130 | #if defined(errno) | ||
131 | /* If errno is a define, assume it's "define errno (*__errno_location())" | ||
132 | * and we will cache it's result in this variable */ | ||
133 | -extern int *const bb_errno; | ||
134 | +extern int *bb_errno; | ||
135 | #undef errno | ||
136 | #define errno (*bb_errno) | ||
137 | #define bb_cached_errno_ptr 1 | ||
138 | @@ -2228,7 +2228,7 @@ struct globals; | ||
139 | /* '*const' ptr makes gcc optimize code much better. | ||
140 | * Magic prevents ptr_to_globals from going into rodata. | ||
141 | * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */ | ||
142 | -extern struct globals *const ptr_to_globals; | ||
143 | +extern struct globals *ptr_to_globals; | ||
144 | |||
145 | #if defined(__clang_major__) && __clang_major__ >= 9 | ||
146 | /* Clang/llvm drops assignment to "constant" storage. Silently. | ||
147 | --- a/libbb/lineedit.c | ||
148 | +++ b/libbb/lineedit.c | ||
149 | @@ -192,7 +192,7 @@ struct lineedit_statics { | ||
150 | }; | ||
151 | |||
152 | /* See lineedit_ptr_hack.c */ | ||
153 | -extern struct lineedit_statics *const lineedit_ptr_to_statics; | ||
154 | +extern struct lineedit_statics * lineedit_ptr_to_statics; | ||
155 | |||
156 | #define S (*lineedit_ptr_to_statics) | ||
157 | #define state (S.state ) | ||
158 | --- a/shell/ash.c | ||
159 | +++ b/shell/ash.c | ||
160 | @@ -300,10 +300,9 @@ typedef long arith_t; | ||
161 | * set "-DBB_GLOBAL_CONST=''" in CONFIG_EXTRA_CFLAGS to disable | ||
162 | * this optimization. | ||
163 | */ | ||
164 | -#ifndef BB_GLOBAL_CONST | ||
165 | -# define BB_GLOBAL_CONST const | ||
166 | -#endif | ||
167 | |||
168 | +#undef BB_GLOBAL_CONST | ||
169 | +#define BB_GLOBAL_CONST | ||
170 | |||
171 | /* ============ Hash table sizes. Configurable. */ | ||
172 | |||
diff --git a/recipes-core/busybox/busybox/0001-apply-const-trick-to-ptr_to_globals.patch b/recipes-core/busybox/busybox/0001-apply-const-trick-to-ptr_to_globals.patch new file mode 100644 index 0000000..84b61a2 --- /dev/null +++ b/recipes-core/busybox/busybox/0001-apply-const-trick-to-ptr_to_globals.patch | |||
@@ -0,0 +1,48 @@ | |||
1 | From d3fbf09787b9dd8a061b541cd931ffbf63e4d610 Mon Sep 17 00:00:00 2001 | ||
2 | From: Khem Raj <raj.khem@gmail.com> | ||
3 | Date: Wed, 12 Jan 2022 10:48:36 -0800 | ||
4 | Subject: [PATCH] apply const trick to ptr_to_globals | ||
5 | |||
6 | This was missing in the previous attempt to fix it via [1] | ||
7 | |||
8 | This helps fix segfaults when compiling with clang ( seen on riscv64 ) | ||
9 | |||
10 | [ 452.428349] less[270]: unhandled signal 11 code 0x1 at 0x000000000000000c in busybox.nosuid[2ab7491000+ba000] | ||
11 | [ 452.430246] CPU: 3 PID: 270 Comm: less Not tainted 5.15.13-yocto-standard #1 | ||
12 | [ 452.431323] Hardware name: riscv-virtio,qemu (DT) | ||
13 | [ 452.431925] epc : 0000002ab74a19ee ra : 0000002ab74a19dc sp : 0000003fec6ec980 | ||
14 | [ 452.432725] gp : 0000002ab754dcb0 tp : 0000003f88783800 t0 : 0000003f8878d4a0 | ||
15 | [ 452.433744] t1 : 0000002ab749b00c t2 : 0000000000000000 s0 : 0000003fec6ecc38 | ||
16 | [ 452.434732] s1 : 000000000000004c a0 : 00000000ffffffff a1 : 0000002ab754dde0 | ||
17 | [ 452.435861] a2 : 0000000000000000 a3 : 0000000000000100 a4 : 0000002ab754f3a0 | ||
18 | [ 452.436787] a5 : 0000002ab754f3a0 a6 : 0000000000000000 a7 : 0000002ab754f2a0 | ||
19 | [ 452.437974] s2 : 0000000000000002 s3 : 0000002ab754b6c8 s4 : 0000002ab749b60e | ||
20 | [ 452.438781] s5 : 0000000000000000 s6 : 0000002ab754b6c8 s7 : 0000003f88943060 | ||
21 | [ 452.439723] s8 : 0000003f88944050 s9 : 0000002ad8502e88 s10: 0000002ad8502de8 | ||
22 | [ 452.440538] s11: 0000000000000014 t3 : 0000003f887fceb6 t4 : 0000003f8893af0c | ||
23 | [ 452.441438] t5 : 0000000000000000 t6 : 0000003f88923000 | ||
24 | |||
25 | [1] https://git.busybox.net/busybox/commit/?id=1f925038a | ||
26 | |||
27 | Upstream-Status: Submitted [http://lists.busybox.net/pipermail/busybox/2022-January/089413.html] | ||
28 | Signed-off-by: Khem Raj <raj.khem@gmail.com> | ||
29 | --- | ||
30 | include/libbb.h | 2 +- | ||
31 | 1 file changed, 1 insertion(+), 1 deletion(-) | ||
32 | |||
33 | diff --git a/include/libbb.h b/include/libbb.h | ||
34 | index a487828..7b76335 100644 | ||
35 | --- a/include/libbb.h | ||
36 | +++ b/include/libbb.h | ||
37 | @@ -2281,7 +2281,7 @@ struct globals; | ||
38 | /* '*const' ptr makes gcc optimize code much better. | ||
39 | * Magic prevents ptr_to_globals from going into rodata. | ||
40 | * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */ | ||
41 | -extern struct globals *const ptr_to_globals; | ||
42 | +extern struct globals *BB_GLOBAL_CONST ptr_to_globals; | ||
43 | |||
44 | #define barrier() asm volatile ("":::"memory") | ||
45 | |||
46 | -- | ||
47 | 2.34.1 | ||
48 | |||
diff --git a/recipes-core/busybox/busybox/clang.cfg b/recipes-core/busybox/busybox/clang.cfg new file mode 100644 index 0000000..450fa7e --- /dev/null +++ b/recipes-core/busybox/busybox/clang.cfg | |||
@@ -0,0 +1 @@ | |||
CONFIG_EXTRA_CFLAGS="-DBB_GLOBAL_CONST=''" | |||
diff --git a/recipes-core/busybox/busybox_1.34%.bbappend b/recipes-core/busybox/busybox_1.35%.bbappend index 2c0af5e..fbe6279 100644 --- a/recipes-core/busybox/busybox_1.34%.bbappend +++ b/recipes-core/busybox/busybox_1.35%.bbappend | |||
@@ -1,7 +1,8 @@ | |||
1 | FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:" | 1 | FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:" |
2 | |||
2 | SRC_URI:append:toolchain-clang = "\ | 3 | SRC_URI:append:toolchain-clang = "\ |
3 | file://0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch \ | 4 | file://0001-apply-const-trick-to-ptr_to_globals.patch \ |
4 | " | 5 | file://clang.cfg" |
5 | 6 | ||
6 | # networking/tls_pstm_sqr_comba.c:514:4: error: inline assembly requires more registers than available | 7 | # networking/tls_pstm_sqr_comba.c:514:4: error: inline assembly requires more registers than available |
7 | # SQRADD2(*tmpx++, *tmpy--); | 8 | # SQRADD2(*tmpx++, *tmpy--); |