blob: 09bb988e92362a8126622c64cdf49682dc4b3863 [file] [log] [blame]
Heinrich Schuchardt7f89e852020-07-25 09:55:46 +02001Building with Clang
2===================
3
4The biggest problem when trying to compile U-Boot with Clang is that almost all
5archs rely on storing gd in a global register and the Clang 3.5 user manual
6states: "Clang does not support global register variables; this is unlikely to
7be implemented soon because it requires additional LLVM backend support."
8
9The ARM backend can be instructed not to use the r9 and x18 registers using
10-ffixed-r9 or -ffixed-x18 respectively. As global registers themselves are not
11supported inline assembly is needed to get and set the r9 or x18 value. This
12leads to larger code then strictly necessary, but at least works.
13
Heinrich Schuchardt7f89e852020-07-25 09:55:46 +020014Debian based
15------------
16
17Required packages can be installed via apt, e.g.
18
19.. code-block:: bash
20
21 sudo apt-get install clang
22
Tom Rini463e3412023-11-21 10:41:07 -050023We make use of the CROSS_COMPILE variable to derive the build target which is
24passed as the --target parameter to clang.
25
26The CROSS_COMPILE variable further determines the paths to other build
27tools. As assembler we use the binary pointed to by '$(CROSS_COMPILE)as'
28instead of the LLVM integrated assembler (IAS).
29
30Here is an example demonstrating building U-Boot for the Raspberry Pi 2
31using clang:
Heinrich Schuchardt7f89e852020-07-25 09:55:46 +020032
33.. code-block:: bash
34
35 make HOSTCC=clang rpi_2_defconfig
Tom Rini463e3412023-11-21 10:41:07 -050036 make HOSTCC=clang CROSS_COMPILE=arm-linux-gnueabi- CC=clang -j8
Heinrich Schuchardt7f89e852020-07-25 09:55:46 +020037
38It can also be used to compile sandbox:
39
40.. code-block:: bash
41
42 make HOSTCC=clang sandbox_defconfig
43 make HOSTCC=clang CC=clang -j8
44
45
46FreeBSD 11
47----------
48
49Since llvm 3.4 is currently in the base system, the integrated assembler as
50is incapable of building U-Boot. Therefore gas from devel/arm-gnueabi-binutils
51is used instead. It needs a symlink to be picked up correctly though:
52
53.. code-block:: bash
54
55 ln -s /usr/local/bin/arm-gnueabi-freebsd-as /usr/bin/arm-freebsd-eabi-as
56
57The following commands compile U-Boot using the Clang xdev toolchain.
58
59**NOTE:** CROSS_COMPILE and target differ on purpose!
60
61.. code-block:: bash
62
63 export CROSS_COMPILE=arm-gnueabi-freebsd-
64 gmake rpi_2_defconfig
65 gmake CC="clang -target arm-freebsd-eabi --sysroot /usr/arm-freebsd" -j8
66
67Given that U-Boot will default to gcc, above commands can be
68simplified with a simple wrapper script - saved as
69/usr/local/bin/arm-gnueabi-freebsd-gcc - listed below:
70
71.. code-block:: bash
72
73 #!/bin/sh
74 exec clang -target arm-freebsd-eabi --sysroot /usr/arm-freebsd "$@"
Leo Yane45fcb02023-05-04 15:54:59 +080075
76
77Known Issues
78------------
79
80When build U-boot for `xenguest_arm64_defconfig` target, it reports linkage
81error:
82
83.. code-block:: bash
84
85 aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `do_hypervisor_callback':
86 /home/leoy/Dev2/u-boot/drivers/xen/hypervisor.c:188: undefined reference to `__aarch64_swp8_acq_rel'
87 aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `synch_test_and_set_bit':
88 /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:40: undefined reference to `__aarch64_ldset1_acq_rel'
89 aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `synch_test_and_clear_bit':
90 /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:28: undefined reference to `__aarch64_ldclr1_acq_rel'
91 aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `synch_test_and_set_bit':
92 /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:40: undefined reference to `__aarch64_ldset1_acq_rel'
93 aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `synch_test_and_clear_bit':
94 /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:28: undefined reference to `__aarch64_ldclr1_acq_rel'
95 aarch64-linux-gnu-ld.bfd: drivers/xen/events.o: in function `synch_test_and_clear_bit':
96 /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:28: undefined reference to `__aarch64_ldclr1_acq_rel'
97 aarch64-linux-gnu-ld.bfd: drivers/xen/events.o: in function `synch_test_and_set_bit':
98 /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:40: undefined reference to `__aarch64_ldset1_acq_rel'
99 aarch64-linux-gnu-ld.bfd: drivers/xen/gnttab.o: in function `gnttab_end_access':
100 /home/leoy/Dev2/u-boot/drivers/xen/gnttab.c:109: undefined reference to `__aarch64_cas2_acq_rel'
101 Segmentation fault
102
103To fix the failure, we need to append option `-mno-outline-atomics` in Clang
104command to not generate local calls to out-of-line atomic operations:
105
106.. code-block:: bash
107
108 make HOSTCC=clang xenguest_arm64_defconfig
109 make HOSTCC=clang CROSS_COMPILE=aarch64-linux-gnu- \
110 CC="clang -target aarch64-linux-gnueabi -mno-outline-atomics" -j8