Skip to content

Build changes for ARM64 #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ jobs:
strategy:
matrix:
build-type: [RelWithDebInfo]
rebuild-glibc: [ON, OFF]
steps:
- name: Setup Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
Expand Down Expand Up @@ -181,6 +182,9 @@ jobs:
working-directory: ${{ github.workspace }}/llvm-project
run: |
ln -s ../build-rtlibs ./
- name: Init glibc submodule if rebuilding glibc
if: matrix.rebuild-glibc == 'ON'
run: git submodule update --init external/glibc
- name: Test ARM build (${{ matrix.build-type }})
run: |
LLVM_DIR=`llvm-config-15 --cmakedir`
Expand All @@ -195,6 +199,7 @@ jobs:
-I${rtlibs_dir}/include/c++/v1"
export LDFLAGS="-L/usr/aarch64-linux-gnu/lib"
cmake .. \
-DLIBIA2_REBUILD_GLIBC=${{ matrix.rebuild-glibc }} \
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \
-DCMAKE_CROSSCOMPILING_EMULATOR=${{ github.workspace }}/qemu/build/qemu-aarch64 \
-DCMAKE_TOOLCHAIN_FILE=../cmake/aarch64-toolchain.cmake \
Expand All @@ -203,11 +208,13 @@ jobs:
-DCMAKE_OBJCOPY=/usr/bin/llvm-objcopy-15 \
-DCMAKE_C_FLAGS="$cross_flags" \
-DCMAKE_CXX_FLAGS="$cross_flags" \
-DCMAKE_EXE_LINKER_FLAGS="$cross_link_flags -lm -Wl,-rpath-link,${rtlibs_dir}/lib" \
-DCMAKE_SHARED_LINKER_FLAGS="$cross_link_flags -Wl,-rpath,${rtlibs_dir}/lib" \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was switching -Wl,-rpath and -Wl,-rpath-link in these two lines intentional?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was intentional and fixed things at the time (see commit message on f5a0587), but may be obviated by other changes in this series. I'll revisit this and see if it's still necessary.

-DCMAKE_EXE_LINKER_FLAGS="$cross_link_flags -Wl,-rpath,${rtlibs_dir}/lib" \
-DCMAKE_SHARED_LINKER_FLAGS="$cross_link_flags -Wl,-rpath-link,${rtlibs_dir}/lib" \
-DClang_DIR=$Clang_DIR \
-DCLANG_EXE=`which clang-15` \
-DLLVM_DIR=$LLVM_DIR \
-G Ninja
# glibc Makefile picks up compiler-rt via LLVM_BUILD_DIR
export LLVM_BUILD_DIR="$(pwd)/../llvm-project"
ninja -v check
popd
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "external/glibc"]
path = external/glibc
url = https://www.sourceware.org/git/glibc.git
url = https://github.com/immunant/IA2-glibc.git
[submodule "external/b63"]
path = external/b63
url = https://github.com/okuvshynov/b63.git
2 changes: 2 additions & 0 deletions cmake/ia2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ function(pad_tls_library INPUT OUTPUT)
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/unpadded"
OUTPUT_NAME "${OUTPUT}"
)
# copy interface link libs from unpadded target to padded one
target_link_libraries(${OUTPUT} INTERFACE $<TARGET_PROPERTY:${INPUT},INTERFACE_LINK_LIBRARIES>)
target_link_options(${OUTPUT} INTERFACE $<TARGET_PROPERTY:${INPUT},INTERFACE_LINK_OPTIONS>)
endfunction()

Expand Down
4 changes: 2 additions & 2 deletions cmake/qemu-command.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ else()
endif()
set(QEMU_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR}
"-one-insn-per-tb"
"-L" "${CMAKE_BINARY_DIR}/external/glibc/sysroot/usr/"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the sysroot not include the headers anymore? Why are we removing the usr suffix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usr/ changes are all in commit 5a078d3. The sysroot still includes the headers. The usr/ for /usr/include where the headers end up is moved to the headers-copying line:

COMMAND ${CMAKE_COMMAND} -E copy_directory ${SYSROOT_SRC_DIR}/include ${SYSROOT_BIN_DIR}/usr/include

We remove this suffix because the libraries should end up in /lib rather than /usr/lib inside the sysroot, to match what happens on a typical FHS filesystem layout and what glibc's install step produces. Then we can use the same relative paths for libraries whether we rebuild glibc or use the prebuilt one.

Arguably it would be cleanest to also store the includes at glibc_sysroot/usr/include (so we would glob them from ${SYSROOT_SRC_DIR}/usr/include/ when setting SYSROOT_HEADERS_SRC), but it doesn't particularly matter as long as they end up at the right place.

