1========================================== 2How to build Windows Itanium applications. 3========================================== 4 5Introduction 6============ 7 8This document contains information describing how to create a Windows Itanium toolchain. 9 10Windows Itanium allows you to deploy Itanium C++ ABI applications on top of the MS VS CRT. 11This environment can use the Windows SDK headers directly and does not required additional 12headers or additional runtime machinery (such as is used by mingw). 13 14Windows Itanium Stack: 15 16* Uses the Itanium C++ abi. 17* libc++. 18* libc++-abi. 19* libunwind. 20* The MS VS CRT. 21* Is compatible with MS Windows SDK include headers. 22* COFF/PE file format. 23* LLD 24 25Note: compiler-rt is not used. This functionality is supplied by the MS VCRT. 26 27Prerequisites 28============= 29 30* The MS SDK is installed as part of MS Visual Studio. 31* Clang with support for the windows-itanium triple. 32* COFF LLD with support for the -autoimport switch. 33 34Known issues: 35============= 36 37SJLJ exceptions, "-fsjlj-exceptions", are the only currently supported model. 38 39link.exe (the MS linker) is unsuitable as it doesn't support auto-importing which 40is currently required to link correctly. However, if that limitation is removed 41then there are no other known issues with using link.exe. 42 43Currently, there is a lack of a usable Windows compiler driver for Windows Itanium. 44A reasonable work-around is to build clang with a windows-msvc default target and 45then override the triple with e.g. "-Xclang -triple -Xclang x86_64-unknown-windows-itanium". 46The linker can be specified with: "-fuse-ld=lld". 47 48In the Itanium C++ ABI the first member of an object is a pointer to the vtable 49for its class. The vtable is often emitted into the object file with the key function 50and must be imported for classes marked dllimport. The pointers must be globally 51unique. Unfortunately, the COFF/PE file format does not provide a mechanism to 52store a runtime address from another DLL into this pointer (although runtime 53addresses are patched into the IAT). Therefore, the compiler must emit some code, 54that runs after IAT patching but before anything that might use the vtable pointers, 55and sets the vtable pointer to the address from the IAT. For the special case of 56the references to vtables for __cxxabiv1::__class_type_info from typeinto objects 57there is no declaration available to the compiler so this can't be done. To allow 58programs to link we currently rely on the -auto-import switch in LLD to auto-import 59references to __cxxabiv1::__class_type_info pointers (see: https://reviews.llvm.org/D43184 60for a related discussion). This allows for linking; but, code that actually uses 61such fields will not work as they these will not be fixed up at runtime. See 62_pei386_runtime_relocator which handles the runtime component of the autoimporting 63scheme used for mingw and comments in https://reviews.llvm.org/D43184 and 64https://reviews.llvm.org/D89518 for more. 65 66Assembling a Toolchain: 67======================= 68 69The procedure is: 70 71# Build an LLVM toolchain with support for Windows Itanium. 72# Use the toolchain from step 1. to build libc++, libc++abi, and libunwind. 73 74It is also possible to cross-compile from Linux. 75 76One method of building the libraries in step 2. is to build them "stand-alone". 77A stand-alone build doesn't involve the rest of the LLVM tree. The steps are: 78 79* ``cd build-dir`` 80* ``cmake -DLLVM_PATH=<path to llvm checkout e.g. /llvm-project/> -DCMAKE_INSTALL_PREFIX=<install path> <other options> <path to project e.g. /llvm-project/libcxxabi>`` 81* ``<make program e.g. ninja>`` 82* ``<make program> install`` 83 84More information on standalone builds can be found in the build documentation for 85the respective libraries. The next section discuss the salient options and modifications 86required for building and installing the libraries using standalone builds. This assumes 87that we are building libunwind and ibc++ as DLLs and statically linking libc++abi into 88libc++. Other build configurations are possible, but they are not discussed here. 89 90Common CMake configuration options: 91----------------------------------- 92 93* ``-D_LIBCPP_ABI_FORCE_ITANIUM'`` 94 95Tell the libc++ headers that the Itanium C++ ABI is being used. 96 97* ``-DCMAKE_C_FLAGS="-lmsvcrt -llegacy_stdio_definitions -D_NO_CRT_STDIO_INLINE"`` 98 99Supply CRT definitions including stdio definitions that have been removed from the MS VS CRT. 100We don't want the stdio functions declared inline as they will cause multiple definition 101errors when the same symbols are pulled in from legacy_stdio_definitions.ib. 102 103* ``-DCMAKE_INSTALL_PREFIX=<install path>`` 104 105Where to install the library and headers. 106 107Building libunwind: 108------------------- 109 110* ``-DLIBUNWIND_ENABLE_SHARED=ON`` 111* ``-DLIBUNWIND_ENABLE_STATIC=OFF`` 112 113libunwind can be built as a DLL. It is not dependent on other projects. 114 115* ``-DLIBUNWIND_USE_COMPILER_RT=OFF`` 116 117We use the MS runtime. 118 119The CMake files will need to be edited to prevent them adding GNU specific libraries to the link line. 120 121Building libc++abi: 122------------------- 123 124* ``-DLIBCXXABI_ENABLE_SHARED=OFF`` 125* ``-DLIBCXXABI_ENABLE_STATIC=ON`` 126* ``-DLIBCXX_ENABLE_SHARED=ON'`` 127* ``-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON`` 128 129To break the symbol dependency between libc++abi and libc++ we 130build libc++abi as a static library and then statically link it 131into the libc++ DLL. This necessitates setting the CMake file 132to ensure that the visibility macros (which expand to dllexport/import) 133are expanded as they will be needed when creating the final libc++ 134DLL later, see: https://reviews.llvm.org/D90021. 135 136* ``-DLIBCXXABI_LIBCXX_INCLUDES=<path to libcxx>/include`` 137 138Where to find the libc++ headers 139 140Building libc++: 141---------------- 142 143* ``-DLIBCXX_ENABLE_SHARED=ON`` 144* ``-DLIBCXX_ENABLE_STATIC=OFF`` 145 146We build libc++ as a DLL and statically link libc++abi into it. 147 148* ``-DLIBCXX_INSTALL_HEADERS=ON`` 149 150Install the headers. 151 152* ``-DLIBCXX_USE_COMPILER_RT=OFF`` 153 154We use the MS runtime. 155 156* ``-DLIBCXX_HAS_WIN32_THREAD_API=ON`` 157 158Windows Itanium does not offer a POSIX-like layer over WIN32. 159 160* ``-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON`` 161* ``-DLIBCXX_CXX_ABI=libcxxabi`` 162* ``-DLIBCXX_CXX_ABI_INCLUDE_PATHS=<libcxxabi src path>/include`` 163* ``-DLIBCXX_CXX_ABI_LIBRARY_PATH=<libcxxabi build path>/lib`` 164 165Use the static libc++abi library built earlier. 166 167* ``-DLIBCXX_NO_VCRUNTIME=ON`` 168 169Remove any dependency on the VC runtime - we need libc++abi to supply the C++ runtime. 170 171* ``-DCMAKE_C_FLAGS=<path to installed unwind.lib>`` 172 173As we are statically linking against libcxxabi we need to link 174against the unwind import library to resolve unwind references 175from the libcxxabi objects. 176 177* ``-DCMAKE_C_FLAGS+=' -UCLOCK_REALTIME'`` 178 179Prevent the inclusion of sys/time that MS doesn't provide. 180 181Notes: 182------ 183 184An example build recipe is available here: https://reviews.llvm.org/D88124 185