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 | |
| 14 | **NOTE:** target compilation only work for _some_ ARM boards at the moment. |
| 15 | Also AArch64 is not supported currently due to a lack of private libgcc |
| 16 | support. Boards which reassign gd in c will also fail to compile, but there is |
| 17 | in no strict reason to do so in the ARM world, since crt0.S takes care of this. |
| 18 | These assignments can be avoided by changing the init calls but this is not in |
| 19 | mainline yet. |
| 20 | |
| 21 | |
| 22 | Debian based |
| 23 | ------------ |
| 24 | |
| 25 | Required packages can be installed via apt, e.g. |
| 26 | |
| 27 | .. code-block:: bash |
| 28 | |
| 29 | sudo apt-get install clang |
| 30 | |
| 31 | Note that we still use binutils for some tools so we must continue to set |
| 32 | CROSS_COMPILE. To compile U-Boot with Clang on Linux without IAS use e.g. |
| 33 | |
| 34 | .. code-block:: bash |
| 35 | |
| 36 | make HOSTCC=clang rpi_2_defconfig |
| 37 | make HOSTCC=clang CROSS_COMPILE=arm-linux-gnueabi- \ |
| 38 | CC="clang -target arm-linux-gnueabi" -j8 |
| 39 | |
| 40 | It can also be used to compile sandbox: |
| 41 | |
| 42 | .. code-block:: bash |
| 43 | |
| 44 | make HOSTCC=clang sandbox_defconfig |
| 45 | make HOSTCC=clang CC=clang -j8 |
| 46 | |
| 47 | |
| 48 | FreeBSD 11 |
| 49 | ---------- |
| 50 | |
| 51 | Since llvm 3.4 is currently in the base system, the integrated assembler as |
| 52 | is incapable of building U-Boot. Therefore gas from devel/arm-gnueabi-binutils |
| 53 | is used instead. It needs a symlink to be picked up correctly though: |
| 54 | |
| 55 | .. code-block:: bash |
| 56 | |
| 57 | ln -s /usr/local/bin/arm-gnueabi-freebsd-as /usr/bin/arm-freebsd-eabi-as |
| 58 | |
| 59 | The following commands compile U-Boot using the Clang xdev toolchain. |
| 60 | |
| 61 | **NOTE:** CROSS_COMPILE and target differ on purpose! |
| 62 | |
| 63 | .. code-block:: bash |
| 64 | |
| 65 | export CROSS_COMPILE=arm-gnueabi-freebsd- |
| 66 | gmake rpi_2_defconfig |
| 67 | gmake CC="clang -target arm-freebsd-eabi --sysroot /usr/arm-freebsd" -j8 |
| 68 | |
| 69 | Given that U-Boot will default to gcc, above commands can be |
| 70 | simplified with a simple wrapper script - saved as |
| 71 | /usr/local/bin/arm-gnueabi-freebsd-gcc - listed below: |
| 72 | |
| 73 | .. code-block:: bash |
| 74 | |
| 75 | #!/bin/sh |
| 76 | exec clang -target arm-freebsd-eabi --sysroot /usr/arm-freebsd "$@" |
Leo Yan | 6feedde | 2023-05-04 15:54:59 +0800 | [diff] [blame] | 77 | |
| 78 | |
| 79 | Known Issues |
| 80 | ------------ |
| 81 | |
| 82 | When build U-boot for `xenguest_arm64_defconfig` target, it reports linkage |
| 83 | error: |
| 84 | |
| 85 | .. code-block:: bash |
| 86 | |
| 87 | aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `do_hypervisor_callback': |
| 88 | /home/leoy/Dev2/u-boot/drivers/xen/hypervisor.c:188: undefined reference to `__aarch64_swp8_acq_rel' |
| 89 | aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `synch_test_and_set_bit': |
| 90 | /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:40: undefined reference to `__aarch64_ldset1_acq_rel' |
| 91 | aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `synch_test_and_clear_bit': |
| 92 | /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:28: undefined reference to `__aarch64_ldclr1_acq_rel' |
| 93 | aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.o: in function `synch_test_and_set_bit': |
| 94 | /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:40: undefined reference to `__aarch64_ldset1_acq_rel' |
| 95 | aarch64-linux-gnu-ld.bfd: drivers/xen/hypervisor.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_clear_bit': |
| 98 | /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:28: undefined reference to `__aarch64_ldclr1_acq_rel' |
| 99 | aarch64-linux-gnu-ld.bfd: drivers/xen/events.o: in function `synch_test_and_set_bit': |
| 100 | /home/leoy/Dev2/u-boot/./arch/arm/include/asm/xen/system.h:40: undefined reference to `__aarch64_ldset1_acq_rel' |
| 101 | aarch64-linux-gnu-ld.bfd: drivers/xen/gnttab.o: in function `gnttab_end_access': |
| 102 | /home/leoy/Dev2/u-boot/drivers/xen/gnttab.c:109: undefined reference to `__aarch64_cas2_acq_rel' |
| 103 | Segmentation fault |
| 104 | |
| 105 | To fix the failure, we need to append option `-mno-outline-atomics` in Clang |
| 106 | command to not generate local calls to out-of-line atomic operations: |
| 107 | |
| 108 | .. code-block:: bash |
| 109 | |
| 110 | make HOSTCC=clang xenguest_arm64_defconfig |
| 111 | make HOSTCC=clang CROSS_COMPILE=aarch64-linux-gnu- \ |
| 112 | CC="clang -target aarch64-linux-gnueabi -mno-outline-atomics" -j8 |