Skip to content

Commit

Permalink
🐛 Fix undefined references in GCC
Browse files Browse the repository at this point in the history
GCC needs a call to throw to force it to link in the exception handling
code. For code size, this a great, but the linker then complains that
the -Wl,wrap= commands are undefined. Those undefined symbols result in
additional functions like _exit not being defined causing a few
linker error messages.

- ⬆️ 0.0.2
  • Loading branch information
kammce committed Mar 10, 2024
1 parent e5b2361 commit e91429f
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 165 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/0.0.2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: 🚀 Deploy 0.0.2

on:
workflow_dispatch:

jobs:
deploy:
uses: libhal/ci/.github/workflows/[email protected]
with:
version: 0.0.2
secrets: inherit
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.files import copy
from conan.tools.build import check_min_cppstd
from conan.errors import ConanInvalidConfiguration
Expand Down
59 changes: 0 additions & 59 deletions demos/CMakeLists.txt

This file was deleted.

8 changes: 0 additions & 8 deletions demos/baremetal.profile

This file was deleted.

36 changes: 0 additions & 36 deletions demos/conanfile.py

This file was deleted.

23 changes: 0 additions & 23 deletions demos/linker.ld

This file was deleted.

36 changes: 0 additions & 36 deletions demos/main.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions src/builtin/gcc/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ extern "C"
std::terminate();
}

struct _reent* _impure_ptr = nullptr; // NOLINT

void* __wrap___cxa_allocate_exception(unsigned int p_size) // NOLINT
{
// Size of the GCC exception object header is 128 bytes. Will have to update
Expand Down
22 changes: 22 additions & 0 deletions src/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@

namespace __cxxabiv1 { // NOLINT
std::terminate_handler __terminate_handler = +[]() { // NOLINT
// So you may be wondering what this code is doing here? Its actually a
// bit of weird circular logic. So the linker will garbage collect any
// functions that are not used in your code. If you somehow have an
// application without any exceptions thrown, the exception code will be
// eliminated. This would be great, but due the fact that our build system
// adds the `-Wl,--wrap=symbol` to the compiler to swap the function
// implementations, this results in the compiler yelling at the user that
// they are missing a wrapped function. In order to prevent the compiler
// from throwing away this function and then turning around demanding that
// we supply it, we simply need to call throw somewhere in the code. That
// will force it to link in the original implementations which will be
// swapped out with our wrapped implementations.
//
// Use a volatile bool that is always set to false to ensure that the
// "throw 5" is NEVER called.
//
// This location was choosen because it always links in for GCC.
volatile bool force_exceptions_to_link = false;
if (force_exceptions_to_link) {
throw 5;
}

while (true) {
continue;
}
Expand Down

0 comments on commit e91429f

Please sign in to comment.