Skip to content

Commit 3f736d0

Browse files
committed
Ship the target's libgcc runtime with the toolchain
cc-as-linker downstream (e.g. the OCaml unikernel toolchain) pulls in the compiler's runtime intrinsics with -lgcc. A GNU cross gcc, and clang with compiler-rt, ship that runtime on their own search path, so -lgcc resolves with nothing to do. Clang ships no compiler-rt builtins for the cross target on some hosts (e.g. macOS), where -print-libgcc-file-name then names a file that does not exist; there, fall back to a bare-metal GNU cross gcc's libgcc and install it as the toolchain's libgcc.a. When the compiler provides its own runtime, install nothing. The macOS CI installs lld (for ld.lld) and aarch64-elf-gcc (for libgcc).
1 parent 8d33cd0 commit 3f736d0

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

.github/workflows/macos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
- name: Checkout code
99
uses: actions/checkout@v4
1010
- name: Install dependencies
11-
run: brew install llvm
11+
run: brew install llvm lld aarch64-elf-gcc
1212
- name: Build
1313
run: |
1414
export PATH="$(brew --prefix llvm)/bin:$PATH"

GNUmakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ install-toolchain: build
144144
"{}" "$(TOOLCHAIN_INCDIR)/{}" \;
145145
$(INSTALL) -m 0644 bindings/solo5_stub.o $(TOOLCHAIN_LIBDIR)
146146
$(INSTALL) -m 0644 bindings/solo5_stub.lds $(TOOLCHAIN_LIBDIR)
147+
# Only when the target compiler ships no runtime of its own (see configure.sh);
148+
# otherwise -lgcc already resolves against the compiler's own libgcc.
149+
ifdef CONFIG_TARGET_LIBGCC
150+
$(INSTALL) -m 0644 $(CONFIG_TARGET_LIBGCC) $(TOOLCHAIN_LIBDIR)/libgcc.a
151+
endif
147152
ifdef CONFIG_HVT
148153
$(INSTALL) -m 0644 bindings/solo5_hvt.o $(TOOLCHAIN_LIBDIR)
149154
$(INSTALL) -m 0644 bindings/solo5_hvt.lds $(TOOLCHAIN_LIBDIR)

configure.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,31 @@ fi
540540
echo "${prog_NAME}: Using ${TARGET_LD} for target linker"
541541
echo "${prog_NAME}: Using ${TARGET_OBJCOPY} for target objcopy"
542542

543+
# "cc as linker" downstream (e.g. the OCaml unikernel toolchain) pulls in the
544+
# compiler's runtime intrinsics with -lgcc, so the toolchain has to ship that
545+
# archive. Ask the target compiler for it rather than shipping a prebuilt copy:
546+
# for Clang on an ELF target this is the GCC cross toolchain's libgcc Clang
547+
# defaults to, for GCC its own. Fail loudly if the compiler provides none.
548+
echo -n "${prog_NAME}: Checking for the target runtime (libgcc): "
549+
CC_LIBGCC="$(${TARGET_CC} -print-libgcc-file-name 2>/dev/null)"
550+
if [ -f "${CC_LIBGCC}" ]; then
551+
# The compiler ships its own runtime (GNU libgcc, or clang's compiler-rt
552+
# builtins) on its search path, so -lgcc already resolves; nothing to ship.
553+
# TARGET_LIBGCC stays empty and the toolchain installs no libgcc.a.
554+
echo "${CC_LIBGCC} (from the compiler)"
555+
TARGET_LIBGCC=
556+
elif TARGET_LIBGCC="$("${TARGET_ARCH}-elf-gcc" -print-libgcc-file-name 2>/dev/null)" &&
557+
[ -f "${TARGET_LIBGCC}" ]; then
558+
# Clang ships no compiler-rt builtins for this cross target on some hosts
559+
# (e.g. macOS); borrow a bare-metal GNU cross gcc's libgcc and bundle it.
560+
echo "${TARGET_LIBGCC} (bundled)"
561+
else
562+
echo "not found"
563+
die "${TARGET_CC} provides no target runtime library (libgcc)." \
564+
"Install one that ships libgcc/compiler-rt for ${TARGET_CC_MACHINE}" \
565+
"(e.g. \`brew install ${TARGET_ARCH}-elf-gcc\`), or set TARGET_CC."
566+
fi
567+
543568
TARGET_TRIPLE="${TARGET_ARCH}-solo5-none-static"
544569
echo "${prog_NAME}: Target toolchain triple is ${TARGET_TRIPLE}"
545570

@@ -586,6 +611,7 @@ CONFIG_TARGET_LD=${TARGET_LD}
586611
CONFIG_TARGET_LD_LDFLAGS=${TARGET_LD_LDFLAGS}
587612
CONFIG_TARGET_LD_MAX_PAGE_SIZE=${TARGET_LD_MAX_PAGE_SIZE}
588613
CONFIG_TARGET_OBJCOPY=${TARGET_OBJCOPY}
614+
CONFIG_TARGET_LIBGCC=${TARGET_LIBGCC}
589615
EOM
590616

591617
#

0 commit comments

Comments
 (0)