"-E" "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/external/glibc/sysroot/usr/lib:/usr/aarch64-linux-gnu/lib:/usr/aarch64-linux-gnu/lib64"
"-L" "${CMAKE_BINARY_DIR}/external/glibc/sysroot/"
"-E" "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/external/glibc/sysroot/lib:/usr/aarch64-linux-gnu/lib:/usr/aarch64-linux-gnu/lib64"
)
endif()
2 changes: 1 addition & 1 deletion external/glibc
Submodule glibc updated 14093 files
37 changes: 31 additions & 6 deletions runtime/libia2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ target_link_options(libia2 INTERFACE
set_property(TARGET libia2 PROPERTY INTERFACE_LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/padding.ld ${CMAKE_CURRENT_BINARY_DIR}/dynsym.syms)

if (LIBIA2_AARCH64)
set(SYSROOT_BIN_DIR ${CMAKE_BINARY_DIR}/external/glibc/sysroot)
set(LIBC_PATH ${SYSROOT_BIN_DIR}/lib/libc.so.6)
set(LD_LINUX_PATH ${SYSROOT_BIN_DIR}/lib/ld-linux-aarch64.so.1)
if (LIBIA2_REBUILD_GLIBC)
ExternalProject_Add(glibc
SOURCE_DIR ${CMAKE_SOURCE_DIR}/external/glibc
BINARY_DIR ${CMAKE_BINARY_DIR}/external/glibc
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env
# ffixed-x18 has to be set here instead of CFLAGS for it to apply to all files
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/ffixed-x18/compiler flags in the comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix in a follow-up.

"CC=aarch64-linux-gnu-gcc -ffixed-x18"
"CC=${CMAKE_C_COMPILER} -target aarch64-linux-gnu -ffixed-x18 -march=armv8.5-a+memtag --rtlib=compiler-rt -O3"
# this is needed for some test files built by the build system
"CXX=aarch64-linux-gnu-g++ -ffixed-x18"
"CXX=${CMAKE_CXX_COMPILER} -target aarch64-linux-gnu -ffixed-x18 -march=armv8.5-a+memtag --rtlib=compiler-rt -O3"
${CMAKE_SOURCE_DIR}/external/glibc/configure
"--build=aarch64-linux-gnu"
"--target=aarch64-linux-gnu"
Expand All @@ -74,14 +77,15 @@ if (LIBIA2_AARCH64)
# This is only for the heap tunable
"--enable-memory-tagging"
BUILD_COMMAND make -j8
# places the built libc.so and ld.so at the locations expected by QEMU's LD_LIBPRARY_PATH
INSTALL_COMMAND make install
USES_TERMINAL_CONFIGURE TRUE
USES_TERMINAL_BUILD TRUE
USES_TERMINAL_INSTALL TRUE
BUILD_BYPRODUCTS ${LIBC_PATH} ${LD_LINUX_PATH}
)
else()
set(SYSROOT_SRC_DIR ${CMAKE_SOURCE_DIR}/external/glibc_sysroot)
set(SYSROOT_BIN_DIR ${CMAKE_BINARY_DIR}/external/glibc/sysroot/usr)
file(GLOB SYSROOT_HEADERS_SRC
${SYSROOT_SRC_DIR}/include/*.h
${SYSROOT_SRC_DIR}/include/*/*.h
Expand All @@ -94,14 +98,13 @@ if (LIBIA2_AARCH64)
${SYSROOT_HEADERS}
COMMAND ${CMAKE_COMMAND} -E copy ${SYSROOT_SRC_DIR}/lib/libc.so.6 ${SYSROOT_BIN_DIR}/lib/
COMMAND ${CMAKE_COMMAND} -E copy ${SYSROOT_SRC_DIR}/lib/ld-linux-aarch64.so.1 ${SYSROOT_BIN_DIR}/lib/
COMMAND ${CMAKE_COMMAND} -E copy_directory ${SYSROOT_SRC_DIR}/include ${SYSROOT_BIN_DIR}/include
COMMAND ${CMAKE_COMMAND} -E copy_directory ${SYSROOT_SRC_DIR}/include ${SYSROOT_BIN_DIR}/usr/include
COMMENT "Copying glibc prebuilts to sysroot"
)
add_custom_target(glibc
DEPENDS ${SYSROOT_BIN_DIR}/lib/libc.so.6 ${SYSROOT_BIN_DIR}/lib/ld-linux-aarch64.so.1 ${SYSROOT_HEADERS}
)
endif()
set(LIBC_PATH ${SYSROOT_BIN_DIR}/lib/libc.so.6)
else()
# Find libc. We cannot simply do `find_library(LIBC c REQUIRED)` because
# `libc.so` itself is a linker script with glibc, while we need the path of
Expand Down Expand Up @@ -130,7 +133,29 @@ add_custom_command(
COMMENT "Padding TLS segment of libc"
)

add_custom_target(libc-ia2 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libc.so.6")
# Explicitly depend on ld.so because we must use one built with our toolchain
if(LIBIA2_AARCH64)
if(NOT DEFINED LD_LINUX_PATH)
message(FATAL_ERROR "Could not find ld-linux-aarch64.so.1 in: ${LIBC_PATHS}\n")
endif()

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ld-linux-aarch64.so.1
COMMAND ${CMAKE_COMMAND} -E copy ${LD_LINUX_PATH} ${CMAKE_CURRENT_BINARY_DIR}/ld-linux-aarch64.so.1
DEPENDS ${LD_LINUX_PATH}
COMMENT "Copying ld-linux-aarch64.so.1"
)
add_library(libc SHARED IMPORTED)
set_property(TARGET libc PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libc.so.6")
target_link_libraries(libia2 PUBLIC libc)
add_library(ld-linux SHARED IMPORTED)
set_property(TARGET ld-linux PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/ld-linux-aarch64.so.1")
target_link_libraries(libia2 PUBLIC ld-linux)
add_custom_target(libc-ia2 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libc.so.6" "${CMAKE_CURRENT_BINARY_DIR}/ld-linux-aarch64.so.1")
else()
add_custom_target(libc-ia2 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libc.so.6")
endif()


if (LIBIA2_AARCH64)
add_dependencies(libc-ia2 glibc)
Expand Down
11 changes: 9 additions & 2 deletions runtime/partition-alloc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ add_library(partition-alloc_unpadded SHARED

pad_tls_library(partition-alloc_unpadded partition-alloc)

target_link_libraries(partition-alloc_unpadded PRIVATE
if(LIBIA2_AARCH64)
target_link_libraries(partition-alloc_unpadded PUBLIC
c++
m # libstdc++ implicitly depends on libm, but libc++ doesn't
)
else()
target_link_libraries(partition-alloc_unpadded PRIVATE
stdc++
)
)
endif()

target_include_directories(partition-alloc BEFORE
INTERFACE
Expand Down