diff --git a/packagetest/cpp-baremetal-semihosting-exceptions.test b/packagetest/cpp-baremetal-semihosting-exceptions.test new file mode 100644 index 00000000..1bf017d5 --- /dev/null +++ b/packagetest/cpp-baremetal-semihosting-exceptions.test @@ -0,0 +1,5 @@ +# RUN: make -C %samples_dir/src/cpp-baremetal-semihosting-exceptions clean +# RUN: make -C %samples_dir/src/cpp-baremetal-semihosting-exceptions run BIN_PATH=%unpack_directory/bin 2>&1 | FileCheck %s +# RUN: make -C %samples_dir/src/cpp-baremetal-semihosting-exceptions clean +# CHECK: No exceptions. +# CHECK: Exception caught. diff --git a/samples/src/cpp-baremetal-semihosting-exceptions/Makefile b/samples/src/cpp-baremetal-semihosting-exceptions/Makefile new file mode 100644 index 00000000..349e888d --- /dev/null +++ b/samples/src/cpp-baremetal-semihosting-exceptions/Makefile @@ -0,0 +1,39 @@ +# +# Copyright (c) 2024, Arm Limited and affiliates. +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +include ../../Makefile.conf + +build: hello.hex hello-exn.hex + +hello.hex: hello.cpp + $(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) $(CPP_FLAGS) -print-multi-directory -g -T ../../ldscripts/microbit.ld -o hello.elf $^ | grep -v "_exn_" + $(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) $(CPP_FLAGS) -g -T ../../ldscripts/microbit.ld -o hello.elf $^ + $(BIN_PATH)/llvm-objcopy -O ihex hello.elf hello.hex + +hello-exn.hex: hello-exn.cpp + $(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) -print-multi-directory -g -T ../../ldscripts/microbit.ld -o hello.elf $^ | grep "_exn_" + $(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) -g -T ../../ldscripts/microbit.ld -o hello-exn.elf $^ + $(BIN_PATH)/llvm-objcopy -O ihex hello-exn.elf hello-exn.hex + +run: hello.hex hello-exn.hex + qemu-system-arm -M microbit -semihosting -nographic -device loader,file=hello.hex + qemu-system-arm -M microbit -semihosting -nographic -device loader,file=hello-exn.hex 2>&1 | grep "caught" + +clean: + rm -f *.elf *.hex + +.PHONY: clean run diff --git a/samples/src/cpp-baremetal-semihosting-exceptions/README.md b/samples/src/cpp-baremetal-semihosting-exceptions/README.md new file mode 100644 index 00000000..94d1338a --- /dev/null +++ b/samples/src/cpp-baremetal-semihosting-exceptions/README.md @@ -0,0 +1,4 @@ +# Bare-metal semihosting exceptions sample + +This sample shows and tests that C++ exceptions and corresponding +library variant selection work correctly. diff --git a/samples/src/cpp-baremetal-semihosting-exceptions/hello-exn.cpp b/samples/src/cpp-baremetal-semihosting-exceptions/hello-exn.cpp new file mode 100644 index 00000000..46299928 --- /dev/null +++ b/samples/src/cpp-baremetal-semihosting-exceptions/hello-exn.cpp @@ -0,0 +1,12 @@ +#include + +int main(void) { + try { + throw "error"; + } catch(...) { + std::cout << "Exception caught." << std::endl; + return 0; + } + std::cout << "Exception skipped." << std::endl; + return 1; +} diff --git a/samples/src/cpp-baremetal-semihosting-exceptions/hello.cpp b/samples/src/cpp-baremetal-semihosting-exceptions/hello.cpp new file mode 100644 index 00000000..a43520dd --- /dev/null +++ b/samples/src/cpp-baremetal-semihosting-exceptions/hello.cpp @@ -0,0 +1,6 @@ +#include + +int main(void) { + std::cout << "No exceptions." << std::endl; + return 0; +}