pw_toolchain: Wrap the C standard library abort function

The abort or std::abort function does not work as intended by default.
Typically, its implementation pulls in some undesired dependencies, such
as std::raise, and ends in a while (true) loop, rather than crashing.

This commit adds a wrapped implementation of abort that redirects to
PW_CRASH.

Change-Id: I62e06f2f875b524723887b5b3f8fda2fbcd72518
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/112310
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index acc8db0..d29514a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -99,6 +99,7 @@
 add_subdirectory(pw_thread_freertos EXCLUDE_FROM_ALL)
 add_subdirectory(pw_thread_stl EXCLUDE_FROM_ALL)
 add_subdirectory(pw_tokenizer EXCLUDE_FROM_ALL)
+add_subdirectory(pw_toolchain EXCLUDE_FROM_ALL)
 add_subdirectory(pw_trace EXCLUDE_FROM_ALL)
 add_subdirectory(pw_trace_tokenized EXCLUDE_FROM_ALL)
 add_subdirectory(pw_transfer EXCLUDE_FROM_ALL)
diff --git a/pw_toolchain/BUILD.bazel b/pw_toolchain/BUILD.bazel
index 7bb1810..6fb38a3 100644
--- a/pw_toolchain/BUILD.bazel
+++ b/pw_toolchain/BUILD.bazel
@@ -1,4 +1,4 @@
-# Copyright 2019 The Pigweed Authors
+# Copyright 2022 The Pigweed Authors
 #
 # 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
@@ -12,6 +12,18 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
+load(
+    "//pw_build:pigweed.bzl",
+    "pw_cc_library",
+)
+
 package(default_visibility = ["//visibility:public"])
 
 licenses(["notice"])
+
+pw_cc_library(
+    name = "wrap_abort",
+    srcs = ["wrap_abort.cc"],
+    linkopts = ["-Wl,--wrap=abort"],
+    deps = ["//pw_assert"],
+)
diff --git a/pw_toolchain/BUILD.gn b/pw_toolchain/BUILD.gn
index 10d4014..3245726 100644
--- a/pw_toolchain/BUILD.gn
+++ b/pw_toolchain/BUILD.gn
@@ -47,3 +47,14 @@
 
 pw_test_group("tests") {
 }
+
+config("wrap_abort_config") {
+  ldflags = [ "-Wl,--wrap=abort" ]
+  visibility = [ ":*" ]
+}
+
+pw_source_set("wrap_abort") {
+  all_dependent_configs = [ ":wrap_abort_config" ]
+  sources = [ "wrap_abort.cc" ]
+  deps = [ dir_pw_assert ]
+}
diff --git a/pw_toolchain/CMakeLists.txt b/pw_toolchain/CMakeLists.txt
new file mode 100644
index 0000000..d587634
--- /dev/null
+++ b/pw_toolchain/CMakeLists.txt
@@ -0,0 +1,24 @@
+# Copyright 2022 The Pigweed Authors
+#
+# 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
+#
+#     https://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("$ENV{PW_ROOT}/pw_build/pigweed.cmake")
+
+pw_add_library(pw_toolchain.wrap_abort STATIC
+  SOURCES
+    wrap_abort.cc
+  PUBLIC_DEPS
+    pw_assert
+  PUBLIC_LINK_OPTIONS
+    "-Wl,--wrap=abort"
+)
diff --git a/pw_toolchain/docs.rst b/pw_toolchain/docs.rst
index 91d0048..2b58ca7 100644
--- a/pw_toolchain/docs.rst
+++ b/pw_toolchain/docs.rst
@@ -134,3 +134,24 @@
   macro. For example, ``201703`` corresponds to C++17. See
   https://en.cppreference.com/w/cpp/preprocessor/replace#Predefined_macros for
   further details.
+
+----------------------------
+Standard library integration
+----------------------------
+``pw_toolchain`` provides features for integrating with the standard library.
+
+``std:abort`` wrapper
+=====================
+The `std::abort <https://en.cppreference.com/w/cpp/utility/program/abort>`_
+function is used to terminate a program abnormally. This function may be called
+by standard library functions, so is often linked into binaries, even if users
+never intentionally call it.
+
+For embedded builds, the ``abort`` implementation likely does not work as
+intended. For example, it may pull in undesired dependencies (e.g.
+``std::raise``) and end in an infinite loop.
+
+``pw_toolchain`` provides the ``pw_toolchain:wrap_abort`` library that replaces
+``abort`` in builds where the default behavior is undesirable. It uses the
+``-Wl,--wrap=abort`` linker option to redirect to ``abort`` calls to
+``PW_CRASH`` instead.
diff --git a/pw_toolchain/wrap_abort.cc b/pw_toolchain/wrap_abort.cc
new file mode 100644
index 0000000..b3d254c
--- /dev/null
+++ b/pw_toolchain/wrap_abort.cc
@@ -0,0 +1,26 @@
+// Copyright 2022 The Pigweed Authors
+//
+// 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
+//
+//     https://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.
+
+// This file provides an implementation of std::abort that can be used with ld's
+// --wrap option. This should be used for embedded tooclahisnt that don't have a
+// proper implementation of std::abort.
+
+#include "pw_assert/check.h"
+
+namespace pw::toolchain {
+
+// Redirect std::abort to PW_CRASH.
+extern "C" void __wrap_abort(void) { PW_CRASH("std::abort"); }
+
+}  // namespace pw::toolchain
diff --git a/targets/stm32f429i_disc1/target_toolchains.gni b/targets/stm32f429i_disc1/target_toolchains.gni
index 2c4f1e1..183f2a9 100644
--- a/targets/stm32f429i_disc1/target_toolchains.gni
+++ b/targets/stm32f429i_disc1/target_toolchains.gni
@@ -86,6 +86,7 @@
     "$dir_pw_assert:impl",
     "$dir_pw_cpu_exception:entry_impl",
     "$dir_pw_log:impl",
+    "$dir_pw_toolchain:wrap_abort",
   ]
 
   current_cpu = "arm"