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