Heinrich Schuchardt | ac49e7b | 2020-07-25 09:55:46 +0200 | [diff] [blame] | 1 | Building with Clang |
| 2 | =================== |
| 3 | |
| 4 | The biggest problem when trying to compile U-Boot with Clang is that almost all |
| 5 | archs rely on storing gd in a global register and the Clang 3.5 user manual |
| 6 | states: "Clang does not support global register variables; this is unlikely to |
| 7 | be implemented soon because it requires additional LLVM backend support." |
| 8 | |
| 9 | The 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 |
| 11 | supported inline assembly is needed to get and set the r9 or x18 value. This |
| 12 | leads to larger code then strictly necessary, but at least works. |
| 13 | |
Heinrich Schuchardt | ac49e7b | 2020-07-25 09:55:46 +0200 | [diff] [blame] | 14 | Debian based |
| 15 | ------------ |
| 16 | |
| 17 | Required packages can be installed via apt, e.g. |
| 18 | |
| 19 | .. code-block:: bash |
| 20 | |
| 21 | sudo apt-get install clang |
| 22 | |
Tom Rini | 3221960 | 2023-11-21 10:41:07 -0500 | [diff] [blame] | 23 | We make use of the CROSS_COMPILE variable to derive the build target which is |
| 24 | passed as the --target parameter to clang. |
| 25 | |
| 26 | The CROSS_COMPILE variable further determines the paths to other build |
| 27 | tools. As assembler we use the binary pointed to by '$(CROSS_COMPILE)as' |
| 28 | instead of the LLVM integrated assembler (IAS). |
| 29 | |
| 30 | Here is an example demonstrating building U-Boot for the Raspberry Pi 2 |
| 31 | using clang: |
Heinrich Schuchardt | ac49e7b | 2020-07-25 09:55:46 +0200 | [diff] [blame] | 32 | |
| 33 | .. code-block:: bash |
| 34 | |
| 35 | make HOSTCC=clang rpi_2_defconfig |
Tom Rini | 3221960 | 2023-11-21 10:41:07 -0500 | [diff] [blame] | 36 | make HOSTCC=clang CROSS_COMPILE=arm-linux-gnueabi- CC=clang -j8 |
Heinrich Schuchardt | ac49e7b | 2020-07-25 09:55:46 +0200 | [diff] [blame] | 37 | |
| 38 | It 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 | |
| 46 | FreeBSD 11 |
| 47 | ---------- |
| 48 | |
| 49 | Since llvm 3.4 is currently in the base system, the integrated assembler as |
| 50 | is incapable of building U-Boot. Therefore gas from devel/arm-gnueabi-binutils |
| 51 | is 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 | |
| 57 | The 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 | |
| 67 | Given that U-Boot will default to gcc, above commands can be |
| 68 | simplified 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 Yan | 6feedde | 2023-05-04 15:54:59 +0800 | [diff] [blame] | 75 | |
| 76 | |
| 77 | Known Issues |
| 78 | ------------ |
| 79 | |
| 80 | When build U-boot for `xenguest_arm64_defconfig` target, it reports linkage |
| 81 | error: |
| 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 | |
| 103 | To fix the failure, we need to append option `-mno-outline-atomics` in Clang |
| 104 | command 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 |